background preloader

Python

Facebook Twitter

Full Stack Python. Don’t Slurp: How to Read Files in Python. A few weeks ago, a well-intentioned Python programmer asked a straight-forward question to a LinkedIn group for professional Python programmers: What’s the best way to read file in Python?

Don’t Slurp: How to Read Files in Python

Invariably a few programmers jumped in and told our well-intentioned programmer to just read the whole thing into memory: f = open('/path/to/file', 'r+') contents = f.read() Just to mix things up, someone followed-up to demonstrate the exact same technique using ‘with’ (a great improvement as it ensures the file is properly closed in all cases): with open('/path/to/file', 'r+') as f: contents = f.read() # do more stuff Either implementation boils down to the use of a technique we call “slurping“, and it’s by far the most common way you’ll encounter files being read in the wild. It’s quite memory inefficientIt’s slower than processing data as it is read, because it defers any processing done on read data until after all data has been read into memory, rather than processing as data is read.

Full Stack Python. Python and Flask Are Ridiculously Powerful. As a developer, I sometimes forget the power I wield.

Python and Flask Are Ridiculously Powerful

It's easy to forget that, when something doesn't work the way I'd like, I have the power to change it. Yesterday, I was reminded of this fact as I finally got fed up with the way payments are processed for my book. After being unhappy with the three different digital-goods payment processors I've used since the book came out, I took two hours and wrote my own solution using Python and Flask. That's right. Two hours. Read on to find out how I created my own digital goods payment solution in an evening. When I began selling the book, I used a combination of two services (one for credit cards and another for PayPal). Additionally, I've had a terrible time trying to get Google Analytics to properly track visitor flow through the entire visit, including the checkout process.

Lastly, sending out book updates is terribly time-consuming using three different processors. Clearly, it's not that complicated. The Best of Python in 2013. It’s hard to believe that 2013 has already come and about to go.

The Best of Python in 2013

However, the year was not without some fantastic articles, tutorials, tips, and freebies spread throughout the Internet. Today, we will take some time to reflect on the most outstanding development related articles of 2013. So hurry up and review these before the Langoliers get them! Here are a few quick lists of big news, posts, and resources. Hope you enjoy! PEPs 252 and 253: Type and Class Changes. Subsections The largest and most far-reaching changes in Python 2.2 are to Python's model of objects and classes.

PEPs 252 and 253: Type and Class Changes

The changes should be backward compatible, so it's likely that your code will continue to run unchanged, but the changes provide some amazing new capabilities. Before beginning this, the longest and most complicated section of this article, I'll provide an overview of the changes and offer some comments. A long time ago I wrote a Web page ( listing flaws in Python's design. One of the most significant flaws was that it's impossible to subclass Python types implemented in C. Python 2.2 fixes this, and in the process adds some exciting new capabilities. You can subclass built-in types such as lists and even integers, and your subclasses should work in every place that requires the original type. Some users have voiced concern about all these changes. Personally, I think there's no need to worry. 2.1 Old and New Classes So how do you define a new-style class? 2.2 Descriptors.

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.

Code Like a Pythonista: Idiomatic Python

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.

Python packaging. Reflection in Python. Python is an interesting programming language that is flexible and easy to hand on.

Reflection in Python

It is syntax is of a bit freestyle and you can access to the inner system easily from the source code. This article will show you an interesting feature of the modern high-level programming language. It is called reflection. What is Reflection Reflection of a programming language is the ability to examine, modify and maintain its inner structure by the programming language itself at runtime. Python Practice Book — Python Practice Book.