Does Python provide "Struct" data structure?

D

Daniel Mark

Hello all:

I have found a useful module in IPython, named 'from IPython.ipstruct
import Struct".

So I can use it as follows:

####################################
from IPython.ipstruct import Struct

mystruct = Struct(echo = 1,
verb = 'Verbose',
filedir = 'C:/temp',
)

print mystruct.filedir
#####################################

I have two following questions:

1> Does Python provide such Struct in this standard libary.
Python has "4.3 struct -- Interpret strings as packed binary data", but
it looks like different
from what I really want to get.

2> Is it safe to use IPython's modules in my python program?
I will pack all my code into an executable application by using py2exe.
The clients would like to use this application without any python
intallation.
What should I pay attention if I include the IPython modules into my
python code and
convert it into executable application by using py2exe?

The setup.py for my application is as follows:
###########################################
from distutils.core import setup
import py2exe

options = {
"bundle_files": 1,
# "ascii": 1, # to make a smaller executable, don't include the
encodings
"compressed": 1, # compress the library archive
}

setup(
# The first three parameters are not required, if at least a
# 'version' is given, then a versioninfo resource is built from
# them and added to the executables.
version = "1.0",
description = "mycode",
name = "mycode",

options = {"py2exe": options},
zipfile = None, # append zip-archive to the executable.

# targets to build
console=['mycode.py'],
)
###################################################

Does I need to make any modification if I include IPython module in my
code?

Thank you for your helps
-Daniel
 
B

Bjoern Schliessmann

Daniel said:
I have two following questions:

1> Does Python provide such Struct in this standard libary.
Python has "4.3 struct -- Interpret strings as packed binary
data", but it looks like different from what I really want to get.

Yes, that module is used when you want to deal with C structs in
binary form (e.g. useful at networking with binary protocols).

Try using dictionaries or custom, field-only class instances
as "struct".

Regards,


Björn
 
M

MonkeeSage

Hi Daniel,

To avoid problems you could vendor the ipython file you require, but an
easier solution may just be to implement your own version of the class
(it's extreemly easy):

class struct():
def __init__(self, *args, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)

mystruct = struct(echo = 1,
verb = 'Verbose',
filedir = 'C:/temp')

print mystruct.filedir

Regards,
Jordan
 
B

Brian Blais

Daniel said:
1> Does Python provide such Struct in this standard libary.
Python has "4.3 struct -- Interpret strings as packed binary data", but
it looks like different
from what I really want to get.

I like the following version:

class Struct(dict):

def __getattr__(self,name):

try:
val=self[name]
except KeyError:
val=super(Struct,self).getattr(self,name)

return val

def __setattr__(self,name,val):

l=dir(self)

if name in self:
super(Struct,self).setattr(self,name,val)
else:
self[name]=val


it has the added benefit of having all of the dict methods too.


bb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top