[PATCH] Factor out duplicate code

Michael Ellerman michael at ellerman.id.au
Wed Oct 12 04:05:25 BST 2005


On Wed, 12 Oct 2005 11:56, John A Meinel wrote:
> Dan Loda wrote:
> > ... by adding a factory method to InventoryEntry.
> >
> > I'm new to Python; constructive criticism is most welcome ;)
...
>     @staticmethod
>     def create(kind, file_id, name, parent_id):
>         """Return an inventory entry of specified kind"""
>         if not InventoryEntry.versionable_kind(kind):
>             raise BadFileKindError("unknown kind %r" % kind)
>         klass = InventoryEntry.versionable_kinds[kind]
>         return klass(file_id, name, parent_id)

This is a bit nit-picky, but a more Pythonish way to do the above is:

@staticmethod
def create(kind, file_id, name, parent_id):
	"""Return an inventory entry of specified kind"""
	try:
		klass = InventoryEntry.versionable_kinds[kind]
		return klass(file_id, name, parent_id)
	except KeyError:
		raise BadFileKindError("unknown kind %r" % kind)
	
cheers

-- 
Michael Ellerman
IBM OzLabs

email: michael:ellerman.id.au
inmsg: mpe:jabber.org
wwweb: http://michael.ellerman.id.au
phone: +61 2 6212 1183 (tie line 70 21183)

We do not inherit the earth from our ancestors,
we borrow it from our children. - S.M.A.R.T Person
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20051012/60a1963b/attachment.pgp 


More information about the bazaar mailing list