Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Python: Generating instances automaticly from imported data

Posted on 2008-10-21
4
Medium Priority
?
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
PaulKeating earned 600 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 29

Assisted Solution

by:pepr
pepr earned 400 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 29

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

705 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