maloja/maloja/jinjaenv/filters.py

53 lines
1.4 KiB
Python
Raw Normal View History

2020-08-27 18:26:56 +03:00
def fixlength(real,target):
t = real[:target]
while len(t)<target: t.append(None)
return t
2020-08-27 23:26:51 +03:00
def find_representative(sequence,attribute_id,attribute_count):
try:
newsequence = [e for e in sequence if e is not None and e[attribute_id] is not None]
for element in newsequence:
element["_j_appears"] = [e[attribute_id] for e in newsequence].count(element[attribute_id])
newsequence = [e for e in newsequence if e["_j_appears"] == max(el["_j_appears"] for el in newsequence)]
newsequence = [e for e in newsequence if e[attribute_count] == max(el[attribute_count] for el in newsequence)]
return newsequence[0]
2022-04-24 21:47:17 +03:00
except Exception:
2020-08-27 23:26:51 +03:00
return None
finally:
for e in newsequence:
del e["_j_appears"]
# groups = []
# for element in sequence:
# for grouprep,groupentries in groups:
# if grouprep == element[attribute_id]:
# groupentries.append(element)
# break
# else:
# groups.append((element[attribute_id],[element]))
#
# groups.sort(key=lambda x:len(x[1]),reverse=True)
#
# # now group this grouping by number of appearances
#
# import itertools
# byappearances = itertools.groupby(groups,key=lambda x:len(x[1]))
# mostappearances = list(next(byappearances)[1])
#
# return mostappearances
# # among those, pick the one with the highest count in one of their appearances
2020-08-31 00:49:14 +03:00
def combine_dicts(dictlist):
Refactoring (#83) * Merge isinstance calls * Inline variable that is immediately returned * Replace set() with comprehension * Replace assignment with augmented assignment * Remove unnecessary else after guard condition * Convert for loop into list comprehension * Replace unused for index with underscore * Merge nested if conditions * Convert for loop into list comprehension * Convert for loop into set comprehension * Remove unnecessary else after guard condition * Replace if statements with if expressions * Simplify sequence comparison * Replace multiple comparisons with in operator * Merge isinstance calls * Merge nested if conditions * Add guard clause * Merge duplicate blocks in conditional * Replace unneeded comprehension with generator * Inline variable that is immediately returned * Remove unused imports * Replace unneeded comprehension with generator * Remove unused imports * Remove unused import * Inline variable that is immediately returned * Swap if/else branches and remove unnecessary else * Use str.join() instead of for loop * Multiple refactors - Remove redundant pass statement - Hoist repeated code outside conditional statement - Swap if/else to remove empty if body * Inline variable that is immediately returned * Simplify generator expression * Replace if statement with if expression * Multiple refactoring - Replace range(0, x) with range(x) - Swap if/else branches - Remove unnecessary else after guard condition * Use str.join() instead of for loop * Hoist repeated code outside conditional statement * Use str.join() instead of for loop * Inline variables that are immediately returned * Merge dictionary assignment with declaration * Use items() to directly unpack dictionary values * Extract dup code from methods into a new one
2021-10-19 15:58:24 +03:00
return {k:d[k] for d in dictlist for k in d}
2020-08-31 00:49:14 +03:00
def compare_key_in_dicts(key,d1,d2):
return d1[key] == d2[key]
def alltrue(seq):
Refactoring (#83) * Merge isinstance calls * Inline variable that is immediately returned * Replace set() with comprehension * Replace assignment with augmented assignment * Remove unnecessary else after guard condition * Convert for loop into list comprehension * Replace unused for index with underscore * Merge nested if conditions * Convert for loop into list comprehension * Convert for loop into set comprehension * Remove unnecessary else after guard condition * Replace if statements with if expressions * Simplify sequence comparison * Replace multiple comparisons with in operator * Merge isinstance calls * Merge nested if conditions * Add guard clause * Merge duplicate blocks in conditional * Replace unneeded comprehension with generator * Inline variable that is immediately returned * Remove unused imports * Replace unneeded comprehension with generator * Remove unused imports * Remove unused import * Inline variable that is immediately returned * Swap if/else branches and remove unnecessary else * Use str.join() instead of for loop * Multiple refactors - Remove redundant pass statement - Hoist repeated code outside conditional statement - Swap if/else to remove empty if body * Inline variable that is immediately returned * Simplify generator expression * Replace if statement with if expression * Multiple refactoring - Replace range(0, x) with range(x) - Swap if/else branches - Remove unnecessary else after guard condition * Use str.join() instead of for loop * Hoist repeated code outside conditional statement * Use str.join() instead of for loop * Inline variables that are immediately returned * Merge dictionary assignment with declaration * Use items() to directly unpack dictionary values * Extract dup code from methods into a new one
2021-10-19 15:58:24 +03:00
return all(seq)