Latest Blogs

Poem: The "Hi"

We met in time,
Strangers to crime,
A love story did commence.

The feelings climb;
We're in our prime;
We gain a seventh sense.

There goes the time.
Is this a crime?
Now the feeling's tense.

I fall; no climb;
Remorse in prime;
My conscience in defense.

Poem: Zero Sum

Burn the universe
Burn the world
All the atoms
Become unfurled

Grab the energy
Grab the world
All the people
In balls they've curled

Run the people
Run the world
All the power
Is silence swirled

Python: Format Datetimes, Stripping Leading Zeros

Python's strftime format directives do their job of creating an understandable representation of datetimes. But they don't care about being pretty. One little thing that bothers me is leading zeros in things like hours, months, and days. Some versions of Python (2.x) on some OSes (Linux and sometimes Windows) allow the use of a dash after the percent sign (%-I) to strip leading zeros, but I don't find that very dependable.

This function will detect directives with the dash flag and strip any leading zeros, completely platform-independent. It shouldn't interfere with any other platform-dependent modifiers, but it does assume directives begin with a percent sign; flags are digits, _, or ^ (the last two being GNU C flags); and identifiers are single alpha characters.

The function could be changed to run all output through strftime, thus allowing any directives (I'm thinking of ones which include the GNU C E and O modifiers), but I wanted to prematurely optimize and reduce calls to strftime.

Making ctypes Structures beautiful

Quick link to code: https://github.com/theY4Kman/pysmx/blob/master/smx/newstruct.py

Whenever I need to read in structured binary data in Python, I'm always frustrated at the disconnect between ctypes and struct. On the one hand, ctypes Structures provide a mechanism for naming bunches of binary data, but on the other hand, it doesn't provide an easy way to read in that data. The recommended method of reading in structured binary data is with struct.unpack, but unpack doesn't know or care about names, leaving extra work for us simple-minded programmers.

As a flagrant Django enthusiast, I want something like its Models:

class MyModel(models.Model):
published = models.BooleanField()
value = models.IntegerField()

After all, if data can be modeled like that in C, well, goddammit, it should be that easy in Python. With that in mind, I made it possible. I call it newstruct, and while the source may be a bit ugly now, using it is painless:

from ctypes import sizeof
from newstruct import *

class DataHeader(Struct):
address = cf_long()
typ = cf_byte()
tag = cf_short()

with open('mydata', 'rb') as fp:
header = DataHeader(fp.read(sizeof(DataHeader)))
print header.address, header.typ, header.tag

It's important to note that the attributes on DataHeader begin with "cf" (for "ctype field"). So if you want to use a ctypes c_long as a field in your Struct, you'd assign it cf_long(). The reason for the difference is Struct needs a custom field type to retain the ordering of fields.

I took my cues from Django's Field class, which increments a creation_counter variable on the Field class every time a new Field is instantiated. It then saves the current value of creation_counter to the new Field instance. Grabbing an ordered list of fields is a cinch sorting by creation_counter:

ordered_fields = sorted(fields, key=lambda field: field.creation_counter)

That's my module. Now, how can I make it better?

Idea Transference

It might be possible to design a learning system which can understand two brains' thoughts and memories, and, just like a version control system, merge them. In the best case, it would produce the least amount of duplication and stronger, but fewer connections. In combination with the ability to reproduce completely a brain state, this would allow for easy temporary idea transference.

We would have to understand the machinery and uses of all the different types of glial cells and their effects on neurons and the brain's neural networks. That's no easy feat, but certainly possible. It'll just take time.

Of course, this learning system will be a bitch to get just right. But that's okay, because it's a learning system, and we have forever. And then of course reproducing a brain state is an extremely fucking complex and difficult task. But certainly possible.

It's all possible in my lifetime. Let's figure it out.

« Previous 1 2 3 4 5 Next »