Python¶
Contents:
- Async Python
- Asyncio (superseded by async page)
- Beautiful Soup
- My Python Development Environment
- Devpi
- How to Fix your Python Code’s Style
- Gitpython
- How Mock Can Improve Your Unit Tests
- The mock methods and mock objects
- Mock a function being called in another module
- Mock an attribute
- Mock something on a class
- Where to mock
- When we can’t mock
- How mock.patch and mock.patch.object work
- Controlling the mock object’s behavior
- Determining what was done with a mock object
- For more information
- Python Packaging
- Pip
- Pipenv
- Better Python dependency management with pip-tools
- Pyenv
- Six
- Timezones in Python
- Time zones and Daylight Saving Time—Oh, the Horror
- Tox
- Python Versions
- Virtual environments
- XML in Python
Most minimal logging¶
Python doesn’t provide any logging handlers by default, resulting in not seeing anything but an error from the logging package itself… Add a handler to the root logger so we can see the actual errors.:
import logging
logging.getLogger('').addHandler(logging.StreamHandler())
Binary data to file-like object (readable)¶
f = io.BytesIO(binary_data) # Python 2.7 and 3
Join list items that are not None¶
Special case use of filter:
join(', ', filter(None, the_list))
Declare file encoding¶
Top of .py file:
# -*- coding: UTF-8 -*-
# vim:fileencoding=UTF-8
Quick ‘n’ easy static web server¶
Change to the top level directory of the static files
Run
python -m http.server
Python snippet copy logging to stdout¶
add to top:
import logging
handler = logging.StreamHandler()
root_logger = logging.getLogger('')
root_logger.setLevel(logging.DEBUG)
root_logger.addHandler(handler)
Warnings¶
Hiding python warnings. e.g. Deprecations:
python -Wignore::DeprecationWarning ...
Pylint¶
Running it:
python /usr/bin/pylint --rcfile=.pylintrc -f colorized aremind.apps.patients | less -r
Disabling a warning in code:
# pylint: disable-msg=E1101