Giter Club home page Giter Club logo

python-snippets's Introduction

How to merge list of dictionaries into a single dictionary, putting values of keys into a list, i.e. turn

foo = [
    {'a': 'x', 'b': 'y', 'c': 'z'},
    {'a': 'j', 'c': 'z'}
]

into

bar = {
    'a': ['x', 'j'],
    'b': ['y', None],
    'c': ['z', 'z']
}
bar = {
    k: [d.get(k) for d in foo]
    for k in set().union(*foo)
}

Simple python to script that executes N times and gives overall average runtime, with runtime for each step

import subprocess
import time
import sys

def measure_time(command, num_runs):
    total_time = 0

    for i in range(1, num_runs + 1):
        print(f"Running iteration {i}...")
        start_time = time.time()
        subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        end_time = time.time()
        elapsed_time = end_time - start_time
        print(f"Iteration {i} took {elapsed_time:.6f} seconds")
        total_time += elapsed_time

    average_time = total_time / num_runs

    minutes = int(average_time // 60)
    seconds = int(average_time % 60)
    milliseconds = int((average_time % 1) * 1000)

    print(f"Average time for {num_runs} runs: {minutes} minutes, {seconds} seconds, {milliseconds} milliseconds")

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage: python script.py '<command>' <num_runs>")
        sys.exit(1)

    command = sys.argv[1]
    num_runs = int(sys.argv[2])

    measure_time(command, num_runs)

python-snippets's People

Contributors

shumakoff avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.