Fast powerset function

2007-07-12 Thread Arash Arfaee

I need a powerset generator function. It's really slow with recursion. Does
anybody have any idea or code(!!) to do it in an acceptable time?
Thanks
-Arash
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Fast powerset function

2007-07-18 Thread Arash Arfaee

Hi All

With your help I found lots of good method and algorithm. Also I found out
if I exchange all for loop with while loop it make the program much faster
and also it consumes less memory (almost half!)
Just wanna thank you all.
Cheers,
Arash

On 7/13/07, Mark Dickinson <[EMAIL PROTECTED]> wrote:


If you don't care about the order of the results, you can use a Gray
code (http://en.wikipedia.org/wiki/Gray_code): this has the advantage
of only adding or removing a single element to get from one subset to
the next.

def powerset(s):
d = dict(zip(
(1<>> list(powerset('abc'))
[set([]), set(['a']), set(['a', 'b']), set(['b']), set(['c', 'b']),
set(['a', 'c', 'b']), set(['a', 'c']), set(['c'])]

If you're using the subsets as they appear and don't need to store
them all at once, then it's significantly faster (on my machine) if
you replace the line subset = subset ^ d[i & -i] with an in-place
update:  subset ^= d[i & -i].

Mark


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

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

How to check if an item exist in a nested list

2007-07-19 Thread Arash Arfaee

Hi All,

Is there any way to check if an item in specific location in a multiple
dimension nested exist?  For example something like:

if M_list[line][row][d] exist:
 do_something_0
else:
 do_something_1

One way is to check the length of each dimension. Does any body know a
simpler way? is there any way to check if "IndexError: list index out of
range" happened or going to happen and stop program from terminating?

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

TypeError: 'int' object is not callable for max(2,3)

2007-07-25 Thread Arash Arfaee

Hi all,
I have a problem. if I enter max(2,3) before I run my program in command
line it returns 3. However if I start to debug my program, I have this
error:

[Dbg]>>> max(2,3)
Traceback (most recent call last):
 File "", line 1, in 
TypeError: 'int' object is not callable

Any idea what should be the reason?


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

psyco problem on mac wingide

2008-01-19 Thread Arash Arfaee
Hello All,

I have no problem using psyco on python shell on my new Mac, however I
cannot import it from WingIDE. I copied psyco directory into mac
python folder. Does wingide installs another python shell?
Thanks,

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


problem With Psyco on Wingide mac

2008-01-21 Thread Arash Arfaee
Hello All,

I am trying to use psyco with wingide on mac. when I open Mac Python
shell I can import psyco, but not inside the wingide. Even python
shell on wingide cannot import psyco.
Can anybody help me to solvethis problem?

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


psyco on mac

2008-01-25 Thread Arash Arfaee
Hello,

I have problem installing psyco on my mac. Can anybody help me?

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


malloc (error code=12)

2009-01-21 Thread Arash Arfaee
Hi All,

I am writing a multiprocessing program using python 2.6. It works in most
cases, however when my input is large sometimes I get this message again and
again:

Python(15492,0xb0103000) malloc: *** mmap(size=393216) failed (error
code=12)
*** error: can't allocate region

and at the and I have these messages:


Python(15492,0xb0103000) malloc: *** mmap(size=393216) failed (error
code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Exception in thread Thread-2:
Traceback (most recent call last):
  File
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py",
line 524, in __bootstrap_inner
self.run()
  File
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py",
line 479, in run
self.__target(*self.__args, **self.__kwargs)
  File
"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/multiprocessing/pool.py",
line 259, in _handle_results
task = get()
MemoryError

Any idea what is wrong here? I didn't attached the code since it is a big
program and I don't know exactly which part of my program causes this error.
And since it is multiprocessing I can't debug it and run it line by line!

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


Re: malloc (error code=12)

2009-01-21 Thread Arash Arfaee
Very BIG Jesse It works on a huge Boolean function.
And thanks Roger. Do you think it will be solved if I run it over another OS
like windows?

Cheers,
Arash

On Wed, Jan 21, 2009 at 7:19 PM, Jesse Noller  wrote:

> On Wed, Jan 21, 2009 at 9:38 PM, Arash Arfaee  wrote:
> >
> > Hi All,
> >
> > I am writing a multiprocessing program using python 2.6. It works in most
> > cases, however when my input is large sometimes I get this message again
> and
> > again:
> >
> > Python(15492,0xb0103000) malloc: *** mmap(size=393216) failed (error
> > code=12)
> > *** error: can't allocate region
> >
> > and at the and I have these messages:
> >
> >
> > Python(15492,0xb0103000) malloc: *** mmap(size=393216) failed (error
> > code=12)
> > *** error: can't allocate region
> > *** set a breakpoint in malloc_error_break to debug
> > Exception in thread Thread-2:
> > Traceback (most recent call last):
> >   File
> >
> "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py",
> > line 524, in __bootstrap_inner
> > self.run()
> >   File
> >
> "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py",
> > line 479, in run
> > self.__target(*self.__args, **self.__kwargs)
> >   File
> >
> "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/multiprocessing/pool.py",
> > line 259, in _handle_results
> > task = get()
> > MemoryError
> >
> > Any idea what is wrong here? I didn't attached the code since it is a big
> > program and I don't know exactly which part of my program causes this
> error.
> > And since it is multiprocessing I can't debug it and run it line by line!
> >
> > Thanks,
> > Arash
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
> >
>
>
> wow. How big are these objects/input?
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: multiprocessing vs thread performance

2009-01-07 Thread Arash Arfaee
Hi All ,

Does anybody know any tutorial for python 2.6 multiprocessing? Or bunch of
good example for it? I am trying to break a loop to run it over multiple
core in a system. And I need to return an integer value as the result of the
process an accumulate all of them. the examples that I found there is no
return for the process.

Thanks,
-Arash

On Mon, Jan 5, 2009 at 7:24 PM, Gabriel Genellina wrote:

> En Sat, 03 Jan 2009 11:31:12 -0200, Nick Craig-Wood 
> escribió:
>
>> mk  wrote:
>>
>
>   The results I got are very different from the benchmark quoted in PEP
>>>  371. On twin Xeon machine the threaded version executed in 5.54 secs,
>>>  while multiprocessing version took over 222 secs to complete!
>>>
>>>  Am I doing smth wrong in code below?
>>>
>>
>> Yes!
>>
>> The problem with your code is that you never start more than one
>> process at once in the multiprocessing example.  Just check ps when it
>> is running and you will see.
>>
>
> Oh, very good analysis! Those results were worriying me a little.
>
> --
> Gabriel Genellina
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


adding in-place operator to Python

2008-09-23 Thread Arash Arfaee
Hi All,

Is there anyway to add new in-place operator to Python? Or is there any way
to redefine internal in-place operators?
Thanks.

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

Re: adding in-place operator to Python

2008-09-25 Thread Arash Arfaee
Thank you very much Gerhard and Terry.

I am trying to add undefined state to some Boolean operator. Here is what I
tried to do and It is not working:

class _3ph:
def __init__(self):
self.value = 0

def __xor__(self,item):
if self.value==2 or item==2:
 return 2
else:
 return self.__xor__(item)

what I am trying to do is assigning 2 to undefined state and have xor
operator return 2 if one of inputs are 2.
it seems Although I defined xor in _3ph class, python treat any object from
this class just like integer variables.
can you help me find what is wrong here?

Cheers,
Arash


On Tue, Sep 23, 2008 at 11:06 AM, Terry Reedy <[EMAIL PROTECTED]> wrote:

> Arash Arfaee wrote:
>
>> Hi All,
>>
>> Is there anyway to add new in-place operator to Python? Or is there any
>> way to redefine internal in-place operators?
>>
>
> Python does not have 'in-place operators'.  It has 'augmented assignment
> statements' that combines a binary operation with an assignment.  *If* the
> target being rebound is mutable, *then* (and only then) the operation can be
> and is recommended to be done 'in-place'.  User-defined mutable classes (as
> most are) can implement in-place behavior with __ixxx__ methods.  But for
> the reason given below, __ixxx__ methods should supplement and not replace
> direct mutation methods.
>
> Correct terminology is important for understanding what augmented
> assigments do and what they are basically about.
>
> First, most augmented assignments target immutables, in particular, numbers
> and strings, which do not have __ixxx__ methods.  So the operation is *not*
> done in-place.  The only difference from separately indicating the
> assignment and operation is that the programmer writes the target expression
> just once and the interpreter evaluates the target expression just once
> instead of each repeating themselves.  (And consequently, any side-effects
> of that evaluation happen just once instead of twice.)  The same __xxx__ or
> __rxxx__ method is used in either case.  This non-repetition is the reason
> for augmented assigments.  The optional in-place optimization for mutables
> is secondary.  It was debated and could have been left out.
>
> Second, all augmented assignments perform an assignment, even if the
> operation is done in place.  However, if a mutable such as a list is
> accessed as a member of an immutable collection such as a tuple, mutation is
> possible, but rebinding is not.  So the mutation is done and then an
> exception is raised.  To avoid the exception, directly call a mutation
> method such as list.extend.
>
> Terry Jan Reedy
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list