[Tutor] (no subject)

Kent Johnson kent37 at tds.net
Thu Jul 5 14:17:15 CEST 2007


Alun Griffiths wrote:
> Hi
> 
> I am trying to build a simple model of a gas field.  At the moment I have 
> these objects:
> GasWell	represents the behaviour of a gas well
> GasFluid	represents the thermodynamic behaviour of the gas
> GasField	represents the field itself
> 
> The GasField object defines properties such as pressure and volume.  The 
> fluid within it is represented by the GasFluid object which also defines 
> the properties of the fluid flowing through GasWell objects associated with 
> a particular GasField.  GasWell can be moved from one GasField object to 
> another, so take the appropriate fluid behaviour from the GasFluid 
> associated with the GasField to which it belongs.
> 
> At the moment, the GasField definition looks something like this
> 
> Class GasField(Object):
> 	def __init__(name, pres, vol, fluid, wells):
> 		self.name=name
> 		self.pres=pres
> 		self.vol=vol
> 		self.fluid=fluid		# GasFluid object
> 		self.wells=wells		# List of GasWell objects
> 
> No problems with creating the WELL list since I just append a bunch of 
> GasWell objects to a new list.
> 
> The problem I have is how a particular GasWell knows which field it belongs 
> to.  The only way I can think of so far involves some form of cross 
> referencing where we define the GasField with an empty WELL list, define 
> the GasWells by setting the field it belongs to explicitly then updating 
> the WELL list "externally".  For example
> 
> wells = []
> stuff = GasFluid( params )
> field = GasField("My field", p, V, stuff, wells)
> well1 = GasWell(params,  field)
> well2 = GasWell(params,  field)
> well3 = GasWell(params,  field)
> 
> wells.append(well1)
> wells.append(well2)
> wells.append(well2)
> new_field.wells = wells
> 
> This cross-referencing of GasField and GasWells doesn't seem right to me - 
> if we move a well from one field to another we have to update the GasWell 
> object and the GasField.wells list.  Is there a more pythonic (or more 
> sensible) way of doing this?

If the well needs to know what field it is in then you are stuck with 
some cross-referencing. The way to keep your sanity is to make a method 
of GasField or GasWell that changes both, then use that method instead 
of direct assignment. For example,
class GasField(object):
   def add_well(self, well):
     self.wells.append(well)
     well.field = self

or, either by itself of together with the above,
class GasWell(object):
   def add_to_field(self, field):
     field.add_well(self)
     self.field = field # don't need this if using the above add_well()

You could make GasWell.field a property so it is automatically added to 
field.wells when you assign it.

Kent


More information about the Tutor mailing list