|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#16
|
|||
|
|||
|
Hi,
just a note about performance: if you have the choice between code readability and performance, it's usually better to chose readability, unless you are really certain you are inside a hotspot of your code. If you want to check if your code tuning really did change execution time, use the timer module. If you post performance tips here, a sample timer output can be helpful. Code:
import timing
def f():
timing.start()
x=[]
for i in xrange(1,10000):
x.append(str(i))
s = ''.join(x)
timing.finish()
return timing.milli()
def f2():
timing.start()
x=''
for i in xrange(1, 10000):
x += str(i)
timing.finish()
return timing.milli()
print "Creating string with append: %sms" % f2()
print "Creating string with list.join: %sms" % f()
which looks like this (don't laugh about my old cpu: ):Quote:
|
|
#17
|
||||
|
||||
|
Maybe this is a Windows thing but there doesn't appear to be a 'timer' or 'timing' module in the standard library. If this is a third party module maby you could post a link to it's homepage
.So for those of you out there who are scratching there heads and saying "ok, now we know why that didnt work... but how do we check preformance ", 'timeit'. http://www.python.org/doc/2.3.3/lib/module-timeit.html Pythons timeit module (new in Python 2.3 - consider using Pystone or one of the various preformance tracking modules to check preformance with earlier Python versions) makes checking code snippets surprisingly easy! Code:
>>> import timeit
>>>
>>> def one(string): return string
...
>>> tone = timeit.Timer("one('string')", "from __main__ import one")
>>> tone.timeit()
1.2665732655966053
>>> timeit.Timer('None').timeit()
0.36021980447235608
>>> timeit.Timer('pass').timeit()
0.15026267304922847
>>>
Edit: Links to other main Python profile modules. http://www.python.org/doc/2.3.3/lib/module-hotshot.html http://www.python.org/doc/2.3.3/lib/module-profile.html Mark.
__________________
... > (define links (list google scheme ruby python others ...)) ; Read my blog at http://netytan.blogspot.com/. > _
Last edited by netytan : February 25th, 2004 at 05:01 PM. |
|
#18
|
|||
|
|||
|
Quote:
Yes, I'm sorry, I didn't expect to find modules in my installation that weren't part of the standard lib. Somehow my distribution installed a C-Style library with python bindings called timing, perhaps it's part of the C-profiler for python under linux, don't know. Glad you catched this before I cluttered too many files with it, would have been portability nightmare .Next time I find something using help() I'll check with the docs if it really is a standard module. That's a nice tip, too . |
|
#19
|
||||
|
||||
|
Posting Code
If you want help - it is a lot easier for people to read your code if you use the # button on the submit form to make a code block that preserves the layout.
__________________
*** Experimental Python Markup CGI V2 *** Last edited by Grim Archon : March 17th, 2004 at 03:32 AM. |
|
#20
|
||||
|
||||
|
Another nice piece of functionality (related to Grims post) is that you can highlight a section of text in you're post and hit pretty much any of the buttons and the respective tags are placed around the highlighted area!
Mark. |
|
#21
|
|||
|
|||
|
A little tip
Tip: During the active shell console in python, you can ask python for documentation as follows:
Code:
# This is a fake function
def myFakeFcn():
""" These are the commented
documents of my function named
myFakeFcn() where spacing is easily
preserved !"""
print "This function has no purpose!!",
print myFakeFcn.__doc__ #this will output the documentation
>>>
These are the commented
documents of my function named
myFakeFcn() where spacing is easily
preserved !
I know another post stated that the __doc__ could be used as a reference, but I wanted to point out that it can be used during an active shell. |
|
#22
|
||||
|
||||
|
This is a commonly used method of adding test code directly into your modules or even telling your program what to do when its run. In any case very usfull stuff... "But we already know this?!"... Most do but as it seems to come up periodically so i thought its worth mentioning here
.Code:
#!/usr/bin/env python
#Any code that you want to be run when Python interprets
#the file. Imported or run.
if __name__ == '__main__':
#This part of the program will only be run if the program
#if executed (is running). If the program is imported then
#Python will ignore this part of the program =].
Note: this isnt a must i.e. main in C/C++. Its just a nice way to organise your programs. Take care guys. Mark. |
|
#23
|
|||
|
|||
|
Link: http://www.onlamp.com/python/python...ution.csp?day=1
Link in detail: Recipe of the day. Every day, a recipe from the Python Cookbook is featured. I just purchased Python Cookbook (edited by Alex Martelli & David Ascher). By the way, I highly recommend this book. Here is what's on the back of the book: "The Python Cookbook is a collection of problems, solutions, and practical examples for Python programmers, written by Python programmers. Over the past year, members of the Python community have contributed material to an online repository of Python recipes hosted by ActiveState. This book contains the best of those recipes, accompanied by overviews and background material by key Python figures. The recipes in the Python Cookbook range from simple tasks, such as working with dictionaries and list comprehensions, to entire modules that demonstrate complex tasks, such as a templating system and network monitoring. This book contains over 200 recipes. This book is a treasure trove of useful code for all Python programmers, from novices to advanced practioners, with contributions from such Python luminaries as Guido van Rossum, David Ascher, Tim Peters, Paul Prescod, Mark Hammond, and Alex Martelli, as well as over 100 other Python programmers. The recipes highlight Python best practices and can be used directly in day-to-day programming tasks, as a source of ideas, or as a way to learn more about Python. The recipes in the Python Cookbook were edited by David Ascher, who is on the board of the Python Software Foundation and is the co-author of Learning Python, and Alex Martelli, who is known for his numerous and exhaustive postings on the Python mailing list. The book contains a foreword by Guido van Rossum, the creator of Python." Read this book on-line. |
|
#24
|
|||
|
|||
|
I thought that this would benefit those who are like me learning python and need a little help in understanding what is going on with their code. You can walk step by step and see what is happening line by line, this is similar to debugging in VB. Hope someone finds it useful in their learning proccess.
Download from https://sourceforge.net/projects/hapdebugger/ |
|
#25
|
|||
|
|||
|
Creating a Local __dict__
Accessing the local namespace dict is already done by calling the builtin method locals(). However, the limitation of doing this is that modifying the dict returned by locals() is not considered 'safe'. Safe in the sense that any modifications are not guaranteed to be reflected in the actual namespace.
Another approach I have taken is to access the local dict via: Code:
sys._getframe().f_locals Using this approach, I have created a psuedo local __dict__. Here is a snippet of the main script code to do this: Code:
import sys
__all__ = ['local_dict']
bad = ['__init__', '__new__', '__repr__']
############################################################
class __dict( dict ):
"""
Wrapper to mimic a local dict.
Written by Derrick Wallace
"""
def __init__( self, *args ):
dict.__init__( self, *args )
for attr in dict.__dict__:
if callable( dict.__dict__[attr] ) and ( not attr in bad ):
exec( 'def %s(self, *args): return dict.%s(sys._getframe(1).f_locals, *args)'%( attr, attr ) )
exec( '__dict.%s = %s'%( attr, attr ) )
#####################################
def __repr__( self, *args ):
# Must implement repr to prevent recursion
if sys._getframe(1).f_code == sys._getframe().f_code:
return '{...}'
return dict.__repr__( sys._getframe(1).f_locals, *args )
local_dict = __dict()
Code:
from local_test import local_dict as __dict__ Code:
>>> from local_test import ldict as __dict__
>>> __dict__
{'__builtins__': <module '__builtin__' (built-in)>, '__file__': 'C:\\Python23\\Scripts\\pyshell', '__dict__': N/A, '__name__': '__main__', '__doc__': None}
>>> def Foo(a, b=1):
... print __dict__
...
>>> Foo(100)
{'a': 100, 'b': 1}
>>> class Test:
... def Get(self):
... print __dict__
...
>>> Test().Get()
{'self': <__main__.Test instance at 0x0140DDA0>}
This definitely works and, as far as I know, safe. Enjoy! Derrick Last edited by p4j : September 8th, 2004 at 10:20 AM. Reason: Improve dict wrapper |
|
#26
|
|||
|
|||
|
Quote:
Or if you're feeling more functional (should be similar efficiency): Code:
somedict = {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
result = reduce(lambda a,b: a+' '+b, ['%s (%s)'%(a,b) for a,b in somedict.items()])
|
|
#27
|
|||
|
|||
|
Hi,
Quote:
Guess I'm violating "the rules" by posting the Pitfalls of Python ... anyway I go ahead..being a complete Python aficionado, I took it in the right spirit! Cld check up this link.... http://zephyrfalcon.org/labs/python_pitfalls.html Rgds, Subha ![]() |
|
#28
|
||||
|
||||
|
look through string and os modules
For newb's like me. Seems to me that time and time again I'm looking through the string and os modules. Get to know them. They are your friends.
If you do a lot of file processing, knowing the os module (there's a lot) makes using the filesystem a breeze. If you ever have to work with string constants, look at the string module. Odds are it's already there. (ie: string.digits, string.punctuation etc) So for example, I was making a general utility to search all my source files for a phrase inside my code. (I forget where I put stuff right?) Checked the os module. Sure enough, I found os.walk, (os.path.walk < 2.3) which was immediately useful for my code. This is a recursive search, taking a walk down each tree branch from whatever starting point. I can't imagine how much time this has saved me. Sweet. Cheers sf2k |