Print traceback "ending" on the assert-line, in Python3 unittest addFailure - python-3.x

I have made a simple, custom TestResult class (not inheriting from anything). When my python unittest fails, addFailure(self, test, err) is called as expected.
err[2] contains a traceback
I now print the traceback with this command: traceback.print_tb(err[2])
The print out contains two more levels than expected/desired.
File "/usr/lib64/python3.4/unittest/case.py", line 58, in testPartExecutor
yield
File "/usr/lib64/python3.4/unittest/case.py", line 580, in run
testMethod()
File "/home/xplatformer/code/tools/python/exception_test/my_test.py", line 23, in test_my4
self.assertEqual(5,4)
File "/usr/lib64/python3.4/unittest/case.py", line 800, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/lib64/python3.4/unittest/case.py", line 793, in _baseAssertEqual
raise self.failureException(msg)
How can I get the traceback to "end" at the assertEqual (line 23 in my_test.py)
Similarly, when extracting the filename like this: err[2].tb_frame.f_code.co_filename, I get case.py and not my_test.py as expected/desired.
How can I get the filename where the assertion occurred?

from the log it is pretty clear that, there is self.assertEqual(5,4) which fails the test case on line 23 in the method test_my4 in file /home/xplatformer/code/tools/python/exception_test/my_test.py
change the self.assertEqual(5,5) will pass the test case.

Related

Pandas - Suppress call to _validate_archive? - Python 3.9.13/Pandas 1.4.2/Openpyxl 3.0.10

I have been digging through questions/answers for the BadZipFile exception when calling read_excel() using the openpyxl engine. I looked at my error stack and dug into the Python files and it looks like ZipFile.py is being very strict when validating an archive. It is looking for an EOCD (end of central directory) signature in my XLSX archive file.
When unzipping an archive, if the EOCD cannot be found or validated, there is supposed to be an error thrown when calling unzip in Linux, but I am not seeing it. I am unsure whether the EOCD is there/correct or not (anyone know of a tool to check?).
However, from looking through my stack (below) I am examining what is happening in openpyxl/reader/excel.py. At line 67, the _validate_archive function is defined. I am wondering about the examination for a "file like object".
My use case is an AWS Lambda function which has an HTTP endpoint. I POST an Excel file (I am testing with Postman and using the binary body for the request where I select my Excel file) to the endpoint. The function needs to handle both CSV and XLSX. I include a custom header in which I specify the original file name. I split the filename, look at the extension, and either call read_csv or read_excel. read_csv is working great.
Either way, the file is coming in as base64. For an XLSX file, Pandas handles this OK - up until we get to _validate_archive... What I am unsure of is how the "file like object" check at line 76...
is_file_like = hasattr(filename, 'read')
... interacts with the type by which the Base64 is handled. I am trying straight string (event["body"]), the bytes() object, the BytesIO class, and the StringIO class, all to the same BadZipFile exception.
So... is it possible in Pandas/Openpyxl to suppress the validation of the archive? I want to be able to call read_excel() but not have the archive validated and see what happens.
My error stack:
"Error: (<class 'zipfile.BadZipFile'>, BadZipFile('File is not a zip file'),
<traceback object at 0x7f1019589dc0>)\r\n<class 'zipfile.BadZipFile'>\r\n
File is not a zip file\r\nTraceback (most recent call last):\n
File "/var/task/lambda_function.py", line 20, in lambda_handler\n inv = pd.read_excel( bufferedString, engine='openpyxl', index_col=0 )\n
File "/opt/python/pandas/util/_decorators.py", line 311, in wrapper\n return func(*args, **kwargs)\n
File "/opt/python/pandas/io/excel/_base.py", line 457, in read_excel\n io = ExcelFile(io, storage_options=storage_options, engine=engine)\n
File "/opt/python/pandas/io/excel/_base.py", line 1419, in init\n self._reader = self._engines[engine](self._io, storage_options=storage_options)\n
File "/opt/python/pandas/io/excel/_openpyxl.py", line 525, in init\n super().init(filepath_or_buffer, storage_options=storage_options)\n
File "/opt/python/pandas/io/excel/_base.py", line 518, in init\n self.book = self.load_workbook(self.handles.handle)\n
File "/opt/python/pandas/io/excel/_openpyxl.py", line 536, in load_workbook\n return load_workbook(\n
File "/opt/python/openpyxl/reader/excel.py", line 315, in load_workbook\n reader = ExcelReader(filename, read_only, keep_vba,\n
File "/opt/python/openpyxl/reader/excel.py", line 124, in init\n self.archive = _validate_archive(fn)\n
File "/opt/python/openpyxl/reader/excel.py", line 96, in _validate_archive\n archive = ZipFile(filename, 'r')\n
File "/var/lang/lib/python3.9/zipfile.py", line 1264, in init\n self._RealGetContents()\n
File "/var/lang/lib/python3.9/zipfile.py", line 1331, in _RealGetContents\n
raise BadZipFile("File is not a zip file")\n
zipfile.BadZipFile: File is not a zip file\n"

Strange issue with Python recursive function in Chalice framework

I have defined this SNS-triggered Lambda in Chalice:
#app.on_sns_message(topic='arn:aws:sns:us-west-1:XXXXXXXX:MyTopic')
def step1_photo_url_preload(event, retry = 3):
try:
js = json.loads(event.message)
... some logic here, event object is never modified ...
except:
if retry:
print("WARNING: failed, %d retries remaining" % retry)
return step1_photo_url_preload(event, retry-1)
else:
raise
When an exception is raised, the function should retry up to 3 times.
Instead, what I get is the exception below. Look closely at the trace: Line 56 shows the error occurs when attempting the recursive call:
[ERROR] TypeError: 'SNSEvent' object is not subscriptable
Traceback (most recent call last):
File "/var/task/chalice/app.py", line 1459, in __call__
return self.func(event_obj)
File "/var/task/app.py", line 56, in step1_photo_url_preload
return step1_photo_url_preload(event, retry-1)
File "/var/task/chalice/app.py", line 1458, in __call__
event_obj = self.event_class(event, context)
File "/var/task/chalice/app.py", line 1486, in __init__
self._extract_attributes(event_dict)
File "/var/task/chalice/app.py", line 1532, in _extract_attributes
first_record = event_dict['Records'][0]
Mysteriously, the function can't work with the event object that it received the first time.
What could cause this?
I suspect this might have something to do with the magic behind #app.on_sns_message, but I'm not sure where to look next.
The problem is the fact that the function is decorated, the failure is in the code the decorator is running. Pull the functionality you want to run recursively into a separate function and the problem should go away.

Jira Python API to insert comment with WIKI formatting

While using jira python module to add comments, is there a possibility to insert comments using WIKI markup?
I noticed some example showed REST API calls with representation:wiki can do this.
But I noticed current python Jira only supports plain text as comments. Is this a limitation or I am missing something?
I checked jira source code and noticed data is represented as dictionary and also being dumped using json.dumps, I tried pushing body as dict "{'storage': {'value': '== Activity: == error-rate-percentage-is-at-acceptable-limits .. Started', 'representation': 'wiki'}}"
But got below error back from Jira API call I guess
[2019-12-02 01:07:22 DEBUG] [__init__:386] Before-control 'jira-integration' failed
Traceback (most recent call last):
File "<<HIDDEN BY ME>>>/lib/python3.7/site-packages/chaoslib/control/__init__.py", line 377, in apply_controls
settings=settings)
File "<<HIDDEN BY ME>>>/python3.7/site-packages/chaoslib/control/python.py", line 147, in apply_python_control
func(context=context, **arguments)
File "<<HIDDEN BY ME>>>/python3.7/site-packages/<<HIDDEN BY ME>>/controls/jira/tickets.py", line 220, in before_activity_control
add_comment(os.environ["SUB_TASK_TICKET"], content_as_wiki(formatting.format_as_heading2("Activity: ") + str(context["name"]) + " .. Started"))
File "<<HIDDEN BY ME>>>/python3.7/site-packages/<<HIDDEN BY ME>>/controls/jira/tickets.py", line 58, in add_comment
test = JIRA_CLIENT.add_comment(issue, comment)
File "<<HIDDEN BY ME>>/python3.7/site-packages/jira/client.py", line 126, in wrapper
result = func(*arg_list, **kwargs)
File "<<HIDDEN BY ME>>/python3.7/site-packages/jira/client.py", line 1367, in add_comment
url, data=json.dumps(data)
File "<<HIDDEN BY ME>>/python3.7/site-packages/jira/resilientsession.py", line 154, in post
return self.__verb('POST', url, **kwargs)
File "<<HIDDEN BY ME>>/python3.7/site-packages/jira/resilientsession.py", line 147, in __verb
raise_on_error(response, verb=verb, **kwargs)
File "<<HIDDEN BY ME>>/python3.7/site-packages/jira/resilientsession.py", line 57, in raise_on_error
r.status_code, error, r.url, request=request, response=r, **kwargs)
jira.exceptions.JIRAError: JiraError HTTP 400 url: https://<<HIDDEN BY ME>>>
text: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: com.enhancera.auditor.common.filter.RestReadingServletRequest$1#4e08280a; line: 1, column: 2] (through reference chain: com.atlassian.jira.issue.fields.rest.json.beans.CommentJsonBean["body"])
I was able to create a comment using markup by passing a string formatted according to the documentation here. For a test I created an issue and added a comment to add a two column 1 row (+ header row) table with:
comment = jira.add_comment(issue, "||header1||header2||\n|one|two|")
Which produced:
Make sure to wrap your comment string in double quotes.

python 3 EOFError: EOF when reading a line

The errors saying
Traceback (most recent call last):
File "rental_car-customer-data-2.py", line 18, in
odoEnd = int(input("Ending Odometer Reading:"))
EOFError: EOF when reading a line
I have tried removing int and copy paste same line from above that doesn't error odoStart and change it to odoEnd but not working??
not sure if the odoEnd - odoStart will work either?
odoStart = int(input ("Starting Odometer Reading:"))
odoEnd = int(input("Ending Odometer Reading:"))****
print(odoStart)
print(odoEnd)
totalMiles = (odoEnd - odoStart)
print (totalMiles)
This is a common error when reading a file. When you get to the end of a file when reading in python you have to go back to line one by using this command: file.seek(0), where file is the name of the file variable.

Rasa ReminderScheduled makes the program crash, error with timezone

I tried to use Rasa's ReminderScheduled as specified in the docs. I'm using Windows 10 with the Ubuntu subsystem to run the code. The code that calls the reminder is the following:
export_timeout = datetime.timedelta(seconds=30)
class ActionGiveListProducts(Action):
def name(self):
return 'action_give_list_products'
def run(self, dispatcher, tracker, domain):
s = getInfo("listeproduits")
dispatcher.utter_message(s)
return [ReminderScheduled("action_export_logs", datetime.datetime.now() + export_timeout)]
Executing this Action causes the following error:
Traceback (most recent call last):
File "bot.py", line 136, in <module>
run()
File "bot.py", line 108, in run agent.handle_channel(ConsoleInputChannel())
File "/usr/local/lib/python3.5/dist-packages/rasa_core/agent.py", line 126, in handle_channel
processor.handle_channel(input_channel)
File "/usr/local/lib/python3.5/dist-packages/rasa_core/processor.py", line 60, in handle_channel
input_channel.start_sync_listening(self.handle_message)
File "/usr/local/lib/python3.5/dist-packages/rasa_core/channels/console.py", line 52, in start_sync_listening
self._record_messages(message_handler)
File "/usr/local/lib/python3.5/dist-packages/rasa_core/channels/console.py", line 45, in _record_messages
self.sender_id))
File "/usr/local/lib/python3.5/dist-packages/rasa_core/processor.py", line 83, in handle_message
self._predict_and_execute_next_action(message, tracker)
File "/usr/local/lib/python3.5/dist-packages/rasa_core/processor.py", line 262, in _predict_and_execute_next_action
dispatcher)
File "/usr/local/lib/python3.5/dist-packages/rasa_core/processor.py", line 312, in _run_action
self._schedule_reminders(events, dispatcher)
File "/usr/local/lib/python3.5/dist-packages/rasa_core/processor.py", line 296, in _schedule_reminders
replace_existing=True)
File "/usr/local/lib/python3.5/dist-packages/apscheduler/schedulers/base.py", line 413, in add_job
'trigger': self._create_trigger(trigger, trigger_args),
File "/usr/local/lib/python3.5/dist-packages/apscheduler/schedulers/base.py", line 907, in _create_trigger
return self._create_plugin_instance('trigger', trigger, trigger_args)
File "/usr/local/lib/python3.5/dist-packages/apscheduler/schedulers/base.py", line 892, in _create_plugin_instance
return plugin_cls(**constructor_kwargs)
File "/usr/local/lib/python3.5/dist-packages/apscheduler/triggers/date.py", line 20, in __init__
timezone = astimezone(timezone) or get_localzone()
File "/usr/local/lib/python3.5/dist-packages/apscheduler/util.py", line 86, in astimezone
'Unable to determine the name of the local timezone -- you must explicitly '
ValueError: Unable to determine the name of the local timezone -- you must explicitly
specify the name of the local timezone.
Please refrain from using timezones like EST to prevent problems with daylight saving time.
Instead, use a locale based timezone name (such as Europe/Helsinki).
I tried to set the timezone in the launch code as following:
os.environ['TZ'] = 'Europe/London'
time.tzset()
but this didn't change anything. I also searched for other solutions, but found nothing relevant.
Does someone know what causes this error exactly and if there is way to eliminate it?
I assume you run this in a Linux environment (as I had the same error), so try to set the following:
keep as you had it in the python code:
keepos.environ['TZ'] = 'Europe/London'
and also set the timezone in the os:
sudo cp /usr/share/zoneinfo/Europe/London /etc/localtime
It worked for me.
The following works on the Ubuntu subsystem for Windows:
sudo cp /usr/share/zoneinfo/America/New_York /etc/localtime
TZ=America/New_York rasa x
You can also make the TZ environment variable permanent by adding export TZ=America/New_York in your ~/.bashrc file.

Resources