Skip to content

singhaidotnish/demo_design_pattern

Repository files navigation


Logo

Demo Design Patterns

Object-Oriented Design and Design Patterns Knowledge
Why Learn Patterns »

Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Examples
  5. Tests
  6. Contributing
  7. Challenges
  8. Contact
  9. Acknowledgments

About The Project

Product Name Screen Shot

Make a tool that is built on "SOLID" principles.

(back to top)

Getting Started

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.

Prerequisites

Additional Modules required.

  • json2html
  • pytest
  • pyyaml

Installation

  • 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.

Usage

For testing module

pip install -e .[dev]

(back to top)

For command line see examples below

To make use of new file type follow these steps

  1. Add new file type to class variable ALL_TYPES of class FileFactory
    ALL_TYPES = ['Json', 'Yaml', '<new_file_type>']
  1. 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

Examples:

Add -

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

Add Command

Display -

python -m demo_design_pattern.cli --display --filetype Yaml

Display Command

Convert -

python -m demo_design_pattern.cli --convert --filetype Yaml --filetype_to Json

Convert Command

Filter -

python -m demo_design_pattern.cli --filter A* --filetype Yaml

Filter Command

List all file types -

python -m demo_design_pattern.cli --all_types

List All Types Command

Tests:

Go to tests folder

Test Add

pytest -k add -v

Test Add

Test Convert

pytest -k convert -v

Test Convert

Test Filter

pytest -k filter -v

Test Convert

Test Display

pytest -k display -v

Test Convert

(back to top)

Contributing

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.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

Challenges

  1. Converting json to yaml is not working. It shows json format instead of yaml format on terminal
  2. Writing Document is more challenging then writing code.

Contact

Your Name - @knishua - singhai(dot)nish(at)gmail.com

Project Link: https://github.com/singhaidotnish/singhaidotnish

(back to top)

Acknowledgments

  • Animal Logic Pipeline - I am Thankful to the pipeline team at Animal Logic for giving this task.

(back to top)

Releases

No releases published

Packages

 
 
 

Languages