Check if a variable exists - python

I am using following method isset(var) to determine if a variable exists.
def isset(variable):
try:
variable
except NameError:
return False
else:
return True
It returns True if variable exists. But if a variable doesn't exist I get following:
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/lenovo/pyth/master/vzero/__main__.py", line 26, in <module>
ss.run()
File "vzero/ss.py", line 4, in run
snap()
File "vzero/ss.py", line 7, in snap
core.display()
File "vzero/core.py", line 77, in display
stdout(session(username()))
File "vzero/core.py", line 95, in session
if isset(ghi): #current_sessions[user]):
NameError: global name 'ghi' is not defined
I don't want all these errors. I just want it return False. No output. How can I do this?

Instead of writing a complex helper function isset and calling it
if not isset('variable_name'):
# handle the situation
in the place where you want to check the presence of the variable do:
try:
# some code with the variable in question
except NameError:
# handle the situation

Related

TypeError: must provide a stream to wrap

When I type pipenv shell in my command line to activate virtual environment I got this error:
Traceback (most recent call last):
File "c:\python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\asus\AppData\Roaming\Python\Python39\Scripts\pipenv.exe\__main_
_.py", line 4, in <module>
File "C:\Users\asus\AppData\Roaming\Python\Python39\site-packages\pipenv\__ini
t__.py", line 41, in <module>
stdout = get_text_stream("stdout")
File "C:\Users\asus\AppData\Roaming\Python\Python39\site-packages\pipenv\vendo
r\vistir\misc.py", line 1191, in get_text_stream
return get_wrapped_stream(sys_stream, encoding)
File "C:\Users\asus\AppData\Roaming\Python\Python39\site-packages\pipenv\vendo
r\vistir\misc.py", line 969, in get_wrapped_stream
raise TypeError("must provide a stream to wrap")
TypeError: must provide a stream to wrap
if you know how to overcome this error, please answer. Thank you.
I think what your problem is, that you're missing stdout. I've had a similar problem with a script that was invoked from KDE. It kind of failed with a similar error message.
My fix for the moment was to simply redirect the stdout to /dev/null. I assume that this won't quite work, considering you are working from within a shell that should have stdin/stout. You might have to reconsider your working environment. But just a guess.

I am getting error while while using "zappa init'

I am using python 3.8 and zappa 0.51.0. I have installed zappa in a virtual environment and created AWS account also but when I am trying to command "zappa init" its showing error given below
(.env) D:\rough work\crud>zappa init
Traceback (most recent call last):
File "c:\users\dwipal shrirao\appdata\local\programs\python\python38\Lib\runpy.py", line 192, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\dwipal shrirao\appdata\local\programs\python\python38\Lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\rough work\crud\.env\Scripts\zappa.exe\__main__.py", line 4, in <module>
File "d:\rough work\crud\.env\lib\site-packages\zappa\cli.py", line 44, in <module>
from .core import Zappa, logger, API_GATEWAY_REGIONS
File "d:\rough work\crud\.env\lib\site-packages\zappa\core.py", line 33, in <module>
import troposphere
File "d:\rough work\crud\.env\lib\site-packages\troposphere\__init__.py", line 586, in <module>
class Template(object):
File "d:\rough work\crud\.env\lib\site-packages\troposphere\__init__.py", line 588, in Template
'AWSTemplateFormatVersion': (basestring, False),
NameError: name 'basestring' is not defined
what is happening and how I can get rid of this error?
The builtin basestring abstract type was removed. Use str instead. The str and bytes types don’t have functionality enough in common to
warrant a shared base class. The 2to3 tool (see below) replaces every
occurrence of basestring with str.
As you are with python version 3.8, use str instead.

cProfiler working weirdly with multiprocessing

I got an error for this code:
from pathos.multiprocessing import ProcessingPool
def diePlz(im):
print('Whoopdepoop!')
def caller():
im = 1
pool = ProcessingPool()
pool.map(diePlz,[im,im,im,im])
if __name__=='__main__':
caller()
when I ran it with the cProfiler: (python3 -m cProfile testProfiler.py)
multiprocess.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/home/rohit/.local/lib/python3.6/site-packages/multiprocess/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/home/rohit/.local/lib/python3.6/site-packages/multiprocess/pool.py", line 44, in mapstar
return list(map(*args))
File "/home/rohit/.local/lib/python3.6/site-packages/pathos/helpers/mp_helper.py", line 15, in <lambda>
func = lambda args: f(*args)
File "testProfiler.py", line 3, in diePlz
print('Whoopdepoop!')
NameError: name 'print' is not defined
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/lib/python3.6/cProfile.py", line 160, in <module>
main()
File "/usr/lib/python3.6/cProfile.py", line 153, in main
runctx(code, globs, None, options.outfile, options.sort)
File "/usr/lib/python3.6/cProfile.py", line 20, in runctx
filename, sort)
File "/usr/lib/python3.6/profile.py", line 64, in runctx
prof.runctx(statement, globals, locals)
File "/usr/lib/python3.6/cProfile.py", line 100, in runctx
exec(cmd, globals, locals)
File "testProfiler.py", line 11, in <module>
caller()
File "testProfiler.py", line 8, in caller
pool.map(diePlz,[im,im,im,im])
File "/home/rohit/.local/lib/python3.6/site-packages/pathos/multiprocessing.py", line 137, in map
return _pool.map(star(f), zip(*args)) # chunksize
File "/home/rohit/.local/lib/python3.6/site-packages/multiprocess/pool.py", line 266, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/home/rohit/.local/lib/python3.6/site-packages/multiprocess/pool.py", line 644, in get
raise self._value
NameError: name 'print' is not defined
But when I ran it without the cProfiler:
$ python3 testProfiler.py
Whoopdepoop!
Whoopdepoop!
Whoopdepoop!
Whoopdepoop!
The code that I've provided is a minimal working example for the problem. There is a much larger code that I want to debug, but am not able to do so because cProfiler keeps raising weird errors.
In this case, the point of importance is
NameError: name 'print' is not defined
which means python3 is not able to recognize print itself. In my code, it was not able to recognize range.
So, I realize this is a long time after the original post, but I have this exact same issue.
In my case I was getting the exact same error as the original post - python builtin functions such as print() or len() resulted in errors like this:
NameError: name 'len' is not defined
I'm currently running multiprocess version 0.70.11.1 and dill version 0.3.3 (components of pathos that make process based parallelism work).
Based on what I found in an issue comment: https://github.com/uqfoundation/pathos/issues/129#issuecomment-536081859 one of the package authors recommends trying:
import dill
dill.settings['recurse'] = True
At least in my case, the above fixed the error!

How to extract error detail from traceback by using python regex?

I want to extract error detail from traceback, those tracebacks are extract from log file by using this method, and there are many different kinds of tracebacks, like below:
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/root/env/common/test/test/__main__.py", line 5, in <module>
main()
File "/root/env/common/test/test/cli/parser.py", line 55, in main
run_script(args)
File "/root/env/common/test/test/cli/runner.py", line 124, in run_script
exec_script(args.script, scope=globals(), root=True)
File "/root/workspace/group/test_regression_utils.py", line 123, in exec_script
cli_exec_script(*args,**kwargs)
File "/root/env/common/test/test/cli/runner.py", line 186, in exec_script
exec(compile(code, scriptpath, 'exec')) in scope
File "shiju_12.py", line 30, in <module>
File "/root/moaworkspace/group/testscript/utils/shiju_public.py", line 37, in shiju_move
Exception: close failed!
EndOfStream
EndOfStream
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/root/env/common/test/test/__main__.py", line 5, in <module>
main()
File "/root/env/common/test/test/cli/parser.py", line 55, in main
run_script(args)
File "/root/env/common/test/test/cli/runner.py", line 124, in run_script
exec_script(args.script, scope=globals(), root=True)
File "/root/env/common/test/test/cli/runner.py", line 186, in exec_script
exec(compile(code, scriptpath, 'exec')) in scope
File "/root/env/common/mator/mator/mator.py", line 520, in start
raise IOError("RPC server not started!")
IOError: RPC server not started
the expect result is:
("XXXX", "Exception: close failed!")
("XXXX","IOError: RPC server not started")
I have tried detail_regex = r'Traceback.*\n(\s+.*\n)*(.*)\n*'
the second traceback is right, but the first traceback result is ("Exception: close failed!", "EndOfStream")
any ideas?
If I change your pattern to Traceback.*\n(\s+.*\n)*(.*?)\n* it works for the given example. I am not sure though, that it solves your problem completely.
That will match:
Traceback \(most recent call last\): Match literally
(?:\n.*)+? Repeat in a non capturing group matching a newline followed by 0+ times any character
\n(.*?(?:Exception|Error):) Match newline and capturinggroup 0+ characters non greedy and than match Exception of Error followed by :`
\s* Match 0+ whitespace characters
(.+) Capturing group 1+ times any character
For example:
import re
import traceback
EXCEPTION_PATTERN = re.compile(
r"Traceback \(most recent call last\):(?:\n.*)+?\n(.*?(?:Exception|Error):)\s*(.+)"
)
try:
hello()
except Exception as ex:
try:
1/0
except Exception as ex:
ex_match = EXCEPTION_PATTERN.findall(traceback.format_exc())
print(ex_match)
output:
[('NameError:', "name 'hello' is not defined"), ('ZeroDivisionError:', 'division by zero')]
If you need to count Exception / Error with details:
for i, data in enumerate(ex_match, start=1):
print(f"No.{i} - {data[0]} {data[1]}")
output:
No.1 - NameError: name 'hello' is not defined
No.2 - ZeroDivisionError: division by zero

Dynamically alter traceback/stack frame function name

From time to time, I write a generic function in Python that gets called multiple times with different arguments. Usually, this is driven by a definition somewhere else.
For example:
def issue_sql_query(name, select_stmt):
...
QUERIES = [
"get_user_rows", "SELECT name, rowid FROM table WHERE type == 'USER';"
...
]
results = []
for name, select_stmt in QUERIES:
results.append((name, issue_sql_query(name, select_stmt)))
If there's an exception in the generic function (i.e., issue_sql_query or somewhere deeper), I have relatively little info in the traceback to identify which definition caused the error.
What I'd like to do is dynamically rename or augment the function name/stack frame so that tracebacks would include some identifying info.
What would be nice is something like this:
File "test.py", line 21, in <module>
results.append((name, issue_sql_query(select_stmt)))
File "test.py", line 11, in issue_sql_query(name="get_user_rows")
raise RuntimeError("Some error")
RuntimeError: Some error
I could, of course, stick exception handling at the generic points and rebuild the exception using traceback to have more context, which is pretty straightforward and likely the right choice. It gets a little tricky when you have multiple levels of generic functions, but that's certainly possible to handle.
Any other ideas on how to accomplish this? Did I miss some easy way to change the stack frame name?
Edit:
Adding an example traceback showing a relatively not useful traceback:
Traceback (most recent call last):
File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "c:\tmp\report.py", line 767, in <module>
main()
File "c:\tmp\report.py", line 750, in main
charts.append(report.get_chart(title, definition))
File "c:\tmp\report.py", line 614, in get_chart
return self.get_bar_chart(title, definition)
File "c:\tmp\report.py", line 689, in get_bar_chart
definition, cursor, **kwargs))
File "c:\tmp\report.py", line 627, in create_key_table
for row in cursor.execute(full_select_stmt):
sqlite3.OperationalError: near "==": syntax error
You could create wrapper functions for each named query so that you can control the exception thrown:
>>> fns = {}
>>> def issue_sql_query(name, stmt):
... if name not in fns:
... def f(name, stmt):
... # run query here
... raise Exception(name)
...
... fns[name] = f
... return fns[name](name, stmt)
...
>>>
>>> issue_sql_query('b', 'SQL')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 9, in issue_sql_query
File "<stdin>", line 5, in f
Exception: b
>>> issue_sql_query('a', 'SQL')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 9, in issue_sql_query
File "<stdin>", line 5, in f
Exception: a
>>>

Categories