# Reasoning

## or why program slicing?

``````def get_primes_wrong(limit=50):
primes = [2, 3]
for x in xrange(4, limit + 1):
if x % 2 == 0 or x % 3 == 0:
continue
primes.append(x)
return primes``````
``````def get_primes_old(limit=50):
potentials = []
primes = []
for i in xrange(2, limit + 1):
potentials.append(i)
potentials.reverse()
while potentials:
x = potentials.pop()
primes.append(x)
new = []
for p in potentials:
if p % x != 0:
new.append(p)
potentials = new

return primes``````
``````# https://github.com/fabric/fabric/blob/master/fabric/operations.py
def _run_command(command, shell=True, pty=True, combine_stderr=True,
sudo=False, user=None, quiet=False, warn_only=False, stdout=None,
stderr=None, group=None, timeout=None, shell_escape=None):
"""
"""
manager = _noop
if warn_only:
manager = warn_only_manager
# Quiet's behavior is a superset of warn_only's, so it wins.
if quiet:
manager = quiet_manager
with manager():
# Set up new var so original argument can be displayed verbatim later.
given_command = command

# Check if shell_escape has been overridden in env
if shell_escape is None:
shell_escape = env.get('shell_escape', True)

# Handle context manager modifications, and shell wrapping
wrapped_command = _shell_wrap(
_prefix_commands(_prefix_env_vars(command), 'remote'),
shell_escape,
shell,
_sudo_prefix(user, group) if sudo else None
)
# Execute info line
which = 'sudo' if sudo else 'run'
if output.debug:
print("[%s] %s: %s" % (env.host_string, which, wrapped_command))
elif output.running:
print("[%s] %s: %s" % (env.host_string, which, given_command))

# Actual execution, stdin/stdout/stderr handling, and termination
result_stdout, result_stderr, status = _execute(
channel=default_channel(), command=wrapped_command, pty=pty,
combine_stderr=combine_stderr, invoke_shell=False, stdout=stdout,
stderr=stderr, timeout=timeout)

# Assemble output string
out = _AttributeString(result_stdout)
err = _AttributeString(result_stderr)

# Error handling
out.failed = False
out.command = given_command
out.real_command = wrapped_command
if status not in env.ok_ret_codes:
out.failed = True
msg = "%s() received nonzero return code %s while executing" % (
which, status
)
if env.warn_only:
msg += " '%s'!" % given_command
else:
msg += "!\n\nRequested: %s\nExecuted: %s" % (
given_command, wrapped_command
)
error(message=msg, stdout=out, stderr=err)

# Attach return code to output string so users who have set things to
# warn only, can inspect the error code.
out.return_code = status

# Convenience mirror of .failed
out.succeeded = not out.failed

# Attach stderr for anyone interested in that.
out.stderr = err

return out``````

# Reasoning gets harder with increasing complexity

``````def get_primes(limit=50):
primes = [2, 3]
for x in xrange(4, limit + 1):
if x % 2 == 0 or x % 3 == 0:
continue
primes.append(x)
return primes``````

def get_primes(limit=50):
primes = [2, 3]
for x in xrange(4, limit + 1):
if x % 2 == 0 or x % 3 == 0:
continue
primes.append(x)
return primes

``````def get_primes_old(limit=50):
potentials = []
primes = []
for i in xrange(2, limit + 1):
potentials.append(i)
potentials.reverse()
while potentials:
x = potentials.pop()
primes.append(x)
new = []
for p in potentials:
if p % x != 0:
new.append(p)
potentials = new

return primes``````

def get_primes_old(limit=50):
potentials = []
primes = []
for i in xrange(2, limit + 1):
potentials.append(i)
potentials.reverse()
while potentials:
x = potentials.pop()
primes.append(x)
new = []
for p in potentials:
if p % x != 0:
new.append(p)
potentials = new

return primes

By Roman Joost

• 2,619