Skip to main content

Various python utilities

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyutils-AlexHenderson-1.1.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

pyutils_AlexHenderson-1.1.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file pyutils-AlexHenderson-1.1.1.tar.gz.

File metadata

  • Download URL: pyutils-AlexHenderson-1.1.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pyutils-AlexHenderson-1.1.1.tar.gz
Algorithm Hash digest
SHA256 15c304da6f2d9409234dde13a7e163b742b316b058c6185dbf5349c90269de61
MD5 90e75a7d3c3dc5eb452895178b741c51
BLAKE2b-256 bc9cb07981fffbc567c2efea9121c8c2f2d7db17d0dfde53144e240c13914c8d

See more details on using hashes here.

File details

Details for the file pyutils_AlexHenderson-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyutils_AlexHenderson-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for pyutils_AlexHenderson-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 17c6e37270e8cd539c1bda42e7aab03ce48f23d0751a7a17c13484f9c0d3a2f7
MD5 c176304c8b91fca4110a41d9b873615d
BLAKE2b-256 e34e7535c7ac952da38e230e19c6c6a1ddfa98e3cb9dc308314a076316acda2e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page