I there an option to enable "standard format" for python traceback. Ie, instead of
Traceback (most recent call last):
File "./make_directory.py", line 7, in <module>
data = json.loads(sys.argv[2])
IndexError: list index out of range
It should look like
Traceback (most recent call last):
./make_directory.py:7: error
7 | data = json.loads(sys.argv[2])
IndexError: list index out of range
I am looking for a solution that does not require any changes to the script in this. In this case make_directory.py.
Related
I've a python call from a powershell terminal.
minimalistic example:
Try {
& python "test.py"
}
Catch {
# Place individual error handling here
$ErrorMessage = "$($_.Exception.Message)"
}
echo $ErrorMessage
test.py raises an error, example content can be:
def make_sum():
return a+b
if __name__ == "__main__":
make_sum()
Why does the $ErrorMessage variable contain only the first line of python traceback?
Traceback (most recent call last):
I'd need complete call stack info as python provides:
Traceback (most recent call last):
File "d:/.../test.py", line 10, in <module>
make_sum()
File "d:/.../test.py", line 7, in make_sum
return a+b
NameError: name 'a' is not defined
Any advice how to get a complete traceback into the string variable in powershell?
Thank you,
Honza
I have a string that is a python traceback collapsed into a single line. I want to retrieve the multiline format of the traceback for the sake of readability. What is the best way to achieve it?
I see that traceback module has similar functionality, but I don't see how to pass a string as an argument there:
https://docs.python.org/3/library/traceback.html
Example input:
'Traceback (most recent call last): File "<stdin>", line 1, in <module> Exception'
Example output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception
I come from a python background, and error traceback back there is so easy to follow and allows for quick exception identification.
I was wondering if there was a package or a plugin (VSCode) that formats or makes error tracing easier in Flutter.
For non Python user, here are two examples:
Traceback (most recent call last):
File "/path/to/example.py", line 4, in <module>
greet('Chad')
File "/path/to/example.py", line 2, in greet
print('Hello, ' + someon)
NameError: name 'someon' is not defined
$ python example.py
Traceback (most recent call last):
File "/path/to/greetings.py", line 19, in <module>
greet('Chad', greting='Yo')
TypeError: greet() got an unexpected keyword argument 'greting'
I am successful in extracting the response from a JSON. However, I am unable to list all or extract what I need on the key and its pair
Below is my code:
import requests
response = requests.get("https://www.woolworths.com.au/apis/ui/Product/Specials/half-price?GroupID=948&isMobile=false&pageNumber=1&pageSize=36&richRelevanceId=SP_948&sortType=Personalised")
data = response.json()
I tried to do data['Stockcode']
but no luck or I use data['Product']
It says:
>>> data['Product']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Product'
>>> data['Products']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Products'
try:
>>> data['Items'][0]['Products']
Print data and see its data structure How its constructed then you can extract values as per your need
I've build a little internal DSL with python. I'm using assert vor validation. If the end user types a wrong parameter the dsl should report whats wrong. At the moment this looks like this:
Traceback (most recent call last):
File "tests/maskedtimefilter_test/FilterDSL_test.py", line 63, in test_dsl_validation
input(0): self.regular
File "/Users/sh/reetz/pythonpath/maskedtimedata/maskedtimefilter.py", line 885, in __lshift__
kwargs = self.dsl_validation(kwargs)
File "/Users/sh/reetz/pythonpath/maskedtimedata/maskedtimefilter.py", line 1483, in dsl_validation
check_if_valid(parameter)
File "/Users/sh/reetz/pythonpath/maskedtimedata/dsl.py", line 47, in kernel_a
def kernel_a (x): assert isinstance(x, (list, tuple, np.ndarray)), "kernel must be a list."
AssertionError: kernel must be a list.
But the end users are engineers and no computer scientists. Therefore a minimal Traceback is handy. Is it possible to shrink the Traceback to the essential information (where is the failure and what's the cause) like this?:
Traceback (most recent call last):
File "tests/maskedtimefilter_test/FilterDSL_test.py", line 63, in test_dsl_validation
input(0): self.regular
AssertionError: kernel must be a list.
Reluctantly I'd like to use normal prints!
Why not return the traceback data as an array and just work back from that?
import traceback
try:
codethatwillthrowanexception()
except:
exceptiondata = traceback.format_exc().splitlines()
exceptionarray = [exceptiondata[-1]] + exceptiondata[1:-1]
Somewhere in your call stack you can do this:
try:
my_function_that_can_raise_exception()
except Exception: #or AssertionError
import traceback
traceback.print_exc(limit=1)
limit is the depth of how many stack trace entries you want to show. (in your case, 1)
demo:
In [21]: def f():
...: assert False == True, 'Assertion!'
...:
In [22]: try:
...: f()
...: except Exception:
...: import traceback
...: traceback.print_exc(limit=1)
...:
Traceback (most recent call last):
File "<ipython-input-22-78f9db8d5188>", line 2, in <module>
f()
AssertionError: Assertion!
read more at traceback docs.
As the first part of the traceback is just what you called, you could so something simple, like:
try:
input(0): self.regular # base call
except AssertionError as ae: # catch the exception
print(ae) # print out the message
You can print out whatever you think would be useful in the except block.
Alternatively, you could do something more complex with the traceback module.