comp.lang.python - 26 new messages in 9 topics - digest
comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en
comp.lang.python@googlegroups.com
Today's topics:
* Tkinter GUI Error - 8 messages, 6 authors
http://groups.google.com/group/comp.lang.python/t/00e1128246e4aefa?hl=en
* efficient way to process data - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/f7c2c58424bf2b3e?hl=en
* L[:] - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/8c4883bbe077624c?hl=en
* plotting slows down - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/22f280b151f3c221?hl=en
* Code review? - 5 messages, 4 authors
http://groups.google.com/group/comp.lang.python/t/25d5dfd105727f9b?hl=en
* proposal: bring nonlocal to py2.x - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/643e84b50e561338?hl=en
* Mistake or Troll (was Re: 'Straße' ('Strasse') and Python 2) - 1 messages, 1
author
http://groups.google.com/group/comp.lang.python/t/93ddbbff468ab95d?hl=en
* Python example source code - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/8594472fa123f77e?hl=en
* What's correct Python syntax? - 4 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/02a96ec3e80cc46a?hl=en
==============================================================================
TOPIC: Tkinter GUI Error
http://groups.google.com/group/comp.lang.python/t/00e1128246e4aefa?hl=en
==============================================================================
== 1 of 8 ==
Date: Mon, Jan 13 2014 10:49 am
From: fluttershy363@gmail.com
Inside the function is where I am having the problem, I am trying to get it to delete the label so that it may then replace it with a shorter text.
Here is the full code:
from tkinter import *
import random
main = Tk()
main.title("Crack the Code")
def check1():
entry = entry1var.get()
if entry == num1:
labelent1.destroy()
labelent1 = Label(main, text="Correct!",fg="green").grid(row = 0, column = 3)
elif entry > num1:
labelent1.destroy()
labelent1 = Label(main, text="Too Big",fg="red").grid(row = 0, column = 3)
elif entry < num1:
labelent1.destroy()
labelent1 = Label(main, text="Too Small",fg="red").grid(row = 0, column = 3)
global num1
global num2
global num3
num1 =str(random.randint(10,99))
num2 =str(random.randint(10,99))
num3 =str(random.randint(10,99))
mastercode = num1+num2+num3
entry1var = StringVar()
entry2var = StringVar()
entry3var = StringVar()
number1 = Label(main, text="Number 1").grid(row = 0, column = 0)
number2 = Label(main, text="Number 2").grid(row = 1, column = 0)
number3 = Label(main, text="Number 3").grid(row = 2, column = 0)
entry1 = Entry(main, textvariable=entry1var).grid(row=0,column=1)
entry2 = Entry(main, textvariable=entry2var).grid(row=1,column=1)
entry3 = Entry(main, textvariable=entry3var).grid(row=2,column=1)
button1 = Button(main, text="Try Number",command=check1).grid(row=0,column=2)
button2 = Button(main, text="Try Number").grid(row=1,column=2)
button3 = Button(main, text="Try Number").grid(row=2,column=2)
labelent1 = Label(main, text="Waiting for Input").grid(row = 0, column = 3)
labelent2 = Label(main, text="Waiting for Input").grid(row = 1, column = 3)
labelent3 = Label(main, text="Waiting for Input").grid(row = 2, column = 3)
mastercodelabel= Label(main, text="Enter master code below:").grid(row=3,column=1)
mastercodeentry= Entry(main).grid(row=4,column=1)
mastercodebutton= Button(main,text="Enter").grid(row=4,column=2)
#main.config(menu=menubar)
main.mainloop()
And this is the error displayed when clicking on button1:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "C:/Users/User/Desktop/Programming/Tkinter/Tkinter.py", line 15, in check1
labelent1.destroy()
UnboundLocalError: local variable 'labelent1' referenced before assignment
Thanks, Lewis.
== 2 of 8 ==
Date: Mon, Jan 13 2014 10:51 am
From: Lewis Wood
Forgot to mention I am using Python 3.3.3
== 3 of 8 ==
Date: Mon, Jan 13 2014 11:03 am
From: Christian Gollwitzer
Am 13.01.14 19:49, schrieb fluttershy363@gmail.com:
>
> Inside the function is where I am having the problem, I am trying to get it to delete the label so that it may then replace it with a shorter text.
> Here is the full code:
>
>
>
>
> from tkinter import *
> import random
> main = Tk()
> main.title("Crack the Code")
>
> def check1():
> entry = entry1var.get()
> if entry == num1:
> labelent1.destroy()
> labelent1 = Label(main, text="Correct!",fg="green").grid(row = 0, column = 3)
This is the wrong way to do it. Yes, in principle you could remove the
label and put a new one there; but it's much better to just change the
text of it by means of either
labelent1.configure(text="New text ")
or by linking a variable with the label variable at the setup time
somestringvar = StringVar("initial text")
Label(main, textvariable=somestringvar)
and then change that variable
somestringvar.set("New text")
Both of these don't solve the error, though; it has nothing to do with
Tk, you just did not make labelent1 global. However, I strongly advise
to use an object for the entire window, where you make this labelent1 an
instance variable (put into self).
Christian
== 4 of 8 ==
Date: Mon, Jan 13 2014 11:21 am
From: Lewis Wood
When I try to use the labelent1.configure, it greets me with an error:
AttributeError: 'NoneType' object has no attribute 'configure'
I changed the labelent's to global. Don't suppose you know why?
Also how would I go about using an object for the entire window. I am still a Novice at Tkinter and Python but can make my way around it.
== 5 of 8 ==
Date: Mon, Jan 13 2014 11:36 am
From: Peter Otten <__peter__@web.de>
fluttershy363@gmail.com wrote:
> Inside the function is where I am having the problem, I am trying to get
> it to delete the label so that it may then replace it with a shorter text.
> Here is the full code:
> def check1():
> entry = entry1var.get()
> if entry == num1:
> labelent1.destroy()
> labelent1 = Label(main, text="Correct!",fg="green").grid(row = 0,
> column = 3)
> elif entry > num1:
> labelent1.destroy()
> labelent1 = Label(main, text="Too Big",fg="red").grid(row = 0,
> column = 3)
> elif entry < num1:
> labelent1.destroy()
> labelent1 = Label(main, text="Too Small",fg="red").grid(row = 0,
> column = 3)
> And this is the error displayed when clicking on button1:
>
> Exception in Tkinter callback
> Traceback (most recent call last):
> File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
> return self.func(*args)
> File "C:/Users/User/Desktop/Programming/Tkinter/Tkinter.py", line 15, in
> check1
> labelent1.destroy()
> UnboundLocalError: local variable 'labelent1' referenced before assignment
>
>
> Thanks, Lewis.
Kudos, your problem description is very clear!
Your post would be perfect, had you reduced the number of Labels from three
to one ;)
The error you are seeing has nothing to do with the GUI. When you assign to
a name inside a function Python determines that the name is local to that
function. A minimal example that produces the same error is
>>> a = "global"
>>> def test():
... print(a)
... a = "local"
...
>>> test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in test
UnboundLocalError: local variable 'a' referenced before assignment
The name `a` passed to print() references the local `a` which is not yet
defined. A possible fix is to tell Python to reference the global `a`
>>> a = "global"
>>> def test():
... global a
... print(a)
... a = "local"
...
>>> test()
global
>>> a
'local'
However, in the case of your GUI code you should not destroy and create
Label instances -- it is more efficient (and easier I think) to modify the
Label's text:
(1) working demo with 'global' -- don't do it that way:
from tkinter import *
main = Tk()
def check1():
global labelent1
labelent1.destroy()
labelent1 = Label(main, text="Correct!", fg="green")
labelent1.grid(row = 0, column = 3) # must be a separate statement as
# grid() returns None
Button(main, text="Try Number", command=check1).grid(row=0, column=2)
labelent1 = Label(main, text="Waiting for Input")
labelent1.grid(row=0, column=3) # must be a separate statement as
# grid() returns None
main.mainloop()
(2) The way to go, modify the label text instead of replacing it:
from tkinter import *
main = Tk()
def check1():
labelent1.configure(text="Correct!", fg="green")
Button(main, text="Try Number", command=check1).grid(row=0, column=2)
labelent1 = Label(main, text="Waiting for Input")
labelent1.grid(row=0, column=3)
main.mainloop()
> global num1
By the way, global declarations on the module level have no effect.
== 6 of 8 ==
Date: Mon, Jan 13 2014 5:39 pm
From: Dennis Lee Bieber
On Mon, 13 Jan 2014 10:49:07 -0800 (PST), fluttershy363@gmail.com declaimed
the following:
>
>Inside the function is where I am having the problem, I am trying to get it to delete the label so that it may then replace it with a shorter text.
>Here is the full code:
>
<snip>
>
>global num1
>global num2
>global num3
These do not do what you think they do...
"global" is used INSIDE of functions (def) to specify that references
to the name on the inside MUST access the module level name.
Note: read-only access does not need "global"; only bindings to the
name (assignments) have to have the "global" otherwise they define a local
name.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
== 7 of 8 ==
Date: Mon, Jan 13 2014 6:47 pm
From: Rick Johnson
On Monday, January 13, 2014 12:49:07 PM UTC-6, Lewis Wood wrote:
> labelent1 = Label(main, text="Correct!",fg="green").grid(row = 0, column = 3)
>
> [snip]
>
> UnboundLocalError: local variable 'labelent1' referenced before assignment
Observe the following interactive session and prepare to be enlightened.
## INCORRECT ##
py> from Tkinter import *
py> root = Tk()
py> label = Label(root, text="Blah").pack()
py> type(label)
<type 'NoneType'>
## CORRECT ##
py> label = Label(root, text="Blah")
py> label.pack()
py> label
<Tkinter.Label instance at 0x027C69B8>
py> type(label)
<type 'instance'>
## ANY QUESTIONS? ##
py> help()
== 8 of 8 ==
Date: Mon, Jan 13 2014 7:12 pm
From: Chris Angelico
On Tue, Jan 14, 2014 at 5:49 AM, <fluttershy363@gmail.com> wrote:
> entry = entry1var.get()
> if entry == num1:
> elif entry > num1:
> elif entry < num1:
>
> num1 =str(random.randint(10,99))
> num2 =str(random.randint(10,99))
> num3 =str(random.randint(10,99))
> mastercode = num1+num2+num3
Be careful of code like this. You've specified that your three parts
range from 10 through 99, so this will work as long as the user knows
this and enters exactly two digits. Doing inequality comparisons on
strings that represent numbers will work as long as they're the same
length, but if the lengths vary, the string comparisons will start at
the beginning - not what most people will expect. These are all true:
"2" > "10"
"3.14159" > "2,000,000"
"42" < "Life, the universe, and everything"
"00012" < "12"
If your intention is to have a six-digit number, you could simply ask
for one, and then format the pieces accordingly:
num = random.randint(1,999999)
num_str = "%06d" % num
You can then slice up num_str as needed (it'll have leading zeroes if
it needs them), or you can do numerical comparisons against num
itself.
ChrisA
==============================================================================
TOPIC: efficient way to process data
http://groups.google.com/group/comp.lang.python/t/f7c2c58424bf2b3e?hl=en
==============================================================================
== 1 of 2 ==
Date: Mon, Jan 13 2014 11:51 am
From: Larry Martell
On Mon, Jan 13, 2014 at 1:32 PM, Chris Angelico <rosuav@gmail.com> wrote:
> On Tue, Jan 14, 2014 at 5:27 AM, Larry Martell <larry.martell@gmail.com> wrote:
>> Thanks. Unfortunately this has been made a low priority task and I've
>> been put on to something else (I hate when they do that).
>
> Ugh, I know that feeling all too well!
Right? You're deep in debugging and researching and waking up in the
middle of the night with potential solutions, and then they say put it
aside. It's so hard to put it down, and then when you pick it up later
(sometimes months) you're like WTF is this all about. I recently
picked up something I had to put down in September - spent an entire
day getting back to where I was, then it was put on the back burner
again.
> Life's better when you're
> unemployed, and you can choose the interesting problems to work on.
Ahhh .... I don't think so.
> Apart from the "has to be in MySQL" restriction (dodged now that the
> work's all being done in Python anyway),
It's a big existing django app.
> yours is _definitely_ an
> interesting problem.
Thanks! I thought so too.
== 2 of 2 ==
Date: Mon, Jan 13 2014 12:47 pm
From: Petite Abeille
On Jan 13, 2014, at 7:42 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> I've not followed this thread closely but would this help http://pandas.pydata.org/ ? When and if you get back to it, that is!!!
I doubt it. The mean overhead by far would be to shuffle pointless data between the server & client. Best to process data closest to their source.
On the other hand:
http://devour.com/video/never-say-no-to-panda/
==============================================================================
TOPIC: L[:]
http://groups.google.com/group/comp.lang.python/t/8c4883bbe077624c?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Jan 13 2014 12:23 pm
From: Terry Reedy
On 1/13/2014 4:00 AM, Laszlo Nagy wrote:
>
>> Unless L is aliased, this is silly code.
> There is another use case. If you intend to modify a list within a for
> loop that goes over the same list, then you need to iterate over a copy.
> And this cannot be called an "alias" because it has no name:
for i in somelist: creates a second reference to somelist that somewhere
in the loop code has a name, so it is effectively an 'alias'. The
essential point is that there are two access paths to the same object.
> for idx,item in enumerate(L[:]):
> # do something with L here, including modification
The copy is only needed in the above if one inserts or deletes. But if
one inserts or deletes more than one item, one nearly always does better
to iterate through the original and construct a new list with new items
added and old items not copied.
--
Terry Jan Reedy
==============================================================================
TOPIC: plotting slows down
http://groups.google.com/group/comp.lang.python/t/22f280b151f3c221?hl=en
==============================================================================
== 1 of 3 ==
Date: Mon, Jan 13 2014 12:33 pm
From: Steven D'Aprano
On Mon, 13 Jan 2014 08:26:11 -0500, Dave Angel wrote:
> norman.elliott@gmail.com Wrote in message:
>> [code]
>> #!/usr/bin/python
>> from graphics import *
>
> First things first. what operating system are you using, and
> where did you get the mysterious graphics. py? Thanks for telling us
> python 2.7.3
>
> Next, please repost any source code with indentation preserved.
He did. If you look at the original message as posted to python-
list@python.org and comp.lang.python, e.g. here:
https://mail.python.org/pipermail/python-list/2014-January/664430.html
you will see that the message is correctly indented with tabs.
> Your message shows it all flushed to the left margin, probably due to
> posting in html mode. Use text mode here.
Looks like perhaps Gmane is stripping tabs from their mirror. You should
report that as a bug to them.
--
Steven
== 2 of 3 ==
Date: Mon, Jan 13 2014 1:42 pm
From: Terry Reedy
On 1/13/2014 12:45 PM, Chris Angelico wrote:
> On Tue, Jan 14, 2014 at 4:39 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>> On Mon, Jan 13, 2014 at 6:26 AM, Dave Angel <davea@davea.name> wrote:
>>> Next, please repost any source code with indentation preserved.
>>> Your message shows it all flushed to the left margin, probably
>>> due to posting in html mode. Use text mode here.
>>
>> That's odd, the message that I got includes proper indentation and is
>> plain text, not html.
>
> Also what I saw. Dave, do you get the newsgroup or the mailing list? I
> get the mailing list - it's possible the HTML version got stripped by
> Mailman.
I am reading via gmane. Viewing the source, there is no html.
BUT, indents are with tabs, not spaces. Some readers just delete tabs,
as there is no standard for conversion to spaces, especially with
proportional fonts. Thunderbird used to do this, but now uses tab stops
every 8 spaces (maybe because a switched to a fixed font?) This means
that the first tab gives an indent 8 chars in the original post, 6 in
the first quotation, and, I presume, 4 in a second quotation, etc. It
works better to post code with space indents.
--
Terry Jan Reedy
== 3 of 3 ==
Date: Tues, Jan 14 2014 1:32 am
From: Dave Angel
Norman Elliott <norman.elliott@gmail.com> Wrote in message:
>
> I cannot see how to change from html to text mode in chromium or within the group.
>
You already did post in text mode, my error. The new newsreader
I'm using apparently eats tabs.
--
DaveA
----Android NewsGroup Reader----
http://www.piaohong.tk/newsgroup
==============================================================================
TOPIC: Code review?
http://groups.google.com/group/comp.lang.python/t/25d5dfd105727f9b?hl=en
==============================================================================
== 1 of 5 ==
Date: Mon, Jan 13 2014 12:43 pm
From: Steven D'Aprano
On Tue, 14 Jan 2014 03:40:25 +1100, Chris Angelico wrote:
> Incidentally, is there a reason you're using Python 2.6? You should be
> able to upgrade at least to 2.7, and Flask ought to work fine on 3.3
> (the current stable Python). If it's the beginning of your project, and
> you have nothing binding you to Python 2, go with Python 3. Converting a
> small project now will save you the job of converting a big project in
> ten years' time
Everything you say is correct, but remember that there is a rather large
ecosystem of people writing code to run on servers where the supported
version of Python is 2.6, 2.5, 2.4 and even 2.3. RedHat, for example,
still has at least one version of RHEL still under commercial support
where the system Python is 2.3, at least that was the case a few months
back, it may have reached end-of-life by now. But 2.4 will definitely
still be under support.
(I don't believe there is any mainstream Linux distro still supporting
versions older than 2.3.)
Not everyone is willing, permitted or able to install Python other than
that which their OS provides, and we ought to respect that.
Hell, if somebody wants to ask questions about Python 1.5, we can answer
them! The core language is still recognisably Python, a surprisingly
large number of libraries were around back then (it was Python 1.4 or 1.5
which first got the reputation of "batteries included"), and I for one
still have it installed so I can even test code for it.
--
Steven
== 2 of 5 ==
Date: Mon, Jan 13 2014 3:34 pm
From: Chris Angelico
On Tue, Jan 14, 2014 at 7:43 AM, Steven D'Aprano <steve@pearwood.info> wrote:
> On Tue, 14 Jan 2014 03:40:25 +1100, Chris Angelico wrote:
>
>> Incidentally, is there a reason you're using Python 2.6? You should be
>> able to upgrade at least to 2.7, and Flask ought to work fine on 3.3
>> (the current stable Python). If it's the beginning of your project, and
>> you have nothing binding you to Python 2, go with Python 3. Converting a
>> small project now will save you the job of converting a big project in
>> ten years' time
>
> Everything you say is correct, but remember that there is a rather large
> ecosystem of people writing code to run on servers where the supported
> version of Python is 2.6, 2.5, 2.4 and even 2.3. RedHat, for example,
> still has at least one version of RHEL still under commercial support
> where the system Python is 2.3, at least that was the case a few months
> back, it may have reached end-of-life by now. But 2.4 will definitely
> still be under support.
Pledging that your app will run on the system Python of RHEL is
something that binds you to a particular set of versions of Python.
It's not just library support that does that.
ChrisA
== 3 of 5 ==
Date: Mon, Jan 13 2014 11:22 pm
From: Bob Martin
in 714500 20140113 233415 Chris Angelico <rosuav@gmail.com> wrote:
>On Tue, Jan 14, 2014 at 7:43 AM, Steven D'Aprano <steve@pearwood.info> wrote:
>> On Tue, 14 Jan 2014 03:40:25 +1100, Chris Angelico wrote:
>>
>>> Incidentally, is there a reason you're using Python 2.6? You should be
>>> able to upgrade at least to 2.7, and Flask ought to work fine on 3.3
>>> (the current stable Python). If it's the beginning of your project, and
>>> you have nothing binding you to Python 2, go with Python 3. Converting a
>>> small project now will save you the job of converting a big project in
>>> ten years' time
>>
>> Everything you say is correct, but remember that there is a rather large
>> ecosystem of people writing code to run on servers where the supported
>> version of Python is 2.6, 2.5, 2.4 and even 2.3. RedHat, for example,
>> still has at least one version of RHEL still under commercial support
>> where the system Python is 2.3, at least that was the case a few months
>> back, it may have reached end-of-life by now. But 2.4 will definitely
>> still be under support.
>
>Pledging that your app will run on the system Python of RHEL is
>something that binds you to a particular set of versions of Python.
>It's not just library support that does that.
Does any Linux distro ship with Python 3? I haven't seen one.
== 4 of 5 ==
Date: Mon, Jan 13 2014 11:29 pm
From: Bayram Güçlü
On 14-01-2014 11:22, Bob Martin wrote:
> in 714500 20140113 233415 Chris Angelico <rosuav@gmail.com> wrote:
>> On Tue, Jan 14, 2014 at 7:43 AM, Steven D'Aprano <steve@pearwood.info> wrote:
>>> On Tue, 14 Jan 2014 03:40:25 +1100, Chris Angelico wrote:
>>>
>>>> Incidentally, is there a reason you're using Python 2.6? You should be
>>>> able to upgrade at least to 2.7, and Flask ought to work fine on 3.3
>>>> (the current stable Python). If it's the beginning of your project, and
>>>> you have nothing binding you to Python 2, go with Python 3. Converting a
>>>> small project now will save you the job of converting a big project in
>>>> ten years' time
>>>
>>> Everything you say is correct, but remember that there is a rather large
>>> ecosystem of people writing code to run on servers where the supported
>>> version of Python is 2.6, 2.5, 2.4 and even 2.3. RedHat, for example,
>>> still has at least one version of RHEL still under commercial support
>>> where the system Python is 2.3, at least that was the case a few months
>>> back, it may have reached end-of-life by now. But 2.4 will definitely
>>> still be under support.
>>
>> Pledging that your app will run on the system Python of RHEL is
>> something that binds you to a particular set of versions of Python.
>> It's not just library support that does that.
>
> Does any Linux distro ship with Python 3? I haven't seen one.
>
Debian GNU/Linux
https://wiki.debian.org/Python/Python3.3
== 5 of 5 ==
Date: Mon, Jan 13 2014 11:36 pm
From: Chris Angelico
On Tue, Jan 14, 2014 at 6:22 PM, Bob Martin <bob.martin@excite.com> wrote:
> Does any Linux distro ship with Python 3? I haven't seen one.
On most Debian-based distros, you can simply 'apt-get install
python3', and you'll get some 3.x version (in Debian Squeeze, that's
3.1, Debian Wheezy packages 3.2; Ubuntu since Raring gives you 3.3).
Whether or not you actually have it - or python2 for that matter -
installed depends on your choices, anything that depends on it will
pull it in or you can grab it manually.
Arch Linux ships 3.3.3 under the name "python", and 2.7.6 under the
name "python2" - an inversion of the Debian practice. Other distros
are looking toward shifting, too.
I'd guess that all mainstream distributions carry both branches. It's
just a question of what people get when they ask for "Python" in the
most normal way to do that.
ChrisA
==============================================================================
TOPIC: proposal: bring nonlocal to py2.x
http://groups.google.com/group/comp.lang.python/t/643e84b50e561338?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Jan 13 2014 1:26 pm
From: Terry Reedy
On 1/13/2014 9:47 AM, Neal Becker wrote:
> py3 includes a fairly compelling feature: nonlocal keywork
[keyword]
> But backward compatibility is lost.
I am not sure what your particular point is. Every new feature, in any
release, if used, makes code not compatible with earlier releases that
do not have the feature. Every new feature is compelling to someone, and
to use it, one must use a version that has it.
> It would be very helpful if this was available on py2.x.
For every new feature, there is someone who thinks it would be helpful
if it were availale in an earlier version. Backports of library features
are sometimes available on PyPI, but this cannot be done for syntax
features like 'nonlocal'.
'2.x' refers to a sequence of feature-frozen versions. It literally
means '2.0 to 2.7', but may refer to '2.2 to 2.7' (because 2.2 gained
new classes and iterators) or even a more restricted sequence. Core
developers consider 3.2, or maybe a later version, to be the successor
of 2.7.
--
Terry Jan Reedy
==============================================================================
TOPIC: Mistake or Troll (was Re: 'Straße' ('Strasse') and Python 2)
http://groups.google.com/group/comp.lang.python/t/93ddbbff468ab95d?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Jan 13 2014 3:05 pm
From: Terry Reedy
On 1/13/2014 4:54 AM, wxjmfauth@gmail.com wrote:
> I'm afraid I'm understanding Python (on this
> aspect very well).
Really?
> Do you belong to this group of people who are naively
> writing wrong Python code (usually not properly working)
> during more than a decade?
To me, the important question is whether this and previous similar posts
are intentional trolls designed to stir up the flurry of responses they
get or 'innocently' misleading or even erroneous. If your claim of
understanding Python and Unicode is true, then this must be a troll
post. Either way, please desist, or your access to python-list from
google-groups may be removed.
> 'ß' is the the fourth character in that text "Straße"
> (base index 0).
As others have said, in the *unicode text "Straße", 'ß' is the fifth
character, at character index 4, ...
> This assertions are correct (byte string and unicode).
whereas, when the text is encoded into bytes, the byte index depends on
the encoding and the assertion that it is always 4 is incorrect. Did you
know this or were you truly ignorant?
>>>> sys.version
> '2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]'
>>>> assert 'Straße'[4] == 'ß'
Sometimes true, sometimes not.
>>>> assert u'Straße'[4] == u'ß'
> PS Nothing to do with Py2/Py3.
This issue has everything to do with Py2, where 'Straße' is encoded
bytes, versus Py3, where 'Straße' is unicode text where each character
of that word takes one code unit, whether each is 2 bytes or 4 bytes.
If you replace 'ß' with any astral (non-BMP) character, this issue
appears even for unicode text in 3.2-, where an astral character
requires 2, not 1, code units on narrow builds, thereby screwing up
indexing, just as can happen for encoded bytes. In 3.3+, all characters
use 1 code unit and indexing (and slicing) always works properly. This
is another unicode issue where you appear not to understand, but might
just be trolling.
--
Terry Jan Reedy
==============================================================================
TOPIC: Python example source code
http://groups.google.com/group/comp.lang.python/t/8594472fa123f77e?hl=en
==============================================================================
== 1 of 1 ==
Date: Mon, Jan 13 2014 11:41 pm
From: Koray YILMAZ
On Sunday, January 12, 2014 4:37:18 PM UTC+2, ngangsia akumbo wrote:
> where can i find example source code by topic?
>
> Any help please
Hi,
Take a look at https://github.com/dabeaz/python-cookbook. I am using "NapCat" mobile application to read codes.
-Koray
==============================================================================
TOPIC: What's correct Python syntax?
http://groups.google.com/group/comp.lang.python/t/02a96ec3e80cc46a?hl=en
==============================================================================
== 1 of 4 ==
Date: Tues, Jan 14 2014 12:46 am
From: Igor Korot
Hi, ALL,
I'm trying to process a file which has following lines:
192.168.1.6 > 192.168.1.7: ICMP echo request, id 100, seq 200, length 30
(this is the text file out of tcpdump)
Now I can esily split the line twice: once by ':' symbol to separate
address and the protocol information and the second time by ',' to get
information about the protocol.
However, I don't need all the protocol info. All I'm interested in is
the last field, which is length.
Is there a way to write something like this:
for data in f:
(address,traffic) = string.split(data, ':')
length = string.split(traffic, ',')[3]
I'm interesred in only one element, so why should care about everything else?
This can be easily done in Perl, but I'm stuck with Python now. ;-)
Thank you.
== 2 of 4 ==
Date: Tues, Jan 14 2014 12:54 am
From: Rustom Mody
On Tuesday, January 14, 2014 2:16:56 PM UTC+5:30, Igor Korot wrote:
> Hi, ALL,
> I'm trying to process a file which has following lines:
>
> 192.168.1.6 > 192.168.1.7: ICMP echo request, id 100, seq 200, length 30
>
> (this is the text file out of tcpdump)
>
>
> Now I can esily split the line twice: once by ':' symbol to separate
> address and the protocol information and the second time by ',' to get
> information about the protocol.
> However, I don't need all the protocol info. All I'm interested in is
> the last field, which is length.
>
>
>
> Is there a way to write something like this:
>
>
> for data in f:
> (address,traffic) = string.split(data, ':')
> length = string.split(traffic, ',')[3]
>
>
>
> I'm interesred in only one element, so why should care about everything else?
> This can be easily done in Perl, but I'm stuck with Python now. ;-)
>>> data="192.168.1.6 > 192.168.1.7: ICMP echo request, id 100, seq 200, length 30"
>>> (add,traff) = data.split(':')
>>> add
'192.168.1.6 > 192.168.1.7'
>>> traff
' ICMP echo request, id 100, seq 200, length 30'
>>> lenn = traff.split(',')
>>> lenn = traff.split(',')[3]
>>> lenn
' length 30'
>>>
== 3 of 4 ==
Date: Tues, Jan 14 2014 12:58 am
From: Chris Angelico
On Tue, Jan 14, 2014 at 7:46 PM, Igor Korot <ikorot01@gmail.com> wrote:
> 192.168.1.6 > 192.168.1.7: ICMP echo request, id 100, seq 200, length 30
>
> However, I don't need all the protocol info. All I'm interested in is
> the last field, which is length.
You can split on any string. If you're confident that this is the only
instance of the word "length", you can split on that:
for data in f:
# This will throw an exception if there's no " length "
# or if there are two of them. This means you're safe;
# if anything unexpected happens, you'll know.
_, length = data.split(" length ")
# process length
Alternatively, you can split on the space and take just the very last word:
for data in f:
length = data.split(" ")[-1]
# process length
Either way, the length will be a string. If you need it as an integer,
just do this:
length = int(length)
>From there, you can do whatever analysis you need.
Hope that helps!
ChrisA
== 4 of 4 ==
Date: Tues, Jan 14 2014 1:25 am
From: Igor Korot
Hi, Rustom,
On Tue, Jan 14, 2014 at 12:54 AM, Rustom Mody <rustompmody@gmail.com> wrote:
> On Tuesday, January 14, 2014 2:16:56 PM UTC+5:30, Igor Korot wrote:
>> Hi, ALL,
>> I'm trying to process a file which has following lines:
>>
>> 192.168.1.6 > 192.168.1.7: ICMP echo request, id 100, seq 200, length 30
>>
>> (this is the text file out of tcpdump)
>>
>>
>> Now I can esily split the line twice: once by ':' symbol to separate
>> address and the protocol information and the second time by ',' to get
>> information about the protocol.
>> However, I don't need all the protocol info. All I'm interested in is
>> the last field, which is length.
>>
>>
>>
>> Is there a way to write something like this:
>>
>>
>> for data in f:
>> (address,traffic) = string.split(data, ':')
>> length = string.split(traffic, ',')[3]
>>
>>
>>
>> I'm interesred in only one element, so why should care about everything else?
>> This can be easily done in Perl, but I'm stuck with Python now. ;-)
>
>
>>>> data="192.168.1.6 > 192.168.1.7: ICMP echo request, id 100, seq 200, length 30"
>>>> (add,traff) = data.split(':')
>>>> add
> '192.168.1.6 > 192.168.1.7'
>>>> traff
> ' ICMP echo request, id 100, seq 200, length 30'
>>>> lenn = traff.split(',')
>>>> lenn = traff.split(',')[3]
>>>> lenn
> ' length 30'
What if I want field 2 and field 3? ("seq 200" and "length 30")
Thank you.
>>>>
> --
> https://mail.python.org/mailman/listinfo/python-list
==============================================================================
You received this message because you are subscribed to the Google Groups "comp.lang.python"
group.
To post to this group, visit http://groups.google.com/group/comp.lang.python?hl=en
To unsubscribe from this group, send email to comp.lang.python+unsubscribe@googlegroups.com
To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.python/subscribe?hl=en
To report abuse, send email explaining the problem to abuse@googlegroups.com
==============================================================================
Google Groups: http://groups.google.com/?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home