自动生成Python类文件 [英] Automatic Generation of Python Class Files

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

问题描述

我在考虑一种可以让Python类文件编写的方法,而不是那么痛苦。我正在考虑一个Ptyhon脚本,它读取一个

文件,其中包含属性名称和方法名称列表,然后生成

a骨架类文件。


我甚至考虑为doc

字符串和epydoc标签自动生成shell。


是否存在执行此类操作的脚本?如果没有,我会试着想出一些东西。如果我成功了,我会在GPL下发布代码

并将报告回列表。


但是,我想我会先检查一下,这样我才不会重新发明

方向盘。


谢谢,


Scott Huey

I was thinking of a way I could make writing Python Class Files a
little less painful. I was considering a Ptyhon script that read a
file with a list of property names and method names and then generated
a skeleton class file.

I was even thinking of automatically generating the shell for doc
strings and epydoc tags.

Is there existing scripts that do something like this? If not, I will
try to come up with something. If I''m sucessful I''ll release the code
under the GPL and will report back to the list.

However, I thought I would check here first so that I don''t reinvent
the wheel.

Thanks,

Scott Huey

推荐答案

Sunburned Surveyoraécrit:
Sunburned Surveyor a écrit :

我在考虑我可以编写Python类文件的方法
I was thinking of a way I could make writing Python Class Files



什么是Python类文件 ?

What''s a "Python Class File" ?


a

少点痛苦。
a
little less painful.



如果你发现用Python编写课程痛苦,那么要么你没有使用任何主流语言的b $ b b经验,要么就是你在做什么错误。

If you find writing classes in Python "painful", then either you have no
experience with any mainstream language or you are doing something wrong.


我正在考虑一个Ptyhon脚本,它读取一个带有属性名称和方法名称列表的

文件,然后生成

a骨架类文件。
I was considering a Ptyhon script that read a
file with a list of property names and method names and then generated
a skeleton class file.



再一次,没有类文件这样的东西。在Python中。模块

通常包含几个(类,函数等)定义。而且因为在Python中有很少的样板代码,我不知道你的

系统会有什么好处 - 直接编写代码不会比写带有属性名称列表的文件和

方法名称更多的时间。


我的2仙......

Once again, there''s no such thing as a "class file" in Python. A module
usually contains several (classes, functions etc) definitions. And since
there''s very few boilerplate code in Python, I don''t see what your
system would be good for - directly writing the code will not take much
more time than writing the "file with a list of property names and
method names".

My 2 cents...


2007年10月22日星期一11:05:52 -0700,Sunburned Surveyor写道:
On Mon, 22 Oct 2007 11:05:52 -0700, Sunburned Surveyor wrote:

10月22日上午10:26,Chris Mellon < arka ... @ gmail.comwrote:


你写道:"不能想到你为什么要这样做的一个原因

这样做,

因为你的方法和属性名称列表而已。就像输入实际的python代码那样简单。


我不觉得我明白这是多少相同的金额

打字。考虑以下示例,该示例将从输入文本文件生成Monster

类文件(我在生成的类文件中只做了一个属性和方法

。):
On Oct 22, 10:26 am, "Chris Mellon" <arka...@gmail.comwrote:

You wrote: " can''t think of a single reason why you would ever want to
do this,
since your "list of method and property names" would be just as
verbose as just typing the actual python code."

I don''t think I understand how this would be the same amount of
typing. Consider the following example that would generate a Monster
class file from an input text file (I only did one property and method
in generated class file.):



真的是一个怪物类。

Really a "monster class".


输入文本文件的内容:

[姓名]

消防呼吸龙


[属性]

力量

伤心

耐力


[方法]

eatMaiden argMaiden

fightKnight argKnight


生成的Python类文件:


def class FireBreathingDragon:


def getStrength(self):

"""

Docstring就在这里。


@return

@rtype

"""

返回self.strength


def setStrength(self,argStrength):

"""

Docstring就在这里。


@param argStrength

@ptype

"""

返回self.strength


def eatMaiden(self,argMaiden):

"""

Docstring就在这里。


@param argMaiden

@ptype

"""
Contents of input text file:

[Name]
Fire Breathing Dragon

[Properties]
Strength
Scariness
Endurance

[Methods]
eatMaiden argMaiden
fightKnight argKnight

Generated Python Class File:

def class FireBreathingDragon:

def getStrength(self):
"""
Docstring goes here.

@return
@rtype
"""
return self.strength

def setStrength(self, argStrength):
"""
Docstring goes here.

@param argStrength
@ptype
"""
return self.strength

def eatMaiden(self, argMaiden):
"""
Docstring goes here.

@param argMaiden
@ptype
"""



好​​吧这可以写成:


class FireBreathingDragon(object):

def __init __(自我,力量):

self.strength =力量

self.scariness = scariness

self.endurance =耐力


def eat_maiden(自我,少女):

通过


def fight_knight(自我骑士):

pass


即使输入文件中的所有内容都比你生成的

生成的课程要短。


我遗漏了文档,因为如果

甚至没有更好的话,没有文档和你的存根一样有价值。这样你至少看不到真正的文档和

像'pylint`这样的工具可以指出丢失的文档。


编辑很好模板支持少量

样板左边。


以防你想要保护默认的getter和setter:read up <属性上的
,即`property()`函数,以及这个关于Python不是Java的
组中的讨论。


Ciao,

Marc''BlackJack''Rintsch

Okay this could be written as:

class FireBreathingDragon(object):
def __init__(self, strength):
self.strength = strength
self.scariness = scariness
self.endurance = endurance

def eat_maiden(self, maiden):
pass

def fight_knight(self knight):
pass

Even with all the stuff from your input file it''s shorter than your
generated class.

I left out the docs because having no docs is as valuable as your stubs if
not even better. This way you at least see that there is no real docs and
tools like `pylint` can point out the missing docs.

There are editors with good template support for the small amount of
boilerplate that''s left.

Just in case you want to defend the default getters and setters: read up
on properties, i.e. the `property()` function, and the discussions in this
group about Python not being Java.

Ciao,
Marc ''BlackJack'' Rintsch


Sunburned Surveyor写道:
Sunburned Surveyor wrote:

输入文本文件的内容:


[姓名]

消防呼吸龙


[属性]

力量

疤痕

耐力


[方法]

eatMaiden argMaiden

fightKnight argKnight


生成的Python类文件:


def class FireBre athingDragon:


def getStrength(self):

"""

Docstring在这里。


@return

@rtype

"""

返回self.strength


def setStrength(self,argStrength):

"""

Docstring就在这里。


@param argStrength

@ptype

"""

return self.strength

>
def eatMaiden(self,argMaiden):

"""

Docstring到这里。


@param argMaiden

@ptype

"""
Contents of input text file:

[Name]
Fire Breathing Dragon

[Properties]
Strength
Scariness
Endurance

[Methods]
eatMaiden argMaiden
fightKnight argKnight

Generated Python Class File:

def class FireBreathingDragon:

def getStrength(self):
"""
Docstring goes here.

@return
@rtype
"""
return self.strength

def setStrength(self, argStrength):
"""
Docstring goes here.

@param argStrength
@ptype
"""
return self.strength

def eatMaiden(self, argMaiden):
"""
Docstring goes here.

@param argMaiden
@ptype
"""



这应该生成::


#从对象继承。没有理由创建旧式的类。

类FireBreathingDragon(对象):


#Python不是Java。你不需要getter和setter。

#使用公共属性。如果您以后决定使用

#需要不同的属性,您可以随时使用property()

#来使您的getter和setter看起来像公共属性

def __init __(自我,强壮,恐惧,耐力):

self.strength =力量

self.scariness = scariness

self.endurance =耐力

STeVe

This should instead generate::

# Inherit from object. There''s no reason to create old-style classes.
class FireBreathingDragon(object):

# Python is not Java. You don''t need getters and setters.
# Use public attributes. If you ever decide later that you
# need different attributes, you can always use property()
# to make your getters and setters look like public attributes
def __init__(self, stregth, scariness, endurance):
self.strength = strength
self.scariness = scariness
self.endurance = endurance
STeVe


这篇关于自动生成Python类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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