You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.4 KiB
Plaintext

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "markdown",
"id": "b656cccd",
"metadata": {},
"source": [
"# Задание. Декоратор to_json #"
]
},
{
"cell_type": "markdown",
"id": "4f319165",
"metadata": {},
"source": [
"Чтобы передавать данные между функциями, модулями или разными системами используются форматы данных. Одним из самых популярных форматов является `JSON`. Напишите декоратор `to_json`, который можно применить к различным функциям, чтобы преобразовывать их возвращаемое значение в `JSON`-формат. Не забудьте про сохранение корректного имени декорируемой функции.\n",
"\n",
"```python\n",
"@to_json\n",
"def get_data():\n",
" return {\n",
" \"data\": 42\n",
" }\n",
" \n",
"get_data() # вернёт '{\"data\": 42}'\n",
"```\n",
"\n",
"Опишите декоратор в файле."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "41266401",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\"data\": 42}'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import json\n",
"\n",
"json.dumps({\"data\": 42})"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "993cc205",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\"data\": 42}'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from to_json import to_json\n",
"\n",
"@to_json\n",
"def get_data():\n",
" return {\n",
" \"data\": 42\n",
" }\n",
"\n",
"get_data()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}