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


Groups > comp.lang.python > #54937

Re: Weird bahaviour from shlex - line no

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <andipersti@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'attribute': 0.07; 'debug': 0.07; 'received:192.168.178': 0.07; 'string': 0.09; '"word"': 0.16; "','": 0.16; 'andreas': 0.16; 'bye,': 0.16; 'comma': 0.16; 'lexer': 0.16; 'popping': 0.16; 'received:192.168.178.20': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'daniel': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'thus': 0.29; 'characters': 0.30; 'this.': 0.32; 'subject:from': 0.34; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'message-id:@gmail.com': 0.38; 'depends': 0.38; 'somebody': 0.38; 'to:addr:python-list': 0.38; 'previous': 0.38; 'explain': 0.39; 'reported': 0.39; 'to:addr:python.org': 0.39; 'tell': 0.60; 'different': 0.65; 'default': 0.69; 'popped': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=PnOp2UeeKgCLPOE5YyXYsFGVJy92U7EXhquSoBf5L+k=; b=IIj1hTiKKJmpHABQ20rWtz5TgqPonaNCAtgWzOwoKpoW7+qA4tKg2KaTlCcBPWTOeD tkcJgcAqJqsOG2KRFN1bF6rJpt09kIua9APwrsEm5OGo/9oqq4Hn/f9ea5Yg/3XL125k Oxc/dRdeGpHSYZLlmtH7CxVOjDXg7SxRWJI59Fz6YcuiBC07yloXeoMxlSNJqXmwgslb KYPp4NtH11kD1jAgn9fUfOwY3XwWD6WOjl99X4TTcchUjUarLm7UbT3uLYPRtnhwcYme m+Hq4XE2VnDTKdzElB9F0z3okayybzu7y8OoCeEXpW9YnEsXluiuftxyPm2HWt8YMF2u MN5g==
X-Received by 10.204.68.142 with SMTP id v14mr10006894bki.18.1380366160836; Sat, 28 Sep 2013 04:02:40 -0700 (PDT)
Date Sat, 28 Sep 2013 13:02:37 +0200
From Andreas Perstinger <andipersti@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Weird bahaviour from shlex - line no
References <CAEvez=x1ySjRtR_7asYtU3FoRsC8c1g0g_OZQ_NE-Ok443bFrg@mail.gmail.com>
In-Reply-To <CAEvez=x1ySjRtR_7asYtU3FoRsC8c1g0g_OZQ_NE-Ok443bFrg@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
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.408.1380366168.18130.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1380366168 news.xs4all.nl 15934 [2001:888:2000:d::a6]:39413
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54937

Show key headers only | View raw


On 28.09.2013 08:26, Daniel Stojanov wrote:
> Can somebody explain this. The line number reported by shlex depends
> on the previous token. I want to be able to tell if I have just popped
> the last token on a line.
>
[SNIP]
>
> second = shlex.shlex("word1 word2,\nword3")

Punctuation characters like the comma are not considered as word 
characters by default and thus are seen as different tokens (consisting 
of only a single character):

 >>> lexer = shlex.shlex("foo, bar, ...")
 >>> token = lexer.get_token()
 >>> while token != lexer.eof:
...   print token
...   token = lexer.get_token()
...
foo
,
bar
,
.
.
.

If you want to treat them as "word" characters you need to add them to 
the string "wordchars" (a public attribute of the "shlex" instance):

 >>> lexer = shlex.shlex("foo.bar, baz")
 >>> lexer.wordchar += '.,'
 >>> print lexer.get_token()
foo.bar,
 >>> print lexer.get_token()
baz

There is also a "debug" attribute (with three different levels: 1, 2, 3; 
default value 0 means no debug output):

 >>> lexer = shlex.shlex("foo, bar, ...")
 >>> lexer.debug = 1
 >>> print lexer.get_token()
shlex: token='foo'
foo
 >>> print lexer.get_token()
shlex: popping token ','
,

Bye, Andreas

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


Thread

Re: Weird bahaviour from shlex - line no Andreas Perstinger <andipersti@gmail.com> - 2013-09-28 13:02 +0200

csiph-web