diff --git a/Chapter2/module_reusable/main_panda.py b/Chapter2/module_reusable/main_panda.py new file mode 100644 index 0000000..c140265 --- /dev/null +++ b/Chapter2/module_reusable/main_panda.py @@ -0,0 +1,15 @@ + +import mypanda + +def my_main(): + + dict = {"country": ["Brazil", "Russia", "India", "China", "South Africa"], + "capital": ["Brasilia", "Moscow", "New Dehli", "Beijing", "Pretoria"], + "area": [8.516, 17.10, 3.286, 9.597, 1.221], + "population": [200.4, 143.5, 1252, 1357, 52.98]} + + mypanda.print_dataframe (dict) + +""" 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/module_reusable/mypanda.py b/Chapter2/module_reusable/mypanda.py new file mode 100644 index 0000000..d7b0c42 --- /dev/null +++ b/Chapter2/module_reusable/mypanda.py @@ -0,0 +1,9 @@ +# mypanda.py with default and custom random functions +import pandas + +def print_dataframe(dict): + """This function output a dictionary as a data frame """ + + brics = pandas.tDataFrame(dict) + print(brics) + diff --git a/Chapter2/module_reusable/myrandomv2.py b/Chapter2/module_reusable/myrandomv2.py new file mode 100644 index 0000000..34d3596 --- /dev/null +++ b/Chapter2/module_reusable/myrandomv2.py @@ -0,0 +1,15 @@ +# myrandomv2.py with default and custom random functions +import random + +#random = __import__('random') + +#random = importlib.import_module('random') +print (globals()) + +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) diff --git a/Chapter2/modules_import/calcmain1.py b/Chapter2/modules_import/calcmain1.py new file mode 100644 index 0000000..2dd2b29 --- /dev/null +++ b/Chapter2/modules_import/calcmain1.py @@ -0,0 +1,18 @@ +# calcmain1.py with a main function +import mycalculator +import myrandom + +def my_main( ): + """ This is a main function which generates two random numbers and then apply calculator functions on them """ + x = myrandom.random_2d( ) + y = myrandom.random_1d( ) + sum = mycalculator.add(x, y) + diff = mycalculator.subtract(x, y) + + print("x = { }, y = { }".format(x, y)) + print("sum is { }".format(sum)) + print("diff is { }".format(diff)) + +""" This is executed only if the special variable '__name__' is set as main""" +if __name__ == "__main__": + my_main() diff --git a/Chapter2/modules_import/calcmain2.py b/Chapter2/modules_import/calcmain2.py new file mode 100644 index 0000000..1579909 --- /dev/null +++ b/Chapter2/modules_import/calcmain2.py @@ -0,0 +1,22 @@ +# calcmain2.py with alias for modules +import mycalculator as calc +import myrandom as rand + + +def my_main(): + """ This is a main function which generates two random numbers and then apply calculator functions on them """ + x = rand.random_2d() + y = rand.random_1d() + + sum = calc.add(x,y) + diff = calc.subtract(x,y) + + print("x = {}, y = {}".format(x,y)) + print("sum is {}".format(sum)) + print("diff is {}".format(diff)) + + print (globals()) + +""" 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/modules_import/calcmain3.py b/Chapter2/modules_import/calcmain3.py new file mode 100644 index 0000000..e353be1 --- /dev/null +++ b/Chapter2/modules_import/calcmain3.py @@ -0,0 +1,22 @@ +# calcmain3.py with from and alias combined +from mycalculator import add as my_add +from mycalculator import subtract as my_subtract +from myrandom import random_2d, random_1d + +def my_main(): + """ This is a main function which generates two random numbers and then apply calculator functions on them """ + x = random_2d() + y = random_1d() + + sum = my_add(x,y) + diff = my_subtract(x,y) + + print("x = {}, y = {}".format(x,y)) + print("sum is {}".format(sum)) + print("diff is {}".format(diff)) + + print (globals()) + +""" 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/modules_import/globalmain.py b/Chapter2/modules_import/globalmain.py new file mode 100644 index 0000000..4d91393 --- /dev/null +++ b/Chapter2/modules_import/globalmain.py @@ -0,0 +1,10 @@ +# globalmain.py with globals function + +def print_globals(): + print (globals()) + +def hello(): + print ("Hello") + +if __name__ == "__main__": + print_globals() \ No newline at end of file diff --git a/Chapter2/modules_import/mycalculator.py b/Chapter2/modules_import/mycalculator.py new file mode 100644 index 0000000..e695dca --- /dev/null +++ b/Chapter2/modules_import/mycalculator.py @@ -0,0 +1,10 @@ +# 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/modules_import/myrandom.py b/Chapter2/modules_import/myrandom.py new file mode 100644 index 0000000..26ccc6c --- /dev/null +++ b/Chapter2/modules_import/myrandom.py @@ -0,0 +1,19 @@ +# myrandom.py with default and custom random functions +import random + +#random = __import__('random') + +#random = importlib.import_module('random') +print (globals()) + +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