Skip to main content

pypama

How to install

pip install pypama

Presentation

This package provides a pattern matching for list of objects. Just as Regex provides regular expression for strings, this package provides regular expression for other type of lists.

An example is worth a thousand words: assume you have a list

>>> example_list = ['a', 'a', 1, '', None, 'b', 'c',  'e']

For some reason, you know that there is an int and a None, and you want to extract that number and the 2 strings following the None

>>> from pypama import build_pattern, is_int, is_none
>>> g = build_pattern((~is_int).star(False), '(', is_int, ')', '.*', is_none, '(',ANY,ANY, ')')
>>> g.match(example_list).groups()
[[1], ['b', 'c']]
  • ~is_int will matching anything that’s not an integer

  • .star(False): equivalent to *? in regular expressions: repeat as many as necessary

  • parenthesis are for capturing groups

  • .* is short for ANY.star() (match anything, repeatedly)

Therefore the pattern above reads as follow: match anything that’s not an int, repeatedly, until you find an int that you capture in group 1. Then match anything until you find a None. That must be followed by two elements that you capture in group 2.

This is therefore very similar to the re package, but applied to a list and with matching that goes beyond strings.

Usage

build_pattern(*args, **functions) where args is a list of strings or Pattern objects, and functions maps function names to executable.

The args can be a string or a list of strings containing:
  • <string>: will match the string inside <>

  • callable unction: X will match if function(X) returns True

  • X?: will be ignored or match X

  • X*: will match if X matches repeatedly (greedy)

  • X*?: will match if X matches repeatedly (non greedy)

  • (X): will capture the X in a group

  • (P<name>X): will capture X in a group named “name”

  • X|Y: will match if X or Y matches

  • \n: will match the nth group previously matched

  • <r:X>: will match if the word matches regexp X

  • <re:X>: same as <r:X>

  • <c:function> or <call:function>: will match if function(X) matches. If function does not exist in the namespace of pypama, add the definition in the function call eg build_pattern('<c:foo>', foo=lambda x:x<2) will match [1]

  • . will match any item

  • $ will match if at the end of the list

  • X{n} will match X exactly n times. n can be a list of integer

  • X!: will match if the item doesn’t match X (X must match exactly 1 element)

Both are equally valid and evaluate to the same result;
  • build_pattern('<hello>','<world>','.*')

  • build_pattern('<hello><world>.*')

Metadata

Release files for pypama 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pypama 1.1
File Size Uploaded Provenance
pypama-1.1.tar.gz 6.3 kB Without provenance Details

Release files / pypama-1.1.tar.gz

Download URL pypama-1.1.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
efe66ce774afd35add9b91fa1600716118d4c8ad44bf49ffc05053c0f97a3c6a
BLAKE2b-256 checksum
How to use checksums
980ff4817eff671c038a4420cab5dd5e28423e7877429f9977eb9cc16fe8c463
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/38.4.0 requests-toolbelt/0.8.0 tqdm/4.32.1 CPython/3.6.9

Security for pypama 1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page