Skip to content

Commit 8d0223d

Browse files
authored
Move the implementations of encoding_overhead() etc back to the Python file
I commented to contributor robamu that it would be good to put the functions in _cobs_py.py etc, instead of __init__.py, and then add an equivalent C API implementation in the .c files. However -- * robamu doesn't have experience with the Python C API. * I haven't used the Python C API for years. * These functions aren't as calculation-intensive as the encode/decode functions are, and aren't expected to be a hotspot in typical usage. So for now, I'll just move these functions back into __init__.py. This pertains to pull request #3.
1 parent 3930ea9 commit 8d0223d

4 files changed

Lines changed: 27 additions & 28 deletions

File tree

python3/cobs/cobs/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@
2626
DecodeError.__module__ = 'cobs.cobs'
2727

2828
from .._version import *
29+
30+
31+
def encoding_overhead(source_len):
32+
"""Calculates the maximum overhead when encoding a message with the given length.
33+
The overhead is a maximum of [n/254] bytes (one in 254 bytes) rounded up."""
34+
if source_len == 0:
35+
return 1
36+
return (source_len + 253) // 254
37+
38+
39+
def max_encoded_length(source_len):
40+
"""Calculates how maximum possible size of an encoded message given the length of the
41+
source message."""
42+
return source_len + encoding_overhead(source_len)

python3/cobs/cobs/_cobs_py.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,3 @@ def decode(in_bytes):
8888
else:
8989
break
9090
return bytes(out_bytes)
91-
92-
93-
def encoding_overhead(source_len):
94-
"""Calculates the maximum overhead when encoding a message with the given length.
95-
The overhead is a maximum of [n/254] bytes (one in 254 bytes) rounded up."""
96-
if source_len == 0:
97-
return 1
98-
return (source_len + 253) // 254
99-
100-
101-
def max_encoded_length(source_len):
102-
"""Calculates how maximum possible size of an encoded message given the length of the
103-
source message."""
104-
return source_len + encoding_overhead(source_len)

python3/cobs/cobsr/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,16 @@
2020

2121
from .._version import *
2222

23+
24+
def encoding_overhead(source_len):
25+
"""Calculates the maximum overhead when encoding a message with the given length.
26+
The overhead is a maximum of [n/254] bytes (one in 254 bytes) rounded up."""
27+
if source_len == 0:
28+
return 1
29+
return (source_len + 253) // 254
30+
31+
32+
def max_encoded_length(source_len):
33+
"""Calculates how maximum possible size of an encoded message given the length of the
34+
source message."""
35+
return source_len + encoding_overhead(source_len)

python3/cobs/cobsr/_cobsr_py.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,3 @@ def decode(in_bytes):
9696
else:
9797
break
9898
return bytes(out_bytes)
99-
100-
101-
def encoding_overhead(source_len):
102-
"""Calculates the maximum overhead when encoding a message with the given length.
103-
The overhead is a maximum of [n/254] bytes (one in 254 bytes) rounded up."""
104-
if source_len == 0:
105-
return 1
106-
return (source_len + 253) // 254
107-
108-
109-
def max_encoded_length(source_len):
110-
"""Calculates how maximum possible size of an encoded message given the length of the
111-
source message."""
112-
return source_len + encoding_overhead(source_len)

0 commit comments

Comments
 (0)