Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Friday, January 11, 2013

Fibonacci speed and exponential growth

For the fun of it I wrote a fibonacci algorithm in:
  • C
  • Lisp
  • Clojure
  • Python
and tested their execution speed.

The result is not surprising, and the chart below is a clear reminder that it's not the speed of the language, it's how good your algorithms perform (at least when exponential growth can be expected).

The slowness of the Clojure tests must be the JVM startup time. I know Clojure and Lisp can be compiled, here neither are.

Thar fibonacci algorithm used here are simple and recursive:

def fib(x):
    if x<=2: return 1
    return fib(x-1)+fib(x-2)

better algorithms for this exists, and i can highly recommend looking at the LiteratePrograms if you are interested in this: 

http://en.literateprograms.org/

Sunday, July 6, 2008

Django for CMS, Part 2

I have previously had doubts about the benefits of using Django as a framework for a CMS. I still have my doubts but after working with Django I might be about to change my mind.

The last year or so I have completed 4 projects using Django. 3 out of 4 projects was completed in time with a result that satisfied the customer. All 4 projects of cause of a higher quality that could be accomplished without the use of a framework.

But Django is not an easy framework to use. Even though the basic usage of the framework if simple, it takes time to master more complex tasks. Or what really takes time is to research the topic and discover the correct way to handle the task from within the framework.

But non the less, it's a greate framework!

So, why choose Django as the framework for a CMS? Here are some points:

1. It's easy to catch and URL and map it to a page-like object.

2. There are more and more "off the shelf" plug-and-play Django applications which could easy be integrated into the CMS.

3. Easy to write new CMS extensions as just another application.

4. Build-in json generator to ease communication with client side javascript.

So, after I have really gotten to know Django I feel that it might be the right choice for a CMS. And as Django is slowly getting to a stable version 1.0 and having got the Google stamp of approvel (used in Google App Engine), it might just be time to start.

Wednesday, July 11, 2007

Primoris CMS

I have created a project on Google Code for a CMS and CMS framework developed in Python.

http://code.google.com/p/primoris/

The framework will support access (users, groups) , pages and page hierarchies , i18n and extension modules.

Saturday, April 21, 2007

Django for CMS

Well, Django is nice for building web applications, but have some drawback if you need to develop a CMS.

  1. The URL dispatcher is not much help. In a CMS you cannot determine the content of a given URL without looking inside some sort of DB which hold the content. This means that the urlpatterns structure most likely will hold a regex to an admin system and a regex to a handers for all other URLS.
  2. The above point also means that you only have 2 Django application, the page handler and the admin system. What you really need in a CMS is the ability to display different content elements in the same page.
  3. The automatic admin system is just to lame to be any help in a CMS. It's lack of features, like a integrated editor (FCKEditor / TinyMCE) or an ability to grant some users access to specific part of the admin, unability to handle record positioning by an index (move up/down), makes it pretty useless.

So, what should you do? Roll your own content mangement framework (CMF), or reuse the parts of Django which can help you?
If it had been any other language I would probertly have gone for the reuse, but the is Python, and we have all sort of nice code (fx Paste) which can be used for a CMF.

I'm I way off?

Saturday, April 7, 2007

Django

So, there is quite a few Python web frameworks to choose from these days. Thats really nice. After Guidos appraisal of Django I have been giving it a try.

After a background in ASP its something quiet different: URL's are routed to a specific function, so no need for a file per URL. Arguments can be incorporated into the URL instead of having it in the querystring. Django also have a really nice ORM so you can write code without having to use SQL.

My initial project is to build a small CMS using Django.

The system will for a start consist of the following element:

Page
- name
- parentpage

Content
- page
- data

I'll post the code when i have got something working.

Tuesday, February 27, 2007

Autoupdate application modules

It's pretty easy to reload modules in Python. Using this feature you can update you whole application to a new version, without leaving or restarting your application.

The code below updates all loaded modules with newer version from any given path. To be included in the update, the module must contain the attribute autoupdate and a version attribute.

import sys
import os

def check(path):
""" Update all modules containing a autoupdate attribute
"""
for name,mod in sys.modules.items():
if hasattr(mod,'autoupdate'):
last_version=getattr(mod,'version')
print "Testing update of : %s v. %d" % (name,last_version)
org_file=os.path.split(mod.__file__)[1].replace('.pyc','.py')
new_module_file="%s\\%s.%d" % (path,org_file,last_version+1)
if os.path.exists(new_module_file):
print "Update found: %s" % new_module_file
update=file(new_module_file,'rb').read()
destination="%s\\%s" % (os.getcwd(), org_file)
print "Will replace: %s" % destination
#os.remove(destination)
#file(destination,'wb').write(update)
reload(mod)


This only works for modules that can work after a reload. Fx. SQLObject has a problem with that...

It would be an easy task to extend this to autoupdate from a FTP- or website.

Sunday, February 25, 2007

Socket performance

Just written a small script to test network/firewall performance in Python, and could not help myself from testing it on both XP and Linux...

OpenSUSE 10.2 performance:
1896.296296 mbit / second

Windows XP performance:
299.645137 mbit / second

(Tested on a dualboot machine.)

So, again the question must be, is this related to Python or the OS?

Well, iperf to localhost on Win XP yields:

C:\Documents and Settings\Martin\Desktop>iperf.exe -c localhost
------------------------------------------------------------
Client connecting to localhost, TCP port 5001
TCP window size: 8.00 KByte (default)
------------------------------------------------------------
[1912] local 127.0.0.1 port 1328 connected with 127.0.0.1 port 5001
[ ID] Interval Transfer Bandwidth
[1912] 0.0-10.0 sec 610 MBytes 511 Mbits/sec


iperf on Linux:

martin@msjdell:~> iperf -c localhost
------------------------------------------------------------
Client connecting to localhost, TCP port 5001
TCP window size: 49.4 KByte (default)
------------------------------------------------------------
[ 3] local 127.0.0.1 port 28150 connected with 127.0.0.1 port 5001
[ 3] 0.0-10.0 sec 3.55 GBytes 3.05 Gbits/sec

Monday, February 12, 2007

SQLObject

SQLObject is a really neat Object Relational Manager for Python. But imho not very well documented.

I have been developing a class which can handle the usual Create, Read, Update and Delete (CRUD) pattern. I'm using Kid for templates.

Here are some bites of the code:

# getting a tables columns
for col in table.sqlmeta.columnList:
print col.name
columnType=table.sqlmeta.columnDefinitions[col.name]
if col.foreignKey==None:
# normal column
else:
# relationship column

# getting remove and add method in join
rm=getattr(obj,'remove' + join.addRemoveName)
rm(record) # remove a record from relation

Tuesday, January 23, 2007

WSGI

Web Server Gateway Interface (WSGI) is the standard interface between web server software and Python web applications.

And this is really neat. The obvious first; you can plug any app supporting WSGI into any webserver supporting WSGI. And most webservers do, in some way or another. But whats even cooler is that WSGI has an architecture which allows applications to be stacked. You can have fx your authorization in a separate application.

Its easy and it allows for minimum coupling.