background preloader

Python - Notes

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. 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' $ python module_using_sys.py The command line arguments are: module_using_sys.py we are arguments The PYTHONPATH is ['/tmp/py', # many entries here, not shown here '/Library/Python/2.7/site-packages', '/usr/local/lib/python2.7/site-packages'] How It Works 11.1. 11.2. 11.3. 11.4. 11.5.

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.

Squeak by Example 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. Continue with the link below

about What is Lua? Lua is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description. Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode with a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping. Where does Lua come from? Lua is designed, implemented, and maintained by a team at PUC-Rio, the Pontifical Catholic University of Rio de Janeiro in Brazil. What's in a name? "Lua" (pronounced LOO-ah) means "Moon" in Portuguese. Joining the community There are several meeting places for the Lua community where you can go to learn and help others and contribute in other ways. Supporting Lua Why choose Lua?

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. 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. In Simple Numeric Expressions and Output we’ll introduce the print statement (and print() function); we’ll use this to see the results of arithmetic expressions including the numeric data types, operators, conversions, and some built-in functions. Data Structures The Data View Expression Statement. The Other Side of the Coin. Sequences.

Python Programming Language -- Official Website 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? 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. When a potential user or contributor lands on your repository’s page, they see a few things: Project NameProject DescriptionBunch O’ Files Only when they scroll below the fold will the user see your project’s README. Sample Repository License How?

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). 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. Second, the idea that TRE is merely an optimization, which each Python implementation can choose to implement or not, is wrong. Third, I don't believe in recursion as the basis of all programming. Last, let's look at how we could implement tail recursion elimination. def f(x): if x > 0: return f(x-1) return 0 if x > 0: x = x-1 <jump to top>return 0

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

Is there a way to access hardware directly in Python 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. 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. 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). This brings us to my single rule for socially responsible screen scraping: screen scraper traffic should be indistinguishable from human traffic. Cache feverently. Now, armed with those three guidelines, lets get started screen scraping. Setup Libraries First we need to install the httplib2 and BeautifulSoup libraries. sudo easy_install BeautifulSoup sudo easy_install httplib2 Choosing a Scraping Target Now lets get scraping.

Related: