----- Original Message -----
> From: "kai peters" <kai.pet...@gmail.com>
> To: python-list@python.org
> Sent: Wednesday, 8 April, 2015 12:43:23 AM
> Subject: Code critique please
> 
> I just wrote this bit (coming from Pascal) and am wondering how
> seasoned Python programmers would have done the same? Anything
> terribly non-python?
> 
> As always, thanks for all input.
> 
> K
> 
> 
> 
> """
>  Creates a PNG image from EPD file
> """
> 
> import os, sys
> from PIL import Image, ImageFont, ImageDraw
> 
> #
> -----------------------------------------------------------------------------
> def RenderByte(draw, byte, x, y):
> 
>     blist = list(bin(byte).lstrip('0b')) # turn byte into list with 8
>     elements,
>     c = 0                                # each representing one bit
>     for bit in blist:
>         if bit:
>             draw.point((x + c, y), fcolor)
>             
>         c += 1
>     return
> 
> #

Apart from what has been already said, you could rewrite your function 
RenderByte this way (untested):

def render_byte(draw, byte, x, y):
  for point in [(x+c, y) for (c, bit) in enumerate(bin(byte)[2:]) if int(bit)]:
    draw.point(point, fcolor)


it involves important notions in the python language:
  - list comprehension, this is the  [...] part, where it combines filtering a 
list and applying a function to the values
  - slicing, bin(byte)[2:] returning the sequence stripped from its 2 first 
elements

Additional remarks : 
  - is fcolor defined ?
  - your test "if bit:" was probably wrong as it was testing either "0" or "1" 
which are both evaluated to True. In other words, bool("0") == True, bool(0) == 
False

Cheers,

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to