Tips
Find all installed modules
help("modules");
Virtual Environment
With virtualenv and virtualenvwrapper
# Installing virtualenv and virtualenvwrapper
sudo pip install virtualenv virtualenvwrapper
# Update the profile ~/.bashrc
# Add the following lines
# Python virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
# Reload the profile
source ~/.bashrc
# Creating python virtual environment
# The py3cv3 is a self-defined name
mkvirtualenv py3cv3 -p python3
# Enter the specified virtual environment
workon py3cv3
# Exit the the specified virtual environment
deactivate
# List all of the environments.
lsvirtualenv
# Remove an environment
rmvirtualenv py3cv3
Timestamp
timestamp = datetime.datetime.now()
print("It is {}".format(timestamp.strftime("%A %d %B %Y %I:%M:%S%p")))
Datetime
from datetime import datetime
date_str = '09-19-2022'
date_object = datetime.strptime(date_str, '%m-%d-%Y').date()
print(type(date_object))
print(date_object) # printed in default format
# Output:
# <class 'datetime.date'>
# 2022-09-19
from datetime import datetime
time_str = '13::55::26'
time_object = datetime.strptime(time_str, '%H::%M::%S').time()
print(type(time_object))
print(time_object)
# Output:
# <class 'datetime.time'>
# 13:55:26
from datetime import datetime
import locale
locale.setlocale(locale.LC_ALL, 'de_DE')
date_str_de_DE = '16-Dezember-2022 Freitag' # de_DE locale
datetime_object = datetime.strptime(date_str_de_DE, '%d-%B-%Y %A')
print(type(datetime_object))
print(datetime_object)
# Output:
# <class 'datetime.datetime'>
# 2022-12-16 00:00:00
Math
total += 1
If-then
# Boolean, none
if motion is not None:
if not flag:
# Number
if delay > 0:
if delay == 0:
if total > frameCount:
# String
if "blue" in style:
if authors.startswith('['):
authors = authors.lstrip('[').rstrip(']')
def doi_url(d): return f'http://{d}' if d.startswith('doi.org') else f'http://doi.org/{d}'
Command Arguments
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--interval", required=False,
help="Seconds to Interval (Default:30)", default="30", type=int)
ap.add_argument("-o", "--output", required=False,
help="Path to Output Logs (Default:std-out)")
args = vars(ap.parse_args())
# Usage
intv = args["interval"]
logfile = args["output"]