良好的编程风格 [英] Good programming style

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

问题描述

我还在学习python,想知道什么是组织代码的好方法。


我正在编写一些脚本刮掉一些不同的网站,

持有类似的信息,然后将它们整理在一起。显然

每个站点需要以不同的方式处理,但是一旦收集了信息,就可以使用更多的通用函数。


最好将它全部放在一个脚本中,还是将其拆分为每个站点

脚本然后可以通过管理器脚本调用?如果一个脚本中的所有内容都是

,那么每个站点的功能都会提取数据或

泛型函数,根据网站的不同而略有不同,因为

示例


导入firstSiteScript

导入secondSiteScript

firstsitedata = firstSiteScript.getData(''search_str)

secondsitedata = secondSiteScript.getData(''search_str)

等等





def getFirstSiteData(search_str):

等等

def getSecondSiteData(search_str):

等等




def getdata(search_str,网站):

if website ==''firstsite'':

....

elif网站==''secondsite'':



I''m still learning python and would like to know what''s a good way of
organizing code.

I am writing some scripts to scrape a number of different website that
hold similar information and then collating it all together. Obviously
each site needs to be handled differently, but once the information is
collected then more generic functions can be used.

Is it best to have it all in one script or split it into per site
scripts that can then be called by a manager script? If everything is
in one script would you have per site functions to extract the data or
generic function that contain vary slightly depending on the site, for
example

import firstSiteScript
import secondSiteScript

firstsitedata = firstSiteScript.getData(''search_str)
secondsitedata = secondSiteScript.getData(''search_str)
etc etc

OR

def getFirstSiteData(search_str):
etc etc
def getSecondSiteData(search_str):
etc etc

OR

def getdata(search_str, website):
if website == ''firstsite'':
....
elif website ==''secondsite'':

etc

推荐答案

Astley Le Jasperaécrit:
Astley Le Jasper a écrit :

我还在学习python,想知道是什么'是一个很好的方式

组织代码。


我正在写一些脚本来刮掉一些不同的网站

保存类似的信息然后将它们整理在一起。显然

每个站点需要以不同的方式处理,但是一旦收集了信息,就可以使用更多的通用函数。


最好是将它全部放在一个脚本中,还是将其拆分为每个站点

脚本然后可以通过管理器脚本调用?

如果一切都是

在一个脚本中你是否有每个网站功能来提取数据或

泛型函数包含略有不同,具体取决于网站,
I''m still learning python and would like to know what''s a good way of
organizing code.

I am writing some scripts to scrape a number of different website that
hold similar information and then collating it all together. Obviously
each site needs to be handled differently, but once the information is
collected then more generic functions can be used.

Is it best to have it all in one script or split it into per site
scripts that can then be called by a manager script?
If everything is
in one script would you have per site functions to extract the data or
generic function that contain vary slightly depending on the site,



就我而言,我会选择第一个解决方案。解耦

来自不变量的变化(这里是特定于站点的东西)到目前为止

我知道保持复杂性可管理的最佳方式。

As far as I''m concerned, I''d choose the first solution. Decoupling
what''s varying (here, site-specific stuff) from "invariants" is so far
the best way I know to keep complexity manageable.


for

example


导入firstSiteScript

导入secondSiteScript

firstsitedata = firstSiteScript.getData(''search_str)

secondsitedata = secondSiteScript.getData(''search_str)

等等
for
example

import firstSiteScript
import secondSiteScript

firstsitedata = firstSiteScript.getData(''search_str)
secondsitedata = secondSiteScript.getData(''search_str)
etc etc



更好:


- 将通用函数放在''通用''模块中

- 将所有特定于站点的内容放入其自己的模块中的特定

'' site_scripts''目录

- 在你的''main''脚本中,扫描site_scripts目录以循环

特定于站点的模块,导入它们并运行它们(查找__import__

功能)。


这是一种Q& D轻量级插件系统,可以避免难以支付
-code在主脚本中导入和调用,所以你必须这样做

在site_scripts目录中添加/删除特定于站点的脚本。


此外,每次导入时都不会重新编译导入的模块 - 仅当

他们改变了 - 而''main''脚本在每次调用时都被重新编译。


(剪辑)

Even better :

- put generic functions in a ''generic'' module
- put all site-specific stuff each in it''s own module in a specific
''site_scripts'' directory
- in your ''main'' script, scan the site_scripts directory to loop over
site-specific modules, import them and run them (look for the __import__
function).

This is kind of a Q&D lightweight plugin system, that avoids having to
hard-code imports and calls in the main script, so you just have to
add/remove site-specific script to/from the site_scripts directory .

Also, imported modules are not recompiled on each import - only when
they change - while the ''main'' script get recompiled on each invocation.

(snip)





def getdata(search_str,网站):

if website ==''firstsite'':

....

elif网站==''secondsite'':
OR

def getdata(search_str, website):
if website == ''firstsite'':
....
elif website ==''secondsite'':



这是恕我直言,这是最糟糕的事情。


我的2美分......

This one is IMHO the very worst thing to do.

My 2 cents...


9月12日,12:44,Bruno Desthuilliers< bruno。

42.desthuilli ... @ websiteburo.invalidwrote:
On 12 Sep, 12:44, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:

Astley Le Jasperaécrit:
Astley Le Jasper a écrit :

我还在学习python,想知道组织代码的好方法是什么。
I''m still learning python and would like to know what''s a good way of
organizing code.


我正在编写一些脚本来刮取许多不同的网站

持有类似信息然后将它们整理在一起。显然

每个站点需要以不同的方式处理,但是一旦信息收集了b
,就可以使用更多的通用函数。
I am writing some scripts to scrape a number of different website that
hold similar information and then collating it all together. Obviously
each site needs to be handled differently, but once the information is
collected then more generic functions can be used.


是否最好将其全部放在一个脚本中或将其拆分为每个站点

脚本然后可以调用通过一个管理器脚本?

如果一个脚本中的所有内容都是

你是否有每个站点的功能来提取数据或

泛型函数包含根据网站略有不同,
Is it best to have it all in one script or split it into per site
scripts that can then be called by a manager script?
If everything is
in one script would you have per site functions to extract the data or
generic function that contain vary slightly depending on the site,



就我而言,我会选择第一个解决方案。解耦

来自不变量的变化(这里是特定于站点的东西)到目前为止

我知道保持复杂性可管理的最佳方式。


As far as I''m concerned, I''d choose the first solution. Decoupling
what''s varying (here, site-specific stuff) from "invariants" is so far
the best way I know to keep complexity manageable.


for

示例
for
example


import firstSiteScript

import secondSiteScript
import firstSiteScript
import secondSiteScript


firstsitedata = firstSiteScript.getData(''search_str)

secondsitedata = secondSiteScript.getData(''search_str)

等等
firstsitedata = firstSiteScript.getData(''search_str)
secondsitedata = secondSiteScript.getData(''search_str)
etc etc



甚至更好:


- 将通用函数放在通用模块中

- 将所有特定于站点的内容放入其中''在特定的

''site_scripts''目录中拥有自己的模块

- 在你的''main''脚本中,扫描site_scripts目录以循环遍历

特定于站点的模块,导入并运行它们(查找__import__

函数)。


这是一种Q& D轻量级插件系统,那避免必须在主脚本中进行硬编码导入和调用,因此您只需要向/从site_scripts目录添加/删除特定于站点的脚本。<另外,导入的模块不会在每次导入时重新编译 - 只有当他们改变了

时 - 每次调用都会重新编译''main''脚本。 br />

(剪辑)


Even better :

- put generic functions in a ''generic'' module
- put all site-specific stuff each in it''s own module in a specific
''site_scripts'' directory
- in your ''main'' script, scan the site_scripts directory to loop over
site-specific modules, import them and run them (look for the __import__
function).

This is kind of a Q&D lightweight plugin system, that avoids having to
hard-code imports and calls in the main script, so you just have to
add/remove site-specific script to/from the site_scripts directory .

Also, imported modules are not recompiled on each import - only when
they change - while the ''main'' script get recompiled on each invocation.

(snip)


OR
OR


def getdata(search_str,website):

* if website ==''firstsite'':

* * ....

* elif网站==''secondsite'':
def getdata(search_str, website):
* if website == ''firstsite'':
* * ....
* elif website ==''secondsite'':



这是恕我直言,这是最糟糕的事情。


我的2美分...


This one is IMHO the very worst thing to do.

My 2 cents...



非常好,谢谢你。

Excellent, thanks for that.


Astley Le Jasper< As * ************ @ gmail.com编写:
Astley Le Jasper <As*************@gmail.comwrites:

最好将它全部放在一个脚本中o r将它拆分为每个站点

脚本然后可以由管理器脚本调用?如果所有内容

都在一个脚本中你会有每个站点的功能来提取

数据或包含的通用函数,具体取决于

网站,例如


导入firstSiteScript

导入secondSiteScript
Is it best to have it all in one script or split it into per site
scripts that can then be called by a manager script? If everything
is in one script would you have per site functions to extract the
data or generic function that contain vary slightly depending on the
site, for example

import firstSiteScript
import secondSiteScript



首先:这些东西中的每一个你导入的是一个模块。在Python中。

为了清楚起见,脚本是我更喜欢调用程序:它是

打算独立执行作为顶级执行。


第二:请帮自己一个忙,放下camelCaseNames。

关注PEP 8< URL:http://www.python。 org / dev / peps / pep-0008for style

并在Python代码中命名。

First: each of these things you''re importing is a "module" in Python.
A script is what I prefer, for clarity, to call a "program": it''s
intended to be executed independently as the top level of execution.

Second: please do yourself a favour and drop the camelCaseNames.
Follow PEP 8 <URL:http://www.python.org/dev/peps/pep-0008for style
and naming in your Python code.


firstsitedata = firstSiteScript.getData(''search_str )

secondsitedata = secondSiteScript.getData(''search_str)

等等
firstsitedata = firstSiteScript.getData(''search_str)
secondsitedata = secondSiteScript.getData(''search_str)
etc etc



我在假设在这些不同的网站之间会有大面积的共同功能

。在此基础上,将b / b
的差异视为可能的差异,而不是为整个网站提供单独的模块,这可能是最好的。 br />

你可能想看看一个网页框架,它为你聚集了很多这个

的功能,并提供灵活的方式来定义
$ b这些共同元素的$ b不同网站

< URL:http://wiki.python.org/moin/WebFrameworks>。


- -

\ a ??追随时尚,现状很简单。思考|

` \用户的生活并创造实用的东西很多|

_o__)harder.a ?? a ?? Ryan Singer,2008-07-09 |

Ben Finney

I''m presuming that there will be large areas of common functionality
between these different sites. On that basis, it''s prbably best to
treat the differences as differences of *configuration* where
possible, instead of having separate modules for the entire site.

You might like to look at a web framework which gathers much of this
functionality together for you, and provides flexible ways to define
different sites in terms of those common elements
<URL:http://wiki.python.org/moin/WebFrameworks>.

--
\ a??Following fashion and the status quo is easy. Thinking about |
`\ your users'' lives and creating something practical is much |
_o__) harder.a?? a??Ryan Singer, 2008-07-09 |
Ben Finney


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

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