Skip to main content
https://travis-ci.org/nlaurance/sourceparse.svg?branch=master

sourceparse

a personal adaptation of pyclbr from the standard python lib

let’s say we want to analyze a source file like this one:

>>> source = '''
... class MixinUser(Sub2, Mixin):
...     """Overrides method1 and method2
...     """
...
...     def method1(self, foo):
...         """ method1 of MixinUser
...         """
...         return
...
...     @manytimes
...     @decorated
...     def method2(self, foo, bar=None):
...         """ method2 of MixinUser
...         """
...         return
...
... # comment
...
... def my_function(foo):
...     """ Stand-alone function.
...     """
...     return
... '''

Of course we’ll start by importing the tool

>>> from sourceparse import CodeCollector
>>> from pprint import pprint

for the purpose of this documentation we’ll override the _readfile method:

>>> def override(foo): return [s+'\n' for s in source.split('\n')]
...
>>> original = CodeCollector._readfile
>>> CodeCollector._readfile=override

instanciation

let’s instantiate a parser, normally we would pass a path to the file to analyze:

>>> parser=CodeCollector("source")

Access to members

Classes

we can now access a list of the classes defined in the module:

>>> parser.classes
[Class MixinUser: from 2 to 19
]

Methods

each class:

>>> mix = parser.classes[0]

can list its methods:

>>> mix.methods
[Method method1: from 6 to 10
, Method method2: from 13 to 19
    decorated from 11 to 13]

each method:

>>> m2 = mix.methods[1]

has a name:

>>> m2.name
'method2'

a start line in the file:

>>> m2.from_line
13

an end line:

>>> m2.to_line
19

wa can access its docstring:

>>> m2.docstring
'method2 of MixinUser\n    '

decorators:

>>> m2.decorators
['    @manytimes\n', '    @decorated\n']

arguments, excluding self:

>>> m2.args
['foo']

and keyword arguments:

>>> m2.kwargs
{'bar': 'None'}

and its complete source, excluding decorators:

>>> pprint(m2.source)
['    def method2(self, foo, bar=None):\n',
 '        """ method2 of MixinUser\n',
 '        """\n',
 '        return\n',
 '\n',
 '# comment\n',
 '\n']

Functions

the module functions provide the same features:

>>> parser.functions
[Function my_function: from 20 to 24
]
>>> my = parser.functions[0]
>>> my.decorators
[]
>>> my.docstring
'Stand-alone function.\n    '
>>> my.args
['foo']

>>> my.from_line
20
>>> my.to_line
24
>>> pprint(my.source)
['def my_function(foo):\n',
 '    """ Stand-alone function.\n',
 '    """\n',
 '    return\n',
 '\n']

let’s put the parser back to normal

>>> CodeCollector._readfile = original

Metadata

Release files for sourceparse 0.2.5

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

Source distribution (sdist)

Source distribution for sourceparse 0.2.5
File Size Uploaded Provenance Actions
sourceparse-0.2.5.tar.gz 6.7 kB Without provenance Details for sourceparse-0.2.5.tar.gz

Release files / sourceparse-0.2.5.tar.gz

Download URL sourceparse-0.2.5.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
15e9286e06074818b5372d859faa6533bd5547a909684f8f7bb72dcab3473752
BLAKE2b-256 checksum
How to use checksums
fca839526ee300f02553e4f0a91472e762c741f116da88e9789e9b44f60cc232
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for sourceparse 0.2.5

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.

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