Skip to content

Instantly share code, notes, and snippets.

@serrasqueiro
Last active February 17, 2020 01:35
Show Gist options
  • Save serrasqueiro/4dc11d55502d227829852393e49e0930 to your computer and use it in GitHub Desktop.
Save serrasqueiro/4dc11d55502d227829852393e49e0930 to your computer and use it in GitHub Desktop.
learningpie
""" learningpie.py -- Henrique Moreira
Simple python tweaks and slips!
"""
# pylint: disable=missing-docstring
import os
try:
_HAS_LINT = True
from lint import Run
except ModuleNotFoundError:
_HAS_LINT = False
def run_learning_pie(args):
"""
Main run.
:param args: system arguments
:return: int, an error-code (0 = Ok)
"""
code = 0
if args == []:
hickup()
show_iters()
show_invalid_paths()
else:
code = try_lint(argv[0], args[1:], _HAS_LINT)
return code
def hickup():
list_of_printers = []
for i in [1, 2, 3]:
def printer():
print(i, "This shows, surprisingly, 3!")
list_of_printers.append(printer)
for func in list_of_printers:
func()
return True
def show_iters():
source = ["a", "bb", "ccc"]
target1 = [len(elem) for elem in source]
print("Target:", target1)
def show_invalid_paths():
# y: list with non-empty strings for paths that are not in 'PYTHONPATH':
y = ["" if os.path.isdir(p) else p for p in os.environ['PYTHONPATH'].split(":")]
test_list = y
try:
res = next(sub for sub in test_list if sub)
except StopIteration:
res = None
if res is None:
print("PYTHONPATH ok: #{}".format(len(y)))
else:
print("At least one path not found:", res)
def try_lint(my_command, args, has_lint):
"""
Try to run pylint
:param my_command: my own script
:param args: system arguments
:param has_lint: whether lint.py module was found
:return: int, an error-code
"""
with_debug = ""
if not has_lint:
print("No pylint, no fun!")
return 4
print("Running pylint on:", my_command)
hint, param = args[0], args[1:]
what_list = []
for a in param:
is_dir, is_file = False, False
if a and not a.startswith("#"):
what_list.append(a)
if os.path.isdir(a):
is_dir = True
elif os.path.isfile(a):
is_file = True
assert a.endswith(".py")
else:
print("WHAT?", a)
return 2
assert is_dir or is_file
if hint == "ME":
assert what_list == []
what_list = [my_command]
elif hint == "DO_DEBUG":
with_debug = "debug"
elif hint == "DO":
with_debug = ""
else:
print("Invalid option:", hint)
assert False
Run(what_list, with_debug=with_debug)
return 0
if __name__ == "__main__":
import sys
from sys import argv
CODE = run_learning_pie(argv)
assert isinstance(CODE, int)
sys.exit(CODE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment