Hi all,
In several occasions, I found myself looking for a function that would
take a value and restrict is within a specified set of boundaries. For
a 1-dimension value, I could simply write
min(max(value,theMin),theMax)
to restrict value within the range made of theMin to theMax.
It assumes that theMax >= theMin.
I was looking for a single call to restrict a value within bounds that
would do pretty much that, so i wrote this trivial one:
def restrain(value, theMin, theMax) :
"""Return a value that is in restricted to the [theMin, theMax] range.
**Example** for val in xrange(-1,7,1):
... print "restrain(%d,1,5) = %d" % (val, restrain(val,1,5))
...
restrain(-1,1,5) = 1
restrain(0,1,5) = 1
restrain(1,1,5) = 1
restrain(2,1,5) = 2
restrain(3,1,5) = 3
restrain(4,1,5) = 4
restrain(5,1,5) = 5
restrain(6,1,5) = 5
"""
assert(theMax >= theMin)
return min(max(value,theMin),theMax)
Without the assertion check, the restrain() named function runs
expectedly slower than the explicit min(max()) calls:
C:\Python23\Lib>timeit "min(max(5,1),0)"
1000000 loops, best of 3: 1.02 usec per loop
C:\Python23\Lib>timeit -s"from ut import restrain" "restrain(5,1,6)"
1000000 loops, best of 3: 1.53 usec per loop
Again, I found myself writing code that has to restrain the values to
some range and prefer using the restrain() function instead of the
min(max()) one.
Therefore, I was wondering if it would it make sense to add a function
like restrain() to the list of Python built-ins. Or is there something
like that already in the Python library?
Thanks
Pierre 2 1610
"Pierre Rouleau" <pr******@impathnetworks.com> wrote in message
news:xK********************@news20.bellglobal.com. .. Hi all,
In several occasions, I found myself looking for a function that would take a value and restrict is within a specified set of boundaries. For a 1-dimension value, I could simply write
min(max(value,theMin),theMax)
to restrict value within the range made of theMin to theMax. It assumes that theMax >= theMin.
I was looking for a single call to restrict a value within bounds that would do pretty much that, so i wrote this trivial one:
def restrain(value, theMin, theMax) : """Return a value that is in restricted to the [theMin, theMax]
range. **Example**
>>> for val in xrange(-1,7,1): ... print "restrain(%d,1,5) = %d" % (val, restrain(val,1,5)) ... restrain(-1,1,5) = 1 restrain(0,1,5) = 1 restrain(1,1,5) = 1 restrain(2,1,5) = 2 restrain(3,1,5) = 3 restrain(4,1,5) = 4 restrain(5,1,5) = 5 restrain(6,1,5) = 5 >>> """
assert(theMax >= theMin) return min(max(value,theMin),theMax)
Without the assertion check, the restrain() named function runs expectedly slower than the explicit min(max()) calls:
C:\Python23\Lib>timeit "min(max(5,1),0)" 1000000 loops, best of 3: 1.02 usec per loop
C:\Python23\Lib>timeit -s"from ut import restrain" "restrain(5,1,6)" 1000000 loops, best of 3: 1.53 usec per loop
Again, I found myself writing code that has to restrain the values to some range and prefer using the restrain() function instead of the min(max()) one.
Therefore, I was wondering if it would it make sense to add a function like restrain() to the list of Python built-ins. Or is there something like that already in the Python library?
Thanks
Pierre
I know I've wanted that on (infrequent) occasion. I think
"minmax" makes a better name, though, and I suspect it
would have to be implemented in C to make any sense,
performancewise.
John Roth
John Roth wrote: "Pierre Rouleau" <pr******@impathnetworks.com> wrote in message news:xK********************@news20.bellglobal.com. ..
Hi all,
In several occasions, I found myself looking for a function that would take a value and restrict is within a specified set of boundaries. For a 1-dimension value, I could simply write
min(max(value,theMin),theMax)
to restrict value within the range made of theMin to theMax. It assumes that theMax >= theMin.
I was looking for a single call to restrict a value within bounds that would do pretty much that, so i wrote this trivial one:
def restrain(value, theMin, theMax) : """Return a value that is in restricted to the [theMin, theMax]
range.
**Example**
>>> for val in xrange(-1,7,1): ... print "restrain(%d,1,5) = %d" % (val, restrain(val,1,5)) ... restrain(-1,1,5) = 1 restrain(0,1,5) = 1 restrain(1,1,5) = 1 restrain(2,1,5) = 2 restrain(3,1,5) = 3 restrain(4,1,5) = 4 restrain(5,1,5) = 5 restrain(6,1,5) = 5 >>> """
assert(theMax >= theMin) return min(max(value,theMin),theMax)
Without the assertion check, the restrain() named function runs expectedly slower than the explicit min(max()) calls:
C:\Python23\Lib>timeit "min(max(5,1),0)" 1000000 loops, best of 3: 1.02 usec per loop
C:\Python23\Lib>timeit -s"from ut import restrain" "restrain(5,1,6)" 1000000 loops, best of 3: 1.53 usec per loop
Again, I found myself writing code that has to restrain the values to some range and prefer using the restrain() function instead of the min(max()) one.
Therefore, I was wondering if it would it make sense to add a function like restrain() to the list of Python built-ins. Or is there something like that already in the Python library?
Thanks
Pierre
I know I've wanted that on (infrequent) occasion. I think "minmax" makes a better name, though, and I suspect it would have to be implemented in C to make any sense, performancewise.
John Roth
I would prefer 'limit' than 'minmax' myself if 'restrain' was not
'retained' ;-)
Pierre Rouleau This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Blair Hall |
last post by:
Can anyone please tell me how to correctly use a built in function
when there is a function of the same name in local scope?
Here is an example. Suppose the following is in myApply.py:
def...
|
by: Gonçalo Rodrigues |
last post by:
Hi,
Does anyone know if and how I can, from within Python, read the
signatures of builtin methods/functions?
The following fails:
>>> import inspect
>>> inspect.getargspec(list.append)...
|
by: BJörn Lindqvist |
last post by:
A problem I have occured recently is that I want to subclass builtin
types. Especially subclassing list is very troublesome to me. But I
can't find the right syntax to use. Take for example this...
|
by: Stephen Ferg |
last post by:
Python has a builtin class for staticmethod. Seems to me that Python
should also have a builtin class for abstractmethod. Something like
this...
#######################################
#...
|
by: rmm |
last post by:
If I replace the open builtin eg
import main
__main__.__builtins__.open=None
Is there any way, from here on, to access the original open function??
Extending this slightly, lets say I put a...
| |
by: Shayne Wissler |
last post by:
I've overloaded the global new operator, so that I can, detect when I've run
out of memory:
extern void* operator new(size_t s) {
void *r = malloc(s);
if (!r && s) {
fprintf(stderr, "Error: No...
|
by: Anders K. Olsen |
last post by:
Hello group
I'm trying to list the users and groups who has read access to a file.
I use .NET 2.0 and FileInfo.GetAccessControl().GetAccessRules(...) and then
loop through the...
|
by: nejucomo |
last post by:
Hi folks,
Quick Synopsis:
A test script demonstrates a memory leak when I use pythonic extensions
of my builtin types, but if I use the builtin types themselves there is
no memory leak.
...
|
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= |
last post by:
Is there some way to get a function-pointer to the operators of the
builtin types? In the following example I have a generic function which
applies an operator on two values, however it does not...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |