globals in python

Today I am going to have some coding fun as I will call it. The following code was written by me. For the simplicity I just create messages with two characters. A message id character and an optional character

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Message(object):
    def __init__(self):
        self.__data = ""
        self.__valid = False

    @property
    def data(self):
        return self.message_id + self.__data

    @data.setter
    def data(self, input):
        if input[0] == self.message_id:
            self.__data = input[1:]
            self.__valid = True
        else:
            self.__valid = False


    @property 
    def valid(self):
        return self.__valid


class MsgRead(Message):
    def __init__(self):
        super(MsgRead, self).__init__()
        self.message_id = '1'


class MsgWrite(Message):
    def __init__(self):
        super(MsgWrite, self).__init__()
        self.message_id = '2'


class GenerateMessage(object):
    @staticmethod
    def create_write(data):
        msg = MsgWrite()
        msg.data = data
        return msg

    @staticmethod
    def create_read(data):
        msg = MsgRead()
        msg.data = data
        return msg

    @staticmethod
    def gen_message(data):
        msg = GenerateMessage.create_read(data)
        if msg.valid:
            return msg
        msg = GenerateMessage.create_write(data)
        if msg.valid:
            return msg

    @staticmethod
    def generate(data):
        msg = GenerateMessage.gen_message(data)
        return msg

g = GenerateMessage()
a = g.generate("1a")
print type(a)
b = g.generate("2b")
print type(b)

Read More

First usage of elixir


I bought a little book of a new programming language named Elixir. The book is called Programming Elixir. I read the first two or three chapters and I must say that it is indeed different from other languages I know. It has some elements of Erlang, but not all.

Then I tried to install elixir on my mac. I followed the guidelines on the official webpage:

1
2
brew update
brew install elixir

This went fine. I could start the interactive shell with the command iex, like it was in the book.

1
2
3
4
Erlang/OTP 17 [erts-6.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (0.14.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>

I could do for example 3 + 4 and I saw :

1
2
3
iex(1)> 3 + 4
7
iex(2)>

I tried other examples and then I decided to close the shell. I did not read about a command to close the shell, so because I saw Erlang on the shell, I tried the command q. In Erlang it is q(). Therefore I also tried it as a function, but no luck.

Aparently there is no command implemented yet to close the interactive shell correctly. What we should do is the following like it is possible in Erlang too:

1
2
CTRL-G 
press now q

My first try with elixir was already ended with a call to google. Nice I would say. Let the explorer in me begin to explore the language.

Variable keywords in robot framework

Suppose we have a library for the robotframework with some functions that are more or less the same. For example, there are three keywords set_parameter_a_in_module_b_to_value, set_parameter_a_in_module_c_to_value, set_parameter_a_in_module_d_to_value. Suppose now that we must set all these functions to a specific value for example the value 3.

Then in robot framework we define a new keyword Set Parameter A

1
2
3
4
5
*** keywords ***
Set Parameter A
    set_parameter_a_in_module_b_to_value  3
    set_parameter_a_in_module_c_to_value  3
    set_parameter_a_in_module_d_to_value  3

We can now make the value to be set in a variable and input it as a parameter in the keyword

Read More

Several git tips

Retrieve a deleted stash

If you delete the stash per accident, and did not want it to be deleted, is is possible to retrieve if it was done recently and the garbage collector was not active yet.
First list all unreachable commits.

1
2
3
4
5
6
7
8
$ git stash drop 
Dropped refs/stash@{0} (1392dfcd6418f5b7cb009c24416630597add918f)

$ git fsck --unreachable
Checking object directories: 100% (256/256), done.
unreachable blob 2762d40c58c9ab3601529eb9bbf35f873be605c2
unreachable commit 11d01fc547c3438e24c06ae9a18828e5eaa0b02e
unreachable commit 1392dfcd6418f5b7cb009c24416630597add918f

This will show you all the commits which are not reachable by a branch or tag and which haven’t been garbage collected yet. Look for the ones that say unreachable commit, ignore the blobs. The chances are it will be the one closest the top, unless you’ve performed some other actions which created more unreachables.

You can examine the commits by doing ‘git show ‘, and this should identify the stash, it’ll probably be called ‘On master: ‘ or similar. Once you’ve found it, copy the SHA.

To recover this you then do:

1
2
3
4
5
$ git show e9fed4e0950aae7ae2943ad0121d2668bd151dbf

This shows the diff we needed

$ git stash apply e9fed4e0950aae7ae2943ad0121d2668bd151dbf

Which will bring it back into your working copy.


Switching branches

In bash, the command cd - switches to the last directory you were in. You can do the same thing with git to switch to the last branch you were on with git checkout -.


1
2
3
4
5
6
7
8
$ git checkout -b b1
Switched to a new branch 'b1'

$ git checkout -
Switched to branch 'master'

$ git checkout -
Switched to branch 'b1'

first robot framework patch

Today I saw a little issue in the Robot Framework. The Robot Framework is a test automation framework for acceptance testing and acceptance test-driven development. The issue that I saw, was when running the unittests. One of them failed on my windows operating system in some cases.

The test was splitting args from letters with a colon (:) In the unittest, a test with “L:” exists, but on my windows machine that drive letter exists, so this means that the test per accident failed on my machine. So I replaced the drive letter by a number in the test and it succeeds. Here is my patch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
diff --git a/utest/conf/test_settings.py b/utest/conf/test_settings.py
index c10972f..ac440ce 100644
--- a/utest/conf/test_settings.py
+++ b/utest/conf/test_settings.py
@@ -32,7 +32,7 @@ class TestSplitArgsFromNameOrPath(unittest.TestCase):
         assert not os.path.exists('foo'), 'does not work if you have foo folder!' 
         assert_equals(self.method('foo:'), ('foo', [''])) 
         assert_equals(self.method('bar:arg1::arg3'), ('bar', ['arg1', '', 'arg3'])) 
-        assert_equals(self.method('L:'), ('L', [''])) 
+        assert_equals(self.method('3:'), ('3', ['']))

     def test_with_windows_path_without_args(self):
         assert_equals(self.method('C:\\name.py'), ('C:\\name.py', []))
--

I created on github a pull request, and I hope it get’s somehow accepted. Maybe the test was correct and I need to change some real code, but I have to wait now what the developers deciding to do with my patch.

classmethods

What is a classmethod in python? It is like a staticmethod but a classmethod has a reference to a class, like a function in a class has a reference to the instantiation of the class, also named as self. Let’s have a little example to start with.

1
2
3
4
5
6
7
class Person(object):
    name = "" 
    given_name = "" 

    def __init__(self, given_name="", name=""):
        self.given_name = given_name
        self.name = name

Suppose now that we must create a lot of persons, and the information of the names is coming from a string something like “Given_Name - Name”. This means that we must create a parsing function that sets the name and given_name variable in the class. There is another way of doing this. We can also create a function that returns an instance of the class person as we create it, so in other languages it is called another constructor.

Let’s extend our example:

Read More

Upgrade all packages with pip

At some time, after installing some python packages, they becomes outdated, because there are new versions available. To upgrade a package it is very simple:

1
pip install -U <package_name>

But what if we do not know what packages are needed for upgrade, or we have to many packages to upgrade? Sometimes, we want to upgrade all packages at once. Unfortunately pip does not have an upgrade all option. But we can make use of the pip freeze command. This lists all packages with the version number. This means that we must do a little command line trick.

At linux or in a unix environment it is simple:

1
pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs pip install -U

In windows however, we use the following:

1
for /F "delims===" %i in ('pip freeze -l') do pip install -U %i

This is my tip of today.