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

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.