Zend Studio forgetting about your ZF Project

June 16th, 2009 by wickeddoc

as a php developer i’m using zend studio for eclipse on a daily basis. sometimes zend studio forgets about my zend framework projects, especially projects which are hosted on a SVN repository. i close my project, reopen it, and for no obvious reasons zend studio no longer recognizes it as a zend framework project. huch!?

until now i was unable to find a real solution to my problem, but here’s a little workaround which should get you up and running again, in case you’re running into the same problem.

close the project, then just open the .project file at the root of your project in your favourite text editor and check the ‘natures’ section, make sure it contains the following line:

<nature>org.zend.php.framework.ZendFrameworkNature</nature>

that should do the trick.

Posted in PHP, Zend Framework | 2 Comments »

Add creation and modification timestamps to an Excel worksheet

February 19th, 2009 by exhuma.twn

Please, for the love of $deity do not hit me….. This is going to be a post about excel!

Excel is a horrid solution for data entry, and even worse for data archival. And yet, it’s one of the most commonly used solutions. One of the most useful information in any given data-set is the information about when the information was created and when it was last modified. This is something that any decent developer in charge of a data collection (let’s just call it that for now) will add to each data record.

Alas, a lot of non-it people manage and store their data in excel worksheets. And that is OK with me as long as they pay attention to data archival. In it’s most simple form, data archival can be achieved by storing the data as a CSV file and including the following metadata:

  • Which column represents which value (the name of the variable)
  • The data type (number, text, date, …) of each column
  • If a column is “coded”, please also include the meaning of each code.
    For example a “Yes”, “No”, “Maybe” column might be stored as “1”, “2” and “3”. Which means in it’s most basic nature it’s a numeric variable, but the different values have a meaning attached to them. So: Add this list in your metadata description.
  • If any computations or checks are performed on the values, please add them to the metadata document as well!

Even if the timestamp values might seem superflous at first, it will be of great help to anyone tracing errors in the data. Imagine that you would at some point need to fix some values that were entered/modified during a specific time period for whatever reason. Without this most basic bit of information you will be up for a treat. However, if it’s been rigurously implemented since the beginning, you’ll have the problem solved in no time.

Now, each halfway serious database system will offer you this kind of functionality out-of-the-box. But Excel is no database system (I intentionally left out the word “management” as this issue is a bit more general!). So it does not offer you a straight-forward way to solve this. But even if it’s not straight-forward, it’s simple enough for about anyone using Excel do add this bit of information.

Assuming that you use the first two columns (numbered 1 and 2 in excel) of your worksheet to add creation- and modification timestamps simply open up the Visual Basic editor (found in Tools->Macro or somesuch), next, in your project tree (in the top left of the screen) select your workbook (the .xls file), and in it’s sub-tree double-click the Worksheet that should have the timestamps set automatically.

Then copy/paste the following text into the just opened code editor and you’re done. I hope the comments will give some insight as to what happens. Note that in this case I will ignore the first row of the sheet, and obviously, the first two columns. If that does not suit your needs, feel free to change this script to your liking.

'
' Callback which is called when a cell in a workbook changes
' @param Target: The cell that changed it's value
'
Private Sub Worksheet_Change(ByVal Target As Range)
   ' We will ignore any changes in the first row, as it contains header labels
   If Target.Row = 1 Then Exit Sub
   
   ' As we set the values of column 1 and 2 we won't need to capture changes in these either
   If Target.Column = 1 Or Target.Column = 2 Then Exit Sub
   
   ' We will update the timestamp in column 2 *always* (last changed time)
   Cells(Target.Row, 2) = Now
   
   ' We will update the timestamp in column 1 only if it is empty (creation time)
   If IsEmpty(Cells(Target.Row, 1)) Then
     Cells(Target.Row, 1) = Now
   End If
End Sub

Posted in Coding Voodoo | No Comments »

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 »

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 »

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 »

Centering a window on the screen with Qt

September 10th, 2007 by exhuma.twn

Yet another thing that’s not automagic in Qt.
Here’s a python solution:

if __name__ == "__main__":

   app = QtGui.QApplication(sys.argv)

   dw = app.desktop().width()
   dh = app.desktop().height()

   myapp = MainWindow()
   myapp.setGeometry(
         int((dw - (dw - (dw / 2)) * 1.5) / 2),
         int((dh - (dh - (dh / 2)) * 1.5) / 2),
         int((dw - (dw / 2)) * 1.5),
         int((dh - (dh / 2)) * 1.5))
   myapp.show()
   sys.exit(app.exec_())

This will also resize the window, adapting to the desktop size.
Found and adapted from the Qt interest archive.

Posted in Python | No Comments »

« Previous Entries Next Entries »

Pages

Recent Posts

Categories

Links


Archives

Meta