Skip to content

Instantly share code, notes, and snippets.

@hirokazumiyaji
Last active December 17, 2015 03:08
Show Gist options
  • Save hirokazumiyaji/5540759 to your computer and use it in GitHub Desktop.
Save hirokazumiyaji/5540759 to your computer and use it in GitHub Desktop.
この出力は何になるでしょう?
# coding: utf-8
class Entity(object):
def __init__(self, entity_type, entity_id):
self.entity_type = entity_type
self.entity_id = entity_id
class Reward(object):
def __init__(self, entity_type, entity_id, number):
self.entity_type = entity_type
self.entity_id = entity_id
self.number = number
@property
def entity(self):
return get_entity(self.entity_type, self.entity_id)
e = {
1: {1: Entity(1, 1), 2: Entity(1, 2)},
2: {1: Entity(2, 1), 2: Entity(2, 2)}
}
def get_entity(entity_type, entity_id):
return e[entity_type][entity_id]
def main():
rewards = [Reward(1, 1, 1), Reward(2, 1, 1), Reward(2, 1, 2), Reward(1, 1, 3)]
entities = []
for r in rewards:
entity = r.entity
entity.number = r.number
print 'reward type = {}, id = {}, number = {}'.format(r.entity_type, r.entity_id, r.number)
entities.append(entity)
for e in entities:
print 'entity type = {}, id = {}, number = {}'.format(e.entity_type, e.entity_id, e.number)
'''
from django.db import models
from django.core.cache import cache
class CacheHelper(object):
@classmethod
def get(cls, pk):
data = cache.get('{}:pk:{}'.format(cls.__module__, cls.__name__, pk), None)
if not data:
data = cls.objects.get(pk=pk)
cache.set('{}:pk:{}'.format(cls.__module__, cls.__name__, pk), data)
return data
@classmethod
def all(cls):
data = cache.get('{}:all'.format(cls.__module__, cls.__name__), [])
if not data:
data = list(cls.objects.all())
cache.set('{}:all'.format(cls.__module__, cls.__name__), data)
return data
---------------
| id | name |
---------------
| 1 | card1 |
---------------
| 2 | card2 |
---------------
class Card(models.Model, CacheHelper):
name = models.CharFiled()
---------------
| id | name |
---------------
| 1 | item1 |
---------------
| 2 | item2 |
---------------
class Item(models.Model, CacheHelper):
name = models.CharField()
def get_entity(entity_type, entity_id):
if entity_type == 1:
model = Card
elif entity_type == 2:
model = Item
return model.get(entity_id)
class Entity(models.Model):
entity_type = models.IntegerField()
entity_id = models.IntegerField()
class Meta:
abstract = True
@property
def entity(self):
return get_entity(self.entity_type, self.entity_id)
-----------------------------------------
| id | entity_type | entity_id | number |
-----------------------------------------
| 1 | 1 | 1 | 1 |
-----------------------------------------
| 2 | 1 | 2 | 2 |
-----------------------------------------
| 3 | 2 | 2 | 5 |
-----------------------------------------
| 4 | 2 | 1 | 2 |
-----------------------------------------
| 5 | 1 | 1 | 3 |
----------------------------------------
| 6 | 2 | 2 | 1 |
-----------------------------------------
class Reward(Entity, CacheHelper):
number = models.IntegerField()
def main():
entities = []
for reward in Reward.all():
print 'reward type = {}, id = {}, number = {}'.format(reward.entity_type, reward.entity_id, reward.number)
entity = reward.entity
entity.number = reward.number
entities.append(entity)
for entity in entities:
print 'entity name = {}, number = {}'.format(entity.name, entity.entity_id, entity.number)
'''
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment