I am making a basic device using Python and I ran into this issue:
File "/home/pi/Adafruit_Python_SSD1306/PyOS_V0.py", line 178, in <module>
gameMenuApp()
File "/home/pi/Adafruit_Python_SSD1306/PyOS_V0.py", line 92, in gameMenuApp
draw.text((0, 32), gameMenuText, font=StartupFinishFont,
UnboundLocalError: local variable 'gameMenuText' referenced before assignment
This doesn't make sense because I assigned the variable at the beginning the file. I also put the code into a python linter online but I did not receive an error. Also, I removed most of the code to try to make it easier to read so if something doesn't make sense, don't worry about it. I posted the code on Github as well. https://github.com/raspiuser6969/PyOS
gameMenuText = " "
gameMenuSelect = int(1)
gameMenu = [' Pong ',' RPS ']
gameMenuSelect = int(1)
def pong():
def rps():
def gameMenuApp():
while GPIO.input(A_pin):
draw.rectangle((0,0,128,64), outline=0, fill=0)
draw.text((0,32), gameMenuText, font=StartupFinishFont, fill=255)
disp.image(image)
disp.display()
if not(GPIO.input(D_pin)):
gameMenuSelect = gameMenuSelect + 1
draw.rectangle((0,0,128,64), outline=0, fill=0)
disp.image(image)
disp.display()
print(gameMenuSelect)
if not(GPIO.input(U_pin)):
gameMenuSelect = gameMenuSelect - 1
draw.rectangle((0,0,128,64), outline=0, fill=0)
disp.image(image)
disp.display()
print(gameMenuSelect)
if gameMenuSelect == 1:
gameMenuText = gameMenu[gameMenuSelect - 1]
shutdownApp()
time.sleep(0.1)
if gameMenuSelect == 2:
gameMenuText = gameMenu[gameMenuSelect - 1]
rps()
time.sleep(0.1)
if 2 < gameMenuSelect:
gameMenuSelect = gameMenuSelect - 1
if 1 > gameMenuSelect:
gameMenuSelect = gameMenuSelect + 1
gameMenuText is defined in your file, but not in your function. You should either pass it as argument:
def gameMenuApp(gameMenuText, gameMenuSelect, gameMenu, gameMenuSelect):
or use it as global:
def gameMenuApp():
global gameMenuText, gameMenuSelect, gameMenu, gameMenuSelect
Same for your other globals, that's why I already added them.
Related
I am outputting the results of my machine learning model into a JSON file but my python code isn't parsing the file correctly. I don't know what is causing it but I know it is my python code and not anything on the ML side of it The code is supposed to run through the JSON file and find out if the results are within a certain distance of each other (0.10).
My Error:
File "vision.py", line 102, in <module>
find_xy(18, 18)
File "vision.py", line 61, in find_xy
x_dif = obj1["relative_coordinates"]["center_x"] - obj2["relative_coordinates"]["center_x"]
TypeError: 'NoneType' object is not subscriptable
My JSON file(result.json):
[
{
"frame_id":1,
"filename":"Captures/Capevent4/capture0.png",
"objects": [
{"class_id":17, "name":"g ", "relative_coordinates":{"center_x":0.789288, "center_y":0.412911, "width":0.339110, "height":0.789139}, "confidence":0.986945},
{"class_id":17, "name":"g ", "relative_coordinates":{"center_x":0.657672, "center_y":0.411369, "width":0.226476, "height":0.744315}, "confidence":0.532443},
{"class_id":17, "name":"g ", "relative_coordinates":{"center_x":0.736259, "center_y":0.559810, "width":0.409916, "height":0.651102}, "confidence":0.474929}
]
}
]
My Code:
Save New Duplicate & Edit Just Text Twitter
results_path = r'result.json'
# function for formatting after every new picture is run through model
with open(results_path, 'r') as opened_results:
results = json.load(opened_results)
with open(results_path, "w") as opened_results:
json.dump(results, opened_results, indent=4)
def find_xy(id1, id2):
desired_id1 = id1
obj1 = None
for thing in results:
for object1 in thing["objects"]:
if object1["class_id"] == desired_id1:
specific_class = object1
obj1 = object1
print("Correct Class")
break
for _class in results:
for object1 in _class["objects"]:
relative_coordinates = object1["relative_coordinates"]
center_x = relative_coordinates["center_x"]
center_y = relative_coordinates["center_y"]
# Do something with these values
desired_id2 = id2
obj2 = None
for thing in results:
for object2 in thing["objects"]:
if object2["class_id"] == desired_id2:
specific_class = object2
obj2 = object2
print("Correct Class")
break
for _class in results:
for object2 in _class["objects"]:
relative_coordinates = object2["relative_coordinates"]
center_x = relative_coordinates["center_x"]
center_y = relative_coordinates["center_y"]
x_dif = obj1["relative_coordinates"]["center_x"] - obj2["relative_coordinates"]["center_x"]
x_absolute_dif = abs(x_dif)
print("X Absolute Dif Is:" + str(x_absolute_dif))
if (x_absolute_dif <= 0.10):
print("X-Cords Within Range")
x_within_range = True
else:
print("X-Cords Not Within Range")
x_within_range = False
y_dif = obj1["relative_coordinates"]["center_y"] - obj2["relative_coordinates"]["center_y"]
y_absolute_dif = abs(y_dif)
print("Y Absolute Difference Is: " + str(y_absolute_dif))
if (y_absolute_dif <= 0.10):
print("Y-Cords Within Range")
y_within_range = True
else:
print("Y-Cords Not Within Range")
y_within_range = False
Okay, I found something.
"class_id":17...
"class_id":17...
"class_id":17...
All objects have the same ID? Is it correct so or not?
You need a check if block or use a try except.
if block
if obj1 == None:
print("No, obj1 found!")
exit(0)
try catch
try:
relative_coordinates = object1["relative_coordinates"]
except:
print("No, obj1 found!")
exit(0)
If you search find_xy(18, 18) and you have only "class_id":17 - that can only go wrong without a break position. So you must check if your variable still Nothing or not!
One point more
Duplicate code is also bad. Make a function for these, than you can read your code easier!
def check_obj():
#do you staff here...
this is the original repo i'm trying to run in my computer: https://github.com/kreamkorokke/cs244-final-project
import os
import matplotlib.pyplot as plt
import argparse
from attacker import check_attack_type
IMG_DIR = "./plots"
def read_lines(f, d):
lines = f.readlines()[:-1]
for line in lines:
typ, time, num = line.split(',')
if typ == 'seq':
d['seq']['time'].append(float(time))
d['seq']['num'].append(float(num))
elif typ == 'ack':
d['ack']['time'].append(float(time))
d['ack']['num'].append(float(num))
else:
raise "Unknown type read while parsing log file: %s" % typ
def main():
parser = argparse.ArgumentParser(description="Plot script for plotting sequence numbers.")
parser.add_argument('--save', dest='save_imgs', action='store_true',
help="Set this to true to save images under specified output directory.")
parser.add_argument('--attack', dest='attack',
nargs='?', const="", type=check_attack_type,
help="Attack name (used in plot names).")
parser.add_argument('--output', dest='output_dir', default=IMG_DIR,
help="Directory to store plots.")
args = parser.parse_args()
save_imgs = args.save_imgs
output_dir = args.output_dir
attack_name = args.attack
if save_imgs and attack_name not in ['div', 'dup', 'opt'] :
print("Attack name needed for saving plot figures.")
return
normal_log = {'seq':{'time':[], 'num':[]}, 'ack':{'time':[], 'num':[]}}
attack_log = {'seq':{'time':[], 'num':[]}, 'ack':{'time':[], 'num':[]}}
normal_f = open('log.txt', 'r')
attack_f = open('%s_attack_log.txt' % attack_name, 'r')
read_lines(normal_f, normal_log)
read_lines(attack_f, attack_log)
if attack_name == 'div':
attack_desc = 'ACK Division'
elif attack_name == 'dup':
attack_desc = 'DupACK Spoofing'
elif attack_name == 'opt':
attack_desc = 'Optimistic ACKing'
else:
raise 'Unknown attack type: %s' % attack_name
norm_seq_time, norm_seq_num = normal_log['seq']['time'], normal_log['seq']['num']
norm_ack_time, norm_ack_num = normal_log['ack']['time'], normal_log['ack']['num']
atck_seq_time, atck_seq_num = attack_log['seq']['time'], attack_log['seq']['num']
atck_ack_time, atck_ack_num = attack_log['ack']['time'], attack_log['ack']['num']
plt.plot(norm_seq_time, norm_seq_num, 'b^', label='Regular TCP Data Segments')
plt.plot(norm_ack_time, norm_ack_num, 'bx', label='Regular TCP ACKs')
plt.plot(atck_seq_time, atck_seq_num, 'rs', label='%s Attack Data Segments' % attack_desc)
plt.plot(atck_ack_time, atck_ack_num, 'r+', label='%s Attack ACKs' % attack_desc)
plt.legend(loc='upper left')
x = max(max(norm_seq_time, norm_ack_time),max(atck_seq_time, atck_ack_time))
y = max(max(norm_seq_num, norm_ack_num),max(atck_seq_num, atck_ack_num))
plt.xlim(0, x)
plt.ylim(0,y)
plt.xlabel('Time (s)')
plt.ylabel('Sequence Number (Bytes)')
if save_imgs:
# Save images to figure/
if not os.path.exists(output_dir):
os.makedirs(output_dir)
plt.savefig(output_dir + "/" + attack_name)
else:
plt.show()
normal_f.close()
attack_f.close()
if __name__ == "__main__":
main()
after running this i get this error
Traceback (most recent call last):
File "plot.py", line 85, in <module>
main()
File "plot.py", line 66, in main
plt.xlim(0, a)
File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 1427, in xlim
ret = ax.set_xlim(*args, **kwargs)
File "/usr/lib/python3/dist-packages/matplotlib/axes/_base.py", line 3267, in set_xlim
reverse = left > right
TypeError: '>' not supported between instances of 'int' and 'list'
Done! Please check ./plots for all generated plots.
how can i solve this problem? or better yet if there is another way of running this project? i installed matplotlib via pip3 install matplotlib command (same with scapy) and my main python version is python2 right now but i run the project with python3, could the issue be about this? what am i missing? or is it about mininet itself?
The problem is in this line
x = max(max(norm_seq_time, norm_ack_time),max(atck_seq_time, atck_ack_time))
IIUC, you wanna assign to x the maximum value among all those four lists. However, when you pass two lists to the max function, such as max(norm_seq_time, norm_ack_time), it will return the list it considers the greater one, and not the highest value considering both lists.
Instead, you can do something like:
x = max(norm_seq_time + norm_ack_time + atck_seq_time + atck_ack_time)
This will concatenate the four lists into a single one. Then, the function will return the highest value among all of them. You might wanna do that to the calculation of y as well.
If this is not what you wanted, or if you have any further issues, please let us know.
with the help of a friend we solved this problem by changing a part in code into this:
max_norm_seq_time = max(norm_seq_time) if len(norm_seq_time) > 0 else 0
max_norm_ack_time = max(norm_ack_time) if len(norm_ack_time) > 0 else 0
max_atck_seq_time = max(atck_seq_time) if len(atck_seq_time) > 0 else 0
max_atck_ack_time = max(atck_ack_time) if len(atck_ack_time) > 0 else 0
x = max((max_norm_seq_time, max_norm_ack_time,\
max_atck_seq_time, max_atck_ack_time))
plt.xlim([0,x])
max_norm_seq_num = max(norm_seq_num) if len(norm_seq_num) > 0 else 0
max_norm_ack_num = max(norm_ack_num) if len(norm_ack_num) > 0 else 0
max_atck_seq_num = max(atck_seq_num) if len(atck_seq_num) > 0 else 0
max_atck_ack_num = max(atck_ack_num) if len(atck_ack_num) > 0 else 0
plt.ylim([0, max((max_norm_seq_num, max_norm_ack_num,\
max_atck_seq_num, max_atck_ack_num))])
```
writing here just in case anyone else needs it.
How to debug "NameError: global name 'X' is not defined" in Python? I am pretty much new in Python. I am using jupyter_notebook with Python 2.7 to execute code. I am facing following error.
My code:
logFile = "NASAlog.txt"
def parseLogs():
parsed_logs=(sc
.textFile(logFile)
.map(parseApacheLogLine)
.cache())
access_logs = (parsed_logs
.filter(lambda s: s[1] == 1)
.map(lambda s: s[0])
.cache())
failed_logs = (parsed_logs
.filter(lambda s: s[1] == 0)
.map(lambda s: s[0]))
failed_logs_count = failed_logs.count()
if failed_logs_count > 0:
print 'Number of invalid logline: %d' % failed_logs.count()
for line in failed_logs.take(20):
print 'Invalid logline: %s' % line
print 'Read %d lines, successfully parsed %d lines, failed to parse %d lines' % (parsed_logs.count(), access_logs.count(), failed_logs.count())
return parsed_logs, access_logs, failed_logs
parsed_logs, access_logs, failed_logs = parseLogs()
ERROR
> NameError Traceback (most recent call last)
> <ipython-input-18-b365aa793252> in <module>()
> 24 return parsed_logs, access_logs, failed_logs
> 25
> ---> 26 parsed_logs, access_logs, failed_logs = parseLogs()
>
> <ipython-input-18-b365aa793252> in parseLogs()
> 2
> 3 def parseLogs():
> ----> 4 parsed_logs=(sc
> 5 .textFile(logFile)
> 6 .map(parseApacheLogLine)
>
> NameError: global name 'sc' is not defined
The problem is that you did never define sc. Therefore python can't find it. (Makes sense, doesn't it?)
Now there are several possible reasons:
- python is case-sensitive. Did you somewhere define SC instead of sc? ... Or Sc instead of sc?
You defined sc in another function (-> you defined it in a function outside parseLogs()). If you only define it there the variable will be local and just be available to the code inside the function. Add the line global sc to the first line of your function to make it accessible everywhere in you whole code.
You simply did not define sc.
A friend and myself are working on creating a basic proof-of-concept decompiler that takes a string of hex values and returns a more readable version. Our code is listed below
testProgram = "00 00 FF 55 47 00"
# should look like this
# NOP
# NOP
# MOV 55 47
# NOP
pc = 0
output = ""
def byte(int):
return testProgram[3 * int:3 * int + 2]
def interpret():
currentByte = byte(pc)
if currentByte == "00":
pc += 1
return "NOP"
if currentByte == "FF":
returner = "MOV " + byte(pc + 1) + " " + byte(pc + 2)
pc += 3
return returner
while(byte(pc) != ""):
output += interpret() + "\n"
print(output)
however, running the code tells us this
Traceback (most recent call last):
File "BasicTest.py", line 62, in <module>
output += interpret() + "\n"
File "BasicTest.py", line 50, in interpret
currentByte = byte(pc)
UnboundLocalError: local variable 'pc' referenced before assignment
Because pc is a global variable, shouldn't it be usable from anywhere? Any and all help is appreciate - if you spot other errors, feel free to leave a comment pointing them out!
Been seeing this a lot lately. When you do
if currentByte == "00":
pc += 1 # <----------
return "NOP"
You're assigning to the local variable pc, but pc isn't declared yet in the local scope. If you want to modify the global pc you need to declare that explicitly at the top of the function
global pc
I'm trying to figure out a way to create unified diffs with line numbers only showing N lines of context. I have been unable to do this with difflib.unified_diff. I need to show changes in both files.
The closest I can come is using diff on the command line like so:
/usr/bin/diff
--unchanged-line-format=' %.2dn %L'
--old-line-format="-%.2dn %L"
--new-line-format="+%.2dn %L"
file1.py
file2.py
BUT I only want to show N lines of context, and /usr/bin/diff doesn't seem to support context with a custom line format (eg. -U2 is not compatible with --line-format "conflicting output style options").
Below is an example of what I'd like to accomplish (the same output as the above diff, but only showing 1 line of context surrounding changes):
+01: def renamed_function()
-01: def original_function():
02:
+03: """ Neat stuff here """
04:
21:
+22: # Here's a new comment
23:
85: # Output the value of foo()
+86: print "Foo is %s"%(foo())
-86: print foo()
87:
I was able to figure out something very close to what I wanted to do. It's slower than regular diff, though. Here's the entire code, from my project GitGate.
def unified_diff(to_file_path, from_file_path, context=1):
""" Returns a list of differences between two files based
on some context. This is probably over-complicated. """
pat_diff = re.compile(r'## (.[0-9]+\,[0-9]+) (.[0-9]+,[0-9]+) ##')
from_lines = []
if os.path.exists(from_file_path):
from_fh = open(from_file_path,'r')
from_lines = from_fh.readlines()
from_fh.close()
to_lines = []
if os.path.exists(to_file_path):
to_fh = open(to_file_path,'r')
to_lines = to_fh.readlines()
to_fh.close()
diff_lines = []
lines = difflib.unified_diff(to_lines, from_lines, n=context)
for line in lines:
if line.startswith('--') or line.startswith('++'):
continue
m = pat_diff.match(line)
if m:
left = m.group(1)
right = m.group(2)
lstart = left.split(',')[0][1:]
rstart = right.split(',')[0][1:]
diff_lines.append("## %s %s ##\n"%(left, right))
to_lnum = int(lstart)
from_lnum = int(rstart)
continue
code = line[0]
lnum = from_lnum
if code == '-':
lnum = to_lnum
diff_lines.append("%s%.4d: %s"%(code, lnum, line[1:]))
if code == '-':
to_lnum += 1
elif code == '+':
from_lnum += 1
else:
to_lnum += 1
from_lnum += 1
return diff_lines