background preloader

Python

Facebook Twitter

An Introduction to Compassionate Screen Scraping. Screen scraping is the art of programatically extracting data from websites. If you think it's useful: it is. If you think it's difficult: it isn't. And if you think it's easy to really piss off administrators with ill-considered scripts, you're damn right. This is a tutorial on not just screen scraping, but socially responsible screen scraping. Its an amalgam of getting the data you want and the Golden Rule, and reading it is going to make the web a better place. We're going to be doing this tutorial in Python, and will use the httplib2 and BeautifulSoup libraries to make things as easy as possible.

We'll look at setting things up in a moment, but first a few words about being kind. Websites crash. For my blog, the error reports I get are all generated by overzealous webcrawlers from search engines (perhaps the most ubiquitous specie of screenscraper). Cache feverently. Now, armed with those three guidelines, lets get started screen scraping. Setup Libraries Choosing a Scraping Target. Code Like a Pythonista: Idiomatic Python. In this interactive tutorial, we'll cover many essential Python idioms and techniques in depth, adding immediately useful tools to your belt.

There are 3 versions of this presentation: ©2006-2008, licensed under a Creative Commons Attribution/Share-Alike (BY-SA) license. My credentials: I am a resident of Montreal,father of two great kids, husband of one special woman,a full-time Python programmer,author of the Docutils project and reStructuredText,an editor of the Python Enhancement Proposals (or PEPs),an organizer of PyCon 2007, and chair of PyCon 2008,a member of the Python Software Foundation,a Director of the Foundation for the past year, and its Secretary. In the tutorial I presented at PyCon 2006 (called Text & Data Processing), I was surprised at the reaction to some techniques I used that I had thought were common knowledge. Many of you will have seen some of these techniques and idioms before. These are the guiding principles of Python, but are open to interpretation. Import this. Tail Recursion Elimination. I recently posted an entry in my Python History blog on the origins of Python's functional features.

A side remark about not supporting tail recursion elimination (TRE) immediately sparked several comments about what a pity it is that Python doesn't do this, including links to recent blog entries by others trying to "prove" that TRE can be added to Python easily. So let me defend my position (which is that I don't want TRE in the language).

If you want a short answer, it's simply unpythonic. Here's the long answer: First, as one commenter remarked, TRE is incompatible with nice stack traces: when a tail recursion is eliminated, there's no stack frame left to use to print a traceback when something goes wrong later. This will confuse users who inadvertently wrote something recursive (the recursion isn't obvious in the stack trace printed), and makes debugging hard. Third, I don't believe in recursion as the basis of all programming. Def f(x): if x > 0: return f(x-1) return 0. Documentation Index. Building Skills in Python — Building Skills in Python. A Programmer’s Introduction to Python Legal Notice This work is licensed under a Creative Commons License. You are free to copy, distribute, display, and perform the work under the following conditions: Attribution.

You must give the original author, Steven F. Lott, credit.Noncommercial. You may not use this work for commercial purposes.No Derivative Works. For any reuse or distribution, you must make clear to others the license terms of this work. Language Basics The Processing View A programming language involves two closely interleaved topics. This part describes the most commonly-used Python statements, sticking with basic numeric data types. Some of the examples in this part refer to the rules of various common casino games. We’ll provide a little background on Python in Background and History. We’ll introduce variables, the assignment statement, and input in Variables, Assignment and Input, allowing us to create simple input-process-output programs.

Data Structures The Data View try. Basic Python Exercises - Google's Python Class - Google Code. ICPCWiki: Problem Resources. About ICPC The ACM International Collegiate Programming Contest (ICPC) is a multitier, team-based, programming competition operating under the auspices of ACM and headquartered at Baylor University. The contest involves a global network of universities hosting regional competitions that advance teams to the ACM-ICPC World Finals.

Participation has grown to several tens of thousands of the finest students and faculty in computing disciplines at almost 2,330 universities from over 91 countries on six continents. The contest fosters creativity, teamwork, and innovation in building new software programs, and enables students to test their ability to perform under pressure. Quite simply, it is the oldest, largest, and most prestigious programming contest in the world. Continue with the link below. Quantitative Reasoning 20 Assignments. There will be weekly assignments listed here. They will usually be assigned on Thursday and due the following Friday. Toward the end of the course, the assignments will be a little more challenging and 2 weeks will be allowed to complete them. Assignment 1, to be completed in section week of Feb. 1, but give it a try before then. Assignment 2, due in your TF's assignment 2 drop box by 5PM, Fri., Feb. 12.

A statement of the homework grading policy Assignment 3, due in your TF's assignment 3 drop box by 5PM, Fri., Feb. 19. Assignment 4, due in your TF's assignment 4 drop box by 5PM, Fri., Feb. 26. Assignment 5, due in your TF's assignment 5 drop box by 5PM, Fri., Mar. 5. Assignment 6, due in your TF's assignment 6 drop box by 5PM, Fri., Apr 2. verbs.py A module to save you from typing in dictionaries of verb roots and inflectional endings.

Assignment 7, due in your TF's assignment 7 drop box by 5PM, Fri., Apr. 9 Assignment 8, due in your TF's assignment 8 drop box by 5PM, Fri., April 16. Python - Notes. You have seen how you can reuse code in your program by defining functions once. What if you wanted to reuse a number of functions in other programs that you write? As you might have guessed, the answer is modules. There are various methods of writing modules, but the simplest way is to create a file with a .py extension that contains functions and variables. Another method is to write the modules in the native language in which the Python interpreter itself was written. For example, you can write modules in the C programming language and when compiled, they can be used from your Python code when using the standard Python interpreter. A module can be imported by another program to make use of its functionality.

Example (save as module_using_sys.py): import sys print('The command line arguments are:')for i in sys.argv: print i print '\n\nThe PYTHONPATH is', sys.path, '\n' How It Works First, we import the sys module using the import statement. 11.1. 11.2. 11.3. 11.4. 11.5. 11.6. 11.7. Python Warmup-1 sleep_in. Structuring Your Project — pythonguide 0.0.1 documentation. By “structure” we mean the decisions you make concerning how your project best meets its objective. We need to consider how to best leverage Python’s features to create clean, effective code. In practical terms, “structure” means making clean code whose logic and dependencies are clear as well as how the files and folders are organized in the filesystem. Which functions should go into which modules? How does data flow through the project? What features and functions can be grouped together and isolated? By answering questions like these you can begin to plan, in a broad sense, what your finished product will look like.

In this section we take a closer look at Python’s module and import systems as they are the central elements to enforcing structure in your project. Structure of the Repository It’s Important. Just as Code Style, API Design, and Automation are essential for a healthy development cycle, Repository structure is a crucial part of your project’s architecture. Sample Repository Bad. Is there a way to access hardware directly in Python.

| Did you know that?