English 中文(简体)
储存以后执行守则的最佳方式(原文)
原标题:best way to store code for later execution (python)

我的一些任务存放在b布,供日后执行。 例如,我可以确定发送电子邮件的任务。 并且通过艰巨的任务(结束)。 我寻求最佳方式将编码储存在干.中,以供日后执行。 将其储存在草坪法典的原始载体上,而不是流,但也必须在此储存相对进口。

例如,发送电子邮件时,必须确定这种方式:

s = "from django.core.mail import send_mail send_mail( subj , body , email@box.ru ,[ email1@box.ru ], fail_silently=False)"

以及以后的间隔期:任何想法,以最好的方式或更妥善的方式执行这种任务?

最佳回答

你做些什么是坏的想法,主要是因为你允许执行哪部法典的变异性太大。 一部法规可以做到任何thing,而我猜测,你只想将一些任务留给以后执行。

因此,图列这些任务中的变数(从非方案意义上的变数:变化的事物),并只储存这些变数,或许是功能论点的图象,以及适用于已知职能的关键词词词词词词的字典。

更可取的是,你可以拥有某种集装箱物体,其功能bu缩,并储存功能名称,以与其论点相呼应。 该集装箱可以像你的例子那样简单地作为进口功能如Django ssend_mail的模块。

那么,你就能够照搬你的榜样:

func =  send_mail 
args = ( subj ,  body ,  email@box.ru , [ email1@box.ru ])
kwargs = { fail_silently : False}

my_call = cPickle.dumps((func, args, kwargs)) 

并如此:

func, args, kwargs = cPickle.loads(my_call)

getattr(my_module, func)(*args, **kwargs)
问题回答

为此使用<条码>。 这是最佳办法。

http://celeryproject.org/“rel=“nofollow”http://celeryproject.org/

我根本不使用这一解决办法。 我将为每项任务创造不同的手稿(发送邮件、删除档案等)。 以这种方式穿着代码是 ha。

http://www.ohchr.org。

其中一个例子是为处理人员制定自己的格式。 例如,以这种格式的每条手递:

handlername;arg1;arg2;arg3;arg4

接下来,你用on语读写,并 par。 例如,这将成为一条储存线:

sendmail;nightcracker@nclabs.org;subject;body

Which would be parsed like this:

for line in database:
    handler, *args = line.split(";")
    if handler == "sendmail":
        recipient, subject, body, = args[:3]
        # do stuff
    elif handler == "delfile":
        #etc

我储存合乎逻辑的指挥,并用类似的东西把这些指挥排除在外。

def run_command(cmd):
    fields = map(unescape, cmd.split(";"))
    handlers[fields[0]](fields[1:])

...

@handler("mail")
def mail_handler(address, template):
    import whatever
    ...
    send_mail(address, get_template(template) % user_info, ...)

这样,你就能够灵活地增加处理人员,而不必触及发送者的任何守则,然而,你没有在数据库中填写守则细节,从而使得检查/统计工作更加困难,或只是一些刚刚开始的热固定工作。

为了直接回答你的问题,电子数据确实只有 评价<>/em>的代码才产生结果。 例如:

>>> eval( 1 + 1 )
2

然而,如果你只是想执行守则,可能的话,要执行几条法典,那么你就希望通过在呼唤者名下执行。

>>> exec("x = 5 + 5")
>>> print x
10

请注意,只有可信赖的法典才能通过。 另见执行档案的外来文件。

Having said all that, I agree with other posters that you should find a way to problematically do what you want to do instead of storing arbitrary code. You could, for example, do something like this:

def myMailCommand(...):
    ...

def myOtherCommand(...):
    ...

available_commands = { mail : myMailCommand,
                       other : myOtherCommand}

to_execute = [( mail , (arg1, arg2, arg3)),
              ( other , (arg1, arg2))]

for cmd, args in to_execute:
    available_commands[cmd](*args)

在上述假体编码中,我确定了两种方法。 之后,我有一套用于指挥的字典绘图行动。 然后,我通过一个行动和论点数据结构,并相应地提出适当的论点。 你们有这个想法。





相关问题
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 ]="...