Show HN: Inline SQL in any Python program
github.com
github.com
stuff: list[mydataclass]
sql_val^ "select * from stuff where stuff.field = 2"
over any kind of 'fmappable' container; perhaps eg. stuff: dict[int, mydataclass]
stuff2: dict[int, mydataclass]
sql_val^ "select (s2.val, s.key) from stuff as s, stuff2 as s2 where s.key > s2.key and s2.value != s.value)"
etc stuff: list[mydataclass]
stuff_df = pd.DataFrame(stuff)
results = sql^ "SELECT * FROM stuff_df WHERE field = 2"
My guess is that this would be a lot faster than dataclasses-sql due to less conversion overhead, but we'd have to benchmark it to be sure.We could use a standard function application, but that doesn't convey the point that there's some DSL embedding going on here, such as variable substitution from the outside scope. It's also less nice to end multi-line queries with the string: """).
Other reasons: the bitwise XOR operator is almost never used in Python, so it's highly unlikely that people would confuse it as such (this is also why it's formatted without a space before the operator). Open to other proposals for syntax though.
Even so, pandas has the same magic substitution going on with just a standard function call, and it’s somewhat intuitive.
However, regardless of the syntax nitpicking, this is a cool project. Nice one!