Daily Sublime Text

Sublime Text is a text editor and if you don't know it you can check it out here:
https://www.sublimetext.com/

Doing a quick google search for the word "sublime" reveals the definition

very great excellence or beauty

This is the exact theme of this blog post. I will try to highlight some of the things I like about Sublime Text and share some of my settings and plugins I like.

I am using Sublime Text for many purposes and it all comes down to text editing. However, text editing can be many things depending on the context. I think mostly developers are familiar with Sublime Text, however, I have been using Sublime Text for taking notes, writing markdown, writing LaTeX files for university projects etc. I am also using Sublime Text daily when i am programming. I have been programming in several languages in Sublime Text and if the language syntax is not part of the standard package delivered by Sublime Text, just bring up the menu and install a plugin which does (after installing package control (an integrated tool to download in install plugins)).

Since I am using Sublime Text on a daily basis I have found things which I have tweaked for getting the ultra good feeling of using the text editor. First off, I am using Sublime Text on all my computers, thus, I have the need for making updates to the settings and keybindings files on all the computers. I have done this the same way as I use Linux configuration files. I have already written about this in a previous blog post which can be found here. TLDR: dropbox and symbolic links.

Okay - now for the real deal. For those who are not familiar with the Vintage plugin for Sublime Text i will explain it. A long long time ago a console based text editor called vi was developed. Later on a console based text editor called vim (V IMproved) came along. What's special about this and what is the relation to the Vintage plugin you might ask? The answer is that depending on the mode normal letter keys serves different purposes. First off there is the navigation. The keys h,j,k,l are used for moving the cursor around in the text file when in command mode. h and l are like the left and right arrow keys respectfully and j and k are like the down and up arrow keys. When in command mode you can press the ":" key and type in a command. For example the command :wq(enter) will save and quit the file. (This is actually a running joke on the internet and have even been asked on stack overflow). I feel like I've become more efficient in writing since I enabled the Vintage plugin since I won't have to move my hand around the keyboard and find the correct keys to navigate the text file.

Now I am going to share some extensions I did myself with you guys. Often I use VIM in terminals and sometimes I even have tabs opened in VIM. This can be done with :tabe, :tabn and :tabp (i refer to those as "tab edit", "tab next" and "tab previous"). These command are not part of the standard Vintage plugin, therefore I chose to edit the plugin files myself to add support for those commands. I did this by editing the file Vintage.sublime-commands. I added the following entries:

1
2
3
4
5
6
7
8
9
10
11
12
{
    "caption": ":tabe - tab edit",
    "command": "prompt_open_file"
},
{
    "caption": ":tabn - tab next",
    "command": "next_view"
},
{
    "caption": ":tabp - tab previous",
    "command": "prev_view"
}

The entries just utilized already implemented commands in Sublime Text so these should work out of the box.

To end this post I'm going to share some of the settings I have in my settings file. I have not covered all the things I wanted to in this blog post, however, I am going to write up a part two later on. I have added comments which describes the purpose of the setting.

1
2
3
4
5
6
7
8
9
10
{
    "bold_folder_labels": true, //If you have added folders to the current open project, this will use a bold font for folders.
    "caret_style": "phase", //The animation of the blinking vertical bar when editing text
    "detect_indentation": true, //Sublime Text tries to figure out indentation style based on the open file
    "fade_fold_buttons": false, //When false the triangles which can collapse code blocks are always shown
    "highlight_line": true, //The background of the line the caret is on in is changed to from the regular background color
    "spell_check": true, //Enables spell check by default for all files
    "translate_tabs_to_spaces": false, //Setting which determines whether tabs should be replaced by a number of spaces. (I have this set to true for specific projects)
    "vintage_start_in_command_mode": true //Starts in Vintage command mode (as previously described) when opening files. This is to match the VIM approach)
}

Posted in Everyday life Tagged , , , ,

Look! It's a snake!!

Following my last post, I'm gonna write a bit more on Python. This time I'm gonna show how we can add to the part where we were replacing methods with vars and vars with methods at runtime. The thing is, this can also be expanded to replace methods with other methods at runtime. This means that we can actually swap out a method's implementation at runtime if we need to do so. First of let's create a simple class called Planet

1
2
3
4
5
6
7
8
9
class Planet():
    def __init__(self, planetName):
        self.name = planetName

    def writeInfo(self):
        print("Planet Name: " + self.name)

earth = Planet("Earth") #Create a new planet instance with the name Earth
earth.writeInfo() #Will print out "Planet Name: Earth"

This example should be straightforward we create a class definition with an init method (constructor) and a method to write out the info we know about the planet instance. And a small example with Earth. Now let's say this system is running 24/7, and we suddenly need to have the mass of planets included, and we need to write the information as well. What do we do? Well we can try to replace the methods for the class at runtime! So let's expand the example a bit

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
class Planet():
    def __init__(self, planetName):
        self.name = planetName

    def writeInfo(self):
        print("Planet Name: " + self.name + "\n")

earth = Planet("Earth") #Create a new planet instance with the name Earth
earth.writeInfo()

#Write the new implementation of the init method (constructor) for the Planet class
def newInit(self, planetName, planetMass):
    self.name = planetName
    self.mass = planetMass

#Write the new implementation of the writeInfo method for the Planet class
def newWriteInfo(self):
    print("Planet name: " + self.name)
    #If the planet mass is defined write it, or else tell that we haven't defined it yet for that planet
    try:
        print("    Planet mass: " + self.mass)
    except AttributeError as e:
        print("    Planet mass unknown")
    print("") #Print automatically creates a new line.

#Replace the methods at class level.
Planet.__init__ = newInit
Planet.writeInfo = newWriteInfo

mars = Planet("Mars", "639E21kg")
mars.writeInfo() #Prints out "Planet name: Mars\n    Planet mass: 639E21kg"
earth.writeInfo() #Prints out "Planet name: Earth\n    Planet mass unknown"

What happens in the new example is: first we have the class like before, no changes at all. Afterwards we define two new methods that we will use to replace the original class methods for planet. So from now on each time a Planet instance is created, we have to provide both a name and a mass for the planet. We also changed the implementation of the write info. We already know that all planets have a name defined, so we can just print the planet name. However, as we have introduced the mass later on, we cannot be sure that the mass variable exists for every instance, so we wrap the print in a try, except block. We first try to write out the planet mass and if we get an AttributeError (because the variable is not defined) we move to the except block and writes out that the mass is unknown.

The complete output will look like this:

1
2
3
4
5
6
7
Planet Name: Earth

Planet name: Earth
    Planet mass unknown

Planet name: Mars
    Planet mass: 639E21kg

This is an interesting feature of Python and is quite fun to play around with, however, i think you have to be careful if you are to use this in a production setup. As you can easily introduce errors when you replace methods like this, so at least test it before just throwing in a new implementation. Once again the code examples can be executed using python 3 (Yes I don't like python 2 as much, as I think there are some weird syntax here and there. Such as the print and the except syntax).

- Archi

Posted in Everyday life Tagged , , ,

Python is a learners' language

Recently I've been using python as part of my job. I've become quite familiar with the language if I should say so myself, and I actually like the language a lot. Although it is a bit hard to shift between Python and languages like C# and Java, because I mix up the syntax and sometimes forget that C# needs curly brackets and python needs indentation. However, I usually get used to it after at couple of minutes.

I've heard many people say that Python is a good language for learners, because it is easy to understand and you are forced to have a readable code style (Because indentation that is enforced to form blocks etc.). For me (as a programmer for a couple of years) it was quite easy to learn the language (I think it took me about 30 mins reading a quick tutorial to understand the basic syntax in the language). So in the end I agree that it is a quite nice and easy-to-learn language.

Now to the fun part of this blog post: Funny aspects that I think is making Python a hard language to understand and use. In my experience with Python I've run into a couple of things that I think is a bit strange, or to speak in a more professional manner, quite different from most languages I know.

Let's take an example class written in C#.

1
2
3
4
5
6
7
8
class TestClass
{
    public string Test { get; set; }
    public void Test()
    {
        Console.WriteLine("test function");
    }
}

Here we have a class definition with a property called Test and a method called Test. It should be noted that this is not legal in C#! The problem is that there will be an error on the Test method as the class already contains a definition for Test.

However the following is actually legal in Python (3).

1
2
3
4
5
6
7
8
9
class TestClass():
    test = "test"

    def test(self):
        print("test method")

var1 = TestClass() # Create a new instance of TestClass
var1.test() # Prints "test method"
print(var1.test) # Prints <bound method TestClass.test of <__main__.TestClass object at {hex value}>>

What happens here is that the variable test is actually never used. The output of the call to var1.test() should be logical. The "confusing" part is that the variable test is actually replaced by the method definition (as it is written after the variable declaration) and the print will print out information about the method

Interesting!!! This means that we can replace variables at runtime with methods! But it also means that we can replace methods with vars at runtime - and we can do it from within the method itself! Let me show you how in the following example.

1
2
3
4
5
6
7
8
class TestClass():
    def test(self):
        print("test method")
        self.test = "test"

var1 = TestClass() #Make an instance.
var1.test() #Prints out "test method"
print(var1.test) #Prints "test"

As we can do this, it also means that we can actually break our own code at runtime! If we were to do the following we will end up with a runtime error!

1
2
3
4
5
6
7
8
class TestClass():
    def test(self):
        print("test method")
        self.test = "test"

var1 = TestClass() #Make an instance.
var1.test() #Prints out "test method"
var1.test() #TypeError: 'str' object is not callable

As the test method is replaced by the test variable in the first call to var1.test() we can no longer call the test method, as the string type is not callable (as the TypeError says). I think this is an example of where Python is confusing and gets a bit hard to debug, because I think new programmers will be confused because the syntax is not illegal, there is no warning, and it is at runtime we find out this is actually wrong, and will produce an error. Also the logical flow of what you think happens (in most languages) is not the same and therefore can be hard to debug.

Another funny thing about Python that I haven't seen before is that Python class variables is actually both static and instance variables. So we can actually do the following

1
2
3
4
5
6
7
8
class TestClass2:
    test = "TestStatic"    

t = TestClass2()
print(t.test) #Prints "TestStatic"
t.test = "TestInstance" #Set an instance variable named test
print(TestClass2.test) #Prints "TestStatic"
print(t.test) #Prints "TestInstance"

This can also be a bit confusing for learners, however this makes much sense if you are an experienced programmer, as you know you can replace variable values, however it is still a bit weird that you can use variables as both static and non-static variables, but you have no way of defining variables to be static or not in python, and this is just a language choice.

So in the end I still agree that Python is a really nice and easy-to-learn language. However, the language also have some strange choices but whenever you know them it's not that hard to get used to.

Feel free to try out the examples! All of them can be run in python3.4 ( except for the C# example of cause Smilie: :D ). You can also run them in python2.7 as long as you change the print(something something) to print something something

Happy coding out there!
- Archi

Posted in Everyday life

To C sharp is the new black

This update is going to be about what I've been doing recently! What I've been doing is I finally got around to update my windows phone app (although I haven't released it yet as I've still got some issues to resolve). The thing is that I've been using C# for developing my application, hence the title, and I've finally gotten back to C# after about a years pause. In my choice and embracing of languages I've probably been a bit intoxicated by C# being my first OOP language. However, I've used quite a lot of python recently which is a decent language as well, however the package handling could be improved.

Anyway back to taking about the thing I came here to write about. In the process of updating the windows phone app I've been implementing tests cases for some functionality. (I didn't have much time when i first wrote the app, because of a deadline, so I skipped it then). In retrospect I should have implemented them when I first started the app, as coming back to the app after a year it would have been great to have the tests. But oh well...

Some time ago I bought the book C# in depth by Jon Skeet, and I've got around to read 4 chapters so far. Most of the text so far has been a refreshment of what I already knew, but it's a nice book, and I like his writing style. Anyway in the process of reading the book and updating the app, I've grown quite fond of some of the things available in C#. Such as LINQ! The thing that one can turn this:

1
2
3
4
5
6
7
8
9
foreach (UserObject user in IEnumrableResult)
{
    if (user.ObjectId == toCheckObjectId)
    {
        var tmp = new testObject(user.greeting);
        list.Add(tmp);
    }
}
return list;

into this:

1
2
3
return (from UserObject user in IenumerableResult
        where user.ObjectId == toCheckObejctId
        select new testObject(user.greeting)).ToList();

Another thing that I've grown fond if is the ?? operator of C#. The ?? operator can be used for setting appropriate values depending on if something is null. So lets take an example! One can turn the following:

1
2
3
4
5
6
7
get
{
    if(prop == null)
        return new Prop();
    else
        return prop;
}

into this:

1
2
3
//If prop is not null it will be returned.
//If prop is null we create a new Prop object and returns this.
get { return prop ?? (new Prop()); }

So this was a little insight on what I've learned about the C# language lately.
Maybe I will write more but for now I'm off.

- Archi

Posted in Everyday life Tagged , , , , ,

Linux, Dropbox and a really long .bashrc file

So lately I've been reworking my Linux configuration structure a bit. Since I've installed Ubuntu in a virtual machine on my desktop PC, I put my .bashrc my .xinitrc and .Xsession config files in my dropbox. Such that I could transfer them from my Arch Linux install on my laptop, to my desktop in an easy way. However, I changed my mind of just copying in the configuration files and maybe lose them if my installs get broken. I decided to symlink them instead! (YEEEY SYMLINKS). For those of you who don't know what a symlink is you can imagine it as a shortcut. In an environment you might have a program file on your desktop that you can use to start an application. For example you might have an icon for launching a browser on your desktop. The actual browser application is not on the desktop - it's just something for the user to easily launch the browser. In the same way I've symlinked my configuration files. The Linux system expects them to be in /home/user/.configfile. Therefore I've made a "shortcut" from /home/user/.bashrc to /home/user/Dropbox/Linux\ configuration\ files/.bashrc.

The main reason for this setup was that I would always have a backup of my configuration files such that I would not have to waste a lot of time writing them again. Another thing is that whenever I make a change on one of my installs, it will get updated on my other Linux installs. This is actually quite useful, such that you keep your aliases on every install, such that you don't have to sit and get confused by a missing alias.

A downside is that desktop environments are using different lock commands and Linux distributions might not use the same package manager. Therefore there was a need for me to write up some functions. Underneath I will show a function making available the "update" command in terminal.

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
function update {
    if [ -f "/etc/arch-release" ];
    then
        #Arch Linux case
        printWithColor $Blue "Running Arch update\n"
        sudo pacman -Syu
        printWithColor $Blue "Finishing system update\n\n"
        if [ -f "/usr/bin/packer" ];
        then
            #if packer is installed run the packer AUR update
            sudo packer -Su
            printWithColor $Blue "Finishing AUR update\n"
        fi
        printWithColor $UGreen "Update finished!\n\n"
    elif grep -q "Ubuntu" "/etc/issue";
    then
        #Ubuntu case
        printWithColor $Blue "Running Ubuntu update\n"
        sudo apt-get update
        sudo apt-get upgrade
        printWithColor $UGreen "\nUpdate finished!\n\n"
    else
        #Else tell that the update
        #command could not be found for the OS
        printWithColor $BRed "Could not determine OS\n\n"
    fi
}

Here I check whether the current running terminal is running in an Arch Linux or Ubuntu environment and do the appropriate system update using the package manager. The print with color is doing what you expect. It takes as input a color and a string and then prints the string in that color and afterwards resets the color to white. I think this is quite smart to be honest, that a common update command is found for every install that I have (although I only support Ubuntu and Arch at the moment)

Hope some of you can use this for something. I just wanted to show how easy I find this setup and think it can be useful for others as well.

- Archi

Posted in Everyday life Tagged , , , ,

Python is a bitch

Today I've been at work and have tried to code on a project. The project is using Python with Django. We got 2 apps in Django already which needs to communicate some data. I've been working on one of the Django apps for some time now, however, today was the first time I tried to run the other app. To spill some beans I can tell you I'm not at all fond of Python at the moment. I've pretty much spend much of the day to try to get Python to use a C/C++ compiler, but I've haven't fully succeeded yet, and to add to that the thing that I have to change between 2.7 and 3.4 is just really frustrating.

First off all, I found out that the Python 3.4 install didn't work with the new app, so I had to download and install Python 2.7 and afterwards install pip. The problem arose when I wanted to install all the packages I needed to run the app. First of all the dependencies was listed in a dependencies.pip file and I had to install them all, so I thought that I would try to install them all at once, so i ran:

1
pip install -r ./dependencies.pip

It began installing all of the dependencies until one of them needed the C/C++ compiler. So I used my google skills to look around to find an answer, however, I didn't.. The problem is that Python expects Visual Studio 2008 (VS200Smilie: 8) on windows machines if nothing else is specified. As we are in 2014 I haven't really had the need for VS2008 so I tried to link the vsvarsall.bat to the VS2013 one instead. This didn't work out so I tried installing MinGW instead. This did the trick - at first. The process seemed to get a bit further but still something went wrong when trying to use gcc. So now I'm stuck here, wondering if it is easier to get VS2008 from somewhere (as Microsoft isn't providing it anymore, I have to get it from somewhere else which might be an untrusted source) or just install a Linux distribution in a virtual machine.

I hope that Python in the future will be bundled with a C/C++ compiler or at least the process of getting Python to recognize a C/C++ compiler will be simpler.

Posted in Everyday life

I see a hole!

hack

Hi again.

Long time no post, deal with it! Smilie: :P So actually I got myself in the mood for writing a blog post. Lately I've been asked about it a lot by a friend who's in my group at the university when I will make a new blog post, so here is one for you! Smilie: :) Him asking so much about my blog has really got me thinking, if I should start using my blog for something more useful than me just jabbing about my life, but I haven't really found something that I would like to write about yet. The main reason why I started this blog, was to improve my English writing skills, and going through my old posts I can spot some improvements! I don't know if the blog is the reason or not or the fact that I'm using English so much in my everyday life.

For now I'm just gonna tell you what's going on. I've started at a new semester at the university, this is the year where I'm going to write my master thesis together with two other. It's going to be about security and at the moment I'm reading a lot about security, holes, exploits and so on. It's really interesting and yesterday me and the friend from before went to a seminar about security and a security game called capture the flag. It was really fun to see someone doing all kinds of exploits on stage! A downside of getting all this knowledge is you get to know how bad applications can be written, and how easy they are to exploit. Just take a look at this webpage: http://www.wordpressexploit.com/ already 2 exploits for today. Today I also read that a security issue in a WordPress plugin would allow hackers to arbitrarily delete files. Going through all of this I'm glad I only got a couple of small plugins for my WordPress installation.

That was it for this time, I really hope I'll find some usage of this blog in a more structured way. Until then BAIII!! Smilie: :P

- Archi

Posted in Everyday life Tagged , ,

Relaxing time!

Finally got myself to get to write a new blog post. During the summer I've been working at my job. I've learned to use some django and python. I've used Jquery and HTML, Javascript and CSS. All in one sentence it's been a wild learning experience! Normally I'm not that fond of web-programming but when the job calls for it you got to do it right? So now I've finished working until September and I've got some time to relax, visit my parents, do what I like to do, play dota 2 and things like that. Tomorrow I will go to the movies to watch how to train your dragon 2 - I'm so excited! I also hope to go to the zoo with a friend during my days off but that's not completely planned yet.

I've installed a SSD in my desktop PC. It's really worth it! It's start up time is amazing! Should have done that a long time ago.

I don't have so much more to say right now, so I'm off for now.

- Archi

Posted in Everyday life

Updated web page!

php code

Yesterday and today I've stayed at home. Yesterday I had an extreme head ache and today my stomach have hurt like hell. Although I haven't been to the university, I managed to finally enable the OAUTH for the new twitter API and decode the JSON from the twitter REST service. Now I can finally get the text of my twitter updates again, and now it doesn't take much time to get the data back to the old form to be displayed correctly. I've also managed to improve it a little such that twitter profiles can be looked up directly from my twitter box, if people would like to check out the people I'm writing to.

It took me some time before I had the

1
json_decode()

function of PHP figured out, but it's relatively easy to use together with the

1
var_dump()

function.

I've also begin to climb for real again. It's really nice to finally have started again! It's so fun when hanging at a wall nearly slipping on the small rocks, and just stretch out to reach the next one. When you finally get your hand or foot on the next rock you just feel so awesome when you have been in a difficult position. In my opinion it's just something that all people should try! A bonus is that you get stronger Smilie: :D

Hope you are all feeling alright

- Archi

Posted in Everyday life Tagged , , , , , ,

Registering a windows phone 8

fail

Tonight I've been trying to register my new windows phone for development, however, this is apparently not an easy task. First of all I'm running windows 8.1 and with a Windows Phone 8 device one would think that this should be as smooth as silk but the devices just failed miserably to connect to each other...

After some searching on the Internet and plugging the device in and out numerous times, uninstalling drivers and installing them again, rebooting the PC and phone I finally got somewhere. After trying 4 USB connectors I finally got a connection and the PC was able to access the phone data using the phone sync tool build in with Windows 8. I could watch my photos and hear my music on my PC from my phone, however, when I tried to use the Windows Phone Developer Registration tool I constantly got the message "unable to find phone... " blah blah blah lots of information that I couldn't use and then a retry button. I tried following the guide that is on the Microsoft help page but that did nothing, even something as crazy as setting the date is suggested but nonetheless that didn't result in anything either.

So here we are! Late in the evening, me being pissed about something that is supposed to work the instant you plug in your phone but isn't. To top it all off I've been doing some android development at work, and I was actually surprised by windows 8, because when I plugged in the Android device and 2 seconds after the driver was installed I could use it as a developer device but Microsoft's own systems not working is just crazy!

lots of happy thoughts from me tonight - I know. Sorry about that
Archi

Posted in Everyday life Tagged , , ,