Skip to main content

Time unit types. For transparency safety and readability.

Project description

PyPI pyversions PyPI version shields.io PyPI license Downloads

TUnit


Time unit types. For transparency, safety and readability.

Installation:

pip install tunit # Install just the base package
pip install tunit[json] # Install with extra JSON serialization/deserialization support
pip install tunit[yaml] # Install with extra YAML serialization/deserialization support
pip install tunit[all] # Install with all extras

Examples:

Type conversions:

from tunit.unit import Days, Hours, Minutes, Seconds, Milliseconds

# Type annotations:
def timestamp() -> Milliseconds:
    # Time unit conversions:
    return Milliseconds(Seconds(1))  # 1_000 ms

# Converting to smaller units:
assert Hours(Days(1)) == Hours(24) == 24

# Converting to bigger units:
assert Minutes(Seconds(65)) == Minutes(1) == 1

# Converting floats to time units:
assert Seconds(Minutes(0.5)) == Seconds(0) == 0  # Time units hold integers!
assert Seconds.fromRawUnit(Minutes, 0.5) == Seconds(500) == 500  # Better approach when fractions matter!

# Converting time units to floats:
assert float(Seconds(Milliseconds(1_500))) == 1.0  # Loses precision!
assert Milliseconds(1_500).toRawUnit(Seconds) == 1.5  # Converts to float representing different time unit with precision.

JSON serialization:

import json
from tunit.config import TUnitConfig
from tunit.core import SerializationMode
from tunit.unit import Seconds

TUnitConfig.registerJsonHandler() # Enable JSON serialization/deserialization

# JSON serialization:
messageDto = {
    "message": "Some message!",
    "delay": Seconds(10)
}
messageJson = json.dumps(messageDto)
print(messageJson) # Prints: '{"message": "Some message!", "delay": "10s"}'

# JSON deserialization:
messageJson = '{"message": "Some message!", "delay": "10s"}'
messageDto = json.loads(messageJson)
from pprint import pprint
pprint(messageDto, width=30)
# Prints:
# {'delay': Seconds(10),
# 'message': 'Some message!'}

# JSON serialization modes:
TUnitConfig.setSerializationMode(mode=SerializationMode.Symbol)
print(json.dumps(messageDto)) # Prints: '{"message": "Some message!", "delay": "10s"}'
TUnitConfig.setSerializationMode(mode=SerializationMode.ClassName)
print(json.dumps(messageDto)) # Prints: '{"message": "Some message!", "delay": "Seconds(10)"}'
TUnitConfig.resetSerializationMode() # Restores default mode, which is: SerializationMode.Symbol

# JSON deserialization vs serialization modes:
TUnitConfig.setSerializationMode(mode=SerializationMode.Symbol)
pprint(json.loads(messageDto))
TUnitConfig.setSerializationMode(mode=SerializationMode.ClassName)
pprint(json.loads(messageDto))
TUnitConfig.resetSerializationMode()
pprint(json.loads(messageDto))
# All three print: {'delay': Seconds(10), 'message': 'Some message!'}

YAML serialization:

import yaml
from yaml import Loader
from tunit.config import TUnitConfig
from tunit.unit import Seconds

TUnitConfig.registerYamlHandler() # Enable YAML serialization/deserialization

# YAML serialization:
configDict = {
    "dispatcher": {
        "name": "queuePublisher",
        "delay": Seconds(10)
    }
}
configYaml = yaml.dump(configDict)
print(configYaml)
# Prints:
# dispatcher:
#     name: queuePublisher
#     delay: 10s

# YAML deserialization:
configYaml = """
dispatcher:
    name: queuePublisher
    delay: 10s
"""
configDict = yaml.load(configYaml, Loader)
from pprint import pprint
pprint(configYaml, width=30)
# Prints:
# {'dispatcher': {'delay': Seconds(10),
#                 'name': 'queuePublisher'}}

Changelog:

  • Version: 1.7.0
    • Made markup serialization/deserialization into installable extras.
    • Added support for YAML markup.
  • Version: 1.6.0
    • Using json-handler-registry for JSON serialization/deserialization.
  • Version: 1.5.1
    • Deep JSON serialization/deserialization fix.
  • Version: 1.5.0
    • JSON serialization/deserialization support.
  • Version: 1.4.0
    • MyPy static analysis support.
  • Version: 1.2.0
    • Float conversions.

License

MIT

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

tunit-1.7.2.tar.gz (7.9 kB view details)

Uploaded Source

File details

Details for the file tunit-1.7.2.tar.gz.

File metadata

  • Download URL: tunit-1.7.2.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.16

File hashes

Hashes for tunit-1.7.2.tar.gz
Algorithm Hash digest
SHA256 e7d5019c7959070789ebbe57ad9d0453dc8efe4d917b6a03ab60d4efa69847f6
MD5 b64cba5c625246e9e577c128ccfa37c0
BLAKE2b-256 8c91f3bdae396c7d797d6fb7930a205aeae617149a690faf51d198f220d494c0

See more details on using hashes here.

Supported by

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