Filtered by JavaScript, Python

Page 46

Reset

HTML entity fixer

November 25, 2004
9 comments Python

Here's a little program I wrote recently to fix incorrectly defined characters into HTML entities. For example, this is incorrect:


<b>Bärs &amp; Öl</b>

But this is correct:


<b>B&amp;auml;rs &amp;amp; &amp;Ouml;l</b>

To demonstrate I have set up a little test page here so that you can test to convert your impure HTML content.
Run test program

Truncated! Read the rest by clicking the link below.

Add links to a text (take II)

November 10, 2004
2 comments Python

If you missed the entry about Add links to text with URLs I suggest you read that first. This script has now been improved with bug fixes thanks to David Otton and Flump Cakes.

Download: addhrefs-0.4.tgz
It does not come with a nice installer because I don't think it belongs to the generic Python library anyway. If you want to use it, copy the file addhrefs.py to somewhere useful.

Truncated! Read the rest by clicking the link below.

Urwid - curses-based UI/widget library for Python

October 30, 2004
0 comments Python, Linux

"Urwid is a curses-based UI/widget library for Python. It features fluid interface resizing, multiple text layout options, simple markup for attributes, powerful scrolling list boxes and flexible edit boxes."

I've been looking for something like this for a long time. See the screenshots to get an idea of what a curses-based UI is.

Truncated! Read the rest by clicking the link below.

Massrenaming with shell and python

October 28, 2004
5 comments Python, Linux

I had the problem today that several files in a directory started with an _ underscore character. (e.g. _red.gif). Instead of manually renaming each file used the power of shell and python to solve it. (and some help from my collegue of course). Fortunately none of the files had an underscore in the middle of the name so I could keep the command quiet simple:


$ find -iname '_*' \
| xargs python -c \
'import sys;print "\n".join(["mv %s %s"%(x, x.replace("_","")) for x in sys.argv[1:]])'\
| sh -s

Truncated! Read the rest by clicking the link below.

Python inspect module

August 16, 2004
2 comments Python

My friend Jacob Lundqvist of Galdrion today showed me a nifty little method I did not know about in Python, namely the inspect module. It allows you to find out what the name of the method was that called the method "you're in". Allow me to show an example:


import inspect

def foo():
    caller_module = inspect.stack()[1][1]
    caller_method = inspect.stack()[1][3]
    print caller_module, caller_method
    return "Something"

def bar():
    foo()

if __name__=='__main__':
   bar()

Truncated! Read the rest by clicking the link below.

Pretty print SQL script

August 6, 2004
6 comments Python

In my latest work stuff I have a custom debugger module that prints the SQL statements used to stdout. To make the debug output more readable I whipped together this quick script that pretty prints SQL statements with hopefully correct case and indentation. It converts something ugly like this:


select * from foo order by bar;

into this:


SELECT
  *
FROM
  foo
ORDER BY bar;

Try with your own SQL statements

Truncated! Read the rest by clicking the link below.

Integer division in programming languages

August 4, 2004
20 comments Python

I have a degree in mathematics and computer science, but still I don't know why different programming languages evaluate Integer/Integer differently. On any calculator if you divide one integer with another you get a decimal number (of course not if the numerator is a factor of the denominator).

Python for example returns a integer number:


>>> print 3/10
0
>>> print 3/10.0
0.3

Perl on the other hand returns a decimal number:


print 3/10;  print "\n";
print 3/10.0;
...gives...
0.3
0.3

PostgreSQL is like Python:


database=# SELECT 3/10 AS quotient1, 3/10.0 As quotient2;
quotient1 |       quotient2
-----------+------------------------
        0 | 0.30000000000000000000

And good old MS Excel gives:


=3/10  <=> 0.3

Why is it so?

Date plus years or months or weeks

July 28, 2004
6 comments Python

I'm envisioning a input form which asks initially for a date on a calendar. The next question is duration, or how long from that date. The input for this must be clever. So now I've put together a little script that adds numerically what someone enters in English.

The "problem" is what do you do when you add "1 month" when incrementing the month number is not enough? This program does like this: 2004-08-31 + 1 month = 2004-09-30.

Please have a try of my little program and let me know what you think

Date formatting in python or in PostgreSQL

July 20, 2004
1 comment Python

I deviced a very simple benchmark through Zope as the web server. Two SQL Select statements that draws from a dummy database table which contains a table with loads of dummy entries with a timestamp field.

Conclusion is that Python's DateTime module is considerably slower than PostgreSQL's builtin date formatting function.

Truncated! Read the rest by clicking the link below.