GithubHelp home page GithubHelp logo

task-tromzo's Introduction

Task-Tromzo

The code assignment.

STEP 1: Design an ObjectMgr which manages a pool of objects. Objects can be considered to be ints (from 1 to n). It should provide api’s to get_object() and free_object():

get_object(): Returns any object available in the pool. An object cannot be given away again unless it has been freed. free_object(int obj): Returns the object back to the pool so that it can be given out again.

Please document the API’s definitions, data structures and write tests.

STEP 2:

  1. Write a GraphQL API schema to use the above functions to create, get and free objects.
  2. Deploy it as a service/container that can be used to deploy on a linux server.

Answer

In .app/object/objectmgr.py, created class ObjectMgr.

from collections import deque
from typing import Union

class ObjectMgr:
    def __init__(self, n: int):
        self.object_pool = deque(range(1, n))
        self.allocated_objects = set()
        self.freed_objects = deque()

Decided to use:

  • deque() - efficient element addition and removal.
    def get_object(self) -> Union[int, None]:
        if self.object_pool:
            obj = self.object_pool.popleft()
            self.allocated_objects.add(obj)
            return obj
        elif self.freed_objects:
            obj = self.freed_objects.popleft()
            self.allocated_objects.add(obj)
            return obj
        else:
            return None
  • set() - efficient to check whether an object is currently allocated or not.
    def free_object(self, obj) -> Union[int, None]:
        if obj in self.allocated_objects:
            self.allocated_objects.remove(obj)
            self.freed_objects.append(obj)
            return obj
        else:
            raise ValueError("Object is not currently allocated.")

Installation

  • Development

  1. Make sure your python is <= "3.10.*"

  2. Create virtual env, with:

python -m venv .venv
  1. move to app folder'
cd ./app
  1. Install requirements with pip:
pip install -r requirements.txt
  1. Or install requirements with poetry:
poetry install
  1. To run app:
flask run
flask --debug run
python app.py
gunicorn --worker-class gevent --workers 8 --bind 127.0.0.1:5000 app:"create_app()" --max-requests 10000 --timeout 5 --keep-alive 5 --log-level info --log-level info
  • To run with docker-compose

  1. create docker-compose.yml from:

    • docker-compose.dev.yml
    • or
    • docker-compose.prod.yml
  2. run

    docker-compose up

Production

To run application as linux service

  • create /etc/systemd/system/tromzo-service.service
[Unit]
Description=Gunicorn instance to serve
After=network.target

[Service]
User=mngr-user
Group=www-data
WorkingDirectory=/home/tromzo/app
Environment="PATH=/home/tromzo/app/.venv/bin"
ExecStart=/home/tromzo/app/.venv/bin gunicorn --worker-class gevent --workers 8 --bind 0.0.0.0:3000 app:"create_app()" --max-requests 10000 --timeout 5 --keep-alive 5 --log-level info

[Install]
WantedBy=multi-user.target
sudo systemctl start tromzo-service
sudo systemctl enable tromzo-service

To run application as container

docker run --publish 3000:3000 -it --entrypoint ./start.sh objmngr-server/graphql

config

  • in config.py if
default = "DevelopmentConfig"
  • visit {your_host}/graphql to see/test API definitions

plgrd

Query examples:

get_object() query:

Returns any object available in the pool

query {
  getObject
}

free_object() mutation:

Returns the object back to the pool so that it can be given out again

mutation {
    freeObject(obj: 1) {
        success
    }
}

postman_collection.json is available

Tests

  • To run tests
pytest

p.s They liked the solution and I was moved to the next step.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.