[Tutor] Benefit/Disadvantage to storing data as dictionary vs. in a class

James Reynolds eire1130 at gmail.com
Fri Jun 1 16:24:09 CEST 2012

On Fri, Jun 1, 2012 at 9:12 AM, Adam <amgaweda at gmail.com> wrote:

> I'm working on a class that handles multiple rooms that generate a large
> amount of data. Currently my class model looks something like this (more
> apologies for any indentation errors):
> Class Model:
>    rooms= {}
>    for z in range(num_of_zones):
>        for i in range(24):
>            tmp[i] = { VAR:0, SD:0, AVG:0, READINGS:[] }
>            tmp[i]['updated'] = datetime.utcnow()
>                for j in OT_RANGE:
>                    tmp[i][j] = { VAR:0, SD:0, AVG:0, READINGS:[] }
>        rooms[z] = tmp
> In case that gets complicated, I'm looking to store readings based off the
> current hour and current outside temperature. The Model class has other
> variables and functions that it uses to compare and manipulate the data as
> it comes in.
> My question is in regards to the storing all this data in a dictionary,
> which makes it easy to reference self.rooms[z][12][85][AVG]; however is
> this better/worse or the same as creating a 'Room' class to store the data?
> Since a 'Room' only contains a large amount of data, but doesn't contain
> any functions, which form of storing the data is considered 'better'?
> Thank you,
> Adam
> ______________________________**_________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>

If you have functions (or in this case methods) which will be used on a
certain 'objects' data structure, a class representation of the data is the
natural fit. Otherwise, you will need to pass the dictionary around to
various functions and this will be seriously confusing.

However, you probably should consider how to represent that data within a
class body, that is, it is extremely confusing to access data such
as self.rooms[z][12][85][AVG].

Instead, you should break down what the data structure into like-things and
how they are related, for example, you might consider something like this:

class Weather(object):
    def __init__(self, temp, precipitation, humidity, bara_pres):
        self. temp = temp
        self. temp = precipitation
        self. temp = humidity
        self. temp = bara_pres
    def my_func(self, *args,**kwargs):
        pass #do stuff here

However, if you need to either further define the datapoints of if you want
the datapoints to be able to "do things" then you could do something like

class Temperature(object):
    def __init__(self, temp):
        self. temp = temp
        self.other_stuff_about_temp = stuff

    def my_temp_func(self, *args,**kwargs):
        pass #do stuff here

class Weather(object):
    def __init__(self, temp, precipitation, humidity, bara_pres):
        self. temp =  Temperature(temp)
        self.precipitation = precipitation
        self.humidity = humidity
        self.bara_pres = bara_pres
    def my_func(self, *args,**kwargs):
        pass #do stuff here

from here you could either put your instantiated objects into a list and do
math on the elements thereof, or you could put them into another class
object like this:

class WeatherOverTime(object):
    def __init__(self, my_list):
        self.my_list =  my_list

    def avg(self, attr): #attr is the attribute you want to take an average
of, so humidity, temp, whatever.
        temp_list = map(lambda x: getattr(x, attr), my_list)
        return sum(temp_list) / count(temp_list)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120601/00d801cd/attachment.html>

More information about the Tutor mailing list