Skip to main content

Parses opening hours from various human-readable strings into a standard JSON format

Project description

Python Opening Hours parser

CircleCI codecov Downloads

This library parses opening hours from various human-readable strings such as "Mon- Fri 9:00am - 5:30pm" into a more standard JSON format that can be processed more easily.

The format

opening_hours = [
	{
		"day": "monday",
		"opens": "9:00",
		"closes": "17:00"
	},
	//..
]

Installation

pip install jsonify-opening-hours

Usage

The simplest example is just printing the JSON for an opening hours string:

from parse_opening_hours import OpeningHours

print(OpeningHours.parse("Mon- Fri 9:00am - 5:30pm").json())

This should give you the below output:

[
	{'day': 'monday', 'opens': '9:00', 'closes': '17:30'},
	{'day': 'tuesday', 'opens': '9:00', 'closes': '17:30'},
	{'day': 'wednesday', 'opens': '9:00', 'closes': '17:30'},
	{'day': 'thursday', 'opens': '9:00', 'closes': '17:30'},
	{'day': 'friday', 'opens': '9:00', 'closes': '17:30'}
]

This has been tested using Python 3.8.5

Environment variables

Setting the environment variable OH_DEBUG to a value of Y will set the root logging level to debug and will cause log entries to appear in stdout for debugging purposes

Troubleshooting

Assumptions

When specifying a time without AM or PM indicators, you may get an error that reads TypeError: Cannot convert a time of unknown type (AM, PM or 24H) without assuming its type.. To resolve this, pass assume_type=TimeType.AM when calling the parse() function. This will use AM in place of an unknown AM or PM designation. In cases like the string "9-5", if the second value in the range (in this case the 5 is smaller than the first (i.e. the 9) then it will be converted to PM automatically

Tests and Coverage

run pytet and generate coverage database pipenv run pytest --cov=./

show coverage report: pipenv run coverage report

Build and Release

This is pretty much here so I can copy-paste the commands to make a new release.

build: python3 setup.py sdist bdist_wheel

check: twine check dist/*

upload: twine upload dist/*

todo:

  • add tests with emojis and stuff

  • add tests with unicode code points (partial)

  • "Noon" as alias for 12 PM

  • "midnight" alias for 12am

  • 24 hours as alias for 12am-11:59 PM also "24 hours" "24hrs" "24h" "All Day" "Open 24 hours"

  • "24/7" alias for 7 days a week, 24 hours a day also "24 Hours/Day, 7 Days/Week", "Seven Days a Week"

  • support for single specific dates ("April 22nd")

  • support lists of specific dates ("April 22nd & 23rd")

  • support "from" as separator betweendate and time

  • documentation

  • allow "Open" as an optional prefix

  • "Mon: 9a-4p, Tue: 11:30a- 6:30p, Wed:11:30a- 6:30p, Thurs: 9a-4p, Fri: 9a-4p, Sat: 9a-4p, Sun: Varies"

Long term:

  • support specific single days i.e. "April 22nd 9 am to 4 pm"

  • day lists "April 22nd & 23rd 9 am to 4 pm", "April 22nd & 23rd 9 am to 4 pm", "April 14th, 15th, 16th 9 am to 4 pm, 17th 9 am to 1 pm", "4/8 10 am to 3 pm & 4/9 9 am to 3 pm"

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

jsonify-opening-hours-0.3.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

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

jsonify_opening_hours-0.3.0-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file jsonify-opening-hours-0.3.0.tar.gz.

File metadata

  • Download URL: jsonify-opening-hours-0.3.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for jsonify-opening-hours-0.3.0.tar.gz
Algorithm Hash digest
SHA256 8c5f129fbf27db085d1f16c003e08bd4185ff5ab563f79ced1d80c39e61b1650
MD5 d00b7a335d2e238a96a76c1271e4b939
BLAKE2b-256 b5e26d95f28b9548b530049555cec0a78d827f1c8fa265e422826f2343d288e0

See more details on using hashes here.

File details

Details for the file jsonify_opening_hours-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: jsonify_opening_hours-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for jsonify_opening_hours-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bc39bb80e2e45cd7ff2b4c3a6d1a2351b5c7492629da67e42baca39ab71774c8
MD5 2a5feef3abea32e501a301fc8a4d854f
BLAKE2b-256 ad84e7e60359c81fb2df0d11e281dd42cf7b37c5ffeb2e1994dbb763c4973954

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