Skip to content

Commit

Permalink
AnalysisFormatter moved to formatters file
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaySimakov committed Jul 1, 2023
1 parent e7b847e commit 7ce19aa
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 55 deletions.
54 changes: 0 additions & 54 deletions pycomplexity/analysis.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import ast
import shutil
from typing import List, Tuple
from pprint import pprint
from .enums import NotationFormat


'''
Expand All @@ -27,54 +24,3 @@ def visit_Import(self, node):

def report(self):
pprint(self.stats)



'''
Class that contains and represents analysis data
'''
class AnalysisFormatter:

def __init__(
self,
notation_format: NotationFormat,
func_name: str = 'No name',
func_attrs: List[Tuple[str, str]] | None = None,
complexity: str = '1',
memory: str = '1',
) -> None:

self.notation_format = notation_format
self.func_name = func_name
self.func_attrs = func_attrs
self.complexity = complexity
self.memory = memory

def report(self, full=True):

print()

if full:
print('Function name:', self.func_name)

if self.func_attrs:
print('Function attributes:', ', '.join([f'{attr_name}: {attr_type}' for attr_name, attr_type in self.func_attrs]))

terminal_width = shutil.get_terminal_size().columns
indent = max((terminal_width - len(self.notation_format.__str__()) - 2)//2, 0)
print('-' * indent + f' {self.notation_format.__str__()} ' + '-' * indent)

if self.notation_format == NotationFormat.BIG_O:
print('Сomplexity of algorithm: O({})'.format(self.complexity))
print('Memory of algorithm: O({})'.format(self.memory))

if self.notation_format == NotationFormat.BIG_OMEGA:
print('Сomplexity of algorithm: Ω({})'.format(self.complexity))
print('Memory of algorithm: Ω({})'.format(self.memory))

if self.notation_format == NotationFormat.BIG_THETA:
print('Сomplexity of algorithm: Θ({})'.format(self.complexity))
print('Memory of algorithm: Θ({})'.format(self.memory))

print()

52 changes: 52 additions & 0 deletions pycomplexity/formatters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import shutil
from typing import List, Tuple
from .enums import NotationFormat

'''
Class that contains and represents analysis data
'''
class AnalysisFormatter:

def __init__(
self,
notation_format: NotationFormat,
func_name: str = 'No name',
func_attrs: List[Tuple[str, str]] | None = None,
complexity: str = '1',
memory: str = '1',
) -> None:

self.notation_format = notation_format
self.func_name = func_name
self.func_attrs = func_attrs
self.complexity = complexity
self.memory = memory

def report(self, full=True):

print()

if full:
print('Function name:', self.func_name)

if self.func_attrs:
print('Function attributes:', ', '.join([f'{attr_name}: {attr_type}' for attr_name, attr_type in self.func_attrs]))

terminal_width = shutil.get_terminal_size().columns
indent = max((terminal_width - len(self.notation_format.__str__()) - 2)//2, 0)
print('-' * indent + f' {self.notation_format.__str__()} ' + '-' * indent)

if self.notation_format == NotationFormat.BIG_O:
print('Сomplexity of algorithm: O({})'.format(self.complexity))
print('Memory of algorithm: O({})'.format(self.memory))

if self.notation_format == NotationFormat.BIG_OMEGA:
print('Сomplexity of algorithm: Ω({})'.format(self.complexity))
print('Memory of algorithm: Ω({})'.format(self.memory))

if self.notation_format == NotationFormat.BIG_THETA:
print('Сomplexity of algorithm: Θ({})'.format(self.complexity))
print('Memory of algorithm: Θ({})'.format(self.memory))

print()

3 changes: 2 additions & 1 deletion pycomplexity/notations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import inspect
from typing import List, Tuple

from .analysis import Analyzer, AnalysisFormatter
from .analysis import Analyzer
from .formatters import AnalysisFormatter
from .enums import NotationFormat

'''
Expand Down

0 comments on commit 7ce19aa

Please sign in to comment.