From f1f6905df8f0a5f77ddf043bea657db32c82c5d5 Mon Sep 17 00:00:00 2001 From: learnp Date: Sun, 11 Sep 2016 16:24:22 -0400 Subject: [PATCH] reorganized directories --- Basics/13_read_write_file.py | 4 ++ .../14_json_addressbook.py | 0 16_exception.py => Basics/16_exception.py | 0 17_class.py => Basics/17_class.py | 0 18_inheritance.py => Basics/18_inheritance.py | 0 .../19_multiple_inheritance.py | 0 .../20_raise_exception.py | 0 21_iterators.py => Basics/21_iterators.py | 0 22_Generators.py => Basics/22_Generators.py | 0 address.py => Basics/address.py | 0 area.py => Basics/area.py | 0 caller.py => Basics/caller.py | 0 dict_tuple.py => Basics/dict_tuple.py | 0 for.py => Basics/for.py | 0 functions.py => Basics/functions.py | 0 if.py => Basics/if.py | 0 Basics/lists | 40 ++++++++++++++++++ myprogram.py => Basics/myprogram.py | 0 test.py => Basics/test.py | 0 .../variable_numbers_strings.py | 0 Debugging/debugging.py | 11 +++++ Debugging/expenses.txt | 41 +++++++++++++++++++ Multiprocessing/multi_proc.py | 31 -------------- .../multiprocessing_introduction.py | 23 +++++++++++ Multiprocessing/multiprocessing_lock.py | 31 ++++++++++++++ .../multiprocessing_value_array.py | 17 ++++++++ .../multithreading_producer_consumer.py | 20 +++++++++ ...ading.py => multthreading_introduction.py} | 4 +- 28 files changed, 189 insertions(+), 33 deletions(-) create mode 100644 Basics/13_read_write_file.py rename 14_json_addressbook.py => Basics/14_json_addressbook.py (100%) rename 16_exception.py => Basics/16_exception.py (100%) rename 17_class.py => Basics/17_class.py (100%) rename 18_inheritance.py => Basics/18_inheritance.py (100%) rename 19_multiple_inheritance.py => Basics/19_multiple_inheritance.py (100%) rename 20_raise_exception.py => Basics/20_raise_exception.py (100%) rename 21_iterators.py => Basics/21_iterators.py (100%) rename 22_Generators.py => Basics/22_Generators.py (100%) rename address.py => Basics/address.py (100%) rename area.py => Basics/area.py (100%) rename caller.py => Basics/caller.py (100%) rename dict_tuple.py => Basics/dict_tuple.py (100%) rename for.py => Basics/for.py (100%) rename functions.py => Basics/functions.py (100%) rename if.py => Basics/if.py (100%) create mode 100644 Basics/lists rename myprogram.py => Basics/myprogram.py (100%) rename test.py => Basics/test.py (100%) rename variable_numbers_strings.py => Basics/variable_numbers_strings.py (100%) create mode 100644 Debugging/debugging.py create mode 100644 Debugging/expenses.txt delete mode 100644 Multiprocessing/multi_proc.py create mode 100644 Multiprocessing/multiprocessing_introduction.py create mode 100644 Multiprocessing/multiprocessing_lock.py create mode 100644 Multiprocessing/multiprocessing_value_array.py create mode 100644 Multiprocessing/multithreading_producer_consumer.py rename Multiprocessing/{mult_threading.py => multthreading_introduction.py} (91%) diff --git a/Basics/13_read_write_file.py b/Basics/13_read_write_file.py new file mode 100644 index 00000000..73730343 --- /dev/null +++ b/Basics/13_read_write_file.py @@ -0,0 +1,4 @@ +with open("C:\\data\\funny.txt","r") as f: + print(f.read()) + +print(f.closed) \ No newline at end of file diff --git a/14_json_addressbook.py b/Basics/14_json_addressbook.py similarity index 100% rename from 14_json_addressbook.py rename to Basics/14_json_addressbook.py diff --git a/16_exception.py b/Basics/16_exception.py similarity index 100% rename from 16_exception.py rename to Basics/16_exception.py diff --git a/17_class.py b/Basics/17_class.py similarity index 100% rename from 17_class.py rename to Basics/17_class.py diff --git a/18_inheritance.py b/Basics/18_inheritance.py similarity index 100% rename from 18_inheritance.py rename to Basics/18_inheritance.py diff --git a/19_multiple_inheritance.py b/Basics/19_multiple_inheritance.py similarity index 100% rename from 19_multiple_inheritance.py rename to Basics/19_multiple_inheritance.py diff --git a/20_raise_exception.py b/Basics/20_raise_exception.py similarity index 100% rename from 20_raise_exception.py rename to Basics/20_raise_exception.py diff --git a/21_iterators.py b/Basics/21_iterators.py similarity index 100% rename from 21_iterators.py rename to Basics/21_iterators.py diff --git a/22_Generators.py b/Basics/22_Generators.py similarity index 100% rename from 22_Generators.py rename to Basics/22_Generators.py diff --git a/address.py b/Basics/address.py similarity index 100% rename from address.py rename to Basics/address.py diff --git a/area.py b/Basics/area.py similarity index 100% rename from area.py rename to Basics/area.py diff --git a/caller.py b/Basics/caller.py similarity index 100% rename from caller.py rename to Basics/caller.py diff --git a/dict_tuple.py b/Basics/dict_tuple.py similarity index 100% rename from dict_tuple.py rename to Basics/dict_tuple.py diff --git a/for.py b/Basics/for.py similarity index 100% rename from for.py rename to Basics/for.py diff --git a/functions.py b/Basics/functions.py similarity index 100% rename from functions.py rename to Basics/functions.py diff --git a/if.py b/Basics/if.py similarity index 100% rename from if.py rename to Basics/if.py diff --git a/Basics/lists b/Basics/lists new file mode 100644 index 00000000..79263582 --- /dev/null +++ b/Basics/lists @@ -0,0 +1,40 @@ +Lists +===== + +Let us say your expense for every month are listed below, + January - 2200 + February - 2350 + March - 2600 + April - 2130 + May - 2190 +Create a list to store these monthly expenses and using that find out, +In Feb, how many dollars you spent extra compare to January? +Find out your total expense in first quarter (first three months) of the year. +Find out if you spent exactly 2000 dollars in any month +June month just finished and your expense is 1980 dollar. Add this item to our monthly expense list +You returned an item that you bought in a month of April and got a refund of 200$. Make a correction to your monthly expense list based on this. +Answer: +>>> exp = [2200,2350,2600,2130,2190] + +# (a) +>>> exp[1]-exp[0] +150 + +# (b) +>>> exp[0]+exp[1]+exp[2] +7150 + +# (c) +>>> 2000 in exp +False # Means you didn’t spend 2000$ in any of the month + +# (d) +>>> exp.append(1980) +>>> exp +[2200, 2350, 2600, 2130, 2190, 1980] + +# (e) +>>> exp[3] = exp[3] - 200 +>>> exp[3] +1930 + diff --git a/myprogram.py b/Basics/myprogram.py similarity index 100% rename from myprogram.py rename to Basics/myprogram.py diff --git a/test.py b/Basics/test.py similarity index 100% rename from test.py rename to Basics/test.py diff --git a/variable_numbers_strings.py b/Basics/variable_numbers_strings.py similarity index 100% rename from variable_numbers_strings.py rename to Basics/variable_numbers_strings.py diff --git a/Debugging/debugging.py b/Debugging/debugging.py new file mode 100644 index 00000000..1e911aee --- /dev/null +++ b/Debugging/debugging.py @@ -0,0 +1,11 @@ +def add_num(a,b): + '''Return sum of two numbers''' + s=a+b + return s + +n1=input('enter first number:') +n1=int(n1) +n2=input('enter second number:') +n2=int(n2) +s = add_num(n1,n2) +print ('sum is: ',s); \ No newline at end of file diff --git a/Debugging/expenses.txt b/Debugging/expenses.txt new file mode 100644 index 00000000..b011d305 --- /dev/null +++ b/Debugging/expenses.txt @@ -0,0 +1,41 @@ +1230 +2240 +1500 +1678 +2020 +1580 +2240 +1500 +1245 +2300 +1246 +3400 +1580 +2240 +1500 +3240 +2240 +1500 +1245 +2300 +1246 +3400 +1580 +2240 +XYGF +1245 +2300 +1246 +3400 +1580 +2240 +1500 +3240 +2240 +1500 +1245 +2300 +1246 +3400 +1580 +2240 \ No newline at end of file diff --git a/Multiprocessing/multi_proc.py b/Multiprocessing/multi_proc.py deleted file mode 100644 index d7c3d268..00000000 --- a/Multiprocessing/multi_proc.py +++ /dev/null @@ -1,31 +0,0 @@ -import time -import multiprocessing - -def calc_square(numbers): - print("calculate square numbers") - for n in numbers: - time.sleep(20) - print('square:',n*n) - -def calc_cube(numbers): - print("calculate cube of numbers") - for n in numbers: - time.sleep(20) - print('cube:',n*n*n) - -if __name__ == "__main__": - arr = [2,3,8,9] - - t = time.time() - - p1= multiprocessing.Process(target=calc_square, args=(arr,)) - # p2= multiprocessing.Process(target=calc_cube, args=(arr,)) - - p1.start() - # p2.start() - - p1.join() - # p2.join() - - print("done in : ",time.time()-t) - print("Hah... I am done with all my work now!") \ No newline at end of file diff --git a/Multiprocessing/multiprocessing_introduction.py b/Multiprocessing/multiprocessing_introduction.py new file mode 100644 index 00000000..8ce3f692 --- /dev/null +++ b/Multiprocessing/multiprocessing_introduction.py @@ -0,0 +1,23 @@ +import time +import multiprocessing + +def calc_square(numbers): + for n in numbers: + print('square ' + str(n*n)) + +def calc_cube(numbers): + for n in numbers: + print('cube ' + str(n*n*n)) + +if __name__ == "__main__": + arr = [2,3,8] + p1 = multiprocessing.Process(target=calc_square, args=(arr,)) + p2 = multiprocessing.Process(target=calc_cube, args=(arr,)) + + p1.start() + p2.start() + + p1.join() + p2.join() + + print("Done!") \ No newline at end of file diff --git a/Multiprocessing/multiprocessing_lock.py b/Multiprocessing/multiprocessing_lock.py new file mode 100644 index 00000000..4ef0c992 --- /dev/null +++ b/Multiprocessing/multiprocessing_lock.py @@ -0,0 +1,31 @@ +import time +import multiprocessing + +def deposit(balance, lock): + for i in range(100): + time.sleep(0.01) + lock.acquire() + balance.value = balance.value + 1 + lock.release() + +def withdraw(balance, lock): + for i in range(100): + time.sleep(0.01) + lock.acquire() + balance.value = balance.value - 1 + lock.release() + +if __name__ == '__main__': + balance = multiprocessing.Value('i', 200) + lock = multiprocessing.Lock() + + d = multiprocessing.Process(target=deposit, args=(balance,lock)) + w = multiprocessing.Process(target=withdraw, args=(balance,lock)) + + d.start() + w.start() + + d.join() + w.join() + + print(balance.value) diff --git a/Multiprocessing/multiprocessing_value_array.py b/Multiprocessing/multiprocessing_value_array.py new file mode 100644 index 00000000..bd88e46b --- /dev/null +++ b/Multiprocessing/multiprocessing_value_array.py @@ -0,0 +1,17 @@ +import multiprocessing + +def calc_square(numbers, result, v): + v.value = 5.67 + for idx, n in enumerate(numbers): + result[idx] = n*n + +if __name__ == "__main__": + numbers = [2,3,5] + result = multiprocessing.Array('i',3) + v = multiprocessing.Value('d', 0.0) + p = multiprocessing.Process(target=calc_square, args=(numbers, result, v)) + + p.start() + p.join() + + print(v.value) \ No newline at end of file diff --git a/Multiprocessing/multithreading_producer_consumer.py b/Multiprocessing/multithreading_producer_consumer.py new file mode 100644 index 00000000..46018d41 --- /dev/null +++ b/Multiprocessing/multithreading_producer_consumer.py @@ -0,0 +1,20 @@ +import time +from random import randint +import threading + +queue = [] + +def produce(): + for i in range(0,5): + time.sleep(1) + queue.append(randint(0,9)) + +def consume(): + while True: + if len(queue) > 0: + + +if "__name__"=="__main__": + p = threading.Thread(target=produce) + c = threading.Thread(target=consume) + diff --git a/Multiprocessing/mult_threading.py b/Multiprocessing/multthreading_introduction.py similarity index 91% rename from Multiprocessing/mult_threading.py rename to Multiprocessing/multthreading_introduction.py index 85479170..a85ffb36 100644 --- a/Multiprocessing/mult_threading.py +++ b/Multiprocessing/multthreading_introduction.py @@ -4,13 +4,13 @@ def calc_square(numbers): print("calculate square numbers") for n in numbers: - time.sleep(20) + time.sleep(1) print('square:',n*n) def calc_cube(numbers): print("calculate cube of numbers") for n in numbers: - time.sleep(20) + time.sleep(1) print('cube:',n*n*n) arr = [2,3,8,9]