Simplify with built-in functions where possible. e.g.

change [x for x in y if x] to filter(None, y)
change [f(x) for x in y] to map(f, x)
change [x for x in y] to list(y)

These changes reduce source code size and complexity and produce some minor performance gains

svn: r14104
This commit is contained in:
Gerald Britton
2010-01-21 18:42:53 +00:00
parent fdfb1b48b0
commit 1f2469b09f
38 changed files with 81 additions and 101 deletions

View File

@@ -1601,7 +1601,7 @@ class DbWriteBase(object):
self.set_default_person_handle(None)
# loop through the family list
for family_handle in [ f for f in person.get_family_handle_list() if f ]:
for family_handle in filter(None, person.get_family_handle_list()):
family = self.get_family_from_handle(family_handle)

View File

@@ -51,10 +51,7 @@ class AddressBase(object):
:param source: Object used to initialize the new object
:type source: AddressBase
"""
if source:
self.address_list = [Address(addr) for addr in source.address_list]
else:
self.address_list = []
self.address_list = map(Address, source.address_list) if source else []
def serialize(self):
"""
@@ -66,7 +63,7 @@ class AddressBase(object):
"""
Convert a serialized tuple of data to an object.
"""
self.address_list = [Address().unserialize(item) for item in data]
self.address_list = map(Address().unserialize, data)
def add_address(self, address):
"""

View File

@@ -67,7 +67,7 @@ class AttributeBase(object):
"""
Convert a serialized tuple of data to an object.
"""
self.attribute_list = [Attribute().unserialize(item) for item in data]
self.attribute_list = map(Attribute().unserialize, data)
def add_attribute(self, attribute):
"""

View File

@@ -241,7 +241,7 @@ class Family(SourceBase, NoteBase, MediaBase, AttributeBase, LdsOrdBase,
:returns: Returns the list of child objects that may carry textual data.
:rtype: list
"""
add_list = [item for item in self.lds_ord_list if item]
add_list = filter(None, self.lds_ord_list)
return self.media_list + self.attribute_list + \
self.source_list + add_list

View File

@@ -68,7 +68,7 @@ class LdsOrdBase(object):
"""
Convert a serialized tuple of data to an object
"""
self.lds_ord_list = [LdsOrd().unserialize(item) for item in data]
self.lds_ord_list = map(LdsOrd().unserialize, data)
def add_lds_ord(self, lds_ord):
"""

View File

@@ -48,11 +48,7 @@ class MediaBase(object):
:param source: Object used to initialize the new object
:type source: MediaBase
"""
if source:
self.media_list = [ MediaRef(mref) for mref in source.media_list ]
else:
self.media_list = []
self.media_list = map(MediaRef, source.media_list) if source else []
def serialize(self):
"""
@@ -64,7 +60,7 @@ class MediaBase(object):
"""
Convert a serialized tuple of data to an object.
"""
self.media_list = [MediaRef().unserialize(item) for item in data]
self.media_list = map(MediaRef().unserialize, data)
def add_media_reference(self, media_ref):
"""

View File

@@ -44,11 +44,7 @@ class NoteBase(object):
:param source: Object used to initialize the new object
:type source: NoteBase
"""
if source:
self.note_list = [handle for handle in source.note_list]
else:
self.note_list = []
self.note_list = list(source.note_list) if source else []
def serialize(self):
"""
@@ -60,7 +56,7 @@ class NoteBase(object):
"""
Convert a serialized tuple of data to an object.
"""
self.note_list = [handle for handle in data]
self.note_list = list(data)
def add_note(self, handle):
"""

View File

@@ -309,7 +309,7 @@ class Person(SourceBase, NoteBase, AttributeBase, MediaBase,
:rtype: list
"""
check_list = self.lds_ord_list
add_list = [item for item in check_list if item]
add_list = filter(None, check_list)
return [self.primary_name] + self.media_list + \
self.alternate_names + self.address_list + \
self.attribute_list + self.urls + \

View File

@@ -82,7 +82,7 @@ class Source(MediaBase, NoteBase, PrimaryObject):
self.marker.unserialize(marker)
NoteBase.unserialize(self, note_list)
MediaBase.unserialize(self, media_list)
self.reporef_list = [RepoRef().unserialize(rr) for rr in reporef_list]
self.reporef_list = map(RepoRef().unserialize, reporef_list)
def _has_handle_reference(self, classname, handle):
"""

View File

@@ -48,10 +48,8 @@ class SourceBase(object):
:param source: Object used to initialize the new object
:type source: SourceBase
"""
if source:
self.source_list = [SourceRef(sref) for sref in source.source_list]
else:
self.source_list = []
self.source_list = map(SourceRef, source.source_list) if source else []
def serialize(self):
"""
@@ -63,7 +61,7 @@ class SourceBase(object):
"""
Convert a serialized tuple of data to an object.
"""
self.source_list = [SourceRef().unserialize(item) for item in data]
self.source_list = map(SourceRef().unserialize, data)
def add_source_reference(self, src_ref) :
"""

View File

@@ -51,11 +51,7 @@ class UrlBase(object):
:param source: Object used to initialize the new object
:type source: UrlBase
"""
if source:
self.urls = [ Url(url) for url in source.urls ]
else:
self.urls = []
self.urls = map(Url, source.urls) if source else []
def serialize(self):
"""
@@ -67,7 +63,7 @@ class UrlBase(object):
"""
Convert a serialized tuple of data to an object.
"""
self.urls = [Url().unserialize(item) for item in data]
self.urls = map(Url().unserialize, data)
def get_url_list(self):
"""

View File

@@ -377,7 +377,7 @@ class PrivateProxyDb(ProxyDbBase):
Note that this is a generator function, it returns a iterator for
use in loops. If you want a list of the results use:
> result_list = [i for i in find_backlink_handles(handle)]
> result_list = list(find_backlink_handles(handle))
"""
# This isn't done yet because it doesn't check if references are