Hozer Central

Mon, 29 Oct 2007

Sorting a python list of dictionaries

Lately I've been using lists of dictionaries a lot to store data to be processed sequentially. More often than not, the data gets loaded into the list in an ordered state, but I needed a way to force it to be ordered by a key of the dictionary. Luckily the sort() list function takes as an argument a function to do the comparison for me. Since the comparison is just a simple is this key less than another key I made my first venture into lambda functions:

l = [ {"name": "1234", "attribute1": "this is an attribute", "attribute2": "this is another"},
        {"name": "4325", "attribute1": "this is an attribute", "attribute2": "this is another"},
        {"name": "3145", "attribute1": "this is an attribute", "attribute2": "this is another"} ]

l.sort( lambda x, y: cmp( x["name"], y["name"] ) )

Ok, so i'll stop lying. The list of dictionaries is a list of Networker NSR Client entries. Each Networker entries is a dictionary relating to the attribute: value; of a Networker client. I need to sort this list so when we have multiple clients for the same server, they appear together when I process them. Using a lambda function as the function to pass to sort() accomplishes this since the lambda function is only calling the cmp() function on the 2 name attributes of 2 clients.

Quick, easy and reliable. Happy Hacking!

posted at: 23:35 | permanent link to this entry

Wed, 07 Feb 2007

nsradmin: or how I stopped clicking and learned to love the cli

Recently on the Networker Mailing List, a windows admin was venturing into the promised land of cli access to networker and wanted some points in the right direction. Since I typed it up, I figure I'll post it here as well so I can find it next time someone asks.

In nsradmin you set a query and then act on it. A query is any set of key and value pairs (attributes). To set the query you use the `.` or `print` command. The benefit of `print` being that it sets the current query and then prints out the attributes returned by that query. Examples are probably easier to understand than theory.

(As I don't have a networker installation to access at the moment all the example output is from memory, the exact output of the various commands may vary)

  1. To print a client's config:
    nsradmin> print type: nsr client; name: client.example.org
    

    This sets the query to any resources that match the named attributes. Here we are using the `type` attribute to restrict to `nsr client` and the `name` attribute to restrict the query to only those matching `client.example.org`. Since we used the `print` command, the results of the query are immediately displayed. The exact same output can be achieved by using:

    nsradmin> . type: nsr client; name: client.example.org
    Current query set.
    nsradmin> print
    
  2. To update a client's config:
    nsradmin> . type: nsr client; name: client.example.org
    Current query set.
    nsradmin> update aliases: "www.example.org"
    

    First the query is set with the `.` command, and then the resources modified are updated. IMPORTANT: if you set a query that returns more than one resource you will update all those resources! Be careful with your queries and be as restrictive as possible. Doing something like:

    nsradmin> . type: nsr client; schedule: default
    Current query set.
    nsradmin> update aliases: "bad.example.org"
    

    Will set the alias for every client with the `default` schedule. Many a first time nsradmin adventurer have made mistakes like this.

    You could just as well use `print` instead of `.`, and I in fact recommend it as a safeguard to accidentally having to stay up for days fixing client configs. ;)

  3. Deleting a group:
    nsradmin> delete type: nsr group; name: MyGroup
    <output of query>
    Delete? yes
    

    You could also split this out using the `.` command to set the query and then the `delete` command to act upon the query.

If you can, I would recommend setting up a linux box (or installing cygwin) and installing the Networker man pages. The syntax of nsradmin is very well documented in the nsr_resource man page.

posted at: 04:47 | permanent link to this entry

< October 2007
MoTuWeThFrSaSu
1 2 3 4 5 6 7
8 91011121314
15161718192021
22232425262728
293031    

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License .

Powered by PyBlosxom.

Icons from the Tango Project.