Tuesday, April 23, 2013

dwm: For lazy people who like to complicate things first

I am a programmer, therefor I am lazy. It is what is it, programmers are lazy but productive people (ironic isn't it ?) In my last blog post, I explained how I got back to Linux after a quick break and how Arch Linux was fit for me. Even though I was happy with my setup, I wanted more.. and I wanted something simpler. Why ? Because I am lazy ! And on my quest for simpleness, I tried other terminals, I tried GNU screen, I tried to add more and more things to my pypanel..

I ended up browsing the Arch Linux Forum and came across some picture of a window manager that looked simple as 1, 2, 3. I made a little research and found out it was dwm (Dynamic Window Manager) by suckless. OH. MY. GOD. It is what I have been looking for. There is one problem though: unlike OpenBox whose config (or what I have been configuring) was in xml, DWM must be compiled each time you make a change.. and it's in C !!! I am pretty conscious that C is probably one of the must know for programmer but I just did an embedded system class and I hated it so I was a bit skeptical. But I still went on, installed it, tried it, changed the .h (booyaa, I only had to change that file), re-build. The result is beautiful. Minimalist yet powerful. Enough talk, here is the result with very few changes to the original config file:


clean desktop


busy desktop

You might say.. "meh", but to me, this is a palace for a work station. Here are the features I exploited:
  • I discovered Alt+p which launches dmenu (another feature of dwm) which lets me run ANY command without opening a terminal.
  • Each tag (more or less desktops) will be used for one and only one thing. And I have named them just so I know. 
    • You can establish rules like "Chromium will only open in the web tag" or "Sublime Text will only open in the dev tag".
    • You can easily switch tag with alt + x where x is the position of the tag (Alt + 2 would display what's in dev)
  • I can launch a terminal with Shit+Alt+Enter, BUT I had to set which one because somehow dwm assumes you are using urxvt.
  • You can switch between the Tiled mode (which is how it looks on the busy screenshot) or Floating mode. Tiled mode means you let the WM place your windows as long as it fills the screen. Floating mode is what most people are used to but this time, to move a window you must hold the Alt key before dragging it.
  • I can switch between windows easily with my keyboard too !


I kept conky from my setup with OpenBox and the auto-changing wallpaper but that is all. I like this one. 
My status bar font is called ohsnap(.icons). It can be found in AUR.

It's simple, neat and I barely use my mouse. I don't mind recompiling the wm each time. I don't mind if it's in C (C is great anyway, I need to make peace with it). It is worth it. After all, a programmer will spend more time working on something that will save him time than the actual time he saves.

Config can be found at : https://github.com/nylogavins/arch-config

Saturday, April 20, 2013

Back to the sources... open sources !

I have been using Windows for quite some time now (I remember using Ubuntu a few years ago), but I always had an interest in Linux and I never was able to justify why. Recently, as my bachelor degree final project, I released an open-source project (perhaps, a blog post about it will come :D) and after making some researches I am now somewhat able to explain why I like free open-source software. In short: I like collaboration, I like sharing and I like having total control. When using almost any Linux distribution, you get all that and more ! Now, I am not skilled enough to contribute to the Linux kernel or to any distribution base system but I like the idea. But giving me total control on my operating system ? Shut up and take my money ! Oh.. it's free ?! Yay !!

In the past, I scrapped a lot of my Linux or BSD installation by doing things I didn't quite understand. With great power comes great responsibilities after all. But today, I am less immature (because I cannot say I am mature yet) and more aware of what I can and should do. So, I decided to install Arch Linux on my workstation !


Why Arch Linux ? Actually, why installing a certain Linux distribution instead of another ? It is a matter of choice. I tried Gentoo in the past and as much as I like the total control and the range of things I can customize, I just didn't like how emerge was pretty long to install or update some packages (i.e.: x11.. oh boy grab yourself a few movies). As mentioned before, I used Ubuntu before but it is "too" user-friendly for me. So much that when I tried the latest version, I just didn't know how to do simple things (because I like to do things, the complicated way). Fedora.. I like it, but I feel it is waaaaay to heavy but maybe that's just me. I also used FreeBSD for a few years but I wanted to see more.


So I ended up with Arch Linux. I tried it before on my eeePC because it decided to stop working. What I liked about it is that it was relatively easy to install, fast and you decide to install what YOU want. After a fresh install, you do not end up with apps you will discover a few months later and realize you never used. Pacman is also really fast and easy to use in my opinion. It is also backed by a very large community (3 official repositories and a users' project repository).
After messing around for hours, I got something I am pretty much proud of:

















So my installation consists of:

  • OpenBox: No Desktop Environment, just a Window Manager. Onyx is my OB theme but I also applied a GTK+ theme for the look and feel: Orta by using lxappearance.
  • SLiM: My login manager, works and looks great.
  • My "desktop" only has 3 things: conky (that line of text at the top of the screen), pypanel (the taskbar at the bottom of the screen) and feh to apply a wallpaper that actually switches every 15 minutes (I wrote a bash script that those that). You decide what Conky and PyPanel are displaying, which is why it got my attention.
  • Very basic "essential" packages: chromium, sublime text 2, xterm, git. Everything else are extras.
    EDIT: I switched to tilda for my terminal. Tilda is basically a drop-down terminal that I make appear by pressing a single button and i can also make it disappear with that same button. And it opens multiple tabs !!
  • python, ruby, Apache, etc. Things I must have to program.
I discovered some pretty nice packages too along the way:
  • cmus: C* Music Player. A text-based music player. It works great ! You can add medias to your library and just play them, create playlists and pretty much do whatever you want to do with a music player.
  • beets: A text-based media manager. It even corrects the tags of your files and it acts as the database of your mp3 files.
  • yaourt: Yet AnOther User RepOsitory. Same as pacman but you can also update AUR packages.
  • ranger: a text-based file manager with vi-like key bindings
  • task warrior (task): A simple text based todo list.
I seem to like text-based programs huh ? Well yeah. They are usually faster and once you get a grip of the basic, pretty much easy to use.
Now, I am learning the shell more in depth to automate more oft-repeated tasks and I hope to find more cool packages in the repositories.


Config can be found at: https://github.com/nylogavins/arch-config

Thursday, November 1, 2012

Build a chat bot with ALMOST no "code"

In this post I will introduce you to PyAIML which is an AIML interpreter coded in pure Python. What I mean about almost no code here is that, starting a bot with PyAIML requires very little code in a python script file. Though, you might write a lot for the dialogues !

What is AIML ?

AIML is an XML dialect used to interact with a bot. The most popular example of a bot who understands that dialect is A.L.I.C.E. (Artificial Linguistic Internet Conputer Entity) developped by Dr. Richard Wallace.

You will need:

  • Python 2.7
  • PyAIML (Install instruction will follow);
  • Good dialogue writing skills :).
Installation

For Windows users, just download the latest version of PyAIML from the official website. Save it on your hard drive and install it.

For Linux users, I only know Arch Linux and I know you can find it in the AUR.


The code

Building a chatbot with PyAIML requires minimally two files:
  • A .py file to start the interpreter and make it learn replies;
  • A .aiml file that describes what the bot can reply to what.
Here is a simple example of a bot who recognizes simple greetings and bye's.

.py file:

.aiml file:
In the python code, I simply instantiate a new Kernel object form the aiml library that represents my bot. I then set up a few predicates or what the bot knows which in this case is its own name (Pedro) and the name of its master (Me !). Then I tell the bot to learn the content of the .aiml file I give him (you can tell your bot to learn more than one file) and I infinitely loop responding to any user input.

As for the AIML file, I will not go through every lines or tags but I will tell you about the very basic of the idea. If you never read/wrote XML files in your life, it is not hard and I recommend W3School's Tutorial

  • An AIML file must begin with the aiml root element. The version property is optional;
  • A category element is every known exchange of words between you and the bot;
  • pattern element is one of the user's possible input. Punctuation is ignored. Always uppercase;
  • A that element refers to what the bot already said and puts a context to the conversation;
  • A template element is what the bot will reply. You can have random responses from a given list;
  • A srai element means it redirects to another pattern element;
  • <bot name="something" /> refers to one of the predicates the bot knows;
  • You can use wildcards such as *. Note: a * has less priority than a precise word;
  • You can refer to what was written as * with the element <star index="n"/>
The only thing that I did not cover here is the topic element and everything that goes with it. That elements puts a subject on the conversation !

Here is what a conversation looks like with Pedro:


Loading basic.aiml... done (0.03 seconds)
> Hello !
Buon Giorno !
>  What's your name ?
My creator called me Pedro. What about you ?
> My name is Nylo !
Nice to meet you Nylo
> Bye !
Talk to you next time then even if I might not remember

Wednesday, November 16, 2011

Unit Tests: Part Deux

Oh well, it's been a while, I know... sorry for that.


Like promised this post here is going to check show how to implement unit tests.


I created a simple project with 2 classes, one of them is implementing an interface. Why interfaces ? Because we are going to use what is called mocking, which will ensure that you will indeed test each units of your code and not many at the same time (which is called an integration test). It is the simplest scenario possible, but understanding it will help you a lot as a programmer.


The Setup
I used Visual Studio 2010 as my IDE. Since I'm using .NET 4, my test framework is NUnit (http://www.nunit.org/) and my mock library is Moq (http://code.google.com/p/moq/). Download these two tools and install them (or extract them somewhere you will find afterward). When you create your test project, link these two libraries to the project (and don't forget to link the project(s) you want to unit test.


The code !!
The project structure is simple:
  • IPingTool: An interface with only one method, void Ping();
  • PingTool: A concrete implementation of the IPingToolInterface
  • Server: A simple class that represents a monitored server.


The definition for IPingTool is simple:



And its concrete implementation is as simple:

Now, how do we test it ? Here is a simple guideline: You can test many "success" at the same time, but only one failure at a time. In our case, it's easy, it either works or not. That gives us two test cases:

Voilà ! You have unit tested a class using NUnit. Now, how about some quick explanations ? 

  • The [TestFixture] attribute tells NUnit that this class is a Test class.
  • Each method with a [Test] attribute is... a test ! 
  • Assertions are used to check your values, A LOT of methods are available for you
  • See that [ExpectedException] attribute, it speaks for itself, because sometimes, you do want to throw an exception

Now.. for the other class. The server class is simple:


Let's say this class is your monitored server and every now and then you send a reset command if your server is unreachable. Now.. the testing phase. If anyone thought that to test the class you have to use a PingTool is heading for the wrong way ! Read the code below and you will get the trick:



Yup, instead of using a real PingTool, we fool the Server object by giving him an object we can absolutely control and he won't notice ! This is what I meant by unit testing, we are now sure that each classes is tested separately. A mock object can be setup with a lambda to manage the behavior of each functions (and methods).


That's it folks ! Have fun unit testing.

Tuesday, June 21, 2011

Unit Tests: How and Why ?

Back in college, we were introduced to unit tests only because our teacher HAD TO and not because he felt it was useful. That's what I felt back then. I remember that he even asked me how could he teach that subject to future students to keep their attention after I told him I wrote batches and batches of tests during my internship.

Unit testing is indeed a tedious task and most programmers, or at least new programmers, don't see the potential in writing tests. Again, back in college, we only wrote them to have a grade, so I guess you have to experience it on the field to understand why it is so important. Though, once you decide to write your test cases, another difficulty shows up: are your tests good enough to prove what you wanted to prove ? In my opinion, experience will answer that.

I will still give my opinion about unit tests and talk about why and how you should write them.

The way I see unit tests is simple: imagine an assemblage of pieces, perhaps a car. Sure the car you buy, new or used, is fully functional, but how can we assert that it is okay and safe to drive this vehicle ? I'm not a mechanic, of course, my first reflex is to be sure that all single parts are in good condition: the engine, the transmission, the carburetor, etc. Like I said, I'm not a mechanic. What I'm trying to say is this: if you aren't sure that every single part of your assemblage works properly
, how can you be sure everything will be fine ? That's what you should have in mind when developing a software. In that situation, the parts are your classes. Now, if you are developing a little helper to ease a daily task or any redundant process at your job or things that falls in the category of helping you, unit tests are not necessary in my opinion. I mostly write them for projects of a certain size.. business or personal.

Here's another example, if I already convinced you, you may skip to the code unless you want to hear my little story. Imagine a guitar, an electric one. Can I really say that my guitar is in good condition if I got fret buzz on specific frets ? If one of my pickup has static noise ? Or if my jack outputs low sound ? No, huh? That's what I thought.

Now a software is the same thing, you want everything to work properly. Mostly because object oriented programming encourages you to reuse code. It also helps you designing your system if you decide to go for test driven development, which is basically writing your test cases before or at the same time as your code (but still before writing your actual code). All this will be clear soon =).

Next post will have code !!