Solved

Python: Generating instances automaticly from imported data

Posted on 2008-10-21
4
345 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

747 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

10 Experts available now in Live!

Get 1:1 Help Now