Wednesday, October 06, 2010

The ? : operator in Python

We're all familiar with the "? :" operator from Java and other languages, which can be useful in some cases, for example:
max = (x > y) ? x : y
While Python doesn't have this operator built-in, one can definitely mock the behavior:
max = x > y and x or y
How it works?
The expression is parsed left to right, so first "x > y" is parsed.
If "x > y" yields "True", the "and x" part is parsed and since "True and x" yields x, this is the result of the entire expression (no need to parse the right side of the "or" since the left side was successful).
if "x > y" yields "False", then the "or y" part is parsed, resulting y.

I like it :)

Tuesday, September 28, 2010

Split URL to sub domains using Python

Hi again,
For my new and exciting project (hopefully more on this later) I needed to get all sub domains possibilities of a URL, so if this is the input URL:
http://a.b.c.d/test.html
I needed to get the following sub domains:
['a.b.c.d', 'b.c.d', 'c.d', 'd']
Here's the good news: this is pretty simple with Python ! :)
import urlparse
url='http://a.b.c.d/test.html'
hostname=urlparse.urlparse(url).hostname
sh=hostname.split('.')
subdomains=[".".join(sh[i:]) for i in range(len(sh))]
print subdomains
>> ['a.b.c.d', 'b.c.d', 'c.d', 'd']
Nice, isn't it?

Tuesday, May 25, 2010

BeautifulSoup Toturial Presentation

I gave an introductory tutorial presentation re: BeautifulSoup python module to the R&D department at work.
It is based on BeautifulSoup documentation which is:
“an HTML/XML parser for Python that can turn even invalid markup into a parse tree. It provides  simple, idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves  programmers hours or days of work.”
Read the tutorial here.
Feel free to ask BeautifulSoup questions.

Cheers.

Thursday, April 29, 2010

JSON eval problem "Error: invalid label"

The other day I created a web service receiving XHR (ajax) requests and returning JSON response.
The calling code eval'ed the JSON in order to use it as a JavaScript object, like this:

var ret_json = service.result; // AJAX response JSON string

var ret_json_obj = eval(ret_json); // ERROR
In IE I got error: "; expected" and in FF I got "Error: invalid label".

Solution was to wrap the ret_json with parenthesis and a string, like this:

var ret_json = service.result; // AJAX response JSON string

var ret_json_obj = eval("(" + ret_json + ")"); // WORKING

The text must be wrapped in parenthesis to avoid tripping on an ambiguity in JavaScript's syntax.

Read here more about JSON.

Cheers.

Monday, March 01, 2010

Usability: Web Form Design Best Practices

Recently I picked up a Usability session about Web Form Design Best Practices, by Luke Wroblewski, which took place last year at MIX09, Las Vegas.
It's a bit lengthy (>70 mins) but worth it.
I bring you here the summary of the session:


Web Form Design
Best practices

According to slides by Luke Wroblewski

1. Path to Completion.
a.     Illuminate clear path to completion
b.     Use progress indicators to communicate scope, status and position
c.     If requiring substantial time or information look-up, consider using a start page
d.     Use more general progress indicators for form with variable sequence.

2. Label Alignment
a.     For reduced completion time & familiar data input (name, address etc.): top aligned.
b.     When vertical screen space is a constraint: right aligned.
c.     For unfamiliar or advanced data entry: left aligned.

3.     Help & Tips
a.     Minimize the amount of help & tips required to fill out a form.
b.     Help visible and adjacent to a data request is most useful.
c.     When people maybe unsure about why or how to answer, consider automatic inline system.
d.     For complex & reused forms, consider user-activated system.
e.     Use inline help unless you have a lot of help content (text, graphics, charts).
f.       Use a consistent help section if you have a lot of help content.

4.     Inline Validation
a.     Use inline validation for inputs that have potentially high error rates.
b.     Use suggested inputs to disambiguate.
c.     Communicate limits.

5.     Primary & secondary actions
a.     Avoid secondary actions if possible
b.     Otherwiste, ensure a clear visual distinction between primary & secondary actions ("submit" and "cancel").
c.     Align primary actions with input fields for a clear path to completion.

6.     Actions in progress
a.     Provide indication of tasks in progress.
b.     Disable "submit" button after user clicks it to avoid supplicate submissions.
c.     Consider opportunities to streamline legal requirements.

7.     Error
a.     Clearly communicate an error has occurred: top placement, visual contrast.
b.     Provide actionable remedies to correct errors.
c.     Associate responsible fields with primary error message.
d.     "Double" the visual language where errors have occurred.

8.     Unnecessary inputs
a.     Look for opportunities to remove unnecessary inputs.
b.     Do not complicate questions for the sake of removing inputs.

9.     Form organization
a.     Take the time to evaluate every question you ask.
b.     Ensure your forms speak with one voice.
c.     Strive for succinctness.
d.     If a form naturally breaks down into few short topics, use a single web page.
e.     When a form contains a large number of questions that are only related by a few topics, try multiple web pages.
f.       When a form contains a large number of questions related to a single topic, one long web page.

10.            Gradual engagement
a.     Try to avoid sign-up forms.
b.     Reflect your service's core essence through lightweight interactions.
c.     Make people successful instantly.
d.     If you auto-generate accounts, ensure there is a clear way to access them.
e.     Do not simply distribute the various input fields in a sign-up form across multiple pages.



That's it. Hope you find it helpful.

Monday, February 01, 2010

Convert PowerPoint to HTML with python

After I converted MS Word to HTML (and fed it to the application..) the next stage was to convert MS PowerPoint to HTML.
I thought it would be rather straight forward, given the success I experienced with openoffice headless api converting Word to HTML. It wasn't.
openoffice converts ppt to html (filter "impress_html_Export"), that's right. The output is a set of files, in which each ppt slide is converted to image (screenshot) and HTML. While the screenshots are good, the HTML is not satisfactory. Embedded images in the ppt doesn't appear in the converted HTML, and the same happened for tables. In addition, using the "2 column layout" produced HTML with only the left-column text, leaving the right-column text out. Same happened for any content added to a blank layout template (e.g. text boxes). In addition, numbered list (ol) where converted to bullets (ul).
Needless to say this solution is out of the question.

So here I was, looking for a way to convert ppt to html, using Java or (preferably) Python.
Looking for a Python module to do the job I found win32com, which may be good but not relevant for me since our servers don't run Windows. Although win32com CAN run on debian I preferred working with software that is not Windows dependant.

AND THEN... I found odfpy.
It's a GPL software defining itself as "Python API and tools to manipulate OpenDocument files".
Since openoffice document is basically an archive file, this module reads and writes the archive structure, allowing for easy manipulation of all kinds of openoffice formats.
In addition, it has some built-in scripts for common tasks, e.g. odf2xhtml(which I'm using), odfoutline, csv2odfand more.
SO, I'm converting the ppt to odp using openoffice headless api, and then convert the odp to HTML using odfpy.

And it works !

Sunday, January 03, 2010

Explore registry of dead pc

My PC is dead.
Well, I have a new one now :-)
As it was a Windows machine, some valuable data was stored in the registry but since the pc refused to launch again (yes, even with Ubuntu 9.0 on-the-fly-disk..) I thought I've lost it.
Searching the Internet I found this simple yet useful Windows Registry File Viewer. Simply download the zip file and run the .exe file.
Now I took the hard disk from my late pc, connected it to my new pc, and browsed to the registry file:
C:\Documents and Settings\username\ntuser.dat

A really important feature of this registry viewer is that it's also a registry exporter. Select the registry node you'd like to export, click "file" and then "export to REGEDIT4 format" and you're good.
In order to import this registry file, just double click it and it's values are added to your current pc registry file.

Original FAQ page.