background preloader

Python

Facebook Twitter

Top 30 Python Projects In GitHub | Idiot Inside. There are several repositories for Python language in GitHub and we are providing you with a list of top 30 among them. 1. Django a high-level Python Web framework that encourages rapid development and clean, pragmatic design 2. HTTP for Humans – HTTP library, written in Python, for human beings 3. A command line HTTP client, a user-friendly cURL replacement. 4. flask a microframework for Python based on Werkzeug, Jinja 2 and good intentions. 5.

A Python web framework and asynchronous networking library, originally developed at FriendFeed. 6. This is a very simple IT orchestration engine with which you can easily deploy your applications and systems. 7. A guidebook on Python best practices, written for Humans. 8. Sentry is a realtime, platform-agnostic error logging and aggregation platform 9. Scrapy is a web crawling framework for Python which is also a fast high-level screen scraping. 11. Small command-line program to download videos from YouTube.com and other video sites 12. 13. 14. 15. 19.

Django. Neopythonic. Python: Lambda Functions. Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called "lambda". This is not exactly the same as lambda in functional programming languages, but it is a very powerful concept that's well integrated into Python and is often used in conjunction with typical functional concepts like filter(), map() and reduce().

This piece of code shows the difference between a normal function definition ("f") and a lambda function ("g"): As you can see, f() and g() do exactly the same and can be used in the same ways. Note that the lambda definition does not include a "return" statement -- it always contains an expression which is returned. The following code fragments demonstrate the use of lambda functions. The above code defines a function "make_inrementor" that creates an anonymous function on the fly and returns it. The following takes this a step further. In the second example, map() is used to convert our list. 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.

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. They are not recognized by the Python bytecode compiler and are not accessible as runtime object attributes (i.e. not assigned to __doc__), but two types of extra docstrings may be extracted by software tools: Please see PEP 258, "Docutils Design Specification" , for a detailed description of attribute and additional docstrings.

XXX Mention docstrings of 2.2 properties. Multi-line Docstrings. Using Python's list index() method on a list of tuples or object. 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. Finally, the desired sorted-list is extracted/reconstructed by "undecorating" the now-sorted auxiliary-list.

Steps 1 and 3 can be performed in several ways, with map, zip, list comprehensions, and explicit loops, all possible. 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. 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. Use pyline to apply a Python expression to every line of standard input, and return a value to be sent to standard output. 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]" Hopefully you get the idea. 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. That report you typed up this morning was, obviously, stored in a file as well.

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: Only the second line is displayed. 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).

For larger structures, you may need a specialized data structure, such as collections.deque. 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) try: i = L.index(value) except ValueError: i = -1. [Tutor] Making a better main() : Critique request. 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). Python is also suitable as an extension language for customizable applications.

The Glossary is also worth going through. 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. It offers a very simple API for traversing the resulting parse tree, and one of its main features is that it also groks non-conformant HTML code (as good as possible) without choking.Whenever you want to parse anything more complex than what regular expressions support (i.e., sth. beyond regular languages), e.g. expressions that need to be properly quoted or with nested parentheses, I propose to use pyparsing, another real gem which allows you to write parsers in the most intuitive way possible.

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. Each is the de facto standard unit testing framework for its respective language. 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 Test suites are implemented by the TestSuite class. 25.3.1. 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.

This article refers to CPython V.2.5, it's not about Python 2.6 yet, or about Jthon, or PyPy or IronPython, that may have subtle differences), but it's a generally clean language still, so with a page like this you can avoid the most common ones. This page hopes to be really simple and short enough, you can find explanations online elsewhere. 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. 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. This will ensure that concatenation occurs in linear time across various implementations.Comparisons to singletons like None should always be done with is or is not, never the equality operators.Also, beware of writing if x when you really mean if x is not None -- e.g. when testing whether a variable or argument that defaults to None was set to some other value. The other value might have a type (such as a container) that could be false in a boolean context! Python Basics. 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. MySQL v. 3.23 or newer 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) 1, apple.

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. Python Exception Handling Techniques - Doug Hellmann.