99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀牛大了,這代碼看著真專業(yè)
牛大了,這代碼看著真專業(yè)
2022-04-02
收藏
牛大了,這代碼看著真專業(yè)

作者:麥?zhǔn)?

來源:麥?zhǔn)寰幊?

引子

今年開始,我在翻譯一本大部頭的,比較經(jīng)典的的Python進階書籍。

有空就翻譯幾頁。這本書不僅是教你很多進階的Python的語法,更重要的是講解很多設(shè)計方法和設(shè)計思想。

這些方法和思想,一點點疊加起來,就會讓你從一個普通的程序員變成一個很專業(yè)的程序員。至少看起來挺唬人的!

昨天我在翻譯關(guān)于docstring的章節(jié)。書中舉的一個例子,把一個很普通的類,轉(zhuǎn)變成了跟Python內(nèi)置的庫一樣專業(yè)的代碼。

我感覺眼睛一亮,覺得有必要跟大家分享一下。

設(shè)計需求

這個類的功能很簡單:

  • 類名Point,代表二維坐標(biāo)系中的一個點。
  • 屬性:x和y代表二維坐標(biāo)
  • 方法:類包含3個方法,實現(xiàn)了“回到原點”,“移動到指點的點”,“計算兩個點之間的距離”。

想想看,你會怎么寫呢?

專業(yè)級的代碼

我就直接分享我認(rèn)為比較專業(yè)的代碼吧,請仔細(xì)閱讀,品味其中專業(yè)的地方:

class Point: """
    Represents a point in two-dimensional geometric coordinates
    >>> p_0 = Point()
    >>> p_1 = Point(3, 4)
    >>> p_0.calculate_distance(p_1)
    5.0
    """ def __init__(self, x: float = 0, y: float = 0) -> None: """
        Initialize the position of a new point. The x and y
        coordinates can be specified. If they are not, the
        point defaults to the origin.
        :param x: float x-coordinate
        :param y: float x-coordinate
        """ self.move(x, y) def move(self, x: float, y: float) -> None: """
        Move the point to a new location in 2D space.
        :param x: float x-coordinate
        :param y: float x-coordinate
        """ self.x = x
        self.y = y def reset(self) -> None: """
        Reset the point back to the geometric origin: 0, 0
        """ self.move(0, 0) def calculate_distance(self, other: "Point") -> float: """
        Calculate the Euclidean distance from this point 
        to a second point passed as a parameter.
        :param other: Point instance
        :return: float distance
        """ return math.hypot(self.x - other.x, self.y - other.y)

來說一下,為什么我覺得這段代碼是專業(yè)級的:

  • 類名和方法名都非常直觀,簡單易懂。有沒有?
  • 通過docstring,類和方法都加了非常簡明扼要的文檔??雌饋砭秃芟駜?nèi)置庫的代碼。
  • 函數(shù)都使用類型提示,增加代碼可讀性,可以使用mypy等做類型檢查。
  • __init__函數(shù)使用了默認(rèn)值,確保每個初始化出的實例都有有效的屬性值。
  • reset函數(shù)沒有自己實現(xiàn)輪子,而是直接調(diào)用move方法。
  • 類的docstring中提供了使用例子,可以用doctest等測試工具做代碼的簡單自動化測試。

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }