Skip to content

Commit ae250fc

Browse files
Евгений БлиновЕвгений Блинов
authored andcommitted
Update README to clarify getsource behavior and add getclearsource
examples
1 parent 05c4a22 commit ae250fc

1 file changed

Lines changed: 34 additions & 7 deletions

File tree

README.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ You can also quickly try this package and others without installing them via [in
4242

4343
## Get dirty sources
4444

45-
The basic function of the library is `getsource`, which works similarly to the function of the same name from the standard library:
45+
The standard library includes the [`getsource`](https://docs.python.org/3/library/inspect.html#inspect.getsource) function that returns the source code of functions and other objects. However, this does not work with functions defined in the [`REPL`](https://docs.python.org/3/tutorial/interpreter.html#interactive-mode).
46+
47+
This library defines a function of the same name that does the same thing but does not have this drawback:
4648

4749
```python
50+
# You can run this code snippet in the REPL.
4851
from getsources import getsource
4952

5053
def function():
@@ -55,15 +58,35 @@ print(getsource(function))
5558
#> ...
5659
```
5760

58-
Unlike its counterpart from the standard library, this thing can also work:
59-
60-
- With lambda functions
61-
- With functions defined inside `REPL`
61+
This way, you can ensure that your functions that work with ASTs can be executed in any way. All other functions in this library are built on top of this one.
6262

6363

6464
## Get clear sources
6565

66-
We also often need to trim excess indentation from a function object to make it easier to further process the resulting code. To do this, use the `getclearsource` function:
66+
The [`getsource`](#get-dirty-sources) function returns the source code of functions in a "raw" format. This means that the code snippet captures some unnecessary surrounding code.
67+
68+
Here's an example where the standard `getsources` function gets rid extra whitespace characters:
69+
70+
```python
71+
if True:
72+
def function():
73+
...
74+
75+
print(getsource(function))
76+
#> def function():
77+
#> ...
78+
```
79+
80+
See? There are extra spaces at the beginning.
81+
82+
Lambda functions also capture the entire surrounding string:
83+
84+
```python
85+
print(getsource(lambda x: x))
86+
#> print(getsource(lambda x: x))
87+
```
88+
89+
To address these issues, there is a special function called `getclearsource`, which returns the original function's code but stripped of any unnecessary elements:
6790

6891
```python
6992
from getsources import getclearsource
@@ -77,9 +100,11 @@ print(getclearsource(SomeClass.method))
77100
#> @staticmethod
78101
#> def method():
79102
#> ...
103+
print(getclearsource(lambda x: x))
104+
#> lambda x: x
80105
```
81106

82-
As you can see, the resulting source code text has no extra indentation, but in all other respects this function is completely identical to the [usual `getsource`](#get-dirty-sources).
107+
When working with AST, this function is the recommended and safe way to retrieve the source code of functions.
83108

84109

85110
## Get hashes
@@ -98,6 +123,8 @@ print(getsourcehash(function))
98123

99124
> ⓘ A hash string contains only characters from the [`Crockford Base32`](https://en.wikipedia.org/wiki/Base32) alphabet, which consists solely of uppercase English letters and digits; letters that resemble digits are excluded from the list, making the hash easy to read.
100125
126+
> ⓘ The `getsourcehash` function operates on top of [`getclearsource`](#get-clear-sources) and ignores "extra" characters in the source code.
127+
101128
By default, the hash string length is 6 characters, but you can set your own values ranging from 4 to 8 characters:
102129

103130
```python

0 commit comments

Comments
 (0)