-
See also:
-
Disregard SSL certificates when installing pytest:
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.
python3 -m venv my_env
= Create a virtual environment called my_env.source my_env/bin/activate
= Activate the virtual environment called my_env.
- Write URL content to a file
python3 -m http.server 8080
= Create a test HTTP server on port 8080.
python3 -i script.py
= Run script.py, then launch an interactive REPL for debugging afterwards.
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
my_number = 123000
print("{:,}".format(my_number))
# 123,000
import datetime
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
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="")
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))