Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use dictionary key,value pair to set class instance attributes "pythonic"ly?

I have created some Python classes to use as multivariate data structures, which are then used for various tasks. In some instances, I like to populate the classes with various value sets. The default parameter filename "ho2.defaults" would look something like this:

name = 'ho2'
mass_option = 'h1o16'
permutation = 'odd'
parity = 'odd'
j_total = 10
lr = 40
br = 60
jmax = 60
mass_lr = 14578.471659
mass_br = 1781.041591
length_lr = ( 1.0, 11.0, 2.65 )
length_br = ( 0.0, 11.0, 2.46 )
use_spline = True
energy_units = 'au'
pes_zpe = -7.407998138300982E-2
pes_cutoff = 0.293994

Currently, I create a dictionary from reading the desired key,value pairs from file, and now I'd like a "pythonic" way of making those dictionary keys be class instance variable names, i.e.

# Instantiate Molecule Class
molecule = Molecule()

# Create Dictionary of default values
default_dict = read_dict_from_file(filename)

# Set populate class instance variables with dictionary values
for key,value in default_dict:
    molecule.key = value

So the Class's instance variable "molecule.name" could be set with the dictionary key,value pair. I could do this by hand, but I'ms sure there is a better way to loop through it. In actuality, the dictionary could be large, and I'd rather allow the user to choose which values they want to populate, so the dictionary could change. What am I missing here?

like image 973
Corey Petty Avatar asked Feb 09 '23 14:02

Corey Petty


2 Answers

You would use setattr: setattr(molecule, key, value)

like image 67
Josh J Avatar answered May 13 '23 13:05

Josh J


The simple way is:

vars(molecule).update(default_dict)

This will clobber any pre-existing attributes though. For a more delicate approach try:

for name, value in default_dict.items():
    if not hasattr(molecule, name):
        setattr(molecule, name value)
like image 33
Dunes Avatar answered May 13 '23 14:05

Dunes