Skip to content

Instantly share code, notes, and snippets.

@tonyseek
Created May 29, 2013 12:10
Show Gist options
  • Save tonyseek/5669827 to your computer and use it in GitHub Desktop.
Save tonyseek/5669827 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import uuid
def entity_repr(entity, attrs):
"""Dumps attributes and values of given entity object."""
attrs_repr = (u"%s=%s" % (n, repr(getattr(entity, n))) for n in attrs)
unicode_repr = u"%s(%s)" % (type(entity).__name__, u", ".join(attrs_repr))
return unicode_repr.encode("utf-8") if str is bytes else unicode_repr
class Book(object):
"""The book entry model."""
def __init__(self, title, creator, language, **kwargs):
self.title = title
self.creator = creator
self.language = language
self.uuid = kwargs.pop("uuid", self.gen_default_uuid())
def __repr__(self):
return entity_repr(self, ["title", "creator", "language", "uuid"])
def gen_default_uuid(self):
"""Creates a new random UUID."""
return str(uuid.uuid4())
def demo():
"""Just a demo."""
book = Book(title=u"背影", creator=u"朱自清", language=u"zh-Hans")
print repr(book)
print eval(repr(book))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment