Skip to main content

A python durations parsing library.

Project description

durations_nlp

CircleCI codecov Downloads

A python durations parsing library, providing a straight-forward API to parse duration string representations such as 1d, 1 day 2 hours or 2 days 3h 26m 52s and convert them to numeric values.

What and Why

It's easier and more straight forward to read a duration expressed in natural language (at least for a human), as an expression rather than an amount. When writing configuration files for example:

interval: 3 hours

is easier to understand for a human than

interval: 10800  # seconds

Installation

durations_nlp can be installed via pip:

$ pip install durations_nlp

Usage

To parse a duration string representation, just instantiate a Duration object and let it work for you. A Duration representation is composed of as many <value><scale> pairs as you need to express it:

  • A value is an integer amount.
  • A scale is a duration unit in it's short or long form (both singular and plural).
  • Duration pairs can be separated with sep characters and expressions such as , or and

Example Input

  • 1d
  • 2 days
  • 2 days and 4 hours
  • 4M, 22d and 6hours

Scales Reference

  • Century: c, century, centuries
  • Decade: D, decade, decades
  • Year: y, year, Year
  • Month: M, month, months
  • Week: w, week, weeks
  • Day: d, day, days
  • Hour: h, hour, hours
  • Minute:m, minute, minutes
  • Second: s, second, seconds
  • Milisecond: ms, milisecond, miliseconds

Usage Example

from durations_nlp import Duration

one_hour = "1hour"

one_hour_duration = Duration(one_hour)
one_hour_duration.to_seconds()
# >>> 3600.0
one_hour_duration.to_minutes()
# >>> 60.0

# You can even compose durations in their short
# and long variations
two_days_three_hours = "2 days, 3h"
two_days_three_hours_duration = Duration(two_days_three_hours)
two_days_three_hours_duration.to_seconds()
# >>> 183600.0
two_days_three_hours_duration.to_hours()
# >>> 51.0

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

durations_nlp-0.4.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

durations_nlp-0.4.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file durations_nlp-0.4.1.tar.gz.

File metadata

  • Download URL: durations_nlp-0.4.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.3 Linux/4.15.0-1052-aws

File hashes

Hashes for durations_nlp-0.4.1.tar.gz
Algorithm Hash digest
SHA256 158211e45f46a6e219a17c26d5c887a93ee3aa2e788cfa57fe7f00d567f648b2
MD5 28efd01e88b323311ac96788dd297d83
BLAKE2b-256 4e56b8b71df23e4dfa8ef3dd78be124396bebe7e97dc18425eab3598a242aa53

See more details on using hashes here.

File details

Details for the file durations_nlp-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: durations_nlp-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.3 Linux/4.15.0-1052-aws

File hashes

Hashes for durations_nlp-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 503b54ef6ff9ff9839aeaa1bf5721b6d9dd8c6f0d0f3af4033bc9301df8534b6
MD5 f350f88670765bd61aca84df427ad390
BLAKE2b-256 8da6461bea7162373be196320ca783280777a400059ac2fdda0a8c20eb9a6936

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