tunit


Nametunit JSON
Version 1.7.2 PyPI version JSON
download
home_pageNone
SummaryTime unit types. For transparency safety and readability.
upload_time2024-04-07 17:05:24
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseMIT
keywords time units timestamp chrono seconds milliseconds
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![PyPI pyversions](https://img.shields.io/pypi/pyversions/tunit.svg)](https://pypi.python.org/pypi/tunit)
[![PyPI version shields.io](https://img.shields.io/pypi/v/tunit.svg)](https://pypi.python.org/pypi/tunit)
[![PyPI license](https://img.shields.io/pypi/l/tunit.svg)](https://pypi.python.org/pypi/tunit)
[![Downloads](https://static.pepy.tech/badge/tunit)](https://pepy.tech/project/tunit)

# TUnit
---
Time unit types. For transparency, safety and readability.

## Installation:

```bash
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:
```python
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:
```python
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:
```python
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

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tunit",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "time, units, timestamp, chrono, seconds, milliseconds",
    "author": null,
    "author_email": "\"P.J. Grochowski\" <pawel.grochowski.dev@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/8c/91/f3bdae396c7d797d6fb7930a205aeae617149a690faf51d198f220d494c0/tunit-1.7.2.tar.gz",
    "platform": null,
    "description": "[![PyPI pyversions](https://img.shields.io/pypi/pyversions/tunit.svg)](https://pypi.python.org/pypi/tunit)\n[![PyPI version shields.io](https://img.shields.io/pypi/v/tunit.svg)](https://pypi.python.org/pypi/tunit)\n[![PyPI license](https://img.shields.io/pypi/l/tunit.svg)](https://pypi.python.org/pypi/tunit)\n[![Downloads](https://static.pepy.tech/badge/tunit)](https://pepy.tech/project/tunit)\n\n# TUnit\n---\nTime unit types. For transparency, safety and readability.\n\n## Installation:\n\n```bash\npip install tunit # Install just the base package\npip install tunit[json] # Install with extra JSON serialization/deserialization support\npip install tunit[yaml] # Install with extra YAML serialization/deserialization support\npip install tunit[all] # Install with all extras\n```\n\n## Examples:\n\nType conversions:\n```python\nfrom tunit.unit import Days, Hours, Minutes, Seconds, Milliseconds\n\n# Type annotations:\ndef timestamp() -> Milliseconds:\n    # Time unit conversions:\n    return Milliseconds(Seconds(1))  # 1_000 ms\n\n# Converting to smaller units:\nassert Hours(Days(1)) == Hours(24) == 24\n\n# Converting to bigger units:\nassert Minutes(Seconds(65)) == Minutes(1) == 1\n\n# Converting floats to time units:\nassert Seconds(Minutes(0.5)) == Seconds(0) == 0  # Time units hold integers!\nassert Seconds.fromRawUnit(Minutes, 0.5) == Seconds(500) == 500  # Better approach when fractions matter!\n\n# Converting time units to floats:\nassert float(Seconds(Milliseconds(1_500))) == 1.0  # Loses precision!\nassert Milliseconds(1_500).toRawUnit(Seconds) == 1.5  # Converts to float representing different time unit with precision.\n```\n\nJSON serialization:\n```python\nimport json\nfrom tunit.config import TUnitConfig\nfrom tunit.core import SerializationMode\nfrom tunit.unit import Seconds\n\nTUnitConfig.registerJsonHandler() # Enable JSON serialization/deserialization\n\n# JSON serialization:\nmessageDto = {\n    \"message\": \"Some message!\",\n    \"delay\": Seconds(10)\n}\nmessageJson = json.dumps(messageDto)\nprint(messageJson) # Prints: '{\"message\": \"Some message!\", \"delay\": \"10s\"}'\n\n# JSON deserialization:\nmessageJson = '{\"message\": \"Some message!\", \"delay\": \"10s\"}'\nmessageDto = json.loads(messageJson)\nfrom pprint import pprint\npprint(messageDto, width=30)\n# Prints:\n# {'delay': Seconds(10),\n# 'message': 'Some message!'}\n\n# JSON serialization modes:\nTUnitConfig.setSerializationMode(mode=SerializationMode.Symbol)\nprint(json.dumps(messageDto)) # Prints: '{\"message\": \"Some message!\", \"delay\": \"10s\"}'\nTUnitConfig.setSerializationMode(mode=SerializationMode.ClassName)\nprint(json.dumps(messageDto)) # Prints: '{\"message\": \"Some message!\", \"delay\": \"Seconds(10)\"}'\nTUnitConfig.resetSerializationMode() # Restores default mode, which is: SerializationMode.Symbol\n\n# JSON deserialization vs serialization modes:\nTUnitConfig.setSerializationMode(mode=SerializationMode.Symbol)\npprint(json.loads(messageDto))\nTUnitConfig.setSerializationMode(mode=SerializationMode.ClassName)\npprint(json.loads(messageDto))\nTUnitConfig.resetSerializationMode()\npprint(json.loads(messageDto))\n# All three print: {'delay': Seconds(10), 'message': 'Some message!'}\n```\n\nYAML serialization:\n```python\nimport yaml\nfrom yaml import Loader\nfrom tunit.config import TUnitConfig\nfrom tunit.unit import Seconds\n\nTUnitConfig.registerYamlHandler() # Enable YAML serialization/deserialization\n\n# YAML serialization:\nconfigDict = {\n    \"dispatcher\": {\n        \"name\": \"queuePublisher\",\n        \"delay\": Seconds(10)\n    }\n}\nconfigYaml = yaml.dump(configDict)\nprint(configYaml)\n# Prints:\n# dispatcher:\n#     name: queuePublisher\n#     delay: 10s\n\n# YAML deserialization:\nconfigYaml = \"\"\"\ndispatcher:\n    name: queuePublisher\n    delay: 10s\n\"\"\"\nconfigDict = yaml.load(configYaml, Loader)\nfrom pprint import pprint\npprint(configYaml, width=30)\n# Prints:\n# {'dispatcher': {'delay': Seconds(10),\n#                 'name': 'queuePublisher'}}\n```\n\n## Changelog:\n- Version: 1.7.0\n    - Made markup serialization/deserialization into installable extras.\n    - Added support for YAML markup.\n- Version: 1.6.0\n    - Using `json-handler-registry` for JSON serialization/deserialization.\n- Version: 1.5.1\n    - Deep JSON serialization/deserialization fix.\n- Version: 1.5.0\n    - JSON serialization/deserialization support.\n- Version: 1.4.0\n    - MyPy static analysis support.\n- Version: 1.2.0\n    - Float conversions.\n\n## License\nMIT\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Time unit types. For transparency safety and readability.",
    "version": "1.7.2",
    "project_urls": {
        "Repository": "https://bitbucket.org/massultidev/tunit/"
    },
    "split_keywords": [
        "time",
        " units",
        " timestamp",
        " chrono",
        " seconds",
        " milliseconds"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8c91f3bdae396c7d797d6fb7930a205aeae617149a690faf51d198f220d494c0",
                "md5": "b64cba5c625246e9e577c128ccfa37c0",
                "sha256": "e7d5019c7959070789ebbe57ad9d0453dc8efe4d917b6a03ab60d4efa69847f6"
            },
            "downloads": -1,
            "filename": "tunit-1.7.2.tar.gz",
            "has_sig": false,
            "md5_digest": "b64cba5c625246e9e577c128ccfa37c0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 7857,
            "upload_time": "2024-04-07T17:05:24",
            "upload_time_iso_8601": "2024-04-07T17:05:24.987600Z",
            "url": "https://files.pythonhosted.org/packages/8c/91/f3bdae396c7d797d6fb7930a205aeae617149a690faf51d198f220d494c0/tunit-1.7.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-07 17:05:24",
    "github": false,
    "gitlab": false,
    "bitbucket": true,
    "codeberg": false,
    "bitbucket_user": "massultidev",
    "bitbucket_project": "tunit",
    "lcname": "tunit"
}
        
Elapsed time: 2.59420s