You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+34-7Lines changed: 34 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,9 +42,12 @@ You can also quickly try this package and others without installing them via [in
42
42
43
43
## Get dirty sources
44
44
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:
46
48
47
49
```python
50
+
# You can run this code snippet in the REPL.
48
51
from getsources import getsource
49
52
50
53
deffunction():
@@ -55,15 +58,35 @@ print(getsource(function))
55
58
#> ...
56
59
```
57
60
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.
62
62
63
63
64
64
## Get clear sources
65
65
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
+
ifTrue:
72
+
deffunction():
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(lambdax: 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:
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.
83
108
84
109
85
110
## Get hashes
@@ -98,6 +123,8 @@ print(getsourcehash(function))
98
123
99
124
> ⓘ 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.
100
125
126
+
> ⓘ The `getsourcehash` function operates on top of [`getclearsource`](#get-clear-sources) and ignores "extra" characters in the source code.
127
+
101
128
By default, the hash string length is 6 characters, but you can set your own values ranging from 4 to 8 characters:
0 commit comments