Skip to content

Instantly share code, notes, and snippets.

@benkehoe
Last active May 14, 2023 21:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benkehoe/4ab771a689c05ed64b122cdd96ed1b12 to your computer and use it in GitHub Desktop.
Save benkehoe/4ab771a689c05ed64b122cdd96ed1b12 to your computer and use it in GitHub Desktop.
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))
@dataclasses.dataclass
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:
print(repr(parts))
if parts != roundtrip:
print(repr(roundtrip))
print(repr(value))
assert parts == roundtrip
l1.append(Value(parts, value))
print(len(l1))
sorted_by_parts = sorted(l1, key=lambda v: v.parts)
sorted_by_value = sorted(l1, key=lambda v: v.value)
if show:
print("Parts")
print("\n".join(repr(v.parts) for v in sorted_by_parts))
print("Values")
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