Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary VS Class
#1
Hi there.

A newbie here wanna ask a newbie question.
Background info:

I've been working actively on an excel project for the last 2 years. It started out small and has been progressively getting bigger and bigger and has gotten to the stage where updating users, departments etc has become a pain.

To make things easier i have moved most of that kind of info into XML files so that they may be edited in a central location and then the changes can be reflected across the rest of the spreadsheets. (over approx 300 of them and that count will increase by approx 120 per year).
To make things easier I wanted to create a web interface where they are able to get a list of completed spreadsheets and also the level of completion and as users always do they give you all the impossible requests to do, so they requested the spreadsheets be to be opened from the web interface and bingo, it all fell apart. Apparently you cannot access your local file system for that.
I did it with some OLD tech(HTA) but I'm not happy with this so I'm turning to Python for a solution.
The plan is to have a simple exe file in the end with a simple interface that they may look at the data and then with the click of a button open one of the spreadsheets and do their damn work :)

Ok now to the question(sorry it took so long)
Reading the info into python is easy with ElemenTree but how do I save that data in python. My initial instinct was to go with Dictionaries. Each user, department and also each spreadsheet has a unique ID. So if <User045> has a task to complete for <Dept004> in spreadsheet <Control089> it would be easy. If I set up my dictionaries in such a way the the "Key" corresponds to the unique ID's there is no need to iterate through a dictionary, i can just say userdict(User045) and have that users info available to me.
If I do the class thing however, each users class instance have to go into a list and then when i need a specific user's info i have to iterate through the list to get to the reference if that users class instance and then retrieve the data.
This is all making me super confused. What do I do?
Reply
#2
The classes can be the values in the dictionary. Consider this class:

class Person(object):

    def __init__(self, name, id):
        self.name = name
        self.id = id
Now say you've made a bunch of instances of that class in a list. You can make a dictionary of them all this way:

people_dict = {person.id: person for person in people_list}
You could skip the middle man of people_list and just put them into the dict as you create them. Now to get someone's name when you have their id, you just use people_dict[some_id].name. That gives you the greater power of the class instances, with the accessibility of the dictionary.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
OMG that rocks! I have so much to learn about python and I'm loving it..
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,926 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Empty attribute class dictionary after saving it in a class object dictionary 3dimensions 6 4,888 May-20-2018, 01:57 PM
Last Post: 3dimensions
  How to print a global dictionary class properly 3dimensions 2 3,229 Apr-18-2018, 05:45 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020