473,803 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using globals with classes

Hi

I am relatively new to Python.

I am using Qt Designer to create a UI for a measurement application that I
use.

Everything seems to be clear but the use of globals (defined in the module
that is generated using pyuic, that contains the form class).

I am using qwtplot to display a running plot :

void Form3::runningp lot(n,plottitle ,xname,x,y1name ,y1,y2name,y2)
{
if n==1 :

plotkey1=self.r unningqwtPlot.i nsertCurve(y1na me,self.running qwtPlot.xBottom ,self.runningqw tPlot.yLeft)

plotkey2=self.r unningqwtPlot.i nsertCurve(y2na me,self.running qwtPlot.xBottom ,self.runningqw tPlot.yRight)
self.runningqwt Plot.setTitle(p lottitle)
self.runningqwt Plot.setXGrid(T rue)
self.runningqwt Plot.setAxisAut oScale(self.run ningqwtPlot.yLe ft)
self.runningqwt Plot.setAxisAut oScale(self.run ningqwtPlot.yRi ght)
self.runningqwt Plot.setAxisAut oScale(self.run ningqwtPlot.xBo ttom)
self.runningqwt Plot.setAxisTit le(self.running qwtPlot.yLeft,y 1name)
self.runningqwt Plot.setAxisTit le(self.running qwtPlot.yRight, y2name)
self.runningqwt Plot.setAxisTit le(self.running qwtPlot.xBottom ,xname)
self.runningqwt Plot.setCurveDa ta(plotkey1,x,y 1,n)
self.runningqwt Plot.setCurveDa ta(plotkey2,x,y 2,n)
self.runningqwt Plot.replot()
else :
self.runningqwt Plot.setCurveDa ta(plotkey1,x,y 1,n)
self.runningqwt Plot.setCurveDa ta(plotkey2,x,y 2,n)
self.runningqwt Plot.replot()
}

where the above routine is called repeatedly to display real time data.
Now, plotkey1 and plotkey2 need to remain unchanged between successive
invocations of setCurveData() and replot() above. I have defined these as
globals (in the Form settings comment field with Python: as usual).
However, when I run it, I get an error indicating that plotkey1 and
plotkey2 are unknown outside.

I also have a global variable named "globaldebu g" that when set to True,
shows some diagnostic information as different slots are called. That too
fails with the same error :

NameError: global name 'globaldebug' is not defined

Is there a way to make a Python function "remember" the values of certain
variables ? Or use fortran 95 like use module, only : varname, type of
within a def ?
Aug 12 '05 #1
6 1810
Is there a way to make a Python function "remember" the values of certain
variables ? Or use fortran 95 like use module, only : varname, type of
within a def ?


I'm not sure what you are trying to do here - but it seems to me that
you are not properly designing your application. You really shouldn't
use the Designers code-insertion features. The reason is simple: you
the have two tools to write code in instead of one (your editor/IDE).
And you don't make plotkey* global - use instance variables.

So I'm going to describe how I dow work with PyQt:

- I create a Widget in the designer
- compile it using pyuic
- _extend_ it
- write my code in the extended version

So I end up with something like this (I use modules to separate
classes):

ui/plot.ui
ui/plot.py
views/plot.py

where views/plot.py looks like this:

class Plot(ui.plot.Pl ot):
def __init__(self, *args):
ui.plot.Plot.__ init__(self, *args)
self.plotkey1 = <whatever>

def update(self):
# access self.plotkey here

HTH,

DIez

Aug 12 '05 #2
Is there a way to make a Python function "remember" the values of certain
variables ? Or use fortran 95 like use module, only : varname, type of
within a def ?


I'm not sure what you are trying to do here - but it seems to me that
you are not properly designing your application. You really shouldn't
use the Designers code-insertion features. The reason is simple: you
the have two tools to write code in instead of one (your editor/IDE).
And you don't make plotkey* global - use instance variables.

So I'm going to describe how I dow work with PyQt:

- I create a Widget in the designer
- compile it using pyuic
- _extend_ it
- write my code in the extended version

So I end up with something like this (I use modules to separate
classes):

ui/plot.ui
ui/plot.py
views/plot.py

where views/plot.py looks like this:

class Plot(ui.plot.Pl ot):
def __init__(self, *args):
ui.plot.Plot.__ init__(self, *args)
self.plotkey1 = <whatever>

def update(self):
# access self.plotkey here

HTH,

DIez

Aug 12 '05 #3
Madhusudan Singh wrote:
.... I am using qwtplot to display a running plot :

void Form3::runningp lot(n,plottitle ,xname,x,y1name ,y1,y2name,y2)
{ ^^ I presume this is just some untranslated stuff ^^ if n==1 :

plotkey1=self.r unningqwtPlot.i nsertCurve(y1na me,self.running qwtPlot.xBottom ,self.runningqw tPlot.yLeft)

plotkey2=self.r unningqwtPlot.i nsertCurve(y2na me,self.running qwtPlot.xBottom ,self.runningqw tPlot.yRight)
self.runningqwt Plot.setTitle(p lottitle)
self.runningqwt Plot.setXGrid(T rue)
self.runningqwt Plot.setAxisAut oScale(self.run ningqwtPlot.yLe ft)
self.runningqwt Plot.setAxisAut oScale(self.run ningqwtPlot.yRi ght)
self.runningqwt Plot.setAxisAut oScale(self.run ningqwtPlot.xBo ttom)
self.runningqwt Plot.setAxisTit le(self.running qwtPlot.yLeft,y 1name)
self.runningqwt Plot.setAxisTit le(self.running qwtPlot.yRight, y2name)
self.runningqwt Plot.setAxisTit le(self.running qwtPlot.xBottom ,xname)
self.runningqwt Plot.setCurveDa ta(plotkey1,x,y 1,n)
self.runningqwt Plot.setCurveDa ta(plotkey2,x,y 2,n)
self.runningqwt Plot.replot()
else :
self.runningqwt Plot.setCurveDa ta(plotkey1,x,y 1,n)
self.runningqwt Plot.setCurveDa ta(plotkey2,x,y 2,n)
self.runningqwt Plot.replot()
}
The way I'd normally accomplish this is to separate the setup and use
by defining a class:

class CurvePlot(objec t):
def __init__(self, plot, plottitle, xname, y1name, y2name,
key1=None, key2=None):
self.plot = plot
if key1 is None:
key1 = plot.insertCurv e(y1name, plot.xBottom, plot.yLeft)
self.key1 = key1
if key2 is None:
key2 = plot.insertCurv e(y2name, plot.xBottom, plot.yRight)
self.key2 = key2
plot.setTitle(p lottitle)
plot.setXGrid(T rue)
plot.setAxisAut oScale(plot.yLe ft)
plot.setAxisAut oScale(plot.yRi ght)
plot.setAxisAut oScale(plot.xBo ttom)
plot.setAxisTit le(plot.yLeft, y1name)
plot.setAxisTit le(plot.yRight, y2name)
plot.setAxisTit le(plot.xBottom , xname)

def curve(self, x, y1, n)
self.plot.setCu rveData(self.ke y1, x, y1, n)
self.plot.setCu rveData(self.ke y2, x, y2, n)
self.plot.replo t()

And then calling it like:

cplot = CurvePlot(self. runningqwtPlot, plottitle,
xname, y1name, y2name)
cplot.curve(n, x, y1, y2)
I also have a global variable named "globaldebu g" that when set to True,
shows some diagnostic information as different slots are called. That too
fails with the same error :

NameError: global name 'globaldebug' is not defined

What you probably don't understand is that "globals" are per-module, not
program-wide. If you write a global from inside a function or method,
you need to declare "global varname" inside the function or method in
which you do the writing. Simply using (reading) a global in a module
does not require the "global" declaration.

--Scott David Daniels
Sc***********@A cm.Org
Aug 12 '05 #4
Many thanks for an excellent solution to the problem and clearing up my mind
about globals.

In some sense, Python globals seem to be a little like the COMMON statement
in the old Fortran 77 standard.
Aug 12 '05 #5
Scott David Daniels wrote:
Madhusudan Singh wrote:
.... I am using qwtplot to display a running plot :

The way I'd normally accomplish this is to separate the setup and use
by defining a class:


How would one enable dynamic autoscaling of the axes ?

I am using setAxisAutoScal e, and when the application ends, I do get
correctly autoscaled axes, but not while the measurement is in progress.
Aug 13 '05 #6
On Fri, 12 Aug 2005 17:13:21 -0400, Madhusudan Singh
<sp************ **@spam.invalid > declaimed the following in
comp.lang.pytho n:

In some sense, Python globals seem to be a little like the COMMON statement
in the old Fortran 77 standard.
Not really. Fortran COMMON block were named pages of memory. The
interpretation of that memory was dependent upon the variables declared
to be /in/ the COMMON block. Prior to the capability to in "include"
files, it was quite easy to make a mistake and misalign references to
COMMON.

program main

double precision a_real
integer int_array(5)
float real_2
common /some_common/ a_real, real_2, int_array

-=-=-=-=-=-

subroutine callme

double precision my_real
double precision really_2
integer my_ints(5)
common /some_common/ my_real, really_2, my_ints

-=-=-=-=-=-

a_real and my_real are okay -- same double precision
real_2 and really_2 differ in size; so really_2 is accessing half
its value from what is int_array(1).
int_array array and my_ints are both five elements long... but the
mismatch on really_2 means that my_ints (1..4) align with
int_array(2..5)

Python's "global" statement, when used inside a function/method
basically says "modify the variable defined at the module (aka, file)
level"
-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Aug 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3849
by: lawrence | last post by:
I get the impression that most people who are using objects in their PHP projects are mixing them with procedural code. The design, I think, is one where the procedural code is the client code, and the classes are a library of utility code. As such, when I've asked about how to get globals into objects, I've been told that I should pass them in as the parameters to a method. Easy enough if you have some procedural code. This answer I've...
8
2192
by: Ron_Adam | last post by:
Is there a way to hide global names from a function or class? I want to be sure that a function doesn't use any global variables by mistake. So hiding them would force a name error in the case that I omit an initialization step. This might be a good way to quickly catch some hard to find, but easy to fix, errors in large code blocks. Examples: def a(x):
6
1315
by: marek | last post by:
Hello All, we are doing a quite a big project that contains at the lowest level an unmenaged c++ classes. Above it there are managed wrappers and at the top there are ASP.NET pages. Can anyone tell me how to force ASP.NET not to "clear" or reinitialize unmenaged global or static variables that are set on unmanaged level between successive requests?
5
1299
by: Steven W. Orr | last post by:
I have two seperate modules doing factory stuff which each have the similar function2: In the ds101 module, def DS101CLASS(mname,data): cname = mname+'DS101' msg_class = globals() msg = msg_class(data) return msg
14
2977
by: raylopez99 | last post by:
KeyDown won't work KeyPress fails KeyDown not seen inspired by a poster here:http://tinyurl.com/62d97l I found some interesting stuff, which I reproduce below for newbies like me. The main reason you would want to do this is for example to trigger something from an OnPaint event without resorting to boolean switches-- say if a user presses the "M" key while the program is Painting, the user gets the PaintHandler to do something else. ...
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.