我正在为公共库制作一个小包装器模块,该库有很多重复,在创建对象之后,可能方法需要相同的数据元素.
我必须在我的包装器类中传递相同的数据,但实际上并不想一遍又一遍地传递相同的东西.所以我想将数据存储在我的包装器类中,如果它不包含在方法中则应用它.但是,如果事情变得毛骨悚然,我希望方法参数覆盖类默认值.这是一个代码片段,说明了我的目标.
class Stackoverflow():
def __init__(self,**kwargs):
self.gen_args = {}
#Optionally add the repeated element to the object
if 'index' in kwargs:
self.gen_args['index'] = kwargs['index']
if 'doc_type' in kwargs:
self.gen_args['doc_type'] = kwargs['doc_type']
#This is where the problem is
def gen_args(fn):
def inner(self,*args,**kwargs):
kwargs.update(self.gen_args)
return fn(*args,**kwargs)
return inner
#There is a bunch of these do_stuffs that require index and doc_type
@gen_args
def do_stuff(self,**kwargs):
print(kwargs['index'])
print(kwargs['doc_type'])
#Just send arguments up with the method
print("CASE A")
a = Stackoverflow()
a.do_stuff(index=1,doc_type=2)
#Add them to the …Run Code Online (Sandbox Code Playgroud) 我正在使用flask-restful构建API.我也使用flask-resfulplus来生成swagger文档.我想返回一个项目字典,其中密钥将根据项目而变化.我的模型看起来像这样:
item = api.model('Item',{
'item':fields.Integer()}) <- This is wrong
ItemModel = api.model('ItemsList', {
'_header': fields.Nested(_header),
'items':fields.Nested(item)
})
Run Code Online (Sandbox Code Playgroud)
请注意,我尝试了一些变体,但似乎没有任何作用; 这只是最新的互动.
我正在寻找的回应是这样的.
{
'_header':{} <-This works fine
'items': {
'item1':5,
'item2':2
}
}
Run Code Online (Sandbox Code Playgroud)
项目字典中的项目将具有项目的不同键和计数作为值.
我已经尝试将item字段设置为field.Raw()并且它工作正常,但它没有显示在swagger文档中.
请注意,我不想返回我正在使用的字典列表.
谢谢
我刚刚开始使用Django,所以这可能是一些愚蠢的事情,但我甚至不确定在这一点上google是什么.
我有一个看起来像这样的方法:
def get_user(self,user):
return Utilities.get_userprofile(user)
Run Code Online (Sandbox Code Playgroud)
该方法如下所示:
@staticmethod
def get_userprofile(user):
return UserProfile.objects.filter(user_auth__username=user)[0]
Run Code Online (Sandbox Code Playgroud)
当我在视图中包含它时,一切都很好.当我编写一个测试用例来使用Utility类中的任何方法时,我得到None:
两个测试用例:
def test_stack_overflow(self):
a = ObjName()
print(a.get_user('admin'))
def test_Utility(self):
print(Utilities.get_user('admin'))
Run Code Online (Sandbox Code Playgroud)
结果:
Creating test database for alias 'default'...
None
..None
.
----------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我为什么这在视图中工作,但不在测试用例内部工作并且不会生成任何错误消息?
谢谢
我正在Django Mezannine和我一起工作,我遇到了一个与jinja有关的奇怪问题.
TemplateSyntaxError at /services/
Could not parse the remainder: '%' from '%'
Request Method: GET
Request URL: http://192.168.1.14/services/
Django Version: 1.8.3
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '%' from '%'
Exception Location: /usr/local/lib/python3.4/dist-packages/django/template/base.py in __init__, line 639
Python Executable: /usr/bin/python3
Python Version: 3.4.3
Run Code Online (Sandbox Code Playgroud)
我的代码如下所示:
{% for image in images %}
{% if loop.index % 3 == 0 %} #this is the line it doesn't like
{{image}}
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
知道这里发生了什么吗? …
使用ansible杀死任务的最佳方法是什么?我有kafka作为常规可执行文件运行,而不是作为服务.当我向它发送一个杀戮时,需要一段时间来包装并关闭.在发送kill命令以确保服务实际停止之后,我希望ansible在继续之前等待.
到目前为止,这就是我所拥有的,我不确定如何可靠地做到这一点.
---
- name: Stopping Kafka
command: "bin/kafka-server-stop.sh config/server.properties"
args:
chdir: "{{base_apps}}/kafka/kafka_2.11-0.9.0.1"
ignore_errors: yes
Run Code Online (Sandbox Code Playgroud)
谢谢