English 中文(简体)
如何为“灰色功能参数”提供额外校正/元数据?
原标题:What s a good way to provide additional decoration/metadata for Python function parameters?
  • 时间:2010-08-31 16:13:12
  •  标签:
  • python

我们再次考虑使用沙捞越(IronPython),但我认为这无关紧要)为另一个控制设备的申请提供某种宏观支持。

我们想写一下在沙尔的相当简单的功能,其中只字不提一些论点,如时间、温度和位置。 不同的功能将提出不同的论据,主要应用将包含用户界面(类似于财产网),使用户能够提供“灰色”功能论点的数值。

因此,例如,功能1可能需要一定的时间和温度,而功能2可能采取立场和几个时期。

我们希望能够动态地建立沙捞越法的用户界面。 容易做的事情是,在模块中找到一份职能清单,(利用检查检查)获得每个职能的理由清单。

然而,仅仅列举理由是不够的——理想的情况是,我们希望能够列入更多关于每一论点的信息,例如,这种说法的类型(高层次的类型——时间、温度等,而不是语言级),或许是一个友好的名称或描述。

因此,问题在于,在职能中增加这种信息,是何等高尚的方法。

我认为的两种可能性是:

  • 利用严格的命名公约进行辩词,然后从姓名(使用假肢)中fer弄。

  • 发明我们自己的美术语言(可能比CSV大得多),并使用我们的元数据显示。

由于沙尔似乎很受欢迎,无法把script打成大片,我想这是一些共同公约的一个解决问题,但我没有能够找到这些公约。

最佳回答

决标是增加功能元数据的良好途径。 添加一个清单,列出对某类财产或某类财产的价值:

def takes(*args):
    def _takes(fcn):
        fcn.params = args
        return fcn
    return _takes

@takes("time", "temp", "time")
def do_stuff(start_time, average_temp, stop_time):
    pass
问题回答

我将使用某种矫正器:

class TypeProtector(object):
    def __init__(self, fun, types):
        self.fun, self.types = fun, types
    def __call__(self, *args, **kwargs)
        # validate args with self.types
        pass
        # run function
        return fun(*args, **kwargs)

def types(*args):
    def decorator(fun):
        # validate args count with fun parameters count
        pass
        # return covered function
        return TypeProtector(fun, args)
    return decorator

@types(Time, Temperature)
def myfunction(foo, bar):
    pass

myfunction( 21:21 ,  32C )
print myfunction.types

http://www.python.org/dev/peps/pep-3107/“rel=“nofollow noreferer”>。

def DoSomething(critical_temp: "temperature", time: "time")
    pass

关于python 2.x,我要使用护法

def my_func(txt):
    """{
    "name": "Justin",
    "age"   :15
    }"""
    pass

并且可以自动在功能标的上划入这种氮。

for f in globals():
    if not hasattr(globals()[f],  __call__ ):
        continue
    try:
        meta = json.loads(globals()[f].__doc__)
    except:
        continue
    for k, v in meta.items():
        setattr(globals()[f], k, v)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签