Example composite key escaping for DynamoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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