美文网首页
代码规范

代码规范

作者: jiaxiaolei | 来源:发表于2022-05-16 10:46 被阅读0次

    关于python代码规范,从网上可以找到很多,编辑器(vim,sublime text, pycharm等)也都有相应的代码规范插件,都会做相应的提醒。
    自己编辑和提交之前检查一下。
    https://www.python.org/dev/peps/pep-0008/
    http://www.runoob.com/w3cnote/google-python-styleguide.html

    理论:

    python官方pep8:

    https://peps.python.org/pep-0008/

    Python 编码规范(Google)
    https://google.github.io/styleguide/pyguide.html

    Python 编码规范(Google)【中文翻译】
    https://www.runoob.com/w3cnote/google-python-styleguide.html

    实践:

    IDE,编辑器(vim,sublime text, visual studio code, pycharm等)也都有相应的代码规范插件,都会做相应的提醒和修复建议。

    # filename: uop/auth/handler.py
    
    # -*- coding: utf-8 -*-
    import json    #NOTE: 必要的空行做分割;
    import sys
    import ldap
    import datetime  #NOTE: 导入了,但是并没有用到
    import os        
    import hashlib
    from flask import request   #NOTE: 导入包的顺序依次为: 标准库—>第三方库—>自定义库。 并用空行做分割。
    from flask import redirect
    from flask import jsonify
    from flask_restful import reqparse, abort, Api, Resource, fields, marshal_with
    from mongoengine import NotUniqueError
    from uop.auth import auth_blueprint
    from uop.models import UserInfo, User
    from uop.auth.errors import user_errors
    from wtforms import ValidationError
    reload(sys)
    sys.setdefaultencoding('utf-8')
    from flask.ext.httpauth import HTTPBasicAuth #NOTE: 过期的module, 用flask_httpauth 替换。
    
    auth = HTTPBasicAuth()
    
    base_dn = 'dc=syswin,dc=com'
    scope = ldap.SCOPE_SUBTREE
    ldap_server = 'ldap://172.28.4.103:389'  #NOTE: 硬编码,统一到配置文件中。
    username = 'crm_test1'
    passwd_admin = 'syswin#'
    
    auth_api = Api(auth_blueprint, errors=user_errors)
    
    
    class LdapConn(object):
        def __init__(self, server, admin_name, admin_pass, base_dn, scope, flag=None, cn=None):
            self.server = server,
            self.name = admin_name,
            self.passwd = admin_pass,
            self.base_dn = base_dn,
            self.scope = scope,
            self.flag = flag,
    
        def conn_ldap(self):
            ldap.set_option(ldap.OPT_REFERRALS, 0)
            conn = ldap.initialize(self.server[0])
            conn.simple_bind_s(self.name[0], self.passwd[0])
            return conn
    
        def verify_user(self, id, password):
            result = []
            con = self.conn_ldap()
            filter_field = "(&(|(cn=*%(input)s*)(sAMAccountName=*%(input)s*))(sAMAccountName=*))" % {'input': id}
            attrs = ['sAMAccountName', 'mail', 'givenName', 'sn', 'department', 'telephoneNumber', 'displayName']
            for i in con.search_s(base_dn, scope, filter_field, None):
                if i[0]:
                    d = {}
                    for k in i[1]:
                        d[k] = i[1][k][0]
                    if 'telephoneNumber' not in d:
                        d['telephoneNumber'] = '(无电话)'
                    if 'department' not in d:
                        d['department'] = '(无部门)'
                    if 'sn' not in d and 'givenName' not in d:
                        d['givenName'] = d.get('displayName', '')
                    if 'sn' not in d:
                        d['sn'] = ''
                    if 'givenName' not in d:
                        d['givenName'] = ''
                    result.append(d)
                    self.cn = d.get('distinguishedName', '')
                    print self.cn   #NOTE: 正式提交的代码不出现 print, 如果需要,可以用 logging.debug()
                    id = d.get('sAMAccountName', '')
                    mail = d.get('mail', '')
                    name = d.get('cn', '')
                    mobile = d.get('mobile', '')
                    department = d.get('department', '')
                    field_value = {
                            'id': id,
                            'mail': mail,
                            'name': name,
                            'mobile': mobile,
                            'department': department
                            }
                    print d
            print '共找到结果 %s 条' % (len(result))
            for d in result:
                print '%(sAMAccountName)s\t%(mail)s\t%(sn)s%(givenName)s\t%(mobile)s %(department)s' % d
            try:
                if con.simple_bind_s(self.cn, password):
                    print 'verify successfully'
                    self.flag = 1
                else:
                    print 'verify fail'
                    self.flag = 0
            except ldap.INVALID_CREDENTIALS, e:
                print e
                self.flag = 0
            return self.flag, field_value
    

    相关文章

      网友评论

          本文标题:代码规范

          本文链接:https://www.haomeiwen.com/subject/ubgukxtx.html