Coding: Bring the Noise

%26lt;0011 Em

Archive for the ‘Python’ Category

A binary serialization mixin for Python: More Metaclasses (Part 4)

leave a comment »

In our last episode, we had a concise field wrapper that let us defer object instantiation so we could call the initializer (with all the args and kwargs specified at class declaration) each time we instantiated the class we were building.  We still didn’t have anything related to serialization.  Today we finalize the metaclass and take stock of our final solution.  Spoilers: there’s a better solution than Metaclasses, but the investigation’s worth it.

Like the previous post, we’ll be focusing on the third point:

  1. Many values fit in one or two bytes at most – booleans fit in a bit, color channels fit in 1 byte. Any solution needs to allow bit-width specification.
  2. However, I shouldn’t need to specify the bit-width of every value. Reasonable defaults should exist that can be dropped in where appropriate.
  3. Serialization shouldn’t get in the way, and I shouldn’t have to give it much thought when I write my classes.
  4. Of course, I should be able to drop into the details when a class requires custom serialization.
  5. Performance overhead, both time and space, should be as low as possible. Per-instance overhead is almost entirely unacceptable.
  6. Serializable classes should be nestable, and the performance overheads for nested classes should be as low as possible.

Read the rest of this entry »

Advertisements

Written by delwinna

May 28, 2013 at 4:45 am

A binary serialization mixin for Python: Metaclasses (Part 3)

leave a comment »

Like part 2, this post will focus on points three and five:

  1. Many values fit in one or two bytes at most – booleans fit in a bit, color channels fit in 1 byte. Any solution needs to allow bit-width specification.
  2. However, I shouldn’t need to specify the bit-width of every value. Reasonable defaults should exist that can be dropped in where appropriate.
  3. Serialization shouldn’t get in the way, and I shouldn’t have to give it much thought when I write my classes.
  4. Of course, I should be able to drop into the details when a class requires custom serialization.
  5. Performance overhead, both time and space, should be as low as possible. Per-instance overhead is almost entirely unacceptable.
  6. Serializable classes should be nestable, and the performance overheads for nested classes should be as low as possible.

This post we’re only going to create a metaclass that leverages some of the magic of the Field class – next post we tackle serialization-specific work in the metaclass.

Read the rest of this entry »

Written by delwinna

May 19, 2013 at 8:48 pm

A binary serialization mixin for Python: Descriptor protocol (Part 2)

leave a comment »

This post we’re going to focus on the third point of our requirements. To recap, here they are:

  1. Many values fit in one or two bytes at most – booleans fit in a bit, color channels fit in 1 byte. Any solution needs to allow bit-width specification.
  2. However, I shouldn’t need to specify the bit-width of every value. Reasonable defaults should exist that can be dropped in where appropriate.
  3. Serialization shouldn’t get in the way, and I shouldn’t have to give it much thought when I write my classes.
  4. Of course, I should be able to drop into the details when a class requires custom serialization.
  5. Performance overhead, both time and space, should be as low as possible. Per-instance overhead is almost entirely unacceptable.
  6. Serializable classes should be nestable, and the performance overheads for nested classes should be as low as possible.

We’ll first take a quick look at the code the last post finished with – hopefully the problems we found can help design an interface that more closely fits the third point, while keeping the fifth (performance) in mind.

Read the rest of this entry »

Written by delwinna

May 19, 2013 at 8:12 am

Posted in Descriptors, Python

A binary serialization mixin for Python: Requirements (Part 1)

leave a comment »

I’ve been working on a project that requires serializing objects.  I could use Google’s protobuf, but this seemed like a great opportunity to see what I could do with python.  Of course, less painful serialization isn’t a new concept.

I have some preferences for how this serialization fits in with my existing code, and a few of them have been notably complex:

  1. Many values fit in one or two bytes at most – booleans fit in a bit, color channels fit in 1 byte.  Any solution needs to allow bit-width specification.
  2. However, I shouldn’t need to specify the bit-width of every value.  Reasonable defaults should exist that can be dropped in where appropriate.
  3. Serialization shouldn’t get in the way, and I shouldn’t have to give it much thought when I write my classes.
  4. Of course, I should be able to drop into the details when a class requires custom serialization.
  5. Performance overhead, both time and space, should be as low as possible.  Per-instance overhead is almost entirely unacceptable.
  6. Serializable classes should be nestable, and the performance overheads for nested classes should be as low as possible.

Read the rest of this entry »

Written by delwinna

May 18, 2013 at 10:32 am

2D random terrain: iterative diamond-square algorithm

leave a comment »

A year and a half ago, I created a simple application that allowed one to specify a folder of column-wise data, which axes to plot, a color scheme, and then with those parameters ran over the files and created an avi file from the data.  This was for my university’s CSUMS program, and while I was excited to release it to my peers, no one really understood what it did, or why they should care.

I needed some examples.  Since I wanted to start working on terrain generation and I was on a minecraft kick at the time, I read up on the Diamond-Square Algorithm and went with an iterative version of that.  The following is a mostly-cleaned-up version of that code.  I’m posting it today because this post reminded me of it, and I want to adapt it to C#/XNA and then implement my alternating directions implicit diffusion algorithm, so that I can see just how fast I can run that in a shader.

Implementation note: this wraps edges, so that you can tile the output seamlessly.

Here’s the code.   (You’ll need numpy and matplotlib.)

Read the rest of this entry »

Written by delwinna

April 30, 2012 at 10:35 pm

Posted in Python

Digit counting in Python

leave a comment »

Question: what’s the fastest way to count the number of digits (base 10) in an integer (integral type, could be long) in python?

This came up when I was working on a ProjectEuler problem.  There are three options I came up with, and we can rule the first out immediately:

  1. Divide (as an integer) the number by ten and keep a counter of how many times we divided it, until the number is 0.
  2. Use log(n, 10) and properly account for floating point black magic
  3. Convert to a string, get the length

Read the rest of this entry »

Written by delwinna

January 27, 2012 at 2:40 am

Posted in Puzzles, Python

Old code: “playing with boids” OR “this would be a cool music visualizer”

leave a comment »

As I was sorting through my old code, I found this “gem” from my visual python experiments last year in college.  The code and instructions are at the end of this post.  I’d recommend reading the rest of the post before watching the video, otherwise it might not make sense.

boids

Playing with boids (you can jump to 0:45)

Attractor jump at 2:05 (which is pretty freakin cool)

I particularly like how they respond to the attractor (blue ball) once they’ve settled into an orbit- it’s like an amoeba reaching out and surrounding a smaller organism and then devouring it all at once.

There’s some music in here from this playlist.

Read the rest of this entry »

Written by delwinna

January 22, 2012 at 4:03 am

Posted in Projects, Python