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
+17-4Lines changed: 17 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,13 @@
21
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).
22
22
23
23
24
+
## Table of contents
25
+
26
+
-[**Installation**](#installation)
27
+
-[**Get dirty sources**](#get-dirty-sources)
28
+
-[**Get clear sources**](#get-clear-sources)
29
+
-[**Get hashes**](#get-hashes)
30
+
24
31
## Installation
25
32
26
33
You can install [`getsources`](https://pypi.python.org/pypi/getsources) using pip:
@@ -29,10 +36,10 @@ You can install [`getsources`](https://pypi.python.org/pypi/getsources) using pi
29
36
pip install getsources
30
37
```
31
38
32
-
You can also quickly try out this and other packages without having to install using[instld](https://github.com/pomponchik/instld).
39
+
You can also quickly try this package and others without installing them via[instld](https://github.com/pomponchik/instld).
33
40
34
41
35
-
## Usage
42
+
## Get dirty sources
36
43
37
44
The basic function of the library is `getsource`, which works similarly to the function of the same name from the standard library:
38
45
@@ -50,7 +57,10 @@ print(getsource(function))
50
57
Unlike its counterpart from the standard library, this thing can also work:
51
58
52
59
- With lambda functions (however, keep in mind that the entire text of the line where they are defined is returned, and if there are multiple lambda functions there, the library won't let you distinguish between them)
53
-
- With functions defined inside REPL
60
+
- With functions defined inside `REPL`
61
+
62
+
63
+
## Get clear sources
54
64
55
65
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:
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`.
81
+
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).
82
+
83
+
84
+
## Get hashes
72
85
73
86
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:
0 commit comments