Copy docker run --rm zchencow/innozverse-python:latest python3 -c "
import ctypes, struct, array, hashlib, zlib
# Step 3: ctypes.Structure — C-compatible types
class Point(ctypes.Structure):
_fields_ = [('x', ctypes.c_double), ('y', ctypes.c_double)]
class ProductRecord(ctypes.Structure):
_pack_ = 1 # no padding — tight packing
_fields_ = [
('id', ctypes.c_uint32),
('price', ctypes.c_double),
('stock', ctypes.c_uint32),
('rating', ctypes.c_float),
('flags', ctypes.c_uint8),
]
# flags bits: 0=active, 1=featured, 2=discounted
r = ProductRecord(id=1, price=864.0, stock=15, rating=4.8, flags=0b00000011) # active+featured
print(f'=== ctypes.Structure ===')
print(f'Record: id={r.id} price={r.price} stock={r.stock} rating={r.rating:.1f} flags={r.flags:08b}')
print(f'Size: {ctypes.sizeof(r)} bytes (tight packed)')
print(f'active: {bool(r.flags & 0b001)}')
print(f'featured: {bool(r.flags & 0b010)}')
print(f'discounted:{bool(r.flags & 0b100)}')
# Serialize ctypes struct to bytes
raw = bytes(r)
print(f'Serialized: {raw.hex()}')
# Deserialize
r2 = ProductRecord.from_buffer_copy(raw)
print(f'Deserialized: id={r2.id} price={r2.price}')
# Step 4: array + memoryview — zero-copy processing
print()
print('=== array + memoryview ===')
prices = array.array('d', [864.0, 49.99, 99.99, 29.99, 1299.0, 39.99, 199.99, 599.0])
stocks = array.array('l', [15, 80, 999, 0, 5, 200, 30, 8 ])
mv_prices = memoryview(prices)
mv_stocks = memoryview(stocks)
# Zero-copy slice (no data copied)
expensive = mv_prices[0:2]
print(f'Expensive (slice, no copy): {list(expensive.cast(\"d\"))}')
# In-place modification via memoryview
mv_prices[0] = 799.99 # apply discount in-place
print(f'After discount on prices[0]: {prices[0]}')
# Read-only memoryview to prevent mutation
ro_mv = memoryview(prices).cast('B') # as bytes, read-only approach
print(f'Byte view size: {ro_mv.nbytes} bytes ({len(prices)} doubles × 8)')
# Step 5: Compute values efficiently with array math
values = array.array('d', (p*s for p,s in zip(prices, stocks)))
total = sum(values)
print(f'Total inventory value: \${total:,.2f}')
# Step 6: Checksums for data integrity
print()
print('=== Checksums & Data Integrity ===')
data = b''.join(struct.pack('!Id', i, 864.0+i) for i in range(100))
crc32 = zlib.crc32(data)
sha256 = hashlib.sha256(data).hexdigest()
blake2b = hashlib.blake2b(data, digest_size=16).hexdigest()
print(f'Data: {len(data):,} bytes')
print(f'CRC32: {crc32:#010x} (fast, not secure)')
print(f'SHA256: {sha256[:32]}... (secure, slower)')
print(f'BLAKE2b:{blake2b} (secure + fast)')
# Verify integrity
tampered = data[:100] + b'\\xff' + data[101:]
print(f'CRC32 detects tamper: {zlib.crc32(tampered) != crc32}')
print(f'SHA256 detects tamper: {hashlib.sha256(tampered).hexdigest() != sha256}')
# Step 7: Variable-length binary protocol
print()
print('=== Variable-Length Records ===')
# TLV: Type(1B) + Length(2B) + Value(nB)
def encode_tlv(type_id: int, value: bytes) -> bytes:
return struct.pack('!BH', type_id, len(value)) + value
def decode_tlv(data: bytes) -> list[tuple[int, bytes]]:
records, pos = [], 0
while pos < len(data):
type_id, length = struct.unpack('!BH', data[pos:pos+3])
value = data[pos+3:pos+3+length]
records.append((type_id, value))
pos += 3 + length
return records
TYPE_NAME = 1; TYPE_PRICE = 2; TYPE_STOCK = 3; TYPE_CATEGORY = 4
encoded = b''
encoded += encode_tlv(TYPE_NAME, 'Surface Pro 12\"'.encode())
encoded += encode_tlv(TYPE_PRICE, struct.pack('!d', 864.0))
encoded += encode_tlv(TYPE_STOCK, struct.pack('!I', 15))
encoded += encode_tlv(TYPE_CATEGORY, 'Laptop'.encode())
print(f'TLV encoded: {len(encoded)} bytes')
for type_id, value in decode_tlv(encoded):
if type_id == TYPE_NAME: print(f' name: {value.decode()}')
elif type_id == TYPE_PRICE: print(f' price: \${struct.unpack(\"!d\", value)[0]}')
elif type_id == TYPE_STOCK: print(f' stock: {struct.unpack(\"!I\", value)[0]}')
elif type_id == TYPE_CATEGORY: print(f' category: {value.decode()}')
# Step 8: Capstone — binary catalog with index
print()
print('=== Capstone: Binary Catalog with Index ===')
import tempfile, os
CATALOG_MAGIC = b'IZC2'
RECORD_FMT2 = '!I32sdI16s'
RECORD_SIZE2 = struct.calcsize(RECORD_FMT2)
def build_catalog(products: list[dict]) -> bytes:
records = []
for p in products:
n = p['name'].encode().ljust(32, b'\x00')[:32]
c = p['category'].encode().ljust(16, b'\x00')[:16]
records.append(struct.pack(RECORD_FMT2, p['id'], n, p['price'], p['stock'], c))
body = b''.join(records)
checksum = zlib.crc32(body)
header = struct.pack('!4sHII', CATALOG_MAGIC, 2, len(products), checksum)
return header + body
def parse_catalog(data: bytes) -> list[dict]:
hfmt = '!4sHII'
hsize = struct.calcsize(hfmt)
magic, version, count, checksum = struct.unpack(hfmt, data[:hsize])
assert magic == CATALOG_MAGIC, f'Bad magic: {magic!r}'
body = data[hsize:]
assert zlib.crc32(body) == checksum, 'Checksum mismatch — data corrupted!'
return [
{k: v for k, v in zip(
['id','name','price','stock','category'],
[pid, n.rstrip(b'\x00').decode(), price, stock, c.rstrip(b'\x00').decode()]
)}
for pid, n, price, stock, c in struct.iter_unpack(RECORD_FMT2, body)
]
products = [
{'id':1,'name':'Surface Pro 12\"','price':864.0, 'stock':15, 'category':'Laptop'},
{'id':2,'name':'Surface Pen', 'price':49.99, 'stock':80, 'category':'Accessory'},
{'id':3,'name':'Office 365', 'price':99.99, 'stock':999,'category':'Software'},
]
blob = build_catalog(products)
print(f'Catalog blob: {len(blob)} bytes')
parsed = parse_catalog(blob)
print(f'Parsed {len(parsed)} records:')
for p in parsed:
print(f' {p}')
# Tamper detection
tampered_blob = blob[:30] + b'\xff' + blob[31:]
try: parse_catalog(tampered_blob)
except AssertionError as e: print(f'Tamper detected: {e}')
"