后,我有命名的参数如何获得命名参数顺序调用函数
def sample(a=None, b=None, c=None)
pass
功能如何调用该函数后得到的这些参数的顺序?
sample(b=1, a=1, c=1)
out: ['b', 'a', 'c']
sample(c=1, a=1)
out: ['c', 'a']
sample(a=1, b=1)
out: ['a', 'b']
sample(b=1, a=1)
out: ['b', 'a']
sample(a=1, b=1, c=1)
out: ['a', 'b', 'c']
也许这可以使用装饰或其他方式完成?
更新:
我想为elasticsearch过滤器像SQLAlchemy的过滤器的方式,而是通过命名参数
class ESQuery(object):
def __init__(self, url, index):
pass
def filter(self, _bool=None, _range=None, _not=None, _and=None, _or=None, _type=None, exists=None, query=None):
return self.query_body
包装调用该函数后,我需要查询的正确顺序,像这样http://www.elastic.co/guide/en/elasticsearch/reference/1.5/query-dsl-and-filter.html
我只是不能建立查询,以便保持严格的顺序
es = Someclass(url, index)
es.filter()
我想用命名的参数,人们觉得方便的工具提示使用做
更新2:
我想另谋出路,一条线是不够的,因为不可能是长时间的电话
from api.search_api import ESQuery
es = ESQuery(index='lot', place='etsy_spider', match_all=True)
print es.\
filter(query='{ "tweet": "full text search" }').\
filter(_range='{ "created": { "gte": "now - 1d/d" }}').\
filter(should='{ "term": { "featured": true }},')
也许有关于如何简化elasticsearch中长查询的一些想法?
您可以举个例子说明这些信息会有用吗? – Kevin 2015-04-03 19:50:10
据我所知这是不可能的,因为命名参数在内部作为字典处理,没有命令 – Finwood 2015-04-03 19:53:03
对于简单的情况,一种方法是从调用帧读取函数调用行并解析它使用正则表达式或其他东西,但如果函数调用跨越多行,这将无法正常工作。 – 2015-04-03 19:58:15