菜鸟说话 [英] Rookie Speaks

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

问题描述

我是一个蟒蛇新秀,有人有建议精简这个

功能吗?在此先感谢.....

def getdata(myurl):

sock = urllib.urlopen(myurl)

xmlSrc =袜子。阅读()

sock.close()


xmldoc = minidom.parseString(xmlSrc)


def getattrs (weatherAttribute):

a = xmldoc.getElementsByTagName(weatherAttribute)

返回[0] .firstChild.data


currtemp = getattrs(" fahrenheit")

currwind = getattrs(" wind")

currdew = getattrs(" dewpoint")

currbarom = getattrs(" relative_humidity")

currhumid = getattrs(" barometric_pressure")

currcondi = getattrs(条件)


print"%13s \t%s \ t%s \ t%s \ t%s \ t% s\t%S" %(currname,currtemp,

currwind,currbarom,currdew,currhumid,currcondi)

解决方案

|因此Spake William S. Perrin现在的历史日期是1月7日星期三

2004 17:37:57 -0600 |

我是蟒蛇菜鸟,有谁有建议精简这个功能?在此先感谢.....




请定义简化在这种情况下。


你的意思是:

更快

更小

更容易阅读




Sam Walters。


-

永远不要忘记万圣节文件。
http://www.opensource.org/halloween/

"""微软今天会把你拖到哪里?

你真的想去那里吗?""


对不起,我猜它有效吗?那就是我给它打了1000次.....


Samuel Walters写道:

|这样Spake William S. Perrin在现在的历史日期1月7日星期三
2004 17:37:57 -0600 |

我是蟒蛇新秀,任何人都有和建议精简这个
功能?在此先感谢.....



请定义简化在这种情况下。

你的意思是:
更快
更小
更容易阅读


山姆沃尔特斯。




|因此Spake William S. Perrin现在的历史日期是1月7日星期三

2004 17 :45:38 -0600 |

对不起,我猜它有效吗?那就是我打了1000次..... .......这里有一个松散的含义。既然你描述了运行它一千次的
,我会假设你的意思是执行速度。


有一句话说过早优化是万恶之源。其中

是另一种说法尝试它,如果它太慢,请找出

。如果它不是太慢,就不要乱用它。所以,尝试在你需要它的上下文中运行它。$没有关于你的代码尖叫

糟糕的实现。事实上,它写得非常清楚。不过,你还是不知道在你尝试之前它是否太慢了。


有办法得到明确的答案关于如何通过python中的配置文件模块运行速度有多快
。对该模块进行一些研究。如果您对此感到困惑,请回过头来询问更多问题。


考虑到它可能不是您的代码''很慢,但是

而不是你获取信息的方式。这被称为I / O

绑定。持有可能不是程序,而是磁盘或

网络。毕竟,在获得

信息之前,您无法处理信息。配置文件模块将帮助您查看这是否是

问题。


慢速代码的一个更加流畅的解决方案是psyco模块。它可以为许多处理密集型功能提供惊人的速度提升,但它有时甚至会减慢你的问题。


如果你想在

动作中看到psyco和profile模块的一个例子,请告诉我,我会给你一些更容易理解的代码,我是

曾写过,看看psyco擅长优化什么类型的东西。


HTH


Sam Walters。
< br $> b $ b -

永远不要忘记万圣节文件。
http://www.opensource.org/halloween/

"""微软今天会把你拖到哪里?

你真的想去那里吗?""


I''m a python rookie, anyone have and suggestions to streamline this
function? Thanks in advance.....
def getdata(myurl):
sock = urllib.urlopen(myurl)
xmlSrc = sock.read()
sock.close()

xmldoc = minidom.parseString(xmlSrc)

def getattrs(weatherAttribute):
a = xmldoc.getElementsByTagName(weatherAttribute)
return a[0].firstChild.data

currname = getattrs("name")
currtemp = getattrs("fahrenheit")
currwind = getattrs("wind")
currdew = getattrs("dewpoint")
currbarom = getattrs("relative_humidity")
currhumid = getattrs("barometric_pressure")
currcondi = getattrs("conditions")

print "%13s\t%s\t%s\t%s\t%s\t%s\t%s" % (currname, currtemp,
currwind, currbarom, currdew, currhumid, currcondi)

解决方案

|Thus Spake William S. Perrin On the now historical date of Wed, 07 Jan
2004 17:37:57 -0600|

I''m a python rookie, anyone have and suggestions to streamline this
function? Thanks in advance.....



Please define "streamline" in this context.

Do you mean:
faster
smaller
easier to read
etc.

Sam Walters.

--
Never forget the halloween documents.
http://www.opensource.org/halloween/
""" Where will Microsoft try to drag you today?
Do you really want to go there?"""


Sorry, I guess is it efficient? That is if I called it 1000 times.....

Samuel Walters wrote:

|Thus Spake William S. Perrin On the now historical date of Wed, 07 Jan
2004 17:37:57 -0600|

I''m a python rookie, anyone have and suggestions to streamline this
function? Thanks in advance.....


Please define "streamline" in this context.

Do you mean:
faster
smaller
easier to read
etc.

Sam Walters.




|Thus Spake William S. Perrin On the now historical date of Wed, 07 Jan
2004 17:45:38 -0600|

Sorry, I guess is it efficient? That is if I called it 1000 times.....



Ponders... Even "efficient" has a loose meaning here. Since you describe
running it a thousand times, I''ll assume you mean speed of execution.

There''s a saying "Premature optimization is the root of all evil." Which
is another way of saying "Try it, and if it''s too slow, figure out what
the hold up is. If it''s not too slow, don''t mess with it." So, try
running it in the context you need it in. Nothing about your code screams
"bad implementation." In fact, it''s quite clearly written. Still, you
won''t know if it''s too slow until you try it.

There''s a way to get a definitive answer on how how fast it''s running
through the profile module in python. Do some research on that module. If
you''re confused about it, come back and ask more questions then.

Take into consideration that it may not be your code that''s slow, but
rather the way you''re getting your information. This is called being "I/O
bound." The holdup might not be the program, but instead the disk or the
network. After all, you can''t process information until you have the
information. The profile module will help you to see if this is the
problem.

One of the slicker solutions to slow code is the psyco module. It can
give an amazing speed boost to many processing intensive functions, but it
can sometimes even slow down your problem.

If you''d like to see an example of both the psyco and profile modules in
action, let me know and I''ll give you some more understandable code that I
once wrote to see what types of things psyco is good at optimizing.

HTH

Sam Walters.

--
Never forget the halloween documents.
http://www.opensource.org/halloween/
""" Where will Microsoft try to drag you today?
Do you really want to go there?"""


这篇关于菜鸟说话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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