hello,

I wanna create a struct in python. the struct needs to have a char and an array of chars.

eg. (ruby code):

Somestruct = Struct .new(:character, :characterArray)
structure = Somestruct.new(" ", [])

How do I do this. I made a Trie in ruby and now I wanna do the same in python. I can use the same algorithm if I can figure out how to store a Trie.

Thanks

drjay

from ctypes import *

class Trie(Structure):
    _fields_ = [("foo", c_char,
                        ("bar", ARRAY(c_char, 10))]

Don't have a testing machine in front of me so that code might be slightly off.

Thanks mate cheers!

I was doing some research last night and I found this:

from collections import namedtuple

namedtuple("MyClass", "foo bar")

MyClass(foo=1, bar=2)

Which is something similar to what I'm looking for. But my python version seems to be off, so I get this ImportError.

Do you know how to fix import errors?

drjay

For named tuple you need >= python26 (that includes python30). Note that if you intend to use it for calling into C code, namedtuple won't work.

Somestruct = Struct .new(:character, :characterArray)
structure = Somestruct.new(" ", [])

This is just a simple list or tuple in python. Try this

structure = []
Somestruct = (" ", [])
structure.append(Somestruct)
Somestruct = ("1", ["abc", "def"])
structure.append(Somestruct)
print structure

If you want more than that, then use a list of classes, with each class defining/declaring what you want.

This is what I did:

class MyClass:
	def __init__(self, f, r):
		self.first = f
		self.rest = r

def main(argv = sys.argv):
	newclass = MyClass(" ", [])
...

Its probably not the most efficient way of doing things but it works.

Thanks for all your reply!

drjay

That works just fine. Being paranoid, I like to supply defaults. Using a class works fine until it becomes too cumbersome or you want a better way of looking things up, then you move up to using an SqLite DB in memory.

class MyClass:
	def __init__(self, f=" ", r=[]):
		self.first = f
		self.rest = r

if __name__ == "__main__":
   list_of_classes = []
   list_of_classes.append(MyClass())
   list_of_classes.append(MyClass("x", ["a", "b"]))
   print "----->", list_of_classes[1].first
   print "----->", list_of_classes[0].rest

I wouldn't worry too much about space usage. On a typical 32bit python installation using __slots__, your class uses 16 bytes per instance, not counting the storage for the data within the class, and you shouldn't (8 of these are mandatory for a base python object, so in reality it uses 8 bytes for your data; two 32-bit pointers, which is what the struct would use in C).

Slots are easy:

class MyClass(object):
    __slots__ = ("first", "rest")
    def __init__(self, f, r):
        ...

I just assigned a sequence of attribute names that the instances will use. That way, python doesn't create a __dict__ for each instance, but allocates just enough extra space for pointers to the names in the sequence.

Also, note that I have used new style classes(subclassing from object). You really shouldn't be using old style classes anymore.

This question has already been answered. Start a new discussion instead.