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


Groups > comp.lang.python > #72566

Re: Unicode and Python - how often do you index strings?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subsequent': 0.05; 'subject:Python': 0.06; '---------': 0.07; 'indexing': 0.07; 'string': 0.09; '%s"': 0.09; 'delimited': 0.09; 'integers': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '-tkc': 0.16; 'alexander': 0.16; 'centers,': 0.16; 'dashes': 0.16; 'each)': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'ideally,': 0.16; 'iterated': 0.16; 'iteration,': 0.16; 'iterator': 0.16; 'presume': 0.16; 'subject:Unicode': 0.16; 'underscores': 0.16; 'wrote:': 0.18; 'discussion': 0.18; "python's": 0.19; 'examples': 0.20; 'import': 0.22; 'cc:addr:python.org': 0.22; 'headers': 0.24; 'mind.': 0.24; 'unicode': 0.24; 'looks': 0.24; '(or': 0.24; 'question': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; '[1]': 0.29; 'characters': 0.30; 'code': 0.31; 'lines': 0.31; 'question:': 0.31; 'file': 0.32; 'critical': 0.32; 'beginning': 0.33; 'implemented': 0.33; 'actual': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'something': 0.35; 'operations': 0.35; 'but': 0.35; 'there': 0.35; 'done,': 0.36; 'indexed': 0.36; 'sequence': 0.36; 'done': 0.36; 'doing': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'skip:- 20': 0.37; 'two': 0.37; 'mapping': 0.38; 'window': 0.38; 'files': 0.38; 'skip:- 10': 0.38; 'that,': 0.38; 'either': 0.39; 'how': 0.40; 'length': 0.61; 'name': 0.63; 'field': 0.63; 'linked': 0.65; 'to:addr:gmail.com': 0.65; 'close': 0.67; 'production': 0.68; 'skip:r 30': 0.69; 'below.': 0.71; 'end.': 0.84; 'n):': 0.84; 'received:50.22': 0.84; 'theories': 0.84; 'subject:you': 0.87
Date Tue, 3 Jun 2014 20:11:54 -0500
From Tim Chase <python.list@tim.thechases.com>
To Chris Angelico <rosuav@gmail.com>
Subject Re: Unicode and Python - how often do you index strings?
In-Reply-To <CAPTjJmr4iHdaCy61w2rz-oL6FcarRzzTeEU44Fxn2Z=gS0fh-Q@mail.gmail.com>
References <CAPTjJmr4iHdaCy61w2rz-oL6FcarRzzTeEU44Fxn2Z=gS0fh-Q@mail.gmail.com>
X-Mailer Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
X-Get-Message-Sender-Via boston.accountservergroup.com: authenticated_id: tim@thechases.com
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.10658.1401844358.18130.python-list@python.org> (permalink)
Lines 69
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1401844358 news.xs4all.nl 2882 [2001:888:2000:d::a6]:51359
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72566

Show key headers only | View raw


On 2014-06-04 10:39, Chris Angelico wrote:
> A current discussion regarding Python's Unicode support centres (or
> centers, depending on how close you are to the cent[er]{2} of the
> universe) around one critical question: Is string indexing common?
> 
> Python strings can be indexed with integers to produce characters
> (strings of length 1). They can also be iterated over from beginning
> to end. Lots of operations can be built on either one of those two
> primitives; the question is, how much can NOT be implemented
> efficiently over iteration, and MUST use indexing? Theories are
> great, but solid use-cases are better - ideally, examples from
> actual production code (actual code optional).

Many of my string-indexing uses revolve around a sliding window which
can be done with itertools[1], though I often just roll it as
something like

  n = 3
  for i in range(1 + len(s) - n):
    do_something(s[i:i+n])

So that could be supplanted by the SO iterator linked below.

The other use big case I have from production code involves a
column-offset delimited file where the headers have a row of
underscores under them delimiting the field widths, so it looks
something like

  EmpID     Name                Cost Center
  --------- ------------------- -----------------------------
  314159    Longstocking, Pippi RJ45
  265358    Davis, Miles        JA22
  979328    Bell, Alexander     RJ15

I then take row 2 and use it to make a mapping of header-name to a
slice-object for slicing the subsequent strings:

  import re
  r = re.compile('-+') # a sequence of 1+ dashes
  f = file("data.txt")
  headers = next(f)
  lines = next(f)
  header_map = dict((
      headers[i.start():i.end()].strip().upper(),
      slice(i.start(), i.end())
      )
    for i in r.finditer(lines)
    )
  for row in f:
    print("EmpID = %s" % row[header_map["EMPID"]].strip())
    print("Name = %s" % row[header_map["NAME"]].strip())
    # ...

which I presume uses string indexing under the hood.

Perhaps there's a better way of doing that, but it's what I currently
use to process these large-ish files (largest max out at 10-20MB each)

There might be other use-cases I've done, but those two leap to mind.

-tkc


[1]
http://stackoverflow.com/questions/6822725/rolling-or-sliding-window-iterator-in-python



Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Unicode and Python - how often do you index strings? Tim Chase <python.list@tim.thechases.com> - 2014-06-03 20:11 -0500

csiph-web