Re: GUI apps in Windows with native widgets?

2007-06-18 Thread Tim Roberts
Gilles Ganault <[EMAIL PROTECTED]> wrote:
>
>   I'd like to write a GUI app in Python exclusively for Windows.
>Apparently, development of PythonWin has stopped a long time ago.

I'm not sure why you would think so.  The latest pywin32 release came out
in September, and supports Python 2.2 through 2.6.

However, PythonWin isn't really a framework for writing GUI Python apps.
It's more of an IDE for Python.

>Is there another thin wrapper to write apps in Windows? I'd rather not
>have to ship eg. WxWidgets, GTK+, or QT.

If you are comfortable with the Win32 API and MFC, pywin32 includes a
relatively thin wrapper around MFC.  It's quite possible to write GUI apps
using it, and there are several good examples.

I'm not sure that I'd prefer it to wxPython, however.
-- 
Tim Roberts, [EMAIL PROTECTED]
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Windows XMLRPC Service

2007-06-18 Thread half . italian
On Jun 18, 2:16 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Mon, 18 Jun 2007 00:25:25 -0300, <[EMAIL PROTECTED]> escribió:
>
>
>
>
>
> > I'm trying to serve up a simple XMLRPC server as a windows service.  I
> > got it to run properly, I'm just not sure how to stop it properly.
> > Most of the documentation/examples I found for this was from forums,
> > so I'd love some links to relevant info also.  Here's what I
> > have...taken from the cookbook with the xmlrpc server added:
>
> > def __init__(self, args):
> > win32serviceutil.ServiceFramework.__init__(self, args)
> > # Create an event which we will use to wait on.
> > # The "service stop" request will set this event.
> > self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
>
> > def SvcStop(self):
> > # Before we do anything, tell the SCM we are starting the stop
> > process.
> > self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
>
> > # quit the xmlrpc sever
> > self.server.quit()
>
> What is quit()? As the server may be processing a request I'd move any  
> finalization code below, after exiting the while loop.
>
>
>
> > # And set my event.
> > win32event.SetEvent(self.hWaitStop)
>
> > def SvcDoRun(self):
> > # Serve up the XMLRPC forever
> > self.server =
> > SimpleXMLRPCServer.SimpleXMLRPCServer(("10.0.1.6", 8000))
> > self.server.register_instance(MyClass())
> > self.server.serve_forever()
>
> > win32event.WaitForSingleObject(self.hWaitStop)
>
> The simplest solution is to replace serve_forever with a loop waiting on  
> hWaitStop:
>
>  while WaitForSingleObject(self.hWaitStop, 0)==WAIT_TIMEOUT:
>  self.server.handle_request()
>
> Set the socket timeout to a reasonable value (you'll have to wait that  
> time before exiting). Also, a ThreadingTCPServer may be better if you  
> expect more than a request at a time. If you search past messages you may  
> find other ways.
>
> --
> Gabriel Genellina- Hide quoted text -
>
> - Show quoted text -

I can't quite figure out where to set the "socket timeout".  I tried
setting win32event.WAIT_TIMEOUT, but I'm pretty sure that's not the
variable you were talking about.  I did manage to make it multi-
threaded by incorporating a different recipe, and I'm beginning to
understand the control flow a bit better, but it doesn't seem to be
doing what I expect.  When SvcStop() is executed and calls
win32event.SetEvent(self.hWaitStop), the while loop should break as
win32event.WaitForSingleObject(self.hWaitStop, 0) returns zero at this
point.  But it doesn't do that.  What am I missing?

import win32serviceutil
import win32service
import win32event

import SocketServer
from SimpleXMLRPCServer import
SimpleXMLRPCServer,SimpleXMLRPCRequestHandler

# Threaded mix-in
class
AsyncXMLRPCServer(SocketServer.ThreadingMixIn,SimpleXMLRPCServer):
pass


class MyClass(object):
def hello(self):
return "Hello World"

class SmallestPythonService(win32serviceutil.ServiceFramework):
_svc_name_ = "PythonXMLRPC"
_svc_display_name_ = "PythonXMLRPC"

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
# Create an event which we will use to wait on.
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
import socket
localhost = socket.gethostbyname(socket.gethostname())
self.server = AsyncXMLRPCServer((localhost, 8000),
SimpleXMLRPCRequestHandler)

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
#print "EVENT:",
win32event.WaitForSingleObject(self.hWaitStop, 0) # returns 0 here

def SvcDoRun(self):
self.server.register_instance(MyClass())

#win32event.WAIT_TIMEOUT = 2 --- This just makes the loop
never execute because
# the WaitFor... part always returns 258

while win32event.WaitForSingleObject(self.hWaitStop, 0) ==
win32event.WAIT_TIMEOUT:
self.server.handle_request()

if __name__ == '__main__':
win32serviceutil.HandleCommandLine(SmallestPythonService)

Thanks for any help!

~Sean

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

How do I trap errors in win32com.client?

2007-06-18 Thread Ramdas
How do I trap errors from win32com.client. I have tried the com_error
classes from pywintypes and pythoncom sub modules. It is not catching
for all cases. Is there any docs available?

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


Re: HTMLParser.HTMLParseError: EOF in middle of construct

2007-06-18 Thread Stefan Behnel
Sergio Monteiro Basto wrote:
> Can someone explain me, what is wrong with this site ?
> 
> python linkExtractor3.py http://www.noticiasdeaveiro.pt > test
> 
> HTMLParser.HTMLParseError: EOF in middle of construct, at line 1173,
> column 1
> 
> at line 1173 of test file is perfectly normal .
> 
> I like to know what I have to clean up before parse the html page 
> I send in attach the python code .

You don't want to do these things with HTMLParser. lxml is much easier to use
and supports broken HTML (as in the page you're parsing).

Note that there is a SVN branch of lxml that comes with an html package
(lxml.html) that provides a "clean()" function. Just parse the page with the
HTML parser provided by the package (a few lines), then call the clean()
function on it with the parameters you want to get rid of scripts and the like.

The docs:
http://codespeak.net/lxml/dev/

The SVN branch:
http://codespeak.net/svn/lxml/branch/html/

You seem to be on Linux, so compiling lxml should be simple enough:
http://codespeak.net/lxml/dev/build.html#subversion

Have fun,
Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Do U have anything to share with this students

2007-06-18 Thread Evan Klitzke
On 6/18/07, Stephen Cowell <[EMAIL PROTECTED]> wrote:
> Are you a solipsist?  I am what John Galt would call
> a 'non-absolutist'... *never* say something's impossible,
> there is a non-zero probability of *anything* happening.

Because it's impossible for something to be impossible, right? ;-)

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


Re: Does altering a private member decouple the property's value?

2007-06-18 Thread Jay Loden

Jay Loden wrote:
> Can you elaborate (or just point me to a good doc) on what 
> you mean by an "old style" class versus the new style? I 
> learned Python (well, am still learning) from an older book,
> and I just want to make sure that I'm using the preferred method.

Answering my own question, I know, but Google and the right keywords is a 
wonderful thing. In case anyone else is interested: 

http://www.python.org/doc/newstyle.html

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


Re: sizeof() in python

2007-06-18 Thread Gabriel Genellina
En Tue, 19 Jun 2007 00:20:02 -0300, Alvin Delagon <[EMAIL PROTECTED]>  
escribió:

> Is there a way to know the size of data in python? Like a sizeof feature  
> in
> C? I need this because I have to make a python application that will send
> packets at 1-2 kilobytes/sec via UDP. Thank you in advance guys.

So you don't need the size of arbitrary data, only the size of your  
packets. Assuming you use strings (perhaps built using struct.pack) just  
use len(a_string).

-- 
Gabriel Genellina

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


Re: HTMLParser.HTMLParseError: EOF in middle of construct

2007-06-18 Thread Gabriel Genellina
En Mon, 18 Jun 2007 16:38:18 -0300, Sergio Monteiro Basto  
<[EMAIL PROTECTED]> escribió:

> Can someone explain me, what is wrong with this site ?
>
> python linkExtractor3.py http://www.noticiasdeaveiro.pt > test
>
> HTMLParser.HTMLParseError: EOF in middle of construct, at line 1173,
> column 1
>
> at line 1173 of test file is perfectly normal .

That page is not valid HTML - http://validator.w3.org/ finds 726 errors in  
it.
HTMLParser expects valid HTML - try a different tool, like BeautifulSoup,  
which is specially designed to handle malformed pages.

-- 
Gabriel Genellina

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


Re: Does altering a private member decouple the property's value?

2007-06-18 Thread Jay Loden

Alex Martelli wrote:
>
> class a_class:
> 
> This is ALL of the problem: you're using a legacy (old-style) class, and
> properties (particularly setters) don't work right on its instances (and
> cannot, for backwards compatibility: legacy classes exist exclusively to
> keep backwards compatibility with Python code written many, many years
> ago and should be avoided in new code).
> 
> Change that one line to
> 
> class a_class(object):
> 
> and everything else should be fine.  If you want, I can try to explain
> the why's and wherefore's of the problem, but to understand it requires
> deeper knowledge of Python than you'll need for just about any practical
> use of it: just retain the tidbit "NEVER use oldstyle classes" and you
> won't need to understand WHY you shouldn't use them:-).

Can you elaborate (or just point me to a good doc) on what you mean by an "old 
style" class versus the new style? I learned Python (well, am still learning) 
from an older book, and I just want to make sure that I'm using the preferred 
method.

Thanks, 

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


Re: Does altering a private member decouple the property's value?

2007-06-18 Thread Alex Martelli
Ethan Kennerly <[EMAIL PROTECTED]> wrote:
   ...
> There are a lot of Python mailing lists.  I hope this is an appropriate one
> for a question on properties.

yep, it's a fine one.

> But a gotcha bit me in the behavior of properties that I didn't expect.
> If another function accesses an underlying data member of a property, then
> the data member returned by the property is no longer valid.

You're interpreting wrongly the symptoms you're observing.

> >>> class a_class:

This is ALL of the problem: you're using a legacy (old-style) class, and
properties (particularly setters) don't work right on its instances (and
cannot, for backwards compatibility: legacy classes exist exclusively to
keep backwards compatibility with Python code written many, many years
ago and should be avoided in new code).

Change that one line to

class a_class(object):

and everything else should be fine.  If you want, I can try to explain
the why's and wherefore's of the problem, but to understand it requires
deeper knowledge of Python than you'll need for just about any practical
use of it: just retain the tidbit "NEVER use oldstyle classes" and you
won't need to understand WHY you shouldn't use them:-).


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


sizeof() in python

2007-06-18 Thread Alvin Delagon

Quick question:

Is there a way to know the size of data in python? Like a sizeof feature in
C? I need this because I have to make a python application that will send
packets at 1-2 kilobytes/sec via UDP. Thank you in advance guys.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Trivial string substitution/parser

2007-06-18 Thread Graham Breed
Duncan Booth wote:

> Also, of course, vars just needs to be something which quacks like a dict:
> it can do whatever it needs to do such as looking up a database or querying
> a server to generate the value only when it needs it, or even evaluating
> the name as an expression; in the OP's case it could call get_variable.

And in case that sounds difficult, the code is

class VariableGetter:
def __getitem__(self, key):
return get_variable(key)

> Anyway, the question seems to be moot since the OP's definition of 'elegant
> and lazy' includes regular expressions and reinvented wheels.

Your suggestion of subclassing string.Template will also require a
regular expression -- and a fairly hairy one as far as I can work out
from the documentation.  There isn't an example and I don't think it's
the easiest way of solving this problem.  But if Samuel really wants
backslash escaping it'd be easier to do a replace('$$','') and
replace('\\$', '$$') (or replace('\\$','\\$$') if he really wants the
backslash to persist) before using the template.

Then, if he really does want to reject single letter variable names,
or names beginning with a backslash, he'll still need to subclass
Template and supply a regular expression, but a simpler one.

> ... and in another message Graham Breed wrote:
> > def get_variable(varname):
> > return globals()[varname]
>
> Doesn't the mere thought of creating global variables with unknown names
> make you shudder?

Not at all.  It works, it's what the shell does, and it's easy to test
interactively.  Obviously the application code wouldn't look like
that.


 Graham

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


Does altering a private member decouple the property's value?

2007-06-18 Thread Ethan Kennerly
Hello,

There are a lot of Python mailing lists.  I hope this is an appropriate one
for a question on properties.

I am relatively inexperienced user of Python.  I came to it to prototype
concepts for videogames.   Having programmed in C, scripted in Unix shells,
and scripted in a number of proprietary game scripting languages, I'm
impressed at how well Python meets my needs.  In almost all respects, it
does what I've been wishing a language would do.

One example is properties.  I really like properties for readonly
attributes, and their ability to make the interface more elegant, by hiding
uninteresting methods (like dumb get/set accessors).  As a user interface
designer, I respect how this may prevent the programmer's deluge of
unimportant information.  I also value the ease of refactoring, which is a
frequent task in my prototypes.

But a gotcha bit me in the behavior of properties that I didn't expect.  
If another function accesses an underlying data member of a property, then 
the data member returned by the property is no longer valid.

Here is a session example.

PythonWin 2.4.3 - Enthought Edition 1.0.0 (#69, Aug  2 2006, 12:09:59) [MSC
v.1310 32 bit (Intel)] on win32.
Portions Copyright 1994-2004 Mark Hammond ([EMAIL PROTECTED]) - see
'Help/About PythonWin' for further copyright information.

>>> class a_class:
... def __init__( self ):  self.__p = None
... def __set_p( self, new_p ):  self.__p = new_p
... def reset( self ):  self.__p = None
... p = property( lambda self:  self.__p, __set_p )
... 
>>> a = a_class()
>>> a.p
>>> a.p = 1
>>> a.p
1
>>> a.reset()
>>> a.p
1

Although the underlying value was reset, the property was not reset!

Instead, if the property is edited, then all is fine.  

>>> class a_class:
... def __init__( self ):  self.__p = None
... def __set_p( self, new_p ):  self.__p = new_p
... def reset( self ):  self.p = None   # Property, not the private
member
... p = property( lambda self:  self.__p, __set_p )
... 
>>> a = a_class()
>>> a.p
>>> a.p = 1
>>> a.reset()
>>> a.p

I had wanted to access the private data member in the class to avoid
side-effects of the set method.

Can someone show me how to reference the data member underlying a property
and update the property without calling the property set method?

By the way, I thought maybe that a variable outside of an __init__ method
would be static, but as far as I can tell, it is dynamic.  For example, the
following class appeared equivalent to the above for tests.   

>>> class a_class:
... __p = None # No __init__
... def __set_p( self, new_p ):  self.__p = new_p
... def reset( self ):  self.p = None
... p = property( lambda self:  self.__p, __set_p )
... 
>>> a = a_class()
>>> a.p
>>> a.p = 1
>>> a.reset()
>>> a.p
>>> 

I preferred not having the __init__ for this example and my prototype,
because I wasn't doing anything fancy, and it meant one less method that the
programmer needed to see.  Again, the interface is cleaner.  But does this
cause an error for derived classes that would use the private data member?
Without the __init__ method, is the derived class' __p equal to the base
class' __p?

I thought maybe the class was being referenced instead of the instance, but
a second object had a different value.

>>> b = a_class()
>>> b.p
>>> b.p = 2
>>> b.p
2
>>> b.reset()
>>> a.p
>>> a.p = 1
>>> a.p
1
>>> b.p
>>> b.p = 2
>>> b.reset()
>>> a.p
1
>>> b.p
>>> 

Also properties don't show up in the dictionary if no assignment has been
made, but once a property's assignment has been called, the property
appears.  An example follows:

>>> import pprint
>>> pprint.pprint( a.__dict__ )
{'p': 1}
>>> pprint.pprint( b.__dict__ )
{'p': None}
>>> c = a_class()
>>> pprint.pprint( c.__dict__ )
{}
>>> c.p
>>> pprint.pprint( c.__dict__ )
{}

Is that dictionary population behavior for detecting an uninitialized
property?

Thanks for your help.  When my feet are properly wet, I look forward to
contributing to the community.

-- Ethan Kennerly


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


Re: MI5 Persecution: Goldfish and Piranha 29/9/95 (5104)

2007-06-18 Thread John W. Kennedy
Mike wrote:
> And this is here because ???

He's a broadband-spamming mental case. Plonk him and move on.
-- 
John W. Kennedy
If Bill Gates believes in "intelligent design", why can't he apply it to 
Windows?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting the size of an object

2007-06-18 Thread Gabriel Genellina
En Mon, 18 Jun 2007 16:48:36 -0300, filox <[EMAIL PROTECTED]>  
escribió:
> "Brett Hoerner" <[EMAIL PROTECTED]> wrote in message

>> Although I have the feeling you mean "how many bytes does this object
>> take in memory" - and I believe the short answer is no.
>
> is there a long answer? what i want is to find out the number of bytes  
> the
> object takes up in memory (during runtime). since python has a lot of
> introspection mechanisms i thought that should be no problem...

Consider this:

x = "x" * 100

x is a string taking roughly 1MB of memory.

y = x

y is a string taking roughly 1MB of memory *but* it is shared, in fact it  
is the same object. So you can't add them to get the total memory usage.

z = (x,y)

z takes just a few bytes: a pointer to x, to y, to its own type, its  
reference count. The total memory for the three objects is a few bytes  
more than 1MB.

For arbitrary objects, a rough estimate may be its pickle size:
len(dumps(x)) == 108
len(dumps(y)) == 108
len(dumps(z)) == 116

-- 
Gabriel Genellina

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


Re: GUI apps in Windows with native widgets?

2007-06-18 Thread Michael Torrie
On Tue, 2007-06-19 at 04:13 +0200, Gilles Ganault wrote:
> Hello
> 
>   I'd like to write a GUI app in Python exclusively for Windows.
> Apparently, development of PythonWin has stopped a long time ago.
> 
> Is there another thin wrapper to write apps in Windows? I'd rather not
> have to ship eg. WxWidgets, GTK+, or QT.

Well shipping wxWidgets isn't a big deal.  If you are already requiring
python be installed, then a wxWidgets dll is trivial.  Even better, if
you are using py2exe, then the wxWidgets dll and module will all be
wrapped cleanly in the exe, so I think it's a clear winner.  Users
needn't know anything about wxWidgets or have to install it.  Bundling
wxWidgets in your python app adds a couple of megabytes to the size.

Certainly you're not going to want to do native win32 GUI development,
even in python.  wxWidgets is an order of magnitude better for doing
this, methinks.

> 
> Thank you.

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


GUI apps in Windows with native widgets?

2007-06-18 Thread Gilles Ganault
Hello

I'd like to write a GUI app in Python exclusively for Windows.
Apparently, development of PythonWin has stopped a long time ago.

Is there another thin wrapper to write apps in Windows? I'd rather not
have to ship eg. WxWidgets, GTK+, or QT.

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


Re: avoid script running twice

2007-06-18 Thread Gabriel Genellina
En Mon, 18 Jun 2007 16:30:05 -0300, Nick Craig-Wood <[EMAIL PROTECTED]>  
escribió:

> Tim Williams <[EMAIL PROTECTED]> wrote:
>>  You can also do this by holding a file open in write mode until the
>>  script has finished.
>>
>>try:
>> open('lock.txt','w')
>> my_script()
>>   except:
>>#print script is already running
>
> That only works under windows

Neither. The same file may be opened many times for writing at the same  
time, from the same or different processes, even on Windows. So this alone  
cannot be used as a locking mechanism.

>   >>> f=open('lock.txt','w')
>   >>> g=open('lock.txt','w')
>   >>> f.write('hi')
>   >>> g.write('ho')
>   >>> f.close()
>   >>> g.close()
>   >>> open('lock.txt').read()
>   'ho'

The same happens on Windows. A file *can* be opened with exclusive access,  
but this is not exposed thru the open() function in Python; one should use  
the CreateFile Win32 API function with adequate flags.

-- 
Gabriel Genellina

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


Re: List sequential initialization

2007-06-18 Thread Steve Holden
Chris Mellon wrote:
> On 6/12/07, HMS Surprise <[EMAIL PROTECTED]> wrote:
>> Thanks for the explaination. It didn't seem natural and from the
>> tutorial I read:
>>
>> A value can be assigned to several variables simultaneously:
>>
>> >>> x = y = z = 0  # Zero x, y and z
>>
>>
>> Maybe I infer too much
>>
> 
> And yet, your answer is right there.
> 
> "A value can be assigned to several variables simultaneously"
> 
> When you say want a value assigned to several variables, Python
> doesn't assume that you actually mean you want 2 different values
> assigned to them.

The crucial difference between

a = b = "ab"
a = "a"

and

a = b = ['a', 'b']
a.append('c')

is that in the first case two names are bound to the immutable object 
"ab". Then the first name is rebound to a different immutable object.

In the second example, both names are bound to the same mutable object, 
a list. That object is then modified. The modification can be performed 
using either name, and both names continue to point to the same (but now 
mutated) object.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd   http://www.holdenweb.com
Skype: holdenweb  http://del.icio.us/steve.holden
--- Asciimercial --
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
--- Thank You for Reading -

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


Re: The Modernization of Emacs

2007-06-18 Thread Galen Boyer
On Mon, 18 Jun 2007, [EMAIL PROTECTED] wrote:

> The problem with this line of thinking is that it aims to make Emacs
> appeal to people -- I think it is rather the other way around.
> Certain people appeal to Emacs:  certain kinds of people like Emacs
> and the way it is set up, and they change it to suit their needs.

Emacs will always be for people who like to be able to constantly fiddle
with their environments which continues to increase the efficiency with
which they perform their tasks, increasing the # of tasks they can
perform and therefore increasing the # of peers it would take to equal
the amount of work they alone perform.  Most other environments will be
for those just trying to perform their tasks and staying even with the
average proficiency chart.

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


Re: cStringIO unicode weirdness

2007-06-18 Thread Paul Rubin
Josiah Carlson <[EMAIL PROTECTED]> writes:
> You would be wrong.  The behavior of StringIO and cStringIO are
> different under certain circumstances, and those differences are
> intended.  Among them is when they are confronted with unicode, as you
> saw.  Another is when provided with an initializer...

The doc says there's only supposed to be a difference if the unicode
can't be represented as ascii.  That is not the case with the example
I posted.

> There is a summer of code project that is working towards making them
> behave the same, but the results will need to wait until Python 2.6
> and/or 3.0 .  Note that there are a few "closed, won't fix" bug
> reports regarding these exact same issues in the Python bug tracker at
> sourceforge.

Thanks, this helps.  At minimum the 2.5 docs should be updated to
explain the issues.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Do U have anything to share with this students

2007-06-18 Thread RichL
"Stephen Cowell" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
>
> "Scott en Aztlán" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > "Stephen Cowell" <[EMAIL PROTECTED]> said in misc.transport.road:
> >
> >>> Hope you're protected against malware.
> >>
> >>Yes, the link probably did what it was supposed to,
> >>just not what you thought it would do.  Please, folks,
> >>don't click tinyurls without knowing the person who
> >>made them.
> >
> > And, since that is impossible, use TinyURL's "preview" function before
> > you click.
>
> Are you a solipsist?  I am what John Galt would call
> a 'non-absolutist'... *never* say something's impossible,
> there is a non-zero probability of *anything* happening.
>
> Big shout-out to all the folks in the other NG's... from
> AGA.  We tend to know each other, even though it's
> 'impossible'.

Oh yeah it's impossible!  Someone in another thread "figured out" I was a
"lib" just because I recommended a Fender Blues Jr.  Pretty clever huh?
LOLOLOLOL


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

Re: Efficient way of generating original alphabetic strings like unix file "split"

2007-06-18 Thread py_genetic
On Jun 14, 3:02 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> On Jun 14, 4:39 pm, py_genetic <[EMAIL PROTECTED]> wrote:
>
> > > You didn't try hard enough. :)
>
> > >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465
>
> > > --
> > > HTH,
> > > Rob
>
> > Thanks Rob, "permutation" was the keyword I shcould have used!
>
> See my other post to see if that is indeed what you mean.

Thanks, mensanator I see what you are saying, I appreciate you
clarification.  I modified the unique version to fit my needs,
sometimes you just want the first x unique combinations and of the
right "width" (A or AA or AAA...) string, so I reworked it a bit to be
more efficient.  Isn't this a case of base^n-1 for # unique
combinations, using the alphabet: 26^strlen - 1 or to figure out
strlen from #of combinations needed: ln(26 * #ofcobinations needed)/
ln(26) obviously a float but a pritty good idea of strlen needed when
rounded?

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


Re: Python and (n)curses

2007-06-18 Thread Josiah Carlson
[EMAIL PROTECTED] wrote:
> I need to develop a cross-platform text-mode application. I would like
> to do it in Python and I would like to use a mature text-mode library
> for the UI stuff.
> 
> The obvious choice, I thought, was ncurses. But as far as I can tell,
> it is not available for Python on Windows?
> 
> Is there a workaround? Or are there alternative libraries that might
> be used instead of (n)curses? I know I can use (n)curses on *nix and
> console on Windows etc., but that is of course suboptimal.


This link offers a series of links for console IO...
http://mail.python.org/pipermail/python-list/2005-January/303984.html

Among them is a link to the 'wcurses' module that has been relocated 
here: http://adamv.com/dev/python/curses/  It doesn't actually work in 
the console, but it seems as though you can program towards one API.

There is always cygwin + Python + ncurses.

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


Re: cStringIO unicode weirdness

2007-06-18 Thread Josiah Carlson
Paul Rubin wrote:
> Python 2.5 (r25:51908, Oct  6 2006, 15:24:43)
> [GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu4)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import StringIO, cStringIO
> >>> StringIO.StringIO('a').getvalue()
> 'a'
> >>> cStringIO.StringIO('a').getvalue()
> 'a'
> >>> StringIO.StringIO(u'a').getvalue()
> u'a'
> >>> cStringIO.StringIO(u'a').getvalue()
> 'a\x00\x00\x00'
> >>> 
> 
> I would have thought StringIO and cStringIO would return the
> same result for this ascii-encodeable string.  Worse:

You would be wrong.  The behavior of StringIO and cStringIO are 
different under certain circumstances, and those differences are 
intended.  Among them is when they are confronted with unicode, as you 
saw.  Another is when provided with an initializer...

 >>> cs = cStringIO.StringIO('a')
 >>> cs.write('b')
 Traceback (most recent call last):
   File "", line 1, in ?
 AttributeError: 'cStringIO.StringI' object has no attribute 'write'
 >>> s = StringIO.StringIO('a')
 >>> s.write('b')

There is a summer of code project that is working towards making them 
behave the same, but the results will need to wait until Python 2.6 
and/or 3.0 .  Note that there are a few "closed, won't fix" bug reports 
regarding these exact same issues in the Python bug tracker at sourceforge.

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


Re: Do U have anything to share with this students

2007-06-18 Thread Stephen Cowell

"Scott en Aztlán" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> "Stephen Cowell" <[EMAIL PROTECTED]> said in misc.transport.road:
>
>>> Hope you're protected against malware.
>>
>>Yes, the link probably did what it was supposed to,
>>just not what you thought it would do.  Please, folks,
>>don't click tinyurls without knowing the person who
>>made them.
>
> And, since that is impossible, use TinyURL's "preview" function before
> you click.

Are you a solipsist?  I am what John Galt would call
a 'non-absolutist'... *never* say something's impossible,
there is a non-zero probability of *anything* happening.

Big shout-out to all the folks in the other NG's... from
AGA.  We tend to know each other, even though it's
'impossible'.
__
Steve
.


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

Python and (n)curses

2007-06-18 Thread [EMAIL PROTECTED]
I need to develop a cross-platform text-mode application. I would like
to do it in Python and I would like to use a mature text-mode library
for the UI stuff.

The obvious choice, I thought, was ncurses. But as far as I can tell,
it is not available for Python on Windows?

Is there a workaround? Or are there alternative libraries that might
be used instead of (n)curses? I know I can use (n)curses on *nix and
console on Windows etc., but that is of course suboptimal.

/David

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


Re: getting the size of an object

2007-06-18 Thread John Machin
On Jun 19, 9:00 am, 7stud <[EMAIL PROTECTED]> wrote:
> On Jun 18, 10:07 am, "filox" <[EMAIL PROTECTED]> wrote:
>
> > is there a way to find out the size of an object in Python? e.g., how could
> > i get the size of a list or a tuple?
>
> > --
> > You're never too young to have a Vietnam flashback
>
> You can use the struct module to find the size in bytes:
>
> import struct
>
> mylist = [10, 3.7, "hello"]
>
> int_count = 0
> float_count = 0
> char_count = 0
>
> for elmt in mylist:
> if type(elmt) == int:
> int_count += 1
> elif type(elmt) == float:
> float_count += 1
> elif type(elmt) == str:
> char_count += len(elmt)
>
> format_string = "%di%dd%dc" % (int_count, float_count, char_count)
> list_size_in_bytes  = struct.calcsize(format_string)
> print list_size_in_bytes
>
> --output:--
> 17

That would give you the size taken up by the values. However each
object has in addition to its value, a pointer to its type, and a
reference count -- together an extra 8 bytes each on a 32-bit CPython
implementation. A second problem is that your calculation doesn't
allow for the interning of some str values and some int values. A
third problem is that it caters only for int, float and str elements
-- others count for nothing.

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


Re: fetching text from the screen

2007-06-18 Thread Gabriel Genellina
En Mon, 18 Jun 2007 09:13:16 -0300, Juergen Kareta <[EMAIL PROTECTED]>  
escribió:

> I'm thinking about a python script which fetch some text from the screen
> independent of what application provides the text on the screen. In this
> regard it should be similar to the babylon software: www.babylon.com
>
>
> Here my thoughts:
>
> 1) getting the mouse position
> 2) calculate a adequate rectangle around the mouse position
> 3) fetch the screen content in this rectangle
> 4) use a OCR library to get the text out of 3)
> 5) do something usefull with it.

On Windows, I'd try first using WindowFromPoint to get a window handle,  
and the sending it a WM_GETTEXT message. This should work for all windowed  
controls that contain text of some kind. I'd use your generic approach  
when this doesn't work.

-- 
Gabriel Genellina

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


Re: Making static dicts?

2007-06-18 Thread Steven D'Aprano
On Mon, 18 Jun 2007 21:46:23 +0100, Ognjen Bezanov wrote:

> Hello!
> 
> Just to ask, is it possible to make a static dictionary in python. So 
> that the keys in the dictionary cannot be removed, changed or new ones 
> added, but the value pairs can.
> 
> Is this possible with python?


I'm sure it is possible, but you'll have to program it yourself.

The usual term for what you are describing is "immutable" rather than
static. For some ways of making an immutable class, see here:

http://northernplanets.blogspot.com/2007/01/immutable-instances-in-python.html

To get the dictionary behaviour, the easiest ways would be either to
sub-class from dict:

class ImmutableDict(dict):
pass

or perhaps use delegation (google on "Python automatic delegation" for
more information). 

You might like to look at the source code for the UserDict module in the
standard library for some ideas (especially the DictMixin class).

I leave putting these pieces together into a working immutable dictionary
up to you. Good luck!


-- 
Steven.

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


Re: cStringIO unicode weirdness

2007-06-18 Thread John Machin
On Jun 19, 8:56 am, Paul Rubin  wrote:
> Python 2.5 (r25:51908, Oct  6 2006, 15:24:43)
> [GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu4)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import StringIO, cStringIO
> >>> StringIO.StringIO('a').getvalue()
> 'a'
> >>> cStringIO.StringIO('a').getvalue()
> 'a'
> >>> StringIO.StringIO(u'a').getvalue()
> u'a'
> >>> cStringIO.StringIO(u'a').getvalue()
> 'a\x00\x00\x00'
> >>>
>
> I would have thought StringIO and cStringIO would return the
> same result for this ascii-encodeable string.

Looks like a bug to me.

> Worse:
>
> >>> StringIO.StringIO(u'a').getvalue().encode('utf-8').decode('utf-8')
> u'a'
>
> does the right thing, but
>
> >>> cStringIO.StringIO(u'a').getvalue().encode('utf-8').decode('utf-8')
> u'a\x00\x00\x00'
>
> looks bogus.  Am I misunderstanding something?

Not worse, no more bogus than before. Note that an explicit design
feature of utf8 is that ASCII characters (ord(c) < 128) are unchanged
by the transformation.

>>> 'a\x00\x00\x00'.encode('utf-8')
# IMPLICIT conversion to unicode (effectively .decode('ascii')), then
encoding as utf8
'a\x00\x00\x00' # no change to original buggy result
>>>
>>> 'a\x00\x00\x00'.decode('utf-8')
u'a\x00\x00\x00' # as expected
>>>

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


Re: getting the size of an object

2007-06-18 Thread 7stud
On Jun 18, 10:07 am, "filox" <[EMAIL PROTECTED]> wrote:
> is there a way to find out the size of an object in Python? e.g., how could
> i get the size of a list or a tuple?
>
> --
> You're never too young to have a Vietnam flashback

You can use the struct module to find the size in bytes:

import struct

mylist = [10, 3.7, "hello"]

int_count = 0
float_count = 0
char_count = 0

for elmt in mylist:
if type(elmt) == int:
int_count += 1
elif type(elmt) == float:
float_count += 1
elif type(elmt) == str:
char_count += len(elmt)

format_string = "%di%dd%dc" % (int_count, float_count, char_count)
list_size_in_bytes  = struct.calcsize(format_string)
print list_size_in_bytes

--output:--
17

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


cStringIO unicode weirdness

2007-06-18 Thread Paul Rubin
Python 2.5 (r25:51908, Oct  6 2006, 15:24:43)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import StringIO, cStringIO
>>> StringIO.StringIO('a').getvalue()
'a'
>>> cStringIO.StringIO('a').getvalue()
'a'
>>> StringIO.StringIO(u'a').getvalue()
u'a'
>>> cStringIO.StringIO(u'a').getvalue()
'a\x00\x00\x00'
>>> 

I would have thought StringIO and cStringIO would return the
same result for this ascii-encodeable string.  Worse:

>>> StringIO.StringIO(u'a').getvalue().encode('utf-8').decode('utf-8')
u'a'

does the right thing, but

>>> cStringIO.StringIO(u'a').getvalue().encode('utf-8').decode('utf-8')
u'a\x00\x00\x00'

looks bogus.  Am I misunderstanding something?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting the size of an object

2007-06-18 Thread Lenard Lindstrom
filox wrote:
> "Brett Hoerner" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
>> On Jun 18, 11:07 am, "filox" <[EMAIL PROTECTED]> wrote:
>>> is there a way to find out the size of an object in Python? e.g., how 
>>> could
>>> i get the size of a list or a tuple?
>> "Size" can mean a lot of things,
>>
>> len(my_list)
>> len(my_tuple)
>>
>> Although I have the feeling you mean "how many bytes does this object
>> take in memory" - and I believe the short answer is no.
>>
> 
> is there a long answer? what i want is to find out the number of bytes the 
> object takes up in memory (during runtime). since python has a lot of 
> introspection mechanisms i thought that should be no problem...
> 
> 

New-style classes have both a __basicsize__ and an __itemsize__ 
attribute. __basicsize__ gives the number of bytes in the fixed size 
portion of an instance. For immutable types with variable size, such as 
tuple and str, multiply __itemsize__ by the object length and add to 
__basicsize__ to get the instance size. long type instances also vary in 
length, but since long has no length property trying to figure out the 
size of a particular instance is harder. And types like list, map and 
unicode keep pointers to blocks of memory allocated separately.


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


I found i great site for it!!!

2007-06-18 Thread CW
http://www.bestpcbackup.com

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


Re: Making static dicts?

2007-06-18 Thread Matimus
On Jun 18, 1:46 pm, Ognjen Bezanov <[EMAIL PROTECTED]> wrote:
> Hello!
>
> Just to ask, is it possible to make a static dictionary in python. So
> that the keys in the dictionary cannot be removed, changed or new ones
> added, but the value pairs can.
>
> Is this possible with python?
>
> thanks,
>
> Ognjen.

How much functionality do you need? Something like this might work
(though it could use better error messages.

[code]
class StaticDict:
def __init__(self,srcdict):
self._srcdict = srcdict
def __getitem__(self,idx):
return self._srcdict[idx]
[/code]

Use it like this:

>>> sd = StaticDict({'a':'b'})
>>> sd['a']
'b'
>>> sd['b']
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 5, in __getitem__
KeyError: 'b'
>>> sd['a'] = "hello"
Traceback (most recent call last):
  File "", line 1, in ?
AttributeError: StaticDict instance has no attribute '__setitem__'
>>>

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


Re: avoid script running twice

2007-06-18 Thread Nick Craig-Wood
Tim Williams <[EMAIL PROTECTED]> wrote:
>  On 18/06/07, Nick Craig-Wood <[EMAIL PROTECTED]> wrote:
>  On Windows the open-a-file-for-writing method works well, but as *nix
>  doesn't work the same way then maybe the socket solution is the best
>  cross-platform option.

Actually you could combine your solution and Jeff McNeil's solution to
make something which should work on both windows and unix and is 100%
guaranteed to release the lock on process exit.

import sys

try:
# use fcntl lock if we can
from fcntl import lockf, LOCK_EX, LOCK_NB
from errno import EAGAIN
locking = True
except ImportError:
# otherwise platform mustn't open a file twice for write
if sys.platform != "win32":
raise AssertionError("Unsupported platform for locking")
locking = False

try:
fhandle = file("ourlockfile.txt", "w")
if locking:
lockf(fhandle.fileno(), LOCK_EX|LOCK_NB)
except IOError, e:
if locking and e.errno != EAGAIN:
raise
print >>sys.stderr, "exiting, another copy currently running"

import time
time.sleep(2)

(I tested it on linux only!)

-- 
Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid script running twice

2007-06-18 Thread Nick Craig-Wood
Jeff McNeil <[EMAIL PROTECTED]> wrote:
>  I've got a rather large log processing job here that has the same
>  requirement.  I process and sort Apache logs from an 8-way cluster. I
>  sort and calculate statistics in 15-minute batch jobs. Only one copy
>  should run at once.
> 
>  I open a file and lock it via something like this:
> 
>  import fcntl
> 
>  fhandle = file("ourlockfile.txt", "w")
> 
>  try:
>  fcntl.lockf(fhandle.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
>  except IOError, e:
>  if e.errno == errno.EAGAIN:
>  print >>sys.stderr, "exiting, another copy currently running"
>  else:
>  raise
> 
>  I've got it wrapped in a 'FileBasedLock' class that quacks like Lock
>  objects in the threading module.

That is the traditional unix locking method.  Note that it may not
work if you are writing the lock file to an NFS mount!

Traditionally you write your os.pid() to the file also.  You can then
send a signal to the running copy, detect stale lock files etc.

-- 
Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A patch to support L.count(value, cmp=None, key=None)

2007-06-18 Thread John Machin
On Jun 19, 5:17 am, "BJörn Lindqvist" <[EMAIL PROTECTED]> wrote:
>
>   persons.count("olle", key = attergetter("name"))
>
> is longer and just barely more readable than
>
>   sum(1 for x in persons if x.name == "olle"))
>

The OP's proposal seems to have a very narrow focus, whereas the
generator approach can handle a much wider range of queries:

 sum(1 for x in persons if x.name == "olle" and x.country == "se"))
 sum(x.salary for x in persons if x.name == "olle"))

By the time one has looked up the syntax for the augmented count
method, remembered the existence of something like "attergetter" [sic]
and nutted out its spelling and usage, somebody else using generators
would have the job and gone to lunch :-)

YAGNI. Case dismissed.

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


Re: getting the size of an object

2007-06-18 Thread Brett Hoerner
On Jun 18, 2:48 pm, "filox" <[EMAIL PROTECTED]> wrote:
> is there a long answer? what i want is to find out the number of bytes the
> object takes up in memory (during runtime). since python has a lot of
> introspection mechanisms i thought that should be no problem...

There isn't an automatic way through the language afaik.  I think
allocating memory in order to keep track of how much memory you have
allocated can begin to be a problem. And most people just don't care
down to each and every byte. :)

Some helpful information here:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/a7b9f3c03fb49aa/0e793beec82884f0?lnk=gst&q=size+object&rnum=4#0e793beec82884f0

Brett

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


Re: using Mac OS X CoreGraphics via ctypes

2007-06-18 Thread Richard Jones
Daniel wrote:
> Thanks Diez. I'll try that if I decide to keep going with ctypes. I
> got a bit further but had some problems with memory management (i.e.
> retaining and releasing object references). It seemed like Python/
> ctypes was accessing referenced objects after I had released them,
> which caused segfaults.

I would be unlikely that this is the case. Much more likely is that you've
got some memory allocated by Python which is being passed on to a C library
and is subsequently freed by Python before the C library is finished with
it. Happens all the time. Just make sure you keep a reference in Python
land to any objects you've handed over to C for the lifetime that the C
library is likely to use it.

Having said that, it doesn't look like that's what's happening with your
CGPDFDocumentRelease call, though perhaps you needed to clean up the page
or rect that you obtained in the loop? 

"k" constants defined as strings are 32-bit numbers derived from 4 character
strings. You'll need to convert those strings to the correct number. Once
you've done that I believe your CGColorSpaceCreateWithName call will work.


Richard

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


Re: XMLRPC and SSL

2007-06-18 Thread Martin v. Löwis
Chaz Ginger schrieb:
> I have a web service that I built and it requires using SSL. I have
> found a few examples of clients using SSL but none that allow me to
> change the client's certificate or the chain of certificates the client
> will use to authenticate the server.
> 
> I was wondering if anyone knows of a good example of this?

I don't have an example, but I think you should create a ServerProxy,
passing explicitly a SafeTransport subclass as the transport= argument.
That subclass should override get_host_info, and return the x509
parameters to be passed to httplib.HTTPSConnection (i.e. keyfile=
and cert_file=).

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: passing arguments to tcpserver classes

2007-06-18 Thread Eric Spaulding

Great -- thanks! (and also to J. Ezequiel).

Mark T wrote:
"Eric Spaulding" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
  
Is there an easy way to pass arguments to a handler class that is used by 
the standard TCPServer?


normally --> srvr =SocketServer.TCPServer(('',port_num), TCPHandlerClass)

I'd like to be able to: srvr =SocketServer.TCPServer(('',port_num), 
TCPHandlerClass, (arg1,arg2))


And have arg1, arg2 available via TCPHandlerClass.__init__ or some other 
way.


Where TCPHandlerClass:

class TCPHandlerClass(SocketServer.StreamRequestHandler):
   def handle(self):
  #handle stream events here#


Thanks for any advice.




In the handler class, self.server refers to the server object, so subclass 
the server and override __init__ to take any additional server parameters 
and store them as instance variables.


import SocketServer

class MyServer(SocketServer.ThreadingTCPServer):
def __init__(self, server_address, RequestHandlerClass,arg1,arg2):

SocketServer.ThreadingTCPServer.__init__(self,server_address,RequestHandlerClass)
self.arg1 = arg1
self.arg2 = arg2

class MyHandler(SocketServer.StreamRequestHandler):
def handle(self):
print self.server.arg1
print self.server.arg2

if __name__ == '__main__':
srv = MyServer(('',5000),MyHandler,123,456)
srv.serve_forever()

--Mark

  


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

Re: avoid script running twice

2007-06-18 Thread Jeff McNeil
Note that in real life, the script exits cleanly if another copy is running...

On 6/18/07, Jeff McNeil <[EMAIL PROTECTED]> wrote:
> I've got a rather large log processing job here that has the same
> requirement.  I process and sort Apache logs from an 8-way cluster. I
> sort and calculate statistics in 15-minute batch jobs. Only one copy
> should run at once.
>
> I open a file and lock it via something like this:
>
> import fcntl
>
> fhandle = file("ourlockfile.txt", "w")
>
> try:
> fcntl.lockf(fhandle.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
> except IOError, e:
> if e.errno == errno.EAGAIN:
> print >>sys.stderr, "exiting, another copy currently running"
> else:
> raise
>
> I've got it wrapped in a 'FileBasedLock' class that quacks like Lock
> objects in the threading module.
>
> If the system bombs and reboots, the locks are cleared. If the first
> process dies via 'kill -XX', the locks it owns are released.  Of
> course, external influences can screw it all up, but that's not so
> much a concern in our environment.
>
> While I have no idea how portable this is, it works great for what I'm
> doing on RHES 5.
>
> -Jeff
>
> On 6/18/07, Tim Williams <[EMAIL PROTECTED]> wrote:
> > On 18/06/07, Nick Craig-Wood <[EMAIL PROTECTED]> wrote:
> > > Tim Williams <[EMAIL PROTECTED]> wrote:
> > > >  You can also do this by holding a file open in write mode until the
> > > >  script has finished.
> > > >
> > > >try:
> > > > open('lock.txt','w')
> > > > my_script()
> > > >   except:
> > > >#print script is already running
> > >
> > > That only works under windows
> > >
> > >  >>> f=open('lock.txt','w')
> > >  >>> g=open('lock.txt','w')
> > >  >>> f.write('hi')
> > >  >>> g.write('ho')
> > >  >>> f.close()
> > >  >>> g.close()
> > >  >>> open('lock.txt').read()
> > >  'ho'
> > >  >>>
> > >
> > > The best cross platform way to create a lock is creating a directory.
> > > It is atomic on both windows and linux anyway.
> > >
> > >  try:
> > >os.mkdir("lock")
> > >  except OSError:
> > >print "locked!"
> > >  else:
> > >try:
> > >  do_stuff()
> > >finally:
> > >  os.rmdir("lock")
> > >
> > > (untested)
> > >
> >
> > Being a windows-only person, I didn't know that :)
> >
> > Actually I think I did,  this thread has happened before - a few months ago 
> > :)
> >
> > I would be worried with the directory-exists option for the same
> > reason I don't use the file-exists method currently.  It is possible
> > for the directory to exist when the script isn't running, thus
> > preventing the script from running again until someone notices.
> >
> > On Windows the open-a-file-for-writing method works well, but as *nix
> > doesn't work the same way then maybe the socket solution is the best
> > cross-platform option.  The socket can't exist when the script isn't
> > running, and if you try and create a duplicate socket you catch the
> > exception and exit.
> >
> > IMHO of course.
> >
> > :)
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid script running twice

2007-06-18 Thread Jeff McNeil
I've got a rather large log processing job here that has the same
requirement.  I process and sort Apache logs from an 8-way cluster. I
sort and calculate statistics in 15-minute batch jobs. Only one copy
should run at once.

I open a file and lock it via something like this:

import fcntl

fhandle = file("ourlockfile.txt", "w")

try:
fcntl.lockf(fhandle.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
except IOError, e:
if e.errno == errno.EAGAIN:
print >>sys.stderr, "exiting, another copy currently running"
else:
raise

I've got it wrapped in a 'FileBasedLock' class that quacks like Lock
objects in the threading module.

If the system bombs and reboots, the locks are cleared. If the first
process dies via 'kill -XX', the locks it owns are released.  Of
course, external influences can screw it all up, but that's not so
much a concern in our environment.

While I have no idea how portable this is, it works great for what I'm
doing on RHES 5.

-Jeff

On 6/18/07, Tim Williams <[EMAIL PROTECTED]> wrote:
> On 18/06/07, Nick Craig-Wood <[EMAIL PROTECTED]> wrote:
> > Tim Williams <[EMAIL PROTECTED]> wrote:
> > >  You can also do this by holding a file open in write mode until the
> > >  script has finished.
> > >
> > >try:
> > > open('lock.txt','w')
> > > my_script()
> > >   except:
> > >#print script is already running
> >
> > That only works under windows
> >
> >  >>> f=open('lock.txt','w')
> >  >>> g=open('lock.txt','w')
> >  >>> f.write('hi')
> >  >>> g.write('ho')
> >  >>> f.close()
> >  >>> g.close()
> >  >>> open('lock.txt').read()
> >  'ho'
> >  >>>
> >
> > The best cross platform way to create a lock is creating a directory.
> > It is atomic on both windows and linux anyway.
> >
> >  try:
> >os.mkdir("lock")
> >  except OSError:
> >print "locked!"
> >  else:
> >try:
> >  do_stuff()
> >finally:
> >  os.rmdir("lock")
> >
> > (untested)
> >
>
> Being a windows-only person, I didn't know that :)
>
> Actually I think I did,  this thread has happened before - a few months ago :)
>
> I would be worried with the directory-exists option for the same
> reason I don't use the file-exists method currently.  It is possible
> for the directory to exist when the script isn't running, thus
> preventing the script from running again until someone notices.
>
> On Windows the open-a-file-for-writing method works well, but as *nix
> doesn't work the same way then maybe the socket solution is the best
> cross-platform option.  The socket can't exist when the script isn't
> running, and if you try and create a duplicate socket you catch the
> exception and exit.
>
> IMHO of course.
>
> :)
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid script running twice

2007-06-18 Thread David Wahler
On 6/18/07, Robin Becker <[EMAIL PROTECTED]> wrote:
> Evan Klitzke wrote:
> > Another method that you can use is to open up a socket on some
> > predetermined port (presumably above 1024), and then have your program
> > try to connect to that port and "talk" to the other program to
> > determine whether or not to run (or whether to do some of the
> > remaining work, etc.).
> >
>
> that might work, but this runs on  someone's java solaris box with possible 
> many
> connections going on. I doubt I'd be able to guarantee a particular port.

On platforms where it's supported -- I assume this includes Solaris --
you can use a UNIX-domain socket to avoid the risk of clashing with
network connections. It would be relatively straightforward to fall
back to a normal IP socket on Windows; just test for the existence of
socket.AF_UNIX.

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


Re: csv search and print

2007-06-18 Thread Sergio Monteiro Basto
On Mon, 2007-06-18 at 13:11 -0700, John Machin wrote: 
> On Jun 19, 4:12 am, [EMAIL PROTECTED] wrote:
> > I have a csv file containing lot of rows & columns. I wanted to search thru 
> > the
> > heading for each column for a string and then print all the headings and the
> > corresponding rows if a match is found.
> >
> > Any advise?

you can put cvs in a MySql DB
mysql -e 'CREATE TABLE table2(col1 VARCHAR(255), col2 VARCHAR(255), col3
VARCHAR(255), col4 VARCHAR(255), col5 VARCHAR(255), col6 VARCHAR(255),
col7 VARCHAR(255), col8 VARCHAR(255), col9 VARCHAR(255), col10
VARCHAR(255), col11 VARCHAR(255), col12 VARCHAR(255), col13
VARCHAR(255), col14 VARCHAR(255))' table2

in mysql:
load data infile '/home/sergio/pesquisa/table2.txt' into table table2
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

and work with python and mysql 
http://dustman.net/andy/python/python-and-mysql

> 
> 1. Clarify your requirements: "corresponding rows" ... corresponding
> to what? How many rows/columns is a "lot"? "I have a csv file" ...
> only one? "search thru the heading for each column for a string" is
> open to misinterpretation of various kinds :-)
> 2. Write your script.
> 3. Ask a specific question if you have a problem.
> 
-- 
Sérgio M. B.


smime.p7s
Description: S/MIME cryptographic signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Making static dicts?

2007-06-18 Thread Ognjen Bezanov
Hello!

Just to ask, is it possible to make a static dictionary in python. So 
that the keys in the dictionary cannot be removed, changed or new ones 
added, but the value pairs can.

Is this possible with python?

thanks,

Ognjen.

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


Re: csv search and print

2007-06-18 Thread John Machin
On Jun 19, 4:12 am, [EMAIL PROTECTED] wrote:
> I have a csv file containing lot of rows & columns. I wanted to search thru 
> the
> heading for each column for a string and then print all the headings and the
> corresponding rows if a match is found.
>
> Any advise?

1. Clarify your requirements: "corresponding rows" ... corresponding
to what? How many rows/columns is a "lot"? "I have a csv file" ...
only one? "search thru the heading for each column for a string" is
open to misinterpretation of various kinds :-)
2. Write your script.
3. Ask a specific question if you have a problem.

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


Re: getting the size of an object

2007-06-18 Thread filox

"Brett Hoerner" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> On Jun 18, 11:07 am, "filox" <[EMAIL PROTECTED]> wrote:
>> is there a way to find out the size of an object in Python? e.g., how 
>> could
>> i get the size of a list or a tuple?
>
> "Size" can mean a lot of things,
>
> len(my_list)
> len(my_tuple)
>
> Although I have the feeling you mean "how many bytes does this object
> take in memory" - and I believe the short answer is no.
>

is there a long answer? what i want is to find out the number of bytes the 
object takes up in memory (during runtime). since python has a lot of 
introspection mechanisms i thought that should be no problem...


-- 
You're never too young to have a Vietnam flashback 


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


Re: avoid script running twice

2007-06-18 Thread Tim Williams
On 18/06/07, Nick Craig-Wood <[EMAIL PROTECTED]> wrote:
> Tim Williams <[EMAIL PROTECTED]> wrote:
> >  You can also do this by holding a file open in write mode until the
> >  script has finished.
> >
> >try:
> > open('lock.txt','w')
> > my_script()
> >   except:
> >#print script is already running
>
> That only works under windows
>
>  >>> f=open('lock.txt','w')
>  >>> g=open('lock.txt','w')
>  >>> f.write('hi')
>  >>> g.write('ho')
>  >>> f.close()
>  >>> g.close()
>  >>> open('lock.txt').read()
>  'ho'
>  >>>
>
> The best cross platform way to create a lock is creating a directory.
> It is atomic on both windows and linux anyway.
>
>  try:
>os.mkdir("lock")
>  except OSError:
>print "locked!"
>  else:
>try:
>  do_stuff()
>finally:
>  os.rmdir("lock")
>
> (untested)
>

Being a windows-only person, I didn't know that :)

Actually I think I did,  this thread has happened before - a few months ago :)

I would be worried with the directory-exists option for the same
reason I don't use the file-exists method currently.  It is possible
for the directory to exist when the script isn't running, thus
preventing the script from running again until someone notices.

On Windows the open-a-file-for-writing method works well, but as *nix
doesn't work the same way then maybe the socket solution is the best
cross-platform option.  The socket can't exist when the script isn't
running, and if you try and create a duplicate socket you catch the
exception and exit.

IMHO of course.

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


Re: Python's "only one way to do it" philosophy isn't good?

2007-06-18 Thread Douglas Alan
"Terry Reedy" <[EMAIL PROTECTED]> writes:

> |>oug writes:

>> Scheme has a powerful syntax extension mechanism

> I did not and do not see this as relevant to the main points of my
> summary above.  Python has powerful extension mechanisms too, but
> comparing the two languages on this basis is a whole other topic.

Please note that Guy Steele in his abstract for "Rabbit: A Compiler
for SCHEME", specifically mentions that Scheme is designed to be a
minimal language in which, "All of the traditional imperative
constructs [...] as well as many standard LISP constructs [...] are
expressed in macros in terms of the applicative basis set. [...] The
macro approach enables speedy implementation of new constructs as
desired without sacrificing efficiency in the generated code."

   http://library.readscheme.org/servlets/cite.ss?pattern=Ste-78b

Do you now see how Scheme's syntax extension mechanism is relevant?

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


Re: Memory problem with Python

2007-06-18 Thread Josiah Carlson
Squzer Crawler wrote:
> On Jun 18, 11:06 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>> On Jun 17, 8:51 pm, Squzer Crawler <[EMAIL PROTECTED]> wrote:
>>
>>> i am developing distributed environment in my college using Python. I
>>> am using therads in client for downloading wepages. Even though i am
>>> reusing the thread, memory usage get increased. I don know why.? I am
>>> using BerkelyDB for URLQueue, BeautifulShop for Parsing the webpages.
>>   Isn't the increased memory resulted from storing the already
>> processed pages?
>>
>>   Look first at all places where your code instantiates new
>> objects - and make sure you don't keep references to such objects that
>> are not needed anymore.
>>
>>   Also, reusing threads has nothing to do with saving memory - but
>> with saving on thread creation time, if I understand your problem
>> description.
> 
> what about the cyclic reference.. can i use GC in my program..
> 
> if so, please tell me how to implement.. i am calling the gc.collect()
> at the enf of the fetching.. Will it reduce my program speed. Else in
> which way i can call it..?

Garbage collection should happen automatically as long as you are 
deleting references to objects you no longer need.  If gc.garbage isn't 
empty, then you have unbreakable reference cycles.  It seems more 
likely, as [EMAIL PROTECTED] says, that you are keeping copies of the things 
you already parsed in memory.

What you can do (if you aren't able to find the bug) is have a wrapper 
program that repeatedly starts up your url fetcher via os.system(). 
Then have your url fetcher close itself down every few hours.

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


HTMLParser.HTMLParseError: EOF in middle of construct

2007-06-18 Thread Sergio Monteiro Basto
Hi,
Can someone explain me, what is wrong with this site ?

python linkExtractor3.py http://www.noticiasdeaveiro.pt > test

HTMLParser.HTMLParseError: EOF in middle of construct, at line 1173,
column 1

at line 1173 of test file is perfectly normal .

I like to know what I have to clean up before parse the html page 
I send in attach the python code .

thanks in advance 
-- 
Sérgio M. B.
import urllib
import urlparse
import re
from HTMLParser import HTMLParser, HTMLParseError

class ParserExtractor(HTMLParser):
def __init__(self, base, content):
HTMLParser.__init__(self)

self.__content  = content

self.__base  = base
self.__links = []

def links(self):
self.feed(self.__content)
self.close()
return self.__links

def handle_starttag(self, tag, attr):
attr = dict(attr)

if 'a' == tag:
self.start_a(attr)
if 'base' == tag:
self.start_base(attr)

def start_a(self, attr):
l = attr.get('href')
if l:
self.__links.append( urlparse.urljoin(self.__base, l) )

def start_base(self, attr):
l = attr.get('href')
if l:
self.__base = l

def getLinks(url):
content = None
base = url
content = urllib.urlopen(url).read(-1)
# clean scripts and comments
p = re.compile('',re.S|re.I)
content = p.sub('', content)
p = re.compile('',re.S)
content = p.sub('', content)
print content

links = []
parser = ParserExtractor(url, content)
links  = parser.links()

return links

if __name__ == '__main__':
from sys import argv

for l in getLinks(argv[1]):
print l


smime.p7s
Description: S/MIME cryptographic signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: avoid script running twice

2007-06-18 Thread Nick Craig-Wood
Robin Becker <[EMAIL PROTECTED]> wrote:
>  I looked at the temporary files idea, but I'm not certain about the exact 
>  details. Normally your create a file and then remove it whilst keeping the 
> file 
>  handle; that allows your program to write to it whilst guaranteeing that it 
> will 
>  vanish when you die, but this has to be a named file so that the second 
> instance 
>  can check for it. Doesn't that prevent it from being already
>  removed?

No

> My unix experience is long, but fairly primitive.

Locking with files alone is racy..

You need flock under unix (this recipe shows windows flock equivalent also)

  http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203

or use the directory idea I posted in another post.

-- 
Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid script running twice

2007-06-18 Thread Nick Craig-Wood
Tim Williams <[EMAIL PROTECTED]> wrote:
>  You can also do this by holding a file open in write mode until the
>  script has finished.
> 
>try:
> open('lock.txt','w')
> my_script()
>   except:
>#print script is already running

That only works under windows

  >>> f=open('lock.txt','w')
  >>> g=open('lock.txt','w')
  >>> f.write('hi')
  >>> g.write('ho')
  >>> f.close()
  >>> g.close()
  >>> open('lock.txt').read()
  'ho'
  >>> 

The best cross platform way to create a lock is creating a directory.
It is atomic on both windows and linux anyway.

  try:
os.mkdir("lock")
  except OSError:
print "locked!"
  else:
try:
  do_stuff()
finally:
  os.rmdir("lock")

(untested)

-- 
Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A patch to support L.count(value, cmp=None, key=None)

2007-06-18 Thread BJörn Lindqvist
> I patched Objects/listobject.c to support
>   L.count(value, cmp=None, key=None).
> I tested it with the same script above by replacing slist
> with built-in list.  It worked correctly with this small
> test.  The patch is below (126 lines, I hope that's not

Great! If you want this change included in Python, you should post it
on SourceForge's patch tracker at
http://sourceforge.net/tracker/?group_id=5470&atid=305470. Optionally,
you can also ask if people like the patch on python-dev. But IMHO, the
odds of this patch being accepted are slim (borrowing from the example
in the last thread):

  persons.count("olle", key = attergetter("name"))

is longer and just barely more readable than

  sum(1 for x in persons if x.name == "olle"))

-- 
mvh Björn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid script running twice

2007-06-18 Thread Jay Loden

Robin Becker wrote:
> I wish to prevent a python script from running twice; it's an hourly job, but 
> can take too long.
> 
> My simplistic script looks like
> 
> 
> ...
> def main():
>  fn = 'MARKER'
>  if os.path.isfile(fn):
>  log('%s: hourly job running already' % formatTime())
>  else:
>  f = open(fn,'w')
>  f.write(str(os.getpid()))
>  f.close()
>  try:
>   work()
>  finally:
>  os.remove(fn)
> 
> if __name__=='__main__':
>  main()
> 
> but it occurs to me that I might be killed with prejudice during the long 
> running work(). Is there a smart way to avoid running simultaneously.

You could also write the PID of the currently running process into your MARKER 
file, rather than just using an empty file. Then if your file exists, you can 
compare against os.getpid() and decide what to do from there, like killing your 
existing script, check to see if that PID is still active on the system, etc. 

I've also seen this done by checking the age of the lock file and if it's too 
old, consider it 'stale' and delete it. 

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


Re: avoid script running twice

2007-06-18 Thread Tim Williams
On 18/06/07, Robin Becker <[EMAIL PROTECTED]> wrote:
> Wildemar Wildenburger wrote:
> > Robin Becker wrote:
> .
> >
> > Well I can think of a dumb way: create a temporary file during the
> > transaction and have your script check for that before running its main
> > body.
> >
> >
> > I think thats the most hassle free way of doing it.
> > /W
> I looked at the temporary files idea, but I'm not certain about the exact
> details. Normally your create a file and then remove it whilst keeping the 
> file
> handle; that allows your program to write to it whilst guaranteeing that it 
> will
> vanish when you die, but this has to be a named file so that the second 
> instance
> can check for it.


Doesn't that prevent it from being already removed? My unix
> experience is long, but fairly primitive.


You don't need to remove the file, write anything to it,  or check for
prior existance :)

If one process has the file open for writing, no other process can
open it for writing at the same time - and this is what you are
checking for!   The exception that the second concurrent open/write
attempt raises tells you that another instance of the script is
running.

This has the added advantage that should something go wrong, such as
an abnormal termination or reboot,  the open/write disappears and the
next time the script runs everything is back to normal.

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


Re: Parsing HTML, extracting text and changing attributes.

2007-06-18 Thread Stefan Behnel
[EMAIL PROTECTED] wrote:
> I see there is a couple of tools I could use, and I also heard of
> sgmllib and htmllib. So now there is lxml, Beautiful soup, sgmllib,
> htmllib ...
> 
> Is there any of those tools that does the job I need to do more easily
> and what should I use? Maybe a combination of those tools, which one
> is better for what part of the work?

Well, as I said, use lxml. It's fast, pythonically easy to use, extremely
powerful and extensible. Apart from being the main author :), I actually use
it for lots of tiny things more or less like what you're off to. It's just
plain great for a quick script that gets you from A to B for a bag of documents.

Parse it in with HTML parser (even from URLs), then use XPath to extract
(exactly) what you want and then work on it as you wish. That's short and
simple in lxml.

http://codespeak.net/lxml/dev/tutorial.html
http://codespeak.net/lxml/dev/parsing.html#parsing-html
http://codespeak.net/lxml/dev/xpathxslt.html#xpath

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


Re: How to get existing frames in non-current thread?

2007-06-18 Thread Fabio Zadrozny


>
> Thanks a lot... I guess I'll have to find another way for versions
before
> 2.5 ;-)

The original idea came from
 but it
uses a compiled C extension, yes. The code is really small and you should
not have problems compiling it...



Thanks for the link... it seems pretty nice ;-)

Cheers,

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

Re: suggestion: recursive collections.defaultdict

2007-06-18 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>,
[EMAIL PROTECTED] wrote:

> It seems like
> 
> x = defaultdict(defaultdict(list))
> 
> should do the obvious, but it doesn't.

It *does* the obvious.  Parenthesis after a name means: call this object
*now*.  Any other behavior wouldn't be obvious.

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


Re: avoid script running twice

2007-06-18 Thread Evan Klitzke
On 6/18/07, Robin Becker <[EMAIL PROTECTED]> wrote:
> Evan Klitzke wrote:
> 
>
> >
> > Another method that you can use is to open up a socket on some
> > predetermined port (presumably above 1024), and then have your program
> > try to connect to that port and "talk" to the other program to
> > determine whether or not to run (or whether to do some of the
> > remaining work, etc.).
> >
>
> that might work, but this runs on  someone's java solaris box with possible 
> many
> connections going on. I doubt I'd be able to guarantee a particular port.
> --
> Robin Becker

If you can run long-lived processes, you can write a small daemon that
runs on a particular port forever and use that to establish a port
that the other scripts can use. Of course now the solution is getting
a bit more complicated than you probably want...

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


Re: BeautifulSoup - extract the

2007-06-18 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, gcmartijn
wrote:

> I'm trying to extract something like this:
> 
>  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/
> swflash.cab#version=7,0,19,0" width=640 height=400>
> 
> 
> http://www.macromedia.com/go/
> getflashplayer type=application/x-shockwave-flash width=640 height=400
> bgcolor=#00 scale= showall>
> 
> 
> 
> 
> I don't know how I can get the param
> 
> # below don't work
> for test in soup.fetch('object'):
> print test # nothing

There's no `fetch()`.

In [15]: soup.fetch('object')
---
exceptions.TypeError  Traceback (most recent call last)

/home/new/

TypeError: 'NoneType' object is not callable

> # nothing
> print soup.findAll('param',{'name':'movie'})

Works for me:

In [16]: soup.findAll('param', {'name': 'movie'})
Out[16]:
[
]

> # nothing
> print soup.findAll('object')

This too:

In [17]: soup.findAll('object')
Out[17]:
[http://download.macromedia.com/pub/shockwave/cabs/flash/
swflash.cab#version=7,0,19,0" width="640" height="400">


http://www.macromedia.com/go/"; 
getflashplayer="getflashplayer" type="application/x-shockwave-flash" 
width="640" height="400" bgcolor="#00" scale="showall">

]

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


Re: Parsing HTML, extracting text and changing attributes.

2007-06-18 Thread sebzzz
I see there is a couple of tools I could use, and I also heard of
sgmllib and htmllib. So now there is lxml, Beautiful soup, sgmllib,
htmllib ...

Is there any of those tools that does the job I need to do more easily
and what should I use? Maybe a combination of those tools, which one
is better for what part of the work?

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


BeautifulSoup - extract the

2007-06-18 Thread gcmartijn
I'm trying to extract something like this:

http://download.macromedia.com/pub/shockwave/cabs/flash/
swflash.cab#version=7,0,19,0" width=640 height=400>


http://www.macromedia.com/go/
getflashplayer type=application/x-shockwave-flash width=640 height=400
bgcolor=#00 scale= showall>




I don't know how I can get the param

# below don't work
for test in soup.fetch('object'):
print test # nothing

# nothing
print soup.findAll('param',{'name':'movie'})

# nothing
print soup.findAll('object')

Is it possible with BeautifulSoup to extract the object tag with his
child param ?
I have made everything in BeautifulSoup so I hope its possible :S

Thanks anyway,
GCMartijn

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


Re: Line Wrapping

2007-06-18 Thread Evan Klitzke
On 6/18/07, TeroV <[EMAIL PROTECTED]> wrote:
> Evan Klitzke wrote:
> > All,
> >
> > Mail messages should be wrapped at 78 characters (as suggested in RFC
> > 2822). I want my python batch scripts/cron jobs to enforce this
> > behavior, and format the mail that is sent out so that newline
> > characters are inserted as appropriate to keep line lengths at 78
> > characters or less. I wrote a function to take a string and format it
> > in this manner, but it seemed a bit awkward and un-Python like (my
> > background is mostly C, and the function looks very much like C code).
> > I'd imagine that there's probably a short, succinct, more elegant
> > solution using regular expressions or some module that is unknown to
> > me. Does anyone have any clean, short solutions to this problem?
> >
>
> Does textwrap module has what you need?
>
>  >>> import textwrap
>  >>>
> textwrap.wrap('a',
> 78)
> ['aa',
> 'aaa']
>  >>> textwrap.wrap('This is a very long String. A linewrap module has
> couple functions which might help you to accoplish your goal. HTH', 78)
> ['This is a very long String. A linewrap module has couple functions
> which might', 'help you to accoplish your goal. HTH']

This sounds like exactly what I was looking for -- thanks!

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


Re: Line Wrapping

2007-06-18 Thread TeroV
Evan Klitzke wrote:
> All,
> 
> Mail messages should be wrapped at 78 characters (as suggested in RFC
> 2822). I want my python batch scripts/cron jobs to enforce this
> behavior, and format the mail that is sent out so that newline
> characters are inserted as appropriate to keep line lengths at 78
> characters or less. I wrote a function to take a string and format it
> in this manner, but it seemed a bit awkward and un-Python like (my
> background is mostly C, and the function looks very much like C code).
> I'd imagine that there's probably a short, succinct, more elegant
> solution using regular expressions or some module that is unknown to
> me. Does anyone have any clean, short solutions to this problem?
> 

Does textwrap module has what you need?

 >>> import textwrap
 >>> 
textwrap.wrap('a',
 
78)
['aa',
 
'aaa']
 >>> textwrap.wrap('This is a very long String. A linewrap module has 
couple functions which might help you to accoplish your goal. HTH', 78)
['This is a very long String. A linewrap module has couple functions 
which might', 'help you to accoplish your goal. HTH']
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid script running twice

2007-06-18 Thread Robin Becker
Wildemar Wildenburger wrote:
> Robin Becker wrote:

> 
> Well I can think of a dumb way: create a temporary file during the 
> transaction and have your script check for that before running its main 
> body.
> 
> 
> I think thats the most hassle free way of doing it.
> /W
I looked at the temporary files idea, but I'm not certain about the exact 
details. Normally your create a file and then remove it whilst keeping the file 
handle; that allows your program to write to it whilst guaranteeing that it 
will 
vanish when you die, but this has to be a named file so that the second 
instance 
can check for it. Doesn't that prevent it from being already removed? My unix 
experience is long, but fairly primitive.
-- 
Robin Becker

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


Re: The Modernization of Emacs

2007-06-18 Thread Hal Vaughan
Joel J. Adamson wrote:

> Xah Lee <[EMAIL PROTECTED]> writes:
> 
>> 
>> SIMPLE CHANGES
>>
>> In the following, i describe some critical changes that are also very
>> easy to fix in emacs. If emacs officially adopt these changes, i think
>> it will make a lot people, at least programers, like emacs and choose
>> emacs as their text editor.
> 
> The problem with this line of thinking is that it aims to make Emacs
> appeal to people -- I think it is rather the other way around.
> Certain people appeal to Emacs:  certain kinds of people like Emacs
> and the way it is set up, and they change it to suit their needs.

I worked for years as a special ed teacher and I learned that people have
different learning styles.  It's not just learning, but it's perceiving and
working as well.  Some people will always do better with a command line and
some will always do better with a GUI with point-and-click.  That doesn't
mean one is smarter than the other or one is a true geek and one isn't. 
It's just the way our brains are wired.

Emacs appeals to the type of personality that is often a hard core
programmer.  It works for those that want to customize everything and have
full control over their environment AND do well with a command line rather
than a more visual and graphic environment.  For those, emacs is probably
the best program for them.  

Some people prefer to drive a Miata and some prefer a Dodge Ram.  One isn't
better than the other, they're just different.  Trying to make a Dodge Ram
look like a convertible so Miata lovers will want to use it is a waste. 
It'll never be a Miata and the more people try to make it adaptable so it
can be one, the more they ruin what's special about it.

The more emacs is adapted for the non-technical, the more it'll lose what
makes it special and a good fit for programmers.

> Among your changes, I found none that made sense to me, a person who
> used Unix before Windows became widely used.  For people like me, who
> always preferred Unix, changes like changing "buffer" to "opened file"
> seem inefficient and unnecessary.

It seems to me that is the kind of person emacs is written for.  What will
it gain if a large number of non-technical people start using it in
a "non-emacs" mode?

> Sorry -- this totally falls flat.  It won't make Emacs more widely
> used.  The only thing that will make Emacs more widely used is making
> people aware of it; as soon as I became aware of Emacs (from reading
> Wikipedia, ironically), I began using it and I knew I was stuck with
> it.  It's not even important for the survival of Emacs that it be more
> widely used -- it was never important in the last thirty years of its
> history, why should it be important now that Microsoft Word is so
> widely used?

Let those who need Word use it.  To try to change emacs into something it
isn't is ignoring what makes it special.

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


csv search and print

2007-06-18 Thread axjacob
I have a csv file containing lot of rows & columns. I wanted to search thru the 
heading for each column for a string and then print all the headings and the 
corresponding rows if a match is found.

Any advise?

Thanks

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


Line Wrapping

2007-06-18 Thread Evan Klitzke
All,

Mail messages should be wrapped at 78 characters (as suggested in RFC
2822). I want my python batch scripts/cron jobs to enforce this
behavior, and format the mail that is sent out so that newline
characters are inserted as appropriate to keep line lengths at 78
characters or less. I wrote a function to take a string and format it
in this manner, but it seemed a bit awkward and un-Python like (my
background is mostly C, and the function looks very much like C code).
I'd imagine that there's probably a short, succinct, more elegant
solution using regular expressions or some module that is unknown to
me. Does anyone have any clean, short solutions to this problem?

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


Re: Thunderbird access to this newsgroup

2007-06-18 Thread Steve Holden
Ben Finney wrote:
> Rostfrei <[EMAIL PROTECTED]> writes:
> 
>> What is the news server for this
>> newsgroup.
> 
> Usenet newsgroups are redistributed over many servers worldwide.
> 
> http://en.wikipedia.org/wiki/Usenet>
> 
>> If I ping comp.lang.python it is not resolved.
> 
> That's right. It's the name of a Usenet newsgroup, not a host name.
> 
> news:comp.lang.python>
> 
>> For instance I had no problem configuring Eclipse news server. I
>> just entered news.eclipse.org as a news server address and after it
>> I can browse available groups.
> 
> You can browse the groups available on that server, yes. To browse
> this group, you need to find a server that carries comp.lang.python.
> 
> http://dmoz.org/Computers/Usenet/Public_News_Servers/>
> 

You might find, as many others do, that it's convenient to read this 
group using the Gmane service - it's group gmane.comp.python.general on 
server news.gmane.org.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd   http://www.holdenweb.com
Skype: holdenweb  http://del.icio.us/steve.holden
--- Asciimercial --
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
--- Thank You for Reading -

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


Re: avoid script running twice

2007-06-18 Thread Robin Becker
Evan Klitzke wrote:


> 
> Another method that you can use is to open up a socket on some
> predetermined port (presumably above 1024), and then have your program
> try to connect to that port and "talk" to the other program to
> determine whether or not to run (or whether to do some of the
> remaining work, etc.).
> 

that might work, but this runs on  someone's java solaris box with possible 
many 
connections going on. I doubt I'd be able to guarantee a particular port.
-- 
Robin Becker

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


Re: copy locked files

2007-06-18 Thread Jay Loden


Adam Pletcher wrote:
> Do you mean files marked in-use by the OS, like DLLs used by an open
> application?
> 
> There shouldn't be anything preventing you from copying in-use files, or
> even read-only files if that's what you meant:
> 
>   import shutil
>   shutil.copy('C:\\my_application\\test.dll',
> 'C:\\new_folder\\test.dll')
> 
> Although you can't move or delete an in-use file, AFAIK.

I had to deal with a similar problem using the Win32 API in C recently. You 
can't move or delete an in-use file, as Adam noted. However, you *can* rename a 
file (see http://www.nntp.perl.org/group/perl.makemaker/2006/01/msg2437.html as 
a nice summation of the problem), and MoveFileEx() in the Win32 API has a flag 
that will delete a file on the next reboot. 

As far as copying an in-use file, there are two possibilities I can think of. 
One would be to try disconnecting the lock using the openfiles command (this 
may not exist on all versions of Windows, but it is there on my WinXP box). The 
other would be to use Locked Files Wizard (used to be called CopyLock) 
http://noeld.com/programs.asp?cat=misc 

It comes with a command line version of the tool that you can call from your 
Python script as necessary. Neither is an ideal solution but I can't find any 
other suggestions out there on dealing with this from Python. 

HTH, 

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


Re: avoid script running twice

2007-06-18 Thread Evan Klitzke
On 6/18/07, Tim Williams <[EMAIL PROTECTED]> wrote:
> On 18/06/07, Evan Klitzke <[EMAIL PROTECTED]> wrote:
> > On 6/18/07, Robin Becker <[EMAIL PROTECTED]> wrote:
> > > I wish to prevent a python script from running twice; it's an hourly job, 
> > > but
> > > can take too long.
> > >
> > > My simplistic script looks like
> > >
> > >
> > > ...
> > > def main():
> > >  fn = 'MARKER'
> > >  if os.path.isfile(fn):
> > >  log('%s: hourly job running already' % formatTime())
> > >  else:
> > >  f = open(fn,'w')
> > >  f.write(str(os.getpid()))
> > >  f.close()
> > >  try:
> > > work()
> > >  finally:
> > >  os.remove(fn)
> > >
> > > if __name__=='__main__':
> > >  main()
> > >
> > > but it occurs to me that I might be killed with prejudice during the long
> > > running work(). Is there a smart way to avoid running simultaneously.
> >
> > Another method that you can use is to open up a socket on some
> > predetermined port (presumably above 1024), and then have your program
> > try to connect to that port and "talk" to the other program to
> > determine whether or not to run (or whether to do some of the
> > remaining work, etc.).
>
> You don't need to talk to the socket, a second script trying to create
> a second socket on the same number will throw an exception and you can
> exit the script cleanly without running a second copy.

Just trying to bind to the port is the simplest thing to do if all you
want to do is test if another instance of the script is running,
because as Tim pointed out, you cannot bind to a port that another
program is using. But there's also an advantage to spending the time
to actual set up some sort of communication protocol using the port
for two reasons: 1) if another program binds to the port, you'll be
able to detect this and 2) it may be advantageous to split up the work
among two (or more) processes, and you can do synchronization among
the processes by having them communicate over the socket. For example,
it may be the case that the first process can tell the second instance
of the script "I'm almost done, if you want to help you can work on
this part of the remaining problem."


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


IPV6 Fatal Error

2007-06-18 Thread Tim Graber
Hello.  I am trying to install version 2.5.1 for the first time on our
Solaris 10 server (64 bit).  When I execute #./config as root user, I
get the following error:
 
checking gettaddrinfo bug... buggy
Fatal:  You must get wroking getaddrinfo() function.
   or you can specify "--disable-ipv6".
 
My question is, are we to disable ipv6 in some setup file prior to
running ./configure?ifconfig -a6 does not return any output.
 
Any information on getting this setup would be greatly appreciated.
Thanks.
 
-- Tim
-- 
http://mail.python.org/mailman/listinfo/python-list

A patch to support L.count(value, cmp=None, key=None)

2007-06-18 Thread Ping
Hi,

I patched Objects/listobject.c to support
  L.count(value, cmp=None, key=None).
I tested it with the same script above by replacing slist
with built-in list.  It worked correctly with this small
test.  The patch is below (126 lines, I hope that's not
too big to be pasted here).  This is the first time that
I modified CPython source, and I may very well make
mistakes in (lack of) reference counting or other things.
Comments and corrections are much appreciated!

Regards,
Ping


--- Objects/listobject.c.orig   Sun Oct 29 05:39:10 2006
+++ Objects/listobject.cTue Jun 19 01:04:30 2007
@@ -919,12 +919,12 @@

 /* Comparison function.  Takes care of calling a user-supplied
  * comparison function (any callable Python object), which must not
be
- * NULL (use the ISLT macro if you don't know, or call
PyObject_RichCompareBool
- * with Py_LT if you know it's NULL).
- * Returns -1 on error, 1 if x < y, 0 if x >= y.
+ * NULL.
+ * Returns -9 on error, otherwise return the result of the user-
supplied
+ * comparison.
  */
 static int
-islt(PyObject *x, PyObject *y, PyObject *compare)
+custom_compare(PyObject *x, PyObject *y, PyObject *compare)
 {
PyObject *res;
PyObject *args;
@@ -936,7 +936,7 @@
 */
args = PyTuple_New(2);
if (args == NULL)
-   return -1;
+   return -9;
Py_INCREF(x);
Py_INCREF(y);
PyTuple_SET_ITEM(args, 0, x);
@@ -944,16 +944,28 @@
res = PyObject_Call(compare, args, NULL);
Py_DECREF(args);
if (res == NULL)
-   return -1;
+   return -9;
if (!PyInt_Check(res)) {
Py_DECREF(res);
PyErr_SetString(PyExc_TypeError,
"comparison function must return
int");
-   return -1;
+   return -9;
}
i = PyInt_AsLong(res);
Py_DECREF(res);
-   return i < 0;
+   return i;
+}
+
+/* "less-than" Comparison function.  Calls custom_compare to do the
+ * actual comparison.
+ * Returns -1 on error, 1 if x < y, 0 if x >= y.
+ */
+static int
+islt(PyObject *x, PyObject *y, PyObject *compare)
+{
+   int res = custom_compare(x, y, compare);
+   if (res == -9) return -1;
+   return res < 0;
 }

 /* If COMPARE is NULL, calls PyObject_RichCompareBool with Py_LT,
else calls
@@ -2232,16 +2244,44 @@
 }

 static PyObject *
-listcount(PyListObject *self, PyObject *v)
+listcount(PyListObject *self, PyObject * args, PyObject *kwds)
 {
+   PyObject *v = NULL; /* value for counting */
+   PyObject *compare = NULL;
+   PyObject *keyfunc = NULL;
+   static char *kwlist[] = {"value", "cmp", "key", 0};
+   PyObject *item;
Py_ssize_t count = 0;
Py_ssize_t i;
+   int cmp;
+
+   assert(self != NULL);
+   assert (PyList_Check(self));
+   if (args != NULL) {
+   if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|
OO:count",
+   kwlist, &v, &compare, &keyfunc))
+   return NULL;
+   }
+   if (compare == Py_None)
+   compare = NULL;
+   if (keyfunc == Py_None)
+   keyfunc = NULL;

for (i = 0; i < self->ob_size; i++) {
-   int cmp = PyObject_RichCompareBool(self->ob_item[i],
v, Py_EQ);
+   item = self->ob_item[i];
+   if (keyfunc != NULL) {
+   item = PyObject_CallFunctionObjArgs(keyfunc,
item,
+  NULL);
+   }
+
+   if (compare != NULL) {
+   cmp = custom_compare(item, v, compare);
+   } else {
+   cmp = PyObject_RichCompareBool(item, v,
Py_EQ);
+   }
if (cmp > 0)
count++;
-   else if (cmp < 0)
+   else if (cmp == -9)
return NULL;
}
return PyInt_FromSsize_t(count);
@@ -2404,7 +2444,7 @@
 PyDoc_STRVAR(index_doc,
 "L.index(value, [start, [stop]]) -> integer -- return first index of
value");
 PyDoc_STRVAR(count_doc,
-"L.count(value) -> integer -- return number of occurrences of
value");
+"L.count(value, cmp=None, key=None) -> integer -- return number of
occurrences of value [in the key] [with the cmp function].");
 PyDoc_STRVAR(reverse_doc,
 "L.reverse() -- reverse *IN PLACE*");
 PyDoc_STRVAR(sort_doc,
@@ -2422,7 +2462,7 @@
{"pop", (PyCFunction)listpop, METH_VARARGS,
pop_doc},
{"remove",  (PyCFunction)listremove,  METH_O, remove_doc},
{"index",   (PyCFunction)listindex,   METH_VARARGS,
index_doc},
-   {"count",   (PyCFunction)listcount,   METH_O, count_doc},
+   {"count",   (PyCFunction)listcount,   METH_VARARGS |
METH_KEYWORDS, count_doc},
{"reverse", (PyCFunction)listreverse, METH_NOARGS,
reverse_doc},
{"sort",(PyCFunction)listsort,METH_V

Re: avoid script running twice

2007-06-18 Thread Tim Williams
On 18/06/07, Wildemar Wildenburger <[EMAIL PROTECTED]> wrote:
> Robin Becker wrote:
> > I wish to prevent a python script from running twice; it's an hourly job, 
> > but
> > can take too long.
> >
> > [snip]
> > but it occurs to me that I might be killed with prejudice during the long
> > running work(). Is there a smart way to avoid running simultaneously.
> >
>
> Well I can think of a dumb way: create a temporary file during the
> transaction and have your script check for that before running its main
> body.

>
> I think thats the most hassle free way of doing it.

If the script finishes abnormally, the file will still exist and
future execution of the script will fail.

:)




-- 

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


Re: Parsing HTML, extracting text and changing attributes.

2007-06-18 Thread Jay Loden

Stefan Behnel wrote:
> Jay Loden wrote:
>> Someone else mentioned lxml but as I understand it lxml will only work if
>> it's valid XHTML that they're working with.
> 
> No, it was meant as the OP requested. It even has a very good parser from
> broken HTML.
> 
> http://codespeak.net/lxml/dev/parsing.html#parsing-html

I stand corrected, I missed that whole part of the LXML documentation :-)

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


Re: Parsing HTML, extracting text and changing attributes.

2007-06-18 Thread Jay Loden

Stefan Behnel wrote:
> Jay Loden wrote:
>> Someone else mentioned lxml but as I understand it lxml will only work if
>> it's valid XHTML that they're working with.
> 
> No, it was meant as the OP requested. It even has a very good parser from
> broken HTML.
> 
> http://codespeak.net/lxml/dev/parsing.html#parsing-html

I stand corrected, I missed that whole part of the LXML documentation :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid script running twice

2007-06-18 Thread Tim Williams
On 18/06/07, Evan Klitzke <[EMAIL PROTECTED]> wrote:
> On 6/18/07, Robin Becker <[EMAIL PROTECTED]> wrote:
> > I wish to prevent a python script from running twice; it's an hourly job, 
> > but
> > can take too long.
> >
> > My simplistic script looks like
> >
> >
> > ...
> > def main():
> >  fn = 'MARKER'
> >  if os.path.isfile(fn):
> >  log('%s: hourly job running already' % formatTime())
> >  else:
> >  f = open(fn,'w')
> >  f.write(str(os.getpid()))
> >  f.close()
> >  try:
> > work()
> >  finally:
> >  os.remove(fn)
> >
> > if __name__=='__main__':
> >  main()
> >
> > but it occurs to me that I might be killed with prejudice during the long
> > running work(). Is there a smart way to avoid running simultaneously.
>
> Another method that you can use is to open up a socket on some
> predetermined port (presumably above 1024), and then have your program
> try to connect to that port and "talk" to the other program to
> determine whether or not to run (or whether to do some of the
> remaining work, etc.).

You don't need to talk to the socket, a second script trying to create
a second socket on the same number will throw an exception and you can
exit the script cleanly without running a second copy.

You can also do this by holding a file open in write mode until the
script has finished.

  try:
   open('lock.txt','w')
   my_script()
 except:
  #print script is already running

If the file is already open the script won't run,   if the script
finshes/crashes or the machine reboots the open file will close.


In both cases if the script finishes normally or crashes, or the
machine is restarted. The lock  (ie socket or open file) is released.

HTH :)






-- 

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


Re: avoid script running twice

2007-06-18 Thread Wildemar Wildenburger
Robin Becker wrote:
> I wish to prevent a python script from running twice; it's an hourly job, but 
> can take too long.
>
> [snip]
> but it occurs to me that I might be killed with prejudice during the long 
> running work(). Is there a smart way to avoid running simultaneously.
>   

Well I can think of a dumb way: create a temporary file during the 
transaction and have your script check for that before running its main 
body.


I think thats the most hassle free way of doing it.
/W
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: avoid script running twice

2007-06-18 Thread Evan Klitzke
On 6/18/07, Robin Becker <[EMAIL PROTECTED]> wrote:
> I wish to prevent a python script from running twice; it's an hourly job, but
> can take too long.
>
> My simplistic script looks like
>
>
> ...
> def main():
>  fn = 'MARKER'
>  if os.path.isfile(fn):
>  log('%s: hourly job running already' % formatTime())
>  else:
>  f = open(fn,'w')
>  f.write(str(os.getpid()))
>  f.close()
>  try:
> work()
>  finally:
>  os.remove(fn)
>
> if __name__=='__main__':
>  main()
>
> but it occurs to me that I might be killed with prejudice during the long
> running work(). Is there a smart way to avoid running simultaneously.

Another method that you can use is to open up a socket on some
predetermined port (presumably above 1024), and then have your program
try to connect to that port and "talk" to the other program to
determine whether or not to run (or whether to do some of the
remaining work, etc.).

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


Re: copy locked files

2007-06-18 Thread rubbishemail
Hi Adam,



On 18 Jun., 18:41, "Adam Pletcher" <[EMAIL PROTECTED]> wrote:
> Do you mean files marked in-use by the OS, like DLLs used by an open
> application?
I dont know the exact name, but some programs totally lock the files,
like Visual Studio

shutil.copy('C:\\a\\test\\test.ncb','C:\\b\test.ncb')

IOError: [Errno 13] Permission denied: 'C:\\a\\test\\test.ncb'

As soon as I quit the application I can copy the file.

(I am writing a backup software which saves the changed files every
few minutes. If there was a slight possibility
to copy a file which is written at the same moment and therefore not
consistent this is still better than no backup)

>
> There shouldn't be anything preventing you from copying in-use files, or
> even read-only files if that's what you meant:

That's exactly my opinion!


>
> Although you can't move or delete an in-use file, AFAIK.
this seems to be easier, you can use inuse.exe
http://support.microsoft.com/kb/228930/en-us


Daniel

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


Re: Parsing HTML, extracting text and changing attributes.

2007-06-18 Thread Stefan Behnel
Jay Loden wrote:
> Someone else mentioned lxml but as I understand it lxml will only work if
> it's valid XHTML that they're working with.

No, it was meant as the OP requested. It even has a very good parser from
broken HTML.

http://codespeak.net/lxml/dev/parsing.html#parsing-html

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


avoid script running twice

2007-06-18 Thread Robin Becker
I wish to prevent a python script from running twice; it's an hourly job, but 
can take too long.

My simplistic script looks like


...
def main():
 fn = 'MARKER'
 if os.path.isfile(fn):
 log('%s: hourly job running already' % formatTime())
 else:
 f = open(fn,'w')
 f.write(str(os.getpid()))
 f.close()
 try:
work()
 finally:
 os.remove(fn)

if __name__=='__main__':
 main()

but it occurs to me that I might be killed with prejudice during the long 
running work(). Is there a smart way to avoid running simultaneously.
-- 
Robin Becker

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


suggestion: recursive collections.defaultdict

2007-06-18 Thread [EMAIL PROTECTED]
It seems like

x = defaultdict(defaultdict(list))

should do the obvious, but it doesn't.  This seems to work

y = defaultdict(lambda: defaultdict(list))

though is a bit uglier.

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


RE: copy locked files

2007-06-18 Thread Adam Pletcher
Do you mean files marked in-use by the OS, like DLLs used by an open
application?

There shouldn't be anything preventing you from copying in-use files, or
even read-only files if that's what you meant:

  import shutil
  shutil.copy('C:\\my_application\\test.dll',
'C:\\new_folder\\test.dll')

Although you can't move or delete an in-use file, AFAIK.

- Adam
 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] 
> On Behalf Of [EMAIL PROTECTED]
> Sent: Monday, June 18, 2007 9:50 AM
> To: python-list@python.org
> Subject: copy locked files
> 
> Hello,
> 
> do you know of any way to copy locked / opened files under win xp?
> I know there is something like "Volume Shadow Copy" but I 
> don't know how to use it.
> Maybe someone already has a python solution?
> 
> 
> Many thanks
> 
> 
> Daniel
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing HTML, extracting text and changing attributes.

2007-06-18 Thread Jay Loden

Neil Cerutti wrote:
> You could get good results, and save yourself some effort, using
> links or lynx with the command line options to dump page text to
> a file. Python would still be needed to automate calling links or
> lynx on all your documents.

OP was looking for a way to parse out part of the file and apply classes to 
certain types of tags. Using lynx/links wouldn't help, since the output of 
links or lynx is going to end up as plain text and the desire isn't to strip 
all the formatting. 

Someone else mentioned lxml but as I understand it lxml will only work if it's 
valid XHTML that they're working with. Assuming it's not (since real-world HTML 
almost never is), perhaps BeautifulSoup will fare better. 

http://www.crummy.com/software/BeautifulSoup/documentation.html

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


Re: Matrix Multiplication

2007-06-18 Thread sturlamolden
On Jun 18, 11:20 am, Jeremy Sanders  wrote:

> > NumPy has a matrix type that overloads the * operator.

> Just a tiny followup, which may be important unless you carefully read the
> documentation. The "*" operator doesn't do matrix multiplication for normal
> numpy arrays


That's what I wrote: "NumPy has a matrix type." It is called called
numpy.matrix.

I did not suggest using the array type numpy.array.

Reading carefully is indeed important...







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


Re: getting the size of an object

2007-06-18 Thread Brett Hoerner
On Jun 18, 11:07 am, "filox" <[EMAIL PROTECTED]> wrote:
> is there a way to find out the size of an object in Python? e.g., how could
> i get the size of a list or a tuple?

"Size" can mean a lot of things,

len(my_list)
len(my_tuple)

Although I have the feeling you mean "how many bytes does this object
take in memory" - and I believe the short answer is no.



Brett

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


Re: The Modernization of Emacs

2007-06-18 Thread Robin Becker
Joel J. Adamson wrote:
> people aware of it; as soon as I became aware of Emacs (from reading
> Wikipedia, ironically), I began using it and I knew I was stuck with
> it.  It's not even important for the survival of Emacs that it be more

many others get stuck with a preferred editor/system/language; I guess it takes 
some real enthusiasm to switch from vi to emacs or jedit or brief. Years ago 
they switched my escape key from top right to top left (dec keyboards were 
weird); the body is very hard to retrain. I don't know what those synapse 
memories are called, bit they go very deep.
-- 
Robin Becker

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


getting the size of an object

2007-06-18 Thread filox
is there a way to find out the size of an object in Python? e.g., how could 
i get the size of a list or a tuple?

-- 
You're never too young to have a Vietnam flashback 


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


Re: Custom wxPython Widget

2007-06-18 Thread kyosohma
On Jun 15, 10:24 pm, Jens Thiede <[EMAIL PROTECTED]> wrote:
> What is the best source code to read? Any tips; suggestions?
>
> Thanks in advance
> Jens Thiede

I would recommend reading the wxPython demo source code. Also, this
website has some custom widgets written in "pure wxPython", some of
which are in the demo: http://xoomer.alice.it/infinity77/eng/freeware.html

Mike

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


Re: PyRun_String with Py_single_input to stdout?

2007-06-18 Thread [EMAIL PROTECTED]
I found a solution using sys.displayhook here:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/593cd28e568c32e1/1e0f930e7ac5ebb2?#1e0f930e7ac5ebb2

On Jun 18, 4:24 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Mon, 18 Jun 2007 01:45:38 -0300, [EMAIL PROTECTED]
> <[EMAIL PROTECTED]> escribió:
>
> > I'm using PyRun_String with Py_single_input for a python interpreter
> > embedded in my application. I'm using Py_single_input. Py_single input
> > is what I want, but it seems to output to stdout. Before when I was
> > using Py_eval_input I was able to grab the result so I could print it
> > in a text box:
>
> >  PyObject *resultObject = PyObject_Str( rstring );
> >  if( resultObject != NULL ) {
> >char *string = PyString_AsString( resultObject );
> >   }
>
> > But Py_eval_input is only for isolated evaluation, not what I want.
> > Py_single_input gives "None" for the string.
>
> Maybe because it actually returns None?

No it doesn't. I know the function returns something because when I
used Py_eval_input, it returned the correct output. The only
difference is thate Py_string_input outputs using sys.write (stdout)

>
> > I wrote a class that redirects std::cout, but this doesn't work for
> > this, I'm guessing because it uses printf to stdout, not cout.
>
> > Anyone know how I can get the string so I can print it in a text box.
>
>  From your description this should be working... try posting some more code
> showing how you call PyRun_String and how you process the result...
>
> --
> Gabriel Genellina


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

Re: smtp server simulation using Python

2007-06-18 Thread Dave Borne
> I have a (web) development computer w/o an SMTP server and want to test
> form generated e-mail using a dummy SMTP server that delivers the mail
> message to a file, or better yet, to a text editor instead of actually
> sending it.

Here's a quick and dirty script I use this for email testing purposes
- it's windows specific, but that's easy enough to change.

import smtpd, os, time, asyncore

class mailserver(smtpd.SMTPServer):
def __init__(self):
smtpd.SMTPServer.__init__(self, ('',25), None)
print 'Mailsink listening on port 25'

def process_message(self, peer, mailfrom, rcpttos, data):
basepath='c:\\.maildump'

print 'mail from: %s to: %s' %(mailfrom, repr(rcpttos))
for rcpt in rcpttos:
rcpt = rcpt.split('@')[0]
try:
os.mkdir(basepath+'\\'+rcpt)
except OSError:
pass

f =
file(basepath+'\\'+rcpt+'\\'+mailfrom+time.strftime('%Y%m%d%H%M%S'),
'w')
f.write(data)
f.close()

def loop ():
x = mailserver()
try:
asyncore.loop(timeout=2)
except KeyboardInterrupt:
print'interrupt'
x.close()

if __name__=='__main__':
loop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Do U have anything to share with this students

2007-06-18 Thread John Ashby
Josh Hill wrote:

> On Sat, 16 Jun 2007 13:06:23 -, Milt
> <[EMAIL PROTECTED]> wrote:
> 
>>On Jun 15, 1:00?pm, [EMAIL PROTECTED] wrote:
>>> This is a network of students. Find the people of your kind there
>>>
>>> http://tinyurl.com/33uvla
>>>
>>> Click and register to access millions of students.
>>
>>Your link doesn't work.
> 
> Hope you're protected against malware.
> 

The URL in question has been terminated by tinyurl for violation of TOS.
Milt, you need to run an uptodate virus/spyware checker on your
computer (although looking at your headers, you seem to be on a Mac, so
it might not be as urgent).

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


Re: Parsing HTML, extracting text and changing attributes.

2007-06-18 Thread Neil Cerutti
On 2007-06-18, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> I work at this company and we are re-building our website: http://caslt.org/.
> The new website will be built by an external firm (I could do it
> myself, but since I'm just the summer student worker...). Anyways, to
> help them, they first asked me to copy all the text from all the pages
> of the site (and there is a lot!) to word documents. I found the idea
> pretty stupid since style would have to be applied from scratch anyway
> since we don't want to get neither old html code behind nor Microsoft
> Word BS code.
>
> I proposed to take each page and making a copy with only the text, and
> with class names for the textual elements (h1, h1, p, strong, em ...)
> and then define a css file giving them some style.
>
> Now, we have around 1 600 documents do work on, and I thought I could
> challenge myself a bit and automate all the dull work. I thought about
> the possibility of parsing all those pages with python, ripping of the
> navigations bars and just keeping the text and layout tags, and then
> applying class names to specific tags. The program would also have to
> remove the table where text is located in. And other difficulty is
> that I want to be able to keep tables that are actually used for
> tabular data and not positioning.
>
> So, I'm writing this to have your opinion on what tools I
> should use to do this and what technique I should use.

You could get good results, and save yourself some effort, using
links or lynx with the command line options to dump page text to
a file. Python would still be needed to automate calling links or
lynx on all your documents.

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


Re: sqlite3 bug??

2007-06-18 Thread Hyuga
On Jun 18, 11:01 am, Hyuga <[EMAIL PROTECTED]> wrote:
> In fact, I have code in which references to a
> db connection are passed around, so I have to be careful about
> explicitly closing the connection, lest it be in use by some other
> method somewhere.
>

Hate to reply to myself, but I should clarify that passing around a
handle to an existing DB connection is necessary as a means of
allowing multiple methods that write to the DB to operate atomically
before calling commit().  So again, if you're doing something like
that, you want to be absolutely certain before you close your
connection that it's not in use elsewhere.

Hyuga

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


  1   2   >