software:python
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
software:python [2020/12/26 20:29] – [Plotly] superwizard | software:python [2020/12/27 05:23] (current) – [Plotly] superwizard | ||
---|---|---|---|
Line 531: | Line 531: | ||
From < | From < | ||
+ | </ | ||
+ | ====== Regular Expression ====== | ||
+ | |||
+ | |||
+ | <WRAP center round box > | ||
2019-05-29 | 2019-05-29 | ||
Line 555: | Line 560: | ||
From < | From < | ||
- | |||
- | |||
- | |||
- | |||
</ | </ | ||
- | ====== | + | ====== |
<WRAP center round box > | <WRAP center round box > | ||
Line 572: | Line 573: | ||
From < | From < | ||
+ | </ | ||
+ | ====== 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 579: | Line 584: | ||
+ | </ | ||
+ | |||
+ | ====== Splitting, Concatenating, | ||
+ | |||
+ | <WRAP center round box > | ||
2019-05-20 | 2019-05-20 | ||
Line 586: | Line 596: | ||
+ | </ | ||
+ | |||
+ | ====== Regex Testor ====== | ||
+ | |||
+ | <WRAP center round box > | ||
Regex Testor | Regex Testor | ||
Line 591: | Line 606: | ||
https:// | https:// | ||
+ | </ | ||
+ | |||
+ | ====== processdokuwikifile ====== | ||
+ | |||
+ | <WRAP center round box > | ||
2019-05-15 | 2019-05-15 | ||
Line 623: | Line 643: | ||
From < | From < | ||
+ | </ | ||
+ | ====== PASS BY OBJECT REFERENCE (Case in python): ====== | ||
+ | <WRAP center round box > | ||
2019-04-08 | 2019-04-08 | ||
Line 656: | Line 679: | ||
From < | From < | ||
+ | </ | ||
+ | ====== Plotly ====== | ||
+ | |||
+ | <WRAP center round box > | ||
2019-03-31 | 2019-03-31 | ||
Line 673: | Line 700: | ||
+ | </ | ||
+ | |||
+ | ====== Python write to CSV ====== | ||
+ | |||
+ | |||
+ | <WRAP center round box > | ||
2019-03-29 | 2019-03-29 | ||
Line 705: | Line 738: | ||
From < | From < | ||
+ | </ | ||
+ | ====== 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 | ||
Line 717: | Line 754: | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== Examples of simple type checking in Python: ====== | ||
+ | |||
+ | |||
+ | <WRAP center round box > | ||
2019-02-17 | 2019-02-17 | ||
Line 740: | Line 783: | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== 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 756: | Line 804: | ||
From < | From < | ||
- | |||
- | |||
- | |||
</ | </ | ||
- | <WRAP center round box 60%> | + | ====== graph-cli ====== |
+ | |||
+ | <WRAP center round box > | ||
2019-01-05 | 2019-01-05 | ||
Line 773: | Line 820: | ||
+ | </ | ||
+ | |||
+ | ====== copy2 ====== | ||
+ | |||
+ | <WRAP center round box > | ||
2018-12-25 | 2018-12-25 | ||
Line 785: | Line 837: | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== Start of String Only: \A ====== | ||
+ | |||
+ | <WRAP center round box > | ||
Start of String Only: \A | Start of String Only: \A | ||
Line 790: | Line 847: | ||
From < | From < | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== 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 821: | Line 884: | ||
From < | From < | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== 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 | ||
Line 856: | Line 926: | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== 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. | ||
Line 876: | Line 951: | ||
+ | </ | ||
+ | |||
+ | ====== 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 | ||
Line 889: | Line 969: | ||
From < | From < | ||
+ | </ | ||
+ | ====== Practical Business Python ====== | ||
+ | <WRAP center round box > | ||
2018-12-24 | 2018-12-24 | ||
Practical Business Python | Practical Business Python | ||
- | |||
pbpython/ | pbpython/ | ||
Line 902: | Line 983: | ||
+ | </ | ||
+ | ====== The divmod() returns ====== | ||
+ | <WRAP center round box > | ||
Line 913: | Line 997: | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== numpy ====== | ||
+ | |||
+ | <WRAP center round box > | ||
2018-11-18 | 2018-11-18 | ||
+ | < | ||
pip install numpy | pip install numpy | ||
+ | </ | ||
From < | From < | ||
+ | < | ||
pip3.6 install numpy | pip3.6 install numpy | ||
pip3.6 install scipy | pip3.6 install scipy | ||
Line 926: | Line 1018: | ||
Install opencv-python instead of cv2. | Install opencv-python instead of cv2. | ||
pip install opencv-python | pip install opencv-python | ||
+ | </ | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== compare the use of lambda ====== | ||
+ | |||
+ | <WRAP center round box > | ||
Line 936: | Line 1034: | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== Key Functions ====== | ||
+ | |||
+ | <WRAP center round box > | ||
Key Functions | Key Functions | ||
Line 951: | Line 1054: | ||
This image was created with the following code. | This image was created with the following code. | ||
+ | < | ||
1 import operator | 1 import operator | ||
2 import pylab | 2 import pylab | ||
Line 978: | Line 1082: | ||
26 pylab.grid() | 26 pylab.grid() | ||
27 pylab.title(" | 27 pylab.title(" | ||
+ | </ | ||
From < | From < | ||
Line 983: | Line 1088: | ||
As already said, | As already said, | ||
Change: | Change: | ||
+ | < | ||
key=lambda(k, | key=lambda(k, | ||
+ | </ | ||
To: | To: | ||
+ | < | ||
key=lambda k, v: sort_order.index(k) | key=lambda k, v: sort_order.index(k) | ||
+ | </ | ||
From < | From < | ||
Line 992: | Line 1101: | ||
</ | </ | ||
- | <WRAP center round box 60%> | + | ====== What problem does pandas solve? ====== |
+ | |||
+ | <WRAP center round box > | ||
2018-11-15 | 2018-11-15 | ||
Line 1002: | Line 1113: | ||
NumPy | NumPy | ||
+ | < | ||
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 1007: | 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 | ||
+ | </ | ||
From < | From < | ||
Line 1013: | Line 1126: | ||
From < | From < | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== scikit-learn ====== | ||
+ | |||
+ | <WRAP center round box > | ||
scikit-learn | scikit-learn | ||
Line 1028: | Line 1147: | ||
From < | From < | ||
+ | </ | ||
+ | ====== python read fails on special characters ====== | ||
+ | <WRAP center round box > | ||
2018-11-06 | 2018-11-06 | ||
Line 1043: | Line 1165: | ||
From < | From < | ||
+ | </ | ||
+ | ====== idle args ====== | ||
+ | |||
+ | <WRAP center round box > | ||
2018-10-31 | 2018-10-31 | ||
Line 1058: | 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. | ||
+ | < | ||
import sys | import sys | ||
# ... | # ... | ||
Line 1072: | Line 1199: | ||
except: | except: | ||
sys.argv = [sys.argv[0], | sys.argv = [sys.argv[0], | ||
+ | </ | ||
From < | From < | ||
+ | </ | ||
+ | ====== Auto detect IDLE and prompt for command-line argument values ====== | ||
+ | |||
+ | |||
+ | <WRAP center round box > | ||
2018-10-31 | 2018-10-31 | ||
Line 1081: | Line 1214: | ||
Auto detect IDLE and prompt for command-line argument values | Auto detect IDLE and prompt for command-line argument values | ||
- | #! / | + | < |
+ | c#! / | ||
import sys | import sys | ||
def ok(x=None): | def ok(x=None): | ||
Line 1097: | Line 1231: | ||
root.bind("< | root.bind("< | ||
e.focus() | e.focus() | ||
+ | </ | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== print the files deleted ====== | ||
+ | |||
+ | <WRAP center round box > | ||
Line 1105: | 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 | ||
+ | < | ||
import os | import os | ||
for line in open(" | for line in open(" | ||
Line 1116: | Line 1257: | ||
except OSError: | except OSError: | ||
pass | pass | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== 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 | ||
+ | < | ||
import os | import os | ||
import shutil | import shutil | ||
Line 1141: | Line 1290: | ||
# | # | ||
pass | pass | ||
+ | </ | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== checkpoints ====== | ||
+ | |||
+ | <WRAP center round box > | ||
2018-10-06 | 2018-10-06 | ||
GLOB | GLOB | ||
+ | < | ||
def delete_previous_checkpoints(self, | def delete_previous_checkpoints(self, | ||
""" | """ | ||
Line 1165: | Line 1321: | ||
ckpt_file_nm = self.get_checkpoint_location() + '/ | ckpt_file_nm = self.get_checkpoint_location() + '/ | ||
os.remove(ckpt_file_nm) | os.remove(ckpt_file_nm) | ||
+ | </ | ||
From < | From < | ||
+ | </ | ||
+ | ====== argparse ====== | ||
+ | <WRAP center round box > | ||
2018-10-06 | 2018-10-06 | ||
Line 1176: | Line 1336: | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== Argparse Tutorial ====== | ||
+ | |||
+ | |||
+ | <WRAP center round box > | ||
Argparse Tutorial | Argparse Tutorial | ||
Line 1183: | Line 1349: | ||
From < | From < | ||
+ | < | ||
*args and **kwargs in Python | *args and **kwargs in Python | ||
*args | *args | ||
+ | </ | ||
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, | 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, | ||
Line 1190: | Line 1358: | ||
+ | </ | ||
+ | |||
+ | ====== recursive ====== | ||
+ | |||
+ | <WRAP center round box > | ||
2018-10-03 | 2018-10-03 | ||
Line 1199: | Line 1372: | ||
From < | From < | ||
+ | I have successfully used | ||
+ | < | ||
+ | for i in d.rglob(' | ||
+ | </ | ||
+ | |||
+ | < | ||
for i in d.iglob(' | for i in d.iglob(' | ||
+ | </ | ||
The “**” pattern means “this directory and all subdirectories, | The “**” pattern means “this directory and all subdirectories, | ||
Line 1205: | Line 1385: | ||
From < | From < | ||
+ | < | ||
errno.ENOTEMPTY | errno.ENOTEMPTY | ||
Directory not empty | Directory not empty | ||
+ | </ | ||
From < | From < | ||
+ | < | ||
errno.EACCES¶ | errno.EACCES¶ | ||
Permission denied | Permission denied | ||
+ | </ | ||
From < | From < | ||
+ | < | ||
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 | ||
+ | </ | ||
From < | From < | ||
+ | < | ||
except OSError as e: | except OSError as e: | ||
if e.errno != EINVAL and strict: | if e.errno != EINVAL and strict: | ||
raise | raise | ||
+ | </ | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== walktree ====== | ||
+ | |||
+ | <WRAP center round box > | ||
2018-10-01 | 2018-10-01 | ||
Line 1255: | Line 1448: | ||
+ | </ | ||
+ | |||
+ | ====== Dropbox in python ====== | ||
+ | |||
+ | |||
+ | |||
+ | <WRAP center round box > | ||
2018-09-29 | 2018-09-29 | ||
Dropbox in python | Dropbox in python | ||
+ | < | ||
from pathlib import Path | from pathlib import Path | ||
import arrow | import arrow | ||
Line 1271: | Line 1471: | ||
#remove it | #remove it | ||
pass | pass | ||
+ | </ | ||
From < | From < | ||
Line 1279: | Line 1479: | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== CSV Toolkit Overview ====== | ||
+ | |||
+ | |||
+ | <WRAP center round box > | ||
2018-09-04 | 2018-09-04 | ||
Line 1287: | Line 1493: | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== 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 1298: | Line 1509: | ||
From < | From < | ||
+ | </ | ||
+ | |||
+ | ====== 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 1313: | Line 1529: | ||
From < | From < | ||
+ | </ | ||
+ | ====== Python data visualization: | ||
+ | |||
+ | <WRAP center round box > | ||
2018-09-04 | 2018-09-04 | ||
Line 1323: | Line 1543: | ||
</ | </ | ||
+ | |||
+ | |||
+ | ====== Best way to sort txt file using csv tools in python ====== | ||
software/python.1609014561.txt.gz · Last modified: 2020/12/26 20:29 by superwizard