Python startup (command completion & history)

August 21st, 2008 by exhuma.twn

If you want command completion and a history in your python shell, export the PYTHONSTARTUP env var (export PYTHONSTARTUP=$HOME/.pystartup) in your bashrc and create a file ~/.pystartup with the following contents:

import atexit
import os
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)

if os.path.exists(historyPath):
readline.read_history_file(historyPath)

readline.parse_and_bind('tab: complete')

atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath

Posted in Python | No Comments »

Martian Headsets

March 18th, 2008 by exhuma.twn

One link, lot’s of text πŸ˜‰

http://www.joelonsoftware.com/items/2008/03/17.html

Posted in Babble, Links | 1 Comment »

Simplifying a polygon

February 10th, 2008 by exhuma.twn

As you may know, polygons are sensitive to the ordering in which vertices are put in. What is more, the normals of a polygon face depend on the direction of the face.

This usually is not a problem when the polygon is constructed by a graphics-savvy person. If, however (as in my case), the polygons are edited in a user frontend, and if the target audience for that UI may not be aware of the problem, you might run into trouble.

I strongly believe, that details like this should be hidden from the less technically-inclined people, so they can get thei job done without swearing at the UI. And therefore be more productive. This piece of javascript allows you to “simplify”an array of 2D-Points by sorting it accordingly. The resulting array will construct a simple polygon with always outward-facing normals.

This uses the well-known paradigm of taking a reference point and then “swiping” counter-clockwise over the available points. Depending on the reference point (and the available vertices), the end-results may vary. So it may not really result in what the user expected. Having an undo ready at hand should prove useful. However, this algorithm assumes the most likely case, in which the reference point is set to the centroid of the polygon.

The entry-point function is this algorithm is “simplify_polygon”. Feel free to play around with it. Documentation is sparse, but if you know your trig you should figure it out by yourself. It’s fairly straightforward.

Note: The 4-way branch in the “angle” function could be simplified. It is as it is because my mind was locked in the 4 quadrants when writing it. Didn’t feel like changing it yet πŸ˜‰

Read the rest of this entry »

Posted in Coding Voodoo | 1 Comment »

Vim script (mapping) to generate python getters and setters

December 31st, 2007 by exhuma.twn

Somethin that I need quite often is to create custom accessors and mutators for class-attributes. For example convert this:

class MyClass(object):
   
   def __init__(self):
      self.has_changes = False
      self.some_attribute = False

into this:

class MyClass(object):
   
   def __init__(self):
      self.__has_changes = False
      self.__some_attribute = False

   def get_some_attribute(self):
      "Accessor: some_attribute"
      return self.__some_attribute

   def set_some_attribute(self, input):
      "Mutator: some_attribute"
      self.__some_attribute = input
      self.__has_changes = True

   some_attribute = property(get_some_attribute, set_some_attribute)

   def get_has_changes(self):
      "Accessor: has_changes"
      return self.__has_changes

   has_changes = property(get_has_changes)

This particular example allows an easy tracking if a class contains changes. Without the need of calling myclass.get_some_attribute() or myclass.set_some_attribute(foo). You can simply do myclass.some_attribute = foo and the has_changes attribut will change accordingly.

If your class has many attributes, writing custom accessors and mutators can be tedious. So here’s a small Vim-mapping that get’s you started. Sure, you may still need to fine-tune some generated code, but the bulk is there.

<font color="#808bed">nmap</font> <font color="#c080d0">&lt;</font><font color="#c080d0">F6</font><font color="#c080d0">&gt;</font> yyP<font color="#c080d0">&lt;</font><font color="#c080d0">home</font><font color="#c080d0">&gt;</font>widef get_<font color="#c080d0">&lt;</font><font color="#c080d0">end</font><font color="#c080d0">&gt;</font>(self):<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">down</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>yyP&gt;&gt;I&quot;Accessor: <font color="#c080d0">&lt;</font><font color="#c080d0">end</font><font color="#c080d0">&gt;</font>&quot;<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">down</font><font color="#c080d0">&gt;</font>yyP&gt;&gt;Ireturn self.__<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>o<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">down</font><font color="#c080d0">&gt;</font>yyPIdef set_<font color="#c080d0">&lt;</font><font color="#c080d0">end</font><font color="#c080d0">&gt;</font>(self, input):<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">down</font><font color="#c080d0">&gt;</font>yyP&gt;&gt;I&quot;Mutator: <font color="#c080d0">&lt;</font><font color="#c080d0">end</font><font color="#c080d0">&gt;</font>&quot;<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">down</font><font color="#c080d0">&gt;</font>yyP&gt;&gt;Iself.__<font color="#c080d0">&lt;</font><font color="#c080d0">end</font><font color="#c080d0">&gt;</font> = input<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>o<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">down</font><font color="#c080d0">&gt;&lt;</font><font color="#c080d0">home</font><font color="#c080d0">&gt;</font>wveyA = property(get_<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>pA, set_<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>pA)<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>o<font color="#c080d0">&lt;</font><font color="#c080d0">esc</font><font color="#c080d0">&gt;</font>

Put this into your vimrc, or (like I do) into the ~/.vim/ftplugin/python.vim file so it get’s only loaded for python files. Then you only need to write the attribute name of the class, put your cursor on that line, be sure to be in normal mode (hit a few time <esc>) πŸ˜‰ and hit F6

If you want to change the shortcut, simply change the first parameter to this mapping line.

Posted in Python | No Comments »

Schneiderman’s Golden Rules

October 19th, 2007 by exhuma.twn

NEVER forget these simple rules. Every computer application with a user interface will benefit from them.

It’s really worth it. People will love you for it. Trust me πŸ˜‰

I personally find that using an ordered list is somewhat misleading. Every point should be considered with equal importance. That’s why I will change to “ol” tag to an “ul” tag now πŸ˜‰

  • Strive for consistency. As we shall see below, it is important for a user interface to be consistent on many levels. For example, screen layouts should be consistent from one screen to another. In an environment using a graphical user interface (GUI), this also implies consistency from one application to another.
  • Enable frequent users to use shortcuts. Frequent users (or, power users) may be turned off by overly tedious procedures. Allow those users a less tedious procedure for accomplishing a given task.
  • Offer informative feedback. Users need to see the consequences of their actions. If a user enters a command but the computer does not show that it is either processing or has processed that command, this can leave the user confused and disoriented.
  • Design dialogues to yield closure. Interacting with a computer is somewhat like a dialogue or conversation. Every task should have a beginning, a middle and an end. It is important for the user to know when a task is at its end. The user needs to have the feeling that a task has reached closure.
  • Offer simple error handling. User errors should be designed into the system. Another way of stating this is that no user action should be considered an error that is beyond the ability of the system to manage. If the user makes a mistake, the user should receive useful, concise and clear information about the nature of the mistake. It should be easy for the user to undo his or her mistake.
  • Permit easy reversal of actions. More generally, users must be permitted to undo what they have done, whether it is in the nature of an error or not.
  • Support internal locus of control. User satisfaction is high when the user feels that he or she is in control and user satisfaction is low when the user feels that the computer is in control. Design interfaces to reinforce the feeling that the user is the focus of control in the human-computer interaction.
  • Reduce short-term memory load. Human short-term memory is remarkably limited. Psychologists often quote Miller’s law to the effect that short-term memory is limited to seven discrete pieces of information. Do everything possible to free the user’s memory burden. For example, instead of asking the user to type in the name of a file which is going to be retrieved, present the user with a list of files currently available.

See page 74-75 Scheiderman: Designing the User Interface, 3rd

Posted in Coding Voodoo | No Comments »

Why vim?

October 18th, 2007 by exhuma.twn
  • Misconception #1: modal editing
  • Misconception #2: it’s not all about regular expressions
  • Misconception #3: you gotta be nuts and/or a genius to use it
  • Misconception #4: hjkl to move around?
  • Misconception #5: since you are thinking 90% of the time, and editing 10%, the productivity gain might be there, but it’s useless anyway
  • Misconception #6: it’s just sticking to a disappearing past
  • Correct-conception #1: steep learning curve

Tickled your interest? Looky there!

Posted in Coding Voodoo | No Comments »

Jhaampe

October 1st, 2007 by exhuma.twn

About these homes and at the intersections of the roads in this nomadic ‘city’ are the gardens. Each is unique. One may centre around an unusually-shaped stump or an arrangement of stones or a graceful bit of wood. They may contain fragrant herbs or bright flowers or any combination of plants. One notable one has at its heart a bubbling spring of steaming water. Here grow plants with fleshy leaves and exotically-scented flowers, denizens of some warmer clime brought here to delight the mountain-dwellers with their mystery. Often visitors leave gifts in the gardens when they depart, a wooden carving or a graceful pot or perhaps merely an arrangement of bright pebbles. The gardens belong to no one, and all tend them.

Robin Hobb, Assassins Quest.
Great Britain: Harper Voyager, 1998

When rading this passage of this book (which is part of the Farseer Trilogy) I could not help but think about Open Source projects. If you did not had the same thought when reading the text, go read it again. It is a beautiful analogy.

Note: If any directly involved party feels that any copyright claims have been violated by posting this, please leave a comment. I will take this text down if so requested.

Posted in Babble | No Comments »

GPG… anyone?!?

September 24th, 2007 by exhuma.twn

I can sympathize that GPG (a free and toolset to encrypt and sign digital data) has not been used by the wide public in the past. It was complex to set up, the concept of private/public keys was a bit flaky to most people, PGP (not GPG!) costs precious money, and integration with mail clients was not trivial.

With the spreading of Mozilla Thunderbird this has changed drastically however. With enigmail (a mozilla-addon to use GPG with the mail client), things are becoming much easier and accessible. It offers most of the functionalities you wish to see in such a tool. It allows you to create a new private/public key pair, has a simple management tool for keys, and lets you very transparently sign and/or encrypt e-mails.

In my opinion, the main reason why GPG is still a thing for “geeks” or “professionals” is the ignorance (or naivetΓ©) of people about how easy mails can be intercepted. Yesterday I demonstrated somebody this exact fact. And him still being quite computer-literate, he was still baffled that I could show him the text of a mail he sent with his password along the lines (All done with his consent of course). And I do not consider myself a hacker. Just a few minutes of googling, downloading and installing the right tool did it for me. And if I can do this, so can many other people as well. With a little bit more effort, it’s even possible to alter the text of a mail in transit. Needless to say, the emails are stored on somebody else’s server. An this in plain text! Even if the server does support SSL, TLS or whatnot. This only means that the transmission between mail-client and mail-server is secured. Not the storage. And you never know who sit’s behind the screen of that server.

Granted, most of the time, people managing servers like this are most likely geeks themselves, which usually share the concern about security, and thus keep things safe.

Considering this, it can become easy to demonstrate the advantages of mail encryption/signing. And usability is increasing steadily. With the mozilla suit (and thunderbird) now having a user-friendly tool at the ready, people are not yet easily convinced to keep security in mind. Other mail-clients offer easy user-interfaces as well. The KDE-Suite (with KMail and KGPG) had brilliant supoprt for this already a long time ago. And there are more free tools available to make it a user-friendly experience. The Swiss-Army-Knife for a Windows environment would probably be GPGShell. It still resorts to the GPG-Cli application from time-to-time, but it keeps it to a minimum. It also offers easy file-encryption via Windows Explorer shell entries. Another alternative is Evolution, which has built-in supoprt for GPG.

Suffice it to say, there is widespread support for GPG (and PGP) in mail-application across all OS’es. To become more widespread in use, people should be made aware of the risks that are posed by e-mails and how easy it can be made to keep personal information really personal.

Posted in Babble | No Comments »

Calculate the distance between two GPS-Coordinates (ctd.)

September 17th, 2007 by exhuma.twn

And here’s the same thing as plpgsql function for Postgres:

CREATE OR REPLACE FUNCTION lldistance( a point, b point)
RETURNS FLOAT AS $$
DECLARE    x FLOAT;
        dlat FLOAT;
        dlon FLOAT;
BEGIN
   dlat = radians(a[0]-b[0]);
   dlon = radians(a[1]-b[1]);
   x := sin(dlat/2)^2 +
        cos(radians(a[0])) * cos(radians(b[0])) *
        sin(dlon/2)^2;
   RETURN 6367442.5 * (2*atan2(SQRT(x), SQRT(1-x)));
END;
$$ LANGUAGE plpgsql

This results in more accurate distances than simply using

SELECT point1 <-> point2 FROM sometable;

You could also use plpython as function language and copy/paste the earlier post, but plpgsql is more portable.

Posted in Coding Voodoo | 1 Comment »

Calculate the distance between two GPS-Coordinates

September 17th, 2007 by exhuma.twn

This function uses the Haversine formula to calculate the distance which takes into account the spherical nature of the earth.
As the earth is not a perfect sphere, this function approximates this by using the average radius.

from math import sin, cos, radians, sqrt, asin

def lldistance(a, b):
   """
   Calculates the distance between two GPS points (decimal)
   @param a: 2-tuple of point A
   @param b: 2-tuple of point B
   @return: distance in m
   """

   r = 6367442.5             # average earth radius in m
   dLat = radians(a[0]-b[0])
   dLon = radians(a[1]-b[1])
   x = sin(dLat/2) ** 2 + \
       cos(radians(a[0])) * cos(radians(b[0])) *\
       sin(dLon/2) ** 2
   y = 2 * asin(sqrt(x))
   d = r * y

   return d

Posted in Python | 1 Comment »

« Previous Entries Next Entries »

Pages

Recent Posts

Categories

Links


Archives

Meta