<class 'str'>
Hello, World.
r
7
January 20, 2025
def detect() -> Dict[str, str]:
"""Run all of the sub-detects."""
run_output: List[Dict[str, str]] = []
run_output = execute.execute_by_name_filter(
constants.project.Detect_Module, constants.project.Detect_Filter
)
return util.union_list_of_dicts(run_output)Calling the detect function causes each line to run sequentially
Invoke execute_by_name_filter function with two inputs
Invoke the union_list_of_dicts function with one input
Return output of union_list_of_dicts function to caller
if platform.system() == "Windows":
total_disk = psutil.disk_usage("C:\\").total
used_disk = psutil.disk_usage("C:\\").used
else:
total_disk = psutil.disk_usage("/").total
used_disk = psutil.disk_usage("/").usedThe psutil module provides access to system information
The disk_usage function returns information about disk usage
But disk_usage needs different inputs for the filesystem root!
Conditional logic determines the input to disk_usage
def union_list_of_dicts(container: List[Dict[str, str]]) -> Dict[str, str]:
"""Union a list of dictionaries into a single dictionary."""
output: Dict[str, str] = {}
for dictionary in container:
output = {**output, **dictionary}
return outputoutput is a dictionary that starts out empty
container is a list of dictionaries
The for loop iterates through each dictionary in container
The body of the loop merges the dictionaries into output
str in Pythonlist in Pythontuple in Pythondict in Pythonset in PythonLet’s explore each of these in greater detail!
<class 'str'>
Hello, World.
r
7
type function?u[9] notation?str function?+ operator?numbers = [1,2,3,4,5,6]
print(type(numbers))
numbers.append(100)
print("The first item is", numbers[0])
print("The second item is", numbers[1])
print("The last item is", numbers[-1])
print("The second to last item is", numbers[-2])
numbers[2] = "skip"
numbers[3] = "a"
numbers[4] = "few"
numbers[-2] = 99
print(numbers)<class 'list'>
The first item is 1
The second item is 2
The last item is 100
The second to last item is 6
[1, 2, 'skip', 'a', 'few', 99, 100]
What does this illustrate about the list type in Python?
<class 'tuple'>
(1, 2, 'skip a few', 99, 100)
100
tuple?
data[4] = 100data.append(100)data.remove(100)data.insert(100)data.pop(100)<class 'dict'>
{5: 'five', 2: 'two', 'pi': 3.1415926}
3.1415926
dict function creates an empty dictionary called d{5: 'five'} and {2: 'two'}intstr and floatd["pi"]values = {2,1}
print(type(values))
values.add(3)
values.add(2)
values.add(2)
values.add(2)
print(values)<class 'set'>
{1, 2, 3}
add function places more data in a setadd with 2 do not change the set{} is an empty dictionary!a = "a string"
b = ["my", "second", "favorite", "list"]
c = (1, "tuple")
d = {"a": "b", "b": 2, "c": False}
e = {1,2,3,4,4,4,4,2,2,2,1}
print(len(a), len(b), len(c), len(d), len(e))8 4 2 3 4
list)dict)set)tuple)str)Listfrom typing import List
def containment_check_list(thelist: List[int], number: int) -> bool:
"""Determine if a value is contained in the list."""
# assume that the value is not inside of the list
found = False
# the value is, in fact, inside of the list
if number in thelist:
found = True
# return bool to indicate whether or not value is found
return foundthelist?number?containment_check_list?if number in thelist?Tuplefrom typing import Tuple
def containment_check_tuple(thetuple: Tuple[int], number: int) -> bool:
"""Determine if a value is contained in the list."""
# assume that the value is not inside of the tuple
found = False
# the value is, in fact, inside of the tuple
if number in thetuple:
found = True
# return the bool to indicate whether or not value is found
return foundthetuple?number?containment_check_tuple?if number in thetuple?Algorithmology