Skip to content

Commit

Permalink
Merge pull request #6 from SatelliteQE/cached_variations
Browse files Browse the repository at this point in the history
Cached Variations in Variations Module
  • Loading branch information
jyejare authored Oct 15, 2023
2 parents 63f3dbc + 028df36 commit d76a5e9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 37 deletions.
45 changes: 8 additions & 37 deletions candore/modules/comparator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import json
import yaml
from pathlib import Path
from candore.config import settings, CURRENT_DIRECTORY
from functools import cached_property, lru_cache
from candore.utils import last_index_of_element
from candore.modules.variatons import Variations


class Comparator:
Expand All @@ -11,42 +9,17 @@ def __init__(self):
self.big_key = []
self.big_compare = {}
self.record_evs = False

@cached_property
def variations(self):
templates_path = Path(CURRENT_DIRECTORY, settings.candore.var_file)
if not templates_path.exists():
print(f"The file {templates_path} does not exist.")
with templates_path.open() as yaml_file:
yaml_data = yaml.safe_load(yaml_file)
return yaml_data

def get_paths(self, variations, prefix='', separator='/'):
paths = []
if isinstance(variations, dict):
for key, value in variations.items():
paths.extend(self.get_paths(value, f"{prefix}{key}{separator}"))
elif isinstance(variations, list):
for item in variations:
paths.extend(self.get_paths(item, prefix, separator))
else:
paths.append(f'{prefix}{variations}')

return paths
self.variations = Variations()
self.expected_variations = self.variations.expected_variations
self.skipped_variations = self.variations.skipped_variations

def remove_non_variant_key(self, key):
reversed_bk = self.big_key[::-1]
reversed_bk.remove(key)
self.big_key = reversed_bk[::-1]

def last_index_of_element(self, arr, element):
for i in range(len(arr) - 1, -1, -1):
if arr[i] == element:
return i
return -1

def remove_path(self, identy):
id_index = self.last_index_of_element(self.big_key, identy)
id_index = last_index_of_element(self.big_key, identy)
if id_index == -1:
return
self.big_key = self.big_key[:id_index]
Expand All @@ -55,13 +28,11 @@ def record_variation(self, pre, post, var_details=None):
big_key = [str(itm) for itm in self.big_key]
full_path = '/'.join(big_key)
var_full_path = '/'.join([itm for itm in self.big_key if not isinstance(itm, int)])
expected_variations = self.get_paths(variations=self.variations.get('expected_variations'))
skipped_variations = self.get_paths(variations=self.variations.get('skipped_variations'))
if var_full_path in expected_variations or var_full_path in skipped_variations:
if var_full_path in self.expected_variations or var_full_path in self.skipped_variations:
if self.record_evs:
variation = {'pre': pre, 'post': post, 'variation': var_details or 'Expected(A)'}
self.big_compare.update({full_path: variation})
elif var_full_path not in expected_variations and var_full_path not in skipped_variations:
elif var_full_path not in self.expected_variations and var_full_path not in self.skipped_variations:
variation = {'pre': pre, 'post': post, 'variation': var_details or ''}
self.big_compare.update({full_path: variation})

Expand Down
40 changes: 40 additions & 0 deletions candore/modules/variatons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
A module responsible for calculating expected and skipped variations from
`conf/variations` yaml file and convert them into processable list
"""
import yaml
from pathlib import Path
from candore.config import settings, CURRENT_DIRECTORY
from functools import cached_property


class Variations:
def get_paths(self, variations, prefix='', separator='/'):
paths = []
if isinstance(variations, dict):
for key, value in variations.items():
paths.extend(self.get_paths(value, f"{prefix}{key}{separator}"))
elif isinstance(variations, list):
for item in variations:
paths.extend(self.get_paths(item, prefix, separator))
else:
paths.append(f'{prefix}{variations}')

return paths

@cached_property
def variations(self):
templates_path = Path(CURRENT_DIRECTORY, settings.candore.var_file)
if not templates_path.exists():
print(f"The file {templates_path} does not exist.")
with templates_path.open() as yaml_file:
yaml_data = yaml.safe_load(yaml_file)
return yaml_data

@cached_property
def expected_variations(self):
return self.get_paths(variations=self.variations.get('expected_variations'))

@cached_property
def skipped_variations(self):
return self.get_paths(variations=self.variations.get('skipped_variations'))
10 changes: 10 additions & 0 deletions candore/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
An utility helpers module
"""


def last_index_of_element(arr, element):
for i in range(len(arr) - 1, -1, -1):
if arr[i] == element:
return i
return -1

0 comments on commit d76a5e9

Please sign in to comment.