473,408 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to rethink functions, and a little background

true911m
I come from a Structured Programming background, and I fall back to old habits sometimes, because they were efficient in their context. But I'm trying to find context here.

In my last environment (Superbase), which was a mess in some ways, I had the flexibility to encapsulate code in OO constructs, or simply to drop oft-used chunks into subroutines, which had NO scope restrictions, and could be loaded on demand.

What I'm trying to do today is to better understand the wx.lib.printout collection in wxPython by wrapping convenient parts like basic setup, final print, etc. in simple -- really simple -- function calls.

The problem with scoped functions is that I have to place all those setup variables (PrintBase, PrintGrid, PrintTable, etc.) in my main app prior to calling the routines. I don't want to do that for two reasons: It stinks of cut-and-paste activity, and it's inelegant.

In this case, it's a couple lines of code, and no biggie, so consider it an academic exercise. To use the full functionality of this module, it's not so trivial, and consumes tons of whitespace in a language that doesn't tend to go that way. The last thing I'd prefer not to do, because all my sloppy VB buddies used to do it, is declare everything global just to get the job done.

So at the core, I would like to have simple, callable functions that initiate wx.lib.printout parameters at the beginning of my main calling app that can be later referenced by the wrap-up printing routines without declaring stuff all over the place.

Can it be done?

Seeya,
Dec 3 '06 #1
4 1345
bartonc
6,596 Expert 4TB
In a word "yes". I wish that I had pencil and paper (a picture being worth a thousand words (and there's a 10000 character limit in these forums)). Here's my example (I haven't played with the printout lib yet):

My mainFrame has textCtrls for data entry and display of data being collected via USB all of which get stored in a SQL database when the user hits the Record button. So my mainFrame requires a USB connection object and a database connection object. Of course, each of these needs lots of their own varibles to setup and maintain these connections plus GUI's for user interaction (password, etc). No problem: Subclass frame (or Dialog in my case) in a separate module, define any "global" values needed by this class (I often set DefaultValueForEntryWidgetXYZ) in the module scope and "getter" fuctions in the class scope. Are you with me so far? In this case, the mainFrame imports the workerFrame and instantiates it in its __init__ function. I use menus to show these frames (which seems pretty natural - especially for "print").
Now, what about the case where another Frame needs access to that instance?
I this example, a third "calibration" frame needs access to data comming from the USB and to the database for storing the calibration data. The mainFrame is the "parent" for all these subframes, so that's where I define things like GetDatabaseServer() so that the subframes can call db = self.parent.GetDatabaseServer(), etc. Now the subframe has access to all the functionality of the dbServer. There is no need to import the module defining the dbServer class in the subframe module. Getting data which is actively changing is a whole other discussion, but I have implemented a version of what Robin Dunn calls Model-View-Controler model in his book wxPython in Action.
I'll post some examples. Keep posting,
Barton
Dec 3 '06 #2
Wow, great.

I'm sure I need to read that a couple more times and play with it to get the full content. From first read, though, I'm seeing an instance of a separate class that, when initialized, takes care of all those annoying startup values in some default sense, while also easily accomodating changes to one or two (or more) customizations as needed. Effectively this instance becomes a "global variable holder" for the processes that will ultimately need those values to function.

This will give me something to chew on for awhile, thanks!
Dec 3 '06 #4
bartonc
6,596 Expert 4TB
Wow, great.

I'm sure I need to read that a couple more times and play with it to get the full content. From first read, though, I'm seeing an instance of a separate class that, when initialized, takes care of all those annoying startup values in some default sense, while also easily accomodating changes to one or two (or more) customizations as needed. Effectively this instance becomes a "global variable holder" for the processes that will ultimately need those values to function.

This will give me something to chew on for awhile, thanks!
That's exactly it.
I think that you have grasped the essentials of oop in python.
You are welcome,
Barton
Dec 3 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
17
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
8
by: Viken Karaguesian | last post by:
Hello all, I'll start with this question: Can I assign an ID *and* a CLASS to a DIV? I am under the impression that you can. I'm having a problem that I can't seem to figure out. Some...
13
by: Tony | last post by:
And that is probably very unclear - so let me explain. Please forgive me if I screw up the terminology (and let me know what the correct term would be): What I want to do is to create a library...
3
by: charchap | last post by:
hello. I'm experiencing problems with my css code (once again) working with Firefox and not working with IE 6 or 7 I have a problem with my head_logo_inner not too sure what's going on . Here is...
2
by: Lumpy | last post by:
Ok all, I have a question regarding the gd library in php. First, let me say that there is really no reason why I am doing this the way that I am. I was a little bored with doing homework and...
1
by: Star258 | last post by:
I have a few paragraphs on my site and an ul that I’ve set up with a css background image. I wanted the image separate from the main background and still have the ability to type on it. Is there a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.