Python alapu mobil backend

13
Python alapú mobil backend GAE felett by Gyuri Horak

Transcript of Python alapu mobil backend

Page 1: Python alapu mobil backend

Python alapú mobil backend GAE felett

by Gyuri Horak

Page 2: Python alapu mobil backend

GAE Python

● Python 2.7● WSGI, webapp2● db, memcache, cloud-sql, url fetch,

blobstore, users, unit testing ...● django (django-nonrel, django+cloud-sql)

Page 3: Python alapu mobil backend

Példaprojekt

● backend iOS, Android és weboldal számára● REST/JSON

Page 5: Python alapu mobil backend

JSONView

● Flask.views.MethodView

● success és failure metódusok● X-Location header● néhány Exception kezelése

Page 6: Python alapu mobil backend

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

Page 7: Python alapu mobil backend

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)

Page 8: Python alapu mobil backend

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()

Page 9: Python alapu mobil backend

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

Page 10: Python alapu mobil backend

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ó

Page 11: Python alapu mobil backend

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

Page 12: Python alapu mobil backend

Képkezelés

● blobstore○ create_upload_url()○ files API (experimental)

● images ~ PIL

● get_serving_url()○ lehet, hogy nincs kész

Page 13: Python alapu mobil backend

Geomodel

● db.GeoPt(lat, lon)● geocells● bounding box és proximity lekérdezéslocalmind is ezt használja clustering megoldásként