Reads a python file/module and statically analyzes it.
Project description
textpy
Reads a python file/module and statically analyzes it, this works well with Jupyter extensions in VScode.
Installation
pip install textpy
Requirements
pandas>=1.4.0
# A lower version is also acceptable, but some features will be invalid.
Examples
Create a new file named myfile.py
under ./examples/
(or any dir, just for an example):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from typing import *
class MyClass:
def __init__(self):
"""Write something."""
self.var_1 = "hahaha"
self.var_2 = "blabla"
def print_my_class(a: MyClass):
"""
Print something.
Parameters
----------
a : ThisIsAClass
An object.
"""
print(a.var_1, a.var_2)
Run the following codes to find all the occurrences of the pattern "va"
in myfile.py
:
from textpy import textpy
res = textpy("./examples/myfile.py").findall("va", styler=False)
print(res)
# Output:
# examples/myfile.py:10: ' self.var_1 = "hahaha"'
# examples/myfile.py:11: ' self.var_2 = "blabla"'
# examples/myfile.py:24: ' print(a.var_1, a.var_2)'
Also, when using a Jupyter notebook in VScode, you can run a cell like this:
from textpy import textpy
textpy("./examples/myfile.py").findall("va")
and the output will be like:
source | match |
---|---|
myfile.MyClass.__init__:10 | self.var_1 = "hahaha" |
myfile.MyClass.__init__:11 | self.var_2 = "blabla" |
myfile.print_my_class:24 | print(a.var_1, a.var_2) |
Note that in the Jupyter notebook case, the matched substrings are clickable, linking to where the patterns were found.
Now suppose you've got a python module consists of a few files, for example, our textpy
module itself, you can do almost the same thing:
module_path = "textpy/" # you can type any path here
pattern = "note.*k" # type any regular expression here
res = textpy(module_path).findall("note.*k", styler=False, line_numbers=False)
print(res)
# Output:
# textpy/abc.py: ' in a Jupyter notebook, this only takes effect when'
# textpy/abc.py: ' in a Jupyter notebook.'
See Also
Github repository
PyPI project
License
This project falls under the BSD 2-Clause License.
History
v0.1.12
- New optional parameters for
textpy.TextPy.findall
:whole_word
: whether to match whole words only.case_sensitive
: specifies case sensitivity.
v0.1.10
- New optional parameter for
textpy.textpy
:encoding
: specifies encoding.
v0.1.9
- Removing unnecessary dependencies.
v0.1.8
- Bugfix for Windows.
v0.1.5
- Compatible with pandas versions lower than 1.4.0.
- Updated
textpy.textpy
:Path
objects are now acceptable as parameters.- New optional parameter
home
to specify the home path.
- More flexible presentation of output when using
textpy.TextPy.findall
.
v0.1.4
- Fixed a display problem of
README.md
on PyPI.
v0.1.3
- Initial release.
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 Distributions
Built Distribution
Hashes for textpy-0.1.12-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48efb6f516a3f19587d050c68ba49fc90b43c0f0ad4c6ff689b631bbaa3aead8 |
|
MD5 | d8b75768bcedfc0a8c22fa03e2da5b47 |
|
BLAKE2b-256 | 2e3b23f6ffc5b3d371f15b80693704ea48cc8bf70b9c044e6e2b600cdadede6c |