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.

Examples:

Type conversions:

from tunit 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 import TUnitConfig, SerializationMode, 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!'}

Changelog:


  • 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.5.1.tar.gz (6.7 kB view hashes)

Uploaded Source

Supported by

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