Question
for this task 01/project_name.py (50 points, group admin) Write a Python program that tries to guess project name when executed. The program will look for
for this task
01/project_name.py (50 points, group admin)
Write a Python program that tries to guess project name when executed.
The program will look for a file named README.md (in current directory) and it will print its first non-empty line. We assume that project name would be on the first line of the read-me file.
If all lines are empty or the file does not exist, the program will try to look for readme.md, README and readme in this order (stopping the search when title is found). That is, if readme.md contains non-empty line, it will not even try to look for README.
If no file from the above list contains non-empty line, the program will print name of the current directory (os.path.basename(os.getcwd())).
We consider line empty if line.strip().lstrip('# ') == ''. Printing the project title should also strip it of blank spaces and leading # (i.e. the above code). This ensures that trailing whitespace is ignored and for Markdown titles we remove extra formatting.
name the file project_name.py.
For this task, we have created a simple skeleton that you should use as a starting point.
Important: the first line in the skeleton code from us starting with #! is there for a reason and you have to keep it there. Also keep the usage of main() and the condition with __name__ as that represents a proper module-ready code.
here is the skeleton code:
#!/usr/bin/env python3 def main(): print('project-name') if __name__ == '__main__': main()
i got this implementation:
#!/usr/bin/env python3
import os
def get_project_name():
readme_files = ["README.md", "readme.md", "README", "readme"]
for readme_file in readme_files:
if os.path.exists(readme_file):
with open(readme_file, "r") as f:
for line in f:
line = line.strip().lstrip('# ')
if line:
return line
return ""
return os.path.basename(os.getcwd())
def main():
print(get_project_name())
if __name__ == '__main__':
main()
but i get this error messages:
ok 1 01/project_name.py - Submitted (informative)
22ok 2 01/project_name.py - Correct Python script (ok=5)
23ok 3 01/project_name.py - Module-ready (ok=5)
24ok 4 01/project_name.py - READMEs not committed (fail=-10)
25ok 5 01/project_name.py - Normal README.md present (ok=5)
26ok 6 01/project_name.py - Normal readme.md present (ok=5)
27ok 7 01/project_name.py - Normal README present (ok=5)
28ok 8 01/project_name.py - Normal readme present (ok=5)
29ok 9 01/project_name.py - Proper file priorities (ok=5)
30not ok 10 01/project_name.py - Ignore empty file (ok=5)
31# (from function `check_it' in file /root/.cache/mff_nswi177_2023/tests/repo/tasks/01-post/project_name.bats, line 28,
32# in test file /root/.cache/mff_nswi177_2023/tests/repo/tasks/01-post/project_name.bats, line 106)
33# `check_it "Project name 06"' failed
34#
35#
36# -- Program output mismatch --
37# file listing (3 lines):
38# .
39# ./readme.md
40# ./README.md
41# expected output (1 lines):
42# Project name 06
43# actual output (0 lines):
44#
45# --
46#
47ok 11 01/project_name.py - No README, use directory name (ok=5)
48ok 12 01/project_name.py - Ignore trailing whitespace (ok=5)
i need it to be fixed
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started