Re: Best practice: Sharing object between different objects

2015-02-23 Thread Rob Gaddi
On Sat, 21 Feb 2015 04:15:50 -0800, pfranken85 wrote:

 Hello!
 
 I have a best-practice question: Imagine I have several hardware devices
 that I work with on the same I2C bus and I am using the python smbus
 module for that purpose. The individual devices are sensors, ADC, DAC
 components. As I said, I would like to derive the corresponding classes
 from one common class, let's say I2CDevice, so that they can share the
 same bus connection (I don't want to do a import smbus, ..., self.bus =
 smbus.SMBus(1) all the time). Of course, I could just pass the the bus
 reference to each instance, but I am pretty sure that there must be a
 nicer way to do this.
 
 In particular, I imagine the following: It should be possible that I
 have two classes, ADC_I2C, DAC_I2C which share the same base class. Once
 I create an instance of ADC_I2C or DAC_I2C it should check whether a bus
 object exists, if not, it should create one and the other class should
 be able to use this bus reference as well. Do you get my point? I am
 pretty sure that such a design pattern should exist, maybe also in the
 reference of DB connections? Unfortunately I did not find something like
 this.
 
 Any hints are highly appreciated!
 
 Thanks!

So my experience doing similar work is with VMEBus, but if I were trying 
to do what you're doing, I'd take advantage of the fact that hardware 
isn't nearly as abstract as software.  SMBus(1) represents three physical 
contiguous pieces of copper on a PCB somewhere.

So I'd solve it with module level global variables.  It's semi-frowned 
upon on software stuff because it creates an unintentional shared state 
between different modules, but you really HAVE a shared state, so it 
needs to be dealt with.

So I'd put your ADC and DAC in the same package, whether all in one file 
or all in a package folder, and I'd give that package a:

def getSMBus(busnumber):

that takes care of the management of only creating each bus once.

Then I'd give each class a:

def __init__(busnumber):
  self.bus = getSMBus(busnumber)

You could commonize that by deriving both classes from a common ancestor 
(all my VME modules derive from a common Region), but with only one piece 
of shared code between I don't think I'd bother.  If you did, you could 
make the getSMBus code a classmethod just to make clear the lack of 
dependency on individual instances, but that starts to be code for code's 
sake.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best practice: Sharing object between different objects

2015-02-23 Thread Ian Kelly
On Mon, Feb 23, 2015 at 1:02 PM,  sohcahto...@gmail.com wrote:
 What's REALLY interesting is that this happens:

 import myModule
 myModule.myInt
 1
 myModule.myInt = 2
 myModule.myInt
 2
 del myModule
 import myModule
 myModule.myInt
 2

 I would REALLY expect that deleting the module object and then re-importing 
 would reset that variable.

Even though you deleted the module locally, it's still referenced in
the sys.modules cache (as well as in any other place where it might
have been imported). That's the place you need to delete it from if
you really want to re-execute it.

 import myModule
 myModule.myInt
1
 myModule.myInt = 2
 myModule.myInt
2
 import sys
 del sys.modules['myModule']
 import myModule
 myModule.myInt
1
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best practice: Sharing object between different objects

2015-02-21 Thread Paul Rubin
pfranke...@gmail.com writes:
 ADC, DAC components. As I said, I would like to derive the
 corresponding classes from one common class, let's say I2CDevice, so
 that they can share the same bus connection (I don't want to do a
 import smbus, ..., self.bus = smbus.SMBus(1) all the time). 

I don't really understand the question but it sounds like you might
want a class variable:

import smbus

class I2CDevice
   bus = smbus.SMBus(1)
   def __init__ (self): ...

This puts the bus object into the I2CDevice class itself.  It is
created when the I2CDevice class is defined.  Methods wanting to access
it can then say

   do_something_with(I2CDevice.bus)

is that what you wanted?
-- 
https://mail.python.org/mailman/listinfo/python-list


Best practice: Sharing object between different objects

2015-02-21 Thread pfranken85
Hello!

I have a best-practice question: Imagine I have several hardware devices that I 
work with on the same I2C bus and I am using the python smbus module for that 
purpose. The individual devices are sensors, ADC, DAC components. As I said, I 
would like to derive the corresponding classes from one common class, let's say 
I2CDevice, so that they can share the same bus connection (I don't want to do a 
import smbus, ..., self.bus = smbus.SMBus(1) all the time). Of course, I could 
just pass the the bus reference to each instance, but I am pretty sure that 
there must be a nicer way to do this.

In particular, I imagine the following: It should be possible that I have two 
classes, ADC_I2C, DAC_I2C which share the same base class. Once I create an 
instance of ADC_I2C or DAC_I2C it should check whether a bus object exists, if 
not, it should create one and the other class should be able to use this bus 
reference as well. Do you get my point? I am pretty sure that such a design 
pattern should exist, maybe also in the reference of DB connections? 
Unfortunately I did not find something like this.

Any hints are highly appreciated!

Thanks!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best practice: Sharing object between different objects

2015-02-21 Thread Dave Angel

On 02/21/2015 07:15 AM, pfranke...@gmail.com wrote:

Hello!

I have a best-practice question: Imagine I have several hardware devices that I 
work with on the same I2C bus and I am using the python smbus module for that 
purpose. The individual devices are sensors, ADC, DAC components. As I said, I 
would like to derive the corresponding classes from one common class, let's say 
I2CDevice, so that they can share the same bus connection (I don't want to do a 
import smbus, ..., self.bus = smbus.SMBus(1) all the time). Of course, I could 
just pass the the bus reference to each instance, but I am pretty sure that 
there must be a nicer way to do this.

In particular, I imagine the following: It should be possible that I have two 
classes, ADC_I2C, DAC_I2C which share the same base class. Once I create an 
instance of ADC_I2C or DAC_I2C it should check whether a bus object exists, if 
not, it should create one and the other class should be able to use this bus 
reference as well. Do you get my point? I am pretty sure that such a design 
pattern should exist, maybe also in the reference of DB connections? 
Unfortunately I did not find something like this.



I think you should write the code (or at least a skeleton version).  I 
suspect the answer will become obvious to you, once you're not worrying 
about design patterns, or being java compatible.


Regardless of whether you inherit from a common base class, both classes 
can have an attribute with a bus object in it.  The question becomes 
who initializes it, and who decides to reuse the same one.  Your 
wording doesn't make that the least bit clear to me;  maybe your sample 
code will.


Many times when you'd have a common base class in Java or C++, you just 
have to have a common member or two in Python.


On the other hand, once you start implementing, maybe it'll become 
obvious that there's enough shared code for a common base class.


The first question is whether an ADC is-a bus, or has-a bus.  Stated 
that way it sounds like you should have an attribute in ADC which is a 
bus object.



Any hints are highly appreciated!

Thanks!




--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list