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-registryfor JSON serialization/deserialization.
- Using
- 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
Metadata
Release files for tunit 1.7.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tunit-1.7.2.tar.gz | 7.9 kB | Without provenance | Details for tunit-1.7.2.tar.gz |
Release files / tunit-1.7.2.tar.gz
| Download URL | tunit-1.7.2.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e7d5019c7959070789ebbe57ad9d0453dc8efe4d917b6a03ab60d4efa69847f6
|
|
BLAKE2b-256 checksum How to use checksums |
8c91f3bdae396c7d797d6fb7930a205aeae617149a690faf51d198f220d494c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.7.16
|
Security for tunit 1.7.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.