Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This is a basic workflow to help you get started with Actions

name: CI

on: push

jobs:
code_quality:
runs-on: ubuntu-latest
name: Checks with black, isort and possibly run tests
container: python:3.9

steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Run script
run: |
pip install flake8 isort pytest
ls -la
flake8 .
isort --check .
py.test tests
132 changes: 0 additions & 132 deletions homework1/.gitignore

This file was deleted.

2 changes: 0 additions & 2 deletions homework1/requirements.txt

This file was deleted.

2 changes: 0 additions & 2 deletions homework1/sample_project/calculator/calc.py

This file was deleted.

2 changes: 0 additions & 2 deletions homework1/sample_project/requirements.txt

This file was deleted.

Empty file.
15 changes: 0 additions & 15 deletions homework1/sample_project/tests/test_calculator.py

This file was deleted.

Empty file removed homework1/tasks/__init__.py
Empty file.
11 changes: 0 additions & 11 deletions homework1/tasks/task02.py

This file was deleted.

17 changes: 0 additions & 17 deletions homework1/tasks/task03.py

This file was deleted.

10 changes: 0 additions & 10 deletions homework1/tasks/task04.py

This file was deleted.

11 changes: 0 additions & 11 deletions homework1/tasks/task05.py

This file was deleted.

Empty file removed homework1/tests/__init__.py
Empty file.
10 changes: 0 additions & 10 deletions homework1/tests/some_file.txt

This file was deleted.

49 changes: 0 additions & 49 deletions homework1/tests/tests.py

This file was deleted.

File renamed without changes.
49 changes: 49 additions & 0 deletions homework3/task01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
In previous homework task 4, you wrote a cache function that remembers other
function output value. Modify it to be a parametrized decorator,
so that the following code:

@cache(times=3)
def some_function():
pass
Would give out cached value up to times number only. Example:

@cache(times=2)
def f():
return input('? ') # careful with input() in python2,
use raw_input() instead
f()
? 1
'1'
f() # will remember previous value
'1'
f() # but use it up to two times only
'1'
f()
? 2
'2'
"""

import functools


def cache(times):
def wrapped_cache(func):
cache_dict = {}
counter = times

@functools.wraps(func)
def wrapped_func(*args, **kwargs):
nonlocal counter
if counter > 0 and args in cache_dict:
counter -= 1
return cache_dict[args]
else:
values_to_cache = func(*args, **kwargs)
cache_dict[args] = values_to_cache
counter = times - 1
return values_to_cache

return wrapped_func

return wrapped_cache
49 changes: 49 additions & 0 deletions homework3/task02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Here's a not very efficient calculation function that calculates
something important:

import time
import struct
import random
import hashlib

def slow_calculate(value):
""""""Some weird voodoo magic calculations""""""
time.sleep(random.randint(1,3))
data = hashlib.md5(str(value).encode()).digest()
return sum(struct.unpack('<' + 'B' * len(data), data))

Calculate total sum of slow_calculate() of all numbers
starting from 0 to 500.
Calculation time should not take more than a minute.
Use functional capabilities of multiprocessing module.
You are not allowed to modify slow_calculate function.
"""

import hashlib
import random
import struct
import time
from multiprocessing import Pool, freeze_support


def slow_calculate(value):
"""Some weird voodoo magic calculations"""
time.sleep(random.randint(1, 3))
data = hashlib.md5(str(value).encode()).digest()
return sum(struct.unpack('<' + 'B' * len(data), data))


def timing():
start_time = time.time()
return lambda x: print("[{:.2f}s] {}".format(time.time() - start_time, x))


def pool_calculation(last_value, processors):
loop_range = range(0, last_value)
with Pool(processors) as pool:
return sum(pool.map(slow_calculate, loop_range))


if __name__ == '__main__':
freeze_support()
Loading