Filtered by Python

Page 28

Reset

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.

XHTML, HTML and CSS compressor

April 7, 2004
16 comments Web development, Python

Last week Fry-IT released CheckoutableTemplates which is a templating module add-on for Zope. It includes a module called slimmer.py which can compress XHTML, HTML and CSS. The CSS had a flaw in it that I hadn't foreseen. This flaw arises when you use M$ Internet Explorer hacks like this for example:


#centercontent {
   margin-left: 259px;
   margin-right:249px;
   voice-family: "\"}\"";
   voice-family: inherit;
   margin-left: 271px;
   margin-right:251px;
   }

Now that bug has been fixed, so I give you: The XHTML, HTML and CSS compressor It's a little application of slimmer.py so that the compressing can be tested and so that one can see the effect.

EuroPython in Sweden, I should go

March 24, 2004
0 comments Python

I'm actually going to Sweden for a Kung Fu competition on the 12th to 13th of June this year. The competition is not too far away from Göteborg so maybe I can get both done in one trip.

Let's hope I have finished my exams by then.

Google PageRank algorithm in Python

March 21, 2004
27 comments Python, Mathematics

There are many articles on the net about how the PageRank algorithm works that all copy from the original paper written by the very founders of Google Larry Page and Sergey Brin. Google itself also has a very good article that explain it with no formulas or numerical explanations. Basically PageRank is like social networks. If you're mentioned by someone important, your importance increases and the people you mention gets upped as well.

We recently had a coursework in discrete mathematics to calculate PageRank values for all web pages in a web matrix. To be able to do this you have to do many simplifications and you're limited in terms of complexity to keep it possible to do "by hand". I wrote a little program that calculates the PageRank for any web with no simplifications. The outcome is that I can quickly calculate the PageRank values for each page.

Here's how to use it:


from PageRank import PageRanker
web = ((0, 1, 0, 0),
       (0, 0, 1, 0),
       (0, 0, 0, 1),
       (1, 0, 0, 0))

pr = PageRanker(0.85, web)
pr.improve_guess(100)
print pr.getPageRank()

Truncated! Read the rest by clicking the link below.

To readline() or readlines()

March 12, 2004
30 comments Python

UPDATE 2 (November 2017)

Sorry for not having updated this in so many years. 2004 was a different Peter and I'm sorry if people landed on this blog post and got the wrong idea.

To read lines from a file do this:


with open('somefile.txt') as f:
   for line in f:
       print(line)

This works universally in Python 2 and Python 3. It reads one line at a time by iterating till it finds line breaks.

When you create a file object in Python you can read from it in several different ways. Not until today did I understand the difference between readline() and readlines(). The answer is in the name. readline() reads one line character at a time, readlines() reads in the whole file at once and splits it by line.

These would then be equivalent:


f = open('somefile.txt','r')
for line in f.readlines():
    print line
f.close()

# ...and...

f = open('somefile.txt','r')
for line in f.read().split('\n'):
    print line
f.close()

The xreadlines() function should be used for big files:


f = open('HUGE.log','r'):
for line in f.xreadlines():
   print line
f.close()

Truncated! Read the rest by clicking the link below.