Why I Like Python

For the past 8 years or so, I've been very much involved with programming using the PHP scripting language. It is a powerful scripting language that suits building websites very well. PHP has a huge set of useful built-in functions, and more recent versions support object-oriented programming. I first started teaching myself PHP when I got tired of having to build each and every web page on my site manually. I hated having to change dozens of web pages just because I added a new link to my navigation. All sort of reasons like this prompted me to investigate PHP. Little did I know then that this language would occupy so much of my time in the future.

I rapidly learned that PHP offered much more than just allowing me to update one part of my website to change all pages. I started tinkering with all aspects of what PHP offered, and I'm still learning about it. After many years of searching, I finally found a programming language that was easy, fast, and efficient for my needs.

Through the years, I continued to develop various applications using PHP. I attempted to write my own forum/bulletin board software while I was still in high school. If I may say so myself, the forum really had some awesome concepts behind it. But my problem was that I lost interest too fast. I also built a very large application that reduced a 1.2GB MS Access database down to less than 15MB using PHP and MySQL. The new application offered many enhancements over the previous system. For one thing, it was much faster. Second, it allowed multiple simultaneous users to modify the database. Three, so far it has lasted more than 3 years, compared to the 1 year maximum that the MS Access solution always seemed to hit before it crashed.

Using PHP, I helped revolutionize the way one of the companies I work for developed websites. I built a simple in-house web framework that supposedly reduced development time by allowing us to forget about the mundane details involved in virtually every website and just get to the developing. In a matter of two weeks (with a full class load and another job), I managed to write an e-commerce solution for the same company using PHP.

Basically, PHP has treated me well over the years. But this post is not supposed to be about PHP. If that's the case, why have I rambled about PHP this whole time, you ask? Well, it's mostly to demonstrate that I have a lot of experience with the language. I have a pretty good feel for what it's capable of and how I can accomplish most anything I need.

With all of that in mind, I've encountered my frustrations with PHP. They may seem petty and moot to most people, but they have turned out to be the determining factor in what scripting language I prefer. Here is a short list of things I now despise about PHP:

  • dollar signs ($) to signify variables -- while this is a useful feature, it becomes quite bothersome when you're programming all day long (at least it does for me). I'll get to why later.
  • using an actual arrow (->) to access attributes -- most other modern programming languages simply use a period (.) for this functionality. I'll comment more on this and why it frustrates me later as well.
  • lack of true object-oriented constructs -- in other object-oriented languages, like Java, if you have a string and you want to determine its length, you call the length() method of that string. In PHP, you call a function such as strlen($var). This sort of behavior plagues the language.
  • too many unnecessary keystrokes -- as I mentioned before, all mutable variables are preceded by a dollar sign ($). That is 2 keystrokes (shift and 4) every time you want to refer to a variable, wheres most languages nowadays have none). Likewise, accessing attributes of objects in PHP uses an arrow (->), which is three keystrokes (minus, shift, and .). Most other object-oriented languages only require a period (one keystroke) for such functionality. The main reason I make such a big deal out of the number of keystrokes is simple. The more keystrokes a program requires, the more likely you are to have bugs. The fewer keystrokes a program requires, the less likely it is that your program will be broken. It boils down to maintainability. Also associated with the number of keystrokes is the pure laziness within me and most other programmers.

These frustrations have been bothering me for several years now. I continued using PHP mostly because it's so widely supported, but also because I could not find a suitable replacement for it. I investigated a few others, but they apparently didn't have a great influence on me right now because I don't remember any names.

When the whole Ruby on Rails bandwagon was rolling through town, I decided to hop on to see what all of the hubbub was about. I started studying the Ruby script language, and I found that it had some really neat things about it. It uses a more solid approach to object-oriented programming, which I really liked. I also noticed that it employs some intriguing structures for accomplishing things in ways I've never seen before. Despite these things, Ruby still didn't seem like a viable replacement for my PHP. It didn't come up to snuff in performance in many cases, so I essentially abandoned it.

For at least a year now, I've been interested in learning Python. I've heard a lot about it over the years, but I just never seemed to make the time to actually sit down and study it. That is, not until about the beginning of August of 2007. After I made my decision that Ruby and Ruby on Rails weren't quite up to par for my needs, I stumbled upon the Django Project, which is a web framework similar to Ruby on Rails, only built using Python.

I decided this was my chance to actually sit down and learn a little about this "Python" so I could see what it had to offer. I mostly used Django as my portal to Python. As I started learning Django, I became more and more familiar with the way Python works and how I work with Python.

At some point in time, I decided that I actually liked Python, and my wife let me buy some really cool books to help me learn it. By the beginning of October 2007, I had convinced my supervisor at work to let me start building websites using Django instead of our home-grown PHP framework.

And here comes a story. This is the main reason I blabbered about my experience with PHP so much at the start of this article. Again, after all these years, I feel very confident that I can do just about anything I want efficiently and elegantly with PHP.

Back in October of 2006 (after using PHP for some 7 years), I was asked to write a PHP script to parse some log files and output various bits of information in a certain format. After maybe a week, I had a script that did the job fairly well. Most of the time it worked, but there were occasions when it didn't and I had to fix it. The script turned out to be 365 lines of code with very few comments scattered throughout. It's also a maintenance nightmare, even for me.

In October of 2007, I rewrote that same script in Python. After only a couple days, the script seemed to be perfect. It did its job, and it did it well. With comments for just about every single line of code, the Python version of the script took up a mere 118 lines of code. Take out the comments and it is 56 lines of code. The script is several times more understandable and maintainable than its PHP counterpart. I also believe that it is much more efficient at doing its task. Keep in mind that I had only been using Python for about 2 months at this point in time.

It's been through various experiences like the log parser that I have decided I prefer Python over PHP. Obviously, I'm not quite as comfortable with it as I am with PHP, but I don't feel too far behind. Now, less than 6 months after deciding that we'd use Django at work, I don't think my supervisor could be happier. Building a typical website with our PHP framework takes between 1 week and a couple months. Thanks to Python and Django, most of our websites can be "ready" within just a few hours. That time assumes that the website's design itself is ready for content to be put into it and also that the client does not require custom-designed applications.

Python and Django have helped revolutionize the way we do things at work, and I can hardly stop thinking about it. Python fixes nearly all of the frustrations I had with PHP. The frustrations it doesn't take care of are worth the sacrifice. Python is capable of object-oriented programming. It uses a period (.) to access object attributes. Variables are not preceded by some arbitrary symbol.

Also, the fact that Python code can be compiled to bytecode (like Java) is enormously beneficial. Each and every time a PHP script is executed, the PHP interpreter must parse the code. With Python, the first time a script is executed after an edit, the program is compiled to bytecode and subsequent executions are faster. That is because the bytecode is processed directly by the Python Virtual Machine (as opposed to being compiled to bytecode _each_ time and then executed). Python also offers a vast amount of standard library functions that I would really appreciate having in PHP. But from now on (at least for the foreseeable future), I will try to do all of my scripting in Python and leave PHP for the special cases.

Wireless Networking With SuSE Linux Enterprise Desktop 10

Note: This tutorial is a continuation of yesterday's tutorial about installing SuSE Linux Enterprise Desktop 10 on my HP Pavilion dv8000. I may or may not refer to steps that I took during installation, so if you are confused, you might want to check out the previous article.

The process of installing and enabling a wireless adapter will vary greatly from machine to machine. Some lucky folks have wireless adapters that come with official Linux drivers. For the rest of us, we usually have a Broadcom-compatible adapter. In order to use a Broadcom device, I use a program called ndiswrapper, which basically takes the drivers for the devices to function with Windows and wraps them in such a manner that Linux can use. Since I have the 64-bit version of SuSE Linux Enterprise Desktop (SLED) 10, I need to get a 64-bit driver in order for my wireless to function properly. These 64-bit drivers took me a while to get ahold of the first time I got my wireless working (on SuSE Linux 10.1), but I still have them in my archives, so I should be fully prepared to get my wireless working. In this article I assume that you are going to use ndiswrapper to install drivers for a Broadcom device. So let's get started.

Install Ndiswrapper

First, make sure that you have ndiswrapper installed on your system. You can install it by entering YaST. In KDE, click the K menu (the little green chameleon in the bottom left), go to System > YaST (Administrator Settings). You will be asked to enter the root password, which you set during installation. Once you've done that, you will see the YaST Control Center, which is a very powerful set of tools and utilities that greatly ease the configuration and management of SLED. Click on the Software category on the left to show a list of software management options (if it's not already displayed). Click on the Software Management module.

Once loaded, you will see an interface which is very similar to what you would see during the expert package selection while installing SLED. Make sure your Filter (in the top left) is set to Search, and enter ndiswrapper in the search box. The search will return a few different results for ndiswrapper. The first result, ndiswrapper by itself, should be sufficient for most of us. When you check the box by ndiswrapper, you will see a warning informing you that ndiswrapper-based network are not officially supported by Novell. Just click OK to dismiss this warning.

Now you should be ready to install ndiswrapper. Click the Accept button in the bottom right. You will be asked to confirm the installation of ndiswrapper; click Continue. If your installation media is not still inserted, YaST will request the disc which contains the ndiswrapper packages. Insert the disc and click OK. In my case, two packages were installed. It may or may not differ for you.

As soon as the packages are done installing, your configuration settings are saved once again, and you will be asked if you want to install or remove more packages. Click No. At this point, ndiswrapper should be installed on your system, and you may dismiss the YaST Control Center.

Determine Your Wireless Adapter Make/Model

This step is absolutely necessary because if you install the wrong drivers, there is a chance (small as it may be) that your wireless adapter will be damaged. So let's ask Linux how our wireless adapter identifies itself. To do this, log into your SLED and open a Terminal or Konsole. On KDE, you can use the third button (a monitor with a black screen and > on it) on the menu panel at the bottom of the screen, or you can also click the "K" menu (same place as a regular start menu in Windows), go to System > Terminal > Konsole (Terminal Program). I am not exactly sure where this item is located with GNOME, but it might be under the System menu.

Once you have opened a terminal window of some sort, you must switch to a root user environment:

$ su -

You will then be asked for the root password, which you set during installation. Enter that password and type

# lspci

This command lists all of your PCI devices, according to the man pages, but you will see most if not all of your devices, PCI or otherwise, listed here. You'll notice that there is probably quite a list of devices. You may be interested in what your computer has in it, but since you're looking specifically for your wireless adapter, try one of the following commands

lspci | grep Broadcom
lspci | grep Wireless

The | after lspci will pipe the output of lspci to a useful and powerful program called grep. In this case, grep just looks for any lines that contain either the word Broadcom or Wireless. If you don't get any results from either of the two commands above, try to think of other keywords that might be used to identify a wireless adapter. My laptop returns the following:

# lspci | grep Broadcom
06:02.0 Network controller: Broadcom Corporation Dell Wireless 1470 DualBand WLAN (rev 02)

When you find the wireless adapter, pay attention to the numbers in front of it (06:02.0 on my laptop). With those numbers, you can get the information you need to find the right drivers for your particular wireless adapter. Enter the following command, substituting my device numbers with yours:

# lspci -n | grep 06:02.0
06:02.0 Class 0280: 14e4:4319 (rev 02)

This command gives you the wireless adapter's numeric ID; mine is 14e4:4319.

Download Your Device Drivers

Now that you know your device's numeric ID, you can go to the ndiswrapper wiki, which has a list of numeric IDs and the drivers that are known to work with that device. Look for your wireless adapter on the list of devices. I would recommend using your browser's search or find on page function to locate your device by the numeric ID that you just found.

I'll leave the retrieval of your device drivers up to you.

Install The Wireless Drivers

Most device drivers will come in an archive of some sort. Mine came in a RAR file. Extract your drivers to the directory of your choice--maybe something like ~/wireless. You can use the archive utility provided by SLED to extract your files. It functions very similar to WinZip, WinRAR, and other popular archive clients. By the way, the ~ in a directory listing refers to the current user's home directory (/home/user, for example).

Now, go back to the root terminal that you used to determine what kind of adapter you have. Navigate to the directory where you extracted your drivers and list the contents of the directory, looking for any *.inf files:

# cd ~/wireless
# ls

Ndiswrapper will use an INF file to know how it is supposed to install the driver. My INF file is called bcmwl5.inf. Now for the actual installation of the drivers:

# ndiswrapper -i bcmwl5.inf
Installing bcmwl5
Forcing parameter IBSSGMode|0 to IBSSGMode|2

Now check to make sure that the driver is there and that it recognizes your hardware:

# ndiswrapper -l
Installed drivers:
nbcmwl5          driver installed

Ooops!!! It doesn't recognize that my hardware is actually there. If you see 'driver installed, hardware present' then you should be good to go. You may proceed to the next step. However, if you have the same problem as me, you either have the wrong drivers or ndiswrapper installed the drivers improperly. This problem took forever to track down when I was first trying to get my wireless to work. Remember the numeric ID that you found earlier? Check this out:

# cd /etc/ndiswrapper/bcmwl5
# ls
14E4:4318.5.conf  bcmwl5.inf  bcmwl564.sys

Wait a second! Remember how my numeric ID was 14E4:4319? Why is there a listing for 14E4:4318.5? To solve this problem, I am just going to make a symlink (a shortcut) to 14E4:4318.5.conf and call it 14E4:4319.5.conf:

# ln -s 14E4:4318.5.conf 14E4:4319.5.conf

Now when I run the command to see if my hardware is recognized, I get this:

# ndiswrapper -l
Installed drivers:
bcmwl5          driver installed, hardware present

Hurray!! It says 'hardware present' in there!!! That means that the drivers are working and that my device can be used!

Enable Your Wireless Device

With ndiswrapper recognizing your wireless adapter, you can now enable it and start wirelessing your life away:

# modprobe ndiswrapper

There have been times when this particular step will lock up my machine and I have to do a hard reset, but most times it will work fine.

Connect to a Wireless Network

This part also gave me issues for a long time when I first installed my wireless drivers on SuSE Linux 10.1. I was able to connect to the wireless access points provided by my apartment complex, but I could not for the life of me connect to my own wireless router. Hopefully you don't encounter the same problem.

To see what access points you have available to you, check out the KNetworkManager applet in your system tray (next to the clock). I have 7 possible access points listed in the menu, including my encrypted router. When I clicked on my network, it asked me for my passphrase and connected immediately. Nice! That's definitely one plus for SLED over SuSE Linux 10.1!!

I am actually amazed at how easy it was to get my wireless working the second time around. Hopefully your wireless adapter installation was as painless as mine with the help of this guide.