元编程的局限性 [英] Limits of Metaprogramming

查看:79
本文介绍了元编程的局限性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个有趣的问题,我希望可以通过

元编程来解决,但我不知道Python支持代码的程度是多少?b $ b代(我不知道我是否采取了正确的方法

...也就是为什么我要问在这个小组上):


我想编写一个编写/操作状态机的程序。我的想法是我会在.py文件中定义状态以及它们的

转换和其他元数据。然后我可以写一套程序

,以有趣的方式处理这些状态,例如

生成图表和文档,测试套件,做验证和

也可以编辑它们。这些程序将导入.py文件并对模块的类使用

内省(状态将被建模为

类)。例如,一个文档工具可以使用graphviz来绘制

带有转换表和文档字符串的声明图...


我的问题是我不是''知道是否可以编辑这些状态

然后将它们写回.py。首先,如果我的编辑工具是创建一个新状态,我想创建一个类(使用类型)和

以某种方式将它附加到导入的状态模块。关于这是否可行,我找不到

的信息。其次,我想

操纵类,然后使用inspect模块获取源

并将其写回文件。现在我很确定检查模块将只需要编写原始.py文件中的源代码。

是否可以用

泛型方式为类或其他python对象生成代码?或者我是否必须咬紧牙关并写入自定义的

文件并从中生成代码(如使用XML的glade)?或者是否有
另一个带有模板的解决方案,我正在寻找它?


在内存中编写类,编辑属性,添加方法
然后将它们序列化为代码在我的脑海中具有优雅性和

希望简化事情。结果将直接由解释器本身执行,并可由python

开发人员编辑。但是我担心我可能需要一个中间符号/格式

来存储。任何建议/提示都会非常有用。


谢谢,

Paul

Hi all,

I have an interesting problem that I''m hoping can be solved with
metaprogramming, but I don''t know how far Python supports code
generation (and I don''t know if I''m taking the correct approach
either... hence why I''m asking on this group):

I''d like to write a program that writes/manipulates a statemachine. My
idea was that I would define states in a .py file along with their
transitions and other metadata. I could then write a suite of programs
that would manipulate these states in interesting ways, such as
generate diagrams and documentation, test suites, do verifications and
also edit them. These programs would import the .py file and use
introspection upon the module''s classes (states would be modelled as
classes). For example, a documentation tool could use graphviz to draw
the statediagram with the transition table and docstring...

My problem is that I don''t know if it''s possible to edit these states
and then write them back to .py. Firstly, if my editing tool was to
create a new state, I would want to create the class (using type) and
attach it to the imported state module somehow. I can''t find
information on whether this is possible. Secondly, I''d like to
manipulate the class and then use the inspect module to get the source
and write it back to file. Now I''m pretty sure the inspect module will
only write the source code as found in the original .py file. Is there
a way to generate the code for a class or other python objects in a
generic way? Or will I have to bite the bullet and write to custom
file and generate code from that (like glade with XML)? Or is there
another solution with templates that I''m over looking?

Having the classes in memory, editing their attributes, adding methods
and then serialising them to code has an elegance to it in my mind and
would hopefully simplify things. The result would be directly
executable by the interpreter itself and editable by python
developers. But I fear that I may need an intermediary notation/format
for storage. Any advice/hints would be very useful.

Thanks,
Paul

推荐答案

您可以编写一个由状态组成的类,然后使用pickle

模块将其序列化为磁盘。
You could write a class composed of states and then use the pickle
module to serialize it to disk.


8月4日, 12:55,Jeff< jeffo ... @ gmail.comwrote:
On 4 Aug, 12:55, Jeff <jeffo...@gmail.comwrote:

你可以编写一个由状态组成的类然后使用pickle

模块将其序列化为磁盘。
You could write a class composed of states and then use the pickle
module to serialize it to disk.



谢谢杰夫。


我想这是我的中间格式!

Thanks Jeff.

I guess this is my intermediary format!

2008年8月4日星期一,Wilson写道:
On Mon, 4 Aug 2008, Wilson wrote:

大家好,
Hi all,



你好,


我不确定我的评论是否会对你有用,但在这里它会发生。

Howdy,

I am not sure if my remarks will be of any use for you, but here it goes.


I有一个有趣的问题,我希望可以通过

元编程来解决,但我不知道Python支持代码的程度是多少?b $ b代(并且我不知道)不知道我是不是采取了正确的方法

...因此为什么我要问这个小组):


我'我想编写一个编写/操作状态机的程序。我的想法是我会在.py文件中定义状态以及它们的

转换和其他元数据。然后我可以写一套程序

,以有趣的方式处理这些状态,例如

生成图表和文档,测试套件,做验证和

也可以编辑它们。这些程序将导入.py文件并对模块的类使用

内省(状态将被建模为

类)。例如,一个文档工具可以使用graphviz来绘制

带有转换表和docstring的声明图...
I have an interesting problem that I''m hoping can be solved with
metaprogramming, but I don''t know how far Python supports code
generation (and I don''t know if I''m taking the correct approach
either... hence why I''m asking on this group):

I''d like to write a program that writes/manipulates a statemachine. My
idea was that I would define states in a .py file along with their
transitions and other metadata. I could then write a suite of programs
that would manipulate these states in interesting ways, such as
generate diagrams and documentation, test suites, do verifications and
also edit them. These programs would import the .py file and use
introspection upon the module''s classes (states would be modelled as
classes). For example, a documentation tool could use graphviz to draw
the statediagram with the transition table and docstring...



也许你实际上两个问题。


一个问题是将源代码处理成有用的东西,但

不可执行。所以它不一定需要元编程来解决,只需要一些代码理解即可。 (比如得到一个python语法,

lex / yacc并阅读很多文档如何使用它们。)

Maybe you have actually two problems.

One problem is processing source code into something useful, but
not executable. So it does not necessarily need metaprogramming to be
solved, just some "code comprehension" (like get a python grammar,
lex/yacc and read a lot of docs how to use them).


我的问题是我不知道是否可以编辑这些状态

然后将它们写回.py。首先,如果我的编辑工具是创建一个新状态,我想创建一个类(使用类型)和

以某种方式将它附加到导入的状态模块。关于这是否可行,我找不到

的信息。其次,我想

操纵类,然后使用inspect模块获取源

并将其写回文件。现在我很确定检查模块将只需要编写原始.py文件中的源代码。

是否可以用

泛型方式为类或其他python对象生成代码?或者我是否必须咬紧牙关并写入自定义的

文件并从中生成代码(如使用XML的glade)?或者是否有另外一个带有模板的解决方案
我在看?
My problem is that I don''t know if it''s possible to edit these states
and then write them back to .py. Firstly, if my editing tool was to
create a new state, I would want to create the class (using type) and
attach it to the imported state module somehow. I can''t find
information on whether this is possible. Secondly, I''d like to
manipulate the class and then use the inspect module to get the source
and write it back to file. Now I''m pretty sure the inspect module will
only write the source code as found in the original .py file. Is there
a way to generate the code for a class or other python objects in a
generic way? Or will I have to bite the bullet and write to custom
file and generate code from that (like glade with XML)? Or is there
another solution with templates that I''m over looking?



嗯,一切用图灵完整的语言是可行的:-)。然而,

这一个问题 - 代码生成和动态使用 - 可能更好

用其他语言解决,也许吧?


并不是说我想劝阻你(或其他任何人)使用Python,但是上次我检查了事情的状态是好的
(几年前好几年,我是>
想用Python中的元编程解决一些与数学相关的问题)

如果我学习了Scheme,我决定做得更好。至少在Scheme中,

程序结构如此简单,它只是一个列表(或它们的序列),

因此操作代码更容易恕我直言。 />

我的意思是,即使从理论上讲,图灵全班的每一种语言都是相同的(b
)一个人可以做同样的东西另一个),实际上他们并不等于
。因此,从我的观点来看,用一种终极的

语言做所有事情都不是很明智。


这并不意味着你应该立即去

http://en.wikipedia.org/wiki/方案_... mming_language


但您可以这样做,然后自己做出明智的选择。为了获得更好的信息,您可以通过谷歌获取更多资料 - 介绍,

意见,比较和一些第一手经验。


另一方面,写metafiles和他们的口译员 - 呃...

:-)。我在某处读过,有些程序员拒绝使用Scheme

(或其他一些Lisp)来解决他们的问题并最终写出他们自己的Lisp-like
(语言|文件格式)和解释器。所以

你最好看自己。如果这种努力比完成并去更具有价值,那么你可能会发现自己处于重新发明轮子的状态,就像你的项目越来越多

复杂。如果发生这种情况,您可以使用现有的轮子和

节省一些时间来考虑更好的事情。

Well, "everything" is doable in a Turing-complete language :-). However,
this one problem - code generation and using it on the fly - may be better
solved with some other language, perhaps?

Not that I want to dissuade you (or anyone else) from using Python, but
last time I checked the state of things (was a good couple of years ago, I
wanted to solve some math-related problem with metaprogramming in Python)
I decided I would do better if I learned Scheme. At least in Scheme,
program structure is so simple, it is just a list (or a sequence of them),
so manipulating code is IMHO much easier.

I mean, even if in theory every language in Turing-complete class is equal
to each other (one can do the same kind of stuff in one as in the other),
in practice they are not equal at all. So doing everything in one ultimate
language of choice is not very wise from my point of view.

It does not mean you should immediately go to

http://en.wikipedia.org/wiki/Scheme_...mming_language)

but you may do so, and then make an informed choice by yourself. To be
better informed, you may also google for more material - introductions,
opinions, comparisons and some first-hand experience.

On the other hand, writing "metafiles" and interpreters for them - ugh...
:-). I have read somewhere, that some programmers resist using Scheme
(or some other Lisp) for solving their problem and end up with writing
their own Lisp-like (language|file format) and interpreter(s) for it. So
you better watch yourself. If this effort is going to be something more
permament than "done and go", then you will probably find yourself in the
situation of reinventing the wheel, as your project goes more and more
complex. If this is going to happen, you might use existing wheels and
save some time for thinking on something better.


让课程进入内存,编辑它们的属性,添加方法

然后将它们序列化为代码在我的脑海里有一种优雅,并且

希望能简化一些事情。结果将直接由解释器本身执行,并可由python

开发人员编辑。但是我担心我可能需要一个中间符号/格式

来存储。任何建议/提示都非常有用。
Having the classes in memory, editing their attributes, adding methods
and then serialising them to code has an elegance to it in my mind and
would hopefully simplify things. The result would be directly
executable by the interpreter itself and editable by python
developers. But I fear that I may need an intermediary notation/format
for storage. Any advice/hints would be very useful.



是的,这种思维方式有一些优雅,至少对我而言。


我(间接)上面说过,我不是最新的Python

能力(我仍然使用Python做很多事情,我只是不要过度使用

for it,that它好像不太好)。我希望有人知道

更好并介入以证明我的错误 - 学习

新东西总是好的。另外,如果你只有一个问题,那么学习新的

语言因为它有点太多(或者可能不是,如果你喜欢它

:-) )。但是如果你计划在

未来做类似或更困难的事情,那么了解Scheme可能对你有好处。即使你选择不使用它,你也会知道可以用它做什么,所以这个知识

可以提供一些现成的模板。


是的,在Lisp家族中还有一些其他语言,但我认为

如果你不知道任何一种语言,那么Scheme是最好的选择。它很小,很明确(我的意思是,它有一些真正的规格,

而不是由其实现指定)并且有一个很多

关于它的信息可以很容易理解。并且,最后但是

并非最不重要,它包含了很多额外的

的元编程。首先,PLT的DrScheme看起来不错(恕我直言 - 是的,还有

其他很好看的Scheme实现,但这个可能是最好的

适合初学者) :

http://www.plt-scheme.org/


问候,

Tomasz Rola


-

** AC程序员询问计算机是否具有佛陀的性质。 **

**作为答案,主人做了rm -rif在程序员的家里**

**目录。然后C程序员开悟了... **

** **

** Tomasz Rola mailto:到********* @ bigfoot .com **

Yes, there is something elegant in this way of thinking, at least for me.

As I (indirectly) said above, I am not up to date with recent Python
abilities (I still use Python for a lot of things, I just do not overuse
it for things, that it seems to be not so good at). I hope someone knows
better and steps in to prove me wrong - it is always good to learn
something new. Also, if you have just one problem, then learning a new
language because of it is a bit too much (or maybe not, if you enjoy it
:-) ). But if you plan doing similar or more difficult things in the
future, then knowing Scheme may be good for you. Even if you choose to not
use it, you will be aware of what can be done with it so this knowledge
can provide some ready to use templates.

And yes, there are also some other languages in Lisp family, but I think
Scheme is best choice if you don''t know any of them already. It is quite
small, it is well defined (I mean, it has some real specification,
instead of being "specified by its implementation") and there is a lot of
info about it on the net that could be easily understood. And, last but
not least, it has metaprogramming included with nice bunch of additional
stuff. For a start, PLT''s DrScheme looks nice (IMHO - yes, there are
other nice looking Scheme implementations but this one is probably best
fitted for a beginner):

http://www.plt-scheme.org/

Regards,
Tomasz Rola

--
** A C programmer asked whether computer had Buddha''s nature. **
** As the answer, master did "rm -rif" on the programmer''s home **
** directory. And then the C programmer became enlightened... **
** **
** Tomasz Rola mailto:to*********@bigfoot.com **


这篇关于元编程的局限性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆