Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wbolster/qualname
__qualname__ emulation for older Python versions
https://github.com/wbolster/qualname
Last synced: about 2 months ago
JSON representation
__qualname__ emulation for older Python versions
- Host: GitHub
- URL: https://github.com/wbolster/qualname
- Owner: wbolster
- License: other
- Created: 2015-04-11T22:34:22.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-05-25T09:27:22.000Z (over 4 years ago)
- Last Synced: 2024-10-06T15:35:23.420Z (3 months ago)
- Language: Python
- Size: 14.6 KB
- Stars: 27
- Watchers: 4
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.rst
- License: LICENSE.rst
Awesome Lists containing this project
README
========
qualname
========Python module to emulate the ``__qualname__`` attribute for classes and methods
(Python 3.3+) in older Python versions. See `PEP 3155`__ for details.__ https://www.python.org/dev/peps/pep-3155/
.. image:: https://travis-ci.org/wbolster/qualname.svg?branch=master
:target: https://travis-ci.org/wbolster/qualname.. image:: https://img.shields.io/pypi/dm/qualname.svg
:target: https://pypi.python.org/pypi/qualname/
:alt: Downloads.. image:: https://img.shields.io/pypi/v/qualname.svg
:target: https://pypi.python.org/pypi/qualname/
:alt: Latest Version.. image:: https://img.shields.io/pypi/pyversions/qualname.svg
:target: https://pypi.python.org/pypi/qualname/
:alt: Supported Python versions.. image:: https://img.shields.io/pypi/status/qualname.svg
:target: https://pypi.python.org/pypi/qualname/
:alt: Development Status.. image:: https://img.shields.io/pypi/l/qualname.svg
:target: https://pypi.python.org/pypi/qualname/
:alt: LicenseInstallation
============::
pip install qualname
Usage
=====Assume these definitions:
::
class C:
def f():
passclass D:
def g():
passIn Python 3.3+, classes have a ``__qualname__`` property::
>>> C.__qualname__
'C'
>>> C.f.__qualname__
'C.f'
>>> C.D.__qualname__
'C.D'
>>> C.D.g.__qualname__
'C.D.g'Unfortunately, Python 2 and early Python 3 versions do not have an (obvious)
equivalent. ``qualname`` to the rescue::from qualname import qualname
>>> qualname(C)
'C'
>>> qualname(C.f)
'C.f'
>>> qualname(C.D)
'C.D'
>>> qualname(C.D.g)
'C.D.g'Victory!
How does it work?
=================Glad you ask.
This module uses source code inspection to figure out how (nested) classes and
functions are defined in order to determine the qualified names for them. That
means parsing the source file, and traversing the AST (abstract syntax tree).
This sounds very hacky, and it is, but the Python interpreter itself does not
have the necessary information, so this justifies extreme measures.Now that you know how it works, you'll also understand that this module only
works when the source file is available. Fortunately this is the case in most
circumstances.License
=======BSD.
Feedback? Issues?
=================https://github.com/wbolster/qualname