Is this your actual code? It feels like some parts are missing.

>> len(self.b1))

self.b1 should be an Python list, which you can always call len() on. It's not 
a special object with a property.

>> address_book =new  AddressBook()

You didn't show an AddressBook struct in any of your code. What is that type? 
Is this actual code or pseudo-code?

The most likely problem here is that whatever you have assigned to the .b1 
field is not actually a Python list.


-----Original Message-----
From: Surbhi Bhasin [mailto:[email protected]] 
Sent: Tuesday, May 31, 2011 11:59 PM
To: [email protected]
Subject: unable to write serialized list of list in a text file

Hi All,

I am very new to thrift.

I have written  a AddressBook.thrift file. This file has a following
structure:

 

Struct A {

  1: required list<string> str1,

  2: required i32 int1,

3: required i64 int2

}

Struct B {

  1: required list<A> a1,

  2: required bool bool1

}

Struct C {

  1: required list<B> b1

}

 

I compile this thrift file using python compiler.

Then I create a python server (addressBookServer.py) which add/modifies
2-3 entries in b1 (which is list of B). Then it serializes it into
binary format using:

 

address_book =new  AddressBook()

transport = TSocket.TServerSocket('localhost', 9090)

tfactory = TTransport.TBufferedTransportFactory()

pfactory = TBinaryProtocol.TBinaryProtocolFactory()

server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

transportOut = TTransport.TMemoryBuffer()

protocolOut = TBinaryProtocol.TBinaryProtocol(transportOut)

address_book.write(protocolOut)

bytes = transportOut.getvalue()

 

Then it writes to the text file:

 

f = open(file_name, "wb")

f.write(bytes)

f.close()

 

While trying to convert to binary format, I get following error:

 

address_book.write(protocolOut)

File "gen-py/addressBook/ttypes.py", line 267, in write

oprot.writeListBegin(TType.STRUCT, len(self.b1))

AttributeError: B instance has no attribute '__len__'

 

On debugging I found out that in generated file ttypes.py where all user
defined data types (struct in AddressBook.thrift) are defined, there is
no _len_ (self)/ len() method defined.

 

I am stuck over here as to how to proceed further. Can someone please
guide me through it.

 

Regards,

Surbhi

 

Reply via email to