Skip to content

Commit

Permalink
version 3.4.2
Browse files Browse the repository at this point in the history
- don't need special case for --sql with --js, since json is already tried early
- don't need except db = x, confusing and prefer error if csv fails on stdin (seems to be able to parse any string, albeit as a single entry)
- create exception to hint at other options if failing to read db from file path
- use db to hold list of tables on db connection, mainly so prior variable db is not used by accident from differing db input types, but more useful than db = None
  • Loading branch information
elesiuta committed Mar 22, 2024
1 parent 1af7295 commit 4e199b7
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 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.1"
__version__: typing.Final[str] = "3.4.2"


def replace_surrogates(string: str) -> str:
Expand Down Expand Up @@ -221,7 +221,7 @@ def execute_command(args: argparse.Namespace, command_dict: dict, user_namespace
cmd = command_dict["cmd"]
if args.input_mode == "file":
os.chdir(dir_path)
# update variables available to the user
# update variables always available to the user
global i, j, n, a, out, d, x, s
i, j = i + 1, j - 1
d = command_dict["dir"]
Expand All @@ -234,6 +234,7 @@ def execute_command(args: argparse.Namespace, command_dict: dict, user_namespace
s = os.path.splitext(x)
else:
s = x.split()
# update variables only available when flag specified
if args.dataframe:
global df
if args.input_mode == "stdin":
Expand All @@ -251,25 +252,18 @@ def execute_command(args: argparse.Namespace, command_dict: dict, user_namespace
global db, conn
if args.dataframe:
db = duckdb.from_df(df)
elif args.json:
tf = tempfile.NamedTemporaryFile(mode="w+")
json.dump(js, tf)
tf.file.flush()
db = duckdb.read_json(tf.name)
elif args.input_mode == "stdin":
try:
tf = tempfile.NamedTemporaryFile(mode="w+")
tf.write(x)
tf.file.flush()
db = duckdb.read_json(tf.name)
except Exception:
try:
db = duckdb.read_csv(io.StringIO(x))
except Exception:
db = x
db = duckdb.read_csv(io.StringIO(x))
else:
try:
conn = duckdb.connect(x, read_only=True)
db = [row[0] for row in conn.sql("SHOW TABLES;").fetchall()]
except Exception:
conn = duckdb.connect(":default:")
try:
Expand All @@ -281,7 +275,7 @@ def execute_command(args: argparse.Namespace, command_dict: dict, user_namespace
try:
db = duckdb.read_csv(x)
except Exception:
db = x
db = Exception(r"Could not read file, try replace-str '{}' or f-string '{x}' to pass the file path, or check duckdb extensions")
# return early if dry run (still safe to do after setting variables, and tests if any fail, but probably still want to do this before evaluating f-strings)
if args.dry_run:
colour_print(cmd, "0")
Expand Down

0 comments on commit 4e199b7

Please sign in to comment.