diff --git a/journal/_src/posts/2022-10-31-lab-14.md b/journal/_src/posts/2022-10-31-lab-14.md new file mode 100644 index 0000000..d31f313 --- /dev/null +++ b/journal/_src/posts/2022-10-31-lab-14.md @@ -0,0 +1,9 @@ + Title: Lab Fourteen + Date: 2022-10-31T08:30:00 + Tags: cs2613, lab, python + +Sample description + + +## Sample Body +Sample Body \ No newline at end of file diff --git a/labs/L14/.idea/.gitignore b/labs/L14/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/labs/L14/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/labs/L14/.idea/L14.iml b/labs/L14/.idea/L14.iml new file mode 100644 index 0000000..74d515a --- /dev/null +++ b/labs/L14/.idea/L14.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/labs/L14/.idea/dataSources.xml b/labs/L14/.idea/dataSources.xml new file mode 100644 index 0000000..441bc28 --- /dev/null +++ b/labs/L14/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:C:\Users\Isaac\OneDrive - University of New Brunswick\Year 3 UNB\CS2613\Git\cs2613-ishoebot\labs\L14\.coverage + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/labs/L14/.idea/inspectionProfiles/profiles_settings.xml b/labs/L14/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/labs/L14/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/labs/L14/.idea/misc.xml b/labs/L14/.idea/misc.xml new file mode 100644 index 0000000..dd95ad9 --- /dev/null +++ b/labs/L14/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/labs/L14/.idea/modules.xml b/labs/L14/.idea/modules.xml new file mode 100644 index 0000000..8bc4f9b --- /dev/null +++ b/labs/L14/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/labs/L14/.idea/vcs.xml b/labs/L14/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/labs/L14/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/labs/L14/__pycache__/client.cpython-310.pyc b/labs/L14/__pycache__/client.cpython-310.pyc new file mode 100644 index 0000000..7306942 Binary files /dev/null and b/labs/L14/__pycache__/client.cpython-310.pyc differ diff --git a/labs/L14/__pycache__/divisive.cpython-310.pyc b/labs/L14/__pycache__/divisive.cpython-310.pyc new file mode 100644 index 0000000..4de7727 Binary files /dev/null and b/labs/L14/__pycache__/divisive.cpython-310.pyc differ diff --git a/labs/L14/__pycache__/humansize.cpython-310.pyc b/labs/L14/__pycache__/humansize.cpython-310.pyc new file mode 100644 index 0000000..dc19437 Binary files /dev/null and b/labs/L14/__pycache__/humansize.cpython-310.pyc differ diff --git a/labs/L14/__pycache__/test_client.cpython-310-pytest-7.2.0.pyc b/labs/L14/__pycache__/test_client.cpython-310-pytest-7.2.0.pyc new file mode 100644 index 0000000..33e805e Binary files /dev/null and b/labs/L14/__pycache__/test_client.cpython-310-pytest-7.2.0.pyc differ diff --git a/labs/L14/__pycache__/test_divisive.cpython-310-pytest-7.2.0.pyc b/labs/L14/__pycache__/test_divisive.cpython-310-pytest-7.2.0.pyc new file mode 100644 index 0000000..ca37451 Binary files /dev/null and b/labs/L14/__pycache__/test_divisive.cpython-310-pytest-7.2.0.pyc differ diff --git a/labs/L14/__pycache__/test_humansize.cpython-310-pytest-7.2.0.pyc b/labs/L14/__pycache__/test_humansize.cpython-310-pytest-7.2.0.pyc new file mode 100644 index 0000000..635d4a6 Binary files /dev/null and b/labs/L14/__pycache__/test_humansize.cpython-310-pytest-7.2.0.pyc differ diff --git a/labs/L14/client.py b/labs/L14/client.py new file mode 100644 index 0000000..5525e75 --- /dev/null +++ b/labs/L14/client.py @@ -0,0 +1,16 @@ +import humansize + + +def approximate_size(size): + """Returns the size of a file in a human-readable format where kilobytes are 1000 bytes + + :param size: the size of a file + :return: string + """ + return humansize.approximate_size(size, False) + + +if __name__ == '__main__': # pragma: no cover + print(approximate_size(1_000)) + print(approximate_size(100_000_000)) + print(approximate_size(1_000_000_000)) diff --git a/labs/L14/divisive.py b/labs/L14/divisive.py new file mode 100644 index 0000000..4776f25 --- /dev/null +++ b/labs/L14/divisive.py @@ -0,0 +1,8 @@ +import math + + +def fraction(a, b): + try: + return a / b + except ZeroDivisionError: + return math.nan diff --git a/labs/L14/fizzbuzz.py b/labs/L14/fizzbuzz.py new file mode 100644 index 0000000..9474f05 --- /dev/null +++ b/labs/L14/fizzbuzz.py @@ -0,0 +1,10 @@ +if __name__ == '__main__': + for i in range(1, 101): + if i % 3 == 0 and i % 5 == 0: + print("FizzBuzz") + elif i % 3 == 0: + print("Fizz") + elif i % 5 == 0: + print("Buzz") + else: + print(i) diff --git a/labs/L14/humansize.py b/labs/L14/humansize.py new file mode 100644 index 0000000..7a33b82 --- /dev/null +++ b/labs/L14/humansize.py @@ -0,0 +1,67 @@ +"""Convert file sizes to human-readable form. + +Available functions: +approximate_size(size, a_kilobyte_is_1024_bytes) + takes a file size and returns a human-readable string + +Examples: +>>> approximate_size(1024) +'1.0 KiB' +>>> approximate_size(1000, False) +'1.0 KB' + +""" + +SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], + 1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']} + + +def approximate_size(size, a_kilobyte_is_1024_bytes=True): + """Convert a file size to human-readable form. + + Keyword arguments: + size -- file size in bytes + a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024 + if False, use multiples of 1000 + + Returns: string + + """ + if size < 0: + raise ValueError('number must be non-negative') + + multiple = 1024 if a_kilobyte_is_1024_bytes else 1000 + for suffix in SUFFIXES[multiple]: + size /= multiple + if size < multiple: + return '{0:.1f} {1}'.format(size, suffix) + + raise ValueError('number too large') + + +if __name__ == '__main__': # pragma: no cover + print(approximate_size(1000000000000, False)) + print(approximate_size(1000000000000)) + +# Copyright (c) 2009, Mark Pilgrim, All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. diff --git a/labs/L14/test_client.py b/labs/L14/test_client.py new file mode 100644 index 0000000..7322664 --- /dev/null +++ b/labs/L14/test_client.py @@ -0,0 +1,18 @@ +import pytest +from client import approximate_size + + +def test_1kb(): + assert approximate_size(1_000) == "1.0 KB" + + +def test_100mb(): + assert approximate_size(100_000_000) == "100.0 MB" + + +def test_1gb(): + assert approximate_size(1_000_000_000) == "1.0 GB" + + +def test_docstring(): + assert approximate_size.__doc__ is not None diff --git a/labs/L14/test_divisive.py b/labs/L14/test_divisive.py new file mode 100644 index 0000000..bd9c673 --- /dev/null +++ b/labs/L14/test_divisive.py @@ -0,0 +1,10 @@ +from divisive import fraction +import math + + +def test_fraction_int(): + assert fraction(4, 2) == 2 + + +def test_fraction_NaN(): + assert math.isnan(fraction(4, 0)) diff --git a/labs/L14/test_humansize.py b/labs/L14/test_humansize.py new file mode 100644 index 0000000..673bd73 --- /dev/null +++ b/labs/L14/test_humansize.py @@ -0,0 +1,20 @@ +import pytest +from humansize import approximate_size + + +def test_1000(): + assert approximate_size(1000000000000, False) == "1.0 TB" + + +def test_1024(): + assert approximate_size(1000000000000) == "931.3 GiB" + + +def test_negative(): + with pytest.raises(ValueError): + approximate_size(-1) + + +def test_huge_number(): + with pytest.raises(ValueError): + approximate_size(1_000_000_000_000_000_000_000_000_000_000)