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)
Json utils is a python module that you can use when working with json files.

Json-utils Json utils is a python module that you can use when working with json files. it comes packed with a lot of featrues Features Converting jso

Advik 4 Apr 24, 2022
With the help of json txt you can use your txt file as a json file in a very simple way

json txt With the help of json txt you can use your txt file as a json file in a very simple way Dependencies re filemod pip install filemod Installat

Kshitij 1 Dec 14, 2022
Simple Python Library to convert JSON to XML

json2xml Simple Python Library to convert JSON to XML

Vinit Kumar 79 Nov 11, 2022
Make JSON serialization easier

Make JSON serialization easier

null 4 Jun 30, 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
Ibmi-json-beautify - Beautify json string with python

Ibmi-json-beautify - Beautify json string with python

Jefferson Vaughn 3 Feb 2, 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
Creates fake JSON files from a JSON schema

Use jsf along with fake data generators to provide consistent and meaningful fake data for your system.

Andy Challis 86 Jan 3, 2023
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
Same as json.dumps or json.loads, feapson support feapson.dumps and feapson.loads

Same as json.dumps or json.loads, feapson support feapson.dumps and feapson.loads

boris 5 Dec 1, 2021
Wikidot-forum-dump - Simple Python script that dumps a Wikidot wiki forum into JSON structures.

wikidot-forum-dump Script is partially based on 2stacks by bluesoul: https://github.com/scuttle/2stacks To dump a Wiki's forum, edit config.py and put

ZZYZX 1 Jun 29, 2022
A Cobalt Strike Scanner that retrieves detected Team Server beacons into a JSON object

melting-cobalt ?? A tool to hunt/mine for Cobalt Strike beacons and "reduce" their beacon configuration for later indexing. Hunts can either be expans

Splunk GitHub 150 Nov 23, 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 7, 2022
Roamtologseq - A script loads a json export of a Roam graph and cleans it up for import into Logseq

Roam to Logseq The script loads a json export of a Roam graph and cleans it up f

Sebastian Pech 4 Mar 7, 2022
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 JSON utility library for Python featuring Django-style queries and mutations.

JSON Enhanced JSON Enhanced implements fast and pythonic queries and mutations for JSON objects. Installation You can install json-enhanced with pip:

Collisio Technologies 4 Aug 22, 2022
Low code JSON to extract data in one line

JSON Inline Low code JSON to extract data in one line ENG RU Installation pip install json-inline Usage Rules Modificator Description ?key:value Searc

Aleksandr Sokolov 12 Mar 9, 2022
Generate code from JSON schema files

json-schema-codegen Generate code from JSON schema files. Table of contents Introduction Currently supported languages Requirements Installation Usage

Daniele Esposti 30 Dec 23, 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

null 1.5k Jan 5, 2023