Skip to main content

A corrector for the 'opening_hours' fields from OpenStreetMap.

Project description

Opening hours sanitizer - A corrector for the 'opening_hours' fields
=========================================================================

If you work with the `opening_hours` fields in OpenStreetMap, you probably need to check somewhere if a field is valid before to parse it, or to do anything with it.

Of course, for simple formats, can do this with regexes, but [specifications](https://wiki.openstreetmap.org/wiki/Key:opening_hours/specification) are so rich that a full regex to handle them is quite impossible.
This is the goal of this tiny module: check if an `opening_hours` field is valid, and try to correct it if it isn't.

In addition to the simple formats, like `Mo-Fr 10:00-20:00`, it can parse and fix almost all shapes of opening hours.
Here is what it can do.

```python
>>> from oh_sanitizer import sanitize_field
>>> print(sanitize_field("mo-fr 10h - 19h00"))
"Mo-Fr 10:00-19:00"
>>> print(sanitize_field("2010 - 2020/2 dec-feb 10:00 am - 12:00 am/1:00 pm-7:00pm"))
"2010-2020/2 Dec-Feb 10:00-12:00,13:00-19:00"
```

It is so small that you can include it directly into your projet.
You can also install it with PyPi:

$ pip3 install oh-sanitizer

The `sanitize_field()` function can raise the following exceptions:
- `TypeError` : If the given field is not a string.
- `SanitizeError` : The generic exception of `oh_sanitizer`,
raised when the field can't be parsed (if it is too complex,
or "too much invalid").
- `InconsistentField` : Inheriting from `SanitizeError`,
raised when the field contains an invalid pattern which can't
be corrected automatically.

So, you can simple do this.

```python
from oh_sanitizer import sanitize_field, SanitizeError
field = "mo-fr 10h - 19h00"
try:
sanitized_field = sanitize_field(field)
print("Here is the sanitized field:", sanitized_field)
except SanitizeError:
print("So bad, the field could not be sanitized.")
```


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oh_sanitizer-0.1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oh_sanitizer-0.1.0-py3-none-any.whl (2.1 kB view details)

Uploaded Python 3

File details

Details for the file oh_sanitizer-0.1.0.tar.gz.

File metadata

  • Download URL: oh_sanitizer-0.1.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for oh_sanitizer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5ed2578df31582bd87ac75e484ace5e015f80eb8d4d3004405802cddb848df2c
MD5 ff6a0eb5e95b9c4d786d9bf41a12898a
BLAKE2b-256 6d0a72c39cd2d31ac8e691d8c4d16b6dd4e4a135a7aa8cdd5799a66bd90536e9

See more details on using hashes here.

File details

Details for the file oh_sanitizer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for oh_sanitizer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f7125d05e0fd8d8aea6dcc095f9861bfa84ad39ca01ed3bc282a49d9573ad5c2
MD5 d168f902b0bb3c64ed7eb4ee5cb45edf
BLAKE2b-256 78e80fbd2351f2fbdb5d336087ae4306cacb483b4ebab4fa16d2e4b76438e183

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page