Changes

Jump to: navigation, search

OPS435 Python/assignment 1 fall2022

1,363 bytes removed, 14:48, 24 September 2022
Rubric
An example of the finished code your script might produce is this:
<code><b>user@host ~ $ ./duim.py -H /usr/local/lib</b></code>
<pre>
61 % [============ ] 160.2 M 164028 /usr/local/lib/heroku 4 % [= ] 10.8 M 11072 /usr/local/lib/python2.7 34 % [======= ] 90.4 M 92608 /usr/local/lib/node_modules 0 % [ ] 8.0 K /usr/local/lib/python3.8Total: 261.4 M 267720 /usr/local/lib
</pre>
Notice that total size of the target directory (/usr/local/lib) is around 260 Megabytes. Of that 260 Megabytes, 160 Megabytes can be found in the heroku subdirectory.
160 MB 164028 represents 61% of the total 160 MB267720. The percentages don't have to add up to 100%, since with these arguments we are excluding files in the target directory. You may choose to add an option to your script to print files as well.
The bar chart in this example is 20 characters long, but this must be dynamic. The 20 characters does <i>not</i> include the square brackets. The resolution of the bar chart must become more accurate as you increase the total size. For example, if the user specifies a length of 100 total characters, in this example 61 of those characters would be equal signs and 39 would be spaces.
The output of each subdirectory should include percentage, size in bytes (or Human readable if the user uses the -H option), the bar chart and the name of the subdirectory.
Specific formatting of the final output will be up to you, but should be formatted in such a way that the output is easy to read. (ie. use columns!)
== Required Functions ==
You will need to complete the functions inside the provided file called <code>duim.py</code>. The provided <code>checkA2checkA1.py</code> will be used to test these functions.
* 1. <code>call_du_sub()</code> should take the target directory as an argument and return a list of strings returned by the command <b>du -d 1<target directory></b>.** Use subprocess.Popenor os.popen.
** '-d 1' specifies a <i>max depth</i> of 1. Your list shouldn't include files, just a list of subdirectories in the target directory.
** Your list should <u>not</u> contain newline characters.
* 2. <code>percent_to_graph()</code> should take two arguments: percent and the total chars. It should return a 'bar graph' as a string.** Your function should check that the percent argument is a valid number between 0 and 100. It should fail if it isn't. You can <code>raise ValueError</code> in this case.
** <b>max_length</b> refers to the total number of characters that the bar graph will be composed of. You can use equal signs <code>=</code> or any other character that makes sense, but the empty space <b>must be composed of spaces</b>, at least until you have passed the first milestone.
** The string returned by this function should only be composed of these two characters. For example, calling <code>percent_to_graph(50, 10)</code> should return:
'===== '
<b>Please note that the '' characters should <u>not</u> be part of the output, they are here to indicate that this is a string!</b>
* 3. <code>create_dir_dict</code> should take a list as the argument, and should return a dictionary.
** The list can be the list returned by <code>call_du_sub()</code>.
** The dictionary that you return should have the full directory name as <i>key</i>, and the number of bytes in the directory as the <i>value</i>. This value should be an integer. For example, using the example of <b>/usr/local/lib</b>, the function would return:
{'/usr/local/lib/heroku': 164028, '/usr/local/lib/python2.7': 11072, ...}
 
4. <code>get_max</code> should take a dictionary and the target directory as arguments, and it should return the total size of the target directory as an integer.
** The dictionary returned by <code>create_dir_dict</code> will be used for this function. When calculating the percentage for each subdirectory, you will need the total size of the target directory as the denominator. This function will return that value.
** There are many approaches you can take to get this number. Some approaches won't require the target directory as an argument, but pass it in anyway.
== Additional Functions ==
You may create any other functions that you think appropriate, especially when if you begin decide to build additional functionality. Part of your evaluation will be on how "re-usable" your functions are, and sensible use of arguments and return values.
== Use of GitHub ==
The repo will contain a check script, a README file, and the file where you will enter your code.
== The First Milestone (due July 26October 7) ==
For the first milestone you will have two functions to complete.
* <code>call_du_sub</code> will take one argument and return a list. The argument is a target directory. The function will use <code>subprocess.Popen</code> to run the command <b>du -d l <target_directory></b>.
To test with the check script, run the following:
<code>python3 checkA2checkA1.py -f -v TestPercent</code>
<code>python3 checkA2checkA1.py -f -v TestDuSub</code>
== Second Milestone (due August 2October 21) ==
For the second milestone you will have two more functions to complete.
* 1. <code>create_dir_dict</code> will take your list from <code>call_du_sub</code> and return a dictionary. ** Every item in your list should create a key in your dictionary.** Your dictionary values should be a number of bytes.
For example: <code>{'/usr/lib/local': 33400}</code>
** Again, test using your Python interpreter or the check script. To run the check script, enter the following:
<code>python3 checkA2.py -f -v TestDirDict</code>* To run the check script, enter the following:
You will be using a module in the standard library called <bcode>Argparse</b>. This will help handle more complex sets of options and arguments than simply using sys.argvpython3 checkA1.Refer to the argparse documentation to complete the <code>parse_command_argspy -f -v TestDirDict</code> function. At minimum, your assignment should handle the following options and arguments:
* -h will print a usage message2. This will automatically be created by argparse itself, you will not need <code>get_total</code> needs to implement this. Howeveraccept your dictionary, refer carefully to and the sample output target directory, and ensure that your help message matches return the required output.* -H will print file sizes total size in Human readable format. For example, 1024 bytes will be printed as 1K, 1024 kilobytes will be printed as 1M, and so on. * -l <number> will set the maximum length of the bar graph. The default should be 20 character. This option will require an option argument that is an integer.* Your script will also require one positional argument which contains the target directory for scanning.
Your assignment should be able to produce * Use the followingto test your code:
<code><b>user@host ~ $ python3 duim.py -h</b></code><pre>usage: duim.py [-h] [-H] [-l LENGTH] [target] DU Improved -- See Disk Usage Report with bar charts positional arguments: target The directory to scan. optional arguments: -h, --help show this help message and exit -H, --human-readable print sizes in human readable format (e.g. 1K 23M 2G) -l LENGTH, --length LENGTH Specify the length of the graph. Default is 20. Copyright 2021</pre> Use the following to test your code: <code>python3 checkA2checkA1.py -f -v TestArgsTestTotal</code>
== Minimum Viable Product ==
Once you have achieved the Milestones, you will have to do the following to get a minimum viable product:
* In your <code>if __name__ == '__main__'</code> block, you will have to call accept a command line argument from the parse_command_args function. Experiment with print statements so that you understand how each option and argument are storeduser This will become your target directory.
** If the user has entered more than one argument, or their argument isn't a valid directory, print an error message.
** If the user doesn't specify any target, use the current directory.
* Call <code>call_du_sub</code> with the target directory.
* Pass the return value from that function to <code>create_dir_dict</code>.* You may wish Pass the dictionary into <code>get_total</code> to create one or more functions to do get the total size of the target.* For each item in your dictionary (excluding the followingtarget):
** Use the total size of the target directory to calculate percentage.
** For each subdirectory of target directory, you will need to calculate a percentage, using the total of the target directory.
** Once you've calculated percentage, call <code>percent_to_graph</code> with a max_size of your choice.
** For every subdirectory, print <i>at least</i> the percent, the bar graph, and the name of the subdirectory.
** The target directory <b>should not</b> have a bar graph.
* Finally, print the total size of the target directory.
== Additional Features ==
After completing the above, you are expected may choose to add some additional features. Some improvements you could make are:
* Format the output in a way that is easy to read.
* Convert bytes into a human readable format.
* Add colour to the output.
* Include files in the output.
* Sort the output by percentage, or by filename.
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 packages to work. (ie: I shouldn't have to run pip to make your assignment work). == The Assignment (due August 6November 4, 11:59pm) ==
* Be sure to make your final commit before the deadline. Don't forget to also use <code>git push</code> to push your code into the online repository!
* Then, copy the contents of your <b>duim.py</b> file into a Word document, and submit it to Blackboard. <i>I will use GitHub to evaluate your deadline, but submitting to Blackboard tells me that you wish to be evaluated.</i>
| Program Authorship Declaration || 5 ||
|-
| required functions design First Milestone || 5 10 ||
|-
| required functions readability Second Milestone || 5 10 ||
|-
| main loop percent function design || 10 ||
|-
| main loop readability get total function design || 10 ||
|-
| output function other functions design || 5 ||
|-
| output function readability Error checking and exception handling || 5 10 ||
|-
| additional features implemented required functions readability/variable naming || 20 5 ||
|-
| docstrings and comments main loop design || 5 ||
|-
| First Milestone main loop readability || 10 5 ||
|-
| Second Milestone docstrings and comments || 10 15 ||
|-
| github.com repository: Commit messages and use || 10 ||
Please submit the following files by the due date:
* [ ] your python script, named as 'duim.py', in your repository, and also '''submitted to Blackboard''', by August 6 November 4 at 11:59pm.

Navigation menu