Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail 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; 'example:': 0.03; 'newbie': 0.05; 'root': 0.05; 'string.': 0.05; 'binary': 0.07; 'string': 0.09; 'bytes,': 0.09; 'bytes.': 0.09; 'converts': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:module': 0.09; 'subject:using': 0.09; 'python': 0.11; "'0'": 0.16; '200,000': 0.16; 'advance!': 0.16; 'command-line': 0.16; 'did,': 0.16; 'digits.': 0.16; 'finishes': 0.16; 'hex': 0.16; 'integer,': 0.16; 'interpreter,': 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; 'skipping': 0.16; 'subject:File': 0.16; 'subject:issue': 0.16; 'unresponsive': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'do.': 0.18; 'seems': 0.21; '>>>': 0.22; 'memory': 0.22; 'shell': 0.22; 'print': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'integer': 0.24; 'tend': 0.24; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'instruction': 0.29; 'tim': 0.29; 'characters': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'code': 0.31; "skip:' 10": 0.31; 'consumption': 0.31; 'decimal': 0.31; 'produces': 0.31; 'run': 0.32; 'becomes': 0.33; 'skip:# 10': 0.33; 'problem': 0.35; 'convert': 0.35; 'equal': 0.35; 'but': 0.35; 'there': 0.35; 'idle': 0.36; 'should': 0.36; 'two': 0.37; 'skip:o 20': 0.38; 'thank': 0.38; 'massive': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'even': 0.60; 'results.': 0.60; 'then,': 0.60; 'tell': 0.60; 'conversion': 0.61; 'first': 0.61; 'become': 0.64; 'here': 0.66; '200k': 0.84; 'horrible': 0.84; 'positively': 0.84; 'quicker': 0.84; 'power,': 0.91; 'convinced': 0.93; 'hand,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: File Read issue by using module binascii Date: Sun, 28 Apr 2013 09:42:13 +0200 Organization: None References: <9b5795ab-baec-4c0a-a3b4-1075cffc8744@googlegroups.com> <319pn8hgkc0pbj0099heq2nq3h5sv68g7s@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a88d.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1367134929 news.xs4all.nl 15975 [2001:888:2000:d::a6]:36862 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44464 Tim Roberts wrote: > Jimmie He wrote: > >>When I run the readbmp on an example.bmp(about 100k),the Shell is become >>to "No respose",when I change f.read() to f.read(1000),it is ok,could >>someone tell me the excat reason for this? Thank you in advance! >> >>Python Code as below!! >> >>import binascii >> >>def read_bmp(): >> f = open('example.bmp','rb') >> rawdata = f.read() #f.read(1000) is ok >> hexstr = binascii.b2a_hex(rawdata) #Get an HEX number >> bsstr = bin (int(hexstr,16))[2:] > > I suspect the root of the problem here is that you don't understand what > this is actually doing. You should run this code in the command-line > interpreter, one line at a time, and print the results. > > The "read" instruction produces a string with 100k bytes. The b2a_hex > then > produces a string with 200k bytes. Then, int(hexstr,16) takes that > 200,000 byte hex string and converts it to an integer, roughly equal to 10 > to the > 240,000 power, a number with some 240,000 decimal digits. You then > convert > that integer to a binary string. That string will contain 800,000 bytes. > You then drop the first two characters and print the other 799,998 bytes, > each of which will be either '0' or '1'. > > I am absolutely, positively convinced that's not what you wanted to do. > What point is there in printing out the binary equavalent of a bitmap? > > Even if you did, it would be much quicker for you to do the conversion one > byte at a time, completely skipping the conversion to hex and then the > creation of a massive multi-precision number. Example: Hm, if you fix the long integer arithmetic "problem" you should also attack the unbounded memory consumption problem in general ;) > f = open('example.bmp','rb') > rawdata = f.read() > bsstr = [] > for b in rawdata: > bsstr.append( bin(ord(b)) ) > bsstr = ''.join(bsstr) > > or even: > f = open('example.bmp','rb') > bsstr = ''.join( bin(ord(b))[2:] for b in f.read() ) Yes, the original is horrible newbie code ;) but that's what you tend to write while learning to program -- and python can handle it alright. On the other hand, Idle becomes unresponsive when I do >>> print("a"*10**6) in its shell. I'm still investigating, but the problem seems to be that it's a single line. >>> print(("a"*100+"\n") * 10**4) takes under 7 secs. Not as good as konsole (KDE's terminal emulation) which finishes in 0.5 secs, but acceptable.