pythonpuppetfacter

How to generate a key:value list wih given values from a python list


i have to solve the following task. First of all i'm using pyscard (python module to interact with smartcards) to query the smartcardreaders that are connected to the host. This works just fine and gives my a list of the connected readers.

To make this list available to puppet via facter, i need the list in form of key:value which i can than convert with json.dumps(list) and use it thru a custom fact.

The actual question is: How can i add the keys (0..8) to the given values from the pyscard list.

At the very end, the output should look similar to something like Reader 0: REINER SCT cyberJack ecom_a (0856136421) 00 00

Thanks in advance


Solution

  • Use dictionary comprehension to convert list to dict.

    lis = ["reader one", "reader two", "reader three"]
    d={'reader '+str(k):v for k,v in enumerate(lis)}
    

    Output:

    {'reader 0': 'reader one',
     'reader 1': 'reader two',
     'reader 2': 'reader three'}