mirror of
https://github.com/nexus-stc/hyperboria
synced 2024-12-04 00:42:53 +01:00
43be16e4bc
- [nexus] Remove outdated protos - [nexus] Development - [nexus] Development - [nexus] Development - [nexus] Development - [nexus] Development - [nexus] Refactor views - [nexus] Update aiosumma - [nexus] Add tags - [nexus] Development - [nexus] Update repository - [nexus] Update repository - [nexus] Update dependencies - [nexus] Update dependencies - [nexus] Fixes for MetaAPI - [nexus] Support for new queries - [nexus] Adopt new versions of search - [nexus] Improving Nexus - [nexus] Various fixes - [nexus] Add profile - [nexus] Fixes for ingestion - [nexus] Refactorings and bugfixes - [idm] Add profile methods - [nexus] Fix stalled nexus-meta bugs - [nexus] Various bugfixes - [nexus] Restore IDM API functionality GitOrigin-RevId: a0842345a6dde5b321279ab5510a50c0def0e71a
29 lines
1.2 KiB
Python
29 lines
1.2 KiB
Python
import random
|
|
|
|
|
|
class Promotioner:
|
|
"""
|
|
Promotioner is used to select promotion randomly based on weights of every promotion.
|
|
"""
|
|
def __init__(self, promotions: list[dict], default_promotion_index: int = 0):
|
|
self.promotions = promotions
|
|
self.default_promotion_index = default_promotion_index
|
|
self.partial_sums: list = [self.promotions[0]['weight']]
|
|
for promotion in self.promotions[1:]:
|
|
self.partial_sums.append(promotion['weight'] + self.partial_sums[-1])
|
|
|
|
def choose_promotion(self, language: str = 'en') -> str:
|
|
pivot = random.randrange(self.partial_sums[-1])
|
|
for partial_sum, promotion in zip(self.partial_sums, self.promotions):
|
|
if partial_sum <= pivot:
|
|
continue
|
|
if language in promotion['texts']:
|
|
return promotion['texts'][language]
|
|
elif promotion.get('local', False):
|
|
default_promotion = self.promotions[self.default_promotion_index]
|
|
if language in default_promotion['texts']:
|
|
return default_promotion['texts'][language]
|
|
return default_promotion['texts']['en']
|
|
else:
|
|
return promotion['texts']['en']
|