Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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; 'assign': 0.04; '(using': 0.05; 'instance': 0.05; 'sys': 0.05; 'char': 0.07; 'subject:code': 0.07; 'python': 0.08; '__name__': 0.09; 'cc:addr:tutor': 0.09; 'compute': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'tuple': 0.09; 'wrote:': 0.15; '"__main__":': 0.16; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; '[gcc': 0.16; 'alphabetic': 0.16; 'cell.': 0.16; 'columns': 0.16; 'count,': 0.16; 'iterator': 0.16; 'linux2': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'row': 0.16; '>>>': 0.16; 'def': 0.16; 'sfxlen:2': 0.19; 'cc:2**0': 0.21; 'column': 0.22; 'cc:no real name:2**0': 0.22; 'alpha': 0.23; 'cheers': 0.23; 'code': 0.24; 'string': 0.26; 'function': 0.26; 'beyond': 0.28; 'skip:_ 20': 0.28; 'correct': 0.29; 'import': 0.29; 'yield': 0.29; 'cc:addr:python.org': 0.30; 'from:addr:web.de': 0.30; 'print': 0.32; "skip:' 10": 0.32; 'apr': 0.32; 'does': 0.32; 'it.': 0.33; 'to:addr:python-list': 0.34; 'header:X-Complaints-To:1': 0.34; 'instead': 0.34; 'there': 0.34; '...': 0.34; "isn't": 0.35; 'instead.': 0.37; 'some': 0.37; 'subject:Please': 0.37; 'but': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; 'something': 0.38; 'think': 0.38; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'format': 0.40; "i'd": 0.40; 'table': 0.40; 'your': 0.60; 'address': 0.61; 'maximum': 0.62; 'subject': 0.62; 'alternative': 0.70; 'engine': 0.71; 'directly?': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Please code review. Date: Tue, 02 Aug 2011 14:27:03 +0200 Organization: None References: <4E37E34B.5080707@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084aa9c.dip.t-dialin.net Cc: tutor@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312288021 news.xs4all.nl 23955 [2001:888:2000:d::a6]:39860 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10724 Karim wrote: > I need a generator to create the cellname in a excell (using pyuno) > document to assign value to the correct cell. Isn't there a way to use a (row, column) tuple directly? If so I'd prefer that. Also, there used to be an alternative format to address a spreadsheet cell with something like "R1C2". > The following code does this but do you have some > optimizations > on it, for instance to get the alphabetic chars instead of hard-coding it. > > Cheers > karim > > Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) > [GCC 4.5.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> def _xrange_cellnames(rows, cols): > ... """Internal iterator function to compute excell table > cellnames.""" > ... cellnames = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' > ... for row in xrange(1, rows+1): > ... for char in cellnames.replace('', ' ').split()[:cols]: That is interesting ;) But for maximum clarity use for char in cellnames[:cols]: instead. > ... yield char + str(row) > ... > >>> list( _xrange_cellnames(rows=3,cols=4)) > ['A1', 'B1', 'C1', 'D1', 'A2', 'B2', 'C2', 'D2', 'A3', 'B3', 'C3', 'D3'] Here's my (untested) attempt to handle columns beyond "Z": from itertools import chain, count, imap, islice, product from string import ascii_uppercase def columnnames(): alpha = (ascii_uppercase,) return imap("".join, chain.from_iterable(product(*alpha*i) for i in count(1))) def cellnames(columns, rows): for row in xrange(1, rows+1): for column in islice(columnnames(), columns): yield column + str(row) if __name__ == "__main__": import sys print list(cellnames(*map(int, sys.argv[1:]))) I think the subject has come up before; goo^h^h^h the search engine of your choice is your friend.