Skip to content

Instantly share code, notes, and snippets.

@westandskif
Created January 29, 2023 15:18
Show Gist options
  • Save westandskif/5b3186396e5f4cd8d808a409f606b9bc to your computer and use it in GitHub Desktop.
Save westandskif/5b3186396e5f4cd8d808a409f606b9bc to your computer and use it in GitHub Desktop.
from convtools import conversion as c
config = {
"key1": "external key1",
"key2": "external key2",
"key3": "external key3",
}
def f_base(data):
return [
{key: item[key_external] for key, key_external in config.items()}
for item in data
]
f_convtools = c.list_comp(
{key: c.item(key_external) for key, key_external in config.items()}
).gen_converter()
data = [{"external key1": 1, "external key2": 2, "external key3": 3}] * 10000
assert f_base(data) == f_convtools(data)
"""
In [36]: %timeit f_base(data)
2.66 ms ± 6.67 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [37]: %timeit f_convtools(data)
1.29 ms ± 3.83 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
CODE-GEN PHASE TAKES REASONABLE TIME (and the converter function can be reused):
In [40]: %%timeit
...: f_convtools = c.list_comp(
...: {key: c.item(key_external) for key, key_external in config.items()}
...: ).gen_converter()
...:
...:
62.4 µs ± 69.7 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment