Home > Enterprise >  how to import multiple variables from another file
how to import multiple variables from another file

Time:09-16

Have a nutrition log I've been running and I'm trying to join multiple dictionary values together.

IE:

food_items = {'egg': 60, 'bacon': 60, 'cheese': 120, 'butter': 100,
              'choc milk': 140, 'protein shake': 110,
              'peanut butter': 180, 'coffee': 5,
              'sour gummies': 110, 'cool ranch doritos': 150,
              'pb m&ms':140, 'diet dr pepper': 0,
              'PC Birthday Cake Protein Bar': 200,
              'DKB slice': 110, 'peanut butter': 90,
              'GF energy bites': 100, 'apple': 45,
              'creamy green salsa': 50, 'green tea': 5,
              'Cava hummus': 45}


meal_prep = {'mahi mahi filet': 250, 'braised chicken thigh': 300, 'seared ahi tuna filet w pan sauce': 300, 'chicken and rice': 400, 'Salmon filet': 420}

WF_items = {'corn tortilla': 130, 'oat protein bites': 160, 'jap hummus': 70,'brussels and romaine salad': 160, 'broccoli salad': 200, 'Potato Wedges': 100,'coyo': 80, 'blueberry scones': 240, 'frozen veg rom and broc blend': 50,'hemp protein powder': 100, 'Hu Kitchen Choc Almonds': 170,'Provencal Vegetable Blend': 80, 'frozen broccoli w olive oil': 100,'European Greens Blend': 90}

whole_meals = [food_items, WF_items, meal_prep]

I want to be able to import from the master log (file that contains all of this info), and then be able to group all of these dictionary values to be referenced onto another file so they can be manipulated via multiplication/division.

This is what I have in mind:

from cal_trac_master_log import whole_meals
bkfast = food_items['green tea']   food_items['choc milk']*1.5   \
         food_items['egg']*3   food_items['DKB slice']*2

lunch = food_items['diet dr pepper']   take_out_pop['Trinity Street Combo Pad Thai']/2   \
        food_items['green tea']

dinner = food_items['pb m&ms']   food_items['cool ranch doritos']   \
         food_items['Cava hummus']*2       
                  
snack = food_items['protein shake']   food_items['sour gummies'] 

CodePudding user response:

You only imported whole_meals. If you also want to access food_items, you have to import that as well.

from cal_trac_master_log import whole_meals, food_items

If you want to access all the variables, you can use

from cal_trac_master_log import *

You can also do

import cal_trac_master_log as cal

and then refer to all the variables using the module prefix, e.g. cal.whole_meals or cal.food_items.

CodePudding user response:

You imported the value of cal_trac_master_log.whole_meals into a module variable called whole_meals. Notice that you did not import the variable itself, just the value it references. If some other code reassigns cal_trac_master_log.whole_meals, this module won't see it. That could be good or bad... but usually bad.

The value in cal_trac_master_log.food_items is also in your module's whole_meals[0] and you could reference it that way.

from cal_trac_master_log import whole_meals
bkfast = whole_meals[0]['green tea'] # (etc...)

But yuck. Better not to import the variables at all. Make that first module something that won't give you carpel tunnel syndrome when you type it

import cal_trac_master_log as cal_trac
bkfast = cal_trac.food_items['green tea'] # and etc... 

Now you know exactly what your code is referencing. Better still, rename that module so you don't have to change its name in your mode.

  • Related