1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00
maloja/maloja/jinjaenv/filters.py
Brian Pepple d1b598a32b
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 14:58:24 +02:00

53 lines
1.4 KiB
Python

def fixlength(real,target):
t = real[:target]
while len(t)<target: t.append(None)
return t
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]
except:
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
def combine_dicts(dictlist):
return {k:d[k] for d in dictlist for k in d}
def compare_key_in_dicts(key,d1,d2):
return d1[key] == d2[key]
def alltrue(seq):
return all(seq)