Coder Social home page Coder Social logo

switcheroo's Introduction

CircleCI

Switcheroo

Efficient dispatch-based calling, that might be a switch statement in another language.

short usage

from switcheroo import Switch

switch = Switch({
    'foo': lambda x: x+1,
})
>>> switch['foo'](1)
2
>>> switch['bar'](1)
Traceback (most recent call last):
...
KeyError: 'bar'
from switcheroo import Switch, default

switch = Switch({
    'foo': lambda x: x+1,
    default: lambda x: x-1,
})
>>> switch['foo'](1)
2
>>> switch['bar'](1)
0

explicit usage

from switcheroo import Switch

def handle_foo(x):
    return x+1

def handle_others(x):
    return x-1

switch = Switch()
switch.register('foo', handler=handle_foo)
switch.default(handle_others)
>>> switch.lookup('foo')(1)
2
>>> switch.lookup('bar')(1)
0
>>> switch.override('foo', lambda x: x+2)
>>> switch.lookup('foo')(1)
3

decorator usage

from switcheroo import Switch

switch = Switch()

@switch.handles('foo')
def handle_foo(x):
    return x+1

@switch.default
def handle_others(x):
    return x-1
>>> switch['foo'](1)
2
>>> switch['bar'](1)
0
@switch.overrides('foo')
def new_handle_foo(x):
    return x+2
>>> switch['foo'](1)
3

class helper usage

class MoarThingz(object):

    switch = Switch()

    def __init__(self, state):
        self.state = state

    @switch.handles('foo')
    def handle_foo(self, x):
        return self.state - x

    @switch.default
    def handle_foo(self, x):
        return self.state + x

    def dispatch(self, case, factor, x):
        return factor * self.switch[case](self, x)
>>> things = MoarThingz(3)
>>> things.dispatch('foo', factor=1, x=1)
2
>>> things.dispatch('bar', factor=-1, x=2)
-5

subclass usage

from switcheroo import Switch, handles, default

class MySwitch(Switch):

    @handles('foo')
    def handles(x):
        return x+1

    @default
    def default(x):
        return x-1
>>> MySwitch['foo'](1)
2
>>> MySwitch['bar'](1)
0

changes

2.0.0 (20 Sep 2022)

  • Drop Python 2 support.

1.1.0 (26 Nov 2020)

  • Add support for overrides.
  • Add support for more explicit usage.

1.0.0 (27 Feb 2019)

  • 100% coverage checking and automated releases.

0.2.0 (13 Dec 2018)

  • Handle subclasses when using the subclass pattern.

0.1.0 (24 Nov 2018)

  • Initial release.

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.