雨山



Python cheat sheet

Pad a string:

# padding on the left
print("234".rjust(10, "0"))
# 0000000234

# padding on the right
print("234".ljust(10, "0"))
# 2340000000

Regular expressions (regex)

import re

# create a pattern
whitespace = re.compile("\\s")

# find the first occurrence of a pattern
whitespace.search("Hello, world!")
# <re.Match object; span=(6, 7), match=' '>

# find all occurrences of a pattern
whitespace.findall("It was the best of times...")
# [' ', ' ', ' ', ' ', ' ']

# capture
# (note: i had to insert extra back-slashes here because eleventy threw a fit
# about the curly braces. you'll probably need to remove them when you actually
# run this code!)
pattern = re.compile("\{\{\\s*(.*?)\\s*\}\}", re.S)
match = pattern.search("I am \{\{ age \}\} years old.")
print(match.group(1))
# 'age'

# multiple flags can be combined with the bit-wise OR operator:
pattern = re.compile("whatever", re.M | re.S)

# it's also possible to use regular expressions without compiling them first:
print(re.findall(r"\s", "It was the best of times..."))
# [' ', ' ', ' ', ' ', ' ']

Use getters, setters, and deleters:

class Foo:
  def __init__(self):
    self._value = None

  @property
  def value(self):
    return self._value

  @value.setter
  def value(self, new_value):
    self._value = new_value

  @value.deleter
  def value(self):
    del self._x

Read an environment variable:

import os
print(os.environ["WHATEVS"])

Check if a file or folder exists:

import os
print(os.path.exists("/some/path"))

Check if a path is a file or folder:

import os
print(os.path.isfile("/some/path"))
print(os.path.isdir("/some/path"))

Get the size of a file:

import os
print(os.path.getsize("/path/to/file"))

Pad numbers:

print("{:07d}".format(3))
# 0000003

Get the absolute path of a script:

__file__

NOTE: In the above example, __file__ returns the path to the script in which that variable name is referenced, not the path of the script that was invoked. For example, consider the following situation. At the root of a project, I have a script called foo.py that imports a function from a/b/c/bar.py in the same project. The function in a/b/c/bar.py references __file__. Like this:

# foo.py
from a.b.c.bar import bar
bar()
# a/b/c/bar.py
def bar():
  print(__file__)
python foo.py
# /path/to/project/a/b/c/bar.py

Get the absolute path of the directory containing a script:

import os
dir = (os.path.sep).join(__file__.split(os.path.sep)[:-1])

NOTE: See the note in the previous section regarding __file__.

Run an external (e.g., Bash) command:

import process
command = "ls -la"
subprocess.run(command, shell=True, executable="/bin/bash")

NOTE: According to this article: "If we pass a single string as a command to subprocess.run(), then the value of the argument must be True for the shell parameter. Otherwise, we must pass the command as a list of strings, and shell must be False."

Serialize and deserialize an object:

import pickle

my_obj = { "hello": "world" }

# note the "wb" argument:
with open("my_file.pkl", "wb") as file:
  pickle.dump(my_obj, file, protocol=5)

# note the "rb" argument:
with open("my_file.pkl", "rb") as file:
  my_new_obj = pickle.load(file)

NOTE: There are serious security implications involved in this ☝️ process. The sklearn "Model persistence" page has a good summary of the different serialization tools.

NOTE: Also note that deserialization (at least with pickle) will fail if any of the relevant resources haven't yet been loaded. For example, if the object being deserialized has a property pointing to a function called foo, then foo must be loaded before deserialization can occur.