Giter Club home page Giter Club logo

loggers's Introduction

"""Functions for other modules (screen messages and error logger).

Has:
- elapsed time decorator
- logger
- progress bar

"""

import time

def create(year):
    prefix = "(%s)" % year
    def foo(*args):
        print (prefix, *args)
    return foo


def print_elapsed_time(foo):
    """Print execution time for *f* to screen."""
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = foo(*args, **kwargs)
        print("Time elapsed: %.1f seconds" % (time.time()-start_time))
        return result
    return wrapper


class GenericLogger():
    """Log errors while reading rows."""
    def __init__(self, filename):
        self.log_filename = filename

    def start(self):
        with open(self.log_filename, 'w') as f:
            print("Log started", file=f)

    def report(self, *msg):
        with open(self.log_filename, 'a') as f:
            print(*msg)
            print(*msg, file=f)


class Logger(GenericLogger):
    """Logger with predefined filename for error stream."""

    def __init__(self, year):
        filename = config.make_path_error_log(year)
        super().__init__(filename)


class Progress():
    """Minimal progress 'spinner'.
       See also <http://docs.astropy.org/en/v0.2/_generated/astropy.utils.console.Spinner.html#>
    """

    STEP = 100*1000

    def __init__(self):
        self.count = 0
        self.k = 0

    def next(self):
        self.count += 1
        self.k += 1
        if self.k == self.STEP:
            print("%7d lines read" % self.count)
            self.k = 0

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        pass


def pipe(gen):
    """Decorate *gen* with progress messages."""
    with Progress() as prog:
        for item in gen:
            prog.next()
            yield item

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.