Skip to main content

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

Project description

CircleCI Alt text

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.every_nth_day(2).on_hour(8)
DailyRule: Starts on: 2014-01-01T08:00:00, repeats every 2 days at 8 o'clock, forever.
>>> rule.except_weekdays(*turoboro.WEEKEND)
DailyRule: Starts on: 2014-01-01T08:00:00, repeats every 2 days at 8 o'clock, forever. Except days Saturday, Sunday.
>>> rule.end_on(datetime(2014, 1, 31))
DailyRule: Starts on: 2014-01-01T08:00:00, repeats 2 days at 8 o'clock, until 2014-02-01T00:00:00. Except days Saturday, Sunday. 

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
... )

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)
DailyRule: Starts on: 2014-01-01T08:00:00, repeats 2 days at 8 o'clock, 100 times. Except days Saturday, Sunday.

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 return 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.2.tar.gz (7.2 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: turoboro-0.0.2.tar.gz
  • Upload date:
  • Size: 7.2 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.2.tar.gz
Algorithm Hash digest
SHA256 803cfa546349f843bf9793f5b72a941cfa588b7990c0aedae94dbfcf9811a546
MD5 a7961d45021a7c46c5a2a27e5545a231
BLAKE2b-256 85bd5f0b5442ec92ba03b4e308cd930b967235e97280edd6dcb5a45d8b50d2cf

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