Colour sampling

2009-10-15 Thread Dylan Palmboom
Does anyone know what python libraries are available to do the following:
 
1. I would like to take a photograph of an object with a colour. In this
case, it is a sheet of sponge.
2. Feed this image into a function in a python library and let the function
"automatically scan" this image's
pixel colours, and return an average of all the pixels' colours in the
image.
3. I could then use the rgb values to set a colour swatch for the object's
colour.
 
Please let me know if you have any ideas. It would be appreciated.
In the meantime, I will search Google and see what I can find.
 
Thanks
 
-- 
http://mail.python.org/mailman/listinfo/python-list


() vs. [] operator

2009-10-15 Thread Ole Streicher
Hi,

I am curious when one should implement a "__call__()" and when a
"__getitem__()" method. 

For example, I want to display functions and data in the same plot. For
a function, the natural interface would to be called as "f(x)", while
the natural interface for data would be "f[x]". On the other hand,
whether a certain data object is a function or a data table is just an
inner detail of the object (imagine f.e. a complex function that
contains a data table as cache), and there is no reason to distinguish
them by interface.

So what is the reason that Python has separate __call__()/() and
__getitem__()/[] interfaces and what is the rule to choose between them?

Regards

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


Error received from _mechanize.py

2009-10-15 Thread Raji Seetharaman
Hi all,

Im learning web scraping with python from the following link
http://www.packtpub.com/article/web-scraping-with-python

To work with it,  mechanize to be installed
I installed mechanize using

sudo apt-get install python-mechanize

As given in the tutorial, i tried the code as below

import mechanize
BASE_URL = "http://www.packtpub.com/article-network";
br = mechanize.Browser()
data = br.open(BASE_URL).get_data()

Received the following error

File "webscrap.py", line 4, in 
data = br.open(BASE_URL).get_data()
  File "/usr/lib/python2.6/dist-packages/mechanize/_mechanize.py", line 209,
in open
return self._mech_open(url, data, timeout=timeout)
  File "/usr/lib/python2.6/dist-packages/mechanize/_mechanize.py", line 261,
in _mech_open
raise response
mechanize._response.httperror_seek_wrapper: HTTP Error 403: request
disallowed by robots.txt


Any Ideas? Welcome
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: () vs. [] operator

2009-10-15 Thread Steven D'Aprano
On Thu, 15 Oct 2009 09:14:35 +0200, Ole Streicher wrote:

> So what is the reason that Python has separate __call__()/() and
> __getitem__()/[] interfaces and what is the rule to choose between them?

They are separate so you can implement both, or just one, or neither, 
whichever makes the most sense for your data type.

If something is function-like, then implement __call__. If something is 
sequence- or dictionary-like, then implement __getitem__. If it's both, 
then make an arbitrary choice of which one you want to support, or 
implement both, whichever you prefer.

The best thing to do is follow the lead of build-in objects. For example, 
chr() is a function which takes an integer and returns a character, but 
it makes little sense to think of it as a table of values. You wouldn't 
sensibly expect to do something like:

chr[32:39] = 'abc'

and have the table replace chars ' !"#$%&' with 'abc' (changing the 
length of the table). Even if chr() is implemented as a table internally, 
it's actually a function.

Contrast that with xrange objects, which are implemented as lazy 
sequences, i.e. something like a function. But xrange objects themselves 
(not the xrange function) use the sequence interface:

>>> obj = xrange(23, 42)
>>> obj[10]
33

with the limitation that it doesn't support slices.



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


Re: () vs. [] operator

2009-10-15 Thread Xavier Ho
On Thu, Oct 15, 2009 at 5:14 PM, Ole Streicher wrote:


> So what is the reason that Python has separate __call__()/() and
> __getitem__()/[] interfaces and what is the rule to choose between them?


Hi,

This is very interesting, a thought that never occured to me before.
Usually, a function is a function; an array (list, iterator, whatever you
have) is, well, an array of data. You access them by index.

Mathematically speaking, functions are usually continuous. For any given
continuous function, you can give it any value of input (1, 0.1, or even
0.01) and it'll give you one output. But obviously we don't call an item
from index 0.1, but 0, 1, 2, 3, and so on. That's probably the first sign
where your function might be best fitted.

Also, one would normally expect a __getitem__() call to be very fast; it
would be awkward to return A[1] a hundred times slower than A[1]. That
said, it's a simple constant-time look up. That'll give you another clue
when to distinquish the two functions.

The other way also works; if a function takes any floating/decimal number,
usually we expect it to be callable, with the () operator.

I can think of funky things like to memorise every function call that has
been done, and look up the memory if this same input has been called before;
it might speed up some calculation for certain purposes. All in all, a good
idea to separate the two operators.

Cheers,
Xav
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: () vs. [] operator

2009-10-15 Thread Chris Rebert
On Thu, Oct 15, 2009 at 12:14 AM, Ole Streicher  wrote:
> Hi,
>
> I am curious when one should implement a "__call__()" and when a
> "__getitem__()" method.
>
> For example, I want to display functions and data in the same plot. For
> a function, the natural interface would to be called as "f(x)", while
> the natural interface for data would be "f[x]". On the other hand,
> whether a certain data object is a function or a data table is just an
> inner detail of the object (imagine f.e. a complex function that
> contains a data table as cache), and there is no reason to distinguish
> them by interface.
>
> So what is the reason that Python has separate __call__()/() and
> __getitem__()/[] interfaces and what is the rule to choose between them?

Because it'd seem somewhat weird to "call" a container (e.g.
list/dict) as if it were a function, in order to subscript it. And,
like many things, the syntax/distinction is inherited from C.
Also, I'm not entirely sure on this, but I don't believe subscripting
allows for the full function call syntax (with e.g. keyword
parameters, etc).
Further, subscripting generally implies the related notion of
keys/indices and values, whereas callables carry no such association.

So that's why there's both.

Personally, I'd say you should choose subscripting if the notion of
"keys" and "values" makes sense in whatever your use case is, and call
syntax if the notion of "operation that performs work/computation" or
"non-simple query" is more applicable, especially if side-effects are
involved; subscripting is generally for fairly cheap operations, call
syntax for non-generally-cheap ones.
In the particular example you gave, I'd favor call syntax because it
ties more strongly to the notion of functions, whereas subscripting
lightly implies the data-table representation, which you rightly point
out should probably be an implementation detail.

However, there's absolutely nothing stopping you from implementing
both __call__ and __getitem__ and just having them do the same thing,
so that's also an option (though the redundancy isn't satisfying from
an aesthetic point of view).

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the usage of 'yield' keyword

2009-10-15 Thread Tim Golden

Dave Angel wrote:


def find(root):
   for pdf in os.walk(root, topdown=False):
   for file in pdf[2]:
   yield os.path.join(pdf[0],file)





At the risk of nitpicking, I think that a modicum of
tuple-unpacking would aid readability here:



for dirpath, dirnames, filenames in os.walk (root, topdown=False):
 for filename in filenames:
   yield os.path.join (dirpath, filename)



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


Re: The rap against "while True:" loops

2009-10-15 Thread Tim Rowe
2009/10/11 Philip Semanchuk :
> IMHO, break, goto, etc. have their place, but they're ripe for abuse which
> leads to spaghetti code.

Unrestricted goto can leat to spaghetti code, but surely break can't?
AFAICS, any break construct will still be H-K reducible.

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


creating a slide show in JES

2009-10-15 Thread Matthew manzo

I need to create a slideshow in JES but am not sure how to do it. Can anyone 
help me with this. How do i create a slideshow that has a song and then two 
images where one image begins then the second image slowly blends in and takes 
over the first image? -- 
http://mail.python.org/mailman/listinfo/python-list


Re: () vs []

2009-10-15 Thread Mick Krippendorf
mattia schrieb:
> Any particular difference in using for a simple collection of element () 
> over [] or vice-versa?

Just try this and you'll see:

tup = (1,2,3)
tup.append(4)

or:

tup = (1,2,3)
tup[0] = 4

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


() vs []

2009-10-15 Thread mattia
Any particular difference in using for a simple collection of element () 
over [] or vice-versa?

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


Re: () vs []

2009-10-15 Thread Nanjundi
On Oct 14, 1:05 pm, mattia  wrote:
> Any particular difference in using for a simple collection of element ()
> over [] or vice-versa?
>
> Thanks, Mattia

From: http://www.faqs.org/docs/diveintopython/odbchelper_tuple.html

1   You can’t add elements to a tuple. Tuples have no append or extend
method.
2   You can’t remove elements from a tuple. Tuples have no remove or
pop method.
3   You can’t find elements in a tuple. Tuples have no index method.
4   You can, however, use in to see if an element exists in the tuple.

So what are tuples good for?

* Tuples are faster than lists. If you’re defining a constant set
of values and all you’re ever going to do with it is iterate through
it, use a tuple instead of a list.
* Remember I said that dictionary keys can be integers, strings,
and “a few other types”? Tuples are one of those types. Tuples can be
used as keys in a dictionary, but lists can’t.[2]
* Tuples are used in string formatting, as we’ll see shortly.

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


Re: the usage of 'yield' keyword

2009-10-15 Thread Paul Rudin
Peng Yu  writes:

> http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt
>
> The explanation of yield is not clear to me, as I don't know what a
> generator is. I see the following example using 'yield'. Could
> somebody explain how 'yield' works in this example? Thank you!
>
> def brange(limit):
>   i = 0
>   while i < limit:
>   yield i
>   i += 1

Try the sections on iterators, generators and generator expressions in
the tutorial: 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Error received from _mechanize.py

2009-10-15 Thread Chris Rebert
On Thu, Oct 15, 2009 at 12:39 AM, Raji Seetharaman  wrote:
> Hi all,
>
> Im learning web scraping with python from the following link
> http://www.packtpub.com/article/web-scraping-with-python
>
> To work with it,  mechanize to be installed
> I installed mechanize using
>
> sudo apt-get install python-mechanize
>
> As given in the tutorial, i tried the code as below
>
> import mechanize
> BASE_URL = "http://www.packtpub.com/article-network";
> br = mechanize.Browser()
> data = br.open(BASE_URL).get_data()
>
> Received the following error
>
> File "webscrap.py", line 4, in 
>     data = br.open(BASE_URL).get_data()
>   File "/usr/lib/python2.6/dist-packages/mechanize/_mechanize.py", line 209,
> in open
>     return self._mech_open(url, data, timeout=timeout)
>   File "/usr/lib/python2.6/dist-packages/mechanize/_mechanize.py", line 261,
> in _mech_open
>     raise response
> mechanize._response.httperror_seek_wrapper: HTTP Error 403: request
> disallowed by robots.txt

Apparently that website's tutorial and robots.txt are not in sync.
robots.txt is part of the Robot Exclusion Standard
(http://en.wikipedia.org/wiki/Robots_exclusion_standard) and is the
standard way websites specify which webpages should and should not be
accessed programmatically. In this case, that site's robots.txt is
forbidding access to the webpage in question from autonomous programs.

There's probably a way to tell mechanize to ignore robots.txt though,
given the standard is not enforced server-side; programs just follow
it voluntarily.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: () vs []

2009-10-15 Thread Xavier Ho
On Thu, Oct 15, 2009 at 3:21 AM, Nanjundi  wrote:

> 3   You can’t find elements in a tuple. Tuples have no index method.
>

I don't know what language you're using there, but my Python tuples have
indexes.

>>> a = (1, 2, 3)
>>> a
(1, 2, 3)
>>> a[1]
2
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: () vs. [] operator

2009-10-15 Thread Ulrich Eckhardt
Ole Streicher wrote:
> I am curious when one should implement a "__call__()" and when a
> "__getitem__()" method.
> 
> For example, I want to display functions and data in the same plot.

Wait: The term 'function' is overloaded. In Python and programming in
general, a function is a piece of code with optional input, output and side
effects. Alternative names are procedure, operation, subroutine.

In mathematics, the term 'function' refers to an algorithm that takes some
parameters, transforms them and returns a result. Note that here no side
effects can ever occur and that you always have both input parameters and
output parameters. Also, a mathematic function will always yield the same
results for the same input, which is basically a result from not having
side effects and not having variables at all.

> For a function, the natural interface would to be called as "f(x)",
> while the natural interface for data would be "f[x]".

In math, a function invocation is written f(x). Since this is a mere
retrieval of a value, regardless of how complicated the implementation may
be, I'd say that the natural syntax would be f[x] in Python. However, since
[] also supports writing a mapping and since an implementation may have a
significant overhead, I'd rather tend towards the function call syntax.

> On the other hand, whether a certain data object is a function or a
> data table is just an inner detail of the object (imagine f.e. a
> complex function that contains a data table as cache), and there is
> no reason to distinguish them by interface.
> 
> So what is the reason that Python has separate __call__()/() and
> __getitem__()/[] interfaces and what is the rule to choose between them?

As their name implies, one invokes a function (procedure, subroutine etc)
while the other retrieves an item from a mapping. I agree that the exact
difference is not really that easy to explain and that there are corner
cases.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


Re: () vs []

2009-10-15 Thread Chris Rebert
On Thu, Oct 15, 2009 at 1:27 AM, Xavier Ho  wrote:
> On Thu, Oct 15, 2009 at 3:21 AM, Nanjundi  wrote:
>> 3       You can’t find elements in a tuple. Tuples have no index method.
>
> I don't know what language you're using there, but my Python tuples have
> indexes.
>
 a = (1, 2, 3)
 a
> (1, 2, 3)
 a[1]
> 2

Nanjundi meant "index method" as in "a method .index()" (i.e. a method
named "index") which searches through the container for the given item
and returns the index of the first instance of said item, like
list.index() does.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: () vs []

2009-10-15 Thread Xavier Ho
On Thu, Oct 15, 2009 at 6:39 PM, Chris Rebert  wrote:

> Nanjundi meant "index method" as in "a method .index()" (i.e. a method
> named "index") which searches through the container for the given item
> and returns the index of the first instance of said item, like
> list.index() does.
>
> Interesting interpretation.. but I just gave it a try.

>>> a = (1,2,3,4)
>>> a
(1, 2, 3, 4)
>>> a.index(3)
2
>>> a.index(5)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: tuple.index(x): x not in tuple

So my Python is saying that tuples do implement .index() method. What gives?

Or maybe the diveintopython version he's quoting is out of date?

Cheers,
Xav
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Colour sampling

2009-10-15 Thread Anthra Norell

Dylan Palmboom wrote:

Does anyone know what python libraries are available to do the following:
 
1. I would like to take a photograph of an object with a colour. In this

case, it is a sheet of sponge.
2. Feed this image into a function in a python library and let the function
"automatically scan" this image's
pixel colours, and return an average of all the pixels' colours in the
image.
3. I could then use the rgb values to set a colour swatch for the object's
colour.
 
Please let me know if you have any ideas. It would be appreciated.

In the meantime, I will search Google and see what I can find.
 
Thanks
 

  
image.histogram () returns a list of pixel counts: red: index 0 - 255, 
green: 256 - 511, blue: 512 - 767. Figure out the median for each color 
and there you go.
  This would obviously not work well with pictures containing a wide 
range of colors, such as a landscape, but then color-averaging a 
colorful picture wouldn't make much sense anyway. You seem to be working 
with uniformly colored objects for which the method should work well, as 
you'll get a narrow range of values with high counts in each color band.


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


Re: () vs []

2009-10-15 Thread Chris Rebert
On Thu, Oct 15, 2009 at 1:46 AM, Xavier Ho  wrote:
>
> On Thu, Oct 15, 2009 at 6:39 PM, Chris Rebert  wrote:
>>
>> Nanjundi meant "index method" as in "a method .index()" (i.e. a method
>> named "index") which searches through the container for the given item
>> and returns the index of the first instance of said item, like
>> list.index() does.
>>
> Interesting interpretation.. but I just gave it a try.
>
 a = (1,2,3,4)
 a
> (1, 2, 3, 4)
 a.index(3)
> 2
 a.index(5)
> Traceback (most recent call last):
>   File "", line 1, in 
> ValueError: tuple.index(x): x not in tuple
>
> So my Python is saying that tuples do implement .index() method. What gives?
>
> Or maybe the diveintopython version he's quoting is out of date?

Apparently...or something stranger.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


HTMLgen???

2009-10-15 Thread andre

Hi,
Does HTMLgen (Robin Friedrich's) still exsist?? And, if so, where can it
be found?

--
   Andre van der Vlies 
   Certifiable Linux/UNIX engineer (CLUE)
   Homepage: http://vandervlies.xs4all.nl/~andre
   Books: http://www.lulu.com/andre14
Key fingerprint = 397C 7479 67DB 9306 23DC B423 7B58 CD5A 6EFF 5CF8
--
"Programming isn't a craft, it's an art."
()  ascii ribbon campaign - against html e-mail
/\- against microsoft attachments
  ^[^#]
--

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


Re: () vs []

2009-10-15 Thread Tim Golden

Xavier Ho wrote:

On Thu, Oct 15, 2009 at 6:39 PM, Chris Rebert  wrote:


Nanjundi meant "index method" as in "a method .index()" (i.e. a method
named "index") which searches through the container for the given item
and returns the index of the first instance of said item, like
list.index() does.

Interesting interpretation.. but I just gave it a try.



a = (1,2,3,4)
a

(1, 2, 3, 4)

a.index(3)

2

a.index(5)

Traceback (most recent call last):
  File "", line 1, in 
ValueError: tuple.index(x): x not in tuple

So my Python is saying that tuples do implement .index() method. What gives?


It was added relatively recently, around Python 2.6 I think,
at least partly as an oh-ok-then reaction to everyone asking:
"how come lists have .index and .count and tuples don't?"
and neverending "tuples-are-immutable-lists-no-they-aren't-yes-they-are"
debate.

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


回复: () vs []

2009-10-15 Thread SmokingDog
the value 5 not in tuple 
reference help
 
list.index(x) 
Return the index in the list of the first item whose value is x. It is an error 
if there is no such item.
 
--
Eric Lee



 
 
 
-- 原始邮件 --
发件人: "Chris Rebert";
发送时间: 2009年10月15日(星期四) 下午4:48
收件人: "Xavier Ho";
主题: Re: () vs []

 
On Thu, Oct 15, 2009 at 1:46 AM, Xavier Ho  wrote:
>
> On Thu, Oct 15, 2009 at 6:39 PM, Chris Rebert  wrote:
>>
>> Nanjundi meant "index method" as in "a method .index()" (i.e. a method
>> named "index") which searches through the container for the given item
>> and returns the index of the first instance of said item, like
>> list.index() does.
>>
> Interesting interpretation.. but I just gave it a try.
>
 a = (1,2,3,4)
 a
> (1, 2, 3, 4)
 a.index(3)
> 2
 a.index(5)
> Traceback (most recent call last):
>  File "", line 1, in 
> ValueError: tuple.index(x): x not in tuple
>
> So my Python is saying that tuples do implement .index() method. What gives?
>
> Or maybe the diveintopython version he's quoting is out of date?

Apparently...or something stranger.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list-- 
http://mail.python.org/mailman/listinfo/python-list


segmentation fault

2009-10-15 Thread ankita dutta
hi,
i am relatively new to python programming,
i am facing the following problem:

i am tring to simply  obtain data from a file ( "B.txt" ,  data in this file
are in single column and floats)
and plot a graph between those values and thier index.  ( for example if in
file , if at 2nd position valuse is 9.34 ,  x=9.34 and y=1).
.
.
.
*ln1=open("B.txt","r+")
lines5=ln1.readlines()
exp=[ ]
c1=[ ]
for i in range (0,len(lines1)):
f1=lines1[i].split()
c1.append(float(f1[0]))
exp.append(i)
c1.sort(reverse=1)

lines=plt.plot(c1,exp,'ro')
plt.show()
*.
.
.

but every time its showing the following error:

*  is dumped
segmentation fault*


can anyone kindly help me out

thank you.


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


Re: () vs []

2009-10-15 Thread Xavier Ho
On Thu, Oct 15, 2009 at 6:55 PM, Tim Golden  wrote:

> It was added relatively recently, around Python 2.6 I think,
> at least partly as an oh-ok-then reaction to everyone asking:
> "how come lists have .index and .count and tuples don't?"
> and neverending "tuples-are-immutable-lists-no-they-aren't-yes-they-are"
> debate.
>
> Thanks Tim, that explains most things.

Well, I thought to throw this out for fun:

>>> listSet = set(dir(list))
>>> tupleSet = set(dir(tuple))

>>> listSet - tupleSet
{'sort', 'insert', '__reversed__', 'reverse', 'extend', '__delitem__',
'remove',
 '__setitem__', '__iadd__', 'pop', 'append', '__imul__'}

>>> tupleSet - listSet
{'__getnewargs__'}


That would explain the major differences... =]

Cheers,
Xav
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HTMLgen???

2009-10-15 Thread eric_dex...@msn.com
On Oct 15, 2:58 am, an...@vandervlies.xs4all.nl wrote:
> Hi,
> Does HTMLgen (Robin Friedrich's) still exsist?? And, if so, where can it
> be found?
>
> --
>                Andre van der Vlies 
>                Certifiable Linux/UNIX engineer (CLUE)
>                Homepage:http://vandervlies.xs4all.nl/~andre
>                Books:http://www.lulu.com/andre14
> Key fingerprint = 397C 7479 67DB 9306 23DC B423 7B58 CD5A 6EFF 5CF8
> --
>     "Programming isn't a craft, it's an art."
>     ()  ascii ribbon campaign - against html e-mail
>     /\                        - against microsoft attachments
>                               ^[^#]
> --

http://gentoo-portage.com/dev-python/htmlgen

looks like it does I am not sure if this is the version you are after
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: segmentation fault

2009-10-15 Thread David Cournapeau
On Thu, Oct 15, 2009 at 6:14 PM, ankita dutta  wrote:

>   is dumped
> segmentation fault
>
>

It looks like you are using matplotlib, and matplotlib is the one
likely to segfault. You could check that it is indeed the case by just
commenting the part which does the plot - but I would be very
surprised if the lines before "lines=plt.plot(c1,exp,'ro')" are the
ones causing the segfault.

You should email the matplotlib user ML,

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


Re: segmentation fault

2009-10-15 Thread Chris Rebert
On Thu, Oct 15, 2009 at 2:14 AM, ankita dutta  wrote:
> hi,
> i am relatively new to python programming,
> i am facing the following problem:
>
> i am tring to simply  obtain data from a file ( "B.txt" ,  data in this file
> are in single column and floats)
> and plot a graph between those values and thier index.  ( for example if in
> file , if at 2nd position valuse is 9.34 ,  x=9.34 and y=1).
> .
> .
> .
> ln1=open("B.txt","r+")
> lines5=ln1.readlines()
> exp=[ ]
> c1=[ ]
> for i in range (0,len(lines1)):
>     f1=lines1[i].split()
>     c1.append(float(f1[0]))
>     exp.append(i)
> c1.sort(reverse=1)
>
> lines=plt.plot(c1,exp,'ro')
> plt.show()
> .
> .
> .
>
> but every time its showing the following error:
>
>   is dumped
> segmentation fault

The problem lies with whatever library you're using that provides
"plt" (or is named "plt"; your code sample is incomplete). It's coded
in C and is apparently crashing.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What command should be use when the testing of arguments is failed?

2009-10-15 Thread Jean-Michel Pichavant

Peng Yu wrote:

I have the following python code snippet. I'm wondering what command I
should use to terminate the program if the arguments are not right.

#!/usr/bin/env python

import sys
import os

if len(sys.argv) <= 1:
  print "usage:", os.path.basename(sys.argv[0]), ''
  return ## what command should be used here to terminate the program?
  

Any non zero value (usually -1) will do the trick thrown with a sys.exit;

By the way, if you are so worried about this kind of detail, you should 
check out http://docs.python.org/library/optparse.html.

Then you'll get easily a GNU/POSIX syntax for you program.

Yet if you don't want to use it, at least read the 16.4.2.8 section, 
where a common way to handle option/argument issues is described.


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


Re: segmentation fault

2009-10-15 Thread Krishnakant
On Thu, 2009-10-15 at 14:44 +0530, ankita dutta wrote:
> hi, 
> i am relatively new to python programming,
> i am facing the following problem:
> 
> i am tring to simply  obtain data from a file ( "B.txt" ,  data in
> this file are in single column and floats)
> and plot a graph between those values and thier index.  ( for example
> if in file , if at 2nd position valuse is 9.34 ,  x=9.34 and y=1).
> .
> .
> .
> ln1=open("B.txt","r+")
> lines5=ln1.readlines()
> exp=[ ]
> c1=[ ]
> for i in range (0,len(lines1)):
> f1=lines1[i].split()
> c1.append(float(f1[0])) 
> exp.append(i) 
> c1.sort(reverse=1)
> 
> lines=plt.plot(c1,exp,'ro')
> plt.show() 
> .
> .
> .
> 
> but every time its showing the following error:
> 
>   is dumped
> segmentation fault
> 
> 
> can anyone kindly help me out

Ankita,
Do you get same result on many machines?
If you can send the code to me off list I would check it for you.

happy hacking.
Krishnakant.



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


Re: efficient running median

2009-10-15 Thread Raymond Hettinger
[Janto Dreijer]
> I found a PDF by Soumya D. Mohanty entitled "Efficient Algorithm for
> computing a Running Median" (2003) by Googling. It has code snippets
> at the end, but it's not going to be a simple cut-and-paste job. It
> will take some work figuring out the missing parts.

See http://code.activestate.com/recipes/576930/ for working Python
code
adapted from that paper.


Raymond

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


Re: segmentation fault

2009-10-15 Thread ankita dutta
thanx david,

yes ,i am using matplotlib for plotting graph.
i am using this lines in my programme:

"import matplotlib.pyplot as plt"

now, if the problem with matplotlib ( and i will send them mail) ,
but can you kindly tell alternatively , how can i plot graph for my
programme.

ankita

On Thu, Oct 15, 2009 at 3:35 PM, David Cournapeau wrote:

> On Thu, Oct 15, 2009 at 6:14 PM, ankita dutta 
> wrote:
>
> >   is dumped
> > segmentation fault
> >
> >
>
> It looks like you are using matplotlib, and matplotlib is the one
> likely to segfault. You could check that it is indeed the case by just
> commenting the part which does the plot - but I would be very
> surprised if the lines before "lines=plt.plot(c1,exp,'ro')" are the
> ones causing the segfault.
>
> You should email the matplotlib user ML,
>
> David
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: segmentation fault

2009-10-15 Thread ankita dutta
hi chris,

i  am using matplotlib for plotting graph, as following:

"import matplotlib.pyplot as plt"

can you kindly tell me how to fix this problem(crashing) ?

On Thu, Oct 15, 2009 at 3:36 PM, Chris Rebert  wrote:

> On Thu, Oct 15, 2009 at 2:14 AM, ankita dutta 
> wrote:
> > hi,
> > i am relatively new to python programming,
> > i am facing the following problem:
> >
> > i am tring to simply  obtain data from a file ( "B.txt" ,  data in this
> file
> > are in single column and floats)
> > and plot a graph between those values and thier index.  ( for example if
> in
> > file , if at 2nd position valuse is 9.34 ,  x=9.34 and y=1).
> > .
> > .
> > .
> > ln1=open("B.txt","r+")
> > lines5=ln1.readlines()
> > exp=[ ]
> > c1=[ ]
> > for i in range (0,len(lines1)):
> > f1=lines1[i].split()
> > c1.append(float(f1[0]))
> > exp.append(i)
> > c1.sort(reverse=1)
> >
> > lines=plt.plot(c1,exp,'ro')
> > plt.show()
> > .
> > .
> > .
> >
> > but every time its showing the following error:
> >
> >   is dumped
> > segmentation fault
>
> The problem lies with whatever library you're using that provides
> "plt" (or is named "plt"; your code sample is incomplete). It's coded
> in C and is apparently crashing.
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>
-- 
http://mail.python.org/mailman/listinfo/python-list


set using alternative hash function?

2009-10-15 Thread Austin Bingham
If I understand things correctly, the set class uses hash()
universally to calculate hash values for its elements. Is there a
standard way to have set use a different function? Say I've got a
collection of objects with names. I'd like to create a set of these
objects where the hashing is done on these names. Using the __hash__
function seems inelegant because it means I have to settle on one type
of hashing for these objects all of the time, i.e. I can't create a
set of them based on a different uniqueness criteria later. I'd like
to create a set instance that uses, say, 'hash(x.name)' rather than
'hash(x)'.

Is this possible? Am I just thinking about this problem the wrong way?
Admittedly, I'm coming at this from a C++/STL perspective, so perhaps
I'm just missing the obvious. Thanks for any help on this.

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


Re: set using alternative hash function?

2009-10-15 Thread Chris Rebert
On Thu, Oct 15, 2009 at 4:24 AM, Austin Bingham
 wrote:
> If I understand things correctly, the set class uses hash()
> universally to calculate hash values for its elements. Is there a
> standard way to have set use a different function? Say I've got a
> collection of objects with names. I'd like to create a set of these
> objects where the hashing is done on these names. Using the __hash__
> function seems inelegant because it means I have to settle on one type
> of hashing for these objects all of the time, i.e. I can't create a
> set of them based on a different uniqueness criteria later. I'd like
> to create a set instance that uses, say, 'hash(x.name)' rather than
> 'hash(x)'.
>
> Is this possible? Am I just thinking about this problem the wrong way?
> Admittedly, I'm coming at this from a C++/STL perspective, so perhaps
> I'm just missing the obvious. Thanks for any help on this.

You could use wrapper objects that define an appropriate __hash__():

#*completely untested*
class HashWrapper(object):
def __init__(self, obj, criteria):
self._wrapee = obj
self._criteria = criteria

#override __hash__() / hash()
def __hash__(self):
return hash(self._criteria(self._wrapee))

#proxying code
def __getattr__(self, name):
return getattr(self._wrapee, name)

def __setattr__(self, name, val):
setattr(self._wrapee, name, val)

#example
def name_of(obj):
return obj.name

def name_and_serial_num(obj):
return obj.name, obj.serial_number

no_same_names = set(HashWrapper(obj, name_of) for obj in some_collection)
no_same_name_and_serial = set(HashWrapper(obj, name_and_serial_num)
for obj in some_collection)

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: segmentation fault

2009-10-15 Thread David Cournapeau
On Thu, Oct 15, 2009 at 7:46 PM, ankita dutta  wrote:
> thanx david,
>
> yes ,i am using matplotlib for plotting graph.
> i am using this lines in my programme:
>
> "import matplotlib.pyplot as plt"
>
> now, if the problem with matplotlib ( and i will send them mail) ,

The problem is how matplotlib was installed, not with matplotlib. I
use matplotlib every day, as many other people do, and it is certainly
not usual to crash on such simple plots.

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


Re: set using alternative hash function?

2009-10-15 Thread Duncan Booth
Austin Bingham  wrote:

> If I understand things correctly, the set class uses hash()
> universally to calculate hash values for its elements. Is there a
> standard way to have set use a different function? Say I've got a
> collection of objects with names. I'd like to create a set of these
> objects where the hashing is done on these names. Using the __hash__
> function seems inelegant because it means I have to settle on one type
> of hashing for these objects all of the time, i.e. I can't create a
> set of them based on a different uniqueness criteria later. I'd like
> to create a set instance that uses, say, 'hash(x.name)' rather than
> 'hash(x)'.
> 
> Is this possible? Am I just thinking about this problem the wrong way?
> Admittedly, I'm coming at this from a C++/STL perspective, so perhaps
> I'm just missing the obvious. Thanks for any help on this.
> 

It doesn't make sense to use just part of an object as the key for a set. A 
set is just a collection of values and there is no key separate from the 
value itself. If you build a set from x.name then that works fine, but only 
the names are stored.

What you want in this particular case is a dict: a dict stores both a key 
and a value and lets you retrieve the value from the key.


-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: set using alternative hash function?

2009-10-15 Thread Austin Bingham
That's definitely a workable solution, but it still rubs me the wrong
way. The uniqueness criteria of a set seems, to me, like a property of
the set, whereas the python model forces it onto each set element.

Another issue I have with the HashWrapper approach is its space
requirements. Logically, what I'm asking to do is switch out a single
function reference (i.e. to point at get_name() rather than hash()),
but in practice I'm forced to instantiate a new object for each of my
set members. On a large set, this could be disastrous.

Don't get me wrong...your solution is a good one, but it's just not
what I am looking for.

Austin

On Thu, Oct 15, 2009 at 1:36 PM, Chris Rebert  wrote:
> On Thu, Oct 15, 2009 at 4:24 AM, Austin Bingham
>  wrote:
>> If I understand things correctly, the set class uses hash()
>> universally to calculate hash values for its elements. Is there a
>> standard way to have set use a different function? Say I've got a
>> collection of objects with names. I'd like to create a set of these
>> objects where the hashing is done on these names. Using the __hash__
>> function seems inelegant because it means I have to settle on one type
>> of hashing for these objects all of the time, i.e. I can't create a
>> set of them based on a different uniqueness criteria later. I'd like
>> to create a set instance that uses, say, 'hash(x.name)' rather than
>> 'hash(x)'.
>>
>> Is this possible? Am I just thinking about this problem the wrong way?
>> Admittedly, I'm coming at this from a C++/STL perspective, so perhaps
>> I'm just missing the obvious. Thanks for any help on this.
>
> You could use wrapper objects that define an appropriate __hash__():
>
> #*completely untested*
> class HashWrapper(object):
>    def __init__(self, obj, criteria):
>        self._wrapee = obj
>        self._criteria = criteria
>
>    #override __hash__() / hash()
>    def __hash__(self):
>        return hash(self._criteria(self._wrapee))
>
>    #proxying code
>    def __getattr__(self, name):
>        return getattr(self._wrapee, name)
>
>    def __setattr__(self, name, val):
>        setattr(self._wrapee, name, val)
>
> #example
> def name_of(obj):
>    return obj.name
>
> def name_and_serial_num(obj):
>    return obj.name, obj.serial_number
>
> no_same_names = set(HashWrapper(obj, name_of) for obj in some_collection)
> no_same_name_and_serial = set(HashWrapper(obj, name_and_serial_num)
> for obj in some_collection)
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: set using alternative hash function?

2009-10-15 Thread Austin Bingham
I guess we see things differently. I think it's quite natural to want
a set of unique objects where "unique" is defined as an operation on
some subset/conflation/etc. of the attributes of the elements. That's
all that the regular set class is, except that it always uses the
hash() function to calculate uniqueness. In any event, the notions of
set and uniqueness I'm using are well established in other languages,
so I don't see why it couldn't be made to work in python.

As far as using a dict, that doesn't really solve my problem. It could
be part of a solution, I guess, but I would still need functionality
extrinsic to the dict. What I want is to make sure that no values in
my set have the same name, and dict won't guarantee that for me. A set
that calculated uniqueness based on its elements' names, on the other
hand, would.

Austin

On Thu, Oct 15, 2009 at 1:48 PM, Duncan Booth
 wrote:
> It doesn't make sense to use just part of an object as the key for a set. A
> set is just a collection of values and there is no key separate from the
> value itself. If you build a set from x.name then that works fine, but only
> the names are stored.
>
> What you want in this particular case is a dict: a dict stores both a key
> and a value and lets you retrieve the value from the key.
>
>
> --
> Duncan Booth http://kupuguy.blogspot.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: set using alternative hash function?

2009-10-15 Thread Diez B. Roggisch
Austin Bingham wrote:

> If I understand things correctly, the set class uses hash()
> universally to calculate hash values for its elements. Is there a
> standard way to have set use a different function? Say I've got a
> collection of objects with names. I'd like to create a set of these
> objects where the hashing is done on these names. Using the __hash__
> function seems inelegant because it means I have to settle on one type
> of hashing for these objects all of the time, i.e. I can't create a
> set of them based on a different uniqueness criteria later. I'd like
> to create a set instance that uses, say, 'hash(x.name)' rather than
> 'hash(x)'.
> 
> Is this possible? Am I just thinking about this problem the wrong way?
> Admittedly, I'm coming at this from a C++/STL perspective, so perhaps
> I'm just missing the obvious. Thanks for any help on this.


This is a principal problem in OO - behavior shall not only change based on
the object, but also the context it's used in.

I think the decorator-pattern is the best thing here (in lieu of python
having a hash-argument to sets). Please don't forget to re-define equality
also!

class OtherCriteriaDecorator(object):


   def __init__(self, delegate):
   self._delegate = delegate

   def __hash__(self):
   return hash_based_on_delegate


   def __eq__(self, other):
   return equality_based_on_delegate



HTH,

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


Re: segmentation fault

2009-10-15 Thread ankita dutta
hi,
well, even i was also using matplotlib for some time, and it was working
fine.
but this time i use it for data which is quite large,( my input file has
single column of float values ,
and length ( no. of rows) of this column is 10,000,000. )
may be because of size it might have crashed.

any way.. thanx all for advices it out.

ankita

On Thu, Oct 15, 2009 at 5:10 PM, David Cournapeau wrote:

> On Thu, Oct 15, 2009 at 7:46 PM, ankita dutta 
> wrote:
> > thanx david,
> >
> > yes ,i am using matplotlib for plotting graph.
> > i am using this lines in my programme:
> >
> > "import matplotlib.pyplot as plt"
> >
> > now, if the problem with matplotlib ( and i will send them mail) ,
>
> The problem is how matplotlib was installed, not with matplotlib. I
> use matplotlib every day, as many other people do, and it is certainly
> not usual to crash on such simple plots.
>
> David
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: segmentation fault

2009-10-15 Thread David Cournapeau
On Thu, Oct 15, 2009 at 9:08 PM, ankita dutta  wrote:
> hi,
> well, even i was also using matplotlib for some time, and it was working
> fine.
> but this time i use it for data which is quite large,( my input file has
> single column of float values ,
> and length ( no. of rows) of this column is 10,000,000. )

Even if the inpyt data are too big, if it crashes, it is a bug (in
matplotlib or in numpy or one of the underlying library). I would
advise you to report the bug on matplotlib ML with your platform
information (which version of matplotlib and numpy). The script looks
simple enough that it should be easy to reproduce by someone else.

You should keep in mind that 10 000 000 points is a lot to plot (I
don't think matplotlib can plot that many points - certainly, some
backends like Cairo will give up way below this number) - even without
the crash, plotting such a huge dataset as it is would be very slow
and almost certainly wasteful.

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


Re: set using alternative hash function?

2009-10-15 Thread Diez B. Roggisch
Chris Rebert wrote:

> On Thu, Oct 15, 2009 at 4:24 AM, Austin Bingham
>  wrote:
>> If I understand things correctly, the set class uses hash()
>> universally to calculate hash values for its elements. Is there a
>> standard way to have set use a different function? Say I've got a
>> collection of objects with names. I'd like to create a set of these
>> objects where the hashing is done on these names. Using the __hash__
>> function seems inelegant because it means I have to settle on one type
>> of hashing for these objects all of the time, i.e. I can't create a
>> set of them based on a different uniqueness criteria later. I'd like
>> to create a set instance that uses, say, 'hash(x.name)' rather than
>> 'hash(x)'.
>>
>> Is this possible? Am I just thinking about this problem the wrong way?
>> Admittedly, I'm coming at this from a C++/STL perspective, so perhaps
>> I'm just missing the obvious. Thanks for any help on this.
> 
> You could use wrapper objects that define an appropriate __hash__():
> 
> #*completely untested*
> class HashWrapper(object):
> def __init__(self, obj, criteria):
> self._wrapee = obj
> self._criteria = criteria
> 
> #override __hash__() / hash()
> def __hash__(self):
> return hash(self._criteria(self._wrapee))
> 
> #proxying code
> def __getattr__(self, name):
> return getattr(self._wrapee, name)
> 
> def __setattr__(self, name, val):
> setattr(self._wrapee, name, val)

This doesn't work for conflicting elements, as the __eq__-method isn't
overriden.

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


Re: set using alternative hash function?

2009-10-15 Thread Diez B. Roggisch
Austin Bingham wrote:

> That's definitely a workable solution, but it still rubs me the wrong
> way. The uniqueness criteria of a set seems, to me, like a property of
> the set, whereas the python model forces it onto each set element.

This is a POV, but to to me, the set just deals with a very minimal
protocol - hash-value & equality. Whatever you feed it, it has to cope with
that. It strikes *me* as odd to ask for something else. 

The ideal solution would be to have several hash/eq-methods on your object,
and somehow make the surroundings decide which to use. But there is no
OO-pragma for that.

> Another issue I have with the HashWrapper approach is its space
> requirements. Logically, what I'm asking to do is switch out a single
> function reference (i.e. to point at get_name() rather than hash()),
> but in practice I'm forced to instantiate a new object for each of my
> set members. On a large set, this could be disastrous.
> 
> Don't get me wrong...your solution is a good one, but it's just not
> what I am looking for.

It is the only one you have, short of implementing set your own. And then
the terms of implementation would be that you pass a hash- and eq-function
& create wrapper-objects internally.

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


Python 2.6.3 and finding init.tcl

2009-10-15 Thread Shawn Wheatley
I'm trying to troubleshoot a bug in VirtualEnv, but in order to do so
I need to better understand how Python initializes Tkinter.

Setup:
Python 2.6.3 on Windows 7 & Windows XP SP3

Problem:
There is a file called init.tcl that gets loaded when first executing
a Tkinter statement. The file is held in the root of the Python folder
in another folder called "tcl"

C:\Python26\tcl\init.tcl

VirtualEnv does not copy this folder; instead it adds the core libs
folders to the Python path. When running Python from a VirtualEnv,
Tkinter statements fail stating that the init.tcl file can't be found.
The strange thing is that the location of the init.tcl file is not
listed in the expected list of locations. As an experiment, I renamed
the "tcl" folder in C:\Python26 to "tcl2" and tried to run
Tkinter._test(). Here is the results:

Python 2.6.3 (r263rc1:75186, Oct  2 2009, 20:40:30) [MSC v.1500 32 bit
(Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> Tkinter._test()
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 3749, in _test
root = Tk()
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 1643, in __init__
self.tk = _tkinter.create(screenName, baseName, className,
interactive, want
objects, useTk, sync, use)
_tkinter.TclError: Can't find a usable init.tcl in the following
directories:
C:/Python26/lib/tcl8.5 C:/lib/tcl8.5 C:/lib/tcl8.5 C:/library C:/
library C:/
tcl8.5.2/library C:/tcl8.5.2/library



This probably means that Tcl wasn't installed properly.

>>>

Note that "C:/Python26/tcl" is not in this list. Renaming "tcl2" back
to "tcl" resolves the problem, but it still doesn't explain to me why
Python is looking in that location in the first place. Can anyone shed
some light on how Python/Tkinter initialization works?

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


Re: id( ) function question

2009-10-15 Thread Mel
Erik Max Francis wrote:
> Tim Chase wrote:
>> In general, if you're using "is" (and not comparing with None) or id(),
>> you're doing it wrong unless you already know the peculiarities of
>> Python's identity implementations.

> Right.  Another way to do look at it is that if you're curious about
> what the value of `id` is or how the `is` operator works, the short
> version is, don't worry about them, as you won't be using them.

As Python has evolved the semantics have got richer, and the implementation 
has got trickier with proxy objects and wrapped functions and more.  
Whatever use there was for `is` in ordinary code is vanishing.

Even a straight-up assignment can fool:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A(object):
...   def __init__ (self):
... self.mangler = self.method1
...   def method1 (self):
... return id (self) ^ 30103
...   def is_mangled (self):
... return self.mangler is self.method1
... 
>>> a=A()
>>> a.is_mangled()
False

I fully understand that there's an excellent reason for that -- I wouldn't 
have it any other way.  Also note that, in the same example

>>> a.mangler == a.method1
True


My poster-child use of `is` is a MUDD game where 

`reference1_to_player is reference2_to_player` 

, if True, means that both refer to the same in-game player.  Even that 
might not last.

> I'm really rather surprised at the number of questions about them.
> They're really something one does not need to worry about.

Particularly to the newbie, `is` and `id` look like they ought to be good 
for *something*.  You have to know a lot about Python internals to know how 
little they mean.

Maybe the docs should have some kind of "Debugging and Introspection" 
dungeon that they could be thrown into.


Mel.

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


reading joystick data

2009-10-15 Thread Ronn Ross
Hello,

I'm using PyUSB 0.4.2 to interact with a Microsoft Sindwinder joystick. I'm
using python 2.6 on windows XP. With the code snippet below I'm able to find
usb device currently plugged into my computer. Now I would like to actually
tap into the data that to joystick is providing to the system. For now I
just want to watch the data and see what it looks like as I move the
joystick around. Does anyone know a good tutorial, or can point me in the
right direction?

Script:
import usb
for bus in usb.busses():
for dev in bus.devices:
print "Bus %s Device %s: ID %04x:%04x %s" % (bus.dirname,
dev.filename, dev.idVendor, dev.idProduct, dev.open().getString(1,30))




Result:
Bus bus-0 Device \\.\libusb0-0005--0x04f2-0xb071: ID 04f2:b071 CNF7129
Bus bus-0 Device \\.\libusb0-0006--0x045e-0x0038: ID 045e:0038 Microsoft
-- 
http://mail.python.org/mailman/listinfo/python-list


set using alternative hash function?

2009-10-15 Thread Austin Bingham
On Thu, Oct 15, 2009 at 2:23 PM, Diez B. Roggisch  wrote:
> Austin Bingham wrote:
> This is a POV, but to to me, the set just deals with a very minimal
> protocol - hash-value & equality. Whatever you feed it, it has to cope with
> that. It strikes *me* as odd to ask for something else.

But I'm not really asking for anything that changes the paradigm of
how things currently work. All I need is the ability to say something
like this:

 s = set(hash_func = lambda x: hash(x.name))

If set could be de-hardcoded from using hash(), nothing else about how
it works would have to change. Or am I wrong on that? I see that you
mention equality in the protocol, but I don't know why a set would
need that if a) hash(x) == hash(y) --> x == y and b) hash function
must return a 32 bit value (which I think is the case)...so maybe I
misunderstand something.

I wonder...does the requirement of using hash() have to do with the
low-level implementation of set? That might explain the state of
things.

> The ideal solution would be to have several hash/eq-methods on your object,
> and somehow make the surroundings decide which to use. But there is no
> OO-pragma for that.

But why force objects to know about all of the wacky contexts in which
they might be used? Let objects be objects and hash-functions be
hash-functions. If someone wants a set where uniqueness is defined by
the number of occurrences of 'a' in an object's name, the object
should never have to know that...it should just have a name.

In any event, it sounds like set doesn't have any notion of switching
out its hash function, and that more or less answers my question.

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


Re: set using alternative hash function?

2009-10-15 Thread Diez B. Roggisch
Austin Bingham wrote:

> On Thu, Oct 15, 2009 at 2:23 PM, Diez B. Roggisch 
> wrote:
>> Austin Bingham wrote:
>> This is a POV, but to to me, the set just deals with a very minimal
>> protocol - hash-value & equality. Whatever you feed it, it has to cope
>> with that. It strikes *me* as odd to ask for something else.
> 
> But I'm not really asking for anything that changes the paradigm of
> how things currently work. All I need is the ability to say something
> like this:
> 
> s = set(hash_func = lambda x: hash(x.name))
> 
> If set could be de-hardcoded from using hash(), nothing else about how
> it works would have to change. Or am I wrong on that? I see that you
> mention equality in the protocol, but I don't know why a set would
> need that if a) hash(x) == hash(y) --> x == y and b) hash function
> must return a 32 bit value (which I think is the case)...so maybe I
> misunderstand something.

You do. Hashes can collide, and then you need equality. Sets are *based* on
equality actually, the hash is just one optimization. Other optimizations
build on order (usually, the STL requires you to define < on objects for
that. Which is enough because a == b <=> a < b && b < a). But eventually,
it all boils down to equality. You could implement a set without hash, by
imposing linear behavior on insert and lookup - but none based purely on a
hash.

> 
> I wonder...does the requirement of using hash() have to do with the
> low-level implementation of set? That might explain the state of
> things.
> 
>> The ideal solution would be to have several hash/eq-methods on your
>> object, and somehow make the surroundings decide which to use. But there
>> is no OO-pragma for that.
> 
> But why force objects to know about all of the wacky contexts in which
> they might be used? Let objects be objects and hash-functions be
> hash-functions. If someone wants a set where uniqueness is defined by
> the number of occurrences of 'a' in an object's name, the object
> should never have to know that...it should just have a name.

Because these functions need intimate knowledge of the objects to actually
work. Sure, in python it's easy to define them outside, and just reach into
the object as you wish. But aside this implementation detail, a hash (and
equality of course) always are based upon the object in question. So I
think it's natural to have it defined right there (which __hash__ and
__eq__ show that this seems to be accepted in general).

And if there were something that would decide on context which of several
implementations to use, you'd have less to worry. As things are, there
isn't such thing (I don't even have the slightest idea what could work),
you are as well off with defining two functions.


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


Re: set using alternative hash function?

2009-10-15 Thread Austin Bingham
On Thu, Oct 15, 2009 at 3:02 PM, Diez B. Roggisch  wrote:
> Austin Bingham wrote:
> You do. Hashes can collide, and then you need equality. Sets are *based* on
> equality actually, the hash is just one optimization. ...

Right, thanks for clearing that up. Not reading closely enough ==
public shaming ;)

> Because these functions need intimate knowledge of the objects to actually
> work. Sure, in python it's easy to define them outside, and just reach into
> the object as you wish. But aside this implementation detail, a hash (and
> equality of course) always are based upon the object in question. So I
> think it's natural to have it defined right there (which __hash__ and
> __eq__ show that this seems to be accepted in general).
>
> And if there were something that would decide on context which of several
> implementations to use, you'd have less to worry. As things are, there
> isn't such thing (I don't even have the slightest idea what could work),
> you are as well off with defining two functions.

But this "context decider" you're talking about sounds exactly like
what I'm talking about.  If I had some class with __hash1__ and
__hash2__ (and associated equality operators), you're saying it would
be nice to be able to select which to use based on the context (e.g.
what type of set I'm using.) It might look like this:

   s = set(hash_func = lambda x: x.__hash2__, eq_func = lambda x, y:
x.__eq2__(y))

And if *that* works for you, do you still have a problem with:

  s = set(hash_func = lambda x: hash(x.name), eq_func = lambda x,y:
x.name == y.name)

?

If you don't like those, what would work for you? As far as I can
tell, your "context decider" and my "alternative hash functions" are
the same thing, and we just need to agree on a syntax.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Clear interface for mail class

2009-10-15 Thread Benedict Verheyen
Francesco Bochicchio wrote:

> 
> I would add a server class, maybe subclassing something in standard
> library, and add to it the 'send' method, so that sending a mail would
> be
> something like:
> 
> myserver = MyMailServer("mysmtpserver", "localhost", ) # this only
> needs to be done once, not for each mail
> 
> m = MyMail( subject, text, separate_emails = False, files=[],
> inline_files=[] ) # mail creation
> 
> myserver.send( m, from= "from...@work", # mail sending
>to = "t...@work",
>cc_to= None  )
> 
> Note that I put sender and destination senders in the send method, not
> as attributes  of the mail object. It makes more sense to me, and yopu
> can reuse
> the same object if you want to send the same mail to many addresses
> ( and/or via different servers ).
> 
> IN general, un case like yours I use a lot default parameters, as you
> did already. Having separate methods to setting specific part of an
> object only makes
> sens (to me) if you need  first to create an object and later change
> some of the attributes. Also, if you are just going to change the
> attributes, you do not
> need a method: just use the object.attribute = value syntax. If you
> are going to need later to do more complex thing, you can always
> transform your
> attribute in a property.


Thanks for the input guys

Cheers,
Benedict

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


Re: set using alternative hash function?

2009-10-15 Thread Diez B. Roggisch
>> And if there were something that would decide on context which of several
>> implementations to use, you'd have less to worry. As things are, there
>> isn't such thing (I don't even have the slightest idea what could work),
>> you are as well off with defining two functions.
> 
> But this "context decider" you're talking about sounds exactly like
> what I'm talking about.  If I had some class with __hash1__ and
> __hash2__ (and associated equality operators), you're saying it would
> be nice to be able to select which to use based on the context (e.g.
> what type of set I'm using.) It might look like this:
> 
>s = set(hash_func = lambda x: x.__hash2__, eq_func = lambda x, y:
> x.__eq2__(y))
> 
> And if *that* works for you, do you still have a problem with:
> 
>   s = set(hash_func = lambda x: hash(x.name), eq_func = lambda x,y:
> x.name == y.name)
> 
> ?
> 
> If you don't like those, what would work for you? As far as I can
> tell, your "context decider" and my "alternative hash functions" are
> the same thing, and we just need to agree on a syntax.

The context-decider isn't the same thing because it isn't designed yet :)
And most probably won't ever be. It's just the abstract idea that
hashing/equality change for one object depending on the circumstances they
are used in, and that one wishes that the decision what to use is as simple
& transparent as possible. 

Your approach is certainly viable, but I guess the current
set-implementation is optimized on working with __hash__ and __eq__ on
objects because for these exist slots in the python object structure in C.
So while you can implement your idea, you will end up passing wrappers as
Chris & me proposed into the current set implementation. 

However, it might be worth thinking of proposing this change to the set-type
in general. But then for orthogonality, dicts should have it as well I
guess. Which opens a whole new can of worms.

I'm undecided on this. I think the cure is straight forward & simple to
implement, and once you have the level to understand & need different
hashing/equality, you should be comfortable writing this simple wrapper
yourself. So for me personally, it's not a needed addition to the language.
YMMV, and thus - go for it if you like.

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


Re: set using alternative hash function?

2009-10-15 Thread Mick Krippendorf
Austin Bingham schrieb:
> I guess we see things differently. I think it's quite natural to want
> a set of unique objects where "unique" is defined as an operation on
> some subset/conflation/etc. of the attributes of the elements. 

What you seem to imply is that the hash function imposes some kind of
uniqueness constraint on the set which uses it. That's just not the
case, the uniqueness constraint is always the (in-)equality of objects,
and for this you can override __eq__. But then you must also ensure that
x.__eq__(y) --> y.__eq__(x) & x.__hash() == y.__hash__().

Diez' solution is the pythonic way, and BTW, mathematically and
pythonically sound, wheras, if the hash function would really impose
uniqueness:

s1 = set(lambda x: hash(x))
s2 = set(lambda x: hash(x.name))

t1 = Buddy(name="jim")
t2 = Buddy(name="joe")
t3 = Buddy(name="joe")

s1.add(t1)
s1.add(t2)
s1.add(t3)

s2.add(t1)
s2.add(t2)
s2.add(t3)

would probaly yield quite strange results:

s3 = s2 | s1 # union. Is s3 == (t1,t2,t3) or is it (t1,t3)?
s4 = s2 - s1 # difference. is s4 == () or do we get an Exception?

In C++/STL the compiler would nag because s1 and s2 are of different
types since the hash function is part of the set's type (AFAIR). With
dynamic typing this isn't possible so you'd have to wait until runtime.

I'd go with Diez' advice and use a dict instead. Or, you could do
something Borg-ish (lookup Borg Pattern) restrained on names, where the
constructor of a class ensures that x.name == y.name --> x == y.

> As far as using a dict, that doesn't really solve my problem. It could
> be part of a solution, I guess, but I would still need functionality
> extrinsic to the dict.

Python isn't strong on encapsulation, so "extrinsic" functionality is
quite common. Nothing to loose sleep over though, because, as Guido
says, "we're all consenting adults here" :-)

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


Re: set using alternative hash function?

2009-10-15 Thread Austin Bingham
On Thu, Oct 15, 2009 at 3:43 PM, Diez B. Roggisch  wrote:
> The context-decider isn't the same thing because it isn't designed yet :)
> And most probably won't ever be. It's just the abstract idea that
> hashing/equality change for one object depending on the circumstances they
> are used in, and that one wishes that the decision what to use is as simple
> & transparent as possible.

Fair enough :)

> Your approach is certainly viable, but I guess the current
> set-implementation is optimized on working with __hash__ and __eq__ on
> objects because for these exist slots in the python object structure in C.
> So while you can implement your idea, you will end up passing wrappers as
> Chris & me proposed into the current set implementation.

Yes, I figured that the guts of set relied on particulars to which we
are not privy at the python level. If the syntax let me describe sets
the way I've been laying out here, I could probably tolerate the
underlying implementation relying on wrappers.

> However, it might be worth thinking of proposing this change to the set-type
> in general. But then for orthogonality, dicts should have it as well I
> guess. Which opens a whole new can of worms.

dicts would certainly have to be looked at as well, but I don't think
the can would have that many worms in it if we solve the set issue to
everyone's satisfaction.

In any event, thanks for helping me work through this issue.

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


Re: set using alternative hash function?

2009-10-15 Thread Anthony Tolle
On Oct 15, 7:24 am, Austin Bingham  wrote:
> [snip] I'd like to create a set of these
> objects where the hashing is done on these names. [snip]

Why not use a dict?  The key would be the object name.  Pretty much
the same behavior as a set (via the key), and you can still easily
iterate over the objects.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: set using alternative hash function?

2009-10-15 Thread Austin Bingham
On Thu, Oct 15, 2009 at 3:50 PM, Mick Krippendorf  wrote:
> Austin Bingham schrieb:
> What you seem to imply is that the hash function imposes some kind of
> uniqueness constraint on the set which uses it. That's just not the
> case, the uniqueness constraint is always the (in-)equality of objects,
> and for this you can override __eq__. But then you must also ensure that
> x.__eq__(y) --> y.__eq__(x) & x.__hash() == y.__hash__().

Yes, as was pointed out earlier, I was reading too much into the hash
function's role. However, given well behaved hash and equality (which
would be a great name for a band), I don't see why those functions
need to be defined on the object itself, per se. Right now that's the
case because hash() only knows how to call obj.__hash__ (etc. for
__eq__).

I guess a good analog for what I'm thinking about is list.sort(). It's
more than happy to take a comparison operator, and in spirit that's
exactly what I'm looking for with sets.

> Diez' solution is the pythonic way, and BTW, mathematically and
> pythonically sound, wheras, if the hash function would really impose
> uniqueness: . . .

Yes, my gray c++ roots are showing here; you're right that my brain
was secretly expecting the "compiler" to take care of things. Your
points about set operations are the strongest in this discussion, and
I haven't got a great answer.

> Python isn't strong on encapsulation, so "extrinsic" functionality is
> quite common.

I guess that's part of what's frustrating for me on this issue. Python
is generally extremely flexible and open, but in this case I feel like
I'm being shut out. Oh well...in the end it looks like there are ways
to get what I need, if not what I want. Thanks for the input.

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


Re: set using alternative hash function?

2009-10-15 Thread Austin Bingham
On Thu, Oct 15, 2009 at 4:06 PM, Anthony Tolle  wrote:
> Why not use a dict?  The key would be the object name.  Pretty much
> the same behavior as a set (via the key), and you can still easily
> iterate over the objects.

To reiterate, dict only gets me part of what I want. Whereas a set
with uniqueness defined over 'obj.name' would guarantee no name
collisions, dict only sorta helps me keep things straight; it doesn't
actually enforce that my values have unique names.

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


Re: HTMLgen???

2009-10-15 Thread Gabriel Genellina

En Thu, 15 Oct 2009 05:58:02 -0300,  escribió:


Does HTMLgen (Robin Friedrich's) still exsist?? And, if so, where can it
be found?


Would you consider using HyperText? It's inspired on HTMLGen but I like  
its design much more (that said, currently I prefer to use a templating  
engine instead of generating HTML by code).


Used to be at http://dustman.net/andy/python/HyperText but no more :(
There is a trimmed down version here:  
http://www.astro.umass.edu/~dpopowich/python/mpservlets/ (see _HTML.py  
inside the tutorial)


--
Gabriel Genellina

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


Re: set using alternative hash function?

2009-10-15 Thread Anthony Tolle
On Oct 15, 10:42 am, Austin Bingham  wrote:
> On Thu, Oct 15, 2009 at 4:06 PM, Anthony Tolle > To reiterate, dict only gets 
> me part of what I want. Whereas a set
> with uniqueness defined over 'obj.name' would guarantee no name
> collisions, dict only sorta helps me keep things straight; it doesn't
> actually enforce that my values have unique names.
>

I don't understand how a set would help you enforce that your values
ave unique names.  If you have two objects, both named "foo", and
added them to the set, no errors would occur.

Please provide an example of how a set provides functionality that a
dict (which enforces unique keys) doesn't.

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


Raw_input with readline in a daemon thread makes terminal text disappear

2009-10-15 Thread John O'Hagan
I'm getting input for a program while it's running by using raw_input in a 
loop in separate thread. This works except for the inconvenience of not having 
a command history or the use of backspace etc. 

That can be solved by loading the readline module; however, it results in a 
loss of visible access to the terminal when the program ends: nothing is 
echoed to the screen and the history is invisible (although it is there - 
hitting return executes whatever should be there normally). The only way to 
get it back is to close the terminal and open a new one.

Here is minimal code that reproduces the problem (python 2.5 on Linux): 

from threading import Thread
import readline

get_input = Thread(target=raw_input)
get_input.setDaemon(True)
get_input.start()

If the thread is not set to daemon mode, there is no such problem (don't know 
why this makes a difference), but in the real program, it needs to be a daemon 
or it hangs the exit waiting for more input.

Any suggestions appreciated.

Thanks,

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


Re: set using alternative hash function?

2009-10-15 Thread Chris Rebert
On Thu, Oct 15, 2009 at 5:22 AM, Diez B. Roggisch  wrote:
> Chris Rebert wrote:
>
>> On Thu, Oct 15, 2009 at 4:24 AM, Austin Bingham
>>  wrote:
>>> If I understand things correctly, the set class uses hash()
>>> universally to calculate hash values for its elements. Is there a
>>> standard way to have set use a different function? Say I've got a
>>> collection of objects with names. I'd like to create a set of these
>>> objects where the hashing is done on these names. Using the __hash__
>>> function seems inelegant because it means I have to settle on one type
>>> of hashing for these objects all of the time, i.e. I can't create a
>>> set of them based on a different uniqueness criteria later. I'd like
>>> to create a set instance that uses, say, 'hash(x.name)' rather than
>>> 'hash(x)'.
>>>
>>> Is this possible? Am I just thinking about this problem the wrong way?
>>> Admittedly, I'm coming at this from a C++/STL perspective, so perhaps
>>> I'm just missing the obvious. Thanks for any help on this.
>>
>> You could use wrapper objects that define an appropriate __hash__():
>>
>> #*completely untested*
>> class HashWrapper(object):
>>     def __init__(self, obj, criteria):
>>         self._wrapee = obj
>>         self._criteria = criteria
>>
>>     #override __hash__() / hash()
>>     def __hash__(self):
>>         return hash(self._criteria(self._wrapee))
>>
>>     #proxying code
>>     def __getattr__(self, name):
>>         return getattr(self._wrapee, name)
>>
>>     def __setattr__(self, name, val):
>>         setattr(self._wrapee, name, val)
>
> This doesn't work for conflicting elements, as the __eq__-method isn't
> overriden.

Indeed. Good catch. :)
This is why I mark code as "completely untested".

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


How to organize code for conversion between different classes? (avoiding cyclic dependencies)

2009-10-15 Thread Peng Yu
Suppose I have classes 'A',  'B',  'C',  'D'. The definition of these
classes are long enough so that I have to put each class in a separate
module 'mA', 'mB', 'mC', 'mD', which are in packages 'pA', 'pB', 'pC',
'pD', respectively. And there were no need to have conversion
functions between these classes.

As my program evolves, I need to have a conversion function between
any pair of classes. I could define the conversion functions as member
methods like the following. But if I do this for the classes B, C, D,
I would end up with cyclic dependencies, which should be avoid.


import pB.mB
import pC.mC
import pD.mD

class A:
  ...
  def convert_to_B():
 

  def convert_to_C():
 

  def convert_to_D():
 


Another choice is that I can put the conversion functions in some
modules and import 'mA', 'mB', 'mC', 'mD' when needed. However, since
mA, mB, mC, mD are in different packages, I'm not sure where to put
the new modules that have the conversion functions.

I'm wondering how this case shall be handled. Could somebody give some hint?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HTMLgen???

2009-10-15 Thread tinnews
Gabriel Genellina  wrote:
> En Thu, 15 Oct 2009 05:58:02 -0300,  escribió:
> 
> > Does HTMLgen (Robin Friedrich's) still exsist?? And, if so, where can it
> > be found?
> 
> Would you consider using HyperText? It's inspired on HTMLGen but I like  
> its design much more (that said, currently I prefer to use a templating  
> engine instead of generating HTML by code).
> 
> Used to be at http://dustman.net/andy/python/HyperText but no more :(
> There is a trimmed down version here:  
> http://www.astro.umass.edu/~dpopowich/python/mpservlets/ (see _HTML.py  
> inside the tutorial)
> 
Or Docutils of course, a complete (python) documentation
generating system which includes, among other things, rst2html.py.

See: http://docutils.sourceforge.net

-- 
Chris Green

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


Re: set using alternative hash function?

2009-10-15 Thread Gabriel Genellina
En Thu, 15 Oct 2009 11:42:20 -0300, Austin Bingham  
 escribió:


On Thu, Oct 15, 2009 at 4:06 PM, Anthony Tolle   
wrote:

Why not use a dict?  The key would be the object name.  Pretty much
the same behavior as a set (via the key), and you can still easily
iterate over the objects.


To reiterate, dict only gets me part of what I want. Whereas a set
with uniqueness defined over 'obj.name' would guarantee no name
collisions, dict only sorta helps me keep things straight; it doesn't
actually enforce that my values have unique names.


I think you didn't understand correctly Anthony Tolle's suggestion:

py> class Foo:
...   def __init__(self, name): self.name = name
...
py> objs = [Foo('Joe'), Foo('Jim'), Foo('Tom'), Foo('Jim')]
py> objs
[<__main__.Foo instance at 0x00BB8238>,
 <__main__.Foo instance at 0x00BB83C8>,
 <__main__.Foo instance at 0x00BB7B20>,
 <__main__.Foo instance at 0x00BB7DC8>]
py> d = dict((o.name, o) for o in objs)
py> d.keys()
['Jim', 'Joe', 'Tom']
py> d.values()
[<__main__.Foo instance at 0x00BB7DC8>,
 <__main__.Foo instance at 0x00BB8238>,
 <__main__.Foo instance at 0x00BB7B20>]

keys in a dict are unique; values form the set you're looking for. If it's  
more complex that just a name, define a function:


def my_funny_key_extractor(item):
  return some(operations[on](item))

d = dict((my_funny_key_extractor(o), o) for o in objs)

If you feel so inclined, you could implement the MutableSet ABC based on  
this and have a full featured set implementation.


--
Gabriel Genellina

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


Yet Another Pic Problem

2009-10-15 Thread Victor Subervi
Hi;
My code was working fine then I must have inadvertently screwed something
up. The result is a mysql insert/update statement that looks something like
the following:

update productsX set Name=%s, Title=%s, Description=%s, Price=%s,
Bedrooms=%s, Bathrooms=%s, Conditions=%s, Acreage=%s, Construction=%s,
Location=%s, Estate=%s, Address=%s, Furnished=%s, pic1=%s, pic2=%s, pic3=%s,
pic4=%s, pic5=%s, pic6=%s where ID=%s;', ('name1', 'title1', 'descr1',
'1.1', '2', '1', 'New', '1.5', 'New', 'arbor', 'abor', 'abor', 'Furnished',
'ÿØÿàJFIFÿÛC     ""
$(4,$&1'-=-157:::#+?D?8C49:7ÿÛC
7%%77ÿÀy|"ÿÄÿÄM
 !1AQa"qs³27Bbc‚#46Rrt‘¡±²$'3C’%5DTUƒ“¢£ÑñÿÄÿÄÿÚ
?œ*‹RP’¥¨%
d’pªÔcµkä«”øZÂáîjXêxö,...@$ýô¨2mÒžÚ¦ÚrÑ¥¬ògud|!%bO:ðç’Z…æµËÚvsM¡·¢&T‘œ¬ºâ“ñâ1Ò¶¶KT[%¦-²{‘£6Ô÷“âNI=äÒfÓø/RhËîR”³q1QüÇ“ƒŸƒEŠŠ(
(¢ŠŠ( (¢ŠŠ( ×ê ´{–mÒYü VTâ†pUŽI$àQvÃ
ɼÜ/ZÒì{IrÝ,´£Èxß#¸$Š·÷F_Ëë}……áRTdHóÁ#ÐU“ö*DÙõ¨Yt]žîêÑ+p}5ùêÿ¹F†vã
Êöy5Ô‚\ˆëO Žc 'ö(Óõku-´^4õÊÚÚ£8Ðð%$ì84Ú*øG¥­·TVû#µc
æ·uÏ;Ö"Óusn\\Ü‹5{Ñʸvoò�...@é¼×cpu¤¡%kpjr2i8t+´½¨®{ß{:)kzD‡;&2~1'¤“ïÙßa¶Õº®íª5Ñúaimçá’o’8•tÆphânÇuºl¤Ûmë¸é«ÝÙøéíû’7�...@gucqÓŠhÙ¶cfØÍ\çðäLJÎ^8$}äí=i²aj©ôœ
«‰J_u/¥<ƒ‰%*áÓˆÎ;ˆ¦ TÐö©6)WûjØZ`ü
dÂsæ–Ýhq...@m5ÐqeebÜîíp]›q’Üx̧yn8pÿ߇3ip5–¨¿´©úgl2å©k)aéÒû¼5ã‚s8žTæÕæ*ýµ)Q’­ä4óp›¦0?ÌU]Br+ç\»]_"韍pTŒŸXU]
Èò ­“¬gK®tTxò^i‰É4‡ Rè ¤à8Y«›`üM]ߊ󌼀ÑK,¥Iü29ƃž6™iU‡]Ý£ 
È/²G5~xÇ£8öT۱͡§SÀ›«¿éˆÈà¥Æ[;ô‡Qן~4»vÒOܬðu”ëÐÙ Ë
%®ad“Ÿg¨Ãe1.õõœÛæû2ëËG$4>9' )$xçh3ö‰´Ë®­ôHî.%œ,†ã
ຐxSׇŽ3NûÐËAûêº5Œ‚ˆ ¨qÁৈÓÜh±ìËOÏÚV¢‰
>`ÛTíÅß«.¤¨¥Gžè<€ãŽg½ãP<ìM£h¸1[ÌÄ®;J)mA- ÐR8:g•ãXjû>‚™W‡Ô
„†Xho8é÷G‡yÀâ8ñ‡¤õ5ëQ¥‰Ÿ{žCjxo7"LÑÚ­à
ØG#Ú‡‘ž¨t–›-LjFžhÈÂXy¥(…\8)ŽJð}-ÜKp¬’T–PÔuœî€“Œ”ôTc¢ö…§íZÒ«õùµMìh‚µ<öwŽ7€É1Î’šÛ2­7
ó";=&b*Sê eÆ7xž|p
è:@Ö[YÓÚq.1Ñs¸'€b2BOÓ_!è>¡¦²Özòbm©}÷ƒêÂ`ÂFâ¤$õŠ•vq±è–bÍÏR†æ\ˆÃÎiƒãùê°tÏA‡§4Þ
Ú<ÆoÚíkfÐ…õ¤%ÁÑE<Â|oœ¯ŒÌm4Û-!¦”6€”$...@‚½t¢ƒˆ_bš}Æ×ä(¥Yçk®vqyM÷DÚf…ï9äéiîþÑj³é#>‚+™ö‘lU§]^¢)
'Ê”êüÅùéýÊñ÷?jÔÛ®¯iÙ®nÇœ­øÅg‚^�...@j@ëA#럔ú̯všÈÛGɝë†|Ö½ò*θÇßö„â3å2º}ZjþÙ¾M/_
ß½Et_Ä™õiþ°ÖZogV÷m q޾´¤¼:°2zð律¿‰³êÓü)/bC7¶Ë´ß.€Ò
ÿJ:ÛÕÀ÷J«ZÍái:9ò•(5ಔŽ' ƒŠ½¥‡ö¡­x‡Ž>©UMT3µ-×ðSýÐ 5†Ñ5«.³6YfÕ‘
ºØ‚ꬽԱ"T‰Kß’û¯+½Å•ßSVÕö[c´YnŠÔãñ”…¡F
š󖔝ީvq“ÝÀVÝÓp!?"D«„·ajkJ¼xà óñ çÆvC¨e†Öë‹8J’¢£à:~Ùö˦ê‰Ó¹H6æ
8–ä6¤öñÝð:ž]ƦMZíñ´%®lxqÛ”ûJ/>†ÀZüõsW3Ê«³ÒF©Öé<…Ñ'ö¢ƒki[6•…äÖhik{£ªóœt÷©\Ï£ènv´¡%...@ÉrŽ«sfgÙs^c1Ùi[Ž,à$¦¢ØÓåm_Pöl¡Ö4m¹à§wI¸:8„Ÿ£Èîôø‘ºÍt¢‚û¢t–˜š–*2ix†r…i)ÏèÔË®0òejmÆÔ…¤à¤Ž
ƒß]«rëE¾sIz4†Ën¶z¤ÿMr¶Ñtýr)XSö×T|šVïôUÜ¡ûù‘lÚÑ:Æû ]x¡7Ò¤·1±Ù
,C>ÐGJzÛÎÍoCêÛ<¾µl‡åÇë•üŠ©ûlc;6½z¶ýê(bþ&Ï«O𤽉üœ[}cþùtç b
‡ÓËÐ)7bß'vÿ[#ß.€ÒÃûQÖ¼?ÇVªóªømOCäsn~?é ®™9Úž²çýÄŸVkΫùTÐÞ®¹
÷¶¿“k¯¥Ÿ|Šm›Ÿ‚_#ýÝXéóiOm_&·ù>ùËw—†Có$µ ÁÇVJxq4
û9ÙµýRýâézϪ-V÷®fÞ%%¤ü$žÍ´ñqÓ¹É)êpêE$Z6·÷¹
íÖK4NÖâÓKH|a¶‰ZˆÂ~qÁëéåKÚ;J^v“¨dJ’ú».Ð9:jÀញTqÀr
^[¨öÏ}òV·íÚr2œJx„þ‘ùý¤Î–[L+²=¶ØÂYŠÂwP‘ûÉ=I™­ŒÍ!aÓ»IÑŒZ-­0ÓÞZ§BŠœß)g)'xžGˆ©1‹d(÷7c6‰’’„¾ðrÂF
Ud[aIsñ›rTMÿ'uCÎo|aXôŽ [iù5¼qèÏ·ðȦ¹ꇿWWòÕÉðb\¢9á™1œÆû/ )*Ád²¯) R
jH(#$pÇunÇ3ýÙsŸîÜçëWVtF±×‰^Ñâ>¬Ó¾kd6áÛã...@„4Òwrœ’n¤“í¢-ºideŒÓojp[î!8sЍõáa“epqeh»qÙëÒ^Œ[bï$0ò‡Ïq~âboy§ª(8®óg¸Øç.Ú#±d
ñCƒŸˆ<ˆñ*FûŸ, ŸªÝ»¸Ùò{sGu];UÐ?˾eH»vü¦¯å5]~@'õ§?‚h$Š(¢€¢Š( (¢€¢©U (ªUh
(¢ƒÿÙ', '', '', '', '', '', '')

MySQL complains about the image in string form. I'm sure the solution is
simple, but I don't know what it is.
TIA,
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: id( ) function question

2009-10-15 Thread Christian Heimes
Mel wrote:
> As Python has evolved the semantics have got richer, and the implementation 
> has got trickier with proxy objects and wrapped functions and more.  
> Whatever use there was for `is` in ordinary code is vanishing.

'is' has important use cases but it's not trivial to use if you leave
the road.

> Even a straight-up assignment can fool:
> 
> Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 class A(object):
>    def __init__ (self):
>  self.mangler = self.method1
>    def method1 (self):
>  return id (self) ^ 30103
>    def is_mangled (self):
>  return self.mangler is self.method1
>  
 a=A()
 a.is_mangled()
> False
> 
> I fully understand that there's an excellent reason for that -- I wouldn't 
> have it any other way.  Also note that, in the same example
> 
 a.mangler == a.method1
> True

Even "obj.method is obj.method" doesn't return true because Python
creates a new method wrapper for every attribute access:

>>> class Example(object):
... def func(self):
... pass
...
>>> Example.func

>>> Example.func is Example.func
False


Christian

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


Re: id( ) function question

2009-10-15 Thread Laszlo Nagy



The built-ins aren't mutable, and the singletons are each immutable
and/or unique; so in no case do objects that are both different and
mutable have the same ID.
  

I know. :-)

Although I have no idea how it is that `id({}) == id({})` as a prior
posted showed; FWIW, I can't manage to reproduce that outcome.
  
Yes, that was the question. I mean, I thought that the id() function can 
be used to tell if two mutable objects are different or not. But 
apparently it cannot. Then why is it there, and what is it for?

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


Re: id( ) function question

2009-10-15 Thread Laszlo Nagy

Christian Heimes írta:

Chris Rebert wrote:
  

The built-ins aren't mutable, and the singletons are each immutable
and/or unique; so in no case do objects that are both different and
mutable have the same ID.



Correct, the fact allows you to write code like "type(egg) is str" to
check if an object *is* an instance of str. The isistance() methods also
returns True if the argument is a subclass of str.

  

Although I have no idea how it is that `id({}) == id({})` as a prior
posted showed; FWIW, I can't manage to reproduce that outcome.



The behavior is caused by a free list in the dict implementation. When
the reference count of a object drops to zero, it's usually removed from
memory entirely. However some types keep a list of unused objects around
to increase efficiency and spare some calls to malloc() and free().

For the code "{} is {}" Python has to create two distinct dict objects.
"id({}) == id({})" returns True under most circumstances because the
reference count of the first dict drops to zero as soon as the id()
function returns its memory address. The second call to id() retrieves
the very same template from the dict type's free list thus returning the
same memory address.
  
All right, I see your point now. So can we say, that the id function can 
be used to tell if two mutable objects are different as long as they are 
both alive during the comparison?

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


Re: Load a list subset with pickle?

2009-10-15 Thread Peng Yu
On Tue, Oct 13, 2009 at 1:23 PM, Robert Kern  wrote:
> On 2009-10-13 13:00 PM, Peng Yu wrote:
>>
>> I use pickle to dump a long list. But when I load it, I only want to
>> load the first a few elements in the list. I am wondering if there is
>> a easy way to do so? Thank you!
>
> Not by pickling the list. However, you can concatenate pickles, so you could
> just pickle each item from the list in order to the same file and only
> unpickle the first few.
>
> In [1]: import cPickle
>
> In [2]: from cStringIO import StringIO
>
> In [3]: very_long_list = range(10)
>
> In [4]: f = StringIO()
>
> In [5]: for item in very_long_list:
>   ...:     cPickle.dump(item, f)
>   ...:
>   ...:
>
> In [6]: f.seek(0,0)
>
> In [7]: cPickle.load(f)
> Out[7]: 0
>
> In [8]: cPickle.load(f)
> Out[8]: 1
>
> In [9]: cPickle.load(f)
> Out[9]: 2
>
> In [10]: cPickle.load(f)
> Out[10]: 3

How do I determine if I have loaded all the elements? I use the
following code. I'm wondering if there is any better solution than
this.


###
import pickle

alist = [1, 2.0, 3, 4+6j]

output=open('serialize_list.output/serialize_list.pkl', 'wb')
for e in alist:
  pickle.dump(e, output)
output.close()

input=open('serialize_list.output/serialize_list.pkl', 'rb')

try:
  while 1:
e = pickle.load(input)
print e
except EOFError:
  pass
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: set using alternative hash function?

2009-10-15 Thread Austin Bingham
On Thu, Oct 15, 2009 at 5:15 PM, Gabriel Genellina
 wrote:
> En Thu, 15 Oct 2009 11:42:20 -0300, Austin Bingham
>  escribió:
> I think you didn't understand correctly Anthony Tolle's suggestion:
>
> py> class Foo:
> ...   def __init__(self, name): self.name = name
> ...
> py> objs = [Foo('Joe'), Foo('Jim'), Foo('Tom'), Foo('Jim')]
> py> objs

I understand Anthony perfectly. Yes, I can construct a dict as you
specify, where all of the keys map to values with name attributes
equal to the key. My point is that dict doesn't really help me enforce
that beyond simply letting me set it up; it doesn't care about the
values at all, just the keys. All that I'm asking for, and I think
it's been made pretty clear, is a set that let's me define a
uniqueness criteria function other than hash(). As has been thrashed
out already, this is not as straightforward as I might have liked.

To put it in code, I want this:

  s = set(hash_func = lambda obj: hash(obj.name), eq_func = ...)
  ...
  x.name = 'foo'
  y.name = 'foo'
  s.add(x)
  s.add(y) # no-op because of uniqueness criteria
  assert len(s) == 1

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


Re: id( ) function question

2009-10-15 Thread Laszlo Nagy




None, True, False, integers and strings are not mutable. The only time 
the id is the "same" between two objects is if they are the identical 
two objects.

I'm aware of that. ;-)


CPython just (as a performance optimization) re-uses the same objects 
sometimes even if people think they're using different objects.


In,
>>> a = 2
>>> b = 2

Integers are immutable so it was not part of my question.
Christian's point is, I believe, that this is all an implementation 
detail to the CPython platform and not a language-defined feature. 
Other implementations may do other things, so one should not rely on 
this behavior. Basically, don't use "is" unless you really know what 
you're doing -- or are testing verses a singleton :) "is" is never the 
right thing for numbers. Usually. Ahem.

Well, I now understand that this is also "theoretically" correct:

>>> id({}) == id({})
True

The explanation is that you can only distinguish two objects if ARE 
objects, e.g. they both must exist. In the one-liner above, the dict 
objects are not directly compared, and they are not BOTH alive when 
their ids are taken, so this one liner is a meaningless expression. :-)


I'm always amazed how logical and clean Python is. I do not want to 
start flame here, but for other programming languages (like XXX :-) ) 
they could not even start a thread like this, because some terms and 
concepts in other languages are not defined precisely.


  L

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


Re: set using alternative hash function?

2009-10-15 Thread Rami Chowdhury
On Thu, 15 Oct 2009 09:11:00 -0700, Austin Bingham  
 wrote:



On Thu, Oct 15, 2009 at 5:15 PM, Gabriel Genellina
 wrote:

En Thu, 15 Oct 2009 11:42:20 -0300, Austin Bingham
 escribió:
I think you didn't understand correctly Anthony Tolle's suggestion:

py> class Foo:
...   def __init__(self, name): self.name = name
...
py> objs = [Foo('Joe'), Foo('Jim'), Foo('Tom'), Foo('Jim')]
py> objs


I understand Anthony perfectly. Yes, I can construct a dict as you
specify, where all of the keys map to values with name attributes
equal to the key. My point is that dict doesn't really help me enforce
that beyond simply letting me set it up; it doesn't care about the
values at all, just the keys.


Perhaps this is an overly naive solution, but could you not define a class  
that implemented the set interface but used a dict for internal storage,  
and use that? You'd still have uniqueness (by dict key, which your class  
would define as object name) and as a bonus, retrievability by name, which  
set wouldn't give you.



--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor

408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
--
http://mail.python.org/mailman/listinfo/python-list


Re: python performance on Solaris

2009-10-15 Thread Antoine Pitrou
Le Wed, 14 Oct 2009 22:39:14 -0700, John Nagle a écrit :
> 
> Note that multithreaded compute-bound Python programs really suck
> on multiprocessors.  Adding a second CPU makes the program go slower,
> due to a lame mechanism for resolving conflicts over the global
> interpreter lock.

I'm not sure what you're talking about. Python has no "mechanism for 
resolving conflicts over the global interpreter lock" (let alone a lame 
one :-)), it just trusts the OS to schedule a thread only when it is not 
waiting on an unavailable resource (a lock). The GIL is just an OS-level 
synchronization primitive and its behaviour (fairness, performance) will 
depend on the behaviour of the underlying OS.

If this belief is derived from Dave Beazley's slides (*) about Python's 
multi-threading issues, I'd say some of his observations are rather 
questionable. First because the measurements were done on OS X, which has 
its own serious concurrency problems (**). Second because one of the 
benchmarks is so synthetic that it doesn't reflect real-world use of 
Python at all.

This is not to say there aren't any issues, of course.


(*) http://www.dabeaz.com/python/GIL.pdf

(**) http://www.nabble.com/Mutex-performance-td24892454.html

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


Module naming convention about StringIO

2009-10-15 Thread Peng Yu
It says on http://www.python.org/dev/peps/pep-0008/

Package and Module Names

  Modules should have short, all-lowercase names.  Underscores can be used
  in the module name if it improves readability.  Python packages should
  also have short, all-lowercase names, although the use of underscores is
  discouraged.

But StringIO does not following this convention. Although on the same
page, it also mentions the following. However, since StringIO is in
the library, shall its name be rectified?

The naming conventions of Python's library are a bit of a mess, so we'll
never get this completely consistent -- nevertheless, here are the
currently recommended naming standards.  New modules and packages
(including third party frameworks) should be written to these standards,
but where an existing library has a different style, internal consistency
is preferred.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python XMLRPC question

2009-10-15 Thread prasanna
Thanks a bunch. Qill give it a shot.

--p

On Oct 14, 8:18 pm, "Gabriel Genellina" 
wrote:
> En Wed, 14 Oct 2009 22:08:09 -0300,prasanna  
> escribió:
>
> > Out of curiosity--one more thing I haven't yet figured out, is there a
> > xmlrpc command I can send that stops or restarts the server?
>
> If you're using Python 2.6, the easiest way is to register its shutdown()  
> method. Note that it *must* be called from a separate thread (just inherit  
>  from ForkingMixIn)
>
> On earlier versions, overwrite the serve_forever loop (so it reads `while  
> not self._quit: ...`) and add a shutdown() method that sets self._quit to  
> True. You'll need to call shutdown twice in that case.
>
> === begin xmlrpcshutdown.py ===
> import sys
>
> def server():
>      from SocketServer import ThreadingMixIn
>      from SimpleXMLRPCServer import SimpleXMLRPCServer
>
>      # ThreadingMixIn must be included when publishing
>      # the shutdown method
>      class MyXMLRPCServer(ThreadingMixIn, SimpleXMLRPCServer):
>          pass
>
>      print 'Running XML-RPC server on port 8000'
>      server = MyXMLRPCServer(("localhost", 8000),
>          logRequests=False, allow_none=True)
>          # allow_none=True because of shutdown
>      server.register_function(lambda x,y: x+y, 'add')
>      server.register_function(server.shutdown)
>      server.serve_forever()
>
> def client():
>      from xmlrpclib import ServerProxy
>
>      print 'Connecting to XML-RPC server on port 8000'
>      server = ServerProxy("http://localhost:8000";)
>      print "2+3=", server.add(2, 3)
>      print "asking server to shut down"
>      server.shutdown()
>
> if sys.argv[1]=="server": server()
> elif sys.argv[1]=="client": client()
> === end xmlrpcshutdown.py ===
>
> C:\TEMP>start python xmlrpcshutdown.py server
>
> C:\TEMP>python xmlrpcshutdown.py client
> Connecting to XML-RPC server on port 8000
> 2+3= 5
> asking server to shut down
>
> C:\TEMP>
>
> --
> Gabriel Genellina

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


Re: Module naming convention about StringIO

2009-10-15 Thread Tim Golden

Peng Yu wrote:

It says on http://www.python.org/dev/peps/pep-0008/


... don't get too hung up on things like this. Just
use the modules.


But StringIO does not following this convention. Although on the same
page, it also mentions the following. However, since StringIO is in
the library, shall its name be rectified?


http://docs.python.org/dev/3.0/whatsnew/3.0.html#library-changes

for the general approach and below (search for "StringIO")

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


Problem with character encoding in commandline

2009-10-15 Thread gialloporpora

Dear all,
I have a strange problem that I am not able to solve myself.
I have written a little Python script to download image from last.fm, 
now, if I call it from the python environment  it works, if I call it 
from Windows console it doesn't works



If I open the prompt and run python  I call the function:
(the code is at the end of message)
>>> lastfm.decodeArgs("é")  (e acute è in html)
I obtain this result:
%C3%A9
that is the correct output.

if I open the windows console (cmd) without enter in Python envinroment, 
and I call script from commandline:


lastfm.py é

the result is:
%C3%9A

that is not the same and I don't understand the reason.

If I give the command : chcp from console I have:
Active code page: 850
the same code table that I see using the  sys.stdin.encoding inside 
Python envinroment.

Someone know  where I make wrong ?

Thanks
Sandro

My code (only the parts that not works)

==
import sys
from urllib import quote_plus
def  decodeArgs(s):
return quote_plus(s.decode(sys.stdin.encoding).encode("utf-8"))
if __name__ == '__main__':
args=sys.argv[1]
print decodeArgs(args)
==



--
*gialloporpora: Alex Faaborg - » Browsing Your Personal Web 
http://ff.im/9JDwm * - http://sn.im/shv8b

*Massimo Bubola - Cuori Ribelli * - http://sn.im/sivjx
* FAQ* di /it-alt.comp.software.mozilla/: http://bit.ly/1MZ04d
--
http://mail.python.org/mailman/listinfo/python-list


Re: Load a list subset with pickle?

2009-10-15 Thread Robert Kern

On 2009-10-15 11:05 AM, Peng Yu wrote:

On Tue, Oct 13, 2009 at 1:23 PM, Robert Kern  wrote:

On 2009-10-13 13:00 PM, Peng Yu wrote:


I use pickle to dump a long list. But when I load it, I only want to
load the first a few elements in the list. I am wondering if there is
a easy way to do so? Thank you!


Not by pickling the list. However, you can concatenate pickles, so you could
just pickle each item from the list in order to the same file and only
unpickle the first few.

In [1]: import cPickle

In [2]: from cStringIO import StringIO

In [3]: very_long_list = range(10)

In [4]: f = StringIO()

In [5]: for item in very_long_list:
   ...: cPickle.dump(item, f)
   ...:
   ...:

In [6]: f.seek(0,0)

In [7]: cPickle.load(f)
Out[7]: 0

In [8]: cPickle.load(f)
Out[8]: 1

In [9]: cPickle.load(f)
Out[9]: 2

In [10]: cPickle.load(f)
Out[10]: 3


How do I determine if I have loaded all the elements? I use the
following code. I'm wondering if there is any better solution than
this.


###
import pickle

alist = [1, 2.0, 3, 4+6j]

output=open('serialize_list.output/serialize_list.pkl', 'wb')
for e in alist:
   pickle.dump(e, output)
output.close()

input=open('serialize_list.output/serialize_list.pkl', 'rb')

try:
   while 1:
 e = pickle.load(input)
 print e
except EOFError:
   pass


You could write out an integer with the number of expected elements at the very 
beginning.


In [1]: import cPickle

In [2]: alist = [1, 2.0, 3, 4+6j]

In [3]: output = open('foo.pkl', 'wb')

In [4]: cPickle.dump(len(alist), output)

In [5]: for item in alist:
   ...: cPickle.dump(item, output)
   ...:
   ...:

In [6]: output.close()

In [7]: input = open('foo.pkl', 'rb')

In [8]: n = cPickle.load(input)

In [9]: n
Out[9]: 4

In [10]: for i in range(n):
   : print cPickle.load(input)
   :
   :
1
2.0
3
(4+6j)

In [11]: assert input.read(1) == ''

--
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 to organize code for conversion between different classes? (avoiding cyclic dependencies)

2009-10-15 Thread Stephen Hansen
On Thu, Oct 15, 2009 at 8:02 AM, Peng Yu  wrote:

> Suppose I have classes 'A',  'B',  'C',  'D'. The definition of these
> classes are long enough so that I have to put each class in a separate
> module 'mA', 'mB', 'mC', 'mD', which are in packages 'pA', 'pB', 'pC',
> 'pD', respectively. And there were no need to have conversion
> functions between these classes.
>
> As my program evolves, I need to have a conversion function between
> any pair of classes. I could define the conversion functions as member
> methods like the following. But if I do this for the classes B, C, D,
> I would end up with cyclic dependencies, which should be avoid.
>

[..]

Another choice is that I can put the conversion functions in some
> modules and import 'mA', 'mB', 'mC', 'mD' when needed. However, since
> mA, mB, mC, mD are in different packages, I'm not sure where to put
> the new modules that have the conversion functions.
>
>
I'd do it this way, as for where? Obviously above pA, pB, pC and pD. Either
in a package those packages are all in, or in just a top-level code
directory if that's how you're organized.

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


Re: Load a list subset with pickle?

2009-10-15 Thread Gabriel Genellina

En Thu, 15 Oct 2009 13:05:18 -0300, Peng Yu  escribió:


How do I determine if I have loaded all the elements? I use the
following code. I'm wondering if there is any better solution than
this.


###
import pickle

alist = [1, 2.0, 3, 4+6j]

output=open('serialize_list.output/serialize_list.pkl', 'wb')
for e in alist:
  pickle.dump(e, output)
output.close()

input=open('serialize_list.output/serialize_list.pkl', 'rb')

try:
  while 1:
e = pickle.load(input)
print e
except EOFError:
  pass


Pickle the list length before its contents.

--
Gabriel Genellina

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


Re: Load a list subset with pickle?

2009-10-15 Thread Gabriel Genellina

En Thu, 15 Oct 2009 13:05:18 -0300, Peng Yu  escribió:


How do I determine if I have loaded all the elements? I use the
following code. I'm wondering if there is any better solution than
this.


###
import pickle

alist = [1, 2.0, 3, 4+6j]

output=open('serialize_list.output/serialize_list.pkl', 'wb')
for e in alist:
  pickle.dump(e, output)
output.close()

input=open('serialize_list.output/serialize_list.pkl', 'rb')

try:
  while 1:
e = pickle.load(input)
print e
except EOFError:
  pass


Pickle the list length before its contents.

--
Gabriel Genellina

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


Re: set using alternative hash function?

2009-10-15 Thread Anthony Tolle
On Oct 15, 12:11 pm, Austin Bingham  wrote:
> To put it in code, I want this:
>
>   s = set(hash_func = lambda obj: hash(obj.name), eq_func = ...)
>   ...
>   x.name = 'foo'
>   y.name = 'foo'
>   s.add(x)
>   s.add(y) # no-op because of uniqueness criteria
>   assert len(s) == 1

I wrote a quick subclass of set that does something similar, but uses
just one function for the object uniqueness:

class MySet(set):
def __init__(self, iterable = (), idfunc = lambda x: x):
self.idfunc = idfunc
self.ids = set()
for item in iterable:
self.add(item)

def add(self, item):
id = self.idfunc(item)
if id not in self.ids:
self.ids.add(id)
set.add(self, item)

>>> class Foo(object):
...  def __init__(self, name):
...   self.name = name
...
>>> x = Foo('foo')
>>> y = Foo('foo')
>>> s = MySet(idfunc = lambda x: x.name)
>>> s.add(x)
>>> s
MySet([<__main__.Foo object at 0x00A89F90>])
>>> s.add(y)
>>> s
MySet([<__main__.Foo object at 0x00A89F90>])

Is this what you are looking for?

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


Re: where's self.assertMatch (for Django)?

2009-10-15 Thread Phlip
> When I google (including codesearch) for assertMatch, I get a mishmash of
> opinions. Am I missing the One True assertMatch(), or isn't there one and I
> gotta write it? or copy and use one of the pretenders?

Ookay, try this:

def assertMatch(self, pattern, slug):
r = re.compile(pattern)
self.assertNotEqual(None, r.search(smart_str(slug)))

To do: Pass thru an optional diagnostic message.

Anyone know why this is not in PyUnit?

> --
>    Phlip
>    http://zeekland.zeroplayer.com/Pigleg_Too/1

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


Problem

2009-10-15 Thread Ander
I can't send emails out. Can u fix this problem please?


The
message could not be sent. The authentication setting might not be
correct for your outgoing e-mail [SMTP] server. For help solving this
problem, go to Help, search for "Troubleshoot Windows Mail", and read
the "I'm having problems sending e-mail" section. If you need help
determining the proper server settings, please contact your e-mail
service provider.

The rejected e-mail address was
'antraxak_1...@hotmail.com'. Subject 'adasf', Account: 'mail.hot.ee',
Server: 'mail.hot.ee', Protocol: SMTP, Server Response: '550 5.7.1
: Recipient address rejected: Mail
appeared to be SPAM or forged. Ask your Mail/DNS-Administrator to
correct HELO and DNS MX settings or to get removed from DNSBLs; MTA
helo: rulltukkpc, MTA hostname:
86-43-219-112-dynamic.b-ras2.bbh.dublin.eircom.net[86.43.219.112]
(helo/hostname mismatch)', Port: 25, Secure(SSL): No, Server Error: 550,
Error Number: 0x800CCC79

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


Re: Problem

2009-10-15 Thread Xavier Ho
On Fri, Oct 16, 2009 at 3:28 AM, Ander  wrote:

> I can't send emails out. Can u fix this problem please?
>

I don't know what I'm missing here, but you evidently sent out an email to
the Python mailing list...

Cheers,
Xav
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: set using alternative hash function?

2009-10-15 Thread Gabriel Genellina
En Thu, 15 Oct 2009 13:24:18 -0300, Rami Chowdhury  
 escribió:


On Thu, 15 Oct 2009 09:11:00 -0700, Austin Bingham  
 wrote:



On Thu, Oct 15, 2009 at 5:15 PM, Gabriel Genellina
 wrote:

En Thu, 15 Oct 2009 11:42:20 -0300, Austin Bingham
 escribió:
I think you didn't understand correctly Anthony Tolle's suggestion:

py> class Foo:
...   def __init__(self, name): self.name = name
...
py> objs = [Foo('Joe'), Foo('Jim'), Foo('Tom'), Foo('Jim')]
py> objs


I understand Anthony perfectly. Yes, I can construct a dict as you
specify, where all of the keys map to values with name attributes
equal to the key. My point is that dict doesn't really help me enforce
that beyond simply letting me set it up; it doesn't care about the
values at all, just the keys.


Perhaps this is an overly naive solution, but could you not define a  
class that implemented the set interface but used a dict for internal  
storage, and use that? You'd still have uniqueness (by dict key, which  
your class would define as object name) and as a bonus, retrievability  
by name, which set wouldn't give you.


Like this?

from collections import MutableSet

class KeyedSet(MutableSet):
"""A set with a custom element comparison function."""

def __init__(self, iterable, key=lambda x: x):
"""Create a KeyedSet from iterable; key function determines  
uniqueness.


`key` must be a callable taking a single argument; it is
applied to every item in the iterable, and its result
is used to determine set membership. That is, if key(item)
returns the same thing for two items, only one of them will
be in the set."""

self._items = dict((key(item),item) for item in iterable)
self._key = key

# NOT a classmethod because self.key must be transferred too!
# Fortunately it is always called as self._from_iterable(...)
# in _abccoll.py
def _from_iterable(self, iterable):
return type(self)(iterable, key=self._key)

def add(self, value):
"""Return True if it was added, False if already there."""
key = self._key(value)
if key in self._items: return False
self._items[key] = value
return True

def discard(self, value):
"""Return True if it was deleted, False if not there."""
key = self._key(value)
if key not in self._items: return False
del self._items[key]
return True

def clear(self):
self._items.clear()

def copy(self):
return type(self)(self._items.values(), key=self._key)

def __iter__(self):
return iter(self._items.values())

def __contains__(self, value):
try: return self._key(value) in self._items
except Exception: return False

def __len__(self):
return len(self._items)

def __repr__(self):
return "%s(%r)" % (type(self).__name__, self._items.values())


def demo():
  class Foo(object):
def __init__(self, name):
  self.name = name
def __repr__(self):
  return "%s(%r)" % (type(self).__name__, self.name)

  objs = [Foo('Joe'), Foo('Jim'), Foo('Tom'), Foo('Jim')]
  print objs
  s = KeyedSet(objs, key=lambda o:o.name)
  print len(s), s
  s.add(Foo('Joe'))
  print len(s), s
  moe = Foo('Moe')
  print "moe in s", moe in s
  s.add(moe)
  print "moe in s", moe in s
  print "'moe' in s", 'moe' in s
  s2 = set([Foo('Luc'), Foo('Jim'), Foo('Dan')])
  print "s | s2", s | s2
  print "s & s2", s & s2
  s3 = KeyedSet(s2, key=lambda o:o.name)
  print "s3 - s", s3 - s
  print "s - s3", s - s3
  print "s.copy()", s.copy()

demo()

--
Gabriel Genellina

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


Re: set using alternative hash function?

2009-10-15 Thread Ethan Furman

Austin Bingham wrote:

Yes, I can construct a dict as you
specify, where all of the keys map to values with name attributes
equal to the key. My point is that dict doesn't really help me enforce
that beyond simply letting me set it up; it doesn't care about the
values at all, just the keys. All that I'm asking for, and I think
it's been made pretty clear, is a set that let's me define a
uniqueness criteria function other than hash(). As has been thrashed
out already, this is not as straightforward as I might have liked.

To put it in code, I want this:

  s = set(hash_func = lambda obj: hash(obj.name), eq_func = ...)
  ...
  x.name = 'foo'
  y.name = 'foo'
  s.add(x)
  s.add(y) # no-op because of uniqueness criteria
  assert len(s) == 1

Austin



I'm feeling really dense about now... What am I missing?

class obj(object):
def __init__(self, id, value):
self.id = id
self.value = value
def __eq__(self, other):
if isinstance(other, obj):
return self.id == other.id
raise NotImplemented
def __hash__(self):
return hash(self.id)
def __repr__(self):
return "obj(%s, %s)" % (self.id, self.value)

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit 
(Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.
--> from obj import obj
--> s1 = set()
--> d1 = dict()
--> o1 = obj(17, 'first')
--> o2 = obj(18, 'second')
--> o3 = obj(17, 'third')
--> o1, o2, o3
(obj(17, first), obj(18, second), obj(17, third))
--> s1.add(o1)
--> s1.add(o2)
--> s1.add(o3)
--> d1[o1.id] = o1
--> d1[o2.id] = o2
--> d1[o3.id] = o3
--> s1
set([obj(17, first), obj(18, second)])
--> d1
{17: obj(17, third), 18: obj(18, second)}
-->

Ah ha!  No-op indeed!  If a set already has an item, the new item is 
completely ignored, whereas if a dict already has a key, the new key's 
value replaces the current value already in the dict.


Learn something new everyday!

I'm still not sure I understand your concern about the values in a set, 
though.  Sets keep the first object of a given key, dicts keep the last 
object of a given key; in both cases, all other objects with the same 
key are lost.


So is that the behavior you're wanting, keeping the first object and 
discarding all others?  Or is there something else I'm still missing?


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


Re: Problem

2009-10-15 Thread Tim Chase

I can't send emails out. Can u fix this problem please?


The problem is on line 72.

Oh wait...you didn't include any code or the traceback.  Yeah,
that's gonna make it a little difficult to diagnose.  But I'm
gonna guess that "The authentication setting might not be correct
for your outgoing e-mail [SMTP] server."  Check your use of the 
SMTP libs to ensure that you're authenticating to your SMTP 
server correctly.  Notably, the login() method.


-tkc




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


struct curiosity

2009-10-15 Thread pjcoup
Hello,

I was fooling around with python's struct lib, looking on how we'd
unpack some data.  I was a little confused by its behavior:
Python 2.5.2 (r252:60911, Jul 22 2009, 15:33:10)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct
>>> struct.calcsize('BB')
11
>>> struct.calcsize('@BB')
11
>>> struct.calcsize('>> struct.calcsize('>BB')
10

I would have expected calcsize('BB') to be either 10 or 12
(padding), but 11?
Is there a simple explanation of what is going on here?  Just a
curiosity.
This is on a x86_64, but have seen the same on i686.  Ideas?

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


Python-URL! - weekly Python news and links (Oct 15)

2009-10-15 Thread Gabriel Genellina
QOTW:  "It is however, much like the framework in question, best kept private
and not made public." - Ed Singleton, on a "perfectly healthful and
acceptable" practice ... left unnamed here
http://groups.google.com/group/comp.lang.python/msg/987b1a7a4b9 01f3f


Looking for a sane way of organizing modules in a project:
http://groups.google.com/group/comp.lang.python/t/204e4f698243c62d/
http://groups.google.com/group/comp.lang.python/t/aa2f93d4d6ce316f/
http://groups.google.com/group/comp.lang.python/t/e24be42ecbee7cad/

A simple error (reusing a name for two different things) helps in
understanding how assignment works:
http://groups.google.com/group/comp.lang.python/t/2e1f7621875b3c43/

Python 2.6.3 broke setuptools - a new release is coming soon:
http://groups.google.com/group/comp.lang.python/t/1dc9f3ef9d7e4679/

Python community action has far outstripped "Python-URL!"'s ability to
keep up with it; in fact, there's a four-year-old (!) site just to keep
track of PyCon-s:
http://pycon.blogspot.com/
Among current highlights: Indian Pythoneers have been doing *great* things:

http://programming-tidbits.blogspot.com/2009/10/pycon-india-2009-my-impressions.html

http://pycon.blogspot.com/2009/09/pycon-india-receives-fantastic-response.html
http://in.pycon.org/
http://scipy.in/
pyTexas is only a week away:

http://pycon.blogspot.com/2009/10/pytexas-regional-conference-oct-2425.html
And KiwiPyCon is coming up fast:

http://pycon.blogspot.com/2009/10/kiwi-pycon-how-programming-language-is.html

How to specify a superclass at runtime:
http://groups.google.com/group/comp.lang.python/t/fc0dd89fae658655/

How to handle dates before 01-01-1970 -- plus, the history of various
date-handling libraries:
http://groups.google.com/group/comp.lang.python/t/52336cb9128c90bf/

Christian Heimes summarizes the reasons why one should NOT use the thread
module directly:
http://groups.google.com/group/comp.lang.python/t/f7e946f84e30b7c3/

A module is executed twice - how could that happen?
http://groups.google.com/group/comp.lang.python/t/e8cc7032ef580124/

Defining 'once' properties (that are computed once for all instances):
http://groups.google.com/group/comp.lang.python/t/e976c6338ab6f79f/

The correct way to define __hash__:
http://groups.google.com/group/comp.lang.python/t/62238f7bbb85f08f/

Pickle backwards compatibility:
http://groups.google.com/group/comp.lang.python/t/3d95c7df243b09f2/

The 'cmp' argument to sort() is gone - was it a good idea?
http://groups.google.com/group/comp.lang.python/t/f70457535a7c252e/

Ideas for a self-editing script:
http://groups.google.com/group/comp.lang.python/t/e3fda49e6a29215c/

Is there any real reason to dislike "while True:" loops?
http://groups.google.com/group/comp.lang.python/t/46a2082b2e2c991c/

Object Relational Mappers are evil (or not?):
http://groups.google.com/group/comp.lang.python/t/d22fd69527205232/



Everything Python-related you want is probably one or two clicks away in
these pages:

Python.org's Python Language Website is the traditional
center of Pythonia
http://www.python.org
Notice especially the master FAQ
http://www.python.org/doc/FAQ.html

PythonWare complements the digest you're reading with the
marvelous daily python url
 http://www.pythonware.com/daily

Just beginning with Python?  This page is a great place to start:
http://wiki.python.org/moin/BeginnersGuide/Programmers

The Python Papers aims to publish "the efforts of Python enthusiasts":
http://pythonpapers.org/
The Python Magazine is a technical monthly devoted to Python:
http://pythonmagazine.com

Readers have recommended the "Planet" site:
http://planet.python.org

comp.lang.python.announce announces new Python software.  Be
sure to scan this newsgroup weekly.
http://groups.google.com/group/comp.lang.python.announce/topics

Python411 indexes "podcasts ... to help people learn Python ..."
Updates appear more-than-weekly:
http://www.awaretek.com/python/index.html

The Python Package Index catalogues packages.
http://www.python.org/pypi/

Much of Python's real work takes place on Special-Interest Group
mailing lists
http://www.python.org/sigs/

Python Success Stories--from air-traffic control to on-line
match-making--can inspire you or decision-makers to whom you're
subject with a vision of what the language makes practical.
http://www.pythonology.com/success

The Python Software Foundation (PSF) has replaced the Python

Re: set using alternative hash function?

2009-10-15 Thread Anthony Tolle
On Oct 15, 1:49 pm, Ethan Furman  wrote:
> I'm still not sure I understand your concern about the values in a set,
> though.  Sets keep the first object of a given key, dicts keep the last
> object of a given key; in both cases, all other objects with the same
> key are lost.
>
> So is that the behavior you're wanting, keeping the first object and
> discarding all others?  Or is there something else I'm still missing?

I think that without a practical example of what this would be used
for, we're all going to be a little lost on this one.

So far, we've not seen the original problem, only the author's
preferred method for solving it.  My guess is there are other, more
pythonic ways to solve the original problem.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Relational Mappers are evil (a meditation)

2009-10-15 Thread Mick Krippendorf
Steve Holden wrote:
>  Many such designs make mistakes like using multiple columns
> (or, even worse, comma-separated values) instead of many-to-many
> relationships.

BTW, the comma-separted-values-in-a-field is officially called the First
Anormal Form. There *has to be* some value to it since I've seen it used
quite a few times...

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


Moving subwindows in curses does not seem to work

2009-10-15 Thread Mitko Haralanov
Hi all,

I am attempting to learn curses programming and in the process have
created a small curses ui program. I am currently working on the code
which is handling resizing the terminal window.
As part of the resizing of the terminal window, I have to resize and
move some of the subwindows in my ui.
However, it doesn't seem to be working (the following is printed out
by my program into a logfile):

Parent size (y,x): 42,159
Height of subwin: 8
subwin size (y,x): 7,159
subwin coord (y,x): 35,0
sibwin size (y,x): 1,159
move to (y,x): 34, 0
subwin coord (y,x): 35,0
attempt resize to (y,x): 8,159

As you can see, the subwin's coordinates within the parent window do
not change before and after the move.

I have not been able to find any information why this might be
happening so if anyone could help, I'd be thankful.

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


Re: Module naming convention about StringIO

2009-10-15 Thread Terry Reedy

Peng Yu wrote:

It says on http://www.python.org/dev/peps/pep-0008/

Package and Module Names

  Modules should have short, all-lowercase names.  Underscores can be used
  in the module name if it improves readability.  Python packages should
  also have short, all-lowercase names, although the use of underscores is
  discouraged.

But StringIO does not following this convention. Although on the same
page, it also mentions the following. However, since StringIO is in
the library, shall its name be rectified?


Already done.  In 3.x, StringIO is a *class* within the io *module*.

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


Re: Object Relational Mappers are evil (a meditation)

2009-10-15 Thread Ethan Furman

Mick Krippendorf wrote:

Steve Holden wrote:


Many such designs make mistakes like using multiple columns
(or, even worse, comma-separated values) instead of many-to-many
relationships.



BTW, the comma-separted-values-in-a-field is officially called the First
Anormal Form. There *has to be* some value to it since I've seen it used
quite a few times...

Mick.


Just because you've seen something, doesn't mean it has value; just 
because something has value, doesn't mean the value outweighs the 
associated disadvantages.


I'm not saying you're wrong, just that you may not be right.  ;-)

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


Re: ?????? () vs []

2009-10-15 Thread Terry Reedy

SmokingDog wrote:


 > Interesting interpretation.. but I just gave it a try.
 >
  a = (1,2,3,4)
  a
 > (1, 2, 3, 4)
  a.index(3)
 > 2
  a.index(5)
 > Traceback (most recent call last):
 >  File "", line 1, in 
 > ValueError: tuple.index(x): x not in tuple
 >
 > So my Python is saying that tuples do implement .index() method. What 
gives?

 >
 > Or maybe the diveintopython version he's quoting is out of date?

Apparently...or something stranger.


The OP did not indicate version, which posters should always do.

methods were added to tuples in 3.0 and perhaps 2.6.

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


Pack optimization

2009-10-15 Thread BDL
I have a large amount of binary data that needs to be pushed across
the network.  It appears from profiling that the dominant time is
being taken up by packing the data.  (50%)  Here is a CME that shows
the problem.

from numpy import random
from struct import pack
import time

lenstim = 10084200
sigma = 0.1
stim = random.normal(0., sigma, lenstim) # 10084200 gaussian random
numbers (doubles)

fmt = '!h'+str(stim.size)+'d'  # makes fmt = '!h10084200d'
cmd = 4

startTime = time.time()
packdat = pack( fmt, cmd, *stim )
elapsed = time.time() - startTime
print "Time to pack the command and data %.6f seconds " % elapsed

Is there a faster method to do this?  Is it possible to use array?
Any suggestions would be appreciated.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Load a list subset with pickle?

2009-10-15 Thread Peng Yu
On Thu, Oct 15, 2009 at 12:01 PM, Gabriel Genellina
 wrote:
> En Thu, 15 Oct 2009 13:05:18 -0300, Peng Yu  escribió:
>
>> How do I determine if I have loaded all the elements? I use the
>> following code. I'm wondering if there is any better solution than
>> this.
>>
>>
>> ###
>> import pickle
>>
>> alist = [1, 2.0, 3, 4+6j]
>>
>> output=open('serialize_list.output/serialize_list.pkl', 'wb')
>> for e in alist:
>>  pickle.dump(e, output)
>> output.close()
>>
>> input=open('serialize_list.output/serialize_list.pkl', 'rb')
>>
>> try:
>>  while 1:
>>    e = pickle.load(input)
>>    print e
>> except EOFError:
>>  pass
>
> Pickle the list length before its contents.

Suppose that the list length was not in
'serialize_list.output/serialize_list.pkl'. Is try-except block the
best solution?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Relational Mappers are evil (a meditation)

2009-10-15 Thread Mick Krippendorf
Ethan Furman schrieb:
> Mick Krippendorf wrote:
>> BTW, the comma-separted-values-in-a-field is officially called the First
>> Anormal Form. There *has to be* some value to it since I've seen it used
>> quite a few times...
> 
> Just because you've seen something, doesn't mean it has value; [...]

The setting of irony poti must've been to low... What I really meant to
say was: First Anormal Form is despicable, and anyone who uses it
schould Rot In Hell For All Eternities To Come! really! :-)

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


Re: Problem with character encoding in commandline

2009-10-15 Thread gialloporpora

Risposta al messaggio di gialloporpora :


Dear all,
I have a strange problem that I am not able to solve myself.



Ok, I have solved my problem, sorry for the post.
First I had  no view this function:
sys.getfilesystemencoding()

that return the console encoding, sorry.
Sandro



*gialloporpora: Alex Faaborg - » Browsing Your Personal Web 
http://ff.im/9JDwm * - http://sn.im/shv8b

*Massimo Bubola - Cuori Ribelli * - http://sn.im/sivjx
* FAQ* di /it-alt.comp.software.mozilla/: http://bit.ly/1MZ04d
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >