473,804 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using names before they're defined

I have a problem. I'm writing a simulation program with a number of
mechanical components represented as objects. When I create instances
of objects, I need to reference (link) each object to the objects
upstream and downstream of it, i.e.

supply = supply()
compressor = compressor(down stream=combusto r, upstream=supply )
combuster = combuster(downs tream=turbine, upstream=compre ssor)
etc.

the problem with this is that I reference 'combustor' before is it
created. If I swap the 2nd and 3rd lines I get the same problem
(compressor is referenced before creation).
aargh!!! any ideas on getting around this?

Dave

Jul 19 '06 #1
34 1972

daveho...@f2s.c om wrote:
I have a problem. I'm writing a simulation program with a number of
mechanical components represented as objects. When I create instances
of objects, I need to reference (link) each object to the objects
upstream and downstream of it, i.e.

supply = supply()
compressor = compressor(down stream=combusto r, upstream=supply )
combuster = combuster(downs tream=turbine, upstream=compre ssor)
etc.

the problem with this is that I reference 'combustor' before is it
created. If I swap the 2nd and 3rd lines I get the same problem
(compressor is referenced before creation).
aargh!!! any ideas on getting around this?

Dave
At the top of your code you could put:

supply = None
compressor = None
combuster = None
turbine = None

It might be better, though, to arrange your code like:
supply = Supply()
compressor = Compressor()
combuster = Combuster()
turbine = Turbine()
compressor.setS treams(down=com buster, up=supply)
combuster.setSt reams(down=turb ine, up=compressor)

Do the streams reflect each other? That is, if supply.down is
compressor, is compressor.up supply? In that case you probably want to
do something like:

class Component():

upstream = None
downstream = None

def setUpstream(sel f, c):
self.upstream = c
if c.downstream != self:
c.setDownstream (self)

def setDownstream(s elf, c):
self.downstream = c
if c.upstream != self:
c.setUpstream(s elf)

class Supply(Componen t):
pass

etc.

Iain

Jul 19 '06 #2
da*******@f2s.c om wrote:
I have a problem. I'm writing a simulation program with a number of
mechanical components represented as objects. When I create instances
of objects, I need to reference (link) each object to the objects
upstream and downstream of it, i.e.

supply = supply()
compressor = compressor(down stream=combusto r, upstream=supply )
combuster = combuster(downs tream=turbine, upstream=compre ssor)
etc.

the problem with this is that I reference 'combustor' before is it
created. If I swap the 2nd and 3rd lines I get the same problem
(compressor is referenced before creation).
aargh!!! any ideas on getting around this?
Yes. You are building a generic data structure, so you shouldn't really
be trying to store individual objects in variables like that. You need a
data structure that's appropriate to your problem.

For example, you could consider storing them in a list, so you have

components = [supply(), compressor(), combuster()]

Then components[n] is upstream of components[n-1] and downstream of
components[n+1].

In short, your thinking about data representation might need to become a
little more sophisticated.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jul 19 '06 #3
What about something like:

supply = supply()
compressor = compressor(supp ly)
combuster = combuster(compr essor)
compressor.appe nd(combuster)
turbine = turbine(combust er)
combuster.appen d(turbine)
-Larry Bates
da*******@f2s.c om wrote:
I have a problem. I'm writing a simulation program with a number of
mechanical components represented as objects. When I create instances
of objects, I need to reference (link) each object to the objects
upstream and downstream of it, i.e.

supply = supply()
compressor = compressor(down stream=combusto r, upstream=supply )
combuster = combuster(downs tream=turbine, upstream=compre ssor)
etc.

the problem with this is that I reference 'combustor' before is it
created. If I swap the 2nd and 3rd lines I get the same problem
(compressor is referenced before creation).
aargh!!! any ideas on getting around this?

Dave
Jul 19 '06 #4
da*******@f2s.c om wrote:
I have a problem. I'm writing a simulation program with a number of
mechanical components represented as objects. When I create instances
of objects, I need to reference (link) each object to the objects
upstream and downstream of it, i.e.

supply = supply()
compressor = compressor(down stream=combusto r, upstream=supply )
combuster = combuster(downs tream=turbine, upstream=compre ssor)
etc.

the problem with this is that I reference 'combustor' before is it
created. If I swap the 2nd and 3rd lines I get the same problem
(compressor is referenced before creation).
aargh!!! any ideas on getting around this?
the only thing you can do is to either use a name to identify the component

supply = supply('supply' )
compressor = compressor(down stream='combust or', upstream='suppl y')
combuster = combuster(downs tream='turbine' , upstream='compr essor')

or to use some shallow objects that you then fill with information later

supply = supply()
combustor = combustor()
compressor = compressor()
turbine = turbine()
combuster.attac h(downstream=tu rbine' upstream=compre ssor)
Diez
Jul 19 '06 #5
Iain, thanks - very helpful.

Really I'm trying to write a simulation program that goes through a
number of objects that are linked to one another and does calculations
at each object. The calculations might be backwards or fowards (i.e.
starting at the supply or demand ends of the system and then working
through the objects). And also, I might have multiple objects linked to
a single object (upstream or downstream) - e.g. compressor -- multiple
combusters - turbine

I like your idea of using something like a setStreams method to
establish the linking. The streams do reflect each other, although
having many-to-one and vice versa will complicate that. I have not
quite got my head around having multiple links. In C++ I would be
thinking about something like a linked-list but I'm not sure that's the
right approach here.

Dave

Jul 19 '06 #6
da*******@f2s.c om wrote:
I have a problem. I'm writing a simulation program with a number of
mechanical components represented as objects. When I create instances
of objects, I need to reference (link) each object to the objects
upstream and downstream of it, i.e.

supply = supply()
NB : Python convention is to use CamelCase for non-builtin types. FWIW,
the above line will rebind name 'supply', so it won't reference the
supply class anymore...
compressor = compressor(down stream=combusto r, upstream=supply )
combuster = combuster(downs tream=turbine, upstream=compre ssor)
etc.

the problem with this is that I reference 'combustor' before is it
created. If I swap the 2nd and 3rd lines I get the same problem
(compressor is referenced before creation).
aargh!!! any ideas on getting around this?
Solution 1: do a two-stages initialisation
supply = Supply()
compressor = Compressor()
combuster = Combuster()
turbine = Turbine()

compressor.chai n(downstream=co mbustor, upstream=supply )
combuster.chain (downstream=tur bine, upstream=compre ssor)

etc...

Tedious and error-prone... Unless you use a conf file describing the
chain and a function building it, so you're sure the 2-stage init is
correctly done.
Solution 2: 'implicit' chaining

if I understand the problem correctly, your objects are chained, ie:
supply <- compressor <- combuster <- turbine...

If yes, what about:

class Chainable(objec t):
def __init__(self, upstream):
self.upstream = upstream
if upstream is not None:
upstream.downst ream = self

class Supply(Chainabl e):
#

# etc

then:

supply = Supply()
compressor = Compressor(upst ream=supply)
combuster = Combuster(upstr eam=compressor)
turbine = Turbine(upstrea m=combuster)
Or if you don't need to keep direct references to all elements of the chain:
class Chainable(objec t):
def __init__(self, downstream=None ):
self.downstream = downstream
if downstream is not None:
downstream.upst ream = self
supply = Supply(
downstream=Comp ressor(
downstream=Comb uster(
downstream=Turb ine()
)
)
)

or more simply:
supply = Supply(Compress or(Combuster(Tu rbine())))
FWIW, you could then make Chainable class an iterable, allowing:
for item in supply:
# will yield supply, then compressor, then combuster, then turbine
but I don't know if it makes any sens wrt/ your app !-)

Now there can of course be a lot of other solutions...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Jul 19 '06 #7
Your can of course initialize the components first:

compr=Compresso r(...),
comb=Combuster( ...),
sup=Supply(...) ,
tur=Turbine(... ).

Then do:

compr.up, compr.down =sup, comb
comb.up, comb.down =compr, tur

Even if you need to do something during attachment of components it is
more Pythonic to use properties. So you will write a method in your
class name something like _set_up(self,up stream_obj) an _get_up(self).
And then at the end of your class put up=property(_ge t_up, _set_up).
You can still use the compr.up=... format.

Also, you might want to re-think your OO design. It seem that all of
your components do a lot of things in common already. For one they all
are connected to other components like themselves, they also propably
will have method to do some computing, perhaps send or receive stuff
from other components, or they all will implement somekind of an event
model. In that case you could create a generic component class and
sublass the specific implementations from it. For example:
class Component(objec t):
send(...)
recv(...)
up
down
print_data(...)
...

Then do:
class Turbine(Compone nt):
method_specific _to_turbine(... )
send(...) #override some methods
...
and so on. Of course I am not familiar with your problem in depth all
this might not work for you, just use common sense.

Hope this helps,
Nick Vatamaniuc

da*******@f2s.c om wrote:
I have a problem. I'm writing a simulation program with a number of
mechanical components represented as objects. When I create instances
of objects, I need to reference (link) each object to the objects
upstream and downstream of it, i.e.

supply = supply()
compressor = compressor(down stream=combusto r, upstream=supply )
combuster = combuster(downs tream=turbine, upstream=compre ssor)
etc.

the problem with this is that I reference 'combustor' before is it
created. If I swap the 2nd and 3rd lines I get the same problem
(compressor is referenced before creation).
aargh!!! any ideas on getting around this?

Dave
Jul 19 '06 #8
Bruno,

Thanks. An issue is that I need to be able to link multiple objects to
a single object etc.
Say for example using the previous wording, I might have compressor -
multiple combustors - turbine

this complicates things slightly.

my current thought is to do a two stage initialisation

1. create the objects
compressor = compressor()
combuster1 = combuster()
combuster2 = combuster()

etc

2. link them
compressor.link (downstream = [combuster1, combuster2])
combuster1.link (upstream = compressor)
etc.

hmmmm I need to give it some more though, particularly how I solve all
the linked objects (which is the point)

Dave

Jul 19 '06 #9
Even if you need to do something during attachment of components it is
more Pythonic to use properties. So you will write a method in your
class name something like _set_up(self,up stream_obj) an _get_up(self).
And then at the end of your class put up=property(_ge t_up, _set_up).
You can still use the compr.up=... format.
sorry, I don't quite follow. what are properties?
Also, you might want to re-think your OO design. It seem that all of
your components do a lot of things in common already. For one they all
are connected to other components like themselves, they also propably
will have method to do some computing, perhaps send or receive stuff
from other components, or they all will implement somekind of an event
model. In that case you could create a generic component class and
sublass the specific implementations from it.
yes, I already do this - I have a component class and then the other
components inherit from it.

Dave

Jul 19 '06 #10

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

Similar topics

6
2681
by: Jocknerd | last post by:
I'm a Python newbie and I'm having trouble with Regular Expressions when reading in a text file. Here is a sample layout of the input file: 09/04/2004 Virginia 44 Temple 14 09/04/2004 LSU 22 Oregon State 21 09/09/2004 Troy State 24 Missouri 14 As you can see, the text file contains a list of games. Each game has a date, a winning team, the winning...
8
7237
by: Michael Winter | last post by:
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were made about the poster's use of macros. What I would like to know is why they are considered bad? I'm not referring to their use as 'functions'; I realise the loss of type-safety and the possible evaluation errors that can occur. However, what would be the harm with numeric and text literals? Consider a number that plays a significant role in the implementation of...
2
2851
by: James Ying | last post by:
Following is the complete code demonstrating the issue: ===================== > cat tt.cc #include <iostream> template <class T> class Base { public: typedef T difference_type; };
15
17921
by: could ildg | last post by:
In re, the punctuation "^" can exclude a single character, but I want to exclude a whole word now. for example I have a string "hi, how are you. hello", I want to extract all the part before the world "hello", I can't use ".*" because "^" only exclude single char "h" or "e" or "l" or "o". Will somebody tell me how to do it? Thanks.
7
1476
by: GregM | last post by:
Hi, I've looked at a lot of pages on the net and still can't seem to nail this. Would someone more knowledgeable in regular expressions please provide some help to point out what I'm doing wrong? I am trying to see if a web page contains the exact text: You have found 0 matches But instead I seem to be matching all sorts of expected line like
2
3679
by: Martin Høst Normark | last post by:
Hi everyone Has anyone got the least experience in integrating the Digital Signature with an ASP.NET Web Application? Here in Denmark, as I supose in many other countries, they're promoting the digital signature. A lot of people already has one, to do their taxes, and much more. I have to use for a business-to-business e-commerce solution, where it's vital that the right user is being logged on, and not give his username and password...
9
11667
by: raghav4web | last post by:
I use a select box with a fix width (via stylesheet). So some of the entries are cut on the right side if they're too long. Is there a way to display them complete like a tool tip on a mouseover? If I have written code as :- <Select name="selName" style="width: 50">
2
1406
by: beary | last post by:
I have a page with a form which has automatically generated fields, (which come from mysql column names). There could be any number of these fields, and I have no way of knowing exactly what they're called. But obviously I need to know their names, in order to extract their values on the submission page. They are either of the form c%status#, e%status# or t%status# % is a number between 1 and 13 (inclusive) # could be anything in the...
2
3104
by: Fabian Braennstroem | last post by:
Hi, I would like to delete a region on a log file which has this kind of structure: #------flutest------------------------------------------------------------ 498 1.0086e-03 2.4608e-04 9.8589e-05 1.4908e-04 8.3956e-04 3.8560e-03 4.8384e-02 11:40:01 499 499 1.0086e-03 2.4608e-04 9.8589e-05 1.4908e-04
0
9715
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...
0
10600
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10354
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
9175
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
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
6867
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
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.