easy-leasy


Nameeasy-leasy JSON
Version 1.0.1 PyPI version JSON
download
home_pageNone
SummaryEasy Leasy is a time set manipulation library
upload_time2024-11-06 19:31:27
maintainerNone
docs_urlNone
authorRémy Sanchez
requires_python<4.0,>=3.10
licenseWTFPL
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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.

```python
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:

```python
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.

```python
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")))
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "easy-leasy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "R\u00e9my Sanchez",
    "author_email": "remy.sanchez@hyperthese.net",
    "download_url": "https://files.pythonhosted.org/packages/db/6b/1c663908a06a7c44d174aa48a6b4882055d7f9f5480d7aa81d0f537c7be2/easy_leasy-1.0.1.tar.gz",
    "platform": null,
    "description": "# Easy Leasy\n\nEasy Leasy is a Python library for defining time-based rules and manipulating\ntime sets.\n\nThe idea is to express easily things like \"open from 9am to 5pm from Monday to\nFriday\" as a single expression, and then use it both to know if a given time\nis in the time set, and to compute the next time change.\n\n## Usage\n\nYou can use the `parse_easy_leasy` function to parse an expression into a\n`BaseTimeSet` object, which will allow you query that time set.\n\n```python\nfrom easy_leasy import parse_easy_leasy\nfrom datetime import datetime\nfrom zoneinfo import ZoneInfo\n\np = parse_easy_leasy(\n    \"\"\"\n    from context import has_pr\n\n    let work_hour be when 9:30~13:30 | 14:30~18:30\n    let work_day be when mon | tue | wed | thu | fri\n    let business_hour be when work_hour & work_day\n\n    let xmas be when 25 & dec\n    let ny be when 1 & jan\n    let work be when 1 & may\n    let holiday be when xmas | ny | work\n\n    return business_hour - holiday\n    \"\"\",\n    dict(has_pr=True),\n)\n\nnow = datetime.now(tz=ZoneInfo(\"Europe/Paris\"))\nprint(p.value_at(now))\nprint(p.next_change(now))\n```\n\nThe `parse_easy_leasy` function takes two arguments: the expression to parse,\nand a dictionary of context variables which can either be a boolean or a\n`BaseTimeSet` object.\n\n## Language reference\n\nThe language is simple and has 3 different statements:\n\n- `from <namespace> import <name>`: import a variable from the context.\n- `let <name> be when <expression>`: declare a new variable `<name>` with the \n  value of `<expression>`.\n- `return <expression>`: return the value of `<expression>`.\n\nThe only statement that is mandatory is `return`, which is the expression that\nwill be evaluated to determine the value of the time set.\n\nExpressions are composed of the different types:\n\n- **Hour ranges**: `hour:minute~hour:minute`\n- **Days of the week**: `mon | tue | wed | thu | fri | sat | sun`\n- **Days of the month**: `1 | 2 | ... | 31`\n- **Months**: `jan | feb | ... | dec`\n- **Absolutes**: `always | never`\n\nThese expressions can be combined using the same operators as the Python sets:\n\n- `|`: union\n- `&`: intersection\n- `-`: difference\n- `~`: complement\n\n## API reference\n\nEverything is based on the `BaseTimeSet` class, which can also be used directly\nfrom Python.\n\nIt has different implementations, which are:\n\n- `Always`: always true\n- `Never`: always false\n- `HourRange`: a range of hours\n- `Day`: a single day of the week\n- `WeekDay`: a single day of the month\n- `Month`: a single month\n- `Constant`: a constant value\n\nAs they are subclasses of `BaseTimeSet`, they can be used with the standard\nPython set operators, for example:\n\n```python\nfrom easy_leasy import Always, Month\n\np = Always() & Month('jan')\n```\n\nOnce you have a `BaseTimeSet` object, you can use the `value_at` and\n`next_change` methods to query the time set.\n\n```python\nfrom easy_leasy import Always\nfrom datetime import datetime\nfrom zoneinfo import ZoneInfo\n\np = Always()\n\n# Check if the time set is true at a given instant\np.value_at(datetime.now(tz=ZoneInfo(\"Europe/Paris\")))\n\n# Get the next time change\np.next_change(datetime.now(tz=ZoneInfo(\"Europe/Paris\")))\n```\n",
    "bugtrack_url": null,
    "license": "WTFPL",
    "summary": "Easy Leasy is a time set manipulation library",
    "version": "1.0.1",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9309f385bb2baf2a995bd5ca577a1f8c5a8154b884d3de8125bdcb96b270c9ee",
                "md5": "1038a04015783784c2d55287e1c2c2d3",
                "sha256": "87c71220f3a38052d3980f76dc3e05327bc4b02bea42773cf0d63f43ac761295"
            },
            "downloads": -1,
            "filename": "easy_leasy-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1038a04015783784c2d55287e1c2c2d3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 8341,
            "upload_time": "2024-11-06T19:31:26",
            "upload_time_iso_8601": "2024-11-06T19:31:26.473538Z",
            "url": "https://files.pythonhosted.org/packages/93/09/f385bb2baf2a995bd5ca577a1f8c5a8154b884d3de8125bdcb96b270c9ee/easy_leasy-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "db6b1c663908a06a7c44d174aa48a6b4882055d7f9f5480d7aa81d0f537c7be2",
                "md5": "09fecdfa1696d120ba1ad2281b838465",
                "sha256": "bcc85a94c6bba44c893a13382ca681ff3a1940f71b9c8d36400f4ba27e1166a8"
            },
            "downloads": -1,
            "filename": "easy_leasy-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "09fecdfa1696d120ba1ad2281b838465",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 8464,
            "upload_time": "2024-11-06T19:31:27",
            "upload_time_iso_8601": "2024-11-06T19:31:27.908658Z",
            "url": "https://files.pythonhosted.org/packages/db/6b/1c663908a06a7c44d174aa48a6b4882055d7f9f5480d7aa81d0f537c7be2/easy_leasy-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-06 19:31:27",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "easy-leasy"
}
        
Elapsed time: 1.27996s