Kamaropoulos / COVID19Py

A tiny Python package for easy access to up-to-date Coronavirus (COVID-19, SARS-CoV-2) cases data.
https://pypi.org/project/COVID19Py/
GNU General Public License v3.0
84 stars 57 forks source link

Applied State Pattern #153

Closed pranishnepal closed 3 years ago

pranishnepal commented 3 years ago

Applied The State Pattern to the getLatest() method

I applied The State Pattern to the project's getLatest() method. State Pattern lets us treat the package as if it were a state machine, and react to the transition between certain state.

How?

First to implement state machines, I imported the state_machine package to the project, and updated requirements.txt accordingly. Since getLatest() is a method that contains important information about the total confirmed cases, deaths, and recoveries, I created a two states (start_state, latest_data_state) and two transitions to transition between the states (transition_to_latest_data_state, transition_to_start_state). Now, whenever, latest_data_state is reached, getLatest() is called, and _mostRecentData holds the data fetched. To return the data held by _mostRecentData, I created a method getMostRecentData().

Does this modification change the behaviour of the project?

Yes; to transition between the states and to access the latest COVID-19 data, I have updated the latest.py file (in examples directory) with the new method of using the package. The new example code is properly commented.