0

How do I use text from a file as a variable name? I am pulling values out of an excel file. I am using xlrd and xlutils with python 3.

class employee(object):
def __init__(self, name):
    self.name = name
    emp_list.append(name)
def bulk_hours(self,sunday=0,monday=0,tuesday=0,wednesday=0,thursday=0,friday=0,saturday=0):
    self.sunday = sunday
    self.monday = monday
    self.tuesday = tuesday
    self.wednesday = wednesday
    self.thursday = thursday
    self.friday = friday
    self.saturday = saturday

I'm pulling employees out of a spreadsheet. I'm trying to use their actual names. I would love to know any working solution. Thanks!

Edit: Pardon my ignorance regarding programming and my horrible post. I'm trying to make a simple program that allows me to load an employees name and work schedule from Excel. I will also make sure any edits are saved back into the spreadsheet. The employees are labeled by their names. I'm trying to load their name as a variable so I can do:

John = employee('John')
John.bulk_hours(0,8,8,8,8,8,0)
Stacy = employee('Stacy')
print(John.monday)

I'm aiming to use their name as the variable I can use dot notation on. Is this feasible? Is their some other way I should approach this?

def load(row):
employee2 = employee(s.cell(row, 0).value)
employee2.bulk_hours(s.cell(row, 1).value, s.cell(row, 2).value, s.cell(row, 3).value, s.cell(row, 4).value,
                     s.cell(row, 5).value, s.cell(row, 6).value, s.cell(row, 7).value)
print(employee2.saturday)

I'm trying to use a function like this to load multiple employees and their hours.

Could I use a list like this somehow?

worker = ['Joe']
worker[0] = employee('Joe')
worker[0].bulk_hours(0,8,8,8,8,8,0)
print(worker[0].monday)

Thank you for your valuable time.

  • 1
    can you show the sample text file and what is your desired output or what you are trying to do? – NinjaGaiden Feb 21 '17 at 00:56
  • 3
    You don't; you are asking the wrong question. Please tell us what you are trying to do; a proper solution will likely involve a dictionary rather than dynamic variable names. – Mark Reed Feb 21 '17 at 00:56
  • @NinjaGaiden I would like to be able to pull out an employee name like 'John' and then do John = employee('John') – Devin Griffin Feb 21 '17 at 01:19
  • You know, you can convert the XLS file to CSV. Maybe you can show us a snippet of the CSV file? – NinjaGaiden Feb 21 '17 at 01:21
  • Possible duplicate of [Create variables from strings in Python](http://stackoverflow.com/questions/3803419/create-variables-from-strings-in-python) – DYZ Feb 21 '17 at 01:23
  • That's restating the same question. Take a step back and tell us what your goal is. Big picture. What is the program you are writing supposed to accomplish? – Mark Reed Feb 21 '17 at 01:32
  • 1
    This sounds like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me. – Paul Rooney Feb 21 '17 at 01:43
  • @MarkReed I tried to add more information. I don't know if I expressed the full big picture. I think I elaborated enough for you guys to grasp my question. – Devin Griffin Feb 21 '17 at 01:59

2 Answers2

1

Override __getattr__ to transparently access an internal dictionary.

class employee(object):
    def __init__(self, ...):
        self._internal_d = extract_data()  # replace extract_data with however you extract CSV values to a dictionary
        ...  # perform other initialization

    def __getattr__(self, name):
        try:
            return self._internal_d[name]
        except KeyError:
            raise AttributeError()

Optionally, you can implement __setattr__ to allow writing properties.

    def __setattr__(self, name, value):
        return self._internal_d[name] = value

Explanation: when python does variable assignment and can't find a variable name "normally", it checks if an object has __getattr__. If it does, it calls __getattr__ to get the value with the specified name. Thus, you can have dynamic variable names. Likewise for __setattr__.

SanBot
  • 11
  • 2
0

You don't want to use variable names comming from the spreadsheet. or one: variable names are internal to the running program, and are not meant to be exported again to an output file.

It is meaningless that the variable is bamed John to represent John's data when the program is running. For example, let's suppose it would be possible to create a special markup to use teh variable name - say a ? prefix to fetch the name from another variable. Your example would be something like this:

def row_data(emp_name, *args):
    ?emp_name = employee(emp_name)
    ?emp_name.bulk_hours(*args)
    print(?emp_name.monday)

So, even if at runtime ?emp_name would be exchanged by the contents of the variable name, yur program would still look the same to someone reading the code. So, it makes more sense to simply let the variable be named person or employee or anything, since it can represent any employee (and in fact will, as you loop through the spreadsheet contents, usually the variable will carry the data about one person a time).

That said, there are times when we do want to have data in the program which do have programmatic labeling. but still on those cases - that is what dictionaries are for - create an employees dict, and fill it - and then you can have the names as the keys:

employees = dict()
def row_data(emp_name, name):
    person = employee(emp_name)
    person.bulk_hours(*args)
    employes[emp_name] = person

def print_employeers():
    for person_name, person_data in employees.items():
         print(person_name, person_data)

As you can see, it is possible to print all employees data without having to type in their names. Also, if it is an interactive program, it is possible to find the data related to a name that is input by the user, using it as the dictionary key.

However if you intend to have a program to generate other Python source files themselves, and end-up with a .py file for each employee. In that case just make use of a templating engine, like Jinja2, or simply use str's format method. (It still hard to imagine why you would need such a thing).

And, just to have a complete answer, it is possible to create dynamic variable names in Python. However, you will be in the exact same situation I described in the first example here.

Global variables for any running code are kept in a regular Python dictionary, which is returned by a call to the globals() function. And similarly, values for local variables are kept in a dictionary that returned by a call to locals() - although these do not behave nicely for variables known at compile time (in that case, the local variables are cached in the frame object, and the locals dict is only synchornized with them when it is read, but they can't be written via locals)

So:

def row_data(emp_name, *args):
    globals()[emp_name] = employee(emp_name)
    globals()[emp_name].bulk_hours(*args)
    print(globals()[emp_name].monday)

will work just as you asked - but it is easy to see it is useless.

jsbueno
  • 99,910
  • 10
  • 151
  • 209