Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Saturday, March 15, 2008

Running the twisted deamon (twistd) on Windows

Twisted is a networking library for python, which I find really good. I've only just started playing with it, and ran into some problems when trying to get it to run as a daemon under windows (using twistd - the twisted daemonizer). The information about this on the web seemed scarce, so I decided to post my findings.

When you install twisted, it comes with a special command-shell. But after looking at it, I found that it wasn't all that special afterall. It's just a batch-script that sets up a regular shell for you, all ready to start twistd.

As I do my programming in Eclipse, I wanted to start the server directly from inside the IDE. The way I did this, was by adding a .bat file to my project. The contents of the .bat file needs to be something like this:

set PATH=%PATH%;c:\python25;c:\python25\scripts
set PATHEXT=%PATHEXT%;.py;.pyc;.pyw;.pyo
set PYTHONPATH=%PYTHONPATH%;c:\your_source_dir
cd c:\your_source_dir
twistd -noy your_server_daemon.tac
pause

You have to change three things here, the two your_source_dir and your_server_daemon.tac and also the python directory.

The first line adds the python executable to your path. In my case this is c:\python25 - you will need to change this depending on your python install. It also adds the scripts directory where your twistd.py should reside.

Next we set the PATHEXT variable, this lets you run .py files (and .pyc and so on) directly from the command line without invoking the python interpreter manually (the way it works on *nix).

After that, we add the directory (or directories) where our source files reside to the PYTHONPATH. Without this, you will get an "ImportError: No module named blabla", or "Failed to load application: No module named blabla" for all your imports. (You can also add to the pythonpath from inside your program, but I find that it's a bit cleaner this way).

Next we change into our source directory, simple as that.

After that, it's simply a matter of starting the twisted daemon (twistd) with our configuration (.tac) file. -noy means that we start the specified .tac and log to stdout (look up twistd in the twisted docs for more info). To stop the server, simply press Ctrl+C.

The pause at the end is only there so that the window doesn't close immediately after the program ends, but waits for a keypress.

More on twisted here:

Sunday, February 24, 2008

Free alternative to Flash / ActionScript

Flash is a technology developed by Adobe, which is quite popular for creating dynamic content for web pages. It lets you make animations, games and interactive applications.

To make these apps, most people use Adobes own development environment. However, their newest Flash Professional CS3 sets you back almost $700.

If you don't have that kind of money (or you are a cheapskate) you can still create Flash apps using the free haXe language. haXe is an open-source compiler, that lets you compile to not only Flash, but also Javascript and Neko thus letting you create both the client-side and the server-side of Ajax applications!

Sound interesting? Well it is! I'll point you in the direction of haXe:

You might also want a good editor to go with that. You can for example use FlashDevelop, which is free and open-source:

Saturday, February 23, 2008

Choosing a Python development environment

If you are going to be doing any kind of serious development, you will most likely want an integrated development environment (an IDE). This will help you organize your projects and source files, and give you additional tools like automatic code completion and syntax highlighting.

Many people get by using just their favourite text-editor, be it a simple one like notepad, or more complex editors like Emacs or UltraEdit. And there is nothing wrong with that, I guess - but me I prefer to use a tool where everything is integrated.

For Python development, there are quite a few free alternatives. Idle is one of them, and comes bundled with Python when you download it. It's a no-fuzz editor and shell, and is great for those who like simplicity. It's fast and does everything you really need.

If you want to step up a bit, there is Pype, or Python Programmers' Editor which is an editor targeted specifically at Python development. It has lots of nice features which helps you speed up development.

None of these are what I would call a real IDE though. For that, look to Eclipse. This is my favourite IDE for any programming language, not only Python. It was originally intended for Java, and is written in Java, and so can be a bit more than you need and also might be a bit sluggish at times. It does however have all the features you will ever need, and more.

Eclipse is fully modular, meaning that you can add support for different languages as you go along. Java, C / C++, Python, Perl, PHP - whatever you throw at it, you can be almost certain that there is a module for Eclipse that supports your language.

Also Eclipse is free and open-source, which is one of the reasons I like it so much.

There are of course also quite a few commercial IDE's for Python. I haven't tried any of them, but some of them (like the Wing IDE) look quite good. You might want to look for some evaluation versions, if you want to fork out some cash for a professional solution (though Eclipse is VERY professional).

Check out the links:

3D Programming

Are you thinking about writing the next Crysis? Well, you most likely won't. At least not by yourself. But if you want to write smaller games or virtual worlds in 3D, there are some good quality free libraries that you can use.

For me, this boils down to a battle between a cuddly pandabear and a trollish ogre, represented by two excellent free libraries called Panda3D and Ogre3D.

Panda3D is a library written by Disney, and maintained by Carnegie Mellon University's Entertainment Technology Center - while Ogre3D is a true open source project.

For simple usage, they are almost equal - but in my opinion Panda is easier to use while Ogre has the most features and cutting edge technology.

While both are written and optimized in C / C++, Panda is intended for programming in Python. If you are a Python user this is a good thing, as everything is very "pythonic". You can also get Python bindings for Ogre, but since this is more of an afterthought you can clearly see it's legacy from C++.

Why would I program a 3D application in Python anyway, you might ask. Isn't that really slow? Well, not really. Since all the 3D functions are already programmed in C / C++ there is no real slowdown in the rendering process. Your Python code is only "glue". And with Python, you get a lot more done in less time - so it makes sense if you are programming on your own or in a small team.

So in the battle of Panda vs. Ogre, who will win? If you are a C / C++ programmer, the choice is clear - Ogre is the most suited for you. If you, like me, like Python, the answer is more "it depends". I would go for Ogre because of it's more advanced features, but you might like the Panda better because it is very pythonic and a little simpler to use.

Check them out yourself here:

There is also Irrlicht, which is an impressive one-man effort. It also has a Python wrapper called Pyrr. It's real easy to start using, all you need is right there. However, it is somewhat lacking in features compared to Ogre and Panda, and seems to be geared at writing FPS games. You can check it out here:

Friday, February 22, 2008

Thinking about learning a programming language?

Have you ever had an idea for a dream application, but never knew how to program it?

A lot of people starting to program use Java or C / C++. Java is considered to be easy, because it handles things like garbage collection for you, and there is no such thing as pointers - which is something that confuses new programmers a lot in C / C++.

Most end up using C / C++ because it is the "industry standard" - but this does not automagically mean that this is the best language. If you are thinking about learning to program, I suggest you check out the Python language.

Python is a "scripting language", but is extremely powerful. It can be used with ease for programming web-pages for example in the place of PHP or Perl, but also for full blown applications with a nice user interface and all the bells and whistles.

The saying among Python programmers is that it makes you 10 times more productive than if you were programming in C++, because you have to write a lot less code. This is an important point if you are programming by yourself or in a small team, because your program is ready for market in a much shorter time - and you also have a lot less code to maintain.

By using wxWidgets for Python, you can have a multi-platform (Linux / Windows / Mac) application with a GUI ready in a few hours, if not minutes.

Python really lets you do a lot of things that C / C++ doesn't, because the philosophy is that you, the programmer, should be in charge - and decide for yourself the best way to accomplish a solution to your problem.

I recommend you check it out if you are interested in programming:

If you want a graphical GUI builder, check out wxGlade, which can generate wxWidgets source code for Python and C++