Skip to content

Latest commit

 

History

History
158 lines (129 loc) · 4.59 KB

python.md

File metadata and controls

158 lines (129 loc) · 4.59 KB

Python

Reference

Useful modules

Pip and packaging

pip \
  --trusted-host pypi.org \
  --trusted-host pypi.python.org \
  --trusted-host files.pythonhosted.org \
  install pytest
  • Add a third-party repo for pip to use:
export PIP_TRUSTED_HOST="gitlab.example.com"
export PIP_EXTRA_INDEX_URL="http://${PACKAGES_TOKEN_NAME}:${PACKAGES_TOKEN}@gitlab.example.com/api/v4/groups/5/-/packages/pypi/simple"
  • pip install -e . = Install package locally in "developer" mode. Code modifications will automatically update within the installed package.

Venv

  • python3 -m venv my_env = Create a virtual environment called my_env.
  • source my_env/bin/activate = Activate the virtual environment called my_env.

Pytest and testing

Web

Misc tips

python3 -i script.py = Run script.py, then launch an interactive REPL for debugging afterwards.

Regex search.

import re
http_proxy = "https://10.0.0.15:8080"
http_proxy_server = re.search("https?://([0-9]{1,3}.){3}[0-9]{1,3}", http_proxy)
print(http_proxy_server.group())
# https://10.0.0.15

Format string.

my_number = 123000
print("{:,}".format(my_number))
# 123,000

Format time.

import datetime
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

Print substring by index.

my_string = "Green eggs and ham."
print(my_string[6:10])
# eggs
print(my_string[6:-4])
# eggs and
print(my_string[6:])
# eggs and ham.
print(my_string[::-1])
# .mah dna sgge neerG

Join list items into string.

my_list = ['1', '2', '3']
print(" ".join(my_list))
# 1 2 3
print("\n".join(my_list))
# 1
# 2
# 3

Pad string with hyphens until it's 10 characters long.

my_string = "test "
print(my_string.ljust(10, "-"))
# test -----

Use _ to define an unused variable in a loop.

for _ in range(10):
  print("*", end="")

File operations

Move files to a different directory using a glob.

glob_string = ("/path/to/files" + "/*.mp4")
mp4_files_destination = os.path.join("/path/to/files", "destination")
for filepath in glob.glob(glob_string):
    shutil.move(filepath, mp4_files_destination)

List directory.

os.listdir("/path/to/directory")

List directories recursively.

for rootpath, dirs, files in os.walk("/path/to/directory"):
    for dirname in dirs:
        print(os.path.join(rootpath, dirname))

Read a file.

with open("/path/to/file") as f:
    content = f.readlines()

Get path depth.

for path, dirs, files in os.walk(auto_screenshots_dir):
    print(path.count)
    if path.count(os.sep) >= 10:
        print(os.path.join(path))