[Numpy-discussion] Style for pad implementation in 'pad' namespace or functions under np.lib

Nathaniel Smith njs@pobox....
Thu Mar 29 11:56:40 CDT 2012


On Thu, Mar 29, 2012 at 5:45 PM, Charles R Harris
<charlesr.harris@gmail.com> wrote:
>
>
> On Thu, Mar 29, 2012 at 10:38 AM, Nathaniel Smith <njs@pobox.com> wrote:
>>
>> On Thu, Mar 29, 2012 at 5:13 PM, Travis Oliphant <travis@continuum.io>
>> wrote:
>> > While namespaces are a really good idea, I'm not a big fan of both
>> > "module"
>> > namespaces and underscore namespaces.   It seems pretty redundant to me
>> > to
>> > have pad.pad_mean.
>> >
>> > On the other hand, one could argue that pad.mean could be confused with
>> > calculating the mean of a padded array.   So, it seems like the function
>> > names need to be called something more than just "mean, median, etc."
>> > Something like padwith_mean, padwith_median, etc. actually makes more
>> > sense.
>> >   Or pad.with_mean, pad.with_median.   The with_ in this case is not
>> > really
>> > a "namespace" it's an indication of functionality.
>>
>> Perhaps it should be
>>  pad(..., mode="mean")
>>
>> , mode="" is only a few more characters than _with_, and this would
>> make it much easier to write functions whose API looks like:
>>    wavelet_decompose(..., pad_mode=....)
>>
>> Also it would solve the namespace question :-).
>>
>
> I like this idea. I'd leave the current functions in the module for those
> who want to import a particular padding type, but import the pad(...,
> mode="xxx") into the numpy namespace.

Your call, but that's about 500 lines of highly-redundant docstrings
to maintain, just to save the occasional person from having to type
one line:
  def pad_with_mean(*args, **kwargs): np.lib.pad(*args, mode="mean", **kwargs)

The unified implementation really is just
  PADDERS = {"mean": _mean, "median": _median, ...}
  return _loop_across(array, pad_width, _PADDERS[mode],
constant_values=constant_values, end_values=end_values)

-- Nathaniel


More information about the NumPy-Discussion mailing list