Hi all, I got this new radio scanner (toy!!) this weekend and I can access it via a serial cable. I want to write a interface around it but I am looking for some suggestions. I thought at first I would simply class the Scanner and write the various methods as attibutes similar to below.. But then I thought about it and I don't like it for a couple of obvious reasons - there isn't any data checking, and it's terribly repetitive.
So I started searching on google - and I think it might be better to use a (sub)class for each function ( class STS, PRG, etc.). where the attributes are accessible as both dict items and lists - which ultimately go and "set" the scanner. I guess my question is this. I have a function SIN which when one argument is given does an effective "get" from the scanner - this returns some 10+ values. When some 10+ parameters are given it does an effective "set" to the scanner. Given that I can't remember all 10+ parameters I attempted below to represent them as a dict where the key tells me what the heck the parameter is supposed to represent. So a simple translation occurs on both the set and get which splits the list into a named dict and vice versa. Question 1 - I think I should be checking the data before a set - is this a smart thing or should I just do a try/ except? Question 2 - Since I only want to do this once (ok twice) should I represent each function as a class or should I keep the code I have below? Thanks == My garbage.. == class Scanner(): def __init__(self, *args, **kwargs): self.uniden=UnidenConnection.UnidenConnection(port=kwargs.get('port', PORT), bps=kwargs.get('bps', 115200), loglevel=self.loglevel) def STS(self): return self.uniden.write("STS") def PRG(self): """Go into programming mode""" if self.uniden.write(sys._getframe().f_code.co_name)[0]=="OK": return True else: return False def PRGWrite(self,command): data = self.uniden.write(command) if data[0] == "NG": if self.PRG(): data = self.uniden.write(command) if not self.EPG(): raise IOError, "Unable to exit programming mode" if data[0] == "ERR": raise IOError, "Error when running command %s" % command return data def EPG(self): """Get out of programming mode""" if self.uniden.write(sys._getframe().f_code.co_name)[0]=="OK": return True else: return False def SCT(self): """Get the system count""" self.systems = int(self.PRGWrite(sys._getframe().f_code.co_name)[0]) return self.systems def SIH(self): """Get the site index head""" self.sih = int(self.PRGWrite(sys._getframe().f_code.co_name) [0]) return self.sih def SIT(self): """Get the site index tail""" self.sit = int(self.PRGWrite(sys._getframe().f_code.co_name) [0]) return self.sit def FWD(self,idx): """Get the next site index""" fwd = int(self.PRGWrite("%s,%s" % (sys._getframe().f_code.co_name,idx))[0]) return fwd def SIF(self, idx, args=None): """Get / Set Site Info""" if args is None: p=self.PRGWrite("%s,%s" % (sys._getframe().f_code.co_name,idx)) t={} t["site_type"],t["name"],t["quick_key"],t["hld"],t["lout"],t["mod"], \ t["att"],t["c- ch"],t["apco"],t["threshold"],t["rev_index"],t["fwd_index"], \ t["sys_index"],t["chn_head"],t["chn_tail"],t["seq_no"],t["start_key"], \ t["latitude"],t["longitude"],t["range"],t["gps_enable"],t["rsv"]=p delitems=[] for item in t: if t[item]=="": delitems.append(item) for x in delitems: del t[x] if len(t.keys())==0: raise IOError, "Unable to program %s - Returned Nothing" % (sys._getframe().f_code.co_name) else: return t else: args["rsv"]="" s=[ args["index"],args["name"],args["quick_key"],args["hld"],args["lout"],args["mod"], \ args["att"],args["c- ch"],args["apco"],args["threshold"],args["start_key"], \ args["latitude"],args["longitude"],args["range"],args["gps_enable"],args["rsv"]] p=self.PRGWrite("%s,%s" % (sys._getframe().f_code.co_name,",".join(s))) if p[0] != "OK": raise IOError, "Unable to program %s - Returned %s" % (sys._getframe().f_code.co_name,p[0]) else: return True def SIN(self, idx, args=None): """Get / Set System Info""" if args is None: p=self.PRGWrite("%s,%s" % (sys._getframe().f_code.co_name,idx)) t={} t["sys_type"],t["name"],t["quick_key"],t["hld"],t["lout"], \ t["dly"],t["skp"],t["rsv"],t["rsv"],t["apco"],t["threshold"], \ t["rev_index"],t["fwd_index"],t["chn_grp_head"],t["chn_grp_tail"], \ t["seq_no"],t["start_key"],t["record"],t["latitude"],t["longitude"], \ t["range"],t["gps enable"],t["rsv"]=p delitems=[] for item in t: if t[item]=="": delitems.append(item) for x in delitems: del t[x] if len(t.keys())==0: raise IOError, "Unable to program %s - Returned Nothing" % (sys._getframe().f_code.co_name) else: return t else: args["rsv"]="" s=[ args["index"],args["name"],args["quick_key"],args["hld"],args["lout"],args["dly"], \ args["skp"],args["rsv"],args["rsv"],args["apco"],args["threshold"],args["start_key"], \ args["record"],args["latitude"],args["longitude"],args["range"],args["gps_enable"],args["rsv"]] p=self.PRGWrite("%s,%s" % (sys._getframe().f_code.co_name,",".join(s))) if p[0] != "OK": raise IOError, "Unable to program %s - Returned %s" % (sys._getframe().f_code.co_name,p[0]) else: return True -- http://mail.python.org/mailman/listinfo/python-list