《珍惜想法 尊重规范-陈泊涵.pptx》由会员分享,可在线阅读,更多相关《珍惜想法 尊重规范-陈泊涵.pptx(13页珍藏版)》请在三个皮匠报告上搜索。
1、IDEA FIRST AND BE PYTHONIC IN NEWBORN PYTHON PROJECT.主讲人:西南大学 2023级软件工程 陈泊涵Python is a programming language that lets you work quickly and integrate systems more effectively.www.python.orgcontextlib.asynccontextmanagerasync def db_session()-AsyncContextManagerAsyncSession:async with async_session_ma
2、ker()as session:yield session await mit()进入:session.begin()transaction._aenter_()yield session离开:transaction._aexit_()session._aexit_()await mit()#define the object without orm session#this kind of object can only invoke methods end with rawclass Raw(GenericT):def _init_(self,obj:T):self.object:T=ob
3、j def _getattribute_(self,name):attribute=super()._getattribute_(name)if inspect.ismethod(attribute):if not name.endwith(_raw):log(fORM object type(self.object)invoke a sql-related function(name)without database session.method invoke has been canceled)return#cancel the method invoked return attribut
4、e def _getattr_(self,name):return getattr(self.object,name)class Score(Base):staticmethod async def from_base(info:ScoreBase)-RawScore:pass async def submit_raw(self)-OptionalScore:passasync def require_score(score_id:int,user:User=Depends(require_user):async with database_session()as session:score=
5、await Score.from_id(session,score_id)if score is None:raise APIExceptions.score_not_exist if score.user_id!=user.id:raise APIExceptions.score_not_belongings return scorerouter.patch(/score_id,response_model=ScoreRead)async def patch_score(info:ScoreUpdate,score:Score=Depends(require_score):async wit
6、h database_session()as session:patched_score=await database.partial_update(session,score,info)return patched_scoreutil.create_proxy_methods(Session,:class:_orm.Session,:class:_asyncio.AsyncSession,classmethods=object_session,identity_key,methods=_contains_,_iter_,add,add_all,expire,expire_all,attrib
7、utes=dirty,deleted,new,identity_map,is_active,autoflush,no_autoflush,info,)class AsyncSession(ReversibleProxySession):def add(self,instance:object,_warn:bool=True)-None:return self._proxied.add(instance,_warn=_warn)def add_all(self,instances:Iterableobject)-None:return self._proxied.add_all(instances)