[Tutor] Pulling items from a dict in a print command

Peter Otten __peter__ at web.de
Wed Jun 18 22:05:16 CEST 2014


Ben Sherman wrote:

> Whats a more pythony way to do this?  I have a dict with a few dozen
> elements, and I want to pull a few out.  I've already shortened it with
> itemgetter, but it still seems redundant.  I feel like I can do something
> like I've seen with *kwargs, but I'm not sure.
> 
> I'm using old style sprintf formatting, so feel free to show me a better
> way to do this with the new way.
> 
> Thanks for the help!
> 
> Code:
> 
>     print(("overall_status=%s|" +
>             "mon_count=%s," +
>             "healthy_mons=%s," +
>             "pg_count=%s," +
>             "pg_clean_count=%s," +
>             "osd_count=%s," +
>             "osd_up=%s," +
>             "osd_in=%s," +
>             "bytes_avail=%s," +
>             "bytes_used=%s," +
>             "bytes_total=%s") %
>             itemgetter("overall_status",
>             "mon_count",
>             "healthy_mons",
>             "pg_count",
>             "pg_clean_count",
>             "osd_count",
>             "osd_up",
>             "osd_in",
>             "bytes_avail",
>             "bytes_used",
>             "bytes_total")(parsed_json))

names = ["overall_status", "mon_count", "pg_count", ...]
print(", ".join("{}={}".format(name, parsed_json[name]) for name in names))




More information about the Tutor mailing list