Skip to content

Commit

Permalink
version 3.4.3
Browse files Browse the repository at this point in the history
- add short -q flag for --sql
  • Loading branch information
elesiuta committed Mar 22, 2024
1 parent 4e199b7 commit f11625c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...]
Expand Down Expand Up @@ -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 <library>' for each library
--im library, --importstar library
Expand Down Expand Up @@ -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 <table>"
> 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}

Expand Down
4 changes: 2 additions & 2 deletions pyxargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 <library>' for each library")
Expand Down

0 comments on commit f11625c

Please sign in to comment.