From add6940dce9376fd58cda5dc15ef275e350ea36b Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 15 Nov 2019 21:04:16 +0100 Subject: [PATCH] rover: use Pydantic for Vector and Rover classes Pydantic is similar to Dataclasses, but enables run-time type-checking. --- rover/Pipfile | 1 + rover/Pipfile.lock | 17 +++++++++++++++-- rover/rover.py | 17 ++++++++--------- rover/test_rover.py | 8 ++++---- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/rover/Pipfile b/rover/Pipfile index 7b6c0f9..2f1a742 100644 --- a/rover/Pipfile +++ b/rover/Pipfile @@ -8,6 +8,7 @@ pytest = "*" pytest-cov = "*" [packages] +pydantic = "*" [requires] python_version = "3.8" diff --git a/rover/Pipfile.lock b/rover/Pipfile.lock index c0bfcad..9966bc4 100644 --- a/rover/Pipfile.lock +++ b/rover/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "4e6c8e77ff1a48b470f5c8aa479b97d87225ac2cd2fb9b9f9ebceae1713e3adb" + "sha256": "afeb69259202b698c70d288232d6bd89a36b75720f9efeb8f0360d5f8a5217dd" }, "pipfile-spec": 6, "requires": { @@ -15,7 +15,20 @@ } ] }, - "default": {}, + "default": { + "pydantic": { + "hashes": [ + "sha256:2588e5c68b7927fd44c4bf2c5768cdaff7948fc4ae0e3f602376b76b1913c3b0", + "sha256:2d8d8c14b6349066e27aca9bfd3fb973fb2c45a860a6ba83fbea16ee42efbfd1", + "sha256:40b62f5e8db4957221e4aa06cea015c4a74740405edea0e11def4c6418e7c62c", + "sha256:43f8ac74e114aaac0d844040fea971fc6c53434f24cee2e1aa0486e6601817b4", + "sha256:4f3baf0b4ef8f68021186448efddbcc259ed473e18acf4433ef75dd13d795594", + "sha256:f85a63028003d8778dc3bf27c5c5cd25da64593422ab82a696cca32b1ce3e750" + ], + "index": "pypi", + "version": "==1.1" + } + }, "develop": { "atomicwrites": { "hashes": [ diff --git a/rover/rover.py b/rover/rover.py index 3d35656..cfa893c 100644 --- a/rover/rover.py +++ b/rover/rover.py @@ -1,11 +1,11 @@ import enum -from dataclasses import dataclass + +from pydantic import BaseModel -@dataclass -class Vector: - x: int - y: int +class Vector(BaseModel): + x: int = 0 + y: int = 0 class Direction(enum.Enum): @@ -15,8 +15,7 @@ class Direction(enum.Enum): WEST = "W" -@dataclass -class Rover: - pos: Vector = (0, 0) - planet_size: Vector = (100, 100) +class Rover(BaseModel): + pos: Vector = Vector(x=0, y=0) + planet_size: Vector = Vector(x=100, y=100) dir: Direction = Direction.NORTH diff --git a/rover/test_rover.py b/rover/test_rover.py index a7595ab..f886133 100644 --- a/rover/test_rover.py +++ b/rover/test_rover.py @@ -1,14 +1,14 @@ -from rover import Direction, Rover +from rover import Direction, Rover, Vector def test_rover_constructor(): - rov = Rover(pos=(0, 0), planet_size=(100, 100)) - assert rov.pos == (0, 0) and rov.planet_size == (100, 100) + rov = Rover(pos={"x": 0, "y": 0}, planet_size={"x": 100, "y": 100}) + assert rov.pos == Vector(x=0, y=0) and rov.planet_size == Vector(x=100, y=100) def test_rover_default_values(): rov = Rover() - assert rov.pos == (0, 0) and rov.planet_size == (100, 100) + assert rov.pos == Vector(x=0, y=0) and rov.planet_size == Vector(x=100, y=100) def test_rover_has_direction():