Python Posts

166 posts tagged with python

Here's the most convoluted “Hello World!” script I could come up with (in response to this). I don't know if it works. I've proven it correct, but I haven't tested it.

I promise my production code is (marginally) more readable this this…

I've been working on a large-ish Javascript project lately (still in stealth mode). One that could be described as a Javascript application. And since I've been up to my elbows in Javascript, I have found my contempt for the language waning. Not entirely, of course – I'm still a Python fan-boy. But enough for me to knock out a 3D(ish) spinning texture-mapped globe, using nothing but Javascript and HTML, as a way of honing my JS skills.

Without further ado, I present you with a 3D Javascript Globe.

This effect is off course a hack in the sense that it produces a novel result, but it still only makes use of standard browser capabilities.

One of the few geometry formulas that I have committed to memory is the relationship of the points on a circle and the radius: x^2 + y^2 = r^2 continue reading…

I have an idea germinating that involves using fuzzy logic, something I only had a passing knowledge of. After reading the Wikipedia articles and Googling for it I'm more informed but still haven't found a perfect resource.

The only Python code I have found was informative, but it was from way back in 1990. If there are any Pythonistas who know of a more up-to-date fuzzy logic module, or fuzzy logic resource in general, please let me know!

I was debugging an issue with our Django app at work today; an admin.py file wasn't being picked up, and nothing was appearing in the admin pages. It turned that an ImportError was being thrown in the admin.py and Django was interpreting this as the file not existing.

I'm assuming that the reason for this is that Django uses __import__ to import the module, and catches ImportError's if the admin.py doesn't exist. The trouble with this is that if admin.py does exist, and throws an ImportError of its own, Django will also interpret that as a missing admin.py – which can be misleading. continue reading…

Today I discovered a rather cool project via Reddit. MiniLight is a “minimal global illumination renderer” (it draws 3D scenes) with implementations in various languages.

The Cornell Box, rendered with Minilight

The Python version is a lot slower than the compiled languages – which is to be expected, number crunching like this is not Python's forte. All the same, I had a go at optimizing it. Using similar techniques I wrote about in a previous blog post I reduced the run-time for the test scene from 61.4 seconds to 53.2 seconds. Hardly stellar, and it's not going to change the comparisons, but it was an interesting exercise. continue reading…

I figured I would write-up some of the features of Django Techblog, the blogging application I wrote to power this site. It does most of what you would expect from a blogging app, but there are a few features that it does differently. The main difference is extended markup, but there are a couple of other features that worthy of note:

The code for Django-techblog is licensed under my politeware license, which means you can use it for any purpose you see fit, but I would appreciate a thank you! It shouldn't be too difficult to set-up if you have worked with Django, but I'd be happy to help if you experience any problems with it. See the Google Code page for the SVN url:

http://code.google.com/p/djangotechblog/

I figured I'd start a series of Python challenges in my blog. Whenever I've posted questions such as these in the past, the discussion that follows is always very entertaining!

A discussion arose today at work about the best way to flatten a tuple consisting of values and other tuples (which may be nested arbitrarily). So the tuple (1, (1, 2, (1, 2, 3), 3)) would become (1, 1, 2, 1, 2, 3, 3). Now there was actually code implemented which was functional, efficient and easy to read – but where is the fun in that?

I figured I could re-write it in a more terse manner, and here is what I came up with:

This flatten function would actually work in the context of our app, but I would never use it in production code. My challenge today, is to tell me why this code should never be used!

As a Godless heathen, I have no scripture from which to guide me in my day-to-day life, and I must look for meaning elsewhere. I believe I have found that meaning in the most unlikely of places; the Python shell. If you enter import this in to the Python interpreter, it will reveal to you an ancient wisdom in the guise of a collection guidelines for the Python language.

I have studied these words for many years and have come to believe that they are in fact guidelines for living a fulfilling and successful life. continue reading…

I've been toying with optimizing the caching on my blog recently – for my own interest (this humble blog doesn't get all that much traffic). All the same, any speed improvements will only mean snappier page-loads and greater capacity to handle a slashdotting, or similar.

I discovered that Nginx has a memcached module that can serve pages directly from memcached without touching the file-system, or a downstream web-app. Which makes for very fast response times. To get it working, you need to set a cache key named with the url of the page, and the value to the HTML. Alas, this means that it would not work with Django's caching mechanism due to the way Django caches unique pages based on the contents of the request header. continue reading…

In my last post I introduced extended-markup, which is the light-weight markup system used to generate posts in Django-Techblog. I'll cover a few other things it can do in this post.

When the Post model is saved to the database, the extended markup is parsed in to a structure that is basically a dictionary containing a list of chunks, and associated variables. The order that the chunks appear in each section is the same as the order they appear in the markup, unless a chunk variable called priority is defined. This causes the chunks to be sorted in descending order of priority, chunks without a priority value are assigned a default of 100.

Here is an example of two chunks with a priority value assigned: continue reading…