Skip to main content

timespan and scheduling helpers for Python

Project description

>>> import datetime
>>> from timelines import timespan, timelayer

A timespan object has a start time and an end time. It can be created either by specifying a start time and an elapsed time, or by specifying both start and end times:

>>> span1 = timespan(datetime.datetime(1984, 11, 26), datetime.timedelta(1))
>>> span2 = timespan(datetime.datetime(1984, 11, 26) + datetime.timedelta(2), datetime.datetime(1984, 11, 26) + datetime.timedelta(2, 50))
>>> span1.start
datetime.datetime(1984, 11, 26, 0, 0)
>>> span1.elapsed
datetime.timedelta(1)

The elapsed time of a timespan is the timedelta between its start and end times:

>>> span1.elapsed == span1.end - span1.start
True

A timelayer object is a sorted bag of non-overlapping timespans which know the order in which they occur:

>>> layer = timelayer(span2, span1)
>>> list(layer) == [span1, span2]
True

Just like a timespan, a timelayer knows its own start and end times:

>>> layer.start == span1.start
True
>>> layer.end == span2.end
True

The elapsed duration of a timelayer is the sum of the elapsed durations of the timespans it contains, NOT the delta between its start and end times:

>>> layer.elapsed == span1.elapsed + span2.elapsed
True
>>> layer.elapsed == layer.end - layer.start
False

You can add new timespans to a timelayer:

>>> layer.add(timespan(datetime.datetime(1984, 11, 26) - datetime.timedelta(1), datetime.timedelta(0, 600)))

However, you cannot add new timespans which overlap any existing timespans:

>>> layer.add(timespan(datetime.datetime(1984, 11, 26) - datetime.timedelta(1), datetime.timedelta(2)))
Traceback (most recent call last):
...
RuntimeError

You can also add constraints to a timelayer. Constraints allow you to freeze the start time and/or end time of a timelayer. New timespans cannot be added to a layer if they fail its constraints:

>>> layer.freeze_start()
>>> layer.add(timespan(datetime.datetime(1984, 11, 26) - datetime.timedelta(3), datetime.timedelta(2)))
Traceback (most recent call last):
...
RuntimeError

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

timelines-0.1.tar.gz (2.2 kB view details)

Uploaded Source

File details

Details for the file timelines-0.1.tar.gz.

File metadata

  • Download URL: timelines-0.1.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for timelines-0.1.tar.gz
Algorithm Hash digest
SHA256 134ff7ef8eb21a1afc085613b4ad84b5b6d1043862f98c664e008dc2eb58a74e
MD5 a92c1a20f151a68f3fc55ebd3b3c0c09
BLAKE2b-256 0f8d1406149660089c5451f9e7adb37b46f96b14a45d1e098d10ac110f1b7f12

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