Archive

Posts Tagged ‘python’

Getting started with Twisted on Windows

March 10, 2012 Leave a comment

Twisted is an excellent networking framework for python applications, and like most of python, your code is easily portable from one platform to another. As an event-driven framework, you can handle very high loads without creating a large number of threads, so it scales quite nicely. Twisted comes preinstalled on recent versions of Mac OS X, and on Linux distros you can get it with your system’s package manager. Windows is a bit more work, so I’ll go through the steps.

The prerequisites:

Python – http://python.org/download
Setuptools – http://pypi.python.org/pypi/setuptools (needed to install the Zope Interface egg)
Zope Interface – http://pypi.python.org/pypi/zope.interface#download (used by twisted for its service interfaces)
Twisted – http://twistedmatrix.com

1. Install Python from http://python.org/download/. There are Windows installers aplenty.
2. Install setuptools. There is an installer for 32-bit windows. For 64-bit, just download the ez_setup.py locally, and then run “c:\Python27\python.exe c:\Users\me\Downloads\ez_setup.py”
3. Zope is a little tricky. When you installed setuptools, it added the easy_install.exe under your python\scripts directory. To install the python .egg file containing zope, you need to get the URL for the zope egg you want to download and pass that as a parameter to easy_install.exe, like this:

c:\Python27\Scripts\easy_install.exe http://pypi.python.org/packages/2.4/z/zope.interface/zope.interface-3.8.0-py2.4-win32.egg

If all goes well, you should be ready to install and run twisted.

4. Install twisted from http://twistedmatrix.com

At this point, you should be able to fire up python and run the sample from the Twisted home page:

from twisted.internet import protocol, reactor

class Echo(protocol.Protocol):
    def dataReceived(self, data):
        self.transport.write(data)

class EchoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return Echo()

reactor.listenTCP(1234, EchoFactory())
reactor.run()

I hope this helps you getting started with the excellent twisted network framework for your python apps.

Categories: python, twisted Tags: , ,

If C# is so awesome, why use anything else?

December 29, 2011 20 comments

Anyone who knows me professionally knows I work in C# most of the time. I think it’s a great language that’s been well designed and made very portable by way of being an open language specification. A lot of people look at C# and say, that’s just Java with some Microsoft-extensions. Sort of, since it’s framework (.NET) ships with quite a few libraries that interoperate well with Windows, although the C# language itself doesn’t have anything to do with Windows, and runs on Linux, OS X, Android, iOS, and so on. In my opinion, Java has stagnated over the years, while C# has been evolving with generics (which Java followed), lambdas (Java finally gets them years later), anonymous types, partial method and class declarations, language integrated query (LINQ), dynamic runtime integration, and soon a simplified asynchronous programming model with await and async that will allow the runtime to deal with the gory details of async programming rather than forcing the programmer to understand and properly implement callbacks and cleanup. Java isn’t catching up fast, so Scala is filling the gaps, but C# remains years ahead.

Every year, my family looks at me funny when I they give me new books. That’s right, I’m a geek that reads computer books. Most of these books are not on C#, but on JavaScript, Python, Haskell, and I even keep an old PERL book on my shelf. What is all this other stuff?

JavaScript – it’s pretty rare these days that other developers would say, “why would you ever want to write JavaScript?” It’s a ubiquitous language amongst web browsers, and it’s pretty rare that anyone can write much of a browser-based application at all without it. Besides, the latest trend is to write a “language X to JavaScript converter” and what good would that be if I didn’t know JavaScript and wasn’t willing to learn language X? There have always been some nice server-side implementations, like Spidermonkey and newer V8, powering trendy applications like MongoDB and Node.js. Until I started down the Python path, whenever I needed extensibility, I would embed Spidermonkey for some JavaScript fun.

Python – in the realm of C#, a lot of people are uncomfortable mixing in Python. They don’t like the idea of losing compile-time checks and worry about needing a myriad of Python frameworks to solve any sizable development tasks. However, Python is an excellent tool for large and small projects alike, and IronPython take the Python language and gives it access to the full .NET framework. In the last few years, I’ve felt constrained if I didn’t have a layer of extensibility that IronPython can add to CLR applications. Python scripts let you treat code as data, meaning you can store it, transmit it, and change it at runtime. Python gives you a new way to move the problem around, solve it at a different time in your overall solution. It’s a great piece of the toolbox.

I remember spending weeks building business rules engines so non-programmers could add some logic to enterprise applications. These engines would use reflection and Lightweight Code Generation (LCG) and a clumsy UI where end users would select data objects and operators and build expression statements. IronPython uses LCG, is highly optimized, and gives you a general purpose scripting language with access to CLR objects. Most end users prefer the ability to write an expression in script rather than fumble with the type of UI needed to build an expression tree. This is just scratching the surface of the Python language, but at the very least, it’s a great tool anywhere you want to offer runtime extensibility.

Haskell is pure functional programming – no state, just functions. I used F# a bit for professional work just to learn it, but it allows you to fall back into the OOP line of thinking. Haskell makes you take a fully functional approach. I recommend every developer that’s looking to expand their approach to problem solving to spend some time with it.

What about that PERL 5 book? Well, I don’t use that, to be honest. I did once upon a time, but I really do avoid PERL at all costs. Maybe one day, I’ll pick it back up.

Categories: C#, IronPython Tags: , ,