Last active
August 29, 2015 14:27
-
-
Save JoaoFelipe/d8401cd1b727b3a1adfd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| import sys | |
| import matplotlib.pyplot as plt | |
| from random import randint | |
| QTD = 10000 | |
| def avg(e): | |
| a, b = e | |
| return ((float(a[0] + b[0]) / 2.0), (float(a[1] + b[1]) / 2.0)) | |
| def simulate(a, b): | |
| return list(map(avg, zip(a, b))) | |
| def run_simulation(): | |
| a = [(randint(0, 100), randint(0, 100)) for x in range(QTD)] | |
| b = [(randint(0, 100), randint(0, 100)) for x in range(QTD)] | |
| data = simulate(a, b) | |
| return data | |
| def extract_column(data, column): | |
| col_data = [] | |
| for row in data: | |
| col_data.append(float(row[column])) | |
| return col_data | |
| def plot(data): | |
| t = extract_column(data, 0) | |
| p = extract_column(data, 1) | |
| plt.scatter(t, p, marker='o') | |
| plt.savefig("output.png") | |
| plot(run_simulation()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Parte do arquivo extraido do noWorkflow: | |
| # https://github.com/gems-uff/noworkflow/blob/master/capture/noworkflow/now/prov_deployment.py | |
| # https://github.com/gems-uff/noworkflow/blob/master/capture/noworkflow/now/cmd/cmd_run.py | |
| # https://github.com/gems-uff/noworkflow/blob/master/capture/noworkflow/now/prov_execution/__init__.py | |
| import importlib | |
| import modulefinder | |
| import sys | |
| import os | |
| import argparse | |
| from functools import wraps | |
| from datetime import datetime | |
| def duration(name): | |
| def dec(f): | |
| @wraps(f) | |
| def wrapper(*args, **kwargs): | |
| before = datetime.now() | |
| result = f(*args, **kwargs) | |
| after = datetime.now() | |
| print("{}:{}".format(name, (after - before).total_seconds())) | |
| return result | |
| return wrapper | |
| return dec | |
| @duration("modules") | |
| def collect_modules_provenance(path): | |
| finder = modulefinder.ModuleFinder() | |
| finder.run_script(path) | |
| for name, module in finder.modules.items(): | |
| if name != '__main__' and name not in sys.builtin_module_names: | |
| try: | |
| module = importlib.import_module(name) | |
| except: | |
| pass | |
| @duration("exec") | |
| def run(compiled, ns): | |
| import profile as profile | |
| pr = profile.Profile() | |
| pr.runctx(compiled, ns, ns) | |
| @duration("total") | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('-b', '--bypass', action='store_true') | |
| parser.add_argument('script', nargs=argparse.REMAINDER) | |
| args = parser.parse_args() | |
| path = os.path.realpath(args.script[0]) | |
| sys.path[0] = os.path.dirname(path) | |
| sys.argv = args.script | |
| import __main__ | |
| __main__.__dict__.clear() | |
| __main__.__dict__.update({'__name__' : '__main__', | |
| '__file__' : path, | |
| '__builtins__': __builtins__, | |
| }) | |
| if not args.bypass: | |
| collect_modules_provenance(path) | |
| with open(path, 'rb') as f: | |
| code = f.read() | |
| compiled = compile(code, path, 'exec') | |
| run(compiled, __main__.__dict__) | |
| if __name__ == '__main__': | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - Para executar carregando os modulos: | |
| $ python mod.py example.py | |
| - Para executar sem carregar os modulos (mas mostrando tempo de execução): | |
| $ python mod.py -b example.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding: utf-8 | |
| # Por algum motivo, quando o finder é executado como __main__, | |
| # o python não encontra a variavel __builtins__ | |
| from finder import main | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment