小编Pav*_*pin的帖子

设置后,SQLAlchemy急切/加入的加载是否可以被抑制?

我有一个案例,大多数情况下,对象之间的关系是这样的,即预先配置对关系的热切(加入)负载是有意义的.但是现在我遇到了一种我真的不希望完成负载的情况.

我应该从关系中删除已加入的加载并更改所有相关查询以在查询位置(ick)加入,还是有某种方法可以在查询设置后抑制查询中的急切加载?

下面是一个示例,其中已在User-> Address关系上设置了预先加载.程序末尾的查询是否可以配置为不急于加载?

import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy.orm as orm

##Set up SQLAlchemy for declarative use with Sqlite...
engine = sa.create_engine("sqlite://", echo = True)
DeclarativeBase = declarative_base()
Session = orm.sessionmaker(bind = engine)

class User(DeclarativeBase):
    __tablename__ = "users"
    id = sa.Column(sa.Integer, primary_key = True, autoincrement = True)
    name = sa.Column(sa.String, unique = True)
    addresses = orm.relationship("Address",
                                 lazy = "joined", #EAGER LOAD CONFIG IS HERE
                                 )
    def __init__(self, Name):
        self.name = Name

class Address(DeclarativeBase):
    __tablename__ = …
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy lazy-loading eager-loading

15
推荐指数
2
解决办法
5962
查看次数

如何在SQLAlchemy的引擎SQL方言中使用DDL生成文件?

假设我engine指向MySQL数据库:

engine = create_engine('mysql://arthurdent:answer42@localhost/dtdb', echo=True)
Run Code Online (Sandbox Code Playgroud)

我可以通过以下方式填充dtdb表格,FK等:

metadata.create_all(engine)
Run Code Online (Sandbox Code Playgroud)

有没有一种简单的方法来生成包含所有DDL语句的SQL文件,而不是实际应用这些DDL语句dtdb

到目前为止,我已经采取了捕获SQLAlchemy日志输出echo=True,并手动编辑它.但那太痛苦了.

看起来SA有相当复杂的模式管理API,但我还没有看到简单地将模式定义作为文本流式传输的示例.

python sqlalchemy

8
推荐指数
1
解决办法
4049
查看次数

如何在Python中编写有效的类装饰器?

我刚刚编写了一个类装饰器,如下所示,试图为目标类中的每个方法添加调试支持:

import unittest
import inspect

def Debug(targetCls):
   for name, func in inspect.getmembers(targetCls, inspect.ismethod):
      def wrapper(*args, **kwargs):
         print ("Start debug support for %s.%s()" % (targetCls.__name__, name));
         result = func(*args, **kwargs)
         return result
      setattr(targetCls, name, wrapper)
   return targetCls

@Debug
class MyTestClass:
   def TestMethod1(self):
      print 'TestMethod1'

   def TestMethod2(self):
      print 'TestMethod2'

class Test(unittest.TestCase):

   def testName(self):
      for name, func in inspect.getmembers(MyTestClass, inspect.ismethod):
         print name, func

      print '~~~~~~~~~~~~~~~~~~~~~~~~~~'
      testCls = MyTestClass()

      testCls.TestMethod1()
      testCls.TestMethod2()


if __name__ == "__main__":
   #import sys;sys.argv = ['', 'Test.testName']
   unittest.main()
Run Code Online (Sandbox Code Playgroud)

运行上面的代码,结果是:

Finding files... …
Run Code Online (Sandbox Code Playgroud)

python closures scope class decorator

6
推荐指数
1
解决办法
749
查看次数

如何从原始HTTP请求字节流构造webob.Request或WSGI'environ'字典?

假设我有一个包含以下内容的字节流:

POST /mum/ble?q=huh
Content-Length: 18
Content-Type: application/json; charset="utf-8"
Host: localhost:80

["do", "re", "mi"]

有没有办法从中产生WSGI风格的"环境"字典?

希望,我忽略了一个简单的答案,并且它与相反的操作一样容易实现.考虑:

>>> import json
>>> from webob import Request
>>> r = Request.blank('/mum/ble?q=huh')
>>> r.method = 'POST'
>>> r.content_type = 'application/json'
>>> r.charset = 'utf-8'
>>> r.body = json.dumps(['do', 're', 'mi'])
>>> print str(r) # Request's __str__ method gives raw HTTP bytes back!
Run Code Online (Sandbox Code Playgroud)
POST /mum/ble?q=huh
Content-Length: 18
Content-Type: application/json; charset="utf-8"
Host: localhost:80

["do", "re", "mi"]

python wsgi webob

3
推荐指数
1
解决办法
1707
查看次数