Is try-except slow?

2008-09-02 Thread ssecorp
or why does this take so god damn long time? and if I run into an IndexError it break out of the inner loop right? so having range up to 1000 or 1000 wouldn't matter if biggest working x is 800? def getPixels(fileName): im = PIL.Image.open(fileName) colors = [] for y in range(1,

Getting an objetcs dict?

2008-09-01 Thread ssecorp
I did nce(I think). class X X.__dict__() and ngot a dict of its variables. Now i get errors doing this. what am i doing wrong? -- http://mail.python.org/mailman/listinfo/python-list

Is this a closure?

2008-08-31 Thread ssecorp
A method on a class: def printSelf(self): def printReviews(): for review in self.reviews: review.printSelf() print Idnbr: , self.idnumber, Reviews: , printReviews() I don't have to pass an argument to printReviews because everything defined inside

Some problems with classes

2008-08-31 Thread ssecorp
Why/how is it possible to add variables like this? I don't understand this mechanism: http://docs.python.org/tut/node11.html#SECTION001133 class Employee: pass john = Employee() # Create an empty employee record # Fill the fields of the record john.name = 'John Doe'

Re: Some problems with classes

2008-08-31 Thread ssecorp
class Animal(object): def __init__(self, name, weight): self.name = name self.weight = weight def speak(self): print speak class Vegetable(object): def __init__(self, name, volume): self.name = name self.volume = volume def split(self):

Re: Some problems with classes

2008-08-31 Thread ssecorp
also, how does super() work more exactly? I can't get it quite to work. class Movie(object): def __init__(self, movieId, grades, date): self.movieId = movieId self.grades = grades self.date = date def newGrade(self, grade): self.grades.append(grade)

Re: Some problems with classes

2008-08-31 Thread ssecorp
It works when I inherit from 2 classes but not when I inherit from 2 subclasses. - from __future__ import division class Movie(object): def __init__(self, movieId, grades, date): self.movieId = movieId self.grades = grades

How to update value in dictionary?

2008-08-27 Thread ssecorp
dict.update({a:1}) SETS the dict item a 's value to 1. i want to increase it by 1. isnt that possible in an easy way? I should use a tuple for this? -- http://mail.python.org/mailman/listinfo/python-list

no string.downer() ?

2008-08-27 Thread ssecorp
if i want to make a string downcase, is upper().swapcase() the onyl choice? there is no downer() ? -- http://mail.python.org/mailman/listinfo/python-list

List of modules available for import inside Python?

2008-08-27 Thread ssecorp
Is there a way to view all the modules I have available for import from within Python? Like writing in the interpreter: import.modules Also, is there anything like Cpan for Python? -- http://mail.python.org/mailman/listinfo/python-list

Replace reduce with listcomprehension?

2008-08-25 Thread ssecorp
GvR wants to eliminate all the functional stuff (lambda map reduce filter) which kind of makes sense for Python, listcomprehensions are more efficient(at least as implemented inpython) from what i have gathered and they can express everything map/reduce/filter with crippled lambdas can. but what

Generate alphabet?

2008-08-22 Thread ssecorp
In Haskell I can do [1..10] for range(1,11) and ['a'..'z'] for a list of the alphabet. Is there a way in Python to generate chars? -- http://mail.python.org/mailman/listinfo/python-list

How to stop iteration with __iter__() ?

2008-08-19 Thread ssecorp
I want a parse a file of the format: movieId customerid, grade, date customerid, grade, date customerid, grade, date etc. so I could do with open file as reviews and then for line in reviews. but first I want to take out the movie id so I use an iterator. then i want to iterate through all the

Python GUI interpreter slower than DOS-version?

2008-08-12 Thread ssecorp
Sometimes when running very intensive computations the Python- interpreter kind of overheats and stops responding. I have gotten the impression that the dos-version is less likely to crash. Can that be true and if so, why? Is there anyway to get around this? Pretty annoying that it stops

super, object and type?

2008-08-12 Thread ssecorp
super(object, type) super: class 'object', type object super(type, object) super: class 'type', type object how can both work? they can't both be the superclass of each other right? or is it some sort of mutually recursive definition? help(object) Help on class object in module

Re: for x,y in word1, word2 ?

2008-08-11 Thread ssecorp
On Aug 11, 6:40 am, Mensanator [EMAIL PROTECTED] wrote: On Aug 10, 11:18 pm, ssecorp [EMAIL PROTECTED] wrote: Is there a syntax for looping through 2 iterables at the same time? for x in y: for a in b: is not what I want. I want: for x in y and for a in b: Something like

Wildcards for regexps?

2008-08-10 Thread ssecorp
If I have an expression like bob marley and I want to match everything with one letter wrong, how would I do? so bob narely and vob marley should match etc. -- http://mail.python.org/mailman/listinfo/python-list

for x,y in word1, word2 ?

2008-08-10 Thread ssecorp
Is there a syntax for looping through 2 iterables at the same time? for x in y: for a in b: is not what I want. I want: for x in y and for a in b: -- http://mail.python.org/mailman/listinfo/python-list

Why doesn't import work?

2008-08-04 Thread ssecorp
I have in Lib/site-packages a module named pdfminer. when I do import pdfminer it complains: import pdfminer Traceback (most recent call last): File pyshell#3, line 1, in module import pdfminer ImportError: No module named pdfminer I created a file pdfminer.py and put it in

Re: Profiling weirdness: Timer.timeit(), fibonacci and memoization

2008-08-03 Thread ssecorp
I think you are confusing 2 people in this thread but that doesn't really matter. What surprised me was that I didn't think fib would benefit from memoization because it didn't repeat the same calculations. fibmem without memoization is the classic naive implementation that grows exponentially and

Profiling weirdness: Timer.timeit(), fibonacci and memoization

2008-08-02 Thread ssecorp
I am not clear about the results here. from timeit import Timer import Decorators def fib(n): a, b = 1, 0 while n: a, b, n = b, a+b, n-1 return b @Decorators.memoize def fibmem(nbr): if nbr 1: return fibmem(nbr-1) + fibmem(nbr-2) if nbr == 1: return

I donä't get while-loops

2008-08-02 Thread ssecorp
in read2 it never quits when I write quit, why? def read(): expr = raw_input(Lisp ) if expr != quit: print parse(expr) read() else: print Good session! def read2(): expr = while expr != quit: expr = raw_input(Lisp ) print parse(expr)

Re: I donä't get while-loops

2008-08-02 Thread ssecorp
oops, embarrassing, I created the while loop not to use recursion then I still did... -- http://mail.python.org/mailman/listinfo/python-list

How smart is the Python interpreter?

2008-07-31 Thread ssecorp
def str_sort(string): s = for a in sorted(string): s+=a return s if i instead do: def str_sort(string): s = so = sorted(string) for a in so: s+=a return s will that be faster or the interpreter can

x*x if x10

2008-07-27 Thread ssecorp
I have seen somewhere that you can write something like: x*x if x10 but exactly that doesn't work and I can't get any variation to work. it is possible to nest with an else too. how do you write it? and also, is it idiomatic? doesn't seem to add functionality, just another way of doing the

Re: Rant (was Re: x*x if x10

2008-07-27 Thread ssecorp
I might be misunderstanding OP but: a+b+c+d+e is simple way of concatenating 5 lists... as a function that takes any amount of lists and concatenates them: def concat(*args): c = [] for elem in args: c += elem return c don't know if extend is faster or

tail-rec decorator, well still blows the stack...

2008-07-21 Thread ssecorp
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691 so I try it and when I run: @Decorators.tail_recursion def fibtr(n): def fibt(a, b, n): if n = 1: return b else: return fibt(b, a + b, n - 1) if n == 0: return 0 else:

Re: tail-rec decorator, well still blows the stack...

2008-07-21 Thread ssecorp
I my function not proper tail-recursion? because this doesn't blow the stack: #!/usr/bin/env python2.4 # This program shows off a python decorator( # which implements tail call optimization. It # does this by throwing an exception if it is # it's own grandparent, and catching such # exceptions

Why is this blowing the stack, thought it was tail-recursive...

2008-07-12 Thread ssecorp
def fib(n): def fibt(a, b, n): if n = 1: return b else: return fibt(b, a + b, n - 1) if n == 0: return 0 else: return fibt(0, 1, n); and can memoization speed up this even more? tesintg with memoization doesnt really say

why is self used in OO-Python?

2008-07-12 Thread ssecorp
I first learned about OO from Java. I much prefer to program in Python though. However I am consufed about 2 things. 1. Why do I have to pass self into every method in a class? Since I am always doing why cant this be automated or abstracted away? Are the instances where I won't pass self? I

Re: why is self used in OO-Python?

2008-07-12 Thread ssecorp
On Jul 12, 8:44 pm, castironpi [EMAIL PROTECTED] wrote: On Jul 12, 1:01 pm, Duncan Booth [EMAIL PROTECTED] wrote: ssecorp [EMAIL PROTECTED] wrote: 1. Why do I have to pass self into every method in a class? Since I am always doing why cant this be automated or abstracted away

Correct use of try,except and raise?

2008-07-12 Thread ssecorp
Is this correct use of exceptions? to raise an indexerror and add my own string insetad of just letting it raise a IndexError by itself and blaming it on list.pop? class Stack(object): def __init__(self, *items): self.stack = list(items) def push(self, item):

Re: Correct use of try,except and raise?

2008-07-12 Thread ssecorp
On Jul 13, 2:32 am, Roy Smith [EMAIL PROTECTED] wrote: In article [EMAIL PROTECTED],  ssecorp [EMAIL PROTECTED] wrote: Is this correct use of exceptions? to raise an indexerror and add my own string insetad of just letting it raise a IndexError by itself and blaming it on list.pop

Re: TypeError, I know why but not how!?

2008-07-10 Thread ssecorp
ty I came to the same conckusion in bed :) now it works. however since there are 400 students and some are incompatible I shouldnt be able to generate a 200room list right? but it works sometimes the other times i get an error. might be because of recursion depth i never let the error finish.

Re: TypeError, I know why but not how!?

2008-07-10 Thread ssecorp
I don't fully understand why I have to do this. On Jul 10, 4:17 am, Robert Kern [EMAIL PROTECTED] wrote: ssecorp wrote: Im looking into PvsNP: http://www.claymath.org/millennium/P_vs_NP/ so I thought I'd write the program just to get a feel for it. But I run into a problem. Why does

Weird lambda rebinding/reassignment without me doing it

2008-07-10 Thread ssecorp
I am never redefining the or reassigning the list when using validate but since it spits the modified list back out that somehow means that the modified list is part of the environment and not the old one. i thought what happend inside a function stays inside a function meaning what comes out is

Re: Weird lambda rebinding/reassignment without me doing it

2008-07-10 Thread ssecorp
ty very good answer. i know i shouldn't use lambda like that, i never do i was just playing around there and then this happened which i thought was weird. On Jul 10, 8:09 pm, Terry Reedy [EMAIL PROTECTED] wrote: David C. Ullrich wrote: In article [EMAIL PROTECTED],  ssecorp [EMAIL

Re: Weird lambda rebinding/reassignment without me doing it

2008-07-10 Thread ssecorp
def mod(x,y): return x.append(y) mod([1,2],3) k=[1,2,3] k [1, 2, 3] l = mod(k,4) l k [1, 2, 3, 4] l k==l False mod(k,5) k [1, 2, 3, 4, 5] mod(l,4) Traceback (most recent call last): File pyshell#29, line 1, in module mod(l,4) File pyshell#18, line 2, in mod return

TypeError, I know why but not how!?

2008-07-09 Thread ssecorp
Im looking into PvsNP: http://www.claymath.org/millennium/P_vs_NP/ so I thought I'd write the program just to get a feel for it. But I run into a problem. Why does it all the sudden return None? I mean I know why the program aborts but I dont understand why the None is generated all the sudden.

Re: Very weird bug!

2008-07-07 Thread ssecorp
i know, idid try it again and it works as expected. but how the h*** did it not work that one time? -- http://mail.python.org/mailman/listinfo/python-list

how are strings immutable in python?

2008-07-06 Thread ssecorp
h = aja baja h += 'e' h 'aja bajae' -- http://mail.python.org/mailman/listinfo/python-list

Re: how are strings immutable in python?

2008-07-06 Thread ssecorp
so if strings were mutable and i did a = b = foo and then did a += bar then a and b would be foobar? -- http://mail.python.org/mailman/listinfo/python-list

Very weird bug!

2008-07-06 Thread ssecorp
I was looking into currying and Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type copyright, credits or license() for more information. Personal firewall software may warn about the

Re: how are strings immutable in python?

2008-07-06 Thread ssecorp
so why would you ever want mutability? seems very counterintuitive and unreliable. -- http://mail.python.org/mailman/listinfo/python-list

Re: PIL(Py Image lib), show() not showing picture...

2008-07-03 Thread ssecorp
On Jul 3, 10:20 pm, Méta-MCI \(MVP\) [EMAIL PROTECTED] wrote: Hi! Which OS?   On Vista, you MUST reconfig the aperçu images et télécopies (in french ; sorry, I don't know the english translation). @-salutations Michel Claveau VISTA. i will try google translate :) --

Re: PIL(Py Image lib), show() not showing picture...

2008-07-03 Thread ssecorp
On Jul 3, 10:20 pm, Méta-MCI \(MVP\) [EMAIL PROTECTED] wrote: Hi! Which OS?   On Vista, you MUST reconfig the aperçu images et télécopies (in french ; sorry, I don't know the english translation). @-salutations Michel Claveau thumbnail images and faxes? and how do i do that? i must do