Skip to content

Commit

Permalink
Fix fromjson() to support reading from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
yaniv-aknin committed May 12, 2024
1 parent 9fe7939 commit 60818d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion petl/io/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from petl.util.base import data, Table, dicts as _dicts, iterpeek


def fromjson(source, *args, **kwargs):
def fromjson(source=None, *args, **kwargs):

Check warning

Code scanning / Prospector (reported by Codacy)

Keyword argument before variable positional arguments list in the definition of fromjson function (keyword-arg-before-vararg) Warning

Keyword argument before variable positional arguments list in the definition of fromjson function (keyword-arg-before-vararg)
"""
Extract data from a JSON file. The file must contain a JSON array as
the top level object, and each member of the array will be treated as a
Expand Down
18 changes: 15 additions & 3 deletions petl/test/test_executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@
import subprocess

Check warning

Code scanning / Bandit (reported by Codacy)

Consider possible security implications associated with the subprocess module. Warning

Consider possible security implications associated with the subprocess module.

def test_executable():

Check warning

Code scanning / Pylint (reported by Codacy)

Missing function docstring Warning

Missing function docstring

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Missing function or method docstring Warning

Missing function or method docstring
result = subprocess.run("""
result = subprocess.check_output("""

Check warning

Code scanning / Bandit (reported by Codacy)

Starting a process with a partial executable path Warning

Starting a process with a partial executable path
(echo foo,bar ; echo a,b; echo c,d) |
petl 'fromcsv().cut("foo").head(1).tocsv()'
""", shell=True, check=True, capture_output=True)
assert result.stdout == b'foo\r\na\r\n'
""", shell=True)

Check failure

Code scanning / Bandit (reported by Codacy)

subprocess call with shell=True seems safe, but may be changed in the future, consider rewriting without shell Error

subprocess call with shell=True seems safe, but may be changed in the future, consider rewriting without shell
assert result == b'foo\r\na\r\n'

Check warning

Code scanning / Bandit (reported by Codacy)

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Warning

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

Check notice

Code scanning / Semgrep (reported by Codacy)

The application was found using assert in non-test code. Note

The application was found using assert in non-test code.

def test_json_stdin():

Check warning

Code scanning / Pylint (reported by Codacy)

Missing function docstring Warning

Missing function docstring

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Missing function or method docstring Warning

Missing function or method docstring
result = subprocess.check_output("""

Check warning

Code scanning / Bandit (reported by Codacy)

Starting a process with a partial executable path Warning

Starting a process with a partial executable path
echo '[{"foo": "a", "bar": "b"}]' |
petl 'fromjson().tocsv()'
""", shell=True)

Check failure

Code scanning / Bandit (reported by Codacy)

subprocess call with shell=True seems safe, but may be changed in the future, consider rewriting without shell Error

subprocess call with shell=True seems safe, but may be changed in the future, consider rewriting without shell
assert result == b'foo,bar\r\na,b\r\n'

Check warning

Code scanning / Bandit (reported by Codacy)

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Warning

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

Check notice

Code scanning / Semgrep (reported by Codacy)

The application was found using assert in non-test code. Note

The application was found using assert in non-test code.
result = subprocess.check_output("""

Check warning

Code scanning / Bandit (reported by Codacy)

Starting a process with a partial executable path Warning

Starting a process with a partial executable path
( echo '{"foo": "a", "bar": "b"}' ; echo '{"foo": "c", "bar": "d"}' ) |
petl 'fromjson(lines=True).tocsv()'
""", shell=True)

Check failure

Code scanning / Bandit (reported by Codacy)

subprocess call with shell=True seems safe, but may be changed in the future, consider rewriting without shell Error

subprocess call with shell=True seems safe, but may be changed in the future, consider rewriting without shell
assert result == b'foo,bar\r\na,b\r\nc,d\r\n'

Check warning

Code scanning / Bandit (reported by Codacy)

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Warning

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

Check notice

Code scanning / Semgrep (reported by Codacy)

The application was found using assert in non-test code. Note

The application was found using assert in non-test code.

0 comments on commit 60818d5

Please sign in to comment.