Re: the annoying, verbose self

2007-11-24 Thread Ton van Vliet
On Fri, 23 Nov 2007 17:16:25 -0800, "Patrick Mullen"
<[EMAIL PROTECTED]> wrote:

>Most of the time self doesn't bother me in the slightest.  The one
>time it does bother me however, is when I am turning a function into a
>method.  In this case, often I have many local variables which I
>actually want to be instance variables, so I have to add self to all
>of them.  Of course, this is going to cause me some grief no matter
>which language I am using.  If it was enough trouble, it wouldn't be
>hard to make a filter that converts my code automatically.

Just bringing up something I sometimes miss from good-old Turbo-Pascal
here, which has the WITH statement to reduce the typing overhead with
(long) record/struct prefixes, used like:

with  do begin
a = ...
b = ...
end;

where all variables would be automatically prefixed with the 
(if present in the already available record definition!)

In your case where you have something like:

def somefunction(...):
a = ...
b = ...
...
c = a + b

could (simply :-) become something like:

def somefunction(self, ...):
using self:
a = ...
b = ...
...
c = a + b

so only one line extra and an indentation shift.

Of course the interpreter would have to be (much) smarter here, since
the  (in this case just a simple 'self', but it could be more
complex, e.g. some module.levels.deep) is not necessarily defined in
advance.

Since 'with' is already in use, another keyword (e.g. 'using') would
be needed

I guess speed would also be a major issue, but readibility would gain
from it (IHMO :-)

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


GREAT VIDEO! >> http://www.tbn.org/films/videos/To_Hell_And_Back.ram << Just click link to view the free video.......... May 22, 2005 7:37:11 pm

2007-11-24 Thread Cris Villavicencio

--
Sent from my T-Mobile Sidekick®
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: md5 wrongness?

2007-11-24 Thread Ayaz Ahmed Khan
John Machin wrote:
> On Nov 24, 1:34 pm, Ron Johnson <[EMAIL PROTECTED]> wrote:
>> Why do Python's md5 and GNU md5sum produce differing results?
> 
> They don't differ. Try feeding them the same input:
> 
 import md5
 md5.new('snagglefrob').hexdigest()
> '9eb2459fcdd9f9b8a9fef7348bcac933'
 md5.new('snagglefrob\n').hexdigest()
> 'f842244d79af85b457811091319d85ff'


Or, alternatively:

$ echo -n snagglefrob | md5sum
9eb2459fcdd9f9b8a9fef7348bcac933  -

-- 
Ayaz Ahmed Khan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Catching a segfault in a Python library

2007-11-24 Thread Ayaz Ahmed Khan
Donn Ingle wrote:
> Already done, the code within PIL is causing the crash. It gets ugly and
> out of my remit. It's a freetype/Pil thing and I simply want to a way to
> catch it when it happens.
>  Since a segfault ends the process, I am asking about "wrappers" around
>  code
> to catch a segfault.
> 
> \d

Wouldn't it be better to narrow down to what in your code is invoking PIL 
in a manner in which PIL exhibits such behaviour, and handle it within 
your code?

Just a thought!

-- 
Ayaz Ahmed Khan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the annoying, verbose self

2007-11-24 Thread Marc 'BlackJack' Rintsch
On Sat, 24 Nov 2007 09:12:34 +0100, Ton van Vliet wrote:

> Just bringing up something I sometimes miss from good-old Turbo-Pascal
> here, which has the WITH statement to reduce the typing overhead with
> (long) record/struct prefixes, used like:
> 
> with  do begin
> a = ...
> b = ...
> end;
> 
> where all variables would be automatically prefixed with the 
> (if present in the already available record definition!)

And here lies the problem:  The compiler decides at compile time which
names are local to a function and there is no equivalent of a record
definition to make that decision.

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


Re: Catching a segfault in a Python library

2007-11-24 Thread Patrick Mullen
On 24 Nov 2007 08:41:24 GMT, Ayaz Ahmed Khan <[EMAIL PROTECTED]> wrote:
> Donn Ingle wrote:
> > Already done, the code within PIL is causing the crash. It gets ugly and
> > out of my remit. It's a freetype/Pil thing and I simply want to a way to
> > catch it when it happens.
> >  Since a segfault ends the process, I am asking about "wrappers" around
> >  code
> > to catch a segfault.
> >
> > \d
>
> Wouldn't it be better to narrow down to what in your code is invoking PIL
> in a manner in which PIL exhibits such behaviour, and handle it within
> your code?
>
> Just a thought!
>
> --
> Ayaz Ahmed Khan
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I think the idea is, certain fonts in his collection may be corrupt,
and he wants to just scan through and load them, ignoring the ones
that make the program crash.  The bug in this case lies with a third
party and isn't something he can easily fix (although he can file
reports to the third party (PIL)).

This has come up for me as well with loading meshes in a 3d
application.  The user or someone may include a corrupt file, and it's
not nice for the application to just crash when that happens, asking
them if they want to debug it.  I haven't really found a solution,
just have tried to prevent corrupted files in the system for now.  Let
me know if you get this solved :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the annoying, verbose self

2007-11-24 Thread Patrick Mullen
Ton Van Vliet:
> [... using/with ...]

This looks like a really nice little construct, and solves my small
quirk issue (which has popped up maybe twice in my python experience).
 It could also be a solution to the OP's problem.  The issue of course
is disambiguation.  Is EVERY name looked up in the tagged object, or
only assignments?  Does it copy attributes over and make everything
local and then copy it back at the end (like you would kind of expect
in a function)?  Or is everything routed to the parent.  It gets very
sticky very fast, but since it's in a block, the issues aren't as evil
as when they show up if you try to get rid of self altogether.  It
also solves more than just the self issue, allowing you to more
quickly assign multiple attributes of an object as well.

In your example:

def somefunction(self, ...):
   using self:
   a = ...
   b = ...
   ...
   c = a + b

The biggest issue is, what if you need to do more:

def somefunction(self, ...):
   using self:
   a = ...
   b = ...
   c = a + int(b)

What is int?  Is it self.int?  I suppose the simplest solution is to:
always assign as an attribute, and lookup as an attribute first going
to normal lookup rules after that.

As far as using [xxx], I don't think the interpreter needs to deal
with [xxx] until it actually executes the line.  [xxx] would just be a
normal python expression, the result of which is used for the
attribute lookup and assignments within the block.

This would somewhat solve the toy vector example:

def abs(self):
   using self:
  return math.sqrt(x*x + y*y + z*z)

Unfortunately the performance would be slightly worse do to the extra
lookup rules (self.math? no, self.__getattr__(math)? no,
self.parentclass.__getattr__(math)? no, etc)

The OP will probably say, "why not build the [using self] block into a
method definition":

def abs():
   return math.sqrt(x*x .

But this would cause problems and slowdown for pretty much all python
code in existence, and reopens the whole discussion of why removing
self is bad :)

Anyway, I'm for the "using" construct, as unlikely as it is.  It's not
necessary, but something similar could help alleviate a bit of the
tedium that is perceived by some python users, without affecting
anyone else.

Of course, this is all theory, and it's unknown (by me) if the python
compiler can even handle such a thing.  Even if it could, it's
probably not an easy task.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the annoying, verbose self

2007-11-24 Thread samwyse
On Nov 23, 7:16 pm, "Patrick Mullen" <[EMAIL PROTECTED]> wrote:
> Most of the time self doesn't bother me in the slightest.  The one
> time it does bother me however, is when I am turning a function into a
> method.  In this case, often I have many local variables which I
> actually want to be instance variables, so I have to add self to all
> of them.  Of course, this is going to cause me some grief no matter
> which language I am using.  If it was enough trouble, it wouldn't be
> hard to make a filter that converts my code automatically.

I've had the same thought, along with another.  You see, on of my pet
peeves about all OO languages that that when creating new code, I
generally begin by writing something like this:

cat = 'felix'
dog = 'rover'
def example():
  global cat, dog  # not always required, but frequently needed
  return ', '.join((cat, dog))

Later, I inevitably decide to encapsulate it inside a class, which
means lots of source changes to change my function into a method:

class my_pets:
  cat = 'felix'
  dog = 'rover'
  def example(self):
return ', '.join((self.cat, self.dog))

My idea is to introduce syntax that treats top-level functions as
methods of a global, anonymous, class:

cat = 'felix'
dog = 'rover'
def example():
  return ', '.join((.cat, .dog))

(btw, we'll also stop auto-magically recognizing global variables
inside functions, forcing the use of the '.' in global names.)  Thus,
to convert the above into a class, we just need to add one line and
alter the indentation:

class my_pets:
  cat = 'felix'
  dog = 'rover'
  def example():  # anonymous 'self' is implicit inside class defs.
return ', '.join((.cat, .dog))

You'd need someway to refer to higher lexical levels, possibly by
chaining periods; this would break ellipsis but that could be replaced
by a keyword such as 'to'.  And you'd probably want to keep 'global',
but as a keyword for those rare cases where you need to force a
reference to the outer-most lexical level:

warming = 42
def generator(factiod):
def decorator(func):
def wrapper(*args, **kwds):
if ..factoid == None:
  return .func(*args, **kwds)
else:
  return ..factoid + global.warming
return wrapper
return decorator


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


Re: the annoying, verbose self

2007-11-24 Thread Marc 'BlackJack' Rintsch
On Sat, 24 Nov 2007 01:55:38 -0800, samwyse wrote:

> I've had the same thought, along with another.  You see, on of my pet
> peeves about all OO languages that that when creating new code, I
> generally begin by writing something like this:
> 
> cat = 'felix'
> dog = 'rover'
> def example():
>   global cat, dog  # not always required, but frequently needed
>   return ', '.join((cat, dog))

Ouch that's bad design IMHO.  The need to use ``global`` is a design
smell, if needed *frequently* it starts to stink.

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


see the jop

2007-11-24 Thread roja
see the jop
http://earnmac.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


setting attributes on external types (was Re: eof)

2007-11-24 Thread samwyse
On Nov 23, 2:06 am, greg <[EMAIL PROTECTED]> wrote:
> There's a fair amount of overhead associated with providing
> the ability to set arbitrary attributes on an object, which
> is almost never wanted for built-in types, so it's not
> provided by default.
>
> You can easily get it if you want it by defining a Python
> subclass of the type concerned.

Speaking of which, I've got a big file:

>>> input = open('LoTR.iso')

I'd like to get the md5 hash of the file:

>>> import md5
>>> m = md5.new()

I've also got this nifty standard module which will allow me, among
other things, to copy an arbitrary file:

>>> import shutil

I'd like to say copy the file to my object, but it doesn't quite work:

>>> shutil.copyfileobj(input, m)

Traceback (most recent call last):
  File "", line 1, in 
shutil.copyfileobj(source, m)
  File "C:\Python25\lib\shutil.py", line 24, in copyfileobj
fdst.write(buf)
AttributeError: '_hashlib.HASH' object has no attribute 'write'

No problem, I'll just add an attribute:

>>> setattr(m, 'write', m.update)
Traceback (most recent call last):
  File "", line 1, in 
setattr(m, 'write', m.update)
AttributeError: '_hashlib.HASH' object has no attribute 'write'

Anyone have an example of how to efficiently do this?  Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


see the jop

2007-11-24 Thread roja
see the jop
http://earnmac.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Custom Tkinter scrollbar

2007-11-24 Thread Hunter . lennon
I ended up giving up on doing the scrollbar as a separate class, which
is probably for the best.  This is the pertinent code for what I
wanted, and it does what I need it to do.  I found the canvas-moving-w-
mouse.py program helpful in getting started with this; some of the
code is directly from that example.  I'm posting my code on the off
chance someone will find something of use in it.

#Constants for the scrollbar icon range.
self.UPPER_LIMIT = 160
self.LOWER_LIMIT = 334

#Prepare pictures
self.upArrow = tk.PhotoImage ("upArrow", file =
"upArrow.gif")
self.scrollIcon = tk.PhotoImage ("scrollIcon", file =
"scrollIcon.gif")
self.downArrow = tk.PhotoImage ("downArrow", file =
"downArrow.gif")



#Main Canvas
self.shell = tk.Canvas (parent, width = 388, height = 408,
borderwidth = - 2)
self.shell.create_image (0, 0, image = self.shell_image,
  anchor = tk.NW)
self.shell.create_image (361, 9, image = self.exit_image,
  anchor = tk.NW, tags = "Exit")

#Inner frame that contains a canvas.
#This is what is scrolled by the scrollbar.
self.masteryFrame = tk.Frame (parent, borderwidth = -2)
self.masteryCanvas = tk.Canvas (self.masteryFrame, width =
326, height = 218,
scrollregion =
(0,0,326,439), borderwidth = -2)
self.masteryCanvas.pack()
self.masteryFrame.pack()
self.masteryCanvas.create_image (0, 0, image =
"masteryFrame", anchor = tk.NW)
self.shell.create_window (22, 135, window =
self.masteryFrame,
  width = 326, height = 218,
anchor = tk.NW)


#Scrollbar
self.shell.create_image (350, 136, image = self.upArrow,
anchor = tk.NW,
 tags = "upArrow")
self.shell.create_image (357, 160, image =
self.scrollIcon, tags = "scroll")
self.shell.create_image (350, 343, image = self.downArrow,
anchor = tk.NW,
 tags = "downArrow")

self.shell.tag_bind ("scroll", "",
self.mouseDown)
self.shell.tag_bind ("scroll", "",
self.mouseMove)
self.shell.tag_bind ("upArrow", "",
self.stepUp)
self.shell.tag_bind ("downArrow", "",
self.stepDown)
self.shell.tag_bind ("Exit", "", self.close)

self.shell.pack (side = tk.LEFT)



def mouseDown (self, event):
#Scrollbar Function
#Remember where the mouse went down"
self.lastx = event.x
self.lasty = event.y

def mouseMove (self, event):
#Scrollbar Function
#Whatever the mouse is over is automatically tagged as
current by tk.
#Only moves vertically.
#Keep the cursor in bounds:
if event.y >= self.UPPER_LIMIT and event.y <=
self.LOWER_LIMIT:
self.shell.move(tk.CURRENT, 0, event.y - self.lasty)
self.lasty = event.y
elif event.y < self.UPPER_LIMIT:
self.shell.coords(tk.CURRENT, 357, self.UPPER_LIMIT )
self.lasty = event.y
elif event.y > 334:
self.shell.coords(tk.CURRENT, 357, self.LOWER_LIMIT)
self.lasty = event.y
self.masteryCanvas.yview_moveto (self.scrollbarPosition())


def stepUp (self, event):
#Scrollbar Function
#Move up one row or to the top, whichever is less.
#43.5 causes the canvas to move one row.
next = self.shell.coords ("scroll")[1] - 43.5
if next >= self.UPPER_LIMIT:
self.shell.coords ("scroll", 357, next)
else:
self.shell.coords ("scroll", 357, self.UPPER_LIMIT)
self.masteryCanvas.yview_moveto (self.scrollbarPosition())

def stepDown (self, event):
#Scrollbar Function
#Move down one row or to the bottom, whichever is less.
#43.5 causes the canvas to move one row.
next = self.shell.coords ("scroll")[1] + 43.5
if next <= self.LOWER_LIMIT:
self.shell.coords( "scroll", 357, next)
else:
self.shell.coords( "scroll", 357, self.LOWER_LIMIT)
self.masteryCanvas.yview_moveto (self.scrollbarPosition())


def scrollbarPosition (self):
#Scrollbar Function that computes movement
#Return a value between 0 and .5 for moving the canvas.
yCoord = self.shell.coords ("scroll")[1]
length = self.LOWER_LIMIT - self.UPPER_LIMIT
current = yCoord - self.UPPER_LIMIT
current /= 2
#print current / length
return (current / length)
-- 
http://mail.python.org/mailman/l

How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Licheng Fang
I mean, all the class instances that equal to each other should be
reduced into only one instance, which means for instances of this
class there's no difference between a is b and a==b.

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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Licheng Fang
I find myself frequently in need of classes like this for two reasons.
First, it's efficient in memory. Second, when two instances are
compared for equality only their pointers are compared. (I think
that's how Python compares 'str's.

On Nov 24, 6:31 pm, Licheng Fang <[EMAIL PROTECTED]> wrote:
> I mean, all the class instances that equal to each other should be
> reduced into only one instance, which means for instances of this
> class there's no difference between a is b and a==b.
>
> Thank you.

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


Re: Code Management

2007-11-24 Thread BlueBird
On Nov 21, 7:05 am, "Sergio Correia" <[EMAIL PROTECTED]> wrote:
> As a side note, I find much easier to drop a PTH file than messing
> with pythonpath. If you are not familiar with PTH files, what I do is
> this
>
> 1) Go to "C:\Program Files\Python25\Lib\site-packages" or whatever is
> appropiate in your case.
> 2) Create a text file, name it something like "MyProjects.PTH" (note
> the extension!)
> 3) in the file, just write the path of the folder that contains all
> your projects (in my case, C:/docs/python)
>
> The idea is to keep the main python installation separated from the
> modules you are currently developing. Your python installation goes to
> "program files/python" or "bin/python", and your personal projects go
> somewhere else (usually inside your 'user' folder). This smooths many
> things, like working with different versions of a package you are
> developing.
>

Hi,

If I understand you correctly, you have the following directory
organisation
[Python installation]/site-packages/[MyProject.pth pointing to /home/
user/python-dev/]

And then you do your development in python-dev. But how do you manage
multiple development branches of the same  program ?

My directory structure looks like this:
python-dev:
+  prog-branch1/
|  + foo
|  |  + foo.py
|  |  + tests
|  |+ test_foo.py
|  + bar
|+ bar.py
|+ tests
|  + test_bar.py
+  prog-branch2/
   + foo
   |  + foo.py
   |  + tests
   |+ test_foo.py
   + bar
 + bar.py
 + tests
   + test_bar.py

bar/bar.py needs to import symbols from foo/foo.py . And bar/tests/
test_bar.py needs some symbols from both bar/bar.py and foo/foo.py

I don't understand how having python-dev in the .pth file solves the
problem.

In my case, I make all my imports relative to the root of my project:
bar/bar.py:
from foo.foo import some_foo

bar/tests/test_bar.py
from foo.foo import some_foo
from foo.bar import some_bar

The way I managed to make it work is by extending sys.path but I would
be happy to find a better solution:

bar/bar.py is actually:
import os, sys
sys.path.append( '..')

from foo.foo import some_foo

and bar/tests/test_bar.py is actually:
import os, sys
sys.path.append( os.path.join('..','..') )

from foo.foo import some_foo
from foo.foo import some_bar

What is not nice with this method is that every runnable test script
must extend unconditionally the sys.path . That creates some clutter.

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


Re: the annoying, verbose self

2007-11-24 Thread samwyse
On Nov 24, 4:07 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Sat, 24 Nov 2007 01:55:38 -0800, samwyse wrote:
> > I've had the same thought, along with another.  You see, on of my pet
> > peeves about all OO languages that that when creating new code, I
> > generally begin by writing something like this:
>
> > cat = 'felix'
> > dog = 'rover'
> > def example():
> >   global cat, dog  # not always required, but frequently needed
> >   return ', '.join((cat, dog))
>
> Ouch that's bad design IMHO.  The need to use ``global`` is a design
> smell, if needed *frequently* it starts to stink.

I'm not sure what you mean.  In the example that I gave, the 'global'
statement isn't needed.  However, here's a different example:

>>> top_score = 0
>>> def leaderboard(my_score):
  if my_score > top_score:
print "A new high score!"
top_score = myscore
  print "Top score:", top_score

Traceback (most recent call last):
  File "", line 1, in 
leaderboard(7, 'samwyse')
  File "", line 2, in leaderboard
if my_score > top_score:
UnboundLocalError: local variable 'top_score' referenced before
assignment
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Bjoern Schliessmann
Licheng Fang wrote:
> I mean, all the class instances that equal to each other should be
> reduced into only one instance, which means for instances of this
> class there's no difference between a is b and a==b.

If you only want that if "a == b" is True also "a is b" is True,
overload the is_ attribute of your class. Personally, I don't see
any advantage in this.

Regards,


Björn

-- 
BOFH excuse #352:

The cables are not the same length.

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


Re: the annoying, verbose self

2007-11-24 Thread jakub silar
BJörn Lindqvist wrote:
> On Nov 22, 2007 2:08 PM, Colin J. Williams <[EMAIL PROTECTED]> wrote:
> 
>>[EMAIL PROTECTED] wrote:
>>
>>>Alexy:
>>>
Sometimes I
avoid OO just not to deal with its verbosity.  In fact, I try to use
Ruby anywhere speed is not crucial especially for @ prefix is better-
looking than self.
>>>
>>>Ruby speed will increase, don't worry, as more people will use it.
>>>
>>>Bye,
>>>bearophile
>>
>>I don't see this as a big deal, but
> 
> 
> The big deal is that "self." occupies important horizontal screen real
> estate. That is, it is usually not self in itself that is problematic,
> but the overflowing lines is. Take this silly vector class for
> example:
> 
> 1class Vector:
> 2def __init__(self, x, y, z):
> 3self.x = x
> 4self.y = y
> 5self.z = z
> 6def abs(self):
> 7return math.sqrt(self.x * self.x + self.y * self.y +
> self.z * self.z)
> 
> Line 7 is 82 characters long which is, if you care about the
> readability of your code, a very real problem because the line is to
> long. So you break it:
> 
> 7return math.sqrt(self.x * self.x +
>   self.y * self.y +
>   self.z * self.z)
> 
> Better, but definitely not as nice as it would have been with a
> shorter self prefix like you propose. And in my eyes, having to break
> lines like this instantly makes the code much less attractive. There
> is probably not a single language in existance in which wrapping lines
> doesn't make the code uglier.
> 
> I also notice that the lines in your mail are nicely broken at about
> column 45, probably because you are typing on a PDA or similar? In
> those situations, where you require much shorter lines than the
> standard 78 characters, for example when typesetting code examples for
> a book, the problem with line breaks is even worse.
> 
> 
>>suppose that the syntax were
>>expanded so that, in a method, a dot
>>".", as a precursor to an identifier,
>>was treated as "self." is currently treated?
> 
> 
> I like that a lot. This saves 12 characters for the original example
> and removes the need to wrap it.
> 
> 7return math.sqrt(.x * .x + .y * .y + .z * .z)
> 
> +1 Readability counts, even on small screens.
> 
> 
Below is my coding standard - I'm lazy, even lazy to persuade 
comutinties into strange (imho) language syntax extensions.


 class Vector:
 def __init__(s, x, y, z):
 s.x = x
 s.y = y
 s.z = z
 def abs(s):
 return math.sqrt(s.x * s.x + s.y * s.y + s.z * s.z)

Admit that changing habits may be more difficult then to change a 
language syntax.

Jakub

occasional lamerish Python user
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Bjoern Schliessmann
Licheng Fang wrote:
> I find myself frequently in need of classes like this for two
> reasons. First, it's efficient in memory. 

Are you using millions of objects, or MB size objects? Otherwise,
this is no argument.

BTW, what happens if you, by some operation, make a == b, and
afterwards change b so another object instance must be created?
This instance management is quite a runtime overhead.

> Second, when two instances are compared for equality only their
> pointers are compared. 

I state that the object management will often eat more performance
than equality testing. Except you have a huge number of equal
objects. If the latter was the case you should rethink your program
design.

> (I think that's how Python compares 'str's. 

Generally not. In CPython, just very short strings are created only
once.

>>> a=" "
>>> b=" "
>>> a is b
True
>>> a="  "
>>> b="  "
>>> a is b
False
 
Regards,


Björn

-- 
BOFH excuse #430:

Mouse has out-of-cheese-error

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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Licheng Fang
On Nov 24, 7:05 pm, Bjoern Schliessmann  wrote:
> Licheng Fang wrote:
> > I find myself frequently in need of classes like this for two
> > reasons. First, it's efficient in memory.
>
> Are you using millions of objects, or MB size objects? Otherwise,
> this is no argument.

Yes, millions. In my natural language processing tasks, I almost
always need to define patterns, identify their occurrences in a huge
data, and count them. Say, I have a big text file, consisting of
millions of words, and I want to count the frequency of trigrams:

trigrams([1,2,3,4,5]) == [(1,2,3),(2,3,4),(3,4,5)]

I can save the counts in a dict D1. Later, I may want to recount the
trigrams, with some minor modifications, say, doing it on every other
line of the input file, and the counts are saved in dict D2. Problem
is, D1 and D2 have almost the same set of keys (trigrams of the text),
yet the keys in D2 are new instances, even though these keys probably
have already been inserted into D1. So I end up with unnecessary
duplicates of keys. And this can be a great waste of memory with huge
input data.

>
> BTW, what happens if you, by some operation, make a == b, and
> afterwards change b so another object instance must be created?
> This instance management is quite a runtime overhead.
>

I probably need this class to be immutable.

> > Second, when two instances are compared for equality only their
> > pointers are compared.
>
> I state that the object management will often eat more performance
> than equality testing. Except you have a huge number of equal
> objects. If the latter was the case you should rethink your program
> design.
>

Yeah, counting is all about equal or not.

> > (I think that's how Python compares 'str's.
>
> Generally not. In CPython, just very short strings are created only
> once.
>
> >>> a=" "
> >>> b=" "
> >>> a is b
> True
> >>> a="  "
> >>> b="  "
> >>> a is b
>

Wow, I didn't know this. But exactly how Python manage these strings?
My interpretator gave me such results:

>>> a = 'this'
>>> b = 'this'
>>> a is b
True
>>> a = 'this is confusing'
>>> b = 'this is confusing'
>>> a is b
False


> False
>
> Regards,
>
> Björn
>
> --
> BOFH excuse #430:
>
> Mouse has out-of-cheese-error

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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Bjoern Schliessmann
Licheng Fang wrote:
> On Nov 24, 7:05 pm, Bjoern Schliessmann > BTW, what happens if you, by some operation, make a == b, and
>> afterwards change b so another object instance must be created?
>> This instance management is quite a runtime overhead.
> 
> I probably need this class to be immutable.

IMHO you don't need a change of Python, but simply a special
implementation (probably using metaclasses/singletons).
 
> Wow, I didn't know this. But exactly how Python manage these
> strings? 

I don't know (use the source, Luke). :) Or perhaps there is a Python
Elder here that knows?

Regards,


Björn

-- 
BOFH excuse #165:

Backbone Scoliosis

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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread samwyse
On Nov 24, 5:44 am, Licheng Fang <[EMAIL PROTECTED]> wrote:
> Yes, millions. In my natural language processing tasks, I almost
> always need to define patterns, identify their occurrences in a huge
> data, and count them. [...] So I end up with unnecessary
> duplicates of keys. And this can be a great waste of memory with huge
> input data.

create a hash that maps your keys to themselves, then use the values
of that hash as your keys.

>>> store = {}
>>> def atom(str):
global store
if str not in store:
store[str] = str
return store[str]

>>> a='this is confusing'
>>> b='this is confusing'
>>> a == b
True
>>> a is b
False
>>> atom(a) is atom(b)
True
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the annoying, verbose self

2007-11-24 Thread Ton van Vliet
On 24 Nov 2007 08:48:30 GMT, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]>
wrote:

>On Sat, 24 Nov 2007 09:12:34 +0100, Ton van Vliet wrote:
>
>> Just bringing up something I sometimes miss from good-old Turbo-Pascal
>> here, which has the WITH statement to reduce the typing overhead with
>> (long) record/struct prefixes, used like:
>> 
>> with  do begin
>> a = ...
>> b = ...
>> end;
>> 
>> where all variables would be automatically prefixed with the 
>> (if present in the already available record definition!)
>
>And here lies the problem:  The compiler decides at compile time which
>names are local to a function and there is no equivalent of a record
>definition to make that decision.
>

The whole clause following 'using self:' could be seen as the record
definition: all attribute assignments/bindings in there should be
prefixed with 'self' (if not already existing in the self namespace?)
and thereby become instance attributes, which outside the 'using self'
clause still need 'self.' as the (namespace?) prefix.

It would not bring an 'implicit' self, but could possibly improve
readability in some areas (btw, personally, I don't have anything
against the 'explicit' self in general)

Even in Pascal the 'with' statement was not meant to improve speed,
but merely for readability, especially with complex records.

Please, bear with me, I am relatively new to Python (reading books,
scanning newsgroups, etc) and feel in no way capable of giving
'educated' or 'well overthought' advise, just my 2 cents ;-)

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


Re: How do I convert escaped HTML into a string?

2007-11-24 Thread leej
On 24 Nov, 05:42, "Just Another Victim of the Ambient Morality"
<[EMAIL PROTECTED]> wrote:

> I did find some people who complained about this and rolled their own
> solution but I refuse to believe that Python doesn't have a built-in
> solution to what must be a very common problem.



Replace "python" with "c++" and would that seem a reasonable belief?
(That said I'm a PyN00b)

Anyways, for all my HTML processing needs my first port of call has
been BeautifulSoup e.g.

soup = BeautifulSoup(html, convertEntities="html")
print soup.findAll(text=True)

Should be in the ballpark of what you want.

http://www.crummy.com/software/BeautifulSoup/documentation.html for
docs.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: md5 wrongness?

2007-11-24 Thread Ron Johnson
On 11/24/07 02:27, Ayaz Ahmed Khan wrote:
> John Machin wrote:
>> On Nov 24, 1:34 pm, Ron Johnson <[EMAIL PROTECTED]> wrote:
>>> Why do Python's md5 and GNU md5sum produce differing results?
>> They don't differ. Try feeding them the same input:
>>
> import md5
> md5.new('snagglefrob').hexdigest()
>> '9eb2459fcdd9f9b8a9fef7348bcac933'
> md5.new('snagglefrob\n').hexdigest()
>> 'f842244d79af85b457811091319d85ff'
> 
> Or, alternatively:
> 
> $ echo -n snagglefrob | md5sum
> 9eb2459fcdd9f9b8a9fef7348bcac933  -

Thanks to all.  I knew there had to be operator error, but I
couldn't figure out where...

-- 
Ron Johnson, Jr.
Jefferson LA  USA

%SYSTEM-F-FISH, my hovercraft is full of eels
-- 
http://mail.python.org/mailman/listinfo/python-list


Installing modules via setuptools in a script

2007-11-24 Thread Thorsten Kampe
Date: Fri, 23 Nov 2007 19:35:21 -
Message-ID: <[EMAIL PROTECTED]>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
User-Agent: MicroPlanet-Gravity/2.70.2067

Hi,

can anyone give me a short code snippet how to install a missing 
module via setuptools (assuming setuptools is already installed)?!

Something like this:

try:
import missing_module
except import_error
import setuptools
setuptools.whatever.install(missing_module)


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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Marc 'BlackJack' Rintsch
On Sat, 24 Nov 2007 13:40:40 +0100, Bjoern Schliessmann wrote:

> Licheng Fang wrote:
>> On Nov 24, 7:05 pm, Bjoern Schliessmann  
>> Wow, I didn't know this. But exactly how Python manage these
>> strings? 
> 
> I don't know (use the source, Luke). :) Or perhaps there is a Python
> Elder here that knows?

AFAIK strings of length 1 and strings that would be valid Python
identifiers are treated this way.

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


Re: the annoying, verbose self

2007-11-24 Thread Colin J. Williams
Kay Schluehr wrote:
> Colin J. Williams schrieb:
>> Kay Schluehr wrote:
>>> On Nov 22, 8:43 pm, Bruno Desthuilliers
>>> <[EMAIL PROTECTED]> wrote:
 Colin J. Williams a écrit :



> [EMAIL PROTECTED] wrote:
>> Alexy:
>>> Sometimes I
>>> avoid OO just not to deal with its verbosity.  In fact, I try to 
>>> use
>>> Ruby anywhere speed is not crucial especially for @ prefix is 
>>> better-
>>> looking than self.
>> Ruby speed will increase, don't worry, as more people will use it.
>> Bye,
>> bearophile
> I don't see this as a big deal, but suppose that the syntax were
> expanded so that, in a method, a dot ".", as a precursor to an 
> identifier,
> was treated as "self." is currently treated?
 

 Python's "methods" are thin wrapper around functions, created at 
 lookup
 time (by the __get__ method of the function type). What you define 
 in a
 class statement are plain functions, period. So there's just no way to
 do what you're suggesting.

 
>>>
>>> The object model is irrelevant here. The substitution is purely
>>> syntactical and gets resolved at compile time:
>>>
>>> def foo(first, ...):
>>> .bar = ...
>>>
>>> is always equivalent with:
>>>
>>> def foo(first, ...):
>>> first.bar = ...
>>>
>>> and generates the same bytecode.
>>>
>>> Whether this is helpfull, beautifull or necessary is another issue.
>>>
>>> Kay
>>
>> I had never thought of trying the above, which is, essentially what I 
>> was
>> suggesting, as the syntax specifies:
>>
>> primary ::=
>>  atom | attributeref
>>   | subscription | slicing | call
>>
>> attributeref ::=
>>  primary "." identifier
>>
>> I did try it and get:
>>
>> # tmp.py
>> class Z():
>>   def __init__(self):
>> a= 1
>> self.b= 2
>> #.c= 3 Marked as a syntax error by PyScripter
>>
>> def y(self):
>>   self.a= 4
>>   #.b= 5   Marked as a syntax error by PyScripter
>>
>> It seems that some, probably simple, change in the parsing is needed.
>>
>> Colin W.
>
> Sure. Since you cite the grammar let me say that I find it somewhat
> confusing that the grammar in the Python documentation doesn't
> correspond to the grammar used by the CPython parser. For the following
> I will use the notations of the Grammar file used by the CPython parser.
>
> In order to adapt the syntax take a look at
>
> atom: ('(' [yield_expr|testlist_gexp] ')' |
>   '[' [listmaker] ']' |
>   '{' [dictmaker] '}' |
>   '`' testlist1 '`' |
>   NAME | NUMBER | STRING+)
>
> The last row must be changed to
>
>   ['.'] NAME | NUMBER | STRING+)
>   ~
>
> But then you run into conflict with the definition of the ellipsis in 
> rule
>
> subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
>
> because you need two token of lookahead. Pythons LL(1) parser might not
> manage this.
>
> This problem vanishes with Python 3.0 which defines an own ellipsis 
> literal:
>
> atom: ('(' [yield_expr|testlist_comp] ')' |
>   '[' [testlist_comp] ']' |
>   '{' [dictorsetmaker] '}' |
>   NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
>
> Kay
>
Many thanks for this clarification.  I hope that those beavering away on
Python 3000 will give further consideration to this matter.

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


Re: the annoying, verbose self

2007-11-24 Thread Marc 'BlackJack' Rintsch
On Sat, 24 Nov 2007 02:54:27 -0800, samwyse wrote:

> On Nov 24, 4:07 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
>> On Sat, 24 Nov 2007 01:55:38 -0800, samwyse wrote:
>> > I've had the same thought, along with another.  You see, on of my pet
>> > peeves about all OO languages that that when creating new code, I
>> > generally begin by writing something like this:
>>
>> > cat = 'felix'
>> > dog = 'rover'
>> > def example():
>> >   global cat, dog  # not always required, but frequently needed
>> >   return ', '.join((cat, dog))
>>
>> Ouch that's bad design IMHO.  The need to use ``global`` is a design
>> smell, if needed *frequently* it starts to stink.
> 
> I'm not sure what you mean.  In the example that I gave, the 'global'
> statement isn't needed.  However, here's a different example:

I mean that global names that are (re)bound from within functions couple
these functions in a non-obvious way and make the code and data flow harder
to follow and understand.  Also it makes refactoring and testing more
difficult because of the dependencies.

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


Re: the annoying, verbose self

2007-11-24 Thread Marc 'BlackJack' Rintsch
On Sat, 24 Nov 2007 14:09:04 +0100, Ton van Vliet wrote:

> On 24 Nov 2007 08:48:30 GMT, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]>
> wrote:
> 
>>On Sat, 24 Nov 2007 09:12:34 +0100, Ton van Vliet wrote:
>>
>>> Just bringing up something I sometimes miss from good-old Turbo-Pascal
>>> here, which has the WITH statement to reduce the typing overhead with
>>> (long) record/struct prefixes, used like:
>>> 
>>> with  do begin
>>> a = ...
>>> b = ...
>>> end;
>>> 
>>> where all variables would be automatically prefixed with the 
>>> (if present in the already available record definition!)
>>
>>And here lies the problem:  The compiler decides at compile time which
>>names are local to a function and there is no equivalent of a record
>>definition to make that decision.
> 
> The whole clause following 'using self:' could be seen as the record
> definition: all attribute assignments/bindings in there should be
> prefixed with 'self' (if not already existing in the self namespace?)
> and thereby become instance attributes, which outside the 'using self'
> clause still need 'self.' as the (namespace?) prefix.

So::

def meth(self):
using self:
tmp = raw_input('Enter age: ')
age = int(tmp)

becomes::

def meth(self):
using self:
self.tmp = self.raw_input('Enter age: ')
self.age = self.int(tmp)

Binding `tmp` unnecessarily to the object and trying to get `raw_input()`
and `int()` from the object.  Ouch.  :-)

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


Re: Catching a segfault in a Python library

2007-11-24 Thread Donn Ingle
> I think the idea is, certain fonts in his collection may be corrupt,
> and he wants to just scan through and load them, ignoring the ones
> that make the program crash.  
Ya got me! Sheesh, I can't hide anywhere :D

> The bug in this case lies with a third 
> party and isn't something he can easily fix (although he can file
> reports to the third party (PIL)).
I've a bad memory and can't recall what I told PIL at the time. It might
have been a case of waiting to see what new versions can do.

> not nice for the application to just crash when that happens, asking
> them if they want to debug it.  
Zigactly! You can wrap try/except around the calls that (by debugging) you
know are the culprits, but a segfault is a segfault and bam! you are at the
command line again.

> I haven't really found a solution, 
> just have tried to prevent corrupted files in the system for now.  Let
> me know if you get this solved
I'll certainly pop a note. I think, though, that the answer may reside in
the basic theme of this thread:

runapp
 result = runActualApp( )
 while True:
  if result == allokay: break
  else: 
   

Unless a segfault goes through that too, like Krypton through Superman.
\d

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

existence of a program

2007-11-24 Thread Navid Parvini
Dear All,
   
  Would you please tell me that how can I check for existence of a program (for 
example Perl) on Windows and Linux by a Python code?
   
  Thank you in advance.
  Navid

   
-
Be a better sports nut! Let your teams follow you with Yahoo Mobile. Try it now.-- 
http://mail.python.org/mailman/listinfo/python-list

Re: the annoying, verbose self

2007-11-24 Thread Ton van Vliet
On 24 Nov 2007 13:56:37 GMT, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]>
wrote:

>On Sat, 24 Nov 2007 14:09:04 +0100, Ton van Vliet wrote:
>
>> On 24 Nov 2007 08:48:30 GMT, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]>
>> wrote:
>> 
>>>On Sat, 24 Nov 2007 09:12:34 +0100, Ton van Vliet wrote:
>>>
 Just bringing up something I sometimes miss from good-old Turbo-Pascal
 here, which has the WITH statement to reduce the typing overhead with
 (long) record/struct prefixes, used like:
 
 with  do begin
 a = ...
 b = ...
 end;
 
 where all variables would be automatically prefixed with the 
 (if present in the already available record definition!)
>>>
>>>And here lies the problem:  The compiler decides at compile time which
>>>names are local to a function and there is no equivalent of a record
>>>definition to make that decision.
>> 
>> The whole clause following 'using self:' could be seen as the record
>> definition: all attribute assignments/bindings in there should be
>> prefixed with 'self' (if not already existing in the self namespace?)
>> and thereby become instance attributes, which outside the 'using self'
>> clause still need 'self.' as the (namespace?) prefix.
>
>So::
>
>def meth(self):
>using self:
>tmp = raw_input('Enter age: ')
>age = int(tmp)
>
>becomes::
>
>def meth(self):
>using self:
>self.tmp = self.raw_input('Enter age: ')
>self.age = self.int(tmp)
>
>Binding `tmp` unnecessarily to the object and trying to get `raw_input()`
>and `int()` from the object.  Ouch.  :-)

Absolutely.

However, I was more thinking in terms of attributes only, like this:

def meth(self):
using self:
self.tmp = raw_input('Enter age: ')
self.age = int(self.tmp)

and for the methods check whether there *is* a self. defined
in the class (or even within the 'using' clause), then use it, or else
leave it as it is.

It would require more 'lookup' work, but we're talking 'readability'
here (which would btw not bring much in the example you give ;-)

It would boil down to choice: explicit/speed vs implicit/readability

-- 
ci vediamo
Ton
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I convert escaped HTML into a string?

2007-11-24 Thread Stefan Behnel
[EMAIL PROTECTED] wrote:
> On 24 Nov, 05:42, "Just Another Victim of the Ambient Morality"
> <[EMAIL PROTECTED]> wrote:
> 
>> I did find some people who complained about this and rolled their own
>> solution but I refuse to believe that Python doesn't have a built-in
>> solution to what must be a very common problem.
> 
> Replace "python" with "c++" and would that seem a reasonable belief?

That's different, as Python comes with batteries included.

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


Re: the annoying, verbose self

2007-11-24 Thread Duncan Booth
Ton van Vliet <[EMAIL PROTECTED]> wrote:

> It would boil down to choice: explicit/speed vs implicit/readability

No, it would boil down to explicit+speed+readability+maintainability vs 
implicit+error prone.

It would mean that as well as the interpreter having to search the 
instance to work out whether each name referenced an attribute or a global 
the reader would also have to perform the same search. It would mean that 
adding a new attribute to an instance would change the meaning of the 
methods which is a recipe for disaster.

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


Re: the annoying, verbose self

2007-11-24 Thread samwyse
On Nov 24, 7:50 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Sat, 24 Nov 2007 02:54:27 -0800, samwyse wrote:
> > On Nov 24, 4:07 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> >> On Sat, 24 Nov 2007 01:55:38 -0800, samwyse wrote:
> >> > I've had the same thought, along with another.  You see, on of my pet
> >> > peeves about all OO languages that that when creating new code, I
> >> > generally begin by writing something like this:
>
> >> > cat = 'felix'
> >> > dog = 'rover'
> >> > def example():
> >> >   global cat, dog  # not always required, but frequently needed
> >> >   return ', '.join((cat, dog))
>
> >> Ouch that's bad design IMHO.  The need to use ``global`` is a design
> >> smell, if needed *frequently* it starts to stink.
>
> > I'm not sure what you mean.  In the example that I gave, the 'global'
> > statement isn't needed.  However, here's a different example:
>
> I mean that global names that are (re)bound from within functions couple
> these functions in a non-obvious way and make the code and data flow harder
> to follow and understand.  Also it makes refactoring and testing more
> difficult because of the dependencies.

The whole point of this sub-thread is the difficulty of turning global
vars and functions into class vars and functions, and that is
something that is usually done precisely because the code and data
flow has become harder to follow and understand.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Licheng Fang
On Nov 24, 9:42 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Sat, 24 Nov 2007 13:40:40 +0100, Bjoern Schliessmann wrote:
> > Licheng Fang wrote:
> >> On Nov 24, 7:05 pm, Bjoern Schliessmann 
> >> Wow, I didn't know this. But exactly how Python manage these
> >> strings?
>
> > I don't know (use the source, Luke). :) Or perhaps there is a Python
> > Elder here that knows?
>
> AFAIK strings of length 1 and strings that would be valid Python
> identifiers are treated this way.
>
> Ciao,
> Marc 'BlackJack' Rintsch

Thanks. Then, is there a way to make python treat all strings this
way, or any other kind of immutable objects?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the annoying, verbose self

2007-11-24 Thread samwyse
On Nov 24, 10:07 am, Duncan Booth <[EMAIL PROTECTED]>
wrote:
> Ton van Vliet <[EMAIL PROTECTED]> wrote:
>
> > It would boil down to choice: explicit/speed vs implicit/readability
>
> No, it would boil down to explicit+speed+readability+maintainability vs
> implicit+error prone.
>
> It would mean that as well as the interpreter having to search the
> instance to work out whether each name referenced an attribute or a global
> the reader would also have to perform the same search. It would mean that
> adding a new attribute to an instance would change the meaning of the
> methods which is a recipe for disaster.

Besides Pascal, Visual Basic also offers a 'with' statement that
behaves almost in this way.  That in itself should be an indication
that the whole thing is a bad idea.  ;-)

The way it works, however, is that you do have to prefix members with
a '.' and the interpreter tries to bind with each nested 'with'
variable in turn.  It's tolerable with one 'with' statment, but once
you start nesting them it becomes dangerous.

My idea in a parallel thread is to treat a '.' prefix like '../' in
file paths; each one moves you up a level in the symbol table.  In a
top-level function, it means a global name; in a class method it means
a member.  The @classmethod and @staticmethod decorators would need to
fix things so that '.' refers to the appropriate things.  There's no
reason why a 'using' statement couldn't perform nesting as well:  '.'
refers to the 'using' variable, '..' refers to what '.' previously
referred to, etc.

OTOH, the point of 'using' is to reduce typing, so you might instead
add 'as' clauses as an alternate way to reduce confusion:

>>> using myclass.new() as p:
p.do_something()
p.something_else()

Of course, now its starting to look more like a Python 'with'
statement, and I think there's a way to do basically this already.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the annoying, verbose self

2007-11-24 Thread Marc 'BlackJack' Rintsch
On Sat, 24 Nov 2007 08:27:56 -0800, samwyse wrote:

> On Nov 24, 7:50 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
>> On Sat, 24 Nov 2007 02:54:27 -0800, samwyse wrote:
>> > On Nov 24, 4:07 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
>> >> On Sat, 24 Nov 2007 01:55:38 -0800, samwyse wrote:
>> >> > I've had the same thought, along with another.  You see, on of my pet
>> >> > peeves about all OO languages that that when creating new code, I
>> >> > generally begin by writing something like this:
>>
>> >> > cat = 'felix'
>> >> > dog = 'rover'
>> >> > def example():
>> >> >   global cat, dog  # not always required, but frequently needed
>> >> >   return ', '.join((cat, dog))
>>
>> >> Ouch that's bad design IMHO.  The need to use ``global`` is a design
>> >> smell, if needed *frequently* it starts to stink.
>>
>> > I'm not sure what you mean.  In the example that I gave, the 'global'
>> > statement isn't needed.  However, here's a different example:
>>
>> I mean that global names that are (re)bound from within functions couple
>> these functions in a non-obvious way and make the code and data flow harder
>> to follow and understand.  Also it makes refactoring and testing more
>> difficult because of the dependencies.
> 
> The whole point of this sub-thread is the difficulty of turning global
> vars and functions into class vars and functions, and that is
> something that is usually done precisely because the code and data
> flow has become harder to follow and understand.

Then don't use "global variables".  If you don't put anything except
constants, classes and functions in the module's namespace there's no
problem "lifting" them into a class later.

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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Bruno Desthuilliers
Licheng Fang a écrit :
> I mean, all the class instances that equal to each other should be
> reduced into only one instance, which means for instances of this
> class there's no difference between a is b and a==b.

Here's a Q&D attempt - without any garantee, and to be taylored to your 
needs.

_values = {}#id(instance) => value mapping
_instances = {} #hash(value) => instance mapping

class Value(object):
   def __new__(cls, value):
 try:
   return _instances[hash(value)]
 except KeyError:
   instance = object.__new__(cls)
   _values[id(instance)] = value
   _instances[hash(value)] = instance
   return instance

   @apply
   def value():
 def fget(self):
   return _values[id(self)]
 def fset(self, ignore):
   raise AttributeError("%s.value is read only" % type(self))
 def fdel(self):
   raise AttributeError("%s.value is read only" % type(self))
 return property(**locals())


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


Re: mod_python

2007-11-24 Thread Bruno Desthuilliers
Vernon Wenberg III a écrit :
> Why do I receive a "File not found" error on a perfect good and simple 
> script but properly receive errors when I deliberately add errors in the 
> script? The file is there, it just doesn't do anything.
> 
> Any help would be appreciated.

Sorry, my crystal ball is out for repair.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I convert escaped HTML into a string?

2007-11-24 Thread Bruno Desthuilliers
Stefan Behnel a écrit :
> [EMAIL PROTECTED] wrote:
> 
>>On 24 Nov, 05:42, "Just Another Victim of the Ambient Morality"
>><[EMAIL PROTECTED]> wrote:
>>
>>
>>>I did find some people who complained about this and rolled their own
>>>solution but I refuse to believe that Python doesn't have a built-in
>>>solution to what must be a very common problem.
>>
>>Replace "python" with "c++" and would that seem a reasonable belief?
> 
> 
> That's different, as Python comes with batteries included.

Unfortunately, you still have to write a couple lines of code every once 
in a while !-)

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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread samwyse
On Nov 24, 10:35 am, Licheng Fang <[EMAIL PROTECTED]> wrote:
> Thanks. Then, is there a way to make python treat all strings this
> way, or any other kind of immutable objects?

The word generally used is 'atom' when referring to strings that are
set up such that 'a == b' implies 'a is b'.  This is usually an
expensive process, since you don't want to do it to strings that are,
e.g., read from a file.  Yes, it could be done only for string
constants, and some languages (starting with LISP) do this, but that
isn't what you (or most people) want.  Whether you realize it or not,
you want control over the process; in your example, you don't want to
do it for the lines read from your file, just the trigrams.

The example that I gave does exactly that.  It adds a fixed amount of
storage for each string that you 'intern' (the usual name given to the
process of generating such a string.  Let's look at my code again:

>>> store = {}
>>> def atom(str):
global store
if str not in store:
store[str] = str
return store[str]

Each string passed to 'atom' already exists.  We look to see if copy
already exists; if so we can discard the latest instance and use that
copy henceforth.  If a copy does not exist, we save the string inside
'store'.  Since the string already exists, we're just increasing its
reference count by two (so it won't be reference counted) and
increasing the size of 'store' by (an amortized) pair of pointers to
that same string.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread samwyse
On Nov 24, 5:44 am, Licheng Fang <[EMAIL PROTECTED]> wrote:
> Yes, millions. In my natural language processing tasks, I almost
> always need to define patterns, identify their occurrences in a huge
> data, and count them. Say, I have a big text file, consisting of
> millions of words, and I want to count the frequency of trigrams:
>
> trigrams([1,2,3,4,5]) == [(1,2,3),(2,3,4),(3,4,5)]

BTW, if the components of your trigrams are never larger than a byte,
then encode the tuples as integers and don't worry about pointer
comparisons.

>>> def encode(s):
return (ord(s[0])*256+ord(s[1]))*256+ord(s[2])

>>> def trigram(s):
return [ encode(s[i:i+3]) for i in range(0, len(s)-2)]

>>> trigram('abcde')
[6382179, 6447972, 6513765]
-- 
http://mail.python.org/mailman/listinfo/python-list


Disk Space Script

2007-11-24 Thread [EMAIL PROTECTED]
Hello all,

I would like to write a script in Python to email me when disk space
gets below a certain value.

My first question (I'm sure of many) is how do get this output into a
dictionary or list to index the values?

import os
os.system("df -x cifs -x iso9660 | grep -E ^/dev | awk '{ print
$1,$4 }'")

[What the output looks like]

/dev/sda3 15866012
/dev/sda4 26126712

I would like to write code that compares /dev/sda* to a number (ex.
200 -> "2GB") and sends an alert if the indexed value is below it.

I'm a noob so keep it simple.

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


Compile Cheetah Template on Windows

2007-11-24 Thread brianrpsgt1
Newbie here

I have been able to successful pull info from a MySQL DB, get the
results and output them in an HTML format using Cheetah to the screen
using IDLE.  I am doing this on a Windows Laptop, running WinXP,
Python 2.5 and the latest version of Cheetah.

I have two questions:
1. How and where do you compile Cheetah templates in Windows?  The
command in the docs is cheetah compile a, however, I believe that this
is for Linux.  This does nothing in a DOS Prompt.  Please provide the
info for this command in Windows.

2. How do I output the HTML to a file?  I tried the following:

FILE = open(filename, "wt")
FILE.writelines(output)
FILE.close()

I get an error though that states that writelines() requires an
interable argument

Thnx for any assistance!

B

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


Re: the annoying, verbose self

2007-11-24 Thread Ton van Vliet
On 24 Nov 2007 16:07:18 GMT, Duncan Booth
<[EMAIL PROTECTED]> wrote:

>Ton van Vliet <[EMAIL PROTECTED]> wrote:
>
>> It would boil down to choice: explicit/speed vs implicit/readability
>
>No, it would boil down to explicit+speed+readability+maintainability vs 
>implicit+error prone.

It would not be a full fledged *implicit*, but only used in small
areas where many self's are coming together, and possibly clutter
readability (a subjective classification anyhow) and even could
degrade (code) maintainability.

>It would mean that as well as the interpreter having to search the 
>instance to work out whether each name referenced an attribute or a global 
>the reader would also have to perform the same search.

IMHO if it is limited to small overviewable sections I think it could
make life of the reader/coder/maintainer even easier and less error
prone.

I cannot judge for the interpreter part (*if* at all doable, it would
probably be the coder's choice between speed and readability)

>It would mean that adding a new attribute to an instance would change 
>the meaning of the methods which is a recipe for disaster.

I don't see what you have in mind here?

As stated some message levels back: 'bear with me', I'm not an expert,
just a beginning user ;-)

I have no problems with the explicit self at all, but just brought up
Pascal's 'with' statement, invented to improve readability only (and
saving some typing as well) and 'transposed' it to python, just as my
2 cents.

I don't want to defend or formally propose an implementation, just
giving comments where *I* do not fully understand the counter
arguments.

So let's not 'beat it to death', ok ;-)

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


Re: OPLC purchase period extended

2007-11-24 Thread Grant Edwards
On 2007-11-24, Aahz <[EMAIL PROTECTED]> wrote:

> Apparently demand has been such that you can buy an OLPC
> through the end of the year:
>
> http://www.laptopgiving.org/en/index.php

I've ordered mine, but I still haven't gotten an answer on how
to sign up for the year of free T-Mobile WiFi access that's
included in the offer.  The T-Mobile site says you need a PIN
from your "confirmation letter".  I never I've gotten neither a
confirmation letter nor a response to the e-mail I sent to OLPC
asking about it. :/

The XO laptop comes with a built-in Python IDE, so everybody on
c.l.p ought to have one...

-- 
Grant

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


OPLC purchase period extended

2007-11-24 Thread Aahz
Apparently demand has been such that you can buy an OLPC through the end
of the year:

http://www.laptopgiving.org/en/index.php
-- 
Fortune cookie: "Focus on the color purple this week to bring you luck"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the annoying, verbose self

2007-11-24 Thread Patrick Mullen
On 24 Nov 2007 13:56:37 GMT, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:

> So::
>
> def meth(self):
> using self:
> tmp = raw_input('Enter age: ')
> age = int(tmp)
>
> becomes::
>
> def meth(self):
> using self:
> self.tmp = self.raw_input('Enter age: ')
> self.age = self.int(tmp)
>
> Binding `tmp` unnecessarily to the object and trying to get `raw_input()`
> and `int()` from the object.  Ouch.  :-)

Yes, that's no good.  So you would write it like so:

def meth(self,*args):
tmp = int(raw_input('Enter age:'))
using self:
age = tmp

Still an unnecessary lookup on tmp though :)  And it would be useless
to use it for one assignment, the idea is to eliminate all the typing
with this:

self.var1 = 5
self.var2 = "a value"
self.var3 = stuff
self.var4 = [2,54,7,7]
self.var5 = "dingaling"
self.var6 = 6.4
self.var7 = 1
self.var8 = False
self.var9 = True

Of course that "self.var3 = stuff" under the using would result in a
bad lookup for "stuff", but the programmer who wanted to use this
would have to realize this and try to avoid it.

I have been known from time to time, for long assignments such as
this, to turn a string with keys and values into a dictionary, and
then update __dict__ with that dictionary, hehe.  "var1,5;var2,"a
value";var3,stuff"  Not the best practice but the fastest to type.
Sometimes I actually use a dictionary, but typing all of the quotes
for the keys gets old.

If there were a "using" or if the with statement would handle
something like this, I wouldn't use it.  "s." is only 2 characters.  I
saw chained dots mentioned.  Chained dots are 2 characters.  Why are
we still discussing this?  "s." is the answer, or pulling the
attributes into local vars if you are going to use them many times, to
save lookup.  This is not a band-aid, this is an actual valid
programming technique.  There is more to programming than typing...



Self is never going away, most python programmers generally like or
aren't bothered by it, if you are new to the language try to get used
to it, if it's too bothersome you can use one of the hacks or try
other languages.  I don't mean to be snobby either, one language does
not fit all.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Disk Space Script

2007-11-24 Thread kyosohma
On Nov 24, 11:46 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> Hello all,
>
> I would like to write a script in Python to email me when disk space
> gets below a certain value.
>
> My first question (I'm sure of many) is how do get this output into a
> dictionary or list to index the values?
>
> import os
> os.system("df -x cifs -x iso9660 | grep -E ^/dev | awk '{ print
> $1,$4 }'")
>
> [What the output looks like]
>
> /dev/sda3 15866012
> /dev/sda4 26126712
>
> I would like to write code that compares /dev/sda* to a number (ex.
> 200 -> "2GB") and sends an alert if the indexed value is below it.
>
> I'm a noob so keep it simple.
>
> Thanks in advance.

I don't know the Unix command for this, but I would just redirect the
output to a file on your system. See the following for more info:

http://www.faqs.org/docs/diveintopython/kgp_stdio.html

You could send the output to a variable and create a file-like stream
too. Either way, read the file (or stream) and then for each line,
split it on the space.

Then you can do the compare and use the email module to email you the
result should it go over your specified amount.

By the by, if you're using Python 2.4 or above, you should switch to
using the subprocess module rather than using os.system since the
latter is being deprecated.

For more on file objects, see the docs:

http://docs.python.org/lib/bltin-file-objects.html

or there's this good article:

http://www.devshed.com/c/a/Python/File-Management-in-Python/

And the email module is explained quite well in the docs too:

http://docs.python.org/lib/module-email.html

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


Re: Refreshing GridBagSizer

2007-11-24 Thread kyosohma
On Nov 23, 12:48 pm, Steve S <[EMAIL PROTECTED]> wrote:
> Hey guys,
>
> I'm stuck with using a GridBagSizer (wxPython) in a GUI Dialog and am having
> a frustrating time with refreshing it properly.
>
> Essentially, I've got to refresh the contents of the GridBagSizer on
> occasion with new values.  The way I'm doing it works 4 times out of 5 but
> on the 5th, *all* of the grid objects that are set to various positions in
> the GBS are set to (0,0) :|
>
> So, the way I refresh it is that I keep a record of all grid objects
> inserted into the GridBagSizer and on refresh, I first iterate through that
> list and destroy everything in it.  I then call Clear() and then Layout()
> and start adding new grid objects.
> i.e. I do this:
> for obj in self.gridObjects:
> obj.Destroy()
> self.gridObjects = []
> self.gbs.Clear()
> self.gbs.Layout
> // Add all new gridObjects, recording them in self.gridObjects
>
> Again, this works most all the time *except* that sometimes all newly added
> grid objects will end up at (0,0) in the GBS :|
>
> Am I refreshing it correctly?  Has anybody experienced a similar issue?
>
> I appreciate any help :)
> Steve
> --
> View this message in 
> context:http://www.nabble.com/Refreshing-GridBagSizer-tf4863314.html#a13917194
> Sent from the Python - python-list mailing list archive at Nabble.com.

You'll probably do better if you email the wxPython user's group for
these kinds of things. But I'll take a shot at it anyway.

Looking at the GridBagSizer's docs, I found it has the following
method: CheckForIntersection. You could use that to make sure there
are no intersections and then handle it when they occur.

See the docs for more info:

http://www.wxpython.org/docs/api/wx.GridBagSizer-class.html

The mailing list is here:

http://wxpython.org/maillist.php

Without knowing what your layout scheme is or how you add the widgets
to the sizer, it's difficult to give a more specific answer. Since you
say it's a dialog, you might want to consider using a simpler sizer or
a group of simpler sizers nested in one another. There's an entry on
the wiki that might be helpful to you as well.

http://wiki.wxpython.org/UsingSizers

Hope that helps.

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


Re: Catching a segfault in a Python library

2007-11-24 Thread MrJean1
Try catching SIGSEGV using the Python signal module

  

An example (for SIGALRM) is on the next page

  

However, it may not work since a SIGSEGV fault is pretty much the end
of everything :-(

/Jean Brouwers



On Nov 24, 6:22 am, Donn Ingle <[EMAIL PROTECTED]> wrote:
> > I think the idea is, certain fonts in his collection may be corrupt,
> > and he wants to just scan through and load them, ignoring the ones
> > that make the program crash.  
>
> Ya got me! Sheesh, I can't hide anywhere :D
>
> > The bug in this case lies with a third
> > party and isn't something he can easily fix (although he can file
> > reports to the third party (PIL)).
>
> I've a bad memory and can't recall what I told PIL at the time. It might
> have been a case of waiting to see what new versions can do.
>
> > not nice for the application to just crash when that happens, asking
> > them if they want to debug it.  
>
> Zigactly! You can wrap try/except around the calls that (by debugging) you
> know are the culprits, but a segfault is a segfault and bam! you are at the
> command line again.
>
> > I haven't really found a solution,
> > just have tried to prevent corrupted files in the system for now.  Let
> > me know if you get this solved
>
> I'll certainly pop a note. I think, though, that the answer may reside in
> the basic theme of this thread:
>
> runapp
>  result = runActualApp( )
>  while True:
>   if result == allokay: break
>   else:
>
>
> Unless a segfault goes through that too, like Krypton through Superman.
> \d

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


Re: OPLC purchase period extended

2007-11-24 Thread Aahz
In article <[EMAIL PROTECTED]>,
Grant Edwards  <[EMAIL PROTECTED]> wrote:
>
>I've ordered mine, but I still haven't gotten an answer on how to sign
>up for the year of free T-Mobile WiFi access that's included in the
>offer.  The T-Mobile site says you need a PIN from your "confirmation
>letter".  I never I've gotten neither a confirmation letter nor a
>response to the e-mail I sent to OLPC asking about it. :/

Given past experience with T-Mobile, you probably need to give them both
social security number and a credit card, so I probably won't take them
up on their "free" offer.

>The XO laptop comes with a built-in Python IDE, so everybody on c.l.p
>ought to have one...

That was more-or-less my reasoning for getting one, but the clincher was
finding out on Tday that my sibling and spouse had independently decided
to get one for my nibling, so I really needed to get one in order to help
them.

Supposedly with the monochrome screen the battery life is good enough to
use the XO as an eBook reader...
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

"Typing is cheap.  Thinking is expensive."  --Roy Smith
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Disk Space Script

2007-11-24 Thread [EMAIL PROTECTED]
On Nov 24, 2:11 pm, [EMAIL PROTECTED] wrote:
> On Nov 24, 11:46 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
> wrote:
>
>
>
> > Hello all,
>
> > I would like to write a script in Python to email me when disk space
> > gets below a certain value.
>
> > My first question (I'm sure of many) is how do get this output into a
> > dictionary or list to index the values?
>
> > import os
> > os.system("df -x cifs -x iso9660 | grep -E ^/dev | awk '{ print
> > $1,$4 }'")
>
> > [What the output looks like]
>
> > /dev/sda3 15866012
> > /dev/sda4 26126712
>
> > I would like to write code that compares /dev/sda* to a number (ex.
> > 200 -> "2GB") and sends an alert if the indexed value is below it.
>
> > I'm a noob so keep it simple.
>
> > Thanks in advance.
>
> I don't know the Unix command for this, but I would just redirect the
> output to a file on your system. See the following for more info:
>
> http://www.faqs.org/docs/diveintopython/kgp_stdio.html
>
> You could send the output to a variable and create a file-like stream
> too. Either way, read the file (or stream) and then for each line,
> split it on the space.
>
> Then you can do the compare and use the email module to email you the
> result should it go over your specified amount.
>
> By the by, if you're using Python 2.4 or above, you should switch to
> using the subprocess module rather than using os.system since the
> latter is being deprecated.
>
> For more on file objects, see the docs:
>
> http://docs.python.org/lib/bltin-file-objects.html
>
> or there's this good article:
>
> http://www.devshed.com/c/a/Python/File-Management-in-Python/
>
> And the email module is explained quite well in the docs too:
>
> http://docs.python.org/lib/module-email.html
>
> Mike

Thanks for the info... I will do some reading...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Catching a segfault in a Python library

2007-11-24 Thread Paul Rubin
Donn Ingle <[EMAIL PROTECTED]> writes:
> runapp
>  result = runActualApp( )
>  while True:
>   if result == allokay: break
>   else: 
>
> 
> Unless a segfault goes through that too, like Krypton through Superman.

No you can't do it that way, when I say "run it under a debugger", I
mean run the entire Python interpreter under something like gdb, and
actually debug the seg fault.  I can understand if you're reluctant to
make that effort.  If it's for something critical then you should
certainly do it.  If it's for something exposed to the internet or
used by potentially malicious users then you should certainly do it.
If it's for something throwaway and exposed only to trusted users,
then maybe your trap-and-restart approach can keep things usable for a
while.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the annoying, verbose self

2007-11-24 Thread Paul Boddie
On 24 Nov, 20:10, "Patrick Mullen" <[EMAIL PROTECTED]> wrote:
>
> Yes, that's no good.  So you would write it like so:
>
> def meth(self,*args):
> tmp = int(raw_input('Enter age:'))
> using self:
> age = tmp
>
> Still an unnecessary lookup on tmp though :)

Indeed. As has been mentioned, it's all about resolving names and how
much of that work gets done at run-time (and whether the magic
confuses the human reader further).

>   And it would be useless
> to use it for one assignment, the idea is to eliminate all the typing
> with this:
>
> self.var1 = 5
> self.var2 = "a value"
> self.var3 = stuff
> self.var4 = [2,54,7,7]
> self.var5 = "dingaling"
> self.var6 = 6.4
> self.var7 = 1
> self.var8 = False
> self.var9 = True
>
> Of course that "self.var3 = stuff" under the using would result in a
> bad lookup for "stuff", but the programmer who wanted to use this
> would have to realize this and try to avoid it.

I think the remedy is worse than the ailment, especially since the
Pascal "with" construct can make use of declared information about
structure attributes, while the absence of such declarations leaves
more work to be done in Python (and more detective work for future
maintainers of code). Of course, for cases like the above, assuming
that one doesn't feel that lists or dictionaries are acceptable
alternatives to large numbers of instance attributes (which might not
have such regular naming), one might suggest a useful extension of the
attribute access syntax. Taking part of the above example and
rewriting...

self.(var1, var2, var3, var4) = 5, "a value", stuff, [2,54,7,7]

I'm sure Mr Schluehr can provide a working demonstration of this with
very little effort. ;-)

Paul

P.S. There were some proposals for generalisations of the attribute
access mechanisms using not completely different syntax, but I don't
recall them addressing the issue of accessing multiple attributes at
the same time, and the use of arbitrary expressions in place of the
attribute name (where a tuple is used above) gave a distinct feeling
of writing something similar to a combination of the worst aspects of
some shell language with some of the nastier parts of microcomputer
assembly language. Let us, therefore, not get too enthusiastic about
such ideas!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Disk Space Script

2007-11-24 Thread kyosohma
On Nov 24, 2:20 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> On Nov 24, 2:11 pm, [EMAIL PROTECTED] wrote:
>
>
>
> > On Nov 24, 11:46 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
> > wrote:
>
> > > Hello all,
>
> > > I would like to write a script in Python to email me when disk space
> > > gets below a certain value.
>
> > > My first question (I'm sure of many) is how do get this output into a
> > > dictionary or list to index the values?
>
> > > import os
> > > os.system("df -x cifs -x iso9660 | grep -E ^/dev | awk '{ print
> > > $1,$4 }'")
>
> > > [What the output looks like]
>
> > > /dev/sda3 15866012
> > > /dev/sda4 26126712
>
> > > I would like to write code that compares /dev/sda* to a number (ex.
> > > 200 -> "2GB") and sends an alert if the indexed value is below it.
>
> > > I'm a noob so keep it simple.
>
> > > Thanks in advance.
>
> > I don't know the Unix command for this, but I would just redirect the
> > output to a file on your system. See the following for more info:
>
> >http://www.faqs.org/docs/diveintopython/kgp_stdio.html
>
> > You could send the output to a variable and create a file-like stream
> > too. Either way, read the file (or stream) and then for each line,
> > split it on the space.
>
> > Then you can do the compare and use the email module to email you the
> > result should it go over your specified amount.
>
> > By the by, if you're using Python 2.4 or above, you should switch to
> > using the subprocess module rather than using os.system since the
> > latter is being deprecated.
>
> > For more on file objects, see the docs:
>
> >http://docs.python.org/lib/bltin-file-objects.html
>
> > or there's this good article:
>
> >http://www.devshed.com/c/a/Python/File-Management-in-Python/
>
> > And the email module is explained quite well in the docs too:
>
> >http://docs.python.org/lib/module-email.html
>
> > Mike
>
> Thanks for the info... I will do some reading...

If you need some more help, just let us know.

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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Steven D'Aprano
On Sat, 24 Nov 2007 03:44:59 -0800, Licheng Fang wrote:

> On Nov 24, 7:05 pm, Bjoern Schliessmann  [EMAIL PROTECTED]> wrote:
>> Licheng Fang wrote:
>> > I find myself frequently in need of classes like this for two
>> > reasons. First, it's efficient in memory.
>>
>> Are you using millions of objects, or MB size objects? Otherwise, this
>> is no argument.
> 
> Yes, millions. 


Oh noes!!! Not millions of words That's like, oh, a few tens of 
megabytes1! How will a PC with one or two gigabytes of RAM cope?

Tens of megabytes is not a lot of data.

If the average word size is ten characters, then one million words takes 
ten million bytes, or a little shy of ten megabytes. Even if you are 
using four-byte characters, you've got 40 MB, still a moderate amount of 
data on a modern system.


> In my natural language processing tasks, I almost always
> need to define patterns, identify their occurrences in a huge data, and
> count them. Say, I have a big text file, consisting of millions of
> words, and I want to count the frequency of trigrams:
> 
> trigrams([1,2,3,4,5]) == [(1,2,3),(2,3,4),(3,4,5)]
> 
> I can save the counts in a dict D1. Later, I may want to recount the
> trigrams, with some minor modifications, say, doing it on every other
> line of the input file, and the counts are saved in dict D2. Problem is,
> D1 and D2 have almost the same set of keys (trigrams of the text), yet
> the keys in D2 are new instances, even though these keys probably have
> already been inserted into D1. So I end up with unnecessary duplicates
> of keys. And this can be a great waste of memory with huge input data.

All these keys will almost certainly add up to only a few hundred 
megabytes, which is a reasonable size of data but not excessive. This 
really sounds to me like a case of premature optimization. I think you 
are wasting your time solving a non-problem.



[snip]
> Wow, I didn't know this. But exactly how Python manage these strings? My
> interpretator gave me such results:
> 
 a = 'this'
 b = 'this'
 a is b
> True
 a = 'this is confusing'
 b = 'this is confusing'
 a is b
> False


It's an implementation detail. You shouldn't use identity testing unless 
you actually care that two names refer to the same object, not because 
you want to save a few bytes. That's poor design: it's fragile, 
complicated, and defeats the purpose of using a high-level language like 
Python.




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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Steven D'Aprano
On Sat, 24 Nov 2007 04:54:43 -0800, samwyse wrote:

> create a hash that maps your keys to themselves, then use the values of
> that hash as your keys.
> 
 store = {}
 def atom(str):
>   global store
>   if str not in store:
>   store[str] = str
>   return store[str]

Oh lordy, that's really made my day! That's the funniest piece of code 
I've seen for a long time! Worthy of being submitted to the DailyWTF.

Samwyse, while I applaud your willingness to help, I think you actually 
need to get some programming skills before doing so. Here's a hint to get 
you started: can you think of a way to optimize that function so it does 
less work?



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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Steven D'Aprano
On Sat, 24 Nov 2007 12:00:25 +0100, Bjoern Schliessmann wrote:

> Licheng Fang wrote:
>> I mean, all the class instances that equal to each other should be
>> reduced into only one instance, which means for instances of this class
>> there's no difference between a is b and a==b.
> 
> If you only want that if "a == b" is True also "a is b" is True,
> overload the is_ attribute of your class. Personally, I don't see any
> advantage in this.

No advantage? That's for sure. There is no is_ attribute of generic 
classes, and even if there was, it would have no special meaning.

Identity testing can't be overloaded. If it could, it would no longer be 
identity testing.


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


Re: Installing modules via setuptools in a script

2007-11-24 Thread Robert Kern
Thorsten Kampe wrote:

> Hi,
> 
> can anyone give me a short code snippet how to install a missing 
> module via setuptools (assuming setuptools is already installed)?!
> 
> Something like this:
> 
> try:
> import missing_module
> except import_error
> import setuptools
> setuptools.whatever.install(missing_module)

The recommended way to handle dependencies using setuptools is to specify them
in the install_requires metadata in the setup() function call in your setup.py:

  # http://peak.telecommunity.com/DevCenter/setuptools#basic-use
  setup(name="foo",
...
install_requires = [
  'some_package >= 1.0',
  'another_package',
],
  )

However, if you have special needs that really do require downloading the
dependency at runtime instead of install-time:

  #
http://peak.telecommunity.com/DevCenter/PkgResources#workingset-methods-and-attributes

  import pkg_resources
  pkg_resources.resolve('some_package >= 1.0')
  pkg_resources.resolve('another_package')

  import some_package
  import another_package

But, please be sure that that your needs are special.

-- 
Robert Kern

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

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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread George Sakkis
On Nov 24, 4:59 pm, Steven D'Aprano

<[EMAIL PROTECTED]> wrote:
> On Sat, 24 Nov 2007 03:44:59 -0800, Licheng Fang wrote:
> > On Nov 24, 7:05 pm, Bjoern Schliessmann  > [EMAIL PROTECTED]> wrote:
> >> Licheng Fang wrote:
> >> > I find myself frequently in need of classes like this for two
> >> > reasons. First, it's efficient in memory.
>
> >> Are you using millions of objects, or MB size objects? Otherwise, this
> >> is no argument.
>
> > Yes, millions.
>
> Oh noes!!! Not millions of words That's like, oh, a few tens of
> megabytes1! How will a PC with one or two gigabytes of RAM cope?
>

Comments like these make one wonder if your real life experience with
massive data matches even the one tenth of your self-importance and
need to be snarky in most of your posts.

To the OP: yes, your use case is quite valid; the keyword you are
looking for is "memoize". You can find around a dozen of recipes in
the Cookbook and posted in this list; here's one starting point:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413717.

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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Bjoern Schliessmann
Steven D'Aprano wrote:

> No advantage? That's for sure. There is no is_ attribute of
> generic classes, and even if there was, it would have no special
> meaning.

Argl, I confused the operator module's attributes with objects ;)
 
Regards,


Björn

-- 
BOFH excuse #378:

Operators killed by year 2000 bug bite.

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


Re: Disk Space Script

2007-11-24 Thread Erik Max Francis
[EMAIL PROTECTED] wrote:

> I would like to write a script in Python to email me when disk space
> gets below a certain value.
> 
> My first question (I'm sure of many) is how do get this output into a
> dictionary or list to index the values?

Read it in line by line with os.popen, or one of the more involved 
popen... modules, and then parse it however you like.

-- 
Erik Max Francis && [EMAIL PROTECTED] && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
   Who'd ever think it / Such a squalid little ending
-- The American and Florence, _Chess_
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the annoying, verbose self

2007-11-24 Thread greg
samwyse wrote:
> so you might instead
> add 'as' clauses as an alternate way to reduce confusion:
> 
using myclass.new() as p:
> 
> p.do_something()
> p.something_else()

or even

   p = myclass.new()
   p.do_something()
   p.something_else()

Doesn't even need any new syntax. :-)

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


Re: the annoying, verbose self

2007-11-24 Thread greg
Patrick Mullen wrote:
> Sometimes I actually use a dictionary, but typing all of the quotes
> for the keys gets old.

If the keys are all identifiers, you can use keyword
args to the dict constructor. So you could write

   self.__dict__.update(dict(var1 = 5,
 var2 = "a value", var3 = stuff))

if you really wanted to. (Don't be surprised if
everyone else refuses to maintain your code, though!)

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


Re: the annoying, verbose self

2007-11-24 Thread greg
samwyse wrote:
> Later, I inevitably decide to encapsulate it inside a class, which
> means lots of source changes to change my function into a method

You'd be better off changing your design habits to make
things into classes from the beginning if you suspect
you may want it that way later.

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


How to display unicode with the CGI module?

2007-11-24 Thread coldpizza
Hi!

I am using the built-in Python web server (CGIHTTPServer) to serve
pages via CGI.
The problem I am having is that I get an error while trying to display
Unicode UTF-8 characters via a Python CGI script.

The error goes like this: "UnicodeEncodeError: 'ascii' codec can't
encode character u'\u026a' in position 12: ordinal not in range(128)".

My question is: (1 ) how and (2) where do I set the encoding for the
page?

I have tried adding  but this does not seem to help, as this is an
instruction for the browser, not for the webserver and/or CGI script.

Do I have to set the encoding in the server script? On in the Python
CGI script?

The data that I want to display comes from a sqlite3 database and is
already in Unicode format.

The webserver script looks like this:

[code]
#
import CGIHTTPServer, BaseHTTPServer
httpd=BaseHTTPServer.HTTPServer(('',8080),
CGIHTTPServer.CGIHTTPRequestHandler)
httpd.serve_forever()
#
[/code]

A simplified version of my Python CGI script would be:
[code]
import cgi

print "text/html"
print

print ""
print " "
print   "my UTF8 string: Français 日本語 Español Português Română"
print " "
print ""

[/code]

Where and what do I need to add to these scripts to get proper display
of UTF8 content?
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Steven D'Aprano
On Sat, 24 Nov 2007 14:58:50 -0800, George Sakkis wrote:

> On Nov 24, 4:59 pm, Steven D'Aprano
> 
> <[EMAIL PROTECTED]> wrote:
>> On Sat, 24 Nov 2007 03:44:59 -0800, Licheng Fang wrote:
>> > On Nov 24, 7:05 pm, Bjoern Schliessmann > > [EMAIL PROTECTED]> wrote:
>> >> Licheng Fang wrote:
>> >> > I find myself frequently in need of classes like this for two
>> >> > reasons. First, it's efficient in memory.
>>
>> >> Are you using millions of objects, or MB size objects? Otherwise,
>> >> this is no argument.
>>
>> > Yes, millions.
>>
>> Oh noes!!! Not millions of words That's like, oh, a few tens of
>> megabytes1! How will a PC with one or two gigabytes of RAM
>> cope?
>>
>>
> Comments like these make one wonder if your real life experience with
> massive data matches even the one tenth of your self-importance and need
> to be snarky in most of your posts.

I cheerfully admit to never needing to deal with "massive data".

However, I have often needed to deal with tens and hundreds of megabytes 
of data, which IS NOT MASSIVE amounts of data to deal with on modern 
systems. Which was my point. 


> To the OP: yes, your use case is quite valid; the keyword you are
> looking for is "memoize". You can find around a dozen of recipes in the
> Cookbook and posted in this list; here's one starting point:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413717.

This has nothing, absolutely NOTHING, to do with memoization. Memoization 
trades off memory for time, allowing slow functions to return results 
faster at the cost of using more memory. The OP wants to save memory, not 
use more of it.



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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Hrvoje Niksic
samwyse <[EMAIL PROTECTED]> writes:

> create a hash that maps your keys to themselves, then use the values
> of that hash as your keys.

The "atom" function you describe already exists under the name
"intern".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OPLC purchase period extended

2007-11-24 Thread Grant Edwards
>>I've ordered mine, but I still haven't gotten an answer on how to sign
>>up for the year of free T-Mobile WiFi access that's included in the
>>offer.  The T-Mobile site says you need a PIN from your "confirmation
>>letter".  I never I've gotten neither a confirmation letter nor a
>>response to the e-mail I sent to OLPC asking about it. :/
>
> Given past experience with T-Mobile, you probably need to give them both
> social security number and a credit card, so I probably won't take them
> up on their "free" offer.

I got an e-mail today from the OLPC support staff saying that
the PIN would be e-mailed.

>>The XO laptop comes with a built-in Python IDE, so everybody on
>>c.l.p ought to have one...
>
> That was more-or-less my reasoning for getting one, but the
> clincher was finding out on Tday that my sibling and spouse
> had independently decided to get one for my nibling, so I
> really needed to get one in order to help them.
>
> Supposedly with the monochrome screen the battery life is good
> enough to use the XO as an eBook reader...

I know that was the goal.  I read somewhere that they're still
working on power-management issues.

It's running a tweaked RedHat linux, and the tools to build a
custom ROM image are freely available -- including a Qemu-based
emulator that lets you run rom images on a simulated XO.

The most imporant thing is that the "control" key is to the
left of the "A" keay where god intened.  Not too surprising
when you realized the design was headed by folks from the media
lab at MIT.  MIT requires everybody to use Emacs, right?

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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Steven D'Aprano
On Sun, 25 Nov 2007 01:38:51 +0100, Hrvoje Niksic wrote:

> samwyse <[EMAIL PROTECTED]> writes:
> 
>> create a hash that maps your keys to themselves, then use the values of
>> that hash as your keys.
> 
> The "atom" function you describe already exists under the name "intern".

Not really. intern() works very differently, because it can tie itself to 
the Python internals. Samwyse's atom() function doesn't, and so has no 
purpose.


In any case, I'm not sure that intern() actually will solve the OP's 
problem, even assuming it is a real and not imaginary problem. According 
to the docs, intern()'s purpose is to speed up dictionary lookups, not to 
save memory. I suspect that if it does save memory, it will be by 
accident.

>From the docs: 
http://docs.python.org/lib/non-essential-built-in-funcs.html

intern(  string)
Enter string in the table of ``interned'' strings and return the interned 
string - which is string itself or a copy. Interning strings is useful to 
gain a little performance on dictionary lookup - if the keys in a 
dictionary are interned, and the lookup key is interned, the key 
comparisons (after hashing) can be done by a pointer compare instead of a 
string compare. Normally, the names used in Python programs are 
automatically interned, and the dictionaries used to hold module, class 
or instance attributes have interned keys. Changed in version 2.3: 
Interned strings are not immortal (like they used to be in Python 2.2 and 
before); you must keep a reference to the return value of intern() around 
to benefit from it.


Note the words "which is string itself or a copy". It would be ironic if 
the OP uses intern to avoid having copies of strings, and ends up with 
even more copies than if he didn't bother.

I guess he'll actually need to measure his memory consumption and see 
whether he actually has a memory problem or not, right?


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


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Hrvoje Niksic
Steven D'Aprano <[EMAIL PROTECTED]> writes:

> On Sun, 25 Nov 2007 01:38:51 +0100, Hrvoje Niksic wrote:
>
>> samwyse <[EMAIL PROTECTED]> writes:
>> 
>>> create a hash that maps your keys to themselves, then use the values of
>>> that hash as your keys.
>> 
>> The "atom" function you describe already exists under the name "intern".
>
> Not really. intern() works very differently, because it can tie itself to 
> the Python internals.

The exact implementation mechanism is subtly different, but
functionally intern is equivalent to the "atom" function.

> In any case, I'm not sure that intern() actually will solve the OP's
> problem, even assuming it is a real and not imaginary
> problem. According to the docs, intern()'s purpose is to speed up
> dictionary lookups, not to save memory. I suspect that if it does
> save memory, it will be by accident.

It's not by accident, it follows from what interning does.  Interning
speeds up comparisons by returning the same string object for the same
string contents.  If the strings you're working with tend to repeat,
interning will save some memory simply by preventing storage of
multiple copies of the same string.  Whether the savings would make
any difference for the OP is another question.

> From the docs: 
> http://docs.python.org/lib/non-essential-built-in-funcs.html
>
> intern(  string)
> Enter string in the table of ``interned'' strings and return the interned 
> string - which is string itself or a copy. [...]
>
> Note the words "which is string itself or a copy". It would be ironic if 
> the OP uses intern to avoid having copies of strings, and ends up with 
> even more copies than if he didn't bother.

That's a frequently misunderstood sentence.  It doesn't mean that
intern will make copies; it simply means that the string you get back
from intern can be either the string you passed it or another
(previously interned) string object that is guaranteed to have the
same contents as your string (which makes it technically a "copy" of
the string you passed to intern).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OPLC purchase period extended

2007-11-24 Thread David Boddie
On Sat Nov 24 19:30:03 CET 2007, Grant Edwards wrote:

> The XO laptop comes with a built-in Python IDE, so everybody on
> c.l.p ought to have one...

A nice idea in theory, but...

  2. XO laptops will be shipped only to G1G1 participants and only to the
  street addresses they provide for themselves in the fifty United States,
  Puerto Rico, Guam, the U.S. Virgin Islands, the District of Columbia and
  Canada.

  [http://laptopgiving.org/en/terms-and-conditions.php]

David :-(

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


Re: the annoying, verbose self

2007-11-24 Thread Paul McGuire
For these localized initialization blocks, I don't see anything wrong
with:

_ = self
_.var1 = 5
_.var2 = "a value"
_.var3 = stuff
_.var4 = [2,54,7,7]
_.var5 = "dingaling"
_.var6 = 6.4
_.var7 = 1
_.var8 = False
_.var9 = True

Or if you wanted to simulate something like using or with:

for _ in [self]:
_.var1 = 5
_.var2 = "a value"
_.var3 = stuff
_.var4 = [2,54,7,7]
_.var5 = "dingaling"
_.var6 = 6.4
_.var7 = 1
_.var8 = False
_.var9 = True

-- Paul


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


Re: OPLC purchase period extended

2007-11-24 Thread Aahz
In article <[EMAIL PROTECTED]>,
Grant Edwards  <[EMAIL PROTECTED]> wrote:
>
>The most imporant thing is that the "control" key is to the left of the
>"A" keay where god intened.  Not too surprising when you realized the
>design was headed by folks from the media lab at MIT.  MIT requires
>everybody to use Emacs, right?

What the fuck does that have to do with emacs?  I use vi; my ctrl key is
immediately left of the "A" key as intended by the Flying Spaghetti
Monster.
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

"Typing is cheap.  Thinking is expensive."  --Roy Smith
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread George Sakkis
On Nov 24, 7:42 pm, Steven D'Aprano

> > To the OP: yes, your use case is quite valid; the keyword you are
> > looking for is "memoize". You can find around a dozen of recipes in the
> > Cookbook and posted in this list; here's one starting point:
> >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413717.
>
> This has nothing, absolutely NOTHING, to do with memoization. Memoization
> trades off memory for time, allowing slow functions to return results
> faster at the cost of using more memory. The OP wants to save memory, not
> use more of it.

If you bothered to click on that link you would learn that memoization
can be used to save space too and matches OP's case exactly; even the
identity tests work. Self-importance is bad enough by itself, even
without the ignorance, but you seem to do great in both.

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


Re: OPLC purchase period extended

2007-11-24 Thread Aahz
In article <[EMAIL PROTECTED]>,
David Boddie  <[EMAIL PROTECTED]> wrote:
>On Sat Nov 24 19:30:03 CET 2007, Grant Edwards wrote:
>>
>> The XO laptop comes with a built-in Python IDE, so everybody on
>> c.l.p ought to have one...
>
>A nice idea in theory, but...
>
>  2. XO laptops will be shipped only to G1G1 participants and only to the
>  street addresses they provide for themselves in the fifty United States,
>  Puerto Rico, Guam, the U.S. Virgin Islands, the District of Columbia and
>  Canada.
>
>  [http://laptopgiving.org/en/terms-and-conditions.php]

I'm sure that some people would be willing to serve as middleware...
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

"Typing is cheap.  Thinking is expensive."  --Roy Smith
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Catching a segfault in a Python library

2007-11-24 Thread Donn Ingle
Paul Rubin wrote:
> then maybe your trap-and-restart approach can keep things usable for a
> while.
Trap and restart gracefully is the road I want to take. If my app (FP)
chokes on a font (it will have made a note before doing what kills it,
because I know the 'danger' areas ) and then will die. The second layer
(launcher) app will then (hopefully) be returned to (after the segfault)
and it will then check the "badfont" file and start a wizard for the user
so that the fonts can be renamed/moved/killed.

This is mainly because I lack the skills to hack deeply into complex stuff
like PIL, even though a lot of it is Python, and also because the goals
keep changing as new versions of libs like PIL and Freetype come out. I'd
rather have a parachute than a scalpel :)

\d

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


Re: Catching a segfault in a Python library

2007-11-24 Thread Donn Ingle
MrJean1 wrote:
> Try catching SIGSEGV using the Python signal module
> 
> An example (for SIGALRM) is on the next page
> 
> However, it may not work since a SIGSEGV fault is pretty much the end
> of everything
Thanks for the lead -- I'll go have a look. 

If I can just write a "BLOODYHELL" flag to a text file and then let it all
crash, the next time the app is run, it can go into "Dude, we have
issues..." mode.


\d

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


Hi I am anuusha 21years of student... find your love r here www.loverguru.blogspot.com

2007-11-24 Thread anuusha122
Hi I am anuusha 21years of student... find your lover here 
www.loverguru.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Disk Space Script

2007-11-24 Thread MonkeeSage
On Nov 24, 11:46 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:

> I would like to write a script in Python to email me when disk space
> gets below a certain value.

OK, I'll give you the easy way using your example and popen, and then
a more complex example that doesn't rely on df/grep/awk and uses only
system calls:


Easy way:

import os

# open a pipe to "df ...", read from its stdout,
# strip the trailing \n, split it into a list on
# every \n, and put the results in 'data'
pipe = os.popen("df -x cifs -x iso9660 | " +
"grep --color=never -E '^/dev' | " +
"awk '{print $1, $4}'")
data = pipe.read().strip().split('\n')
pipe.close()

# 'data' now looks like:
# ['/dev/hda1 1405056', '/dev/hda3 152000']

quota = 2097152 # 2GB

# iterate the list, splitting each element
# on ' ', and assigning the first and second
# elements to 'device' and 'free'
for node in data:
  device, free = node.split(' ')
  if int(free) < quota:
# your mail stuff here
print "Device `%s' has less than %.2f GB free space" % \
  (device, quota/1024.0/1024)


More complex way (well, less complex actually, just requires more
knowledge of python/fs):

quota = 2097152 # 2 GB

# map of mount point / device name
device_map = {'/'   : '/dev/hda1',
  '/mnt/shared' : '/dev/hda3'}

for mount, device in device_map.items():

  # statvfs returns tuple of device status,
  # see also "man statvfs" which this call wraps
  vstat = os.statvfs(mount)

  # (block size * free blocks) / 1024 = free bytes
  # NB: for large drives, there is a margin of error
  # in this naive computation, because it doesn't account
  # for things like inode packing by the fs. But for a
  # large quota like 2 GB, a +/- hundrend megs margin of
  # error should not cause any problem.
  free = (vstat[0] * vstat[4]) / 1024

  if free < quota:
# your mail stuff here
print "Device `%s' has less than %.2f GB free space" % \
  (device, quota/1024.0/1024)


References:

statvfs:
http://www.python.org/doc/lib/os-file-dir.html
http://www.python.org/doc/lib/module-statvfs.html

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


Running unmodified CGI scripts persistently under mod_wsgi.

2007-11-24 Thread Graham Dumpleton
On Nov 23, 8:49 am, Graham Dumpleton <[EMAIL PROTECTED]>
wrote:
> On Nov 23, 4:00 am, Istvan Albert <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Nov 21, 12:15 am, Graham Dumpleton <[EMAIL PROTECTED]>
> > wrote:
>
> > > I would say that that is now debatable. Overall mod_wsgi is probably a
> > > better package in terms of what it has to offer. Only thing against
> > > mod_wsgi at this point is peoples willingness to accept something that
> > > is new in conjunction with Linux distributions and web hosting
> > > companies being slow to adopt new packages.
>
> > Yes that is to be expected, many people want someone else to pay the
> > early adopter's costs. Nonetheless mod_wsgi seems like the right
> > direction to move the python world.
>
> > One confounding factor that may slow its adoption could be the need of
> > running plain old CGI in an efficient way. I'm not sure how that fits
> > into the WSGI picture.
>
> Do note that using mod_wsgi doesn't preclude you still running plain
> old CGI scripts using mod_cgi or mod_cgid. As to making it more
> efficient, one can go two paths on this.
>
> The preferred path would be to put in the effort to convert the Python
> CGI application to WSGI. If one still needs to run it as CGI with
> other hosting solutions, then use a CGI-WSGI adapter.
>
> Second, albeit a bit of a kludge, just like mod_python.cgihandler is a
> kludge, is to emulate the CGI environment on top of WSGI. This would
> work if using single threaded Apache prefork MPM, or using mod_wsgi
> daemon mode with multiple processes but where each is single threaded.
> It wouldn't be practical though to do it for multithread Apache worker
> MPM, or multithreaded daemon processes with mod_wsgi daemon mode.
> Because of how Python leaks environment variables between sub
> interpreters, you would also only want to be running one sub
> interpreter within a process. This would be fine if using mod_wsgi
> daemon mode as different CGI scripts could be delegated to run in
> different daemon processes as necessary to keep them separate, but may
> not be practical if using embedded mode if hosting a range of other
> WSGI applications at the same time in embedded mode.
>
> So, it is doable, but effort would be better off expended at least
> converting the Python CGI script to WSGI instead. It would save a lot
> of trouble in the long run, especially with CGI scripts which weren't
> designed to be run multiple times in same memory context, ie., where
> they don't clean up after themselves.
>
> If someone really wanted to host an existing CGI script under WSGI
> where the same process was used over and over, and had good reasons
> for doing so, it wouldn't be hard for me to come up with a workable
> adapter that allowed it.

I had a play with this and got an initial solution working. It allows
mod_wsgi to be pointed at a directory of existing unmodified Python
CGI scripts and it will run them, maintaining the code for the CGI
script in memory between requests. One doesn't even have to rename
scripts to have a .py extension or follow Python module naming
conventions like mod_python required with its CGI handler.

Now, where as CGI script would run say at 10 requests per second for a
simple hello world program, using mod_wsgi to manage the CGI scripts
one could achieve 450+ requests per second with embedded mode of
mod_wsgi, and 250+ requests per second with daemon mode. Not as quick
as what can be achieved for an equivalent WSGI specific version of the
script, which runs at 900+ requests per second for embedded mode and
500+ requests for daemon mode, but still a lot better than a plain old
CGI script which requires a new process for each request.

One would still need to see how it works for more complicated Python
CGI scripts. Also may need to be tweaked to allow one to configure its
behaviour to suit the CGI script. For example, should additional
modules imported by the script be deleted out of sys.modules when done
or preserved for next request. Similarly, should the globals and/or
locals of the script be preserved between requests. Whether one would
need to adjust such behaviour would depend on the nature of the
specific CGI script.

The other question is whether there is even a demand for this. Do
people want to be able to take unmodified Python CGI scripts and try
to run them persistently in this way, or would they be better off
converting them to proper WSGI applications.

Graham

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


Re: How to display unicode with the CGI module?

2007-11-24 Thread Marc 'BlackJack' Rintsch
On Sat, 24 Nov 2007 15:58:56 -0800, coldpizza wrote:

> The problem I am having is that I get an error while trying to display
> Unicode UTF-8 characters via a Python CGI script.
> 
> The error goes like this: "UnicodeEncodeError: 'ascii' codec can't
> encode character u'\u026a' in position 12: ordinal not in range(128)".

Unicode != UTF-8.  You are not trying to send an UTF-8 encoded byte string
but an *unicode string*.  That's not possible.  If unicode strings should
"leave" your program they must be encoded into byte strings.  If you don't
do this explicitly Python tries to encode as ASCII and fails if there's
anything non-ASCII in the string.  The `encode()` method is your friend.

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