Python Tutorial - This is probably the socond best tutorial on the web on Python. Why second best you ask? Simple because www.python.org has already done such a wonderfull job on thier tutorial. But if you want to get your hands dirty from the start, you may want to take a whack out our Python tutorial thats assembled for the beginners.
TW Tech Glossary - Misplaced your bible? Well here it is! This truly took a while to complete and should be used by all from beginners to advance techies. Look into it, you won't be sorry. (Very Resourceful)
Software Engineering Phases - There are four fundamental phases in most, if not all, software engineering methodologies. These phases are analysis, design, implementation, and testing. These phases address what is to be built, how it will be built, building it, and making it high quality…
What is Python?
Python is an interpreted, interactive, object-oriented programming language developed by Guido van Rossum. The name comes from one of van Rossum's favorite television shows, Monty Python's Flying Circus. Python combines remarkable power with very clear syntax. It has modules, classes, exceptions, very high level dynamic data types, and dynamic typing. There are interfaces to many system calls and libraries, as well as to various windowing systems (X11, Motif, Tk, Mac, MFC). New built-in modules are easily written in C or C++. Python is also usable as an extension language for applications that need a programmable interface. The Python implementation is portable: it runs on many brands of UNIX, on Windows, DOS, OS/2, Mac, Amiga... If your favorite system isn't listed here, it may still be supported, if there's a C compiler for it. The Python implementation is copyrighted but freely usable and distributable, even for commercial use.
TipsModule runningIn the Python IDE on Windows, you can run a module with File->Run?(Ctrl-R). Output is displayed in the interactive window.In the Python IDE on MacOS, you can run a module with Python->Run window?(Cmd-R), but there is an important option you must set first. Open the module in the IDE, pop up the module's options menu by clicking the black triangle in the upper-right corner of the window, and make sure ?Run as __main__? is checked. This setting is saved with the module, so you only have to do this once per module. On UNIX-compatible systems (including MacOS X), you can run a module from the command line: python odbc helper.py ? Declaring FunctionsAutomatic data typing is a double-edged sword. It's convenient, and it can be extremely powerful. But it places an additional burden on you to understand when and how Python coerces data into different types.Documenting FunctionsMany Python IDEs use the docstring to provide context-sensitive documentation, so that when you type a function name, its docstring appears as a tool tip. This can be incredibly helpful, but it's only as good as the doc strings you write.Testing ModulesOn Mac Python, there is an additional step to make the if __name__trick work. Pop up the module's options menu by clicking the black triangle in the upper-right corner of the window, and make sure Run as __main__ is checked.Profiling CodeThe first step to speeding up your program is learning where the bottlenecks lie. It hardly makes sense to optimize code that is never executed or that already runs fast. I use two modules to help locate the hotspots in my code, profile and trace.Profile ModuleThe profile module is included as a standard module in the Python distribution. Using it to profile the execution of a set of functions is quite easy. Suppose your main function is called main, takes no arguments and you want to execute it under the control of the profile module. In its simplest form you just execute
When main() returns, the profile module will print a table of function calls and execution times. The output can be tweaked using the Stats class included with the module. For more details, checkout the profile module's documentation (Lib/profile.doc). SortingSorting lists of basic Python objects is generally pretty efficient. The sort method florists takes an optional comparison function as an argument that can be used to change the sorting behavior. This is quite convenient, though it can really slowdown your sorts.An alternative way to speed up sorts is to construct a list of tuples whose first element is a sort key that will sort properly using the default comparison, and whose second element is the original list element. Suppose, for example, you have a list of tuples that you want to sort by the n-th field of each tuple. String ConcatenationStrings in Python are immutable. This fact frequently sneaks up and bites novice Python programmers on the rump. Immutability confers some advantages and disadvantages. In the plus column, strings can be used a keys in dictionaries and individual copies can be shared among multiple variable bindings. (Python automatically shares one- and two-character strings.) In the minus column, you can't say something like, "change all the 'a's to 'b's" in any given string. Instead, you have to create a new string with the desired properties. This continual copying can lead to significant inefficiencies in Python programs.Don't forget that Python does all method lookup at runtime. LoopsPython supports a couple of looping constructs. The for statement is most commonly used. It loops over the elements of a sequence, assigning each to the loop variable. If the body of your loop is simple, the interpreter overhead of the for loop itself can be a substantial amount of the overhead. This is where the map function is handy. You can think of map as a for moved into C code. The only restriction is that the "loop body" of map must be a function call.Here's a straight forward example. Instead of looping over a list of words and convert ing them to upper case:
you can use map to push the loop from the interpreter into compiled C code:
List comprehensions were added to Python in version 2.0 as well. They provide a syntactically more compact way of writing the above for loop:
Avoiding dots...Suppose you can't use map? The example above of converting words in a list to upper case has another inefficiency. Both newlist.append and string.upper are function references that are recalculated each time through the loop. The original loop can be replaced with:
Local VariablesThe final speedup available to us for the non-map version of the for loop is to use local variables wherever possible. If the above loop is cast as a function, append and upper become local variables.
Initializing Dictionary ElementsSuppose you are building a dictionary of word frequencies and you've already broken your words up into a list. You might execute something like:
forword in words:
Except for the first time, each time a word is seen the if statement's test fails. If you are counting a large number of words, many will probably occur multiple times. In a situation where the initialization of a value is only going to occur once and the augmentation of that value will occur many times it is cheaper to use a try statement:
It's important to catch the expected Key Error exception, and not have a default except clause to avoid trying to recover from an exception you really can't handle by the statement [s] in the try clause. Import Statement Over headzimport statements can be executed just about anywhere. It's often useful to place them inside functions to restrict their visibility and/or reduce initial startup time. Although Python's interpreter is optimized to not import the same module multiple times, repeatedly executing an import statement can seriously affect performance in some circumstances.Using map with DictionariesI found it frustrating that to use map to eliminate simple for loops like:
I had to use a lambda form or define a namedfunction that would probably negate any speedup I was getting by using map in the first place. I decided I needed some functions to allow me to set, get or delete dictionary keys and values enmasse. I proposed a change to Python's dictionary object and used it for a while. However, a more general solution appears in the form of the operator module in Python 1.4. Suppose you have a list and you want to eliminate its duplicates. Instead of the code above, you can execute:
This moves the for loop into C where it executes much faster. Data AggregationFunction call overhead in Python is relatively high, especially compared with the execution speed of a built-in function. This strongly suggests that extension module functions should handle aggregates of data where possible. Here's a contrived example written in Python. (Just pretend the function was written in C. :-)
Even written in Python, the second example runs about four times faster than the first. Had do it been written in C the difference would likely have been even greater (exchanging a Python for loop for a C for loop as well as removing most of the function calls). Doing Stuff Less OftenThe Python interpreter performs some periodic checks. In particular, it decides whether or not to let another thread run and whether or not to run appending call (typically a call established by a signal handler). Most of the time there's nothing to do, so performing these checks each pass around the interpreter loop can slow things down. There is a function in the sys module, set check interval, which you can call to tell the interpreter how often to perform these periodic checks. In Python 1.4 it defaults to 10. If you aren't running with threads and you don't expect to be catching lots of signals, setting this to a larger value can improve the interpreter's performance, sometimes substantially.Adding a Missi ng string. replace FunctionThe Python 1.4 string module lacks there place (str, old, new [, max]) function. The following code adds it to the imported string module just in case it isn't there:
How can I check if a number is contained in a range?
|
|
>>>x=5 |
How can I check that the Python version is recent enough to run my program?
Note that [1, 5, 2] < [1, 6].? in other words, this will do what you want:|
?import string, sys |
I can be better to not check the version - this will bound poor user to one version. Instead, check for the feature you need. You need a library? Try to import it and fall back gracefully:
|
? ?import pagecast_lib |
How can I trap a keyboard interrupt using Python?
|
importsys |
Is it possible to not have to explicitly do imports?
I would suggest instead that you add an object to the global dictionary in your site setup. It could be called "mods" (for brevity). You would hookup "mods.__getattr__" so that you would say:|
fl=mods.glob.glob('*') |
I thought about this once and it seemed pretty cool to me as a shortcut.
You could argue that we could dump the import statement...(but I wouldn't) Having an explicit object as the root for the dynamically generated package tree would be more Pythonic than having every module magically appear in your global package namespace. Package support needs more thinking through...





