Open the context
Choose the project folder and name the files or behavior involved.
Measure the Shannon entropy in bits per byte of a byte string no longer than 65536 bytes. Check empty and constant input as zero and all 256 equally frequent byte values as eight, rejecting non-byte input.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from collections import Counter
from math import log2
def byte_entropy(data):
if not isinstance(data, bytes) or len(data) > 65536:
raise ValueError("bytes.limit")
if not data:
return 0.0
n = len(data)
return sum((count / n) * log2(n / count) for count in Counter(data).values())The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.