Skip to content

Instantly share code, notes, and snippets.

Last active October 17, 2022 18:33
What would you like to do?
Example composite key escaping for DynamoDB
import random
import re
import string
from typing import Iterable
import dataclasses
def escape(s: str) -> str:
return s.replace("#", "##")
def unescape(s: str) -> str:
return s.replace("##", "#")
def split(s: str) -> tuple:
return tuple(s.split("/#/"))
def join(l: Iterable) -> str:
return "/#/".join(l)
def split_and_unescape(s: str) -> tuple:
return tuple(unescape(v) for v in split(s))
def escape_and_join(l: Iterable) -> str:
return join([escape(v) for v in l])
chars = string.printable
def rand_str():
return "".join(random.choice(chars) for _ in range(8))
class Value:
parts: tuple
value: str
def _create(prefix, N):
values = []
for _ in range(N):
values.append(prefix + [rand_str()])
return values
def test(M, N, show=False):
# create N^M values each with M parts
values = _create([], N)
for _ in range(M-1):
new_values = []
for value in values:
new_values.extend(_create(value, N))
values = new_values
l1 = []
for parts in values:
parts = tuple(parts)
value = escape_and_join(parts)
roundtrip = split_and_unescape(value)
if show:
if parts != roundtrip:
assert parts == roundtrip
l1.append(Value(parts, value))
sorted_by_parts = sorted(l1, key=lambda v:
sorted_by_value = sorted(l1, key=lambda v: v.value)
if show:
print("\n".join(repr( for v in sorted_by_parts))
print("\n".join(repr(v.value) for v in sorted_by_parts))
assert sorted_by_parts == sorted_by_value
test(4, 10, show=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment