Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 31st, 2003, 12:42 PM
MasterChief MasterChief is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 543 MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 11 h 18 m 34 sec
Reputation Power: 23
Lightbulb Tips, tricks, inspiration, etc

I had an idea to make a thread where people can post tips, tricks, thing to inspire or motivate people, links, etc.

Hopefully this thread will be able to help any Pythoner. Whether it be a complete beginner or a very advanced expert.

Tips: An example would be stuff that will help improve someone's programming. Such as: "Instead of using all of these functions, you can easily accomplish the same thing just using these two functions."

Tricks: An example would be stuff that will help someone program faster.

Inspiration: An example would be anything that serves as a great source of inspiration.

Motivation: An example would be anything that helps motivate someone.

Links: This could be newsgroups, mailing lists, IRC channels, web sites, etc.

If people like this thread and post a lot of stuff, I will make a documentation.

Enjoy!
Comments on this post
mxatzis12 agrees: awesome!!!

Reply With Quote
  #2  
Old December 31st, 2003, 04:06 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
I like this idea. Hope ppl can try keep the msgs consice

Here's a couple of things I just picked up.
I'm on a few python mailing lists and this one came up recently.
This is supposed to be much faster than providing a comparison function to list.sort()
Code:
unsorted_list = [{'id':99, 'title':'a'},{'id':42, 'title':'b'}, ... ] 
decorated_list = [(x['id'],x) for x in unsorted_list]
decorated_list.sort()
sorted_list = [y for (x,y) in decorated_list]
Another thing: using : 'dictarray = [{}]*10' creates only one dictionary, with ten references to it.
whereas 'dictarray = [{} for i in range(10)]' creates 10 seperate dictionaries

Cheers,
Eli

Reply With Quote
  #3  
Old December 31st, 2003, 04:30 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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
Sounds like a greate idea Chief! I've made this thead sticky for now and depending on how it goes i'll leave it that way .

Link: http://www.python.org/doc/essays/styleguide.html

The link points at the Python Style Guide, one of the main problems i find is everyone has a different style and some totaly different!

Get Posting guys! Can't wait to see what other ppl add!

Mark.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : December 31st, 2003 at 04:43 PM.

Reply With Quote
  #4  
Old January 2nd, 2004, 11:48 AM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
TIP:
Write unit tests! Python comes with PyUnit (the unittest module), and you should take advantage of that. It makes adding incremental improvements a LOT less painful because you can test and make sure your existing functionality isn't broken. In fact, the way we prefer to do tests on the IRC bot I'm working (link) is that you write the test first and then you code the functionality so that it passes the tests that you code. Same goes for bugs that we find. Instead of trying to fix the bug right away we write a test that (currently) pokes the bug and causes the error. Then we know the bug is fixed once we run the test and it passes.
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
  #5  
Old January 2nd, 2004, 12:05 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Oh yeah, just remembered another trick.

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)


instead of:
Code:
somedict = {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
s = ""
for (k,v) in somedict:
    s += '%s (%s) ' % (v, k)
results = s[:-1]   # Remove trailing space


Actually, I hadn't thought of the trailing space issue, but using a list makes that issue go away too

The reason it's faster is because every time you concatenate a string it has to use 3 strings: the original, the stuff to add, and the result string (because strings are immutable!!, so you can't just add to the original string object). So all the build-up/tear-down time for each of those objects is incurred with each iteration. When using a list you just have to build a string and append it to a list (which is a cheap operation, no new objects required since lists are mutable) each time. Then you just join them at the end, which is cheaper than concatenating one at a time.

Reply With Quote
  #6  
Old January 29th, 2004, 08:04 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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
print without the new line...

Usually not a problem but from time to time that extra \n can get in the way! Now before today i would have used sys.stdout.write(str) but i just discovered this handy little syntax treat.

By adding an extra comma at the end of you're print statment you can actually avoid outputting the \n char i.e.

Code:
>>> for each in ('just', 'a', 'few', 'strings'):
...     print each
...     
just
a
few
strings
>>> for each in ('just', 'a', 'few', 'strings'):
...     print each,
...     
just a few strings
>>>


Mark.

Reply With Quote
  #7  
Old January 29th, 2004, 08:13 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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
PyCrust

Just started using PyCrust, i'm very impressed so far, definatly a good enough reason to install wxPython even if you don't plan on doing any GUI development!

Mark.

Reply With Quote
  #8  
Old January 29th, 2004, 10:27 PM
MasterChief MasterChief is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 543 MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level)MasterChief User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 11 h 18 m 34 sec
Reputation Power: 23
I just installed wxPython 2.4 for Python 2.3

It's really cool! I also recommend you getting this wonderful thing.

Not sure what PyCrust is? I'm quoting this from http://sourceforge.net/projects/pycrust/:

PyCrust is an interactive Python shell written in Python using wxPython. PyCrust is now part of wxPython (www.wxpython.org), so packaged files are no longer available here, and the latest development version has moved to the wxWindows CVS repository.

All of the replies so far work very well! Thanks guys! Keep them coming.

Reply With Quote
  #9  
Old January 30th, 2004, 03:35 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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
PyCrust is awesum, yet another reason in my lets do away with Tkinter argument .

When installing wxPython (on windows) a new folder is created in the menu; containing docs, examples and a few wx applications with a Pie icon... and one called PyCrust!

Check it out!

Mark.

Reply With Quote
  #10  
Old January 30th, 2004, 10:34 AM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
Send a message via MSN to SolarBear
Why the HECK did I never even tried these Py script ?

Tell me, PyALaMode seems like a nice enough editor but... is there any to actually execute the files inside the shell ? Just like hitting F5 in IDLE.

Reply With Quote
  #11  
Old January 30th, 2004, 03:31 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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
As far as i know it doesn't, but you can select all and copy you're program then 'paste plus' it into you're Python shell with the same effect as running (in theory).

I would quite like to know why PyCrust, PyAlaMode etc arn't just one program... this would make an awesum IDE for Python! But it seems to be in pieces

In any case the wxPython shell is definatly gonna replace IDLE for me!

Mark.

Reply With Quote
  #12  
Old February 2nd, 2004, 01:39 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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
Backticks

I did something today i'd been wanting to do for a while... that is figure out what in hell backticks do in Python. And to my surprise what they actually did was no-where near what i expected having used perl.

In perl backticks indicate system calls but in Python, using backticks is like putting an object directly into a string and escaping special chars that could cause a problem (kinda like raw strings) i.e.

Code:
>>> v1 = 'string'
>>> v2 = "string"
>>> v3 = 'C:\Documents and Settings\Mark\Desktop'
>>> v4 = 'just a "string" in double quotes'
>>> `v1`
"'string'"
>>> `v2`
"'string'"
>>> `v3`
"'C:\\\\Documents and Settings\\\\Mark\\\\Desktop'"
>>> `v4`
'\'just a "string" in double quotes\''
print `v1`
'string'
print `v2`
'string'
print `v3`
'C:\\Documents and Settings\\Mark\\Desktop'
print `v4`
'just a "string" in double quotes'
>>> v5 = (`v1`,`v2`,`v3`,`v4`)
>>> v5
("'string'", "'string'", "'C:\\\\Documents and Settings\\\\Mark\\\\Desktop'", '\'just a "string" in double quotes\'')
>>> 


Mark.

Reply With Quote
  #13  
Old February 2nd, 2004, 02:36 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Backticks are shorthand for repr(), basically.

Reply With Quote
  #14  
Old February 2nd, 2004, 04:20 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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
Sweet , never actually had a use for repr() yet but is still nice to know. Thanks for the info!

Mark.

Reply With Quote
  #15  
Old February 21st, 2004, 02:51 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
Get to know the Python help system

One of favourite features of Python is the extensive help system, but it is not well documented and many people probably don't know of it's existence. Spending a little time to learn how to use it can pay huge dividends.

There are three components to the help system:

1) the extensive documentation that is installed with Python. This is also available in a wide range of formats, including PDF, HTML and Windows Help. If you have installed the Windows version of Python then you will get the WinHelp version, but I recommend installing the HTML version as well, for reasons we shall see in a minute. Download it from http://www.python.org/doc/current/download.html and unzip it, then set the environment variable PYTHONDOCS to point to the directory.

2) the interactive help system. Type help() at the interactive prompt and you will enter the help mode. You can get help on any module, class or function that has a docstring - not just from the modules that originally came with Python, but from any module on the path. If you have the HTML help files installed as described above, then you can also get help on a wide range of language features, such as keywords.
When at the interactive prompt you can also get help directly for any object currently in the namespace, e.g.

Code:
>>> import string
>>> help(string.atoi)
Help on function atoi in module string:

atoi(s, base=10)
    atoi(s [,base]) -> int
    
    Return the integer represented by the string s in the given
    base, which defaults to 10.  The string s must consist of one
    or more digits, possibly preceded by a sign.  If base is 0, it
    is chosen from the leading characters of s, 0 for octal, 0x or
    0X for hexadecimal.  If base is 16, a preceding 0x or 0X is
    accepted.


You can also get help on language features or from modules that are not currently imported by putting the help string in quotes:
Code:
>>> help('re.match')
Help on function match in re:

re.match = match(pattern, string, flags=0)
    Try to apply the pattern at the start of the string, returning
    a match object, or None if no match was found.


Type help('modules') for a list of modules that Python currently knows about.

3) there is a script that will run an HTTP server that will let you browse web pages generated dynamically from the __doc__ strings: PYTHON/tools/scripts/pydocgui.pyw. If you have installed the Windows version then you should have a shortcut to it set up in the Start menu called 'module docs'. Again, this Tkinter program displays help for any module on the path, not just the standard library. It also lets you search the __doc__ strings.

Finally there is an excellent quick reference guide to Python available from http://rgruet.free.fr/#QuickRef.

I believe that the combination of of the help system with the interactive mode and the simple, regular syntax make Python the easiest language on earth to learn.

Dave - The Developers' Coach

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Tips, tricks, inspiration, etc

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap