User Tools

Site Tools


software:python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
software:python [2020/12/26 19:56] superwizardsoftware:python [2020/12/27 05:23] (current) – [Plotly] superwizard
Line 260: Line 260:
 To retrieve all groups for a specific domain or the account, use the following GET request and include the authorization described in Authorize requests. For the query strings, request, and response properties, see the API Reference. For readability, this example uses line returns: To retrieve all groups for a specific domain or the account, use the following GET request and include the authorization described in Authorize requests. For the query strings, request, and response properties, see the API Reference. For readability, this example uses line returns:
  
 +<code>
 GET https://www.googleapis.com/admin/directory/v1/groups?domain=domain name GET https://www.googleapis.com/admin/directory/v1/groups?domain=domain name
 &customer=my_customer or customerId&pageToken=pagination token &customer=my_customer or customerId&pageToken=pagination token
 &maxResults=max results &maxResults=max results
 +</code>
 When retrieving: When retrieving:
-• All groups for a sub-domain — Use the domain argument with the domain's name. +   • All groups for a sub-domain — Use the domain argument with the domain's name. 
-• All groups for the account — Use the customer argument with either my_customer or the account's customerIdvalue. As an account administrator, use the string my_customer to represent your account's customerId. If you are a reseller accessing a resold customer's account, use the resold account's customerId. For the customerIdvalue use the account's primary domain name in the Retrieve all users in a domain operation's request. The resulting response has the customerId value. +   • All groups for the account — Use the customer argument with either my_customer or the account's customerIdvalue. As an account administrator, use the string my_customer to represent your account's customerId. If you are a reseller accessing a resold customer's account, use the resold account's customerId. For the customerIdvalue use the account's primary domain name in the Retrieve all users in a domain operation's request. The resulting response has the customerId value. 
-• Using both domain and customer arguments — The API returns all the groups for the domain. +   • Using both domain and customer arguments — The API returns all the groups for the domain. 
-• Not using the domain and customer arguments — The API returns all the groups for the account associated with my_customer. This is the account customerId of the administrator making the API request.+   • Not using the domain and customer arguments — The API returns all the groups for the account associated with my_customer. This is the account customerId of the administrator making the API request.
  
 From <https://developers.google.com/admin-sdk/directory/v1/guides/manage-groups>  From <https://developers.google.com/admin-sdk/directory/v1/guides/manage-groups> 
Line 308: Line 310:
  
  
 +</WRAP>
 +
 +====== Algorithms ======
 +
 +<WRAP center round box >
 2019-08-28 2019-08-28
  
Line 320: Line 327:
  
  
 +</WRAP>
 +
 +====== You can still miss attachments ======
 +
 +<WRAP center round box >
 2019-08-23 2019-08-23
  
Line 330: Line 342:
 From <https://stackoverflow.com/questions/37445865/gmail-api-where-to-find-body-of-email-depending-of-mimetype#37463491>  From <https://stackoverflow.com/questions/37445865/gmail-api-where-to-find-body-of-email-depending-of-mimetype#37463491> 
  
- +   • Now with this service you can read your emails and read any attachments you may have in your e-mails 
- +   • First you can query your e-mails with a search string to find the e-mail ids you need that have the attachments: 
-• Now with this service you can read your emails and read any attachments you may have in your e-mails +   search_query = "ABCD" 
-• First you can query your e-mails with a search string to find the e-mail ids you need that have the attachments: +   result = service.users().messages().list(userId='me', q=search_query).execute() 
-search_query = "ABCD" +   msgs = results['messages'
-result = service.users().messages().list(userId='me', q=search_query).execute() +   msg_ids = [msg['id'] for msg in msgs] 
-msgs = results['messages'+   • now for each messageId you can find the associated attachments in the email.
-msg_ids = [msg['id'] for msg in msgs] +
-• now for each messageId you can find the associated attachments in the email.+
  
 From <https://stackoverflow.com/questions/41749236/download-a-csv-file-from-gmail-using-python>  From <https://stackoverflow.com/questions/41749236/download-a-csv-file-from-gmail-using-python> 
  
  
-payload.headers[] list List of headers on this message part. For the top-level message part, representing the entire message payload, it will contain the standard RFC 2822 email headers such as To, From, and Subject.+   payload.headers[] list List of headers on this message part. For the top-level message part, representing the entire message payload, it will contain the standard RFC 2822 email headers such as To, From, and Subject.
  
 From <https://developers.google.com/gmail/api/v1/reference/users/messages>  From <https://developers.google.com/gmail/api/v1/reference/users/messages> 
Line 350: Line 360:
  
  
-headers=messageheader["payload"]["headers"+   headers=messageheader["payload"]["headers"
-    subject= [i['value'] for i in headers if i["name"]=="Subject"]+   subject= [i['value'] for i in headers if i["name"]=="Subject"]
  
 From <https://stackoverflow.com/questions/55144261/python-how-to-get-the-subject-of-an-email-from-gmail-api>  From <https://stackoverflow.com/questions/55144261/python-how-to-get-the-subject-of-an-email-from-gmail-api> 
Line 358: Line 368:
  
  
 +</WRAP>
 +
 +====== the Gmail API ======
 +
 +<WRAP center round box >
 the Gmail API the Gmail API
  
Line 369: Line 384:
  
 Example 3 Example 3
 +<code>
 import re re.sub('\W+','', string) import re re.sub('\W+','', string)
  • string1 - Result: 3.11899876595  • string1 - Result: 3.11899876595
  • string2 - Result: 2.78014397621  • string2 - Result: 2.78014397621
 +</code>
  
 From <https://stackoverflow.com/questions/5843518/remove-all-special-characters-punctuation-and-spaces-from-string>  From <https://stackoverflow.com/questions/5843518/remove-all-special-characters-punctuation-and-spaces-from-string> 
  
 +</WRAP>
 +
 +====== Access Dates ======
 +
 +<WRAP center round box >
 Access Dates Access Dates
  
 and then access the data using a loop: and then access the data using a loop:
 +<code>
 for msg in msgs['messages']: for msg in msgs['messages']:
     m_id = msg['id'] # get id of individual message     m_id = msg['id'] # get id of individual message
Line 388: Line 410:
            date = item['value']            date = item['value']
            ** DATA STORAGE FUNCTIONS ETC **            ** DATA STORAGE FUNCTIONS ETC **
 +
 +</code>
  
 From <https://stackoverflow.com/questions/46615395/gmail-api-quickly-access-the-dates-of-every-email-ever-sent-received>  From <https://stackoverflow.com/questions/46615395/gmail-api-quickly-access-the-dates-of-every-email-ever-sent-received> 
- 
  
  
Line 398: Line 421:
 From <http://strftime.org/>  From <http://strftime.org/> 
  
 +</WRAP>
  
 +====== Sort a Dictionary ======
  
- +<WRAP center round box >
 2019-08-22 2019-08-22
  
Line 410: Line 434:
  
  
 +</WRAP>
 +
 +====== Plotly Cufflinks ======
 +
 +<WRAP center round box >
 2019-08-15 2019-08-15
  
Line 420: Line 449:
  
 From <https://medium.com/@ozan/interactive-plots-with-plotly-and-cufflinks-on-pandas-dataframes-af6f86f62d94>  From <https://medium.com/@ozan/interactive-plots-with-plotly-and-cufflinks-on-pandas-dataframes-af6f86f62d94> 
- 
- 
  
 </WRAP> </WRAP>
  
-<WRAP center round box 60%>+====== Parsing text with Python ====== 
 + 
 +<WRAP center round box >
 2019-07-11 2019-07-11
  
Line 438: Line 467:
  
  
 +</WRAP>
 +
 +====== DataFrames ======
 +
 +<WRAP center round box >
 2019-08-14 2019-08-14
  
Line 447: Line 481:
  
  
 +</WRAP>
 +
 +====== Backblaze ======
 +
 +<WRAP center round box >
 2019-7-11 2019-7-11
  
Line 463: Line 502:
  
  
 +</WRAP>
 +
 +====== Plotly ======
 +
 +<WRAP center round box >
 2019-07-01 2019-07-01
  
 +<code>
 import plotly.plotly as py import plotly.plotly as py
 import plotly.graph_objs as go import plotly.graph_objs as go
Line 480: Line 525:
 fig = go.Figure(data=data, layout=layout) fig = go.Figure(data=data, layout=layout)
 py.iplot(fig, filename='axes-reversed') py.iplot(fig, filename='axes-reversed')
 +
 +
 +</code>
  
 From <https://plot.ly/python/axes/>  From <https://plot.ly/python/axes/> 
  
 +</WRAP>
  
 +====== Regular Expression ======
 +
 +
 +<WRAP center round box >
  
 2019-05-29 2019-05-29
  
-ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";+    ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
  
 From <https://stackoverflow.com/questions/10086572/ip-address-validation-in-python-using-regex>  From <https://stackoverflow.com/questions/10086572/ip-address-validation-in-python-using-regex> 
  
 +<code>
 \b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\. \b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.
   (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.   (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.
   (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.   (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.
   (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b   (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b
 +</code>
  
 From <https://www.regular-expressions.info/ip.html>  From <https://www.regular-expressions.info/ip.html> 
Line 506: Line 561:
 From <https://www.regular-expressions.info/regexmagic.html>  From <https://www.regular-expressions.info/regexmagic.html> 
  
 +</WRAP>
  
 +====== Split the string at the last occurrence of sep ======
  
- +<WRAP center round box >
 2019-05-23 2019-05-23
  
-str.rpartition(sep) +    str.rpartition(sep) 
- Split the string at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself.+ 
 +Split the string at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself.
  
 From <https://docs.python.org/3/library/stdtypes.html#string-methods>  From <https://docs.python.org/3/library/stdtypes.html#string-methods> 
 +</WRAP>
  
 +====== The built-in os module has a number of useful functions ======
 +
 +<WRAP center round box >
 The built-in os module has a number of useful functions that can be used to list directory contents and filter the results. To get a list of all the files and folders in a particular directory in the filesystem, use os.listdir() in legacy versions of Python or os.scandir() in Python 3.x. os.scandir() is the preferred method to use if you also want to get file and directory properties such as file size and modification date. The built-in os module has a number of useful functions that can be used to list directory contents and filter the results. To get a list of all the files and folders in a particular directory in the filesystem, use os.listdir() in legacy versions of Python or os.scandir() in Python 3.x. os.scandir() is the preferred method to use if you also want to get file and directory properties such as file size and modification date.
  
Line 523: Line 584:
  
  
 +</WRAP>
 +
 +====== Splitting, Concatenating, and Joining Strings in Python ======
 +
 +<WRAP center round box >
 2019-05-20 2019-05-20
  
Line 530: Line 596:
  
  
 +</WRAP>
 +
 +====== Regex Testor ======
 +
 +<WRAP center round box >
 Regex Testor Regex Testor
  
Line 535: Line 606:
 https://regex101.com/ https://regex101.com/
  
 +</WRAP>
 +
 +====== processdokuwikifile ======
 +
 +<WRAP center round box >
 2019-05-15 2019-05-15
  
 +<code>
 def processdokuwikifile(in_file,par_out_file): def processdokuwikifile(in_file,par_out_file):
     """lkjshflkjlsk"""     """lkjshflkjlsk"""
-    #with open('C:\\Users\\Administrator\\Desktop\\Gopala-Toi\\FTPListingOutput2018-10-25-01-31-alhp.txt','w') as outlog:+    #with open('C:\\Users\\An\\Desktop\\GoTo\\Listing2018-10-25-01-31-.txt','w') as outlog:
     with open(par_out_file,'w',encoding='utf-8') as out_file:     with open(par_out_file,'w',encoding='utf-8') as out_file:
         out = csv.writer(out_file)         out = csv.writer(out_file)
-        #with open('C:\\Users\\Administrator\\Desktop\\Gopala-Toi\\FTPListingSearch2018-10-25-01-31-alhp.txt','r') as ftplog+        #with open('C:\\Users\\An\\Desktop\\GoTo\\Search2018-10-25-01-31-.txt','r') as log
-        #with open('C:\\Users\\Administrator\\Desktop\\Gopala-Toi\\FTPListingSearch2018-10-25-01-31-innova.txt','r') as ftplog+        #with open('C:\\Users\\An\\Desktop\\GoTo\\Search2018-10-25-01-31-.txt','r') as log
-        with open(in_file,'r',encoding='utf-8') as indokuwikifile:+        with open(in_file,'r',encoding='utf-8') as infile: 
 +</code>
  
  
Line 565: Line 643:
  
 From <https://www.vipinajayakumar.com/parsing-text-with-python/>  From <https://www.vipinajayakumar.com/parsing-text-with-python/> 
 +</WRAP>
  
 +====== PASS BY OBJECT REFERENCE (Case in python): ======
  
 +<WRAP center round box >
  
 2019-04-08 2019-04-08
Line 572: Line 653:
 PASS BY OBJECT REFERENCE (Case in python): PASS BY OBJECT REFERENCE (Case in python):
 Here, "Object references are passed by value." Here, "Object references are passed by value."
 +<code>
 def append_one(li): def append_one(li):
     li.append(1)     li.append(1)
Line 577: Line 659:
 append_one(x) append_one(x)
 print x print x
 +</code>
 +
 Here, the statement x = [0] makes a variable x (box) that points towards the object [0] Here, the statement x = [0] makes a variable x (box) that points towards the object [0]
 On the function being called, a new box li is created. The contents of li is the SAME as the contents of box x. Both the boxes contain the same object. That is, both the variables point to the same object in memory. Hence, any change to the object pointed at by li will also be reflected by the object pointed at by x. On the function being called, a new box li is created. The contents of li is the SAME as the contents of box x. Both the boxes contain the same object. That is, both the variables point to the same object in memory. Hence, any change to the object pointed at by li will also be reflected by the object pointed at by x.
Line 584: Line 668:
 If the variable li is reassigned in the function, then li will point to a seperate object in memory. x however, will continue pointing to the same object in memory it was pointing to earlier. If the variable li is reassigned in the function, then li will point to a seperate object in memory. x however, will continue pointing to the same object in memory it was pointing to earlier.
 Example: Example:
 +<code>
 def append_one(li): def append_one(li):
     li = [0, 1]     li = [0, 1]
Line 589: Line 674:
 append_one(x) append_one(x)
 print x print x
 +</code>
 The output of the program will be: The output of the program will be:
 [0] [0]
  
 From <https://stackoverflow.com/questions/13299427/python-functions-call-by-reference>  From <https://stackoverflow.com/questions/13299427/python-functions-call-by-reference> 
 +</WRAP>
  
  
  
 +====== Plotly ======
 +
 +<WRAP center round box >
 2019-03-31 2019-03-31
  
 Plotly Plotly
  
 +<code>
 1962_2006_walmart_store_openings.csv Update 1962_2006_walmart_store_openings.csv 4 years ago  1962_2006_walmart_store_openings.csv Update 1962_2006_walmart_store_openings.csv 4 years ago
  2010_alcohol_consumption_by_country.csv Create 2010_alcohol_consumption_by_country.csv 3 years ago  2010_alcohol_consumption_by_country.csv Create 2010_alcohol_consumption_by_country.csv 3 years ago
  2011_february_aa_flight_paths.csv Create 2011_february_aa_flight_paths.csv 4 years ago  2011_february_aa_flight_paths.csv Create 2011_february_aa_flight_paths.csv 4 years ago
  2011_february_us_airport_traffic.csv Create 2011_february_us_airport_traffic.csv 4 years ago  2011_february_us_airport_traffic.csv Create 2011_february_us_airport_traffic.csv 4 years ago
 +</code>
  
 From <https://github.com/plotly/datasets>  From <https://github.com/plotly/datasets> 
  
  
 +</WRAP>
 +
 +====== Python write to CSV ======
 +
 +
 +<WRAP center round box >
  
 2019-03-29 2019-03-29
Line 613: Line 711:
 Python write to CSV Python write to CSV
  
 +<code>
 import csv import csv
 with open(..., 'wb') as myfile: with open(..., 'wb') as myfile:
     wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)     wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
     wr.writerow(mylist)     wr.writerow(mylist)
 +</code>
  
 From <https://stackoverflow.com/questions/2084069/create-a-csv-file-with-values-from-a-python-list>  From <https://stackoverflow.com/questions/2084069/create-a-csv-file-with-values-from-a-python-list> 
  
 +<code>
     with open(iniFile.absolute(), 'w', newline='') as iniSettings:     with open(iniFile.absolute(), 'w', newline='') as iniSettings:
         #spamwriter = csv.writer(iniSettings, delimiter=',',         #spamwriter = csv.writer(iniSettings, delimiter=',',
Line 630: Line 731:
         for val in folder_list:         for val in folder_list:
             spamwriter.writerow(val)             spamwriter.writerow(val)
 +</code>
  
 you can also use wr.writerows(list) – tovmeod Dec 25 '11 at 22:29 you can also use wr.writerows(list) – tovmeod Dec 25 '11 at 22:29
Line 636: Line 738:
  
 From <https://stackoverflow.com/questions/2084069/create-a-csv-file-with-values-from-a-python-list>  From <https://stackoverflow.com/questions/2084069/create-a-csv-file-with-values-from-a-python-list> 
 +</WRAP>
  
 +====== CSV in Python adding an extra carriage return, on Windows ======
 +
 +<WRAP center round box >
  
 CSV in Python adding an extra carriage return, on Windows CSV in Python adding an extra carriage return, on Windows
  
 One of the possible fixes in Python3, as described in @YiboYang's answer, is opening the file with the newline parameter set to be an empty string: One of the possible fixes in Python3, as described in @YiboYang's answer, is opening the file with the newline parameter set to be an empty string:
 +<code>
 f = open(path_to_file, 'w', newline='') f = open(path_to_file, 'w', newline='')
 writer = csv.writer(f) writer = csv.writer(f)
 +</code>
  
 From <https://stackoverflow.com/questions/3191528/csv-in-python-adding-an-extra-carriage-return-on-windows>  From <https://stackoverflow.com/questions/3191528/csv-in-python-adding-an-extra-carriage-return-on-windows> 
  
 +</WRAP>
 +
 +====== Examples of simple type checking in Python: ======
 +
 +
 +<WRAP center round box >
 2019-02-17 2019-02-17
  
  
 Examples of simple type checking in Python: Examples of simple type checking in Python:
 +<code>
 assert type(variable_name) == int assert type(variable_name) == int
 assert type(variable_name) == bool assert type(variable_name) == bool
 assert type(variable_name) == list assert type(variable_name) == list
 +</code>
  
 From <https://stackoverflow.com/questions/402504/how-to-determine-a-python-variables-type>  From <https://stackoverflow.com/questions/402504/how-to-determine-a-python-variables-type> 
  
 Use type Use type
 +<code>
 >>> type(one) >>> type(one)
 <type 'int'> <type 'int'>
Line 662: Line 779:
 >>> type(one).__name__ >>> type(one).__name__
 'int' 'int'
 +</code>
  
 From <https://stackoverflow.com/questions/402504/how-to-determine-a-python-variables-type>  From <https://stackoverflow.com/questions/402504/how-to-determine-a-python-variables-type> 
  
 +</WRAP>
 +
 +====== isinstance() ======
 +
 +<WRAP center round box >
 With one argument, return the type of an object. The return value is a type object. The isinstance() built-in function is recommended for testing With one argument, return the type of an object. The return value is a type object. The isinstance() built-in function is recommended for testing
  
Line 670: Line 793:
  
 • Syntax: • Syntax:
 +<code>
 isinstance(object, classinfo)  isinstance(object, classinfo) 
 +</code>
  
 The isinstance() takes two parameters: The isinstance() takes two parameters:
 +<code>
 object : object to be checked object : object to be checked
 classinfo : class, type, or tuple of classes and types classinfo : class, type, or tuple of classes and types
 +</code>
  
 From <https://www.geeksforgeeks.org/type-isinstance-python/>  From <https://www.geeksforgeeks.org/type-isinstance-python/> 
  
 +</WRAP>
  
 +====== graph-cli ======
  
- +<WRAP center round box >
-</WRAP> +
- +
-<WRAP center round box 60%>+
 2019-01-05 2019-01-05
  
Line 694: Line 820:
  
  
 +</WRAP>
 +
 +====== copy2 ======
 +
 +<WRAP center round box >
  
 2018-12-25 2018-12-25
Line 700: Line 831:
 As with the previous methods, copy2 method is identical to the copy method, but in addition to copying the file contents it also attempts to preserve all the source file's metadata. If the platform doesn't allow for full metadata saving, then copy2 doesn't return failure and it will just preserve any metadata it can. As with the previous methods, copy2 method is identical to the copy method, but in addition to copying the file contents it also attempts to preserve all the source file's metadata. If the platform doesn't allow for full metadata saving, then copy2 doesn't return failure and it will just preserve any metadata it can.
 The syntax is as follows: The syntax is as follows:
 +<code>
 shutil.copy2(src_file, dest_file, *, follow_symlinks=True)   shutil.copy2(src_file, dest_file, *, follow_symlinks=True)  
 +</code>
  
 From <https://stackabuse.com/how-to-copy-a-file-in-python/>  From <https://stackabuse.com/how-to-copy-a-file-in-python/> 
  
 +</WRAP>
 +
 +====== Start of String Only: \A ======
 +
 +<WRAP center round box >
  
 Start of String Only: \A Start of String Only: \A
Line 709: Line 847:
  
 From <https://docs.microsoft.com/en-us/dotnet/standard/base-types/anchors-in-regular-expressions>  From <https://docs.microsoft.com/en-us/dotnet/standard/base-types/anchors-in-regular-expressions> 
 +
 +</WRAP>
 +
 +====== Decimals interact well with much of the rest of Python ======
 +
 +<WRAP center round box >
  
 decimal — Decimal fixed point and floating point arithmetic decimal — Decimal fixed point and floating point arithmetic
Line 715: Line 859:
  
 Unlike hardware based binary floating point, the decimal module has a user alterable precision (defaulting to 28 places) which can be as large as needed for a given problem: Unlike hardware based binary floating point, the decimal module has a user alterable precision (defaulting to 28 places) which can be as large as needed for a given problem:
 +<code>
 >>> from decimal import * >>> from decimal import *
 >>> getcontext().prec = 6 >>> getcontext().prec = 6
Line 722: Line 867:
 >>> Decimal(1) / Decimal(7) >>> Decimal(1) / Decimal(7)
 Decimal('0.1428571428571428571428571429') Decimal('0.1428571428571428571428571429')
 +</code>
  
 From <https://docs.python.org/3/library/decimal.html>  From <https://docs.python.org/3/library/decimal.html> 
  
 Decimals interact well with much of the rest of Python. Here is a small decimal floating point flying circus: Decimals interact well with much of the rest of Python. Here is a small decimal floating point flying circus:
 +<code>
 >>> data = list(map(Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split())) >>> data = list(map(Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split()))
 >>> max(data) >>> max(data)
Line 734: Line 881:
 [Decimal('0.03'), Decimal('1.00'), Decimal('1.34'), Decimal('1.87'), [Decimal('0.03'), Decimal('1.00'), Decimal('1.34'), Decimal('1.87'),
  Decimal('2.35'), Decimal('3.45'), Decimal('9.25')]  Decimal('2.35'), Decimal('3.45'), Decimal('9.25')]
 +</code>
  
 From <https://docs.python.org/3/library/decimal.html>  From <https://docs.python.org/3/library/decimal.html> 
 +
 +</WRAP>
 +
 +====== splitting a number into the integer and decimal parts ======
 +
 +<WRAP center round box >
 +
  
 splitting a number into the integer and decimal parts splitting a number into the integer and decimal parts
  
  
 +<code>
 >>> a = 147.234 >>> a = 147.234
 >>> a % 1 >>> a % 1
Line 746: Line 902:
 147.0 147.0
 >>> >>>
 +</code>
 If you want the integer part as an integer and not a float, use int(a//1) instead. To obtain the tuple in a single passage: (int(a//1), a%1) If you want the integer part as an integer and not a float, use int(a//1) instead. To obtain the tuple in a single passage: (int(a//1), a%1)
 EDIT: Remember that the decimal part of a float number is approximate, so if you want to represent it as a human would do, you need to use the decimal library EDIT: Remember that the decimal part of a float number is approximate, so if you want to represent it as a human would do, you need to use the decimal library
Line 751: Line 908:
 From <https://stackoverflow.com/questions/6681743/splitting-a-number-into-the-integer-and-decimal-parts>  From <https://stackoverflow.com/questions/6681743/splitting-a-number-into-the-integer-and-decimal-parts> 
  
 +<code>
 import math import math
 x = 1234.5678 x = 1234.5678
 math.modf(x) # (0.5678000000000338, 1234.0) math.modf(x) # (0.5678000000000338, 1234.0)
 +</code>
  
 From <https://stackoverflow.com/questions/6681743/splitting-a-number-into-the-integer-and-decimal-parts>  From <https://stackoverflow.com/questions/6681743/splitting-a-number-into-the-integer-and-decimal-parts> 
Line 759: Line 918:
  
 Create a date object: Create a date object:
 +<code>
 import datetime import datetime
  
 x = datetime.datetime(2020, 5, 17) x = datetime.datetime(2020, 5, 17)
 +</code>
  
 From <https://www.w3schools.com/python/python_datetime.asp>  From <https://www.w3schools.com/python/python_datetime.asp> 
  
 +</WRAP>
 +
 +====== Module datetime provides ======
 +
 +<WRAP center round box >
  
 Module datetime provides classes for manipulating date and time in more object oriented way. One of them is datetime.datetime.now which return number of seconds since the epoch. Module datetime provides classes for manipulating date and time in more object oriented way. One of them is datetime.datetime.now which return number of seconds since the epoch.
  
 +<code>
 import datetime; import datetime;
 ts = datetime.datetime.now().timestamp() ts = datetime.datetime.now().timestamp()
 print(ts) print(ts)
 # 1545665588.52 # 1545665588.52
 +</code>
 +
  
 From <http://timestamp.online/article/how-to-get-current-timestamp-in-python>  From <http://timestamp.online/article/how-to-get-current-timestamp-in-python> 
  
 +<code>
 x = int(datetime.datetime(2070, 12, 13, 1, 48, 35).timestamp() - datetime.datetime.now().timestamp()//1) x = int(datetime.datetime(2070, 12, 13, 1, 48, 35).timestamp() - datetime.datetime.now().timestamp()//1)
  
 print(x) print(x)
 +</code>
  
  
 +</WRAP>
 +
 +====== Example 2: Right justify string and fill the remaining spaces ======
 +
 +<WRAP center round box >
 Example 2: Right justify string and fill the remaining spaces Example 2: Right justify string and fill the remaining spaces
  
 +<code>
 # example string # example string
 string = 'cat' string = 'cat'
Line 788: Line 965:
 # print right justified string # print right justified string
 print(string.rjust(width, fillchar)) print(string.rjust(width, fillchar))
 +</code>
  
 From <https://www.programiz.com/python-programming/methods/string/rjust>  From <https://www.programiz.com/python-programming/methods/string/rjust> 
  
 +</WRAP>
  
 +====== Practical Business Python ======
  
 +<WRAP center round box >
 2018-12-24 2018-12-24
  
 Practical Business Python Practical Business Python
- 
  
 pbpython/extras/Pathlib-Cheatsheet.pdf pbpython/extras/Pathlib-Cheatsheet.pdf
Line 804: Line 983:
  
  
 +</WRAP>
  
 +====== The divmod() returns ======
  
 +<WRAP center round box >
  
  
Line 815: Line 997:
 From <https://www.programiz.com/python-programming/methods/built-in/divmod>  From <https://www.programiz.com/python-programming/methods/built-in/divmod> 
  
 +</WRAP>
 +
 +====== numpy ======
 +
 +<WRAP center round box >
 2018-11-18 2018-11-18
  
 +<code>
 pip install numpy pip install numpy
 +</code>
  
 From <https://pypi.org/project/numpy/>  From <https://pypi.org/project/numpy/> 
  
 +<code>
 pip3.6 install numpy pip3.6 install numpy
 pip3.6 install scipy pip3.6 install scipy
Line 828: Line 1018:
 Install opencv-python instead of cv2. Install opencv-python instead of cv2.
 pip install opencv-python pip install opencv-python
 +</code>
  
 From <https://github.com/jazzsaxmafia/video_to_sequence/issues/3>  From <https://github.com/jazzsaxmafia/video_to_sequence/issues/3> 
  
 +</WRAP>
 +
 +====== compare the use of lambda ======
 +
 +<WRAP center round box >
  
  
Line 838: Line 1034:
  
 From <https://stackoverflow.com/questions/8966538/syntax-behind-sortedkey-lambda>  From <https://stackoverflow.com/questions/8966538/syntax-behind-sortedkey-lambda> 
 +</WRAP>
 +
 +====== Key Functions ======
 +
 +<WRAP center round box >
  
 Key Functions Key Functions
Line 853: Line 1054:
 This image was created with the following code. This image was created with the following code.
  
 +<code>
 1 import operator                                                   1 import operator                                                  
 2 import pylab 2 import pylab
Line 880: Line 1082:
 26 pylab.grid() 26 pylab.grid()
 27 pylab.title("Performance sorted dictionary by values") 27 pylab.title("Performance sorted dictionary by values")
 +</code>
  
 From <http://thomas-cokelaer.info/blog/2017/12/how-to-sort-a-dictionary-by-values-in-python/>  From <http://thomas-cokelaer.info/blog/2017/12/how-to-sort-a-dictionary-by-values-in-python/> 
Line 885: Line 1088:
 As already said, iteritems() will be a problem, but you mention a syntax error, which comes from the lambda declaration with parenthesis: As already said, iteritems() will be a problem, but you mention a syntax error, which comes from the lambda declaration with parenthesis:
 Change: Change:
 +<code>
 key=lambda(k, v): sort_order.index(k) key=lambda(k, v): sort_order.index(k)
 +</code>
 To: To:
 +<code>
 key=lambda k, v: sort_order.index(k) key=lambda k, v: sort_order.index(k)
 +</code>
  
 From <https://stackoverflow.com/questions/47749438/lambda-sorting-in-python-3>  From <https://stackoverflow.com/questions/47749438/lambda-sorting-in-python-3> 
Line 894: Line 1101:
 </WRAP> </WRAP>
  
-<WRAP center round box 60%>+====== What problem does pandas solve? ====== 
 + 
 +<WRAP center round box >
  
 2018-11-15 2018-11-15
Line 904: Line 1113:
  
 NumPy NumPy
 +<code>
 NumPy is the fundamental package for scientific computing with Python. It contains among other things: NumPy is the fundamental package for scientific computing with Python. It contains among other things:
 • a powerful N-dimensional array object • a powerful N-dimensional array object
Line 909: Line 1119:
 • tools for integrating C/C++ and Fortran code • tools for integrating C/C++ and Fortran code
 • useful linear algebra, Fourier transform, and random number capabilities • useful linear algebra, Fourier transform, and random number capabilities
 +</code>
  
 From <http://www.numpy.org/>  From <http://www.numpy.org/> 
Line 915: Line 1126:
  
 From <https://machinelearningmastery.com/broadcasting-with-numpy-arrays/>  From <https://machinelearningmastery.com/broadcasting-with-numpy-arrays/> 
 +
 +</WRAP>
 +
 +====== scikit-learn ======
 +
 +<WRAP center round box >
  
 scikit-learn scikit-learn
Line 930: Line 1147:
 From <http://pybrain.org/>  From <http://pybrain.org/> 
  
 +</WRAP>
  
 +====== python read fails on special characters ======
  
 +<WRAP center round box >
  
 2018-11-06 2018-11-06
Line 945: Line 1165:
  
 From <https://stackoverflow.com/questions/47635759/how-to-read-a-text-file-with-special-characters-in-python>  From <https://stackoverflow.com/questions/47635759/how-to-read-a-text-file-with-special-characters-in-python> 
 +</WRAP>
  
 +====== idle args ======
  
 +
 +<WRAP center round box >
  
 2018-10-31 2018-10-31
Line 960: Line 1184:
  
 Pending application of a patch, the following will work to only add args to sys.argv when running from an Idle editor. Pending application of a patch, the following will work to only add args to sys.argv when running from an Idle editor.
 +<code>
 import sys import sys
 # ... # ...
Line 974: Line 1199:
 except: except:
     sys.argv = [sys.argv[0], 'argument1', 'argument2', 'argument2']     sys.argv = [sys.argv[0], 'argument1', 'argument2', 'argument2']
 +</code>
  
 From <https://stackoverflow.com/questions/2148994/when-running-a-python-script-in-idle-is-there-a-way-to-pass-in-command-line-arg>  From <https://stackoverflow.com/questions/2148994/when-running-a-python-script-in-idle-is-there-a-way-to-pass-in-command-line-arg> 
  
 +</WRAP>
  
 +====== Auto detect IDLE and prompt for command-line argument values ======
 +
 +
 +<WRAP center round box >
  
 2018-10-31 2018-10-31
Line 983: Line 1214:
 Auto detect IDLE and prompt for command-line argument values Auto detect IDLE and prompt for command-line argument values
  
-#! /usr/bin/env python3+<code> 
 +c#! /usr/bin/env python3
 import sys import sys
 def ok(x=None): def ok(x=None):
Line 999: Line 1231:
       root.bind("<Escape>", lambda x: root.destroy())       root.bind("<Escape>", lambda x: root.destroy())
 e.focus() e.focus()
 +</code>
  
 From <https://stackoverflow.com/questions/2148994/when-running-a-python-script-in-idle-is-there-a-way-to-pass-in-command-line-arg/44687632#44687632>  From <https://stackoverflow.com/questions/2148994/when-running-a-python-script-in-idle-is-there-a-way-to-pass-in-command-line-arg/44687632#44687632> 
  
 +</WRAP>
 +
 +====== print the files deleted ======
 +
 +<WRAP center round box >
  
  
Line 1007: Line 1245:
 Python Script Python Script
 Here's a Python script that will also print the files deleted Here's a Python script that will also print the files deleted
 +<code>
 import os import os
 for line in open("./data/deleted.files"): for line in open("./data/deleted.files"):
Line 1018: Line 1257:
     except OSError:     except OSError:
         pass         pass
 +</code>
 +
 +</WRAP>
 +
 +====== delete directories ======
 +
 +<WRAP center round box >
 Here's an alternative Python script that is case sensitive and will also delete directories included in the list Here's an alternative Python script that is case sensitive and will also delete directories included in the list
 +<code>
 import os import os
 import shutil import shutil
Line 1043: Line 1290:
                 #print('File not found => ' + path)                 #print('File not found => ' + path)
                 pass                 pass
 +</code>
  
 From <https://www.dokuwiki.org/install:unused_files>  From <https://www.dokuwiki.org/install:unused_files> 
  
  
 +</WRAP>
 +
 +====== checkpoints ======
 +
 +<WRAP center round box >
  
 2018-10-06 2018-10-06
 GLOB GLOB
  
 +<code>
 def delete_previous_checkpoints(self, num_previous=5): def delete_previous_checkpoints(self, num_previous=5):
         """         """
Line 1067: Line 1321:
                 ckpt_file_nm = self.get_checkpoint_location() + '/model_' + str(ckpt_id) + '.ckpt'                 ckpt_file_nm = self.get_checkpoint_location() + '/model_' + str(ckpt_id) + '.ckpt'
                 os.remove(ckpt_file_nm)                  os.remove(ckpt_file_nm) 
 +</code>
  
 From <https://www.programcreek.com/python/example/92/glob.glob>  From <https://www.programcreek.com/python/example/92/glob.glob> 
 +</WRAP>
  
 +====== argparse ======
  
 +<WRAP center round box >
  
 2018-10-06 2018-10-06
Line 1078: Line 1336:
 From <https://stackoverflow.com/questions/35365344/python-sys-argv-and-argparse>  From <https://stackoverflow.com/questions/35365344/python-sys-argv-and-argparse> 
  
 +</WRAP>
 +
 +====== Argparse Tutorial ======
 +
 +
 +<WRAP center round box >
  
 Argparse Tutorial Argparse Tutorial
Line 1085: Line 1349:
 From <https://docs.python.org/3.7/howto/argparse.html>  From <https://docs.python.org/3.7/howto/argparse.html> 
  
 +<code>
 *args and **kwargs in Python *args and **kwargs in Python
 *args *args
 +</code>
 The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-keyworded, variable-length argument list. The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-keyworded, variable-length argument list.
  
Line 1092: Line 1358:
  
  
 +</WRAP>
 +
 +====== recursive ======
 +
 +<WRAP center round box >
  
 2018-10-03 2018-10-03
Line 1101: Line 1372:
 From <https://stackoverflow.com/questions/14798220/how-can-i-search-sub-folders-using-glob-glob-module-in-python>  From <https://stackoverflow.com/questions/14798220/how-can-i-search-sub-folders-using-glob-glob-module-in-python> 
  
 +I have successfully used 
 +<code>
 +for i in d.rglob('**/*'):
 +</code>
 +
 +<code>
 for i in d.iglob('**/*'): for i in d.iglob('**/*'):
 +</code>
  
 The “**” pattern means “this directory and all subdirectories, recursively”. In other words, it enables recursive globbing: The “**” pattern means “this directory and all subdirectories, recursively”. In other words, it enables recursive globbing:
Line 1107: Line 1385:
 From <https://docs.python.org/3/library/pathlib.html>  From <https://docs.python.org/3/library/pathlib.html> 
  
 +<code>
 errno.ENOTEMPTY errno.ENOTEMPTY
  Directory not empty  Directory not empty
 +</code>
  
 From <https://docs.python.org/2/library/errno.html>  From <https://docs.python.org/2/library/errno.html> 
  
 +<code>
 errno.EACCES¶ errno.EACCES¶
  Permission denied  Permission denied
 +</code>
  
 From <https://docs.python.org/2/library/errno.html>  From <https://docs.python.org/2/library/errno.html> 
  
 +<code>
 except OSError as e:  except OSError as e:
  if e.errno not in _IGNORED_ERROS:  if e.errno not in _IGNORED_ERROS:
  raise  raise
  return False  return False
 +</code>
  
 From <https://github.com/python/cpython/blob/3.7/Lib/pathlib.py>  From <https://github.com/python/cpython/blob/3.7/Lib/pathlib.py> 
  
 +<code>
 except OSError as e:  except OSError as e:
  if e.errno != EINVAL and strict:  if e.errno != EINVAL and strict:
  raise  raise
 +</code>
  
 From <https://github.com/python/cpython/blob/3.7/Lib/pathlib.py>  From <https://github.com/python/cpython/blob/3.7/Lib/pathlib.py> 
  
 +</WRAP>
 +
 +====== walktree ======
 +
 +<WRAP center round box >
 2018-10-01 2018-10-01
  
Line 1157: Line 1448:
  
  
 +</WRAP>
 +
 +====== Dropbox in python ======
 +
 +
 +
 +<WRAP center round box >
  
 2018-09-29 2018-09-29
 Dropbox in python Dropbox in python
  
 +<code>
 from pathlib import Path from pathlib import Path
 import arrow import arrow
Line 1173: Line 1471:
             #remove it             #remove it
             pass             pass
 +</code>
 From <https://stackoverflow.com/questions/12485666/python-deleting-all-files-in-a-folder-older-than-x-days>  From <https://stackoverflow.com/questions/12485666/python-deleting-all-files-in-a-folder-older-than-x-days> 
  
Line 1181: Line 1479:
 From <https://stackoverflow.com/questions/4289937/how-to-repeat-last-command-in-python-interpreter-shell>  From <https://stackoverflow.com/questions/4289937/how-to-repeat-last-command-in-python-interpreter-shell> 
  
 +</WRAP>
 +
 +====== CSV Toolkit Overview ======
 +
 +
 +<WRAP center round box >
 2018-09-04 2018-09-04
  
Line 1189: Line 1493:
 From <https://pypi.org/project/csv.toolkit/>  From <https://pypi.org/project/csv.toolkit/> 
  
 +</WRAP>
 +
 +====== What is Bonobo? ======
 +
 +<WRAP center round box >
 What is Bonobo? What is Bonobo?
 Bonobo is a lightweight Extract-Transform-Load (ETL) framework for Python 3.5+. Bonobo is a lightweight Extract-Transform-Load (ETL) framework for Python 3.5+.
Line 1200: Line 1509:
 From <https://csvkit.readthedocs.io/en/1.0.3/>  From <https://csvkit.readthedocs.io/en/1.0.3/> 
  
 +</WRAP>
 +
 +====== Awesome Python ======
 +
 +<WRAP center round box >
 Awesome Python  Awesome Python 
 A curated list of awesome Python frameworks, libraries, software and resources. A curated list of awesome Python frameworks, libraries, software and resources.
Line 1215: Line 1529:
 From <https://github.com/vinta/awesome-python>  From <https://github.com/vinta/awesome-python> 
  
 +</WRAP>
  
 +====== Python data visualization: Comparing 7 tools ======
 +
 +<WRAP center round box >
  
 2018-09-04 2018-09-04
Line 1224: Line 1542:
 From <https://www.dataquest.io/blog/python-data-visualization-libraries/>  From <https://www.dataquest.io/blog/python-data-visualization-libraries/> 
  
 +</WRAP>
  
  
 +====== Best way to sort txt file using csv tools in python ======
 +
 +
 +<WRAP center round box >
 2018-09-04 2018-09-04
  
Line 1232: Line 1555:
 From <https://stackoverflow.com/questions/45221637/best-way-to-sort-txt-file-using-csv-tools-in-python>  From <https://stackoverflow.com/questions/45221637/best-way-to-sort-txt-file-using-csv-tools-in-python> 
  
 +<code>
 import csv import csv
 import operator import operator
Line 1248: Line 1572:
   sortedlist=sorted(f,key=operator.itemgetter(0),reverse=True)   sortedlist=sorted(f,key=operator.itemgetter(0),reverse=True)
   print(sortedlist)   print(sortedlist)
 +</code>
  
 From <https://stackoverflow.com/questions/45221637/best-way-to-sort-txt-file-using-csv-tools-in-python>  From <https://stackoverflow.com/questions/45221637/best-way-to-sort-txt-file-using-csv-tools-in-python> 
Line 1255: Line 1580:
 2018-08-10 2018-08-10
 The sys.path list contains the list of directories which will be searched for modules at runtime: The sys.path list contains the list of directories which will be searched for modules at runtime:
 +<code>
 python -v python -v
 >>> import sys >>> import sys
 >>> sys.path >>> sys.path
 ['', '/usr/local/lib/python25.zip', '/usr/local/lib/python2.5', ... ] ['', '/usr/local/lib/python25.zip', '/usr/local/lib/python2.5', ... ]
 +
 +</code>
  
 From <https://stackoverflow.com/questions/269795/how-do-i-find-the-location-of-python-module-sources>  From <https://stackoverflow.com/questions/269795/how-do-i-find-the-location-of-python-module-sources> 
  
 For speedtest - /usr/local/lib For speedtest - /usr/local/lib
- 
  
  
 </WRAP> </WRAP>
  
software/python.1609012597.txt.gz · Last modified: 2020/12/26 19:56 by superwizard