28. May 2014

Book review: Visual Studio 2013 Cookbook

Visual Studio 2013 is a huge technological leap forward. The book Visual Studio 2013 Cookbook helps to get on track with latest features including enhanced support for JavaScript and C++.visual-studio-2013-cookbook

Software projects often combine many different technologies like C++, .NET or JavaScript. Professional developer must be aware of all these technologies. The book is a great source of essential information which helps developer to grasp important concepts implemented in Visual Studio.

Cookbook provides short and easy to understand chapters about wide range of features. Each chapter contains some links to web sites or related technologies.

I recommend to read the book one chapter a day. Learn new stuff, experiment with it and next day continue with next chapter.

What you can find inside the book? Here is selection of some interesting topics

Chapter 1: Discovering Visual Studio 2013

  • contains productive tips how to use Visual Studio in effective way (keyboard shortcuts, navigation in project)

Chapter 2: Getting Started with Windows Store Application

  • good starting point for building own application for Windows Store
  • introduction to analysiss of application performance

Chapter 3: Web development

  • learn how to work with Bootstrap
  • minification of JavaScript and CSS

Chapter 4: .NET 4.5.1 Development

  • unit testing of .Net apps
  • managing packages with NuGet

Chapter 5: Debugging .NET Application

  • debugging with IntelliTrace
  • debugging parallel code

Chapter 6: Asynchrony in .NET

  • introduction to popular way of programming applications
  • integration of async concepts with web applications

Chapter 7: Unwrapping C++ Development

  • C++ is still very popular and Visual Studio 2013 introduces several concepts known in .NET or JavaScript
  • Native Unit Test Project for C++
  • graphics programming – 3D, DGSL and diagnostics

Chapter 8: Working with Team Foundation Server

  • getting feedback from users
  • git integration

Chapter 9: Languages

  • highly improved integration with further languages like TypeScript or Python
  • Python support is simply awesome

Visual Studio 2013 Cookbook is available at Packt Publishing store.

22. March 2014

Kivy – buildozer android debug failed with libstdc++.so.6: cannot open shared object file

Kivy is awesome library for developing GUI applications in Python.

It’s possible to build same application for desktop, Android or iOS. It’s something like Cordova/PhoneGap for JavaScript.

I was following Kivy crash course 2: Building an android apk tutorial recorded by Alexander Taylor.

I was trying to build application on Linux Debian for Android:

buildozer android debug

Build failed with quite strange message:

[mergemanifest] Manifest merger disabled. Using project manifest only.
     [echo] Handling aidl files...
     [aidl] Found 1 AIDL files.
     [aidl] Compiling 1 AIDL files.
     [aidl] /home/georgik/.buildozer/android/platform/android-sdk-21/platform-tools/aidl: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory

BUILD FAILED

assets/private.mp3: /home/georgik/idea/kivytest/.buildozer/android/app/sitecustomize.pyo
Traceback (most recent call last):
  File "build.py", line 431, in 
    make_package(args)
  File "build.py", line 346, in make_package
    subprocess.check_call([ANT, arg])
  File "/usr/lib/python2.7/subprocess.py", line 540, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['ant', 'debug']' returned non-zero exit status 1

The reason of this problem is not so obvious.

My operating system is 64bit, but Android build system requires 32 bit version of stdc++ library.

Fix is very easy. Just install lib32stdc++6 package 🙂

apt-get install lib32stdc++6

If build is still failing, because of other missing libraries, then you can use online Debian package search to find missing dependencies: https://www.debian.org/distrib/packages

Other missing 32bit libraries are often libz, libncurses5:

apt-get install lib32z1 lib32ncurses5

29. June 2013

True/False gotcha Python vs Ruby vs PowerShell vs NodeJS vs PHP

Evaluation of conditions is very important part programming. True is ture, false is false…

Or not? It might surprise you, but not all languages evaluate numbers, empty strings or empty arrays in the same way.

Let’s examine several technologies. Python, Ruby, PowerShell, NodeJS and PHP.

Here is small code written in Python:

def test(value):
    if value:
        print "True"
    else:
        print "False"

test(0)
test(1)
test("")
test([])
test({})

Here is similar code written in Ruby:

def test(value)
    if value
        print "True"
    else
        print "False"
    end
end

test(0)
test(1)
test("")
test([])
test({})

Let’s compare it to PowerShell:

Function Test($value) {
    if ($value) {
        Write-Host "True"
    } else {
        Write-Host "False"
    }
}

Test(0)
Test(1)
Test("")
Test(@())
Test(@{})

Same logic in JavaScript for NodeJS:

function test(value) {
    if (value) {
        console.log("True");
    } else {
        console.log("False");
    }
}

test(0);
test(1);
test("");
test([]);
test({});

Code in PHP:

<?php 
function test($value) {
    if ($value) {
        echo "True";
    } else {
        echo "False";
    }
}

test(0);
test(1);
test("");
test(array());
test(array());
?>

Results are little bit surprising:

 

Python Ruby PowerShell NodeJS PHP
0 False True False  False False
1 True True True True True
“” False True False False False
[] False True True  True False
{} False True True  True False

You can test code on-line in Cloud9 IDE except PowerShell example 😉

2. June 2013

Configure Nginx with uWSGI to serve Pootle

It’s quite easy to set up Pootle. Just follow the manual – Quickstart.

virtualenv /var/www/pootle/env/
 source /var/www/pootle/env/bin/activate
 (env) $ pip install pootle
 (env) $ pootle init
 (env) $ pootle syncdb --noinput
 (env) $ pootle initdb

You can start Pootle with default CherryPy server. In order to boost performance it is necessary to use more powerfull server.

Let’s take intermediate step and serve Pootle by uWSGI.

First of all you’ll need to create wsgi.py file, because this file is missing in default Pootle installation. File: /var/www/pootle/wsgi.py

import os

# this part is only required when running from checkout instead of an install
try:
    import sys
    ROOT_DIR = os.path.abspath(os.path.dirname(__file__))
    sys.path.insert(0, ROOT_DIR) # Top level directory
    import syspath_override
except ImportError:
    # not running from checkout
    pass

# comment the above lines if running from install

os.environ['DJANGO_SETTINGS_MODULE'] = 'pootle.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

You can start uWSGI server by following command:

uwsgi --socket 127.0.0.1:3031 --chdir /var/www/pootle/lib/python2.7/site-packages/pootle --wsgi-file wsgi.py --master --virtualenv /var/www/pootle/ --http ":9090"

This will create uWSGI server and you can test it by connecting to http://localhost:9090

Let’s take further step and configure Nginx to server Pootle. We can modify previous command, because we do not need to server it as http at port 9090:

uwsgi --socket 127.0.0.1:3031 --chdir /var/www/pootle/lib/python2.7/site-packages/pootle --wsgi-file wsgi.py --master --virtualenv /var/www/pootle/

Create webroot directory where you will put static assets, just to avoid information leackage. Store there symlinks to assets and static files.

mkdir /var/www/pootle/webroot
ln -s /var/www/pootle/lib/python2.7/site-packages/pootle/assets /var/www/pootle/webroot/
ln -s /var/www/pootle/lib/python2.7/site-packages/pootle/static /var/www/pootle/webroot/

Store configuration to /etc/nginx/sites-available and create symlink to /etc/nginx/sites-enabled. Configuration of site:

upstream emperor {
  server 127.0.0.1:3031;
}

server {
  listen 80;
  server_name pootle.sinusgear.com;
  root /var/www/pootle/webroot;

  access_log /var/log/nginx/pootle.sinusgear.com_access.log;
  error_log /var/log/nginx/pootle.sinusgear.com_error.log;

  location / {
    location ~* ^.+\.$ {
      root /var/www/pootle/webroot/static/styles;
      expires 30d;
    }
    try_files $uri @django;
  }

  location @django {
    root /var/www/pootle;
    include uwsgi_params;
    uwsgi_param UWSGI_FASTROUTER_KEY $host;
    uwsgi_pass emperor;
  }

  location = /robots.txt {
    root /var/www/pootle/webroot/static/;
    access_log off;
  }

  location ~* ^.+\.py$ {
    return 404;
  }
}

Reload Nginx and Pootle should be much faster 🙂

I recommend book Mastering Nginx. You can learn there much more about Nginx configuration.

26. May 2013

Python virtualenv and PowerShell

It’s quite easy to set up virtual environment with isolated packages for Python in PowerShell.

Creating virtual environment is the same like in any other shell. Let’s call this environment pyenv:

pip install virtualenv
virtualenv pyenv

The last step is to activate this environment in PowerShell (watch dots and spaces):

. .\pyenv\Scripts\activate.ps1

You should see name of your virtual environment in command line. Virtual environment is active.