This is a collection of a various Python scripts and helper information.
- scripts
- Script to create virtualenv from
requirements.txt - Script to start and use virtualenv
- Script to create virtualenv from
- helpers
cmdUninstallGlobalPackages.bathelps to uninstall globally installed pacakges
Virtual environments are highly recommended for Python development and must be used by default. However, in certain cases, it's beneficial to install some Python packages globally:
- Extremely fast Python package installer and resolver
pip install --upgrade uv
- Multi-language pre-commit hooks for Git
pip install --upgrade pre-commit
Located in subdirectory snippets
- urls_availability_in_markdown.py
- Downloads markdown files and checks, if provided URLS are accessible
- jupyter_notebook_config_post_save.py
- Jupyter helpers
- pyparser_gist_comments.py
- Parser that fetches comments from GitHub Gist
- deprecated
- helper_directory.py
- Useful functions wrapper for working with files and directories
- helper_directory.py
- Random password generators
python-c"from random import choice; import uuid; print ('{0}{1}{2}'.format(''.join([choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') for i in range(8)]), str(uuid.uuid4().get_hex().upper()[0:6]), ''.join([choice('%^*(-_=+)') for i in range(2)])))"python-c"from random import choice; import uuid; print ('{0}'.format(''.join([choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') for i in range(10)])))"- Current date of the week
python-c"import datetime; print('week of the year {0}'.format(datetime.date.today().isocalendar()[1]))"- Various Python Paths
python-c"import sys; print ('\n'.join(sys.path))"python3-c"import sys; print ('\n'.join(sys.path))"- Check if virtual environment is active
python -c "import sys; print('virtualenv is active' if sys.prefix != sys.base_prefix else 'virtualenv is NOT active')"
- Python 2
python -m SimpleHTTPServer 80
- Python 3
python -m http.server 5151 --bind 127.0.0.1
- Formatting json
python -m json.tool myfile.json- OR
python3 -m json.tool myfile.jsonfromdatetimeimportdatetimenow=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# ornow=datetime.now().strftime("%Y-%m-%d %H:%M[:%S[.%f]]")On Linux
export PYTHONDONTWRITEBYTECODE=1On Windows
set PYTHONDONTWRITEBYTECODE=1importsyssys.dont_write_bytecode=True- Simple implementation
importtracebacktry:
print ('[i] Exception handling that shows the line with exception')
raiseException('Test')
exceptException:
print (traceback.format_exc())- Further implementation
deflog_traceback(ex, ex_traceback=None):
''' Logs exceptions'''ifex_tracebackisNone: ex_traceback=ex.__traceback__tb_lines= [ line.rstrip('\n') forlineintraceback.format_exception(ex.__class__, ex, ex_traceback)]
#logging.exception(tb_lines)print(tb_lines)
try:
raiseException('A test exception')
exceptExceptionasex:
_, _, ex_traceback=sys.exc_info()
log_traceback(ex, ex_traceback)defdump(obj):
""" Makes dump of the object (helper function)"""forattrindir(obj):
print('obj.{0} = {1}'.format(attr, getattr(obj, attr)))- Disable pip outside of a virtual environment. Further info here
pip config set global.require-virtualenv True- pre-commit
- Install
pip install --upgrade pre-commit - View sample config
pre-commit sample-config - Create empty config file and copt sample config (
pre-commit sample-config)touch .pre-commit-config.yaml - Install the git hook scripts:
pre-commit install - Run manually:
pre-commit run --all-files - Bypass pre-commit hooks in git
git commit -m "will fix asap" --no-verify - Add further plugins
- Overview
- Plugins
- Install
pre-commitwroking example:- https://github.com/vdmitriyev/pymultissher/blob/main/.pre-commit-config.yaml
pre-commitby default places its repository store in~/.cache/pre-commit
- isort
- https://github.com/pycqa/isort
- VS Code Extension + Config
- Black Formatter
- Materials
- https://gist.github.com/subfuzion/db7f57fff2fb6998a16c
- Create a new file
%USERPROFILE%\.gitignore_global - Add what you want to ignore globally in git
- Config git to find global gitignore file:
git config --global core.excludesfile "%USERPROFILE%\.gitignore_global" - Verify by checking file
%USERPROFILE%\.gitconfigcat %USERPROFILE%\.gitconfig
- Viktor Dmitriyev