Python language basics 23: using the global __name__ attribute

Introduction

In the previous post we saw one way to break up the number guessing game into short functions. We saw examples of void methods and functions with and without parameters. We also said that there are usually several possible ways to organise the code into functions.

However, we also saw that calling the number guessing game from the command line didn’t execute anything. Python has no way of knowing what you want to do with the code, it only sees a collection of functions.

The solution is easy but we need to learn a new language construct first.

Double-underscore variables

Python has a small number of built-in variables surrounded by double underscores ‘__’. These are populated by Python during code execution, i.e. the Python runtime, and can be accessed in code if you need them.

One such variable is…

Read more of this post

Python language basics 22: organising code into functions

Introduction

In the previous post we looked at functions which are one of the most important building blocks of any serious programming language. We saw how to use the ‘def’ keyword to define a function. We also discussed the difference between void functions – methods – and functions that have a return value.

In this post we’ll organise our previous number guessing game into functions as an exercise.

Note that there’s most often no single solution when you break up your code into functions. The one presented here is only one solution out of many.

Starting point

As a reminder here’s the number guessing game code:

Read more of this post

Basics of working with pipes in C# .NET part 3: message transmission

So far in the posts on pipe streams in .NET we’ve been considering the transmission of single bytes. That’s not really enough for a real-world messaging application. In this post we’ll extend our discussion to complete messages.

We have to explicitly indicate that we want to process messages in the NamedPipeServerStream constructor. Also, we’ll need to read the messages in chunks. A message is represented by a byte array and we don’t know in advance how long the message will be. Fortunately the NamedPipeServerStream object has an IsMessageComplete property which we’ll be able to use in the do-while loop:

Read more of this post

Various topics from software architecture part 2: the Repository pattern

Introduction

In the previous post we looked at the RequestResponse messaging pattern. This pattern is often used in Service Oriented Architecture projects to simplify the communication between the service and its client.

In this post we’ll look at something very different. The Repository pattern is frequently employed in layered architectures where the domains in the Domain layer expose data access related operations through abstract interfaces. The actual repository layer, e.g. EntityFramework or MongoDb then implement those interfaces.

The primary goal of the Repository pattern is to decouple the domain objects from the physical repository layer. The domain objects and the domain layer are not supposed to know about the actual data access operations such as opening a database connection and querying a database. This is a common approach in Domain Driven Design (DDD).

We’ll go through the basic concepts of the pattern in this post. If you’d like to see a more complex design then you can go through the series on DDD. Also, there are multiple ways to implement the pattern but the main objective is to keep the technology-specific data access operations out of the domain layer.

A simple domain

Read more of this post

Basics of working with pipes in C# .NET part 2: continuous byte communication

In this post we briefly introduced how interprocess communication pipes are represented in .NET. The server sent a single byte to the client and the client sent a single byte in response. We’ll add some more flesh to the code but still keep it very simple. We’ll let the client and server send each other individual bytes as messages. Obviously that is still very childish but it will do for demo purposes. We’ll continue with proper messages in the next post.

Here’s the extended server code with a lot more output. We read a line of input from the console but only transmit the very first byte. We wait for the client’s response. The byte value of ‘x’, i.e. 120 will indicate that the client wants to end the communication:

Read more of this post

Basics of working with pipes in C# .NET part 1: send and receive a single byte

Pipes are used for interprocess communication. Typically there’s a single pipe server that one or more clients can connect to and exchange messages.

There are named and anonymous pipes. Anonymous pipes come with a couple of limitations compared to named pipes:

  • They are one-way only i.e. the server and client cannot exchange messages. Communication where both the server and client are allowed to send messages is called duplex
  • Anonymous pipes only allow communication with a single client. Named pipes provide a multi-client scenario
  • Anonymous pipes cannot function over the network. They are limited to the same machine. Named pipes have no such limitation

Read more of this post

Various topics from software architecture part 1: the RequestResponse messaging pattern

Introduction

In this series we’ll look at a couple of topics from the world of software architecture. If you have read other architecture-related series on this blog then some of them will be familiar to you by now.

I’m planning to write an updated series dedicated to Domain Driven Design during/after the summer of 2015. I don’t want to rewrite everything that’s been said in the original series. Therefore I decided to factor some major chunks out into separate posts for easier reference. Most of them appear within the series but are “hidden”, meaning it’s difficult to just refer to them separately.

We’ll start by looking at a simple pattern used in messaging, called RequestResponse.

The RequestResponse

Read more of this post

Python language basics 21: functions in Python

Introduction

In the previous post we took a break from going through the Python language features and looked at how to execute a Python source file from the Windows command prompt. We saw how easy it was to execute a file without having to go through any steps before such as code compilation like in Java with the “javac” tool.

In this post we’ll return to Python language constructs and see how functions are built.

Functions and methods

Functions are reusable code blocks that execute the code within the function body.

Consider the following code:

Read more of this post

Python language basics 20: running a Python file from the command line

Introduction

In the previous post we looked at how for-loops are constructed in Python using the range function. They are in fact practically the same as foreach-loops as we iterate through a range of integers and we perform something with each integer. However, that is the closes approximation of the more traditional integer-based for-loops of other programming languages like Java:

for (int i = 0; i < 10; i++)
{
}

Read more of this post

LIFO collections with Stack of T in .NET C#

LIFO, that is last-in-first-out, collections are represented by the generic Stack of T class in .NET. Stacks are collections where a new element is placed on top of the collection and is removed first when an item is being retrieved. Hence the item that was entered first will get to stay longest in the stack.

Let’s say that you’re throwing a party where you follow a Stack policy as far as guests are concerned. As time goes by you’d like all of them to leave eventually and the first one to go will be the last person who has arrived. This might not be a very fair way to treat your guests but there you go.

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

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