Link to home
Start Free TrialLog in
Avatar of Ashwee
Ashwee

asked on

Python: Generating instances automaticly from imported data

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.
   
ASKER CERTIFIED SOLUTION
Avatar of PaulKeating
PaulKeating
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pepr
pepr

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.
Avatar of Ashwee

ASKER

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 :)