Please code review.

Martin Gracik grco at brute4ce.com
Tue Aug 2 08:22:17 EDT 2011


On Tue, Aug 2, 2011 at 1:45 PM, Karim <kliateni at gmail.com> wrote:

>
> Hello,
>
> I need a generator to create the cellname in a excell (using pyuno)
> document to assign value to
> the correct cell. 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]:
> ...             yield char + str(row)
> ...
> >>> list( _xrange_cellnames(rows=3,cols=**4))
> ['A1', 'B1', 'C1', 'D1', 'A2', 'B2', 'C2', 'D2', 'A3', 'B3', 'C3', 'D3']
>
>
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>

You could use something like this:

Python 2.7 (r27:82500, Sep 16 2010, 18:02:00)
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from itertools import imap, product
>>> import string
>>>
>>> def get_cellnames(rows, cols):
...     return imap(str().join, product(string.ascii_uppercase[:cols],
...                                     imap(str, range(1, rows + 1))))
...
>>> print list(get_cellnames(rows=3, cols=4))
['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3', 'D1', 'D2', 'D3']
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110802/0364fcc4/attachment-0001.html>


More information about the Python-list mailing list