forked from clarete/forbiddenfruit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
18 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,23 +4,33 @@ | |
|
||
![Forbidden Fruit](logo.png) | ||
|
||
This project aims to help you reach heaven while writing tests, but it | ||
may lead you to hell if used on production code. | ||
This project allows Python code to extend built-in types. | ||
|
||
It basically allows you to patch built-in objects, declared in C through | ||
python. Just like this: | ||
If that's a good idea or not, you tell me. The first need this project | ||
attended was allowing a [Python assertion | ||
library](https://github.com/gabrielfalcao/sure) to implement a similar | ||
API to [RSpec | ||
Expectations](https://github.com/rspec/rspec-expectations) and | ||
[should.js](https://shouldjs.github.io/). But people got creative and | ||
used it to among other things [spy on | ||
things](https://github.com/ikamensh/flynt/blob/43a64ac1a030be79741402d8920a6da253a96670/src/flynt/file_spy.py) | ||
or to [integrate | ||
profiling](https://github.com/localstack/localstack/blob/e38eae0d1fe442924f4256d4bc87710a4cb6f142/localstack/utils/analytics/profiler.py). | ||
|
||
## Tiny Example | ||
|
||
It basically allows you to patch built-in objects, declared in C | ||
through python. Just like this: | ||
|
||
1. Add a new method to the `int` class: | ||
```python | ||
>>> from forbiddenfruit import curse | ||
>>> def words_of_wisdom(self): | ||
... return self * "blah " | ||
>>> curse(int, "words_of_wisdom", words_of_wisdom) | ||
>>> assert (2).words_of_wisdom() == "blah blah " | ||
``` | ||
|
||
Boom! That's it, your `int` class now has the `words_of_wisdom` method. Do | ||
you want to add a `classmethod` to a built-in class? No problem, just do this: | ||
|
||
2. Add a `classmethod` to a built-in class | ||
```python | ||
>>> from forbiddenfruit import curse | ||
>>> def hello(self): | ||
|
@@ -50,9 +60,6 @@ the versions 2.5, 2.6, 2.7, 3.2 and 3.3. Since Forbidden Fruit is fundamentally | |
dependent on the C API, this library won't work on other python | ||
implementations, such as Jython, pypy, etc. | ||
|
||
I might add support for pypy in the future, but It's unlikely that I'll do it | ||
for Jython. But I could happily accept patches for them. | ||
|
||
## License | ||
|
||
Copyright (C) 2013,2019 Lincoln Clarete <[email protected]> | ||
|