Python class that automatically appends values to existing attribute OR creates and fills new attribute -


my goal here able create nested dictionaries have attributes hold lists of values. example, want able this:

mydict['person 1']['height'].vals = [23, 25, 32] mydict['person 2']['weight'].vals = [100, 105, 110] mydict['person 2']['weight'].excel_locs ['a1', 'a2', 'a3'] 

so, each "person" can keep track of multiple things might have data on, such height , weight. attribute i'm calling 'vals' list of values heights or weights. importantly, want able keep track of things raw data came from, such location in excel spreadsheet.

here's working off of:

import collections class vals:     def add(self, list_of_vals=[], attr_name=[]):         setattr(self, attr_name, val)      def __str__(self):         return str(self.__dict__) mydict = collections.defaultdict(vals) 

so, want able add new keys needed, such mydict['person 10']['test scores'], , create new attribute such "vals" if doesn't exist, append new values if does.

example of want achieve:

mydict['person 10']['test scores'].add([10, 20, 30], 'vals') 

which should allow mydictmydict['person 10']['test scores'].vals return [10, 20, 30]

but want able append list later on if needed, such using .add again append existing list. example, mydict['person 10']['test scores'].add([1, 2, 3], 'vals') should allow me return [10, 20, 30, 1, 2, 3] mydict['person 10']['test scores'].vals.

i'm still getting used object oriented programming, classes, etc. open better strategies might exist achieving goal, nested dictionary structure find convenient holding data.

if modify vals class above, needs way determine whether attribute exists. if so, create , populate list_of_vals, otherwise append existing list

thanks!

from understand, want can conveniently hold data. build class instead of nested dictionary, because allows easier way see how works (and helps organize everything!).

class person(object):     """__init__() functions class constructor"""     def __init__(self, name=none, testscores=none):         self.name = name         self.testscores = testscores   # make list of class person(s) personlist = [] personlist.append(person("person 1",[10,25,32])) personlist.append(person("person 2",[22,37,45]))  print("show 1 particular item:") print(personlist[0].testscores) personlist[0].testscores.append(50) print(personlist[0].testscores)  print(personlist[1].name) 

basically, person class holds of data instance of it. if want add different types of data, add parameter init() function this:

def __init__(self, name=none, testscores=none, weight = none):     self.name = name     self.testscores = testscores     self.weight = weight 

you can edit values variable.

if isn't looking for, or confused, willing try more.


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -