Python alapu mobil backend
Transcript of Python alapu mobil backend
Python alapú mobil backend GAE felett
by Gyuri Horak
GAE Python
● Python 2.7● WSGI, webapp2● db, memcache, cloud-sql, url fetch,
blobstore, users, unit testing ...● django (django-nonrel, django+cloud-sql)
Példaprojekt
● backend iOS, Android és weboldal számára● REST/JSON
Flask
Flask microframework, @mitsuhiko● url routing● class based views● debugger, gae-profiler● ...
JSONView
● Flask.views.MethodView
● success és failure metódusok● X-Location header● néhány Exception kezelése
ExceptionHandlerViewMeta
class ReferenceErrorMeta(MethodViewType):
def __new__(mcs, name, bases, odict):
if 'get' in odict: odict['get'] = mcs.wrapmethod(odict['get'])
return MethodViewType.__new__(mcs, name, bases, odict)
@staticmethod def wrapmethod(method): @wraps(method) def wrapper(self, *args, **kwargs): try: return method(self, *args, **kwargs) except db.ReferencePropertyResolveError: return self.failure(10, 'Reference object seems to be deleted.')
return wrapper
DB: a lekérdezés pénz
● NE előre optimalizálj!● cache: memcache, request scope cache● aggregálni, okosan● Mérj! (GAE profiler, datastore stats)
CachedModelclass CachedModel(db.Model):
@classmethod def get_from_cache(id): ...
@classmethod def get(cls, key): obj = cls.get_from_cache(key.id()) if not obj: obj = super(CachedModel, cls).get(key) return obj
def put(self): super(CachedModel, self).put() self.update_in_cache()
def delete(self): super(CachedModel, self).delete() self.remove_in_cache()
def __eq__(self, other): return self.key() == other.key()
class CachedReferenceProperty(db.ReferenceProperty):
def __get__(self, model_instance, model_class):
...
# instance = get(reference_id) # WTF? instance = self.reference_class.get(reference_id)
if instance is None:
# Feature check vs. isinstance if hasattr(model_instance, 'update_references'): model_instance.update_references()
raise db.ReferencePropertyResolveError(...)
CachedReferenceProperty
class CachedReferenceProperty(db.ReferenceProperty):
def __get__(self, model_instance, model_class):
...
# instance = get(reference_id) # WTF? instance = self.reference_class.get(reference_id)
if instance is None:
# Feature check vs. isinstance if hasattr(model_instance, 'update_references'): model_instance.update_references()
raise db.ReferencePropertyResolveError(...)
Aggregáció
Aggregáció
Sérülhet, akár rajtunk kívül álló okok miatt is.● cron jobok takarítani / update-elni● javítás, amikor hibát találunk
Képkezelés
● blobstore○ create_upload_url()○ files API (experimental)
● images ~ PIL
● get_serving_url()○ lehet, hogy nincs kész
Geomodel
● db.GeoPt(lat, lon)● geocells● bounding box és proximity lekérdezéslocalmind is ezt használja clustering megoldásként