Skip to content

Instantly share code, notes, and snippets.

@arx8x
Created November 22, 2022 04:03
Show Gist options
  • Save arx8x/44bdeae5901d5ad817853948232f5868 to your computer and use it in GitHub Desktop.
Save arx8x/44bdeae5901d5ad817853948232f5868 to your computer and use it in GitHub Desktop.
Simple control flow tactics - for while
# This program goes through dictionary of lists containing
# different students in different divisions of a classs
# and finds a single student who has passed with above 80% score
divisions = {
'division A': [
{'name': 'Student 1', 'pass': False, 'percent': 10},
{'name': 'Student 2', 'pass': True, 'percent': 50},
{'name': 'Student 3', 'pass': True, 'percent': 77},
{'name': 'Student 4', 'pass': True, 'percent': 90}
],
'divison D': [
{'name': 'Student 11', 'pass': True, 'percent': 45},
{'name': 'Student 12', 'pass': False, 'percent': 5},
{'name': 'Student 13', 'pass': True, 'percent': 99},
{'name': 'Student 14', 'pass': True, 'percent': 80}
]
}
# We'll have an inner loop and outer loop here
# The outer loop iterates over divisions
# The innter loop iterates over students in the division
# We want the outer loop and inner loop to break when
# a match is found
# Why break both loops?
# We only need one match. In this example, the check to
# find a match is fairly simple, but in bigger programs,
# the check can be a very costly operation and we don't
# want the program to sit there and loop through everything
# even after finding a match. There could be thousands of items
# and if you find the match in the first few iterations, iterating
# over the rest of the items wastes a lot of time and resources
for division, students in divisions.items():
print(division)
for student in students:
print(student['name'])
if student['pass'] and student['percent'] > 80:
print(f"passed with {student['percent']} % marks")
# if this break happens, our else block won't work
# so the control flow goes back to outer loop
# and the outer loop has a 'break' terminating
# it.
break
else:
# control gets here only if the inner loop finishes
# in that case, we don't want the outer loop to break
# so the 'continue' below skips over the rest of the
# code, which is basically what stops the outer loop
continue
# after every inner loop finishes, the control comes here
# and it breaks the outer loop. We have set-up an else
# block in the inner loop to prevent this from happening
break
else:
# control comes here only if both loops finish
print("No student passed with above 80% marks")
import os
import random
import hashlib
# This program writes random data to a path with
# the data's hash as the filename
# WARNING: do not run this as root
# a set of paths the user prefs to write to
preferred_paths = ['/', '/sock/', '/sys/']
# create 2KiB for random data
data = random.randbytes(2048)
# check each preferred path
for path in preferred_paths:
# check if the path actually exists
if not os.path.exists(path):
print(f"[WARN] {path} doesn't exist")
continue
# check if the path is a diractory
if not os.path.isdir(path):
print(f"[WARN] Path isn't a directory")
continue
# check if the path can be written to
if os.access(path, os.W_OK | os.X_OK):
# if all checks pass, break the loop
# so the control flow goes back to main block
break
else:
print(f"[WARN] {path} is readonly")
else:
# this else is executed only when the loop finishes
# control doesn't get here if the loop was broken
# in our program, if a usable path is found, the loop
# is broken. So this else is basically a fallback/default
path = '/tmp/test_data/hash_test/'
# here we create the directory to write to
os.makedirs(path, exist_ok=True)
# now we write the data
hash = hashlib.sha1(data)
path += hash.hexdigest()
with open(path, 'wb') as f:
f.write(data)
print(f"[OK] written to {path}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment