Thiiink: Ideas, Imagination, and Innovation in GIS


She Sells C# by the C-shore
April 2, 2009, 8:04 pm
Filed under: Python

Another great get away to recharge and read and read and read and read and … yup, always lots to stay on top of in our ever changing dynamic world.  Had some real fun taking in the “new” features of C# 3.0 and the .NET framework 3.5, some really nice things included there like LINQ and lambda but other than that, just more CLR chatter and hype around how the new VS makes coding sooo much easier.  Probably a good thing considering how user expectations continue to heighten more rapidly than quality code can be created and pushed into the cloud.

Most would know that I’m into Python fairly heavy so I figured to bridge some of the “new” .NET features like LINQ and lambda back into their corresponding cohorts inside Python.  Although not exactly the same thing, it does for my purposes cover off the majority of items that we handle in our code development, the LINQ as is pertains to SQL is YAORM (yet another object relational modeler).  In Python we’ve had for quite some time a set of wonderful modules that provide this exact capability, granted they are not part of the core development but none the less they are beneficial packages if you’re doing anything more than one or two simple SQL statements.  The modules include some favorites like SQL Object and SQLAlchemy – if you like the idea of being efficient for your clients then you want to look at these and some of the plug-ins too.

Onto lamdba’s, well this isn’t a new thing for Python peeps, been around forever.  Of course you always need to ask yourself “is there ever a chance that this function will be used elsewhere?” which normally equates to “Yes” but if not then lambda away!  Here is one of my favorite ways to use a lambda, it is in the key kwarg for sort or sorted.  This approach I find really handy for being able to capture the sorting logic directly inline in the code which for me really helps in readability!

import random
import operator

lowerBound, upperBound = 0, 1000
values = [tuple([random.randint(lowerBound, upperBound) for i in range(10)]) for i in range(1000)]

sorted(values, key=lambda v: sum(v[:2])**2 + sum(v[2:5]) + reduce(operator.mul, v[5:], 1.))

What this code does is generate a list of tuples containing values and then the sort is based on a combination (like a weighting) of the tuple components.  Way cool and super fast.

Well enough on that for now, nice to see that C# is beginning to catch-up with where Python has been for quite some time…YATTOS (Yet another testament to open source).


No Comments Yet so far
Leave a comment



Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>