I finally tried Python

296 days ago
tags:

python
I’ve been branching out lately and trying to learn Python. I got a Python book a while ago, and just didn’t take the time to work with it at all. However, the Fedora Project uses Python extensively for just about everything, from the build system, to web-hosting to smolt (A really cool HW tracker in Fedora), to Yum and the system-config-* tools, so I thought there must be something great in here somewhere and I just haven’t found it yet.

I normally write a lot of PHP and Perl. I use bash for the quick and dirty also. After working through a few hours of Python, I was amazed. The biggest plus, it’s readable. There’s nothing worse than having to maintain someone else’s Perl code. It’s just not nice, even if the person was a wonderful Perl coder, I normally find it takes me quite a while to figure out exactly how/where something is occurring. With Python, at least thus far, it’s readable, it requires indenting, and it’s easy. Now that I have worked through Beginning Python, and written a few administration scripts, I feel better. I can see a real future for my usage of Python as an administrative tool.

I’m also enjoying it having me think about problems from an OO approach. In my normal administrative world, problems are isolated and often times solved by a quick script and series of scripts that run procedurally and not in OO format. The OO format hopefully will offer a bit more code reuse, or at least help me recall some knowledge forgotten in CS classes of yesteryear.



Programming with Configuration Files

308 days ago
tags: , , ,

Recently I was working on developing an application that required a configuration file to setup. I considered several options for the file, keep it in the native language (PHP), make it plain-text or use XML. I didn’t like any of the options because the configuration has to be approved by certain stakeholders of the data at various stages throughout the life of the application.

XML is too hard to read for management-types. Plain text wasn’t all that easy to handle from a code perspective. I could have used a native language configuration file that read more like plain text (mostly comments, few directives, etc), but still was not liking it.

Then I remembered that when I played with Ruby on Rails it used something called YAML, and thought I’d give that go. It didn’t take too long at all to get the hang of writing the syntax for YAML. I then needed to find a way to process it with PHP. I found a few.

I started with the top entry on a Google search for PHP YAML. I found spyc, which is a very simple YAML class for PHP. I also found Syck, which is apparently more widely used and ported to several languages (although Ruby looks to be the primary focus). But I decided to download spyc, which appeared to geared toward PHP, gave it a whirl and instantly had what I needed. I liked it so much, I packaged it up for Fedora. I should probably also mention that I looked at JSON, but found YAML to be more complete and easier to read for what I needed to do. JSON is cool though in that it is a subset of YAML for the most part and works nicely with Python.