[ale] Anyone familiar with Python?

Pete Hardie pete.hardie at gmail.com
Tue Mar 8 17:10:58 EST 2016


Using a static or class method might give you the effect you want, but I
haven't used those in Python, so I can't speak to their pitfalls

On Tue, Mar 8, 2016 at 5:05 PM, Alex Carver <agcarver+ale at acarver.net>
wrote:

> On 2016-03-08 13:24, Pete Hardie wrote:
> > My first thought is not so much Python-related as design related.  If you
> > want to have only one instance of the wire, you need to make it so that
> the
> > base class functions are either bound to the class, or to an object
> passed
> > in, so you can use the same 'wire' object for each sensor class
> >
> > (I am assuming that you are dealing with just a single one of these
> buses)
>
> Yep, only one address.  But passing it in made sense.  I split off the
> serial into a top level class within the imported module and then turned
> it into a parameter for the other classes.
>
> I had hoped for a slightly more "automagic" method where the serial port
> was already in place without having to pass it as an argument but that
> doesn't appear to be possible.  Fortunately I only have to pass it once
> when the class is instantiated.
>
> >
> >
> > On Tue, Mar 8, 2016 at 4:12 PM, Alex Carver <agcarver+ale at acarver.net>
> > wrote:
> >
> >> Ok, so this should be fun since I've spent the past hour and a half on
> >> Google and haven't really gotten anywhere.
> >>
> >> I'm writing some code to control data collection modules.  Physically,
> >> all the modules are hanging off an RS485 bus with a single wire to the
> >> computer.  Each of the modules has a set of very core commands
> >> (specifically for things like fetching the module name, firmware number,
> >> and base configuration) and then it has module specific commands that
> >> vary depending on the type of module (read voltage inputs, set relays on
> >> outputs, etc.)  All of the modules share the same protocol for comms.
> >>
> >> So my original thought was to create a class which contained the very
> >> bottom stuff, serial I/O/ that would handle the basic serial functions
> >> and the protocol.  Let's call this data_collectors.
> >>
> >> I then thought to make a subclass of data_collectors that contains the
> >> more specific functions mapped to the protocol commands (e.g. I'd have a
> >> get_voltage() function for the specific voltage input module and a
> >> set_relay() function for a relay output module).  The modules each have
> >> their own address.
> >>
> >> So in code form (some pseudo code in here, too for brevity):
> >>
> >> <file is my_data_collector_file>
> >> class data_collector(object):
> >>
> >>         serial_port = None
> >>
> >>         def open_port(self, tty=''):
> >>                 self.serial_port = serial.open(tty)
> >>
> >>         def write_port(...):
> >>
> >>         def read_port(...):
> >>
> >>         def get_module_name(...):
> >>                 write_port()
> >>                 read_port()
> >>
> >> class voltage_input(data_collector):
> >>         __init__(self, module_address):
> >>                 self.module_address  = module_address
> >>
> >>         get_voltage(self, channel):
> >>                 <stuff here including write_port() read_port()>
> >>
> >> class relay_output(data_collector):
> >>         __init__(self, module_address):
> >>                 self.module_address = module_address
> >>
> >>
> >>         set_relay(self, channel, value):
> >>                 <stuff here including value validation, write_port(),
> >> read_port()
> >>
> >>
> >> The goal was to be able to import this block of code and then do the
> >> following (somehow):
> >>
> >> import my_data_collector_file as my_dcf
> >>
> >> dcf.open_port(tty=...0)  #somehow do this, not sure how
> >> gauges = dcf.voltage_input(module_address=1)
> >> valves = dcf.relay_output(module_address=2)
> >>
> >>
> >> Right now, with the same structure I can execute the gauges = and valves
> >> = lines but they get independent copies of the base class.  I want to
> >> share the base class (so the port opens and stays open) among all of the
> >> subclasses but have the subclasses behave individually (since they each
> >> have their own set of commands that don't overlap).
> >>
> >> I haven't figured out if there's a way to accomplish this even if
> >> there's a third call (as above with the dcf.open_port) to just get
> >> everything started.
> >> _______________________________________________
> >> Ale mailing list
> >> Ale at ale.org
> >> http://mail.ale.org/mailman/listinfo/ale
> >> See JOBS, ANNOUNCE and SCHOOLS lists at
> >> http://mail.ale.org/mailman/listinfo
> >>
> >
> >
> >
> >
> >
> > _______________________________________________
> > Ale mailing list
> > Ale at ale.org
> > http://mail.ale.org/mailman/listinfo/ale
> > See JOBS, ANNOUNCE and SCHOOLS lists at
> > http://mail.ale.org/mailman/listinfo
> >
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>



-- 
Pete Hardie
--------
Better Living Through Bitmaps
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.ale.org/pipermail/ale/attachments/20160308/1b2951b6/attachment.html>


More information about the Ale mailing list