Skip to main content

pyutils

Various Python utilities

Requires Python 3.7 or higher.
Other requirements are listed per utility.

pip install pyutils-AlexHenderson==1.1.1

Utilities

  • unit_manager
  • json_pint_encoder
  • json_datetime_encoder
  • multiple_json_encoders

pint_utils/unit_manager

Requirements

pint

Details

This is a wrapper around the Pint unit management system (https://pint.readthedocs.io/en/stable/). Formats a quantity label suitable for use on the x-axis of a chart. Units are output in their shortform (nm for nanometer). If a unit is not provided, the quantity will be treated as dimensionless.

Example for a wavelength of 280 nm:

    >>> from pyutils.pint_utils import unit_manager
    >>> ureg = unit_manager.pint.UnitRegistry()
    >>> output = unit_manager.unit_manager('wavelength', ureg.nanometer, 280)
    >>> output
    {'name': 'wavelength', 'unit': 'nm', 'label': 'wavelength (nm)', 'value': 280, 'quantity': '280 nm'}
    >>> print(output['label'])
    wavelength (nm)
    >>> print(f'peak position = {output["quantity"]}')
    peak position = 280 nm

Example for unitless absorbance:

    >>> from pyutils.pint_utils import unit_manager
    >>> ureg = unit_manager.pint.UnitRegistry()
    >>> output = unit_manager.unit_manager('absorbance')
    >>> output
    {'name': 'absorbance', 'unit': '', 'label': 'absorbance'}
    >>> print(output['label'])
    absorbance

pint_utils/json_pint_encoder

Requirements

pint

Details

JSON can only encode certain variable types as output. Variables from the Pint unit management system (https://pint.readthedocs.io/en/stable/) are not handled automatically. This code converts a Pint unit to a string, and passes that to the JSON encoding function.

Example:

>>> from pint import Quantity, Unit, UnitRegistry
>>> from pyutils.pint_utils.json_pint_encoder import json_pint_encoder

>>> ureg = UnitRegistry()
>>> duration = Quantity(5.6, ureg.sec)

>>> encoder = json_pint_encoder
>>> jsonoutput = json.dumps(duration, cls=encoder)
>>> print(jsonoutput)
"5.6 second"

datetime_utils/json_datetime_encoder

Details

JSON can only encode certain variable types as output. datetime variables are not handled automatically. This code converts a datetime variable into its isoformat string (ISO 8601), and passes that to the JSON encoding function.

Example:

>>> import datetime
>>> import json

>>> from pyutils.datetime_utils.json_datetime_encoder import json_datetime_encoder

>>> example = datetime.datetime(2000, 12, 25, 13, 23)

>>> encoder = json_datetime_encoder
>>> jsonoutput = json.dumps(example, cls=encoder)
>>> print(jsonoutput)
"2000-12-25T13:23:00"

multiple_json_encoders

Details

JSON can only encode certain variable types as output. We can supply our own encoder to convert a variable into a format the JSON engine can work with. However, there are occasions when we need to manage more than one type of variable. This class takes multiple encoders and dispatches each variable to its own encoder, before forwarding the output to the JSON engine. The order the encoders are listed as arguments to the multiple_encoders constructor, is the order in which they are evaluated.

Example:

>>> import datetime 
>>> import json

>>> from pint import Quantity, Unit, UnitRegistry

>>> from pyutils.json_utils.multiple_json_encoders import multiple_json_encoders
>>> from pyutils.pint_utils.json_pint_encoder import json_pint_encoder
>>> from pyutils.datetime_utils.json_datetime_encoder import json_datetime_encoder

>>> christmas_time = datetime.datetime(2000, 12, 25, 13, 23)
>>> ureg = UnitRegistry()
>>> duration = Quantity(5.6, ureg.sec)

>>> encoder = multiple_json_encoders(json_pint_encoder, json_datetime_encoder)

>>> christmas_time_output = json.dumps(christmas_time, cls=encoder)
>>> print(christmas_time_output)
"2000-12-25T13:23:00"

>>> durationoutput = json.dumps(duration, cls=encoder)
>>> print(durationoutput)
"5.6 second"

Usage rights

Copyright (c) 2021-2022 Alex Henderson (alex.henderson@manchester.ac.uk)
Licensed under the MIT License. See https://opensource.org/licenses/MIT
SPDX-License-Identifier: MIT
Version 1.1.1
See https://github.com/AlexHenderson/pyutils/ for the most recent version

Metadata

Release files for pyutils-AlexHenderson 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyutils-AlexHenderson 1.1.1
File Size Uploaded
pyutils-AlexHenderson-1.1.1.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyutils-AlexHenderson 1.1.1
File Interpreter ABI Platform
pyutils_AlexHenderson-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.1 kB

Release files / pyutils-AlexHenderson-1.1.1.tar.gz

Download URL pyutils-AlexHenderson-1.1.1.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
15c304da6f2d9409234dde13a7e163b742b316b058c6185dbf5349c90269de61
BLAKE2b-256 checksum
How to use checksums
bc9cb07981fffbc567c2efea9121c8c2f2d7db17d0dfde53144e240c13914c8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.4.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.6

Release files / pyutils_AlexHenderson-1.1.1-py3-none-any.whl

Download URL pyutils_AlexHenderson-1.1.1-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17c6e37270e8cd539c1bda42e7aab03ce48f23d0751a7a17c13484f9c0d3a2f7
BLAKE2b-256 checksum
How to use checksums
e34e7535c7ac952da38e230e19c6c6a1ddfa98e3cb9dc308314a076316acda2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.4.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.6

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page