Use top-level variable references in match cases
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file match-ref-1.0.1.tar.gz.
File metadata
- Download URL: match-ref-1.0.1.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71e06d8096c12e184651353c150174c73f3bedc773fc67133cb17673f12e337e
|
|
| MD5 |
b7a66bdd62bedc57462f9827611b0bef
|
|
| BLAKE2b-256 |
bff54a9faf6e92c6666df6a38f17a08d659ec8be5254415a79b1b0deefdffac3
|
File details
Details for the file match_ref-1.0.1-py3-none-any.whl.
File metadata
- Download URL: match_ref-1.0.1-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d918d3f85a6cf32e392f6fb7b5324de2943f7099f7446fd9e7c70c0fd85feba
|
|
| MD5 |
e72515663d675c29884566d922b0c509
|
|
| BLAKE2b-256 |
8c14c9ed1866b650231eec99060ce9f54bc17a0b0cb4f70f98c099d7c5641df3
|