GithubHelp home page GithubHelp logo

pombredanne / pyfpm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from martinblech/pyfpm

0.0 1.0 0.0 260 KB

Scala-like functional pattern matching in Python

License: MIT License

Python 100.00%

pyfpm's Introduction

pyfpm

pyfpm stands for PYthon Functional Pattern Matching. It's been heavily inspired by the Pattern Matching and Case Classes implementation in Scala.

Build status at Travis CI: Build Status

Usage

With pyfpm you can unpack objects using the Unpacker class:

unpacker = Unpacker()
unpacker('head :: tail') << (1, 2, 3)
unpacker.head # 1
unpacker.tail # (2, 3)

or function parameters using the match_args decorator:

@match_args('[x:str, [y:int, z:int]]')
def match(x, y, z):
    return (x, y, z)

match('abc', (1, 2)) # ('abc', 1, 2)

You can also create simple matchers with lambda expressions using the Matcher class:

what_is_it = Matcher([
    ('_:int', lambda: 'an int'),
    ('_:str', lambda: 'a string'),
    ('x', lambda x: 'something else: %s' % x),
    ])

what_is_it(10)    # 'an int'
what_is_it('abc') # 'a string'
what_is_it({})    # 'something else: {}'

or more complex ones using the Matcher.handler decorator:

parse_options = Matcher()
@parse_options.handler("['-h'|'--help', None]")
def help():
    return 'help'
@parse_options.handler("['-o'|'--optim', level:int] if 1<=level<=5")
def set_optimization(level):
    return 'optimization level set to %d' % level
@parse_options.handler("['-o'|'--optim', bad_level]")
def bad_optimization(bad_level):
    return 'bad optimization level: %s' % bad_level
@parse_options.handler('x')
def unknown_options(x):
    return 'unknown options: %s' % repr(x)

parse_options(('-h', None))     # 'help'
parse_options(('--help', None)) # 'help'
parse_options(('-o', 3))        # 'optimization level set to 3'
parse_options(('-o', 0))        # 'bad optimization level: 0'
parse_options(('-v', 'x'))      # "unknown options: ('-v', 'x')"

For more information, see the files in the examples directory alongside the links within them, or read the docs.

Installation

pyfpm is in PyPi:

$ pip install pyfpm

pyfpm's People

Contributors

martinblech 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.