On Sat, Sep 20 2014, Sriram Karra wrote:

[...]

> But your problem can be addressed by naming the functions after what they
> do - after all your consolidate function does something tangible other than
> just return a different type of data.

[...]

Let me give you a specific example. 

The psutils library has a function to get cpu times (idle, busy
etc.). It can either do this per CPU (in which case, it will return a
list of CPUTime objects, one per CPU) or give you the total (in which
case, you'll get back a single CPUTime object). This is controlled by a
per_cpu boolean flag.

You can write a separate method to sum the individual times and then
keep the "conslidation" outside get_cpu_times.

The library chooses to sometimes return a list and sometimes return a
single item. I don't like that. 

Now, I can write two functions like get_cpu_times and
get_cpu_times_per_cpu. I don't particularly like that either.

>From the thread so far, Anand's solution is the one I like
best. Although it's something tailored for my problem rather than a
general pattern.


-- 
Cordially,
Noufal
http://nibrahim.net.in
_______________________________________________
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers

Reply via email to