Giter VIP home page Giter VIP logo

oddsquarehuffmanservice's Introduction

Odd square Huffman service

A service with an odd square number list, huffman encode and huffman decode methods

Features

  • An odd square number list method that returns a list with odd numbers squared.
  • A huffman encode method that returns a huffman encoded string.
  • A huffman decode method that returns a huffman decoded string, requires a given or stored frequency table.

Requirements

Tested with:

  • Python: 3.6, Pypy 3.5
  • rabbitmq: 3.9
  • docker: 20.10
  • docker-compose: 1.29

Installation

  1. git clone [email protected]:cblignaut/oddsquarehuffmanservice.git
  2. Install python 3.6, docker and docker-compose
  3. Go to root directory of cloned repo
  4. Run to build docker service container:
   docker-compose build
  1. Run to start docker services as a daemon:
   docker-compose up -d

Usage

Start nameko shell:

  1. Once the docker services are up (see installation)
  2. Bash into the docker service:
   docker exec -ti service bash 
  1. Run nameko shell:
   nameko shell --config config.yml

Square odd numbers:

Run the square odd numbers method in the nameko shell:

   n.rpc.service.square_odd_numbers([1,2,3,4,5])

Huffman encode a list of strings:

Run the huffman encode method in the nameko shell:

   n.rpc.service.huffman_encode(['test'])

Huffman decode an encoded string:

To decode a huffman encoded string a stored frequency table is required. Note: For this example the service will store all encoded strings in memory (this is not a production solution).

Run the huffman decode method in the nameko shell:

  1. With a frequency table (recommended option):
   from collections import Counter
   
   frequency_table = Counter("A man")
   
   n.rpc.service.huffman_decode('110101110100', frequency_table)
  1. For a previously encoded string ('test'), please read warning in the design decisions before using this option:
   n.rpc.service.huffman_decode('010110')

Task

It was a fun open-ended task with a few gotchas (see design decisions). Nameko looks like a good alternative to flask for microservices.

Pros:

  • The Nameko & Rabbitmq works well together.
  • Nameko reduce CPU cycle & memory usage to help with scaling
  • Easy to monitor and maintain services.

Cons:

  • Increased latency of each call to other non microservice solutions.
  • Nameko is less well known with fewer examples and documentation than other microservice python options such as flask.

Design Decisions:

Huffman module

The first options I found was dahuffman & huffman (outdated).

The dahuffman module looked like a good option but the task requested PyPy. Execution speed is important.

I used the bitarray module before which was written in CPython, I tested the decodetree method and it is faster than dahuffman.

Decoding huffman

To decode a huffman string we need a frequency table of the original string. The task didn't specify if we will get a frequency table.

I decided to create an optional argument for the frequency table but also store the frequency table of the previously encoded strings as a fallback in memory.

n.rpc.service.huffman_decode('110101110100', OPTIONAL_FREQUENCY_TABLE)
WARNING: WHEN USING THE huffman_decode() METHOD WITHOUT A FREQUENCY TABLE ARGUMENT:

Ignoring the frequency table argument will return the previous value for an encoded string (requirement). 

If a different string had the same encoded string then the previous encoded string will be overwritten.

Currently, the frequency table is stored in memory (scaling risk) and will be lost if the service restart.
A better solution would be to store the frequency table in a key-value store like redis or always require the client to supply the frequency table.

Time Management:

Unfortunately I had to do this task while working on assignments for my MEng. Data Science & Machine Learning and my time was limited.

  1. I did some research on huffman encoding & decoding 1h
  2. Search for a huffman package to use, this took longer than I expected 2h
  3. Write tests & tox 1h
  4. Coding & cleanup 3h
  5. Write README 2h

Contribution

Please create an issue New Issue

License

See LICENSE file for more information.

oddsquarehuffmanservice's People

Contributors

cblignaut avatar

Watchers

 avatar

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.