Last active
December 31, 2015 01:09
-
-
Save odoku/7911902 to your computer and use it in GitHub Desktop.
Pythonのディスクリプタのサンプル。
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
class Thumb(object): | |
def __init__(self, path): | |
self.url = 'http://hoge.com' + path | |
self.path = path | |
class ThumbDiscriptor(object): | |
BASE_PATH = '/img/{0}/{0}_{1}.{2}' | |
def __init__(self, name): | |
self.name = name | |
def __get__(self, model, klass): | |
extension = model.image.split('.')[-1] | |
tmp_path = self.BASE_PATH.format(model.id, self.name, extension) | |
return Thumb(tmp_path) | |
class Hoge(object): | |
id = 10 | |
image = '/img/hoge.jpg' | |
thumb = ThumbDiscriptor('main_480x360') | |
c = Hoge() | |
print c.thumb.url | |
print c.thumb.path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment