tags:

views:

104

answers:

4

I know next to nothing about Python and I'm using scons. (if you're reading this and know Python but not scons, you can probably help me!)

Could someone help me out and explain how I could have a variable that contains two lists? I'm not sure of the syntax. Is this right?

buildinfo = // how do you initialize a variable that has fields?
buildinfo.objectFiles = []; // list of the object files
buildinfo.sourceFiles = []; // list of the source files

If I have a function f() that returns a variable of this structure, what's the shortest way to append f()'s return value onto both lists? (Really f() is Sconscript() but never mind.)

// call f() several times and append the results onto buildinfo
buildinfo_sub = f(...);
buildinfo.objectFiles.append(buildinfo_sub.objectFiles);
buildinfo.sourceFiles.append(buildinfo_sub.sourceFiles);
buildinfo_sub = f(...);
buildinfo.objectFiles.append(buildinfo_sub.objectFiles);
buildinfo.sourceFiles.append(buildinfo_sub.sourceFiles);
buildinfo_sub = f(...);
buildinfo.objectFiles.append(buildinfo_sub.objectFiles);
buildinfo.sourceFiles.append(buildinfo_sub.sourceFiles);

Is there a shorter way? this isn't too long but it's long enough to be error-prone.

edit: or better yet, I want to define a simple class that has two fields, objectFiles and sourceFiles, and if I call

object1.append(object2)

then object1 will append object2's objectFiles and sourceFiles fields onto its own, so I could just do:

buildinfo = BuildInfo([],[]);
buildinfo.append(f(...));
buildinfo.append(f(...));
buildinfo.append(f(...));
+1  A: 

You could use a dict to make code very similar to your original, but it wouldn't be use dots. Take a look:

buildinfo = dict()
buildinfo['objectFiles'] = []
buildinfo['sourceFiles'] = []
buildinfo['objectFiles'].append("Foo")
buildinfo['sourceFiles'].append("Bar")

It would work, but I'm not sure if this is what you're looking for.

Regarding updated question: You can easily combine two lists, without a second object.

allobjects = []
objs1 = ["foo", "bar"]
objs2 = ["baz", "bam"]

allobjects.extend(objs1) # ['foo', 'bar']
allobjects.extend(objs2) # ['foo', 'bar', 'baz', 'bam']
Jack M.
ok, that answers one of my questions (syntax for something containing named subfields). thanks!
Jason S
I'm not sure that I understood your edit. If you're looking to combine the two lists, my edit should help. If you're looking to append two custom objects together, you'll need to write a Class for it.
Jack M.
yeah, i meant a custom class
Jason S
A: 
class BuildInfo(object):
   objectFiles = [];
   sourceFiles = [];

which you can create with:

buildInfo = BuildInfo()

but I don't know about making the appending syntax shorter or cleaner, other than adding some looping over the calls to f(). which adds a different kind of error prone-ness.

Oren Mazor
+1  A: 

If you create a separate class (e.g. Oren's BuildInfo) then you can certainly add a method to that class that appends data to both lists.

class BuildInfo(object):
    def append(self, data):
        self.objectFiles.append(data)
        self.sourceFiles.append(data)
qid
that's kind of what i want, except "data" would be a BuildInfo object, I don't want to append data to both lists, i want to append data's objectFiles to the appendee's objectFiles, and the same for sourceFiles.
Jason S
+2  A: 

How about something like this:

class BuildInfo(object):
    def __init__(self, objectFiles = [], sourceFiles = []):
        self.objectFiles = objectFiles
        self.sourceFiles = sourceFiles 
    def append(self, build_info):
        self.objectFiles.extend(build_info.objectFiles)
        self.sourceFiles.extend(build_info.sourceFiles)

To use it you would then say:

a = BuildInfo() #uses default value of an empty list for object/sourceFiles
b = BuildInfo(["hello.dat", "world.dat"], ["foo.txt", "bar.txt"])
a.append(b) #a now has the same info as b
a.append(b) #a now has ["hello.dat", "world.dat", "hello.dat", "world.dat"], ["foo.txt", "bar.txt", "foo.txt", "bar.txt"]

The difference betweeen append and extend is this

a = [1,2,3]
b = [4,5,6]
a.append(b) #a is now [1,2,3[4,5,6]]

a = [1,2,3]
b = [4,5,6]
a.extend(b) #a is now [1,2,3,4,5,6]
Davy8
ah, thx for clarifying append vs. extend
Jason S