A clean way to program an interface

rh0dium steven.klass at gmail.com
Wed Jul 11 15:49:48 CEST 2007


On Jul 9, 1:19 pm, rh0dium <steven.kl... at gmail.com> wrote:

Hi all,

OK so I've started re-writing this based on the feedback you all gave
me.  How does this look?

class Scanner:

    def __init__(self, *args, **kwargs):
        """description"""
        # Setup Logging
        self.log = logging.getLogger(module+self.__class__.__name__)
        self.loglevel = kwargs.get('loglevel', logging.WARN)
        self.setLoglevel(self.loglevel)

        self.serialport=kwargs.get('port', PORT)
        self.serialbps=kwargs.get('bps', 115200)
 
self.uniden=UnidenConnection.UnidenConnection(port=self.serialport,
bps=self.serialbps, loglevel=self.loglevel)

        if self.inProgramMode():
            self.PRGMode=True
        else: self.PRGMode=False


    def setLoglevel(self,loglevel):
        """setLog log level"""
        if loglevel is not False:
            self.log.setLevel(loglevel)
            self.log.debug("Setting Logging level to %s" % loglevel)
        else:
            self.log.setLevel(logging.WARN)

        if self.loglevel == logging.DEBUG: self.debug = True
        else: self.debug=False

    def ProgramMode(fn):
        def _prgmode(self,*args,**kwargs):
            progmode=True
            if not self.PRGMode:
                self.log.debug("Going into programming mode")
                progmode=False
                self.PRG()
            ret = fn(self,*args,**kwargs)
            if not progmode:
                self.log.debug("Getting out of programming mode")
                self.EPG()
            return ret
        return _prgmode

    def __orderedDictValues(self,orderedlist,dictvals):

        rlist = []
        for x in orderedlist:
            if dictvals.has_key(x) or x == "rsv":
                if x == "rsv": rlist.append("")
                else: rlist.append(str(dictvals[x]))
            else:
                break
        return rlist

    def STS(self):
        """Get Current Status"""
        cmd = "STS"
        p = self.uniden.write(cmd)
        retargs = "dsp_form l1_char l1_mode l2_char l2_mode l3_char
l3_mode \
                   l4_char l4_mode l5_char l5_mode l6_char l6_mode
l7_char \
                   l7_mode l8_char l8_mode sql mut rsv wat led_1 led_2
sig_lvl \
                   bk_color bk_dimmer".split()

        t = dict((k,v) for k, v in zip(retargs,p) if v != "")
        return t

    def PRG(self):
        """Enter Programming Mode"""
        self.PRGMode=True
        return self.uniden.write("PRG")

    def EPG(self):
        """Exit Programming Mode"""
        self.PRGMode=True
        return self.uniden.write("EPG")

    @ProgramMode
    def SIN(self, *args, **kwargs):
        """Get/Set System Index"""

        cmd = "SIN"
        iptargs="index name quick_key hld lout dly skp rsv rsv apco \
                 threshold start_key record latitude longitude range \
                 gps_enable rsv".split()

        iptarglist=self.__orderedDictValues(iptargs,kwargs)

        if len(iptarglist) != len(iptargs) and len(iptarglist) != 1:
            raise SyntaxError, "Missing arguments for %s expected 1 or
%d got %d - given %s" % (cmd, len(iptargs), len(iptarglist), kwargs)

        p = self.uniden.write("%s,%s" % (cmd,",".join(iptarglist)))

        retargs = "sys_type name quick_key hld lout dly skp rsv rsv
apco \
                   threshold rev_index fwd_index chn_grp_head
chn_grp_tail \
                   seq_no start_key record latitude longitude range
gps enable \
                   rsv".split()

        t = dict((k,v) for k, v in zip(retargs,p) if v != "")

        return t


    def inProgramMode(self):
        try:
            print"PRGMODE",self.STS()
            if self.STS()["l2_char"].strip()=="Remote Mode":
                print "IN PRG"
                return True
            else: return False
        except: return False




More information about the Python-list mailing list