background preloader

Dive Into Python

Dive Into Python

Python re Module - Use Regular Expressions with Python - Regex Support Python is a high level open source scripting language. Python's built-in "re" module provides excellent support for regular expressions, with a modern and complete regex flavor. The only significant features missing from Python's regex syntax are atomic grouping, possessive quantifiers, and Unicode properties. The first thing to do is to import the regexp module into your script with import re. Regex Search and Match Call re.search(regex, subject) to apply a regex pattern to a subject string. You can set regex matching modes by specifying a special constant as a third parameter to re.search(). re.I or re.IGNORECASE applies the pattern case insensitively. re.S or re.DOTALL makes the dot match newlines. re.M or re.MULTILINE makes the caret and dollar match after and before line breaks in the subject string. By default, Python's regex engine only considers the letters A through Z, the digits 0 through 9, and the underscore as "word characters". Do not confuse re.search() with re.match().

Planet Python Python Programming - Free computer books Python is an object-oriented high-level programming language created by Guido van Rossum in 1990. Python has a fully dynamic type system and uses automatic memory management; it is thus similar to Perl, Ruby, Scheme, Smalltalk, and Tcl. The philosophy behind Python is noteworthy among high-level programming languages because it emphasizes the importance of programmer effort over computer effort, and because it rejects more arcane language features, prioritizing readability over speed or expressiveness. Python is often characterised as minimalist, although this only applies to the core language's syntax and semantics; the standard library provides the language with a large number of additional libraries and extensions. Miscellaneous parts of the language have formal specifications and standards, but not the language as a whole.

labs :: Python beginner's mistakes Every Python programmer had to learn the language at one time, and started out as a beginner. Beginners make mistakes. This article highlights a few common mistakes, including some I made myself. Beginner's mistakes are not Python's fault, nor the beginner's. They're merely a result of misunderstanding the language. To put it another way, the mistakes in this article are often cases of "the wrong tool for the job", rather than coding errors or sneaky language traps. Mistake 1: trying to do low-level operations Python is sometimes described as a VHLL, a Very High-Level Language. This doesn't mean that it isn't possible to do these things with Python; but it's probably just not the right language for these jobs. Mistake 2: writing "language X" code in Python This is a mistake that is almost unavoidable. Some notorious symptoms of "language X" code, and the languages that may cause them: The point here is not to slam the language that you're used to (although that is always fun ;-).

Python Hitchhiker's Guide to Python « late.am I first heard about The Hitchhiker’s Guide to Python at PyCodeConf a few months ago. It’s a fantastic idea: open source, community-driven documentation on how to do Python right: everything from how to learn Python, to how to write idiomatic code, to how to distribute your projects, to surveys of best-of-breed open source projects and libraries you can build projects and applications on top of. Many many thanks to Kenneth Reitz for creating and maintaining the project, which is hosted at GitHub. At this time, the Hitchhiker’s guide is a little rough around the edges: many sections are only outlined, and need content written; other sections may not even exist yet. This sort of undertaking is effectively impossible for one person to maintain—one person can’t possibly know of every project, library, and idiom. Finally, since this is a community effort, I want to give a shout-out to all those who’ve contributed to the Guide so far:

Text Processing in Python (a book) A couple of you make donations each month (out of about a thousand of you reading the text each week). Tragedy of the commons and all that... but if some more of you would donate a few bucks, that would be great support of the author. In a community spirit (and with permission of my publisher), I am making my book available to the Python community. Minor corrections can be made to later printings, and at the least errata noted on this website. A few caveats: (1) This stuff is copyrighted by AW (except the code samples which are released to the public domain).

The Hitchhikers Guide to Python! — pythonguide 0.0.1 documentation Greetings, Earthling! Welcome to The Hitchhiker’s Guide to Python. This is a living, breathing guide. If you’d like to contribute, fork us on GitHub! This handcrafted guide exists to provide both novice and expert Python developers a best practice handbook to the installation, configuration, and usage of Python on a daily basis. This guide is opinionated in a way that is almost, but not quite, entirely unlike Python’s official documentation. Note The use of Python 3 is highly preferred over Python 2. Let’s get started! Getting Started with Python New to Python? Properly Install Python on your system: Using Virtualenvs with Pipenv: Python Development Environments This part of the guide focus on the Python development environment, and the best-practice tools that are available for writing Python code. Writing Great Python Code This part of the guide focuses on the best-practices for writing Python code. Scenario Guide for Python Applications Shipping Great Python Code Additional Notes

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.

Lesson 10 - File I/O Introduction Last lesson we learnt how to load external code into our program. Without any introduction (like what I usually have), let's delve into file input and output with normal text files, and later the saving and restoring of instances of classes. (Wow, our lingo power has improved greatly!) Opening a file To open a text file you use, well, the open() function. Code Example 1 - Opening a file openfile = open('pathtofile', 'r') openfile.read() That was interesting. Seek and You Shall Find Did you try typing in print openfile.read()? whence is optional, and determines where to seek from. offset decribes how far from whence that the cursor moves. for example: openfile.seek(45,0) would move the cursor to 45 bytes/letters after the beginning of the file.openfile.seek(10,1) would move the cursor to 10 bytes/letters after the current cursor position.openfile.seek(-77,2) would move the cursor to 77 bytes/letters before the end of the file (notice the - before the 77) Try it out now. Nifty, eh?

Related: