A tiny DSL for calendar interval algebra - compose, filter, and query time ranges using set operations.
Project description
calgebra 🗓️
A tiny DSL for merging and searching over calendar-like intervals.
Installation
pip install calgebra
# Or with Google Calendar support
pip install calgebra[google-calendar]
Quick Start
from datetime import datetime, timezone
from calgebra import day_of_week, time_of_day, hours, HOUR
# Compose time windows from primitives
weekdays = day_of_week(["monday", "tuesday", "wednesday", "thursday", "friday"])
work_hours = time_of_day(start=9*HOUR, duration=8*HOUR, tz="US/Pacific")
business_hours = weekdays & work_hours
# Union: combine busy times (in practice: from Google Calendar, databases, etc.)
monday_meetings = day_of_week("monday") & time_of_day(start=10*HOUR, duration=2*HOUR)
friday_focus = day_of_week("friday") & time_of_day(start=14*HOUR, duration=3*HOUR)
busy = monday_meetings | friday_focus
# Difference: find free time during business hours
free = business_hours - busy
# Filter: only slots >= 2 hours
long_slots = free & (hours >= 2)
# Fetch results with slice notation (supports int, datetime, or date)
start = datetime(2025, 1, 1, tzinfo=timezone.utc)
end = datetime(2025, 1, 31, tzinfo=timezone.utc)
meeting_options = list(long_slots[start:end])
Intervals in calgebra are inclusive of both start and end—durations therefore reflect every second covered by an interval. Timeline slices accept integer seconds (Unix timestamps), timezone-aware datetime objects, or date objects. When you subclass Interval, define your subclass as a dataclass (ideally frozen=True) so the algebra can clone and clamp events internally.
Common helpers and aggregates are exposed alongside the core DSL:
Recurring Patterns (RFC 5545 via python-dateutil):
recurring(freq, ...)generates intervals based on recurrence rules (weekly, bi-weekly, monthly, etc.)day_of_week(days, tz)convenience wrapper for filtering by day(s) of weektime_of_day(start, duration, tz)convenience wrapper for daily time windowsHOUR,MINUTE,DAY,SECONDconstants for readable time specifications- Compose with
&to create complex patterns like business hours, recurring meetings, etc.
Aggregation & Analysis:
flatten(timeline)converts overlapping/adjacent spans into a coalesced timeline (returns maskIntervals and must be sliced with explicit bounds)union(*timelines)/intersection(*timelines)offer functional set operationstotal_durationsums inclusive coverage inside a windowmax_duration/min_durationfind the longest or shortest clamped intervalscount_intervalstallies events over a slicecoverage_ratioreports utilization as a 0–1 fraction
Transformations:
buffer(timeline, before, after)adds buffer time around each interval (useful for travel time, setup/teardown)merge_within(timeline, gap)coalesces intervals separated by at mostgapseconds (useful for grouping related events)
Integrations:
calgebra.gcsa.Calendarprovides Google Calendar integration with timezone normalization and automatic paging; it assumes locally stored OAuth credentials
→ Read the full tutorial for a complete guide to the DSL
→ API Reference for detailed function signatures and parameters
Status
calgebra is in beta (v0.1.0). The core API is stable and ready for use. Feedback and contributions are welcome!
License
MIT License - see LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file calgebra-0.2.0.tar.gz.
File metadata
- Download URL: calgebra-0.2.0.tar.gz
- Upload date:
- Size: 44.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38980bb7ddef039b91a5c3427d8a03936f31481323ff9294f62c90771063ef8c
|
|
| MD5 |
2adc1acbe3a6e7b76b6cd0781a40db02
|
|
| BLAKE2b-256 |
a6e88b9072ccecb54cc2f1ddd99b6214d55b1cf857a382771b8a8bdac273a42a
|
File details
Details for the file calgebra-0.2.0-py3-none-any.whl.
File metadata
- Download URL: calgebra-0.2.0-py3-none-any.whl
- Upload date:
- Size: 34.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5be0a11f307abd21cb1bc3ebfd20ff883b456bd685259ea663be3b46ebf047d
|
|
| MD5 |
dd32dd75c200b463f6a96763fcb3c7b9
|
|
| BLAKE2b-256 |
7b91fc86aea833bc936fdbf81082967c8dcc73ff16903538af639254852a1d3a
|