Skip to content

Commit

Permalink
lab with decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
“Ilia committed Apr 12, 2024
1 parent d43d4e1 commit cb8b99d
Show file tree
Hide file tree
Showing 12 changed files with 662 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ООП и работа с API
Netology PD-97
38 changes: 38 additions & 0 deletions decorators/log_task_02_1.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"timestamp": "2024-04-12 16:47:00.372140",
"name": "hello_world",
"args": [],
"kwargs": {},
"result": "Hello World"
},
{
"timestamp": "2024-04-12 16:47:00.372719",
"name": "summator",
"args": [
2,
2
],
"kwargs": {},
"result": 4
},
{
"timestamp": "2024-04-12 16:47:00.373231",
"name": "div",
"args": [
6,
2
],
"kwargs": {},
"result": 3.0
},
{
"timestamp": "2024-04-12 16:47:00.373578",
"name": "summator",
"args": [
4.3
],
"kwargs": {
"b": 2.2
},
"result": 6.5
},
38 changes: 38 additions & 0 deletions decorators/log_task_02_2.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"timestamp": "2024-04-12 16:47:00.374138",
"name": "hello_world",
"args": [],
"kwargs": {},
"result": "Hello World"
},
{
"timestamp": "2024-04-12 16:47:00.375032",
"name": "summator",
"args": [
2,
2
],
"kwargs": {},
"result": 4
},
{
"timestamp": "2024-04-12 16:47:00.375575",
"name": "div",
"args": [
6,
2
],
"kwargs": {},
"result": 3.0
},
{
"timestamp": "2024-04-12 16:47:00.375831",
"name": "summator",
"args": [
4.3
],
"kwargs": {
"b": 2.2
},
"result": 6.5
},
38 changes: 38 additions & 0 deletions decorators/log_task_02_3.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"timestamp": "2024-04-12 16:47:00.376093",
"name": "hello_world",
"args": [],
"kwargs": {},
"result": "Hello World"
},
{
"timestamp": "2024-04-12 16:47:00.376384",
"name": "summator",
"args": [
2,
2
],
"kwargs": {},
"result": 4
},
{
"timestamp": "2024-04-12 16:47:00.376543",
"name": "div",
"args": [
6,
2
],
"kwargs": {},
"result": 3.0
},
{
"timestamp": "2024-04-12 16:47:00.376698",
"name": "summator",
"args": [
4.3
],
"kwargs": {
"b": 2.2
},
"result": 6.5
},
9 changes: 9 additions & 0 deletions decorators/log_task_03_1.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "new_function",
"timestamp": "2024-04-12 16:46:58.412284",
"args": [
"10006"
],
"kwargs": {},
"result": "\u0410\u0440\u0438\u0441\u0442\u0430\u0440\u0445 \u041f\u0430\u0432\u043b\u043e\u0432"
},
48 changes: 48 additions & 0 deletions decorators/main.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "hello_world",
"timestamp": "2024-04-12 16:40:02.552160",
"args": [],
"kwargs": {},
"result": "Hello World"
},
{
"name": "summator",
"timestamp": "2024-04-12 16:40:02.552166",
"args": [
2,
2
],
"kwargs": {},
"result": 4
},
{
"name": "div",
"timestamp": "2024-04-12 16:40:02.552167",
"args": [
6,
2
],
"kwargs": {},
"result": 3.0
},
{
"name": "summator",
"timestamp": "2024-04-12 16:40:02.552166",
"args": [
4.3
],
"kwargs": {
"b": 2.2
},
"result": 6.5
},
{
"name": "summator",
"timestamp": "2024-04-12 16:40:02.552166",
"args": [],
"kwargs": {
"a": 0,
"b": 0
},
"result": 0
},
Empty file added decorators/modules/__init__.py
Empty file.
76 changes: 76 additions & 0 deletions decorators/modules/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
documents = [
{"type": "passport", "number": "2207 876234", "name": "Василий Гупкин"},
{"type": "invoice", "number": "11-2", "name": "Геннадий Покемонов"},
{"type": "insurance", "number": "10006", "name": "Аристарх Павлов"},
{"type": "driver license", "number": "5455 028765", "name": "Василий Иванов"},
]

directories = {
'1': ['2207 876234', '11-2', '5455 028765'],
'2': ['10006'],
'3': []
}

### My
def get_name(doc_number):
result = "Документ не найден"

for document in documents:
if doc_number in document["number"]:
result = document["name"]

return result


def get_directory(doc_number):
result = "Полки с таким документом не найдено"

for number in directories:
if doc_number in directories[number]:
result = number

return result


def add(document_type, number, name, shelf_number):
new_document = {"type": document_type, "number": number, "name": name}
documents.append(new_document)

if shelf_number in directories:
if number not in directories[str(shelf_number)]:
directories[str(shelf_number)].append(str(number))
else:
directories[str(shelf_number)] = [str(number)]

### Netology
def get_name(doc_number):
for document in documents:
if document['number'] == doc_number:
return document['name']
return "Документ не найден"


def get_directory(doc_number):
for number, docs in directories.items():
if doc_number in docs:
return number
return "Полки с таким документом не найдено"


def add(document_type, number, name, shelf_number):
new_doc = {"type": document_type, "number": number, "name": name}
documents.append(new_doc)
if shelf_number in directories:
directories[shelf_number].append(number)
else:
directories[shelf_number] = [number]


if __name__ == '__main__':
print(get_name("10006"))
print(get_directory("11-2"))
print(get_name("101"))
add('international passport', '311 020203', 'Александр Пушкин', 3)
print(get_directory("311 020203"))
print(get_name("311 020203"))
print(get_directory("311 020204"))
77 changes: 77 additions & 0 deletions decorators/task_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import os
import json
from datetime import datetime


def logger(old_function):
log_info = {}

log_info.update({
"name": old_function.__name__,
"timestamp": datetime.now()
})

def new_function(*args, **kwargs):
result = old_function(*args, **kwargs)

log_info.update({
"args": args,
"kwargs": kwargs,
"result": result
})

json_object = json.dumps(log_info, indent=4, default=str)

with open('main.log', "a+") as outfile:
outfile.writelines(json_object + ",\n")

return result

return new_function


def test_1():
path = 'main.log'
if os.path.exists(path):
os.remove(path)

@logger
def hello_world():
return 'Hello World'

@logger
def summator(a, b=0):
return a + b

@logger
def div(a, b):
return a / b

# hello_world
assert 'Hello World' == hello_world(), "Функция возвращает 'Hello World'"

# summator
result = summator(2, 2)
assert isinstance(result, int), 'Должно вернуться целое число'
assert result == 4, '2 + 2 = 4'

# div
result = div(6, 2)
assert result == 3, '6 / 2 = 3'

# main.log
assert os.path.exists(path), 'файл main.log должен существовать'

summator(4.3, b=2.2)
summator(a=0, b=0)

with open(path) as log_file:
log_file_content = str(log_file.read())

assert 'summator' in log_file_content, 'должно записаться имя функции'
for item in (4.3, 2.2, 6.5):
assert str(item) in log_file_content, f'{item} должен быть записан в файл'


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

0 comments on commit cb8b99d

Please sign in to comment.