Re: How to create a list and append to list inside a mqtt and GUI program?

2019-09-01 Thread Richard Damon
On 9/1/19 4:02 PM, Spencer Du wrote:
> Hi 
>
> I have code for GUI and MQTT. In GUI.py I have "def loadGUI" which loads up a 
> GUI file if the file exists in current directory. I want to add the file name 
> to a list when a file is imported and for each subsequent file that is 
> imported I want the file name to be imported to the same list and print the 
> list or create a new list but with the imported file named added to list 
> which has the existing file names that have already been imported. I was 
> wondering how I do this. By the way run GUI.py to test this and test1.py and 
> test2.py are the files which can be used to import GUI . 

To make the list persistent you need to create it as a module global or
as a class member of a persistent object (like your MainWindow)

-- 
Richard Damon

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to create a list and append to list inside a mqtt and GUI program?

2019-09-01 Thread MRAB

On 2019-09-01 21:02, Spencer Du wrote:

Hi

I have code for GUI and MQTT. In GUI.py I have "def loadGUI" which loads up a 
GUI file if the file exists in current directory. I want to add the file name to a list 
when a file is imported and for each subsequent file that is imported I want the file 
name to be imported to the same list and print the list or create a new list but with the 
imported file named added to list which has the existing file names that have already 
been imported. I was wondering how I do this. By the way run GUI.py to test this and 
test1.py and test2.py are the files which can be used to import GUI .

GUI.py

[snip]
import json



[snip]

 def loadGUI(self):
 print("Searching file", self.fileName_UI)
 try:
 module = __import__(self.fileName_UI)
 my_class = getattr(module, "SubWindow")

 sub = QMdiSubWindow()

 sub.setWidget(my_class())
 sub.setWindowTitle("New GUI:  " + self.fileName_UI)
 self.mdi.addSubWindow(sub)
 sub.show()

 print("creating new instance " + self.fileName_UI)
 client = device("Device")
 client.run()

 client.loop_start()  # start the loop
 device_message = self.fileName_UI
 time.sleep(2)
 print("Subscribing to topic", 
"microscope/light_sheet_microscope/UI")
 client.subscribe("microscope/light_sheet_microscope/UI")
 print("Publishing message to topic", 
"microscope/light_sheet_microscope/UI")
 client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "device", 
"payload":{"name": self.fileName_UI, "cmd": "adding device"}}, indent=2))
 time.sleep(1)  # wait
 client.loop_stop()  # stop the loop
 print("Device added" + "\n")


This is a local name, local to the method.


 listofdevice = []
 listofdevice.append(self.fileName_UI)
 print(listofdevice)


You could store the list in a file: read the current list from the file 
and then write the modified list to the file. The file won't exist 
initially, so you can treat that as the list being empty.


You're already importing the json module, so you could read/write it as 
JSON.


Also, _don't_ use a "bare" except like here:


 except:


because catches _all_ exceptions. Instead, catch only those you're 
prepared to handle.


[snip]
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to create a list and append to list inside a mqtt and GUI program?

2019-09-01 Thread Sayth Renshaw
On Monday, 2 September 2019 06:02:58 UTC+10, Spencer Du  wrote:
> Hi 
> 
> I have code for GUI and MQTT. In GUI.py I have "def loadGUI" which loads up a 
> GUI file if the file exists in current directory. I want to add the file name 
> to a list when a file is imported and for each subsequent file that is 
> imported I want the file name to be imported to the same list and print the 
> list or create a new list but with the imported file named added to list 
> which has the existing file names that have already been imported. I was 
> wondering how I do this. By the way run GUI.py to test this and test1.py and 
> test2.py are the files which can be used to import GUI . 
> 
> GUI.py 
> 
> import logging 
> from datetime import timedelta 
> import time 
> from thespian.actors import * 
> from transitions import Machine 
> import paho.mqtt.client as mqtt 
> import importlib 
> import os.path 
> import sys 
> from PyQt5.QtWidgets import * 
> from PyQt5.QtCore import * 
> from PyQt5 import QtWidgets, uic 
> from mqtt import * 
> import json 
> 
> class MainWindow(QtWidgets.QMainWindow): 
> def __init__(self,parent = None): 
> QMainWindow.__init__(self) 
> super(MainWindow, self).__init__(parent) 
> self.mdi = QMdiArea() 
> self.setCentralWidget(self.mdi) 
> 
> self.setMinimumSize(QSize(800, 600)) 
> self.setWindowTitle("PyQt button example - 
> pythonprogramminglanguage.com") 
> 
> pybutton = QPushButton('Add device', self) 
> 
> pybutton.clicked.connect(self.importbutton) 
> 
> pybutton.move(100, 400) 
> pybutton.resize(150, 32) 
> 
> self.textbox = QLineEdit(self) 
> self.textbox.move(100,350) 
> self.textbox.resize(100, 32) 
> 
> self.fileName_UI = "" 
> 
> def importbutton(self): 
> self.fileName_UI = self.textbox.text() 
> self.loadGUI() 
> 
> def getGUIFilename(self): 
> return self.fileName_UI 
> 
> def loadGUI(self): 
> print("Searching file", self.fileName_UI)   
> try: 
> module = __import__(self.fileName_UI) 
> my_class = getattr(module, "SubWindow") 
> 
> sub = QMdiSubWindow() 
> 
> sub.setWidget(my_class()) 
> sub.setWindowTitle("New GUI:  " + self.fileName_UI) 
> self.mdi.addSubWindow(sub) 
> sub.show() 
> 
> print("creating new instance " + self.fileName_UI) 
> client = device("Device") 
> client.run() 
> 
> client.loop_start()  # start the loop 
> device_message = self.fileName_UI 
> time.sleep(2) 
> print("Subscribing to topic", 
> "microscope/light_sheet_microscope/UI") 
> client.subscribe("microscope/light_sheet_microscope/UI") 
> print("Publishing message to topic", 
> "microscope/light_sheet_microscope/UI") 
> client.publish("microscope/light_sheet_microscope/UI", 
> json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": 
> "adding device"}}, indent=2)) 
> time.sleep(1)  # wait 
> client.loop_stop()  # stop the loop 
> print("Device added" + "\n") 
> listofdevice = [] 
> listofdevice.append(self.fileName_UI) 
> print(listofdevice) 
> except: 
> print("creating new instance " + self.fileName_UI) 
> client = device("Device") 
> client.run() 
> 
> client.loop_start()  # start the loop 
> device_message = self.fileName_UI 
> time.sleep(2) 
> print("Subscribing to topic", 
> "microscope/light_sheet_microscope/UI") 
> client.subscribe("microscope/light_sheet_microscope/UI") 
> print("Publishing message to topic", 
> "microscope/light_sheet_microscope/UI") 
> client.publish("microscope/light_sheet_microscope/UI", 
> json.dumps({"type": "device", "payload":{"name": self.fileName_UI}}, 
> indent=2)) 
> time.sleep(2)  # wait 
> client.loop_stop()  # stop the loop 
> print(device_message + ".py " + "file doesn't exist") 
> print("Device not added") 
> if __name__ == "__main__": 
> app = QApplication(sys.argv) 
> mainWin = MainWindow() 
> mainWin.show() 
> publishedMessage = mainWin.getGUIFilename() 
> sys.exit(app.exec_()) 
> 
> MQTT.py 
> import logging 
> from datetime import timedelta 
> import time 
> from thespian.actors import * 
> from transitions import Machine 
> import paho.mqtt.client as mqtt 
> import importlib 
> import os.path 
> import sys 
> from PyQt5.QtWidgets import * 
> from PyQt5.QtCore import * 
> from PyQt5 import QtWidgets, uic 
> 
> class device(mqtt.Client): 
> def on_connect(self, mqttc, obj, flags, rc): 
> if rc == 0: 
> print("Connected to broker") 
> else: 
> 

How to create a list and append to list inside a mqtt and GUI program?

2019-09-01 Thread Spencer Du
Hi 

I have code for GUI and MQTT. In GUI.py I have "def loadGUI" which loads up a 
GUI file if the file exists in current directory. I want to add the file name 
to a list when a file is imported and for each subsequent file that is imported 
I want the file name to be imported to the same list and print the list or 
create a new list but with the imported file named added to list which has the 
existing file names that have already been imported. I was wondering how I do 
this. By the way run GUI.py to test this and test1.py and test2.py are the 
files which can be used to import GUI . 

GUI.py 

import logging 
from datetime import timedelta 
import time 
from thespian.actors import * 
from transitions import Machine 
import paho.mqtt.client as mqtt 
import importlib 
import os.path 
import sys 
from PyQt5.QtWidgets import * 
from PyQt5.QtCore import * 
from PyQt5 import QtWidgets, uic 
from mqtt import * 
import json 

class MainWindow(QtWidgets.QMainWindow): 
def __init__(self,parent = None): 
QMainWindow.__init__(self) 
super(MainWindow, self).__init__(parent) 
self.mdi = QMdiArea() 
self.setCentralWidget(self.mdi) 

self.setMinimumSize(QSize(800, 600)) 
self.setWindowTitle("PyQt button example - 
pythonprogramminglanguage.com") 

pybutton = QPushButton('Add device', self) 

pybutton.clicked.connect(self.importbutton) 

pybutton.move(100, 400) 
pybutton.resize(150, 32) 

self.textbox = QLineEdit(self) 
self.textbox.move(100,350) 
self.textbox.resize(100, 32) 

self.fileName_UI = "" 

def importbutton(self): 
self.fileName_UI = self.textbox.text() 
self.loadGUI() 

def getGUIFilename(self): 
return self.fileName_UI 

def loadGUI(self): 
print("Searching file", self.fileName_UI)   
try: 
module = __import__(self.fileName_UI) 
my_class = getattr(module, "SubWindow") 

sub = QMdiSubWindow() 

sub.setWidget(my_class()) 
sub.setWindowTitle("New GUI:  " + self.fileName_UI) 
self.mdi.addSubWindow(sub) 
sub.show() 

print("creating new instance " + self.fileName_UI) 
client = device("Device") 
client.run() 

client.loop_start()  # start the loop 
device_message = self.fileName_UI 
time.sleep(2) 
print("Subscribing to topic", 
"microscope/light_sheet_microscope/UI") 
client.subscribe("microscope/light_sheet_microscope/UI") 
print("Publishing message to topic", 
"microscope/light_sheet_microscope/UI") 
client.publish("microscope/light_sheet_microscope/UI", 
json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": 
"adding device"}}, indent=2)) 
time.sleep(1)  # wait 
client.loop_stop()  # stop the loop 
print("Device added" + "\n") 
listofdevice = [] 
listofdevice.append(self.fileName_UI) 
print(listofdevice) 
except: 
print("creating new instance " + self.fileName_UI) 
client = device("Device") 
client.run() 

client.loop_start()  # start the loop 
device_message = self.fileName_UI 
time.sleep(2) 
print("Subscribing to topic", 
"microscope/light_sheet_microscope/UI") 
client.subscribe("microscope/light_sheet_microscope/UI") 
print("Publishing message to topic", 
"microscope/light_sheet_microscope/UI") 
client.publish("microscope/light_sheet_microscope/UI", 
json.dumps({"type": "device", "payload":{"name": self.fileName_UI}}, indent=2)) 
time.sleep(2)  # wait 
client.loop_stop()  # stop the loop 
print(device_message + ".py " + "file doesn't exist") 
print("Device not added") 
if __name__ == "__main__": 
app = QApplication(sys.argv) 
mainWin = MainWindow() 
mainWin.show() 
publishedMessage = mainWin.getGUIFilename() 
sys.exit(app.exec_()) 

MQTT.py 
import logging 
from datetime import timedelta 
import time 
from thespian.actors import * 
from transitions import Machine 
import paho.mqtt.client as mqtt 
import importlib 
import os.path 
import sys 
from PyQt5.QtWidgets import * 
from PyQt5.QtCore import * 
from PyQt5 import QtWidgets, uic 

class device(mqtt.Client): 
def on_connect(self, mqttc, obj, flags, rc): 
if rc == 0: 
print("Connected to broker") 
else: 
print("Connection failed") 

# mqttc.subscribe("microscope/light_sheet_microscope/UI") 

def on_message(self, mqttc, userdata, message): 
msg = str(message.payload.decode("utf-8")) 
print("message recieved= " + msg) 
# print("File which you want to import(with .py extension)") 
print("message topic=",