See also: Heapify

Pages: 1 2 3

Basic XML Parsing with Python and LXML

Recently I've been developing an API using python and Django for work, which uses XML responses to speak to clients. One of my goals for the client was to be able to easily parse the XML responses that the server sends, so that I could appropriately handle errors.

Fortunately, python has many tools for building and parsing XML. During my research, I tested several options, but found that the well supported library LXML was a perfect match for what I needed. Unfortunately, I had a hard time figuring this out, as examples to just parse XML content was lacking in the official tutorial, and there were no good resources online with code samples.

So let's take a quick peek at a sample XML document, then we'll analyze some simple LXML code to see how it works. Of course, before you can run any of these code samples, you'll need to download and install LXML (there are packages available on most linux systems already).

...

Read more

There are no 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.

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.

Auto Generate Forms With Django's ModelForm

In this short article, we'll analyze a better way (in some cases) to create forms for your Django models.

If you've ever worked with Django forms, then you know that there is a lot of repetitive code involved in the process of writing a form to create your model. Take, for instance, the following model, which represents a physical server (somewhere):

from django.db import models
 
class Server(models.Model):
    """
    This class represents a physical server.
    """
    hostname = models.CharField('Server Name',
        help_text = 'Hostname of the server.',
        max_length = 50
    )
    ip = models.IPAddressField('Server IP Address',
        help_text = 'Public IP of the server.',
        unique = True
    )
    disk_space = models.IntegerField('Disk Space on Server',
        help_text = 'Total disk space in MB.'
    )
    ram = models.IntegerField('RAM on Server',
        help_text = 'Total RAM in MB.'
    )
    cpu = models.IntegerField('Processing Power',
        help_text = 'Total Processing Power in MHz.'
    )
 
    def __unicode__(self):
        """
        Make the model human readable.
        """
        return self.hostname

...

Read more

There are 4 comments on this post.

tvnamer v2 development

I've been working on a new version of tvnamer, the utility I discussed in "Automatically rename your torrent'd TV episodes"

It's a pretty much complete rewrite. Some of the new features include:

  • Configuration files. In tvnamer v1 you could only customise things like the output filename format by modifying the Python code directly. Now you can create a JSON file at ~/.tvnamer.json which changes any of the default options.
  • Support for multi-episode files, such as scrubs.s01e23e24.avi
  • Support for anime filenames, such as [Shinsen-Subs] Beet - 19 [24DAB497].mkv
  • Much better UTF-8 filename handling
  • Ability to move files to specific location after renaming (/media/tv/{series name}/season {seasonnumber}/ for example)

...

Read more

There are 2 comments on this post.

How to convert infix to postfix (Python)

I wrote this originally some time ago. Mostly for my own amusement. It converts from infix to postfix notation.

I expect most of our readers know what these are but for the benefits of completeness infix is a functional notation we humans use for instance in mathematics where the operator sits in between the operands. Example:

(8 * 5) / 4

...

Read more

There are no comments on this post.

How to build a string representation of an integer in any base without bitwise operators

Last week I gave the geeks at #neverfear IRC a coding task, something I've been trying lately to both challenge them and myself. The challenge I gave them was actually a real-life problem I'd faced in industry as a developer. The challenge was as follows.

Produce an algorithm in the language of your choice that recursively converts a numerical value to a hexidecimal string without bitwise operators. Bonus points awarded for generalising your algorithm to the nth base.

We had a few good solutions mostly following a similiar approach in various languages that I'd like to share.

...

Read more

There are no comments on this post.

BandsInTownPy released

A Python module on the API at bandsintown.com released and is available for download at http://github.com/kay/BandsInTownPy

Sample usage:

from bandsintown import *
site = BandsInTownRequest()
 
print "Getting Flyleaf information by name"
artist = site.GetArtistByName("Flyleaf")
print artist.get_as_string().encode('utf-8')
print
 
print "Getting events for Flyleaf by Artist object"
events = artist.GetEvents()
 
print len(events), "events"
 
for event in events:
    print (event)
print
 
print "Getting todays events"
events = site.GetEventsToday()
 
print len(events), "events today"
 
for event in events:
    print event.get_as_string().encode('utf-8')
print
 
print "Getting events for Exilia by name"
events = site.GetEventsByArtistName("Exilia")
 
print len(events), "events"
 
for event in events:
    print event.get_as_string().encode('utf-8')
print

...

Read more

There are no comments on this post.

User Authentication With Django

Introduction

This article will teach you how to authenticate users with Django in a simple, quick, and secure manner. You'll also learn how to require authentication on certain pages of your website, and how to gracefully handle login and logout functionality.

The target audience is people who have had minimal experience with Django, and are aware of how Django works in a basic manner.

...

Read more

There are 13 comments on this post.

Performing a Denial of Service (DoS) Attack on a Phone Line

Intro

Denial of Service attacks are nothing new to people in the IT and computer security world. DoS attacks are a very simplistic form of attack which aim to flood the target (whether it be a computer, mobile device, or phone line) with traffic so that it cannot process legitimate traffic. While being simple simple to perform, DoS attacks are often difficult to defend against without significant downtime.

Today I'm going to show you how to perform a DoS attack on a phone line. This process is simple, quick to perform, and very illegal. Before we get started, I'd just like to remind you that this article is for educational purposes only! All the code I'm putting into this article is python, and should work on any system with python 2.4+.

...

Read more

There are 15 comments on this post.

Pages: 1 2 3

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