|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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! ![]() |
|
#2
|
|||
|
|||
|
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]
whereas 'dictarray = [{} for i in range(10)]' creates 10 seperate dictionaries Cheers, Eli |
|
#3
|
||||
|
||||
|
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. Last edited by netytan : December 31st, 2003 at 04:43 PM. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
||||
|
||||
|
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. |
|
#8
|
|||
|
|||
|
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. ![]() |
|
#9
|
||||
|
||||
|
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. |
|
#10
|
||||
|
||||
|
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. |
|
#11
|
||||
|
||||
|
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. |
|
#12
|
||||
|
||||
|
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. |
|
#13
|
|||
|
|||
|
Backticks are shorthand for repr(), basically.
|