background preloader

Dive Into Python

Dive Into Python

The Python Tutorial — Python v2.6.4 documentation Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms. The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python web site, and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation. The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). This tutorial introduces the reader informally to the basic concepts and features of the Python language and system.

Pyline: a grep-like, sed-like command-line tool. « ActiveState C Save the script as 'pyline' somewhere on your path, e.g. /usr/local/bin/pyline, and make it executable (e.g. chmod +x /usr/local/bin/pyline). When working at the command line, it's very useful to pipe multiple commands together. Common tools used in pipes include 'head' (show the top lines of a file), 'tail' (show the bottom lines), 'grep' (search the text for a pattern), 'sed' (reformat the text), etc. However, Python is found lacking in this regard, because it's hard to write the kind of one-liner that works well in an ad-hoc pipe statement. Pyline tries to solve this problem. Here are a couple examples: Print out the first 20 characters of every line in the tail of my Apache access log: tail access_log | pyline "line[:20]" Print just the URLs in the access log (the seventh "word" in the line): tail access_log | pyline "words[6]" Here's a tricker one, showing how to do an import. ls | pyline -m os "os.path.isfile(line) and os.stat(line).st_size > 1024 and line" Hopefully you get the idea.

An Introduction to Python Lists You can use the list type to implement simple data structures, such as stacks and queues. stack = [] stack.append(object) object = stack.pop() queue = [] queue.append(object) object = queue.pop(0) The list type isn’t optimized for this, so this works best when the structures are small (typically a few hundred items or smaller). Another data structure for which a list works well in practice, as long as the structure is reasonably small, is an LRU (least-recently-used) container. lru.remove(item) lru.append(item) If you do the above every time you access an item in the LRU list, the least recently used items will move towards the beginning of the list. Searching Lists The in operator can be used to check if an item is present in the list: if value in L: print "list contains", value To get the index of the first matching item, use index: i = L.index(value) The index method does a linear search, and stops at the first matching item. try: i = L.index(value) except ValueError: i = -1 Sorting Lists

Python and MySQL | PC Tips & Tricks Digg this story ? In this post I will show you how to connect to a MySQL database, retrieve data from it and insert data in it using Python. What you need: 1. Python v. 2.3.4 or newer 2. 3. First verify that your version of Python is 2.3.4 or later, and that the MySQLdb module is installed. % python Python 2.5.2 (r252:60911, Jun 6 2008, 23:32:27) [GCC 4.3.1 20080507 (prerelease) [gcc-4_3-branch revision 135036]] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb Traceback (most recent call last): File " Scripts that access MySQL through DB-API using MySQLdb generally perform the following steps: * Import the MySQLdb module * Open a connection to the MySQL server * Issue statements and retrieve their results * Close the server connection Writing the scripts For the script examples I created a database test and a table fruits ; mysql> select * from fruits; | id | name | 1 | apple | 2 | pear | 3 | orange 3 rows in set (0.00 sec) The result is: 1, apple 2, pear

Python Tricks Here, I am collecting python snippets that I find enlightening and/or just useful. On a subpage, you find a JPype-using hack to access Weka's Java classes from Python. Additionally, I want to share some interesting links: When working with files or directories (using os.path), you can make your live much easier if you check out the path Python module. It is very simple, but incredibly useful!Another great python module is BeautifulSoup, which can be used to parse real-world HTML (or XML) files. Uncluttered, Clean Imports I tend to write code in an unordered way, usually copy-pasting stuff from and to IPython. pyflakes is the solution to this problem. Completion in the Python Console It is handy to be able to tab-complete properties of python objects at the python prompt. However, I find it interesting to note that it is possible to have completion in the standard python console (if compiled with readline, which it really should be)! Now, go check out IPython. ;-) Flattening Lists Caution!

Managing DNS zone files with dnspython I've been using dnspython lately for transferring some DNS zone files from one name server to another. I found the package extremely useful, but poorly documented, so I decided to write this post as a mini-tutorial on using dnspython. Running DNS queries This is one of the things that's clearly spelled out on the Examples page. Here's how to run a DNS query to get the mail servers (MX records) for dnspython.org: import dns.resolver answers = dns.resolver.query('dnspython.org', 'MX')for rdata in answers: print 'Host', rdata.exchange, 'has preference', rdata.preference To run other types of queries, for example for IP addresses (A records) or name servers (NS records), replace MX with the desired record type (A, NS, etc.) Reading a DNS zone from a file In dnspython, a DNS zone is available as a Zone object. $TTL 36000example.com. To have dnspython read this file into a Zone object, you can use this code: import dns.zonefrom dns.exception import DNSException Modifying a DNS zone file

File Management in Python Introduction The game you played yesterday uses files to store game saves. The order you placed yesterday was saved in a file. File management is an important part of many applications written in nearly every language. Reading and Writing The most basic tasks involved in file manipulation are reading data from files and writing data to files. fileHandle = open ( 'test.txt', 'w' ) The "w" indicates that we will be writing to the file, and the rest is pretty simple to understand. fileHandle.write ( 'This is a test. This will write the string "This is a test." to the file's first line and "Really, it is." to the file's second line. fileHandle.close() As you can see, it's very easy, especially with Python's object orientation. fileHandle = open ( 'test.txt', 'a' )fileHandle.write ( '\n\n\nBottom line.' )fileHandle.close() Now let's read our file and display the contents: fileHandle = open ( 'test.txt' )print fileHandle.read()fileHandle.close() Only the second line is displayed.

Python best practices by leonardo maffi Version 1.30, Jun 16 2011 Sometimes even good programmers at their first tries of Python use less than optimal solutions and language constructs. In the years Python has accumulated few redundancies and few warts (and some of them will be removed with Python 3.0. For some things I may be wrong, but this page comes from some experience, so when you don't agree with me, I suggest you to go look for the truth inside many newsgroups and Web pages, just don't assume you are right. [Go back to the index (email in the Home Page)] I have to thank many people for suggestions and spotting typos. See PEP 8 too for more style guides for Python code:

26.3. unittest — Unit testing framework — Python v2.6.4 document New in version 2.1. (If you are already familiar with the basic concepts of testing, you might want to skip to the list of assert methods.) The Python unit testing framework, sometimes referred to as “PyUnit,” is a Python language version of JUnit, by Kent Beck and Erich Gamma. JUnit is, in turn, a Java version of Kent’s Smalltalk testing framework. unittest supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework. To achieve this, unittest supports some important concepts: test fixture A test fixture represents the preparation needed to perform one or more tests, and any associate cleanup actions. test case A test case is the smallest unit of testing. test suite A test suite is a collection of test cases, test suites, or both. test runner A test runner is a component which orchestrates the execution of tests and provides the outcome to the user. 25.3.1. 25.3.2. 25.3.2.1. 25.3.3.

PEP 8 -- Style Guide for Python Code Code should be written in a way that does not disadvantage other implementations of Python (PyPy, Jython, IronPython, Cython, Psyco, and such).For example, do not rely on CPython's efficient implementation of in-place string concatenation for statements in the form a += b or a = a + b. This optimization is fragile even in CPython (it only works for some types) and isn't present at all in implementations that don't use refcounting. In performance sensitive parts of the library, the ''.join() form should be used instead.

guaranteed-stable sort with the decorate-sort-undecorate idiom ( "decorate-sort-undecorate" is a general and common idiom that allows very flexible and speedy sorting of Python sequences. An auxiliary list is first built (the 'decorate' step) where each item is made up of all sort-keys (in descending order of significance) of the corresponding item of the input sequence (must include all of the information in the whole corresponding item, and/or an index to it so we can fetch it back [or reconstruct it] in the third step). This is then sorted by its builtin sort method without arguments. This 'naturally' supplies a sorted _copy_, but if the input-sequence is a list we can just assign to its "include everything" slice to get in-place effect. This recipe specifically demonstrates using this to achieve a stable sort (where items that compare equal keep the same relative order in the result list as they had in the input sequence).

PEP 257 -- Docstring Conventions What is a Docstring? A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object. All modules should normally have docstrings, and all functions and classes exported by a module should also have docstrings. Public methods (including the __init__ constructor) should also have docstrings. A package may be documented in the module docstring of the __init__.py file in the package directory. String literals occurring elsewhere in Python code may also act as documentation. String literals occurring immediately after a simple assignment at the top level of a module, class, or __init__ method are called "attribute docstrings".String literals occurring immediately after another docstring are called "additional docstrings". Please see PEP 258, "Docutils Design Specification" , for a detailed description of attribute and additional docstrings. Multi-line Docstrings

HowTo/Sorting Original version by Andrew Dalke with a major update by Raymond Hettinger Python lists have a built-in sort() method that modifies the list in-place and a sorted() built-in function that builds a new sorted list from an iterable. There are many ways to use them to sort data and there doesn't appear to be a single, central place in the various manuals describing them, so I'll do so here. Sorting Basics A simple ascending sort is very easy -- just call the sorted() function. >>> sorted([5, 2, 3, 1, 4]) [1, 2, 3, 4, 5] You can also use the list.sort() method of a list. >>> a = [5, 2, 3, 1, 4] >>> a.sort() >>> a [1, 2, 3, 4, 5] Another difference is that the list.sort() method is only defined for lists. Key Functions Starting with Python 2.4, both list.sort() and sorted() added a key parameter to specify a function to be called on each list element prior to making comparisons. For example, here's a case-insensitive string comparison: The same technique works for objects with named attributes.

Related: