Skip to main content

A python library for specifying recurring time rules and getting timestamps in return.

Project description

CircleCI

turoboro

A python library for specifying recurring time rules and getting timestamps in return.

Typical usage would be to articulate the behaviour of recurring rules and to get exact points in time where those rules ought to be executed.

I.e this library answers the following questions:

Given a recurring rule,

  1. when is the next time it happens?
  2. when are all of the times it happens?
  3. when are n times it happens?

Say for example you want to find out at what specific UTC time the rule "every thursday of every third week of every second month at 8 o'clock UTC" would happen.

Ask turoboro!

However, this is very much work in progress. So... basically don't use this library yet.

Installation

$ pip install turoboro

Usage

turoboro allows you to set up recurring rules of three main times:

  1. With an end-date, or
  2. that end after a certain number of occurrences, or
  3. that are infinite.

All recurring rules must have a start datetime (defaults to now).

Lets go through them one at a time.

Daily rule examples

>>> import turoboro
>>> from datetime import datetime
>>> rule = turoboro.DailyRule(start=datetime(2014, 1, 1))
>>> rule
{"end": null, "every_nth_day": 1, "except_days": null, "except_months": null, "on_hour": 0, "repeat": null, "rule": "daily", "start": "2014-01-01T00:00:00+00:00", "timezone": "UTC"}
>>> rule.every_nth_day(2).on_hour(8)
{"end": null, "every_nth_day": 2, "except_days": null, "except_months": null, "on_hour": 8, "repeat": null, "rule": "daily", "start": "2014-01-01T08:00:00+00:00", "timezone": "UTC"}
>>> rule.except_weekdays(*turoboro.WEEKEND)
{"end": null, "every_nth_day": 2, "except_days": [5, 6], "except_months": null, "on_hour": 8, "repeat": null, "rule": "daily", "start": "2014-01-01T08:00:00+00:00", "timezone": "UTC"}
>>> rule.end_on(datetime(2014, 1, 31))
{"end": "2014-02-01T00:00:00+00:00", "every_nth_day": 2, "except_days": [5, 6], "except_months": null, "on_hour": 8, "repeat": null, "rule": "daily", "start": "2014-01-01T08:00:00+00:00", "timezone": "UTC"}

Alright, now we have an instance of the DailyRule class.

You can also instantiate the exact same thing using only a constructor:

>>> rule = turoboro.DailyRule(
...     datetime(2014, 1, 1), every_nth_day=2, except_weekdays=turoboro.WEEKEND,
...     end_on=datetime(2014, 1, 31), on_hour=8
... )

Or with a factory method using the "pure" json spec:

>>> rule = turoboro.Rule.from_json_spec({
    "end": "2014-02-01T00:00:00+00:00",
    "every_nth_day": 2,
    "except_days": [5, 6],
    "except_months": null,
    "on_hour": 8,
    "repeat": null,
    "rule": "daily",
    "start": "2014-01-01T08:00:00+00:00",
    "timezone": "UTC"
})

Lets see what actual times this resolves to.

>>> computed = rule.compute()
>>> computed.first
'2014-01-01T08:00:00'
>>> computed.last
'2014-01-31T08:00:00'
>>> computed.count
12
>>> computed.all
['2014-01-01T08:00:00', '2014-01-03T08:00:00', '2014-01-07T08:00:00', '2014-01-09T08:00:00',
 '2014-01-13T08:00:00', '2014-01-15T08:00:00', '2014-01-17T08:00:00', '2014-01-21T08:00:00',
 '2014-01-23T08:00:00', '2014-01-27T08:00:00', '2014-01-29T08:00:00', '2014-01-31T08:00:00']

As a convenience, you can get a handle on a generator function that will iterate through the entire set, as such:

>>> result = rule.result()
>>> next(result)
'2014-01-01T08:00:00'
>>> next(result)
'2014-01-03T08:00:00'
>>> [r for r in result]
['2014-01-07T08:00:00', '2014-01-09T08:00:00', '2014-01-13T08:00:00', '2014-01-15T08:00:00',
'2014-01-17T08:00:00', '2014-01-21T08:00:00', '2014-01-23T08:00:00', '2014-01-27T08:00:00',
'2014-01-29T08:00:00', '2014-01-31T08:00:00']

Variations of the daily rule

You don't have to provide an end date - you can instead provide a number of occurrences or in the lingo of turoboro: repeat_n_times. As such:

>>> rule.repeat_n_times(10)
# ValueError: You may not specify both an end date and a repeat count

Oops, lets first kill the end date, (because lets be explicit with our intentions.)

>>> rule.end_on(None).repeat_n_times(100)
{"end": null, "every_nth_day": 2, "except_days": [5, 6], "except_months": null, "on_hour": 8, "repeat": 100, "rule": "daily", "start": "2014-01-01T08:00:00+00:00", "timezone": "UTC"}

You can also let your rule be infinite by omitting to provide an end date or a number of occurrences. The computed.first, computed.last and computed.all attributes will still behave as if the result is a bounded set (defaults to 100 occurrences). However, by using the rule.result() generator function you can iterate forward beyond the bounds that rule.compute() would give. Not infinitely far though, so iterating through the generator will eventually come to a stop, how quickly depends on the batch size that you specify with the keyword max_count_if_infinite. A max_count_if_infinite=2 will likely give you results several years into the future.

>>> result = rule.result(max_count_if_infinite=2)
>>> [r for r in result] # 1974 results

In essence - iterating through an infinite set of datetimes isn't very useful. Since, well... they will never end. However, once you have an "infinite" rule you can always find the next upcoming set of valid datetimes by simply telling compute or result and specify the from_dt parameter, such as:

>>> result = rule.result(from_dt=datetime.utcnow())
>>> next(result)
'2019-06-26T08:00:00'

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

turoboro-0.0.3.tar.gz (10.4 kB view details)

Uploaded Source

File details

Details for the file turoboro-0.0.3.tar.gz.

File metadata

  • Download URL: turoboro-0.0.3.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

File hashes

Hashes for turoboro-0.0.3.tar.gz
Algorithm Hash digest
SHA256 227d13f426005a2c0c208a7c195b2b41d7a8576262ecf5e9d99ccaea3023547e
MD5 68c0965b2c085e3fd0579d2f8f72674e
BLAKE2b-256 5259190f62abded9eec64ca90feb4e587bad5206c675db3e06f9046f791e075a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page