访问类代码 [英] accessing a classes code

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

问题描述

我有一组Python类,用于表示振动建模的结构模型中的元素(有点像FEA)。该模型的一些

参数最初是未知的,我做了一些系统

标识来确定参数。在我确定这些

未知参数后,我想将它们替换回

模型并将模型保存为新的python类。为此,我认为

每个元素都需要能够在代码中读取其__init__

方法,进行替换,然后编写新的__init__方法

到一个定义带有现在已知参数的新类的文件。


有没有办法让Python实例访问自己的代码

(特别是__init__方法)?如果有,是否有一种干净的方式将修改后的代码写回文件?我假设如果我将
可以将代码作为字符串列表获取,我可以将其轻松输出到文件




我很想读取代码并编写一些Python脚本

来解析它以获取__init__方法,但这似乎是

重新发明轮子。


谢谢,


瑞安

解决方案

Ryan Krauss < RY ******* @ gmail.com>在留言中写道

新闻:ma ************************************ *** @ pyt hon.org ...

======================

我有一组Python类,用于表示振动建模的结构模型中的元素(有点像FEA)。该模型的一些

参数最初是未知的,我做了一些系统

标识来确定参数。在我确定这些

未知参数后,我想将它们替换回

模型并将模型保存为新的python类。为此,我认为

每个元素都需要能够在代码中读取其__init__

方法,进行替换,然后编写新的__init__方法

到一个定义带有现在已知参数的新类的文件。


有没有办法让Python实例访问自己的代码

(特别是__init__方法)?如果有,是否有一种干净的方式将修改后的代码写回文件?我假设如果我可以将代码作为字符串列表获取,我可以很容易地将它输出到文件



= =====================


你有可能想出一个不那么hacky的设计,比如创建一个

你的一个基类的子类?如:
class BaseClass(object):

def __init __(self):


打印做共同的基本功能


类SpecialCoolClass(BaseClass):

def __init __(self,specialArg1,coolArg2 ):

#调用常见的初始化内容

#(比提取源代码行简单得多,并且用b / b来修改它们)

super(SpecialCoolClass,self).__ init __()


#现在用额外的init args做特殊/酷的东西

print"但是这真是特别/很酷!"

打印specialArg1

打印coolArg2


bc = BaseClass()

scc = SpecialCoolClass(" Grabthar''s Hammer",6.02e23)


打印:

-------- -

做共同的基本功能

做共同的基本功能

但这真的很特别/很酷!

Grabthar的锤子

6.02e + 023

如果你仍然坚持生成代码,至少现在你可以专注于
$ b $关注如何生成特别酷的课程,而不是从运行类中提取源代码,而不是那么多。


- Paul



Ryan Krauss写道:

我有一组Python类来表示结构中的元素
振动建模模型(有点像FEA)。模型的一些参数最初是未知的,我做了一些系统识别来确定参数。在确定这些未知参数后,我想将它们替换回
模型并将模型保存为新的python类。为此,我认为每个元素都需要能够在代码中读取其__init__
方法,进行替换,然后将新的__init__方法写入定义新文件的文件中。使用现在已知参数的类。

有没有办法让Python实例访问自己的代码
(尤其是__init__方法)?如果有,是否有一种干净的方法将修改后的代码写回文件?我假设如果我可以将代码作为字符串列表获取,我可以轻松地将其输出到文件中。

我很想读取代码并写一个小的Python脚本来解析它以获得__init__方法,但这似乎是重新发明轮子。




使用词典对于那些参数,并在你的实例上设置它们。


类Foo(对象):


def __init __(self,** unknown_params) :

表示密钥,其值为unknown_params:

setattr(自我,密钥,值)

HTH,


Diez


我认为这很像我打算做的,除了新的

类将动态生成并且在我将它们写入文件之前,我想要指定新的默认值

。但是我怎么做

那个?


Ryan


2006年4月19日,Paul McGuire< ; PT *** @ austin.rr._bogus_.com>写道:

" Ryan Krauss" < RY ******* @ gmail.com>在消息中写道
新闻:ma *************************************** @ pyt hon.org ...
======================
我有一组Python类来表示结构中的元素
振动建模模型(有点像FEA)。模型的一些参数最初是未知的,我做了一些系统识别来确定参数。在确定这些未知参数后,我想将它们替换回
模型并将模型保存为新的python类。为此,我认为每个元素都需要能够在代码中读取其__init__
方法,进行替换,然后将新的__init__方法写入定义新文件的文件中。使用现在已知参数的类。

有没有办法让Python实例访问自己的代码
(尤其是__init__方法)?如果有,是否有一种干净的方法将修改后的代码写回文件?我假设如果我可以将代码作为字符串列表获取,我可以很容易地将它输出到文件中。
============= =========

你有没有机会想出一个不那么hacky的设计,比如创建一个基类的
子类?如下所示:

类BaseClass(对象):
def __init __(自我):
#在这里做共同基类的东西
打印做共同的基本功能

类SpecialCoolClass(BaseClass):
def __init __(self,specialArg1,coolArg2):
#调用常用的初始化东西
#(比提取源代码行简单得多)代码和
#与它们混淆)
super(SpecialCoolClass,self).__ init __()

#now做特殊/酷的东西,带有额外的init args
print" ;但这真的很特别/酷!打印specialArg1
打印coolArg2

bc = BaseClass()
scc = SpecialCoolClass(" Grabthar''s Hammer",6.02e23)

打印:
----------
做共同的基本功能
做共同的基本功能
但这真的很特别/很酷!
Grabthar'的锤子
6.02e + 023
如果你仍然坚持生成代码,至少现在你可以专注于如何生成特别酷的类,而不是那么多关于提取源代码来自跑步课程的代码。

- 保罗

-
http://mail.python.org/mailman/listinfo/python-list



I have a set of Python classes that represent elements in a structural
model for vibration modeling (sort of like FEA). Some of the
parameters of the model are initially unknown and I do some system
identification to determine the parameters. After I determine these
unknown parameters, I would like to substitute them back into the
model and save the model as a new python class. To do this, I think
each element needs to be able to read in the code for its __init__
method, make the substitutions and then write the new __init__ method
to a file defining a new class with the now known parameters.

Is there a way for a Python instance to access its own code
(especially the __init__ method)? And if there is, is there a clean
way to write the modified code back to a file? I assume that if I
can get the code as a list of strings, I can output it to a file
easily enough.

I am tempted to just read in the code and write a little Python script
to parse it to get me the __init__ methods, but that seems like
reinventing the wheel.

Thanks,

Ryan

解决方案

"Ryan Krauss" <ry*******@gmail.com> wrote in message
news:ma***************************************@pyt hon.org...
======================
I have a set of Python classes that represent elements in a structural
model for vibration modeling (sort of like FEA). Some of the
parameters of the model are initially unknown and I do some system
identification to determine the parameters. After I determine these
unknown parameters, I would like to substitute them back into the
model and save the model as a new python class. To do this, I think
each element needs to be able to read in the code for its __init__
method, make the substitutions and then write the new __init__ method
to a file defining a new class with the now known parameters.

Is there a way for a Python instance to access its own code
(especially the __init__ method)? And if there is, is there a clean
way to write the modified code back to a file? I assume that if I
can get the code as a list of strings, I can output it to a file
easily enough.
======================

Any chance you could come up with a less hacky design, such as creating a
sub-class of one of your base classes? As in:

class BaseClass(object):
def __init__(self):
# do common base class stuff here
print "doing common base functionality"

class SpecialCoolClass(BaseClass):
def __init__(self,specialArg1, coolArg2):
# invoke common initialization stuff
# (much simpler than extracting lines of source code and
# mucking with them)
super(SpecialCoolClass,self).__init__()

# now do special/cool stuff with additional init args
print "but this is really special/cool!"
print specialArg1
print coolArg2

bc = BaseClass()
scc = SpecialCoolClass("Grabthar''s Hammer", 6.02e23)

Prints:
----------
doing common base functionality
doing common base functionality
but this is really special/cool!
Grabthar''s Hammer
6.02e+023

If you''re still stuck on generating code, at least now you can just focus
your attention on how to generate your special-cool classes, and not so much
on extracting source code from running classes.

-- Paul



Ryan Krauss wrote:

I have a set of Python classes that represent elements in a structural
model for vibration modeling (sort of like FEA). Some of the
parameters of the model are initially unknown and I do some system
identification to determine the parameters. After I determine these
unknown parameters, I would like to substitute them back into the
model and save the model as a new python class. To do this, I think
each element needs to be able to read in the code for its __init__
method, make the substitutions and then write the new __init__ method
to a file defining a new class with the now known parameters.

Is there a way for a Python instance to access its own code
(especially the __init__ method)? And if there is, is there a clean
way to write the modified code back to a file? I assume that if I
can get the code as a list of strings, I can output it to a file
easily enough.

I am tempted to just read in the code and write a little Python script
to parse it to get me the __init__ methods, but that seems like
reinventing the wheel.



Use dictionaries for those parameters, and set them on your instances.

class Foo(object):

def __init__(self, **unknown_params):
for key, value in unknown_params:
setattr(self, key, value)
HTH,

Diez


I think this is a lot like I am planning to do, except that the new
classes will be dynamically generated and will have new default values
that I want to specify before I write them to a file. But how do I do
that?

Ryan

On 4/19/06, Paul McGuire <pt***@austin.rr._bogus_.com> wrote:

"Ryan Krauss" <ry*******@gmail.com> wrote in message
news:ma***************************************@pyt hon.org...
======================
I have a set of Python classes that represent elements in a structural
model for vibration modeling (sort of like FEA). Some of the
parameters of the model are initially unknown and I do some system
identification to determine the parameters. After I determine these
unknown parameters, I would like to substitute them back into the
model and save the model as a new python class. To do this, I think
each element needs to be able to read in the code for its __init__
method, make the substitutions and then write the new __init__ method
to a file defining a new class with the now known parameters.

Is there a way for a Python instance to access its own code
(especially the __init__ method)? And if there is, is there a clean
way to write the modified code back to a file? I assume that if I
can get the code as a list of strings, I can output it to a file
easily enough.
======================

Any chance you could come up with a less hacky design, such as creating a
sub-class of one of your base classes? As in:

class BaseClass(object):
def __init__(self):
# do common base class stuff here
print "doing common base functionality"

class SpecialCoolClass(BaseClass):
def __init__(self,specialArg1, coolArg2):
# invoke common initialization stuff
# (much simpler than extracting lines of source code and
# mucking with them)
super(SpecialCoolClass,self).__init__()

# now do special/cool stuff with additional init args
print "but this is really special/cool!"
print specialArg1
print coolArg2

bc = BaseClass()
scc = SpecialCoolClass("Grabthar''s Hammer", 6.02e23)

Prints:
----------
doing common base functionality
doing common base functionality
but this is really special/cool!
Grabthar''s Hammer
6.02e+023

If you''re still stuck on generating code, at least now you can just focus
your attention on how to generate your special-cool classes, and not so much
on extracting source code from running classes.

-- Paul


--
http://mail.python.org/mailman/listinfo/python-list



这篇关于访问类代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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