用 type 生成的类, 要怎么导出到 xxx.py 文件中
cls = type("ClsName", (object, ), attr) code = xxxGetSource(cls) writeToFile(code, "moduleName/ClsName.py")
django,makemigrations 的时候,需要把 Model 的继承的父类 注册到 migrations 的文件中, 然后, 我的一些 Model 继承的类是动态生成的 譬如:
class XXXModel(GenerateCls({ .... }), models.Model): pass def GenerateCls(....): ... return type(name, bases, attr)
1 ebingtel 2019-07-03 08:40:34 +08:00 ![]() code = xxxGetSource(cls),在里面遍历属性、通过 inspect.getsource 拿到 function 的源码……其他的就是格式化的问题了 |
2 matrix273 2019-07-03 09:08:30 +08:00 via Android ![]() pickle 这个库可以吗?不是很懂。 |