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)
A JSON API for returning Godspeak sentences. Based on the works of Terry A Davis (Rest in Peace, King)

GodspeakAPI A simple API for generating random words ("godspeaks"), inspired by the works of Terrence Andrew Davis (Rest In Peace, King). Installation

Eccentrici 3 Jan 24, 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
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
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
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
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
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
JSONManipulator is a Python package to retrieve, add, delete, change and store objects in JSON files.

JSONManipulator JSONManipulator is a Python package to retrieve, add, delete, change and store objects in JSON files. Installation Use the package man

Andrew Polukhin 1 Jan 07, 2022
Atom, RSS and JSON feed parser for Python 3

Atoma Atom, RSS and JSON feed parser for Python 3. Quickstart Install Atoma with pip: pip install atoma

Nicolas Le Manchet 95 Nov 28, 2022
A fast streaming JSON parser for Python that generates SAX-like events using yajl

json-streamer jsonstreamer provides a SAX-like push parser via the JSONStreamer class and a 'object' parser via the ObjectStreamer class which emits t

Kashif Razzaqui 196 Dec 15, 2022
JSON Schema validation library

jsonschema A JSON Schema validator implementation. It compiles schema into a validation tree to have validation as fast as possible. Supported drafts:

Dmitry Dygalo 309 Jan 01, 2023
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
Simple, minimal conversion of Bus Open Data Service SIRI-VM data to JSON

Simple, minimal conversion of Bus Open Data Service SIRI-VM data to JSON

Andy Middleton 0 Jan 22, 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
A python library to convert arbitrary strings representing business opening hours into a JSON format that's easier to use in code

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

Adrian Edwards 9 Dec 02, 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
The ldap2json script allows you to extract the whole LDAP content of a Windows domain into a JSON file.

ldap2json The ldap2json script allows you to extract the whole LDAP content of a Windows domain into a JSON file. Features Authenticate with password

Podalirius 68 Dec 07, 2022
Define your JSON schema as Python dataclasses

Define your JSON schema as Python dataclasses

62 Sep 20, 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
A query expression for extracting data from JSON.

JSONPATH A selector expression for extracting data from JSON. Quickstarts Installation Install the stable version from PYPI. pip install jsonpath-extr

林玮 (Jade Lin) 33 Oct 22, 2022