1

I'm new to Python and struggling a bit with a piece of code. I am using rundeckrun which is an open source python client for the Rundeck API. There is one bit of code in the client that seems to be locked to python 2.7+ and I really need to get it to work on Python 2.6. I've tried searching but don't even really know what this construct is called so hard to find the 2.6 equivalent for it.

node_attr_keys = (
        'name',
        'hostname',
        'username',
        'description',
        'osArch',
        'osFamily',
        'osName',
        'editUrl',
        'remoteUrl',
    )

    data = {k: getattr(self, k)
            for k in node_attr_keys if getattr(self, k, None) is not None}

The specific error is:

File "/usr/lib/python2.6/site-packages/rundeckrun-0.1.11-py2.6.egg/rundeck/client.py", line 21, in from .api import RundeckApiTolerant, RundeckApi, RundeckNode File "/usr/lib/python2.6/site-packages/rundeckrun-0.1.11-py2.6.egg/rundeck/api.py", line 135 for k in node_attr_keys if getattr(self, k, None) is not None} ^ SyntaxError: invalid syntax

moshjeier
  • 196
  • 6
  • possible duplicate of [Alternative to dict comprehension prior to Python 2.7](http://stackoverflow.com/questions/21069668/alternative-to-dict-comprehension-prior-to-python-2-7) – Jeremy Nov 06 '14 at 19:39
  • ^ It's called a "dictionary comprehension". Hope this other Question helps. – Jeremy Nov 06 '14 at 19:40
  • I don't officially support python 2.6 in rundeckrun. Well, I don't test on it anyway (obviously). If you submit a github issue or pull request I'll take a look at it. If this is the only change, should be pretty easy. Hopefully there are few other land mines. – marklap Nov 08 '14 at 23:30

3 Answers3

1

That is a dictionary comprehension. They are not supported in Python 2.6. The code you provided is roughly equivalent to this code:

node_attr_keys = (
 # Same as your code, omitted for brevity
)
data = {}
for k in node_attr_keys:
    if getattr(self, k, None) is not None:
        data[k] = getattr(self, k)
Kevin
  • 28,963
  • 9
  • 62
  • 81
1

As Kevin points out, this is a dictionary comprehension.

In Python 2.6, you can write it as a generator expression yielding a list of tuples (key/value pairs) and pass that to the dict constructor:

data = dict((k, getattr(self, k))
             for k in node_attr_keys if getattr(self, k, None) is not None)
kindall
  • 178,883
  • 35
  • 278
  • 309
0

Apart from this error, people might get the following errors due the same reason.

File "/usr/lib/python2.6/site-packages/rundeck/transforms.py", line 256
_transforms = {obj_key: obj_val for obj_key, obj_val in locals().items() if hasattr(obj_val, '__is_transform__')}
                                  ^

SyntaxError: invalid syntax

  File "/usr/lib/python2.6/site-packages/rundeck/util.py", line 22
return {c.tag: c.text for c in el}
                        ^

SyntaxError: invalid syntax

File "/usr/lib/python2.6/site-packages/rundeck/util.py", line 36
return {k: v for k, v in el.items()}
               ^

SyntaxError: invalid syntax

File "/usr/lib/python2.6/site-packages/rundeck/util.py", line 58
return {k: kwargs.pop(k) for k in api_keys if k in kwargs}

You might have to apply the same fix as kindall gave. Just modify the files as follows respectively.

_transforms = dict((obj_key, obj_val) for obj_key, obj_val in locals().items() if hasattr(obj_val, '__is_transform__'))
 return dict((c.tag, c.text) for c in el)
return dict((k, v) for k, v in el.items())
return dict((k, kwargs.pop(k)) for k in api_keys if k in kwargs)

Credit goes to kindall.

Leo Prince
  • 2,019
  • 26
  • 29