parsed_date('2pm')core
We write source code first, and then tests come after. The tests serve as both a means to confirm that the code works and also serves as working examples. The first exported function, parsed_date, is an example of this pattern.
parsed_date
parsed_date (s:str)
Convert s to a datetime
isinstance(date.fromtimestamp(0), date)The Friend dataclass : this is our main business object.
Note that I use Python dataclass to define this class.
Friend
Friend (id:str, family_name:str, first_name:str, email:str, phone:str, linkedin:str, twitter:str)
Class for keeping track of a friend item.
f1 = Friend(3, 'doo', 'john', 'john@doo.com', '0000000000', 'linkedin', 'twitter')
f1db = Database("sqlite:///:memory:")friends1 = db.create(Friend, pk='id')print(db.schema())friends1.exists()friends1.insert(f1)friends1()friends1(where="family_name = :my", my="doo")Let’s try to remove this object from the database.
friends1.delete(f1.id)After these tests, let’s build our Repository service class.
Repository
Repository (_db_file_path:str)
Repository Service for the Friend dataclass. The repository implementation is based on SQLLite with a file storage
Let’s have an example here.
First, we create the repository.
! rm friends_v1.dbrepo = Repository('friends_v1.db')
repoYou need to call the create method.
repo.add('doo', 'john', 'john@doo.com', '0000000000', 'linkedin', 'twitter')Now, let’s retrieve my friends:
repo.friends()