Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #88628

Re: Code critique please

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
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; 'resulting': 0.04; 'syntax': 0.04; 'value,': 0.04; 'none:': 0.07; 'string': 0.09; '__name__': 0.09; 'bits': 0.09; 'executed': 0.09; 'function,': 0.09; 'iterate': 0.09; 'occurrences': 0.09; 'okay': 0.09; 'style.': 0.09; 'terms,': 0.09; 'python': 0.11; 'def': 0.12; '"from': 0.16; "'0'": 0.16; "'__main__':": 0.16; "'b'": 0.16; '__future__': 0.16; 'byte,': 0.16; 'customarily': 0.16; 'elements,': 0.16; 'script,': 0.16; 'substring': 0.16; 'y):': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'value.': 0.19; 'import': 0.22; 'print': 0.22; 'byte': 0.24; 'case.': 0.24; 'string,': 0.24; 'subject:Code': 0.24; 'header': 0.24; 'header:In- Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'characters': 0.30; 'said,': 0.30; 'subject:please': 0.30; 'message-id:@mail.gmail.com': 0.30; 'that.': 0.31; 'file': 0.32; 'probably': 0.32; "i'd": 0.34; 'skip:u 20': 0.35; 'something': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'really': 0.36; 'ones,': 0.36; 'representing': 0.36; 'returning': 0.36; 'yield': 0.36; 'doing': 0.36; 'method': 0.36; 'should': 0.36; 'example,': 0.37; 'turn': 0.37; 'list': 0.37; 'minimum': 0.38; 'represent': 0.38; 'skip:o 20': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'called': 0.40; 'remove': 0.60; 'black': 0.61; 'length': 0.61; 'matter': 0.61; "you're": 0.61; 'guarantee': 0.63; 'skip:n 10': 0.64; 'forward': 0.65; 'here': 0.66; '2015': 0.84; 'off"': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=pSExqhiZjTz+ZRCdeNUWXPrcsqnGrTmtLXyne/Vv7dA=; b=Tz4eaSR2RnmCLgILWx7oJx5mOP2DyWU9Y+Ryql4RgEGG3jqXJinEizQpH1tK2nGcne OVpQlAImI8noBn6mKwdBe+cqHIWZIJvxX10fLNTgowgtSN1kwwidtlw+QvNOENt6FyuS 470xy3CP3Vbvn1BmAziU/FuwJ0ErWOjCwLgTBfY1h/tHA3dJu4FFXqpPjSqW8pu6QfLj kU8lH5wAI1gtC+8lKTTAYPSRLiwgzVFi9J22TgvyyBLmnK/+/FItEUhWcziu2GmbcjjM 9Z+TG53XBVqle0nOekqA5Qk/a3IyzHWHTwZw711qRrvcV2rP5fXPp4YtQpd5vwMmprNj Z7bg==
X-Received by 10.50.57.112 with SMTP id h16mr7493062igq.35.1428449161389; Tue, 07 Apr 2015 16:26:01 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <53f4c2fd-a20c-4d09-8719-8f3f9b80670b@googlegroups.com>
References <53f4c2fd-a20c-4d09-8719-8f3f9b80670b@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Tue, 7 Apr 2015 17:25:21 -0600
Subject Re: Code critique please
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.121.1428449165.12925.python-list@python.org> (permalink)
Lines 81
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1428449165 news.xs4all.nl 2833 [2001:888:2000:d::a6]:50228
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:88628

Show key headers only | View raw


On Tue, Apr 7, 2015 at 4:43 PM,  <kai.peters@gmail.com> wrote:
> def RenderByte(draw, byte, x, y):

Python function and method names customarily use the
lowercase_with_underscores style.

>     blist = list(bin(byte).lstrip('0b')) # turn byte into list with 8 elements,

There's no guarantee that the resulting list will have 8 elements
here. The result of bin() will use the minimum number of digits needed
to represent the value, which has nothing to do with the size of a
byte. If you want exactly 8, use str.zfill to add zeroes.

Also, lstrip('0b') will remove *all* occurrences of the characters '0'
and 'b' from the left of the string, not just the exact substring
'0b'. So for example, bin(0).lstrip('0b') would return the empty
string since all the characters in the string are either '0' or 'b'.

That said, all this string manipulation to convert from an int to a
list of bits smells a bit funky to me. I'd probably write a generator
to yield the bits from the number. Something like:

    def bits(number, length=None):
        if length is None:
            length = number.bit_length()
        for i in range(length):
            yield (number >> i) & 1

Now you can iterate directly over bits(byte, 8), or if you really want
a list then you can call list(bits(byte, 8)).

>     c = 0                                # each representing one bit
>     for bit in blist:
>         if bit:
>             draw.point((x + c, y), fcolor)
>
>         c += 1

Instead of incrementing c on each loop, use enumerate.

    for c, bit in enumerate(blist):

>     return

Not needed if you're not returning a value. It's perfectly okay to
just let execution "fall off" the end of the function.

>     # get out of here if EPD file not present
>     if not os.path.isfile(epdfilename):
>         print 'file not found: ' + edpfilename

Recommend using print's function syntax for forward compatibility with Python 3:

    print('file not found: ' + edpfilename)

Also consider doing "from __future__ import print_function" at the top
of the file so that print really is a function, although it doesn't
matter in this case.

> xdim   = 1024
> ydim   = 1280
> header = 16
> black  = 0
> white  = 1
> bcolor = black
> fcolor = white

Constants, particularly global ones, are conventionally
UPPERCASE_WITH_UNDERSCORES.

> EPD_2_Image(epdfilename, imagefilename)

If you use the construct

    if __name__ == '__main__':
        EPD_2_Image(epdfilename, imagefilename)

then the function will automatically be called when the file is
executed as a script, but it will also let you import the file from
other modules and call it on your own terms, should you ever desire to
do that.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Code critique please kai.peters@gmail.com - 2015-04-07 15:43 -0700
  Re: Code critique please Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-07 17:25 -0600
  Re: Code critique please Chris Kaynor <ckaynor@zindagigames.com> - 2015-04-07 16:38 -0700
  Re: Code critique please Cameron Simpson <cs@zip.com.au> - 2015-04-08 09:49 +1000
  Re: Code critique please kai.peters@gmail.com - 2015-04-07 16:51 -0700
  Re: Code critique please Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-04-08 01:54 +0100
  Re: Code critique please Robert Kern <robert.kern@gmail.com> - 2015-04-08 13:25 +0100
  Re: Code critique please Peter Otten <__peter__@web.de> - 2015-04-09 12:26 +0200

csiph-web