Re: extending dictonary

2009-12-11 Thread InvisibleRoads Patrol
On Sat, 21 Nov 2009 09:25:38 +0100, nospam <"knutjbj(nospam)"@online.no>
wrote:
> Is there any way to extend the dictonary in such manner that I can 
> insert muliplay value to each keys and return one of the value as the 
> default value. I would like to have similar syste that I drawed out
below.
> 
> 
> tree[nucelotide_postionc][nucleotide]=default(value subtree) This should 
> be returned when doing lookup without any
> tree[nucelotide_postionc][nucleotide]=Postive value
> tree[nucelotide_postionc][nucleotide]=Negative value


You could use the collections.defaultdict method.

>>> import collections
>>> example1 = collections.defaultdict(int)
>>> example1[1]
0

>>> example2 = collections.defaultdict(list)
>>> example2[1]
[]

>>> example3 = collections.defaultdict(dict)
>>> example3[1]
{}

>>> # Make nested default dictionary
>>> def getNewOuterDictionary():
...return collections.defaultdict(getNewInnerDictionary)
>>> def getNewInnerDictionary():
...return collections.defaultdict([])
>>> example4[1][1]
[]   

Hope that helps.


-- 
Roy Hyunjin Han
http://invisibleroads.com
Training people to become software developers and connecting them to jobs
and projects for local businesses
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: extending dictonary

2009-12-11 Thread InvisibleRoads Patrol
On Sat, 21 Nov 2009 09:25:38 +0100, nospam <"knutjbj(nospam)"@online.no>
wrote:
> Is there any way to extend the dictonary in such manner that I can 
> insert muliplay value to each keys and return one of the value as the 
> default value. I would like to have similar syste that I drawed out
below.
> 
> 
> tree[nucelotide_postionc][nucleotide]=default(value subtree) This should 
> be returned when doing lookup without any
> tree[nucelotide_postionc][nucleotide]=Postive value
> tree[nucelotide_postionc][nucleotide]=Negative value


You could use the collections.defaultdict method.

>>> >>> import collections
>>> >>> example1 = collections.defaultdict(int)
>>> >>> example1[1]
0

>>> >>> example2 = collections.defaultdict(list)
>>> >>> example2[1]
[]

>>> >>> example3 = collections.defaultdict(dict)
>>> >>> example3[1]
{}

>>> >>> # Make nested default dictionary
>>> >>> def getNewOuterDictionary():
...return collections.defaultdict(getNewInnerDictionary)
>>> >>> def getNewInnerDictionary():
...return collections.defaultdict([])
>>> >>> example4[1][1]
[]   

Hope that helps.


-- 
Roy Hyunjin Han
http://invisibleroads.com
We train people to become software developers and connect them to jobs and
projects for local businesses.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Zlib: correct checksum but error decompressing

2009-08-26 Thread InvisibleRoads Patrol
On Wed, 26 Aug 2009 07:19:42 -0700 (PDT), Andre 
wrote:
> I have been trying to solve this issue for a while now. I receive data
> from a TCP connection which is compressed. I know the correct checksum
> for the data and both the client and server generate the same
> checksum. However, in Python when it comes to decompressing the data I
> get the exception: "Error -5 while decompressing data"! I would assume
> that if the string in python is equivalent to the correct checksum
> than the decompress function should also work on the same string, but
> that's clearly not the case.
> 
> # convert data to a byte array
> data = array('b', raw_data)
> # print checksum for visual inspection
> print zlib.crc32(data.tostring())
> # try to decompress, but fails!
> str = zlib.decompress(data.tostring())
> 
> Does anyone know what's going on?

Hi Andre,

Hmm.  Can you decompress the string on the server before it was sent? 
Maybe the zipfile or gzip module will work.
Reference:
http://bytes.com/topic/python/answers/42131-zlib-decompress-cannot-gunzip-can
from cStringIO import StringIO
from gzip import GzipFile
body = GzipFile('', 'r', 0, StringIO(raw_data)).read()

You might want to try experimenting with the wbits parameter of
zlib.decompress()
Reference:
http://mail.python.org/pipermail/python-list/2008-December/691694.html
zlib.decompress(data, -15)

The zlib module seems to work fine with both strings and byte arrays.
import array, zlib
dataAsString = zlib.compress('example string')
dataAsArray = array.array('b', dataAsString)
zlib.decompress(dataAsString) == zlib.decompress(dataAsArray)
zlib.decompress(dataAsString) == zlib.decompress(dataAsArray.tostring())

-- 
http://invisibleroads.com

We train ordinary people into Python software developers and connect them
with jobs and projects for local businesses.
-- 
http://mail.python.org/mailman/listinfo/python-list