Top level use modules, implement separate parts in their respective modules, then refer to those in your main:
import random
import time
if time.time() > random.random():
pass
Next level (optional, use sparingly) use classes
class Foo:
def function1():
pass
class Bar:
def function2():
pass
Next level, what you need, use functions
def connect(...):
filename = ...
params = ...(filename)
return mysql.connect(*params)
def mainloop(...):
for xx in connect():
pass
Sublevel use blocks
def foo(path=None, port=None):
if not path:
filename = ...
path = ...(filename)
if not port:
foobar = ...
port = ...(foobar)
xxx.connect(path, port)
Subsublevel use blank lines and comments
def foo(...):
bar.bar()
assert path # <-- this is essentially a comment
smth_with(path)
some_other()
data = xxx.yyy()
assert data
foo = blahblah
bar = lambda: blahblah
filtered = filter(yada, data)
# data is clean at this point # <-- an actual comment
for x, y in data:
foo.bar.baz()
Final thoughts large comment blocks like in OQ show “code smell.” You are right to start wondering how to organise your code at this point 🙂