Match-Ref
Top-Level Variable References For Python's match
Objective
This library provides a convenient interface to variables in the local and global namespaces, using qualified (dotted) names.
Python 3.10 introduced the match-case structure
which allows you to write flow control code like this:
my_choice = input("Your choice: ")
fruit_on_sale = "peach"
match my_choice:
case "apple": print("Delivering an apple.")
case "banana": print("Delivering a banana.")
case fruit_on_sale:
print("Good choice!")
If you now entered a value like tomato, he output would be fruit_on_sale. This is, because
fruit_on_sale is considered a placeholder/capture pattern and not a reference to a variable.
Therefore, we need another way to reference variables. We can accomplish this using dotted names (i.e. fruit_data.fruit_on_sale, because Python does not interpret these names as placeholders.
Installation
You can install this package using pip :
pip install match-refon all platforms, if availablepython3 -m pip install match-refon Unixpy -m pip install match-refon Windows
Usage
from matchref import ref
my_choice = "banana"
fruit_on_sale = "peach"
match my_choice:
case ref.fruit_on_sale: print("Good choice!")
You can use the ref helper to resolve all variables in the current local and global namespaces which you could usually reference without using a dotted name.
You can use ref.any_local_or_global_variable_name in any expression, not only inside case conditions, if you need to.
Only value retrieval is supported, as attribute-setting is not the purpose of this library.
How does it work?
ref is the default instance of the ScopeReference class, defined in match-ref. It has a method __getattr__ which gets called whenever you request an attribute of ref. This method then starts looking up the local namespace it was called from (your script or your function) and, if it doesn't find the variable you're looking for, continues searching the global namespace as well.
If it doesn't find the variable you are looking for, the following error is being raised:
ValueError: referenced name '...' is not defined
Metadata
Release files for match-ref 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| match-ref-1.0.1.tar.gz | 3.1 kB | Without provenance | Details for match-ref-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| match_ref-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for match_ref-1.0.1-py3-none-any.whl |
Total release size: 6.2 kB
Release files / match-ref-1.0.1.tar.gz
| Download URL | match-ref-1.0.1.tar.gz |
|---|---|
| Size | 3.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
71e06d8096c12e184651353c150174c73f3bedc773fc67133cb17673f12e337e
|
|
BLAKE2b-256 checksum How to use checksums |
bff54a9faf6e92c6666df6a38f17a08d659ec8be5254415a79b1b0deefdffac3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.6
|
Release files / match_ref-1.0.1-py3-none-any.whl
| Download URL | match_ref-1.0.1-py3-none-any.whl |
|---|---|
| Size | 3.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9d918d3f85a6cf32e392f6fb7b5324de2943f7099f7446fd9e7c70c0fd85feba
|
|
BLAKE2b-256 checksum How to use checksums |
8c14c9ed1866b650231eec99060ce9f54bc17a0b0cb4f70f98c099d7c5641df3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.6
|
Security for match-ref 1.0.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.