Solved

Python: Generating instances automaticly from imported data

Posted on 2008-10-21
4
347 Views
Last Modified: 2010-04-16
I am coding a sort of timetabling thing in python and need some advice.

i have a bunch of data - the following is an example:

xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx
vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv
xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx

basicly its a big block of integers but they all have specifc meaning and i need them all. So i import the integers from a text file into python then, split them all and add store it as a list. so i now have a list of integers:

print listOfInt

yields: 'xxx', 'xxx', 'xxx' ........ 'vvv', 'vvv', 'vvv'.....'xxx', 'xxx', 'xxx'

this is all good and is working how i want it to.

now i have a class setup that takes 1 value from the 'vvv'  (vvv can be thought of as a unique identifer) section of the data and some values from the 'xxx' set of data.

so i generate an instance of my class on one occaison. the instance is named 'vvv'. However i have many 'vvv's like a few hundred. the order of the imported data is all preknown.

Is there a way of automaticly generating instances of my class from the imported data? as in can i create instances of a class from my list and if so how would i go about doing this?

heres a pseudo code of kind of what i want.


for item in list:
   item = ClassName(xxx1, xxx2, xxx3)



where item = 'vvv'

so i end up with a bunch of instances of ClassName all called whatever there specific value of 'vvv' is initiated with a bunch of the xxx values.

Sorry for the really long winded post... i really dont know how to go about explaining it better. Thanks for any help you can offer.
   
0
Comment
Question by:Ashwee
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
PaulKeating earned 150 total points
ID: 22769032
for item in list:
   locals()[item] = ClassName(xxx1, xxx2, xxx3)

Of course, item has to be a valid Python identifier. If it isn't (all digits, say) you'll need to make it valid by putting a _ on the front.

If that is not feasible then:

items = {}
for item in list:
   items[item] = ClassName(xxx1, xxx2, xxx3)
 
0
 
LVL 28

Assisted Solution

by:pepr
pepr earned 100 total points
ID: 22773782
Basically, everything in Python is an object. When creating the object of a class, you get the untyped reference to the object. The type is bound to the object, not to the variable that it is assigned to.

Variables in Python are just names stored in a hidden, internal dictionary (the key) bound to the value of the reference (the dictionary item value. (Dictionary is a look-up table, a.k.a. hash table,...).

However, you probably would like to access your 'vvv' objects using their string/number/whatever hashable value. They probably are not hardcoded in the application. This way the second approach presented by PaulKeating is probably more suitable. Then you can call the methods the object the way like:

items['vvv'].myMethod()

You can also process all objects via iterating through the items dictionary like:

for name, obj in items.iteritems():
    if name == 'vvv':
        obj.myMethod()
     
0
 
LVL 28

Expert Comment

by:pepr
ID: 22773794
To add, the first approach by PaulKeating shows using the internal dictionary (with limitations for the key value related to what the identifier in Python can contain). The second approach uses your own, explicit dictionary.
0
 

Author Comment

by:Ashwee
ID: 22774431
Thanks guys, i wont be able to check this till tommorow (audit day at work) but it looks like its exactly what im after. I will distribute points and supply further feedback in a day or so.

Thanks again :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now