<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <span style="color
Step1:  Writing a Module
There will always be some code that you have to write yourself. When you're working on a single script that does the same things repeatedly, it makes sense to start writing functions within the script. However, you may also have operations that are carried out in multiple scripts for various projects. In this case, we'd like to be able to use that same function in all scripts. This will be very similar to writing a single function, so we'll try out the function, but the file that contains our module is stellar_radius.py
For this function, we'll say that we want to be able to take the mass of a star and calculate the radius. For smaller-mass stars, we can approximate this as $R=M^{0.8}$
Step2:  This is a really simple equation, but by making this a function, we can make it more sophisticated or change the exponent in one spot, and it will be updated everywhere. We can quickly test this out to see that the function works the way that we expect when called.
Step3:  You can also pass a NumPy array to the function
Step4:  If we want to use this module that we've created, we can inport it as we have with other modules. When we import it, we're using the file name, and then when we call the function, we have to include the function name and for this module those two are the same. These could also be different, where the file name is one thing, and then it contains one or more functions that have names that don't match it.
Step5:  This is what the "stellar_radius.py" file looks like
Step6:  This all works for if we simply want to import the function into another python function, but it can often be useful to be able to run these functions from a command line (for example, so that we can use execute this python function in another scripting language or so that we can run it from the command line for quick calculations)
The simplest form of a module file just contains a series of functions without running any of them, but we can add some additional code to the module file that will be run if this file is executed rather than imported.
This code consists of an if statement. The __name__ property relates to what the code is called, and if the file is called directly, it will be True. If the code is imported, then this code won't be executed. What follows is a single line that calls the stellar radius function using the argument that will get passed in from the command line.
python
    if __name__ == "__main__"
Step8:  The structure of your directory would be
Step10:  And one that computes the square roots for a given number _n_.
Step11:  For each folder and subfolder, you need a "__init__.py" file
This is how it looks like
Step12:  And for the one in the adv folder. This file can be empty
Step13:  The last step to take when creating your own modules is to append your PYTHONPATH.
<ASSISTANT_TASK:>
Python Code:
# %load ./arguments.py
#!/usr/bin/python
import sys
# it's easy to print this list of course:
print sys.argv
# or it can be iterated via a for loop:
for i in range(len(sys.argv)):
    if i == 0:
        print "Function name: %s" % sys.argv[0]
    else:
        print "%d. argument: %s" % (i,sys.argv[i])
# copied from http://www.python-course.eu/sys_module.php
Explanation: <span style="color:blue">Week 13 - Sys and Modules</span>
<span style="color:red">Today's Agenda</span>
Sys  
Writing your own module
Adding your module to you Python Path
Sys
The sys module provides a lot of information about the Python interpreter. The functionality that we're interested in is the ability to use sys to parse information from the command line. This can be used to make programs and modules useful when run from the command line.
After importing sys, sys.argv provides a list of all the parameters that the current python script was called with. A program that doesn't take any paramters will only have one item in this list, where the 0th item is the name of the program. Any paramters that are passed into the script will be listed after.
From a command line, try running the following in the notebooks directory:  
```sh
python arguments.py arg1 arg2
```
In this script from http://www.python-course.eu/sys_module.php, we see that the 0th term in sys.argv is arguments.py and the rest of the list is the parameters we passed in. This will come in handy with our module.
End of explanation
def stellar_r(mass):
   starR=mass**0.8
   return starR
Explanation: Writing a Module
There will always be some code that you have to write yourself. When you're working on a single script that does the same things repeatedly, it makes sense to start writing functions within the script. However, you may also have operations that are carried out in multiple scripts for various projects. In this case, we'd like to be able to use that same function in all scripts. This will be very similar to writing a single function, so we'll try out the function, but the file that contains our module is stellar_radius.py
For this function, we'll say that we want to be able to take the mass of a star and calculate the radius. For smaller-mass stars, we can approximate this as $R=M^{0.8}$
End of explanation
print(stellar_r(1))
print(stellar_r(0.9))
Explanation: This is a really simple equation, but by making this a function, we can make it more sophisticated or change the exponent in one spot, and it will be updated everywhere. We can quickly test this out to see that the function works the way that we expect when called.
End of explanation
%matplotlib inline
import matplotlib.pyplot
import numpy as np
mass_arr = np.arange(0.8, 5, 0.1)
stellar_r( mass_arr )
Explanation: You can also pass a NumPy array to the function
End of explanation
import stellar_radius as staR
print staR.stellar_radius(0.9)
Explanation: If we want to use this module that we've created, we can inport it as we have with other modules. When we import it, we're using the file name, and then when we call the function, we have to include the function name and for this module those two are the same. These could also be different, where the file name is one thing, and then it contains one or more functions that have names that don't match it.
End of explanation
# %load ./stellar_radius.py
#!/usr/bin/env python
import numpy as np
import sys
def stellar_radius(mass):
   starR=mass**0.8
   return starR
if __name__ == "__main__":
   print stellar_radius(float(sys.argv[1])) #mass
Explanation: This is what the "stellar_radius.py" file looks like:
End of explanation
!mkdir "$HOME/custom_utilities"
!mkdir "$HOME/custom_utilities/python_modules/"
Explanation: This all works for if we simply want to import the function into another python function, but it can often be useful to be able to run these functions from a command line (for example, so that we can use execute this python function in another scripting language or so that we can run it from the command line for quick calculations)
The simplest form of a module file just contains a series of functions without running any of them, but we can add some additional code to the module file that will be run if this file is executed rather than imported.
This code consists of an if statement. The __name__ property relates to what the code is called, and if the file is called directly, it will be True. If the code is imported, then this code won't be executed. What follows is a single line that calls the stellar radius function using the argument that will get passed in from the command line.
python
    if __name__ == "__main__":  
       print stellar_radius(float(sys.argv[1])) #mass
We can test this out, then, by going into the directory for the notebook and calling the code from the terminal
    python stellar_radius.py 0.9  
We could also call this code from another operating language if needed.
Adding your modules to your Python Path
Normally, you want to have a unique folder, in which you save all of your modules and you can reuse them whenever you want.
This can be done by first creating a folder in your path.
Let's first create a folder with the name "custom_utilities" in your directory
End of explanation
# %load ./modules/adv/fib.py
#! /usr/bin/env python
# NAME
# DATE
# Vanderbilt University
from __future__ import print_function, division, absolute_import
__author__     =['YOUR NAME']
__copyright__  =["Copyright 2017 YOUR NAME, Name of Project"]
__email__      =['Email Address']
__maintainer__ =['Your Name']
__all__["add", "division", "multiply", "subtract"]
from math import sqrt
 
#----------------------------------------------------------------------
def fibonacci(n):
    
    http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python
    
    return ((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5))
Explanation: The structure of your directory would be:
sh
myMath/
    __init__.py
    adv/
        __init__.py
        sqrt.py
        fib.py
    add.py
    subtract.py
    multiply.py
    divide.py
For example, let's create a module that computes the fibonacci sequence for a given n, under "./modules/adv/fib.py"
End of explanation
# %load ./modules/adv/sqrt.py
#! /usr/bin/env python
# NAME
# DATE
# Vanderbilt University
from __future__ import print_function, division, absolute_import
__author__     =['YOUR NAME']
__copyright__  =["Copyright 2017 YOUR NAME, Name of Project"]
__email__      =['Email Address']
__maintainer__ =['Your Name']
__all__["add", "division", "multiply", "subtract"]
import math
 
#----------------------------------------------------------------------
def squareroot(n):
    
    return math.sqrt(n)
Explanation: And one that computes the square roots for a given number _n_.
End of explanation
# %load ./modules/__init__.py
#! /usr/bin/env python
# NAME
# DATE
# Vanderbilt University
from __future__ import print_function, division#, absolute_import
__author__     =['YOUR NAME']
__copyright__  =["Copyright 2017 YOUR NAME, Name of Project"]
__email__      =['Email Address']
__maintainer__ =['Your Name']
__all__ = ["add", "division", "multiply", "subtract", "fibonacci", "squareroot"]
from arithmetic import add
from arithmetic import division
from arithmetic import multiply
from arithmetic import subtract
from adv.fib import fibonacci
from adv.sqrt import squareroot
Explanation: For each folder and subfolder, you need a "__init__.py" file
This is how it looks like
End of explanation
# %load ./modules/adv/__init__.py
Explanation: And for the one in the adv folder. This file can be empty
End of explanation
!echo 'export PYTHONPATH="$HOME/custom_utilities/python_modules/:$PYTHONPATH"' >> $HOME/.bash_profile
Explanation: The last step to take when creating your own modules is to append your PYTHONPATH.
End of explanation
<END_TASK>