Friday, November 14, 2008

Improve the performance of you string

As we know,Python's string object is immutable,that means you can't change it in-place.Each time when you operate on a string it'll generate a new string object.Sometime it's not good,especially when your string is big.A method to solving this performance issue is use List object.first we convert a string to a list,now it's mutable ,secondly we manipulate this list,if it's done we convert to string.following is an example:
#Python code
#firstly, we define a new string,it's a poem ,a big string
bigstring=""""
Today I saw a butterfly,
as it floated in the air;
Its wings were spread in splendor,
Unaware that I was there.
""""
#secondly, we convert it to a list
biglist=bigstring.split(' ')
biglist[1]='you'
biglist[3]="one"
#convert it back
bigstring=''.join(biglist)
bigstring

Because list is mutable,each time we operate on it,no new object generated.So we have high performance.

Thursday, November 13, 2008

How to read and write the CSV file

We can do everything in Python mostly.today we'll show how to read and write csv file by Python.The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. There is no “CSV standard”, so the format is operationally defined by the many applications which read and write it.Python provide a lib which name csv to manipulate csv file.I think the best way to understand a new technology is an example,now let's see how to read a csv file.
#Python code:
import csv # first we need import necessary lib:csv
file=open("c:/Book1.csv") #prepare a csv file for our example

testReader=csv.reader(file,delimiter=' ', quotechar='|')
#now the testReader is an array,so we can iterate it
for row in testReader:
print "|".join(row)

#The result is:
Test1:Test1
Test2:Test2
.....

Mostly the value of a csv file will separate by comma,but sometime may be other App will generate a csv file and separate other quotechar(|,! etc).So csv lib provide a parameter :delimiter and quotechar, so you can custom it by yourself.
Now let's see how to write something into a csv file:
#Python code:
import csv # first we need import necessary lib:csv
file=open("c:/Book1.csv") #prepare a csv file for our example

testWriter = csv.writer(open('Book1.csv', 'w'), delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamWriter.writerow(['Test'] * 5 + ['Wow'])
spamWriter.writerow(['Hello', 'I'm', 'Super Star'])


It's that easy,right?

Wednesday, November 5, 2008

NumPy :a powerful computing library

In my recently project,we plan to develop a financial system.There are many of mathematical computing in it.For performance reason, we need a high speed,fixable,reliable and efficient library.We estimate many libs ,in the end we decide to use Numpy,we highly recommend it.As the introduce of them:

The fundamental package needed for scientific computing with Python is called NumPy. This package contains:

  • a powerful N-dimensional array object
  • sophisticated (broadcasting) functions
  • basic linear algebra functions
  • basic Fourier transforms
  • sophisticated random number capabilities
  • tools for integrating Fortran code.
  • tools for integrating C/C++ code.

Immutability or mutability

As a programmer I develop many of application,using Java,C++,Python and Ruby.Today I want to talk about the "mutability" in these different language.Let's begin with Python language,Python is a dynamic language different to Java or C++.but as we talk about mutability,especially "string" of them.it's same.In python every object is is classified as mutable or immutable.or we can call them "changeable" or "unchangeable".For instance,We have a string "Spam",in Python you can create it as:
>>s="Spam"
"Spam"

In Java you can declare it as:
String s="Spam"

Now,the requirement is we want to access the sub-string of them:
#Python code
>>> s="Spam"
>>> s[0]
'S'
>>> s[0]="z"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

As you can see,we can access Python string as a sequence by use 'index'but if we want to assign a new value into the item of this string we got an exception,that means the string object in Python is immutable,it's similar to Java and C++,but it's different to Ruby.In Ruby you can assign a new value into the item of a string,and after assignment ,the original string object changed:
#Ruby code
irb(main):001:0> s="Spam"
=> "Spam"
irb(main):002:0> s[0]
=> "S"
irb(main):003:0> s[0]="z"
=> "z"
irb(main):004:0> s
=> "zpam" #the value of s has been changed.

That means the string object is mutable in Ruby.In terms of the core types, numbers, strings, and tuples are immutable; lists and dictionaries are not (they can be changed in-place freely).

Wednesday, October 8, 2008

How to show the full error stack in wxPython

If you as me, a python developer,you must have this question,when I'm developing wxPython program,and I type a wrong python method or wrong method etc,then I run it,I found I can't get nothing,I expect my program will display a Frame but it didn't,I know it must because I have some wrong,but I can get any error stack.how to deal with it?
The solution is:
1)run it in command module.
2)switch you method ,app=wx.App() to
app=wx.App(False)
through this way,when there is any error in your application you will get a full error stack,it'll helpful for your developing

How to develope the Adobe FLV program with Python

My recent project need a feature to operate .flv file,I google and goole then I find a good one,aa-lib
you can download it from http://aa-project.sourceforge.net/index.html
AAlib is an portable ascii art GFX library.try use it ,maybe you'll find some cool thing on it.

Monday, July 14, 2008

'ManyRelatedManager' object is not iterable

When I'm writing an app ,in fact it's just a test.following is the actually code:
{% if query%}
{%for book in results%}

{{book.title}}


{%for author in book.authors%}
  • {{author.first_name}}|{{author.last_name}}

  • {%endfor%}

    {{book.publisher.name}}

    {%endfor%}
    {%else%}
  • No Books Found

  • {%endif%}

    When I run it.I got an error message:
    'ManyRelatedManager' object is not iterable.

    the solution for this is

    change the for loop syntax to:

    {%for author in book.authors.all%}

    then it'll be done.