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.

Recommended Answers

All 3 Replies

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
    ly=0
    if (y-2000)%4==0: ly=1
    md=(31,28+ly,31,30,31,30,31,31,30,31,30,31)
    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

so:

def ymd2doy(y,m,d):
    if y<100: y+=2000
    ly=0
    if leapyr(y): ly=1
    md=(31,28+ly,31,30,31,30,31,31,30,31,30,31)
    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:
http://my.safaribooksonline.com/book/programming/python/0596007973/python-shortcuts/pythoncook2-chp-4-sect-22

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).'''
    try:
        f(*a, **k)
    except t:
        return True
    else:
        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')    
commented: interesting method +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.