Pinboard: /u:kittell/t:python
pip
Update pip (ref):
[Mac] pip install -U pip
[Windows] python -m pip install -U pip
Update package: pip install [package_name] --upgrade
docstring
PEP 257 -- Docstring Conventions
def complex(real=0.0, imag=0.0):
"""Form a complex number.
Keyword arguments:
real -- the real part (default 0.0)
imag -- the imaginary part (default 0.0)
"""
Input
User input
input(prompt)
Files
Copy file: shutil.copy2(fullpath, fullpath_copy)
Delete file: os.remove(fullpath)
Read/write to files: docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
with open('workfile') as f:
for line in f:
print(line, end='')
CSV files
with open(filepath, newline='') as f:
reader = csv.reader(f)
for row in reader:
# do something
List/dict/set comprehensions
list_comp = [expr for val in collection if condition]
dict_comp = {key-expr : value-expr for value in collection if condition}
set_comp = {expr for value in collection if condition}
Operating system interfaces
os
Walk through directory tree:
for root, dirs, files in os.walk(start_dir):
# do something
Create directory if it doesn't exist (via SO):
os.makedirs("path/to/directory", exist_ok=True)
os.path
Base user directory: os.path.expanduser('~')
Datetime
string to datetime: datetime.strptime()
datetime to string: datetime.strftime()
openpyxl
Load workbook: wb = load_workbook(filename=fullpath, read_only=True)
Loop over rows in worksheet:
for row in ws.rows:
for col in row:
v = row[col].value
Plotting
# Draw an ellipse using parametric equations
# Source: https://stackoverflow.com/a/48409811/752784
import numpy
from matplotlib import pyplot
from math import pi
x_0 = 1.0
y_0 = 0.5
a = 2.0 # semimajor axis
b = 1.5 # semiminor axis
t = numpy.linspace(0, 2 * pi, 100)
pyplot.plot(x_0 + a * numpy.cos(t), y_0 + b * numpy.sin(t))
pyplot.show()
# Draw a random path
import numpy
import matplotlib.pyplot as pyplot
# Add random number of points
import random
n = random.randint(5,10)
points = numpy.zeros((n,2))
for i in range(n):
# Add random points
a = random.randint(1,10)
b = random.randint(1,10)
points[i,:] = [a, b]
# Complete the loop: repeat first point as last point
# Appending to numpy arrays: https://stackoverflow.com/a/42545300/752784
points = numpy.append(points, [points[0,:]], axis=0)
pyplot.plot(points[:,0], points[:,1])
pyplot.show()
Pandas
From Chris Moffitt, Tips for Selecting Columns in a DataFrame, Practical Business Python, 2019-11-26:
df = pd.read_csv(
'https://data.cityofnewyork.us/api/views/vfnx-vebw/rows.csv?accessType=DOWNLOAD&bom=true&format=true'
)
col_mapping_dict = {c[0]:c[1] for c in enumerate(df.columns)}
df.iloc[:, np.r_[0:3,15:19,24,25]]
Trees
tkinter
Dialogs
tkinter.simpledialog, tkinter.filedialog, tkinter.commondialog
Simple file selection (inspired by)
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import os.path
import pandas as pd
def load_data_file():
Tk().withdraw()
data_filepath = askopenfilename()
data_filename = os.path.split(data_filepath)[1]
ext = os.path.splitext(data_filename)[1]
if ext == '.xlsx' or ext == '.xls':
df = pd.read_excel(data_filepath)
elif ext == '.csv':
df = pd.read_csv(data_filepath)
return df
Miscellaneous
Pyperclip
pyperclip.copy('Hello, world!')