Yahoo, please respect my choices!

Mar 2 2014

Yahoo added me to different marketing newsletters without my permissions. When I subscribe they tell me that it will take 10 business days to take effect! This is the 3rd time I do this!

Yahoo, I know you want to make more money, but please respect my choices!

My toolbox

May 12 2013

It makes me sad when I find out that a lot of people use bad tools, so here is a list of tools that I use almost everyday (as of May 2013). Most of them are great & some of them are acceptable. Feel free to suggest better tools.

Your life is miserable without these

VLC Player

There are 2 types of people:

People who try to open a movie with Windows Media Player, but find that they cannot open any movie with it, so they download the XviD codec & install it. Later, they try to open a movie with extension RMVB, then they find that they want another codec which conflicts with the 1st codec, so they remove the 1st codec & install the 2nd, but not all movies requiring XviD works, so they ask a friend who tells them about the Combined Community Codec Pack which plays all kind of videos except Real Media & QuickTime, but if you install Real Alternative & QuickTime Lite to play RM & MOV videos. Now they just need to install DirectVobSub for subtitles & they are finished. This is just for Windows.

The 2nd type of people installed VLC player & lived happily ever after.

Everything search engine

So you want to search for files by their name or extension. You open Windows search & type the file name, then you wait, and wait, and wait, and ....

During this, I downloaded & installed Everything search engine, searched for the files I need, found them & already working on them.

Now, Windows just showed the results. Please remind me why I searched for these files?

SumatraPDF

Why install 48 MB slow application to view PDFs when you can install a 5MB lightning-fast application which can view PDF, MOBI & many different formats?

Google Chrome or FireFox

Which one you choose depends on you, but you should not use IE.

Paint.net

Swiss-army knife for raster image editing. What MS Paint is supposed to be.

Virtual CloneDrive

Free Virtual CD program without ads. You won't find a better one.

Free Download manager (FDM)

Free & easy to use download manager.

youtube-dl

Did you see James Shore series Let's Play TDD & wondered how you are going to download all of these videos to see them offline? youtube-dl can download from youtube & tens of other sites as well.

ffmpeg

FFmpeg is a media conversion tool. I've mixed feelings against it. It's very powerful & fast, but also very hard to use. Each time I've to google how I can use it, but it's performance makes it practical than other conversion tools I tried.

CCleaner & Defraggler

The best crap cleaner & disk defragmenter.

For command-line gurus

ConEmu

It's a shell container which groups all your shells in a single window with multiple tabs, including putty.

PyCmd

A smarter shell for Windows. I like that when you press TAB it shows all the available options, instead of cycling through all the options. It also supports persistent history.

For the Ninja programmer

Python

I used different programming languages, but Python is the swiss-army knife that I use when I've the choice, and it didn't fail me. I used it for small scripts, GUI apps, to high-performance web apps.

Sublime text

I wanted a powerful text editor (like VI or Emacs) but didn't need me to relearn how to use a text editor. I used Notepad++ for a long time, before I switch to Sublime.

SQLite & Postgres

SQLite is convenient for development, because it doesn't require installation of any kind.

I had nice experience with Postgres when I built rawa7.com. Installing PostGIS (Postgres GIS extension) with other GeoDjango dependencies on Windows is painful, but after this it's a joy to use.

It also supports compression, recursive queries, Multiversion concurrency control, custom types, table inheritance, regular expressions & other interesting features. DB choice is probably the hardest thing to change in an enterprise, but Postgres is worth a try.

Dynamic languages on .net

Jan 23 2011

Today was the 2nd joint event by dotnetworks and EGJUG. There was 2 sessions: Oracle ADF a practical experience (which I didn't attend, unfortunately), and dynamic languages on .net, by yours truely :). There were very few people who attended, about 15 people. They were very active and participating, and I believe they liked the session. The main goal of the session was introducing Python (specially IronPython) to the people, differences and similarities to C# & Java, and what to apply lessons learnt from Python to these languages. This is the presentation (which is very simple, because most of the session was practical)

Also you will find the DreamPie session (with the sample files used in the session). DreamPie is an excellent Python shell, which can integrate with CPython, IronPython and Jython, also giving you correct AutoComplete. As a bonus, this is also an example of using BeautifulSoup - a Python library for parsing badly-formatted HTML - from C# 4.0. BeautifulSoupUser.zip, 11.csv.zip, 11 - Copy.csv.zip, Python-Session-2011-01-22.html

MIX09 videos

Mar 25 2009

MIX conference is my favorite conference. It is always full of surprises, and it is not restricted to Microsoft technologies. It has been a habit for me to grab the links to MIX videos since MIX 07, and I'm keeping this habit for this year too :). Here is the source code to the Python program which grabs the links. It generates two files: mix09_links.txt which contain only the links so you can import them in your favorite text editor, and mix09_title.txt which contain the title of every session and its URL, so you know the title of the session.

from urllib import urlopen
import re
links_file = open('mix09_links.txt', 'w')
description_file = open('mix09_title.txt', 'w')
try:
    for i in range(16):
        url = 'http://videos.visitmix.com/MIX09/page%s' % (i+1)
        print url # for tracking
        s = urlopen(url).read()
        links = re.findall(r'<h2 class="title" title="(.*?)"><a href="/MIX09/(.*?)">.*?</a></h2>', s)
        for link in links:
            links_file.write('http://mschannel9.vo.msecnd.net/o9/mix/09/wmv-hq/%s.wmv\\n' % link[1].lower())
            description_file.write('%s\\nhttp://mschannel9.vo.msecnd.net/o9/mix/09/wmv-hq/%s.wmv\\n\\n' % (link[0].lower(), link[1]))
finally:
    links_file.close()
    description_file.close()

and for the lazy person, here are the two files: mix09_title.txt, mix09_links.txt

Automatic admin interface for SQL Server using Django

Nov 20 2008

A problem that we face everyday is creating CRUD interfaced for our databases. Creating a CRUD interface is always a boring and tedious job. Even if you want to initialize your data, you may break something by mistake if you use SQL Server Management Studio, because it doesn't know anything about your business rules. Django_ - A web framework for Python_ - gives you the solution. It has an application called "Django Admin" that provides a customizable, production-ready, nice-looking UI on top of your DB. If you want to see the final results, see the screenshots at the end of the post.

Setting the environment

  1. Install Python 2.5 or newer.
  2. Install Django 1.0 or newer.
  3. Download and install pyodbc. You should install the one that is compatible with your version of Python, e.g. if you installed Python 2.5, you should select pyodbc-2.0.58.win32-py2.5.exe.
  4. Checkout django-pyodbc from http://django-pyodbc.googlecode.com/svn/trunk/ using a Subversion client, then you should move the directory sql_server to C:\Python25\Lib\site-packages\.

Creating the Django project

A Django application is a complete component, including the DB model, views and templates. Good examples of Django application are user registration, tagging, search, etc. You can find a lot of reusable applications on http://www.djangopackages.com/. A Django project is a group of application that works together. Now you have the environment setup for Django. The next step is creating a new Django project. On the console, write this command

C:\Python25\Lib\site-packages\django\bin\django-admin.py startproject sqlserveradmin

Then create an application using this command:

manage.py startapp mydb

To add the application to the project. You must change INSTALLED_APPS in settings.py to include the app.

INSTALLED_APPS = (    
   'django.contrib.auth',     
   'django.contrib.contenttypes',    
   'django.contrib.sessions',     
   'django.contrib.sites',     
   'mydb',
)

Setting the database

Now you have the project in place. You need to modify settings.py to point to your DB. Change these fields to be

DATABASE_ENGINE = 'sql_server.pyodbc' 
DATABASE_ODBC_DRIVER = 'SQL Native Client'
DATABASE_NAME = 'db_name' 
DATABASE_USER = 'webapp' 
DATABASE_PASSWORD = 'sikrit' 
DATABASE_HOST = r'test_server\SQLEXPRESS'

Generating DB models

On the console, run the following command

manage.py inspectdb > inspected_models.py

This will generate Python models for your tables. Unfortunately, they are not arranged. To arrange them, use the script that I posted to http://www.djangosnippets.org/snippets/1203/.

rearrange_models.py inspected_models.py models.py

Use the new models.py to replace the old one in the folder mydb.

Installing the admin

To install the admin, you need to add the following line to INSTALLED_APPS in settings.py. It should look like

INSTALLED_APPS = (
    'django.contrib.auth',    
    'django.contrib.contenttypes',     
    'django.contrib.sessions',    
    'django.contrib.sites',     
    'django.contrib.admin', 
    'mydb',
)

then run this command to add the tables necessary for the admin to work.

manage.py syncdb

It will ask you to add a superuser. This superuser will allow you to access the admin.

You need to tell Django what models that you need to be viewed in the admin. For our case, we want to view all the tables. I will use a little bit of Python magic to add all the models to be viewed in the admin. Put the following code in a file called admin.py in your application folder.

from django.contrib import admin 
from django.db.models import Model 
import models # This is your models' module 
for m in dir(models):
   class_ = getattr(models, m, None)
   if class_ and isinstance(class_, type) and issubclass(class_,Model):
      admin.site.register(class_)

Congratulations. We are done.

What you can do more

  1. Change the templates used to view the UI.
  2. Customize the names of the models and fields, for example you can supply correct pluralization.
  3. For enums, you can specify the available choices, and the admin will view it as a drop down.
  4. You can add more users to the admin, and specify their permissions.
  5. And a lot more. See http://docs.djangoproject.com/en/dev/

I hope this wets your appetite for learning Django.

Screenshots

The home page. I had to remove everything that reveals the application. As you can see, Django admin also stores a history of changes.

1

This is the view for students. I customized it a little bit to view students' emails, but you can customize it more.

2

This is the form to change the student. The "UserId" is a foreign key to the "Users" table. Django admin is smart enough to grab them for your. If you want to add a new user, you can add it by clicking the small "+" sign beside the drop down list.

3

This shows the screen for users in the "Auth" application (which is added by Django itself). This shows a more advanced listing: you can select the columns that will be displayed, with sorting. You can add filters (on the right), and you can add search also.

4

This is the form for editing users.

5

Python Arabic Book

Jun 23 2008

A friend told me that there is an arabic book for Python, which is a translation of "Byte of Python", one of the most popular Python books. This is a good step toward popularizing Python, specially among children. Please go and download the arabic version. Read it, and tell the author about any enhancements that the book needs.

Stupid Type Systems - Innecessary Casting in C#

May 13 2008

If List<> inherits IList<>, and MyIdentity inherits IIdentity, then why the heck the C# compiler cannot cast from List<MyIdentity> to IList<IIdentity>?????? It is the same, the compiler is not smart enough to know this. So to satisfy the compiler - acting as a good compiler slave - I have to write this

IList<IIdentity> returnList = new List<IIdentity>();
// A variable name cannot be stupider
foreach (var item in originalList)
{
    returnList.Add(item);
}
return returnList;

And people were wondering why dynamic languages takes less lines of code.

C#.Next, What it must NOT include?

Feb 12 2008

There are some talks lately about the next version of C#, and what is should add. People has gone too far asking for new features, most notably the dynamic lookup

static void Main(string[] args)
{
    dynamic
    {
        object myDynamicObject = GetDynamicObject();
        myDynamicObject.SomeMethod();
        // call a method
        myDynamicObject.someString = "value";
        // Set a field
        myDynamicObject[0] = 25;
        // Access an indexer
    }
}

The above looks ridiculous to me. C# is statically-typed language, and it should stay like this. Trying to add dynamic typing to it - even if it was optional - is stupid. For me, a language must establish a few basic concept and stick to them, not try to satisfy every one out there. C# has incomplete features. These should be completed first before adding any new features. Take type inference as an example. You cannot return anonymous types from methods, because you don't know their names. It should allow something like

public anonymous MyMethod(string email)
{
    return from user in Users
    where user.Email = email
    select new { FullName = user.FirstName + " " + user.LastName };
}

It can also add named parameters, and default values for them, just like Python. It can be supported indirectly now by passing anonymous types, but complete support for them would be better. Dynamic languages are not just about dynamic lookup, and supporting broken dynamic lookup - just like the above mentioned example - is going to be, really, broken. What about dynamically adding new methods? What about using making a class as a proxy, say, for a web service? What about object-specific members - members which exists for a specific member? It can get very complex, and the only known way to allow dynamic features is to make your language dynamic. The beauty of the CLR, is allowing different languages to run and inter-operate. Unfortunately, it was designed with static typing in mind, which appears clearly in the BCL design. I wish the DLR team has these limitations in mind so they address them better, specially the importance of providing libraries which fits the dynamic languages way of doing things. One language is not enough, and one language which tries to fit all purposes is going to be very complex - just like C++. The solution is learning different languages and using the appropriate one when it fits, and integrating them when you need to.

RSS feeds for youtube

Jan 12 2008

Everyone uses youtube to see his favorite videos. Most of the time, you need to subscribe to a certain user’s feed using RSS. Unfortunately, the 'Subscribe' buttons subscribes using their own notification system, and you have to be a member to subscribe.

Contrary to popular beliefs, youtube provides RSS feeds, but they are hidden. You can subscribe to a user's feed using

http://gdata.youtube.com/feeds/base/users/mtayseer/uploads

And to a certain tag using

http://gdata.youtube.com/feeds/base/videos/-/egypt

The question is: why did they hide this?