Python Return a list in Bottle - mysql

I have a list from an mysql query that I'm trying to return in my bottle website. Is this possible? Here's what I have:
def create_new_location():
kitchen_locations = select_location()
return template('''
% for kitchen_location in {{kitchen_locations}}:
{{kitchen_location}} Kitchen
<br/>
% end''',kitchen_locations=kitchen_locations)
This is the error that I get.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/bottle.py", line 862, in _handle
return route.call(**args)
File "/usr/local/lib/python2.7/site-packages/bottle.py", line 1732, in wrapper
rv = callback(*a, **ka)
File "index.py", line 32, in create_new_location
</form>''',kitchen_locations=kitchen_locations)
File "/usr/local/lib/python2.7/site-packages/bottle.py", line 3609, in template
return TEMPLATES[tplid].render(kwargs)
File "/usr/local/lib/python2.7/site-packages/bottle.py", line 3399, in render
self.execute(stdout, env)
File "/usr/local/lib/python2.7/site-packages/bottle.py", line 3386, in execute
eval(self.co, env)
File "<string>", line 6, in <module>
TypeError: unhashable type: 'set'

Got It (took me a while...)
% for kitchen_location in {{kitchen_locations}}:
Should be
% for kitchen_location in kitchen_locations:
When using the % at the beginning you don't need the {{}}.
This error:
TypeError: unhashable type: 'set'
is trying to use a set literal {{kitchen_locations}} ==>
kitchen_locations in a set in another set. since set is not hash-able you got the error

Related

TypeError: argument of type 'function' is not iterable

So I tried to make Jarvis (Inspired by iron man) and everything was going well, until lines 32-39.
Here is the output:Here is an image of my code
PS C:\Users\HP> & C:/Users/HP/AppData/Local/Programs/Python/Python311/python.exe c:/Users/HP/Desktop/Python/Jarvis_AI.py
<function take_command at 0x000001C3A659C7C0>
Traceback (most recent call last):
File "c:\Users\HP\Desktop\Python\Jarvis_AI.py", line 42, in <module>
run_jarvis()
File "c:\Users\HP\Desktop\Python\Jarvis_AI.py", line 35, in run_jarvis
if "play" in command:
^^^^^^^^^^^^^^^^^
TypeError: argument of type 'function' is not iterable
'''
In line 35, you're saving the function take_command to the variable command. I imagine you're wanting to save whatever gets returned from take_command.
So on line 35, change
command = take_command
to
command = take_command()
So just adding the brackets to take_command.

Json loads fails on mqtt message

I'm trying to parse the messages sent on an MQTT topic as JSONs, but when i try to call json.loads() on the decoded string, the code goes into exception:
File "/usr/lib/python3.7/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 153 (char 152)
The code is the following:
def on_message_mqtt(self, client, userdata, message):
m_decode = message.payload.decode('utf-8')
logger.info(m_decode)
m_in = json.loads(m_decode)
logger.info(m_in)
While the payload of the messages has the following format:
{"Timestamp":"2021-05-24-13:27:13.450","AI":[1204,7,0,4,18,2,1176,802,11,0,381,2496,0,0,810,1282],"DI":[false,false,false,false,false,false,false,false]}
I tried to put the text into a string and loads() is able to parse it, so I bet that the problem is on the initial string decoding.
Here you can see a print of the decoded string, and then the error on the call to json.loads():
2021-05-24 16:31:47 - INFO - {"Timestamp":"2021-05-24-14:31:44.790","AI":[912,9,0,2,16,2,886,605,11,0,321,1924,1,963,620,9],"DI":[false,false,false,false,false,false,false,false]}
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "router.py", line 136, in run
self.mqtt.loop_forever()
File "/opt/control/env/lib/python3.7/site-packages/paho/mqtt/client.py", line 1779, in loop_forever
rc = self.loop(timeout, max_packets)
File "/opt/control/env/lib/python3.7/site-packages/paho/mqtt/client.py", line 1181, in loop
rc = self.loop_read(max_packets)
File "/opt/control/env/lib/python3.7/site-packages/paho/mqtt/client.py", line 1572, in loop_read
rc = self._packet_read()
File "/opt/control/env/lib/python3.7/site-packages/paho/mqtt/client.py", line 2310, in _packet_read
rc = self._packet_handle()
File "/opt/control/env/lib/python3.7/site-packages/paho/mqtt/client.py", line 2940, in _packet_handle
return self._handle_pubrel()
File "/opt/control/env/lib/python3.7/site-packages/paho/mqtt/client.py", line 3246, in _handle_pubrel
self._handle_on_message(message)
File "/opt/control/env/lib/python3.7/site-packages/paho/mqtt/client.py", line 3444, in _handle_on_message
self.on_message(self, self._userdata, message)
File "router.py", line 87, in on_message_mqtt
logger.info(json.loads(m_decode), exc_info=True)
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 151 (char 150)
Judging from the error, character 152 is the square bracket closing the arroy of the value for the key of "DI" ] which would imply incorrect parsing using the payload.decode function
Can you check if this works
def on_message_mqtt(self, client, userdata, message):
m_in = json.loads(message.decode("utf-8"))
logger.info(m_in)

Getting this error with py2.7 as well as with py3.7

Getting this error with py2.7 as well as with py3.7
enter code here
Exception happened during processing of request from ('10.0.2.15', 41994)
Traceback (most recent call last):
File "/usr/lib/python3.8/socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python3.8/socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python3.8/socketserver.py", line 720, in __init__
self.handle()
File "/usr/lib/python3.8/http/server.py", line 427, in handle
self.handle_one_request()
File "/usr/lib/python3.8/http/server.py", line 415, in handle_one_request
method()
File "/usr/share/set/src/webattack/harvester/harvester.py", line 334, in do_POST
filewrite.write(cgi.escape("PARAM: " + line + "\n"))
AttributeError: module 'cgi' has no attribute 'escape'
I think, you need to add import html under import cgi and then change cgi.escape to html.escape. You need to do that in /usr/share/set/src/webattack/harvester/harvester.py (for details you can check this link - https://github.com/trustedsec/social-engineer-toolkit/issues/721)
import html
html.escape(string_).encode('ascii', 'xmlcharrefreplace')

App randomly crashes with a JSON error

I've posted first the error message with full traceback and the code as well.
The code that is below is within a loop, that will run multiple times changing the variables within the session.get() link for the API data call.
From time to time I will get an error that the json.decoder.JSONDecodeError is expecting value... (full error below)
I can't figure out why.
Traceback (most recent call last):
File "C:\Users\CarlosMiguel\Desktop\experiments\data_collector\data_collector.py", line 100, in <module>
get_data = session.get(api_data_link+symbol+"?limit=1000&period="+i).json()
File "C:\Users\CarlosMiguel\Anaconda3\lib\site-packages\requests\models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\CarlosMiguel\Anaconda3\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\CarlosMiguel\Anaconda3\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\CarlosMiguel\Anaconda3\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Currently It's written like this (everything that needs to be is imported):
try:
get_data = session.get(api_data_link+symbol+"?limit=1000&period="+i).json()
for candle in get_data:
#Do things with the requested data....
except:
print("JSONError")
print('Could not update data for')
print(symbol + ' ' + i)
Could it be that the API just doesn't return anything on some random call?
Because it seems to be completely random when it crashes with that error.
If it could be the API just not returning anything, how could I write it to call it again for a few times, before continuing on with the rest of the app?

have no idea what the exception is here

I am trying to read a csv file. using below command:
sample = pd.read_csv("C:/kushal/DataMining/hillary/out.txt" ,header = 0, delimiter = "\t")
Unfortunately it gives me some exception about which I have no idea what is causing it. Does anyone knows anything about this exception
sample["ExtractedBodyText"][1]
Traceback (most recent call last):
File "<pyshell#75>", line 1, in <module>
hillary["ExtractedBodyText"][1]
File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 1914, in __getitem__
return self._getitem_column(key)
File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 1921, in _getitem_column
return self._get_item_cache(key)
File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 1090, in _get_item_cache
values = self._data.get(item)
File "C:\Python34\lib\site-packages\pandas\core\internals.py", line 3102, in get
loc = self.items.get_loc(item)
File "C:\Python34\lib\site-packages\pandas\core\index.py", line 1692, in get_loc
return self._engine.get_loc(_values_from_object(key))
File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:3979)
File "pandas\index.pyx", line 157, in pandas.index.IndexEngine.get_loc (pandas\index.c:3843)
File "pandas\hashtable.pyx", line 668, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12265)
File "pandas\hashtable.pyx", line 676, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12216)
KeyError: 'ExtractedBodyText'