YANQ: Yet Another Newbie Question ! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: YANQ: Yet Another Newbie Question ! (/thread-4437.html) |
YANQ: Yet Another Newbie Question ! - webdef - Aug-16-2017 Here is the code I try to improve: def getEventFromId(self, service, eventid): event = None if self.epgcache is not None and eventid is not None: event = self.epgcache.lookupEventId(service.ref, eventid) return eventWhen I print service.ref.ToString(), I have '4097:1:5:23' I want to change "4097:" into "1:" without changing "service". Something like: def getEventFromId(self, service, eventid): event = None if self.epgcache is not None and eventid is not None: service2 = service mywork=str(service) if mywork.startswith("4097:"): service2= [b]??? [/b]mywork.replace("4097:","1:",1) event = self.epgcache.lookupEventId(service2.ref, eventid) return eventI am also a bit confuse about the meaning of "service.ref" ! Thanks for help. RE: YANQ: Yet Another Newbie Question ! - nilamo - Aug-16-2017 Without knowing what service is, we should assume it's an object, and ref is just a property of that object. We can fake it like so:>>> class Service: ... def __init__(self, tag): ... self.ref = tag ... >>> service = Service("4097:1:5:23") >>> service.ref '4097:1:5:23'If that's true (...and it might not be), then we could take the fact that strings are immutable in python, and work with it directly... >>> service.ref.startswith("4097:") True >>> new_tag = service.ref.replace("4097:", "1:", 1) >>> service.ref '4097:1:5:23' >>> new_tag '1:1:5:23'Because strings are immutable, we have the new tag that we wanted (with 4097 replaced with 1), while service.ref is left unchanged. Also, I added a count parameter to service.ref.replace, so that only the first 4097 found will be replaced. That way, if the tag happens to look like "4097:1:4097:23", it won't be changed to "1:1:1:23". RE: YANQ: Yet Another Newbie Question ! - webdef - Aug-17-2017 Some feedback: 1) service.ref.replace(...) Crash: service.ref has no attribute replace 2) service.ref=service2.ref(mywork.replace("4097:","1:",1)) Crash: TypeError: 'eServiceReference' object is not callable I have found this for eServicereference: class eServiceReference(object): thisown = _swig_property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc='The membership flag') __repr__ = _swig_repr idInvalid = _enigma.eServiceReference_idInvalid idStructure = _enigma.eServiceReference_idStructure idDVB = _enigma.eServiceReference_idDVB idFile = _enigma.eServiceReference_idFile idUser = _enigma.eServiceReference_idUser type = _swig_property(_enigma.eServiceReference_type_get, _enigma.eServiceReference_type_set) isDirectory = _enigma.eServiceReference_isDirectory mustDescent = _enigma.eServiceReference_mustDescent canDescent = _enigma.eServiceReference_canDescent flagDirectory = _enigma.eServiceReference_flagDirectory shouldSort = _enigma.eServiceReference_shouldSort hasSortKey = _enigma.eServiceReference_hasSortKey sort1 = _enigma.eServiceReference_sort1 isMarker = _enigma.eServiceReference_isMarker isGroup = _enigma.eServiceReference_isGroup flags = _swig_property(_enigma.eServiceReference_flags_get, _enigma.eServiceReference_flags_set) def __init__(self, *args): _enigma.eServiceReference_swiginit(self,_enigma.new_eServiceReference(*args)) __swig_destroy__ = _enigma.delete_eServiceReference eServiceReference.getSortKey = new_instancemethod(_enigma.eServiceReference_getSortKey,None,eServiceReference) eServiceReference.getPath = new_instancemethod(_enigma.eServiceReference_getPath,None,eServiceReference) eServiceReference.setPath = new_instancemethod(_enigma.eServiceReference_setPath,None,eServiceReference) eServiceReference.getUnsignedData = new_instancemethod(_enigma.eServiceReference_getUnsignedData,None,eServiceReference) eServiceReference.getData = new_instancemethod(_enigma.eServiceReference_getData,None,eServiceReference) eServiceReference.setUnsignedData = new_instancemethod(_enigma.eServiceReference_setUnsignedData,None,eServiceReference) eServiceReference.setData = new_instancemethod(_enigma.eServiceReference_setData,None,eServiceReference) eServiceReference.getName = new_instancemethod(_enigma.eServiceReference_getName,None,eServiceReference) eServiceReference.setName = new_instancemethod(_enigma.eServiceReference_setName,None,eServiceReference) eServiceReference.toString = new_instancemethod(_enigma.eServiceReference_toString,None,eServiceReference) eServiceReference.toCompareString = new_instancemethod(_enigma.eServiceReference_toCompareString,None,eServiceReference) eServiceReference.__eq__ = new_instancemethod(_enigma.eServiceReference___eq__,None,eServiceReference) eServiceReference.__ne__ = new_instancemethod(_enigma.eServiceReference___ne__,None,eServiceReference) eServiceReference.__lt__ = new_instancemethod(_enigma.eServiceReference___lt__,None,eServiceReference) eServiceReference.valid = new_instancemethod(_enigma.eServiceReference_valid,None,eServiceReference) eServiceReference_swigregister = _enigma.eServiceReference_swigregister eServiceReference_swigregister(eServiceReference) RE: YANQ: Yet Another Newbie Question ! - nilamo - Aug-17-2017 Neat. service.ref.toString().replace( instead, then.
|