Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old February 25th, 2004, 02:10 PM
dTRauMa dTRauMa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 3 dTRauMa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb Performance tweaks

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:
Creating string with append: 225ms
Creating string with list.join: 58ms

Reply With Quote
  #17  
Old February 25th, 2004, 04:55 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,479 netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 7 h 39 m 48 sec
Reputation Power: 46
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #18  
Old March 14th, 2004, 09:40 PM
dTRauMa dTRauMa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 3 dTRauMa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Cool Oops

Quote:
Originally Posted by netytan
Maybe this is a Windows thing but there doesn't appear to be a 'timer' or 'timing' module in the standard library.


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 .

Reply With Quote
  #19  
Old March 17th, 2004, 03:06 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
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.

Reply With Quote
  #20  
Old March 17th, 2004, 05:17 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,479 netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 7 h 39 m 48 sec
Reputation Power: 46
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #21  
Old March 25th, 2004, 08:01 PM
Cryo Cryo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 1 Cryo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #22  
Old March 26th, 2004, 07:09 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,479 netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level)netytan User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 7 h 39 m 48 sec
Reputation Power: 46
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #23  
Old April 8th, 2004, 04:02 AM
MasterChief MasterChief is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Virginia
Posts: 491 MasterChief User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 47 m 47 sec
Reputation Power: 6
Send a message via AIM to MasterChief Send a message via MSN to MasterChief
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.

Reply With Quote
  #24  
Old May 27th, 2004, 12:49 PM
Random Random is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 65 Random User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 39 m 56 sec
Reputation Power: 5
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/

Reply With Quote
  #25  
Old September 5th, 2004, 12:57 AM
p4j p4j is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 27 p4j User rank is Lance Corporal (50 - 100 Reputation Level)p4j User rank is Lance Corporal (50 - 100 Reputation Level)p4j User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 h 37 m 17 sec
Reputation Power: 0
Send a message via MSN to p4j
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
From all of the documentation I've read and input I've received, the f_locals seems 'safe' for modification.

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()
I've only 'wrapped' some basic dict methods here, but all of them should and can be wrapped. Notice the repr method is different in that it must perform a check, this is to prevent recursion. In order to finish implementing this as a psuedo local __dict__ in another script, it must be imported. Assuming the above script is saved in a file called local_test.py, then this is how it could be done:
Code:
from local_test import local_dict as __dict__
Now __dict__ acts like the local __dict__ for ANY namespace, whether it be a class method or a function. I just think that is cool! For example:
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

Reply With Quote
  #26  
Old November 27th, 2004, 04:38 PM
cgreb cgreb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: Toronto
Posts: 3 cgreb User rank is Sergeant (500 - 2000 Reputation Level)cgreb User rank is Sergeant (500 - 2000 Reputation Level)cgreb User rank is Sergeant (500 - 2000 Reputation Level)cgreb User rank is Sergeant (500 - 2000 Reputation Level)cgreb User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 32 m 40 sec
Reputation Power: 0
Quote:
If you're going to be building a string iteratively (in a loop, for example), then instead of using string concatenation each time, the most efficient way of doing it is actually aggregating the component strings into a list and then joining them. So, for example:

Code:

somedict = {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
L = []
for (k,v) in somedict:
L.append('%s (%s)' % (v, k))

results = ' '.join(L)


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()])

Reply With Quote
  #27  
Old November 30th, 2004, 04:44 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 6
Send a message via Yahoo to NewPythoner
Hi,
Quote:
Originally Posted by MasterChief
I had an idea to make a thread where people can post tips, tricks, thing to inspire or motivate people, links, etc.


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

Reply With Quote
  #28  
Old January 10th, 2005, 01:33 PM
sf2k's Avatar
sf2k sf2k is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 173 sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level)sf2k User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 18 h 19 m 21 sec
Reputation Power: 5
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

Reply With Quote