Hello good people, I wanted to talk today about auxiliary functions. I have a brief understanding of what it is, however, I am still rather confused on what it actually does.

Lets say for example I have a function that calculates leapyear as TRUE or FALSE, how would I use it as an auxiliary function? How would it be incorporated into another functions, lets say one that determines whether or not a date is real or not.

Many thanks for any claifications in advance.

3 Years
Discussion Span
Last Post by slate

Here's my 2 cents:
Let's say I have a function that turns Year,month,day into day of year:

def ymd2doy(y,m,d):
    if y<100: y+=2000
    if (y-2000)%4==0: ly=1
    for i in xrange(m-1): d+=md[i]
    return d

Instead of if (y-2000)%4==0: ly=1 I want to use a function:

def leapyr (y):
   return (y-2000)%4==0


def ymd2doy(y,m,d):
    if y<100: y+=2000
    if leapyr(y): ly=1
    for i in xrange(m-1): d+=md[i]
    return d

Is that what you meant?


You're amazing rrashkin! Yes that is what I am looking for, you made it rather simple to understand. Many thanks for the help. (:


Some remarks...

I would say that a function is auxiliary if it is called and used by a limited set of functions (methods, other callables) and does not provide functionality(meaning) on its own.

If you write some date arithmetic module, then isleapyear appears not to be an auxiliary function. It has functionality, that can be understood and possibly used by the user (caller) of the module.

Here is an example of an auxiliary function:

Your original question was to determine if a date is valid:

def throws(t, f, *a, **k):
    '''Return True iff f(*a, **k) raises an exception whose type is t
      (or, one of the items of _tuple_ t, if t is a tuple).'''
        f(*a, **k)
    except t:
        return True
        return False

def is_valid_date(date_string):
    import datetime
    return not throws(ValueError,datetime.datetime.strptime,date_string, '%Y-%m-%d')

print is_valid_date('2014-02-28')
print is_valid_date('2014-02-29')    
Votes + Comments
interesting method
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.