0

Ok, I recently started programming in Python, and I really like it.

However, I have run into a little issue.

I want to be able to define a function to take in some data and assign it to a variable that I designate, rather than have to perform the operation every time I want to submit the value.

Here is a code fragment:

       try:
            if elem.virtual.tag:
                virt = True
                temp_asset.set_virtual(True)
        except AttributeError:
            temp_asset.set_virtual(False)
        if virt: #if virtual, get only faction, value, and range for presence
            try:
                fac = elem.presence.faction #an xml tag (objectified)
            except AttributeError:
                fac = "faction tag not found"
                temp_asset.misload = True
            try:
                val = elem.presence.value
            except AttributeError:
                val = "value tag not found"
                temp_asset.misload = True
            try:
                rang = elem.presence.range
            except AttributeError:
                rang = "range tag not found"
                temp_asset.misload = True
            #Set presence values
            temp_asset.set_presence(fac, val, rang)

The functions set the values, but I want to be able to perform the error checking with something like this:

def checkval(self, variable_to_set, tag_to_use)
    try:
         variable_to_set = tag_to_use
    except AttributeError:
         variable_to_set = "tag not found"
         temp_asset.misload = True

Is this doable? Let me know if I need to show more code.

Edit: I don't need pointers per se, just anything that works this way and saves typing.

Edit 2: Alternatively, I need a solution of how to check whether an objectified xml node exists (lxml).

Biosci3c
  • 772
  • 5
  • 15
  • 35
  • 1
    You might need to show *less* code. Distilling down your question with a minimal code snippet that shows what you're after without too much specifics might help. – Santa Mar 22 '11 at 01:07
  • Flagging "not a real question", as the questioner has asked a different, more focused (thus, more answerable) question intended to replace. – Charles Duffy Mar 22 '11 at 11:42
  • Ok, I am going to ask another question, since I went in the wrong direction with this one. I also need a better title. Sorry about that. I will link to it from here in case you want to follow its progress. Edit: Here is the new question: http://stackoverflow.com/questions/5385821/python-lxml-objectify-checking-whether-a-tag-exists – Biosci3c Mar 22 '11 at 01:41

1 Answers1

1

Have you tried/looked into the getattr and setattr functions?

For example, assuming these "variables" are object attributes:

def checkval(self, attr, presence, tagstr):
    tag = getattr(presence, tagstr, None)           # tag = presence."tagstr" or None
    setattr(self, attr, tag or 'tag not found')     # ?? = presence."tagstr" or 'tag not found'     
    if tag is None:
        self.temp_asset.misload = True   

You call it like,

your_object.checkval('fac', elem.presence, 'faction')

Alternatively, you can pre-define these variables and set them default values before you attempt to look up the tags. For example:

class YourObject(object):
    _attrmap = {
        'fac': 'faction',
        'val': 'value',
        'rang': 'range',
    }

    def __init__(self):
        # set default values
        for attr, tagstr in self._attrmap.items():
            setattr(self, attr, '%s tag not found' % tagstr)

    def checkval(self, attr, presence):
        for attr, tagstr in self._attrmap.items():
            tag = getattr(presence, tagstr, None)
            if tag is not None:
                setattr(self, attr, tag)
            else:
                self.temp_asset.misload = True
Santa
  • 11,381
  • 8
  • 51
  • 64
  • Ok, that's a thought. The variable that I want to set is indeed an object attribute (an object's variable). The tag is an objectified lxml node that contains a string. Basically, I am checking to see if an xml node exists, then setting the variable to whatever it contains. If it doesn't exist, then I set it to a predefined string. – Biosci3c Mar 22 '11 at 01:18
  • Edited for that use-case. You'd basically have to play with `getattr` and `setattr` on two separate objects: the object these "variables" belong to, and the objectified node. – Santa Mar 22 '11 at 01:20
  • Yeah, I'm probably going to create a new question about lxml and objectify, as I don't think I want to go the "pointer" route. – Biosci3c Mar 22 '11 at 01:25
  • 1
    @Biosci3c There are no variables containing some value , in Python. There are only objects to which point references. You should learn more concerning the object model, the data model and the execution model of Python, that are peculiar. – eyquem Mar 22 '11 at 01:27
  • What pointer? There is no "pointer" involved, here... (not at C level). – Santa Mar 22 '11 at 01:33