Cue / scales

scales - Metrics for Python
Apache License 2.0
920 stars 73 forks source link

Example on how to do dynamic stats collection? #2

Closed joeshaw closed 12 years ago

joeshaw commented 12 years ago

(I couldn't find any other good contact info for this software, so I'm filing it as an issue. It would be good to add an example of this to the README. :)

What's the best way to dynamically define stats to collect? I don't know until runtime what the different possible values are. (Think different "types" that are pulled from a database at runtime.)

data = get_data()
stats = scales.collection('/data', *[scales.IntStat(n) for n in names])

for d in data:
    # increment the count
    setattr(stats, d, getattr(stats, d) + 1)

This is kinda ugly but it works. Is there a nicer way?

robbywalker commented 12 years ago

You're probably looking for an IntDictStat - I added an example in the Class Stats section of the README. Thanks!

joeshaw commented 12 years ago

Awesome! This is exactly what I was looking for, thank you.