Skip to content

Instantly share code, notes, and snippets.

@westandskif
Created August 19, 2022 10:44
Show Gist options
  • Save westandskif/caaa7008cdeaa4694412e5f71a04ee9b to your computer and use it in GitHub Desktop.
Save westandskif/caaa7008cdeaa4694412e5f71a04ee9b to your computer and use it in GitHub Desktop.
codegen-medium-2022-08-19
from convtools import conversion as c
conversion = c.and_(
c.item(0) == b"abc",
c.item(2) > 15,
)
In [5]: converter = conversion.gen_converter(debug=True)
...:
def converter(data_, *, __v=__naive_values__["__v"]):
try:
return (data_[0] == __v) and (data_[2] > 15)
except __exceptions_to_dump_sources:
__convtools__code_storage.dump_sources()
raise
In [6]: converter
Out[6]: <function _convtools.converter(data_, *, __v=b'abc')>
"""Of course I'd suggest building the full conversion pipeline, so convtools
can inline as much as possible, e.g.:"""
In [8]: c.filter(
...: c.and_(
...: c.item(0) == b"abc",
...: c.item(2) > 15,
...: )
...: ).gen_converter(debug=True)
...:
def converter(data_, *, __v=__naive_values__["__v"]):
try:
return (i_ for i_ in data_ if ((i_[0] == __v) and (i_[2] > 15)))
except __exceptions_to_dump_sources:
__convtools__code_storage.dump_sources()
raise
Out[8]: <function _convtools.converter(data_, *, __v=b'abc')>
@westandskif
Copy link
Author

of course, remove debug=True not to print generated code, formatted with black.

Here's the link to convtools library - https://github.com/westandskif/convtools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment