Pages: 1 2 3

OS X Screen capture from Python/PyObjC

Looking through the unanswered Python questions on StackOverflow, I found one that seemed interesting.. "Python Get Screen Pixel Value in OS X" - how to access screen pixel values, without the overhead of calling the screencapture command, then loading the resulting image.

After a bit of searching, the best supported way of grabbing a screenshot is provided by the CoreGraphics API, part of Quartz, specifically CGWindowListCreateImage.

Since CoreGraphics is a C-based API, the code map almost directly to Python function calls. It's also simplified a bit, because PyObjC handles most of the memory-management (when the wrapping Python object goes out of scope, the underlying object is freed)

...

Read more

There are no comments on this post.

Investigating memory leaks in Python

Memory leaks are difficult to trace down. A nice description of finding one is "A server memory leak", a story about tracing a memory leak in a Django application.

I'm trying to work out why a server process is slowly using more and more memory. The server parses a stream of text, and shoves stuff into a database. It's written in Python, and about the only external code it uses is SQLAlchemy

I found a combination of Meliae and objgraph useful. Pympler also seems useful (the tracker class particularly). Dowser also looks useful, and might have made things a bit easier..

...

Read more

There are 3 comments on this post.

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.

Python Challenge Thoughts

If you didn't read my previous post, you'll want to check that out first: The Purely Functional Python Brainfuck Challenge.

So, today was the first day I really invested some thought and time into the challenge. I decided that before just diving into code I'd spend some time reading up on functional programming in python, and learn a bit.

The first thing I read through was the official python functional programming page which you can find here. This had a bunch of useful information regarding functional programming tools in python, but didn't really have any examples of actual functional programs.

...

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.

Serving Static Content With Django

A question that is frequently asked by new Django programmers is: "How can I serve static content (css, images, javascript) with the Django development server?". This article is my attempt to answer that question by demonstrating the best practices way to do so.

Why Doesn't Django Serve Static Content Automatically?

Well, Django (and python in general) is built around the idea that it is better to be explicit than implicit. This concept means that you may need to write more code in order to do something, but that by doing it that way, you preserve code clarity and reduce complexity.

...

Read more

There are 3 comments 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.

Populating Default ManyToMany Field Values in Django

At work, I'm the lead developer of a rather large, complex web application which interacts with many different technologies (Asterisk, Freeswitch, Cisco routers, python, XML-RPC, JSON, Django--to name a few). A few days ago, while implementing a ban system, I bumped into an interesting problem that was not trivial to find a solution to. So, here it is :)

Background

The web application I'm developing is a private portal which allows users to manage teleconference lines real time. Since all of our telephony services are free of charge, we often get callers onto certain teleconference lines who want to abuse services (think of those trolls on the internet, except over the phone). As you can probably imagine, without strict regulation & technology in place, telephone trolls could cause huge problems for normal users.

...

Read more

There is 1 comment on this post.

Pages: 1 2 3

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