Archive for the ‘python’ category

Easy access to log files


Here is the scoop: I need to provide access to multiple log files spread across multiple directories of multiple machines. For example – the JBoss log files and nohup.out, ATG log files, and so on – so that the testers can see what happened on server side when something looks fishy on front end.

One option would be to write a document that contails IP’s of the machines, create user accounts, make sure that they have just enough access rights to see the files but not disturb anything else …

Other option is to leverage the already installed Python across all the servers. I wish it was Ruby, but one cannot have everything.

On each machine, I created directory – e.g. ~/webaccess and made symbolic links to all log files or log directories required. Then I placed this python script into the directory:


import SimpleHTTPServer
import SocketServer

# minimal web server.  serves files relative to the
# current directory.

PORT = 9999

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

'': 'application/octet-stream', # Default
'.out': 'text/plain',
'.log': 'text/plain'
httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT

and started it as

nohup ./ &

From this moment on, all files linked (actually all files in subtree of server directory) are accessible by going to http://SERVERNAME:9999/ – which shows directory listing for linked files.

On one of the machine, I have placed additional file – index.html that contained links to all other server names.

Thanks to this page for inspiration.

Microsoft is getting more dynamic


at least as the programming language languages are concerned. One of the things that will be presented on the MIX07 is Dynamic Language Runtime, a new platform suitable to host high level, dynamic languages such as Python and Ruby. For Python, there is already pretty decent .NET implementation – namely IronPython. What is coming now is IronPython 2.0 based on the DLR as well as – yesss – IronRuby.

It is great that this is happening. In Java world, JRuby is very close to being full implementation of the language. The speed of the JRuby is still not quite in the proximity of the C implementation, but the large selling point of JRuby is its capability of deploying Ruby application (or even a Rails application) inside Tomcat or any other Java Web container. This removes largest obstacle of using Ruby in enterprise environment – namely being “new” and not approved platform. When the .NET runtime will be able to host Ruby code, acceptance in enterprise space will be even higher.

It is interesting how many news related to Ruby and Rails I see lately. One explanation is a new car owner syndrom. When you buy a new car, you start notice that many more people must have done the same thing because there are suddenly lots of cars of same make and model around you. In reality – there is about the same number of your car’s instances in circulation as before, just your attention is now focused and you start to see them, where before they went unnoticed.

But the other explanation is that maybe, maybe there is really something changing :-). Would not it be great ?

Couple of things I really like about Ruby


I guess it is too early to write any more systematic opinion about the language you know less than 10 days, but after reading the Programming Ruby 2nd edition (thank you, Safari Books) I have found out that I actually do like couple of Ruby features a lot. Your mileage may vary – what speaks here is my Java/C#/C++/C/Python/Perl background and bias. Here there are my favorite Ruby-isms (so far) in no particular order:

– expressive power of the language – few lines can take you a long way
– syntax that is nice and readable, with curly brackets allowed (but not required) and without names like __something__
– classes and OOP are organic part of the language, not a hack on top of it – everything is a class
– no need to explicitly declare self in each function or bless references
– reflection which is so easy that it is almost funny – 1.class.methods – how many lines would that be in Java ? 🙂
– access level control that works, not underscore naming hacks
– text processing power of Perl, without most of cryptic Perl features
– right middle point between language style dictatorship of Python and anarchy of Perl
– ability to send messages, not call methods 🙂 – reminds me of Smalltalk and Objective-C
– almost everything I loved about Lisp is available again …
– multiple inheritance functionality without multiple inheritance mess – mixin’s are fantastic
– gem installer – it just works
– documentation and unit tests are part of the system, not optional afterthought

I have not really reached critical mass of Ruby lines written or read and have not looked into Rails framework everybody is crazy about. As a strange coincidence, on today’s Mac User’s group meeting Tobias Lütke did a presentation on their product Shopify, written in – surprise surprise – Ruby on Rails. Once again – I wish I had more time …

Will C# 3.0 become an uglier version of Python ?


It is now about 1 year since official release of C# 2.0 and .NET framework 2.0. The next version of C# language – the C# 3.0 seems to get more and more attention – in blogs, official channels and MSDN. There is lot of excitement and big expectations – but I have nagging feeling that the general direction of the C# development is not necessary an improvement.

The main new features of the C# 3.0 language are implicitly typed variables and anonymous types, extension methods, lambda expressions, and LINQ – possibility of using SQL-ish dialect inside the C#. With exception of the last, all of them must sound very familiar to every Python programmer. All variables in Python are implicitly typed, we can extend classes to much larger degree than extension methods and lambda expressions were always integral part of Python (inherited from Lisp and Scheme).

For several reasons, I believe that this is wrong. Strong typing and type safety is very important for every programming language that is suitable for building large and complicated systems – enterprise level applications. Allowing dynamic typing may be tempting, but what it does is moving discovery of errors from compilation time to runtime. There is nothing wrong with dynamic typing – Python, Perl and few others are great languages. Albeit there are large and complex systems written in Python (e.g. Zope), I think that “classical” strongly typed languages (Java, C#) still does result in more robust systems.

Adding functional constructs such as lambda expressions and extension methods basically creates a hybrid functional-OO language, sort of a cat-dog. For seasoned Lisp programmer, even Python’s support for functional programming is not good enough – and Python is internally much more dynamic language than C#: functions, classes and modules are first class objects (just compare the simplicity of event handling in Python with events and delegates). What value will it add to embedd some limited functional constructs to fairly traditional OO language as C# is ?

The project LINQ and mixing up SQL with C# in same source file is from my point of view worst idea of all C# 3.0 new features. It may sound great and be very helpful for small projects, but for enterprise size applications it makes so much easier to create complete mess in application architecture… Same thing happened few years ago with visual GUI building tools – people started to place lots of business logic into form event handlers – this is no problem for 3-4 screen mini app, but disaster for large and complex system, where you do want to have proper layers, roles and responsibilities and at minimum some form of MVC pattern. Again, there was nothing wrong with UI designers, it is just that they made doing incorrect thing so much easier …

The problem of “impedance mismatch” between world of objects and world or relational tables is real and none of the solutions we have today is perfect and applicable everywhere: code generators (of either database or object layer), “active” datasets, business object frameworks, mappers and ORM layers. What they do give us, though, is modularization and separation of concerns which is very important for code maintainability and readability. Mixing everything together inside same class just does not sound right.

Very important for success of programming language is simplicity and support for writing understandable and maintainable code. Somehow, both Java and C# seem to loose the original simple elegance of their origin. Adding generics in C# 2.0 and Java 5 was for me strange mixture of blessing and curse – yes, generics do help solve some type of problems in a very elegant way, but quite often lead to unexpected issues and complications elsewhere. Our recent adventures with CSLA framework are a very good illustration of this – but that’s topic for another entry.

No programming language is perfect for every purpose. This is why we have so many of them. So if you want to have very dynamic language with functional programming support in .NET environment why not to use Python directly ? The .NET implementation IronPython is amazingly fast, compiles to CLR and works just fine. If you want very solid language for enterprise development on Microsoft operating system, there is absolutely nothing wrong with C# 2.0. There is no need to mix them together …

Why do I suggest that C# would become uglier version of Python ? Because I believe it will look uglier to almost everybody. For somebody with strong traditional OO background and lots of strongly-typed languages history (some ex-Java, ex-ex-C++ programmer like myself), “var i” sounds too much like Visual Basic and extending class by method injection and not by inheritance is close to blasphemy. And for Python enthusiast, who enjoys dynamic features and scripting power of Python (like myself ;-)) all that stuff with data types and private/protected hide-and-seek games is just unnecessary ballast – not speaking about those terrible curly brackets, ouch …

Besides, if you *really* must do all the things above, how about using “Object i” instead “var i”, injecting methods to classes by using Spring.NET (which can, btw do much cooler things thanks to AOP and dynamic proxies) and trying out something like iBatis.NET for making sure that your C# code and SQL code coexist peacefully ?