deal with object reference notations
Project description
objectref
objectref is a library to parse object references and find referenced objects.
Notations
Object references are notations to locate modules or objects from the top level of a module.
package.module
package.module:object.attr
The first form simply refers a module, while the second form an object in module. It is also described in here.
This library also supports relative object references (prefixed with dots).
Usage
-
find(ref: str, package: str | None = None) -> AnyThe
findfunction takes an object reference orparse()result, and returns the object pointed by the reference.The
packageargument must be set when the reference is relative.If qualified name is unspecified (the first form), the result is a module (
types.ModuleTypeinstance).ModuleNotFoundErroris raised if the module cannot be found nor imported;LookupErrorif the object is not accessible from the top level of the module. -
parse(ref: str) -> tuple[str, str | None]The
parsefunction takes an object reference, and results in a tuple of module name and qualified name. The qualified name would beNonein first form.ValueErroris raised if the reference is invalid.
Example
import os.path
import objectref
print(objectref.find("os.path") == os.path) # True
print(objectref.find("os.path:join") == os.path.join) # True
print(objectref.parse("os.path")) # ("os.path", None)
print(objectref.parse("os.path:join")) # ("os.path", "join")
Entrypoint example
There are two main ways to execute a Python program.
-
Run
python -m myapp.mainin shells, which executes the module as__main__under the hood. This relates to the first form of notation. -
Run
myappinstalled by pip. It is generated according to entrypoint defined in the project metadata (e.g.myapp = myapp.main:main). This requires the second form notation with callable objects.
Here is a code snippet to unify these two ways and start a program by its object reference.
import objectref
def run(ref: str, package: str | None = None):
modname, qualname = objectref.parse(ref)
if qualname is None:
# Use runpy to run a module with __name__ = "__main__"
if modname.startswith("."):
# runpy does not support relative module name.
from importlib.util import resolve_name
modname = resolve_name(modname, package)
import runpy
return runpy.run_module(modname, run_name="__main__")
fn = objectref.find((modname, qualname), package)
return fn()
if __name__ == "__main__":
run("this") # run the "this" module dynamically.
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 objectref-2023.10.31.tar.gz.
File metadata
- Download URL: objectref-2023.10.31.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9fdd7487ab58c98160ad25978f41d2bec9acbc8bd7c39871e0c01785661401f5
|
|
| MD5 |
16979c9b81dcd96e33de5f9e310beadb
|
|
| BLAKE2b-256 |
44cfe30f6dce72122cb00e9d20ece2b798f42a98aa56b516050bd2957ae02cff
|
File details
Details for the file objectref-2023.10.31-py3-none-any.whl.
File metadata
- Download URL: objectref-2023.10.31-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03c1df453a4cf349c3ac4862f4ec65788efecd0814ac20c65f11dde361595aa8
|
|
| MD5 |
e141a6682664023bee61bbc83f30700a
|
|
| BLAKE2b-256 |
aaef005617e34ec167d841a838c0b5b047c19ab56f843522fa83ae606ff6af5b
|