From 7633022799d03f69201af7e789a3d987bb837a75 Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Sat, 26 Jan 2019 16:04:52 +0300 Subject: [PATCH 01/22] homework3 --- hw3_condition.py | 22 ++++++++++++++++++++++ hw3_events.py | 29 +++++++++++++++++++++++++++++ hw3_lock.py | 27 +++++++++++++++++++++++++++ hw3_semaphore.py | 26 ++++++++++++++++++++++++++ hw3_timer.py | 23 +++++++++++++++++++++++ 5 files changed, 127 insertions(+) create mode 100644 hw3_condition.py create mode 100644 hw3_events.py create mode 100644 hw3_lock.py create mode 100644 hw3_semaphore.py create mode 100644 hw3_timer.py diff --git a/hw3_condition.py b/hw3_condition.py new file mode 100644 index 0000000..326c674 --- /dev/null +++ b/hw3_condition.py @@ -0,0 +1,22 @@ +import threading + +i = 0 + + +def consumer(cv, thread_name): + global i + while i < 101: + with cv: + print("{}\t- {}".format(thread_name, i)) + i = i + 1 + cv.notify_all() + cv.wait(1) + + +if __name__ == "__main__": + condition = threading.Condition() + cs1 = threading.Thread(target=consumer, args=(condition, "ThreadEven",)) + cs2 = threading.Thread(target=consumer, args=(condition, "ThreadOdd",)) + + cs1.start() + cs2.start() diff --git a/hw3_events.py b/hw3_events.py new file mode 100644 index 0000000..52b434a --- /dev/null +++ b/hw3_events.py @@ -0,0 +1,29 @@ +import threading + + +even, odd = 0, 1 + + +def counter(thread_name, x, event_for_wait, event_for_set): + for i in range(101): + event_for_wait.wait() + event_for_wait.clear() + if (i % 2) == x: + print("{}\t- {}".format(thread_name, i)) + event_for_set.set() + + +if __name__ == "__main__": + e1 = threading.Event() + e2 = threading.Event() + + t1 = threading.Thread(target=counter, args=("ThreadEven", even, e1, e2)) + t2 = threading.Thread(target=counter, args=("ThreadOdd", odd, e2, e1)) + + t1.start() + t2.start() + + e1.set() + + t1.join() + t2.join() diff --git a/hw3_lock.py b/hw3_lock.py new file mode 100644 index 0000000..572ec08 --- /dev/null +++ b/hw3_lock.py @@ -0,0 +1,27 @@ +import threading +import time + + +thread_lock = threading.Lock() + +i = 0 +even, odd = 0, 1 + + +def counter(thread_name, parity): + global i + while i < 100: + thread_lock.acquire() + if (i % 2) == parity: + print("{} - {}".format(thread_name, i)) + i = i + 1 + time.sleep(0.1) + thread_lock.release() + + +if __name__ == "__main__": + th1 = threading.Thread(target=counter, args=("ThreadEven", even,)) + th2 = threading.Thread(target=counter, args=("ThreadOdd", odd,)) + + th1.start() + th2.start() diff --git a/hw3_semaphore.py b/hw3_semaphore.py new file mode 100644 index 0000000..e14e8d5 --- /dev/null +++ b/hw3_semaphore.py @@ -0,0 +1,26 @@ +import threading +import time + + +sem = threading.Semaphore() + +even, odd = 0, 1 +i = 0 + + +def counter(thread_name, parity): + global i + while i < 100: + sem.acquire() + if (i % 2) == parity: + print("{}\t- {}".format(thread_name, i)) + i = i + 1 + time.sleep(0.1) + sem.release() + + +if __name__ == "__main__": + cs1 = threading.Thread(target=counter, args=("ThreadEven", even,)) + cs1.start() + cs2 = threading.Thread(target=counter, args=("ThreadOdd", odd,)) + cs2.start() diff --git a/hw3_timer.py b/hw3_timer.py new file mode 100644 index 0000000..49fa073 --- /dev/null +++ b/hw3_timer.py @@ -0,0 +1,23 @@ +import threading + + +even, odd = 0, 1 +i = 0 + + +def counter(thread_name, parity): + global i + if i > 100: + return + print("{}\t- {}".format(thread_name, i)) + i = i + 1 + if parity == even: + cs2 = threading.Timer(0.1, counter, args=["ThreadOdd", odd]) + cs2.start() + else: + cs1 = threading.Timer(0.1, counter, args=["ThreadEven", even]) + cs1.start() + + +if __name__ == "__main__": + counter("TreadEven", even) From 399962bca197aeffa81ac7105521cdded702dd24 Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Sat, 26 Jan 2019 23:57:51 +0300 Subject: [PATCH 02/22] homework4 --- hw3_condition.py | 22 ---------------------- hw3_events.py | 29 ----------------------------- hw3_lock.py | 27 --------------------------- hw3_semaphore.py | 26 -------------------------- hw3_timer.py | 23 ----------------------- hw4_process.py | 24 ++++++++++++++++++++++++ 6 files changed, 24 insertions(+), 127 deletions(-) delete mode 100644 hw3_condition.py delete mode 100644 hw3_events.py delete mode 100644 hw3_lock.py delete mode 100644 hw3_semaphore.py delete mode 100644 hw3_timer.py create mode 100644 hw4_process.py diff --git a/hw3_condition.py b/hw3_condition.py deleted file mode 100644 index 326c674..0000000 --- a/hw3_condition.py +++ /dev/null @@ -1,22 +0,0 @@ -import threading - -i = 0 - - -def consumer(cv, thread_name): - global i - while i < 101: - with cv: - print("{}\t- {}".format(thread_name, i)) - i = i + 1 - cv.notify_all() - cv.wait(1) - - -if __name__ == "__main__": - condition = threading.Condition() - cs1 = threading.Thread(target=consumer, args=(condition, "ThreadEven",)) - cs2 = threading.Thread(target=consumer, args=(condition, "ThreadOdd",)) - - cs1.start() - cs2.start() diff --git a/hw3_events.py b/hw3_events.py deleted file mode 100644 index 52b434a..0000000 --- a/hw3_events.py +++ /dev/null @@ -1,29 +0,0 @@ -import threading - - -even, odd = 0, 1 - - -def counter(thread_name, x, event_for_wait, event_for_set): - for i in range(101): - event_for_wait.wait() - event_for_wait.clear() - if (i % 2) == x: - print("{}\t- {}".format(thread_name, i)) - event_for_set.set() - - -if __name__ == "__main__": - e1 = threading.Event() - e2 = threading.Event() - - t1 = threading.Thread(target=counter, args=("ThreadEven", even, e1, e2)) - t2 = threading.Thread(target=counter, args=("ThreadOdd", odd, e2, e1)) - - t1.start() - t2.start() - - e1.set() - - t1.join() - t2.join() diff --git a/hw3_lock.py b/hw3_lock.py deleted file mode 100644 index 572ec08..0000000 --- a/hw3_lock.py +++ /dev/null @@ -1,27 +0,0 @@ -import threading -import time - - -thread_lock = threading.Lock() - -i = 0 -even, odd = 0, 1 - - -def counter(thread_name, parity): - global i - while i < 100: - thread_lock.acquire() - if (i % 2) == parity: - print("{} - {}".format(thread_name, i)) - i = i + 1 - time.sleep(0.1) - thread_lock.release() - - -if __name__ == "__main__": - th1 = threading.Thread(target=counter, args=("ThreadEven", even,)) - th2 = threading.Thread(target=counter, args=("ThreadOdd", odd,)) - - th1.start() - th2.start() diff --git a/hw3_semaphore.py b/hw3_semaphore.py deleted file mode 100644 index e14e8d5..0000000 --- a/hw3_semaphore.py +++ /dev/null @@ -1,26 +0,0 @@ -import threading -import time - - -sem = threading.Semaphore() - -even, odd = 0, 1 -i = 0 - - -def counter(thread_name, parity): - global i - while i < 100: - sem.acquire() - if (i % 2) == parity: - print("{}\t- {}".format(thread_name, i)) - i = i + 1 - time.sleep(0.1) - sem.release() - - -if __name__ == "__main__": - cs1 = threading.Thread(target=counter, args=("ThreadEven", even,)) - cs1.start() - cs2 = threading.Thread(target=counter, args=("ThreadOdd", odd,)) - cs2.start() diff --git a/hw3_timer.py b/hw3_timer.py deleted file mode 100644 index 49fa073..0000000 --- a/hw3_timer.py +++ /dev/null @@ -1,23 +0,0 @@ -import threading - - -even, odd = 0, 1 -i = 0 - - -def counter(thread_name, parity): - global i - if i > 100: - return - print("{}\t- {}".format(thread_name, i)) - i = i + 1 - if parity == even: - cs2 = threading.Timer(0.1, counter, args=["ThreadOdd", odd]) - cs2.start() - else: - cs1 = threading.Timer(0.1, counter, args=["ThreadEven", even]) - cs1.start() - - -if __name__ == "__main__": - counter("TreadEven", even) diff --git a/hw4_process.py b/hw4_process.py new file mode 100644 index 0000000..1dca3bd --- /dev/null +++ b/hw4_process.py @@ -0,0 +1,24 @@ +from multiprocessing import Process, Value +import time + + +def counter(thread_name, parity, q): + while q.value < 101: + if (q.value % 2) == parity: + print("{}\t- {}".format(thread_name, q.value)) + q.value = q.value + 1 + time.sleep(0.1) + + +if __name__ == "__main__": + q = Value('i', 0) + + even, odd = 0, 1 + jobs = [Process(target=counter, args=("ProccesEven", even, q)), + Process(target=counter, args=("ProcessOdd", odd, q))] + + for x in jobs: + x.start() + + for x in jobs: + x.join() From 20b9e4aef47b5981499dca725afa97a50a9b1bfd Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Sun, 27 Jan 2019 00:04:54 +0300 Subject: [PATCH 03/22] homework4 --- hw4_process.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hw4_process.py b/hw4_process.py index 1dca3bd..5d040d3 100644 --- a/hw4_process.py +++ b/hw4_process.py @@ -1,4 +1,5 @@ -from multiprocessing import Process, Value +from multiprocessing import Process +from multiprocessing import Value import time From a3e344834dbbb064ee2e3e6da9287a5c42df453e Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Sun, 27 Jan 2019 18:29:37 +0300 Subject: [PATCH 04/22] homework5 --- .hw5_asyncio.py.swp | Bin 0 -> 12288 bytes hw4_process.py | 25 ------------ hw5_asyncio.py | 91 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 .hw5_asyncio.py.swp delete mode 100644 hw4_process.py create mode 100644 hw5_asyncio.py diff --git a/.hw5_asyncio.py.swp b/.hw5_asyncio.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..cb3c3eaef7befaeb8310569fbbf27836b8bd0f58 GIT binary patch literal 12288 zcmeI2O^6&t6vrzuF>W-wA@QK1P#J~kk=dT?N3t?HUd%zkXkcS5YiN3>Yi8Q*>FRXV z?68IyQ1PHBs0SfN1o7e}f_hL7IRzClm)tz~0a5VaNpE`bU)A07xgRsDpoVJrO>cF* zdiCmG^{P6gmk*zM>^NPp=LxR;gxvSd@u%;Yc$9p4Hz8|otHDBF)ogmDcGFRHQ#0~h zYFIkPtF0yrr6{q;Z8}xniUkuT*IRYND)XFZO*VK~YP63yu2>7JKDVQ_&3&6%qkvIh zD+TtEx!IY+%Iebz&$ORn^C|hU=%P47zK<1MggOMQNSo*6fg=H1&jj!Lj_!qke7B5GI=wC$N&Fl z|Ng%|MaWff8GHyXfY-o_;05qJco_UKNyx__02cUZA0h96Q(y{wbsHgHf-k^F;7#xb zcpaPti(n4y0zco1_TW1B8hipSg15m1a2^~1^WYw^7fgWP_7d_V_yK$mJ_oM?24=zS z;2<~v{+vKe;AOyo2ddx+umJXhDKH6szXi775_k)o12&ijcY!;>uX_l&20jDtf)n6L z@DO+q>;%6++biHAcmRfL zbw#lsN-MXB4?d{J+hM?6kMfOcYV_9yeWS1uV2$aUcps zY6;0BNBT`hr>T%`EOnt!D3DRz57`Yp$VRnc1s@)ivgN{7w^;u)othsokpxbNdX`Fe zjEBrts=#0jQj#d&b7Yztgt6T9p@dZ5n2O6iI_If5-6OGs<;yO7tXDnoUGSlRd9cm2bBCv63_k zIFIaVz(wDKxD`6BQ2GHr6>CNTlgw(meww=7hO|Pn&&BFYI&2l6m^sR0>Rco#4YR>+ zJJX!*I&EJzsO$5Flo549FY{ZbxguquSIOlHN#^~lYRN{-T+hK~srJfrL(l?`H`2t9 z_>YZFefhVYv8e;35%aJmI>$)g$ov|0oX~AD$Dx%9%{z`-OpcQ;<4{%VsS7)9%$j_a z;arF`1^FLRFdip}vbU@mQ^!pQ=t2dY!s$*O%E^vg@vFu-Lj^x&n4Y zcZ(lg#2Q_yF33%y zmiqYL!ZdBDChDY4LksT=h_sTuy0ExN@kCVgqA3>Eoi?R@;;FOJhv%W|QOls^R3pF` z2i9I+$_zWIrE}@I(sd+v^vouTY%rV3NB?vtv)ShX8mMybEUGddBf`L6M#Y&H6VgUe zFBt7rCri)J^uuh*VkTR0l2*2e0$*DBQogV>J#$(&>(9rmky>JTwPlNPsnl+_@zJpQ zj@agLy;PG?DgCT?&OT}{luqcnXDIV z8Pm;hG~04B7|lNs%65DlwIAq;i^4X=U9e}nZcx~!o55((5n(|KwF8W_>IOqeeGuX| zBOXf}IoV(8^(6aS?}z&3iMM3uVe)FyDXP5pc~yGu>qTuujw7l1+>J1-^%q AuK)l5 literal 0 HcmV?d00001 diff --git a/hw4_process.py b/hw4_process.py deleted file mode 100644 index 5d040d3..0000000 --- a/hw4_process.py +++ /dev/null @@ -1,25 +0,0 @@ -from multiprocessing import Process -from multiprocessing import Value -import time - - -def counter(thread_name, parity, q): - while q.value < 101: - if (q.value % 2) == parity: - print("{}\t- {}".format(thread_name, q.value)) - q.value = q.value + 1 - time.sleep(0.1) - - -if __name__ == "__main__": - q = Value('i', 0) - - even, odd = 0, 1 - jobs = [Process(target=counter, args=("ProccesEven", even, q)), - Process(target=counter, args=("ProcessOdd", odd, q))] - - for x in jobs: - x.start() - - for x in jobs: - x.join() diff --git a/hw5_asyncio.py b/hw5_asyncio.py new file mode 100644 index 0000000..44a6f64 --- /dev/null +++ b/hw5_asyncio.py @@ -0,0 +1,91 @@ +from threading import Thread +import urllib.request +import asyncio +import aiohttp +import async_timeout +import time +import os + + +urls = ['https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz', + 'https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz', + 'https://www.python.org/ftp/python/3.7.2/Python-3.7.2rc1.tgz', + 'https://www.python.org/ftp/python/3.6.8/Python-3.6.8rc1.tgz', + 'https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz', + 'https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tgz', + 'https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc2.tgz', + 'https://www.python.org/ftp/python/3.6.7/Python-3.6.7rc2.tgz', + 'https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc1.tgz', + 'https://www.python.org/ftp/python/3.6.7/Python-3.6.7rc1.tgz'] + + +def get_name(link): + return link.split('/')[-1] + + +def downloader(link): + data = urllib.request.urlopen(link) + data_to_write = data.read() + with open(str(get_name(link)), 'wb') as f: + f.write(data_to_write) + + +def sync_dowload(links): + for url in links: + downloader(url) + + +def thread_dowload(links): + threads = [Thread(target=downloader, args=(link,)) for link in links] + + for th in threads: + th.start() + + for th in threads: + th.join() + + +async def fetch_page(session, url): + async with session.get(url) as response: + content = await response.read() + with open(str(get_name(url)), 'wb') as fd: + fd.write(content) + + +async def main(links): + async with aiohttp.ClientSession() as session: + tasks = [fetch_page(session, link) for link in links] + await asyncio.gather(*tasks) + + +def rm_files(links): + for link in links: + os.remove(str(get_name(link))) + + +if __name__ == '__main__': + print("Synchronus download: ", end="") + st_time = time.time() + sync_dowload(urls) + sp_time = time.time() + print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) + + rm_files(urls) + + print("Download with threads: ", end="") + st_time = time.time() + thread_dowload(urls) + sp_time = time.time() + print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) + + rm_files(urls) + + print("Download with aiohttp and asyncio: ", end="") + st_time = time.time() + loop = asyncio.get_event_loop() + loop.run_until_complete(main(urls)) + loop.close() + sp_time = time.time() + print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) + + rm_files(urls) From 03d6438fa0001b75cf2e2c41cfbda2ca0b18b5a8 Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Sun, 27 Jan 2019 18:43:19 +0300 Subject: [PATCH 05/22] homework5 --- hw5_asyncio.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/hw5_asyncio.py b/hw5_asyncio.py index 44a6f64..b027f2d 100644 --- a/hw5_asyncio.py +++ b/hw5_asyncio.py @@ -1,10 +1,9 @@ -from threading import Thread -import urllib.request -import asyncio import aiohttp -import async_timeout -import time +import asyncio import os +from threading import Thread +import time +import urllib.request urls = ['https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz', From b3cd0c2b6c7b91dd0ac03f42f412478db2736990 Mon Sep 17 00:00:00 2001 From: lubuntuVM Date: Mon, 28 Jan 2019 16:19:14 +0300 Subject: [PATCH 06/22] homework5 --- hw5_asyncio.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/hw5_asyncio.py b/hw5_asyncio.py index b027f2d..ef12895 100644 --- a/hw5_asyncio.py +++ b/hw5_asyncio.py @@ -1,6 +1,7 @@ import aiohttp import asyncio import os +import re from threading import Thread import time import urllib.request @@ -18,6 +19,19 @@ 'https://www.python.org/ftp/python/3.6.7/Python-3.6.7rc1.tgz'] +def get_urls(url): + request = urllib.request.Request(url) + website = urllib.request.urlopen(request) + html = str(website.read()) + #links = re.findall('"((http)|(ftp))s?://.*\.tgz$"', html) + links = re.findall('"(https://.*?)"', html) + links = [l for l in links if 'tgz' in l] + return links + + + return html + + def get_name(link): return link.split('/')[-1] @@ -63,13 +77,16 @@ def rm_files(links): if __name__ == '__main__': + urls = get_urls('https://www.python.org/downloads/source/') + print(urls) + print(len(urls)) print("Synchronus download: ", end="") st_time = time.time() - sync_dowload(urls) + #sync_dowload(urls) sp_time = time.time() print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) - rm_files(urls) + #rm_files(urls) print("Download with threads: ", end="") st_time = time.time() From ef6995e3c8a0ada025f0d256f404b62fcc5d8751 Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Tue, 29 Jan 2019 12:41:08 +0300 Subject: [PATCH 07/22] homework5 --- hw5_asyncio.py | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/hw5_asyncio.py b/hw5_asyncio.py index ef12895..62f3cb6 100644 --- a/hw5_asyncio.py +++ b/hw5_asyncio.py @@ -23,9 +23,10 @@ def get_urls(url): request = urllib.request.Request(url) website = urllib.request.urlopen(request) html = str(website.read()) - #links = re.findall('"((http)|(ftp))s?://.*\.tgz$"', html) - links = re.findall('"(https://.*?)"', html) - links = [l for l in links if 'tgz' in l] + links = re.findall('"((ht|f)tps?://\S*\.(tgz|tar\.xz))"', html) + links = [l[0] for l in links] + #links = re.findall('"(https://.*?)"', html) + #links = [l for l in links if 'tgz' in l] return links @@ -53,10 +54,11 @@ def thread_dowload(links): for th in threads: th.start() - - for th in threads: th.join() +# for th in threads: +# th.join() + async def fetch_page(session, url): async with session.get(url) as response: @@ -73,18 +75,19 @@ async def main(links): def rm_files(links): for link in links: - os.remove(str(get_name(link))) + try: + os.remove(str(get_name(link))) + except: + print("No file {}".format(str(get_name(link)))) if __name__ == '__main__': urls = get_urls('https://www.python.org/downloads/source/') - print(urls) - print(len(urls)) - print("Synchronus download: ", end="") + #print("Synchronus download: ", end="") st_time = time.time() #sync_dowload(urls) sp_time = time.time() - print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) + #print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) #rm_files(urls) @@ -96,12 +99,12 @@ def rm_files(links): rm_files(urls) - print("Download with aiohttp and asyncio: ", end="") - st_time = time.time() - loop = asyncio.get_event_loop() - loop.run_until_complete(main(urls)) - loop.close() - sp_time = time.time() - print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) + #print("Download with aiohttp and asyncio: ", end="") + #st_time = time.time() + #loop = asyncio.get_event_loop() + #loop.run_until_complete(main(urls)) + #loop.close() + #sp_time = time.time() + #print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) - rm_files(urls) + #rm_files(urls) From f9a90c1ad78edc216e0f36e1f27a47fdabc6e30c Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Sun, 3 Feb 2019 16:11:50 +0300 Subject: [PATCH 08/22] homework6 --- .hw5_asyncio.py.swp | Bin 12288 -> 0 bytes hw5_asyncio.py | 90 -------------------------------------------- hw6_class.py | 47 +++++++++++++++++++++++ 3 files changed, 47 insertions(+), 90 deletions(-) delete mode 100644 .hw5_asyncio.py.swp delete mode 100644 hw5_asyncio.py create mode 100644 hw6_class.py diff --git a/.hw5_asyncio.py.swp b/.hw5_asyncio.py.swp deleted file mode 100644 index cb3c3eaef7befaeb8310569fbbf27836b8bd0f58..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2O^6&t6vrzuF>W-wA@QK1P#J~kk=dT?N3t?HUd%zkXkcS5YiN3>Yi8Q*>FRXV z?68IyQ1PHBs0SfN1o7e}f_hL7IRzClm)tz~0a5VaNpE`bU)A07xgRsDpoVJrO>cF* zdiCmG^{P6gmk*zM>^NPp=LxR;gxvSd@u%;Yc$9p4Hz8|otHDBF)ogmDcGFRHQ#0~h zYFIkPtF0yrr6{q;Z8}xniUkuT*IRYND)XFZO*VK~YP63yu2>7JKDVQ_&3&6%qkvIh zD+TtEx!IY+%Iebz&$ORn^C|hU=%P47zK<1MggOMQNSo*6fg=H1&jj!Lj_!qke7B5GI=wC$N&Fl z|Ng%|MaWff8GHyXfY-o_;05qJco_UKNyx__02cUZA0h96Q(y{wbsHgHf-k^F;7#xb zcpaPti(n4y0zco1_TW1B8hipSg15m1a2^~1^WYw^7fgWP_7d_V_yK$mJ_oM?24=zS z;2<~v{+vKe;AOyo2ddx+umJXhDKH6szXi775_k)o12&ijcY!;>uX_l&20jDtf)n6L z@DO+q>;%6++biHAcmRfL zbw#lsN-MXB4?d{J+hM?6kMfOcYV_9yeWS1uV2$aUcps zY6;0BNBT`hr>T%`EOnt!D3DRz57`Yp$VRnc1s@)ivgN{7w^;u)othsokpxbNdX`Fe zjEBrts=#0jQj#d&b7Yztgt6T9p@dZ5n2O6iI_If5-6OGs<;yO7tXDnoUGSlRd9cm2bBCv63_k zIFIaVz(wDKxD`6BQ2GHr6>CNTlgw(meww=7hO|Pn&&BFYI&2l6m^sR0>Rco#4YR>+ zJJX!*I&EJzsO$5Flo549FY{ZbxguquSIOlHN#^~lYRN{-T+hK~srJfrL(l?`H`2t9 z_>YZFefhVYv8e;35%aJmI>$)g$ov|0oX~AD$Dx%9%{z`-OpcQ;<4{%VsS7)9%$j_a z;arF`1^FLRFdip}vbU@mQ^!pQ=t2dY!s$*O%E^vg@vFu-Lj^x&n4Y zcZ(lg#2Q_yF33%y zmiqYL!ZdBDChDY4LksT=h_sTuy0ExN@kCVgqA3>Eoi?R@;;FOJhv%W|QOls^R3pF` z2i9I+$_zWIrE}@I(sd+v^vouTY%rV3NB?vtv)ShX8mMybEUGddBf`L6M#Y&H6VgUe zFBt7rCri)J^uuh*VkTR0l2*2e0$*DBQogV>J#$(&>(9rmky>JTwPlNPsnl+_@zJpQ zj@agLy;PG?DgCT?&OT}{luqcnXDIV z8Pm;hG~04B7|lNs%65DlwIAq;i^4X=U9e}nZcx~!o55((5n(|KwF8W_>IOqeeGuX| zBOXf}IoV(8^(6aS?}z&3iMM3uVe)FyDXP5pc~yGu>qTuujw7l1+>J1-^%q AuK)l5 diff --git a/hw5_asyncio.py b/hw5_asyncio.py deleted file mode 100644 index b027f2d..0000000 --- a/hw5_asyncio.py +++ /dev/null @@ -1,90 +0,0 @@ -import aiohttp -import asyncio -import os -from threading import Thread -import time -import urllib.request - - -urls = ['https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz', - 'https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz', - 'https://www.python.org/ftp/python/3.7.2/Python-3.7.2rc1.tgz', - 'https://www.python.org/ftp/python/3.6.8/Python-3.6.8rc1.tgz', - 'https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz', - 'https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tgz', - 'https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc2.tgz', - 'https://www.python.org/ftp/python/3.6.7/Python-3.6.7rc2.tgz', - 'https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc1.tgz', - 'https://www.python.org/ftp/python/3.6.7/Python-3.6.7rc1.tgz'] - - -def get_name(link): - return link.split('/')[-1] - - -def downloader(link): - data = urllib.request.urlopen(link) - data_to_write = data.read() - with open(str(get_name(link)), 'wb') as f: - f.write(data_to_write) - - -def sync_dowload(links): - for url in links: - downloader(url) - - -def thread_dowload(links): - threads = [Thread(target=downloader, args=(link,)) for link in links] - - for th in threads: - th.start() - - for th in threads: - th.join() - - -async def fetch_page(session, url): - async with session.get(url) as response: - content = await response.read() - with open(str(get_name(url)), 'wb') as fd: - fd.write(content) - - -async def main(links): - async with aiohttp.ClientSession() as session: - tasks = [fetch_page(session, link) for link in links] - await asyncio.gather(*tasks) - - -def rm_files(links): - for link in links: - os.remove(str(get_name(link))) - - -if __name__ == '__main__': - print("Synchronus download: ", end="") - st_time = time.time() - sync_dowload(urls) - sp_time = time.time() - print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) - - rm_files(urls) - - print("Download with threads: ", end="") - st_time = time.time() - thread_dowload(urls) - sp_time = time.time() - print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) - - rm_files(urls) - - print("Download with aiohttp and asyncio: ", end="") - st_time = time.time() - loop = asyncio.get_event_loop() - loop.run_until_complete(main(urls)) - loop.close() - sp_time = time.time() - print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) - - rm_files(urls) diff --git a/hw6_class.py b/hw6_class.py new file mode 100644 index 0000000..884bebf --- /dev/null +++ b/hw6_class.py @@ -0,0 +1,47 @@ +class Money: + def __init__(self, value, currency="USD"): + self.int_currency = "USD" + self.rate = {"USD": 1, "BYN": 0.46, "EUR": 1.15, "CYN": 0.15} + self.value = value + self.currency = currency + self.int_value = self.value * self.rate[self.currency] + + def __str__(self): + return ("{} {}".format(self.value, self.currency)) + + def __mul__(self, other): + result_currency = self.currency + result_value = self.value * other + return Money(result_value, result_currency) + + def __rmul__(self, other): + result_currency = self.currency + result_value = self.value * other + return Money(result_value, result_currency) + + def __add__(self, other): + result_currency = self.int_currency + result_value = self.int_value + other.int_value + return Money(result_value, result_currency) + + def __radd__(self, other): + result_currency = self.int_currency + result_value = self.int_value + other + return Money(result_value, result_currency) + + def __iadd__(self, other): + self.int_value += other.int_value + self.value += other.int_value / self.currency + + +if __name__ == "__main__": + a = Money(10, "BYN") + b = Money(100, "CYN") + c = Money(10, "USD") + print(a, b, c) + + print(a + 3.11 * b + c * 0.8) + + z = [Money(10, "BYN"), Money(10, "USD"), Money(10, "CYN")] + r = sum(z) + print(r) From 57a0a4a31d10ebedb97ac15e02fe92520c2b1d27 Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Sun, 3 Feb 2019 21:55:16 +0300 Subject: [PATCH 09/22] homework6 --- hw6_class.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/hw6_class.py b/hw6_class.py index 884bebf..ba122a7 100644 --- a/hw6_class.py +++ b/hw6_class.py @@ -29,19 +29,25 @@ def __radd__(self, other): result_value = self.int_value + other return Money(result_value, result_currency) - def __iadd__(self, other): - self.int_value += other.int_value - self.value += other.int_value / self.currency + def change(self, cur1, cur2): + return self.rate[cur1] / self.rate[cur2] + def changeTo(self, cur): + self.value = self.value * self.change(self.currency, cur) + self.currency = cur -if __name__ == "__main__": - a = Money(10, "BYN") - b = Money(100, "CYN") - c = Money(10, "USD") - print(a, b, c) - - print(a + 3.11 * b + c * 0.8) - z = [Money(10, "BYN"), Money(10, "USD"), Money(10, "CYN")] - r = sum(z) - print(r) +if __name__ == "__main__": + x = Money(10, "BYN") + y = Money(11) + z = Money(12.34, "EUR") + print(x) + print(y) + print(z) + + print(z + 3.11*x + y*0.8) + print(z + 1*x + y*1) + + lst = [Money(10, "BYN"), Money(10), Money(12.01, "CYN")] + s = sum(lst) + print(s) From 10c3665e3981828fc2ad6eda7f05ec6a2ba025bd Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Sun, 3 Feb 2019 21:59:18 +0300 Subject: [PATCH 10/22] homework6 --- hw6_class.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hw6_class.py b/hw6_class.py index ba122a7..fb68fb8 100644 --- a/hw6_class.py +++ b/hw6_class.py @@ -45,8 +45,7 @@ def changeTo(self, cur): print(y) print(z) - print(z + 3.11*x + y*0.8) - print(z + 1*x + y*1) + print(z + 3.11 * x + y * 0.8) lst = [Money(10, "BYN"), Money(10), Money(12.01, "CYN")] s = sum(lst) From 52f25d807040832df95155138a46d24bfbdea863 Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Tue, 5 Feb 2019 22:28:38 +0300 Subject: [PATCH 11/22] homework6 --- hw6_class.py | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/hw6_class.py b/hw6_class.py index fb68fb8..a6a33a9 100644 --- a/hw6_class.py +++ b/hw6_class.py @@ -1,39 +1,35 @@ class Money: def __init__(self, value, currency="USD"): - self.int_currency = "USD" self.rate = {"USD": 1, "BYN": 0.46, "EUR": 1.15, "CYN": 0.15} self.value = value self.currency = currency - self.int_value = self.value * self.rate[self.currency] def __str__(self): - return ("{} {}".format(self.value, self.currency)) + return "{:.2f} {}".format(self.value, self.currency) def __mul__(self, other): - result_currency = self.currency - result_value = self.value * other - return Money(result_value, result_currency) + return Money(self.value * other, self.currency) def __rmul__(self, other): - result_currency = self.currency - result_value = self.value * other - return Money(result_value, result_currency) + return self.__mul__(other) def __add__(self, other): - result_currency = self.int_currency - result_value = self.int_value + other.int_value - return Money(result_value, result_currency) + if type(other) != type(self): + return self + if self.currency != other.currency: + result_value = other.change(self.currency) + else: + result_value = other.value + return Money(result_value + self.value, self.currency) def __radd__(self, other): - result_currency = self.int_currency - result_value = self.int_value + other - return Money(result_value, result_currency) + return self.__add__(other) - def change(self, cur1, cur2): - return self.rate[cur1] / self.rate[cur2] + def change(self, cur): + return self.value * self.rate[self.currency] / self.rate[cur] def changeTo(self, cur): - self.value = self.value * self.change(self.currency, cur) + self.value = self.change(cur) self.currency = cur @@ -41,12 +37,9 @@ def changeTo(self, cur): x = Money(10, "BYN") y = Money(11) z = Money(12.34, "EUR") - print(x) - print(y) - print(z) print(z + 3.11 * x + y * 0.8) - lst = [Money(10, "BYN"), Money(10), Money(12.01, "CYN")] + lst = [Money(10, "BYN"), Money(11), Money(12.01, "CYN")] s = sum(lst) print(s) From fc71711920db2bfa6c4e9d68ed8759a26a0eeeb2 Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Tue, 5 Feb 2019 22:32:48 +0300 Subject: [PATCH 12/22] homework6 --- hw6_class.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw6_class.py b/hw6_class.py index a6a33a9..8401a85 100644 --- a/hw6_class.py +++ b/hw6_class.py @@ -1,4 +1,4 @@ -class Money: +class Money(object): def __init__(self, value, currency="USD"): self.rate = {"USD": 1, "BYN": 0.46, "EUR": 1.15, "CYN": 0.15} self.value = value From 1d4a60bd86ccbff6ac447dcc3d4cd788fc9d60f9 Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Tue, 5 Feb 2019 23:48:51 +0300 Subject: [PATCH 13/22] homework5 --- hw5_asyncio.py | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/hw5_asyncio.py b/hw5_asyncio.py index 62f3cb6..a8992c3 100644 --- a/hw5_asyncio.py +++ b/hw5_asyncio.py @@ -23,16 +23,11 @@ def get_urls(url): request = urllib.request.Request(url) website = urllib.request.urlopen(request) html = str(website.read()) - links = re.findall('"((ht|f)tps?://\S*\.(tgz|tar\.xz))"', html) + links = re.findall(r'"((ht|f)tps?://\S*\.(tgz|tar\.xz))"', html) links = [l[0] for l in links] - #links = re.findall('"(https://.*?)"', html) - #links = [l for l in links if 'tgz' in l] return links - return html - - def get_name(link): return link.split('/')[-1] @@ -56,9 +51,6 @@ def thread_dowload(links): th.start() th.join() -# for th in threads: -# th.join() - async def fetch_page(session, url): async with session.get(url) as response: @@ -77,19 +69,19 @@ def rm_files(links): for link in links: try: os.remove(str(get_name(link))) - except: + except IOError: print("No file {}".format(str(get_name(link)))) if __name__ == '__main__': - urls = get_urls('https://www.python.org/downloads/source/') - #print("Synchronus download: ", end="") + urls = get_urls('https://www.python.org/downloads/source/') + print("Synchronus download: ", end="") st_time = time.time() - #sync_dowload(urls) + sync_dowload(urls) sp_time = time.time() - #print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) + print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) - #rm_files(urls) + rm_files(urls) print("Download with threads: ", end="") st_time = time.time() @@ -99,12 +91,12 @@ def rm_files(links): rm_files(urls) - #print("Download with aiohttp and asyncio: ", end="") - #st_time = time.time() - #loop = asyncio.get_event_loop() - #loop.run_until_complete(main(urls)) - #loop.close() - #sp_time = time.time() - #print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) + print("Download with aiohttp and asyncio: ", end="") + st_time = time.time() + loop = asyncio.get_event_loop() + loop.run_until_complete(main(urls)) + loop.close() + sp_time = time.time() + print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) - #rm_files(urls) + rm_files(urls) From bfe64e539007a74ec90290423c561568e6009835 Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Wed, 13 Feb 2019 22:46:27 +0300 Subject: [PATCH 14/22] homework78 --- hw78_meta.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 hw78_meta.py diff --git a/hw78_meta.py b/hw78_meta.py new file mode 100644 index 0000000..8d8af82 --- /dev/null +++ b/hw78_meta.py @@ -0,0 +1,43 @@ +class YourMetaClass(type): + def __init__(self, name, bases, dct): + new_dct = {} + for name in dct.keys(): + v = getattr(self, name) + if name.startswith('set_'): + t = name.replace('set_', '') + if t not in new_dct.keys(): + new_dct[t] = [None, None, None] + new_dct[t][1] = v + if name.startswith('get_'): + t = name.replace('get_', '') + if t not in new_dct.keys(): + new_dct[t] = [None, None, None] + new_dct[t][0] = v + if name.startswith('del_'): + t = name.replace('del_', '') + if t not in new_dct.keys(): + new_dct[t] = [None, None, None] + new_dct[t][2] = v + for name, (getter, setter, deler) in new_dct.items(): + setattr(self, name, property(getter, setter, deler, "")) + + +class Example(metaclass=YourMetaClass): + def __init__(self): + self._x = None + + def get_x(self): + return self._x + + def set_x(self, value): + self._x = value + + def get_y(self): + return 'y' + + +if __name__ == "__main__": + ex = Example() + ex.x = 255 + print(ex.x) + print(ex.y) From 2db23bcc04296e8fb1e0b83bd065e5820590948b Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Wed, 13 Feb 2019 23:09:32 +0300 Subject: [PATCH 15/22] homework78 --- .hw5_asyncio.py.swp | Bin 12288 -> 0 bytes hw5_asyncio.py | 102 -------------------------------------------- 2 files changed, 102 deletions(-) delete mode 100644 .hw5_asyncio.py.swp delete mode 100644 hw5_asyncio.py diff --git a/.hw5_asyncio.py.swp b/.hw5_asyncio.py.swp deleted file mode 100644 index cb3c3eaef7befaeb8310569fbbf27836b8bd0f58..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2O^6&t6vrzuF>W-wA@QK1P#J~kk=dT?N3t?HUd%zkXkcS5YiN3>Yi8Q*>FRXV z?68IyQ1PHBs0SfN1o7e}f_hL7IRzClm)tz~0a5VaNpE`bU)A07xgRsDpoVJrO>cF* zdiCmG^{P6gmk*zM>^NPp=LxR;gxvSd@u%;Yc$9p4Hz8|otHDBF)ogmDcGFRHQ#0~h zYFIkPtF0yrr6{q;Z8}xniUkuT*IRYND)XFZO*VK~YP63yu2>7JKDVQ_&3&6%qkvIh zD+TtEx!IY+%Iebz&$ORn^C|hU=%P47zK<1MggOMQNSo*6fg=H1&jj!Lj_!qke7B5GI=wC$N&Fl z|Ng%|MaWff8GHyXfY-o_;05qJco_UKNyx__02cUZA0h96Q(y{wbsHgHf-k^F;7#xb zcpaPti(n4y0zco1_TW1B8hipSg15m1a2^~1^WYw^7fgWP_7d_V_yK$mJ_oM?24=zS z;2<~v{+vKe;AOyo2ddx+umJXhDKH6szXi775_k)o12&ijcY!;>uX_l&20jDtf)n6L z@DO+q>;%6++biHAcmRfL zbw#lsN-MXB4?d{J+hM?6kMfOcYV_9yeWS1uV2$aUcps zY6;0BNBT`hr>T%`EOnt!D3DRz57`Yp$VRnc1s@)ivgN{7w^;u)othsokpxbNdX`Fe zjEBrts=#0jQj#d&b7Yztgt6T9p@dZ5n2O6iI_If5-6OGs<;yO7tXDnoUGSlRd9cm2bBCv63_k zIFIaVz(wDKxD`6BQ2GHr6>CNTlgw(meww=7hO|Pn&&BFYI&2l6m^sR0>Rco#4YR>+ zJJX!*I&EJzsO$5Flo549FY{ZbxguquSIOlHN#^~lYRN{-T+hK~srJfrL(l?`H`2t9 z_>YZFefhVYv8e;35%aJmI>$)g$ov|0oX~AD$Dx%9%{z`-OpcQ;<4{%VsS7)9%$j_a z;arF`1^FLRFdip}vbU@mQ^!pQ=t2dY!s$*O%E^vg@vFu-Lj^x&n4Y zcZ(lg#2Q_yF33%y zmiqYL!ZdBDChDY4LksT=h_sTuy0ExN@kCVgqA3>Eoi?R@;;FOJhv%W|QOls^R3pF` z2i9I+$_zWIrE}@I(sd+v^vouTY%rV3NB?vtv)ShX8mMybEUGddBf`L6M#Y&H6VgUe zFBt7rCri)J^uuh*VkTR0l2*2e0$*DBQogV>J#$(&>(9rmky>JTwPlNPsnl+_@zJpQ zj@agLy;PG?DgCT?&OT}{luqcnXDIV z8Pm;hG~04B7|lNs%65DlwIAq;i^4X=U9e}nZcx~!o55((5n(|KwF8W_>IOqeeGuX| zBOXf}IoV(8^(6aS?}z&3iMM3uVe)FyDXP5pc~yGu>qTuujw7l1+>J1-^%q AuK)l5 diff --git a/hw5_asyncio.py b/hw5_asyncio.py deleted file mode 100644 index a8992c3..0000000 --- a/hw5_asyncio.py +++ /dev/null @@ -1,102 +0,0 @@ -import aiohttp -import asyncio -import os -import re -from threading import Thread -import time -import urllib.request - - -urls = ['https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz', - 'https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz', - 'https://www.python.org/ftp/python/3.7.2/Python-3.7.2rc1.tgz', - 'https://www.python.org/ftp/python/3.6.8/Python-3.6.8rc1.tgz', - 'https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz', - 'https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tgz', - 'https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc2.tgz', - 'https://www.python.org/ftp/python/3.6.7/Python-3.6.7rc2.tgz', - 'https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc1.tgz', - 'https://www.python.org/ftp/python/3.6.7/Python-3.6.7rc1.tgz'] - - -def get_urls(url): - request = urllib.request.Request(url) - website = urllib.request.urlopen(request) - html = str(website.read()) - links = re.findall(r'"((ht|f)tps?://\S*\.(tgz|tar\.xz))"', html) - links = [l[0] for l in links] - return links - - -def get_name(link): - return link.split('/')[-1] - - -def downloader(link): - data = urllib.request.urlopen(link) - data_to_write = data.read() - with open(str(get_name(link)), 'wb') as f: - f.write(data_to_write) - - -def sync_dowload(links): - for url in links: - downloader(url) - - -def thread_dowload(links): - threads = [Thread(target=downloader, args=(link,)) for link in links] - - for th in threads: - th.start() - th.join() - - -async def fetch_page(session, url): - async with session.get(url) as response: - content = await response.read() - with open(str(get_name(url)), 'wb') as fd: - fd.write(content) - - -async def main(links): - async with aiohttp.ClientSession() as session: - tasks = [fetch_page(session, link) for link in links] - await asyncio.gather(*tasks) - - -def rm_files(links): - for link in links: - try: - os.remove(str(get_name(link))) - except IOError: - print("No file {}".format(str(get_name(link)))) - - -if __name__ == '__main__': - urls = get_urls('https://www.python.org/downloads/source/') - print("Synchronus download: ", end="") - st_time = time.time() - sync_dowload(urls) - sp_time = time.time() - print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) - - rm_files(urls) - - print("Download with threads: ", end="") - st_time = time.time() - thread_dowload(urls) - sp_time = time.time() - print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) - - rm_files(urls) - - print("Download with aiohttp and asyncio: ", end="") - st_time = time.time() - loop = asyncio.get_event_loop() - loop.run_until_complete(main(urls)) - loop.close() - sp_time = time.time() - print("Done. {} files. Took {} sec.".format(len(urls), sp_time - st_time)) - - rm_files(urls) From 346db7b1cd322e86906d59ca5de3ad710d884f98 Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Tue, 19 Feb 2019 23:49:39 +0300 Subject: [PATCH 16/22] homework9 --- hw78_meta.py | 43 ---------------------- hw9_knapsack.py | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 43 deletions(-) delete mode 100644 hw78_meta.py create mode 100644 hw9_knapsack.py diff --git a/hw78_meta.py b/hw78_meta.py deleted file mode 100644 index 8d8af82..0000000 --- a/hw78_meta.py +++ /dev/null @@ -1,43 +0,0 @@ -class YourMetaClass(type): - def __init__(self, name, bases, dct): - new_dct = {} - for name in dct.keys(): - v = getattr(self, name) - if name.startswith('set_'): - t = name.replace('set_', '') - if t not in new_dct.keys(): - new_dct[t] = [None, None, None] - new_dct[t][1] = v - if name.startswith('get_'): - t = name.replace('get_', '') - if t not in new_dct.keys(): - new_dct[t] = [None, None, None] - new_dct[t][0] = v - if name.startswith('del_'): - t = name.replace('del_', '') - if t not in new_dct.keys(): - new_dct[t] = [None, None, None] - new_dct[t][2] = v - for name, (getter, setter, deler) in new_dct.items(): - setattr(self, name, property(getter, setter, deler, "")) - - -class Example(metaclass=YourMetaClass): - def __init__(self): - self._x = None - - def get_x(self): - return self._x - - def set_x(self, value): - self._x = value - - def get_y(self): - return 'y' - - -if __name__ == "__main__": - ex = Example() - ex.x = 255 - print(ex.x) - print(ex.y) diff --git a/hw9_knapsack.py b/hw9_knapsack.py new file mode 100644 index 0000000..18458e0 --- /dev/null +++ b/hw9_knapsack.py @@ -0,0 +1,95 @@ +from itertools import accumulate + +items_tuples = [ + ("map", 9,150), + ("compass", 13,35), + ("water", 153,200), + ("sandwich", 50,160), + ("glucose", 15,60), + ("tin", 68,45), + ("banana", 27,60), + ("apple", 39,40), + ("cheese", 23,30), + ("beer", 52,10), + ("suntan cream", 11,70), + ("camera", 32,30), + ("T-shirt", 24,15), + ("trousers", 48,10), + ("umbrella", 73,40), + ("waterproof trousers", 42,70), + ("waterproof overclothes", 43,75), + ("note-case", 22,80), + ("sunglasses", 7,20), + ("towel", 18,12), + ("socks", 4,50), + ("book", 30,10) + ] + +volume = 400 + +def foo(items, volume): + sorted_items = sorted(items, key=lambda x: x[1]/x[2]) + + knapsnack = [] + for item in sorted_items: + if sum([x[1] for x in knapsnack]) + item[1] <= volume: + knapsnack.append(item) + print(knapsnack, sum([x[1] for x in knapsnack]), sum([x[2] for x in knapsnack])) + +def get_items(items, num): + list_items = [] + i = num + while i != 0: + x = items[i % len(items)] + if x not in list_items: + list_items.append(x) + i = i // len(items) + return list_items + +def get_volume(l): + return sum([x[1] for x in l]) + +def get_value(l): + return sum([x[2] for x in l]) + +def get_keys(items): + return [x[0] for x in items] + +def run_brootforce(items, volume): + + num = old_max_value = 0 + while True: + l = get_items(items, num) + if get_volume(l) <= volume: + if get_value(l) > old_max_value: + old_max_value = get_value(l) + print(get_keys(l), get_volume(l), get_value(l), num) + num = num + 1 + if (num % 1000000) == 0: + print(num) + return 0 + + +if __name__ == "__main__": + #foo(items_tuples, volume) + #for i in range(1000): + #print(get_items(items_tuples, i)) + run_brootforce(items_tuples, volume) + #['note-case', 'waterproof overclothes', 'waterproof trousers', 'suntan cream', 'sandwich', 'map', 'water'] 330 805 227576685 + + + + + + + + + + + + + + + + + From 76a7bb3530fff4714943c2c1984d914a76ed397b Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Fri, 22 Feb 2019 00:02:15 +0300 Subject: [PATCH 17/22] homework9 --- hw9_knapsack.py | 122 ++++++++++++------------------------------------ 1 file changed, 30 insertions(+), 92 deletions(-) diff --git a/hw9_knapsack.py b/hw9_knapsack.py index 18458e0..cbadb39 100644 --- a/hw9_knapsack.py +++ b/hw9_knapsack.py @@ -1,95 +1,33 @@ -from itertools import accumulate - -items_tuples = [ - ("map", 9,150), - ("compass", 13,35), - ("water", 153,200), - ("sandwich", 50,160), - ("glucose", 15,60), - ("tin", 68,45), - ("banana", 27,60), - ("apple", 39,40), - ("cheese", 23,30), - ("beer", 52,10), - ("suntan cream", 11,70), - ("camera", 32,30), - ("T-shirt", 24,15), - ("trousers", 48,10), - ("umbrella", 73,40), - ("waterproof trousers", 42,70), - ("waterproof overclothes", 43,75), - ("note-case", 22,80), - ("sunglasses", 7,20), - ("towel", 18,12), - ("socks", 4,50), - ("book", 30,10) - ] - -volume = 400 - -def foo(items, volume): - sorted_items = sorted(items, key=lambda x: x[1]/x[2]) - - knapsnack = [] - for item in sorted_items: - if sum([x[1] for x in knapsnack]) + item[1] <= volume: - knapsnack.append(item) - print(knapsnack, sum([x[1] for x in knapsnack]), sum([x[2] for x in knapsnack])) - -def get_items(items, num): - list_items = [] - i = num - while i != 0: - x = items[i % len(items)] - if x not in list_items: - list_items.append(x) - i = i // len(items) - return list_items - -def get_volume(l): - return sum([x[1] for x in l]) - -def get_value(l): - return sum([x[2] for x in l]) - -def get_keys(items): - return [x[0] for x in items] - -def run_brootforce(items, volume): - - num = old_max_value = 0 - while True: - l = get_items(items, num) - if get_volume(l) <= volume: - if get_value(l) > old_max_value: - old_max_value = get_value(l) - print(get_keys(l), get_volume(l), get_value(l), num) - num = num + 1 - if (num % 1000000) == 0: - print(num) - return 0 +from itertools import accumulate as ac + + +v = 400 + +i = [ + ("map", 9, 150), + ("compass", 13, 35), + ("water", 153, 200), + ("sandwich", 50, 160), + ("glucose", 15, 60), + ("tin", 68, 45), + ("banana", 27, 60), + ("apple", 39, 40), + ("cheese", 23, 30), + ("beer", 52, 10), + ("suntan cream", 11, 70), + ("camera", 32, 30), + ("T-shirt", 24, 15), + ("trousers", 48, 10), + ("umbrella", 73, 40), + ("waterproof trousers", 42, 70), + ("waterproof overclothes", 43, 75), + ("note-case", 22, 80), + ("sunglasses", 7, 20), + ("towel", 18, 12), + ("socks", 4, 50), + ("book", 30, 10)] if __name__ == "__main__": - #foo(items_tuples, volume) - #for i in range(1000): - #print(get_items(items_tuples, i)) - run_brootforce(items_tuples, volume) - #['note-case', 'waterproof overclothes', 'waterproof trousers', 'suntan cream', 'sandwich', 'map', 'water'] 330 805 227576685 - - - - - - - - - - - - - - - - - + print(sorted(i, key=lambda x: x[1] / x[2])[:len([x for x in ac([ + x[1] for x in sorted(i, key=lambda x: x[1] / x[2])]) if x <= v])]) From 2498b3ae0d9099b85bbd89173ae06a17dda3aff1 Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Mon, 25 Feb 2019 21:39:10 +0300 Subject: [PATCH 18/22] homework10 --- class.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 class.py diff --git a/class.py b/class.py new file mode 100644 index 0000000..8401a85 --- /dev/null +++ b/class.py @@ -0,0 +1,45 @@ +class Money(object): + def __init__(self, value, currency="USD"): + self.rate = {"USD": 1, "BYN": 0.46, "EUR": 1.15, "CYN": 0.15} + self.value = value + self.currency = currency + + def __str__(self): + return "{:.2f} {}".format(self.value, self.currency) + + def __mul__(self, other): + return Money(self.value * other, self.currency) + + def __rmul__(self, other): + return self.__mul__(other) + + def __add__(self, other): + if type(other) != type(self): + return self + if self.currency != other.currency: + result_value = other.change(self.currency) + else: + result_value = other.value + return Money(result_value + self.value, self.currency) + + def __radd__(self, other): + return self.__add__(other) + + def change(self, cur): + return self.value * self.rate[self.currency] / self.rate[cur] + + def changeTo(self, cur): + self.value = self.change(cur) + self.currency = cur + + +if __name__ == "__main__": + x = Money(10, "BYN") + y = Money(11) + z = Money(12.34, "EUR") + + print(z + 3.11 * x + y * 0.8) + + lst = [Money(10, "BYN"), Money(11), Money(12.01, "CYN")] + s = sum(lst) + print(s) From 445808b1421492eb606de6095e998792e2cbf3c0 Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Mon, 25 Feb 2019 21:41:04 +0300 Subject: [PATCH 19/22] homework10 --- hw6_class.py | 45 --------------------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 hw6_class.py diff --git a/hw6_class.py b/hw6_class.py deleted file mode 100644 index 8401a85..0000000 --- a/hw6_class.py +++ /dev/null @@ -1,45 +0,0 @@ -class Money(object): - def __init__(self, value, currency="USD"): - self.rate = {"USD": 1, "BYN": 0.46, "EUR": 1.15, "CYN": 0.15} - self.value = value - self.currency = currency - - def __str__(self): - return "{:.2f} {}".format(self.value, self.currency) - - def __mul__(self, other): - return Money(self.value * other, self.currency) - - def __rmul__(self, other): - return self.__mul__(other) - - def __add__(self, other): - if type(other) != type(self): - return self - if self.currency != other.currency: - result_value = other.change(self.currency) - else: - result_value = other.value - return Money(result_value + self.value, self.currency) - - def __radd__(self, other): - return self.__add__(other) - - def change(self, cur): - return self.value * self.rate[self.currency] / self.rate[cur] - - def changeTo(self, cur): - self.value = self.change(cur) - self.currency = cur - - -if __name__ == "__main__": - x = Money(10, "BYN") - y = Money(11) - z = Money(12.34, "EUR") - - print(z + 3.11 * x + y * 0.8) - - lst = [Money(10, "BYN"), Money(11), Money(12.01, "CYN")] - s = sum(lst) - print(s) From 66c30b805720828bc5a7af94b1bd2c262c270084 Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Mon, 25 Feb 2019 23:10:14 +0300 Subject: [PATCH 20/22] homework10 --- money/README.txt | 1 + money/setup.py | 10 ++++++++++ money/src/money.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 money/README.txt create mode 100644 money/setup.py create mode 100644 money/src/money.py diff --git a/money/README.txt b/money/README.txt new file mode 100644 index 0000000..d9379e3 --- /dev/null +++ b/money/README.txt @@ -0,0 +1 @@ +Simple package for currencies converting. diff --git a/money/setup.py b/money/setup.py new file mode 100644 index 0000000..cb47e19 --- /dev/null +++ b/money/setup.py @@ -0,0 +1,10 @@ +from distutils.core import setup + +setup(name='Money', + version='1.0', + package_dir={'': 'src'}, + packages=[''], + author = 'Yauheni Karatsevich', + author_email = 'yauheni_karatsevich@epam.com', + url = 'https://epam.com' + ) diff --git a/money/src/money.py b/money/src/money.py new file mode 100644 index 0000000..8401a85 --- /dev/null +++ b/money/src/money.py @@ -0,0 +1,45 @@ +class Money(object): + def __init__(self, value, currency="USD"): + self.rate = {"USD": 1, "BYN": 0.46, "EUR": 1.15, "CYN": 0.15} + self.value = value + self.currency = currency + + def __str__(self): + return "{:.2f} {}".format(self.value, self.currency) + + def __mul__(self, other): + return Money(self.value * other, self.currency) + + def __rmul__(self, other): + return self.__mul__(other) + + def __add__(self, other): + if type(other) != type(self): + return self + if self.currency != other.currency: + result_value = other.change(self.currency) + else: + result_value = other.value + return Money(result_value + self.value, self.currency) + + def __radd__(self, other): + return self.__add__(other) + + def change(self, cur): + return self.value * self.rate[self.currency] / self.rate[cur] + + def changeTo(self, cur): + self.value = self.change(cur) + self.currency = cur + + +if __name__ == "__main__": + x = Money(10, "BYN") + y = Money(11) + z = Money(12.34, "EUR") + + print(z + 3.11 * x + y * 0.8) + + lst = [Money(10, "BYN"), Money(11), Money(12.01, "CYN")] + s = sum(lst) + print(s) From 6988775f1ed70d2afde50898f8b8249b55eba66d Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Mon, 25 Feb 2019 23:24:07 +0300 Subject: [PATCH 21/22] homework10 --- class.py | 45 --------------------------------------------- hw9_knapsack.py | 33 --------------------------------- 2 files changed, 78 deletions(-) delete mode 100644 class.py delete mode 100644 hw9_knapsack.py diff --git a/class.py b/class.py deleted file mode 100644 index 8401a85..0000000 --- a/class.py +++ /dev/null @@ -1,45 +0,0 @@ -class Money(object): - def __init__(self, value, currency="USD"): - self.rate = {"USD": 1, "BYN": 0.46, "EUR": 1.15, "CYN": 0.15} - self.value = value - self.currency = currency - - def __str__(self): - return "{:.2f} {}".format(self.value, self.currency) - - def __mul__(self, other): - return Money(self.value * other, self.currency) - - def __rmul__(self, other): - return self.__mul__(other) - - def __add__(self, other): - if type(other) != type(self): - return self - if self.currency != other.currency: - result_value = other.change(self.currency) - else: - result_value = other.value - return Money(result_value + self.value, self.currency) - - def __radd__(self, other): - return self.__add__(other) - - def change(self, cur): - return self.value * self.rate[self.currency] / self.rate[cur] - - def changeTo(self, cur): - self.value = self.change(cur) - self.currency = cur - - -if __name__ == "__main__": - x = Money(10, "BYN") - y = Money(11) - z = Money(12.34, "EUR") - - print(z + 3.11 * x + y * 0.8) - - lst = [Money(10, "BYN"), Money(11), Money(12.01, "CYN")] - s = sum(lst) - print(s) diff --git a/hw9_knapsack.py b/hw9_knapsack.py deleted file mode 100644 index cbadb39..0000000 --- a/hw9_knapsack.py +++ /dev/null @@ -1,33 +0,0 @@ -from itertools import accumulate as ac - - -v = 400 - -i = [ - ("map", 9, 150), - ("compass", 13, 35), - ("water", 153, 200), - ("sandwich", 50, 160), - ("glucose", 15, 60), - ("tin", 68, 45), - ("banana", 27, 60), - ("apple", 39, 40), - ("cheese", 23, 30), - ("beer", 52, 10), - ("suntan cream", 11, 70), - ("camera", 32, 30), - ("T-shirt", 24, 15), - ("trousers", 48, 10), - ("umbrella", 73, 40), - ("waterproof trousers", 42, 70), - ("waterproof overclothes", 43, 75), - ("note-case", 22, 80), - ("sunglasses", 7, 20), - ("towel", 18, 12), - ("socks", 4, 50), - ("book", 30, 10)] - - -if __name__ == "__main__": - print(sorted(i, key=lambda x: x[1] / x[2])[:len([x for x in ac([ - x[1] for x in sorted(i, key=lambda x: x[1] / x[2])]) if x <= v])]) From cbf9261ccacfc515f105125ea587ef688586d53c Mon Sep 17 00:00:00 2001 From: Yauheni Karatsevich Date: Mon, 25 Feb 2019 23:36:42 +0300 Subject: [PATCH 22/22] homework10 --- money/setup.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/money/setup.py b/money/setup.py index cb47e19..b857bd5 100644 --- a/money/setup.py +++ b/money/setup.py @@ -1,10 +1,11 @@ from distutils.core import setup -setup(name='Money', - version='1.0', - package_dir={'': 'src'}, - packages=[''], - author = 'Yauheni Karatsevich', - author_email = 'yauheni_karatsevich@epam.com', - url = 'https://epam.com' - ) +setup( + name='Money', + version='1.0', + package_dir={'': 'src'}, + packages=[''], + author='Yauheni Karatsevich', + author_email='yauheni_karatsevich@epam.com', + url='https://epam.com' +)