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.

160 lines
4.0 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": "9e725e75",
"metadata": {},
"source": [
"# Файл с магическими методами #"
]
},
{
"cell_type": "markdown",
"id": "4899791d",
"metadata": {},
"source": [
"В этом задании вам нужно создать интерфейс для работы с файлами. Класс `File` должен поддерживать несколько необычных операций.\n",
"\n",
"Класс инициализируется полным путем."
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "f6f38438",
"metadata": {},
"outputs": [],
"source": [
"from file import File"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "285d10f8",
"metadata": {},
"outputs": [],
"source": [
"obj = File(\"/tmp/file.txt\")"
]
},
{
"cell_type": "markdown",
"id": "d5b3cceb",
"metadata": {},
"source": [
"Класс должен поддерживать метод `write`."
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "457f1257",
"metadata": {},
"outputs": [],
"source": [
"obj.write(\"line\\n\")"
]
},
{
"cell_type": "markdown",
"id": "0e5ca18a",
"metadata": {},
"source": [
"Объекты типа `File` должны поддерживать сложение."
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "aee4e554",
"metadata": {},
"outputs": [],
"source": [
"first = File(\"/tmp/first\")\n",
"second = File(\"/tmp/second\")\n",
"\n",
"new_obj = first + second"
]
},
{
"cell_type": "markdown",
"id": "370a538c",
"metadata": {},
"source": [
"В этом случае создается новый файл и файловый объект, в котором содержимое второго файла добавляется к содержимому первого файла. Новый файл должен создаваться в директории, полученной с помощью [tempfile.gettempdir](https://docs.python.org/3/library/tempfile.html \"11.6. tempfile — Generate temporary files and directories\"). Для получения нового пути можно использовать [os.path.join](https://docs.python.org/3/library/os.path.html#os.path.join \"1.2. os.path — Common pathname manipulations\").\n",
"\n",
"Объекты типа `File` должны поддерживать протокол итерации, причем итерация проходит по строкам файла."
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "8041194e",
"metadata": {},
"outputs": [],
"source": [
"for line in File(\"/tmp/file.txt\"):\n",
" ..."
]
},
{
"cell_type": "markdown",
"id": "c177c4f2",
"metadata": {},
"source": [
"И наконец, при выводе файла с помощью функции `print` должен печататься его полный путь, переданный при инициализации."
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "0bc40684",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/tmp/file.txt\n"
]
}
],
"source": [
"obj = File(\"/tmp/file.txt\")\n",
"\n",
"print(obj)"
]
},
{
"cell_type": "markdown",
"id": "b54fb2db",
"metadata": {},
"source": [
"Опишите свой класс в скрипте."
]
}
],
"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
}