Giter Site home page Giter Site logo

python-rpncalc-20170506's Introduction

RPN Calculator Engine for Python Workshops

The purpose of this project is to create a generalized calculator engine for use in Python workshops teaching how to use git, pull requests, and other open-source contribution processes.

The Calculator

This calculator uses Reverse-Polish Notation to express functions.

Sample Usage

Instantiate the calculator, then apply operations by calling push() and execute(). The only built-in functions are noop (no-operation), + (add), and / (floating-point division); additional desired functionality can be registered as needed (see Extending the Calculator).

import rpncalculator

calc = rpncalculator.Engine()
calc.push(1)
calc.execute('noop')
calc.push(2)
calc.push(3)
calc.execute('+')  # returns 5

To get a bare-bones calculator with no registered functions other than noop, pass auto_register=False to the constructor:

import rpncalculator

calc = rpncalculator.Engine(auto_register=False)
calc.execute('noop')  # succeeds
calc.execute('+')     # fails, function is not defined

Unit Tests

Installing the test harness

You might need to install setuptools in your environment:

pip install setuptools

Once setuptools is installed, all testing is handled through setup.py.

Running the test suite

The easiest way to run the test suite is using setup.py:

python setup.py test

Adding new tests

If new tests need to be added to the project, put them in test/<module_name>.py and add from <module_name> import * to test/__init__.py.

Extending the Calculator

Additional functions can be added to the calculator using the register(name, function) function:

def add(engine):
    engine.push(engine.pop() + engine.pop())

calc.register('+', add)

Registered functions must have the signature fn(engine) or the call to register will fail. An attempt to register the same function name twice will throw an error. The function may perform any sequence of calls to push, pop, and execute; however, to be RPN-correct it should only pop exactly the number of times needed to retrieve the necessary values and then push once (performing necessary algorithmic steps in-between).

Using the Parser

Instead of interacting programmatically with the calculator, you can also supply input as a string or as an open file object:

import rpncalculator

# Note: this example assumes the '+' operation has been registered
# as previously demonstrated.

parser = rpncalculator.Parser()
result = parser.process('1 1 +')    # result = 2

with open(myinputfile) as f:
    result = parser.process(f)

Files can be arbitrarily large; the tokenization engine is smart about only tokenizing small chunks of the input at a time.

python-rpncalc-20170506's People

Contributors

seawolf42 avatar

Watchers

James Cloos avatar  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.