Skip to content

Commit

Permalink
fix Timer
Browse files Browse the repository at this point in the history
  • Loading branch information
sirfoga committed Oct 14, 2018
1 parent ce40668 commit 1a75fad
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 18 deletions.
20 changes: 20 additions & 0 deletions docs/source/hal.hashes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
hal.hashes package
==================

.. automodule:: hal.hashes
:members:
:undoc-members:
:show-inheritance:

Submodules
----------

hal.hashes.md5 module
---------------------

.. automodule:: hal.hashes.md5
:members:
:undoc-members:
:show-inheritance:


8 changes: 8 additions & 0 deletions docs/source/hal.profile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ hal.profile.mem module
:undoc-members:
:show-inheritance:

hal.profile.models module
-------------------------

.. automodule:: hal.profile.models
:members:
:undoc-members:
:show-inheritance:

hal.profile.performance module
------------------------------

Expand Down
2 changes: 2 additions & 0 deletions docs/source/hal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Subpackages
hal.cvs
hal.data
hal.files
hal.hashes
hal.internet
hal.maths
hal.meta
Expand All @@ -23,6 +24,7 @@ Subpackages
hal.profile
hal.streams
hal.strings
hal.system
hal.tests
hal.times
hal.wrappers
Expand Down
20 changes: 20 additions & 0 deletions docs/source/hal.system.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
hal.system package
==================

.. automodule:: hal.system
:members:
:undoc-members:
:show-inheritance:

Submodules
----------

hal.system.process module
-------------------------

.. automodule:: hal.system.process
:members:
:undoc-members:
:show-inheritance:


2 changes: 1 addition & 1 deletion hal/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__description__ = 'Your swiss knife to perform fast and easy pythonic stuff'
__url__ = 'https://sirfoga.github.io/pyhal/'
__version__ = '10.2.6'
__build__ = 'f431a5c25ca4b900e42b8407e48b6c3dfd04c382'
__build__ = '3ab1a4ac77c2310fb7bf8964fa78dab018b43d72'
__author__ = 'Stefano Fogarollo'
__author_email__ = 'sirfoga@protonmail.com'
__license__ = 'MIT'
Expand Down
9 changes: 9 additions & 0 deletions hal/meta/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,12 @@ def get_class_name(obj):
:return: Name of class
"""
return str(obj.__class__.__name__)


def get_method_name(func):
"""Finds name of method
:param func: method
:return: Name of method
"""
return str(func.__name__)
2 changes: 1 addition & 1 deletion hal/profile/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Timer(object):
def __enter__(self):
self.__start = time.time()

def __exit__(self, type, value, traceback):
def __exit__(self, exception_type, exception_value, traceback):
self.__finish = time.time()

def elapsed_time(self):
Expand Down
25 changes: 14 additions & 11 deletions hal/profile/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""Perform benchmarks and tests on your PC """

import getpass
import os
import random
import sys

Expand Down Expand Up @@ -68,8 +69,9 @@ def run_test(self):
"""
timer = Timer()

problem = EightQueen(self.size)
problem.solve(problem.board_size)
with timer:
problem = EightQueen(self.size)
problem.solve(problem.board_size)

return timer.elapsed_time()

Expand All @@ -96,18 +98,19 @@ def run(self):
"""Runs test and safes results"""
max_board_size = self.size
timer = Timer()
with timer:
for size in range(max_board_size + 1):
timing = self.run_test()
self.update_std_out_and_log(
"BOARD SIZE".ljust(10) +
str(size).ljust(10) + "TIME REQUIRED (s)".ljust(20) +
str('{:03.3f}'.format(timing))
)

for size in range(max_board_size + 1):
timing = self.run_test()
self.update_std_out_and_log(
"BOARD SIZE".ljust(10) +
str(size).ljust(10) + "TIME REQUIRED (s)".ljust(20) +
str('{:03.3f}'.format(timing))
)
length = timer.elapsed_time()
seconds = timer.elapsed_time()

self.update_std_out_and_log(
"It took me " + str(length) + " seconds to complete all " + str(
"It took me " + str(seconds) + " seconds to complete all " + str(
max_board_size) + " problems.\nThanks for your patience!")

if input("Do you wanna save the results? [y/n]").startswith("y"):
Expand Down
6 changes: 4 additions & 2 deletions hal/wrappers/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import functools

from hal.meta.attributes import get_method_name
from hal.profile.models import Timer
from hal.streams.logger import log_message

Expand All @@ -26,11 +27,12 @@ def _execute(*args, **kwargs):
:return: function result
"""

func_name = func.func_name
func_name = get_method_name(func)
timer = Timer()
log_message(func_name, "has started")

result = func(*args, **kwargs)
with timer:
result = func(*args, **kwargs)

seconds = "{:.3f}".format(timer.elapsed_time())
log_message(func_name, "has finished. Execution time:", seconds, "s")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_hal_maths_primes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def test_blum_blum_shub():

assert len(randoms) == amount

distro = describe(randoms)
distribution = describe(randoms)

assert abs(distro.skewness) <= 0.5
assert abs(distribution.skewness) <= 0.5

assert not blum_blum_shub(seed, 0, primes[0], primes[1])

Expand Down
13 changes: 12 additions & 1 deletion tests/test_hal_meta_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@


"""Tests hal.meta.attributes implementation"""

import os

from hal.meta.attributes import ModuleFile
from hal.meta.attributes import ModuleFile, get_method_name, get_class_name
from hal.meta.attributes import get_modules

HERE = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -32,6 +33,16 @@ def test_get_modules():
assert meta_module not in modules


def test_get_class_name():
"""Tests hal.meta.attributes.get_class_name method"""
assert get_class_name(None) == "NoneType"


def test_get_method_name():
"""Tests hal.meta.attributes.get_method_name method"""
assert get_method_name(get_method_name) == "get_method_name"


class TestModuleFile:
"""Tests ModuleFile class"""

Expand Down

0 comments on commit 1a75fad

Please sign in to comment.