Skip to content

Commit b552a2d

Browse files
committed
Bootstrap pyvarint package
0 parents  commit b552a2d

16 files changed

Lines changed: 927 additions & 0 deletions

.drone.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
matrix:
3+
include:
4+
- IMAGE: 3.6-stretch
5+
TOXENV: py36
6+
- IMAGE: 3.7-stretch
7+
TOXENV: py37
8+
- IMAGE: 3.8-buster
9+
TOXENV: py38
10+
- IMAGE: 3.8-buster
11+
TOXENV: lint
12+
- IMAGE: 3.8-buster
13+
TOXENV: sort
14+
- IMAGE: 3.8-buster
15+
TOXENV: format
16+
- IMAGE: 3.8-buster
17+
TOXENV: type
18+
19+
pipeline:
20+
build:
21+
image: python:${IMAGE}
22+
commands:
23+
- pip install tox
24+
- tox -e ${TOXENV}

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.egg-info/
2+
*.pyc
3+
.coverage
4+
.eggs/
5+
.mypy_cache/
6+
.tox/
7+
__pycache__
8+
build/
9+
dist/
10+
pip-wheel-metadata/

CHANGELOG.md

Whitespace-only changes.

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include LICENSE README.md CHANGELOG.md

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# pyvarint
2+
3+
## varint, a method of serializing integers using one or more bytes

mypy.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[mypy]
2+
python_version = 3.8
3+
platform = linux
4+
ignore_missing_imports = True

pyproject.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[build-system]
2+
requires = [
3+
"setuptools",
4+
"setuptools-scm",
5+
"wheel",
6+
]
7+
build-backend = "setuptools.build_meta"
8+
9+
[tool.black]
10+
line-length = 80
11+
target-version = ["py38"]

pyvarint/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""pyvarint module."""
2+
3+
from pyvarint.varint import decode, encode, encoding_length # noqa
4+
5+
__all__ = ["encode", "decode", "encoding_length"]
6+
7+
try:
8+
import pkg_resources
9+
except ImportError:
10+
pass
11+
12+
13+
try:
14+
__version__ = pkg_resources.get_distribution("pyvarint").version
15+
except Exception:
16+
__version__ = "unknown"

pyvarint/varint.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""Varint encode and decode"""
2+
3+
import sys
4+
from StringIO import StringIO as BytesIO
5+
6+
7+
def encode(number):
8+
"""Encode to varint"""
9+
buf = b""
10+
11+
while True:
12+
towrite = number & 0x7F
13+
number >>= 7
14+
if number:
15+
buf += bytes((towrite | 0x80,))
16+
else:
17+
buf += bytes((towrite,))
18+
break
19+
20+
return buf
21+
22+
23+
def decode(buf):
24+
"""Decode to bytes"""
25+
stream = BytesIO(buf)
26+
shift = 0
27+
result = 0
28+
29+
while True:
30+
single_byte = stream.read(1)
31+
32+
if single_byte == b"":
33+
raise EOFError("Unexpected EOF while reading bytes")
34+
ord_int = ord(single_byte)
35+
36+
result |= (ord_int & 0x7F) << shift
37+
shift += 7
38+
39+
if not (ord_int & 0x80):
40+
break
41+
42+
return result
43+
44+
45+
def encoding_length(n):
46+
"""The number of bytes this number will be encoded as."""
47+
N1 = pow(2, 7)
48+
N2 = pow(2, 14)
49+
N3 = pow(2, 21)
50+
N4 = pow(2, 28)
51+
N5 = pow(2, 35)
52+
N6 = pow(2, 42)
53+
N7 = pow(2, 49)
54+
N8 = pow(2, 56)
55+
N9 = pow(2, 63)
56+
57+
if n < N1:
58+
return 1
59+
elif n < N2:
60+
return 2
61+
elif n < N3:
62+
return 3
63+
elif n < N4:
64+
return 4
65+
elif n < N5:
66+
return 5
67+
elif n < N6:
68+
return 6
69+
elif n < N7:
70+
return 7
71+
elif n < N8:
72+
return 8
73+
elif n < N9:
74+
return 9
75+
else:
76+
return 10

0 commit comments

Comments
 (0)