Giter VIP home page Giter VIP logo

smasher's Introduction

smasher

Typed Exception-Based Message Passing for Python

At some point in the recent past, I found myself enjoying the process of creating my own exceptions for specific business logic needs at my Python day job. So, naturally, I thought: what if you could use exceptions for all control flow in Python?

smasher applications have two main components: Actor objects that communicate with each other by way of raised exceptions, called Atoms, and the global Atoms namespace.

Looking at an example, we see:

class SimpleGame(Actor):
    atoms = ['Correct', 'Wrong', 'Win', 'KeepGoing', 'Done']

Actor classes declare their own atoms, which are instantiated as subclasses of the Atom exception class. Actors catch atoms using the dispatch decorator:

@dispatch({('Wrong', 'ValueError', 'Again'): 'take_a_guess',
           'Correct': 'correct',
           'KeepGoing': 'play_a_game',
           'Guess': ('evaluate', int)})
def take_a_guess(self):
    self.server.get_guess()

Here we see that the main locus for control flow is the take_a_guess method. If any of the Wrong, ValueError, or Again exceptions are raised, we recurse; the Correct and KeepGoing atoms are routed to the appropriate methods; finally, we see that the Guess atom is expected to contain a single element, an int, which will be passed to the evaluate method. Atoms can also be dispatched to raw lambdas or even other atoms, though in practice it will usually suffice simply to catch the original signal further up.

smasher's People

Contributors

subsetpark avatar

Watchers

 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.