Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Brief Code Review Please - Learning Python Date: Sun, 06 Dec 2015 19:14:11 +0100 Organization: None Lines: 151 Message-ID: References: <8fce9c71-a7bf-43fa-b8cc-f23c268e2b05@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de nmhp+O0M3Se0keE71hpHdwDgA+Y9UR4tMSOE5n0RMyig== 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; 'subject:Python': 0.05; '"""': 0.05; 'defaults': 0.05; 'none:': 0.05; '"__main__":': 0.07; "'')": 0.07; '__name__': 0.07; 'correct.': 0.07; 'executed': 0.07; 'extent': 0.07; 'false,': 0.07; 'prefix': 0.07; 'tests,': 0.07; 'width': 0.07; '40,': 0.09; 'collections': 0.09; 'meaningful': 0.09; 'metrics': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'spreading': 0.09; 'surrounded': 0.09; 'underlying': 0.09; 'underscore': 0.09; 'advance': 0.10; 'python': 0.10; 'def': 0.13; 'argument': 0.15; 'everyone,': 0.15; 'variables': 0.15; '"some': 0.16; 'accordingly': 0.16; 'backslashes': 0.16; 'datatype:': 0.16; 'guide.': 0.16; 'measures': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'spurious': 0.16; 'subject:Learning': 0.16; 'tabs': 0.16; 'wrote:': 0.16; 'stick': 0.18; 'tests': 0.18; 'variable': 0.18; 'library': 0.20; 'changes': 0.20; 'fix': 0.21; 'aligned': 0.22; 'arguments': 0.22; 'names.': 0.22; 'referring': 0.22; 'subject:Code': 0.22; 'text,': 0.22; 'pass': 0.22; 'bit': 0.23; 'consistent': 0.23; 'tried': 0.24; 'import': 0.24; 'recognized': 0.24; 'module': 0.25; "i've": 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'appreciated.': 0.27; 'skip:m 30': 0.27; 'not.': 0.27; 'question': 0.27; 'said,': 0.27; 'function': 0.28; 'skip:( 20': 0.28; 'implicitly': 0.29; 'measure': 0.29; 'multiline': 0.29; 'pep': 0.29; 'code:': 0.29; 'raise': 0.29; "i'm": 0.30; 'code': 0.30; 'convention': 0.30; "i'd": 0.31; 'everyone': 0.31; 'changed': 0.33; 'class': 0.33; "i'll": 0.33; 'similar': 0.33; 'case,': 0.34; 'correctly': 0.34; 'definition': 0.34; 'editor': 0.34; 'skip:d 20': 0.34; 'list': 0.34; 'text': 0.35; 'fail': 0.35; 'jason': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'should': 0.36; 'instead': 0.36; 'lines': 0.36; 'beginning': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'missing': 0.37; 'brief': 0.38; 'feedback': 0.38; 'names': 0.38; 'skip:p 20': 0.38; 'thank': 0.38; 'google': 0.39; 'sure': 0.39; 'does': 0.39; 'along': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'your': 0.60; 'avoid': 0.61; 'provide': 0.61; 'email addr:gmail.com': 0.62; 'qualified': 0.63; 'skip:w 30': 0.64; 'python-list': 0.66; 'below.': 0.66; 'exceed': 0.72; 'journey': 0.72 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd84f0.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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:100060 qualityaddictorg@gmail.com 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. > 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? > * 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")): > 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) > 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 Use short variable names - the "desired_" prefix does not carry much information. Avoid unrelated but similar names: measure or measures -- what's what? Use consistent names. This is a bit tricky, but I would prefer multiline over is_multi_lines because the underlying library uses the former. Use the right datatype: multiline should be boolean, i. e. True or False, not "True" or "False". Provide defaults if possible: if the multiline argument is missing you can scan the text for "\n" and set the flag accordingly Prefer parens for line continuations (a and b) over backslashes a and \ b Move code that only should be executed once from the function to the global namespace: the namedtuple definition should be on the module level. Use qualified names if you are referring library code: wand.drawing.Drawing instead of just Drawing. Avoid spurious checks, but if you do argument validation fail noisyly, i. e. "wrong": def f(fontname): if is_valid(fontname): return meaningful_result() # implicitly return None "correct": class InvalidFontname(Exception): pass def f(fontname): if not is_valid(fontname): raise InvalidFontname(fontname) return meaningful_result() With changes along these lines your code might become from collections import namedtuple import wand.drawing import wand.image Extent = namedtuple("Extent", "width height") def get_text_extent( text, font_family, font_size, multiline=None): """Determine width and heights for `text`. """ if multiline is None: multiline = "\n" in text with wand.image.Image(filename='wizard:') as image: with wand.drawing.Drawing() as measure: measure.font_family = font_family measure.font_size = font_size metrics = measure.get_font_metrics( image, text, multiline=multiline) return Extent( width=metrics.text_width, height=metrics.text_height) if __name__ == "__main__": print( get_text_extent( "some text\na new line", "Segoe UI", 40))