- Saved searches
- Use saved searches to filter your results more quickly
- License
- wolph/python-utils
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.rst
- About
- utils 1.0.1
- Provided things
- enum
- math
- dicts
- lists
- bools
- dates
- objects
- python-utils 3.7.0
- Links
- Security contact information
- Requirements for installing:
- Installation:
- Quickstart
- Examples
- Retrying until timeout
- Formatting of timestamps, dates and times
- Converting your test from camel-case to underscores:
- Attribute setting decorator. Very useful for the Django admin
- Scaling numbers between ranges
- Get the screen/window/terminal size in characters:
- Extracting numbers from nearly every string:
- Doing a global import of all the modules in a package programmatically:
- Automatically named logger for classes:
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Python Utils is a module with some convenient utilities not included with the standard Python install
License
wolph/python-utils
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.rst
Python Utils is a collection of small Python functions and classes which make common patterns shorter and easier. It is by no means a complete collection but it has served me quite a bit in the past and I will keep extending it.
One of the libraries using Python Utils is Django Utils.
- The source: https://github.com/WoLpH/python-utils
- Project page: https://pypi.python.org/pypi/python-utils
- Reporting bugs: https://github.com/WoLpH/python-utils/issues
- Documentation: https://python-utils.readthedocs.io/en/latest/
- My blog: https://wol.ph/
Security contact information
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
Requirements for installing:
For the Python 3+ release (i.e. v3.0.0 or higher) there are no requirements. For the Python 2 compatible version (v2.x.x) the six package is needed.
The package can be installed through pip (this is the recommended method):
Or if pip is not available, easy_install should work as well:
Or download the latest release from Pypi (https://pypi.python.org/pypi/python-utils) or Github.
Note that the releases on Pypi are signed with my GPG key (https://pgp.mit.edu/pks/lookup?op=vindex&search=0xE81444E9CE1F695D) and can be checked using GPG:
gpg --verify python-utils-version>.tar.gz.asc python-utils-version>.tar.gz
This module makes it easy to execute common tasks in Python scripts such as converting text to numbers and making sure a string is in unicode or bytes format.
Automatically converting a generator to a list, dict or other collections using a decorator:
>>> @decorators.listify() . def generate_list(): . yield 1 . yield 2 . yield 3 . >>> generate_list() [1, 2, 3] >>> @listify(collection=dict) . def dict_generator(): . yield ‘a‘, 1 . yield ‘b‘, 2 >>> dict_generator()
To easily retry a block of code with a configurable timeout, you can use the time.timeout_generator:
>>> for i in time.timeout_generator(10): . try: . # Run your code here . except Exception as e: . # Handle the exception
Formatting of timestamps, dates and times
Easy formatting of timestamps and calculating the time since:
>>> time.format_time('1') '0:00:01' >>> time.format_time(1.234) '0:00:01' >>> time.format_time(1) '0:00:01' >>> time.format_time(datetime.datetime(2000, 1, 2, 3, 4, 5, 6)) '2000-01-02 03:04:05' >>> time.format_time(datetime.date(2000, 1, 2)) '2000-01-02' >>> time.format_time(datetime.timedelta(seconds=3661)) '1:01:01' >>> time.format_time(None) '--:--:--' >>> formatters.timesince(now) 'just now' >>> formatters.timesince(now - datetime.timedelta(seconds=1)) '1 second ago' >>> formatters.timesince(now - datetime.timedelta(seconds=2)) '2 seconds ago' >>> formatters.timesince(now - datetime.timedelta(seconds=60)) '1 minute ago'
Converting your test from camel-case to underscores:
>>> camel_to_underscore('SpamEggsAndBacon') 'spam_eggs_and_bacon'
Attribute setting decorator. Very useful for the Django admin
A convenient decorator to set function attributes using a decorator:
You can use: >>> @decorators.set_attributes(short_description='Name') . def upper_case_name(self, obj): . return ("%s %s" % (obj.first_name, obj.last_name)).upper() Instead of: >>> def upper_case_name(obj): . return ("%s %s" % (obj.first_name, obj.last_name)).upper() >>> upper_case_name.short_description = 'Name'
This can be very useful for the Django admin as it allows you to have all metadata in one place.
Scaling numbers between ranges
>>> converters.remap(500, old_min=0, old_max=1000, new_min=0, new_max=100) 50 # Or with decimals: >>> remap(decimal.Decimal('250.0'), 0.0, 1000.0, 0.0, 100.0) Decimal('25.0')
Get the screen/window/terminal size in characters:
>>> terminal.get_terminal_size() (80, 24)
That method supports IPython and Jupyter as well as regular shells, using blessings and other modules depending on what is available.
Extracting numbers from nearly every string:
>>> converters.to_int('spam15eggs') 15 >>> converters.to_int('spam') 0 >>> number = converters.to_int('spam', default=1) 1
Doing a global import of all the modules in a package programmatically:
To do a global import programmatically you can use the import_global function. This effectively emulates a from . import *
from python_utils.import_ import import_global # The following is the equivalent of `from some_module import *` import_global('some_module')
Automatically named logger for classes:
Or add a correclty named logger to your classes which can be easily accessed:
class MyClass(Logged): def __init__(self): Logged.__init__(self) my_class = MyClass() # Accessing the logging method: my_class.error('error') # With formatting: my_class.error('The logger supports %(formatting)s', formatting='named parameters') # Or to access the actual log function (overwriting the log formatting can # be done n the log method) import logging my_class.log(logging.ERROR, 'log')
Alternatively loguru is also supported. It is largely a drop-in replacement for the logging module which is a bit more convenient to configure:
First install the extra loguru package:
pip install 'python-utils[loguru]'
Now you can use the Logurud class to make functions such as self.info() available. The benefit of this approach is that you can add extra context or options to you specific loguru instance (i.e. self.logger):
Convenient type aliases and some commonly used types:
# For type hinting scopes such as locals/globals/vars Scope = Dict[str, Any] OptionalScope = O[Scope] # Note that Number is only useful for extra clarity since float # will work for both int and float in practice. Number = U[int, float] DecimalNumber = U[Number, decimal.Decimal] # To accept an exception or list of exceptions ExceptionType = Type[Exception] ExceptionsType = U[Tuple[ExceptionType, . ], ExceptionType] # Matching string/bytes types: StringTypes = U[str, bytes]
About
Python Utils is a module with some convenient utilities not included with the standard Python install
utils 1.0.1
Well, we perhaps can’t answer that question. But we can collect those functions into a centralized place!
Provided things
Utils is broken up into broad swathes of functionality, to ease the task of remembering where exactly something lives.
enum
Python doesn’t have a built-in way to define an enum, so this module provides (what I think) is a pretty clean way to go about them.
Once defined, use is straightforward:
math
Currently only has the multiplicative analogue of the built-in sum .
dicts
intersections, differences, winnowing, a few specialized dicts…
lists
flatten and unlisting. also flat_map !
bools
currently only provides an xor function.
dates
Mostly cool for the TimePeriod classes:
and so on and so forth. There’s also a DiscontinousTimePeriod class, which stores a collection of TimePeriods.
There’s also helper functions for common operations like days_ahead and days_ago , which pretty much do what they say on the tin.
objects
provides get_attr , which is really just a convenient way to do deep getattr chaining:
There’s also an immutable utility, which will wrap an object and preven all attribute changes, recursively by default. Any attempt to set attributes on the wrapped object will raise an AttributeError :
You can toggle the recursive immutability by specifying the ‘recursive’ flag.
python-utils 3.7.0
Python Utils is a collection of small Python functions and classes which make common patterns shorter and easier. It is by no means a complete collection but it has served me quite a bit in the past and I will keep extending it.
One of the libraries using Python Utils is Django Utils.
Links
- The source: https://github.com/WoLpH/python-utils
- Project page: https://pypi.python.org/pypi/python-utils
- Reporting bugs: https://github.com/WoLpH/python-utils/issues
- Documentation: https://python-utils.readthedocs.io/en/latest/
- My blog: https://wol.ph/
Security contact information
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
Requirements for installing:
For the Python 3+ release (i.e. v3.0.0 or higher) there are no requirements. For the Python 2 compatible version (v2.x.x) the six package is needed.
Installation:
The package can be installed through pip (this is the recommended method):
Or if pip is not available, easy_install should work as well:
Or download the latest release from Pypi (https://pypi.python.org/pypi/python-utils) or Github.
Note that the releases on Pypi are signed with my GPG key (https://pgp.mit.edu/pks/lookup?op=vindex&search=0xE81444E9CE1F695D) and can be checked using GPG:
gpg --verify python-utils-.tar.gz.asc python-utils-.tar.gz
Quickstart
This module makes it easy to execute common tasks in Python scripts such as converting text to numbers and making sure a string is in unicode or bytes format.
Examples
Automatically converting a generator to a list, dict or other collections using a decorator:
Retrying until timeout
To easily retry a block of code with a configurable timeout, you can use the time.timeout_generator :
Formatting of timestamps, dates and times
Easy formatting of timestamps and calculating the time since:
Converting your test from camel-case to underscores:
Attribute setting decorator. Very useful for the Django admin
A convenient decorator to set function attributes using a decorator:
This can be very useful for the Django admin as it allows you to have all metadata in one place.
Scaling numbers between ranges
Get the screen/window/terminal size in characters:
That method supports IPython and Jupyter as well as regular shells, using blessings and other modules depending on what is available.
Extracting numbers from nearly every string:
Doing a global import of all the modules in a package programmatically:
To do a global import programmatically you can use the import_global function. This effectively emulates a from … import *
Automatically named logger for classes:
Or add a correclty named logger to your classes which can be easily accessed:
Alternatively loguru is also supported. It is largely a drop-in replacement for the logging module which is a bit more convenient to configure:
First install the extra loguru package:
pip install Now you can use the Logurud class to make functions such as self.info() available. The benefit of this approach is that you can add extra context or options to you specific loguru instance (i.e. self.logger ):
Convenient type aliases and some commonly used types: