分析一套源代码的代码规范和风格并讨论如何改进优化代码

sklearn源码规范性

  工程实践的项目为手写汉字脱机识别,人工智能方面的项目一般就跟tensorflow和sklearn紧密联系在了一起,

还包括常用的Pandas、Numpy、pol等。下面进行个人对sklearn源码的规范性进行的分析。

   整个模型的架构清晰,一眼看过去知道有哪些东西,这跟命名的艺术分不开,名字一眼看过去就可以大体知道

这一部分应该会实现什么功能。

 

 分析一套源代码的代码规范和风格并讨论如何改进优化代码_第1张图片

  每个类里面都有自己固定的私有方法,接口,初始化方法等等。

 

分析一套源代码的代码规范和风格并讨论如何改进优化代码_第2张图片

 

   进入其中一个方法。首先是一大段的注释,告诉user是用来干什么的,接着导入模块,按照系统包、第三方包、自己写的包的顺序,

还加上必要的单行注释,注释虽然没有执行,但也是程序必不可少的一部分,无论是给别人看,还是回过头来自己看。考虑程序的健壮性,

添加了必要的异常捕获和异常处理。

"""Machine learning module for Python
==================================

sklearn is a Python module integrating classical machine
learning algorithms in the tightly-knit world of scientific Python
packages (numpy, scipy, matplotlib).

It aims to provide simple and efficient solutions to learning problems
that are accessible to everybody and reusable in various contexts:
machine-learning as a versatile tool for science and engineering.

See http://scikit-learn.org for complete documentation.
"""
import sys
import re
import warnings
import logging
import os

from ._config import get_config, set_config, config_context

logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)


# Make sure that DeprecationWarning within this package always gets printed
warnings.filterwarnings('always', category=DeprecationWarning,
                        module=r'^{0}\.'.format(re.escape(__name__)))

# PEP0440 compatible formatted version, see:
# https://www.python.org/dev/peps/pep-0440/
#
# Generic release markers:
#   X.Y
#   X.Y.Z   # For bugfix releases
#
# Admissible pre-release markers:
#   X.YaN   # Alpha release
#   X.YbN   # Beta release
#   X.YrcN  # Release Candidate
#   X.Y     # Final release
#
# Dev branch marker is: 'X.Y.dev' or 'X.Y.devN' where N is an integer.
# 'X.Y.dev0' is the canonical version of 'X.Y.dev'
#
__version__ = '0.21.2'


# On OSX, we can get a runtime error due to multiple OpenMP libraries loaded
# simultaneously. This can happen for instance when calling BLAS inside a
# prange. Setting the following environment variable allows multiple OpenMP
# libraries to be loaded. It should not degrade performances since we manually
# take care of potential over-subcription performance issues, in sections of
# the code where nested OpenMP loops can happen, by dynamically reconfiguring
# the inner OpenMP runtime to temporarily disable it while under the scope of
# the outer OpenMP parallel section.
os.environ.setdefault("KMP_DUPLICATE_LIB_OK", "True")


try:
    # This variable is injected in the __builtins__ by the build
    # process. It is used to enable importing subpackages of sklearn when
    # the binaries are not built
    __SKLEARN_SETUP__
except NameError:
    __SKLEARN_SETUP__ = False

if __SKLEARN_SETUP__:
    sys.stderr.write('Partial import of sklearn during the build process.\n')
    # We are not importing the rest of scikit-learn during the build
    # process, as it may not be compiled yet
else:
    from . import __check_build
    from .base import clone
    from .utils._show_versions import show_versions

  类名采用大写字母开头,方法名采用小写字母开头,分别使用大驼峰和小驼峰命名方式,便于理解和观看。

对于python这门语言缩进显得尤为重要,缩进必须要规范。在复杂的条件表达式中,用括号清楚地表示逻辑优先级。

必要的断行,可以直观的看代码,避免看串行,比如:

if condition:

    DoSomething(); 

else :

    DoSomethingElse();

 

 

你可能感兴趣的:(分析一套源代码的代码规范和风格并讨论如何改进优化代码)