Python language basics 74: validating class level properties

Introduction

In the previous post we discussed class level properties in some detail. The most important detail we learned is that there are only public members in a Python class. Properties can be added to “self” on the fly within the class but those properties will be visible to all external callers. So all you’ve learnt about access modifiers from your Java course, such as private or public is not applicable in Python.

In this post we’ll explore the basics of validating the values that are sent into the object initialiser. Validation is a large topic so consider this only as a light introduction.

Read more of this post

Python language basics 73: class level properties

Introduction

In the previous post we looked at initializers and class level properties. We saw how to add the init method to a class so that we could provide the name property. We also added a second init method so that we could still have an “empty” Person object with no preset name. We also discussed how to attach class level fields to “self” on the fly. The value assigned to those properties was available from another class level method.

In this post we’ll consider class level properties. I felt it was important to provide a post dedicated to that topic because the behaviour of class properties in Python is markedly different from other popular OOP languages such as Java, C#, C++ or VB.NET. At least I was quite surprised when I was first exposed to these class related details in Python.

Read more of this post

Overriding the + and – operators in C# .NET

It’s easy to override the mathemtical operators like + or – in C# to build custom operations.

Consider the following simple Rectangle class:

public class Rectangle
{
	public Rectangle(int width, int height)
	{
		Height = height;
		Width = width;
	}

	public int Width
	{
		get;
		set;
	}

	public int Height
	{
		get;
		set;
	}
}

Read more of this post

Domain Driven Design with Web API extensions part 12: seeding the MongoDb database

Introduction

In the previous post we constructed the database objects that represent the database version of the load testing domain objects. Recall that there’s no automated code generation and mapping tool for MongoDb .NET – at least not yet. Therefore decoupling database and domain objects with MongoDb as the backing store usually means some extra coding but in turn you’ll have completely independent database and domain objects.

We also inserted an abstraction and a concrete implementation for the connection string repository.

In this post we’ll seed the database with the same initial values as we had for the EF data store.

Read more of this post

Checking for arithmetic overflow in C# .NET

As you know primitive numeric types in C# – and other modern programming languages – have maximum and minimum values that can be stored in memory. If you’re trying to store a larger number in the variable then you’ll get an overflow with unexpected results.

Let’s see an example with the “byte” type. It is actually not one of the primitive types, like int or long, but simply a keyword for an integral type to store the numbers 0-255. Why 255? 1 byte consists of 8 bits and 8 bits in the computer’s memory allows us to store 255 as the highest number. 255 in binary is all 1’s for all 8 bits:

11111111

What happens if we add 1 to that? On paper we can easily solve it by some basic binary maths:

11111111
+ 00000001
===========
100000000

Read more of this post

Overriding explicit and implicit conversion in C# .NET

Custom implicit and explicit conversions for numeric types can be defined in C# quite easily. You need to be aware of the “implicit”, “explicit” and “operator” keywords.

Consider the following class:

public class Measurement
{
	public int Value { get; set; }
}

Read more of this post

Domain Driven Design with Web API extensions part 11: the MongoDb database objects

Introduction

In the previous post we mainly discussed the advantages and limitations of coding against MongoDb using .NET. We also discussed the MongoDb context a little bit and started building the MongoDb version of the repository. We said that there’s not much automation available in the .NET MongoDb driver compared to what you get in EF. However, that’s not necessarily a bad thing since you’re not tied to some “secret” and “magic” underlying mechanism that does a lot of work in the background. Instead you’re free to implement the objects, the rules, the conversions etc. as you wish. It usually means more code, but you get absolute freedom for your repository implementation in return.

In this post we’ll first add a new element to the common infrastructure layer. Then we’ll add the MongoDb database representation of our domain objects.

Read more of this post

Python language basics 72: object initializers and class level properties

Introduction

We continued to explore the notion of classes in the previous post. We looked at the difference between constructors and initializers although we haven’t seen the initialisers in action yet. We also built our first class called Person that had one class method called shout. The shout method could be called on an instance of the Person class, i.e. a Person object using the standard dot notation.

In this post we’ll look at the role of initializers and how to supply method arguments to class methods.

Read more of this post

Python language basics 71: continuing with classes

Introduction

In the previous post we started discussing classes. We said that classes are the models for objects. The objects are instantiated based on the classes. A class describes the properties for the objects. E.g. a Car class can have a number of variables, such as colour, make, etc. A class can not only contain simple properties, but methods as well, as we’ll see later. Also, a class controls how an object can be instantiated.

Read more of this post

Projection in LINQ C# with the Select operator

You can use the Select() extension method in LINQ to create an output of type T from an input sequence of type other than T. Let’s see some examples:

Source data:

string[] bands = { "ACDC", "Queen", "Aerosmith", "Iron Maiden", "Megadeth", "Metallica", "Cream", "Oasis", "Abba", "Blur" , "Chic", "Eurythmics", "Genesis", "INXS", "Midnight Oil", "Kent", "Madness", "Manic Street Preachers", "Noir Desir", "The Offspring", "Pink Floyd", "Rammstein", "Red Hot Chili Peppers", "Tears for Fears", "Deep Purple", "KISS"};

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.