Skip to main content

Python library that models available periods in a week

Project description

weekdaytime: modelling availability within a week

This library includes Python classes for modelling available periods within a week.

Key features

  • Simple checking of whether visiting period is within an available period. The expression
    visit_period in available_period
    
    evaluates to True if and only if visit_period is within available_period.
  • Manipulate period objects with bitwise operators (|, &, ~, ^)
  • Convert period string into a period object, and vise versa (e.g., '09:00~15:00,17:00~21:00;15:00~02:00(Fri,Sat)')

Installation

  1. Python version must not be less than 3.6.
  2. The dependent library bitarray requires Microsoft Visual C++ 14.0. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/
  3. Install with pip using the command
    pip install weekdaytime
    

Usage

weekdaytime class

A weekdaytime instance specifies a point in time in a week.

from weekdaytime import weekdaytime as wdt

my_wdt = wdt(weekday=1, hour=9, minute=0)
print(my_wdt.weekday)     # 1
print(my_wdt.hour)        # 9
print(my_wdt.minute)      # 0
print(my_wdt)             # 'Mon 09:00'
print(my_wdt.min_of_week) # 1980

my_wdt.add(
    day=1, 
    hour=2, 
    minute=30
)
print(my_wdt)             # 'Tue 11:30'

period class

A period instance specifies the available periods within a week. Compatibility of period instances can be checked using the in keyword.

from weekdaytime import period
from weekdaytime import weekdaytime as wdt

# Available from Mon 09:00 to Mon 15:30, and from Mon 17:30 to Mon 21:00
mon0900 = wdt(1, 9, 0)
mon1530 = wdt(1, 15, 30)
mon1730 = wdt(1, 17, 30)
mon2100 = wdt(1, 21, 0)
avail = period(mon0900, mon1530, mon1730, mon2100)
print(avail)             # '09:00~15:30,17:30~21:00(Mon)'

# Visiting period from Mon 10:00 to Mon 12:00
mon1000 = wdt(1, 10, 0)
mon1200 = wdt(1, 12 ,0)
visit_1 = period(mon1000, mon1200)
print(visit_1)           # '10:00~12:00(Mon)'
print(visit_1 in avail)  # True

# Visiting period from Mon 15:00 to Mon 17:00
mon1500 = wdt(1, 15, 0)
mon1700 = wdt(1, 17 ,0)
visit_2 = period(mon1500, mon1700)
print(visit_2)           # '15:00~17:00(Mon)'
print(visit_2 in avail)  # False

A period can have arbitrary number of discontinuous segments and can be manipulated using boolean operators.

from weekdaytime import period
from weekdaytime import weekdaytime as wdt

# Another way to instantiate a period with regular hours 09:00~18:00 for Monday to Friday
avail_mon2fri = period.from_regulars(
    start_hour=9,
    start_minute=0,
    end_hour=18,
    end_minute=0,
    repeat_from=1,
    repeat_to=5
)
print(avail_mon2fri)   # '09:00~18:00(Mon,Tue,Wed,Thu,Fri)'

# Weekend hours are 12:00~17:00
avail_weekend = period(
    wdt(6, 12, 0), wdt(6, 17, 0),
    wdt(0, 12, 0), wdt(0, 17, 0)
)
print(avail_weekend)   # '12:00~17:00(Sun,Sat)'

avail_or = avail_mon2fri | avail_weekend  # or operator, add the hours together
print(avail_or)        # '09:00~18:00(Mon,Tue,Wed,Thu,Fri);12:00~17:00(Sun,Sat)'

avail_and = avail_mon2fri & avail_weekend  # intersection
print(avail_and)       # ''

avail_not = ~avail_or  # inverse, unavailable becomes available
print(avail_not)       # '00:00~09:00,18:00~24:00(Mon,Tue,Wed,Thu,Fri);00:00~12:00,17:00~24:00(Sun,Sat)'

avail_xor = avail_mon2fri ^ avail_weekend  # exclusive or
print(avail_xor)       # '09:00~18:00(Mon,Tue,Wed,Thu,Fri);12:00~17:00(Sun,Sat)'

A period can be instantiated by parsing a formatted string. Suppose we want to model shop with the following opening hours:

  • Default: 09:00~15:00,17:00~21:00
  • Wed: Not open
  • Fri,Sat: 12:00~02:00 (i.e., to 02:00 AM the following day)
  • Sun: 09:00~21:00
from weekdaytime import period

# Using a default string
avail_string = '09:00~15:00,17:00~21:00;00:00~00:00(Wed);12:00~02:00(Fri,Sat);09:00~21:00(Sun)'
p = period.strpperiod(avail_string)
print(p)  # '09:00~15:00,17:00~21:00(Mon,Tue,Thu);00:00~02:00,09:00~21:00(Sun);12:00~24:00(Fri);00:00~02:00,12:00~24:00(Sat)'

# Specifying all opening weekdays, the effect is the same
avail_string = '09:00~15:00,17:00~21:00(Mon,Tue,Thu);12:00~02:00(Fri,Sat);09:00~21:00(Sun)'
p = period.strpperiod(avail_string)
print(p)  # '09:00~15:00,17:00~21:00(Mon,Tue,Thu);00:00~02:00,09:00~21:00(Sun);12:00~24:00(Fri);00:00~02:00,12:00~24:00(Sat)'

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

weekdaytime-0.0.10.tar.gz (5.8 kB view details)

Uploaded Source

File details

Details for the file weekdaytime-0.0.10.tar.gz.

File metadata

  • Download URL: weekdaytime-0.0.10.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.8

File hashes

Hashes for weekdaytime-0.0.10.tar.gz
Algorithm Hash digest
SHA256 f338f4eea7e1add56ec8f2e17408def5aea49059db20e1bb630d76f8773c1727
MD5 c7774a95454c2b4a48de525042faba5b
BLAKE2b-256 3b7a4e78949a3a51d722659309b35d5c8e2028c04bd9006f5922d7e4bf180e3b

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