Programming

Python 2.5 alpha released

Tags: •  • 

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…


Eclipse configuration

Tags:

URLs for the update manager

  • CDT: http://download.eclipse.org/tools/cdt/releases/eclipse3.1
  • phpEclipse: http://phpeclipse.sourceforge.net/update/releases/
  • Eclipse Web Tools Platform: http://download.eclipse.org/webtools/updates/
  • PyDEV: http://pydev.sf.net/updates/

Computer Organization and Design For More Practice Problem 2.28

Tags: •  • 

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

Tags: •  • 

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:

$onlyelementwecareabout = 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:

$temporyvariable = explode($seperator, $string); $onlyelementwecareabout = arraypop($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

Tags: • 

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

Tags: •  •  • 

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

Tags: •  •  • 

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.


Syndicate content