From 1a487ea9c7864f56d8e79e582edf83f5dfe82a8d Mon Sep 17 00:00:00 2001 From: Klaas Schoute Date: Thu, 4 Aug 2022 00:35:59 +0200 Subject: [PATCH] Fix created value be none (#8) * Datetime can be NONE now * Update documentation * Fix pytest code for 100% codecov --- README.md | 6 ++++-- liege/models.py | 27 ++++++++++++++++++++++----- tests/__init__.py | 2 +- tests/fixtures/disabled_parkings.json | 1 - 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9b6f5d9..c732bee 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,8 @@ pip install liege You can read the following datasets with this package: -- Disabled parking spaces / Stationnement PMR (950 locations) -- Garages / Les parkings voitures hors voirie (10 locations) +- [Disabled parking spaces / Stationnement PMR][disabled_parking] (952 locations) +- [Garages / Les parkings voitures hors voirie][garages] (26 locations)
Click here to get more details @@ -193,6 +193,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. [api]: https://opendata.liege.be/explore +[disabled_parking]: https://opendata.liege.be/explore/dataset/stationnement-pmr +[garages]: https://opendata.liege.be/explore/dataset/parkings-voitures-hors-voirie [nipkaart]: https://www.nipkaart.nl diff --git a/liege/models.py b/liege/models.py index 618838d..e6de803 100644 --- a/liege/models.py +++ b/liege/models.py @@ -51,8 +51,8 @@ def from_dict(cls, data: dict[str, Any]) -> Garage: url=attr.get("website"), longitude=geo[0], latitude=geo[1], - created_at=datetime.strptime(attr.get("created"), "%Y-%m-%d"), - updated_at=datetime.strptime(attr.get("last_modified"), "%Y-%m-%d"), + created_at=strptime(attr.get("created"), "%Y-%m-%d"), + updated_at=strptime(attr.get("last_modified"), "%Y-%m-%d"), ) @@ -70,7 +70,7 @@ class DisabledParking: longitude: float latitude: float - created_at: datetime + created_at: datetime | None updated_at: datetime @classmethod @@ -95,6 +95,23 @@ def from_dict(cls, data: dict[str, Any]) -> DisabledParking: status=attr.get("status"), longitude=geo[0], latitude=geo[1], - created_at=datetime.strptime(attr.get("created"), "%Y-%m-%d"), - updated_at=datetime.strptime(attr.get("last_modified"), "%Y-%m-%d"), + created_at=strptime(attr.get("created"), "%Y-%m-%d"), + updated_at=strptime(attr.get("last_modified"), "%Y-%m-%d"), ) + + +def strptime(date_string: str, date_format: str, default: None = None) -> Any: + """Strptime function with default value. + + Args: + date_string: The date string. + date_format: The format of the date string. + default: The default value. + + Returns: + The datetime object. + """ + try: + return datetime.strptime(date_string, date_format) + except (ValueError, TypeError): + return default diff --git a/tests/__init__.py b/tests/__init__.py index c6f187b..b4a3e9b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,4 +1,4 @@ -"""Asynchronous Python client providing Open Data information of Gent.""" +"""Asynchronous Python client providing Open Data information of Liege.""" import os diff --git a/tests/fixtures/disabled_parkings.json b/tests/fixtures/disabled_parkings.json index 7880ab2..16c947d 100644 --- a/tests/fixtures/disabled_parkings.json +++ b/tests/fixtures/disabled_parkings.json @@ -366,7 +366,6 @@ "gid": 1257, "municipality": "Liège", "street_name": "place des Abeilles", - "created": "2021-03-25", "geo_shape": { "coordinates": [ 5.6330619893,