Programming

Python-like tuple unpacking for PHP

Python provides a neat way for functions to return multiple arguments via “tuple unpacking”. For example:

def blah:
  return ('one', 'two')

rval_1, rval_2 = blah()

The same can be done in PHP relatively easily via the list construct:

function blah()
{
  return array('one', 'two');
}

list($rval_1, $rval_2) = blah();

jQuery: the new defacto Javascript web framework

News from a couple days ago: both Microsoft and Nokia are now including the jQuery Javascript framework as part of their development kits. That is: jQuery will be part of Microsoft’s ASP.NET AJAX framework and be available for use in applications written for ASP.NET; and jQuery will also be distributed on millions of Nokia phones.

Defacto standards, I believe, are a good way to inform the development of real standards. Standards developed the other way around, at least in the tech industry, have had a habit of taking a very long time to reach end consumers… for example, how many decades has it taken for your average web user to gain access to a fully CSS2-compliant web browser? How many more decades will it take for OASIS’s OpenDocument format to supplant Microsoft Word and its *.doc files?

Hopefully, this is the beginning of a path that will lead to jQuery’s inclusion into the Javascript language, as well as initiatives that will improve jQuery’s performance.

I like the fact that Microsoft and Nokia are not trying to reinvent the wheel, and roll their own Javascript frameworks. Sun did this with Java Server Faces. A frequent lament with JSF is that it’s nearly impossible to customize any of the widgets. There is too much complex, custom Javascript, and the adoption of the frameworks used makes figuring out how to work with them difficult.

Also, as others have noted, this is the first time Microsoft itself is distributing an open-source project with one of their products. A sign of things to come?

Python 2.5 alpha released

Python 2.5’s first alpha has been released. From a quick look at the What’s New in Python 2.5 document, what I thought was interesting…

Computer Organization and Design For More Practice Problem 2.28

I’m using Computer Organization and Design, 3rd ed (David Patterson and John Hennessy) for an electrical engineering course (that I have to take to graduate) in computer systems architecture. Assigned as homework, from the For More Practice section, is problem 2.28:

PHP and passing non-variables by reference

In PHP 5.0.5, they’ve now made it a requirement that things that are passed by reference must now explicitly be a variable. You would think this kind of behavior is obvious, but apparently it’s been allowed for all versions of PHP previous. Appararently without even warnings. You’ll get an error:

Fatal error: Only variables can be passed by reference ...

So you may think, where is this useful? Consider something short and concise like:

$only_element_we_care_about = array_pop(explode($seperator, $string));

You cannot do this now. The return value of a function is not a “variable” and cannot be passed by reference; a temporary variable needs to be used instead:

$tempory_variable = explode($seperator, $string); $only_element_we_care_about = array_pop($temporary_variable);

Yes, there are other ways to do the above, but that isn’t the point. The fix is not difficult, but it is a total complete pain to go back to legacy code and fix things like this.

Remind me to find another web programming language.

Looking at Python web development frameworks

I’ve a lot of interactive websites I’ve been wanting to do lately. After learning Python earlier this year, I’m wanting to write all these projects in Python versus something conventional like PHP. I’ve come to the conclusion that web development with Python sucks; it’s no wonder it’s not being used very much. There are too many frameworks, many doing similar things in different ways, or doing totally dissimilar things in totally different ways. I’ve been spending the last week reviewing several different frameworks and deciding which ones I thought were best.

Today's g++/C++ funny: ofstream's open constructor

So, I’m working on a project, and have some code like this:

string blah="somefile"; ofstream fp;

fp.open(blah);

And it doesn’t work. Apparently, what you need to do when using ofstream’s open method:

fp.open(blah.c_str());

Yes, it looks like what it is. Apparently g++’s iostreams library (gcc 3.2) does not support things that have been part of the C++ standard for years–like C++ STL strings… You have to convert back to a C string.

Last I checked this was 2005, not 1999. You’d have thought someone would have overloaded this by now… I’ve only checked this in g++; will check with Intel’s C++ compiler and Microsoft Visual Studio later, though I’m not sure whether to expect better. What didn’t help me to find this out, of course, is g++’s totally wonderful (sarcasm) compiler error messages…

Splitting a Sequence into Subsequences with Python

I’m writing a load balancing algorithm for this cluster I’m working on, and have a bunch of things in a Python list sequence. So came the issue, how to divide the list into sublists for each node in the cluster to compute? I came up with the algorithm:


def split_seq_stride(l, n):
  """Splits a sequence l into a list of subsequences containing at least n
  elements each, not preserving order. The first few subsequences may contain
  n+1 elements, containing the last few elements. (Algorithm is good for load
  balancing)"""
  r=[]
  k=len(l)/n
  [r.append(l[i::k]) for i in range(k)]
  return r

which behaves like:

>>> l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> print split_seq_stride(l, 3)
[[1, 4, 7, 10], [2, 5, 8], [3, 6, 9]]

Assuming each task is an equal amount of work, this algorithm is a good load balancing algorithm. it prevents any node from having to do any less significant work than the others.

Probably useful for someone: what about splitting up a sequence in-order, with the last subsequence containing fewer elements? This code segment does just that:


def split_seq(l, n):
  """Splits a sequence l into a list of subsequences containing at most n
  elements each, preserving order. The last subsequence may contain less
  than n elements."""
  r=[]
  [r.append(l[s:s+n]) for s in range(0, len(l), n)]
  return r

which behaves like:

>>> l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> print split_seq(l, 3)
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

Thanks to yason and others on EFnet’s #python for pointers.

The semantics of a fork, followed by setsid, and another fork

So I came across a piece of code that looked like this:

if (fork() != 0) exit(0); setsid(); if (fork() != 0) exit(0);

What each individual system call can easily be looked up and understood (well, maybe not setsid), but what the entire block of code is doing is not clear at all

…and I really can’t say I understand the mechanics of it all to explain myself. But this python code segment’s comments does a good job of what the entire block of code is trying to do–daemonize a process completely.

Projects

Various projects I’ve written some kind of useful snippet of code, helper program, and etc for.

Syndicate content