diff --git a/Chapter2/mypackage_mains/pkgmain3.py b/Chapter2/mypackage_mains/pkgmain3.py new file mode 100644 index 0000000..1c24894 --- /dev/null +++ b/Chapter2/mypackage_mains/pkgmain3.py @@ -0,0 +1,27 @@ +# pkgmain3.py with with main function + +import masifutil + + +def my_main(): + """ This is a main function which generates two random numbers and then apply calculator functions on them """ + x = masifutil.random_2d() + y = masifutil.random_1d() + + sum = masifutil.add(x,y) + diff = masifutil.subtract(x,y) + + sroot = masifutil.sqrt(x) + log10x = masifutil.log(x) + log2x = masifutil.ln(x) + + print("x = {}, y = {}".format(x, y)) + print("sum is {}".format(sum)) + print("diff is {}".format(diff)) + print("square root is {}".format(sroot)) + print("log base of 10 is {}".format(log10x)) + print("log base of 2 is {}".format(log2x)) + +""" This is executed only if the special variable '__name__' is set as main""" +if __name__ == "__main__": + my_main() \ No newline at end of file diff --git a/Chapter2/mypackage_mains/pkgmain4.py b/Chapter2/mypackage_mains/pkgmain4.py new file mode 100644 index 0000000..96c09a5 --- /dev/null +++ b/Chapter2/mypackage_mains/pkgmain4.py @@ -0,0 +1,30 @@ +# pkgmain4.py with with main function + +import sys +sys.path.append('/Users/muasif/Google Drive/PythonForGeeks/source_code/chapter2/mypackages') + +import masifutil + + +def my_main(): + """ This is a main function which generates two random numbers and then apply calculator functions on them """ + x = masifutil.random_2d() + y = masifutil.random_1d() + + sum = masifutil.add(x,y) + diff = masifutil.subtract(x,y) + + sroot = masifutil.sqrt(x) + log10x = masifutil.log(x) + log2x = masifutil.ln(x) + + print("x = {}, y = {}".format(x, y)) + print("sum is {}".format(sum)) + print("diff is {}".format(diff)) + print("square root is {}".format(sroot)) + print("log base of 10 is {}".format(log10x)) + print("log base of 2 is {}".format(log2x)) + +""" This is executed only if the special variable '__name__' is set as main""" +if __name__ == "__main__": + my_main() \ No newline at end of file diff --git a/Chapter2/mypackage_mains/pkgmain5.py b/Chapter2/mypackage_mains/pkgmain5.py new file mode 100644 index 0000000..86ddfc5 --- /dev/null +++ b/Chapter2/mypackage_mains/pkgmain5.py @@ -0,0 +1,30 @@ +# pkgmain5.py with with main function + +import site +site.addsitedir('/Users/muasif/Google Drive/PythonForGeeks/source_code/chapter2/mypackages') + +import masifutil + + +def my_main(): + """ This is a main function which generates two random numbers and then apply calculator functions on them """ + x = masifutil.random_2d() + y = masifutil.random_1d() + + sum = masifutil.add(x,y) + diff = masifutil.subtract(x,y) + + sroot = masifutil.sqrt(x) + log10x = masifutil.log(x) + log2x = masifutil.ln(x) + + print("x = {}, y = {}".format(x, y)) + print("sum is {}".format(sum)) + print("diff is {}".format(diff)) + print("square root is {}".format(sroot)) + print("log base of 10 is {}".format(log10x)) + print("log base of 2 is {}".format(log2x)) + +""" This is executed only if the special variable '__name__' is set as main""" +if __name__ == "__main__": + my_main() \ No newline at end of file diff --git a/Chapter2/mypackages/masifutil/__init__.py b/Chapter2/mypackages/masifutil/__init__.py new file mode 100644 index 0000000..4b6a106 --- /dev/null +++ b/Chapter2/mypackages/masifutil/__init__.py @@ -0,0 +1,4 @@ + +from .mycalculator import add, subtract +from .myrandom import random_1d, random_2d +from .advcalc.advcalculator import sqrt, log, ln \ No newline at end of file diff --git a/Chapter2/mypackages/masifutil/advcalc/advcalculator.py b/Chapter2/mypackages/masifutil/advcalc/advcalculator.py new file mode 100644 index 0000000..588eee1 --- /dev/null +++ b/Chapter2/mypackages/masifutil/advcalc/advcalculator.py @@ -0,0 +1,14 @@ +# advcalculator.py with sqrt,log and ln functions +import math + +def sqrt(x): + """This function takes square root of a number""" + return math.sqrt(x) + +def log(x): + """This function returns log of base 10""" + return math.log(x,10) + +def ln(x): + """This function returns log of base 2""" + return math.log(x,2) \ No newline at end of file diff --git a/Chapter2/mypackages/masifutil/mycalculator.py b/Chapter2/mypackages/masifutil/mycalculator.py new file mode 100644 index 0000000..7872eac --- /dev/null +++ b/Chapter2/mypackages/masifutil/mycalculator.py @@ -0,0 +1,9 @@ +# mycalculator.py with add and subtract functions + +def add(x, y): + """This function adds two numbers""" + return x + y + +def subtract(x, y): + """This function subtracts two numbers""" + return x - y diff --git a/Chapter2/mypackages/masifutil/myrandom.py b/Chapter2/mypackages/masifutil/myrandom.py new file mode 100644 index 0000000..b4a45fd --- /dev/null +++ b/Chapter2/mypackages/masifutil/myrandom.py @@ -0,0 +1,14 @@ +# myrandom.py with default and custom random functions +import random + +def random_1d(): + """This function get a random number between 0 and 9""" + return random.randint(0,9) + +def random_2d(): + """This function get a random number between 10 and 99""" + return random.randint(10,99) + +def get_random(lower, upper): + """This function get a random number between lower and upper""" + return random.randint(lower,upper) \ No newline at end of file