Skip to main content

A tiny DSL for calendar interval algebra - compose, filter, and query time ranges using set operations.

Project description

calgebra 🗓️

Set algebra for calendars. Compose lazily and query efficiently.

Installation

pip install calgebra

# Or with Google Calendar support
pip install calgebra[google-calendar]

# Or with iCalendar (.ics) file support
pip install calgebra[ical]

Quick Start

from calgebra import day_of_week, time_of_day, hours, at_tz, pprint, HOUR
from itertools import islice

# Setup
tz = "US/Pacific"
at = at_tz(tz)

# 1. Define Availability
weekdays = day_of_week(
    ["monday", "tuesday", "wednesday", "thursday", "friday"],
    tz=tz
)
work_hours = time_of_day(start=9*HOUR, duration=8*HOUR, tz=tz)
business_hours = weekdays & work_hours

# 2. Define Constraints
lunch = time_of_day(start=12*HOUR, duration=1*HOUR, tz=tz) 
monday_sync = day_of_week("monday", tz=tz) & time_of_day(start=9*HOUR, duration=1*HOUR, tz=tz)
busy = lunch | monday_sync

# 3. Calculate Free Time
free_time = (business_hours - busy) & (hours >= 2)

# 4. Query (Jan 2025)
start, end = at("2025-01-01"), at("2025-02-01")

print("Forward options:")
pprint(islice(free_time[start:end], 5), tz=tz)

print("\nReverse options (Last 3 in Jan):")
pprint(islice(free_time[end:start:-1], 3), tz=tz)

Intervals use exclusive end bounds ([start, end)), matching Python slicing. Interval(start=10, end=13) represents 3 seconds. Intervals are automatically clipped to query bounds.

Core Features:

  • Set operations: | (union), & (intersection), - (difference), ~ (complement)
  • Recurring patterns: recurring(), day_of_week(), time_of_day() (RFC 5545 via python-dateutil)
  • Reverse iteration: timeline[end:start:-1] for reverse chronological order
  • Aggregations: total_duration, max_duration, min_duration, count_intervals, coverage_ratio
  • Transformations: buffer() (add time around intervals), merge_within() (coalesce nearby intervals)
  • Google Calendar: calgebra.gcsa.calendars() for read/write operations
  • iCalendar (.ics): Load/save timelines to standard RFC 5545 files

Quick-start | Tutorial | API Reference | Google Calendar

License

MIT License - see LICENSE file for details.

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

calgebra-0.9.1.tar.gz (100.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

calgebra-0.9.1-py3-none-any.whl (77.1 kB view details)

Uploaded Python 3

File details

Details for the file calgebra-0.9.1.tar.gz.

File metadata

  • Download URL: calgebra-0.9.1.tar.gz
  • Upload date:
  • Size: 100.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for calgebra-0.9.1.tar.gz
Algorithm Hash digest
SHA256 43409ab38cd55bae5eacdd53c86ef50a1c5f7eb568e0aaeeb0b6a27e7fc64c44
MD5 dcf34d368c37ae621762fec23421f992
BLAKE2b-256 6b2fff0fc9ed19960ddda21a6efed8b8621cb725222d57f5f33e814394f5e619

See more details on using hashes here.

File details

Details for the file calgebra-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: calgebra-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 77.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for calgebra-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fcd1a510eaf5a3b095f00e993fad244b67bd4a49f3bd26495175181aa8896575
MD5 22869c60e7ab6e531a5e6a8229ac827f
BLAKE2b-256 07a22013bd8112b991bb8208bfb6ad228c0d4cf9f71a5b4b67c214f299d111d2

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