Skip to content

Commit f889c01

Browse files
Евгений БлиновЕвгений Блинов
authored andcommitted
Update README to reflect new function names and clarify usage
1 parent ae250fc commit f889c01

1 file changed

Lines changed: 23 additions & 25 deletions

File tree

README.md

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,32 @@
1717

1818
![logo](https://raw.githubusercontent.com/mutating/getsources/develop/docs/assets/logo_1.svg)
1919

20-
21-
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).
2221

2322

2423
## Table of contents
2524

2625
- [**Installation**](#installation)
27-
- [**Get dirty sources**](#get-dirty-sources)
28-
- [**Get clear sources**](#get-clear-sources)
29-
- [**Get hashes**](#get-hashes)
26+
- [**Get raw source**](#get-raw-source)
27+
- [**Get cleaned source**](#get-cleaned-source)
28+
- [**Generate source hashes**](#generate-source-hashes)
3029

3130

3231
## Installation
3332

34-
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:
3534

3635
```bash
3736
pip install getsources
3837
```
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.
4139

4240

43-
## Get dirty sources
41+
## Get raw source
4442

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).
4644

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:
4846

4947
```python
5048
# You can run this code snippet in the REPL.
@@ -58,14 +56,14 @@ print(getsource(function))
5856
#> ...
5957
```
6058

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.
6260

6361

64-
## Get clear sources
62+
## Get cleaned source
6563

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.
6765

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:
6967

7068
```python
7169
if True:
@@ -77,16 +75,16 @@ print(getsource(function))
7775
#> ...
7876
```
7977

80-
See? There are extra spaces at the beginning.
78+
> ↑ Notice the extra leading spaces.
8179
82-
Lambda functions also capture the entire surrounding string:
80+
For lambda functions, it may also return the entire surrounding expression:
8381

8482
```python
8583
print(getsource(lambda x: x))
8684
#> print(getsource(lambda x: x))
8785
```
8886

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:
9088

9189
```python
9290
from getsources import getclearsource
@@ -104,12 +102,12 @@ print(getclearsource(lambda x: x))
104102
#> lambda x: x
105103
```
106104

107-
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.
108106

109107

110-
## Get hashes
108+
## Generate source hashes
111109

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 functions 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:
113111

114112
```python
115113
from getsources import getsourcehash
@@ -121,11 +119,11 @@ print(getsourcehash(function))
121119
#> 7SWJGZ
122120
```
123121

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.
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.
125123
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.
127125
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:
129127

130128
```python
131129
print(getsourcehash(function, size=4))
@@ -134,7 +132,7 @@ print(getsourcehash(function, size=8))
134132
#> XG7SWJGZ
135133
```
136134

137-
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`:
138136

139137
```python
140138
def function_1():

0 commit comments

Comments
 (0)