background preloader

Programming

Facebook Twitter

Development

Raganwald. Denormalization Patterns. Welcome to the Database Programmer! The main theme of these essays is that your applications will be leaner, faster, and easier to write and maintain if you understand how databases work. This essay is part of the Keys, Normalization and Denormalization series. There is also a Complete Table of Contents and a Skills-oriented Table Of Contents. The Non-normalized Database A denormalized database is one that has been meticulously normalized to eliminate redundancies, only to have redundancies deliberately put back in to meet other needs. That is the kind of database we are going to talk about today. By contrast, a database is "non-normalized" if nobody ever bothered to normalize it. The Fully Normalized Database The fully normalized database will have few if any redundancies, each fact will be stored in exactly one place.

No calculated values. The Denormalized Database We must dream up a way to keep the redundant values correct. The Foreign Key Is Our Friend (Again) The First Pattern: FETCH. Best Programming Jokes. Two bytes meet. The first byte asks, “Are you ill?” The second byte replies, “No, just feeling a bit off.” Eight bytes walk into a bar. The bartender asks, “Can I get you anything?” “Yeah,” reply the bytes. “Make us a double.” Q. How many programmers does it take to change a light bulb? Why do programmers always mix up Halloween and Christmas? There are only 10 kinds of people in this world: those who know binary and those who don’t. A programmer walks to the butcher shop and buys a kilo of meat. “Knock, knock.” Programming is 10% science, 20% ingenuity, and 70% getting the ingenuity to work with the science.

Programming is like sex:One mistake and you have to support it for the rest of your life. A man is smoking a cigarette and blowing smoke rings into the air. To which the man replies, “I am a programmer. There are three kinds of lies: Lies, damned lies, and benchmarks. A programmer is walking along a beach and finds a lamp. The genie responds, “Gee, I don’t know. Funny Error Messages. Writing plug-ins in Python. Introduction In a previous article for IBM developerWorks I wrote about the joy of creating command-line tools with Python. This article takes the command-line tools to the next level by creating plug-ins to extend them. Both plug-ins and command-line tools offer a convenient way to extend the functionality of existing code. Used together, they can be a very powerful tool. To get started writing the plug-in, you are going to use an open source Python package I wrote called pathtool, which uses generators to walk a filesystem and yield a file object.

This library was specifically written to allow developers to extend it by writing their own filters that do something to a file object, and then return the result. The actual Python module code is a bit larger than you would like to see for an article, so I will only post a snippit of the API that you will actually use: Listing 1. pathtool API from pathtool import path path("/tmp", pattern="*.mp3", action=(lambda rec: print_rec(rec))) Listing 2. The Tao of Programming, Geoffery James. Communicating with code. Some people can sell their ideas with a brilliant speech or a slick powerpoint presentation. I can't. Maybe that's why I'm skeptical of ideas that are sold via brilliant speeches and slick powerpoints. Or maybe it's because it's too easy to overlook the messy details, or to get caught up in details that seem very important, but aren't.

I also get very bored by endless debate. We did a lot of things wrong during the 2.5 years of pre-launch Gmail development, but one thing we did very right was to always have live code. The first version of Gmail was literally written in a day. The great thing about this process was that I didn't need to sell anyone on my ideas. The most dramatic example of this process was the creation of content targeted ads (now known as "AdSense", or maybe "AdSense for Content"). However, we needed a way for Gmail to make money, and Sanjeev Singh kept talking about using relevant ads, even though it was obviously a "bad idea". The os module. This module provides a unified interface to a number of operating system functions. Most of the functions in this module are implemented by platform specific modules, such as posix and nt.

The os module automatically loads the right implementation module when it is first imported. Working with files The built-in open function lets you create, open, and modify files. This module adds those extra functions you need to rename and remove files: Example: Using the os module to rename and remove files Working with directories The os module also contains a number of functions that work on entire directories. The listdir function returns a list of all filenames in a given directory. Example: Using the os module to list the files in a directory # File: os-example-5.py import os for file in os.listdir("samples"): print file sample.au sample.jpg sample.wav ...

The getcwd and chdir functions are used to get and set the current directory: Example: Using the os module to change the working directory os.umask(0) Similarity of texts: The Vector Space Model with Python | All My Brain. Articles - How to change the User-Agent of Python's urllib. This article explains how to change the User-Agent of urllib, a module of the standard library of Python. Table of Contents The problem While using Python's urllib module you may want to change the User-Agent sent by the functions for two main reasons: you want to define your own UA ; you need a valid UA in order to access some sites.

The Python Library Reference said that, by default, the URLopener class sends a User-Agent header of , where is the urllib version number , as we can see in the following code: >>> from urllib import URLopener >>> URLopener.version 'Python-urllib/1.16' Several sites (e.g. >>> from urllib import urlopen >>> page = urlopen( ) >>> page.read() […]<b>Error</b><H1>Forbidden</H1>Your client does not have permission to get URL <code>/search? >>> page = urlopen( ) >>> page.read() […]Error: ERR_ACCESS_DENIED, errno [No Error] at Tue, 25 Dec 2007 15:45:20 GMT[…] The solution So, how can we change the User-Agent? Let's see how it works: >>> MyOpener.version 'My new User-Agent' Using Metaclasses to Create Self-Registering Plugins. Fredrik Lundh | July 2008 | Based on a comp.lang.python post Background: This was posted to a thread about how to best register plugin classes.

The original approach was to scan all plugin modules for Plugin subclasses; this note describes an alternative solution that uses a self-registering base class. I have no opinion on what’s best here; this is just another way to do it. A metaclass is a something that’s responsible for creating a class, much like an ordinary class is responsible for creating an object. When Python executes the following statement, class Spam: attrib = 1 def func(self): pass # <-- end of class statement it will create a new scope for the class content, execute the class body, and then, when it reaches the end, call the “metaclass” to create the actual class object. Registry = [] # list of subclasses class Plugin(object): class __metaclass__(type): def __init__(cls, name, bases, dict): type. For more on this, see e.g. Beauty and the Geek Game Theory: Answering the Freakonomics Challenge. In the August 15, 2008 Freakonomics Blog entry, guest author Alon Nir poses the question of determining the best strategy for the game Beauty and the Geek, as seen on TV.

Roughly, the rules of Nir's version of the game is as follows: There are 7 teams playing. It is common knowledge that 3 teams have strength 4, one team has strength 2, and three teams have strength 1, in the sense that the ratio of two teams strength is equal to the ratio of the probabilities that each will prevail in a contest between them. So a team with strength 4 wins 4/5 of the time against a team with strength 1. In each round of the game, first there is a set of two contests, for each one a winner is chosen according to the strength odds. The same team may win both contests. Then the winner of the first contest picks a team to go into the elimination round.

Nir poses the question of what is the best strategy for the team with strength 2. Results Here is the output from my program: Additional Commentary Program.