2020-06-26 15:19:34 +00:00
|
|
|
# pyvarint
|
|
|
|
|
2020-06-29 23:40:01 +00:00
|
|
|
[](https://drone.autonomic.zone/hyperpy/pyvarint)
|
|
|
|
|
2020-06-29 23:47:23 +00:00
|
|
|
## Varints, a method of serializing integers using one or more bytes
|
2020-06-29 23:40:01 +00:00
|
|
|
|
2020-07-09 08:12:54 +00:00
|
|
|
> Generally in Python, integers are stored as long meaning that they will use
|
|
|
|
> at least 32 bits. When storing many numbers which do not require 32 bits,
|
|
|
|
> this would seem to be significantly wasteful; variable length representation
|
|
|
|
> should be able to assist in such cases.
|
|
|
|
|
2020-07-07 13:59:42 +00:00
|
|
|
## Install
|
|
|
|
|
2020-06-29 23:40:01 +00:00
|
|
|
```sh
|
|
|
|
$ pip install pyvarint
|
|
|
|
```
|
2020-07-07 13:59:42 +00:00
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
```python
|
|
|
|
from random import sample
|
|
|
|
from pyvarint import decode, encode
|
|
|
|
|
|
|
|
ten_rand_ints = sample(range(100), 10)
|
|
|
|
|
|
|
|
for rand_int in ten_rand_ints:
|
|
|
|
encoded = encode(rand_int)
|
|
|
|
decoded = decode(encoded)
|
|
|
|
assert decoded == rand_int
|
|
|
|
```
|