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)
Editor for json/standard python data

Editor for json/standard python data

1 Dec 07, 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
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
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
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
Json GUI for No Man's Sky save file

NMS-Save-Parser Json GUI for No Man's Sky save file GUI python NMS_SAVE_PARSER.py [optional|save.hg] converter only python convert.py usage: conver

2 Oct 19, 2022
Convert your subscriptions csv file into a valid json for Newpipe!

Newpipe-CSV-Fixer Convert your Google subscriptions CSV file into a valid JSON for Newpipe! Thanks to nikcorg for sharing how to convert the CSV into

Juanjo 44 Dec 29, 2022
RedisJSON - a JSON data type for Redis

RedisJSON is a Redis module that implements ECMA-404 The JSON Data Interchange Standard as a native data type. It allows storing, updating and fetching JSON values from Redis keys (documents).

3.4k Dec 29, 2022
Fileson - JSON File database tools

Fileson is a set of Python scripts to create JSON file databases

Joonas Pihlajamaa 2 Feb 02, 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
Convert Wii UI formats to JSON5 and vice versa

Convert Wii UI formats to JSON5 and vice versa

Pablo Stebler 11 Aug 28, 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 09, 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
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
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
Ibmi-json-beautify - Beautify json string with python

Ibmi-json-beautify - Beautify json string with python

Jefferson Vaughn 3 Feb 02, 2022
Simple Python Library to convert JSON to XML

json2xml Simple Python Library to convert JSON to XML

Vinit Kumar 79 Nov 11, 2022
JSONx - Easy JSON wrapper packed with features.

🈷️ JSONx Easy JSON wrapper packed with features. This was made for small discord bots, for big bots you should not use this JSON wrapper. 📥 Usage Cl

2 Dec 25, 2022
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
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