Python


  • Thursday, February 25, 2010 - 12:31

    In my work I ended up needing to know the minimum volume bounding box (MVBB) of a given of points in 3d. This is also called the oriented bounding box (OBB). Finding the axis-aligned bounding box (AABB) is a simple task, however it can have a volume much larger than needed. Come to find out, finding the minimum volume bounding box is a very difficult problem with several techniques for finding it. However, I didn't find any useful code for finding the MVBB.

  • Wednesday, February 17, 2010 - 15:36

     So I needed to bind a lambda function to an object. I've been writing code that needs to run faster than it currently does and I've traced some slow-downs to repeated comparisons that are really wasted and things don't change once the object is instantiated. For instance below is an example to show you how one might code something (note this is just an example, not actual code)

  • Monday, April 6, 2009 - 13:16

  • Sunday, February 15, 2009 - 15:18

    The more I work with python the cooler it becomes, especially the more I let my mind think of way out there things to do with the tools python provides. In my work I frequently find my self writing code in this order:

  • Friday, February 13, 2009 - 15:22

    So one of the coolest things I just found out about python is that it will let you chain comparisons, so 1 < a < 10 works. Even 1 < a < 10 < b < 25, and so on, you can even get really crazy and try a < 5 > b, wich is a funky way of saying a < 5 and b < 5, pretty cool huh!

  • Monday, February 9, 2009 - 18:25

    So today I used my first function closure in Python. I had always wondered why/how someone would use them, but today after reading an article earlier this morning I ran across a place to use them while coding this evening. Here we go:

  • Monday, November 24, 2008 - 11:40

    Python has a really cool feature called decorators. Which allow you to wrap functions around other functions in a clean way. The classic example would be the @trace decorator which would let us do this:

    @trace
    def sayHello(name):
       print 'Hello %s!' % name
    

    If we define the trace decorator like this:

  • Thursday, November 6, 2008 - 16:57

    Okay, so things everyone should know about dictionaries, they are in the python manual but I still didn't know them until today. First off creating a dictionary in the code (ie hardcoded)

    d = dict(one=2, two=3)

    This is a simpler way of doing things, and it sure beats having to rember all your quotes when doing:

    d = {'one': 2, 'two': 3}

    Granted the second way looks cooler! But we have to make compromises. Another cool thing, is taking a list of keys and list of values and turning them into a dictionary:

  • Wednesday, October 8, 2008 - 23:31

    So I wanted to swap to objects, and I prefer to write short clean code, especially to carry out simple processes. The idea was that I have two objects, in this case treeA and treeB and I wanted treeA to be the deeper of the two trees. Now the long way would look something like this:

    if treeA.depth < treeB.depth:
        tree = treeA
        treeA = treeB
        treeB = tree

    Which is a little ugly, 4 lines to reorder something, so there neat trick is that if you want to swap two objects a and b simply use:

    a,b = b,a

    So the above code becomes: