Path: csiph.com!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed0.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Joel Goldstick Newsgroups: comp.lang.python Subject: Re: Brief Code Review Please - Learning Python Date: Sun, 6 Dec 2015 12:34:31 -0500 Lines: 95 Message-ID: References: <8fce9c71-a7bf-43fa-b8cc-f23c268e2b05@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de quATH/P8l/IuWRr7X0K3RAVjL/9N4tGHpFYU5TnQv9Hw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'received:209.85.223': 0.03; 'subject:Python': 0.05; "'')": 0.07; 'correct.': 0.07; 'false.': 0.07; 'tests,': 0.07; 'cc:addr:python- list': 0.09; '40,': 0.09; 'meaningful': 0.09; 'spreading': 0.09; 'surrounded': 0.09; 'underscore': 0.09; 'advance': 0.10; 'python': 0.10; 'def': 0.13; 'everyone,': 0.15; 'variables': 0.15; 'guide.': 0.16; 'measures': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Learning': 0.16; 'tabs': 0.16; 'wrote:': 0.16; 'string': 0.17; 'stick': 0.18; 'email addr:gmail.com>': 0.18; 'tests': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'fix': 0.21; 'aligned': 0.22; 'arguments': 0.22; 'subject:Code': 0.22; 'defined': 0.23; 'fit': 0.23; 'dec': 0.23; 'tried': 0.24; 'recognized': 0.24; 'header:In- Reply-To:1': 0.24; "i've": 0.25; 'appreciated.': 0.27; 'skip:m 30': 0.27; 'not.': 0.27; 'parameters': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'said,': 0.27; 'function': 0.28; 'skip:( 20': 0.28; '"': 0.29; 'multiline': 0.29; 'pep': 0.29; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.30; 'convention': 0.30; "i'd": 0.31; 'everyone': 0.31; 'changed': 0.33; 'class': 0.33; 'url:python': 0.33; 'common': 0.33; 'quotes': 0.33; 'true.': 0.33; "i'll": 0.33; 'url:listinfo': 0.34; 'case,': 0.34; 'correctly': 0.34; 'editor': 0.34; 'skip:d 20': 0.34; 'list': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'jason': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'url:org': 0.36; 'lines': 0.36; 'received:209.85': 0.36; 'beginning': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'skip:& 10': 0.37; "won't": 0.38; 'received:209': 0.38; 'brief': 0.38; 'feedback': 0.38; 'skip:p 20': 0.38; 'thank': 0.38; 'google': 0.39; 'test': 0.39; 'sure': 0.39; 'url:mail': 0.40; 'some': 0.40; 'your': 0.60; 'more': 0.63; 'skip:w 30': 0.64; 'python-list': 0.66; 'below.': 0.66; 'skip:\xc2 10': 0.67; 'exceed': 0.72; 'journey': 0.72; 'saw': 0.77; 'to:none': 0.91; 'joel': 0.91; 'imagine': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=CMu7LvkbjPv6xoEKLMO6pLux2AYZ4+dYNCbzIWuxe88=; b=qneNLpPEn+eo246+2RZS2NCfD+CfpPt2VwEGHVnRk2cMZifs8ULHQ63aScYzvqXfVF /lDLfZ9XaMUSOofJT7PBr1UA4zNxkH7EQAO8DCtlzXiM2rOOGTYRMRn9l2j1Z3ZHoSjK YZjTuK/UdR2kSuqyPrUlnutLxGAog7dUw9mTyftIBlKIA6FM5h1/A28Y3+4ghi/zko8f oExlvAj0U45TEphytjIj2HP+4zGaneCmj+ondnlTq6RBZoEqSJZU/RTvN90+MKUsOcvk eLBVELfjAtTjPIvCzObjOqFtU+mKzZC6KTUqVf/xOEpqKojgT5AXFL0fmNaYp+29K+44 YQNQ== X-Received: by 10.107.30.80 with SMTP id e77mr22647584ioe.180.1449423271426; Sun, 06 Dec 2015 09:34:31 -0800 (PST) In-Reply-To: <8fce9c71-a7bf-43fa-b8cc-f23c268e2b05@googlegroups.com> X-Content-Filtered-By: Mailman/MimeDel 2.1.20+ X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100058 On Sun, Dec 6, 2015 at 12:21 PM, wrote: > [Note: Tried sending this twice to the Python-List mail list but I never > saw it come through to the list.] > > Hello everyone, > > I am beginning to learn Python, and I've adapted some code from Google > into the function below. I'm also looking at the PEP 8 style guide. I'd > appreciate a brief code review so I can start this journey off on a solid > foundation. :) > > * I've already changed my editor from tabs to 4 spaces. > * I've also set my editor to alert me at 72 characters and don't > exceed 79 characters. > * I've named the function and variables with lower case, underscore > separated, and meaningful words. > * I've surrounded this function with two blank lines before and after. > * I just recognized I need to pick a quote style and stick to it so > I'll fix that (" " or ' '). > * I'm not sure about the WidthAndHeight on lines 16 and 17 regarding > capitalization. > You don't need the parens. If you feel it is easier to understand with them, they won't hurt > If I understand correctly a namedtuple is a class so the CapWords > convention is correct. > * Is how I've aligned the function arguments on lines 1-4 and 22-25 > good style or \ > is spreading these out onto fewer lines preferred? > If the parameters fit on one line, I think that is more common > * Same question as right above but with the if tests on lines 5-8. > * I've also used ( ) around the if tests, but I'm not sure if this is > good Python style or not. > > 1 def measure_string(desired_text, > 2 desired_font_family, > 3 desired_font_size, > 4 is_multi_lines): > 5 if (desired_text != '') and \ > 6 (desired_font_family != '') and \ > 7 (desired_font_size != '') and \ > 8 ((is_multi_lines == "True") or (is_multi_lines == "False")): > The above test will always be True. Look up what is considered True in Python, and what is False. I imagine you don't want quotes around True and False. Any string will == True > 9 with Image(filename='wizard:') as temp_image: > 10 with Drawing() as measure: > 11 measure.font_family = desired_font_family > 12 measure.font_size = desired_font_size > 13 measures = measure.get_font_metrics(temp_image, > 14 desired_text, > 15 > multiline=is_multi_lines) > No need to set multiline = ... when is_multiline is already defined > 16 WidthAndHeight = namedtuple("WidthAndHeight", "Width Height") > 17 width_and_height = WidthAndHeight(measures.text_width, \ > 18 measures.text_height) > 19 return width_and_height > 20 > 21 > 22 print(measure_string("some text\na new line", > 23 "Segoe UI", > 24 40, > 25 "True")) > > Any and all feedback is much appreciated. As I said, I'm just beginning > to learn Python and want to start off with a solid foundation. Thank you > to everyone in advance for your time and thoughts. > > Jason > -- > https://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com/stats/birthdays