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:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old December 31st, 2003, 12:42 PM
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
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!

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: 6
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,529 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 17 h 19 m 5 sec
Reputation Power: 63
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: 7
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: 7
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,529 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 17 h 19 m 5 sec
Reputation Power: 63
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,529 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 17 h 19 m 5 sec
Reputation Power: 63
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 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
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,529 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 17 h 19 m 5 sec
Reputation Power: 63
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 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear 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,529 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 17 h 19 m 5 sec
Reputation Power: 63
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,529 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 17 h 19 m 5 sec
Reputation Power: 63
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: 7
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