Python language basics 87: reading from a text file

Introduction

In the previous post we looked at context managers in with-blocks in Python. We said that context managers could be used with objects that require some type of opening and closing. We can open and close – or release – files. We can also open a HTTP channel to make a web request and then close it when we’re done. With-blocks makes our code more concise in that we don’t need to call the close() function anymore in code. Python will take care of that for us. In addition our code will be more robust since it can easily happen that the programmer forgets to call the close method on all open resources.

In this post we’ll look into reading text files. We’ll also see an example of multiple context managers.

Read more of this post

Python language basics 86: file I/O resource management in a context manager

Introduction

In the previous post we went through a possible improvement of our first unreliable file I/O code using try-except-finally. The code is fine, it does what it is supposed to do. However, we’re still required to actively call the close function on the file handle. We’re dealing with resource management ourselves whereas we can ask Python to take care of it for us. If we forget to include the close method in our code then the file handle will stay open leading to all sorts of problems.

This is where context managers enter the scene. It’s a scary name that might at first imply a lot of complex code. We’ll in fact see that it makes our code shorter and more readable.

Read more of this post

Python language basics 85: file I/O resource management with try-finally

Introduction

In the previous post we started looking into file operations in Python. We discussed various basic terms such as the file access mode and character encoding. We also went through some very basic code to write to a text file. We saw that the code wasn’t well designed. It is error-prone and not very reliable.

In this post we’ll present a way to make the code better with a traditional try-except-finally block.

Read more of this post

Python language basics 84: starting with file I/O

Introduction

In the previous post we finished discussing the basics of object inheritance in Python. We saw an example where the Dog and a Duck classes inherited from an abstract base class called Animal. All common functionality was encapsulated within the Animal class. Dog and Duck retained the code specific to them.

In this post we’ll start looking into something very different: file input and output.

Read more of this post

Python language basics 83: polymorphism through inheritance III

Introduction

In the previous post we started working on a scenario where class inheritance can be advantageous. We built two classes, Dog and Duck, and we saw that they had some similarities and also some unique features.

In this post we’ll see how to apply inheritance in code.

Read more of this post

Python language basics 82: polymorphism through inheritance II

Introduction

In the previous post we looked at some theory behind inheritance. First we looked at the general meaning of the word ‘inheritance’ and then discussed its role in programming. We went through the basic terms such as superclasses, also knows as abstract classes or base classes, and derived classes.

In this post we’ll start building a simple example to make all of that clearer. If you’re have a background in strongly typed languages, like Java, then you’ll see that inheritance is implemented slightly differently in Python. However, the end result is much the same.

Read more of this post

Python language basics 81: polymorphism through inheritance

Introduction

In the previous post we looked at interface polymorphism in Python. In fact there are no explicit interfaces in Python like in Java so I simply made up the term “interface polymorphism” referring to strictly typed languages.

We saw an example of two methods: one that printed the provided data in XML format, and another which performed the same in JSON format. The print_as_xml and print_as_json functions, which are also objects, implicitly implemented an interface with one method. This method is void, i.e. has no return type, and accepts two input parameters: a root name and the properties to be printed arranged in a dictionary. The Person object then had a format_me method which accepted a formatter and delegated the actual formatting implementation details to the mechanism that was passed into it. It’s important to keep in mind that the Person object remained oblivious of these implementation details and didn’t need to concern itself with the rules concerning JSON, XML, etc. Also if we pass in a function that doesn’t adhere to the implicit interface then an exception is thrown.

In this post we’ll start looking into another key idea in object-oriented design: inheritance. We’ll start with some theory and key terms. The upcoming posts will show the ideas in action.

Read more of this post

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Ricos Blog zu Softwaredesign- und architektur

Ideen und Gedanken rund um Softwaredesign und -architektur, Domain-Driven Design, C# und Windows Azure

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: