Skip to main content

A Python package to parse human readable lengths of time.

Project description

timelength

A Python package to parse human readable lengths of time, including long form durations such as 1 day, 5 hours, and 30 seconds, short form durations such as 1d5h30s, a mix thereof such as 1 day 5h 30s, and numerals such as half a day and twelve hours.

Installation

timelength can be installed via pip:

pip install timelength

Or added to your project via poetry:

poetry add timelength

Usage (English)

Default

While TimeLength.strict is False (default), TimeLength.result.success will be True if at least one valid result is found, regardless of invalid results.

from timelength import TimeLength

output = TimeLength("1d5h25m15.5s and 23miles")
print(output.result.success)
# True
print(output.result.seconds)
# 105915.5
print(output.to_minutes(max_precision = 3))
# 1765.258
print(output.result.invalid)
# [('miles', 'UNKNOWN_TERM'), (23.0, 'LONELY_VALUE')]
print(output.result.valid)
# [(1.0, Scale(86400.0, "day", "days")), (5.0, Scale(3600.0, "hour", "hours")), (25.0, Scale(60.0, "minute", "minutes")), (15.5, Scale(1.0, "second", "seconds"))]

Additionally, if a single lone value is parsed without a paired scale, seconds will be assumed. However, if more than one value is parsed, nothing will be assumed.

output = TimeLength("45")
print(output.result.invalid)
# []
print(output.result.valid)
# [(45.0, Scale(1.0, "second", "seconds"))]

output = TimeLength("45 minutes, 33")
print(output.result.invalid)
# [(33.0, 'LONELY_VALUE')]
print(output.result.valid)
# [(45.0, Scale(60.0, "minute", "minutes"))]

Strict

While TimeLength.strict is True, TimeLength.result.success will only be True if at least one valid result is found and no invalid results are found.

from timelength import TimeLength

output = TimeLength("3.5d, 35m, 19", strict = True)
print(output.result.success)
# False
print(output.result.invalid)
# [(19.0, "LONELY_VALUE")]
print(output.result.valid)
# [(3.5, Scale(86400.0, "day", "days")), (35.0, Scale(60.0, "minute", "minutes"))]

Additionally, unlike with the default behavior, scales must be present. No assumptions will be made.

Supported Locales

  1. English
  2. Spanish
  3. Basic Custom (Copy & modify an existing config with new terms as long as your new Locale follows the existing config parser's grammar structure)
  4. Advanced Custom (Write your own parsing logic if your Locale's grammar structure differs too drastically) (PRs welcome)

Customization

timelength allows for customizing the parsing behavior through JSON configuration. To get started, copy an existing locale JSON in timelength/locales/. The custom JSON may be placed anywhere.

Ensure the JSON being used is from a trusted source, as the parser is loaded dynamically based on the file specified in the JSON. This could allow for unintended code execution if an unsafe config is loaded.

Valid JSONs must include the following keys, even if their contents are empty:

  • connectors
    • Characters/phrases that join two parts of the same segment.
  • segmentors
    • Characters/phrases that join two segments together.
  • allowed_terms
    • Characters or terms that won't be categorized as an invalid input. If sent multiple times in a row (ex: !!), they will still be marked as invalid.
  • decimal_separators
    • Characters used to separate decimals from digits. Can't have overlap with thousand_separators.
  • thousand_separators
    • Characters used to break up large numbers. Can't have overlap with decimal_separators.
  • parser_file
    • The name of this locale's parser file (extension included) located in timelength/parsers/, or the path to the parser file if stored elsewhere.
    • Ensure only a trusted file is used as this could allow unintended code execution.
    • The internal parser method must share a name with the file.
  • numerals
    • Word forms of numbers. May be populated or left empty. Each element must itself have the following keys, even if their contents are not used:
      • type
        • The numeral type.
      • value
        • The numerical value of this numeral.
      • terms
        • Characters/phrases that parse to this numeral's value.
  • scales
    • Periods of time. The defaults are millisecond, second, minute, hour, day, week, month, year, decade, and century. Default scales can be disabled by removing their entry completely. In their place an empty scale with no terms will be created. Custom scales can be added following the format of the others. The following keys must be present and populated:
      • scale
        • The number of seconds this scale represents.
      • singular
        • The lowercase singular form of this scale.
      • plural
        • The lowercase plural form of this scale.
      • terms
        • All terms that could be parsed as this scale. Accents and other NFKD markings should not be present as they are filtered from the user input.
  • extra_data
    • Any data a parser needs that is not already covered. May be populated or left empty. The locale loads this into a Locale._extra_data attribute, leaving the parser to utilize it.

Once your custom JSON is filled out, you can use it as follows:

from timelength import TimeLength, CustomLocale

output = TimeLength("30 minutes", locale = CustomLocale("path/to/config.json"))

If all goes well, the parsing will succeed, and if not, an error will point you in the right direction.

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

timelength-2.0.5.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

timelength-2.0.5-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file timelength-2.0.5.tar.gz.

File metadata

  • Download URL: timelength-2.0.5.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.4 Windows/10

File hashes

Hashes for timelength-2.0.5.tar.gz
Algorithm Hash digest
SHA256 7e70b4fc83479f7c2994497933bd891d32bb76ebf2321530be4655e6ef12e4c7
MD5 1cc72f3ec31890903d363f614e6ac1fc
BLAKE2b-256 2b6616424258b2a82ff288c4605a983853d33ba7988e293ab38fc688a66ba2f4

See more details on using hashes here.

File details

Details for the file timelength-2.0.5-py3-none-any.whl.

File metadata

  • Download URL: timelength-2.0.5-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.4 Windows/10

File hashes

Hashes for timelength-2.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 1782656eac1225dc423b9e66cb8ca411e0c8c7d871e089e787094822d1049e81
MD5 9e224dabac48bf412f879141e0717323
BLAKE2b-256 7adc1481bf02cb975a809975862028a4b55682ada4d97031142d9c23de4cb96e

See more details on using hashes here.

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