Skip to content

Commit dc40df6

Browse files
authored
Merge pull request #24 from saxbophone/josh/docs-revision
Revise the Docs
2 parents 8238b47 + f4bad38 commit dc40df6

File tree

1 file changed

+80
-78
lines changed

1 file changed

+80
-78
lines changed

README.md

Lines changed: 80 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ It is also not just 8-bit binary data that could be serialised. Any collection o
1212

1313
This library is my implementation of a generic, base-to-base converter which addresses this last point. An encoder and decoder for every binary-to-text format currently existing can be created and used with this library, requiring only for the details of the desired format to be given. Due to its flexibility, the library also makes it trivial to invent new wonderful and interesting base-to-base serialisation/conversion formats (I myself plan to work on and release one that translates binary files into a purely emoji-based format!).
1414

15+
One limitation of the library is that it cannot encode data from a smaller input base to a larger output base with padding on the input (i.e. if you're encoding from base 2 to base 1000, you need to ensure that the number of input symbols exactly matches the encoding ratio you're using). This is an accepted limitation due to the complexities of implementing a padding system that works in the same manner as base-64 and others but which can be extended to any arbitrary base.
16+
1517
So, I hope you find this library fun, useful or both!
1618

1719
## Installation
@@ -43,20 +45,20 @@ There is a functional interface and a class-based interface (the class-based one
4345
To use the class-based interface, you will need to create a subclass of `basest.encoders.Encoder` and override attributes of the class, as shown below (using base64 as an example):
4446

4547
```py
46-
>>> from basest.encoders import Encoder
47-
>>>
48-
>>> class CustomEncoder(Encoder):
49-
... input_base = 256
50-
... output_base = 64
51-
... input_ratio = 3
52-
... output_ratio = 4
53-
... # these attributes are only required if using decode() and encode()
54-
... input_symbol_table = [chr(c) for c in range(256)]
55-
... output_symbol_table = [
56-
... s for s in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
57-
... ]
58-
... padding_symbol = '='
59-
>>>
48+
from basest.encoders import Encoder
49+
50+
class CustomEncoder(Encoder):
51+
input_base = 256
52+
output_base = 64
53+
input_ratio = 3
54+
output_ratio = 4
55+
# these attributes are only required if using decode() and encode()
56+
input_symbol_table = [chr(c) for c in range(256)]
57+
output_symbol_table = [
58+
s for s in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
59+
]
60+
padding_symbol = '='
61+
6062
```
6163

6264
> **Note:** You must subclass `Encoder`, you cannot use it directly!
@@ -67,36 +69,36 @@ Subclasses of `Encoder` have the following public methods available:
6769
`encode()` will encode an iterable of symbols in the class' **input symbol table** into an iterable of symbols in the class' **output symbol table**, observing the chosen encoding ratios and padding symbol.
6870

6971
```py
70-
>>> encoder = CustomEncoder()
71-
>>> encoder.encode(['c', 'a', 'b', 'b', 'a', 'g', 'e', 's'])
72-
['Y', '2', 'F', 'i', 'Y', 'm', 'F', 'n', 'Z', 'X', 'M', '=']
72+
encoder = CustomEncoder()
73+
encoder.encode(['c', 'a', 'b', 'b', 'a', 'g', 'e', 's'])
74+
# -> ['Y', '2', 'F', 'i', 'Y', 'm', 'F', 'n', 'Z', 'X', 'M', '=']
7375
```
7476

7577
#### Encode Raw
7678
`encode_raw()` works just like `encode()`, except that symbols are not interpreted. Instead, plain integers within range 0->(base - 1) should be used. the value of the base is used as the padding symbol.
7779

7880
```py
79-
>>> encoder = CustomEncoder()
80-
>>> encoder.encode_raw([1, 2, 3, 4, 5, 6, 7])
81-
[0, 16, 8, 3, 1, 0, 20, 6, 1, 48, 64, 64]
81+
encoder = CustomEncoder()
82+
encoder.encode_raw([1, 2, 3, 4, 5, 6, 7])
83+
# -> [0, 16, 8, 3, 1, 0, 20, 6, 1, 48, 64, 64]
8284
```
8385

8486
#### Decode from one base to another
8587
`decode()` works in the exact same way as `encode()`, but in the inverse.
8688

8789
```py
88-
>>> encoder = CustomEncoder()
89-
>>> encoder.decode(['Y', '2', 'F', 'i', 'Y', 'm', 'F', 'n', 'Z', 'X', 'M', '='])
90-
['c', 'a', 'b', 'b', 'a', 'g', 'e', 's']
90+
encoder = CustomEncoder()
91+
encoder.decode(['Y', '2', 'F', 'i', 'Y', 'm', 'F', 'n', 'Z', 'X', 'M', '='])
92+
# -> ['c', 'a', 'b', 'b', 'a', 'g', 'e', 's']
9193
```
9294

9395
#### Decode Raw
9496
`decode_raw()` works just like `decode()`, except that symbols are not interpreted. Instead, plain integers within range 0->(base - 1) should be used. the value of the base is used as the padding symbol.
9597

9698
```py
97-
>>> encoder = CustomEncoder()
98-
>>> encoder.decode_raw([0, 16, 8, 3, 1, 0, 20, 6, 1, 48, 64, 64])
99-
[1, 2, 3, 4, 5, 6, 7]
99+
encoder = CustomEncoder()
100+
encoder.decode_raw([0, 16, 8, 3, 1, 0, 20, 6, 1, 48, 64, 64])
101+
# -> [1, 2, 3, 4, 5, 6, 7]
100102
```
101103

102104
### Functional Interface
@@ -107,33 +109,33 @@ Return the input data, encoded into the specified base using the specified encod
107109
Returns the output data as a list of items that are guaranteed to be in the **output symbol table**, or the **output padding** symbol.
108110

109111
```py
110-
>>> import basest
111-
>>>
112-
>>> basest.core.encode(
113-
... input_base=256,
114-
... input_symbol_table=[chr(c) for c in range(256)],
115-
... output_base=64,
116-
... output_symbol_table=[
117-
... s for s in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
118-
... ],
119-
... output_padding='=', input_ratio=3, output_ratio=4,
120-
... input_data='falafel'
121-
... )
122-
['Z', 'm', 'F', 's', 'Y', 'W', 'Z', 'l', 'b', 'A', '=', '=']
112+
import basest
113+
114+
basest.core.encode(
115+
input_base=256,
116+
input_symbol_table=[chr(c) for c in range(256)],
117+
output_base=64,
118+
output_symbol_table=[
119+
s for s in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
120+
],
121+
output_padding='=', input_ratio=3, output_ratio=4,
122+
input_data='falafel'
123+
)
124+
# -> ['Z', 'm', 'F', 's', 'Y', 'W', 'Z', 'l', 'b', 'A', '=', '=']
123125
```
124126

125127
#### Encode Raw
126128
Similar to the function above, `basest.core.encode_raw` will encode one base into another, but only accepts and returns arrays of integers (e.g. bytes would be passed as integers between 0-255, not as `byte` objects). As such, it omits the **padding** and **symbol table** arguments, but is otherwise identical in function and form to `encode`.
127129

128130
```py
129-
>>> import basest
130-
>>>
131-
>>> basest.core.encode_raw(
132-
... input_base=256, output_base=85,
133-
... input_ratio=4, output_ratio=5,
134-
... input_data=[99, 97, 98, 98, 97, 103, 101, 115]
135-
... )
136-
[31, 79, 81, 71, 52, 31, 25, 82, 13, 76]
131+
import basest
132+
133+
basest.core.encode_raw(
134+
input_base=256, output_base=85,
135+
input_ratio=4, output_ratio=5,
136+
input_data=[99, 97, 98, 98, 97, 103, 101, 115]
137+
)
138+
# -> [31, 79, 81, 71, 52, 31, 25, 82, 13, 76]
137139
```
138140

139141
#### Decode from one encoded base to another.
@@ -143,33 +145,33 @@ Returns the output data as a list of items that are guaranteed to be in the **ou
143145
> This is essentially the inverse of `encode()`
144146
145147
```py
146-
>>> import basest
147-
>>>
148-
>>> basest.core.decode(
149-
... input_base=64,
150-
... input_symbol_table=[
151-
... s for s in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
152-
... ],
153-
... input_padding='=',
154-
... output_base=256, output_symbol_table=[chr(c) for c in range(256)],
155-
... input_ratio=4, output_ratio=3,
156-
... input_data='YWJhY3VzIFpaWg=='
157-
... )
158-
['a', 'b', 'a', 'c', 'u', 's', ' ', 'Z', 'Z', 'Z']
148+
import basest
149+
150+
basest.core.decode(
151+
input_base=64,
152+
input_symbol_table=[
153+
s for s in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
154+
],
155+
input_padding='=',
156+
output_base=256, output_symbol_table=[chr(c) for c in range(256)],
157+
input_ratio=4, output_ratio=3,
158+
input_data='YWJhY3VzIFpaWg=='
159+
)
160+
# -> ['a', 'b', 'a', 'c', 'u', 's', ' ', 'Z', 'Z', 'Z']
159161
```
160162

161163
#### Decode Raw
162164
Similar to the function above, `basest.core.decode_raw` will decode from one base to another, but only accepts and returns arrays of integers (e.g. base64 would be passed as integers between 0-65 (65 is for the padding symbol), not as `str` objects). As such, it omits the **padding** and **symbol table** arguments, but is otherwise identical in function and form to `decode`.
163165

164166
```py
165-
>>> import basest
166-
>>>
167-
>>> basest.core.decode_raw(
168-
... input_base=85, output_base=256,
169-
... input_ratio=5, output_ratio=4,
170-
... input_data=[31, 79, 81, 71, 52, 31, 25, 82, 13, 76]
171-
... )
172-
[99, 97, 98, 98, 97, 103, 101, 115]
167+
import basest
168+
169+
basest.core.decode_raw(
170+
input_base=85, output_base=256,
171+
input_ratio=5, output_ratio=4,
172+
input_data=[31, 79, 81, 71, 52, 31, 25, 82, 13, 76]
173+
)
174+
# -> [99, 97, 98, 98, 97, 103, 101, 115]
173175
```
174176

175177
#### Finding the best encoding ratio from one base to any base within a given range
@@ -178,14 +180,14 @@ For a given **input base** (e.g. base-256 / 8-bit Bytes), a given desired **outp
178180
Returns tuples containing an integer as the first item (representing the output base that is most efficient) and a tuple as the second, containing two integers representing the ratio of **input base** symbols to **output base** symbols.
179181

180182
```py
181-
>>> import basest
182-
>>>
183-
>>> basest.core.best_ratio(input_base=256, output_bases=[94], chunk_sizes=range(1, 256))
184-
(94, (68, 83))
185-
>>> basest.core.best_ratio(input_base=256, output_bases=[94], chunk_sizes=range(1, 512))
186-
(94, (458, 559))
187-
>>> basest.core.best_ratio(input_base=256, output_bases=range(2, 95), chunk_sizes=range(1, 256))
188-
(94, (68, 83))
189-
>>> basest.core.best_ratio(input_base=256, output_bases=range(2, 334), chunk_sizes=range(1, 256))
190-
(333, (243, 232))
183+
import basest
184+
185+
basest.core.best_ratio(input_base=256, output_bases=[94], chunk_sizes=range(1, 256))
186+
# -> (94, (68, 83))
187+
basest.core.best_ratio(input_base=256, output_bases=[94], chunk_sizes=range(1, 512))
188+
# -> (94, (458, 559))
189+
basest.core.best_ratio(input_base=256, output_bases=range(2, 95), chunk_sizes=range(1, 256))
190+
# -> (94, (68, 83))
191+
basest.core.best_ratio(input_base=256, output_bases=range(2, 334), chunk_sizes=range(1, 256))
192+
# -> (333, (243, 232))
191193
```

0 commit comments

Comments
 (0)