Converting numbers into string representations

I quiet like this one. My first go program snippet without any peaking or googling. I’m proud, though it could be improved with a bit of struct magic and such and such. And it only counts ’till 1000. package main import "fmt" var words = map[int]string{1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten", 11: "eleven", 12: "twelve", 13: "thirteen", 14: "fourteen", 15: "fifteen", 16: "sixteen", 17: "seventeen", 18: "eighteen", 19: "nineteen", 20: "twenty", 30: "thirty", 40: "forty", 50: "fifty", 60: "sixty", 70: "seventy", 80: "eighty", 90: "ninety"} // CountLetters count the letters in a long string number representation func CountLetters(limit int) { myLongNumberString := "" for i := 1; i <= limit; i++ { addLettersToMyString(&myLongNumberString, i) } // fmt.Println("1-9 written with letters is: ", len(myLongNumberString)) fmt.Println("The string is:", myLongNumberString) fmt.Println("Lenght of string is:", len(myLongNumberString)) } func addLettersToMyString(myString *string, num int) { if num < 20 { *myString += words[num] } if num >= 20 && num < 100 { *myString += countMiddle(num) } if num >= 100 && num < 1000 { hundred, tenth := countHundred(num) if tenth == { *myString += hundred } else if tenth >= 11 && tenth < 20 { *myString += hundred + "and" + words[tenth] } else { *myString += hundred + "and" + countMiddle(tenth) } } if num == 1000 { *myString += "onethousand" } } func countMiddle(num int) string { minues := num % 10 num -= minues return words[num] + words[minues] } func countHundred(num int) (string, int) { minues := num % 100 num -= minues return (words[(num/100)] + "hundred"), minues }

July 19, 2015 · 2 min · hannibal

Selenium Testing with Packer and Vagrant

So, recently, the tester team talked to me, that their build takes too long, and why is that? A quick look at their configuration and build scripts showed me, that they are actually using a vagrant box, which never gets destroyed or re-started at least. To remedy this problem, I came up with the following solution. ...

July 16, 2015 · 7 min · hannibal

Bitwise & Operator

The first, and only time so far, that I got to use the bitwise & operator. I enjoyed doing so!! And of course from now on, I’ll be looking for more opportunities to (ab)use it. package secret import "sort" const REVERSE = 16 func Handshake(code int) []string { // binary_rep := convertDecimalToBinary(code) if code < { return nil } secret_map := map[int]string { 1: "wink", 2: "double blink", 4: "close your eyes", 8: "jump", } var keys []int for k := range secret_map { keys = append(keys, k) } // To make sure iteration is always in the same order. sort.Ints(keys) code_array := make([]string, ) for _, key := range keys { if code & key == key { code_array = append(code_array, secret_map[key]) } } if code & REVERSE == REVERSE { code_array = reverse_array(code_array) } return code_array } func reverse_array (array_to_reverse []string) []string { for i, j := , len(array_to_reverse) -1 ; i < j; i, j = i + 1, j - 1 { array_to_reverse[i], array_to_reverse[j] = array_to_reverse[j], array_to_reverse[i] } return array_to_reverse }

July 15, 2015 · 1 min · hannibal

Packer 0.8.1.

Previously I wrote that the scripts I’m writing, are failing because Packer hangs. Apparently, this was a known issue. And apparently, I was using an older version, 0.7.5. After I updated everything is working wonderfully!!! And for my thanks, here is an updated PowerShell script for provisioning my dotnet stuff. $source = "http://download.microsoft.com/download/1/6/7/167F0D79-9317-48AE-AEDB-17120579F8E2/NDP451-KB2858728-x86-x64-AllOS-ENU.exe" $destination = "C:\Windows\Temp\dotnet.exe" Write-Host 'Starting to download dotnet file.' try { (New-Object System.Net.WebClient).DownloadFile($source, $destination) } catch [Exception] { Write-Host "Exception during download. Probable cause could be that the directory or the file didn't exist." Write-Host '$_.Exception is' $_.Exception } Write-Host 'Download done. Checking if file exists.' if (!(Test-Path $destination)) { Write-Host 'Downloading dotnet Failed!' } else { Write-Host 'Download successful.' } Write-Host 'Starting install process.' try { Start-Process -FilePath $source -ArgumentList "/q /norestart" -Wait -PassThru } catch [Exception] { Write-Host 'Exception during install process.' Write-Host '$_.Exception is' $_.Exception } Write-Host 'All done. Goodbye.' Thanks for reading! ...

July 1, 2015 · 1 min · hannibal

Powershell can also be nice -Or Installing Java silently and waiting

Hello folks. Today, I would like to show you a small script. It installs Java JDK, both version, x86 and 64 bit, silently, and wait for that process to finish. The wait is necessary because /s on a java install has the nasty habit of running in the background. If you are using a .bat file, you shouldn’t, than you would use something like: start /w jdk-setup.exe /s. This gets it done, but is ugly. Also, if you are using Packer and PowerShell provisioning, you might want to set up some environment variables as well for the next script. And you want that property to be available and you don’t want to mess it up with setting a path into a file and then re-setting your path on the begin of your other script. Or pass it around with Packer. No. Use a proper PowerShell script. Learn it. It’s not that hard. Be a professional. Don’t hack something together for the next person to suffer at. ...

June 30, 2015 · 3 min · hannibal

The Packer, The Windows, and the Vagrant box

Hello folks. Today, I would like to write about something close to my heart recently. I’ve been fiddling with Packer, Windows and Vagrant these days. Trying to get a Windows box up in running is a pain in the arse though, so I thought I share my pain with you nice folks out there. Let’s begin. ...

June 27, 2015 · 9 min · hannibal

Docker + Java + Vagrant+ GO.CD

Hello folks. Today, I would like to write about something interesting and close to me at the moment. I’m going to setup Go.cd with Docker, and I’m going to get a Ruby Lotus app running. Let’s get started. ...

June 6, 2015 · 6 min · hannibal

Setting up a new Laptop with Puppet

Hello folks. So, some of you know puppet, some of you don’t. Puppet is a configuration management system. It’s quite awesome. I like working with it. One of the benefits of puppet is, that I never, ever, EVER have to setup a new laptop from scratch, EVER again. I’m writing a puppet manifest file which sets up my new laptop to my liking. I will improve it as I go along. Here is version 1.0. ...

May 21, 2015 · 2 min · hannibal

Busy building the future

Fact is, I’ve been busy. I’ve got a new job as a build engineer. As sort of a devops kind of guy. It’s extremely interesting considering that I made a career as a tester. Granted, I always was technical, and never really knew my path; but it seems my path is finding me after all. In the past years, I got better at Docker, Puppet, Chef, AWS, Packer, Vagrant, Gradle, and a hell of a lot more. Also honed my linux skills from the ability of doing an ls -l to do an find . -type f -atime +5 | xargs rm -fr (find all the files which are 5 days older and pipe them to a delete command). I already read many books about devops but this time, it’s different. This time, I can actually do these things as well in a live environment. ...

May 19, 2015 · 2 min · hannibal

Django – RPG – Part 3

Hello folks. A small update to this. I created the model now, which is the database design for this app. It’s very simple, nothing fancy. Also, I’m writing the app with Python 3 from now on. Here is the model now: from django.db import models from django.contrib.auth.models import User # Create your models here. class Item(models.Model): name = models.CharField(max_length=100, default="Item") damage = models.IntegerField(default=) defense = models.IntegerField(default=) consumable = models.BooleanField(default=False) def __str__(self): return self.name class Inventory(models.Model): items = models.ManyToManyField(Item) def __str__(self): return self.items class Character(models.Model): # By default Django uses the primery key of the related object. # Hence, no need to specify User.id. user = models.OneToOneField(User, null=True) name = models.CharField(max_length=100) inventory = models.ForeignKey(Inventory) def __str__(self): return self.name Worth noting a few things here. The __str__ is only with Python 3. In Python 2 it would be unicode. And the OneToOne and the foreign key are automatically using Primary keys defined in the references model. The __str__ is there to return some view when you are debugging in the console instead of [<Item: Item object>]. ...

April 21, 2015 · 2 min · hannibal