### Install dahuffman Package
Source: https://github.com/soxofaan/dahuffman/blob/main/README.rst
Install the dahuffman package using pip. This is the first step to using the library.
```bash
pip install dahuffman
```
--------------------------------
### Import dahuffman library
Source: https://github.com/soxofaan/dahuffman/blob/main/examples.ipynb
Import the necessary library to start using Huffman coding functionalities.
```python
import dahuffman
```
--------------------------------
### Encode Data and Get Length
Source: https://github.com/soxofaan/dahuffman/blob/main/examples.ipynb
Encode a string using a codec trained on data and retrieve the length of the compressed output.
```python
len(codec.encode('do lo er ad od'))
```
--------------------------------
### Build Huffman Codec from Data
Source: https://github.com/soxofaan/dahuffman/blob/main/examples.ipynb
Create a Huffman codec by providing raw data directly. The library will analyze the data to determine symbol frequencies and build the optimal code table.
```python
codec = dahuffman.HuffmanCodec.from_data('hello world how are you doing today foo bar lorem ipsum')
```
--------------------------------
### Load Pre-trained Codecs for Common Text Formats
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Utilize ready-made codecs trained on real-world corpora like Shakespeare, JSON, and XML for quick compression of text in known formats without a training step.
```python
from dahuffman import (
load_shakespeare,
load_shakespeare_lower,
load_json,
load_json_compact,
load_xml,
)
from dahuffman.codecs import load # generic loader by name
# Shakespeare corpus (mixed case)
codec = load_shakespeare()
text = "To be, or not to be; that is the question;"
encoded = codec.encode(text)
print(len(encoded)) # 24 bytes (vs 42 characters)
print(codec.decode(encoded)) # 'To be, or not to be; that is the question;'
# Lowercase-only Shakespeare codec
lc_codec = load_shakespeare_lower()
encoded_lower = lc_codec.encode(text.lower())
print(lc_codec.decode(encoded_lower)) # 'to be, or not to be; that is the question;'
# JSON codec
json_codec = load_json()
data = '{"foo":"bar","baz":[1,2,3],"title":"data stuff"}'
encoded_json = json_codec.encode(data)
print(json_codec.decode(encoded_json)) # original JSON string
# XML codec
xml_codec = load_xml()
xml = '- foo
'
encoded_xml = xml_codec.encode(xml)
print(xml_codec.decode(encoded_xml)) # original XML string
# Generic loader by name
codec_by_name = load("shakespeare")
print(type(codec_by_name)) #
```
--------------------------------
### Create Codec from Data
Source: https://github.com/soxofaan/dahuffman/blob/main/README.rst
Train a Huffman codec by providing raw data. The codec will automatically determine symbol frequencies from the input data.
```python
codec = HuffmanCodec.from_data(
"hello world how are you doing today foo bar lorem ipsum"
)
codec.encode("do lo er ad od")
len(_)
```
--------------------------------
### Load Pre-trained Codecs and Compare Compression
Source: https://github.com/soxofaan/dahuffman/blob/main/examples.ipynb
Loads pre-trained codecs for common data formats (Shakespeare, JSON, XML) and compares their compression efficiency against original data. The `try_codecs` function iterates through the codecs, encodes the provided data, and prints the resulting size and compression percentage.
```python
codecs = {
'shakespeare': dahuffman.load_shakespeare(),
'json': dahuffman.load_json(),
'xml': dahuffman.load_xml()
}
def try_codecs(data):
print("{n:12s} {s:5d} bytes".format(n="original", s=len(data)))
for name, codec in codecs.items():
try:
encoded = codec.encode(data)
except KeyError:
continue
print("{n:12s} {s:5d} bytes ({p:.1f}%)".format(n=name, s=len(encoded), p=100.0*len(encoded)/len(data)))
```
```python
try_codecs("""To be, or not to be; that is the question;
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by opposing, end them. To die, to sleep""")
```
```python
try_codecs('''{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"children": [],
"spouse": null
}''')
```
```python
try_codecs('''
Gambardella, Matthew
XML Developer's Guide
44.95
Ralls, Kim
Midnight Rain
5.95
''')
```
--------------------------------
### Load pre-trained codecs
Source: https://context7.com/soxofaan/dahuffman/llms.txt
dahuffman ships with several pre-trained `HuffmanCodec` instances built from real-world corpora (Shakespeare plays, JSON, XML). These are loaded via convenience functions and are ready to use without any training step, making them ideal for quick compression of text in known formats.
```APIDOC
## Pre-trained codecs — Load ready-made codecs for common text formats
dahuffman ships with several pre-trained `HuffmanCodec` instances built from real-world corpora (Shakespeare plays, JSON, XML). These are loaded via convenience functions and are ready to use without any training step, making them ideal for quick compression of text in known formats.
```python
from dahuffman import (
load_shakespeare,
load_shakespeare_lower,
load_json,
load_json_compact,
load_xml,
)
from dahuffman.codecs import load # generic loader by name
# Shakespeare corpus (mixed case)
codec = load_shakespeare()
text = "To be, or not to be; that is the question;"
encoded = codec.encode(text)
print(len(encoded)) # 24 bytes (vs 42 characters)
print(codec.decode(encoded)) # 'To be, or not to be; that is the question;'
# Lowercase-only Shakespeare codec
lc_codec = load_shakespeare_lower()
encoded_lower = lc_codec.encode(text.lower())
print(lc_codec.decode(encoded_lower)) # 'to be, or not to be; that is the question;'
# JSON codec
json_codec = load_json()
data = '{"foo":"bar","baz":[1,2,3],"title":"data stuff"}'
encoded_json = json_codec.encode(data)
print(json_codec.decode(encoded_json)) # original JSON string
# XML codec
xml_codec = load_xml()
xml = '- foo
'
encoded_xml = xml_codec.encode(xml)
print(xml_codec.decode(encoded_xml)) # original XML string
# Generic loader by name
codec_by_name = load("shakespeare")
print(type(codec_by_name)) #
```
```
--------------------------------
### Persist and Restore Codec with `save` and `load`
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Serialize a trained codec's code table to a file using pickle for later restoration without retraining. Supports saving with optional metadata.
```python
from dahuffman import HuffmanCodec
from dahuffman.huffmancodec import PrefixCodec
# Train and save
codec1 = HuffmanCodec.from_data("aabcbcdbabdbcbd")
codec1.save("/tmp/my_codec.huff") # parent directories are created automatically
# Save with optional metadata
codec1.save("/tmp/my_codec_meta.huff", metadata={"trained_on": "sample corpus v1"})
# Load and use
codec2 = PrefixCodec.load("/tmp/my_codec.huff")
original = "abcdabcd"
encoded = codec2.encode(original)
decoded = codec2.decode(encoded)
assert decoded == original
print(decoded) # 'abcdabcd'
```
--------------------------------
### Train Huffman Codec from Raw Data
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Builds a HuffmanCodec by counting symbol frequencies automatically from any iterable input — strings, bytes, or lists of arbitrary hashable tokens. This is the most convenient way to create a codec when you have sample data rather than pre-computed frequencies.
```python
from dahuffman import HuffmanCodec
# Train from a string corpus
codec = HuffmanCodec.from_data(
"hello world how are you doing today foo bar lorem ipsum"
)
encoded = codec.encode("do lo er ad od")
print(encoded) # b'^O\x1a\xc4S\xab\x80'
print(len(encoded)) # 7
# Train from a list of arbitrary symbols (e.g., country codes)
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"])
print(encoded) # b'L\xca'
print(len(encoded)) # 2
decoded = codec.decode(encoded)
print(decoded) # ['FR', 'IT', 'BE', 'FR', 'UK']
```
--------------------------------
### Build Huffman Codec from Frequencies
Source: https://github.com/soxofaan/dahuffman/blob/main/examples.ipynb
Create a Huffman codec by providing a dictionary of symbol frequencies. This is useful when you know the expected distribution of characters in your data.
```python
codec = dahuffman.HuffmanCodec.from_frequencies({'e': 100, 'n':20, 'x':1, 'i': 40, 'q':3})
```
--------------------------------
### Huffman Coding with Sequences
Source: https://github.com/soxofaan/dahuffman/blob/main/examples.ipynb
Demonstrates using the dahuffman library with sequences of symbols, such as country codes. The codec is trained on a list of country codes and then used to encode and decode a subsequence.
```python
countries = ['FR', 'UK', 'BE', 'IT', 'FR', 'IT', 'GR', 'FR', 'NL', 'BE', 'DE']
codec = dahuffman.HuffmanCodec.from_data(countries)
```
```python
encoded = codec.encode(['FR', 'IT', 'BE', 'FR', 'UK'])
len(encoded), encoded.hex()
```
```python
codec.decode(encoded)
```
--------------------------------
### Create Codec from Frequencies
Source: https://github.com/soxofaan/dahuffman/blob/main/README.rst
Build a Huffman codec by providing symbol frequencies. This is useful when you know the expected distribution of symbols.
```python
from dahuffman import HuffmanCodec
codec = HuffmanCodec.from_frequencies(
{"e": 100, "n": 20, "x": 1, "i": 40, "q": 3}
)
codec.print_code_table()
```
--------------------------------
### HuffmanCodec.from_data
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Builds a HuffmanCodec by counting symbol frequencies automatically from any iterable input — strings, bytes, or lists of arbitrary hashable tokens. This is the most convenient way to create a codec when you have sample data rather than pre-computed frequencies.
```APIDOC
## HuffmanCodec.from_data
### Description
Builds a `HuffmanCodec` by counting symbol frequencies automatically from any iterable input — strings, bytes, or lists of arbitrary hashable tokens. This is the most convenient way to create a codec when you have sample data rather than pre-computed frequencies.
### Method
`HuffmanCodec.from_data(data: iterable)`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **data** (iterable) - Required - Any iterable input (string, bytes, list of symbols) to train the codec from.
### Request Example
```python
from dahuffman import HuffmanCodec
# Train from a string corpus
codec = HuffmanCodec.from_data(
"hello world how are you doing today foo bar lorem ipsum"
)
encoded = codec.encode("do lo er ad od")
print(encoded) # b'^O\x1a\xc4S\xab\x80'
print(len(encoded)) # 7
# Train from a list of arbitrary symbols (e.g., country codes)
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"])
print(encoded) # b'L\xca'
print(len(encoded)) # 2
decoded = codec.decode(encoded)
print(decoded) # ['FR', 'IT', 'BE', 'FR', 'UK']
```
### Response
#### Success Response (200)
- **codec** (HuffmanCodec) - The trained Huffman codec object.
#### Response Example
None provided in source.
```
--------------------------------
### Load Pre-trained Shakespeare Codec
Source: https://github.com/soxofaan/dahuffman/blob/main/README.rst
Load a pre-trained Huffman codec optimized for Shakespearean text. Useful for compressing or decompressing Shakespearean content.
```python
from dahuffman import load_shakespeare
codec = load_shakespeare()
codec.print_code_table()
len(codec.encode('To be, or not to be; that is the question;'))
```
--------------------------------
### HuffmanCodec.from_frequencies
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Constructs a HuffmanCodec by building the Huffman tree from a mapping of symbols to their relative frequencies. An EOF sentinel is automatically injected if not present in the frequency table.
```APIDOC
## HuffmanCodec.from_frequencies
### Description
Constructs a `HuffmanCodec` by building the Huffman tree from a mapping of symbols to their relative frequencies. Higher-frequency symbols receive shorter codes. An EOF sentinel is automatically injected if not present in the frequency table.
### Method
`HuffmanCodec.from_frequencies(frequencies: dict)`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **frequencies** (dict) - Required - A mapping of symbols to their relative frequencies.
### Request Example
```python
from dahuffman import HuffmanCodec
codec = HuffmanCodec.from_frequencies({"e": 100, "n": 20, "x": 1, "i": 40, "q": 3})
```
### Response
#### Success Response (200)
- **codec** (HuffmanCodec) - The constructed Huffman codec object.
#### Response Example
```python
# Inspect the generated code table
codec.print_code_table()
# Bits Code Value Symbol
# 5 00000 0 _EOF
# 5 00001 1 'x'
# 4 0001 1 'q'
# 3 001 1 'n'
# 2 01 1 'i'
# 1 1 1 'e'
# Encode a string — returns bytes
encoded = codec.encode("exeneeeexniqneieini")
print(encoded) # b'\x86|%\x13i@'
print(len(encoded)) # 6 (vs 19 original characters)
# Decode back to original
decoded = codec.decode(encoded)
print(decoded) # 'exeneeeexniqneieini'
```
```
--------------------------------
### Print Huffman Code Table
Source: https://github.com/soxofaan/dahuffman/blob/main/examples.ipynb
Display the generated Huffman code table, showing the bit representation for each symbol. This can be helpful for understanding the compression scheme.
```python
codec.print_code_table()
```
--------------------------------
### Persist and restore a codec
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Serializes a trained codec's code table to a file using `pickle`, preserving the symbol-to-code mapping and the concat function. The file can be loaded later with `PrefixCodec.load` to reconstruct a working codec without retraining.
```APIDOC
## `PrefixCodec.save` / `PrefixCodec.load` — Persist and restore a codec
Serializes a trained codec's code table to a file using `pickle`, preserving the symbol-to-code mapping and the concat function. The file can be loaded later with `PrefixCodec.load` to reconstruct a working codec without retraining.
```python
from dahuffman import HuffmanCodec
from dahuffman.huffmancodec import PrefixCodec
# Train and save
codec1 = HuffmanCodec.from_data("aabcbcdbabdbcbd")
codec1.save("/tmp/my_codec.huff") # parent directories are created automatically
# Save with optional metadata
codec1.save("/tmp/my_codec_meta.huff", metadata={"trained_on": "sample corpus v1"})
# Load and use
codec2 = PrefixCodec.load("/tmp/my_codec.huff")
original = "abcdabcd"
encoded = codec2.encode(original)
decoded = codec2.decode(encoded)
assert decoded == original
print(decoded) # 'abcdabcd'
```
```
--------------------------------
### Build Huffman Codec from Frequencies
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Constructs a HuffmanCodec by building the Huffman tree from a mapping of symbols to their relative frequencies. Higher-frequency symbols receive shorter codes. An EOF sentinel is automatically injected if not present in the frequency table.
```python
from dahuffman import HuffmanCodec
# Build codec from known character frequencies
codec = HuffmanCodec.from_frequencies({"e": 100, "n": 20, "x": 1, "i": 40, "q": 3})
# Inspect the generated code table
codec.print_code_table()
# Bits Code Value Symbol
# 5 00000 0 _EOF
# 5 00001 1 'x'
# 4 0001 1 'q'
# 3 001 1 'n'
# 2 01 1 'i'
# 1 1 1 'e'
# Encode a string — returns bytes
encoded = codec.encode("exeneeeexniqneieini")
print(encoded) # b'\x86|%\x13i@'
print(len(encoded)) # 6 (vs 19 original characters)
# Decode back to original
decoded = codec.decode(encoded)
print(decoded) # 'exeneeeexniqneieini'
```
--------------------------------
### Inspect the code table
Source: https://context7.com/soxofaan/dahuffman/llms.txt
`get_code_table()` returns the raw code table as a dict mapping each symbol to a `(bitsize, value)` tuple. `print_code_table()` renders a human-readable table sorted by bit depth, useful for understanding compression efficiency and debugging.
```APIDOC
## `PrefixCodec.get_code_table` / `print_code_table` — Inspect the code table
`get_code_table()` returns the raw code table as a dict mapping each symbol to a `(bitsize, value)` tuple. `print_code_table()` renders a human-readable table sorted by bit depth, useful for understanding compression efficiency and debugging.
```python
from dahuffman import HuffmanCodec
import io
codec = HuffmanCodec.from_frequencies({"a": 2, "b": 4, "c": 8})
# Raw code table
table = codec.get_code_table()
print(table)
# {'c': (1, 1), 'b': (2, 1), 'a': (3, 1), _EOF: (3, 0)}
# Human-readable table (write to stdout by default)
codec.print_code_table()
# Bits Code Value Symbol
# 1 1 1 'c'
# 2 01 1 'b'
# 3 001 1 'a'
# 3 000 0 _EOF
# Capture output to a string buffer
buf = io.StringIO()
codec.print_code_table(out=buf)
print(buf.getvalue())
```
```
--------------------------------
### Encode and Decode Data with Huffman Codec
Source: https://github.com/soxofaan/dahuffman/blob/main/examples.ipynb
Encode a string using the created codec and then decode the resulting bytes. Also shows how to print the hexadecimal representation and length of the encoded data.
```python
encoded = codec.encode('exeneeeexniqneieini')
print(encoded)
print(encoded.hex())
print(len(encoded))
```
```python
codec.decode(encoded)
```
--------------------------------
### Generate Shakespeare Codec Table
Source: https://github.com/soxofaan/dahuffman/blob/main/train/README.md
Execute this script to generate a Huffman codec table using the Shakespeare dataset. Copy the generated codec files to the `dahuffman/codecs` directory for use.
```python
python train/shakespeare.py
```
--------------------------------
### Encode and Decode Byte Data
Source: https://github.com/soxofaan/dahuffman/blob/main/README.rst
Encode data into bytes and decode it back using the Huffman codec. Shows how to work with byte representations of encoded data.
```python
list(encoded)
codec.decode([134, 124, 37, 19, 105, 64])
```
--------------------------------
### Inspect Code Table with `get_code_table` and `print_code_table`
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Retrieve the raw code table as a dictionary or print a human-readable table sorted by bit depth for analysis and debugging. Output can be captured to a string buffer.
```python
from dahuffman import HuffmanCodec
import io
codec = HuffmanCodec.from_frequencies({"a": 2, "b": 4, "c": 8})
# Raw code table
table = codec.get_code_table()
print(table)
# {'c': (1, 1), 'b': (2, 1), 'a': (3, 1), _EOF: (3, 0)}
# Human-readable table (write to stdout by default)
codec.print_code_table()
# Bits Code Value Symbol
# 1 1 1 'c'
# 2 01 1 'b'
# 3 01 1 'a'
# 3 000 0 _EOF
# Capture output to a string buffer
buf = io.StringIO()
codec.print_code_table(out=buf)
print(buf.getvalue())
```
--------------------------------
### Encode and Decode String Data
Source: https://github.com/soxofaan/dahuffman/blob/main/README.rst
Encode a string into bytes and then decode it back using the Huffman codec. Demonstrates basic encoding and decoding functionality.
```python
encoded = codec.encode("exeneeeexniqneieini")
print(encoded)
len(encoded)
codec.decode(encoded)
```
--------------------------------
### Encode and Decode Sequences of Symbols
Source: https://github.com/soxofaan/dahuffman/blob/main/README.rst
Use the Huffman codec with sequences of symbols, such as country codes. Demonstrates encoding and decoding lists of arbitrary hashable items.
```python
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"])
print(encoded)
len(encoded)
codec.decode(encoded)
```
--------------------------------
### Low-level codec with a custom code table
Source: https://context7.com/soxofaan/dahuffman/llms.txt
`PrefixCodec` is the base class used when you want to supply an entirely hand-crafted code table rather than deriving one via the Huffman algorithm. Each symbol maps to a `(bitsize, value)` tuple; an EOF entry is required for correct decoding.
```APIDOC
## `PrefixCodec` — Low-level codec with a custom code table
`PrefixCodec` is the base class used when you want to supply an entirely hand-crafted code table rather than deriving one via the Huffman algorithm. Each symbol maps to a `(bitsize, value)` tuple; an EOF entry is required for correct decoding.
```python
from dahuffman.huffmancodec import PrefixCodec, _EOF
```
```
--------------------------------
### Low-level Codec with Custom Code Table
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Use `PrefixCodec` as a base class when supplying a hand-crafted code table. Each symbol maps to a (bitsize, value) tuple, and an EOF entry is required.
```python
from dahuffman.huffmancodec import PrefixCodec, _EOF
```
--------------------------------
### PrefixCodec.encode / HuffmanCodec.encode
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Encodes a sequence of symbols into a compact bytes object using the codec's Huffman code table. Accepts strings, bytes, or any iterable of hashable symbols that exist in the code table. Raises KeyError for unknown symbols.
```APIDOC
## PrefixCodec.encode / HuffmanCodec.encode
### Description
Encodes a sequence of symbols into a compact `bytes` object using the codec's Huffman code table. Accepts strings, bytes, or any iterable of hashable symbols that exist in the code table. Raises `KeyError` for unknown symbols.
### Method
`codec.encode(symbols: iterable)`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **symbols** (iterable) - Required - A sequence of symbols (string, bytes, or hashable tokens) to encode.
### Request Example
```python
from dahuffman import HuffmanCodec
codec = HuffmanCodec.from_frequencies({"A": 5, "B": 4, "C": 2})
# Encode a string
encoded = codec.encode("AABCBA")
print(type(encoded)) #
print(encoded) # bytes result
# Decode to verify round-trip
assert codec.decode(encoded) == "AABCBA"
# Decode from a plain list of integers (byte values)
byte_values = list(encoded)
print(codec.decode(byte_values)) # 'AABCBA'
```
### Response
#### Success Response (200)
- **encoded_data** (bytes) - The encoded data as a bytes object.
#### Response Example
None provided in source.
```
--------------------------------
### Define and Use a 2-Bit Prefix Code Table
Source: https://context7.com/soxofaan/dahuffman/llms.txt
This snippet demonstrates how to manually define a 2-bit prefix code table and use it with PrefixCodec for encoding and decoding data. Ensure the _EOF symbol is included in the code table.
```python
code_table = {
"A": (2, 0), # 00
"B": (2, 1), # 01
_EOF: (2, 3), # 11
}
codec = PrefixCodec(code_table, check=True)
encoded = codec.encode("ABBA")
print(encoded) # b'\x14'
print(codec.decode(encoded)) # 'ABBA'
```
--------------------------------
### Streaming Encoding and Decoding with Generators
Source: https://github.com/soxofaan/dahuffman/blob/main/README.rst
Perform Huffman encoding and decoding in a streaming fashion using generators. This is memory-efficient for large datasets.
```python
import random
def sample(n, symbols):
for i in range(n):
if (n-i) % 5 == 1:
print(i)
yield random.choice(symbols)
codec = HuffmanCodec.from_data(countries)
encoded = codec.encode_streaming(sample(16, countries))
decoded = codec.decode_streaming(encoded)
list(decoded)
```
--------------------------------
### Streaming Encode and Decode with `PrefixCodec`
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Use generator-based streaming variants for lazy processing of large datasets. `encode_streaming` yields byte integers, and `decode_streaming` yields decoded symbols.
```python
import random
from dahuffman import HuffmanCodec
symbols = ["FR", "UK", "BE", "IT", "GR", "NL", "DE"]
training = ["FR", "FR", "UK", "BE", "IT", "FR", "IT", "GR", "FR", "NL", "BE", "DE"]
codec = HuffmanCodec.from_data(training)
# Generator producing random symbols
def random_stream(n, pool):
for _ in range(n):
yield random.choice(pool)
# Streaming encode: returns a generator of byte integers
encoded_stream = codec.encode_streaming(random_stream(100, symbols))
print(type(encoded_stream)) #
# Streaming decode: returns a generator of symbols
decoded_stream = codec.decode_streaming(encoded_stream)
result = list(decoded_stream)
print(result[:5]) # e.g. ['NL', 'FR', 'IT', 'BE', 'UK']
print(len(result)) # 100
```
--------------------------------
### Encode Data to Bytes
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Encodes a sequence of symbols into a compact `bytes` object using the codec's Huffman code table. Accepts strings, bytes, or any iterable of hashable symbols that exist in the code table. Raises `KeyError` for unknown symbols.
```python
from dahuffman import HuffmanCodec
codec = HuffmanCodec.from_frequencies({"A": 5, "B": 4, "C": 2})
# Encode a string
encoded = codec.encode("AABCBA")
print(type(encoded)) #
print(encoded) # bytes result
# Decode to verify round-trip
assert codec.decode(encoded) == "AABCBA"
# Decode from a plain list of integers (byte values)
byte_values = list(encoded)
print(codec.decode(byte_values)) # 'AABCBA'
```
--------------------------------
### Streaming encode and decode
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Generator-based streaming variants that process data lazily without loading everything into memory. `encode_streaming` yields integer byte values one at a time; `decode_streaming` yields decoded symbols one at a time. These are ideal for large datasets or pipeline-based processing.
```APIDOC
## `PrefixCodec.encode_streaming` / `decode_streaming` — Streaming encode and decode
Generator-based streaming variants that process data lazily without loading everything into memory. `encode_streaming` yields integer byte values one at a time; `decode_streaming` yields decoded symbols one at a time. These are ideal for large datasets or pipeline-based processing.
```python
import random
from dahuffman import HuffmanCodec
symbols = ["FR", "UK", "BE", "IT", "GR", "NL", "DE"]
training = ["FR", "FR", "UK", "BE", "IT", "FR", "IT", "GR", "FR", "NL", "BE", "DE"]
codec = HuffmanCodec.from_data(training)
# Generator producing random symbols
def random_stream(n, pool):
for _ in range(n):
yield random.choice(pool)
# Streaming encode: returns a generator of byte integers
encoded_stream = codec.encode_streaming(random_stream(100, symbols))
print(type(encoded_stream)) #
# Streaming decode: returns a generator of symbols
decoded_stream = codec.decode_streaming(encoded_stream)
result = list(decoded_stream)
print(result[:5]) # e.g. ['NL', 'FR', 'IT', 'BE', 'UK']
print(len(result)) # 100
```
```
--------------------------------
### PrefixCodec.decode
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Decodes a bytes object (or any iterable of integers 0–255) back to the original sequence of symbols. An optional `concat` function can override how decoded symbols are assembled — useful for custom aggregation instead of joining into a list or string.
```APIDOC
## PrefixCodec.decode
### Description
Decodes a `bytes` object (or any iterable of integers 0–255) back to the original sequence of symbols. An optional `concat` function can override how decoded symbols are assembled — useful for custom aggregation instead of joining into a list or string.
### Method
`codec.decode(encoded_data: bytes, concat: callable = None)`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **encoded_data** (bytes) - Required - The bytes object to decode.
- **concat** (callable) - Optional - A function to assemble the decoded symbols. Defaults to joining strings or creating a list.
### Request Example
```python
from dahuffman import HuffmanCodec
codec = HuffmanCodec.from_data([1, 2, 3, 1, 2, 3, 1])
encoded = codec.encode([1, 2, 1, 2, 3, 2, 1])
# Default decode — returns a list for non-string input
decoded = codec.decode(encoded)
print(decoded) # [1, 2, 1, 2, 3, 2, 1]
# Custom concat — e.g., sum all decoded integers
total = codec.decode(encoded, concat=sum)
print(total) # 12
```
### Response
#### Success Response (200)
- **decoded_symbols** (list or other type based on `concat`) - The decoded sequence of symbols.
#### Response Example
None provided in source.
```
--------------------------------
### Decode Bytes to Original Symbols
Source: https://context7.com/soxofaan/dahuffman/llms.txt
Decodes a `bytes` object (or any iterable of integers 0–255) back to the original sequence of symbols. An optional `concat` function can override how decoded symbols are assembled — useful for custom aggregation instead of joining into a list or string.
```python
from dahuffman import HuffmanCodec
codec = HuffmanCodec.from_data([1, 2, 3, 1, 2, 3, 1])
encoded = codec.encode([1, 2, 1, 2, 3, 2, 1])
# Default decode — returns a list for non-string input
decoded = codec.decode(encoded)
print(decoded) # [1, 2, 1, 2, 3, 2, 1]
# Custom concat — e.g., sum all decoded integers
total = codec.decode(encoded, concat=sum)
print(total) # 12
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.