Re: [Tutor] Reading gzip files
On Sun, Nov 30, 2008 at 9:42 PM, Dinesh B Vadhia [EMAIL PROTECTED] wrote: Strangely, for the files that don't work I can read/print the file almost to the end and then it falls over with the CRC error. That's because the CRC isn't checked until after the file is read; as you can see from the traceback, the error is in _read_eof(). You could put a print statement or breakpoint in that function to get more information about the failure. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] attribute of built-in type
Kent Johnson a écrit : [...big snip...] Do you know that you can probably just assign a __name__ attribute to the objects? Or name, or whatever you like? In [13]: class Foo(object): pass : In [14]: f=Foo() In [15]: f.name --- AttributeErrorTraceback (most recent call last) /Users/kent/ipython console in module() AttributeError: 'Foo' object has no attribute 'name' In [16]: f.name = 'f' In [17]: f.name Out[17]: 'f' This will work for custom objects that do not have a __slots__ attribute. Perhaps you could wrap the creation of the objects in a function that gives them a name? Exactly. I presently use a tool func (actually, a Classmethod) that executes this (pseudocode): def set_names(scope): for name,obj in scope.__dict__.items(): # exclude not_to_be_named objects of the scope if name_has_the_proper_form: # 'name' attr already used for other purpose obj.id = name Which works. I will use this method if I cannot find a way to let the objects natively have __name__ attributes. Conceptually , it is not the same thing -- at least for me. That a class of objects has such an attribute may be seen as an interface characteristics (comparable to 'iterable' or 'ordered') that could be inherited. If I want a class to define/construct ordered containers, I will simply let it inherit list's interface. The facts that python sets __name__ attributes at a low level (as you explain below) and that workarounds are not satisfying, both confirm that this is a fondamental interface charasteristics, not a superficial one. Also, I need a name format rule to distinguish to_be_named from not_to_be_named objects. This is not a big issue, as this rule has only to be followed inside a specific scope. But maybe you understand that I do not find this conceptually /satisfying/. A logical feature that should not depend on such a random detail, rather it should be a basic property. Imagine a case where you would need to give objects characteristics such as iterable or ordered (or mutable / immutable!), by implementing the proper __xxx__ methods, based on the fact that their name has this or that format. Which may happen if such characteristics could not be inherited in python. [...smaller snip...] It is an illustration of what i'm trying to do: let a class inherit from 'function' so that its instances get a __name__. This wouldn't be a harmful overload I guess, as these objects have a __call__ method anyway (the reason why I chose 'function'). I doubt this would do what you want. AFAICT the function name is assigned by the compiler, not by the function constructor. (That is a bit of oversimplification but close enough. I think the compiler creates a code object, passing its name to the constructor; when a function object is wrapped around the code object, it pulls its name from the code object.) This let me play a bit further with functions. The following confirms both that __name__ is a very low-level attribute, and that it lies in the code itself: = import types ; Function = types.FunctionType def typ(obj): return obj.__class__.__name__ def f():pass g = f print id(f) print %s: %s at %s \n % ( g.__name__,typ(g),id(g) ) print dir(f) cod = g.func_code print cod is a '%s' object\n % typ(cod) h = Function(cod,{}) print %s: %s at %s \n % ( h.__name__,typ(h),id(h) ) == 10464752 f: function at 10464752 ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name'] cod is a 'code' object f: function at 10464816 == g points to the same, unique, function as f. So that we can understand that g's __name__ actually is 'f'. But this shows that an object's birthname is somehwhat different from further name to which the object may be bound. Now, h points to brand new object (as confirmed by its id); still, its __name__ is 'f'. This attr is well carried by the code and retrieved there, but it is not an attribute of code objects (cod.__name__ does not exist). As I see it, the __name__ is somehow a third kind of name/id for objects. In fact, if 'id()' was called 'address()' instead, then __name__ could well be called __id__. I also find highly meaningful that, among built_in types, the bool, int, float, str,... series do not have a __name__. (But this is probably an off-topic subject for the python_tutor list.) Kent Salutation, denis ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 58, Issue 2
Stooges.py i,j,k = 3,3,3 while i != 1: print 'Larry, Moe Curly Joe!' i -= 1 while j != 1: print 'Go Mad!!' j -= 1 while k != 1: print 'Go-go bad-bad!!' k -= 1 print '\nBye-bye.' I am trying to learn loops. These nested 'whiles' work OK but I would like to wrap this script in a 'for' loop. I have not been able to make that work. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 58, Issue 2
Try this: for x in xrange(3, 0, -1): : print x : : 3 2 1 HTH, Wayne On Mon, Dec 1, 2008 at 2:20 PM, WM. [EMAIL PROTECTED] wrote: Stooges.py i,j,k = 3,3,3 while i != 1: print 'Larry, Moe Curly Joe!' i -= 1 while j != 1: print 'Go Mad!!' j -= 1 while k != 1: print 'Go-go bad-bad!!' k -= 1 print '\nBye-bye.' I am trying to learn loops. These nested 'whiles' work OK but I would like to wrap this script in a 'for' loop. I have not been able to make that work. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn't. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] converting processing code to python code
I have a simple processing code that I'm trying to work with in python, but I keep getting syntax errors. Any help on changing the code would be greatly appreciated. Thanks. void setup() { size(550, 500); noStroke(); smooth(); fill(255, 255, 255, 150); } //Square 1 vars int x1 = 5; int y1 = 10; int x1Speed = 2; int y1Speed = 2; //Square 2 Vars int x2 = 150; int y2 = 100; int x2Speed = 4; int y2Speed = 4; int size = 100; void draw() { background(180, 0, 0); drawSquare1(); drawSquare2(); checkCollision(); } void drawSquare1() { if(x10 || x1width-size) { x1Speed = -x1Speed; } if(y10 || y1height-size) { y1Speed = -y1Speed; } x1+= x1Speed; y1+= y1Speed; rect(x1, y1, size, size); } void drawSquare2() { if(x20 || x2width-size) { x2Speed = -x2Speed; } if(y20 || y2height-size) { y2Speed = -y2Speed; } x2+= x2Speed; y2+= y2Speed; rect(x2, y2, size, size); } void checkCollision() { if(abs(x1-x2) size abs(y1-y2) size) { println(Collision); //fill(255, 255, 255, 200); x1Speed=-x1Speed; x2Speed=-x2Speed; y1Speed=-y1Speed; y2Speed=-y2Speed; } else { fill(255, 255, 255, 100); }; } -- Erica Osher ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting processing code to python code
On Mon, Dec 01, 2008 at 03:48:59PM -0500, Erica Osher wrote: I have a simple processing code that I'm trying to work with in python, but I keep getting syntax errors. Any help on changing the code would be greatly appreciated. Thanks. Could you show us what you have so far in Python and what syntax errors you get? Just as one example, this would be pretty straightforward to convert straight to Python, syntactically. Whether you'd want to restructure the application to be more optimally Pythonic is another topic depending on what else is going on (or is this the entire program)? void drawSquare1() { if(x10 || x1width-size) { x1Speed = -x1Speed; } if(y10 || y1height-size) { y1Speed = -y1Speed; } def drawSquare1(): if (x1 0 or x1 width-size): x1 += x1Speed y1 += y1Speed rect(x1, y1, size, size) Is that the sort of code you're coming up with? One thing that strikes me off the top here is that (in either language) you'd be better off not using all those global variables. Make your functions take parameters and use them in your calculations. -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting processing code to python code
On Mon, Dec 01, 2008 at 12:59:23PM -0800, Steve Willoughby wrote: void drawSquare1() { if(x10 || x1width-size) { x1Speed = -x1Speed; } if(y10 || y1height-size) { y1Speed = -y1Speed; } def drawSquare1(): if (x1 0 or x1 width-size): x1 += x1Speed y1 += y1Speed rect(x1, y1, size, size) I think my eyes skipped somewhere while copying that over. The exact translation of that code snippet would of course have been: def drawSquare1(): if x1 0 or x1 width-size: x1Speed = -x1Speed if y1 0 or y1 height-size: y1Speed = -y1Speed Sorry 'bout that. -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting processing code to python code
Erica Osher [EMAIL PROTECTED] wrote I have a simple processing code that I'm trying to work with in python, but I keep getting syntax errors. Any help on changing the code would be greatly appreciated. Thanks. It would help to have some background. What language are you translating from? It could be C/C++/JavaScript or Java. Or possibly other C type languages. It might be significant! Also show us what you tried and the syntax error messages. That way we can figure out what it is you are doing wrong. Otherwise we wind up writing your code for you and you learn nothing. Then we have to do it all o er again next time you get stuck. That's inefficient for both you and us! -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting processing code to python code
The code was originally created for a Processing Project and I'm just starting to learn python and I'd like to build on this code. The syntax error I get is *Traceback (most recent call last): File nodebox/gui/mac/__init__.pyo, line 332, in _compileScript File untitled, line 8 int x1 = 5; ^ SyntaxError: invalid syntax * any suggestions? Thanks. On Mon, Dec 1, 2008 at 6:30 PM, Alan Gauld [EMAIL PROTECTED]wrote: Erica Osher [EMAIL PROTECTED] wrote I have a simple processing code that I'm trying to work with in python, but I keep getting syntax errors. Any help on changing the code would be greatly appreciated. Thanks. It would help to have some background. What language are you translating from? It could be C/C++/JavaScript or Java. Or possibly other C type languages. It might be significant! Also show us what you tried and the syntax error messages. That way we can figure out what it is you are doing wrong. Otherwise we wind up writing your code for you and you learn nothing. Then we have to do it all o er again next time you get stuck. That's inefficient for both you and us! -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Erica Osher ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting processing code to python code
On Mon, Dec 01, 2008 at 06:40:03PM -0500, Erica Osher wrote: The code was originally created for a Processing Project and I'm just starting to learn python and I'd like to build on this code. The syntax error I get is *Traceback (most recent call last): File nodebox/gui/mac/__init__.pyo, line 332, in _compileScript File untitled, line 8 int x1 = 5; That's not even close to Python syntax. That looks like a C-derived language. I'd recommend going through a basic Python tutorial first, so you can see how Python works in a general sense before trying to convert code. In Python, you don't need to declare variables like that, you just assign them values. So that line would be simply: x1 = 5 SyntaxError: invalid syntax * any suggestions? Thanks. On Mon, Dec 1, 2008 at 6:30 PM, Alan Gauld [EMAIL PROTECTED]wrote: Erica Osher [EMAIL PROTECTED] wrote I have a simple processing code that I'm trying to work with in python, but I keep getting syntax errors. Any help on changing the code would be greatly appreciated. Thanks. It would help to have some background. What language are you translating from? It could be C/C++/JavaScript or Java. Or possibly other C type languages. It might be significant! Also show us what you tried and the syntax error messages. That way we can figure out what it is you are doing wrong. Otherwise we wind up writing your code for you and you learn nothing. Then we have to do it all o er again next time you get stuck. That's inefficient for both you and us! -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Erica Osher ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 58, Issue 2
On Mon, Dec 1, 2008 at 5:24 PM, Alan Gauld [EMAIL PROTECTED]wrote: snipSince the OP isn't using the loop counter a simpler solution is simply for x in range(3): but the OP was looping from 3 to 1, and that's the easiest way I knew of. -Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] 'for' loops
I recently asked a question about 'for' loops, expecting them to be similar to 'for-next' loops. I have looked at several on-line tutors but am still in the dark about what 'for' loops do. Does anyone have a plain English about the use of 'for' loops? Are 'while' loops the only way Python runs a sub-routine over over? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'for' loops
On Mon, Dec 01, 2008 at 04:44:02PM -0800, WM. wrote: I recently asked a question about 'for' loops, expecting them to be similar to 'for-next' loops. I have looked at several on-line tutors but am still in the dark about what 'for' loops do. Does anyone have a plain English about the use of 'for' loops? Are 'while' loops the only way Python runs a sub-routine over over? No, both 'while' and 'for' loops are for running a block of code (whether subroutine calls or whatever) over and over. The difference between the two is that 'while' will continue repeating the block for however many iterations it takes until the condition is satisfied ('while x is true, for some expression x'), a 'for' loop will run the block of code a set number of times ('once for each element of some set of values'). So if you want to execute 'print' for every line of a file, you would do this: for line in file: print line If you wanted to double a value until it exceeded 100, you would use a while loop: while x = 100: x *= 2 If you just want something executed a specific number of times, (like print hello 10 times), you can use a for loop: for i in range(10): print hello This is, just like any 'for' loop, executing the block once per element of a list. The list in this case is range(10) which is an expression that generates the list (0, 1, 2, ..., 9), so you get one run through the code for each of those. Does that help? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'for' loops
On Mon, Dec 1, 2008 at 6:44 PM, WM. [EMAIL PROTECTED] wrote: I recently asked a question about 'for' loops, expecting them to be similar to 'for-next' loops. I have looked at several on-line tutors but am still in the dark about what 'for' loops do. Does anyone have a plain English about the use of 'for' loops? Are 'while' loops the only way Python runs a sub-routine over over? For loops are mainly used when you want a specific number of iterations, such as looping over the elements of a list. In C/C++ you would do something like this: int myarray[] = {1, 2, 3, 4, 5}; for(int x = 0; x 5; x++) printf(%d, myarray[x]) In python it would be much cleaner: myarray = [1, 2, 3, 4, 5] for x in myarray: print x HTH, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn't. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'for' loops
On 02/12/2008, WM. [EMAIL PROTECTED] wrote: I recently asked a question about 'for' loops, expecting them to be similar to 'for-next' loops. I have looked at several on-line tutors but am still in the dark about what 'for' loops do. Does anyone have a plain English about the use of 'for' loops? Are 'while' loops the only way Python runs a sub-routine over over? I'm not sure exactly what you understand by a for-next loop. A for loop, essentially, iterates over a list [1]. e.g. for fruit in ['apple', 'pear', 'banana', 'tomato']: print fruit The loop will set the variable 'fruit' to be 'apple', 'pear', etc. on each pass through the loop. If you just want to do something n times, the usual idiom is: for i in range(n): # do something, possibly involving i range(n) is a function that will produce the list [0, 1, 2, ..., n-1]. Tutorials should cover this, so I'm not sure if I'm telling you anything new. If there's something particular you're stuck on, ask :-) -- John. [1] Technically, it iterates over an iterator, which you can think of as an object that behaves like a list when you throw it at a for loop. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'for' loops
WM. [EMAIL PROTECTED] wrote I recently asked a question about 'for' loops, expecting them to be similar to 'for-next' loops. I have looked at several on-line tutors but am still in the dark about what 'for' loops do. Python for loops are like foreach loops in other languages. A Python for loop executes a bit of code for each element in a sequence (list, string, dictionary, set, file etc) It will keep looping until it runs out of items in the sequence. Thus to print each letter in a string: mystring = 'foobar' for ch in mystring: print ch Or to print each element of a list: mlist = [1,'2,'a',45, True] for item in mylist: print item And if you want to loop for a fixed number of iterations simply construct a list with that number of elements. The range() function does that for us, thus: for n in range(12): print 'hi' will print 'hi' 12 times. Does anyone have a plain English about the use of 'for' loops? Are 'while' loops the only way Python runs a sub-routine over over? while loops are used much less in Python than in other languages because for loops are so powerful. while lops are generally used in cases where you don't know how many times you need to loop or you want to loop 'forever'. while True: print 'Can't stop me now!' will keep on looping until you close the program c = 0 while c != -1: c = int(raw_input('Enter a number(-1 to stop) ')) print c will keep looping until the user enters -1 More info and a comparison with JabaScript and VBScript can be found in my tutor in the looping topic. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting processing code to python code
Erica Osher [EMAIL PROTECTED] wrote The code was originally created for a Processing Project and I'm just starting to learn python and I'd like to build on this code. The fact that you still don't tell us what the original language is suggests you are not a very experienced programmer in any language. Is that assumption correct? The syntax error I get is *Traceback (most recent call last): File nodebox/gui/mac/__init__.pyo, line 332, in _compileScript File untitled, line 8 int x1 = 5; ^ SyntaxError: invalid syntax In Python variables are just names that refer to objects. The objects can be of any type so we don't need to declare them as int, float, char etc. You just need x1 = 5 No semi colons are needed either. However since this is such a basic Python statement I do think you should take an hour or two to go through some of the complete beginners tutorials found here: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers Try mine if you like but there is a variety of styles for you to choose from. Once you are familiar with the basics try converting your code again and ask specific questions here for help. That will be much more efficient for all of us than simply randomly trying things then asking for help. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting processing code to python code
I wrote the original code in a program called Processing. ( http://processing.org/) Thanks for your advice, I definitely need some tutorials. On Mon, Dec 1, 2008 at 8:23 PM, Alan Gauld [EMAIL PROTECTED]wrote: Erica Osher [EMAIL PROTECTED] wrote The code was originally created for a Processing Project and I'm just starting to learn python and I'd like to build on this code. The fact that you still don't tell us what the original language is suggests you are not a very experienced programmer in any language. Is that assumption correct? The syntax error I get is *Traceback (most recent call last): File nodebox/gui/mac/__init__.pyo, line 332, in _compileScript File untitled, line 8 int x1 = 5; ^ SyntaxError: invalid syntax In Python variables are just names that refer to objects. The objects can be of any type so we don't need to declare them as int, float, char etc. You just need x1 = 5 No semi colons are needed either. However since this is such a basic Python statement I do think you should take an hour or two to go through some of the complete beginners tutorials found here: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers Try mine if you like but there is a variety of styles for you to choose from. Once you are familiar with the basics try converting your code again and ask specific questions here for help. That will be much more efficient for all of us than simply randomly trying things then asking for help. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Erica Osher ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 58, Issue 2
W W [EMAIL PROTECTED] wrote for x in range(3): but the OP was looping from 3 to 1, and that's the easiest way I knew of. Yes, but they weren't using the counter so it didn't matter which way they looped. I suspect the decrement pattern was just the one they were most familiar with in some other language (like Java? or VB?) Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting processing code to python code
Erica Osher [EMAIL PROTECTED] wrote I wrote the original code in a program called Processing. ( http://processing.org/) Aha! The language is actually called Processing. Now that's a new one on me. Thanks for the link, it does help to have the context. :-) Now the next question, are you experienced in Processing or are you a beginner there too? If you are experienced in another language the best tutiorial is the standard Python tutorial found on the Python website. If you are a beginner in Processing as well stick to the newbies tutors I mentioned last time. Regards, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting processing code to python code
Erica Osher [EMAIL PROTECTED] wrote I wrote the original code in a program called Processing. ( http://processing.org/) Having had a look at the web site it is obvious that the hardest bit of porting the Processing code to Python is that Python does not support all the visual drawing functions that Processing does. Most of them can be replicated by writing equivalent functions in Python but that is a lot of work - effectively building a python vesion of Processing! (That would be an exellent project BTW but not one for a beginner!) I tried a Google search. The best I could find was: http://i.document.m05.de/?p=483 which suggests using Jython whicjh allows you to call the Java processing libraies/classes from within Python code. But to understand it I think you still need to go through the tutorials first. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] try except block for multiple statements
I would like to use a try except to see if a value exists. But, when I use the following, if a does not exist it exits. I understand why this does this, but is there a way to get b,c, and d if a does not exist without using a try except for every statement? try: fo.write(a = %s\n %plan.a) fo.write(b = %s\n %plan.b) fo.write(c = %s\n %plan.c) fo.write(d = %s\n %plan.d) except AttributeError: pass -- Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. -Albert Einstein ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] try except block for multiple statements
On 02/12/2008, Bryan Fodness [EMAIL PROTECTED] wrote: I would like to use a try except to see if a value exists. But, when I use the following, if a does not exist it exits. I understand why this does this, but is there a way to get b,c, and d if a does not exist without using a try except for every statement? try: fo.write(a = %s\n %plan.a) fo.write(b = %s\n %plan.b) fo.write(c = %s\n %plan.c) fo.write(d = %s\n %plan.d) except AttributeError: pass AFAIK, no -- but you could always use a loop. attrs = ['a', 'b', 'c', 'd'] for attr in attrs: try: fo.write('A = %s\n' % getattr(plan, attr)) except AttributeError: pass -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] try except block for multiple statements
Bryan Fodness wrote: I would like to use a try except to see if a value exists. But, when I use the following, if a does not exist it exits. I understand why this does this, but is there a way to get b,c, and d if a does not exist without using a try except for every statement? try: fo.write(a = %s\n %plan.a) fo.write(b = %s\n %plan.b) fo.write(c = %s\n %plan.c) fo.write(d = %s\n %plan.d) except AttributeError: pass def foo(obj, attr): val = getattr(obj, attr, None) if val is not None: obj.write(%s = %s\n % (attr, val)) foo(plan, a) foo(plan, b) foo(plan, c) foo(plan, d) -- Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. -Albert Einstein ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'for' loops
On Mon, Dec 1, 2008 at 7:56 PM, John Fouhy [EMAIL PROTECTED] wrote: [1] Technically, it iterates over an iterator, which you can think of as an object that behaves like a list when you throw it at a for loop. The object of the 'in' must be an iterable, which is an object that can produce an iterator when asked. A list is an iterable, not an iterator. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] attribute of built-in type
Here is an idea that might help - you do have some control over assignment to attributes of an object. If you stored your objects in another object you could assign __name__ attributes automatically. For example: class Container(object): def __setattr__(self, name, value): if not hasattr(value, '__name__'): try: value.__name__ = name except: pass object.__setattr__(self, name, value) class Item(object): pass c = Container() c.s = 'string' c.i = Item() print c.i.__name__ # i print c.s.__name__ # AttributeError, can't add __name__ attribute to string Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] I asked about loops
and your response was most gratifying. I think that I now have a handle on the subject and want to say, Thanks to you all. WM ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] noise function
Hi! Does anyone know if python has a noise function? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] sqlite3 Python25 parameter binding problem with UPDATE please help
Hello, Does sqlite3 in python 2.5 supports parameter bindings in UPDATE statement? When I do like the following: oCon.execute(UPDATE rezerve SET latusaldo =? where gads =?;,(result, [year])) oCon.commit() it throws me the error: sqlite3.InterfaceError: error binding parameter 1 probably unsupported type All works OK when using INSERT with the same parameters. On Google I found that python with MySQL supports this syntax (sorry I am not able to find the link now) but I am not able to get it working with sqlite3 Thanks in advance Maybe I should as this question on sqlite list? Aivars ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor