Global Variables Handling across PyQt6 Windows

This demo program shows how to handle global variables across files or PyQt6 windows and execute a function from another file.


The output:




The code:

config.py:

1
2
a = 0
b = "empty"

update.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import config

config.a = 10
config.b = "alphabet"

def update(arg1, arg2):
    config.a = arg1
    config.b = arg2
    
def add(arg1, arg2):
    return arg1 + arg2

main.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from imports import *
from main1 import WindowApp1
import config
import update



class WindowApp(a.QMainWindow):
    def __init__(self, parent=None):
        super().__init__()
        self.setWindowTitle("Post 31")
        self.setFixedSize(320, 245)
        
        lbla =  a.QLabel('Global a: ', self)        
        lbla.setGeometry(25, 55, 100, 30) 
        
        self.txta = a.QLineEdit(str(config.a), self)        
        self.txta.setGeometry(100, 55, 50, 30) 
        
        self.lbla_r =  a.QLabel(str(config.a), self)        
        self.lbla_r.setGeometry(170, 55, 50, 30)
        
        lblb =  a.QLabel('Global b: ', self)        
        lblb.setGeometry(25, 100, 100, 30) 
        
        self.txtb = a.QLineEdit(config.b, self) 
        self.txtb.setGeometry(100, 100, 50, 30) 
        
        self.lblb_r =  a.QLabel(config.b, self)        
        self.lblb_r.setGeometry(170, 100, 50, 30) 
        
        self.pblogin = a.QPushButton('Login', self)
        self.pblogin.setGeometry(50, 195, 65, 25)
        self.pblogin.clicked.connect(self.onClick_pb3)
        
        pbcanc = a.QPushButton('Update', self)
        pbcanc.setGeometry(125, 195, 65, 25)
        pbcanc.clicked.connect(self.onClick_pb4)
        
        pbcalc = a.QPushButton('Function', self)
        pbcalc.setGeometry(200, 195, 65, 25)
        pbcalc.clicked.connect(self.onClick_pb5)
        
        lblf =  a.QLabel('Function: ', self)        
        lblf.setGeometry(25, 140, 75, 30) 
        
        self.txtfa = a.QLineEdit(self) 
        self.txtfa.setGeometry(100, 140, 40, 30)
        
        lblfp =  a.QLabel('+', self)        
        lblfp.setGeometry(150, 140, 10, 30) 
        
        self.txtfb = a.QLineEdit(self) 
        self.txtfb.setGeometry(165, 140, 40, 30)
        
        self.lblf_r =  a.QLabel( self)        
        self.lblf_r.setGeometry(215, 140, 40, 30)
        
    def onClick_pb3(self):
        self.mdiwindow = WindowApp1()
        self.mdiwindow.show()
        self.hide()
    def onClick_pb4(self): 
          
        a = int(self.txta.text())
        b = self.txtb.text()

        update.update(a,b)
        
        self.lbla_r.setText(str(config.a))
        self.lblb_r.setText(config.b)
 
    def onClick_pb5(self):
        self.lblf_r.setText('= ' + str(update.add(int(self.txtfa.text()),int(self.txtfb.text())))) 

if __name__ == "__main__":       
    app = a.QApplication(s.argv)
 
    splash = WindowApp()
    splash.show()
    s.exit(app.exec())        

main1.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from imports import *
import config




class WindowApp1(a.QMainWindow):
    def __init__(self, parent=None):
        super().__init__()
        self.setWindowTitle("Post 31")
        self.setFixedSize(320, 245)
        self.lbla =  a.QLabel('Global a: ', self)        
        self.lbla.setGeometry(25, 55, 100, 30) 
        
        self.lblb =  a.QLabel('Global b: ', self)        
        self.lblb.setGeometry(25, 100, 100, 30)
        
        self.pblogin = a.QPushButton('End', self)
        self.pblogin.setGeometry(100, 165, 65, 25)
        self.pblogin.clicked.connect(self.onClick_pb3)
        pbcanc = a.QPushButton('Print', self)
        pbcanc.setGeometry(180, 165, 65, 25)
        pbcanc.clicked.connect(self.onClick_pb4)
        
        
    def onClick_pb3(self):       
        self.close()
        
    def onClick_pb4(self): 
     
        self.lbla.setText('Global a: ' + str(config.a))
        self.lblb.setText('Global b: ' + config.b)
if __name__ == "__main__":       
    app = a.QApplication(s.argv)
 
    splash = WindowApp1()
    splash.show()
    s.exit(app.exec())          

Python ML Analyzing Air Quality Obtained from SDS011 Sensor via VB6/PHP

I have been so desperate to measure air quality inside my room because I have neighbors that burn their garbage and most often all of those polluted air find its way inside my room, some of them burn their garbage at night sometimes even during wee hours of the morning. Their habits is unpredictable. I tried to look at shopee or lazada but there is no such device that records air quality and at least save it to an sd card. So I was forced to buy the SDS011 sensor, the one with USB adaptor to enable to create a software that will read the sensor's readings, save it to a database and use Machine Learning to discover patterns and predict the effects of the air that gets into my room(optional).

I thought, after connecting the sensor to my pc and downloading the required driver, I could already use the readings right away, but it was not that easy, the output was in the form of binary data, so I will need to find a way to decode it. Being a VB6 developer in the past, my first choice of programming language to use is VB6 but it is not fully equipped with the tools I need, it can read the data coming from the sensor but it could not decode the data so I need to do some research and found out that PHP can decode it easily, and PHP is very flexible, it can easily save the decoded data to MySql database and send back the decoded data to the VB6 application. I could have chosen PHP or VB6 to do the data anaylsis part but both languages are not equipped with the libraries/ocx components to do the task that is why I still need python. I will be using Python for the analysis of decoded data and discover patterns and display the output as html file and this html file will be displayed in the VB6 front end or dashboard application. 

I am almost done with VB6 front end program and the PHP script is already working. I will soon do the data analysis module. You may download the source code of this project at my github repository.

VB6 Front End Program

The front end program shall consist of 2 executable files therefore 2 projects. One will be to display data analysis part and the other one is to display in realtime the decoded data. The latter is optimised for a 5.5" screen. This is to save on energy and for portability because sometimes I need to transfer the location to get more accurate readings. The realtime air quality monitoring is already working but still with a few bugs but generally it is working already.


Disclaimer: Based on my experiments, the php script works on certain php versions and WAMP servers. I know because I tried it on my other pc and it did not worked right away. And also, the programming algorithm and naming conventions are of no commercial quality because it is just a personal project. So if you cared to download, you may need to make a lot of improvements.

The Data Analysis Part

I will adopt the US Air Quality Index in determining air quality. See the chart below, I have sourced from this website. Air Quality Index varies from country to country, and since there is no comprehensive study on this subject here in the Philippines, it is appropriate that the US AQI Chart should be used.




And to determine the overall air quality, I'll just compute the overall air quality by getting the average of the computed pm2.5 and pm10. I am not an authority on this subject, so consider this as my own opinion.

Here is the screenshot of my dashboard:



I guess this end my post here for further updates pls visit my github repository.

Announcement:

A commercial software version of this project may be out soon!  This version will have more features and will support odor and hazardouz gas sensors.

Python Machine Learning Threat Hunting with Wireshark

 Wireshark is an app that captures live network traffic. All activities in a pc that is connected to a network has to send and receive data over the network. Viruses nowadays often originates from websites. but wireshark does not only scans network traffic, it can also check USB traffic. To understand the network traffic, a solid understanding of the OSI model is a must.

As a beginning, malwares are often get downloaded using the HTTP protocol, So to analyze the packets inside it, it should be downloaded from wireshark and upload this file at Virustotal.com for further analysis.

This project will be a python program to gather network packets and save it to a pcap file and submit this file to virustotal.com using their api for virus detection, this will be our raw data to discover patterns using keras/tensorflow.

Capturing Network Packets

I used TSHARK(an application that gets installed when Wireshark is installed) to capture network packets from the command line, and for example, I need to capture packets in 10 seconds over the wifi network and save the captured packets to file, I would use the following python code:

import os
os.system('cmd /c "tshark -i 3 -w packet_log.pcap -a duration:10"')

 

....(to be continued)

A simple face Detection Program with Python and Opencv

A face detection program will enable a user to have another eye. Imagine how a machine will be able to see face and tells you that it saw a face by either alerting you or saving the face in the form of an image for you to view later among other things.

A lot of people may ask how does the program identify a face among billions of possible objects that may be present in that image. The answer is using the image as unstructured data and programmers use machine learning algorithms. Of course initially, the programmer identify the basic characteristics of a face, like answering the question "what are the basic features of a face?", "what is it composed of?", and the list goes on, and on, and on until they introduce an image with a face, and the program checks it and using statistical methods from an initial data, like eyes has to be always 2 and it records the distance between the 2 eyes, distance between the eye and the nose, and the mouth, and so on. The program also identifies certain data points on the face and record the distance between data points.

 

 That is just for a start, the machine learning algorithm needs a lot of images to train itself. These algorithms are thoroughly discussed in Data Science courses so I wont be discussing the full details of the algorithms.

However, not every programmers need to create their face detection program from scratch because the training activities could be saved to a file and can be shared  to other programmers who are interested to use the file for other meaningful applications and ultimately make the world a better place.

Having said that, in python, there is a library called opencv that lets programmers create face detection programs without starting from scratch. And that is the main topic of this post.