Schedule Task

2007-04-21 Thread Robert Rawlins - Think Blue
Hello Guys,

 

I've got a method in my app that I want to run every 20 minutes that the
application it running, what's the best way of achieving this? At the moment
I'm using this kind of method.

 

import sched

import time, sys

scheduler = sched.scheduler(time.time, time.sleep)

 

scheduler.enter(1.0, 0, sys.stdout.write, ("one\n",))

scheduler.run()

 

Now when the function runs after 20 minutes I just have it schedule itself
to run again in 20 minutes time, which seems to work for me quite nicely, it
just seems a little 'hacky' to keep the task reoccurring like that.

 

Is there any other method? I've spent some time on Google but couldn't
really find anything. Not sure if it makes any difference but I'm running
Linux.

 

Thanks,

 

Rob

-- 
http://mail.python.org/mailman/listinfo/python-list

newbie: HTTPS screen scraping

2007-04-21 Thread user
Hi,
Can anyone help me out here. I would like to authenticate myself to 
a website which uses HTTPS and then after authentication, I would like 
to get the contents of the webpage. How can this be done using python.
I have tried urllib and urllib2 but it has not solved my problem.

TIA
/varun
-- 
http://mail.python.org/mailman/listinfo/python-list


Class Not Auto-Init On Import

2007-04-21 Thread Robert Rawlins - Think Blue
Hello Guys,

 

>From my understanding of what I've read, the 'import' is meant to auto Init
my class ready for me to access its methods, but it doesn't appear too, I'm
having to init them myself before I can access them, like this.

 

import LocationService

 

Location = LocationService.LocationService()

 

LocationService.setIP('192.168.1.1')

 

Why is this the case? Should i not just be able to access the setIP() method
by doing LocationService.setIP('192.168.1.1') Without having to create my
own inited reference?

 

Thanks,

 

Rob

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: TK-grid problem, please help

2007-04-21 Thread Anton Vredegoor
Ray wrote:

> hi, I have a question about how to use .grid_forget (in python/TK)
> 
> I need to work on grid repeatly. everytime when a button is pressed,
> the rows of grid is different. such like, first time, it generate 10 
> rows of data.
> 2nd time, it maybe only 5 rows. so I need a way to RESET the grid data
> every time. how can I do it? by grid_forger()?, then would anyone can 
> help on
> how to use grid_forget()
> the sample code as following:

I'm not sure if it solves your problem but this modification of your 
code at least *looks* like it works better. The entries are completely 
destroyed so that the next time you call the function they can be 
recreated.

The trick I am using is to use a list in the arguments of the function 
but that is a bit of a hack, the list 'remembers' its state from the 
last time the function was called, I think one should use classes for 
bookkeeping such things instead.

from Tkinter import *

def mygrid(text,M = []):
   how to use grid_forget() to clean the grid??###
  while M:
  x = M.pop()
  x.destroy()
  rows = []
  count=int(text)
  for i in range(count):
  cols = []
  for j in range(4):
  e = Entry(frame3, relief=RIDGE)
  M.append(e)
  e.grid(row=i, column=j, sticky=NSEW)
  e.insert(END, '%d.%d' % (i, j))
  cols.append(e)
  rows.append(cols)


root=Tk()

frame1=Frame(root, width=150, height=100)
frame1.pack()

text=Entry(frame1)
text.pack(side=LEFT)

button=Button(frame1, text='generate grid', command=(lambda:
mygrid(text.get(

button.pack()

frame2=Frame(root, width=150, height=100)
frame2.pack()

button2=Button(frame2, text='exit', command=root.quit)
button2.pack()

frame3=Frame(root, width=150, height=300)
frame3.pack()

root.mainloop()

A.
-- 
http://mail.python.org/mailman/listinfo/python-list


vi editing mode does not work: Python install ok

2007-04-21 Thread pudugramam . s . narayanan
Hi
Could someone kindly give me an answer to this:

I have installed Python 2.5 on my linux box 2.4.9-e.35enterprise that has 
readline.so version 4.2.  During configure, I got the message
checking for readline in -lreadline... no
checking for rl_callback_handler_install in -lreadline... no
checking for rl_pre_input_hook in -lreadline... no
checking for rl_completion_matches in -lreadline... no

So I then updated Modules/Setup to include readline
readline: readline.c -L/usr/lib -lreadline -ltermcap.

Python got installed correctly as per the logs.  I then modified 
~/.inputrc with
set editing-mode vi

Now when I run the interpreter, I am not able to perform "vi" style 
editing.  How come???

Narayan


-
This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to UK legal entities.
-- 
http://mail.python.org/mailman/listinfo/python-list

Styled Output

2007-04-21 Thread Robert Rawlins - Think Blue
Chaps,

 

Hope you're all having a good weekend, I'm sure it'll only be the more 'hard
core' of you reading this, anyone with any sanity would be out in the
sunshine right now.

 

I'm running a program of mine from Linux bash script and it currently
outputs status messages to the command prompt, but they all look a little
boring to say the least, it'll be like this.

 

Application Started

Sucess

Application Configured

Failed

Retry

Application Configured

Success

 

What I'd ideally like to do it give it a little bit of format, so it perhaps
looks something like this.

 

Application Started
[Success]

Application Configured
[Success]

 

And perhaps have the word 'success' in green, or red for 'failed' and things
like that, just to make it a little more presentable to the user.

 

Any ideas on the best way to achieve this?

 

Thanks,

 

Rob

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Do other Python GUI toolkits require this? (OT)

2007-04-21 Thread Hendrik van Rooyen
 "Roel Schroeven" <[EMAIL PROTECTED]> wrote:

> Hendrik van Rooyen schreef:
> >  "Steve Holden" <[EMAIL PROTECTED]> wrote:
> > 
> >  
> >> Perhaps in Belgium they prefer climbing mountains over walking up and 
> >> down gentle hills?
> > 
> > Mountains ?   Hills ?   In Belgium ??
> > 
> > Its not called the battlefield of Europe for nothing...
> 
> I'm not sure if this adds anything of interest (well actually I'm pretty 
> sure it doesn't), but our king Albert I was a fanatic mountain climber, 
> until he died from a fall in 1934.
> 

Thanks - I was not aware of that.
Adds a whole new dimension to the phrase:

"The Fall of a King"

What was he climbing when he fell, btw?

- Hendrik

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Not Auto-Init On Import

2007-04-21 Thread Parthan SR

On 4/21/07, Robert Rawlins - Think Blue <[EMAIL PROTECTED]>
wrote:


 Hello Guys,

From my understanding of what I've read, the 'import' is meant to auto
Init my class ready for me to access its methods, but it doesn't appear too,
I'm having to init them myself before I can access them, like this.

import LocationService

Location = LocationService.LocationService()

LocationService.setIP('192.168.1.1')

Why is this the case? Should i not just be able to access the setIP()
method by doing LocationService.setIP('192.168.1.1') Without having to
create my own inited reference?


Did you try this,

from LocationService import *
LocationService.setIP('192.168.1.1')
# If you have something like LocationService --> LocationService() -->
setIP()

This is similar to using datetime.now() of the datetime module.


import datetime
datetime.now()

Traceback (most recent call last):
 File "", line 1, in 
AttributeError: 'module' object has no attribute 'now'

datetime.datetime.now()

datetime.datetime(2007, 4, 21, 14, 49, 4, 506309)

Now for the same,


from datetime import datetime
datetime.now()

datetime.datetime(2007, 4, 21, 14, 45, 42, 463064)

Or,


from datetime import *
datetime.now()

datetime.datetime(2007, 4, 21, 14, 46, 27, 767389)

--
With Regards


Parthan "Technofreak"

http://technofreakatchennai.wordpress.com
-- 
http://mail.python.org/mailman/listinfo/python-list

lambda generator - curious behavior in 2.5

2007-04-21 Thread Boris Borcic
 >>> x = (lambda : ((yield 666),(yield 777),(yield 888)))()
 >>> x.next()
666
 >>> x.next()
777
 >>> x.next()
888
 >>> x.next()
(None, None, None)
 >>> x = (lambda : ((yield 666),(yield 777),(yield 888)) and None)()
 >>> x.next()
666
 >>> x.next()
777
 >>> x.next()
888
 >>> x.next()

Traceback (most recent call last):
   File "", line 1, in 
 x.next()
StopIteration
 >>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lambda generator - curious behavior in 2.5

2007-04-21 Thread James Stroud
Boris Borcic wrote:
>  >>> x = (lambda : ((yield 666),(yield 777),(yield 888)))()
>  >>> x.next()
> 666
>  >>> x.next()
> 777
>  >>> x.next()
> 888
>  >>> x.next()
> (None, None, None)
>  >>> x = (lambda : ((yield 666),(yield 777),(yield 888)) and None)()
>  >>> x.next()
> 666
>  >>> x.next()
> 777
>  >>> x.next()
> 888
>  >>> x.next()
> 
> Traceback (most recent call last):
>   File "", line 1, in 
> x.next()
> StopIteration
>  >>>

Seems like a bug:

py> def doit():
   ((yield 1), (yield 2), (yield 3))
...
py> x = doit()
py> x.next()
1
py> x.next()
2
py> x.next()
3
py> x.next()

Traceback (most recent call last):
   File "", line 1, in 


James
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie: HTTPS screen scraping

2007-04-21 Thread ici
On Apr 21, 11:38 am, [EMAIL PROTECTED] wrote:
> Hi,
> Can anyone help me out here. I would like to authenticate myself to
> a website which uses HTTPS and then after authentication, I would like
> to get the contents of the webpage. How can this be done using python.
> I have tried urllib and urllib2 but it has not solved my problem.
>
> TIA
> /varun

http://pycurl.sourceforge.net/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Styled Output

2007-04-21 Thread Daniel Nogradi
> I'm running a program of mine from Linux bash script and it currently
> outputs status messages to the command prompt, but they all look a little
> boring to say the least, it'll be like this.

[snip]

> And perhaps have the word 'success' in green, or red for 'failed' and things
> like that, just to make it a little more presentable to the user.


You might find that urwid, a console user interface library, meets
your needs: http://excess.org/urwid/

Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Styled Output

2007-04-21 Thread Daniel Nogradi
> > I'm running a program of mine from Linux bash script and it currently
> > outputs status messages to the command prompt, but they all look a little
> > boring to say the least, it'll be like this.
>
> [snip]
>
> > And perhaps have the word 'success' in green, or red for 'failed' and
> things
> > like that, just to make it a little more presentable to the user.

Something like this might also help on linux:


def blue( text ):
print '\033[1m' + text + '\033[0m'

blue( 'this will be blue' )

See http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html

HTH,
Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TK-grid problem, please help

2007-04-21 Thread James Stroud
Ray wrote:
> hi, I have a question about how to use .grid_forget (in python/TK)
> 
> I need to work on grid repeatly. everytime when a button is pressed,
> the rows of grid is different. such like, first time, it generate 10 
> rows of data.
> 2nd time, it maybe only 5 rows. so I need a way to RESET the grid data
> every time. how can I do it? by grid_forger()?, then would anyone can 
> help on
> how to use grid_forget()
> the sample code as following:
> 
> #begin of program###
> 
> from Tkinter import *
> def mygrid(text):
>  how to use grid_forget() to clean the grid??###
> rows = []
> count=int(text)
> for i in range(count):
> cols = []
> for j in range(4):
> e = Entry(frame3, relief=RIDGE)
> e.grid(row=i, column=j, sticky=NSEW)
> e.insert(END, '%d.%d' % (i, j))
> cols.append(e)
> rows.append(cols)
> 
> 
> root=Tk()
> 
> frame1=Frame(root, width=150, height=100)
> frame1.pack()
> 
> text=Entry(frame1)
> text.pack(side=LEFT)
> 
> button=Button(frame1, text='generate grid', command=(lambda: 
> mygrid(text.get(
> 
> button.pack()
> 
> frame2=Frame(root, width=150, height=100)
> frame2.pack()
> 
> button2=Button(frame2, text='exit', command=root.quit)
> button2.pack()
> 
> frame3=Frame(root, width=150, height=300)
> frame3.pack()
> 
> root.mainloop()
> 
> #end of program###


Using grid_forget() is probably optimization overkill, but may be handy 
for slower computers where you can watch the widgets appear one by one 
(older than about 5 years--for example original mac ibook). Also, you 
should get a good book on Tkinter because your design here will pretty 
difficult to maintain and is not very flexible.

But...if you want to know how it might be done with grid_forget using 
the code you already have (i.e. making widgets only if necessary):


#START#
from Tkinter import *
from tkMessageBox import showerror
def mygrid(text):
  how to use grid_forget() to clean the grid??###
 numrows = len(frame3.rows)
 try:
   count=int(text)
 except:
   showerror('Entry Error',
 '''Hey, "%s" don't make an int, Fool!''' % text,
 parent=frame3)
   return 'break'
 for i in range(count):
 if i < numrows:
   cols = frame3.rows[i]
 else:
   cols = [Entry(frame3, relief=RIDGE) for j in range(4)]
   frame3.rows.append(cols)
 for j in range(4):
 e = cols[j]
 e.grid(row=i, column=j, sticky=NSEW)
 e.delete(0,END)
 e.insert(END, '%d.%d' % (i, j))
 for i in range(i+1, numrows):
   for e in frame3.rows[i]:
 e.grid_forget()


root=Tk()

frame1=Frame(root, width=150, height=100)
frame1.pack()

text=Entry(frame1)
text.pack(side=LEFT)

button=Button(frame1, text='generate grid', command=(lambda: 
mygrid(text.get(

button.pack()

frame2=Frame(root, width=150, height=100)
frame2.pack()

button2=Button(frame2, text='exit', command=root.quit)
button2.pack()

frame3=Frame(root, width=150, height=300)
# adding an attribute here
frame3.rows = []
frame3.pack()

root.mainloop()
#END#

Notice also the necessity for the "e.delete(0, END)" line to get the 
desired text in the entries.

Also demonstrated is how to handle poor input.

*Note*
Remember to always call the user "Fool" when he does something stupid.


James
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lambda generator - curious behavior in 2.5

2007-04-21 Thread Gabriel Genellina
En Sat, 21 Apr 2007 06:21:00 -0300, Boris Borcic <[EMAIL PROTECTED]>  
escribió:

>  >>> x = (lambda : ((yield 666),(yield 777),(yield 888)))()
>  >>> x.next()
> 666
>  >>> x.next()
> 777
>  >>> x.next()
> 888
>  >>> x.next()
> (None, None, None)

I think nobody thought in advance this case (a lambda expression with  
yield?), else it would have been forbidden. The lambda is roughly  
equivalent to:

def anonymous():
   return ((yield 666),(yield 777),(yield 888))
x = anonymous()

but *that* function is forbidden: a generator cannot contain a "return  
something" statement. Writing it as a lambda expression, you are bypassing  
the compiler check.
Those three None are the 3 yield values, combined into one tuple. You can  
verify using send instead of next; the Nones are replaced by the received  
values:

py> x = (lambda : ((yield 666),(yield 777),(yield 888)))()
py> x.send(None)
666
py> x.send(1)
777
py> x.send(2)
888
py> x.send(3)
(1, 2, 3)

That was the return in effect. As the function (or lambda) is exited, the  
next try should raise StopIteration:

py> x.send(4)
Traceback (most recent call last):
   File "", line 1, in 
StopIteration

Let's check with dis:

py> dis.dis(x.gi_frame.f_code)
   1   0 LOAD_CONST   0 (666)
   3 YIELD_VALUE
   4 LOAD_CONST   1 (777)
   7 YIELD_VALUE
   8 LOAD_CONST   2 (888)
  11 YIELD_VALUE
  12 BUILD_TUPLE  3
  15 RETURN_VALUE



>  >>> x = (lambda : ((yield 666),(yield 777),(yield 888)) and None)()
>  >>> x.next()
> 666
>  >>> x.next()
> 777
>  >>> x.next()
> 888
>  >>> x.next()
>
> Traceback (most recent call last):
>File "", line 1, in 
>  x.next()
> StopIteration
>  >>>

This time, the ((tuple) and None) is like saying "discard the tuple and  
return None instead", and that fires the usual StopIteration.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Not Auto-Init On Import

2007-04-21 Thread Gabriel Genellina
En Sat, 21 Apr 2007 05:46:07 -0300, Robert Rawlins - Think Blue  
<[EMAIL PROTECTED]> escribió:

>> From my understanding of what I've read, the 'import' is meant to auto  
>> Init
> my class ready for me to access its methods, but it doesn't appear too,  
> I'm
> having to init them myself before I can access them, like this.

The import statement does not "auto Init" anything, in principle.

> import LocationService

Here, you import the LocationService module. If it is imported by the  
first time, its code (at the module level) is executed. The imported  
module is available under the name "LocationService" in the current  
namespace.

> Location = LocationService.LocationService()

Presumably, the module LocationService defines a class of the same name;  
you refer to that class using LocationService.LocationService. The last ()  
create an instance of that class; that instance is bound to the name  
Location.

> LocationService.setIP('192.168.1.1')

This may or may not be correct; here you call a (global) function setIP  
inside the LocationService module.

> Why is this the case? Should i not just be able to access the setIP()  
> method
> by doing LocationService.setIP('192.168.1.1') Without having to create my
> own inited reference?

Not enough info, without knowing how LocationService is supposed to be  
used. But as I said, the setIP call looks suspicious.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


how to find the OS name..??

2007-04-21 Thread pradeep nair
How do i determine the type of OS i'm working on using python??(i.e
whether CentOS,Fedora,SLES,...along with the version)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to find the OS name..??

2007-04-21 Thread Gabriel Genellina
En Sat, 21 Apr 2007 08:13:10 -0300, pradeep nair <[EMAIL PROTECTED]>  
escribió:

> How do i determine the type of OS i'm working on using python??(i.e
> whether CentOS,Fedora,SLES,...along with the version)

os.uname()
If you need more detailed information try the platform module

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: is laziness a programer's virtue?

2007-04-21 Thread Peter Maas
Xah Lee wrote:
> For those reading this, i also want to mention, that although i think
> Perl is a motherfucking language on earth, and its leader and
> “inventor” Larry Wall has done massive damage to the computing world,
> but Perl the community is in fact very tolerant in general (which is
> to Larry's credit), when compared to the motherfucking Pythoners (who
> knew SHIT) as well as many of the self-appointed lisp bigwig
> characters.

Be careful not to damage the English language by making "motherfucking"
a compliment :)

-- 
Regards/Gruesse,

Peter Maas, Aachen
E-mail 'cGV0ZXIubWFhc0B1dGlsb2cuZGU=\n'.decode('base64')
-- 
http://mail.python.org/mailman/listinfo/python-list

ANN: eric3 version 3.9.5 released

2007-04-21 Thread Detlev Offenbach
Hi,

this is to inform you about the release of eric3 version 3.9.5. This
release fixes a few bugs and includes an updated PyLint interface. As
usual it is available via

http://www.die-offenbachs.de/detlev/eric.html

Regards,
Detlev
-- 
Detlev Offenbach
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: wx.TextCtrl.SetDefaultStyle not working?

2007-04-21 Thread 7stud
On Apr 20, 8:38 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> I'm running Python2.5 with wxPython v2.8.3.0 under WinXP and I cannot
> get the SetDefaultStyle method to work.
>
> I'm trying:
>
> self.output.SetDefaultStyle(wx.TextAttr(wx.RED))
> self.output.AppendText(text)
> self.output.SetDefaultStyle(wx.TextAttr())
>
> where "self.output" is a TextCtrl window.  The text appears, but it's
> always black.  If I print the output of
> self.output.GetDefaultStyle().GetTextColour() before resetting it back
> to default, I see "(255, 0, 0, 255)".  After reset, it's "(-1, -1, -1,
> 255)".
>
> The font in that window is the system default.
>
> Am I doing something wrong?
>
> -- Brian

You didn't show the code that creates the TextCtrl.  Is it something
like this:

self.output = wx.TextCtrl(myFrame, -1, style=wx.TE_MULTILINE |
wx.TE_RICH2)

On my platform, styling the text doesn't work for single line
TextCtrl's(which seems kind of stupid), and on Windows I think you are
required to specify wx.TE_RICH2 to style the text.  This following
code colors the entered text red for me:


import wx

myApp = wx.App(False)
myFrame = wx.Frame(None, -1, title="Test")

panel = wx.Panel(myFrame, -1)
my_tc = wx.TextCtrl(myFrame, -1, style=wx.TE_MULTILINE | wx.TE_RICH2)
my_tc.SetDefaultStyle(wx.TextAttr("red") )#wx.RED works too

myFrame.Show()
myApp.MainLoop()


-- 
http://mail.python.org/mailman/listinfo/python-list


Suggestion: str.itersplit()

2007-04-21 Thread Dustan
>From my searches here, there is no equivalent to java's
StringTokenizer in python, which seems like a real shame to me.

However, str.split() works just as well, except for the fact that it
creates it all at one go. I suggest an itersplit be introduced for
lazy evaluation, if you don't want to take up recourses, and it could
be used just like java's StringTokenizer.

Comments?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Suggestion: str.itersplit()

2007-04-21 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Dustan wrote:

> From my searches here, there is no equivalent to java's
> StringTokenizer in python, which seems like a real shame to me.
> 
> However, str.split() works just as well, except for the fact that it
> creates it all at one go. I suggest an itersplit be introduced for
> lazy evaluation, if you don't want to take up recourses, and it could
> be used just like java's StringTokenizer.
> 
> Comments?

Does it really make such a difference?

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Suggestion: str.itersplit()

2007-04-21 Thread Michael Hoffman
Marc 'BlackJack' Rintsch wrote:
> In <[EMAIL PROTECTED]>, Dustan wrote:
> 
>> From my searches here, there is no equivalent to java's
>> StringTokenizer in python, which seems like a real shame to me.
>>
>> However, str.split() works just as well, except for the fact that it
>> creates it all at one go. I suggest an itersplit be introduced for
>> lazy evaluation, if you don't want to take up recourses, and it could
>> be used just like java's StringTokenizer.
>>
>> Comments?
> 
> Does it really make such a difference?

It would if you were dealing with enormous blocks of text at once, say 
from a database.
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested dictionaries trouble

2007-04-21 Thread rzed
IamIan <[EMAIL PROTECTED]> wrote in
news:[EMAIL PROTECTED]: 

> Thank you again for the great suggestions. I have one final
> question about creating a httpMonths dictionary like {'Jan':'01'
> , 'Feb':'02' , etc} with a minimal amount of typing. My code
> follows (using Python 2.3.4):
> 
> import calendar
> 
> # Create years list, formatting as strings
> years = map(str, xrange(1990,2051))
> 
> # Create months list with three letter abbreviations
> months = list(calendar.month_abbr)
> 
> # Create monthTotals dictionary with default value of zero
> monthTotals = dict.fromkeys(months[1:],0)
> 
> # Create yearTotals dictionary with years for keys
> # and copies of the monthTotals dictionary for values
> yearTotals = dict([(year, monthTotals.copy()) for year in
> years]) 
> 
> # Create httpMonths dictionary to map month abbreviations
> # to Apache numeric month representations
> httpMonths =
> {"Jan":"01","Feb":"02","Mar":"03","Apr":"04","May":"05","Jun":"0
6
> ","Jul":"07","Aug":"08","Sep":"09","Oct":"10","Nov":"11","Dec":"
1
> 2"} 
> 
> It is this last step I'm referring to. I got close with:
> httpMonths = {}
> for month in months[1:]:
>   httpMonths[month] = str(len(httpMonths)+1)
> 
> but the month numbers are missing the leading zero for 01-09.
> Thanks! 
> 

Maybe something like: 
httpMonths = dict((k,"%02d" % (x+1)) 
for x,k in enumerate(months[1:]) )

-- 
rzed
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Do other Python GUI toolkits require this? (OT)

2007-04-21 Thread Roel Schroeven
Hendrik van Rooyen schreef:
>  "Roel Schroeven" <[EMAIL PROTECTED]> wrote:
> 
>> Hendrik van Rooyen schreef:
>>>  "Steve Holden" <[EMAIL PROTECTED]> wrote:
>>>
>>>  
 Perhaps in Belgium they prefer climbing mountains over walking up and 
 down gentle hills?
>>> Mountains ?   Hills ?   In Belgium ??
>>>
>>> Its not called the battlefield of Europe for nothing...
>> I'm not sure if this adds anything of interest (well actually I'm pretty 
>> sure it doesn't), but our king Albert I was a fanatic mountain climber, 
>> until he died from a fall in 1934.
>>
> 
> Thanks - I was not aware of that.
> Adds a whole new dimension to the phrase:
> 
> "The Fall of a King"
> 
> What was he climbing when he fell, btw?

A rock in Marche-les-Dames, see the picture at 
http://nl.wikipedia.org/wiki/Afbeelding:Roy-m%C3%A5tche.jpg

That's in the Walloon region; Flanders is of course much more flat than 
that.

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using tkinter to display html

2007-04-21 Thread Kevin Walzer
Stephen M. Gava wrote:
> On Fri, 20 Apr 2007 03:28:22 -0700, Rob Wolfe wrote:
 The following thread has various ideas in it:
 http://mail.python.org/pipermail/python-list/2001-October/107989.html
>>> thanks mike, i found that thread before i posted here, it doesn'rt
>>> answer my question though.
>> Why not? Did you try to use tkhtml [1]_ with python wrapper [2]_. IMHO
>> it works pretty nice.
> 
> i found the tkhtml page but it hasn't been updated since 2002 and the 
> mailing list doesn't even exist anymore. so unsure of the future on that.
> 
>> .. [1] http://www.hwaci.com/sw/tkhtml/ .. [2]
> 
> didn't find these bindings though, so thanks, i'll have a play.
> 
> stephen.
> 
>> http://tix.sourceforge.net/Tixapps/src/Python/TkHtml.py
>>

TkHTML:

http://tkhtml.tcl.tk

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Suggestion: str.itersplit()

2007-04-21 Thread subscriber123
On Apr 21, 8:58 am, Dustan <[EMAIL PROTECTED]> wrote:
> >From my searches here, there is no equivalent to java's
>
> StringTokenizer in python, which seems like a real shame to me.
>
> However, str.split() works just as well, except for the fact that it
> creates it all at one go. I suggest an itersplit be introduced for
> lazy evaluation, if you don't want to take up recourses, and it could
> be used just like java's StringTokenizer.
>
> Comments?

That would be good, because then you could iterate over strings the
same way that you iterate over files:

for line in string.itersplit("\n"):
## for block ##


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unexpected exception from socket.getaddrinfo on Unicode URL

2007-04-21 Thread Steve Holden
John Nagle wrote:
>  Here's a strange little bug.  "socket.getaddrinfo" blows up
> if given a bad domain name containing ".." in Unicode.  The
> same string in ASCII produces the correct "gaierror" exception.
> 
>  Actually, this deserves a documentation mention.  The "socket" module,
> given a Unicode string, calls the International Domain Name parser,
> "idna.py", which has a a whole error system of its own.  The IDNA
> documentation says that "Furthermore, the socket module transparently 
> converts 
> Unicode host names to ACE, so that applications need not be concerned about 
> converting host names themselves when they pass them to the socket module."
> However, that's not quite true; the IDNA rules say that syntax errors must
> be treated as errors, so you have to be prepared for IDNA exceptions.
> They are all "UnicodeError" exceptions.
> 
>  It's worth a mention in the documentation for "socket".
> 
>   John Nagle
> 
> D:\>/python25/python.exe
> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on 
> win
> 32
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> ss = 'www.gallery84..com'
>  >>> uss = unicode(ss)
>  >>> import socket
>  >>> socket.getaddrinfo(ss,"http")
> Traceback (most recent call last):
>File "", line 1, in 
> socket.gaierror: (11001, 'getaddrinfo failed')
>  >>> socket.getaddrinfo(uss,"http")
> Traceback (most recent call last):
>File "", line 1, in 
>File "D:\python25\lib\encodings\idna.py", line 164, in encode
>  result.append(ToASCII(label))
>File "D:\python25\lib\encodings\idna.py", line 73, in ToASCII
>  raise UnicodeError("label empty or too long")
> UnicodeError: label empty or too long
>  >>>
> 
I took a look at the documentation but couldn't see where to add what, 
given that the documentation for socket already says:

"""All errors raise exceptions. The normal exceptions for invalid 
argument types and out-of-memory conditions can be raised; errors 
related to socket or address semantics raise the error socket.error.
""".

Do we really need to specifically mention Unicode errors?

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings   http://holdenweb.blogspot.com

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Do other Python GUI toolkits require this?

2007-04-21 Thread Diez B. Roggisch
Antoon Pardon schrieb:
> On 2007-04-20, Diez B. Roggisch <[EMAIL PROTECTED]> wrote:
>>> So if you have the choice between a steep or a shalow income curve
>>> you will prefer the shalow curve because a steep curve makes you
>>> think about verticale clifs and such?
>>>
>>> The analogy with a walk is just silly because curves are not like walks.
>>> Nobody will say something like: I won't invest in that company because
>>> it has a steep profit curve or the reverse: I'll invest in this company
>>> because it has an easy looking downhill going profit curve.
>> Your whole argumentation bases on the fact that the result of the 
>> learning process, and the success of it, has something to do with the 
>> reached height - or y-axis-value - of your climb.
>>
>> Which is nonsense. The goal is to go from A - ignorance - to B - 
>> knowledge - which both lie on the X-Axis.
> 
> Well if you want to do it that way, nobody can stop you, but people
> in the habit of processing numbers usually put the time on the X-axis
> like in time spend learning or exercising and put the other value
> on the Y-axis. 


You seem to live in a very limited world, where bezier-curves (note the 
name...)
are parametrized over t, but rendered on the x/y-axis happily going 
forth and back and whatnot.

If using knowledge as the x-axis and effort on the y-axis, the figure of 
speech makes perfect sense.


> That is because people prefer a curve going up and down while moving
> to the right instead of going left and right while moving up.

Which is obviously something people don't want to do in this context, 
because "going down" doesn't make too much sense here, doesn't it? Or do 
you want to cram the process of unlearning in the little figure of 
speech as well?


But even a perfectly sense-making explanation can be found, I doubt that 
you will ever acknowledge that you did make a mistake on this one - as 
you always (or better never) do...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Styled Output

2007-04-21 Thread Michael Bentley

On Apr 21, 2007, at 3:21 AM, Robert Rawlins - Think Blue wrote:

> Chaps,
>
>
>
> Hope you’re all having a good weekend, I’m sure it’ll only be the  
> more ‘hard core’ of you reading this, anyone with any sanity would  
> be out in the sunshine right now.
>
>
>
> I’m running a program of mine from Linux bash script and it  
> currently outputs status messages to the command prompt, but they  
> all look a little boring to say the least, it’ll be like this.
>
>
>
> Application Started
>
> Sucess
>
> Application Configured
>
> Failed
>
> Retry
>
> Application Configured
>
> Success
>
>
>
> What I’d ideally like to do it give it a little bit of format, so  
> it perhaps looks something like this.
>
>
>
> Application  
> Started
>   [Success]
>
> Application  
> Configured 
>   [Success]
>
>
>
> And perhaps have the word ‘success’ in green, or red for ‘failed’  
> and things like that, just to make it a little more presentable to  
> the user.
>
>
>
> Any ideas on the best way to achieve this?
>
>
He, he -- umm yeah (and who said all that time with THEDRAW as a kid  
would never pay off) ;-) Try this:

#! /usr/bin/env python

def statusMessage(msg, status, msgWidth=60):
 if status.lower() == 'success':
 status = '\033[32m[%s]\033[00m' % status
 elif status.lower() == 'failed':
 status = '\033[31m[%s]\033[00m' % status
 else:
 status = '\033[33m[%s]\033[00m' % status

 msg = msg + ' ' * (msgWidth - len(msg))
 print '%s%s' % (msg, status)

if __name__ == '__main__':
 statusMessage('Application Started', 'Success')
 statusMessage('Application Configured', 'Failed')
 statusMessage('', 'Retry')
 statusMessage('Application Configured', 'Success')

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Do other Python GUI toolkits require this?

2007-04-21 Thread Kevin Walzer
Kevin Walzer wrote:
>  From the introduction to PyObjC, the Python-Objective-C bridge on Mac 
> OS X:
> 
> "As described in Objective-C for PyObjC users the creation of 
> Objective-C objects is a two-stage process. To initialize objects, first 
> call a class method to allocate the memory (typically alloc), and then 
> call an initializer (typically starts with init). Some classes have 
> class methods which perform this behind the scenes, especially classes 
> that create cached, immutable, or singleton instances."
> 
> An example:
> 
> myObject = NSObject.alloc().init()
> 
> I know Tkinter doesn't require any manual memory allocation of this 
> sort. Does wxPython, PyQt, PyGtk require anything like this when 
> creating objects?
> 


Thanks to all for an illuminating thread on the mathematical 
implications of "learning curve" and other aspects. This thread has 
wandered pretty far from my original question (above) but I got at least 
one useful answer, so I am grateful.

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter wiki down?

2007-04-21 Thread Kevin Walzer
Jeff Epler's Tkinter wiki has been down for several days now:

http://tkinter.unpythonic.net/wiki/FrontPage

Anyone heard about when, or if, it will be restored? I've tried to 
contact Mr. Epler but have not yet heard back. The site has been the 
target of some heavy spam/spidering recently; I've taken a few hours to 
clean up lots of pages.

My own concern is that I had posted some resources there on the 
assumption that it would be reasonably stable/permanent home for them; 
if that's not the case, I will being looking into other resources.

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How To Find Currently Selected Tile.Notebook Tab?

2007-04-21 Thread Kevin Walzer
[EMAIL PROTECTED] wrote:
>>> How can I determine what tab is currently selected in a Tile.Notebook
>>> widget?
>>> The best suggestion I've been able to find via Google is
>>> "mynotebook.index('current')", but that gets an exception from Tcl.
>> How about
>>
>> mynotebook.index.current()
> 
> No good:  AttributeError: 'function' object has no attribute 'current'
> 
> I tried "mynotebook.index().current()", too, but get a different
> error.
> 
> -- Brian
> 
Not sure what the problem is then--perhaps that function hasn't been 
mapped into Tkinter. I don't use that feature myself.

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Not Auto-Init On Import

2007-04-21 Thread Steve Holden
Robert Rawlins - Think Blue wrote:
> Hello Guys,
> 
>  
> 
>  From my understanding of what I’ve read, the ‘import’ is meant to auto 
> Init my class ready for me to access its methods, but it doesn’t appear 
> too, I’m having to init them myself before I can access them, like this.
> 
Importing a module (the first time) executes its code. This means that 
assignments bind to names in the module's namespace, class statements 
define classes in the module's namespace, def statements define 
functions in the module's namespace. The statement
> 
> import LocationService
> 
after, running the module's code, makes the module's namespace available 
as the name LocationService in the importing module's namespace. So 
"name" in the module's namespace can be referred to in the importing 
module as LocationService.name.
> 
> Location = LocationService.LocationService()
> 
So that statement creates an instance of the LocationService class 
defined in the LocationService module. Since no arguments are provided, 
the new instance's __init__() method is called with a single argument, 
the new instance itself.
> 
> LocationService.setIP(‘192.168.1.1’)
> 
This isn't a call on a specific LocationService instance, it's a call on 
the SetIP method of the class (presumably you have to set the IP address 
of the server or whatever - typically class methods are used to invoke 
functions or set up conditions that must apply to all instances of the 
class. If you wanted to set the IP for a particular instance you would 
normally call a method instance, as in

Location.SetIP('129.168.1.1')

But if the class defines the method as a class method then a call like 
the above is likely to fail. So it's ultimately all down to the design 
of the class you are using.
> 
> Why is this the case? Should i not just be able to access the setIP() 
> method by doing LocationService.setIP(‘192.168.1.1’) Without having to 
> create my own inited reference?
> 
That would make a certain amount of sense, but whether this would be 
acceptable depends how the class's __init__ method is defined. There's 
certainly nothing in the Python language that would *stop* you doing that.

It seems that there might be a certain amount of confusion, either in 
your mind or in the module you are using, between object-oriented 
programming techniques and the more process-oriented traditional 
techniques. I don't know whether this will have helped or not.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings   http://holdenweb.blogspot.com

-- 
http://mail.python.org/mailman/listinfo/python-list


pyHook equivalent for WH_SHELL hooks?

2007-04-21 Thread Jan Böcker
Hi,

i want to write an app which has to log various user activity, such as opening 
folders and office documents, to help the user keep an overview about what open 
windows are related to one task he/she is currently doing.

I thought of using a windows shell hook, which notifies the program similar to 
a 
keyboard hook when a top-level window is created or destroyed. The pyHook 
extension shows that this is possible, but only supports keyboard and mouse 
hooks.

My own experiments have not succeeded, as I never have programmed in C/C++ 
before, let alone a python extension, but I am willing to learn. (I managed to 
do a test extension without any useful functionality).

I have difficulties understanding how my hook will cope with multithreading. As 
far as I understand, a windows hook dll is loaded into the address space of 
every running program. How does a hook handle concurrency and how exactly does 
it call the python function? Will the python function run on another thread? 
How 
do I call a python function on another thread?

pyHook claims to be open source, but I could only find the python part of the 
source, the C part seems to be missing on sourceforge.net (correct me if I am 
wrong).

Is there already something like pyHook for WH_SHELL hooks?
If not, how does pyHook work internally and is its C source available?

I would be glad if anybody could help or point me in the right direction. I 
already tried the de.comp.lang.python and a web forum full of nerds ;)

Regards,
Jan Böcker
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Shebang or Hashbang for modules or not?

2007-04-21 Thread Bruno Desthuilliers
Jorgen Grahn a écrit :
> On Fri, 13 Apr 2007 22:46:03 +0200, Bruno Desthuilliers <[EMAIL PROTECTED]> 
> wrote:
> 
>>Jorgen Grahn a écrit :
> 
(snip)
> 
>>More seriously, and as far as I'm concerned, when I want to make a 
>>python script (by opposition to a python 'module') available as a unix 
>>command, I either use a symlink or a shell script calling the python 
>>script.
> 
> 
> A symlink yes, but a shell script? Wouldn't it be easier to write a
> one-liner (well, two-liner) Python script in that case?

Not necessarily. Just like there are cases where it makes sens to use 
Perl instead of Python, some things are really far simpler to do with 
shell scripts...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Shebang or Hashbang for modules or not?

2007-04-21 Thread Michael Hoffman
Chris Lasher wrote:
> Should a Python module not intended to be executed have shebang/
> hashbang (e.g., "#!/usr/bin/env python") or not? I'm used to having a
> shebang in every .py file but I recently heard someone argue that
> shebangs were only appropriate for Python code intended to be
> executable (i.e., run from the command line).

Personally I include it in all of them, as part of boilerplate in a 
template.
-- 
Michael Hoffman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When are immutable tuples *essential*? Why can't you just use lists *everywhere* instead?

2007-04-21 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
> On Apr 20, 4:37 pm, John Machin <[EMAIL PROTECTED]> wrote:
> 
>>One inessential but very useful thing about tuples when you have a lot
>>of them is that they are allocated the minimum possible amount of
>>memory. OTOH lists are created with some slack so that appending etc
>>can avoid taking quadratic time.
> 
> 
> Speaking of inessential but very useful things, I'm also a big fan of
> the tuple swap...

Which relies on unpacking, which also works with lists
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie: HTTPS screen scraping

2007-04-21 Thread John Nagle
[EMAIL PROTECTED] wrote:
> Hi,
>Can anyone help me out here. I would like to authenticate myself to a 
> website which uses HTTPS and then after authentication, I would like to 
> get the contents of the webpage. How can this be done using python.
> I have tried urllib and urllib2 but it has not solved my problem.
> 
> TIA
> /varun

Most of the various URL libraries (urllib, urllib2, and pycurl)
can do this.

With "urllib", you can subclass FancyURLopener, then redefine
"get_user_passwd(self, host, realm, clear_cache=0)" in the
subclass. That function will be called when a password is needed,
and you return (username, password) as a tuple, which gets sent
to the web server.

Python, instead of having one library for reading URLs that works,
has at least three, all with different problems.

John Nagle
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When are immutable tuples *essential*? Why can't you just use lists *everywhere* instead?

2007-04-21 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
> Please help me think of an example where immutable tuples are
> essential.

Well, I don't know if they are "essential" - much of Python can be seen 
as 'unessential' syntactic sugar (my, even the class statement is not 
"essential" if you go that way).

> It seems that everywhere a tuple is used one could just as easily use
> a list instead.

Ever tried using a list as key in a dict ?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question regarding string.split()

2007-04-21 Thread Bruno Desthuilliers
kevinliu23 a écrit :
> Hey guys,
> 
> So I have a question regarding the split() function in the string
> module. Let's say I have an string...
> 
> input = "2b 3 4bx 5b 2c 4a 5a 6"
> projectOptions = (input.replace(" ", "")).split('2')
The parens around the call to input.replace are useless:
   projectOptions = input.replace(" ", "").split('2')

> print projectOptions
> 
> ['', 'b34bx5b', 'c4a5a6']

(snip)

> What can I do so that the first element is not an empty
> string? but the 'b34bx5b' string as I expected?


projectOptions = filter(None, input.replace(" ", "").split('2'))


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: class __getitem__ when item is not a sequence ???

2007-04-21 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
> Sorry for the vague subject. Not sure what the right terminology is.
> 
> How can I use an instance's data by reference to the instance name,
> not the instance attribute? OK the question is probably really poor
> but hopefully an example will make it clear.
> 
> 
>>x=1
>>type(x)
> 
> 
> 
>>x.__add__(1)
> 
> 2
> 
>>print x
> 
> 1
> 
>>3*x
> 
> 3
> 
> In this case x is an integer. My understanding is that x in an
> instance of an integer class. Since it refers to only a single value
> things like print x, 3*x etc operate on the instance name which seems
> to refer to the instance data, not the instance itself. I want to do
> the same for my own classes.

http://docs.python.org/ref/specialnames.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Better dict of dicts

2007-04-21 Thread Bruno Desthuilliers
Bill Jackson a écrit :
> I have a dictionary of dictionaries where the keys are typically very 
> long tuples and repeated in each inner dictionary.  The dictionary 
> representation is nice because it handles sparseness well...and it is 
> nice to be able to look up values based on a string rather than a 
> number.  However, since my keys are quite long, I worry that I am 
> wasting a lot of memory.  I'm looking for better data structures.
> 
Is this actually a *real* problem (or do you have evidences - based on 
both measurements of the behaviour with test data sets and knowledge of 
the real data sets- that there will be a problem) ? Or this this just 
"worrying" ? In the second case, I suggest that you bench and profile 
your code to know for sure...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested dictionaries trouble

2007-04-21 Thread Bruno Desthuilliers
IamIan a écrit :
> I am using the suggested approach to make a years list:
> 
> years = ["199%s" % x for x in range(0,10)]
> years += ["200%s" % x for x in range(0,10)]
> 
> I haven't had any luck doing this in one line though. Is it possible?

# Q, D and pretty obvious
years = ["199%s" % x for x in range(0,10)] + ["200%s" % x for x in 
range(0,10)]

# hardly more involved, and quite more generic
years = ["%s%s" % (c, y) for c in ("199", "201") for y in range(10)]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Do other Python GUI toolkits require this?

2007-04-21 Thread Bruno Desthuilliers
Kevin Walzer a écrit :
(snip)
> Thanks to all for an illuminating thread on the mathematical 
> implications of "learning curve" and other aspects. This thread has 
> wandered pretty far from my original question (above) 

Noticed this too ?-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unexpected exception from socket.getaddrinfo on Unicode URL

2007-04-21 Thread John Nagle
Steve Holden wrote:
> John Nagle wrote:
> 
>>  Here's a strange little bug.  "socket.getaddrinfo" blows up
>> if given a bad domain name containing ".." in Unicode.  The
>> same string in ASCII produces the correct "gaierror" exception.
>>
>>  Actually, this deserves a documentation mention.  The "socket" 
>> module,
>> given a Unicode string, calls the International Domain Name parser,
>> "idna.py", which has a a whole error system of its own.  The IDNA
>> documentation says that "Furthermore, the socket module transparently 
>> converts Unicode host names to ACE, so that applications need not be 
>> concerned about converting host names themselves when they pass them 
>> to the socket module."
>> However, that's not quite true; the IDNA rules say that syntax errors 
>> must
>> be treated as errors, so you have to be prepared for IDNA exceptions.
>> They are all "UnicodeError" exceptions.
>>
>>  It's worth a mention in the documentation for "socket".
>>
>> John Nagle
>>
>> D:\>/python25/python.exe
>> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit 
>> (Intel)] on win
>> 32
>> Type "help", "copyright", "credits" or "license" for more information.
>>  >>> ss = 'www.gallery84..com'
>>  >>> uss = unicode(ss)
>>  >>> import socket
>>  >>> socket.getaddrinfo(ss,"http")
>> Traceback (most recent call last):
>>File "", line 1, in 
>> socket.gaierror: (11001, 'getaddrinfo failed')
>>  >>> socket.getaddrinfo(uss,"http")
>> Traceback (most recent call last):
>>File "", line 1, in 
>>File "D:\python25\lib\encodings\idna.py", line 164, in encode
>>  result.append(ToASCII(label))
>>File "D:\python25\lib\encodings\idna.py", line 73, in ToASCII
>>  raise UnicodeError("label empty or too long")
>> UnicodeError: label empty or too long
>>  >>>
>>
> I took a look at the documentation but couldn't see where to add what, 
> given that the documentation for socket already says:
> 
> """All errors raise exceptions. The normal exceptions for invalid 
> argument types and out-of-memory conditions can be raised; errors 
> related to socket or address semantics raise the error socket.error.
> """.
> 
> Do we really need to specifically mention Unicode errors?

 It says "errors related to socket or address semantics raise the
error 'socket.error'", so, yes.  The error really has nothing to
do with Unicode; it's that a different parser is used when a domain
name is in Unicode.  It really shouldn't be a "Unicode error" at
all.

 When Python goes to Unicode by default, this is likely to break
some existing code.  Python's IDNA support is good, but not entirely
invisible.  The socket module documentation should mention IDNA
support.  It's not clear, for example, when you call "getnameinfo()",
whether you get back the name in Unicode or in Punycode.

John Nagle
-- 
http://mail.python.org/mailman/listinfo/python-list


Python "robots.txt" parser broken since 2003

2007-04-21 Thread John Nagle
This bug, "[ 813986 ] robotparser interactively prompts for username and 
password", has been open since 2003.  It killed a big batch job of ours
last night.

Module "robotparser" naively uses "urlopen" to read "robots.txt" URLs.
If the server asks for basic authentication on that file, "robotparser"
prompts for the password on standard input.  Which is rarely what you
want.  You can demonstrate this with:

import robotparser
url = 'http://mueblesmoraleda.com' # this site is password-protected.
parser = robotparser.RobotFileParser()
parser.set_url(url)
parser.read()   # Prompts for password

That's the tandard, although silly, "urllib" behavior.

This was reported in 2003, and a patch was uploaded in 2005, but the patch
never made it into Python 2.4 or 2.5.

A temporary workaround is this:

import robotparser  
def prompt_user_passwd(self, host, realm):
return None, None
robotparser.URLopener.prompt_user_passwd = prompt_user_passwd   # temp patch


John Nagle
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Do other Python GUI toolkits require this?

2007-04-21 Thread Bruno Desthuilliers
Kevin Walzer a écrit :
>  From the introduction to PyObjC, the Python-Objective-C bridge on Mac 
> OS X:
> 
> "As described in Objective-C for PyObjC users the creation of 
> Objective-C objects is a two-stage process. To initialize objects, first 
> call a class method to allocate the memory (typically alloc), and then 
> call an initializer (typically starts with init). Some classes have 
> class methods which perform this behind the scenes, especially classes 
> that create cached, immutable, or singleton instances."
> 
> An example:
> 
> myObject = NSObject.alloc().init()
> 
> I know Tkinter doesn't require any manual memory allocation of this 
> sort. Does wxPython, PyQt, PyGtk require anything like this when 
> creating objects?
> 

PyObjC is not a GUI toolkit, it's a bridge between Python and 
Objective-C (a smalltalk-inspired OO superset of ansi C).

And FWIW, under the hood, Python also uses this 2-stages 
instanciation/initialisation scheme (methods __new__ and __init__), even 
if it doesn't requires you to call them both explicitly.
-- 
http://mail.python.org/mailman/listinfo/python-list


A simple photosharing application written in Python

2007-04-21 Thread Rico
I d like to show you
http://www.morecute.com

It has been awesome runs on Fedora

I wanted to share it early on so there is litte load on the server and
the  early adopter crowd can try it out

thanks
Jerry

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lambda generator - curious behavior in 2.5

2007-04-21 Thread Raymond Hettinger
[Gabriel Genellina]
> This time, the ((tuple) and None) is like saying "discard the tuple and  
> return None instead", and that fires the usual StopIteration.

It looks like the offending code is in the gen_send_ex() function in
Objects/genobject.c:

if (result == Py_None && f->f_stacktop == NULL) {
Py_DECREF(result);
result = NULL;
/* Set exception if not called by gen_iternext() */
if (arg)
PyErr_SetNone(PyExc_StopIteration);
}

The conditional should probably be:

if (result 1= NULL && f->f_stacktop == NULL)

Please open a bug report on SF and assign to me.


Raymond Hettinger

-- 
http://mail.python.org/mailman/listinfo/python-list


Easy make money!

2007-04-21 Thread GPcapital
Lately I've been surfing trought some web pages and got this one
www.gpcapitalgroup.com . It looks quite interesting, it may be a great
chance to invest.

-- 
http://mail.python.org/mailman/listinfo/python-list


Returning a date as string

2007-04-21 Thread Björn Keil
Hello pythons,

I have little problem with understanding conversions in python. I've
written a little class - nothing much, just to try out Python a little
- containing the following method:

def __repr__(self):
"""Serializes the note.

Currently the format of notes isn't decided upon. XML output
is
projected."""
return "Due: " + str(self.dateDue) + \
   "\nDate: " + str(self.dateCreated) + \
   "\nSubject: " + self.subject + \
   "\n" + self.content

The fields "dateDue" and "dateCreated" contain datetime.date objects.
Now when I try to serialize the whole thing:

>>> myNote
Traceback (most recent call last):
  File "", line 1, in ?
  File "notes.py", line 81, in __repr__
return "Due: " + str(self.dateDue) + \
TypeError: cannot concatenate 'str' and 'datetime.date' objects

I tryed different variant before I wrapped "self.dateDue" in a str()
constructor:
I tried to put it in brackets, or call its .isoformat() method.
Nothing works. It still complains that I was trying to concatenate a
string with a date, but I really wanna concatenate a string with a
string!

Could anyone please tell me what I am doing wrong?

Greetings,
Björn

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question regarding string.split()

2007-04-21 Thread kevinliu23
On Apr 21, 3:30 pm, Bruno Desthuilliers
<[EMAIL PROTECTED]> wrote:
> kevinliu23 a écrit :> Hey guys,
>
> > So I have a question regarding the split() function in the string
> > module. Let's say I have an string...
>
> > input = "2b 3 4bx 5b 2c 4a 5a 6"
> > projectOptions = (input.replace(" ", "")).split('2')
Thanks for all your help everyone. :)

> The parens around the call to input.replace are useless:
>projectOptions = input.replace(" ", "").split('2')
>
> > print projectOptions
>
> > ['', 'b34bx5b', 'c4a5a6']
>
> (snip)
>
> > What can I do so that the first element is not an empty
> > string? but the 'b34bx5b' string as I expected?
>
> projectOptions = filter(None, input.replace(" ", "").split('2'))


-- 
http://mail.python.org/mailman/listinfo/python-list


No speedup on multi-processor machine?

2007-04-21 Thread danfan1981
Hi,
I am using Python Thread library for my parallel processing course
project. I am doing matrix convolution on a multi-processor machine
running Solaris. I just found out that no speed-up is obtained with
threading. It is probably because of something called GIL in Python.
How can I get around
that GIL and get speed-up?
Thanks in advance.
Daniel

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: CGI Script using Python

2007-04-21 Thread Tim Roberts
"Ralf" <[EMAIL PROTECTED]> wrote:
>
>is there anybody who can help me.
>I would like to use a Python Script to download Files from a Server to the 
>Client, to update the Client.

Remember, though, that a CGI script cannot actually put files anywhere. You
can certainly download files to the client browser, but the user will have
to be involved to tell the browser where to store the incoming file.

Doing anything else would be a security hole.
-- 
Tim Roberts, [EMAIL PROTECTED]
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Easy make money!

2007-04-21 Thread Dustan
On Apr 21, 3:45 pm, GPcapital <[EMAIL PROTECTED]> wrote:
> Lately I've been surfing trought some web pages and got this one
> www.gpcapitalgroup.com. It looks quite interesting, it may be a great
> chance to invest.

>>> advertisement.delete()
Traceback (most recent call last):
  File "", line 1, in 
advertisement.delete()
ActionNotPossibleError: advertisements are a fact of life

>>> advertisement.ignore()
Traceback (most recent call last):
  File "", line 1, in 
advertisement.ignore()
ActionNotPossibleError: they're all around us

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Suggestion: str.itersplit()

2007-04-21 Thread Dustan
On Apr 21, 7:58 am, Dustan <[EMAIL PROTECTED]> wrote:
> >From my searches here, there is no equivalent to java's
>
> StringTokenizer in python, which seems like a real shame to me.
>
> However, str.split() works just as well, except for the fact that it
> creates it all at one go. I suggest an itersplit be introduced for
> lazy evaluation, if you don't want to take up recourses, and it could
> be used just like java's StringTokenizer.
>
> Comments?

If anybody could inform me on how to get my hands on the python source
code, I might even be able to come up with an example of how it could
be implemented. I have no idea how to unzip that tgz or tar.bz2 file
on a windows machine, though (and that's not from lack of trying).

-- 
http://mail.python.org/mailman/listinfo/python-list


Building browser-like GET request

2007-04-21 Thread Gilles Ganault
Hello

I'd like to download pages from a site, but it checks whether
the requests are coming from a live user or a script; If the latter,
the server returns a blank page.

Using a proxy (Paros), I can see what information my script and
FireFox send, and there are a lot of information that Python is
missing:

 PYTHON ===
http://www.acme.com/cgi-bin/read?code=123  HTTP/1.1
Accept-Encoding: identity
Host: www.acme.com
Connection: close
User-Agent: Python-urllib/2.4 Paros/3.2.12
 FIREFOX ===
http://www.acme.com/cgi-bin/read?code=123  HTTP/1.1
Host: www.acme.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3 Paros/3.2.12
Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: fr-fr,en-us;q=0.7,en;q=0.3
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
=

How can Python be told to send the same information?

Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Building browser-like GET request

2007-04-21 Thread Björn Keil
On 21 Apr., 23:28, Gilles Ganault <[EMAIL PROTECTED]> wrote:
> I'd like to download pages from a site, but it checks whether
> the requests are coming from a live user or a script; If the latter,
> the server returns a blank page.
>
> Using a proxy (Paros), I can see what information my script and
> FireFox send, and there are a lot of information that Python is
> missing:

Well, I am brand new to Python, so it takes me a lot of guessing, but
since it seems you're using urlib2:

On http://docs.python.org/lib/module-urllib2.html is written that you
may add custom headers to your http requests.
Either by calling "addheader()" or by passing a dictionary with
headers to the constructor.

I hope that helped and I wasn't telling things you already new.
As a sidenote: For the task you describe I'd rather use an actual
sniffer - such as Wireshark (http://en.wikipedia.org/wiki/Wireshark),
than logs of a Proxy... Not sure wether Wireshark works under Windows,
though.

Good luck!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Suggestion: str.itersplit()

2007-04-21 Thread attn . steven . kuo
On Apr 21, 5:58 am, Dustan <[EMAIL PROTECTED]> wrote:
> >From my searches here, there is no equivalent to java's
>
> StringTokenizer in python, which seems like a real shame to me.
>
> However, str.split() works just as well, except for the fact that it
> creates it all at one go. I suggest an itersplit be introduced for
> lazy evaluation, if you don't want to take up recourses, and it could
> be used just like java's StringTokenizer.
>
> Comments?



If your delimiter is a non-empty string, you
can use an iterator like:

def it(S, sub):
start = 0
sublen = len(sub)
while True:
idx = S.find(sub,start)
if idx == -1:
yield S[start:]
raise StopIteration
else:
yield S[start:idx]
start = idx + sublen

target_string = 'abcabcabc'
for subs in it(target_string,'b'):
print subs


For something more complex,
you may be able to use
re.finditer.

--
Hope this helps,
Steven

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Suggestion: str.itersplit()

2007-04-21 Thread Jorge Godoy
Dustan <[EMAIL PROTECTED]> writes:

> If anybody could inform me on how to get my hands on the python source
> code, I might even be able to come up with an example of how it could
> be implemented. I have no idea how to unzip that tgz or tar.bz2 file
> on a windows machine, though (and that's not from lack of trying).

You can try WinZip.  Last time I had to use a Windows machine it was
able to untar + gunzip some files perfectly fine (as we are able to
unzip and unrar on *nix...). 

-- 
Jorge Godoy  <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Suggestion: str.itersplit()

2007-04-21 Thread Alex Martelli
Dustan <[EMAIL PROTECTED]> wrote:

> On Apr 21, 7:58 am, Dustan <[EMAIL PROTECTED]> wrote:
> > >From my searches here, there is no equivalent to java's
> >
> > StringTokenizer in python, which seems like a real shame to me.
> >
> > However, str.split() works just as well, except for the fact that it
> > creates it all at one go. I suggest an itersplit be introduced for
> > lazy evaluation, if you don't want to take up recourses, and it could
> > be used just like java's StringTokenizer.
> >
> > Comments?
> 
> If anybody could inform me on how to get my hands on the python source
> code, I might even be able to come up with an example of how it could
> be implemented. I have no idea how to unzip that tgz or tar.bz2 file
> on a windows machine, though (and that's not from lack of trying).

Top search hit for
windows tar
is  , but its contents
suggest using 
instead (it has "the ability to direcly create and manipulate .tar,
.tar.gz, tar.bz2, .zip, .gz and .bz2 archives, understands the most-used
options of GNU Tar, and is also much faster; for most purposes it is to
be preferred to GNU Tar", to quote).


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to transfer integer on socket?

2007-04-21 Thread Irmen de Jong
Frank Potter wrote:
> Is there any easy way to transfer 4 bit integer on socket?

I think you mean a 4-byte integer?
Look at the struct module, anyway.

--Irmen
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Building browser-like GET request

2007-04-21 Thread Gilles Ganault
On 21 Apr 2007 14:47:55 -0700, Björn Keil <[EMAIL PROTECTED]>
wrote:
>Well, I am brand new to Python, so it takes me a lot of guessing, but
>since it seems you're using urlib2:

 Thanks. Indeed, it looks like urlib2 is the way to go when going
through a proxy.

For those interested, here's how to download a page through a proxy:


import sys 
import urllib
import urllib2
import re

#set up proxy
proxy_info = { 'host' : 'localhost','port' : 8080}
proxy_support = urllib2.ProxyHandler({"http" :
"http://%(host)s:%(port)d" % proxy_info})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)

#call page with specific headers
url  = 'http://www.acme.com/cgi-bin/read?code=123'
headers = {
'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows
NT)',
'Accept' :
'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
'Accept-Language' : 'fr-fr,en-us;q=0.7,en;q=0.3',
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
}
#None = GET; set values to use POST
req = urllib2.Request(url, None, headers)

response = urllib2.urlopen(req).read()
log = open('output.html','w') 
log.write(response) 
log.close()


Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Returning a date as string

2007-04-21 Thread 7stud
On Apr 21, 2:59 pm, Björn Keil <[EMAIL PROTECTED]> wrote:
> Hello pythons,
>
> I have little problem with understanding conversions in python. I've
> written a little class - nothing much, just to try out Python a little
> - containing the following method:
>
> def __repr__(self):
> """Serializes the note.
>
> Currently the format of notes isn't decided upon. XML output
> is
> projected."""
> return "Due: " + str(self.dateDue) + \
>"\nDate: " + str(self.dateCreated) + \
>"\nSubject: " + self.subject + \
>"\n" + self.content
>
> The fields "dateDue" and "dateCreated" contain datetime.date objects.
> Now when I try to serialize the whole thing:
>
> >>> myNote
>
> Traceback (most recent call last):
>   File "", line 1, in ?
>   File "notes.py", line 81, in __repr__
> return "Due: " + str(self.dateDue) + \
> TypeError: cannot concatenate 'str' and 'datetime.date' objects
>
> I tryed different variant before I wrapped "self.dateDue" in a str()
> constructor:
> I tried to put it in brackets, or call its .isoformat() method.
> Nothing works. It still complains that I was trying to concatenate a
> string with a date, but I really wanna concatenate a string with a
> string!
>
> Could anyone please tell me what I am doing wrong?
>
> Greetings,
> Björn

This works for me:

import datetime

class MyDate(object):
def __init__(self, date):
self.d = date
def __repr__(self):
return str(self.d)

md = MyDate(datetime.date.today())
print "the result is: " + repr(md)

##output:the result is: 2007-04-21

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Easy make money!

2007-04-21 Thread John Nagle
Dustan wrote:
> On Apr 21, 3:45 pm, GPcapital <[EMAIL PROTECTED]> wrote:
> 
>>Lately I've been surfing trought some web pages and got this one
>>www.gpcapitalgroup.com. It looks quite interesting, it may be a great
>>chance to invest.
> 
> 
advertisement.delete()

 I couldn't resist.  I ran that through SiteTruth, our automatic
website legitimacy checker (written in Python, of course).

http://www.sitetruth.com/cgi-bin/ratingdetails.cgi?url=www.gpcapitalgroup.com

Not found in Open Directory.
Not found in US business database.
No address found on site.
No usable SSL certificate.
Rating: "Site ownership unknown or questionable."

Any questions?

John Nagle
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question regarding string.split()

2007-04-21 Thread Karthik Gurusamy
On Apr 20, 11:51 am, kevinliu23 <[EMAIL PROTECTED]> wrote:
> Hey guys,
>
> So I have a question regarding the split() function in the string
> module. Let's say I have an string...
>
> input = "2b 3 4bx 5b 2c 4a 5a 6"
> projectOptions = (input.replace(" ", "")).split('2')
> print projectOptions
>
> ['', 'b34bx5b', 'c4a5a6']
>

The confusion, as you can see from other posts, is because the
behavior is different from default split().
Default split works on whitespace and  we don't get leading/trailing
empty list items.

So just add input = input.strip('2') after the input assignment (BTW
someone had
pointed input is a reserved identifier). Note this solution will work
for splitting on any sequence of chars..just strip them first. Note we
still get empty elements in the middle of the string -- this probably
we want to get in most cases.

Karthik

> My question is, why is the first element of projectOptions an empty
> string? What can I do so that the first element is not an empty
> string? but the 'b34bx5b' string as I expected?
>
> Thanks so much guys. :)


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: No speedup on multi-processor machine?

2007-04-21 Thread Fuzzyman
On Apr 21, 10:53 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On 21 Apr 2007 14:02:12 -0700, [EMAIL PROTECTED] declaimed the
> following in comp.lang.python:
>
> > Hi,
> > I am using Python Thread library for my parallel processing course
> > project. I am doing matrix convolution on a multi-processor machine
> > running Solaris. I just found out that no speed-up is obtained with
> > threading. It is probably because of something called GIL in Python.
> > How can I get around
> > that GIL and get speed-up?
>
> Threading in Python is optimized for I/O bound processing, wherein
> the threads spend most of their lives sleeping (blocked waiting for some
> I/O to complete, or some lock/event/condition to change state). It is
> not optimized for parallel number crunching.
>
> Options:
>
> Don't use the common "CPython" (eg, the Python built from C-language
> source using the C-runtime library). Jython (a version that runs on the
> JVM, using Java libraries) may not be afflicted with the GIL.
>

IronPython is *definitely* not restricted by the GIL.

Fuzzyman
http://www.voidspace.org.uk/ironpython/index.shtml

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: CGI Script using Python

2007-04-21 Thread Fuzzyman
On Apr 21, 10:02 pm, Tim Roberts <[EMAIL PROTECTED]> wrote:
> "Ralf" <[EMAIL PROTECTED]> wrote:
>
> >is there anybody who can help me.
> >I would like to use a Python Script to download Files from a Server to the
> >Client, to update the Client.
>
> Remember, though, that a CGI script cannot actually put files anywhere. You
> can certainly download files to the client browser, but the user will have
> to be involved to tell the browser where to store the incoming file.
>
> Doing anything else would be a security hole.

But a CGI script can send a file in response to an automated request
from a client application, without ever needing a browser to be
involved.

Sending the right headers means that existing libraries (urllib2 I
guess) will know how to handle the response.

Fuzzyman
http://www.voidspace.org.uk/python/articles.shtml

> --
> Tim Roberts, [EMAIL PROTECTED]
> Providenza & Boekelheide, Inc.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: No speedup on multi-processor machine?

2007-04-21 Thread Caleb Hattingh
On Apr 21, 11:02 pm, [EMAIL PROTECTED] wrote:
> Hi,
> I am using Python Thread library for my parallel processing course
> project. I am doing matrix convolution on a multi-processor machine
> running Solaris. I just found out that no speed-up is obtained with
> threading. It is probably because of something called GIL in Python.
> How can I get around
> that GIL and get speed-up?
> Thanks in advance.
> Daniel

Perhaps try

http://www.parallelpython.com/

or

http://www.its.caltech.edu/~astraw/seppo.html

Caleb

-- 
http://mail.python.org/mailman/listinfo/python-list


Python for Vcard Parsing in UTF16

2007-04-21 Thread R Wood
Greetings -

A recent Perl experiment hasn't turned out so well, which has piqued my
interest in Python.  The project is this: take a Vcard file exported from
Apple's Addressbook and use a language that is good at parsing text to convert
it into a mutt alias file.  There are better ways to use Mutt with Mac's 
addressbook, but I want to be able to periodically convert my working 
addressbook file into an alias file I can then transfer across all my different 
machines - two Macs, two Linux, and one FreeBSD. It's basically a couple of 
regexes that look for FN: followed by a name and convert all the words of the 
name into a single structure separated by underscores, followed by the email 
addresses.  You would wind up with

alias Linus_Torvalds Linus Torvalds <[EMAIL PROTECTED]>

To me this was a natural task for Perl.  Turns out however, there's a catch.  
Apple exports the file in UTF-16 to ensure anyone with Chinese characters in 
their addressbook gets a legitimate Vcard file.  And of course Perl somewhat 
chokes on UTF.  I've found several ways to do it that involve complicated 
downloads and installations of Perl modules, but that defeats the purpose of 
making it simple. In an ideal world you should be able to say "try this cool 
script" and be done with it.  Once you have to say "go to CPAN, download and
compile this module, then ..." it gets less exciting.

I know nothing about Python except that it interests me and has interested me
since I first learned the Rekall database frontend (Linux) runs on it.  I just 
ordered Learning Python and if that works out satisfactorily I'm going to go 
back for Programming Python.  In the meantime, I thought I would pose the 
question to this newsgroup: would Python be useful for a parsing exercise like 
this one?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Suggestion: str.itersplit()

2007-04-21 Thread Dustan
On Apr 21, 4:54 pm, [EMAIL PROTECTED] wrote:
> On Apr 21, 5:58 am, Dustan <[EMAIL PROTECTED]> wrote:
>
> > >From my searches here, there is no equivalent to java's
>
> > StringTokenizer in python, which seems like a real shame to me.
>
> > However, str.split() works just as well, except for the fact that it
> > creates it all at one go. I suggest an itersplit be introduced for
> > lazy evaluation, if you don't want to take up recourses, and it could
> > be used just like java's StringTokenizer.
>
> > Comments?
>
> If your delimiter is a non-empty string, you
> can use an iterator like:
>
> def it(S, sub):
> start = 0
> sublen = len(sub)
> while True:
> idx = S.find(sub,start)
> if idx == -1:
> yield S[start:]
> raise StopIteration
> else:
> yield S[start:idx]
> start = idx + sublen
>
> target_string = 'abcabcabc'
> for subs in it(target_string,'b'):
> print subs

Thanks.

Well, now I know it can be implemented in a reasonably efficient
manner in pure python (ie without having side-efect strings that
aren't of any use, as with concatenation). That's what I was mainly
concerned about.

I feel that it could be a builtin function (seriously, the world
wouldn't end if it was, and nor would python), but this'll work.
That's my last word on the subject.

> For something more complex,
> you may be able to use
> re.finditer.
>
> --
> Hope this helps,
> Steven

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: No speedup on multi-processor machine?

2007-04-21 Thread John Nagle
Caleb Hattingh wrote:
> On Apr 21, 11:02 pm, [EMAIL PROTECTED] wrote:
> 
>>Hi,
>>I am using Python Thread library for my parallel processing course
>>project. I am doing matrix convolution on a multi-processor machine
>>running Solaris. I just found out that no speed-up is obtained with
>>threading. It is probably because of something called GIL in Python.
>>How can I get around
>>that GIL and get speed-up?
>>Thanks in advance.
>>Daniel

If you're actually doing the convolution in Python, you need
optimization before you need more CPUs.  There's a numerics library
for Python called NumPy, but it doesn't have a convolution function,
although it has an FFT, which may be useful.

But this is just homework.  Do something reasonable and turn it
in.  A high performance solution to this problem is probably more
work than it's worth.

John Nagle
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Suggestion: str.itersplit()

2007-04-21 Thread Dustan
On Apr 21, 4:18 pm, Dustan <[EMAIL PROTECTED]> wrote:
> On Apr 21, 7:58 am, Dustan <[EMAIL PROTECTED]> wrote:
>
> > >From my searches here, there is no equivalent to java's
>
> > StringTokenizer in python, which seems like a real shame to me.
>
> > However, str.split() works just as well, except for the fact that it
> > creates it all at one go. I suggest an itersplit be introduced for
> > lazy evaluation, if you don't want to take up recourses, and it could
> > be used just like java's StringTokenizer.
>
> > Comments?
>
> If anybody could inform me on how to get my hands on the python source
> code, I might even be able to come up with an example of how it could
> be implemented. I have no idea how to unzip that tgz or tar.bz2 file
> on a windows machine, though (and that's not from lack of trying).

Thanks to both Jorge Godoy and Alex Martelli for their responses; I
went with winzip. After spending about 10 minutes looking at this
stuff, I can easily conclude that having the code and understanding
the code are 2 very different things (and yes, I do have some
experience in C and C++). But that's a matter to tackle on another day.

-- 
http://mail.python.org/mailman/listinfo/python-list


'\\' in regex affects the following parenthesis?

2007-04-21 Thread voxiac
Could someone tell me why:
>>> import re
>>> p = re.compile('\\.*\\(.*)')

Fails with message:

Traceback (most recent call last):
  File "", line 1, in 
re.compile('\\dir\\(file)')
  File "C:\Python25\lib\re.py", line 180, in compile
return _compile(pattern, flags)
  File "C:\Python25\lib\re.py", line 233, in _compile
raise error, v # invalid expression
error: unbalanced parenthesis

I thought '\\' should just be interpreted as a single '\' and not
affect anything afterwards...

The script 'redemo.py' shipped with Python by default is just fine
about this regex however.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python for Vcard Parsing in UTF16

2007-04-21 Thread Alex Martelli
R Wood <[EMAIL PROTECTED]> wrote:
   ...
> alias Linus_Torvalds Linus Torvalds <[EMAIL PROTECTED]>
> 
> To me this was a natural task for Perl.  Turns out however, there's a catch.
> Apple exports the file in UTF-16 to ensure anyone with Chinese characters in
> their addressbook gets a legitimate Vcard file.  And of course Perl somewhat
> chokes on UTF.  I've found several ways to do it that involve complicated
> downloads and installations of Perl modules, but that defeats the purpose of
> making it simple. In an ideal world you should be able to say "try this cool
> script" and be done with it.  Once you have to say "go to CPAN, download and
> compile this module, then ..." it gets less exciting.
> 
> I know nothing about Python except that it interests me and has interested me
> since I first learned the Rekall database frontend (Linux) runs on it.  I just
> ordered Learning Python and if that works out satisfactorily I'm going to go
> back for Programming Python.  In the meantime, I thought I would pose the
> question to this newsgroup: would Python be useful for a parsing exercise like
> this one?

Sure, Python and Perl (and Ruby) should be equally suitable for the
task, so, if Python appears more suitable by having built-in unicode
capabilities, go for it.  I'm a bit uncertain about the UTF-16 export
though; I know some applications do use it (e.g., Microsoft Entourage),
but I thought Apple's Address Book didn't, and, having just tried a
VCard export from mine, it looks quite ASCII to me.  Maybe you've set
some kind of preference, or...?


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: No speedup on multi-processor machine?

2007-04-21 Thread Neil Hodgson
Fuzzyman:

> IronPython is *definitely* not restricted by the GIL.

IronPython is currently mostly slower than CPython although the 
particular problem should be tested to see if IronPython helps.

Some recent benchmarks between IronPython and CPython:
http://sparcs.kaist.ac.kr/~tinuviel/pybench/

Neil
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: No speedup on multi-processor machine?

2007-04-21 Thread Fuzzyman
On Apr 22, 1:03 am, Neil Hodgson <[EMAIL PROTECTED]>
wrote:
> Fuzzyman:
>
> > IronPython is *definitely* not restricted by the GIL.
>
> IronPython is currently mostly slower than CPython although the
> particular problem should be tested to see if IronPython helps.
>
> Some recent benchmarks between IronPython and 
> CPython:http://sparcs.kaist.ac.kr/~tinuviel/pybench/
>

Yep, I've seen that. :-)

http://www.voidspace.org.uk/python/weblog/arch_d7_2007_04_21.shtml#e687

It's not entirely slower, but mainly slower.

OTOH, if you split a job into two threads on a twin-core machine,
IronPython performance will increase dramatically whilst CPython will
go down...

Additionally, extending IronPython from C# is orders of magnitude
easier than extending CPython from C.

Fuzzyman
http://www.voidspace.org.uk/ironpython/index.shtml

> Neil


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Returning a date as string

2007-04-21 Thread John Machin
On Apr 22, 6:59 am, Björn Keil <[EMAIL PROTECTED]> wrote:
> Hello pythons,
>
> I have little problem with understanding conversions in python. I've
> written a little class - nothing much, just to try out Python a little
> - containing the following method:
>
> def __repr__(self):
> """Serializes the note.
>
> Currently the format of notes isn't decided upon. XML output
> is
> projected."""

Insert here:

print "Due: %r" % self.dateDue
print "Created: %r" % self.dateCreated
print "Subject: %r" % self.subject
print "Content: %r % self.content


> return "Due: " + str(self.dateDue) + \
>"\nDate: " + str(self.dateCreated) + \
>"\nSubject: " + self.subject + \
>"\n" + self.content
>
> The fields "dateDue" and "dateCreated" contain datetime.date objects.
> Now when I try to serialize the whole thing:
>
> >>> myNote
>
> Traceback (most recent call last):
>   File "", line 1, in ?
>   File "notes.py", line 81, in __repr__
> return "Due: " + str(self.dateDue) + \
> TypeError: cannot concatenate 'str' and 'datetime.date' objects
>
> I tryed different variant before I wrapped "self.dateDue" in a str()
> constructor:
> I tried to put it in brackets, or call its .isoformat() method.
> Nothing works. It still complains that I was trying to concatenate a
> string with a date, but I really wanna concatenate a string with a
> string!
>
> Could anyone please tell me what I am doing wrong?

Not showing us the *whole* code that you are executing.

Suggestions:
(1) Maybe dates have crept into self.subject and self.content. The
print statements above should show exactly what you've got.

(2) Maybe you didn't save it before you ran it. Maybe you forgot to
reload(yourModule). Try making up the smallest possible script that
demonstrates your problem. Get out of whatever IDE you may be using
and go to the OS command-line prompt. Use type (windows) or cat (*x)
to print your script on the console. Run your script. Copy the console
contents and paste it into your mail/news client.

HTH,
John

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: '\\' in regex affects the following parenthesis?

2007-04-21 Thread Paul McGuire
On Apr 21, 6:56 pm, [EMAIL PROTECTED] wrote:
> Could someone tell me why:
>
> >>> import re
> >>> p = re.compile('\\.*\\(.*)')
>
> Fails with message:
>
> Traceback (most recent call last):
>   File "", line 1, in 
> re.compile('\\dir\\(file)')
>   File "C:\Python25\lib\re.py", line 180, in compile
> return _compile(pattern, flags)
>   File "C:\Python25\lib\re.py", line 233, in _compile
> raise error, v # invalid expression
> error: unbalanced parenthesis
>
> I thought '\\' should just be interpreted as a single '\' and not
> affect anything afterwards...
>
> The script 'redemo.py' shipped with Python by default is just fine
> about this regex however.

You are getting overlap between the Python string literal \\ escaping
and re's \\ escaping.  In a Python string literal '\\' gets collapsed
down to '\', so to get your desired result, you would need to double-
double every '\', as in:

p = re.compile('.*(.*)')

Ugly, no?  Fortunately, Python has a special form for string literals,
called "raw" which suppresses Python's processing of \'s for escaping
- I think this was done expressly to help simplify entering re
strings.  To use raw format for a string literal, just precede the
opening quotation mark with an r.  Here is your original string, using
a raw literal:

p = re.compile(r'\\.*\\(.*)')

This will compile ok.

(Sometimes these literals are referred to as "raw strings" - I think
this is confusing because new users think this is a special type of
string type, different from str.  This creates the EXACT SAME type of
str; the r just tells the compiler/interpreter to handle the quoted
literal a little differently.  So I prefer to call them "raw
literals".)

-- Paul

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: '\\' in regex affects the following parenthesis?

2007-04-21 Thread John Machin
On Apr 22, 9:56 am, [EMAIL PROTECTED] wrote:
> Could someone tell me why:
>
> >>> import re
> >>> p = re.compile('\\.*\\(.*)')

Short answer: *ALWAYS* use raw strings for regexes in Python source
files.

Long answer:

'\\.*\\(.*)' is equivalent to
r'\.*\(.*)'

So what re.compile is seeing is:

\. -- a literal dot or period or full stop (not a metacharacter)
* -- meaning 0 or more occurrences of the dot
\( -- a literal left parenthesis
. -- dot metacharacter meaning any character bar a newline
* -- meaning 0 or more occurences of almost anything
) -- a right parenthesis grouping metacharacter; a bit lonely hence
the exception.

What you probably want is:

\\ -- literal backslash
.* -- any stuff
\\ -- literal backslash
(.*) -- grouped (any stuff)


>
> Fails with message:
>
> Traceback (most recent call last):
>   File "", line 1, in 
> re.compile('\\dir\\(file)')
>   File "C:\Python25\lib\re.py", line 180, in compile
> return _compile(pattern, flags)
>   File "C:\Python25\lib\re.py", line 233, in _compile
> raise error, v # invalid expression
> error: unbalanced parenthesis
>
> I thought '\\' should just be interpreted as a single '\' and not
> affect anything afterwards...

The second and third paragraphs of the re docs (http://docs.python.org/
lib/module-re.html) cover this:
"""
Regular expressions use the backslash character ("\") to indicate
special forms or to allow special characters to be used without
invoking their special meaning. This collides with Python's usage of
the same character for the same purpose in string literals; for
example, to match a literal backslash, one might have to write ''
as the pattern string, because the regular expression must be "\\",
and each backslash must be expressed as "\\" inside a regular Python
string literal.

The solution is to use Python's raw string notation for regular
expression patterns; backslashes are not handled in any special way in
a string literal prefixed with "r". So r"\n" is a two-character string
containing "\" and "n", while "\n" is a one-character string
containing a newline. Usually patterns will be expressed in Python
code using this raw string notation.
"""

Recommended reading: 
http://www.amk.ca/python/howto/regex/regex.html#SECTION00042

>
> The script 'redemo.py' shipped with Python by default is just fine
> about this regex however.

That's because you are typing the regex into a Tkinter app. Likewise
if you were reading the regex from (say) a config file or were typing
it to a raw_input call. The common factor is that you are not passing
it through an extra level of backslash processing.

HTH,
John

-- 
http://mail.python.org/mailman/listinfo/python-list


Handy short cut for formatting elapsed time in floating point seconds

2007-04-21 Thread Paul McGuire
I am doing some simple timing of some elements of Python scripts, and
the simplest is to just call time.time() before and after key elements
of the script:

t1 = time.time()

# do lengthy operation

t2 = time.time()
print "That took %f seconds" % (t2-t1)

Unfortunately, this gives very ugly timing output, as just a floating
point number of seconds.  After several iterations of writing a
formatter (strftime is not straightforward to use - it omits
milliseconds for one thing), I came up with this:

def secondsToStr(t):
rediv = lambda ll,b : list(divmod(ll[0],b)) + ll[1:]
return "%d:%02d:%02d.%03d" % tuple(reduce(rediv,[[t*1000,],
1000,60,60]))

Now I can write:

print "That took", secondsToStr(t2-t1),"seconds"

and get nicely-formatted 0:00:12.345 style output.

(I also posted this to the Python Cookbook.)

-- Paul

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Handy short cut for formatting elapsed time in floating point seconds

2007-04-21 Thread John Machin
On Apr 22, 11:09 am, Paul McGuire <[EMAIL PROTECTED]> wrote:
> I am doing some simple timing of some elements of Python scripts, and
> the simplest is to just call time.time() before and after key elements
> of the script:
>
> t1 = time.time()
>
> # do lengthy operation
>
> t2 = time.time()
> print "That took %f seconds" % (t2-t1)

Why would you do %f and not %.3f ?

>
> Unfortunately, this gives very ugly timing output, as just a floating
> point number of seconds.  After several iterations of writing a
> formatter (strftime is not straightforward to use - it omits
> milliseconds for one thing), I came up with this:
>
> def secondsToStr(t):
> rediv = lambda ll,b : list(divmod(ll[0],b)) + ll[1:]
> return "%d:%02d:%02d.%03d" % tuple(reduce(rediv,[[t*1000,],
> 1000,60,60]))
>
> Now I can write:
>
> print "That took", secondsToStr(t2-t1),"seconds"
>
> and get nicely-formatted 0:00:12.345 style output.

and it it runs for (say) 83+ seconds you will get
"That took 0:01:23.456 seconds" which is ludicrous.
And if you want to calculate a percentage speed-up later you will have
to convert back to seconds.
>
> (I also posted this to the Python Cookbook.)
>
> -- Paul

IMHO if your job is short,
print "That took %.3f seconds" % (t2 - t1)
is adequate, and your leading 0:00: is useless.
If it runs for minutes or hours, then minutes or hours to (say) 4
significant digits is more than adequate, and printing milliseconds is
useless.

-- 
http://mail.python.org/mailman/listinfo/python-list


Another Python Game Programming Challenge concludes

2007-04-21 Thread Richard Jones
The fourth Python Game Programming Challenge (PyWeek) has now concluded with 
judges (PyWeek being peer-judged) declaring the winners:

 Individual: Which way is up? by Hectigo
   http://www.pyweek.org/e/Hectic/

 Team: Bubble Kong by The Olde Battleaxe
   http://www.pyweek.org/e/toba4/

Congratulations to them and to the other entrants. You may view the complete 
listing of entries here:

   http://www.pyweek.org/4/entries/

and the scores here:

   http://media.pyweek.org/static/pyweek4_ratings.html

and everything else about PyWeek is here:

   http://www.pyweek.org/

Before anyone asks, the next challenge will be in 6 months. There is no a date 
set.


Richard Jones
(the PyWeek guy ;)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Handy short cut for formatting elapsed time in floating point seconds

2007-04-21 Thread George Sakkis
On Apr 21, 9:09 pm, Paul McGuire <[EMAIL PROTECTED]> wrote:

> I am doing some simple timing of some elements of Python scripts, and
> the simplest is to just call time.time() before and after key elements
> of the script:
>
> t1 = time.time()
>
> # do lengthy operation
>
> t2 = time.time()
> print "That took %f seconds" % (t2-t1)
>
> Unfortunately, this gives very ugly timing output, as just a floating
> point number of seconds.  After several iterations of writing a
> formatter (strftime is not straightforward to use - it omits
> milliseconds for one thing), I came up with this:
>
> def secondsToStr(t):
> rediv = lambda ll,b : list(divmod(ll[0],b)) + ll[1:]
> return "%d:%02d:%02d.%03d" % tuple(reduce(rediv,[[t*1000,],
> 1000,60,60]))
>
> Now I can write:
>
> print "That took", secondsToStr(t2-t1),"seconds"
>
> and get nicely-formatted 0:00:12.345 style output.
>
> (I also posted this to the Python Cookbook.)
>
> -- Paul

Cute... for obfuscated python contests :-) Whenever I needed this, a
simple
"Completed in %d minutes and %.1f seconds" % divmod(end-start, 60)
was more than enough.

George

-- 
http://mail.python.org/mailman/listinfo/python-list


Give me the google group address in Chinese.

2007-04-21 Thread 坏蛋
It's really annoying to find there are all English language posts here
because I wanna talk about something about the Chinese related
programs.

Could you give me some ?

-- 
http://mail.python.org/mailman/listinfo/python-list

Can __init__ not return an object?

2007-04-21 Thread Steven W. Orr
When I go to create an object I want to be able to decide whether the 
object is valid or not in __init__, and if not, I want the constructor to 
return something other than an object, (like maybe None). I seem to be 
having problems. At the end of __init__ I say (something like)

if self.something < minvalue:
del self
return None

and it doesn't work. I first tried just the return None, then I got crafty 
and tried the del self. Is what I'm trying to do possible in the 
constructor or do I have to check after I return? Or would raising an 
exception in the constructor be appropriate?

Am I even being clear?

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: No speedup on multi-processor machine?

2007-04-21 Thread Grant Edwards
On 2007-04-21, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

> I am using Python Thread library for my parallel processing
> course project. I am doing matrix convolution on a
> multi-processor machine running Solaris. I just found out that
> no speed-up is obtained with threading. It is probably because
> of something called GIL in Python. How can I get around that
> GIL and get speed-up?

Not much of a parallel processing course.  It appears they
haven't taught you anything about parallel processing.

http://wiki.python.org/moin/ParallelProcessing


-- 
Grant Edwards   grante Yow!  There's a SALE on
  at   STRETCH SOCKS down at the
   visi.com"7-11"!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can __init__ not return an object?

2007-04-21 Thread Carsten Haese
On Sat, 2007-04-21 at 22:36 -0400, Steven W. Orr wrote:
> When I go to create an object I want to be able to decide whether the 
> object is valid or not in __init__, and if not, I want the constructor to 
> return something other than an object, (like maybe None). 
> [...]
>  Or would raising an 
> exception in the constructor be appropriate?

Raising an exception is perfectly appropriate.

-Carsten


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: No speedup on multi-processor machine?

2007-04-21 Thread Robert Kern
John Nagle wrote:
> There's a numerics library
> for Python called NumPy, but it doesn't have a convolution function,
> although it has an FFT, which may be useful.

In [1]: from numpy import *

In [2]: convolve?
Type:   function
Base Class: 
Namespace:  Interactive
File:
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/numpy-1.0.3.dev3714-py2.5-macosx-10.3-fat.egg/numpy/core/numeric.py
Definition: convolve(a, v, mode='full')
Docstring:
Returns the discrete, linear convolution of 1-D sequences a and v; mode
can be 'valid', 'same', or 'full' to specify size of the resulting sequence.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using tkinter to display html

2007-04-21 Thread Stephen M. Gava
On Sat, 21 Apr 2007 09:57:09 -0400, Kevin Walzer wrote:

> Stephen M. Gava wrote:
>> On Fri, 20 Apr 2007 03:28:22 -0700, Rob Wolfe wrote:
> The following thread has various ideas in it:
> http://mail.python.org/pipermail/python-list/2001-
October/107989.html
 thanks mike, i found that thread before i posted here, it doesn'rt
 answer my question though.
>>> Why not? Did you try to use tkhtml [1]_ with python wrapper [2]_. IMHO
>>> it works pretty nice.
>> 
>> i found the tkhtml page but it hasn't been updated since 2002 and the
>> mailing list doesn't even exist anymore. so unsure of the future on
>> that.
>> 
>>> .. [1] http://www.hwaci.com/sw/tkhtml/ .. [2]
>> 
>> didn't find these bindings though, so thanks, i'll have a play.
>> 
>> stephen.
>> 
>>> http://tix.sourceforge.net/Tixapps/src/Python/TkHtml.py
>>>
>>>
> TkHTML:
> 
> http://tkhtml.tcl.tk

ah ok, thanks. i did find this but thought it was unrelated to the widget 
metionned on the old hwaci.com page and that there weren't any existing 
tkinter bindings for this project.

so the tix.sourceforge.net bindings are for this up to date widget, 
including the current V3 that supports css and such?  (thinking aloud, 
i'll check that out myself.)

thanks mate,
stephen.

>
-- 
http://mail.python.org/mailman/listinfo/python-list


Support for new items in set type

2007-04-21 Thread Prateek
I have a bit of a specialized request.

I'm reading a table of strings (specifically fixed length 36 char
uuids generated via uuid.uuid4() in the standard library) from a file
and creating a set out of it.
Then my program is free to make whatever modifications to this set.

When I go back to save this set, I'd like to be able to only save the
new items. Currently I am creating a copy of the set as soon as I load
it and then when I go back to save it, i'm calculating the difference
and saving just the difference.

There are many problems with this approach so far:
1) Calculating the difference via the standard set implementation is
not very scalable -> O(n) I presume
2) Maintaining a copy wastes memory
3) I don't have a good solution if I delete items from the set
(calculating the difference will return an empty set but I need to
actually delete stuff).

I was thinking of writing a specialized set implementation (I have no
idea how to start on this) which tracks new items (added after
initialization) in a separate space and exposes a new API call which
would enable me to directly get those values. This is kind of ugly and
doesn't solve problem 3.

I also thought of using a hastable but I'm storing many ( > 1 million)
of these sets in the same file (most of them will be empty or contain
just a few values but a few of them will be very large - excess of
10,000 items). The file is already separated into tablespaces.

Does anyone have any ideas?

Thanks in advance.
Prateek
PS: Yes - I need blazing fast performance - simply pickling/unpickling
won't do. Memory constraints are important but definitely secondary.
Disk space constraints are not very important.

-- 
http://mail.python.org/mailman/listinfo/python-list


Problem redefining __getitem__ for str subclass

2007-04-21 Thread tsm8015
I do not think I am understanding how to redefine the getitem function
for string. Why does the following not work:

class NStr(str):
def __getitem__(self,idx):
print "NStr:getitem",idx,type(idx)
return str.__getitem__(self,idx)

s=NStr("abcde")

print s[1]
print s[1:4:2]
print s[1:2]

if I run this program (python 2.5; Mac OSX) i get:

$ python strProb.py
NStr:getitem 1 
b
NStr:getitem slice(1, 4, 2) 
bd
b

ie the last statement (s[1:2]) with a simple slice does not call the
new __getitem__ What am I missing.

Thanks

tom

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem redefining __getitem__ for str subclass

2007-04-21 Thread Michel Claveau
Hi!

Same result, with Python 2.5.1 on win-XP;
I have compassion with you.





-- 
@-salutations

Michel Claveau


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's handling of unicode surrogates

2007-04-21 Thread Josiah Carlson
On Apr 20, 7:34 pm, Rhamphoryncus <[EMAIL PROTECTED]> wrote:
> On Apr 20, 6:21 pm, "Martin v. Löwis" <[EMAIL PROTECTED]> wrote:
>
> > > I don't believe this specific variant has been discussed.
> > Now that you clarify it: no, it hasn't been discussed. I find that
> > not surprising - this proposal is so strange and unnatural that
> > probably nobody dared to suggest it.
>
> Difficult problems sometimes need unexpected solutions.
>
> Although Guido seems to be relenting slightly on the O(1) indexing
> requirement, so maybe we'll end up with an O(log n) solution (where n
> is the number of surrogates, not the length of the string).

The last thing I heard with regards to string indexing was that Guido
was very adamant about O(1) indexing.  On the other hand, if one is
willing to have O(log n) access time (where n is the number of
surrogate pairs), it can be done in O(n/logn) space (again where n is
the number of surrogate pairs).  An early version of the structure can
be found here: 
http://mail.python.org/pipermail/python-3000/2006-September/003937.html

I can't seem to find my later post with an updated version (I do have
the source somewhere).

> If you pick an index at random you will get IndexError.  If you
> calculate the index using some combination of len, find, index, rfind,
> rindex you will be unaffected by my change.  You can even assume the
> length of a character so long as you know it fits in 16 bits (ie any
> '\u' escape).
>
> I'm unaware of any practical use cases that would be harmed by my
> change, so that leaves only philosophical issues.  Considering the
> difficulty of the problem it seems like an okay trade-off to me.

It is not ok for s[i] to fail for any index within the range of the
length of the string.

 - Josiah

-- 
http://mail.python.org/mailman/listinfo/python-list


Flat DB seeking speed

2007-04-21 Thread Jia Lu
Hello all

 I see there are lots of flat db or db-like modules in the standard
python modules.
 What about the keywords seeking speed of them ?

 (I want to put about 1 articles with 1 IDs, and I can do
searching keywords with them)

 The db-like modules are :
 dbm, gdbm, dbhash,anydbm,
 pickle(cPickle), shelve, marshal

 Any advice? Thank you.

Jia Lu

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python "robots.txt" parser broken since 2003

2007-04-21 Thread Terry Reedy

"John Nagle" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
| This was reported in 2003, and a patch was uploaded in 2005, but the 
patch
| never made it into Python 2.4 or 2.5.

If the patch is still open, perhaps you could review it.

tjr



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Flat DB seeking speed

2007-04-21 Thread Alex Martelli
Jia Lu <[EMAIL PROTECTED]> wrote:

> Hello all
> 
>  I see there are lots of flat db or db-like modules in the standard
> python modules.
>  What about the keywords seeking speed of them ?
> 
>  (I want to put about 1 articles with 1 IDs, and I can do
> searching keywords with them)
> 
>  The db-like modules are :
>  dbm, gdbm, dbhash,anydbm,
>  pickle(cPickle), shelve, marshal

Your question is somewhat hard to parse (I sympathize, since English
isn't my mother-tongue, either) -- "keywords seeking speed" is very hard
to understand in this context.

Marshal and pickle/cPickle aren't "db-like" at all -- they're just
sequences of serialized objects; marshal is low-level (you can only
serialize objects of some fundamental built-in types), pickle (and the
faster cPickle) higher-level (you can serialize objects of many
different types), but in either case there is no "seeking", "keywords"
or otherwise, just sequential reloading of the objects you serialized.
Handy when that's what you need -- persist a bunch of objects to a disk
file, rebuild them in memory later -- but it doesn't appear to have much
to do with anything in your question and I'm really perplexed as to why
you think it might (what materials did you use to study these subjects,
that gave you such a horrendously wrong impression?!).

All forms of dbm files (including the implementation known as gdbm, as
well as the lookalike one built on top of bsddb), as well as bsddb
(which you don't even mention), represent on disk a map from "key"
strings to "value" strings.  shelve is a modest extension to this
concept: it maps from "key" strings to arbitrary picklable objects
(using cPickle to map such objects to and from strings of bytes).
anydbm is a very thin layer on top of various dbm implementation (it
uses whichdbm to make an informed guess as to what kind of dbm module
was used to make a given dbm-like file, and also uses the best dbm
implementation you have installed to build a new file of some dbm type).

If by "keywords seeking" you mean performing full-text search, none of
these modules are at all suitable.  Perhaps you might want to try
pylucene, , or other such
third-party modules, for the purpose -- it that IS indeed your purpose.


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem redefining __getitem__ for str subclass

2007-04-21 Thread Carsten Haese
On 21 Apr 2007 20:36:13 -0700, tsm8015 wrote
> I do not think I am understanding how to redefine the getitem 
> function for string. Why does the following not work:
> 
> class NStr(str):
> def __getitem__(self,idx):
> print "NStr:getitem",idx,type(idx)
> return str.__getitem__(self,idx)
> 
> s=NStr("abcde")
> 
> print s[1]
> print s[1:4:2]
> print s[1:2]
> 
> if I run this program (python 2.5; Mac OSX) i get:
> 
> $ python strProb.py
> NStr:getitem 1 
> b
> NStr:getitem slice(1, 4, 2) 
> bd
> b
> 
> ie the last statement (s[1:2]) with a simple slice does not call the
> new __getitem__ What am I missing.

You're overriding __getitem__ just fine, but if your object has a __getslice__
method, __getslice__ will be called instead of __getitem__ to fetch simple
slices. Since you are deriving from str which does implement __getslice__,
you'll need to override __getslice__, too.

-Carsten

-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >