Created
December 11, 2011 05:21
-
-
Save gustavofonseca/1458555 to your computer and use it in GitHub Desktop.
descritores somente leitura
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding:utf-8 | |
""" | |
>>> livro = Livro('Eram os deuses astronautas?', 'Erich von Daniken') | |
>>> livro.autor_individual | |
'DANIKEN, Erich von' | |
>>> livro2 = Livro('O menino maluquinho', 'Ziraldo') | |
Traceback (most recent call last): | |
... | |
ValueError: Deve ser informado o nome e sobrenome | |
""" | |
class Autor(object): | |
def __init__(self, nome_atr): | |
self.nome_atr = '_'+nome_atr | |
def __get__(self, instancia, classe): | |
desmontado = getattr(instancia, self.nome_atr).split() | |
sobrenome = desmontado[-1] | |
restante = ' '.join(desmontado[:-1]) | |
return '%s, %s' % (sobrenome.upper(), restante) | |
class Livro(object): | |
autor_individual = Autor('autor_individual') | |
def __init__(self, titulo, autor_individual): | |
self.titulo = titulo | |
if len(autor_individual.split()) < 2: | |
raise ValueError('Deve ser informado o nome e sobrenome') | |
self._autor_individual = autor_individual | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment