1

I'm trying to eliminate a lot of repetitive code in my Python and this function repeats itself in various forms. Is there not an easier way I can just pass a variable (using a list or similar) to the function "Create_me" and then get it to:

  1. Create the global variable "var1".
  2. Take the global variable and use it in place of "var1"

I've tried writing some code and it just doesn't reference the correct variables. Am I trying to do something which can't be done? Please see the 2 sections of code below, the 1st one shows the function working with static variables and the 2nd shows the code I'm trying to get working.

I've also done a screenshot of the whats in the memory so you see what I'm facing.

Thanks in advance!

def CLASS_POLICY_DEF_C350():
    global var1
    var1 = defaultdict(list)
    worksheet = workbook.sheet_by_name('maps-C350')
    headers = worksheet.row(0)
    for index in range(worksheet.nrows)[1:]:
        try:
            for header, col in zip(headers, worksheet.row(index)):
                if col.value != "":
                    var1[header.value].append(col.value)
        except:
            print sys.exc_info()

def main():
    CLASS_POLICY_DEF_C350()

The code I've written is:

#!/usr/bin/python
from collections import defaultdict
import sys,xlrd

def Create_me(test_var):
    workbook = xlrd.open_workbook("""templates.xls""")
    for dynamic_list in test_var:
        globals()[dynamic_list] = 0
        dynamic_list = defaultdict(list)
        worksheet = workbook.sheet_by_name('maps-C350')
        headers = worksheet.row(0)
        for index in range(worksheet.nrows)[1:]:
            try:
                for header, col in zip(headers, worksheet.row(index)):
                    if col.value != "":
                        dynamic_list[header.value].append(col.value)
             except:
                 print sys.exc_info()
        print 'var1 = ',var1
        print 'dynamic_list =',dynamic_list
    print '\nlocal var = ',locals()
    print '\nglobal var = ',globals()

def main():
    test_var = ('var1','var2','var3')
    Create_me(test_var)

if __name__ == "__main__":
    main()

Frankus$ ./test-var.py

local var = {'index': 1, 'header': text:u'class-map B2', 'worksheet': , 'dynamic_list': defaultdict(, {u'class-map A1': [u' match A2'], u'class-map B2': [u' match B2']}), 'headers': [text:u'class-map A1', text:u'class-map B2'], 'test_var': ('var1', 'var2', 'var3'), 'workbook': , 'col': text:u' match B2'}

global var = {'defaultdict': , 'var1': 0, 'var3': 0, 'var2': 0, 'builtins': , 'file': './test-var.py', 'package': None, 'sys': , 'Create_me': , 'name': 'main', 'xlrd': , 'main': , 'doc': None}

Frankus$

closed as unclear what you're asking by Bart van Ingen Schenau, amon, user40980, Jules, durron597 Oct 4 '15 at 1:03

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • It's not really clear what you're trying to achieve here, and I'm afraid the code dump doesn't really help. Could you explain what you're trying to achieve in more detail? – Jules Oct 3 '15 at 19:57
  • Hi - I've reworded the question and added the code I'm trying to write as well as a dump of of the local and global variables. Hopefully makes it easier to understand. – frankus Oct 4 '15 at 9:55
  • 1
    Implementation questions like this are off-topic for the site. – GlenH7 Oct 4 '15 at 13:05

Browse other questions tagged or ask your own question.