Kivy: 'name 'getValue' is not defined' - python
So I made a little game that prints everything to the console which consists of some functions, a couple of lists and two extra text files with stats. The standalone game works fine, however, when I stick all the functions in a class and then call the class (so it works with the layout I made using Kivy), the error "name "getValue" is not defined" comes up.
I am confused because putting the functions into a class were the only change I have made. What did I miss?
I know that this would be very vague and unclear without the full code so here it is:
original working file
import time
import random
import os
import operator
import tkinter
import sys
homeIp = "1.1.1.1"
ipNum = homeIp
workingDir = "home/"
found = []
ports = {"ssh":[True,22],"sql":[True,1433],"http":[True,80],"ftp":[True,21]}
admin = True
def load(text,delay):
print(text, end="")
for x in range(0,delay-1):
time.sleep(0.5)
print(".", end="")
print(".")
def createIpList():
global found
for x in range(0,random.randint(1,3)):
found.append(str(random.randint(0,255))+"."+str(random.randint(0,255))+"."+str(random.randint(0,255))+"."+str(random.randint(0,255)))
def connect(ip):
global ipNum
load("trying to connect",8)
if ip in found:
print("connected to "+ip)
ipNum = ip
setPorts()
createIpList()
admin = False
else:
print("ip not found")
def scan():
load("scanning for devices",6)
for x in found:
print(x)
def setPorts():
ports["ssh"][0] = [random.choice([True,False])]
ports["sql"][0] = [random.choice([True,False])]
ports["http"][0] = [random.choice([True,False])]
ports["ftp"][0] = [random.choice([True,False])]
def probe():
print("SSH\t"+str(ports["ssh"][1])+"\t"+str(ports["ssh"][0]).strip("[").strip("]")+"\n"+
"SQL\t"+str(ports["sql"][1])+"\t"+str(ports["sql"][0]).strip("[").strip("]")+"\n"+
"HTTP\t"+str(ports["http"][1])+"\t"+str(ports["http"][0]).strip("[").strip("]")+"\n"+
"FTP\t"+str(ports["ftp"][1])+"\t"+str(ports["ftp"][0]).strip("[").strip("]")+"\n"+
"ALL PORTS MUST BE FALSE TO PORTHACK")
def shutDown():
load("Shutting down ",4)
quit()
def clearScreen():
os.system('cls')
def giveHelp():
print("COMMANDS :")
for key in commands0:
print(key)
for key in commands1:
print(key+" [ ]")
def openPort(portName,portNum):
if portNum == ports[portName][1]:
load("opening port "+str(portNum),10)
ports[portName][0] = False
print("port "+str(portNum)+" opened")
else:
print("Wrong port number")
def portHack():
shut = False
for port in ports:
if str(ports[port][0])== "[True]":
shut = True
if shut == True:
print("Can not operate port hack - all ports must be open")
else:
admin = True
print("You are now system admin")
def inbox():
file = open("emails.txt","r")
for line in file:
if line.strip()=="###EMAIL"+str(int(getValue("email")))+"###":
ended = False
for line in file:
if line.strip()=="###EMAIL"+str(int(getValue("email"))+1)+"###":
ended = True
if ended == False:
print(line)
file.close()
def editValue(var,val):
originalLines = []
file = open("saveData.txt","r")
for line in file:
if len(line)>1:
originalLines.append(line)
file.close()
for x in range(0,len(originalLines)):
if originalLines[x].split("=")[0].strip()==var:
newVal = (float(originalLines[x].split("=")[1].strip())+val)
originalLines[x] = var+"="+str(newVal)
file = open("saveData.txt","w")
for item in originalLines:
file.write(item+"\n")
file.close
def reply():
load("SENDING",3)
def getValue(var):
file = open("saveData.txt","r")
for line in file:
if line.split("=")[0].strip() == var:
return float(line.split("=")[1].strip())
file.close()
def sysSpecs():
print("CPU\t\t"+str(getValue("cpu"))+"GHz\n"+
"RAM\t\t"+str(getValue("ram"))+"GB\n"+
"PSU\t\t"+str(getValue("psu"))+"W\n"+
"STORAGE\t\t"+str(getValue("storage"))+"GB")
if getValue("gpu") == 0:
print("GPU\t\tNONE")
else:
print("GPU\t\t"+str(getValue("gpu"))+"GHz")
def market():
category = input("WELCOME TO AMAZING\n\nCategories\n->CPU\n->RAM\n->storage\n->PSU\n->GPU\n\n[Press ENTER to leave]\n")
measurement={"CPU":"GHz","RAM":"GB","storage":"GB","PSU":"W","GPU":"GHz"}
sortedParts = (sorted(parts[category].items(), key=operator.itemgetter(1)))
print("ITEM\t\t\t|"+measurement[category]+"\t|PRICE\n==================================")
for item in sortedParts:
if len(item[0])>=8:
print(item[0]+"\t\t|"+str(item[1][0])+"\t|"+str(item[1][1]))
else:
print(item[0]+"\t\t\t|"+str(item[1][0])+"\t|"+str(item[1][1]))
def buy(item):
exist = False
itemCat = ""
for category in parts:
for part in parts[category]:
if part == item:
exist = True
itemCat = category
if exist == False:
print("ITEM NOT IN STOCK")
else:
if parts[itemCat][item][1] <= getValue("money"):
print("PURCHASED")
editValue(itemCat.lower(),float(parts[itemCat][item][0])-getValue(itemCat.lower()))
else:
print("INSUFFICIENT BALANCE")
def balance():
print(getValue("money"))
def eventCheck(command):
if int(getValue("email")) == 0:
inbox()
editValue("email",1)
inbox()
if command[0] == "reply":
if int(getValue("email")) == 1:
editValue("email",1)
inbox()
print("$100 recieved")
editValue("money",100)
elif int(getValue("email")) == 3:
if ipNum != homeIp:
if admin == True:
print("CONGRATS")
if int(getValue("email")) == 2:
if getValue("cpu")>=1.5:
editValue("email",1)
def main():
command = input(ipNum+":"+workingDir+">").split(' ')
try:
if len(command) == 1:
commands0[command[0]]()
elif len(command) == 2:
if command[0] == "sshCrack":
commands1[command[0]]("ssh",int(command[1]))
elif command[0] == "sqlInjection":
commands1[command[0]]("sql",int(command[1]))
elif command[0] == "webServerWorm":
commands1[command[0]]("http",int(command[1]))
elif command[0] == "ftpSpoof":
commands1[command[0]]("ftp",int(command[1]))
else:
commands1[command[0]](command[1])
except:
print("",end="")
eventCheck(command)
parts = {"CPU":{"LEG_1":[1.5,100],"LEG_2":[2,200],"AMP_Mk3":[2.5,300],"Inzel_Pontium":[3,500],"Inzel_Eye7":[3.5,700],"AMP_String_Cutter":[4,999]},
"RAM":{"InsaneX_2":[2,40],"Revenger_4":[4,60],"InsaneX_8":[8,100],"Korsair_16":[16,160],"Z.SKILL_FORK":[32,400]},
"storage":{"Sumsing_10":[10,20],"Queenston_50":[50,50],"EastDigital_100":[100,75],"SinDisk_250":[250,120],"CrazyX_500":[500,200],"Sumsing_TB":[1000,499]},
"PSU":{"Korsair_Feather":[200,50],"Korsair_Beefy":[500,130],"Korsair_Ultra":[800,200]},
"GPU":{"ZTX_1050":[2,100],"ZTX_1060":[4,200],"ZTX_1070":[8,400],"ZTX_1080":[16,800]}}
commands0 = {"scan":scan,"probe":probe,"shutdown":shutDown,"cls":clearScreen,"help":giveHelp,"portHack":portHack,"inbox":inbox,
"reply":reply,"market":market,"sysSpecs":sysSpecs,"balance":balance}
commands1 = {"connect":connect,"sshCrack":openPort,"sqlInjection":openPort,"webServerWorm":openPort,"ftpSpoof":openPort,"buy":buy}
setPorts()
createIpList()
eventCheck([""])
while True:
main()
file where the only change is that all functions are put into a class which is then called:
from kivy.app import App
from kivy.clock import Clock
from kivy.core.text import LabelBase
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Color, Ellipse, Rectangle
from kivy.uix.widget import Widget
import time
import random
import os
import operator
import sys
homeIp = "1.1.1.1"
ipNum = homeIp
workingDir = "home/"
found = []
ports = {"ssh":[True,22],"sql":[True,1433],"http":[True,80],"ftp":[True,21]}
admin = True
class MainWindow(BoxLayout):
def load(text,delay):
print(text, end="")
for x in range(0,delay-1):
time.sleep(0.5)
print(".", end="")
print(".")
def createIpList():
global found
for x in range(0,random.randint(1,3)):
found.append(str(random.randint(0,255))+"."+str(random.randint(0,255))+"."+str(random.randint(0,255))+"."+str(random.randint(0,255)))
def connect(ip):
global ipNum
load("trying to connect",8)
if ip in found:
print("connected to "+ip)
ipNum = ip
setPorts()
createIpList()
admin = False
else:
print("ip not found")
def scan():
load("scanning for devices",6)
for x in found:
print(x)
def setPorts():
ports["ssh"][0] = [random.choice([True,False])]
ports["sql"][0] = [random.choice([True,False])]
ports["http"][0] = [random.choice([True,False])]
ports["ftp"][0] = [random.choice([True,False])]
def probe():
print("SSH\t"+str(ports["ssh"][1])+"\t"+str(ports["ssh"][0]).strip("[").strip("]")+"\n"+
"SQL\t"+str(ports["sql"][1])+"\t"+str(ports["sql"][0]).strip("[").strip("]")+"\n"+
"HTTP\t"+str(ports["http"][1])+"\t"+str(ports["http"][0]).strip("[").strip("]")+"\n"+
"FTP\t"+str(ports["ftp"][1])+"\t"+str(ports["ftp"][0]).strip("[").strip("]")+"\n"+
"ALL PORTS MUST BE FALSE TO PORTHACK")
def shutDown():
load("Shutting down ",4)
quit()
def clearScreen():
os.system('cls')
def giveHelp():
print("COMMANDS :")
for key in commands0:
print(key)
for key in commands1:
print(key+" [ ]")
def openPort(portName,portNum):
if portNum == ports[portName][1]:
load("opening port "+str(portNum),10)
ports[portName][0] = False
print("port "+str(portNum)+" opened")
else:
print("Wrong port number")
def portHack():
shut = False
for port in ports:
if str(ports[port][0])== "[True]":
shut = True
if shut == True:
print("Can not operate port hack - all ports must be open")
else:
admin = True
print("You are now system admin")
def inbox():
file = open("emails.txt","r")
for line in file:
if line.strip()=="###EMAIL"+str(int(getValue("email")))+"###":
ended = False
for line in file:
if line.strip()=="###EMAIL"+str(int(getValue("email"))+1)+"###":
ended = True
if ended == False:
print(line)
file.close()
def editValue(var,val):
originalLines = []
file = open("saveData.txt","r")
for line in file:
if len(line)>1:
originalLines.append(line)
file.close()
for x in range(0,len(originalLines)):
if originalLines[x].split("=")[0].strip()==var:
newVal = (float(originalLines[x].split("=")[1].strip())+val)
originalLines[x] = var+"="+str(newVal)
file = open("saveData.txt","w")
for item in originalLines:
file.write(item+"\n")
file.close
def reply():
load("SENDING",3)
def getValue(var):
file = open("saveData.txt","r")
for line in file:
if line.split("=")[0].strip() == var:
return float(line.split("=")[1].strip())
file.close()
def sysSpecs():
print("CPU\t\t"+str(getValue("cpu"))+"GHz\n"+
"RAM\t\t"+str(getValue("ram"))+"GB\n"+
"PSU\t\t"+str(getValue("psu"))+"W\n"+
"STORAGE\t\t"+str(getValue("storage"))+"GB")
if getValue("gpu") == 0:
print("GPU\t\tNONE")
else:
print("GPU\t\t"+str(getValue("gpu"))+"GHz")
def market():
category = input("WELCOME TO AMAZING\n\nCategories\n->CPU\n->RAM\n->storage\n->PSU\n->GPU\n\n[Press ENTER to leave]\n")
measurement={"CPU":"GHz","RAM":"GB","storage":"GB","PSU":"W","GPU":"GHz"}
sortedParts = (sorted(parts[category].items(), key=operator.itemgetter(1)))
print("ITEM\t\t\t|"+measurement[category]+"\t|PRICE\n==================================")
for item in sortedParts:
if len(item[0])>=8:
print(item[0]+"\t\t|"+str(item[1][0])+"\t|"+str(item[1][1]))
else:
print(item[0]+"\t\t\t|"+str(item[1][0])+"\t|"+str(item[1][1]))
def buy(item):
exist = False
itemCat = ""
for category in parts:
for part in parts[category]:
if part == item:
exist = True
itemCat = category
if exist == False:
print("ITEM NOT IN STOCK")
else:
if parts[itemCat][item][1] <= getValue("money"):
print("PURCHASED")
editValue(itemCat.lower(),float(parts[itemCat][item][0])-getValue(itemCat.lower()))
else:
print("INSUFFICIENT BALANCE")
def balance():
print(getValue("money"))
def getValue(var):
file = open("saveData.txt","r")
for line in file:
if line.split("=")[0].strip() == var:
return float(line.split("=")[1].strip())
file.close()
def eventCheck(command):
if int(getValue("email")) == 0:
inbox()
editValue("email",1)
inbox()
if command[0] == "reply":
if int(getValue("email")) == 1:
editValue("email",1)
inbox()
print("$100 recieved")
editValue("money",100)
elif int(getValue("email")) == 3:
if ipNum != homeIp:
if admin == True:
print("CONGRATS")
if int(getValue("email")) == 2:
if getValue("cpu")>=1.5:
editValue("email",1)
def main():
command = input(ipNum+":"+workingDir+">").split(' ')
try:
if len(command) == 1:
commands0[command[0]]()
elif len(command) == 2:
if command[0] == "sshCrack":
commands1[command[0]]("ssh",int(command[1]))
elif command[0] == "sqlInjection":
commands1[command[0]]("sql",int(command[1]))
elif command[0] == "webServerWorm":
commands1[command[0]]("http",int(command[1]))
elif command[0] == "ftpSpoof":
commands1[command[0]]("ftp",int(command[1]))
else:
commands1[command[0]](command[1])
except:
print("",end="")
eventCheck(command)
parts = {"CPU":{"LEG_1":[1.5,100],"LEG_2":[2,200],"AMP_Mk3":[2.5,300],"Inzel_Pontium":[3,500],"Inzel_Eye7":[3.5,700],"AMP_String_Cutter":[4,999]},
"RAM":{"InsaneX_2":[2,40],"Revenger_4":[4,60],"InsaneX_8":[8,100],"Korsair_16":[16,160],"Z.SKILL_FORK":[32,400]},
"storage":{"Sumsing_10":[10,20],"Queenston_50":[50,50],"EastDigital_100":[100,75],"SinDisk_250":[250,120],"CrazyX_500":[500,200],"Sumsing_TB":[1000,499]},
"PSU":{"Korsair_Feather":[200,50],"Korsair_Beefy":[500,130],"Korsair_Ultra":[800,200]},
"GPU":{"ZTX_1050":[2,100],"ZTX_1060":[4,200],"ZTX_1070":[8,400],"ZTX_1080":[16,800]}}
commands0 = {"scan":scan,"probe":probe,"shutdown":shutDown,"cls":clearScreen,"help":giveHelp,"portHack":portHack,"inbox":inbox,
"reply":reply,"market":market,"sysSpecs":sysSpecs,"balance":balance}
commands1 = {"connect":connect,"sshCrack":openPort,"sqlInjection":openPort,"webServerWorm":openPort,"ftpSpoof":openPort,"buy":buy}
setPorts()
createIpList()
eventCheck([""])
class MainApp(App):
def build(self):
Window.clearcolor = (1,1,1,1)
return MainWindow()
if __name__ == '__main__':
MainApp().run()
Just so you know, the whole point of putting it in a class is so I can use the layout I created using the kv design language in a different piece of code. If it is necessary, I can post my .kv file that is opened by the program.
Related
opponet can't apply a move in python mulitplayer Tic-Tac-Toe
so I was basically was trying to make a multiplayer Tic-Tac-Toe game but then I've been running into this following problems This is the host side: Enter row and column numbers to fix spot(row,column): 1,2 - X - - - - - - - This is the guest side: X - - - - - - - - The problems are: It did not pass the turn to the guest side. It displays a false mark on the grid. import socket import threading HOST = '192.168.1.115' PORT = 9090 class TicTacToe: def __init__(self): self.you = 'O' self.opponet = 'X' self.turn = 'X' self.board = [] self.game_over = False self.winner = None self.counter = 0 self.create_board() def create_board(self): for i in range(3): row = [] for j in range(3): row.append('-') self.board.append(row) def show_board(self): for row in self.board: for item in row: print(item, end=" ") print() def is_board_filled(self): for row in self.board: for item in row: if item == '-': return False return True def fix_move(self, row, col, player): if self.game_over: return self.counter +=1 self.board[row-1][col-1] = player self.show_board() if self.is_player_win(player): if self.winner == self.you: print('You win!') exit() if self.winner == self.opponet: print('You lose!') exit() else: if self.counter == 9: print('Its a draw!') exit() def valid_move(self, row, col): if self.board[row][col] == 'O' or self.board[row][col] == 'X': return False return True def swap_player_turn(self): self.turn = self.you if self.turn == self.opponet else self.opponet def handle(self, client): while not self.game_over: if self.turn == self.you: move = input("Enter row and column numbers to fix spot(row,column): ") place = move.split(',') if self.valid_move(int(place[0]), int(place[1])): self.fix_move(int(place[0]), int(place[1]), self.you) self.turn = self.opponet client.send(move.encode('utf-8')) else: print('Invalid Move!') else: data = client.recv(1024) if not data: client.close() break else: place = data.decode('utf-8').split(',') self.fix_move(int(place[0]), int(place[0]), self.opponet) client.close() def is_player_win(self, player): if self.board[0][0] == self.board[0][1] == self.board[0][2] == 'X': return True if self.board[1][0] == self.board[1][1] == self.board[1][2] == 'X' or 'O': return True if self.board[2][0] == self.board[2][1] == self.board[2][2] == 'X' or 'O': return True # etc etc def host_game(self, host, port): server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((host, port)) server.listen(1) client, addr = server.accept() self.you = 'X' self.opponet = 'O' threading.Thread(target=self.handle, args=(client,)).start() server.close() def connect_to_game(self, host, port): client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((host, port)) self.you = 'O' self.opponet = 'X' threading.Thread(target=self.handle, args=(client,)).start() game = TicTacToe() game.host_game(HOST, PORT) the structure of the code is in a way that you take the source code above and copy it and then generate 2 files, one for the host and another for the guest the difference between them is that you you use the host_game function in the host file and the connect_to_game function in the guest file. I've tried everything I could , searching in here , googling and nothing I've also tried to ask ChatGPT but it was worthless
I'll address the immediate two problems: Use your swap_player_turn() method and place it at the bottom of the while loop in handle so it happens on both host and client after the turn is handled. This will pass the turn correctly. After asking for input or receiving data, call a common a function to convert to 0-based row/col data, because sometimes you subtract one from row/col, and sometimes you don't. For example: def parse_move(move): return [int(n)-1 for n in move.split(',')] move = input("Enter row and column numbers to fix spot(row,column): ") row, col = parse_move(move) ... data = client.recv(1024) row, col = parse_move(move) Then use row/col instead of int(place[n]) everywhere. This will fix the false mark. Other issues are the code isn't complete to determine a win or draw, opponet is misspelled, and (while this slow turn-based code will probably never have the issue) TCP is not a message-based protocol, but the code assumes it is. A useful debug feature is to make the game able to print its state. For example, add: def __repr__(self): return f'TicTacToe(you={self.you!r}, op={self.opponent!r}, turn={self.turn!r})' Then print(self) at the top of the while loop.
Keylogging using python on windows
My attempt is as below, it captures well when i'm typing, but sucks if multiple key pressed. It only capture the first pressed key. I removed all unrelated to keylogging. import win32console import win32gui import pythoncom, pyHook import threading import pickle class keylogger(object): def init(self, ifPrintDetail = False): self.keylog_enable = False self.ifPrintDetail = ifPrintDetail self.log = '' def KeyEvent(self, event): if self.keylog_enable: self.log += event.Key if self.ifPrintDetail: print ('MessageName:',event.MessageName ) print ('Message:',event.Message) print ('Time:',event.Time) print ('Window:',event.Window) print ('WindowName:',event.WindowName) print ('Ascii:', event.Ascii, chr(event.Ascii) ) print ('Key:', event.Key) print ('KeyID:', event.KeyID) print ('ScanCode:', event.ScanCode) print ('Extended:', event.Extended) print ('Injected:', event.Injected) print ('Alt', event.Alt) print ('Transition', event.Transition) print ('---') elif event.MessageName == 'key down': print(event.Key, end='') def threadkeylog(self): win = win32console.GetConsoleWindow() win32gui.ShowWindow(win, 0) # create a hook manager object for both key up and down self.hm=pyHook.HookManager() #i want both up and down key event self.hm.KeyDown = self.KeyEvent self.hm.KeyUp = self.KeyEvent # set the hook self.hm.HookKeyboard() #start sending messages, this seems not to stop except WM_QUIT pythoncom.PumpMessages() def go(self): #build and start a thread self.keylog_enable = True self.threadkl = threading.Thread(target = self.threadkeylog) self.threadkl.start() def pause(self): self.keylog_enable = False def save(self): pickle.dump(self.log, open('keylog.txt', 'wb')) You may use it with kl = keylogger() kl.init() kl.go()#build and run a thread kl.pause()#"pause" the thread kl.save()#will save what you have typed to a file in working directory print(pickle.load(open('keylog.txt', "rb")))#take a look at it I do this to collect key log of myself playing car racing game, for training data of my machine learning project. So, say, if I control my racing car with simple "WASD" buttons, I would probably hold "W" and "A" to turn left and accelerate. So, I would like to have both of them captured at the same time, but those keys conflicts, and it capture only one character.
Using ctypes alone you can make quite an effective keylogger. ctypes allows you to check if a key is currently pressed, or toggled. If you set up a dictionary where all the keys are virtual keycodes and all of the values are the keycode's respective string character then you can iterate through the dictionary, and check if the key is pressed. Having another dictionary called TrackedKeys which is empty, you can just set TrackedKeys[key] = The bool returned by the ctypes function which sees if a key is pressed when you are ticking in your "main loop" (A while true in the main python file). then when you update the value of trackedkeys before doing so you can see if the value in trackedkey is different from the value returned by ctypes checking if a key is pressed, and if it is then call a function and pass either "key up" or "key down" to that function. import threading, time from ctypes import * class Thread(): def __init__(self, addressOf, args): self.terminate = False self.Instance = threading.Thread(target=addressOf, args=args) self.Instance.daemon = True self.Instance.start() VKStr = {} VKStr[0x01] = "LEFT_MOUSEE" VKStr[0x02] = "RIGHT_MOUSE" VKStr[0x03] = "MIDDLE_MOUSE" VKStr[0x08] = "BACKSPACE" VKStr[0x09] = "TAB" VKStr[0x0D] = "ENTER" VKStr[0x10] = "SHIFT" VKStr[0x11] = "CTRL" VKStr[0x12] = "ALT" VKStr[0x14] = "CAPSLOCK" VKStr[0x18] = "ESCAPE" VKStr[0x20] = " " VKStr[0x25] = "LEFT_ARROW" VKStr[0x26] = "UP_ARROW" VKStr[0x27] = "RIGHT_ARROW" VKStr[0x28] = "DOWN_ARROW" VKStr[0x2C] = "PRINT_SCREEN" VKStr[0x30] = "0" VKStr[0x31] = "1" VKStr[0x32] = "2" VKStr[0x33] = "3" VKStr[0x34] = "4" VKStr[0x35] = "5" VKStr[0x36] = "6" VKStr[0x37] = "7" VKStr[0x38] = "8" VKStr[0x39] = "9" VKStr[0x41] = "a" VKStr[0x42] = "b" VKStr[0x43] = "c" VKStr[0x44] = "d" VKStr[0x45] = "e" VKStr[0x46] = "f" VKStr[0x47] = "g" VKStr[0x48] = "h" VKStr[0x49] = "i" VKStr[0x4A] = "j" VKStr[0x4B] = "k" VKStr[0x4C] = "l" VKStr[0x4D] = "m" VKStr[0x4E] = "n" VKStr[0x4F] = "o" VKStr[0x50] = "p" VKStr[0x51] = "q" VKStr[0x52] = "r" VKStr[0x53] = "s" VKStr[0x54] = "t" VKStr[0x55] = "u" VKStr[0x56] = "v" VKStr[0x57] = "w" VKStr[0x58] = "x" VKStr[0x59] = "y" VKStr[0x5A] = "z" ShiftEquivs={} ShiftEquivs[0x30] = ")" ShiftEquivs[0x31] = "!" ShiftEquivs[0x32] = "\"" ShiftEquivs[0x33] = "£" ShiftEquivs[0x34] = "$" ShiftEquivs[0x35] = "%" ShiftEquivs[0x36] = "^" ShiftEquivs[0x37] = "&" ShiftEquivs[0x38] = "*" ShiftEquivs[0x39] = "(" ActiveKeys = {} def StringToVK(string): for key, value in VKStr.items(): if value == string: return key def VKToString(VK): return VKStr[VK] def IsKeyPressed(VK_KEYCODE): if type(VK_KEYCODE) == str: try: VK_KEYCODE = StringToVK(VK_KEYCODE) except: raise Exception("Exception caught in sub: 'IsKeyPressed' arg VK_KEYCODE is invalid") return return windll.user32.GetKeyState(c_int(VK_KEYCODE)) & 0x8000 != 0 def IsKeyToggled(VK_KEYCODE): return windll.user32.GetKeyState(c_int(VK_KEYCODE)) & 0x0001 != 0 class KeyTracker: def __init__(self): self.tracking = False self.tracked_string_concat = "" self.file_open = False def StartTracking(self): self.tracking = True def StopTracking(self): self.tracking = False self.CompileData() def KeyDown(self, key): if self.tracking and VKToString(key) != "SHIFT": if IsKeyToggled(StringToVK("CAPSLOCK")): self.tracked_string_concat = self.tracked_string_concat + VKToString(key).upper() elif IsKeyPressed(StringToVK("SHIFT")): shiftEquiv = False try: ShiftEquivs[key] shiftEquiv = True except: pass if shiftEquiv: self.tracked_string_concat = self.tracked_string_concat + ShiftEquivs[key] else: self.tracked_string_concat = self.tracked_string_concat + VKToString(key).upper() else: self.tracked_string_concat = self.tracked_string_concat + VKToString(key) def KeyUp(self, key): if self.tracking and VKToString(key) == "SHIFT": #self.tracked_string_concat = self.tracked_string_concat + VKToString(key) pass def UpdateKeyState(self, key, state): def SetKeyState(key, state): ActiveKeys[key] = state if state == True: self.KeyDown(key) elif state == False: self.KeyUp(key) keyExists = False try: ActiveKeys[key] keyExists = True except: pass if keyExists: if ActiveKeys[key] != state: SetKeyState(key, state) else: SetKeyState(key, state) def CompileData(self): try: file = open("logger_data.txt", "a") file.write("\n") file.write("-"*15) file.write("\n") file.write(self.tracked_string_concat) file.close() except: pass def TrackData(self, time_length): #timeLength in seconds KeyTracker.StartTracking() time.sleep(time_length) KeyTracker.StopTracking() KeyTracker = KeyTracker() t = Thread(KeyTracker.TrackData, [5]) while True: for key, key_name in VKStr.items(): KeyTracker.UpdateKeyState(key, IsKeyPressed(key)) change the argument passed to the thread stored in the variable t to change how long your keylogger records data for, 5 is just a short testing value.
List not copying (empty) [duplicate]
This question already has answers here: Defining lists as global variables in Python (3 answers) Slicing list inside a method (Python 3) (1 answer) Closed 5 years ago. I have the following code: import sys import os.path import ConfigParser import copy import time import colorama from colorama import init from colorama import Fore, Back, Style init() #init variables discoveredelements = [] discoveredgroups = [] combo = [] savefile = ConfigParser.ConfigParser() os.chdir(os.getcwd()) #init other stuff class style: BOLD = '\033[1m' END = '\033[0m' def combos(): #all combos combo.append(('Air', 'Air', 'Wind')) combo.append(('Earth', 'Earth', 'Pressure')) combo.append(('Fire', 'Fire', 'Explosion')) combo.append(('Water', 'Water', 'Sea')) combo.append(('Air', 'Earth', 'Dust')) combo.append(('Air', 'Fire', 'Energy')) combo.append(('Air', 'Water', 'Steam')) combo.append(('Earth', 'Fire', 'Lava')) combo.append(('Earth', 'Water', 'Swamp')) combo.append(('Fire', 'Water', 'Alcohol')) def mainmenu(): print(style.BOLD + "ALCHEMY" + style.END) print(style.BOLD + "Load Game" + style.END) print(style.BOLD + "New Game" + style.END) print(style.BOLD + "Exit" + style.END) print("Type \"load\" or \"new\" or \"exit\" ") mainmenuinput = raw_input() if mainmenuinput == "exit": sys.exit() elif mainmenuinput == "load": if os.path.exists('save.ini'): savefile.read('save.ini') discoveredelements = savefile.get('Elements','discoveredelements') print("Game Loaded") rungame() else: print("Save file not found, check file directory or start a new game.") mainmenu() elif mainmenuinput == "new": if os.path.exists("save.ini"): print("Current save file will be overwritten. Proceed?") print("Y or N") overwriteinput = raw_input() if overwriteinput == "Y": newgame() rungame() elif overwriteinput == "N": mainmenu() else: newgame() rungame() def newgame(): save = open('save.ini','w') #reset data savefile.add_section('Elements') savefile.add_section('Groups') savefile.set('Elements','discoveredelements',"") savefile.set('Groups','discoveredgroups',"") #adds the default elements discoveredelements.append("Air") discoveredelements.append("Earth") discoveredelements.append("Fire") discoveredelements.append("Water") savefile.set('Elements','discoveredelements',discoveredelements) discoveredgroups.append("Air") discoveredgroups.append("Earth") discoveredgroups.append("Fire") discoveredgroups.append("Water") savefile.set('Groups','discoveredgroups',discoveredgroups) savefile.write(save) save.close() print("Game Loaded") def gameloop(): #actual gameplay print("Type two elements (seperately) or \"list\" or \"hint\" or \"save\" or \"exit\"") gameinput = raw_input() if gameinput == "list": displayelements = copy.copy(discoveredelements) print(','.join(map(str, displayelements))) gameloop() elif gameinput == "hint": if (time.time() - timerstart) >= 10: print('hint') timerstart = time.time() gameloop() else: print("Hint is still on cooldown") gameloop() elif gameinput == "save": savefile.set('Elements','discoveredelements',discoveredelements) savefile.set('Groups','discoveredgroups',discoveredgroups) print("Game saved") elif gameinput == "exit": savefile.read('save.ini') savelist = savefile.get('Elements','discoveredelements') if len(savelist) < len(discoveredelements): print("Game not saved! Do you wish to exit without saving?") print("Y or N") overwriteinput = raw_input() if overwriteinput == "Y": mainmenu() else: gameloop() else: elementA = gameinput elementB = raw_input() if (elementA in discoveredelements) and (elementB in discoveredelements): i = 0 created = 0 while True: if (combo[i][0] == elementA and combo[i][1] == elementB) or (combo[i][1] == elementA and combo[i][0] == elementB): print("You created " + combo[i][2]) discoveredelements.append(combo[i][2]) created = 1 break i += 1 if i == len(combo): break if created == 0: print("No elements created") gameloop() else: print("Error, using non-existent or not yet discovered elements") gameloop() def rungame(): #initializing game timerstart = time.time() displayelements = copy.copy(discoveredelements) print(','.join(map(str, displayelements))) gameloop() #game starts here print(Style.RESET_ALL) combos() mainmenu() When I type "load" into the console, nothing is outputted for displayelements. So I tried to see if the list contained anything (and if the copy.copy() worked or not) by doing print(displayelements) and it printed [] Then I checked if discoveredelements contained anything and it did: ['Air', 'Earth', 'Fire', 'Water'] Why isn't the copy.copy() working? EDIT: I declared discoveredelements as global: global discoveredelements discoveredelements = [] The copying still doesn't work, displayelements is still an empty list.
To assign to a global var in function you must declare is as global: discoveredelements = [] def func(): # a new var is created in the scope of this function discoveredelements = [1,2,3,4] func() print (discoveredelements) discoveredelements = [] def func(): # declare as global global discoveredelements # assign to the global var discoveredelements = [1,2,3,4] func() print (discoveredelements)
Saving objects to a list python; List not updating for some reason?
Alright so what I am trying to do is to get objects to save in list form when a user creates a NoteSet. It appends the objects to the list db properly when I input NoteSet('ex','example',True). I made a function called makeNewNoteSet() and it seems to be working correctly but it doesnt append to the db list. I can not figure out why. import sys import datetime import pickle notesets = [] db = [] def save(): global db filename = "notesets.dat" file = open(filename, "wb") if file == None: print("There was an error creating your file") return pickle.dump(db, file) file.close() print("Saved words to ",filename) def load(): global db filename = "notesets.dat" file = open(filename, "rb") db = pickle.load(file) print("There are ",len(db)," Note Sets") file.close() class NoteSet: nextseqNum = len(db)+2 def __init__(self,name,description,hidden): global db self.seqNum = NoteSet.nextseqNum self.name = name self.description = description self.dateCreated = datetime.date.today() self.hidden = hidden self.notes = list() NoteSet.nextseqNum += 1 print(self) notesets.append(self) notelist = [self.seqNum,self.name,self.description,self.dateCreated,self.hidden,self.notes] print(notelist) db.append(notelist) NoteSet.nextseqNum += 1 def __str__(self): printstr = str(self.seqNum),self.name,self.description,str(self.dateCreated) printstr = str(printstr) return printstr class Note: nextseqNum = 0 def __init__(self,text,dateCreated,description,category,priority,hidden): self.text = text self.dateCreated = str self.dateRead = str self.description = str self.category = str self.priority = int self.hidden = bool self.seqNum = Note.nextseqNum Note.nextseqNum += 1 def main(): while True: load() printMainMenu() selection = int(input("? ")) if selection == 1: listNoteSets() elif selection == 2: listAllNoteSets() elif selection == 3: makeNewNoteSet() elif selection == 4: selectNoteSet() # this makes the working note set elif selection == 5: deleteNoteSet() elif selection == 6: sys.exit() else: print("Invalid choice") def printMainMenu(): print("1. List note sets") print("2. List all note sets (including hidden sets)") print("3. Make a new note set") print("4. Select a working note set") print("5. Delete a note set") print("6. Quit") def listNoteSets(): num = 0 for row in db: if db[num][4] == False: print('#',db[num][0],' ',db[num][1],'----',db[num][2]) num += 1 input("[Press return to see main menu]") main() def listAllNoteSets(): num = 0 for row in db: print('#',db[num][0],' ',db[num][1],'----',db[num][2]) num += 1 input("[Press return to see main menu]") main() def makeNewNoteSet(): num = 0 name = input("What would you like to name your note set? --- ") for row in db: if name == db[num][1]: print("That note set has already been created") makeNewNoteSet() description = input("Describe your note set briefly --- ") hidden = input("Would you like this note set to be hidden? --- ") if hidden == 'y' or 'yes': hidden = True else: hidden = False NoteSet(name, description, hidden) print("noteset created you can now access it through the menu") input("[Press enter to return to menu]") main() def selectNoteSet(): num = 0 for row in db: print('#',db[num][0],' ',db[num][1],'----',db[num][2]) num += 1 response = input("Enter the number assosciated with the noteset you would like to access") print("Note set #",response," was selected") main()
After you add a new note in makeNewNoteSet(), you call main() which calls load() which overwrites the in-memory copy of the database you just changed. You probably want to call save() somewhere in there.
'raw_input' does nothing, regardless of the input given
I am trying to make a tool that will encode/decode a string according to values in a dictionary however, I am stuck with the following code: class edt(): e_dic = {} def main(self): c = "hcqnxmytwukgirzoeaspdfvlbj" a = "abcdefghijklmnopqrstuvwxyz" e_dic = {} for i in range(len(c)): e_dic[a[i]] = c[i] e_dic[" "] = " " self.e_dic = e_dic e = edt() user_input = raw_input("1.Encode\n2.Decode\n") if user_input == "1": e.encode() elif user_input == "2": e.decode() else: False def encode(self): print("test") def decode(self): print("test") def run(): run_main = None run_main = edt() run_main.main() I have omitted encode() and decode() as I don't think that they are causing the problem. The problem is that this happens when I run it: >>run() 1.Encode 2.Decode >> (The script pauses here to wait for the return key to be pressed, but does nothing regardless of the input given) >> I am given no error after this completes. Any help would be greatly appreciated
The code looks fine. But shouldn't you be using an object to call the method? like so class edt(): e_dic = {} def encode(self): print "encoded " def decode(self): print "decoded " def main(self): c = "hcqnxmytwukgirzoeaspdfvlbj" a = "abcdefghijklmnopqrstuvwxyz" e_dic = {} for i in range(len(c)): e_dic[a[i]] = c[i] e_dic[" "] = " " self.e_dic = e_dic e = edt() user_input = raw_input("1.Encode\n2.Decode\n") if user_input == "1": e.encode() elif user_input == "2": e.decode() else: False def run(): run_main = None run_main = edt() run_main.main() Output: >>> run() 1.Encode 2.Decode 1 encoded Fiddle here: http://ideone.com/9eET4T
You need to use self in encode and decode. class edt(object): def __init__(self): self.e_dic = {} def main(self): c = "hcqnxmytwukgirzoeaspdfvlbj" a = "abcdefghijklmnopqrstuvwxyz" for i in range(len(c)): self.e_dic[a[i]] = c[i] self.e_dic[" "] = " " user_input = raw_input("1.Encode\n2.Decode\n") if user_input == "1": self.encode() elif user_input == "2": self.decode() else: return False def encode(self): print("test") def decode(self): print("test") def run(): run_main = None run_main = edt() run_main.main() run()