Object-Oriented Design and Design Patterns Knowledge
Why Learn Patterns »
Report Bug
·
Request Feature
Table of Contents
Make a tool that is built on "SOLID" principles.
This is a simple API allowing you to add new records, filter users. Display data in Json and Yaml. A CLI exists to add, display, convert and filter the whole data set. It is written so as to have an extendable system in future. No Validation is included at this point as the focus is only on patterns.
- json2html
- pytest
- pyyaml
- Create a python3.9 virtual environment and activate it
virtualenv .venv
.venv\Scripts\activate.bat
- cd into cloned repo and run
cd singhaidotnish
Ready to test.
pip install -e .[dev]
To make use of new file type follow these steps
- Add new file type to class variable ALL_TYPES of class FileFactory
ALL_TYPES = ['Json', 'Yaml', '<new_file_type>']
- Based on class description below, replace "yaml" with new file type. and use its corresponding load and dump methods to read and write.
class Yaml(IFile):
DEMO_YAML = 'demo.yaml'
def __init__(self):
super().__init__()
self.name = 'Yaml'
self.file = os.path.join(os.path.dirname(__file__), Yaml.DEMO_YAML)
def read(self, read_from=None):
_data = None
try:
if read_from:
_data = json.dumps(read_from, indent=4)
_data = json.loads(_data)
else:
with open(self.file, 'r') as yamlFile:
_data = yaml.safe_load(yamlFile) or []
except yaml.YAMLError as e:
raise Exception('Error Reading Yaml {e}')
return _data
def write(self, data):
try:
_tmp_data = self.read()
_tmp_data['data'].append(data)
with open(self.file, 'w') as stream:
yaml.safe_dump(_tmp_data, stream, default_flow_style=False)
except (IOError, ) as e:
return False
return _tmp_data
python -m demo_design_pattern.cli --add --name FF --phone 9090909090 --address "a\\b building no X, floor X, landmark, city, state pincode" --filetype Yaml
Display -
python -m demo_design_pattern.cli --display --filetype Yaml
Convert -
python -m demo_design_pattern.cli --convert --filetype Yaml --filetype_to Json
Filter -
python -m demo_design_pattern.cli --filter A* --filetype Yaml
List all file types -
python -m demo_design_pattern.cli --all_types
Go to tests folder
pytest -k add -v
pytest -k convert -v
pytest -k filter -v
pytest -k display -v
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
- Converting json to yaml is not working. It shows json format instead of yaml format on terminal
- Writing Document is more challenging then writing code.
Your Name - @knishua - singhai(dot)nish(at)gmail.com
Project Link: https://github.com/singhaidotnish/singhaidotnish