Skip to main content

filename_generator_function.py


# -*- coding: utf-8 -*-
"""
Exercise: File name generator function
Do the file name generator challenge (Part 1) if you haven't done it yet.
Turn the program into a function with
	Arguments:
            number: id of sample
            location : sample location
            date : date of sampling
	Return value:
            A string containing the name of the file which will contain the data
Example of usage:
>>> filename=filenamegen(1, 'Ilkley', '10oct2013')
>>> print filename
Sample_Ilkley_10oct2013_site1.dat
2. Make the arguments location and date optional.
If no value for location and date is provided,
return the value 'Sample_sitenumber.dat' where number is the sample id.
>>> filename=filenamegen(23)
>>> print filename
Sample_site23.dat
Author: Lauren J Gregoire
"""
def filenamegen(number, location=None, date=None):
    mylist=['Sample']
    if location:
        mylist.append(location)
    if date:
        mylist.append(date)
    mylist.append('site'+str(number))
    print mylist
    filename='_'.join(mylist)+'.dat'
    return filename
filename = filenamegen(1, location='Ilkley',date='10oct2013')
print filename
filename = filenamegen(23)
print filename