0x0B. Python - Input/Output
Name: Wendy Munyasi.
Email: wendymunyasi@gmail.com
Tel: +254707240068.
- Allowed editors:
vi
,vim
,emacs
. - All your files will be interpreted/compiled on Ubuntu 20.04 LTS using gcc, using python3 (version 3.8.5).
- All your files should end with a new line.
- The
main.py
files are used to test your functions, but you don’t have to push them to your repo. - The first line of all your files should be exactly
#!/usr/bin/python3
. - Your code should use the pycodestyle (version
2.8.*
). - All your files must be executable.
- All your test files should be inside a folder
tests
. - All your test files should be text files (extension:
.txt
). - All your tests should be executed by using this command:
python3 -m doctest ./tests/*
. - The length of your files will be tested using
wc
. - All your modules should have a documentation (
python3 -c 'print(__import__("my_module").__doc__)'
). - All your classes should have a documentation (
python3 -c 'print(__import__("my_module").MyClass.__doc__)'
). - All your functions (inside and outside a class) should have a documentation (
python3 -c 'print(__import__("my_module").my_function.__doc__)
' andpython3 -c 'print(__import__("my_module").MyClass.my_function.__doc__)'
). - A documentation is not a simple word, it’s a real sentence explaining what’s the purpose of the module, class or method (the length of it will be verified).
- You are not allowed to import any module unless stated otherwise.
Learn how to open a file.
How to write text in a file.
How to read the full content of a file.
How to read a file line by line.
How to move the cursor in a file.
How to make sure a file is closed after using it.
What is and how to use the with
statement.
What is JSON
.
What is serialization.
What is deserialization.
How to convert a Python data structure to a JSON string.
How to convert a JSON string to a Python data structure.
- 0. Read file - Write a function that reads a text file (
UTF8
) and prints it to stdout. -0-read_file.py
. - 1. Write to a file - Write a function that writes a string to a text file (
UTF8
) and returns the number of characters written. -1-write_file.py
. - 2. Append to a file - Write a function that appends a string at the end of a text file (
UTF8
) and returns the number of characters added. -2-append_write.py
. - 3. To JSON string - Write a function that returns the JSON representation of an object (string). -
3-to_json_string.py
. - 4. From JSON string to Object - Write a function that returns an object (Python data structure) represented by a JSON string. -
4-from_json_string.py
. - 5. Save Object to a file - Write a function that writes an Object to a text file, using a JSON representation. -
5-save_to_json_file.py
. - 6. Create object from a JSON file - Write a function that creates an Object from a “JSON file”. -
6-load_from_json_file.py
. - 7. Load, add, save - Write a script that adds all arguments to a Python list, and then save them to a file. -
7-add_item.py
. - 8. Class to JSON - Write a function that returns the dictionary description with simple data structure (list, dictionary, string, integer and boolean) for JSON serialization of an object. -
8-class_to_json.py
. - 9. Student to JSON - Write a class
Student
that defines a student by the given attributes. -9-student.py
. - 10. Student to JSON with filter - Write a class
Student
that defines a student by the given attributes. (based on9-student.py
). -10-student.py
. - 11. Student to disk and reload - Write a class
Student
that defines a student by the given attributes. (based on10-student.py
). -11-student.py
. - 12. Pascal's Triangle - Create a function
def pascal_triangle(n):
that returns a list of lists of integers representing the Pascal’s triangle ofn
. -12-pascal_triangle.py
. - 13. Search and update - Write a function that inserts a line of text to a file, after each line containing a specific string. -
100-append_after.py
. - 14. Log parsing - Write a script that reads
stdin
line by line and computes metrics. -101-stats.py
.
To collaborate, reach me through my email address wendymunyasi@gmail.com.