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


Groups > comp.lang.python > #88705

Re: Code critique please

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'conventions': 0.07; 'assuming': 0.09; 'bits': 0.09; 'image,': 0.09; 'lookup': 0.09; 'pep': 0.09; 'pil': 0.09; 'pixel': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'spec': 0.09; 'width': 0.09; 'yeah,': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; '(),': 0.16; '(0,': 0.16; '(1),': 0.16; '1),': 0.16; 'byte,': 0.16; 'comprises': 0.16; 'data)': 0.16; 'elements,': 0.16; 'height,': 0.16; 'len(data)': 0.16; 'naming': 0.16; 'offsets': 0.16; 'pdf:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'terribly': 0.16; 'uncle': 0.16; 'url:peps': 0.16; 'width,': 0.16; 'y):': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'pointed': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'skip:{ 20': 0.24; 'subject:Code': 0.24; 'tend': 0.24; 'url:dev': 0.24; 'header': 0.24; 'handling': 0.26; 'pass': 0.26; 'header:X -Complaints-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'wondering': 0.29; 'raise': 0.29; 'subject:please': 0.30; "i'm": 0.30; 'code': 0.31; 'easier': 0.31; 'depth': 0.31; 'follows': 0.31; 'img': 0.31; 'libraries': 0.31; 'pascal': 0.31; 'struct': 0.31; 'file': 0.32; 'class': 0.32; 'there.': 0.32; 'url:python': 0.33; 'programmers': 0.33; 'table': 0.34; 'core': 0.34; "i'd": 0.34; 'but': 0.35; 'height': 0.36; 'representing': 0.36; 'sequence': 0.36; 'done': 0.36; 'url:org': 0.36; 'turn': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'how': 0.40; 'read': 0.60; 'color': 0.61; 'range': 0.61; 'offer': 0.62; 'email addr:gmail.com': 0.63; 'size.': 0.65; 'therefore': 0.72; 'faster.': 0.84; 'comfort': 0.96; 'picture': 0.97
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Code critique please
Date Thu, 09 Apr 2015 12:26:24 +0200
Organization None
References <53f4c2fd-a20c-4d09-8719-8f3f9b80670b@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd919a.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 <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.160.1428575207.12925.python-list@python.org> (permalink)
Lines 87
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1428575207 news.xs4all.nl 2871 [2001:888:2000:d::a6]:44812
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:88705

Show key headers only | View raw


kai.peters@gmail.com wrote:

> I just wrote this bit (coming from Pascal) 

>     if (((xdim / 8) * ydim) + header) <> filesize:

Yeah, either Pascal or Barry Warsaw is your uncle ;)

https://www.python.org/dev/peps/pep-0401/

> and am wondering how seasoned
> Python programmers would have done the same? Anything terribly non-python?

> def RenderByte(draw, byte, x, y):

Please read PEP 8 for naming conventions etc.

The function at the core of your code

> 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

can be fixed/micro-optimised, and if I were to do that I'd precreate a 
lookup table that maps every byte (i. e. value in the range 0...255) to a 
sequence of offsets 

lookup = [
    (),      # for b"\x00" the inner loop is empty
    (0),
    (1),
    (0, 1),
    ...
    (0, 1, 2, 3, 4, 5, 6, 7), # for b"\xFF" the inner loop comprises 
                              # all 8 bits
]
or to a 8x1 image, but my idea of a "seasoned Python programmer" will try to 
keep the big picture in mind -- and that is that handling individual 
bits/pixels in Python is inefficient. Therefore many libraries tend to offer 
an alternative that is both easier to use and faster.

In this case that's

   img = Image.frombytes("1", (width, height), data)
 
and with the extra comfort of reading the size from the EPD file

from PIL import Image
import struct

EPDFILE = "tmp.epd"
PNGFILE = "tmp.png"

class EPDError(Exception):
    pass

with open(EPDFILE, "rb") as f:
    header = f.read(16)
    width, height, colordepth = struct.unpack("<HHb", header[1:6])
    if colordepth != 1:
        raise EPDError("Unsupported color depth {}".format(colordepth))
    data = f.read()
    datasize = width // 8 * height
    if len(data) != datasize:
        raise EPDError(
            "Inconsistent pixel data size. "
            "Expected {} but got {} bytes".format(
                datasize, len(data)))
    img = Image.frombytes("1", (width, height), data)
    img.save(PNGFILE)


I'm assuming the header follows the format spec given in this PDF:

http://www.pervasivedisplays.com/_literature_112691/Developer_Guide_for_Demo_Kit

PS: I thought that Image.frombytes() was already pointed out to you, but I 
failed to find it in the archives. So there.

Back to comp.lang.python | Previous | NextPrevious 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