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.

201 lines
5.2 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": "4a1dd7c8",
"metadata": {},
"source": [
"# Задание. Key-value хранилище #"
]
},
{
"cell_type": "markdown",
"id": "4ba56dc0",
"metadata": {},
"source": [
"В этом блоке мы с вами реализуем собственный `key-value storage`. Вашей задачей будет написать скрипт, который принимает в качестве аргументов ключи и значения и выводит информацию из хранилища (в нашем случае — из файла)."
]
},
{
"cell_type": "markdown",
"id": "2cdd1fd1",
"metadata": {},
"source": [
"Запись значения по ключу"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f72ba88b",
"metadata": {},
"outputs": [],
"source": [
"! python storage.py --key key1 --val value1"
]
},
{
"cell_type": "markdown",
"id": "3ecc5e82",
"metadata": {},
"source": [
"Получение значения по ключу.\n",
"\n",
"Ответом в данном случае будет вывод с помощью print соответствующего значения:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "4d251cca",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"value1\r\n"
]
}
],
"source": [
"! python storage.py --key key1"
]
},
{
"cell_type": "markdown",
"id": "91c57474",
"metadata": {},
"source": [
"или"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "8346149f",
"metadata": {},
"outputs": [],
"source": [
"! python storage.py --key key1 --val value2"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "c6e52029",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"value1, value2\r\n"
]
}
],
"source": [
"! python storage.py --key key1"
]
},
{
"cell_type": "markdown",
"id": "e1e413c0",
"metadata": {},
"source": [
"Если значений по этому ключу было записано несколько. Метрики сохраняйте в порядке их добавления. Обратите внимание на пробел после запятой."
]
},
{
"cell_type": "markdown",
"id": "c16d0e67",
"metadata": {},
"source": [
"Если значений по ключу не было найдено, выводите пустую строку или `None`."
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "520ff264",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\r\n"
]
}
],
"source": [
"! python storage.py --key key2"
]
},
{
"cell_type": "markdown",
"id": "0646920d",
"metadata": {},
"source": [
"Для работы с аргументами командной строки используйте модуль [argparse](https://docs.python.org/3/howto/argparse.html \"Argparse Tutorial\"). Вашей задачей будет считать аргументы, переданные вашей программе, и записать соответствующую пару ключ-значение в файл хранилища или вывести значения, если был передан только ключ. Хранить данные вы можете в формате **JSON** с помощью стандартного модуля [json](https://docs.python.org/3/library/json.html \"19.2. json — JSON encoder and decoder\"). Проверьте добавление нескольких ключей и разных значений.\n",
"\n",
"Файл следует создавать с помощью модуля [tempfile](https://docs.python.org/3/library/tempfile.html \"11.6. tempfile — Generate temporary files and directories\")."
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "0fbd81cf",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import tempfile\n",
"\n",
"storage_path = os.path.join(tempfile.gettempdir(), \"storage.data\")\n",
"with open(storage_path, \"w\") as f:\n",
" ..."
]
},
{
"cell_type": "markdown",
"id": "de5a5212",
"metadata": {},
"source": [
"Создайте скрипт хранилища."
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "2a4a9396",
"metadata": {},
"outputs": [],
"source": [
"! python storage.py --clear"
]
}
],
"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
}