Difference between revisions of "OPS435 Assignment 1 for Section C"
(Created page with "Category:OPS435-PythonCategory:rchan = Overview = When making back up of data files or log files, it is a very common practice to name the backup directories and/or fi...") |
Eric.brauer (talk | contribs) (update for fall 2020) |
||
Line 1: | Line 1: | ||
− | [[Category:OPS435-Python]][[Category: | + | [[Category:OPS435-Python]][[Category:ebrauer]] |
= Overview = | = Overview = | ||
− | + | Programs such as screenfetch and top are used to generate a summary of the computer's current state. These types of programs are useful because they present a lot of information quickly, and can quickly suggest a possible avenue of investigation for the systems administrator. | |
+ | For this assignment you will create a "System Information" type program. This program will briefly present important information about the state of the computer system. | ||
− | The | + | The preliminary code should look like this: |
+ | <b><code> | ||
+ | Hostname: NeoMex | ||
+ | Kernel Release: 5.4.0-48-generic | ||
+ | Uptime: up 1 week, 1 day, 17 hours, 14 minutes | ||
+ | ---------------------------------------- | ||
+ | /dev [ ] 0 % | ||
+ | / [============= ] 65 % | ||
+ | /boot/efi [==== ] 18 % | ||
+ | /home [=============== ] 77 % | ||
+ | ---------------------------------------- | ||
+ | Mem [======== ] 40 % | ||
+ | Swap [= ] 3 % | ||
+ | </b></code> | ||
− | In addition, | + | In addition, you will be expected to build on this capability with some relevant functions. |
= Assignment Requirements = | = Assignment Requirements = | ||
− | == The First Milestone (due | + | == Permitted Modules == |
+ | <b><font color='blue'>Your python script is allowed to import only the <u>os, subprocess and sys</u> modules from the standard library and all the built-in functions.</font></b> | ||
+ | |||
+ | == Required Functions == | ||
+ | You will need to complete the functions inside the provided file called <b>assignment1.py</b>. The provided <code>checkA1.py</code> will be used to test these functions. | ||
+ | |||
+ | * <code>call_df()</code> should take no arguments return a list of strings returned by the shell command. | ||
+ | * <code>call_free()</code> should take no arguments, and should return a list of strings from the shell. | ||
+ | * <code>call_hostname()</code> and <code>call_uptime()</code> should take no arguments, and should return strings from the shell. | ||
+ | * <code>percent_to_graph(percent)</code> will take an integer 'percent' and return a bar graph that represents this percentage. The bar graph should begin with '[', and end with ']'. Then contents inside should be 20 characters long. | ||
+ | * <code>print_percent_line(name, percent)</code> is provided as a convenience for you. It will print a properly formatted line, such as the one in the example above. | ||
+ | |||
+ | == Additional Functions == | ||
+ | Your code will need to have some additional functions that will accomplish the following: | ||
+ | * The output from <code>call_df()</code> should be filtered to omit any lines that contain <b>loop</b> or <b>tmpfs</b>. These are not proper file systems and should not be displayed. | ||
+ | * The output from <code>call_free()</code> should be used to calculate a percent of <b>memory used</b> divided by <b>total memory</b>. | ||
+ | * The output from uptime should be in "pretty" format, that is, in weeks, days, and so on. You may create this as a Python function, or you may also want to explore another way to do this. | ||
+ | |||
+ | Part of your evaluation will be on how "re-usable" your functions are, and sensible use of arguments and return values. | ||
+ | |||
+ | == Use of GitHub == | ||
+ | You will be graded partly on the quality of your Github commits. <b>Assignments that do not adhere to these requirements may not be accepted.</b> | ||
+ | |||
+ | Professionals generally follow these guidelines: | ||
+ | * commit their code after every significant change, | ||
+ | * the code should run without errors after each commit, and | ||
+ | * every commit has a descriptive commit message. | ||
+ | |||
+ | After completing each function, make a commit and push your code. | ||
+ | |||
+ | After fixing a problem, make a commit and push your code. | ||
+ | |||
+ | <b><u>GitHub is your backup and your proof of work.</b></u> | ||
+ | |||
+ | These guidelines are not always possible, but you will be expected to follow these guidelines as much as possible. Break your problem into smaller pieces, and work iteratively to solve each small problem. Test your code after each small change you make, and address errors as soon as they arise. It will make your life easier! | ||
+ | |||
+ | == Additional Features == | ||
+ | |||
+ | After completing the above, you are expected to add some additional (two or more) functions providing useful information. Some programs you might want to look at are: | ||
+ | |||
+ | * [https://https://ostechnix.com/neofetch-display-linux-systems-information/ screenfetch/neofetch] | ||
+ | * [https://htop.dev/ top/htop/Bashtop] | ||
+ | |||
+ | It is expected that the additional features you provided should be useful, non-trivial, they should not require super-user privileges and should not require the installation of additional modules or packages. | ||
+ | <b>In this part of the assignment, it is better to try for something useful and fail than it is to implement something trivial! I am looking for evidence that you have worked with Linux machines and know what kinds of information are useful to see at a glance.</b> | ||
+ | |||
+ | You might consider: | ||
+ | * Network information/IP addresses | ||
+ | * The state of some important daemons/systemd services | ||
+ | * process information | ||
+ | * information about online users | ||
+ | * number of packages installed | ||
+ | * cpu load | ||
+ | |||
+ | You may even choose to make the output more attractive/colourful etc. If so, you <i>are permitted to use more modules</i> than those specified above, but make sure that the required functions still succeed as they are. You may want to look into default arguments, ask me for clarification if you're interested. | ||
+ | |||
+ | == Coding Standard == | ||
+ | Your python script must follow the following coding guide: | ||
+ | * [https://www.python.org/dev/peps/pep-0008/ PEP-8 -- Style Guide for writing Python Code] | ||
+ | |||
+ | === Documentation === | ||
+ | * Please use python's docstring to document your python script (script level documentation) and each of the functions (function level documentation) you created for this assignment. The docstring should describe 'what' the function does, not 'how' it does. | ||
+ | * Your script should also include in-line comments to explain anything that isn't immediately obvious to a beginner programmer. <b><u>It is expected that you will be able to explain how each part of your code works in detail.</u></b> | ||
+ | * Refer to the docstring for after() to get an idea of the function docstrings required. | ||
+ | |||
+ | === Authorship Declaration === | ||
+ | All your Python code for this assignment must be placed in the provided Python file called <b>assignment1.py</b>. <u>Do not change the name of this file.</u> Please complete the declaration <b><u>as part of the docstring</u></b> in your Python source code file (replace "Student Name" with your own name). | ||
+ | |||
+ | = Submission Guidelines = | ||
+ | == The First Milestone (due October 19) == | ||
* Before you begin programming, it is important to plan your algorithm. Therefore your first task will be to complete and submit an algorithm document. This document should be named '''algorithm_[student_id].txt'''. This file should be plaintext. The document will contain two sections: | * Before you begin programming, it is important to plan your algorithm. Therefore your first task will be to complete and submit an algorithm document. This document should be named '''algorithm_[student_id].txt'''. This file should be plaintext. The document will contain two sections: | ||
* A description of how the "after()" function works. The "after()" function is provided to you in a1_template.py. Open the file, and use clear English to describe what line of code does in such a way that a competent programmer could reproduce the code without seeing it firsthand. | * A description of how the "after()" function works. The "after()" function is provided to you in a1_template.py. Open the file, and use clear English to describe what line of code does in such a way that a competent programmer could reproduce the code without seeing it firsthand. | ||
Line 17: | Line 100: | ||
* You should also do some research to find out when we started using the Calendar in the current form. (This will pose a limit on the validity of your algorithm.) | * You should also do some research to find out when we started using the Calendar in the current form. (This will pose a limit on the validity of your algorithm.) | ||
− | == The Assignment (due | + | == The Assignment (due November 3) == |
* As stated before, your code will be inside the file "a1_[studentid].py". The first step will be to clone the Assignment 1 repository. The invite link will be provided to you by your professor. Once you clone the repository, run this command: "cp a1_template.py a1_[studentid].py". (Replace studentid with your myseneca username). Begin writing the content that is required. Additional requirements are outlined below. | * As stated before, your code will be inside the file "a1_[studentid].py". The first step will be to clone the Assignment 1 repository. The invite link will be provided to you by your professor. Once you clone the repository, run this command: "cp a1_template.py a1_[studentid].py". (Replace studentid with your myseneca username). Begin writing the content that is required. Additional requirements are outlined below. | ||
− | == The Debrief (due | + | == The Debrief (due November 12) == |
This document, like the algorithm document, will be submitted to Blackboard one week after the assignment. Answer the following questions: | This document, like the algorithm document, will be submitted to Blackboard one week after the assignment. Answer the following questions: | ||
* Research Python modules that you could have used to accomplish the same goals as the today() and leap_year() functions. | * Research Python modules that you could have used to accomplish the same goals as the today() and leap_year() functions. | ||
Line 28: | Line 111: | ||
* Additionally, your professor may have questions specific to your submission. You should answer these questions as well. | * Additionally, your professor may have questions specific to your submission. You should answer these questions as well. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
== Tests and Test results == | == Tests and Test results == |
Revision as of 12:54, 9 October 2020
Contents
Overview
Programs such as screenfetch and top are used to generate a summary of the computer's current state. These types of programs are useful because they present a lot of information quickly, and can quickly suggest a possible avenue of investigation for the systems administrator. For this assignment you will create a "System Information" type program. This program will briefly present important information about the state of the computer system.
The preliminary code should look like this:
Hostname: NeoMex
Kernel Release: 5.4.0-48-generic
Uptime: up 1 week, 1 day, 17 hours, 14 minutes
/dev [ ] 0 % / [============= ] 65 % /boot/efi [==== ] 18 % /home [=============== ] 77 %
Mem [======== ] 40 % Swap [= ] 3 % </b>
In addition, you will be expected to build on this capability with some relevant functions.
Assignment Requirements
Permitted Modules
<b>Your python script is allowed to import only the os, subprocess and sys modules from the standard library and all the built-in functions.
Required Functions
You will need to complete the functions inside the provided file called assignment1.py. The provided checkA1.py
will be used to test these functions.
-
call_df()
should take no arguments return a list of strings returned by the shell command. -
call_free()
should take no arguments, and should return a list of strings from the shell. -
call_hostname()
andcall_uptime()
should take no arguments, and should return strings from the shell. -
percent_to_graph(percent)
will take an integer 'percent' and return a bar graph that represents this percentage. The bar graph should begin with '[', and end with ']'. Then contents inside should be 20 characters long. -
print_percent_line(name, percent)
is provided as a convenience for you. It will print a properly formatted line, such as the one in the example above.
Additional Functions
Your code will need to have some additional functions that will accomplish the following:
- The output from
call_df()
should be filtered to omit any lines that contain loop or tmpfs. These are not proper file systems and should not be displayed. - The output from
call_free()
should be used to calculate a percent of memory used divided by total memory. - The output from uptime should be in "pretty" format, that is, in weeks, days, and so on. You may create this as a Python function, or you may also want to explore another way to do this.
Part of your evaluation will be on how "re-usable" your functions are, and sensible use of arguments and return values.
Use of GitHub
You will be graded partly on the quality of your Github commits. Assignments that do not adhere to these requirements may not be accepted.
Professionals generally follow these guidelines:
- commit their code after every significant change,
- the code should run without errors after each commit, and
- every commit has a descriptive commit message.
After completing each function, make a commit and push your code.
After fixing a problem, make a commit and push your code.
GitHub is your backup and your proof of work.</b>
These guidelines are not always possible, but you will be expected to follow these guidelines as much as possible. Break your problem into smaller pieces, and work iteratively to solve each small problem. Test your code after each small change you make, and address errors as soon as they arise. It will make your life easier!
Additional Features
After completing the above, you are expected to add some additional (two or more) functions providing useful information. Some programs you might want to look at are:
It is expected that the additional features you provided should be useful, non-trivial, they should not require super-user privileges and should not require the installation of additional modules or packages. <b>In this part of the assignment, it is better to try for something useful and fail than it is to implement something trivial! I am looking for evidence that you have worked with Linux machines and know what kinds of information are useful to see at a glance.
You might consider:
- Network information/IP addresses
- The state of some important daemons/systemd services
- process information
- information about online users
- number of packages installed
- cpu load
You may even choose to make the output more attractive/colourful etc. If so, you are permitted to use more modules than those specified above, but make sure that the required functions still succeed as they are. You may want to look into default arguments, ask me for clarification if you're interested.
Coding Standard
Your python script must follow the following coding guide:
Documentation
- Please use python's docstring to document your python script (script level documentation) and each of the functions (function level documentation) you created for this assignment. The docstring should describe 'what' the function does, not 'how' it does.
- Your script should also include in-line comments to explain anything that isn't immediately obvious to a beginner programmer. It is expected that you will be able to explain how each part of your code works in detail.
- Refer to the docstring for after() to get an idea of the function docstrings required.
Authorship Declaration
All your Python code for this assignment must be placed in the provided Python file called assignment1.py. Do not change the name of this file. Please complete the declaration as part of the docstring in your Python source code file (replace "Student Name" with your own name).
Submission Guidelines
The First Milestone (due October 19)
- Before you begin programming, it is important to plan your algorithm. Therefore your first task will be to complete and submit an algorithm document. This document should be named algorithm_[student_id].txt. This file should be plaintext. The document will contain two sections:
* A description of how the "after()" function works. The "after()" function is provided to you in a1_template.py. Open the file, and use clear English to describe what line of code does in such a way that a competent programmer could reproduce the code without seeing it firsthand. * You will then apply the same principles to create an algorithm for "before()", and "dbda()". Inside the code, if you are calling another function like "leap_year", you may simply describe what the function will return, and not the operation of the function itself.
- This file will be submitted to Blackboard a week after the assignment goes live, and should be your first priority. The object of the milestone is not to have a 100% perfect algorithm, but to plan ahead and anticipate challenges and issues with the assignment. The milestone will also give your professor an opportunity for feedback.
- Here is an basic introduction to Algorithm
- While you are working on the step-by-step instructions, note that there are different number of days in each month and some years have 365 days and some years have 366 days.
- You should also do some research to find out when we started using the Calendar in the current form. (This will pose a limit on the validity of your algorithm.)
The Assignment (due November 3)
- As stated before, your code will be inside the file "a1_[studentid].py". The first step will be to clone the Assignment 1 repository. The invite link will be provided to you by your professor. Once you clone the repository, run this command: "cp a1_template.py a1_[studentid].py". (Replace studentid with your myseneca username). Begin writing the content that is required. Additional requirements are outlined below.
The Debrief (due November 12)
This document, like the algorithm document, will be submitted to Blackboard one week after the assignment. Answer the following questions:
- Research Python modules that you could have used to accomplish the same goals as the today() and leap_year() functions.
- Which solution is preferable, in terms of performance? Which solution is preferable, in terms of programmer hours? (which solution would take longer for a programmer to implement?)
- Which approach would be preferable in the "real world"? Why is it useful to try creating our own algorithm?
- What challenges did you encounter during the assignment, and what resources did you use to solve your issues? (help from classmates, help from Stackoverflow, debuggers, etc.)
- Additionally, your professor may have questions specific to your submission. You should answer these questions as well.
Tests and Test results
You must name your python 3 script as a1_[Student_id].py
. The following examples assumes that the student_id is rchan.The script should accept two command line arguments, the first one is the date in "YYYY-MM-DD" format, and the second one is the number of day from the given date, a positive value indicates the number of days after the given date, and a negative value indicates the number of days before the given date. There is an optional flag called --step which can be provided at the command line that makes the program print out all dates until the target date. If the "YYYY-MM-DD" format is broken, your script should give an appropriate error message. Invalid months (>12) or invalid days of month(different for each month), should be detected and give appropriate error messages. For examples:
-
python3 a1_rchan.py 01-01-2019 02-01-2019
, and the output should be
1
-
python3 a1_rchan.py 01-01-2019 31-12-2018
, and the output should be
-1
-
python3 a1_rchan.py 01-06-2020
, and since today is June 3, the output should be
2
-
python3 a1_rchan.py 01-01-2019 01-01-2020
, and the output should be
365
-
python3 a1_rchan.py 01-01-2021 01-01-2020
, and the output should be
-366
-
python3 a1_rchan.py 01-13-2018
, and the output should be
Error: wrong month entered
-
python3 a1_rchan.py 99-01-2020 01-01-2020
, and the output should be
Error: wrong day entered
-
python3 a1_rchan.py 2018 2
, and the output should be
Error: wrong date entered
If there is too few or too many command line arguments given, display the proper usage:
-
Usage: a1_rchan.py DD-MM-YYYY [DD-MM-YYYY] >/code>
Script structure and sample template
The following is a brief description of each function:
- The dbda() function should be the main function of your script. The dbda() function will take two dates in "DD-MM-YYYY" format, and return an integer that corresponds to the number of days between the given dates. If the start date is earlier than the stop date, the number is positive. If start date is later than the stop date, the number is negative. Your dbda() function should delegate the actual calculation of the target date to either the after() function or the before() function.
- The today() function will be called if the user has not specified a second argument. It will return your Linux computer's local time in the format DD-MM-YYYY. Hint: you may need to read man pages for a shell command in order to return a usable date. You may also use string formatting to modify output.
- The before() function will take a date in "DD-MM-YYYY" format and return the date of the previous day in the same format.
- The after() function will take a date in "DD-MM-YYYY" format and return the date of the next day in the same format. Next paragraph is a sample python code for the after() function. To earn the maximum possible mark for the assignment, you should modify the sample after() function to make use of the days_in_mon() function.
- The leap_year() function will take a year in "YYYY" format, and return True if the given year is a leap year, otherwise return False.
- The valid_date() function will take a date in "DD-MM-YYYY" format, and return True if the given date is a valid date, otherwise return False plus an appropriate status message. The valid_date() function should make use of the days_in_mon() function.
- The days_in_mon() function will take a year in "YYYY" format, and return a dictionary object which contains the total number of days in each month for the given year. The days_in_mon() function should make use of the leap_year() function.
- The usage() function will take no argument and return a string describing the usage of the script.
Sample code for the after() function
# Return the date in DD-MM-YYYY after the given day # def after(today): if len(today) != 10: return '00-00-0000' else: str_day, str_month, str_year = today.split('-') year = int(str_year) month = int(str_month) day = int(str_day) lyear = year % 4 if lyear == 0: feb_max = 29 # this is a leap year else: feb_max = 28 # this is not a leap year lyear = year % 100 if lyear == 0: feb_max = 28 # this is not a leap year lyear = year % 400 if lyear == 0: feb_max = 29 # this is a leap year tmp_day = day + 1 # next day mon_max = { 1:31, 2:feb_max, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31} if tmp_day > mon_max[month]: to_day = tmp_day % mon_max[month] # if tmp_day > this month's max, reset to 1 tmp_month = month + 1 else: to_day = tmp_day tmp_month = month + 0 if tmp_month > 12: to_month = 1 year = year + 1 else: to_month = tmp_month + 0 next_date = str(to_day)+"-"+str(to_month).zfill(2)+"-"+str(year).zfill(2) return next_date
Rubric
Task | Maximum mark | Actual mark |
---|---|---|
Program Authorship Declaration | 5 | |
Check script passed | 30 | |
today() function design | 2 | |
before() function design | 5 | |
dbda() function design | 10 | |
script level docstring | 5 | |
leap_year() function design | 2 | |
valid_date() function design | 5 | |
usage() function design | 1 | |
First Milestone | 10 | |
Reflection Essay | 10 | |
github.com repository: Commit messages and use | 15 | |
Total | 100 |
Due Date and Final Submission requirement
Check with your professor for the due date for your section.
Please submit the following files by the due date:
- [ ] your algorithm document, named as 'algorithm_username.txt', to Blackboard.
- [ ] your python script, named as 'a1_[seneca-id].py', should be included in your repository, and also submitted to Blackboard.
- [ ] the output of the checking script checkA1.py, named as 'a1_output.txt', should be included in your repository.
- [ ] your debrief document should be submitted to Blackboard.