Skip to content

Instantly share code, notes, and snippets.

@ajfriend
Created August 27, 2024 04:36
Show Gist options
  • Save ajfriend/eea0795546c7c44f1c24ab0560a846b9 to your computer and use it in GitHub Desktop.
Save ajfriend/eea0795546c7c44f1c24ab0560a846b9 to your computer and use it in GitHub Desktop.
Monkey Pipes
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@nwh
Copy link

nwh commented Aug 28, 2024

This is pretty cool. One way to avoid random table names is to make it a function.

def pipe(rel, *stmts):
    for i, stmt in enumerate(stmts):
        name = f"_tlb_{i}"
        rel = rel.query(name, f"from {name} {stmt}")
    return rel

result = pipe(
    rel,
    "select species, sepal_length as sl, sepal_width as sw",
    "where sl > 4.5",
    "where sw between 3.1 and 3.5",
    "select species, avg(sl), max(sw) group by all",
    "order by 2",
    "limit 2",
)

but not as cool as overloading |

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