From: Chris Couzens Date: Fri, 25 Jun 2010 03:53:25 +0000 (+0200) Subject: python: extend ServiceTypeDatabase.py API a little X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=b578920285735e2926dfb5bd800f6d3d04b4d69c;p=catta python: extend ServiceTypeDatabase.py API a little Added a few useful methods to ServiceTypeDatabase.py such as get(key, default). These methods will be familiar to any python programmer. Fixes #294 --- diff --git a/avahi-python/avahi/ServiceTypeDatabase.py.in b/avahi-python/avahi/ServiceTypeDatabase.py.in index 8a77a31..8ad8d5e 100644 --- a/avahi-python/avahi/ServiceTypeDatabase.py.in +++ b/avahi-python/avahi/ServiceTypeDatabase.py.in @@ -24,6 +24,7 @@ import re locale.setlocale(locale.LC_ALL, '') class ServiceTypeDatabase: + """ServiceTypeDatabase maps service types to descriptions""" def __init__(self, filename = "@pkglibdir@/service-types.db"): @@ -66,14 +67,7 @@ class ServiceTypeDatabase: def items(self): - items = [] - @FIRST_KEY@ - @CHECK_KEY@ - if re.search('_[a-zA-Z0-9-]+\._[a-zA-Z0-9-]+', key) and not re.search('_[a-zA-Z0-9-]+\._[a-zA-Z0-9-]+\[.*\]', key): - localized_service_name = self[key] - items.append((key, localized_service_name)) - @NEXT_KEY@ - return items + return list(self.iteritems()) def has_key(self, key): @@ -94,6 +88,52 @@ class ServiceTypeDatabase: return False + def __iter__(self): + + @FIRST_KEY@ + @CHECK_KEY@ + + if re.search('_[a-zA-Z0-9-]+\._[a-zA-Z0-9-]+', key) and not re.search('_[a-zA-Z0-9-]+\._[a-zA-Z0-9-]+\[.*\]', key): + yield key + + @NEXT_KEY@ + + def __len__(self): + + count = 0 + for _ in self: + + count+=1 + + self.__len__ = lambda : count + return len(self) + + def get(self, key, default=None): + + if key in self: + return self[key] + else: + return default + + def iteritems(self): + + return ((key, self[key]) for key in self) + + def iterkeys(self): + + return self.__iter__() + + def itervalues(self): + + return (self[key] for key in self) + + def keys(self): + + return list(self) + + def values(self): + + return list(self.itervalues()) if __name__ == "__main__": @@ -105,4 +145,17 @@ if __name__ == "__main__": print b["_webdav._tcp"] print b["_webdavs._tcp"] - print b["gurki._tcp"] + print b.get("gurki._tcp") + print len(b) + + for key, _ in zip(b, xrange(3)): + + print key + + for key, _ in zip(b.iteritems(), xrange(3)): + + print key + + for key, _ in zip(b.itervalues(), xrange(3)): + + print key