how to compile c-extensions under WinXP?
What should I do to be able to compile C-extensions (with python 2.4, winXP)? I get an error message, approximately "The .NET Framework SDK needs to be installed"; I tried to get something from the Microsoft web site, but maybe not the right version (or didn't set some variables), since the error remains. Could you please help me (it will need some patience with a computer newbie)? -- http://mail.python.org/mailman/listinfo/python-list
why no user-def. attributes?
I appologize in advance for stupid question, which is:
why are user-defined attributes not allowed for builtin types?
[I guess I undestand why *instances* cannot have them (e.g. then every
dict would have a dict which would have a dict..), but this is a
different question]
I can imagine several answers, so I put here those that don't seem
satisfactory to me :)
1. You can subclass, eg.
class my_int(int): pass
and then do whatnot, like my_int.__getitem__=some_vicious_function
Here the problem is just that it's easier to write 4 than
my_int(4), or "ha" than my_string("ha") etc.
2. You would probably want to add new methods, so why don't you
define a function and then write e.g. dowhatIwant([1,2,3]) instead of
[1,2,3].dowhatIwant()
That's OK, except for the convenience of special methods like __add__ or
__iter__
3. It would lead to a confusing code
Oops, maybe I shouldn't have written this one..
Anyway, please let me know the true reason (which is perhaps technical)
Best whishes
Paul
--
http://mail.python.org/mailman/listinfo/python-list
How to write this iterator?
Given a list of iterators, I'd like to have a new one that would cyclically walk over the list calling the next() method of the iterators (removing any iterator which is exhausted). It should also support adding a new iterator to the list; it should be added in front of the current position (so that it's called only after all the others). This is what I was able to write with my zero python skills (self.iters is the list of iterators, self.i is the index of the iterator that should be used next). Is there a nicer/more pythonic solution, maybe using generators? class Liter(object): def __init__(self, *iters): self.i=0 self.iters=[iter(x) for x in iters] def append(self,what): self.iters.insert(self.i,what) def __iter__(self): return self def next(self): while True: try: result=self.iters[self.i].next() except StopIteration: del self.iters[self.i] except IndexError: if len(self.iters) is 0: raise StopIteration else: self.i=0 else: self.i+=1 return result -- http://mail.python.org/mailman/listinfo/python-list
Re: How to write this iterator?
Sorry, my description was not very good, I meant something behaving as:
>>>example=Liter("abc","12345","XY")
>>>for x in example: print x,
a 1 X b 2 Y c 3 4 5
or for that append() method,
>>>example=Liter("abc", "12345")
>>>for i in range(3): print example.next(),
a 1 b
>>>example.append("XY")
>>>for x in example: print x,
2 c X 3 Y 4 5
Hope this clarifies what I mean
Best regards
Pavol
>A generator version:
>
>def iterconcat (collectionlist):
> for collection in collectionlist:
> for element in collection:
> yield element
>
>Extending collectionlist at runtime will work only with lists and
>similar collections. And you'll need to keep a reference to it as well.
>And it would be called after all the others, which matches your
>description, but not your code.
>
>Daniel
--
http://mail.python.org/mailman/listinfo/python-list
Re: How to write this iterator?
>> Sorry, my description was not very good, I meant something behaving as:
>>
>> >>>example=Liter("abc","12345","XY")
>> >>>for x in example: print x,
>>
>> a 1 X b 2 Y c 3 4 5
>>
>> or for that append() method,
>>
>> >>>example=Liter("abc", "12345")
>> >>>for i in range(3): print example.next(),
>>
>> a 1 b
>>
>> >>>example.append("XY")
>> >>>for x in example: print x,
>>
>> 2 c X 3 Y 4 5
>Check the module I posted on
>http://rafb.net/paste/results/CRT7bS68.html. append() makes things more
>complicated -- mainly because generators don't accept attributes, so it
>has to be wrapped in a class -- but the simple generator (without
>append) is more manageable
Thank you very much for your solution. Actually I needed that append()
method quite a lot, so maybe my solution (posted at the beginning of this
thread) was not that stupid :) (to inflate my ego a bit)
Anyway, this is roughly what I used it for; is it a sane use of iterators?
I was looking for the Holy Grail. I had an iterator 'quest' that produced
either junk that was to be thrown away, or the Holy Grail itself, or
another iterator of the same kind (that had to be searched for the Holy
Grail as well). The iterators were potentially infinite. The code was
rougly:
quest=Liter(quest)
for x in quest:
if is_Holy_Grail(x):
share_and_enjoy(x)
break
elif is_another_iterator(x):
quest.append(x)
Best regards
P.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Sudoku solver: reduction + brute force
ago wrote: > Inspired by some recent readings on LinuxJournal and an ASPN recipe, I > decided to revamp my old python hack... The new code is a combination > of (2) reduction methods and brute force and it is quite faster than > the > ASPN program. If anyone is interested I attached the code in > http://agolb.blogspot.com/2006/01/sudoku-solver-in-python.html I suggest trying input=""" 0,0,0,0,9,6,8,0,0 0,0,1,0,0,0,0,7,0 0,2,0,0,0,0,0,0,3 0,3,0,0,0,8,0,0,6 0,0,4,0,2,0,3,0,0 6,0,0,5,0,0,0,8,0 9,0,0,0,0,0,0,5,0 0,7,0,0,0,0,1,0,0 0,0,5,9,4,0,0,0,0""" your program seems to take too long to solve it. I think the hard part is not to solve, but rather to create *difficult* sudoku grids. But to inflate my ego beyond the known universe, here is my solver (that solves the avove mentioned grid reasonably fast). I suppose the only difference is that is uses 3, rather than 2, rules to simplify before starting tree-like search. # #if a copyryght is needed: #this is pulbic domain, do with it whatever you want #i.e. most probably nothing # class DeadEnd(Exception): pass class sudoku(object): def __init__(self,*args): self.changed=True self.possible=[] if len(args) != 81: raise ValueError, "need 81 numbers" for i in args: if i==0: self.possible.append(range(1,10)) else: self.possible.append([i]) def __getitem__(self,(x,y)): return self.possible[9*x+y] def __setitem__(self,(x,y),what): self.possible[9*x+y]=what def copy(self): result=sudoku(*(81*[1])) for i in range(9): for j in range(9): result[i,j]=list(self[i,j]) return result def solved(self): for i in range(9): for j in range(9): if len(self[i,j]) != 1: return False return True def trials(self): for i,j in ((i,j) for ln in range(2,10) for i in range(9) for j in range(9) if len(self[i,j])==ln): for k in self[i,j]: new=self.copy() new[i,j]=[k] yield new def clean1(self,x,y): self.changed=False if len(self[x,y]) == 1: return remove=set() for places in self.regions(x,y): missing=set(range(1,10)) for xx,yy in places: if xx==x and yy==y: continue if len(self[xx,yy])==1: remove.add(self[xx,yy][0]) missing-=set(self[xx,yy]) if missing: a=missing.pop() self[x,y]=[a] self.changed=True for a in remove: try: self[x,y].remove(a) if not self[x,y]: raise DeadEnd self.changed=True except ValueError: pass def clean3(self,out1,out2): for (o1, o2) in ((out1,out2), (out2,out1)): remove=set(range(1,10)) for x,y in o1: remove-=set(self[x,y]) for x,y in o2: for n in remove: try: self[x,y].remove(n) if not self[x,y]: raise DeadEnd self.changed=True except ValueError: pass @staticmethod def regions(x,y): return (((xx,y) for xx in range(9)), ((x,yy) for yy in range(9)), ((xx,yy) for xx in range(3*(x//3),3*(x//3)+3) for yy in range(3*(y//3),3*(y//3)+3))) @staticmethod def outs(): for i in range(3): for j in range(3): for k in range(3): out1=[(a+3*i,b+3*j) for a in range(3) if a is not k for b in range(3)] out2=[(k+3*i,n) for n in range(9) if n//3!=j] yield out1, out2 for k in range(3): out1=[(a+3*i,b+3*j) for a in range(3) for b in range(3) if b is not k] out2=[(n,k+3*j) for n in range(9) if n//3!=i] yield out1, out2 def clean_all(self): while self.changed: self.changed=False for x in range(9): for y in range(9): self.clean1(x,y) for out1,out2 in self.outs(): self.clean3(out1,out2) def __repr__(self): result="" for x in range(9): for y in range(9): if len(self[x,y])==1: haf=self[x,y][0] else: haf=self[x,y] result+=str(haf)+' ' result+='\n' return result from collections import deque cla
Re: Sudoku solver: reduction + brute force
ago wrote: > Inspired by some recent readings on LinuxJournal and an ASPN recipe, I > decided to revamp my old python hack... The new code is a combination > of (2) reduction methods and brute force and it is quite faster than > the > ASPN program. If anyone is interested I attached the code in > http://agolb.blogspot.com/2006/01/sudoku-solver-in-python.html I suggest trying input=""" 0,0,0,0,9,6,8,0,0 0,0,1,0,0,0,0,7,0 0,2,0,0,0,0,0,0,3 0,3,0,0,0,8,0,0,6 0,0,4,0,2,0,3,0,0 6,0,0,5,0,0,0,8,0 9,0,0,0,0,0,0,5,0 0,7,0,0,0,0,1,0,0 0,0,5,9,4,0,0,0,0""" your program seems to take too long to solve it. I think the hard part is not to solve, but rather to create *difficult* sudoku grids. But to inflate my ego beyond the known universe, here is my solver (that solves the avove mentioned grid reasonably fast). I suppose the only difference is that is uses 3, rather than 2, rules to simplify before starting tree-like search. # #if a copyryght is needed: #this is pulbic domain, do with it whatever you want #i.e. most probably nothing # class DeadEnd(Exception): pass class sudoku(object): def __init__(self,*args): self.changed=True self.possible=[] if len(args) != 81: raise ValueError, "need 81 numbers" for i in args: if i==0: self.possible.append(range(1,10)) else: self.possible.append([i]) def __getitem__(self,(x,y)): return self.possible[9*x+y] def __setitem__(self,(x,y),what): self.possible[9*x+y]=what def copy(self): result=sudoku(*(81*[1])) for i in range(9): for j in range(9): result[i,j]=list(self[i,j]) return result def solved(self): for i in range(9): for j in range(9): if len(self[i,j]) != 1: return False return True def trials(self): for i,j in ((i,j) for ln in range(2,10) for i in range(9) for j in range(9) if len(self[i,j])==ln): for k in self[i,j]: new=self.copy() new[i,j]=[k] yield new def clean1(self,x,y): self.changed=False if len(self[x,y]) == 1: return remove=set() for places in self.regions(x,y): missing=set(range(1,10)) for xx,yy in places: if xx==x and yy==y: continue if len(self[xx,yy])==1: remove.add(self[xx,yy][0]) missing-=set(self[xx,yy]) if missing: a=missing.pop() self[x,y]=[a] self.changed=True for a in remove: try: self[x,y].remove(a) if not self[x,y]: raise DeadEnd self.changed=True except ValueError: pass def clean3(self,out1,out2): for (o1, o2) in ((out1,out2), (out2,out1)): remove=set(range(1,10)) for x,y in o1: remove-=set(self[x,y]) for x,y in o2: for n in remove: try: self[x,y].remove(n) if not self[x,y]: raise DeadEnd self.changed=True except ValueError: pass @staticmethod def regions(x,y): return (((xx,y) for xx in range(9)), ((x,yy) for yy in range(9)), ((xx,yy) for xx in range(3*(x//3),3*(x//3)+3) for yy in range(3*(y//3),3*(y//3)+3))) @staticmethod def outs(): for i in range(3): for j in range(3): for k in range(3): out1=[(a+3*i,b+3*j) for a in range(3) if a is not k for b in range(3)] out2=[(k+3*i,n) for n in range(9) if n//3!=j] yield out1, out2 for k in range(3): out1=[(a+3*i,b+3*j) for a in range(3) for b in range(3) if b is not k] out2=[(n,k+3*j) for n in range(9) if n//3!=i] yield out1, out2 def clean_all(self): while self.changed: self.changed=False for x in range(9): for y in range(9): self.clean1(x,y) for out1,out2 in self.outs(): self.clean3(out1,out2) def __repr__(self): result="" for x in range(9): for y in range(9): if len(self[x,y])==1: haf=self[x,y][0] else: haf=self[x,y] result+=str(haf)+' ' result+='\n' return result from collections import deque cla
