2012-12-13

Quick Bits #1 - Nginx proxy

I've been developing social games for over a year now for most of the time I had simply open ports to my laptop at office. Also our office has domain. So inside of social apps I have set iframe url as myofficedomain.com:<myport>. This is convenient only when working inside office. Sometimes shit happens and you have to help/work from home and if you want to load the same app within laptop on different network - you're shit out of luck. You have to get outgoing domain for that, network you're in might require port-forwarding that cannot be changed and so on. So I found the easiest solution for that is to setup proxy to witch you could ssh from any machine and use it as gateway for your social apps.

This brings few good things. You don't need port (for example new vk.com payment system disallows to use links with ports in payment system). You don't need change iframe urls within apps since sometimes that can become annoying since it might require confirmation true emal or even worse - phone, also sometimes these notifications lags quite a bit and it's dead time waiting.

So all you need to do is to get a server that has your soon-to-be-proxy domain pointing to it and Nginx installed in it.

You need to setup Nginx vhost as displayed:
server {
    listen 80;
    server_name myawesomeproxydomain.com;

    location / {
        proxy_pass        http://127.0.0.1:5000;
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
This will proxy all requests that will come to your domain to localhost:5000

Sure on the server nothing runs on localhost:5000 (sure you can change the port) so we need now to setup proxy from your machine to the server. It's simple as connecting to the server. SSH has -R attribute that will setup reverse proxy. Or as manual writes:
     -R [bind_address:]port:host:hostport
             Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side.  This works by allocating a socket to listen to port on the remote side, and whenever a connection is made to this port, the
             connection is forwarded over the secure channel, and a connection is made to host port hostport from the local machine.

             Port forwardings can also be specified in the configuration file.  Privileged ports can be forwarded only when logging in as root on the remote machine.  IPv6 addresses can be specified by enclosing the address in square braces.

             By default, the listening socket on the server will be bound to the loopback interface only.  This may be overridden by specifying a bind_address.  An empty bind_address, or the address ‘*’, indicates that the remote socket should listen on all
             interfaces.  Specifying a remote bind_address will only succeed if the server's GatewayPorts option is enabled (see sshd_config(5)).

             If the port argument is ‘0’, the listen port will be dynamically allocated on the server and reported to the client at run time.  When used together with -O forward the allocated port will be printed to the standard output.
So it's as easy as opening SSH connection with such command line:
ssh -R 127.0.0.1:5000:127.0.0.1:8000 proxyuser@yourawesomeserver.com
So if you run your project in localhost:8000 it will receive everything that comes to proxy domain.

Another good thing to do here is to increase life time of a SSH session. This can be done on both sides - server and your local machine. I suggest to do it on your local machine. Simply edit /etc/ssh/ssh_config if you're in Linux and add ServerAliveInterval 60 this will send each 60 seconds a packet to the server to mention that the user is still around.

SSH session fix is taken from here: http://ocaoimh.ie/2008/12/10/how-to-fix-ssh-timeout-problems/

2012-10-24

Flask With Green Pony. Part #1 - Putting Damn Pony into the Cage.

So I figured to share some evening battle stories. Maybe it will help someone while fighting against their own demons and zombies via Google jails.

TL;DR - scroll to the bottom.

Right now I'm fighting Gunicorn - a green nifty WSGY server. It's not the best. But I like it. Buzz off.
The situation is this. I'm developing quite big flask app. The reasons that made me choose flask over others was that I'm for sure going to use SQLAlchemy and flask has some nifty plugins. For me Django without it's ORM seems bit pointless. So I figured to take something smaller and build on top of it.

One of my friends always says how he's used to start big and just remove stuff he don't need. As I saw him using snippets that was exactly what he did. Vim combo results in huge snippet and then a third of that is deleted. I have quite opposite plan of actions. Start small, but clean and then build up. Sure I see advantage in his approach that you don't need to remember/find that much stuff. Anyways. Flask.

Flask has some issues. Devs loves to put everything in single file. That pisses me off. I like hierarchical clean structures. Also Flask just adores global variables. I would suggest to find a room for those two [flask and global variables], but hey who I am to judge?

So the idea is that I have Flask app that is developed by my standards meaning that there is startup scripts, management commands, soon to be plug-in modules, clean settings for app. Yeah. More or less lighter Django with more litter in the code. But it's MINE. Buzz off.

So challenge is to run my fat-ass Flask app with gunicorn. What Flask docs has to offer?

http://flask.pocoo.org/docs/deploying/wsgi-standalone/#gunicorn

myproject:app? that's what?.. Method? setup.py has possibility to add global console scripts (terminal global scrips per say) that uses package:method declaration. But this does not work in my case if I give it main method. I wish... So what Google has to offer?

http://www.capsunlock.net/2011/04/running-flask-with-gunicorn.html
http://community.webfaction.com/questions/7229/nginxgunicornflask-setup
http://samrat.me/blog/2012/05/flask-nginx-gunicornon-a-vagrant-box/
http://wirtel.be/posts/en/2011/02/24/nginx_gunicorn_flask/

So it expects global variable once again. Great. But then again what's the point of it? I don't want to always run my app with $ gunicorn <stupid_miles_long_path>/main:app <crap-ton of properties>
I want to make it that I just could run MyApp within terminal and give few SIMPLE arguments and everything would be fine. So I want to implement gunicorn directly to my app. Most of the internet here is useless, all you can find is how to proxy with nginx to $ gunicorn main:app

Here is the golden link:

http://damianzaremba.co.uk/2012/08/running-a-wsgi-app-via-gunicorn-from-python/

It's almost right. Few fixes - main file is not main.wsgi in most projects, it's proper *.py and there is few problems. Last line of MyCustomApplication class.

If you look at util file https://github.com/benoitc/gunicorn/blob/master/gunicorn/util.py#L275 you'll find that it tries to extract app file from string (you're giving it string - what do you expect). If you're doing this directly from app - you have everything on your finger tips - why the hell you would want to get file from same folder try to parse it's name and then fail. Yep. It leads to error of booting workers. Something similar to -
https://github.com/benoitc/gunicorn/issues/338

If you're doing everything cleanly - it will simply wont find THE app. And it's easy to fix it. Instead of calling utg.import() simply import your global variable from whatever you placed it and return THE app.

Oh also I added ProxyFix. Not sure if I need it. In most google places it was added so I would assume it's a good thing.

So here is that custom extra class. I named it Server:

from gunicorn.app.base import Application

from werkzeug.contrib.fixers import ProxyFix

from MyApp import app


class Server(Application):
    '''
    Custom Gunicorn Application
    '''

    def __init__(self, options={}):
        '''__init__ method

        Load the base config and assign some core attributes.
        '''
        self.usage = None
        self.callable = None
        self.options = options
        self.do_load_config()

    def init(self, *args):
        '''init method

        Takes our custom options from self.options and creates a config
        dict which specifies custom settings.
        '''
        cfg = {}
        for k, v in self.options.items():
            if k.lower() in self.cfg.settings and v is not None:
                cfg[k.lower()] = v
        return cfg

    def load(self):
        '''load method

        Imports our application and returns it to be run.
        '''
        app.wsgi_app = ProxyFix(app.wsgi_app)
        return app

And this is how my part of main:main looked with flask running:

def main():
    startup()

    app.secret_key = settings.SECRET
    app.run(
        host=settings.ARGS['--host'],
        port=int(settings.ARGS['--port']),
        debug=settings.DEBUG,
    )

And this is how it looks now:

def main():
    startup()

    app.secret_key = settings.SECRET
    Server({
        'bind': '%s:%s' % (settings.ARGS['--host'], settings.ARGS['--port']),
        'debug': settings.DEBUG,
        'worker_class': 'gevent',
        'workers': numCPUs() * 2 + 1,
    }).run()

Sure main has right now access to numCPUs() method and sure Server is imported. numCPUs() method can be found in previously mentioned link - http://www.capsunlock.net/2011/04/running-flask-with-gunicorn.html
Also my app uses docopt, so I just removed __doc__ string from examples here.

I still right now have some issues - it seems gunicorn ignores my debug option. Demonizer suicides. But that's for another evening.

Over and out.

2012-09-09

Asus EEE X101CH Review


For quite I while I have to fix, update and maintain this little netbook. Although it's not mine - I've used it for quite a while and feel comfortable writing this review.

The Outside

It's meant to be cheep. Plastic only. However it means that it's light. Just a bit over 1 Kg with charger in your bag. So don't expect anything special, it's meant just to do it's job and it does it quite well.

Keyboard is surprisingly comfy. Touch pad is incorporated to plastic cover. With this one I had few problems, but about that latter on. It has full size HDMI connection, VGA, 2xUSB 2.0 LAN, Audio connections and Card Reader. That's it.

Screen is reasonable - 10.1 inch LED Backlight WSVGA (1024x600) screen. It's not glossy, colorful and sharp enough that it wouldn't strain your eyes.

All in all - it's good enough that it wouldn't bother you.

The Inside

This one is a bit tricky. Sure it has standard Intel Atom N2600 Dual Core processor and a gigabyte of RAM. 320Gb HDD, 0.3 Mp front camera, single (quite bad) speaker,  WLAN, Bluetooth... Nothing special until you touch operating system... Here goes my rant...

It is suggested to be used with Windows 7. What a load of bullshit that is. First touch pad drivers... Non of both types of drivers provided by Asus does not work and it's better to leave it as is after installing Windows. One of those makes touch-pad unusable other simply does nothing. Great.

Secondly it has only a gig of RAM, immedeatly at least half of it is occupied by Windows. So if you want to write a document while opening 3 or so tabs of chrome while listening for music in the meantime - you gonna have a bad time... You cannot upgrade RAM since it's stuck right to the motherboard and does not have SO-DIMM.

I'm not sure ether it's fault of Windows since it does not have swap space and tendency to ran out of memory or ether it's problem with overheating, but netbook tends to freeze up from time to time (also it gets hot) when under heavier loads. It's really annoying and it's a huge problem. On this alone I would not recommend buying this one, but only if you cannot live without windows.

Yep. Linux to the rescue! Linux did had a problem with this laptop since Linux did not sported Intel GMA 3600 graphics (this netbook Intel Atom uses these). So you got 800x600 resolution only, or something similarly small, but since release of Linux Kernel 3.4.2 it works fine. So newest batch of Linux distributions will work better and better with this laptop. Currently I've installed Ubuntu 12.04 and 3.4.2 Linux Kernel and it works fine. Sure Battery wont hold as long, brightness settings does not work yet as well, YouTube video lags a bit too (from time to time), but hey, at least it does not freeze up for a minute once per 5 minutes and can multitask. Ubuntu 12.10 already contains this Kernel update as far as I know, so there wont be any need for addition fiddling with Linux Kernels very soon. Also running Linux makes it faster and more responsive. And no more - "you [pathetic human being] need TouchPad Drivers" popups again!

All in All

It's small little netbook, probably will be on sale for very low price in official retailer shops since Asus is killing eee netbook series due to tablet invasion for small portable devices. If you do need something like that and you're comfortable with Linux I would really suggest grabbing one just because of low price it could end up. Otherwise if you do need windows and you do need exactly netbook, I would suggest to avoid this model.

Update

So it seems that Ubuntu 12.10 will not boot at all since there is a bug that requires to blacklist that particular GMA chipset. So far the best is to run 12.04 with 3.4.2 kernel. At least that works. More info here: http://linuxrepository.blogspot.com/2012/09/how-to-install-linux-342-quantal-kernel.html

Update 2.0

Ubuntu 13.04 works fine. Unity is quite slow, but Xubuntu is perfectly usable. No issues whatsoever. Maybe something is not quite right (again it's not my laptop and I am not a primary user of it so I didin't test everything), but from first sight it's fine and so far I had no complains.

Asus Transformer Prime Revisited or Bashing The Prime

I should keep my promises... I haven't found any time to do any blogging, or I was too lazy. Who knows... Anyways this is my retaliation. As promised Asus transformer review revisited! If you haven't seen last review - look here.

The Bad


WiFi... I've mentioned that WiFi is not the strongest point of this tablet. It's still true. Range wise it's OK...ish. But the main problem with it is speed. Downloading larger files suffers from quite a speed loss. I can't tell the exact figures since it depends on place and WiFi spot, but I've ran into situations where cheap eee net-book downloads up to 4x times faster. For regular usage it's fine, just for larger files you will feel lack of speed.

Battery life is amazing. I love it. Problems appear when you ran out of battery. There is two stages of battery death. When it does not turn on, but displays an empty battery if trying. And another stage is when it does nothing at all. On first stage it takes somewhere up to 15 mins of charging time till you'll be able to turn it on. On second stage it takes up to half an hour of charging. It sometimes becomes really annoying. For example if you're not using tablet for few days or a week and just want to watch a movie in little time you got.

It's bit on the heavy side. While reading a book or playing a game it may become a bit uncomfortable. It's not a big deal but...

The Ugly

Brushed aluminium finish does look good, and I can't see any scratches on it. Fingerprints don't shine on it ether (the screen is still a magnet for them). However it gathers fat from finger prints and then dusts so that might look a bit ugly. It's still quite easy to clean it though, but it's not as easy to maintain shiny as some plastics.
OK i'm nitpicking here, but my point is that it looks really good only if it's clean therefore it requires regular cleaning of all surfaces.

Opening keyboard a bit too wide and pushing on it a bit makes a gap in the keyboard dock back side. I does not break just makes a little gap that is closed easily with little push. It's just a tiny bit annoying.

Although I was worried about audio jack connection, it turned out fine. However micro-HDMI connection turned out to be fiddly. It will loose connection from time to time if you're moving tablet. So it might not be the best option for presentations if you want to have tablet in your hands and connected via micro-HDMI. I have to point out that it won't happen randomly, so for connecting to TV and watching movies/YouTube there are no problems.

The Weird

Physical keyboard is only good in so few languages. If your native language is English or one of few supported that it's fine for you, but for me it sucks. I can't reply to the most of the emails, I get uncomfortable when using IRC or other chatting programs... The only option is to use non standard on-screen keyboard. It does work, but leaving out physical keyboard in spots where it would be most useful is a bummer to say the least. Sure I could write using only standard characters, but I feel like a retard by doing that.
It is possible to solve this issue with external apps apparently.  Also incoming Jelly Bean update might contain cure for this issue.

If I'm trying to watch a better quality movie on my TV via HDMI connection... First of all most of such movies won't be playable by default player. Sure it's a minor complain since there is so much to choose from. However most players stops playing those movies after 3-15 mins randomly. It's due to software decoders as far as I know and only few support hardware decoders well enough to be usable. So I ended up using BSPlayer. So don't rush to purchase any of the player apps and test those before buying if you need one.

Round up

These are the main bad point I've seen so far. I still think it's awesome tablet and worth it's price, just it's not as perfect as someone may portray it. Sure new model is already out there, but if you get chance to get this one for cheap, I would say it's a good deal.

2012-03-12

Asus Transformer Prime and Android ICS first impressions

I've recevied Asus Transformer Prime just few days back and these are my initial impressions. In case you thinking about buying one you probably allready saw bunch of reviews on youtube, so I'll try to sum up my afterthoughts. Display cons and pros. I hope it will help someone to decide. 

Just keep in mind that this is additional info that I trow into the wild. It is something that was unexpected for me after all those reviews I've seen and have red.


TL;DR
If you like Android - it is awesome. In other case, whatever I'll tell you - you won't care, so buzz off.

If you do have a chance - wait for next generation. Having in mind how much Prime got better compared to it's precesessor, next one should be awesome. However if you need a tablet right now - it is so far the best tablet experience I had so far. Sure I can compare only to Samsung Galaxy Tab 10.1 and iPad, but damn this one is good.


Bad

Grey color looks purple
Prime is available in two collors - silver and grey, atleast these two collors are being written on specification in shops I've looked at. Silver looks like elephant tusk (yellowish white) and grey model looks dark purple... Nowhere near grey. Not that I would care that mutch about collor, but still it would be nice to be happy about it. For now I can say my purple model is a bit ugly OK (after living with it few days).

Brushed aluminium looks like pre-scratched
Prime is also proud about it's metal "spun" finish. It looks as if someone would scratch it in circular motion, nothing special. Sure usualy you will find brushed aluminium to be done in straight lines, but the visual effect of this is just too small to be worth bragging so much about it.

Connection protectors - one time deal
Most of the connection slots are protected by soft plastic plugs that are quite hard to pull out. Also they are perfect parts to loose. So that is what I sugest you - just pull it out if you need that port and forget the plug...

Sloped edge connectors

Tablet has sloping edges that give smooth finish. It sure looks nice, but sticking in headphone jack will mean that on the shorter side of the connector - will stick out ~3-4mm in my case. It applies to most of the connectors, yet the most problematick might be jack one since with uncarefull actions you might damage both the plug annd the socket although build quality seems to be strong.

Noise when music app is paused
Sometimes when music app that uses sound channel is paused/muted I can hear noise in my headphones. It may be due to connector, but having in mind that I've used my headphones on numerous devices and this is the first one to have white-noise i'm blaming jack socket or the software.

Fingerprints
It is litteraly fingerprint maget. Although fingerprints are not that visible but they also are a bit harder to clean than from other devices (I'm talking abouth both - the screen and metal finish).

Heavy
As from tablet I would like that It would be bit lighter. Adding keyboard means the unit will weight over a kilo. 

No Linux support
Asus putted in some really nice apps for syncing, remote desktom and even streaming, yet non of those work under Linux. Pity.

Lousy mono speaker
Although Asus claims that they added stereo speakers, in reality there is only one. And after making some stereo tests it's clearly mono one, just maybe with seperated mid and low sound channels (doubt it). Also some people are angry that they putted in just where right handed people are holding tablet it self thus covering the speakers, but I think this is better decision that putting it on a side in other place (with exception of top/bottom center). Why? Covering grills disturbs the sound waves making them spread to any dirrection available so if you're not holding it firmly you might feel "stereo" effect. If you're on a dock, then it's totaly mono. On the right side only. Yup. It's like a youtube video just with right channel. After some time it becomes painfull to listen. I have to mention that quality of the speaker it self is actually good. It's just that it is mono and placement could be better.

No F row

Keyboard dock does not have F row keys. Yes Android does not need it, but if you connect to a remote machine via rdp/vnc/ssh you only will be able to access those true the screen if app will allow it. I think it would be an wesome if prime dock would have those accessible with fn+shortcut key.

Random reboots
I've witnesed few random reboots. 4 so far.

App refresh on keyboard plug/unplug
If you're whatching youtube video on full-screen or playng game, after connecting/disconnecting dock - app will be refreshed restarting the game or reloading youtube video. Annoying.

Slippery bottom
Although dock has rubber feets, it is quite slippery when opened since only front feet touches the surface and back ones are raised by hinge so pushing it from the table is not that hard. When closed it's quite steady.

Only few languages for dock
There is not a lot to say more. Samsung clearly does care a bit more that Asus about adding other keyboard layouts. And remmember that on-screen keyboard layout might be customized with additional apps while dock cannot. Atleast it is my initial impression, I hope I'm wrong.

Box contains
There were literally charger and USB to proprietary connector. Oh and a wiper. That's it. I hoped to get more gadgets by it. Mini HDMI to HDMI converter maybe? Although I must say it was known to me. Still, this is a field where they can do better.


Good
Aluminium
It is covered in cold and nice aluminium (with exception of front side of tablet). It mght be negative thing for some, but I do really love it.

No heating
Because of aluminium ability to transfer heat, even on high loads or charging, it remains cold. The only part that gets hot is the charger it self.

Screen

Every single review I've looked at commented how good the screen was yet some mentioned - "hey there are some problems with wifi, but you already know it!". Sure regarding the tablet it is one of the main features and boy oh boy it is good. Sure iPad3 now offers cosmical resolution, but i doubt if you need that much on such small screen. The only thing that would be cool is better resolution output true HDMI (maybe it does, haven't tested it yet) besides that I doubt that you would see a difference on the screen. Maybe. Anyway, screen is gorgeous. Crystal clear, bright and sharp.

Build quality
You feel that it is premium product. No squeeking, no wobling... Apple once was famous because of quality. This goes on the same road.

Keyboard/dock advantage
Keyboard is as in the regullar notebook, reminds me Apple MacBook. Personally - quite comfortable having in mind that I'm holding it on my knees. Sure after long time of typing your hands might start to hurt, but it a common problem with all the notebooks.

CPU power

As with all Androids from time to time you will ran into rare studdering, but besides that it is the soothest expirience so far even with quite a few apps running.

Sound true headphones
Although speaker on the tabled is a shame - sound comming true headphones is astounding. I haven't got a portable device with such hight quality in years. Besides that bug of noise on paused songs, there are no white-noise in the background while playing and music is crisp and ritch. I know that it is my new favorite device for music listening.

1080p playback


Default video played does not understand all the types of videos, but MX player does understand quite a lot of them. Also VLC is coming to town... And if Prime understands the codecs - it can playback 1080p. Great!

Battery life
That is another ace of prime. Dock contains another battery extending battery life. So far I unloaded doc's battery within 6-7 hours so I don't know yet how long tablet battery will last. Having a dock means that at most you will need to charge it once a day. At most. For me it lasted ~14 hours or so of constant usage on a single charge.

Initial app set

I own HTC Desire Z that by default contains a crap ton of apps that I don't need. Facebook, Amazon MP3 Store and so on. Also I cannot delete them and that annoys me. On this tablet on the other hand, I havent found a totaly uselless app. Maybe some news readers are not as needed but besides that there are ton of really good apps. Polaris office syncs with google docs and is perfect for Android, file manager is also really easy and nice to use. Asus really thought about what to stick in instead of trowing in what brings money.


Wifi
In quite a few reviews it mentioned wifi problems, that wifi signal is weak due to aluminium backplate. To be honest, I can't see that happening. My laptop has quite bad wifi and phone (since it is a small device) has one even worse. I hoped that this one would have atleast same strength antenna as my phone, yet it has almost as strong as my laptop, if not the same. So I really cannot say that wifi signal is that weak. Because of that many negative reviews I can say it is a good thing since I underestimated it.

Mini HDMI connection.
I'm looking forward to test it out.

Conclusion
Latter on (after a month or so) I'll try to post an update about this thus making it a proper review. I have updated only a little of this one, but I've finally have written follow-up here
Despite all that nagging with negative things so far it is worth this status:


2012-03-05

IT Window - For Loop

WTF?
IT Window is a new series of articles special for younger people who are "into computers". I have a friend who is learning programming and looking over to "big boys" tools, and sure he have some questions, those still occurs even after reading tutorial or two. Here is the place where I step in and try to help him by writing these articles. Sure samples could be done better, but I try to use as simple format as possible avoiding one liners.

Wikipedia
In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement.
Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. For loops are also typically used when the number of iterations is known before entering the loop. For loops are shorthand way to make loops when the number of iterations is known, as a for loop can be written as a while loop.
The name for loop comes from the English word for, which is used as the keyword in most programming languages to introduce a for loop. The loop body is executed "for" the given values of the loop variable, though this is more explicit in the ALGOL version of the statement, in which a list of possible values and/or increments can be specified. In FORTRAN and PL/I though, the keyword DO is used and it is called a do loop, but it is otherwise identical to the for loop described here.

http://en.wikipedia.org/wiki/For_loop

Simple
For loop is a method/function that runs for certain time - for 5 minutes, for 5 times, for 5 objects, etc. During each cycle it usually contains value that changes each time. Lets show it using pseudo-code. Lets say we have 4 friends and we want to give each a number from 1 to 10, without a loop it would look something like this:
Step to friend #1
    give him a number 1
Step to friend #2
    give him a number bigger than last one by 1
Step to friend #3
    give him a number bigger than last one by 1
Step to friend #4
    give him a number bigger than last one by 1
Already see repeating part? so with for loop we could do that:
 For each friend
    Give number bigger than previous
OK different situation - lets say we have two numbers (2 and 100) and we want to find even numbers between those two. No I won't write pseudo-code without a loop (that would be just spamming), but if we would use for loop:
For each number starting with 2 ending with 100
    if its even
        shout "EVEN!"
    else
        shout "D'OH!"
So each time (each number or loop) we would take the same variable that contains new number and look if its even or not, if its even we shout "EVEN!" if not - we shout "D'OH!".

And our third and final situation - lets say we have unknown number of apples, some are yellow, some are red and some are green. We want to count each color. In our mind it's simple task, just look at each apple and count different colors. With for cycle we do the same:
For each apple
    look at the color
    add +1 to that color
After this we would have 3 values each representing different color and count.

Python
Now lets do the same in real programming language - Python!

For our first case - firstly define our list of friends and base number:
friend_list = [['John', 0], ['Tom', 0], ['Billy', 0], ['Harry', 0]]
number = 0
Now lets do the loop:
for friend in friend_list:
    number += 1
    friend[1] = number
Have in mind that Python starts counting from 0, so second element in list is reached with array key 1 not 2. So now if we would run:
print friend_list
We get:
[['John', 1], ['Tom', 2], ['Billy', 3], ['Harry', 4]]
Now lets do the second case:
for number in range(2, 100):
    if number % 2 == 0:
        print number, 'is even'
    else:
        print number, 'is odd'
Yep, to go from one number to another just use range(), easy eh? OK now the final one. Lets define our apples and colors:
apples = ['yellow', 'green', 'red', 'yellow', 'red', 'red']
green, red, yellow = 0, 0, 0 # All colors have 0 value
Now simple for loop:
for apple in apples:
    if apple == 'yellow':
        yellow += 1
    if apple == 'red':
        red += 1
    if apple == 'green':
        green += 1
So final result is:
print 'yellow = ', yellow, ' red = ', red, ' green = ', green
And we sure get:
yellow = 2  red = 3  green = 1
More about For loop in Python - http://wiki.python.org/moin/ForLoop

Notes
In most more basic languages such as Pascal we would need to know how many elements there are in the array therefore #1 and #3 samples are different since in #3 we don't know the number of apples, yet in Python it's not a problem. In other languages we firstly would count elements in the array using some libs or hold a number somewhere to determine count of data inside an array.

2012-02-23

Stop Bashing on Linux Terminal and Start Using Zsh


Zsh? Bash? Dude... WTF?
If you are new to the Linux world or haven't really been into it you might have seen or heard of such scary program - Terminal. It's basically a window to the past when everything was not ran true nice graphical user interface (GUI), but rather true text inputs or ASCII looking ugly programs. Remember Windows XP or older Windows installations? That's what we call ncurses looks. Sure I must say/write that most of the programs that use ncurses looks a lot better so sorry if I made someone angry by this comparison...

Anyway back to the point... So people uses this window to the past to use some sort of command line tools or  programs and the tool that understands those calls and runs proper programs inside that window is ussually bash... Or as I now will propose - Zsh. Why this matters? Terminal can do stuff a lot faster since typing is faster than point and click also it's distraction free and easy on your resources. Really! Therefore most of Linux wolfs heavily uses terminal.

Perfection lies in simplicity
If so - Zsh if far from perfect. Yet small and simple parts of it brings it so much closer. If you tried Zsh before, you probably haven't noticed a huge difference to the bash, also you might think that bash has better integration to current Linux distributions (better looking prompts), but under certain point of customization it becomes insignificant what integration is done for the distribution you are using and if you like personalizing stuff as much as possible, you will like Zsh.

So just change bash to Zsh?
No. Zsh alone is pretty boring (just as bash is), unless you at the end of the line of geekyness and you don't like what others has to offer, or you just don't care about using terminal.
So the magical pack is called "Oh My Zsh!", it just trows a pile of configs on top of Zsh, and allows you to change anything you like.
It adds multiple aliases (for e.a. sudo==_) and allows to use themes. Sure it also brings full configuration for Zsh after which you are able to navigate in terminal easier.

Give me samples!
O.K. For example if you previously executed 'vi foo' and then 'ls ~', after pressing up arrow you will be given command 'ls ~' just like in bash, but if you would start typing 'vi' and press up, it would give only those history results that starts with 'vi'. Cool eh?

Some more. If in your $HOME directory you have a file 'Documents', and you would start typing 'doc', press <TAB> - it would autocomplete to 'Document' fixing the first letter, sure having in mind if there would not be any documents/files starting with 'doc'. Remember in Linux capital and non-capital letters are different.

All of this was just Zsh, but combining yih "Oh My Zsh" you get good looking and smooth terminal experience. Why I say so? Because that magic pack also adds some aliases that fixes your common mistakes. For e.a. if you miss 'cd' at the beginning of command and you would just type folder name it would automatically go to that directory.

Or for example have you ever typed 'sl' instead of 'ls'? It also fixes that (sorry steam locomotive fans).

For more complicated mistypes ('emaxs' instead of 'emacs') in case when Zsh would not find 'emaxs' executable it would ask if you mistyped 'emaxs' and would like to fix it to 'emacs' [Y/N]? Now thats really neat.

Thats it? Bash have awesome configs too!
At first those things looks small, but latter on they become so important that you will stick Zsh in every single of your machines, believe me...

Also one of now my favorite Zsh features is globbing.

Usually if you use wildcard '*' in a command you probably referring to every single file in (lets say) current directory, Zsh allows to use '**/*' that means not only every file in current directory but also all the files within folders that are in current directory. And to objection to your -R comment, just have in mind that not all tools (especially small ones) have support for recursion.

As with wildcard (*) you can use full patters with this. '**/*.py' will find all .py files.

Zsh has really good regular expression engine build in so finding files for commands is really easy.

It can also give you files modified within past 24 hours or with certain permissions just out of a simple shell, completely replacing 'find' and pipes (|).

All of it is nicely covered here by Stanford university: http://openclassroom.stanford.edu/MainFolder/VideoPage.php?course=PracticalUnix&video=zsh-globbing&speed=100

So How to Install This?
Simple, just install Zsh (It should be available on most of major/alive distribution within standard packages). Then install git if you don't have it.

Finally follow instructions on https://github.com/robbyrussell/oh-my-zsh.

P.S. I do trust automated installer option - it works.

Anything else?
Just look at the Oh my zsh wiki (first link above). Also you might look up Zsh reddit community at http://reddit.com/r/zsh besides that - just give it a week or so, you'll like it.

Also as suggested by Alexander these links are also worth checking out if you're interested in scripting:
http://www.linux-mag.com/id/1079/
http://grml.org/zsh/zsh-lovers.html

Happy usage, Over and Out.
JackLeo

2012-02-14

My valentine day's post for Vim

WTF is Vim?
From vim.org:
Vim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems.
Vim is often called a "programmer's editor," and so useful for programming that many consider it an entire IDE. It's not just for programmers, though. Vim is perfect for all kinds of text editing, from composing email to editing configuration files.
Asides the official/commercial crap it's a minimalistic editor meant for text editing. It has a totally different approach to editing so it takes some time learn it. Yet it is impossible to completely master it. Why? Because it has almost infinite ways to be customized and there will always be a something that you forgot/did not knew about it. Despite that it is really small. 

Why do people use it?
Vim usually comes with all major Linux distributions (or at least Vi - older and even smaller version of Vim). So it means that if you ssh to a server - you probably will find Vim to edit files/configs with.

It's cross platform so you can have same editor (with mostly same configuration and same look and feel) in your Linux server, Mac home box and Windows work-desk. 

Since it has crap-tons of plugins - it can be extended to change IDE.

Options
As mentioned before - it has crap-tons  of plugins so you can end up most of the arguments with - "there is a plugin for that". Sure with the exception of image processing. That's left for Emacs.

Also if you don't like cmd/terminal apps there is GUI versions of vim as well, but primarily I concentrate on Terminal version, because I'm geek like that. Sure there is even different spins of Vim (pre-configured Vim's) or easy Vim (evim), but I tend to ignore those as well, because in the end you will end up with your own version of Vim anyways...

But IDE is made for development of <insert programming language here>
Yes. And it works as a charm. Some of the IDE's can handle even few languages, but what happens if you need to edit just a config file? Using same IDE would be overkill. Just to load damn thing would take a considerable amount of time. So you would load up tiny text editor of some sort (just for that [sometimes edit a config file] learning Vim is not worth your time since you would just drop in, make changes and get out).

But what happens when you enter that tiny editor? Suddenly it becomes harder to navigate, there is no auto-completion, no tags, no manual shortcuts or what not. 

Yes I am aware that you can config gedit or other small editor to do quick tasks, but I'm trying to make a point so shut up. Also you cannot use gedit in Linux server. If you can - this means you have GUI there and that means that you're doing the whole server wrong - it's not windows. Back to the point.

IDE is meant for specific task. Like a screwdriver for a screw or hammer for the nail. But when it comes to different situations either you have to spend a lot of time reconfiguring damn thing or you will do a sloppy job using the very same IDE. Sure hammered in screw will hold, but one day you might end up screwing a nail...

Whole academic world uses Emacs!
That's not entirely true. Yes Emacs is a lot more popular in academic world and I know few professors who use it, also database Stanford online courses was recorded while professor was using Emacs. Yet if you would look at system administrators - Vim is a lot more popular there as well so 1-1.

Jokes aside Emacs is a great tool to create text. Yet I personally don't like it's ideology. It tries to become everything at one. Need an extra terminal window - here Emacs terminal. Want to see that picture - here you go. Want to watch that film sure!? So Emacs tries to be your OS and change tools that is meant for specific tasks to make things more "unite". That's why there is a joke that Emacs is a great OS, yet it lacks decent text editor and I have to say it is soo true. So if you need complete and easy package for doing anything - Emacs way to go. But If you leave films for tools such as VLC and in search for tool to edit text - Vim is way to go.

My story
Why I use Vim - simple. I always fancied that hacker style desktops with tons of text and ncurses GUI's, also I love Linux and do web development - this means I have sometimes to do editing/configuration in servers and I wanted a tool that would be the same in everywhere. Oh and Ubuntu server (I started my Linux career there) have Vi pre-installed.

I've used to use heavily configured Gedit, yet it felt lame. Potential of an editor ended up quickly... At my first Job I tried to use IDE as a "professional" developer (net-beans) yet it also did not stick. It loaded slowly, felt bulky and boring stuff taken too much screen space. It distracted me and was pain to configure/personalize. Since I knew basic usage of Vim - I started to use it more and more especially when few colleges were using it as well, they helped me to move along and showed plugin magic. Now I wouldn't change it to any IDE.

So, WTF is Vim?
It's an editor. Primarily - a code/configuration file editor. If you would monitor what you're doing with a text, you would see that most of the time you're changing text not creating it. And by changing I mean moving or modifying text. That's why Vim has 3 modes. To insert text (you do that the least amount of time), to manipulate text (normal mode) and to highlight text for easier bulk manipulation of it. It is build in mind that moving around has to be simple and efficient, that change text should be easy and comfortable.

That's why academics prefer Emacs - they are creating texts, not editing them and that's the same reasoning why system administrators prefer Vim - they're changing aka editing config files, not creating those.

Development is also primarily consist of existing projects support or development - extending the same project and thus requires mainly to edit the code not to create it.

Don't get me wrong by edit I mean also extend it and all those fancy features for auto-completion and syntax checking is included. As I mentioned - "there is plugin for that". Think with a bit more open mindset when reading this.


Final notes
I had this article/post/blah in mind for over a couple of months and just now I decided to put it finally on bits, probably kilobytes... (wanted to say paper but damn that's internet)...

The main idea is that Vim is great tool for anything you trow at it. It might not be as easy to learn and it might make you mentally ill by using "hjkl" everywhere but it is worth it.

Some people will love it, some will hate it. So choose your own weapon for work. If you're still reading this and not writing an angry comment - it might be just for you. Otherwise I will not even bother to give a suggestion since you're not reading and you already have an editor you (probably) love.

So it happens that today is Valentines day and this is how show my love to my weapon of choice. Now it's time to go home and show some love to a person I truly love, so I'm leaving you here with your thoughts. If you will be patient enough you will find more articles about Vim here, yet I cannot promise when it might happen or how many of these [articles] you will have to skip until mentioning Vim again.

2012-02-07

Algorithmic passwords


What?! Why?
Usually we use a string for a password that we memorize it. Making a random string does help if someone tries to read while you're typing or in case of someone tries to guess it. Besides... It's pretty much useless. As shown, longer passwords are more secure, yet easy to read (steal). My old password was 11 symbols long and easy to memorize. I thought it was good and rather hard to breach. I haven't thought of other problem. What if your password would be lost because of the website, not you? Sure some may say - use few passwords! Yet again if top tear password would be breach, it could be tried for as important things and... Fuck! Or if your email password is breached most of the websites allows recovery of password using simple email form... Shit happens bro!


What's the cure?
Instead of memorizing string, memorize an algorithm. If you're familiar with MD5 you may know about salting. MD5 takes a value and returns unique for that value string. Salting adds extra characters to a final string in predetermined way (not exactly, it's just a example). This makes same string have different value of MD5 than it would get in another machine so thus making brute force cracking a lot harder when salt is unknown. The final result of such algorithm is always a unique password for each service and no additional memorizing required. Its easier than it sounds.


Samples
I suggest to take a peace of paper and write down a series of test cases:
foo
bar
foobar
se7en
localhost
123654
a
google.com
apps.facebook.com
Now write down salt you might use. For example birth date last numbers and first letters of your favorite poem:
1950-01-15 => 015
Roses are red Violets are blue => RarVab
Now come up with an algorithm. In this example lets say we will take a number of letters inside service name (apps.facebook.com => facebook) then we add last number from salt.
After this we will take first 3 letters from salt and first capitalized letter from domain.
Then we will write first and second number from our salt and if service has even number of letters we write capitalized last letter else non-capitalized second to last.
Now we end up with last three letters from salt.


Now look what kind of passwords we get:
foo => 35RarF01oVab
bar => 35RarB01aVab
foobar => 65RarF01RVab
se7en => 55RarS01eVab
localhost => 95RarL01sVab
123654 => 65Rar1014Vab
a => 15RarA01aVab
google.com => 65RarG01EVab
apps.facebook.com => 85RarF01KVab
But what if I forget?!
Chose salt that you won't forget. Also you may use some sort of written formula. For example in this case <C><Snl><Ss1><C1><Snf2><eCl/sl><Ssl>. If someone would read this for a moment he will definitely wont get WTF is that or especially what it means to you. While you would just read it in such manner:
<C> - count of letters in service name
<Snl> - salt number last character
<Ss1> - Salt string first part
<C1> - capitalized first letter od servce name
<Snf2> - salt number first 2 numbers
<eCl/sl> - if even then capitalized last letter or else second to last letter
<Ssl> - salt string last part
This looks so hard...
After writing these tests I already memorized it. Sure password entering will take to get used to, but after a while you will start to solve this algorithm in a split of a second just like you did multiplication table in school. Just remember - double password fields is your best friends! Yes for this you do need a certain mindset but if you still reading this you probably got it. Just remember this is only an example. Algorithm might be anything you like. Take your current password and trow in some characters from service and this will help a ton in automated attack scenario witch is the most common.


Why the hell you need this?
I raised this question all the time I thought about using algorithm instead of string. Today I got a letter with title: "ACTION REQUIRED - Password issue on djangopackages.com". Here main point of this email:
ACTION REQUIRED - If you use the same password on djangopackages.com as you use on other sites, you should change all of your passwords. 
[Full disclosure:  We were alerted that some account information was publicly exposed. There have been no reported incidents of passwords being stolen. We have corrected this error, but as a precautionary measure are moving to OAUTH, so that we don't store your password at all.]
No I'm not mad about this on djangopackages, It might happened to anyone. At least they got the dignity to inform its users unlikely some huge companies, moreover it forced me to use algorithmic passwords.


Notes
Image source: http://xkcd.com/936/

Fresh start

Hi, it's me again.

So it happens that now I'm on blogger. Why? Mainly due to integration with Google services as well as my server being development battle ground.

I got bored just messing up with Django simple and primitive blog so started looking at the other areas thus making blog in quite bad state as well as being unpractical. Maybe when things go better I'll perfect it and run it once again. For now - blogger it is.

I'm going to import my old post latter on.

Over and out - JackLeo.