python动态创建对象属性_python – 动态创建方法和装饰器,得到错误’functools.partial’对象没有属性’__module__’...

我目前正在使用EndpointsModel为AppEngine上的所有模型创建一个RESTful API.由于它是RESTful的,这些api有很多重复的代码,我想避免

例如:

class Reducer(EndpointsModel):

name = ndb.StringProperty(indexed=False)

@endpoints.api(

name="bigdata",

version="v1",

description="""The BigData API""",

allowed_client_ids=ALLOWED_CLIENT_IDS,

)

class BigDataApi(remote.Service):

@Reducer.method(

path="reducer",

http_method="POST",

name="reducer.insert",

user_required=True,

)

def ReducerInsert(self, obj):

pass

## and GET, POST, PUT, DELETE

## REPEATED for each model

我想让它们变成通用的.所以我尝试给类动态添加方法.

到目前为止我已经尝试过

from functools import partial, wraps

def GenericInsert(self, obj, cls):

obj.owner = endpoints.get_current_user()

obj.put()

return obj

# Ignore GenericDelete, GenericGet, GenericUpdate ...

import types

from functools import partial

def register_rest_api(api_server, endpoint_cls):

name = endpoint_cls.__name__

# create list method

query_method = types.MethodType(

endpoint_cls.query_method(

query_fields=('limit', 'pageToken'),

path="%ss" % name,

http_method="GET",

name="%s.list" % name,

user_required=True

)(partial(GenericList, cls=endpoint_cls)))

setattr(api_server, "%sList", query_method)

# create insert method

# ...

register_rest_api(BigDataApi, Reducer)

但是我有’functools.partial’对象没有属性’__module__’异常.

我认为这是因为在endpoints.method的装饰器和部分之间有一些冲突.但不知道如何避免它.

Traceback (most recent call last):

File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 239, in Handle

handler = _config_handle.add_wsgi_middleware(self._LoadHandler())

File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 298, in _LoadHandler

handler, path, err = LoadObject(self._handler)

File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 84, in LoadObject

obj = __import__(path[0])

File "/Users/Sylvia/gcdc2013/apis.py", line 795, in

register_rest_api(BigDataApi, Reducer)

File "/Users/Sylvia/gcdc2013/apis.py", line 788, in register_rest_api

)(partial(GenericList, cls=endpoint_cls)))

File "/Users/Sylvia/gcdc2013/endpoints_proto_datastore/ndb/model.py", line 1544, in RequestToQueryDecorator

@functools.wraps(api_method)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 33, in update_wrapper

setattr(wrapper, attr, getattr(wrapped, attr))

AttributeError: 'functools.partial' object has no attribute '__module__'

相关文章:

更新

>完成调用堆栈

>让问题更短

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值