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
This library is needed to obtain the source code of functions at runtime. It can be used, for example, as a basis for libraries that work with [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree) on the fly. In fact, it is a thin layer built around [`inspect.getsource`](https://docs.python.org/3/library/inspect.html#inspect.getsource) and [`dill.source.getsource`](https://dill.readthedocs.io/en/latest/dill.html#dill.source.getsource).
20
+
This library lets you retrieve a function's source code at runtime. It can serve as a foundation for tools that work with [ASTs](https://en.wikipedia.org/wiki/Abstract_syntax_tree). It is a thin wrapper around [`inspect.getsource`](https://docs.python.org/3/library/inspect.html#inspect.getsource) and [`dill.source.getsource`](https://dill.readthedocs.io/en/latest/dill.html#dill.source.getsource).
You can install [`getsources`](https://pypi.python.org/pypi/getsources)using pip:
33
+
You can install [`getsources`](https://pypi.python.org/pypi/getsources)with pip:
35
34
36
35
```bash
37
36
pip install getsources
38
37
```
39
-
40
-
You can also quickly try this package and others without installing them via [instld](https://github.com/pomponchik/instld).
38
+
You can also use [`instld`](https://github.com/pomponchik/instld) to quickly try this package and others without installing them.
41
39
42
40
43
-
## Get dirty sources
41
+
## Get raw source
44
42
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).
43
+
The standard library provides 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
44
47
-
This library defines a function of the same name that does the same thing but does not have this drawback:
45
+
This library provides a function with the same name and nearly the same interface, but without this limitation:
48
46
49
47
```python
50
48
# You can run this code snippet in the REPL.
@@ -58,14 +56,14 @@ print(getsource(function))
58
56
#> ...
59
57
```
60
58
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.
59
+
This makes AST-based tools work reliably in both scripts and the REPL. All other functions in the library are built on top of it.
62
60
63
61
64
-
## Get clear sources
62
+
## Get cleaned source
65
63
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.
64
+
The [`getsource`](#get-raw-source) function a function's source code in raw form. This means that the code snippet captures some unnecessary surrounding code.
67
65
68
-
Here's an example where the standard `getsources` function gets rid extra whitespace characters:
66
+
Here is an example where the standard `getsource` output includes extra leading whitespace:
69
67
70
68
```python
71
69
ifTrue:
@@ -77,16 +75,16 @@ print(getsource(function))
77
75
#> ...
78
76
```
79
77
80
-
See? There are extra spaces at the beginning.
78
+
> ↑ Notice the extra leading spaces.
81
79
82
-
Lambda functionsalso capture the entire surrounding string:
80
+
For lambda functions, it may also return the entire surrounding expression:
83
81
84
82
```python
85
83
print(getsource(lambdax: x))
86
84
#> print(getsource(lambda x: x))
87
85
```
88
86
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:
87
+
To address these issues, the library provides a function called `getclearsource`, which returns the function's source with unnecessary context removed:
When working with AST, this function is the recommended and safe way to retrieve the source code of functions.
105
+
When working with ASTs, this is the recommended way to retrieve a function's source code.
108
106
109
107
110
-
## Get hashes
108
+
## Generate source hashes
111
109
112
-
In some cases, you may not care what exactly is inside a function, but you need to distinguish between functions with different contents. In this case, the `getsourcehash` function is useful, as it returns a short string representation of the function’s source code hash:
110
+
In some cases, you may not care about a function's exact source, but you still need to distinguish between different implementations. In this case, the `getsourcehash` function is useful. It returns a short hash string derived from the function's source code:
> ⓘ 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.
122
+
> ⓘ A hash string uses only characters from the [`Crockford Base32`](https://en.wikipedia.org/wiki/Base32) alphabet, which consists solely of uppercase English letters and digits; ambiguous characters are excluded, which makes the hash easier to read.
125
123
126
-
> ⓘ The `getsourcehash` function operates on top of [`getclearsource`](#get-clear-sources) and ignores "extra" characters in the source code.
124
+
> ⓘ The `getsourcehash` function is built on top of [`getclearsource`](#get-cleaned-source) and ignores "extra" characters in the source code.
127
125
128
-
By default, the hash string length is 6 characters, but you can set your own values ranging from 4 to 8 characters:
126
+
By default, the hash string length is 6 characters, but you can choose a length from 4 to 8 characters:
By default, the full text representation of a function is used, including its name and arguments. However, in some cases, we need to compare only the contents of the functions while ignoring these details; in such cases, we need to pass the argument`only_body=True`:
135
+
By default, the full source code of a function is used, including its name and arguments. If you only want to compare function bodies, pass `only_body=True`:
0 commit comments