Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zTrix/reprs
Blazing fast repr and eval like string encoding/decoding for python/go!
https://github.com/zTrix/reprs
Last synced: about 1 month ago
JSON representation
Blazing fast repr and eval like string encoding/decoding for python/go!
- Host: GitHub
- URL: https://github.com/zTrix/reprs
- Owner: zTrix
- License: other
- Created: 2016-04-03T09:34:14.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-05-20T10:06:49.000Z (over 3 years ago)
- Last Synced: 2024-08-02T05:11:36.893Z (4 months ago)
- Language: C
- Homepage:
- Size: 9.77 KB
- Stars: 17
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-blazingly-fast - reprs - Blazing fast repr and eval like string encoding/decoding for python/go! (C)
README
# reprs
reprs provides a safe and blazing-fast implementation for python `repr` and `eval` like string encoding/decoding: `reprs` and `evals`.
Behold!
```python
>>> from reprs import reprs, evals
>>> a = "asdf;lkjs\x00\n\xff\xaafasdf">>> print reprs(a)
asdf;lkjs\x00\n\xff\xaafasdf>>> evals(reprs(a)) == a
True
```## speed test
```python
In [1]: import random
In [2]: from reprs import reprs, evals, pyreprs, pyevalsIn [3]: t = ''.join(chr(random.choice(range(256))) for _ in range(1024))
In [4]: len(t)
Out[4]: 1024In [5]: %timeit reprs(t)
100000 loops, best of 3: 3.22 µs per loopIn [6]: %timeit repr(t)
10000 loops, best of 3: 64.2 µs per loopIn [7]: %timeit pyreprs(t)
1000 loops, best of 3: 742 µs per loopIn [8]: x = reprs(t)
In [9]: len(x)
Out[9]: 2983In [10]: %timeit evals(x)
100000 loops, best of 3: 3.68 µs per loopIn [11]: %timeit pyevals(x)
1000 loops, best of 3: 941 µs per loopIn [12]: xx = '"' + x + '"'
In [13]: %timeit eval(xx)
10000 loops, best of 3: 42.1 µs per loopIn [14]: evals(x) == t
Out [14]: TrueIn [15]: eval(xx) == t
Out [15]: True
```As seen above, the speed for `reprs` vs `repr` vs `pyreprs` is roughly `200:10:1`.
The speed for `evals` vs `eval` for `pyevals` is roughly `280:20:1`.
## Safety
`evals` is implemented using pure string manipulation, don't worried about being pwned(which is a necessary concern when calling `eval`)!
## Interface
### repr related
`reprs` is the c implementation. `pyreprs` is implemented using pure python, which is slow.
- param: `str`, any str input
- return: `str`, the human readable representation of the input```python
>>> from reprs import reprs, evals, pyreprs, pyevals
>>> import random
>>> t = ''.join(chr(random.choice(range(256))) for _ in range(20))
>>> t
'K\xffE\xe7\xd9\x93:\x1f\x96\xb5k/=]6\x8d-`\xae\xb4'>>> reprs(t)
'K\\xffE\\xe7\\xd9\\x93:\\x1f\\x96\\xb5k/=]6\\x8d-`\\xae\\xb4'>>> print reprs(t)
K\xffE\xe7\xd9\x93:\x1f\x96\xb5k/=]6\x8d-`\xae\xb4>>> reprs(t) == pyreprs(t)
True
```### evals related
`evals` is the c implementation, `pyevals` is implemented using pure python, which is slow.
`unreprs` is an alias for `evals`, `pyunreprs` is an alias for `pyevals`.