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


Groups > comp.lang.python > #72622

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!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'subject:Python': 0.06; 'indexing': 0.07; 'indices': 0.07; 'utf-8': 0.07; 'string': 0.09; 'integers': 0.09; 'lawrence': 0.09; 'prefix': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; '8bit%:32': 0.16; 'centers,': 0.16; 'ideally,': 0.16; 'internally': 0.16; 'iterated': 0.16; 'iteration': 0.16; 'iteration,': 0.16; 'offsets': 0.16; 'preserve': 0.16; 'rarely': 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; 'string:': 0.16; 'subject:Unicode': 0.16; 'wrote:': 0.18; 'discussion': 0.18; "python's": 0.19; 'examples': 0.20; '>>>': 0.22; 'example': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'unicode': 0.24; '(or': 0.24; 'question': 0.24; 'second': 0.26; 'header:X-Complaints- To:1': 0.27; 'idea': 0.28; 'chris': 0.29; 'characters': 0.30; 'needed.': 0.30; 'code': 0.31; 'question:': 0.31; 'critical': 0.32; 'interface': 0.32; 'quite': 0.32; 'text': 0.33; 'beginning': 0.33; 'cases': 0.33; 'implemented': 0.33; 'skip:b 30': 0.33; 'actual': 0.34; 'could': 0.34; "can't": 0.35; 'skip:s 30': 0.35; 'operations': 0.35; 'but': 0.35; 'indexed': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'example,': 0.37; 'two': 0.37; 'to:addr:python- list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'how': 0.40; 'most': 0.60; 'length': 0.61; 'new': 0.61; 'simple': 0.61; 'first': 0.61; 'linked': 0.65; 'here': 0.66; 'close': 0.67; 'production': 0.68; 'end.': 0.84; 'theories': 0.84; 'try,': 0.84; 'subject:you': 0.87; 'collective': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Unicode and Python - how often do you index strings?
Date Wed, 04 Jun 2014 12:10:41 +0200
Organization None
References <CAPTjJmr4iHdaCy61w2rz-oL6FcarRzzTeEU44Fxn2Z=gS0fh-Q@mail.gmail.com> <lmmkvk$73h$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p57bd8523.dip0.t-ipconnect.de
User-Agent KNode/4.11.5
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.10695.1401876662.18130.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1401876662 news.xs4all.nl 2943 [2001:888:2000:d::a6]:45820
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72622

Show key headers only | View raw


Mark Lawrence wrote:

> On 04/06/2014 01: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).
>>
>> I know the collective experience of python-list can't fail to bring up
>> a few solid examples here :)
>>
>> Thanks in advance, all!!
>>
>> ChrisA
>>
> 
> Single characters quite often, iteration rarely if ever, slicing all the
> time, but does that last one count?

The indices used for slicing typically don't come out of nowhere. A simple 
example would be

def strip_prefix(text, prefix):
    if text.startswith(prefix):
        text = text[len(prefix):] 
    return text

If both prefix and text use UTF-8 internally the byte offset is already 
known. The question is then how we can preserve that information.

The first approach that comes to mind is an int subtype:

>>> for i, c in enumerate("123αλφα"):
...     print(i, byteoffset(i), c)
... 
0 0 1
1 1 2
2 2 3
3 3 α
4 5 λ
5 7 φ
6 9 α

This would work in the strip_prefix() example, but lead to data corruption 
in most other cases unless limited to a specific string -- in which case it 
would no longer work with strip_prefix().

So a new interface would be needed. My second try, an object with two byte 
offsets linked to a specific string:

>>> span("foobar").startswith("oob")
>>> p = span("foobar").startswith("foo")
>>> p.replace("baz")
'bazbar'
>>> p.before()
''
>>> p.after()
'bar'
>>> span("foo bar baz").find("bar").replace("spam")
'foo spam bar'

I have no idea if that could work out...

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


Thread

Re: Unicode and Python - how often do you index strings? Peter Otten <__peter__@web.de> - 2014-06-04 12:10 +0200

csiph-web