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)
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 01, 2021
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
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
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
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
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
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
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
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
A daily updated JSON dataset of all the Open House London venues, events, and metadata

Open House London listings data All of it. Automatically scraped hourly with updates committed to git, autogenerated per-day CSV's, and autogenerated

Jonty Wareing 4 Jan 01, 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
Easy JSON wrapper modfied to wrok with suggestions

🈷️ Suggester Easy JSON wrapper modfied to wrok with suggestions. This was made for small discord bots, for big bots you should not use this. 📥 Usage

RGBCube 1 Jan 22, 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
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
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
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
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
MOSP is a platform for creating, editing and sharing validated JSON objects of any type.

MONARC Objects Sharing Platform Presentation MOSP is a platform for creating, editing and sharing validated JSON objects of any type. You can use any

CASES Luxembourg 72 Dec 14, 2022
simdjson : Parsing gigabytes of JSON per second

JSON is everywhere on the Internet. Servers spend a *lot* of time parsing it. We need a fresh approach. The simdjson library uses commonly available SIMD instructions and microparallel algorithms to

16.3k Dec 29, 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