Using Amazon RedShift with the AWS .NET API Part 6: Postgresql to master node using ODBC

Introduction

In the previous post we tested how to connect to the master node in code using the .NET AWS SDK and ODBC. We also executed our first simple Postgresql remotely. In this post we’ll continue in those tracks and execute some more Postgresql statements on our master node.

Preparation

We’ll execute most of the scripts we saw in this blog post. Prepare a text file called postgresscript.txt with the following content and save it somewhere on your harddrive:

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

How to create custom string formatters with C# .NET

.NET has a fairly large number of built-in string formatters that you can pass into the string.Format method. Here are some examples from the MSDN page about formatting:

String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
                                city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
                                (city.Item5 - city.Item3)/ (double)city.Item3);
String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n",
                                    "City", "Year", "Population", "Change (%)");
String.Format("{0,-10:C}", 126347.89m);         

The IFormatProvider and ICustomFormatter interfaces will provide you with the methods required to create your own formats.

Read more of this post

Using Amazon RedShift with the AWS .NET API Part 5: connecting to master node using ODBC

Introduction

In the previous post we went through some basic C# code to communicate with Amazon RedShift. We saw how to get a list of clusters, start a new cluster and terminate one using the .NET AWS SDK.

We haven’t yet seen how to execute Postgresql commands on RedShift remotely from code. That is the main goal of this post.

Installing the ODBC driver

In this section we’ll prepare our Windows environment to be able to connect to RedShift using ODBC. At times this can be a frustrating experience so I’ll try to give you as much detail as I can.

Read more of this post

Python language basics 11: while loops

Introduction

In the previous post in this series we looked at if statements in Python. We saw how to set up conditions and tweak our actions accordingly. An if-block is only carried out if the boolean expression results in true.

However, what if we want to carry out an action as long as some condition is true and stop as soon as the condition is false? Consider the following sentences:

If the weather is nice then we’ll play football.

vs.

While the weather is nice we’ll be playing football.

Read more of this post

Python language basics 10: conditional statements

Introduction

In the previous post we looked at relational operators in Python. We saw how to compare two integers using operators like ‘==’ or ‘<='. In this post we will look at a very important building block of any popular – and probably any not-so-popular – programming language: the if-statement.

The word ‘if’ is also an important element of the English language – and its translated version of any other spoken language. You can create one or more condition for your actions like in the following examples:

If the weather is nice then we will play football else we will go to the movie.
If everything goes well then I will save the world tonight else the world will crumble.

It works exactly the same way in programming. The segment between "if" and "else" is a boolean statement: “the weather is nice”, that is either true or false. In everyday life the result can be indecisive, like “it is cloudy but it is not raining”, but in programming the boolean statement will have a definite result. The weather is either good or bad depending on how you’ve defined “good” and “bad” in your program logic.

The if statement in Python

Not surprisingly the keyword ‘if’ is reserved for conditional statements. It’s important to recall the role of whitespace in Python as the indented rows below the if statement will be executed if the boolean condition is true. Check the syntax, the if statement must be closed with a colon ‘:’ :

val = 5
if val > 4:
    print('The value is greater than 4')
    print('I said that value is greater than 4')
    print('Which part of "The value is greater than 4" do you not understand?')

All three statements will be printed on the standard output window.

Change the boolean condition…

if val < 4:

…and not surprisingly the if-block will be ignored.

OK, what about the ‘else’ section of our example sentences? Easy:

val = 5
if val < 4:
    print('The value is greater than 4')
    print('I said that value is greater than 4')
    print('Which part of "The value is greater than 4" do you not understand?')
else:
    print('The value is smaller than 4')
    print('Bye')

Guess which block is executed? The else block of course with two printouts. The ‘else’ statement is a catch-all statement which is executed if the boolean expression is false. However, we can refine our conditions further. Consider the following sentence:

If the sun is shining then we’ll play football, else if the rain is falling then we’ll stay inside, else if it starts snowing then we’ll start a snowball fight, else we’ll go shopping.

Note that only one of these statements will be carried out depending on the weather conditions. In reality it’s conceivable that the sun is shining and it’s raining at the same time so that the action we carry out might be inconclusive. That’s not that case in programming. The first expression that results in “true” wins regardless of any other outcomes. If all statements give false then the else statement wins. The “else if” bit is expressed by the “elif” keyword in Python:

val = 1
if val < 4:
    print('The value is greater than 4')
    print('I said that value is greater than 4')
    print('Which part of "The value is greater than 4" do you not understand?')
elif val < 3:
    print('Carrying our the first elif block')
elif val < 2:
    print('Executing the second elif block')
else:
    print('The value is smaller than 4')
    print('Bye')

Can you guess which block is executed? The first one as the first boolean condition results in true. The other branches are ignored even if they are also true.

Read all Python-related posts on this blog here.

Compose extremely large integers using BigInteger in C# .NET

The largest integer that can be stored in a long, i.e. a 64-bit integer is 9,223,372,036,854,775,807 in .NET. That is a very large number that will be enough for most applications out there.

However, sometimes it’s not enough and you need to handle something larger. This article on Wikipedia shows several use cases where this can occur. .NET also provides the float and double numeric types that provide much larger ranges. They won’t be integers exactly but they may suit your needs anyhow.

The .NET framework has an object called BigInteger that lets you construct arbitrarily large integers. This object resides in the System.Numerics namespace so you’ll need to reference the System.Numerics.dll library.

Read more of this post

Using Amazon RedShift with the AWS .NET API Part 4: code beginnings

Introduction

In the previous post we looked into how to connect to the Amazon RedShift master node using a tool called WorkBenchJ. We also went through some very basic Postgresql statements and tested an equally basic aggregation script.

In this post we’ll install the .NET SDK and start building some test code.

Note that we’ll be concentrating on showing and explaining the technical code examples related to AWS. We’ll ignore software principles like SOLID and layering so that we can stay focused. It’s your responsibility to organise your code properly. There are numerous posts on this blog that take up topics related to software architecture.

Installing the SDK

Read more of this post

Read the byte array representation of a C# date time

In this post we saw how to extract the byte array representation of primitive types in C#. Here we’ll look at how to do the same with DateTime objects.

DateTimes are not primitive types so the BitConverter.GetBytes method has no overload for it. Therefore we’ll need to go through some more steps. We’ll convert the date into its 64-bit (long) representation. The long can then be supplied to the GetBytes method:

DateTime utcNow = DateTime.UtcNow;
long utcNowAsLong = utcNow.ToBinary();
byte[] utcNowBytes = BitConverter.GetBytes(utcNowAsLong);

utcNowAsLong is a very large integer. It was 5247234542978972986 at time of running the code example. Then we get the bytes using GetBytes which will return a byte of 8 elements as a long is a 64-bit / 8-byte integer.

The reverse operation mirrors what we did above:

long utcNowLongBack = BitConverter.ToInt64(utcNowBytes, 0);
DateTime utcNowBack = DateTime.FromBinary(utcNowLongBack);

“utcNowBack” and the original “utcNow” will be set to the same time.

View all various C# language feature related posts here.

Read the byte array representation of a C# base type

The BitConverter class has a static GetBytes method that has overloads to accepts all the primitive types in C#: int, long, uint, char, bool, ulong, double, float, short, ushort. The method returns the byte array representation if the supplied primitive type.

Here comes an example:

private static void ReadByteArrayFromTypes()
{
	int value = 15;
	byte[] bytesOfValue = BitConverter.GetBytes(value);
}

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.