Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

patterns

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

patterns

Pattern matching for python

  • 0.3
  • PyPI
  • Socket score

Maintainers
1

Patterns

Pattern matching for python. Works in Python 2.7, 3.3+ and pypy.

Installation

::

pip install patterns

Usage

.. code:: python

from patterns import patterns, Mismatch

@patterns
def factorial():
    if 0: 1
    if n is int: n * factorial(n-1)
    if []: []
    if [x] + xs: [factorial(x)] + factorial(xs)
    if {'n': n, 'f': f}: f(factorial(n))

assert factorial(0) == 1
assert factorial(5) == 120
assert factorial([3,4,2]) == [6, 24, 2]
assert factorial({'n': [5, 1], 'f': sum}) == 121

factorial('hello') # raises Mismatch

See tests for more examples.

TODO

  • docs
  • aliases for structures
  • destructure objects
  • name parameter
  • better handling of Mismatch passing to function env
  • non-strict dict matching

.. |Build Status| image:: https://travis-ci.org/Suor/patterns.svg?branch=master :target: https://travis-ci.org/Suor/patterns

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc