Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Project: aiotense
License: Apache 2.0
About: Time Processing Tool
OS: Independent
Python: 3.9+
Typing: Typed
Topic: Utilities


Documentation · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Examples
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

PyPi Mypy badge Black Supported python versions

Welcome

Have you ever needed to convert, for example, the string "1d1minute 2 sec" to the number of seconds or a datetime.timedelta object?

No? Then advise us to your friends :) And if you really need our tool - let's move on!

Getting started

With PyPi

$ pip3 install aiotense

With Poetry

$ poetry add aiotense

Usage

Built-in basic

import asyncio
import datetime
from aiotense import TenseParser

time_string = "1d2minutes 5 sec"

# <-- Digit parser -->
digit_parser = TenseParser(TenseParser.DIGIT)
digit_value = asyncio.run(digit_parser.parse(time_string))
# <-- Assertions -->
assert digit_value == 86525

# <-- Timedelta parser -->
delta_parser = TenseParser(TenseParser.TIMEDELTA)
delta_value = asyncio.run(delta_parser.parse(time_string))
# <-- Assertions -->
assert isinstance(delta_value, datetime.timedelta)
assert str(delta_value) == "1 day, 0:02:05"

Reconfiguring existing settings

import asyncio
from aiotense import TenseParser, from_tense_file_source

config_emulation = """
[model.Tense]
multiplier = 2  # each unit of time will be multiplied by 2
# !!! Note: If the multiplier is <= 0, then the parsers will 
# not work correctly. In this case, a warning will be sent to the console.

[units.Minute]
duration = 120  # Why not?...
aliases = my_minute, my_minutes, my_min, my_mins
"""
parser = TenseParser(
    TenseParser.TIMEDELTA,
    config=from_tense_file_source(config_emulation),
)
delta_value = asyncio.run(parser.parse("1 my_min 10my_mins 9  my_minutes"))
# <-- Assertions -->
assert str(delta_value) ==  "1:20:00" # (each 120 * 2)

Adding new settings

import asyncio
from aiotense import TenseParser, from_tense_file_source

config_emulation = """
[model.Tense]  # This header is required.

[virtual]
duration = exp(year * 10)
aliases = decade, dec, decs, decades
"""

parser = TenseParser(
    TenseParser.TIMEDELTA,
    config=from_tense_file_source(config_emulation),
)
delta_value = asyncio.run(parser.parse("1year 10 decades5   seconds"))
# <-- Assertions -->
assert str(delta_value) == "36865 days, 0:00:05"

FAQ

But what if you need to parse a string like: "1day and 10 minutes + 5 seconds"? Let's see:

>>> import asyncio
>>> from aiotense import TenseParser

>>> complex_string = "1day and 10 minutes + 5 seconds"

>>> parser = TenseParser(TenseParser.TIMEDELTA)
>>> asyncio.run(parser.parse(complex_string))
'0:00:05'

Wait... What? 5 second? But there are days and minutes...

  • It's okay, you're using flexible aiotense! This problem is solved in two ways:
    1. You write your own time_resolver and pass it
    2. Choose an existing one from aiotense.resolvers

Let's demonstrate! I will use the second option, since the built-in time resolvers in aiotense are suitable for me.

>>> import asyncio
>>> from aiotense import TenseParser, resolvers

>>> complex_string = "1day and 10 minutes + 5 seconds"

>>> parser = TenseParser(TenseParser.TIMEDELTA, time_resolver=resolvers.smart_resolver)
>>> asyncio.run(parser.parse(complex_string)) 
'1 day, 0:10:05'

Well, that's better!

aiotense.application.resolvers.smart_resolver() is also case insensitive!

>>> import asyncio
>>> from aiotense import TenseParser, resolvers

>>> complex_string = "1DAY and 10 MINUTES + 5 SECONDS"

>>> parser = TenseParser(TenseParser.TIMEDELTA, time_resolver=resolvers.smart_resolver)
>>> asyncio.run(parser.parse(complex_string)) 
'1 day, 0:10:05'

Examples.

If you think that this is where the possibilities of aiotense ends, then you are wrong! The possibilities of aiotense are too many for a README, so I suggest you move on to viewing the usage examples here:


Aiotense Examples

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the Apache 2.0 License. See LICENSE for more information.

Contact

Acknowledgments

Metadata

Release files for aiotense 0.0.3a0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiotense 0.0.3a0
File Size Uploaded Provenance Actions
aiotense-0.0.3a0.tar.gz 1.2 MB Without provenance Details for aiotense-0.0.3a0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aiotense 0.0.3a0
File Interpreter ABI Platform Actions
aiotense-0.0.3a0-py3-none-any.whl Python 3 none any Details for aiotense-0.0.3a0-py3-none-any.whl

Total release size: 1.3 MB

Release files / aiotense-0.0.3a0.tar.gz

Download URL aiotense-0.0.3a0.tar.gz
Size 1.2 MB
Tags Source
SHA-256 checksum
How to use checksums
bee2a14b8374719a092c2afb234df8e1d019459352c9eece183622351bf812c2
BLAKE2b-256 checksum
How to use checksums
dc4476f79754885bb4ed5f461b0085670348a72f73cded67da94c7c1a34765a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.4 Windows/10

Release files / aiotense-0.0.3a0-py3-none-any.whl

Download URL aiotense-0.0.3a0-py3-none-any.whl
Size 38.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e593bb2a6953502ecd20eb5a1172b2e9cf811e548e7f73fe8cb39a13918a569
BLAKE2b-256 checksum
How to use checksums
f85cfc00d92cdd3c50338e8f6a045c2fbad8a704f900d3d0ddcc9063d35cef01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.4 Windows/10

Security for aiotense 0.0.3a0

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page