Skip to main content

Easy Leasy is a time set manipulation library

Project description

Easy Leasy

Easy Leasy is a Python library for defining time-based rules and manipulating time sets.

The idea is to express easily things like "open from 9am to 5pm from Monday to Friday" as a single expression, and then use it both to know if a given time is in the time set, and to compute the next time change.

Usage

You can use the parse_easy_leasy function to parse an expression into a BaseTimeSet object, which will allow you query that time set.

from easy_leasy import parse_easy_leasy
from datetime import datetime
from zoneinfo import ZoneInfo

p = parse_easy_leasy(
    """
    from context import has_pr

    let work_hour be when 9:30~13:30 | 14:30~18:30
    let work_day be when mon | tue | wed | thu | fri
    let business_hour be when work_hour & work_day

    let xmas be when 25 & dec
    let ny be when 1 & jan
    let work be when 1 & may
    let holiday be when xmas | ny | work

    return business_hour - holiday
    """,
    dict(has_pr=True),
)

now = datetime.now(tz=ZoneInfo("Europe/Paris"))
print(p.value_at(now))
print(p.next_change(now))

The parse_easy_leasy function takes two arguments: the expression to parse, and a dictionary of context variables which can either be a boolean or a BaseTimeSet object.

Language reference

The language is simple and has 3 different statements:

  • from <namespace> import <name>: import a variable from the context.
  • let <name> be when <expression>: declare a new variable <name> with the value of <expression>.
  • return <expression>: return the value of <expression>.

The only statement that is mandatory is return, which is the expression that will be evaluated to determine the value of the time set.

Expressions are composed of the different types:

  • Hour ranges: hour:minute~hour:minute
  • Days of the week: mon | tue | wed | thu | fri | sat | sun
  • Days of the month: 1 | 2 | ... | 31
  • Months: jan | feb | ... | dec
  • Absolutes: always | never

These expressions can be combined using the same operators as the Python sets:

  • |: union
  • &: intersection
  • -: difference
  • ~: complement

API reference

Everything is based on the BaseTimeSet class, which can also be used directly from Python.

It has different implementations, which are:

  • Always: always true
  • Never: always false
  • HourRange: a range of hours
  • Day: a single day of the week
  • WeekDay: a single day of the month
  • Month: a single month
  • Constant: a constant value

As they are subclasses of BaseTimeSet, they can be used with the standard Python set operators, for example:

from easy_leasy import Always, Month

p = Always() & Month('jan')

Once you have a BaseTimeSet object, you can use the value_at and next_change methods to query the time set.

from easy_leasy import Always
from datetime import datetime
from zoneinfo import ZoneInfo

p = Always()

# Check if the time set is true at a given instant
p.value_at(datetime.now(tz=ZoneInfo("Europe/Paris")))

# Get the next time change
p.next_change(datetime.now(tz=ZoneInfo("Europe/Paris")))

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

easy_leasy-1.0.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

easy_leasy-1.0.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file easy_leasy-1.0.1.tar.gz.

File metadata

  • Download URL: easy_leasy-1.0.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.5 Linux/6.1.0-1027-oem

File hashes

Hashes for easy_leasy-1.0.1.tar.gz
Algorithm Hash digest
SHA256 bcc85a94c6bba44c893a13382ca681ff3a1940f71b9c8d36400f4ba27e1166a8
MD5 09fecdfa1696d120ba1ad2281b838465
BLAKE2b-256 db6b1c663908a06a7c44d174aa48a6b4882055d7f9f5480d7aa81d0f537c7be2

See more details on using hashes here.

Provenance

File details

Details for the file easy_leasy-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: easy_leasy-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.5 Linux/6.1.0-1027-oem

File hashes

Hashes for easy_leasy-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 87c71220f3a38052d3980f76dc3e05327bc4b02bea42773cf0d63f43ac761295
MD5 1038a04015783784c2d55287e1c2c2d3
BLAKE2b-256 9309f385bb2baf2a995bd5ca577a1f8c5a8154b884d3de8125bdcb96b270c9ee

See more details on using hashes here.

Provenance

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