Skip to content

Commit

Permalink
Blackify code snippets in readme a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Jul 13, 2024
1 parent 5811d9f commit 744f9c6
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ Usage
Basic usage example, where the code table is built based on given symbol frequencies::

>>> from dahuffman import HuffmanCodec
>>> codec = HuffmanCodec.from_frequencies({'e': 100, 'n':20, 'x':1, 'i': 40, 'q':3})
>>> codec = HuffmanCodec.from_frequencies(
... {"e": 100, "n": 20, "x": 1, "i": 40, "q": 3}
... )
>>> codec.print_code_table()
Bits Code Value Symbol
5 00000 0 _EOF
Expand All @@ -62,7 +64,7 @@ Basic usage example, where the code table is built based on given symbol frequen

Encode a string, get the encoded data as ``bytes`` and decode again::

>>> encoded = codec.encode('exeneeeexniqneieini')
>>> encoded = codec.encode("exeneeeexniqneieini")
>>> encoded
b'\x86|%\x13i@'
>>> len(encoded)
Expand All @@ -80,18 +82,20 @@ If desired: work with byte values directly:

You can also "train" the codec by providing it data directly::

>>> codec = HuffmanCodec.from_data('hello world how are you doing today foo bar lorem ipsum')
>>> codec.encode('do lo er ad od')
>>> codec = HuffmanCodec.from_data(
... "hello world how are you doing today foo bar lorem ipsum"
... )
>>> codec.encode("do lo er ad od")
b'^O\x1a\xc4S\xab\x80'
>>> len(_)
7


Using it with sequences of symbols (country codes in this example)::

>>> countries = ['FR', 'UK', 'BE', 'IT', 'FR', 'IT', 'GR', 'FR', 'NL', 'BE', 'DE']
>>> countries = ["FR", "UK", "BE", "IT", "FR", "IT", "GR", "FR", "NL", "BE", "DE"]
>>> codec = HuffmanCodec.from_data(countries)
>>> encoded = codec.encode(['FR', 'IT', 'BE', 'FR', 'UK'])
>>> encoded = codec.encode(["FR", "IT", "BE", "FR", "UK"])
>>> encoded
b'L\xca'
>>> len(encoded)
Expand Down

0 comments on commit 744f9c6

Please sign in to comment.