See also: Heapify

Pages: 1 2 3 4 5 6

Object pools (Python)

A very common problem in software engineering is optimising out the problem of construction and destruction of objects. I don't necessarily mean objects exclusively in the context of object orientated programming. I mean objects as in data objects, which could be a large array or a struct and so on.

If you don't need objects for a long time it does seem silly to construct an object, initialise it, then destroy it and discard it. Especially as memory allocation can be expensive.

The most common approach to minimising the impact of this is object pools. An object pool is a collection of pre-constructed objects, sometimes even pre-initialised objects. When a program wants to use an object for a short period of time they can check-out an object, configure it to their needs (if necessary), use the object and then check it back into the pool.

...

Read more

There are no comments on this post.

Brute forcing the touring knight problem

The touring knight problem is a maths problem that takes the movement of a knight in chess and attempts to find a path around a two dimensional board such that every single square is visited exactly once.

There are more sofisicated solutions but as you might have imagined, brute forcing is the most naive approach to solving this problem.

By trying every possible combination of legal move from your current position you can follow through every legal path and check whether you've completed the board or not.

...

Read more

There are no comments on this post.

Prototyping in Python

Our good friend Randall was talking about how he liked prototype inheritance such as that in JavaScript. He enjoyed how you can add, modify and remove methods from JavaScript classes and have them apply to all object instances.

Amazed as he was, he's now forcing me at gun point to write a post about it!

The first thing you have to understand about Python classes is that all they are is a collection of references to members. Methods are just function pointers that are referenced by the class, not embedded or copied into the class. This is why all methods require that the first parameter be self, the current instance. Instances themselves contain the real state and reference the class. When you invoke a method on the instance it looks up what function to call.

...

Read more

There are no comments on this post.

Ternary operator in Python

People are always wondering if Python has a ternary operator. The answer is yes but no.

There is no recognised ternary operator. However it is very simple to construct one out of logic operations.

Python short circuits conditional evaluations. This means that given a series of logic operations, if it can plainly see that a certain branch of that logic is unreachable or of no consequence it wont even attempt to execute it.

...

Read more

There is 1 comment on this post.

Fetching network statistics (Python and Linux)

A little bit of code that loads network device statistics from any operating system that supports procfs.

Just a small snippet. Originally used in some network monitoring code that I wrote that graphed your network traffic.

I would include that here too but that's a little messy.

...

Read more

There is 1 comment on this post.

Setting the desktop wallpaper programmatically (C# Snippet)

Here's a little C# program I made that gets the filename of the currently set desktop wallpaper and also can be used to set the desktop wallpaper.

Example usage: SetDesktopWallpaper.exe C:\MyNewWallpaper.jpg

Just something I made for my sister who has Windows 7 Starter edition and lacks the presentation controls. Add it to your 'Send To' folder and just select the file you want to set the wallpaper as and go.

...

Read more

There are 3 comments on this post.

Ignoring certain content-types with urllib (Python Snippet)

This is a little piece of code I wrote to intercept and disregard a http or https request from urllib.urlopen if the Content-Type header on the response is not within a list of accepted content types.

I'm sure somebody might find a use for this.

This snippet creates a customer URLopener and then overrides the open_http and open_https methods, checks for MIME type and halts the request if the response is of a MIME type you do not accept.

...

Read more

There are no comments on this post.

Shut Up Woman, Get On My Horse

I recently found a rather odd but amusing flash animation by Weebl at http://shutupwomangetonmyhorse.com/.

Enjoy

...

Read more

There are no comments on this post.

Bash Script: Start irssi (or any process) if not already running via a cronjob

I know there are a thousand of these available online. This is one I just wrote and wanted to make it a thousand and one. This script will start irssi if there is no irssi process available.

Save the following script somewhere (example /home/kay/bin/irssi_init.sh) and add the following to your crontab:

*/1 * * * * /home/kay/bin/irssi_init.sh >> ~/irssi_init.log

...

Read more

There are no comments on this post.

Designing a user-orientated permission system

System permissions are important. Defining what people can and can't do with your application is a significant part of security.

There are two perspectives I tend to care about with permissioning. The first is user-orientated and the second is data-orientated. In this article I will talk about designing a user-orientated permission system.

For the purposes of this post a permission will be considered a boolean value that represents whether a person can or can't perform an operation. In other systems you might go as far as to consider the extent to which they have permission which ends up working like a priority based permissiong system. This is only really useful in my opinion if you've an operation two people can perform at once and you wish to provide a fine grained hints to the system as to who should have the operation performed first. It's something to consider but usually unnecessary and out of the scope of this article.

...

Read more

There is 1 comment on this post.

Pages: 1 2 3 4 5 6

RSS
Powered by Debian, Guinness, and excessive quantities of caffeine and sugar.