Skip to content

Commit f4bad38

Browse files
committed
Format all code usage examples in README to be copy-pasteable
1 parent c38e202 commit f4bad38

File tree

1 file changed

+78
-78
lines changed

1 file changed

+78
-78
lines changed

README.md

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@ There is a functional interface and a class-based interface (the class-based one
4545
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):
4646

4747
```py
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-
>>>
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+
6262
```
6363

6464
> **Note:** You must subclass `Encoder`, you cannot use it directly!
@@ -69,36 +69,36 @@ Subclasses of `Encoder` have the following public methods available:
6969
`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.
7070

7171
```py
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', '=']
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', '=']
7575
```
7676

7777
#### Encode Raw
7878
`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.
7979

8080
```py
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]
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]
8484
```
8585

8686
#### Decode from one base to another
8787
`decode()` works in the exact same way as `encode()`, but in the inverse.
8888

8989
```py
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']
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']
9393
```
9494

9595
#### Decode Raw
9696
`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.
9797

9898
```py
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]
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]
102102
```
103103

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

111111
```py
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', '=', '=']
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', '=', '=']
125125
```
126126

127127
#### Encode Raw
128128
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`.
129129

130130
```py
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]
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]
139139
```
140140

141141
#### Decode from one encoded base to another.
@@ -145,33 +145,33 @@ Returns the output data as a list of items that are guaranteed to be in the **ou
145145
> This is essentially the inverse of `encode()`
146146
147147
```py
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']
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']
161161
```
162162

163163
#### Decode Raw
164164
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`.
165165

166166
```py
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]
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]
175175
```
176176

177177
#### Finding the best encoding ratio from one base to any base within a given range
@@ -180,14 +180,14 @@ For a given **input base** (e.g. base-256 / 8-bit Bytes), a given desired **outp
180180
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.
181181

182182
```py
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))
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))
193193
```

0 commit comments

Comments
 (0)