Re: Generating all combinations

2009-06-01 Thread pataphor

Mensanator wrote:


I couldn't do that if they weren't subsets.


Right. Sometimes one just has to assume things are different even if 
they look the same on the surface. That is because else one wouldn't be 
able to produce the other generators. I guess it would also work the 
other way around, assuming things are the same even when they look 
different.


For example see my two functions:

def repeat_each(seq,n):
while True:
for x in seq:
for i in range(n):
yield x

def repeat_all(seq,n):
while True:
for i in range(n):
for x in seq:
yield x

(I should probably not smoke stuff before posting, but anyway)


They are the same, except for switching two lines of code. But for the 
second one ('repeat_all') the argument 'n' seems redundant. What does it 
even mean to repeat a sequence n times, and do that forever? Isn't that 
the same as just repeating the sequence itself, forever? So that's how 
we arrive at itertools.cycle . The second argument is there, but it 
would be foolish to include it, so it is left out.


But now let's look at how itertools.repeat should really be. It should 
look like 'repeat_each' above. Here second argument ('n') *is* 
necessary, or else the generator would just infinitely repeat only the 
first element of the sequence, which is obviously nonsense. But that is 
exactly why itertools.repeat does not accept a sequence (like cycle, its 
virtual brother) but instead it has degenerated into something that just 
repeats only one thing n times, which is stupid.


So to set things right one has to forget everything and just write 
complete balderdash if necessary, if it only leads to finally 
understanding how the two are related. Then one can see that 
itertools.repeat should be an infinite generator *on sequences* that 
however still needs a second argument specifying how many times each 
individual item should be repeated, and that itertools.cycle's second 
argument is there but hidden.


P.


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


Re: Why date do not construct from date?

2009-06-01 Thread Chris Rebert
On Mon, Jun 1, 2009 at 10:25 PM, Alexandr N Zamaraev  wrote:
> Simple example:
> [code]
> Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)]
> on win32
 import datetime as dt
 dt.date(2009, 10, 15)
> datetime.date(2009, 10, 15)
 d = dt.date(2009, 10, 15)
 dt.date(d)
> Traceback (most recent call last):
>  File "", line 1, in 
> TypeError: function takes exactly 3 arguments (1 given)

> [/code]
> Why int form int, str from str, Decumal from Decumal can construct bat date
> from date not?

Probably because the function signatures would be so different. str(),
int(), etc *always* take *exactly one* argument -- the object to
convert. In contrast, date() takes several integers corresponding to
the year, month, and day. Adding a second signature to it that took
exactly one argument (of type `date`) and copied it would be
significantly different from its other signature; in idiomatic Python,
one would typically make a separate, new function for this drastically
different signature.
However, the `date` type is immutable, so there's no reason at all to
try and copy a new instance from an existing one anyway, thus a
single-argument copy-constructor is completely unnecessary, hence why
there isn't one.

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


Re: Installing 3.1 questions

2009-06-01 Thread Martin v. Löwis
> Seems to work, but I'd like to know where Python installs its items.  I'm 
> wanting to test version 3.1 and downloaded the 'Bzipped source tar ball' 
> file (not sure of the difference between it and the 'Gzipped' one).  Do I 
> need to run some sort of 'install' command from the Terminal to get 
> 3.1 to work? 

You need to build it. Read the README file in the source distribution.
If you don't know what a "tar ball" is, read tar(1).

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


Re: Generating all combinations

2009-06-01 Thread Mensanator
On Jun 1, 8:28 pm, Steven D'Aprano  wrote:
> On Mon, 01 Jun 2009 17:24:49 -0700, Mensanator wrote:
> > On Jun 1, 6:40 pm, Steven D'Aprano  > cybersource.com.au> wrote:
> >> On Mon, 01 Jun 2009 11:23:35 -0700, Mensanator wrote:
> >> > I believe the name you're looking for is
> >> > combinations_with_replacement. It is one of the features being added
> >> > to 3.1 which should give all the subsets of the Cartesian Product:
>
> >> > permutations_with_replacement:    product()
> >> > combinations_with_replacement:    combinations_with_replacement()
> >> > permutations_without_replacement: permutations()
> >> > combinations_without_replacement: combinations()
>
> >> What, no partitions?
>
> > Itertools does partitions?
>
> Er, no. That's why I asked "What, no partitions?" instead of saying
> "Look, itertools also does partitions!"
>
> >>http://en.wikipedia.org/wiki/Partition_of_a_set
>
> > I didn't see any reference to Cartesian Product there.
>
> Wikipedia is the encyclopedia anyone can edit. Go right ahead and put it
> in if you think it needs to be there. While you're at it, there is no
> mention of Cartesian Product in any of
>
> http://en.wikipedia.org/wiki/Permutationshttp://en.wikipedia.org/wiki/Combinations
>
> http://mathworld.wolfram.com/Permutation.htmlhttp://mathworld.wolfram.com/k-Subset.html
>
> either. Are you sure that permutations and combinations are subsets of
> the Cartesian Product?

Didn't notice this before - it says so in the docs!

itertools.product(*iterables[, repeat])¶
Cartesian product of input iterables.

The code for permutations() can be also expressed as a subsequence of
product(), filtered to exclude entries with repeated elements (those
from the same position in the input pool):

The code for combinations() can be also expressed as a subsequence of
permutations() after filtering entries where the elements are not in
sorted order (according to their position in the input pool):



>
> --
> Steven

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


Why date do not construct from date?

2009-06-01 Thread Alexandr N Zamaraev

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

>>> import datetime as dt
>>> dt.date(2009, 10, 15)
datetime.date(2009, 10, 15)
>>> d = dt.date(2009, 10, 15)
>>> dt.date(d)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: function takes exactly 3 arguments (1 given)
>>>
[/code]
Why int form int, str from str, Decumal from Decumal can construct bat 
date from date not?

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


Re: Absolute imports in Python 2.4

2009-06-01 Thread Gabriel Genellina
En Mon, 01 Jun 2009 21:40:26 -0300, Steven D'Aprano  
 escribió:



I have a package which includes a module which shadows a module in the
standard library. For example:

package
+-- __init__.py
+-- ham.py
+-- spam.py
+-- sys.py

Inside that package, I want to import the standard library sys. In other
words, I want an absolute import. [...]
What can I do in Python 2.4 to get an absolute import?


sys = __import__("sys", {})

The import statement uses the global namespace to determine which package  
it is called on; if you pass an empty namespace, it cannot infer package  
information.


Anyway, the best move would be to rename the offending module...

--
Gabriel Genellina

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


re: Dabo 0.9.2 released

2009-06-01 Thread oyster
I have to say: please let dabo use english if it does not find any
langauge resource!
I have 'e:\prg\py\sap-24\python
e:\prg\py\pure_pylib\_dabo\tools\remove_dLocalize.py  .', but then the
scripts says 'I don't know _"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating all combinations

2009-06-01 Thread Mensanator
On Jun 1, 8:28�pm, Steven D'Aprano  wrote:
> On Mon, 01 Jun 2009 17:24:49 -0700, Mensanator wrote:
> > On Jun 1, 6:40�pm, Steven D'Aprano  > cybersource.com.au> wrote:
> >> On Mon, 01 Jun 2009 11:23:35 -0700, Mensanator wrote:
> >> > I believe the name you're looking for is
> >> > combinations_with_replacement. It is one of the features being added
> >> > to 3.1 which should give all the subsets of the Cartesian Product:
>
> >> > permutations_with_replacement: � �product()
> >> > combinations_with_replacement: � �combinations_with_replacement()
> >> > permutations_without_replacement: permutations()
> >> > combinations_without_replacement: combinations()
>
> >> What, no partitions?
>
> > Itertools does partitions?
>
> Er, no. That's why I asked "What, no partitions?" instead of saying
> "Look, itertools also does partitions!"
>
> >>http://en.wikipedia.org/wiki/Partition_of_a_set
>
> > I didn't see any reference to Cartesian Product there.
>
> Wikipedia is the encyclopedia anyone can edit. Go right ahead and put it
> in if you think it needs to be there. While you're at it, there is no
> mention of Cartesian Product in any of
>
> http://en.wikipedia.org/wiki/Permutationshttp://en.wikipedia.org/wiki/Combinations
>
> http://mathworld.wolfram.com/Permutation.htmlhttp://mathworld.wolfram.com/k-Subset.html
>
> either.

You might have better luck with Google.

> Are you sure that permutations and combinations are subsets of
> the Cartesian Product?

Sure looks that way (SQL examples):

Cartesian Product (Permutaions w/replacement)

SELECT B.q, A.p
FROM A, B;

q   p
a   a
a   b
b   a
b   b


Permutaions wo/replacement
SELECT B.q, A.p
FROM A, B
WHERE (((A.p)<>[B].[q]));

q   p
a   b
b   a


Combinations w/replacement

SELECT B.q, A.p
FROM A, B
WHERE (((A.p)>=[B].[q]));

q   p
a   a
a   b
b   b



Combinations wo/replacement

SELECT B.q, A.p
FROM A, B
WHERE (((A.p)>[B].[q]));

q   p
a   b


I couldn't do that if they weren't subsets.


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


Re: Challenge supporting custom deepcopy with inheritance

2009-06-01 Thread Gabriel Genellina
En Mon, 01 Jun 2009 14:19:19 -0300, Michael H. Goldwasser  
 escribió:



  I can examine the inherited slots to see which special methods are
  there, and to implement my own __deepcopy__ accordingly. But to do
  so well seems to essentially require reimplementing the complicated
  logic of the copy.deepcopy function.  That is, if my new class is
  the first to be implementing an explicit __deepcopy__ function, I
  seem to have no easy way to invoke the inherited version of
  "deepcopy(self)".


Yes, that's a problem. But there is a workaround: since __deepcopy__ is  
searched *in the instance* (unlike many other __special__ methods, that  
are usually searched in the class itself) you can fool the copy logic into  
thinking there is no __deepcopy__ method defined, just by (temporarily)  
setting an instance attribute __deepcopy__ to None. (It's a hack, anyway)



import copy

class A(object):
  def __init__(self, x, y):
self.x = x
self.y = y

  # these two methods implement copy and pickle behaviour
  # we can't change them
  def __reduce__(self):
return (self.__newobj__, (), self.__dict__)

  @classmethod
  def __newobj__(cls, *args):
return cls.__new__(cls, *args)


class B(A):
  def __init__(self, x, y, filename):
A.__init__(self, x, y)
self.file = open(filename, "at")

  def __deepcopy__(self, memo):
# Problem: how to call the inherited __deepcopy__ implementation
# when there is none?
# The logic is inside the copy.deepcopy function, not in
# object.__deepcopy__

# hack: make deepcopy() think this method doesn't exist
self.__deepcopy__ = None
try:
  dup = copy.deepcopy(self, memo)
  del dup.__deepcopy__
finally:
  del self.__deepcopy__

# now, do the special stuff
dup.file = open(self.file.name, "at")
return dup


obj = B(1, 2, "testfile")
print "obj", obj, vars(obj)
dup = copy.deepcopy(obj)
print "obj", obj, vars(obj)
print "dup", dup, vars(dup)


obj <__main__.B object at 0x00BEC0F0> {'y': 2, 'x': 1, 'file': <
open file 'testfile', mode 'at' at 0x00B46C00>}
obj <__main__.B object at 0x00BEC0F0> {'y': 2, 'x': 1, 'file': <
open file 'testfile', mode 'at' at 0x00B46C00>}
dup <__main__.B object at 0x00BEC7F0> {'y': 2, 'x': 1, 'file': <
open file 'testfile', mode 'at' at 0x00B46CA0>}


  I wonder if the logic inherent in the copy.deepcopy function could
  instead be implemented directly within object.__deepcopy__ (rather
  than the current model in which object does not have __deepcopy__).
  Then I would always have a means for simulating a call to
  deepcopy(self) based upon the super.__deepcopy__ logic.
  I wouldn't be surprised if I'm overlooking some undesirable
  consequence of such a major change in the model, but I don't see one
  upon first thought.


This deserves to be looked at with more detail. Try the python-ideas list,  
or submit a RFE to http://bugs.python.org/


--
Gabriel Genellina

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


Re: Installing 3.1 questions

2009-06-01 Thread Benjamin Kaplan
On Mon, Jun 1, 2009 at 10:15 PM,  wrote:

> Can anyone give me info on what and where Python installs on OS X?
> I ran the installers for version 2.6 and 3.0 and only installed Python
> (not IDLE, etc).  I then added this to my .profile:
>
> PATH="/Library/Frameworks/Python.framework/Versions/3.0/bin:${PATH}"
> PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:${PATH}"
>
> PATH="/System/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"
> export PATH
>
> Seems to work, but I'd like to know where Python installs its items.  I'm
> wanting to test version 3.1 and downloaded the 'Bzipped source tar ball'
> file (not sure of the difference between it and the 'Gzipped' one).  Do I
> need to run some sort of 'install' command from the Terminal to get
> 3.1 to work?  Or can I manually drag the items to a correct location and
> then update my .profile?
>
> Thanks for taking the time to look at my questions.
>

I take it you've never compiled anything yourself before, so I'll go through
it step by step. The bzip and gzip are just two different compression
methods, so it doesn't matter which tarball you download. In order to go
further, you'll need to register at developer.apple.com and download Xcode.
You won't need to use Xcode itself, but you need the compilers that come
with it. It's a pretty big download, so this will take a while.

Once you install Xcode, open up a terminal
(/Applications/Utilities/Terminal.app) and cd (change directory) to wherever
you put the extracted tarball. In that directory, run the configure file.
Since your on OS X, you probably want a framework build (installed to
/Library/Frameworks) instead of a normal build, which installs to
/usr/local. To do this, execute the command "./configure
--enable-framework". If you want to look up the other options, run
"./configure --help".

You'll probably get an error about missing dependencies. The easiest place
to find these is in a package manager such as fink (www.finkproject.org) or
macports (www.macports.org). Fink is easier and faster, but macports is
usually more up-to-date and flexible since it compiles everything locally.
Both of them will work. Just don't forget to add the paths (macports is in
/opt/local/ and fink is in /sw/)

Once the configure command works successfully, run the commands "make" and
"sudo make install". The first one will compile everything, the second will
put it where it's supposed to be. After running the latter, you'll be
prompted to enter your password. That should install it to
/Library/Frameworks/Python.framework/versions/3.1. I haven't installed 3.1
myself but assuming it does the same thing as 3.0, it should a python3.1
executable (symlink actually) to /usr/local/bin so you shouldn't need to add
anything to the path.

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


Re: Is there any module for sea tides?

2009-06-01 Thread alex23
"alejandro"  wrote:
> I found some in C but could not find in Python

The best I could find was a reference to some in-house code used by
the US National Oceanoic & Atmospheric Association:

  http://tinyurl.com/mct9zz

You might be able to contact the email address at the bottom and
inquire about the code.

Another alternative is to look into using the ctypes module to create
bindings for the C libs you found...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Not going to let this drop ...

2009-06-01 Thread Tim Chase

If anyone who reads this email has anything to offer - time,
money, equipment, whatever, I'd be *really* grateful to hear
from them. I can put you in touch with people who know about
things like the range of interface devices available for
paraplegic and quadriplegic people. Ideally the program would
be adaptable to a wide range of hardware accommodating a broad
spectrum of disability, but many quadriplegics use equipment
that emulates a standard keyboard and/or mouse, so that would 
be a great start.


While I don't have too much to offer, I'll pass along what I do have:

A) An OLPC (mostly Python-based kit) project[1] to provide simple 
icons-to-speech that sounds a bit like what you're describing. 
My addition to the thread is about Dasher[2], posted as Gumnos. 
A Dasher-like interface may allow for a greater range of 
expression with minimal "2-axis plus sip-puff" selection.  If I 
were mobility-impaired, I'd use Dasher to communicate & code 
(perhaps even coding Python atoms/tokens as their own entities 
for ease of entry :)


B) There are also groups of folks such as the IGDA (Independent 
Game Developers Association)'s SIG-Access[3] which focuses on 
promoting accessibility in gaming.  Robert Florio[4] is one of 
the folks in the SIG and has a vested interest in improving 
accessibility for mobility-impaired gamers (others on the list 
have particular interests/focii in visual, auditory or mental 
challenges).  I mention this group first because they have some 
innovative solutions for taking existing applications/games and 
either retrofitting accessibility solutions as well as exploring 
new design goals to make applications/games accessible upon 
launch.  Other similar groups exist for things like 
web-accessibility (WCAG WG[5], WAG[6]) but that seems a little 
outside your focus.


Hope this gives you some ideas/connections that help you out.

-tkc


[1]
http://www.olpcnews.com/software/applications/free_icon-to-speech_open-source_speech_for_disabled.html

[2]
http://www.inference.phy.cam.ac.uk/dasher/

[3]
http://www.igda.org/wiki/index.php/Game_Accessibility_SIG

[4]
http://www.robertflorio.com

[5]
http://www.w3.org/WAI/PA/

[6]
http://www.bristol.ac.uk/webaccessibility/




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


Re: how to get the path of a module (myself) ?

2009-06-01 Thread Carl Banks
On Jun 1, 4:46 pm, Stef Mientki  wrote:
> MRAB wrote:
> > Stef Mientki wrote:
> >> hello,
>
> >> I've pictures stored in a path relative to my python source code.
> >> To get a picture, I need to know what path I'm on in each python module.
> >> I thought __file__ would do the job,
> >> but apparently I didn't read the documentation carefully enough,
> >> because file is the path to the module that called my module.
>
> >> Any ways to get the path of "myself" ?
>
> > I'm not sure what you mean. I just did a quick test.
>
> > # File: C:\Quick test\child.py
> > print "name is %s" % __name__
> > print "file is %s" % __file__
>
> > # File: C:\Quick test\parent.py
> > import child
>
> > print "name is %s" % __name__
> > print "file is %s" % __file__
>
> > # Output:
> > name is child
> > file is C:\Quick test\child.py
> > name is __main__
> > file is C:\Quick test\parent.py
>
> Yes, that's what I (and many others) thought,
> but now put your code in a file, let's say the file "test.py",
> and now run this file by :
>     execfile ( 'test.py' )

How can you execfile test.py without knowing the path to it?  Whatever
path you used to locate test.py is where the file is.  If you're just
execfiling a bare filename, then it's in the current directory, so
just construct a relative pathname.


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


Installing 3.1 questions

2009-06-01 Thread jyoung79
Can anyone give me info on what and where Python installs on OS X?  
I ran the installers for version 2.6 and 3.0 and only installed Python 
(not IDLE, etc).  I then added this to my .profile:

PATH="/Library/Frameworks/Python.framework/Versions/3.0/bin:${PATH}"
PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:${PATH}"
PATH="/System/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"
export PATH

Seems to work, but I'd like to know where Python installs its items.  I'm 
wanting to test version 3.1 and downloaded the 'Bzipped source tar ball' 
file (not sure of the difference between it and the 'Gzipped' one).  Do I 
need to run some sort of 'install' command from the Terminal to get 
3.1 to work?  Or can I manually drag the items to a correct location and 
then update my .profile?

Thanks for taking the time to look at my questions.

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


Re: how to get the path of a module (myself) ?

2009-06-01 Thread Steven D'Aprano
On Tue, 02 Jun 2009 02:55:13 +0200, Stef Mientki wrote:

>> What are you trying to do? Using execfile is probably not the right
>> solution.
>>
>>
> Maybe you're right, and it's not the best solution for my problem. I've
> written a program, that contains many files, both python files and data
> files,
> and I would like to distribute the program. For Linux, I'll just bundle
> the files in a zip-file, but for windows I want to make a one button
> installer, and the files generated by Py2Exe, don't work at all.

Define "don't work at all".


> Through this discussion, I discovered another problem, because __file__
> isn't the current file, I can't run 1 module(file) from another module
> (file) .
...

It sounds like you are trying to fight Python's module system instead of 
working with it. In Python programs, you don't "run" other modules, you 
import them, then call functions inside the module. You treat the modules 
you write exactly the same as standard library modules.

As a general rule, you would never say:

execfile('/usr/lib/python2.5/os.py')

Instead you would say:

import os
os.some_function()


The same rule applies for your modules. As a general rule, NEVER say:

execfile('mymodule.py')

instead do:

import mymodule
mymodule.some_function()
mymodule.another_function()


(There are exceptions, but if you need to ask what they are, you're not 
ready to learn them! *wink*)



> The structure of my files is something like this:
> 
> Base_Path
>   Main_Program_Path
> Main_Program_1.py
> Main_Program_2.py
> Brick_Path
>   Brick_1.py
>   Brick_2.py
>   Support_Libraries
> Support_Library_1.py
> Support_Library_2.py
>   Sounds_Path
> Sound_1.wav
> Sound_2.wav
>   Picture_Path
> Picture_1.png
> Picture_2.bmp


Are these packages? Or just random folders with random files in them?


> The Main_Programs, should be able to "run/launch" other Main_Programs
> and Support_Libraries,
> in several ways (wait / nowait, fetch output or not, ... ). So each
> Support_Libraries will have a "main-section". 


Sounds like a horribly fragile, complicated and complex way of doing 
things. 


> Everything is highly
> dynamical, just dumping a new py-file in the Brick_Path, will make the
> py-files available ( i.e. directly visible and usable to the user) in
> all Main_Programs.

Sounds like you're trying to implement plugins. For that, __import__() is 
your friend. execfile() is *not* your friend -- you might think it is, 
but don't listen to it. It's like one of those guys who calls you "buddy" 
and talks you into doing things that you regret the next day. Trust me, 
use execfile() and you'll end up waking up naked in an alleyway in Vegas 
married to someone called Lula-Mae, with no memory of the last three days 
and a tattoo of Paris Hilton on your butt.


> Distributing the files through Py2Exe doesn't work at all. So I was
> thinking of a hack:

As soon as you start thinking of using a hack in production code, go take 
a cold shower and lie down until the urge goes away.


> Any suggestions ?

Have you tried to find out why Py2Exe is broken and fix that?



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


Re: Generating all combinations

2009-06-01 Thread Steven D'Aprano
On Mon, 01 Jun 2009 17:24:49 -0700, Mensanator wrote:

> On Jun 1, 6:40 pm, Steven D'Aprano  cybersource.com.au> wrote:
>> On Mon, 01 Jun 2009 11:23:35 -0700, Mensanator wrote:
>> > I believe the name you're looking for is
>> > combinations_with_replacement. It is one of the features being added
>> > to 3.1 which should give all the subsets of the Cartesian Product:
>>
>> > permutations_with_replacement:    product()
>> > combinations_with_replacement:    combinations_with_replacement()
>> > permutations_without_replacement: permutations()
>> > combinations_without_replacement: combinations()
>>
>> What, no partitions?
> 
> Itertools does partitions?

Er, no. That's why I asked "What, no partitions?" instead of saying 
"Look, itertools also does partitions!"


>> http://en.wikipedia.org/wiki/Partition_of_a_set
> 
> I didn't see any reference to Cartesian Product there.

Wikipedia is the encyclopedia anyone can edit. Go right ahead and put it 
in if you think it needs to be there. While you're at it, there is no 
mention of Cartesian Product in any of

http://en.wikipedia.org/wiki/Permutations
http://en.wikipedia.org/wiki/Combinations

http://mathworld.wolfram.com/Permutation.html
http://mathworld.wolfram.com/k-Subset.html

either. Are you sure that permutations and combinations are subsets of 
the Cartesian Product?



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


Re: how to get the path of a module (myself) ?

2009-06-01 Thread Stef Mientki

Steven D'Aprano wrote:

On Tue, 02 Jun 2009 01:46:23 +0200, Stef Mientki wrote:

  

MRAB wrote:


Stef Mientki wrote:
  

hello,

I've pictures stored in a path relative to my python source code. To
get a picture, I need to know what path I'm on in each python module.
I thought __file__ would do the job,
but apparently I didn't read the documentation carefully enough,
because file is the path to the module that called my module.

Any ways to get the path of "myself" ?



I'm not sure what you mean. I just did a quick test.

# File: C:\Quick test\child.py
print "name is %s" % __name__
print "file is %s" % __file__

# File: C:\Quick test\parent.py
import child

print "name is %s" % __name__
print "file is %s" % __file__

# Output:
name is child
file is C:\Quick test\child.py
name is __main__
file is C:\Quick test\parent.py
  

Yes, that's what I (and many others) thought, but now put your code in a
file, let's say the file "test.py", and now run this file by :
execfile ( 'test.py' )



In that case, test.py is not a module. It's just a file that by accident 
has a .py extension, which is read into memory and executed.


If you bypass the module mechanism, don't be surprised that you've 
bypassed the module mechanism :)


What are you trying to do? Using execfile is probably not the right 
solution.


  

Maybe you're right, and it's not the best solution for my problem.
I've written a program, that contains many files, both python files and 
data files,

and I would like to distribute the program.
For Linux, I'll just bundle the files in a zip-file,
but for windows I want to make a one button installer,
and the files generated by Py2Exe, don't work at all.

Through this discussion, I discovered another problem,
because __file__ isn't the current file,
I can't run 1 module(file) from another module (file) .

The structure of my files is something like this:

Base_Path
 Main_Program_Path
   Main_Program_1.py
   Main_Program_2.py
   Brick_Path
 Brick_1.py
 Brick_2.py
 Support_Libraries
   Support_Library_1.py
   Support_Library_2.py
 Sounds_Path
   Sound_1.wav
   Sound_2.wav
 Picture_Path
   Picture_1.png
   Picture_2.bmp

The Main_Programs, should be able to "run/launch" other Main_Programs 
and Support_Libraries,

in several ways (wait / nowait, fetch output or not, ... ).
So each Support_Libraries will have a "main-section".
Everything is highly dynamical, just dumping a new py-file in the 
Brick_Path, will make the py-files available ( i.e. directly visible and 
usable to the user) in all Main_Programs.


Moving the complete file-structure to Linux or Windows works good.

Distributing the files through Py2Exe doesn't work at all.
So I was thinking of a hack:
- make dummy programs, that will start Main_Program_x.py through a 
execfile function

- Create executables with Py2Exe of the dummy programs
- add manually the whole directory structure to the files generated by 
Py2Exe

- automate the above process by Inno setup

Any suggestions ?

thanks,
Stef


  






  


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


Absolute imports in Python 2.4

2009-06-01 Thread Steven D'Aprano
I have a package which includes a module which shadows a module in the 
standard library. For example:

package
+-- __init__.py
+-- ham.py
+-- spam.py
+-- sys.py

Inside that package, I want to import the standard library sys. In other 
words, I want an absolute import. In Python 2.7, absolute imports will be 
the default, and "import sys" will import the standard library module. To 
get to the package.sys module, I'll need "from . import sys".

In Python 2.5 and 2.6, relative imports are the default, and package.sys 
will shadow the std lib version. I can say:

from __future__ import absolute_import

to use the Python 2.7 behaviour.

What can I do in Python 2.4 to get an absolute import?

I've read PEP 328 and googled, but haven't found any useful advice other 
than "well don't do that then". Plenty of pages complaining about 
relative imports, but I haven't found any work-arounds others than 
renaming the offending module. Are there any other ways around this?

http://www.python.org/dev/peps/pep-0328/



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


Re: Generating all combinations

2009-06-01 Thread Mensanator
On Jun 1, 6:40 pm, Steven D'Aprano  wrote:
> On Mon, 01 Jun 2009 11:23:35 -0700, Mensanator wrote:
> > I believe the name you're looking for is combinations_with_replacement.
> > It is one of the features being added to 3.1 which should give all the
> > subsets of the Cartesian Product:
>
> > permutations_with_replacement:    product()
> > combinations_with_replacement:    combinations_with_replacement()
> > permutations_without_replacement: permutations()
> > combinations_without_replacement: combinations()
>
> What, no partitions?

Itertools does partitions?

>
> http://en.wikipedia.org/wiki/Partition_of_a_set

I didn't see any reference to Cartesian Product there.

>
> --
> Steven

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


Re: What text editor is everyone using for Python

2009-06-01 Thread MRAB

Emile van Sebille wrote:

On 6/1/2009 4:57 PM Steven D'Aprano said...
Having noted that the word "Quit" does appear, how do you then 
*actually* Quit? Apart from taunting the user, what is it that Ctrl-G 
is actually doing when it displays the word "Quit" in what seems to be 
some sort of status bar?


Ahhh.. memories of discovering that F7 gets you out of WordPerfect...


And if you need help, just press F1, uh, I mean F3... :-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to get the path of a module (myself) ?

2009-06-01 Thread MRAB

Stef Mientki wrote:

MRAB wrote:

Stef Mientki wrote:

hello,

I've pictures stored in a path relative to my python source code.
To get a picture, I need to know what path I'm on in each python module.
I thought __file__ would do the job,
but apparently I didn't read the documentation carefully enough,
because file is the path to the module that called my module.

Any ways to get the path of "myself" ?


I'm not sure what you mean. I just did a quick test.

# File: C:\Quick test\child.py
print "name is %s" % __name__
print "file is %s" % __file__

# File: C:\Quick test\parent.py
import child

print "name is %s" % __name__
print "file is %s" % __file__

# Output:
name is child
file is C:\Quick test\child.py
name is __main__
file is C:\Quick test\parent.py

Yes, that's what I (and many others) thought,
but now put your code in a file, let's say the file "test.py",
and now run this file by :
   execfile ( 'test.py' )


You didn't say you were using execfile.

# File: C:\Quick test\main.py
parent_path = r"C:\Quick test\parent.py"
execfile(parent_path, {"__file__": parent_path})

# Output:
name is child
file is C:\Quick test\child.pyc
name is __builtin__
file is C:\Quick test\parent.py


Notice how the extension of the child is now .pyc because it has already
been compiled.

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


Re: What text editor is everyone using for Python

2009-06-01 Thread Emile van Sebille

On 6/1/2009 4:57 PM Steven D'Aprano said...
Having noted that the word "Quit" does appear, how do you then *actually* 
Quit? Apart from taunting the user, what is it that Ctrl-G is actually 
doing when it displays the word "Quit" in what seems to be some sort of 
status bar?


Ahhh.. memories of discovering that F7 gets you out of WordPerfect...

Emile

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


Re: how to get the path of a module (myself) ?

2009-06-01 Thread Dave Angel

Stef Mientki wrote:

MRAB wrote:

Stef Mientki wrote:

hello,

I've pictures stored in a path relative to my python source code.
To get a picture, I need to know what path I'm on in each python 
module.

I thought __file__ would do the job,
but apparently I didn't read the documentation carefully enough,
because file is the path to the module that called my module.

Any ways to get the path of "myself" ?


I'm not sure what you mean. I just did a quick test.

# File: C:\Quick test\child.py
print "name is %s" % __name__
print "file is %s" % __file__

# File: C:\Quick test\parent.py
import child

print "name is %s" % __name__
print "file is %s" % __file__

# Output:
name is child
file is C:\Quick test\child.py
name is __main__
file is C:\Quick test\parent.py

Yes, that's what I (and many others) thought,
but now put your code in a file, let's say the file "test.py",
and now run this file by :
   execfile ( 'test.py' )

cheers,
Stef



Your original post asked about "what path I'm on in each python 
module".  Now it turns out you're using execfile(), which doesn't create 
a module, it shortcircuits most of that.


So why not import the file as a module instead of using execfile?   
Maybe using  __import__() ?



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


Re: how to get the path of a module (myself) ?

2009-06-01 Thread Steven D'Aprano
On Tue, 02 Jun 2009 01:46:23 +0200, Stef Mientki wrote:

> MRAB wrote:
>> Stef Mientki wrote:
>>> hello,
>>>
>>> I've pictures stored in a path relative to my python source code. To
>>> get a picture, I need to know what path I'm on in each python module.
>>> I thought __file__ would do the job,
>>> but apparently I didn't read the documentation carefully enough,
>>> because file is the path to the module that called my module.
>>>
>>> Any ways to get the path of "myself" ?
>>>
>> I'm not sure what you mean. I just did a quick test.
>>
>> # File: C:\Quick test\child.py
>> print "name is %s" % __name__
>> print "file is %s" % __file__
>>
>> # File: C:\Quick test\parent.py
>> import child
>>
>> print "name is %s" % __name__
>> print "file is %s" % __file__
>>
>> # Output:
>> name is child
>> file is C:\Quick test\child.py
>> name is __main__
>> file is C:\Quick test\parent.py
> Yes, that's what I (and many others) thought, but now put your code in a
> file, let's say the file "test.py", and now run this file by :
> execfile ( 'test.py' )

In that case, test.py is not a module. It's just a file that by accident 
has a .py extension, which is read into memory and executed.

If you bypass the module mechanism, don't be surprised that you've 
bypassed the module mechanism :)

What are you trying to do? Using execfile is probably not the right 
solution.



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


Re: What text editor is everyone using for Python

2009-06-01 Thread Steven D'Aprano
On Tue, 02 Jun 2009 10:54:48 +1200, Lawrence D'Oliveiro wrote:

> In message , Albert van der Horst wrote:
> 
>> An indication of how one can see one is in emacs is also appreciated.
> 
> How about, hit CTRL/G and see if the word "Quit" appears somewhere.

Ah, one has to love user interfaces designed with mnemonic keyboard 
commands so as to minimize the burden of rote learning on the user. 
Presumably it is G for "Get me the frack outta here!".

Having noted that the word "Quit" does appear, how do you then *actually* 
Quit? Apart from taunting the user, what is it that Ctrl-G is actually 
doing when it displays the word "Quit" in what seems to be some sort of 
status bar?



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


Creating a Google Code project for GSoC

2009-06-01 Thread Eric Pruitt
Hello everyone,

I am a student working on GSoC 2009 for PSF. My proposal involves making
changes to the subprocess module and subprocess.Popen. I wish to create a
Google Code project to host my changes so that I can receive feedback from
the community. Some of the code I have incorporated falls under an MIT
license. Python's license is not GPL but is GPL compatible. What license
should the Google Code project fall under? MIT, GPL or something else?

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


Re: how to get the path of a module (myself) ?

2009-06-01 Thread Stef Mientki

Thanks David,
but 

David Lyon wrote:

On Mon, 01 Jun 2009 23:28:16 +0200, Stef Mientki 
wrote:
  

hello,

I've pictures stored in a path relative to my python source code.
To get a picture, I need to know what path I'm on in each python module.
I thought __file__ would do the job,
but apparently I didn't read the documentation carefully enough,
because file is the path to the module that called my module.

Any ways to get the path of "myself" ?



This ain't the official way... but the hackers way.

Check site.path (import site)...
  

always return None in my case

If your module got loaded, and it's own succinct directory or .egg, then
it will have been added to site.path. 


You might have to parse the values in site.path but we're only talking
a few lines of code because you already know the package name.

If not, there's another way through pkg_utils...
  

I don't seem to have pkg_utils,
only pkgutil, which doesn't have an apropiate function.

But I found another way, don't know if that's reliable:
import inspect
print inspect.currentframe().f_code.co_filename

cheers,
Stef

Regards

David

  


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


Re: how to get the path of a module (myself) ?

2009-06-01 Thread Stef Mientki

MRAB wrote:

Stef Mientki wrote:

hello,

I've pictures stored in a path relative to my python source code.
To get a picture, I need to know what path I'm on in each python module.
I thought __file__ would do the job,
but apparently I didn't read the documentation carefully enough,
because file is the path to the module that called my module.

Any ways to get the path of "myself" ?


I'm not sure what you mean. I just did a quick test.

# File: C:\Quick test\child.py
print "name is %s" % __name__
print "file is %s" % __file__

# File: C:\Quick test\parent.py
import child

print "name is %s" % __name__
print "file is %s" % __file__

# Output:
name is child
file is C:\Quick test\child.py
name is __main__
file is C:\Quick test\parent.py

Yes, that's what I (and many others) thought,
but now put your code in a file, let's say the file "test.py",
and now run this file by :
   execfile ( 'test.py' )

cheers,
Stef
--
http://mail.python.org/mailman/listinfo/python-list


Re: Generating all combinations

2009-06-01 Thread Steven D'Aprano
On Mon, 01 Jun 2009 11:23:35 -0700, Mensanator wrote:

> I believe the name you're looking for is combinations_with_replacement.
> It is one of the features being added to 3.1 which should give all the
> subsets of the Cartesian Product:
> 
> permutations_with_replacement:product()
> combinations_with_replacement:combinations_with_replacement()
> permutations_without_replacement: permutations()
> combinations_without_replacement: combinations()

What, no partitions?

http://en.wikipedia.org/wiki/Partition_of_a_set



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


Re: Python, Tkinter and popen problem

2009-06-01 Thread norseman

MRAB wrote:

norseman wrote:

Piet van Oostrum wrote:

norseman  (n) wrote:

[snip]

n> Some questions:
n> 1) "...], stdout=PIPE).stdout
n>^^  why the double use?


It is not a double use. Popen(["z6.py"], stdout=PIPE) gives you a Popen
object, not a file object. If you add .stdout you get the stdout
attribute of the Popen object of which you just before stated that it
should be a pipe. So the stdout=PIPE parameter makes it create a pipe,
and the .stdout returns you that pipe.



I rather thought it might be something like the military:
Company - Dress Right - Dress
Get the attention, state what is to be done, order it done. :)

Python goes to great lengths to "be helpful" but drops the ball on the 
obvious.  stdout=PIPE means the user wants stdout piped back so it 
should be helpful and do the obvious rather than have the user be 
redundant.



What if the user requests both stdin and stdout? and what about all the
other useful bits which the returned object provides?


---
"... stdin and stdout?..."
  I tried that - results were even worse.  Have to kill the window to 
get out.  (both redirects are to master who is not running, until child 
dies. meanwhile keyboard is effectively locked out)


"... other useful bits..."
  Beauty is in the eye of the beholder. Or so the saying goes.
  "Useful" to one may not be so to another.  It is kinda like this:
I have been in some beautifully painted cars. Really great workmanship. 
They had lots of 'extras' too. Soft leather seats, built in two way 
radios and built in mobile phones and built in ice makers and the list 
goes on.  But in the USofA Western States that have lots of dirt and 
gravel roads they are a complete waste of time, effort and money. Two 
weeks and they look like any other beat up jalopy.  The dust gets into 
everything.  Ever wanted to put ice cubes full of frozen dust and grit 
into your favorite drink?  The ice maker had been installed into a 
friend's old pickup a week or so earlier. A month or so later I noticed 
it had been removed.  "Useful" is a vague measurement at best.



By the way - bits and pieces as told by yourself and Peter and Piet have 
accomplished painting a useful picture for me.


My thanks to all three of you.


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


Re: screen scraping

2009-06-01 Thread David

ubuntu@gmail.com wrote:

Hello,
does anybody have a simple tutorial for screen scrapping?

I want to extract IP addresses from particular web page, reading
documentation for a couple of days and writing some really simple
scripts, but cant get it to work.

Did anybody see any manual explicittly for screen scraping?

Thanks.

How about this:
http://iwiwdsmp.blogspot.com/2007/02/how-to-use-python-and-beautiful-soup-to.html

--
Powered by Gentoo GNU/Linux
http://linuxcrazy.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple metaclass code failing

2009-06-01 Thread LittleGrasshopper
On Jun 1, 3:44 am, Piet van Oostrum  wrote:
> > Piet van Oostrum  (I) wrote:
> >I> But your class definition:
> >I> class C3(C1, C2):
> >I> says that C1 should be before C2. Conflict!!
> >I> Change it to class C3(C2, C1):
>
> Of course the C1 is then superfluous.
>
> I wonder why you want this. What is the problem you want to solve?
>
> Apart from the metaclasses (that you still can use with `class C3(C2)')
> I could think of the following use case:
>
> class C1(object):
>       def m1(self):
>           return 'C1.m1'
>
> class C2(C1):
>       # override m1
>       def m1(self):
>           return 'C2.m1'
>       def m2(self):
>           return 'C2.m2'+self.m1()
>
> class C3(C1, C2):
>       def test(self):
>           print self.m1()+self.m2()
>
> i.e. in C3 we `ignore' the override of m1 in C2 but still want to make
> use of the m2 from C2.
>
> The question that arises then is: the self.m1() inside m2, which m1
> should it use? For an instance of C3 it would use C1.m1, but for an
> instance of C2 it would use C2.m2.
>
> However, every instance of C3 can also be considered an instance of C2,
> (Liskov substitution principle), therefore there is a conflict. That is
> exactly the conflict that the MRO signals.
>
> If you want that kind of behaviour it can be solved by using a mixin
> class for the m2 method:
>
> class C1(object):
>      __metaclass__ = M1
>      def m1(self):
>          return 'C1.m1'
> class Cmix(object):
>      def m2(self):
>          return 'C2.m2'+self.m1()
> class C2(C1, Cmix):
>      __metaclass__ = M2
>      # override m1
>      def m1(self):
>          return 'C2.m1'
> class C3(C1, Cmix):
>      __metaclass__ = M3
>      def test(self):
>          print self.m1()+self.m2()
>
> --
> Piet van Oostrum 
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: p...@vanoostrum.org

Your discussion of this scenario, why it justifiably fails, and the
alternative solution using a mixing, is all excellent. I am going to
keep this for future reference. I am at a stage of my Python journey
that some of these things seem rather mysterious, so I especially
appreciate this very detailed discussion.

To answer your question, I actually got this hierarchy directly from
Guido's paper on class unification that came out with 2.2.3.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple metaclass code failing

2009-06-01 Thread LittleGrasshopper
On Jun 1, 2:38 am, Piet van Oostrum  wrote:
> > LittleGrasshopper  (L) wrote:
> >L> On May 31, 3:59 pm, Carl Banks  wrote:
> >>> On May 31, 3:52 pm, LittleGrasshopper  wrote:
>
> >>> > This is some simple code which I got from Guido's paper on the
> >>> > unification of classes and types, which Arnaud suggested to improve my
> >>> > knowledge of metaclasses:
>
> >>> > class M1(type):
> >>> >     pass
> >>> > class M2(M1):
> >>> >     pass
> >>> > class M3(M2):
> >>> >     pass
> >>> > class C1:
> >>> >     __metaclass__ = M1
> >>> > class C2(C1):
> >>> >     __metaclass__ = M2
> >>> > class C3(C1, C2):
> >>> >     __metaclass__ = M3
>
> >>> > It is failing when processing C3:
> >>> > Traceback (most recent call last):
> >>> >   File "metaerror.py", line 18, in 
> >>> >     class C3(C1, C2):
> >>> > TypeError: Error when calling the metaclass bases
> >>> >     Cannot create a consistent method resolution
> >>> > order (MRO) for bases C2, C1
>
> [snip]
>
> >L> I guess the resulting MROs do not satisfy monotonicity, I
> >L> just have to find out how to derive the MRO. I had the notion that it
> >L> was constructed by listing the current class, followed by the MROs of
> >L> each base in order, and then retaining the rightmost instance of each
> >L> class in the MRO list, but I think that might be an
> >L> oversimplification, since in this case that would be:
> >L> (C1, object)
> >L> (C2, C1, object)
> >L> (C3, C2, C1, object)
> >L> And I don't see any issues. But I'll read the paper to figure out what
> >L> the problem is, thanks.
>
> Here is exactly the problem. Merging the two MRO's as you state would
> give C3, C2, C1, object, i.e. C2 before C1.
>
> But your class definition:
>
> class C3(C1, C2):
> says that C1 should be before C2. Conflict!!
> Change it to class C3(C2, C1):
>
> You see it has nothing to do with the metaclasses. The following code
> gives the same error:
>
> class C1(object):
>     pass
> class C2(C1):
>     pass
> class C3(C1, C2):
>     pass
>
> --
> Piet van Oostrum 
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: p...@vanoostrum.org

Thank you, Piet. You are correct. I actually went through the whole C3
MRO paper from Michele and I realized the issue (local precedence is
not maintained.) And, like you said, the way to fix it is to change
the order of the bases in C3.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-06-01 Thread Lawrence D'Oliveiro
In message , Albert van der Horst wrote:

> An indication of how one can see one is in emacs is also appreciated.

How about, hit CTRL/G and see if the word "Quit" appears somewhere.

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


Re: What text editor is everyone using for Python

2009-06-01 Thread Lawrence D'Oliveiro
In message , Albert van der Horst wrote:

> You can carry vim (or Edwin's editor for that matter) on a FAT
> [USB] stick.
> (As they say: "Speak softly, and carry a large stick.")

I like that. :)

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


Re: how to get the path of a module (myself) ?

2009-06-01 Thread MRAB

Stef Mientki wrote:

hello,

I've pictures stored in a path relative to my python source code.
To get a picture, I need to know what path I'm on in each python module.
I thought __file__ would do the job,
but apparently I didn't read the documentation carefully enough,
because file is the path to the module that called my module.

Any ways to get the path of "myself" ?


I'm not sure what you mean. I just did a quick test.

# File: C:\Quick test\child.py
print "name is %s" % __name__
print "file is %s" % __file__

# File: C:\Quick test\parent.py
import child

print "name is %s" % __name__
print "file is %s" % __file__

# Output:
name is child
file is C:\Quick test\child.py
name is __main__
file is C:\Quick test\parent.py
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to get the path of a module (myself) ?

2009-06-01 Thread David Lyon
On Mon, 01 Jun 2009 23:28:16 +0200, Stef Mientki 
wrote:
> hello,
> 
> I've pictures stored in a path relative to my python source code.
> To get a picture, I need to know what path I'm on in each python module.
> I thought __file__ would do the job,
> but apparently I didn't read the documentation carefully enough,
> because file is the path to the module that called my module.
> 
> Any ways to get the path of "myself" ?

This ain't the official way... but the hackers way.

Check site.path (import site)...

If your module got loaded, and it's own succinct directory or .egg, then
it will have been added to site.path. 

You might have to parse the values in site.path but we're only talking
a few lines of code because you already know the package name.

If not, there's another way through pkg_utils...

Regards

David

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


Re: Ah, ctypes

2009-06-01 Thread Nick Craig-Wood
David Bolen  wrote:
>  Nick Craig-Wood  writes:
> 
> > ctypes could potentially note that function types don't have enough
> > references to them when passed in as arguments to C functions?  It
> > might slow it down microscopically but it would fix this problem.
> 
>  Except that ctypes can't know the lifetime needed for the callbacks.  If
>  the callbacks are only used while the called function is executing (say,
>  perhaps for a progress indicator or internal completion callback) then
>  it's safe to create the function wrapper just within the function
>  call.

Good point...  However I wouldn't mind if ctypes emitted a warning or
even threw an exception in this case too though as the other case is
so invidious.

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


how to get the path of a module (myself) ?

2009-06-01 Thread Stef Mientki

hello,

I've pictures stored in a path relative to my python source code.
To get a picture, I need to know what path I'm on in each python module.
I thought __file__ would do the job,
but apparently I didn't read the documentation carefully enough,
because file is the path to the module that called my module.

Any ways to get the path of "myself" ?

thanks,
Stef Mientki
--
http://mail.python.org/mailman/listinfo/python-list


Urllib2 proxy settings

2009-06-01 Thread K-Dawg
Hello,

I am having trouble with an application running on a linux server.  It keeps
reverting to old proxy settings and messing up the web application.  I have
checked everything I can think of, and since the application is written in
Python and uses urllib to call a web service (which is where the error is
occurring) I thought that I could open up the interpreter, import URLLib2
and then see what it thinks the proxy is.

How can I just echo this in the interpreter?

Thanks.

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


RE: which database is suitable for small applications

2009-06-01 Thread Phil Runciman
Hi Lawrence,

I appreciate your remarks. However database engines cache their table/views to 
support sequential accessing within a set. With a good accessing scheme and 
with enough cache memory you will have all your small tables in memory. 

So the simplest thing is let the DBMS do its thing. The good ones will cope 
quite happily. You then have the advantage that the app can grow without 
program changes.

It is a long time since I delved into DBMS internals (25 years) but I cannot 
see that they will have changed from what I have said above, however I am sure 
to be corrected if I am wrong. ;-)

Cheers,

phil


-Original Message-
From: Lawrence D'Oliveiro [mailto:l...@geek-central.gen.new_zealand] 
Sent: Sunday, 31 May 2009 11:21 p.m.
To: python-list@python.org
Subject: Re: which database is suitable for small applications

In message <52801358-c037-458d-8857-
a78c2d881...@z16g2000prd.googlegroups.com>, Ankit wrote:

> If your application does not require you to add very heavy data then
> you can also use flat files to do your stuff.
> Its always a better to use Databases ...

It's not always better to use databases. By definition, data for a “small” 
application is bound to fit entirely in RAM. So certainly the simplest thing 
to do is read a whole data file in at the start, and write the entire 
updated data structure back out again at the end.


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


Re: Parsing Data

2009-06-01 Thread babypython

Thanks a lot. That works for me.



Chris Rebert-6 wrote:
> 
> On Mon, Jun 1, 2009 at 11:45 AM, babypython <2myemailaddr...@gmail.com>
> wrote:
>>
>> I am trying to parse through  this data for analysis. I am new to python
>> and
>> wondering what would be the quickest way to extract the data from this
>> file.
>> The data files consists of comments (starting with ! and #). Then, the
>> data
>> follows. All I want is the data in array ( I don't care about the
>> comments),and the data format is  freq[], s11[real], s11[imag],
>> s21[real],s21[imag]
>>
>>  Any help will be appreciated. Thanks.
>>
>>
>> !Date: Jan 29, 2008 14:40:26
>> !Correction: S11(Full 2 Port(1,2)) S21(Full 2 Port(1,2))
>> !Measurements: S11, S21:
>> ! Freq  S11[real, imag]  S21[real,imag]
>> 14 -2.104572e+001 4.153887e+001 -4.084314e+001 8.417739e+001
>> 140025 -2.089971e+001 4.028599e+001 -4.087196e+001 7.026196e+001
>> 140050 -2.114216e+001 4.086434e+001 -4.055134e+001 6.559201e+001
>> 140075 -2.112057e+001 3.681709e+001 -4.024515e+001 5.503412e+001
>> 140100 -2.110984e+001 3.622524e+001 -4.056519e+001 5.162795e+001
>> 140125 -2.123562e+001 3.602308e+001 -4.125660e+001 4.330296e+001
>> 140150 -2.152193e+001 3.345480e+001 -4.035107e+001 3.937940e+001
>> 140175 -2.144410e+001 3.189340e+001 -4.097492e+001 2.802726e+001
>> 140200 -2.155891e+001 3.002732e+001 -4.146297e+001 1.666007e+001
>> 140225 -2.170474e+001 2.896428e+001 -4.146934e+001 1.514847e+001
>> 140250 -2.185459e+001 2.863795e+001 -4.053018e+001 1.130192e+001
> 
> #completely untested
> data = []
> for line in the_file:
> if line.startswith("!"): continue
> fields = line.strip().split()
> datapoint = [int(fields[0]), complex(float(fields[1]),
> float(fields[2])), complex(float(fields[3]), float(fields[4]))]
> data.append(datapoint)
> 
> Cheers,
> Chris
> -- 
> http://blog.rebertia.com
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Parsing-Data-tp23820003p23821445.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


screen scraping

2009-06-01 Thread ubuntu . exe
Hello,
does anybody have a simple tutorial for screen scrapping?

I want to extract IP addresses from particular web page, reading
documentation for a couple of days and writing some really simple
scripts, but cant get it to work.

Did anybody see any manual explicittly for screen scraping?

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


Re: PyQt4 + WebKit

2009-06-01 Thread David Boddie
On Monday 01 June 2009 16:16, dudekks...@gmail.com wrote:

> On 31 Maj, 02:32, David Boddie  wrote:

>> So, you only want to handle certain links, and pass on to WebKit those
>> which you can't handle? Is that correct?
> 
> Yes, I want to handle external links (out of my host) and links
> starting with download://... (my specific protocol).

If you want to handle them in a different way to normal Web pages, it seems
that calling setForwardUnsupportedContent(True) on the QWebPage is the way
to do it.

However, if you want to handle those links and present the content for the
browser to render then you may need to override the browser's network access
manager, as discussed in this message:

  http://lists.trolltech.com/pipermail/qt-interest/2009-March/004279.html

I experimented a little and added an example to the PyQt Wiki:

  http://www.diotavelli.net/PyQtWiki/Using a Custom Protocol with QtWebKit

I hope it helps to get you started with your own custom protocol.

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


Re: How can I install a newer version of python

2009-06-01 Thread Chris Rebert
On Mon, Jun 1, 2009 at 12:16 PM, nathan.charles.sum...@gmail.com
 wrote:
> I am on MacOS 10.5. It has python 2.5.1 installed.
>
> How can I install a newer version of python (e.g. 3.1) without
> breaking my current environment (I am not sure what programs/scripts
> are using the python comes with MacOS). I just want to use the newer
> version of python for my own project.
>
> I found this "http://wiki.python.org/moin/MacPython/Leopard";, but i
> have no idea what that is related to my situation.
>
> Thank you for any tip.

I think you can just use the normal Mac installer from
http://www.python.org/download/. As it says on
http://wiki.python.org/moin/MacPython/Leopard, this doesn't clobber
the default Apple Python install.

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


How can I install a newer version of python

2009-06-01 Thread nathan.charles.sum...@gmail.com
I am on MacOS 10.5. It has python 2.5.1 installed.

How can I install a newer version of python (e.g. 3.1) without
breaking my current environment (I am not sure what programs/scripts
are using the python comes with MacOS). I just want to use the newer
version of python for my own project.

I found this "http://wiki.python.org/moin/MacPython/Leopard";, but i
have no idea what that is related to my situation.

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


Re: Parsing Data

2009-06-01 Thread Chris Rebert
On Mon, Jun 1, 2009 at 11:45 AM, babypython <2myemailaddr...@gmail.com> wrote:
>
> I am trying to parse through  this data for analysis. I am new to python and
> wondering what would be the quickest way to extract the data from this file.
> The data files consists of comments (starting with ! and #). Then, the data
> follows. All I want is the data in array ( I don't care about the
> comments),and the data format is  freq[], s11[real], s11[imag],
> s21[real],s21[imag]
>
>  Any help will be appreciated. Thanks.
>
>
> !Date: Jan 29, 2008 14:40:26
> !Correction: S11(Full 2 Port(1,2)) S21(Full 2 Port(1,2))
> !Measurements: S11, S21:
> ! Freq  S11[real, imag]  S21[real,imag]
> 14 -2.104572e+001 4.153887e+001 -4.084314e+001 8.417739e+001
> 140025 -2.089971e+001 4.028599e+001 -4.087196e+001 7.026196e+001
> 140050 -2.114216e+001 4.086434e+001 -4.055134e+001 6.559201e+001
> 140075 -2.112057e+001 3.681709e+001 -4.024515e+001 5.503412e+001
> 140100 -2.110984e+001 3.622524e+001 -4.056519e+001 5.162795e+001
> 140125 -2.123562e+001 3.602308e+001 -4.125660e+001 4.330296e+001
> 140150 -2.152193e+001 3.345480e+001 -4.035107e+001 3.937940e+001
> 140175 -2.144410e+001 3.189340e+001 -4.097492e+001 2.802726e+001
> 140200 -2.155891e+001 3.002732e+001 -4.146297e+001 1.666007e+001
> 140225 -2.170474e+001 2.896428e+001 -4.146934e+001 1.514847e+001
> 140250 -2.185459e+001 2.863795e+001 -4.053018e+001 1.130192e+001

#completely untested
data = []
for line in the_file:
if line.startswith("!"): continue
fields = line.strip().split()
datapoint = [int(fields[0]), complex(float(fields[1]),
float(fields[2])), complex(float(fields[3]), float(fields[4]))]
data.append(datapoint)

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


Re: Challenge supporting custom deepcopy with inheritance

2009-06-01 Thread Michael H . Goldwasser

On Monday June 1, 2009, Scott David Daniels wrote: 

>Michael H. Goldwasser wrote:
>> Chris,
>> 
>>   Thanks for your well-written reply.  Your analogy to the
>>   complexities of other special methods is well noted.  I'll accept
>>   the "small price for flexibility" that you note, if necessary.
>>   However, I still desire a cleaner solution.
>
>You seem to think that "deepcopy" is a well-defined concept.  It is
>not and cannot be.  A copy that understands its own abstractions can
>work, but sometimes a container is used as a simple abstraction, and
>sometimes it is used as a container.  The choice between the two is
>not one of structure, but one of intent.  A true deepcopy could
>survive making a copy of the number 23, but might fail as it makes
>a copy of None, True, or False.  Certainly a dictionary might or
>might not be copied, depending on how the dictionary is used.
>
>--Scott David Daniels
>scott.dani...@acm.org

Hi Scott,

  It is clear from my original statement of the question that there is
  a need for classes to be able to customize their own semantics for
  supporting the deepcopy function.  That is why the deepcopy function
  looks for a __deepcopy__ method within a class as a hook.

  My concern involves the challenge of providing a valid
  implementation for __deepcopy__ at one level of inheritance without
  being overly dependent on the internal mechanism used by ancestor
  classes in supporting deepcopy.  I don't see how your comments
  address that question.

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


Re: Ah, ctypes

2009-06-01 Thread David Bolen
Nick Craig-Wood  writes:

> ctypes could potentially note that function types don't have enough
> references to them when passed in as arguments to C functions?  It
> might slow it down microscopically but it would fix this problem.

Except that ctypes can't know the lifetime needed for the callbacks.  If
the callbacks are only used while the called function is executing (say,
perhaps for a progress indicator or internal completion callback) then
it's safe to create the function wrapper just within the function call.

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


Re: Not going to let this drop ...

2009-06-01 Thread Steve Holden
Leslie Hawthorn wrote:
> 
> 
> On Sun, May 31, 2009 at 9:35 PM, Steve Holden  > wrote:
> 
> I don't know if Google are planning to run a Highly-Open Participation
> event this year, but if they are then maybe we could establish a few
> tasks for that.
> 
> 
> We are planning to do so in Q4 of this year, further details TBD. I'll
> make sure Titus Brown is in the loop when we get started planning so he
> can point me to the right folks who'd like to help with the PSF's
> participation in GHOP this year.
> 
Thanks for letting me know. I am sure the PSF will be delighted to pitch
in, and Titus will be an entirely satisfactory first point of contact.

Hope you are well. Good to hear from you. Won't be at OSCON this year,
but maybe next year.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Watch PyCon on video now!  http://pycon.blip.tv/

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


Re: Python, Tkinter and popen problem

2009-06-01 Thread MRAB

norseman wrote:

Piet van Oostrum wrote:

norseman  (n) wrote:

[snip]

n> Some questions:
n> 1) "...], stdout=PIPE).stdout
n>^^  why the double use?


It is not a double use. Popen(["z6.py"], stdout=PIPE) gives you a Popen
object, not a file object. If you add .stdout you get the stdout
attribute of the Popen object of which you just before stated that it
should be a pipe. So the stdout=PIPE parameter makes it create a pipe,
and the .stdout returns you that pipe.



I rather thought it might be something like the military:
Company - Dress Right - Dress
Get the attention, state what is to be done, order it done. :)

Python goes to great lengths to "be helpful" but drops the ball on the 
obvious.  stdout=PIPE means the user wants stdout piped back so it 
should be helpful and do the obvious rather than have the user be 
redundant.



What if the user requests both stdin and stdout? and what about all the
other useful bits which the returned object provides?
--
http://mail.python.org/mailman/listinfo/python-list


Parsing Data

2009-06-01 Thread babypython

I am trying to parse through  this data for analysis. I am new to python and
wondering what would be the quickest way to extract the data from this file.
The data files consists of comments (starting with ! and #). Then, the data
follows. All I want is the data in array ( I don't care about the
comments),and the data format is  freq[], s11[real], s11[imag],
s21[real],s21[imag]

 Any help will be appreciated. Thanks.


!Date: Jan 29, 2008 14:40:26
!Correction: S11(Full 2 Port(1,2)) S21(Full 2 Port(1,2))
!Measurements: S11, S21:
! Freq  S11[real, imag]  S21[real,imag] 
14 -2.104572e+001 4.153887e+001 -4.084314e+001 8.417739e+001
140025 -2.089971e+001 4.028599e+001 -4.087196e+001 7.026196e+001  
140050 -2.114216e+001 4.086434e+001 -4.055134e+001 6.559201e+001 
140075 -2.112057e+001 3.681709e+001 -4.024515e+001 5.503412e+001  
140100 -2.110984e+001 3.622524e+001 -4.056519e+001 5.162795e+001  
140125 -2.123562e+001 3.602308e+001 -4.125660e+001 4.330296e+001 
140150 -2.152193e+001 3.345480e+001 -4.035107e+001 3.937940e+001
140175 -2.144410e+001 3.189340e+001 -4.097492e+001 2.802726e+001 
140200 -2.155891e+001 3.002732e+001 -4.146297e+001 1.666007e+001 
140225 -2.170474e+001 2.896428e+001 -4.146934e+001 1.514847e+001 
140250 -2.185459e+001 2.863795e+001 -4.053018e+001 1.130192e+001


-- 
View this message in context: 
http://www.nabble.com/Parsing-Data-tp23820003p23820003.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: Not going to let this drop ...

2009-06-01 Thread Leslie Hawthorn
On Sun, May 31, 2009 at 9:35 PM, Steve Holden  wrote:

> I don't know if Google are planning to run a Highly-Open Participation
> event this year, but if they are then maybe we could establish a few
> tasks for that.
>

We are planning to do so in Q4 of this year, further details TBD. I'll make
sure Titus Brown is in the loop when we get started planning so he can point
me to the right folks who'd like to help with the PSF's participation in
GHOP this year.

Cheers,
LH



-- 
Leslie Hawthorn
Program Manager - Open Source
Google Inc.

http://code.google.com/opensource/

I blog here:

http://google-opensource.blogspot.com - http://www.hawthornlandings.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python, Tkinter and popen problem

2009-06-01 Thread norseman

Piet van Oostrum wrote:

norseman  (n) wrote:



n> Piet van Oostrum wrote:

norseman  (n) wrote:

n> I have tried both and Popen2.popen2().
n> os.popen runs both way, contrary to docs.

What do you mean `os.popen runs both way'?



n> It reads from child while console writes directly to child -  thus
n> eliminating the problem of coding a pass through from master.


Yes, but that is not `both way': popen connects the parent to the child
through a pipe. The pipe works one way: from the child to the parent
with 'r' (default), from the parent to the child with 'w'. You can't
communicate the other way through the pipe. So the communication from
the parent process to the child through the popen is ONE WAY. If you
want TWO WAY communication you can use popen2, or better use
subprocess.Popen.


We are actually saying the same thing - you do say it better.




A child process always inherits stdin, stdout and stderr from the parent
unless you change that (e.g. by redirecting to a pipe, like popen does
for one of them). It doesn't matter whether you use os.popen,
subprocess.Popen, os.system, or os.fork to create the child process. So
in your case if the parent inputs from the console, so does the child.
But note: this is not communication from the parent process to the
child, but from YOU to the child. So the parent-child communication is
ONE WAY.


n> "...

doesn't work as the iterator for a file, including pipes, does a
read ahead (see the doc on file.next()) and therefore is not suitable
for interactive use.

n> ..."



n> If I understand you the above can be stated as:



n> The above does not work as an iterator for any file type, including
n> pipes, but it does do read aheads  and therefore is not suitable for
n> interactive use.


For files in general it is no problem because the contents of the file
is not interactively generated. Read ahead on a file works as long as
you do'nt use readline() on the file in between the iterator actions.
For a socket it could be the same problem if the other side generates
the output interactively.


n> If that is correct then read ahead is simply buffered file reads (grab a
n> chunk, parcel it out on demand) - yes?


I don't know, I have looked into the source code but it isn't clear to
me. I noticed that iteration didn't work and then looked up the
documentation. It talks about read ahead for efficiency.
 

n> As for "... not suitable for interactive ..." Really?  Except for
n> special purpose use the current interactive components are all buffered
n> for read ahead use.  Check the actual code for your keyboard, your mouse
n> and so forth. It's the read ahead that allows faster time to completion.
n> It's why C-code has the putch function.


Yes, but they only read what is available. The iterator apparently tries
to read more and then has to wait.


In actuality the read ahead does 'run off the end' and then waits. 
Specifics are coder dependent. But I think I understand what you mean. 
It may not be treating the incoming buffer as circular.  That could 
explain a few things I'm seeing.





n> Yes - Sync IS the bigger hammer!  If that is what is needed - so be it.
n> All character readers (byte at a time) should obey a flush(). Depending
n> on type, code for the reader controls whether or not it flushes
n> incomplete "lines" in the in-buffer(s). Proper implementation limits lost
n> data on system crash.


I don't understand what you say here. As I told before it has nothing to
do with sync(). Also for reading there is no flush(); the flush() is
done on the other side of the pipe. Yes, and sync() has to do with
system crashes but that is not what we are talking about in this thread.



The line with Sync is just a comment. Sync'ing the whole system just to 
force a singular flush is not a good way to proceed.  The comment is not 
actually connected to the comments on readers.



n> In trying to use flush at the master side I keep getting messages
n> indicating strings (completed or not) are not flushable.  Strange practice.


If you print to the console in the master the flush is done
automatically.


The symptoms have been otherwise. Going back to your comments on 
iterators not proceeding with 'in-line' processing but rather holding 
onto the bytes until later would give the effect of flush not working.





n> ---
n> from subprocess import Popen, PIPE
n> xx = Popen(["z6.py"], stdout=PIPE).stdout



n> while True:
n> line = xx.readline()
n> if not line: break
n> print "\t" + line,
n> ---
n> DOES WORK on Python 2.5.2 on Slackware 10.2 - THANK YOU VERY MUCH!!!
n> Isn't working on Windows. error message comes as one of two forms.
n>   1- %1 not found   #as shown above
n>   2- file not found #as ...["python z6.py"]...
n> same#as #2 even with full paths given


That should be Popen(["python", "z6.py"], stdout=PIPE).stdout
And then with both python and z6.py given as full paths.


n> I get the impression subprocess ignores system t

Re: Generating all combinations

2009-06-01 Thread Mensanator
On Jun 1, 10:11 am, pataphor  wrote:
> Johannes Bauer wrote:
> > Any help is appreciated!
>
> This is on the fringe of exploitation, but hey, maybe the code helps you
> think about the algorithm.
>
> IMHO the following code is a glaring complaint about the injustice of
> omission itertools inflicts on the perfectly natural and obvious
> procedure of repeat_each (whatever it's name ought to be):

I believe the name you're looking for is
combinations_with_replacement.
It is one of the features being added to 3.1 which should give all
the subsets of the Cartesian Product:

permutations_with_replacement:product()
combinations_with_replacement:combinations_with_replacement()
permutations_without_replacement: permutations()
combinations_without_replacement: combinations()

>
> from itertools  import izip, islice, cycle
>
> def repeat_each(seq,n):
>      while True:
>          for x in seq:
>              for i in range(n):
>                  yield x
>
> def repeat_all(seq,n):
>      while True:
>          for i in range(n):
>              for x in seq:
>                  yield x
>
> def product(X):
>      N = []
>      total = 1
>      for x in X:
>          N.append(total)
>          total *= len(x)
>      R = [repeat_all(repeat_each(x,k),n)
>                      for x,k,n in izip(X,N,reversed(N))]
>      return islice(izip(*R),total)
>
> def test1():
>      L = ['a', 'bc','def' ]
>      for x in product(L):
>          print x
>      print
>
> def test2():
>      repeat_all = cycle
>      test1()
>
> if __name__ == '__main__':
>      test1()
>      test2()
>
> See? Repeat_all and repeat_each are almost brothers, just separated by
> the tiniest rearrangement of their genetic code (or should I say code
> genetics?). Yet one is included as 'itertools.cycle' and the other is
> doomed to live in limbo. Such injustice! Can it be that
> 'itertools.repeat' has usurped repeat_each's rightful position?
>
> P.

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


Re: Metaclass mystery

2009-06-01 Thread LittleGrasshopper
On Jun 1, 11:11 am, Michele Simionato 
wrote:
> On Jun 1, 7:18 pm, LittleGrasshopper  wrote:
>
> > I have to thank you for all the invaluable materials you have provided
> > to the python community. The process that you followed must have been
> > incredibly arduous.
>
> *Incredibly ardous* is an exaggeration, but in the case of the MRO
> I needed to do some work of reverse engineering in order to go
> from code (which was posted by Samuele Pedroni on 
> python-devhttp://mail.python.org/pipermail/python-dev/2002-October/029176.html)
> to the rules and to figure out the algorithm. Anyway, there are
> things much more complicated than the C3 algorithm or metaclasses,
> nowadays I am fighting with the Scheme module system and I know
> what I am talking about ;-)

I saw that you were involved right now in Scheme/Lisp. Good luck with
it!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Metaclass mystery

2009-06-01 Thread Michele Simionato
On Jun 1, 7:18 pm, LittleGrasshopper  wrote:

> I have to thank you for all the invaluable materials you have provided
> to the python community. The process that you followed must have been
> incredibly arduous.

*Incredibly ardous* is an exaggeration, but in the case of the MRO
I needed to do some work of reverse engineering in order to go
from code (which was posted by Samuele Pedroni on python-dev
http://mail.python.org/pipermail/python-dev/2002-October/029176.html)
to the rules and to figure out the algorithm. Anyway, there are
things much more complicated than the C3 algorithm or metaclasses,
nowadays I am fighting with the Scheme module system and I know
what I am talking about ;-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Challenge supporting custom deepcopy with inheritance

2009-06-01 Thread Scott David Daniels

Michael H. Goldwasser wrote:

Chris,

  Thanks for your well-written reply.  Your analogy to the
  complexities of other special methods is well noted.  I'll accept
  the "small price for flexibility" that you note, if necessary.
  However, I still desire a cleaner solution.


You seem to think that "deepcopy" is a well-defined concept.  It is
not and cannot be.  A copy that understands its own abstractions can
work, but sometimes a container is used as a simple abstraction, and
sometimes it is used as a container.  The choice between the two is
not one of structure, but one of intent.  A true deepcopy could
survive making a copy of the number 23, but might fail as it makes
a copy of None, True, or False.  Certainly a dictionary might or
might not be copied, depending on how the dictionary is used.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Metaclass mystery

2009-06-01 Thread LittleGrasshopper
On Jun 1, 12:42 am, Michele Simionato 
wrote:
> On May 31, 2:32 am, LittleGrasshopper  wrote:
>
> > Seriously, metaclasses are making my brain hurt. How do people like
> > Michele Simionato and David Mertz figure these things out? Does it all
> > come to looking at the C source code for the CPython interpreter?
>
> Actually I never looked at the C source code. I performed lots of
> experiments
> and figured things out the hard way, with trial and errors.
> Metaclasses
> are not that hard, descriptors and super were much harder to grasp at
> that
> time, since there was next to zero documentation and a set of subtle
> bugs.
> For descriptors now there is Raymond Hettinger essay and for super
> there are my blog posts
> on 
> Artima:http://www.artima.com/weblogs/index.jsp?blogger=micheles&start=45&thR...
> (there are also two posts of mine about metaclasses in Python 3.0 that
> you may want to
> read)
>
> HTH,
>
>             Michele

I have to thank you for all the invaluable materials you have provided
to the python community. The process that you followed must have been
incredibly arduous. A while back I gathered a lot of papers, many of
which you either authored or co-authored, and they have been a great
help. Just yesterday I went through your paper on the MRO on the
advice of Carl Banks, and it absolutely clarified how the C3 MRO rules
work. Once you understand how the MRO works it is actually quite
intuitive, and your paper helped me understand this.

Thanks for pointing your Artima papers on super(). A few days ago I
hit a brick wall due to the terminology used regarding "bound" and
"unbound" super objects, so I went on a search for suitable materials,
and of course I found your papers on super(). I skimmed through them
enough to understand the issues that were escaping me at the moment,
but I intend to go through them completely at some point. It was quite
fortunate that your papers acted as a catalyst for the change of the
official documentation on super() in 2.6. The original documentation
was very misleading.

As you mentioned, Raymond Hettinger paper on descriptors is excellent
as well, and I highly recommend it.

Regards,

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


Re: Challenge supporting custom deepcopy with inheritance

2009-06-01 Thread Michael H . Goldwasser

Chris,

  Thanks for your well-written reply.  Your analogy to the
  complexities of other special methods is well noted.  I'll accept
  the "small price for flexibility" that you note, if necessary.
  However, I still desire a cleaner solution.

  I can examine the inherited slots to see which special methods are
  there, and to implement my own __deepcopy__ accordingly. But to do
  so well seems to essentially require reimplementing the complicated
  logic of the copy.deepcopy function.  That is, if my new class is
  the first to be implementing an explicit __deepcopy__ function, I
  seem to have no easy way to invoke the inherited version of
  "deepcopy(self)".

  I wonder if the logic inherent in the copy.deepcopy function could
  instead be implemented directly within object.__deepcopy__ (rather
  than the current model in which object does not have __deepcopy__).
  Then I would always have a means for simulating a call to
  deepcopy(self) based upon the super.__deepcopy__ logic.

  I wouldn't be surprised if I'm overlooking some undesirable
  consequence of such a major change in the model, but I don't see one
  upon first thought.
  
Michael

On Monday June 1, 2009, Gabriel Genellina wrote: 

>In general, you have to know whether A implements __deepcopy__ or not.  
>This is a small price for the flexibility (or anarchy) in the copy/pickle  
>interfases: there are several ways to implement the same thing, and you  
>have to know which one your base class has chosen in order to extend it.
>
>The following is a possible implementation that doesn't use __init__ at  
>all, so their different signature is not an issue:
>
>  # class A:
>  def __deepcopy__(self, memo={}):
>  dup = type(self).__new__(type(self))
>  dup.__aTag = self.__aTag
>  dup.__aList = copy.deepcopy(self.__aList, memo)
>  dup.__aList.reverse()
>  return dup
>
>  # class B:
>  def __deepcopy__(self, memo={}):
>  dup = A.__deepcopy__(self, memo)
>  dup.__bTag = self.__bTag
>  dup.__bList = copy.deepcopy(self.__bList, memo)
>  return dup
>
>Note that A.__deepcopy__ does two things: a) create a new, empty,  
>instance; and b) transfer state. B.__deepcopy__ handles *its* portion of  
>state only. This can be written in a more generic way, relying on  
>__getstate__/__setstate__ (the methods that should return the current  
>state of the object):
>
>  # class A:
>  def __deepcopy__(self, memo={}):
>  dup = type(self).__new__(type(self))
>  if hasattr(self, '__getstate__'): state = self.__getstate__()
>  else: state = self.__dict__
>  state = copy.deepcopy(state, memo)
>  if hasattr(dup, '__setstate__'): dup.__setstate__(state)
>  else: dup.__dict__.update(state)
>  dup.__aList.reverse()
>  return dup
>
>  # remove __deepcopy__ definition from class B
>
>Now, B (and any other subclass) is concerned only with __getstate__ /  
>__setstate__, and only when the default implementation isn't appropriate.
>
>> As another basic puzzle, consider a class definition for B where B has
>> a registry list that it doesn't want cloned for the new instance (but
>> it does want pickled when serialized).  This would seem to require
>> that B implement its own __deepcopy__.   We want to somehow rely on
>> the class definition for A to enact the cloning fo the state defined
>> by A.   But without knowing about how A supports the deepcopy
>> semantics, I don't see how to accomplish this goal.
>
>I don't understand such bizarre requirement, but anyway, you can override  
>__deepcopy__ (make B fix only the part that the default implementation  
>doesn't implement well)
>
>  # A.__deepcopy__ as above
>
>  # class B:
>  def __deepcopy__(self, memo={}):
>  dup = A.__deepcopy__(self, memo)
>  dup.__registry = self.__registry
>  return dup
>
>This [the need to know how certain feature is implemented in the base  
>class] is not special or peculiar to pickle/copy, although the multiple  
>(and confusing) ways in which a class can implement pickling doesn't help  
>to understand the issue very well.
>
>Consider the + operator: when some subclass wants to implement addition,  
>it must know which of the several special methods involved (__add__,  
>__iadd__, __radd__) are implemented in the base class, in order to  
>extend/override them. Same for __cmp__/__eq__/__hash__: you can't just  
>ignore what your base class implements. All of this applies to other  
>languages too, but in Python, there is an additional consideration: the  
>mere *existence* of some methods/attributes

Re: Metaclass mystery

2009-06-01 Thread LittleGrasshopper
On Jun 1, 12:18 am, Lie Ryan  wrote:
> LittleGrasshopper wrote:
> > On May 31, 2:03 pm, a...@pythoncraft.com (Aahz) wrote:
> >> In article 
> >> ,
>
> >> LittleGrasshopper   wrote:
>  On May 31, 12:19=A0am, Arnaud Delobelle  wrote:
> > [1]http://www.python.org/download/releases/2.2.3/descrintro/
> >>> I'm about 2/3 of the way through this paper (although I don't claim to
> >>> understand all of it.) There is some heavy duty material in there,
> >>> enough to make me feel really stupid and frustrated at times. I'm
> >>> making connections as I go though, hopefully everything will sink in
> >>> eventually.
> >>> Is this stuff actually tough, or am I just a dummy?
> >> Definitely tough!  Metaclasses can cause dain bramage.
> >> --
> >> Aahz (a...@pythoncraft.com)           <*>        
> >> http://www.pythoncraft.com/
>
> >> my-python-code-runs-5x-faster-this-month-thanks-to-dumping-$2K-
> >>     on-a-new-machine-ly y'rs  - tim
>
> > Good to know, I'll stick to it and persevere. Will check doctor
> > regularly for dain bramage though.
>
> Fortunately python makes it rare that we actually need to use metaclass.
> Especially with class decorators and such...
>
> With that said, the rare cases where it is really needed; brain
> hemorrhage is not only possible but when.

Lie,

I understand what you mean about metaclasses not being something that
everybody needs to master on an everyday basis. Unfortunately, I'm the
type of person that has to know how things work. I consider this an
unfortunate defect, due to the fact that I am unable to compromise and
let go even when practical considerations would dictate that is the
appropriate course of action.

I'm going to stick with this. I haven't even read the official
Reference manual, so maybe I'm getting ahead of myself though. My
plans are to read the reference manual, then selected parts of the
library manual, and take it from there.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: BMP32 for linux

2009-06-01 Thread Scott David Daniels

(1) Please don't top post; it is not the norm here.
Re-arrange and prune as I've done below.

Djames Suhanko wrote:
>  wrote:
>> Djames Suhanko wrote:
>>> Hello, all!
>>> ... the bmp32 module for linux? This module can to load bmp image
>>> with alpha channel where is not supported others images formats.
>> Hmmm, why do you think PNG and TGA do not support alpha?

The python in my embbeded system have not support to others formats. :-(
Then, I found a tip about bmp32 with alpha channel, but I don't have
this module (bmp32) in Ubuntu.

Sounds like this might be a capability problem on your embedded system.

Look for and install PIL (the Python Imaging Library).  It might help if
you are not simply running up against hardware issues.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-06-01 Thread Albert van der Horst
In article ,
Lawrence D'Oliveiro   wrote:
>In message , Lie Ryan wrote:
>
>> norseman wrote:
>>
>>> Suggestion:
>>> Take a look at the top two most used OS you use and learn the default
>>> (most often available) text editors that come with them.
>>
>> Which means Notepad on Windows?
>
>Or you could take a Linux preinstallation on a Live CD/DVD or USB stick.
>There's no Windows system so brain-dead it can't be fixed with a simple
>ctrl-alt-del. :)
>

You can carry vim (or Edwin's editor for that matter) on a FAT
stick.
(As they say: "Speak softly, and carry a large stick.")

Groetjes Albert

--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
alb...@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

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


Re: What text editor is everyone using for Python

2009-06-01 Thread Albert van der Horst
In article ,
Roy Smith   wrote:
>In article ,
> Bar Shirtcliff  wrote:
>
>> I can't say a thing about other editors, except that when some shell
>> script perversely dumped me into vi a month ago, I felt as horrified
>> as if some actually living bugs had crawled out of my own reflection
>> on the computer screen and fallen, clicking and scraping, onto the
>> keyboard.  That's a personal reaction - totally irrelevant, of course.
>
> : q ! 

Make that
  : q ! 

>All the vi you ever need to know :-)

Now some kind soul give a similar sequence for emacs.

>
>The real problem is when you get dumped into some editor other than you one
>you expected and don't realize it for a while.  It's really amazing how
>much damage you can do to a file by typing (for example) emacs commands at
>vi.  Especially if you accidentally stumble upon the sequence for "save
>file".

Exactly.
An indication of how one can see one is in emacs is also appreciated.
[Especially insidious is helpful help that lands you in emacs-ing
help file for powerful searching (not!).]

Groetjes Albert


--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
alb...@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

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


Re: BMP32 for linux

2009-06-01 Thread Djames Suhanko
Thank you for answer, Scott !

The python in my embbeded system have not support to others formats. :-(
Then, I found a tip about bmp32 with alpha channel, but I don't have
this module (bmp32) in Ubuntu.

I'm using the cx_Freeze to compile my program, but when I put them in
my embbeded system, the png images aren't supported anymore.

 I did use of  pygame.image.get_extended() function. In my desktop it
return 1, but when put in my system, this function returns 0.

Do you have some tip more?
Thank you again!!

On Mon, Jun 1, 2009 at 12:53 PM, Scott David Daniels
 wrote:
> Djames Suhanko wrote:
>>
>> Hello, all!
>> Did you know the bmp32 module for linux? This module can to load bmp
>> image with alpha channel where is not supported others images formats.
>
> Hmmm, why do you think PNG and TGA do not support alpha?
>
> --Scott David Daniels
> scott.dani...@acm.org
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Djames Suhanko
LinuxUser 158.760
-- 
http://mail.python.org/mailman/listinfo/python-list


Illegal seek with os.popen

2009-06-01 Thread pruebauno
Should I open a bug report for this?

Python 2.5.1 (r251:54863, Sep 19 2007, 14:58:06) [C] on aix5
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.popen('cat','w')

>>>

Python 3.1rc1 (r31rc1:73054, Jun  1 2009, 10:49:24) [C] on aix5
Type "help", "copyright", "credits" or "license" for more information.
>>> os.popen('cat','w')
Traceback (most recent call last):
  File "", line 1, in 
  File "/Python-3.1rc1/Lib/os.py", line 641, in popen
return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
IOError: [Errno 29] Illegal seek
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem building 64-bit python 2.6.2 on Solaris 10

2009-06-01 Thread Martin v. Löwis
> Ok, so it looks like the only option here is to use LD_LIBRARY_PATH.

Not really: there is also crle, and LD_RUN_PATH.

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


Re: subprocess and win32security.ImpersonateLoggedOnUser

2009-06-01 Thread Martin P. Hellwig

Emin.shopper Martinian.shopper wrote:

The source for subprocess just uses CreateProcess. Which means that,
short of monkey-patching it, you're going to have to roll your own
subprocess-like code (I think). Basically, you'll need to run
CreateProcessAsUser or CreateProcessAsLogonW. They're both a bit
of a pig in terms of getting the right combination of parameters
and privileges,


Thanks. I tried rolling my own via CreateProcessAsUser but it
complained about needing some special permissions so its probably not
going to work. I'd like to try CreateProcessAsLogonW but can't see how
to access that via python. I will start a new thread on the
python-win32 list about that.

Thanks,
-Emin


Maybe this post on my blog 
http://blog.dcuktec.com/2009/05/python-on-windows-from-service-launch.html
can be of some help for you, although it was more thought to run under 
LocalSystem instead of another active user.


--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: BMP32 for linux

2009-06-01 Thread Scott David Daniels

Djames Suhanko wrote:

Hello, all!
Did you know the bmp32 module for linux? This module can to load bmp
image with alpha channel where is not supported others images formats.

Hmmm, why do you think PNG and TGA do not support alpha?

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ah, ctypes

2009-06-01 Thread Nick Craig-Wood
Lawrence D'Oliveiro  wrote:
>  I wrote some code months ago to output 1-bit-per-pixel PNG files from
>  Python, doing direct calls to libpng using ctypes, because PIL didn't give
>  me sufficient control over colour tables and pixel depths.
> 
>  I thought the code was working fine. I left it aside for some months, came
>  back to it a week or two ago, and found it was crashing. I was creating
>  CFUNCTYPE objects to do callbacks to my own I/O routines
[snip]
>  Make sure you keep references to CFUNCTYPE objects as long as they are
>  used from C code. ctypes doesn’t, and if you don’t, they may be garbage
>  collected, crashing your program when a callback is made.
> 
>  Yup, that was it. I changed my installation of the callbacks from
[snip]

As a ctypes user I found this an interesting story - thanks for
posting it!

ctypes could potentially note that function types don't have enough
references to them when passed in as arguments to C functions?  It
might slow it down microscopically but it would fix this problem.

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Building PIL under Cygwin & Python 2.5

2009-06-01 Thread Martin v. Löwis
> I think this is an issue that probably needs to be addressed by PIL
> maintainers that fully understand the root of the problem (and it
> should probably go in the PIL FAQ), but in the meantime does anyone
> out there know how to get around this issue?

Why do you need to use Cygwin Python?

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


Re: Generating all combinations

2009-06-01 Thread pataphor

Johannes Bauer wrote:


Any help is appreciated!


This is on the fringe of exploitation, but hey, maybe the code helps you 
think about the algorithm.


IMHO the following code is a glaring complaint about the injustice of 
omission itertools inflicts on the perfectly natural and obvious 
procedure of repeat_each (whatever it's name ought to be):


from itertools  import izip, islice, cycle

def repeat_each(seq,n):
while True:
for x in seq:
for i in range(n):
yield x

def repeat_all(seq,n):
while True:
for i in range(n):
for x in seq:
yield x

def product(X):
N = []
total = 1
for x in X:
N.append(total)
total *= len(x)
R = [repeat_all(repeat_each(x,k),n)
for x,k,n in izip(X,N,reversed(N))]
return islice(izip(*R),total)

def test1():
L = ['a', 'bc','def' ]
for x in product(L):
print x
print

def test2():
repeat_all = cycle
test1()

if __name__ == '__main__':
test1()
test2()

See? Repeat_all and repeat_each are almost brothers, just separated by 
the tiniest rearrangement of their genetic code (or should I say code 
genetics?). Yet one is included as 'itertools.cycle' and the other is 
doomed to live in limbo. Such injustice! Can it be that 
'itertools.repeat' has usurped repeat_each's rightful position?


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


Re: what I would like python.el to do (and maybe it does)

2009-06-01 Thread J Kenneth King
Piet van Oostrum  writes:

>> J Kenneth King  (JKK) wrote:
>
>>JKK> Well, that's the thing -- type a statement into a python interpreter and
>>JKK> you just get a new prompt.
>
>>JKK> LISP has a REPL, so you get some sort of feedback printed.
>
> iPython also has a REPL, but only when you enter the Python code
> manually in the iPython window.
>
>>JKK> However, some sort of visual cue on the emacs side would be nice. Either
>>JKK> just flash the block of code being sent or a minibuffer message would be
>>JKK> nice.
>
>>JKK> Look for some SLIME tutorial videos on youtube to see some great
>>JKK> interpreter <-> editor interaction.
>
> I have tried out SLIME with SBCL (just some simple code) but I didn't
> like the feedback. I got unnecessary compiler warnings, and it was
> difficult to find some useful information in it.
>
>>JKK> The stock Python interpreter probably wouldn't cut it close to something
>>JKK> like SLIME in terms of features, but the iPython package might be a
>>JKK> start.
>
> For now the iPython package for me has more options than I have had time
> to try out.
> On the other hand when you execute some code form a Python file (with
> C-c C-c or C-c |) you get this
>  ## working on region in file /tmp/python-26084kfr.py... in the *Python*
> buffer which is very uninformative. This is generated by python-mode,
> not by iPython. You do get any output printed in the code, however, as
> well as exceptions.
>
> I have made a change in my Python mode such that the  
> ## working on region in file /tmp/python-26084kfr.py...
> message will be replaced by the actual code executed, if that code is
> not too big (size configurable). And that looks nicer. 
> -- 
> Piet van Oostrum 
> URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
> Private email: p...@vanoostrum.org

If you have a patch file for that, I'd be interested in trying it out. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess and win32security.ImpersonateLoggedOnUser

2009-06-01 Thread Emin.shopper Martinian.shopper
> The source for subprocess just uses CreateProcess. Which means that,
> short of monkey-patching it, you're going to have to roll your own
> subprocess-like code (I think). Basically, you'll need to run
> CreateProcessAsUser or CreateProcessAsLogonW. They're both a bit
> of a pig in terms of getting the right combination of parameters
> and privileges,

Thanks. I tried rolling my own via CreateProcessAsUser but it
complained about needing some special permissions so its probably not
going to work. I'd like to try CreateProcessAsLogonW but can't see how
to access that via python. I will start a new thread on the
python-win32 list about that.

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


Re: PyQt4 + WebKit

2009-06-01 Thread dudekksoft
On 31 Maj, 02:32, David Boddie  wrote:
> On Saturday 30 May 2009 17:39, dudekks...@gmail.com wrote:
>
> > I need to grab clicked links in QWebView. Everything is fine when I
> > use linkClicked() signal. LinkDelegationPolicy is set to
> > DelegateAllLinks and there is a problem. If some site has Javascript
> > my procedure receives QUrl from linkClicked, next calls:
>
> > webView.setUrl(url) #Where "url" is received QUrl
>
> > Certainly this method doesn't work. Site loads again (in most cases
> > it's calledhttp://mywwwsite.net/#).
>
> OK, so if I understand correctly, you don't know how to handle these
> special links if you use a delegation policy, and you would prefer it
> if they were handled by WebKit.
>
> > As I use
> > QWebPage::DelegateExternalLinks it happens the same, because my
> > procedure grabs links with Javascript. I don't know how to cope with
> > this problem. Maybe it's possible to set own policy in
> > QWebPage.LinkDelegationPolicy? Or maybe some other method to grab
> > protocol, host(if external or not) when user clicks link in webView?
>
> So, you only want to handle certain links, and pass on to WebKit those which
> you can't handle? Is that correct?
>
> David

Yes, I want to handle external links (out of my host) and links
starting with download://... (my specific protocol).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess and win32security.ImpersonateLoggedOnUser

2009-06-01 Thread Tim Golden

[slightly rearranged for top-to-bottom reading...]


On Mon, Jun 1, 2009 at 9:38 AM, Tim Golden  wrote:

Emin.shopper Martinian.shopper wrote:

Dear Experts,

I am having some issues with the subprocess module and how it
interacts with win32security.ImpersonateLoggedOnUser. Specifically, I
use the latter to change users but the new user does not seem to be
properly inherited when I spawn further subprocesses.

I am doing something like

   import win32security, win32con
   handle = win32security.LogonUser(
   user,domain,password,win32con.LOGON32_LOGON_INTERACTIVE,
   win32con.LOGON32_PROVIDER_DEFAULT)

   win32security.ImpersonateLoggedOnUser(handle)

Then spawning subprocesses but the subprocesses cannot read the same
UNC paths that that the parent could.

http://support.microsoft.com/kb/111545

"""
Even if a thread in the parent process impersonates a client and then
creates a new process, the new process still runs under the parent's
original security context and not the under the impersonation token. """

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



Emin.shopper Martinian.shopper wrote:

Thanks. But how do I fix this so that the subprocess does inherit the
impersonated stuff?



The source for subprocess just uses CreateProcess. Which means that,
short of monkey-patching it, you're going to have to roll your own
subprocess-like code (I think). Basically, you'll need to run
CreateProcessAsUser or CreateProcessAsLogonW. They're both a bit
of a pig in terms of getting the right combination of parameters
and privileges, I seem to remember. Haven't got time right now
to fish for an example, I'm afraid: maybe someone else on the list
has a canned example...?

Also worth cross-posting this to the python-win32 list where more
win32 expertise resides.

TJG

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


Re: subprocess and win32security.ImpersonateLoggedOnUser

2009-06-01 Thread Emin.shopper Martinian.shopper
Thanks. But how do I fix this so that the subprocess does inherit the
impersonated stuff?

On Mon, Jun 1, 2009 at 9:38 AM, Tim Golden  wrote:
> Emin.shopper Martinian.shopper wrote:
>>
>> Dear Experts,
>>
>> I am having some issues with the subprocess module and how it
>> interacts with win32security.ImpersonateLoggedOnUser. Specifically, I
>> use the latter to change users but the new user does not seem to be
>> properly inherited when I spawn further subprocesses.
>>
>> I am doing something like
>>
>>    import win32security, win32con
>>    handle = win32security.LogonUser(
>>        user,domain,password,win32con.LOGON32_LOGON_INTERACTIVE,
>>        win32con.LOGON32_PROVIDER_DEFAULT)
>>
>>    win32security.ImpersonateLoggedOnUser(handle)
>>
>> Then spawning subprocesses but the subprocesses cannot read the same
>> UNC paths that that the parent could.
>
> http://support.microsoft.com/kb/111545
>
> """
> Even if a thread in the parent process impersonates a client and then
> creates a new process, the new process still runs under the parent's
> original security context and not the under the impersonation token. """
>
> TJG
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess and win32security.ImpersonateLoggedOnUser

2009-06-01 Thread Tim Golden

Emin.shopper Martinian.shopper wrote:

Dear Experts,

I am having some issues with the subprocess module and how it
interacts with win32security.ImpersonateLoggedOnUser. Specifically, I
use the latter to change users but the new user does not seem to be
properly inherited when I spawn further subprocesses.

I am doing something like

import win32security, win32con
handle = win32security.LogonUser(
user,domain,password,win32con.LOGON32_LOGON_INTERACTIVE,
win32con.LOGON32_PROVIDER_DEFAULT)

win32security.ImpersonateLoggedOnUser(handle)

Then spawning subprocesses but the subprocesses cannot read the same
UNC paths that that the parent could.


http://support.microsoft.com/kb/111545

"""
Even if a thread in the parent process impersonates a client and then creates a new process, the new process still runs under the parent's original security context and not the under the impersonation token. 
"""


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


Re: Class Methods help

2009-06-01 Thread Jean-Michel Pichavant

FYI, same without decorators, if you python version does not support it.

class MyClass:
   def  some_func(x):
   return x+2
   some_func = staticmethod(some_func)


JM

bd satish wrote:

Thanks to Tim Chase & Lie Ryan !!  That was exactly what I was looking for !!

It's time for me to now read the documentation of "decorators" and
@classmethod and also @staticmethod.

I'm quite new to decorators...


-- Satish BD


On Sun, May 31, 2009 at 4:44 PM, Lie Ryan  wrote:
  

bdsatish wrote:


Hi,

I have a question regarding the difference b/w "class methods" and
"object methods". Consider for example:

class  MyClass:
x = 10

Now I can access MyClass.x -- I want a similar thing for functions. I
tried

class MyClass:
def  some_func(x):
return x+2

When I call MyClass.some_func(10) -- it fails, with error message:


TypeError: unbound method some_func() must be called with MyClass
instance as first argument (got int instance instead)

OK. I figured out that something like this works:
obj = MyClass()
y = obj.some_func(10)

BUT, this means that we have functions applying for instances. That is
we have "instance method". Now, how do I implement some function which
I can invoke with the class name itself ? Instead of creating a dummy
object & then calling In short, how exactly do I create "class
methods" ??
  

with staticmethod decorator:



class MyClass:
  

... @staticmethod
... def some_func(x):
... return x+2
...


MyClass.some_func(10)
  

12

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




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


Re: Problem building 64-bit python 2.6.2 on Solaris 10

2009-06-01 Thread John Center
On May 29, 7:13 pm, "Martin v. Löwis"  wrote:
>
> Ah, too much text - I was confused by you reporting two issues in a
> single email message. That has exhausted my capacity for quick message
> scanning.
>
Sorry about that.  I have a tendency to over document...

> So this is a ctypes problem. You'll have to ignore it - there is
> absolutely no way that you could possibly build the ctypes module
> with Sun CC (short of rewriting ctypes to support it, of course).
> Use gcc if you need the ctypes module, or accept not having the ctypes
> module available.
>
I was afraid that would be the case.  I have gcc4sparc, but I usually
build with Sun Studio.  I believe gcc4sparc uses the Sun Studio
backend.  I'll try this, but do you know if I would need to do
anything different to get the ctypes code to compile?

> Unfortunately, no. It is definitely *not* Python who is searching for
> these libraries. That you had been passing them to ld during linkage
> doesn't help at all. Linking succeeds just fine; Python then tries
> to load the the _ssl module, which in turn causes the *dynamic* linker
> (ld.so.1) to search for the shared library; it doesn't find it and
> therefore gives up loading _ssl.so.
>
Ok, so it looks like the only option here is to use LD_LIBRARY_PATH.

Thanks, Martin.

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


Dabo 0.9.2 released

2009-06-01 Thread Ed Leafe
	At long last, we are finally releasing Dabo 0.9.2. This fixes the  
errors that were found in 0.9.1; adds a brand-new Web Update  
implementation that should make keeping current much easier, as well  
as a whole bunch of improvements under the hood.


You can grab the latest version at http://dabodev.com/download.


-- Ed Leafe



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


subprocess and win32security.ImpersonateLoggedOnUser

2009-06-01 Thread Emin.shopper Martinian.shopper
Dear Experts,

I am having some issues with the subprocess module and how it
interacts with win32security.ImpersonateLoggedOnUser. Specifically, I
use the latter to change users but the new user does not seem to be
properly inherited when I spawn further subprocesses.

I am doing something like

import win32security, win32con
handle = win32security.LogonUser(
user,domain,password,win32con.LOGON32_LOGON_INTERACTIVE,
win32con.LOGON32_PROVIDER_DEFAULT)

win32security.ImpersonateLoggedOnUser(handle)

Then spawning subprocesses but the subprocesses cannot read the same
UNC paths that that the parent could.

Any advice on either spawning subprocesses which inherit parent user
properly or changing users in a better way on Windows would be greatly
appreciated.

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


Re: "TypeError: 'int' object is not callable"

2009-06-01 Thread Rhodri James

On Mon, 01 Jun 2009 11:40:50 +0100, Visco Shaun  wrote:


when I was executing the below code I got "TypeError: 'int' object is
not callable" exception. Why is it so?

if type(c) == type(ERROR):

c can be a string or an integer representing an error


In the absence of the rest of your code this is a wild guess, but have
you used `type` as a variable?  If you have, that would mask the builtin
name `type`, and would give you this error assuming `type` contains an
integer.

Moral: don't use builtin names for variables.

--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


BMP32 for linux

2009-06-01 Thread Djames Suhanko
Hello, all!
Did you know the bmp32 module for linux? This module can to load bmp
image with alpha channel where is not supported others images formats.
But I couldn't to found this module to install in my linux box. :-(
do you have tips?

Thanks !
--
Djames Suhanko
LinuxUser 158.760
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: automated web with python?

2009-06-01 Thread P. Kaminski
OK, I found a solution -- Selenium, from http://selenium.org. I
downloaded Selenium RC and this works fantastic! I'll get the job done
by tomorrow ;)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "TypeError: 'int' object is not callable"

2009-06-01 Thread Chris Rebert
On Mon, Jun 1, 2009 at 3:40 AM, Visco Shaun  wrote:
> when I was executing the below code I got "TypeError: 'int' object is
> not callable" exception. Why is it so?
>
> if type(c) == type(ERROR):
>
> c can be a string or an integer representing an error

Please also, now and in the future, provide the full error traceback
as it is very necessary in order to determine the problem.

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


Re: Simple metaclass code failing

2009-06-01 Thread Piet van Oostrum
> Piet van Oostrum  (I) wrote:

>I> But your class definition: 

>I> class C3(C1, C2):
>I> says that C1 should be before C2. Conflict!!
>I> Change it to class C3(C2, C1):

Of course the C1 is then superfluous.

I wonder why you want this. What is the problem you want to solve?

Apart from the metaclasses (that you still can use with `class C3(C2)')
I could think of the following use case:

class C1(object):
  def m1(self):
  return 'C1.m1'

class C2(C1):
  # override m1
  def m1(self):
  return 'C2.m1'
  def m2(self):
  return 'C2.m2'+self.m1()

class C3(C1, C2):
  def test(self):
  print self.m1()+self.m2()

i.e. in C3 we `ignore' the override of m1 in C2 but still want to make
use of the m2 from C2. 

The question that arises then is: the self.m1() inside m2, which m1
should it use? For an instance of C3 it would use C1.m1, but for an
instance of C2 it would use C2.m2. 

However, every instance of C3 can also be considered an instance of C2,
(Liskov substitution principle), therefore there is a conflict. That is
exactly the conflict that the MRO signals.

If you want that kind of behaviour it can be solved by using a mixin
class for the m2 method:

class C1(object):
 __metaclass__ = M1
 def m1(self):
 return 'C1.m1'
class Cmix(object):
 def m2(self):
 return 'C2.m2'+self.m1()
class C2(C1, Cmix):
 __metaclass__ = M2
 # override m1
 def m1(self):
 return 'C2.m1'
class C3(C1, Cmix):
 __metaclass__ = M3 
 def test(self):
 print self.m1()+self.m2()

-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "TypeError: 'int' object is not callable"

2009-06-01 Thread Andre Engels
On Mon, Jun 1, 2009 at 12:40 PM, Visco Shaun  wrote:
> when I was executing the below code I got "TypeError: 'int' object is
> not callable" exception. Why is it so?
>
> if type(c) == type(ERROR):
>
> c can be a string or an integer representing an error

Could you please give a larger piece of code, preferably such that it
can functions as a small stand-alone program that gives the same error
message? It looks to me that the problem is most likely in a previous
or subsequent line.



-- 
André Engels, andreeng...@gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Not going to let this drop ...

2009-06-01 Thread Steve Holden
If you have a few minutes to read this message you might just be able to
make a big difference to the lives of some very disadvantaged people. If
you can't help, don't worry. Sorry for bothering you. There's a project
that needs help.

The chuzer project is moribund (the rest of this message explains what
it is). I accept responsibility for that. I have had a lot on my plate
recently, but everything is relative, and my information from other
sources tells me that some recipients of this email have far worse
problems than I.

Paul McNett said in a recent email "this seems like too noble a project
to die", and I agree with him. It seems to me that a really quite small
amount of development time could see us producing a system that would
add a huge amount to the lives of some of the most needy people we can
imagine.

Or not. Frankly, it's the imagining that scares me. I am having real
difficulty imagining what it might be like to be in a position where I
can't easily tell someone what I need. The chuzer project will allow
severely disabled people to express the most basic needs. Maybe "what I
want for pudding" wasn't a particularly good example, but it was the one
the project was motivated by, and it's hard to think how frustrating it
must be when being able to tell someone what you want for pudding
represents an improvement in your quality of life.

There are quadriplegics out there with such a limited range of
expressive ability that the only way they can express any choice at all
is to watch a series of selections tick by until the right one comes up,
and then puff into a pipe or bite down on something - basically, perform
one of the very few movements of which they are capable - to say "that's
what I want". Yes, it might be "what I want for pudding", but how about
even more basic choices:

a) I need to pee
b) I have to take a dump
c) I am hungry
d) I am thirsty
e) Please hug me
f) My bedsores are hurting

The big problem for a lot of quadriplegics, as far as I understand it,
is that uninformed people treat them as though their brains were as
damaged as their bodies. Most readers of this email will be able-bodied.
Please try and imagine what it would be like to be entombed in a
full-body plaster cast and be unable to speak, but to still retain your
full mental faculties. Don't you think you might really appreciate even
something as simple as a choice program?

I was intending to try and recruit assistance at PyCon, but sadly on the
Saturday I received news of my mother's death, so that kind of put the
kibosh on that, and I had to leave early. I also recently realized
rather too late that this project would be an excellent candidate for a
Google Summer of Code project, but that's already underway now. Again, I
apologize for not having the time to bring chuzer to enough people's
attention to get it on the GSoc radar. My bad.

I don't know if Google are planning to run a Highly-Open Participation
event this year, but if they are then maybe we could establish a few
tasks for that.

Basically this is a project that really needs some help right now.
There's a basic working model, around which an image-selection and
audio-recording infrastructure needs to be built so that it's easy for
carers to set up appropriate choices and easy for users to indicate
choices. It's not rocket science, it just needs a few of us to care
enough to keep moving this project forward until it's done. It would
make an excellent wxPython learning project.

If anyone who reads this email has anything to offer - time, money,
equipment, whatever, I'd be *really* grateful to hear from them. I can
put you in touch with people who know about things like the range of
interface devices available for paraplegic and quadriplegic people.
Ideally the program would be adaptable to a wide range of hardware
accommodating a broad spectrum of disability, but many quadriplegics use
equipment that emulates a standard keyboard and/or mouse, so that would
be a great start.

Please help, even if only by publicizing this project. You don't even
have to code, just to help with ideas for recruiting coding assistance.

  http://chuzer.org/

Even better, please join the mailing list linked on the site, and help
to make the world a better place.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Watch PyCon on video now!  http://pycon.blip.tv/



-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Watch PyCon on video now!  http://pycon.blip.tv/

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


Ah, ctypes

2009-06-01 Thread Lawrence D'Oliveiro
I wrote some code months ago to output 1-bit-per-pixel PNG files from
Python, doing direct calls to libpng using ctypes, because PIL didn't give
me sufficient control over colour tables and pixel depths.

I thought the code was working fine. I left it aside for some months, came
back to it a week or two ago, and found it was crashing. I was creating
CFUNCTYPE objects to do callbacks to my own I/O routines, and with GDB I was
able to narrow down the crashes to the point where libpng was trying to
invoke one of my callbacks.

What had changed? I had switched my OS from Gentoo to Debian Unstable. I
chrooted back to the Gentoo system, tried my script again--still crashed.

Went back to the doc  and tried
the qsort callback example. Worked fine! And then I noticed this little bit:

Important note for callback functions:

Make sure you keep references to CFUNCTYPE objects as long as they are
used from C code. ctypes doesn’t, and if you don’t, they may be garbage
collected, crashing your program when a callback is made.

Yup, that was it. I changed my installation of the callbacks from

png.png_set_write_fn \
  (
write_struct,
None,
ct.CFUNCTYPE(None, ct.c_void_p, ct.c_void_p, ct.c_size_t)(write_data),
ct.CFUNCTYPE(None, ct.c_void_p)(flush_write)
  )

to

cb_write_data = ct.CFUNCTYPE(None, ct.c_void_p, ct.c_void_p, 
ct.c_size_t)(write_data)
cb_flush_write = ct.CFUNCTYPE(None, ct.c_void_p)(flush_write)
png.png_set_write_fn \
  (
write_struct,
None,
cb_write_data,
cb_flush_write
  )

and it works again.

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


"TypeError: 'int' object is not callable"

2009-06-01 Thread Visco Shaun
when I was executing the below code I got "TypeError: 'int' object is
not callable" exception. Why is it so?

if type(c) == type(ERROR):

c can be a string or an integer representing an error
-- 
Thanks & Regards
visco

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


Re: Simple metaclass code failing

2009-06-01 Thread Piet van Oostrum
> LittleGrasshopper  (L) wrote:

>L> On May 31, 3:59 pm, Carl Banks  wrote:
>>> On May 31, 3:52 pm, LittleGrasshopper  wrote:
>>> 
>>> 
>>> 
>>> > This is some simple code which I got from Guido's paper on the
>>> > unification of classes and types, which Arnaud suggested to improve my
>>> > knowledge of metaclasses:
>>> 
>>> > class M1(type):
>>> >     pass
>>> > class M2(M1):
>>> >     pass
>>> > class M3(M2):
>>> >     pass
>>> > class C1:
>>> >     __metaclass__ = M1
>>> > class C2(C1):
>>> >     __metaclass__ = M2
>>> > class C3(C1, C2):
>>> >     __metaclass__ = M3
>>> 
>>> > It is failing when processing C3:
>>> > Traceback (most recent call last):
>>> >   File "metaerror.py", line 18, in 
>>> >     class C3(C1, C2):
>>> > TypeError: Error when calling the metaclass bases
>>> >     Cannot create a consistent method resolution
>>> > order (MRO) for bases C2, C1

[snip]

>L> I guess the resulting MROs do not satisfy monotonicity, I
>L> just have to find out how to derive the MRO. I had the notion that it
>L> was constructed by listing the current class, followed by the MROs of
>L> each base in order, and then retaining the rightmost instance of each
>L> class in the MRO list, but I think that might be an
>L> oversimplification, since in this case that would be:
>L> (C1, object)
>L> (C2, C1, object)
>L> (C3, C2, C1, object)
>L> And I don't see any issues. But I'll read the paper to figure out what
>L> the problem is, thanks.

Here is exactly the problem. Merging the two MRO's as you state would
give C3, C2, C1, object, i.e. C2 before C1.

But your class definition: 

class C3(C1, C2):
says that C1 should be before C2. Conflict!!
Change it to class C3(C2, C1):

You see it has nothing to do with the metaclasses. The following code
gives the same error:

class C1(object):
pass
class C2(C1):
pass
class C3(C1, C2):
pass

-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NameError function not found

2009-06-01 Thread Jean-Michel Pichavant

Cameron Pulsford wrote:

Hey everyone, I am extremely stumped on this. I have 2 functions..

def _determinant(m):
   return m[0][0] * m[1][1] - m[1][0] * m[0][1]

def cofactor(self):
   """Returns the cofactor of a matrix."""
   newmatrix = []
   for i, minor in enumerate(self.minors()):
   newmatrix.append(_determinant(minor.matrix) * ((i%2) * -1))
   return newmatrix

And I get the following error when I try to run a.cofactor()...

"NameError: global name '_determinant' is not defined"

When I put the _determinant function nested within the cofactor 
function it works fine. Normally I would do this, but a lot of other 
methods use the _determinant method. They are both in the same class, 
and they are at the same level, they are even in that order so I know 
it should be  able to see it. I have a lot of functions that call 
other functions in this class and everything is fine. [Also for the 
picky, I know my cofactor method isn't mathematically correct yet ;-) 
] Am I missing something obvious here? Also if it helps the rest of 
the code is 
herehttp://github.com/dlocpuwons/pymath/blob/d1997329e4473f8f6b5c7f11635dbd719d4a14fa/matrix.py though 
it is not the latest.


Maybe someone has already answered...

Looks like cofactor is in a class, so I'm assuming _determinant is in 
that class as well.


3 solutions:
- declare _determinant outside of the class, making it a private module 
function

- declare _determinant as method of the instance : def _determinant(self)
- if you want to keep _determinant as part of your class, it's up to you 
but you have to declare it as staticmethod (goolge it for details)


For isntance :
class MyClass
 def _determant()m:
...
 _determant = staticmethod(_determinant)

 def cofactor(self):
 ...
 x = MyClass._determinant(y) # this is how you call static Class 
method, but rarely these methods are set private (_ prefix)



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


Re: what I would like python.el to do (and maybe it does)

2009-06-01 Thread Piet van Oostrum
> J Kenneth King  (JKK) wrote:

>JKK> Well, that's the thing -- type a statement into a python interpreter and
>JKK> you just get a new prompt.

>JKK> LISP has a REPL, so you get some sort of feedback printed.

iPython also has a REPL, but only when you enter the Python code
manually in the iPython window.

>JKK> However, some sort of visual cue on the emacs side would be nice. Either
>JKK> just flash the block of code being sent or a minibuffer message would be
>JKK> nice.

>JKK> Look for some SLIME tutorial videos on youtube to see some great
>JKK> interpreter <-> editor interaction.

I have tried out SLIME with SBCL (just some simple code) but I didn't
like the feedback. I got unnecessary compiler warnings, and it was
difficult to find some useful information in it.

>JKK> The stock Python interpreter probably wouldn't cut it close to something
>JKK> like SLIME in terms of features, but the iPython package might be a
>JKK> start.

For now the iPython package for me has more options than I have had time
to try out.
On the other hand when you execute some code form a Python file (with
C-c C-c or C-c |) you get this
 ## working on region in file /tmp/python-26084kfr.py... in the *Python*
buffer which is very uninformative. This is generated by python-mode,
not by iPython. You do get any output printed in the code, however, as
well as exceptions.

I have made a change in my Python mode such that the  
## working on region in file /tmp/python-26084kfr.py...
message will be replaced by the actual code executed, if that code is
not too big (size configurable). And that looks nicer. 
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Tutor] Challenge supporting custom deepcopy with inheritance

2009-06-01 Thread Gabriel Genellina

En Mon, 01 Jun 2009 00:28:12 -0300, Michael H. Goldwasser
 escribió:


Hi Kent,

Thanks for your thoughts.

For the original example code, you are correct that we could make it
work by having B provide the one-arg constructor to match A's
constructor signature.  But I don't see this as a general solution.

Having B.__deepcopy__() make a call to A.__deepcopy__() assumes that A
supports __deepcopy__ either direclty or indirectly.  This is not
guaranteed as __deepcopy__ is not supported all the way back to the
object base class.  It could be that the author of A has guaranteed
that the syntax deepcopy(instanceA) is properly supported, but that
could be done by other means without an explicit __deepcopy__ (such as
relying on a true deep copy, or using getstate/setstate to affect both
pickling and deepcopy).


In general, you have to know whether A implements __deepcopy__ or not.  
This is a small price for the flexibility (or anarchy) in the copy/pickle  
interfases: there are several ways to implement the same thing, and you  
have to know which one your base class has chosen in order to extend it.


The following is a possible implementation that doesn't use __init__ at  
all, so their different signature is not an issue:


 # class A:
 def __deepcopy__(self, memo={}):
 dup = type(self).__new__(type(self))
 dup.__aTag = self.__aTag
 dup.__aList = copy.deepcopy(self.__aList, memo)
 dup.__aList.reverse()
 return dup

 # class B:
 def __deepcopy__(self, memo={}):
 dup = A.__deepcopy__(self, memo)
 dup.__bTag = self.__bTag
 dup.__bList = copy.deepcopy(self.__bList, memo)
 return dup

Note that A.__deepcopy__ does two things: a) create a new, empty,  
instance; and b) transfer state. B.__deepcopy__ handles *its* portion of  
state only. This can be written in a more generic way, relying on  
__getstate__/__setstate__ (the methods that should return the current  
state of the object):


 # class A:
 def __deepcopy__(self, memo={}):
 dup = type(self).__new__(type(self))
 if hasattr(self, '__getstate__'): state = self.__getstate__()
 else: state = self.__dict__
 state = copy.deepcopy(state, memo)
 if hasattr(dup, '__setstate__'): dup.__setstate__(state)
 else: dup.__dict__.update(state)
 dup.__aList.reverse()
 return dup

 # remove __deepcopy__ definition from class B

Now, B (and any other subclass) is concerned only with __getstate__ /  
__setstate__, and only when the default implementation isn't appropriate.



As another basic puzzle, consider a class definition for B where B has
a registry list that it doesn't want cloned for the new instance (but
it does want pickled when serialized).  This would seem to require
that B implement its own __deepcopy__.   We want to somehow rely on
the class definition for A to enact the cloning fo the state defined
by A.   But without knowing about how A supports the deepcopy
semantics, I don't see how to accomplish this goal.


I don't understand such bizarre requirement, but anyway, you can override  
__deepcopy__ (make B fix only the part that the default implementation  
doesn't implement well)


 # A.__deepcopy__ as above

 # class B:
 def __deepcopy__(self, memo={}):
 dup = A.__deepcopy__(self, memo)
 dup.__registry = self.__registry
 return dup

This [the need to know how certain feature is implemented in the base  
class] is not special or peculiar to pickle/copy, although the multiple  
(and confusing) ways in which a class can implement pickling doesn't help  
to understand the issue very well.


Consider the + operator: when some subclass wants to implement addition,  
it must know which of the several special methods involved (__add__,  
__iadd__, __radd__) are implemented in the base class, in order to  
extend/override them. Same for __cmp__/__eq__/__hash__: you can't just  
ignore what your base class implements. All of this applies to other  
languages too, but in Python, there is an additional consideration: the  
mere *existence* of some methods/attributes can have consequences on how  
the object behaves. In short, you can't blindly write __special__ methods.


--
Gabriel Genellina

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


Re: automated web with python?

2009-06-01 Thread Jeremiah Dodds
On Mon, Jun 1, 2009 at 8:26 AM, P. Kaminski  wrote:

> Ech... The problem is that mechanize doesn't support JavaScript, and
> these web forms are full of various JS functions... Maybe someone
> knows a way out of this? Doesn't have to be Python...
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Selenium _might_ be able to help you out.

Otherwise, a method you can use (tedious at first, but can speed things up
significantly) would be to either use firefox and grab the headers you're
sending during a session (the site may use javascript really heavily, but in
the end all that matters is that you're sending the same POST/GET requests),
and then use mechanize, or read the javascript, and then use mechanize.

At my job, I've had to automate a few really javascript-heavy web
applications. Firefox+FireBug+HTTPFox with python and mechanize can be a
lifesaver here.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Metaclass mystery

2009-06-01 Thread Michele Simionato
On May 31, 2:32 am, LittleGrasshopper  wrote:

> Seriously, metaclasses are making my brain hurt. How do people like
> Michele Simionato and David Mertz figure these things out? Does it all
> come to looking at the C source code for the CPython interpreter?

Actually I never looked at the C source code. I performed lots of
experiments
and figured things out the hard way, with trial and errors.
Metaclasses
are not that hard, descriptors and super were much harder to grasp at
that
time, since there was next to zero documentation and a set of subtle
bugs.
For descriptors now there is Raymond Hettinger essay and for super
there are my blog posts
on Artima: 
http://www.artima.com/weblogs/index.jsp?blogger=micheles&start=45&thRange=15
(there are also two posts of mine about metaclasses in Python 3.0 that
you may want to
read)

HTH,

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


Re: automated web with python?

2009-06-01 Thread P. Kaminski
Ech... The problem is that mechanize doesn't support JavaScript, and
these web forms are full of various JS functions... Maybe someone
knows a way out of this? Doesn't have to be Python...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Metaclass mystery

2009-06-01 Thread Lie Ryan
LittleGrasshopper wrote:
> On May 31, 2:03 pm, a...@pythoncraft.com (Aahz) wrote:
>> In article 
>> ,
>>
>> LittleGrasshopper   wrote:
 On May 31, 12:19=A0am, Arnaud Delobelle  wrote:
> [1]http://www.python.org/download/releases/2.2.3/descrintro/
>>> I'm about 2/3 of the way through this paper (although I don't claim to
>>> understand all of it.) There is some heavy duty material in there,
>>> enough to make me feel really stupid and frustrated at times. I'm
>>> making connections as I go though, hopefully everything will sink in
>>> eventually.
>>> Is this stuff actually tough, or am I just a dummy?
>> Definitely tough!  Metaclasses can cause dain bramage.
>> --
>> Aahz (a...@pythoncraft.com)   <*>http://www.pythoncraft.com/
>>
>> my-python-code-runs-5x-faster-this-month-thanks-to-dumping-$2K-
>> on-a-new-machine-ly y'rs  - tim
> 
> Good to know, I'll stick to it and persevere. Will check doctor
> regularly for dain bramage though.

Fortunately python makes it rare that we actually need to use metaclass.
Especially with class decorators and such...

With that said, the rare cases where it is really needed; brain
hemorrhage is not only possible but when.
-- 
http://mail.python.org/mailman/listinfo/python-list