From f11625ca000199cbbfcc14fda7500944d943a82a Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 22 Mar 2024 15:26:35 -0400 Subject: [PATCH] version 3.4.3 - add short -q flag for --sql --- README.md | 21 ++++++++++++++++++--- pyxargs.py | 4 ++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1a98757..6af4107 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,10 @@ This started as a simple solution to the [encoding problem with xargs](https://en.wikipedia.org/wiki/Xargs#Encoding_problem). It is a partial and opinionated implementation of xargs with the goal of being easier to use for most use cases. -It also contains additional features for AWK-like data processing, such as taking python code as arguments to be executed, or filtering with regular expressions. +It also contains additional features for AWK-like data processing, such as taking python code as arguments to be executed, or filtering with regular expressions. Some of these features take inspiration from [pyp](https://github.com/hauntsaninja/pyp/), [Pyed Piper](https://github.com/thepyedpiper/pyp), and [Pyped](https://github.com/ksamuel/Pyped/). A great comparison of them is provided by [pyp](https://github.com/hauntsaninja/pyp?tab=readme-ov-file#related-projects), which mainly differs from pyxargs in that pyxargs has more of an xargs-like interface and built in file tree traversal (replacing the need for find), but lacks the AST introspection and manipulation of pyp which infers more from the command without passing flags. + +You can install [pyxargs](https://github.com/elesiuta/pyxargs/) from [PyPI](https://pypi.org/project/pyxargs/). Optionally depends on [duckdb](https://pypi.org/project/duckdb/) and [pandas](https://pypi.org/project/pandas/). -You can install [pyxargs](https://github.com/elesiuta/pyxargs/) from [PyPI](https://pypi.org/project/pyxargs/). ## Command Line Interface ``` usage: pyxr [options] command [initial-arguments ...] @@ -90,7 +91,7 @@ options: then prints the result -p, --pypr evaluates commands as python f-strings then prints them (implies --fstring) - --sql reads each input into variable db then runs commands + -q, --sql reads each input into variable db then runs commands as SQL queries using duckdb.sql(), requires duckdb --import library executes 'import ' for each library --im library, --importstar library @@ -143,6 +144,20 @@ options: # given variables are only in the global scope, so they won't overwrite locals > pyxr --pre "i=1;j=2;n=5;x=3;a=3;" -p "i={i} j={j} n={n} x={x} a={l}" +# you can also use dataframes as df with --df (requires pandas) + > echo A,B,C\n1,2,3\n4,5,6 | pyxr -0 --df -p "{df}" + +# or query sql databases as db with -q (requires duckdb) + > echo A,B,C\n1,2,3\n4,5,6 | pyxr -0 -q "SELECT * FROM db" + > echo '{"a": 1,"b": 2}' | pyxr -0 -q "SELECT * FROM db" + +# it can also read from databases, csv files, etc. (see duckdb extensions) + > pyxr -t -r .sqlite -q "SELECT * FROM " + > pyxr -t -r .sqlite -f -q "SELECT * FROM {db[0]}" + > pyxr -t -r .csv -q "SELECT * FROM db" + > pyxr -t -q "SELECT * FROM db" + > pyxr -t -q "SELECT * FROM '{}'" + # regular expressions can be used to filter and modify inputs > pyxr -r \.py --resub \.py .txt {new} echo {} -\> {new} diff --git a/pyxargs.py b/pyxargs.py index b981aa4..a492f80 100644 --- a/pyxargs.py +++ b/pyxargs.py @@ -34,7 +34,7 @@ import typing -__version__: typing.Final[str] = "3.4.2" +__version__: typing.Final[str] = "3.4.3" def replace_surrogates(string: str) -> str: @@ -404,7 +404,7 @@ def _split_lines(self, text, width): help="evaluates commands as python expressions using eval() then prints the result") group1.add_argument("-p", "--pypr", action="store_true", dest="pyprt", help="evaluates commands as python f-strings then prints them (implies --fstring)") - group1.add_argument("--sql", action="store_true", dest="sql", + group1.add_argument("-q", "--sql", action="store_true", dest="sql", help="reads each input into variable db then runs commands as SQL queries using duckdb.sql(), requires duckdb") parser.add_argument("--import", action="append", type=str, default=[], metavar=("library"), dest="imprt", help="executes 'import ' for each library")