Python pass a global variable when the script reloads - python

So I am trying to pass a global variable when the script reloads with a click of a button. The problem is that when the script loads the global variable is again empty. How can I solve this issue? Will I need a second .py file? Basically I am trying to pass a folder path when the script reloads.
Thank you.
# Global variable
folder_path = ''
class TestDialog(QtWidgets.QDialog):
def __init__(self, folder_path_arg ,parent=maya_main_window()):
super(TestDialog, self).__init__(parent)
self.setWindowTitle('HDRI Browser')
self.setMinimumWidth(430)
self.setMaximumWidth(430)
self.setMinimumHeight(500)
self.setMaximumHeight(500)
self.create_widgets()
self.create_connections()
print( "printing in TestDialog: "+ str(folder_path) )
# (...)
def show_folder_selected_dialog(self):
selected_folder_dialog = QtWidgets.QFileDialog()
sel_dir = selected_folder_dialog.getExistingDirectory(self, 'Select a folder')
if sel_dir:
self.filepath_le.setText(sel_dir)
# SET Global variable folder_path
listOfGlobals = globals()
listOfGlobals['folder_path'] = self.filepath_le.text()+str('/')
def load_folder_selected_dialog(self):
from importlib import reload
import hdri_browser_script_v1
reload(hdri_browser_script_v1)
if __name__ == "hdri_browser_script_v1":
try:
test_dialog.close()
test_dialog.deleteLater()
except:
pass
# passing the variable folder_path as argument
test_dialog = TestDialog(folder_path)
test_dialog.show()

Related

Python class variables not updating while inside while loop. (A thread with another function inside the same class is changing them)

I'm trying to update the value of a variable inside a while loop inside a class.
The value isn't updating and the information I need to transfer isn't reaching.
Can anyone help me with this? Thank you.
Here is my code:
import random, threading
class TestClass:
def __init__(self):
pass
def loop(self):
while True:
print(self.players)
def loop2(self):
while True:
self.players = random.randint(1, 100)
if __name__ == '__main__':
a = TestClass()
t = threading.Thread(target=a.loop2)
t.start()
a.loop()

Python - call multiple external functions in Class instance

I have a script that watches for changes in file and if occurred it should trigger some actions. Those actions come from two functions defined outside Class. In Class I have defined portion of code to look for changes in file. I cannot figure out how to pass two functions in Class arguments. Here is simplified portion of my script:
import time, os
watch_file = 'my_file.txt'
def first_action():
print('First action called')
def second_action():
print('Second action called')
class Watcher():
def __init__(self, watch_file, first_action=None, second_action=None):
self._cached_stamp = 0
self.filename = watch_file
self.first_action = first_action
self.second_action = second_action
# Look for changes in 'my_file.txt'
def look(self):
stamp = os.stat(self.filename).st_mtime
if stamp != self._cached_stamp:
self._cached_stamp = stamp
# File has changed, so do something...
print('File changed')
if self.first_action is not None:
print('Call first action')
self.first_action(self)
if self.second_action is not None:
print('Call second action')
self.second_action(self)
watcher = Watcher(watch_file, first_action(), second_action())
Doing it like above calls first_action() and second_action() but not inside Class. I know it because I dont see printed 'Call first action' or 'Call second action'
I was able to correctly trigger first action with below code:
watch_file = 'my_file.txt'
def first_action():
print('First action called')
def second_action():
print('Second action called')
class Watcher():
def __init__(self, watch_file, first_action=None, *args):
self._cached_stamp = 0
self.filename = watch_file
self.first_action = first_action
self.args = args
# Look for changes in 'my_file.txt'
def look(self):
stamp = os.stat(self.filename).st_mtime
if stamp != self._cached_stamp:
self._cached_stamp = stamp
# File has changed, so do something...
print('File changed')
if self.first_action is not None:
print('Call first action')
self.first_action(*self.args)
watcher = Watcher(watch_file, first_action)
For some reason I need to specify *args even for function which does not take any argument on call. Can someone explain why *args must be used?
You did it right in the init, but not in the call of "Watcher". To pass the function itself, instead of it's return value, you have to remove the braces.
watcher = Watcher(watch_file, first_action, second_action)
And you should be fine.

re-using the variable in a function in a class in python

I will try to explain the problem I am facing with a small piece of code:
class MyHandler(PatternMatchingEventHandler):
patterns = ["*.csv","*.processing", "*.transforming","*.loading"]
def process(self, event):
eventFileName = event.src_path
eventType = event.event_type
if eventType == 'moved':
eventFileName = event.dest_path
fileNameWithPath, fileExtension = os.path.splitext(eventFileName)
if fileExtension == '.processing':
# Here some function is called to do something, and then appends ".loading" to the file name
testVariable = 75.3
if fileExtension == '.loading':
print testVariable
def on_moved(self, event):
self.process(event)
def on_created(self, event):
self.process(event)
if __name__ == '__main__':
observer = Observer()
observer.schedule(MyHandler(), path='.')
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
When I try to do the above I am getting this error: global name 'testVariable' is not defined which kinda makes sense but how do I make the above work? I tried to define "testVariable" globally and initiated to 0 and tried using it the way I showed in above code but it did not work as well.
I also tried initiating testVariable as testVariable=0 inside the class (right after "patterns = ....." line but I got this error: local variable "testVariable" referenced before assignment pointing towards print testVariable. So that didnot work as well.
"(...) how do I make the above work?"
By defining testVariable outside your conditional statements. E.g. here:
def process(self, event):
eventFileName = event.src_path
testVariable = 0
...
This will make it available within the process function. If you want it to be available throughout the class, you can define it here:
class MyHandler(PatternMatchingEventHandler):
patterns = ["*.csv","*.processing", "*.transforming","*.loading"]
testVariable = 0
But then you have to access it via the self object within functions like so:
def process(self, event):
...
if fileExtension == '.processing':
# Here some function is called to do something, and then appends ".loading" to the file name
self.testVariable = 75.3
testVariable only exists if you have the extension ".processing". If it's ".loading", the program tries to print a variable that hasn't been made to exist.
If statements do not create a garbage collecting scope in Python, so you don't have to "declare" it outside, so long as somewhere in your if-tree, tesVariable gets a value.
def process(self, event):
def extension():
eventFileName = event.src_path
eventType = event.event_type
if eventType == 'moved':
eventFileName = event.dest_path
return os.path.splitext(eventFileName)[1]
if extension() == '.processing':
...
if extension() == '.loading':
...

PyQt program randomly not responding without a Traceback

I wrote a program to batch change the filenames of many pictures, using PyQt5.
When I run it, it runs through some pictures successfully, and randomly crash without a Python Traceback, showing "has stopped working" in windows 8.1. Even if I use the same testcases, the program will sometimes crash after 20+ pictures, sometimes only 2, sometimes runs to the end.
I don't even know the program crashed in which line. How do I solve this problem?
Here is the program, I minimized the code that still work, and randomly crash as before.
# -*- coding: utf-8 -*-
import sys
import os
import re
from PyQt5 import Qt
if __name__ == '__main__':
application_Variable = Qt.QApplication(sys.argv)
from MainWindow import Ui_MainWindow_Widget
from FileName import Ui_FileName_Widget
class filename_class: # just a class to get path, filename, append
def __init__(self, fullpath):
self.re_path_temp = re.match(r".+/", fullpath)
self.path = self.re_path_temp.group(0)
self.name = fullpath[len(self.path):]
self.append = self.name[len(self.name[:self.name.rfind('.')])-len(self.name)+1:]
class fileNameDefine_Widget(Qt.QWidget): # the rename widget
def __init__(self, filename):
super(fileNameDefine_Widget, self).__init__()
self.fileWidget = Ui_FileName_Widget()
self.fileWidget.setupUi(self)
self.filename = filename
self.file_append = filename_class(self.filename).append # get the append
self.fileWidget.InputFileName_LineEdit.textChanged.connect(self.input_file_name_Change)
def input_file_name_Change(self):
self.export_name = self.fileWidget.InputFileName_LineEdit.text() + "." + self.file_append
self.fileWidget.ExportFileName_LineEdit.setText(self.export_name)
self.fileWidget.InputFileName_LineEdit.setEnabled(True)
class MainWindow_Class(Qt.QWidget): # the main widget
def __init__(self):
super(MainWindow_Class, self).__init__()
self.main = Ui_MainWindow_Widget() # init
self.main.setupUi(self)
self.root_directory = r"D:\TLCTest"
self.file_list = Qt.QFileDialog.getOpenFileNames(caption="Select file", directory=self.root_directory)[0]
self.count = 0 # count which file are being processed
self.show()
self.initiate_change_filename()
def initiate_change_filename(self):
file = self.file_list[self.count]
# show the picture
self.pixmap = Qt.QPixmap()
self.pixmap.load(file)
self.graphicsPixmapItem = Qt.QGraphicsPixmapItem(self.pixmap)
self.graphicsScene = Qt.QGraphicsScene()
self.graphicsScene.addItem(self.graphicsPixmapItem)
self.main.graphicsView.setScene(self.graphicsScene)
# start the rename widget
self.fileName_Widget = fileNameDefine_Widget(file)
self.fileName_Widget.fileWidget.InputFileName_LineEdit.returnPressed.connect(self.submit) # press enter to submit
self.fileName_Widget.show()
def submit(self):
filename = self.fileName_Widget.filename
path = filename_class(filename).path
final_name = self.fileName_Widget.fileWidget.ExportFileName_LineEdit.text()
os.rename(filename, path + final_name)
self.count += 1
if self.count == len(self.file_list):
exit()
else:
self.fileName_Widget.close()
self.initiate_change_filename()
if __name__ == '__main__':
my_Qt_Program = MainWindow_Class()
my_Qt_Program.show()
sys.exit(application_Variable.exec_())
As ekhumoro's comment, this problem was caused by repeatedly creating a fileNameDefine_Widget object like:
def initiate_change_filename(self):
self.fileName_Widget = fileNameDefine_Widget(file)
Instead, one should create this object once, and init it every time required, like:
class TLC_processor_Class(Qt.QWidget):
def init(self):
# Blabla
self.fileName_Widget = fileNameDefine_Widget(file)
def initiate_change_filename(self):
self.fileName_Widget.__init__(file)

How to pass an argument to my Python watchdog.events.PatternMatchingEventHandler

I'm quite new to Python and hope the answer to this is obvious to most of you.
I'm creating a class in Python that represents a ScanFolder.
In the __init__ of that class, I start a watchdog.observers
This observer will fire a watchdog.events.PatternMatchingEventHandler whenever a file is changed under the watched directory.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import time
from watchdog.observers import Observer
import watchdog.events
path = sys.argv[1] if len(sys.argv) > 1 else '.'
class MyEventHandler(watchdog.events.PatternMatchingEventHandler):
def on_any_event(self, event):
print(event.src_path, event.event_type)
class ScanFolder:
'Class defining a scan folder'
def __init__(self, path):
self.path = path
self.documents = dict() # key = document label value = Document reference
self.event_handler = MyEventHandler(patterns=["*.jpg", "*.jpeg", "*.png", "*.bmp", "*.pdf"],
ignore_patterns=[],
ignore_directories=True)
self.observer = Observer()
self.observer.schedule(self.event_handler, self.path, recursive=False)
self.observer.start()
def stop(self):
self.observer.stop()
self.observer.join()
scan_folder = ScanFolder(path)
try:
while True:
time.sleep(1)
"""Here, I'll act on my scan_folder object that lists the discovered files"""
except KeyboardInterrupt:
log.warning("Ouch !!! Keyboard interrupt received.")
scan_folder.stop()
My problem is the following:
How can I have my scan_folder object modified by my scan_folder.event_handler() ?
Actually, I would like to populate the scan_folder.documents dictionary wherever a file is detected in the scan folder.
Thank you very much and sorry for my ignorance.
There are a lot of way to do it: but the simplest way is set a bound method of ScanFolder as on_any_event callback function of watchdog.events.PatternMatchingEventHandler. So your code become:
class ScanFolder:
'Class defining a scan folder'
def __init__(self, path):
self.path = path
self.documents = dict() # key = document label value = Document reference
self.event_handler = watchdog.events.PatternMatchingEventHandler(patterns=["*.jpg", "*.jpeg", "*.png", "*.bmp", "*.pdf"],
ignore_patterns=[],
ignore_directories=True)
self.event_handler.on_any_event = self.on_any_event
self.observer = Observer()
self.observer.schedule(self.event_handler, self.path, recursive=False)
self.observer.start()
def on_any_event(self, event):
print(event.src_path, event.event_type)
print("Complete ScanFolder() access")
def stop(self):
self.observer.stop()
self.observer.join()
Other way could be derive ScanFolder from watchdog.events.PatternMatchingEventHandler .... But injecting function is one of the power of python

Categories