开发者

Python Function with "Pointers"

Ok, I recently st开发者_JAVA百科arted 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).


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜