A python library to convert arbitrary strings representing business opening hours into a JSON format that's easier to use in code

Overview

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 parse-opening-hours

Usage

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

from 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

Documentation

In addition to this README, there is some documentation generated from inline documentation comments. This is available at https://moralcode.github.io/parse-opening-hours/

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

Comments
  • make sure

    make sure "special" apostrophes are also handled

    a string like Tuesday’s has a single left-facing apostophe () in it, which is not the same as a "regular" straight apostrophe (').

    Need to double check whether this is currently handled by the unicode normalizing or the pyparsing patterns and if not, handle it. Also, regardless, there should be a unit test to make sure that this doesnt become an issue later

    good first issue 
    opened by MoralCode 1
  • Consider renaming package

    Consider renaming package

    Despite having jsonify in the name, this is mainly a general parser for opening hours strings that can be made to output to pretty much any format.

    Might be nice to swap to a new name early on

    opened by MoralCode 1
  • Automatically combine adjacent times

    Automatically combine adjacent times

    For example, something that would otherwise parse to:

    [{"day": "sunday", "opens": "09:00", "closes": "17:00"}, {"day": "sunday", "opens": "17:00", "closes": "19:00"}]
    

    would automatically get merged into:

    [{"day": "sunday", "opens": "09:00", "closes": "19:00"}]
    
    opened by MoralCode 0
  • handle full iso datetime format

    handle full iso datetime format

    2021-07-11T11:54:00.000+00:00

    should be pretty easy since i think pyparsing supports this pattern already. Still need to figure out how to get it to work with the Classes for handling a specific date

    opened by MoralCode 0
  • separate out some key patterns and offer them as modules for people to use

    separate out some key patterns and offer them as modules for people to use

    this would be similar to how pyparsing_common offers classifiers for things like ISO dates, but would include things like date strings "January 31st, 2021" etc

    opened by MoralCode 1
  • Detect and separate notes

    Detect and separate notes

    Sometimes notes are included in opening hours strings:

    By appointment
    By appointment. Schedule in MyChartPLUS or by phone.
    F: Hours Vary; By appointment; Clinics are usually held on Friday mornings. Time and location is usually to-be-determined the week of the clinic and subject to change. Individuals will be givien the location and time when scheduling appointment.
    M,T,W: 9am-4pm; By appointment
    

    Would probably be useful to find a solid way to detect these and make them available separately from the times themselves. Some problems this might pose:

    • notes arent as universally structured as date or time ranges are
    • associating notes with one specific day might require significant work given how multi-day handling is implemented
    opened by MoralCode 1
  • Breaking API updates

    Breaking API updates

    This is an issue for compiling a list of breaking API changes that would be nice to have, but arent really worth doing on their own. The plan is to save them all up and apply them all at once whenever version 1.0 is considered ready.

    The list:

    • [ ] rename the model classes to have more descriptive names
    • [ ] give all the model classes a consistent API (and maybe create some superclass or interface for this?)
      • [ ] make the parse() methods on each class generic (using fields and methods defined by the model classes) and pull it up into a superclass for deduplication
    • [ ] convert the constructor of the Days class to a class method from_range and make a simpler constructor (this will require some pretty major refactoring)
    • [ ] #3
    • [ ] rename Times to TimeRange and create a class Times that can store multiple TimeRange's similar to Days (see #5)
    • [ ] make is_24_hr() and is_12_hour() in Time consistent
    • [ ] create a common system for specifying assumptions to make when the information is not available (for AM/PM, and century/year)
    • [ ] make all the from_parse_results methods consistent with regard to if they are processing a clean dictionary or a pyparsing.ParseResult object and possibly rename them to from_parse_result_dict
    discussion 
    opened by MoralCode 0
  • Handle small typos and other small mistakes

    Handle small typos and other small mistakes

    It would be nice to be able to pass in real-world opening hours data that may have some typos and have this library automatically handle that.

    Maybe a more complex solution would be better but for now an easy solution to this would be to change some of the patterns that currently use CaselessMatch for parsing so they can tolerate a certain number of incorrect characters and still be considered valid.

    This "ignore up to x typos" functionality does not currently exist in this specific class in pyparsing, although it exists in other classes (CloseMatch if i recall correctly). There is currently an open PR in pyparsing to add this functionality.

    blocked 
    opened by MoralCode 0
Releases(v0.4.2)
  • v0.4.2(Jun 25, 2021)

    • convert times to military time when printing the string reperesentation of a Times object
    • correctly handle strings like "12pm" and "12am" so they correctly parse to "12:00" and "0:00" in military time respectively
    Source code(tar.gz)
    Source code(zip)
  • v0.4.1(May 30, 2021)

  • v0.4(May 27, 2021)

    • support optional prefix of "open" on strings, like "open monday 9am-5pm"
    • support shortcuts like "open 24 hours a day" and "24/7"
    • package is now imported as opening_hours instead of parse_opening_hours (see example in README)
    Source code(tar.gz)
    Source code(zip)
  • v0.3.1(May 16, 2021)

    The pypi build for v0.3 contained additional information from the local working tree that was not committed to the repository and may have introduced some unintended behavior (all tests still passed though). This re-build and re-release removes this information from the production package.

    Source code(tar.gz)
    Source code(zip)
  • v0.3(May 16, 2021)

    • [BREAKING] rename the main class to OpeningHours since its not exclusive to json
    • more refactoring and cleanup
    • add generated documentation page
    • improve robustness of some of the matching patterns
    • count the single-letter "H" as "thursday" when used as a day (i.e. "H 9am - 5 pm")
    • parse the word "from" as a separator between dates and times (i.e. "Monday from 9:00 - 5:00")
    • support dots following abbreviated days of the week (i.e. "Mon.")
    • support commas separating days and times (i.e. Monday, 9am - 5pm")
    • support times in military style (i.e. "Monday 0900-1700")
    • support short/abbreviated AM/PM designations (i.e. "9a - 5p" and "9a. to 5p.")
    Source code(tar.gz)
    Source code(zip)
  • v0.2(May 7, 2021)

    Changelog

    • support plural day names with appostrophes (i.e. "Monday's 9am to 5 pm")
    • support listing specific days (i.e. "Mondays, Tuesdays, Thursdays 9am to 5 pm")
    • add support for commonly used shortcuts like "7 days a week" "daily" "weekdays" "business days" "weekends" .etc
    • treat no specified days (i.e. "9am to 5pm") the same as "every day"
    • made parsing of day names more robust
    Source code(tar.gz)
    Source code(zip)
  • v0.1(May 3, 2021)

    This release is what could be considered an MVP. it can handle basic strings like "Mon- Fri 9:00am - 5:30pm" (with some variations in day and time format) and has test cases covering most if not all of these

    Source code(tar.gz)
    Source code(zip)
An tiny CLI to load data from a JSON File during development.

JSON Server - An tiny CLI to load data from a JSON File during development.

Yuvraj.M 4 Mar 22, 2022
jq for Python programmers Process JSON and HTML on the command-line with familiar syntax.

jq for Python programmers Process JSON and HTML on the command-line with familiar syntax.

Denis Volk 3 Jan 09, 2022
import json files directly in your python scripts

Install Install from git repository pip install git+https://github.com/zaghaghi/direct-json-import.git Use With the following json in a file named inf

Hamed Zaghaghi 51 Dec 01, 2021
Python script for converting .json to .md files using Mako templates.

Install Just install poetry and update script dependencies Usage Put your settings in settings.py and .json data (optionally, with attachments) in dat

Alexey Borontov 6 Dec 07, 2021
JSON for Modern C++ Release Scripts

JSON for Modern C++ Release Scripts Preparations Install required tools: make install_requirements. Add required keys to config.json (apparently not c

Niels Lohmann 4 Sep 19, 2022
This open source Python project allow you to create JSON data trees using Minmup.com

This open source Python project allow you to create JSON data trees using Minmup.com. I try to develop this project all the time. But feel free to use :).

Arttu Väisänen 1 Jan 30, 2022
JSON Interoperability Vulnerability Labs

JSON Interoperability Vulnerability Labs Description These are the companion labs to my research article "An Exploration of JSON Interoperability Vuln

Bishop Fox 168 Dec 25, 2022
No more boilerplate to check and build a Python object from JSON.

JSONloader This module is for you if you're tired of writing boilerplate that: builds a straightforward Python object from loaded JSON. checks that yo

3 Feb 05, 2022
Editor for json/standard python data

Editor for json/standard python data

1 Dec 07, 2021
A Python tool that parses JSON documents using JsonPath

A Python tool that parses JSON documents using JsonPath

8 Dec 18, 2022
Convert your JSON data to a valid Python object to allow accessing keys with the member access operator(.)

JSONObjectMapper Allows you to transform JSON data into an object whose members can be queried using the member access operator. Unlike json.dumps in

Owen Trump 4 Jul 20, 2022
Package to Encode/Decode some common file formats to json

ZnJSON Package to Encode/Decode some common file formats to json Available via pip install znjson In comparison to pickle this allows having readable

ZINC 2 Feb 02, 2022
JsonParser - Parsing the Json file by provide the node name

Json Parser This project is based on Parsing the json and dumping it to CSV via

Ananta R. Pant 3 Aug 08, 2022
simplejson is a simple, fast, extensible JSON encoder/decoder for Python

simplejson simplejson is a simple, fast, complete, correct and extensible JSON http://json.org encoder and decoder for Python 3.3+ with legacy suppo

1.5k Jan 05, 2023
Define your JSON schema as Python dataclasses

Define your JSON schema as Python dataclasses

62 Sep 20, 2022
API that provides Wordle (ES) solutions in JSON format

Wordle (ES) solutions API that provides Wordle (ES) solutions in JSON format.

Álvaro García Jaén 2 Feb 10, 2022
json|dict to python object

Pyonize convert json|dict to python object Setup pip install pyonize Examples from pyonize import pyonize

bilal alpaslan 45 Nov 25, 2022
Random JSON Key:Pair Json Generator

Random JSON Key:Value Pair Generator This simple script take an engish dictionary of words and and makes random key value pairs. The dictionary has ap

Chris Edwards 1 Oct 14, 2021
cysimdjson - Very fast Python JSON parsing library

Fast JSON parsing library for Python, 7-12 times faster than standard Python JSON parser.

TeskaLabs 235 Dec 29, 2022
A fast JSON parser/generator for C++ with both SAX/DOM style API

A fast JSON parser/generator for C++ with both SAX/DOM style API Tencent is pleased to support the open source community by making RapidJSON available

Tencent 12.6k Dec 30, 2022