Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print('Change log:')
pprint(hivemind_state.change_log(max_depth=0))
print('Test initial options is an empty list')
assert hivemind_state.options == []
print('\n\n###############################')
print('#Hivemind option #')
print('###############################')
option_hashes = {}
option_values = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven']
for i, option_value in enumerate(option_values):
print('\nadding option %s: %s' % (i+1, option_value))
option = HivemindOption()
option.set_hivemind_issue(hivemind_issue_hash=hivemind_issue_hash)
option.answer_type = answer_type
option.set(value=option_value)
option_hashes[option_value] = option.save()
print('saved with ipfs hash %s' % option.multihash())
address = get_address_from_wallet(account=0, index=0)
message = '/ipfs/%s' % option.multihash()
private_key = get_private_key_from_wallet(account=0, index=0)[address]
signature = sign_message(message=message, private_key=private_key)
try:
# note there is a restriction on the maximum number of options per address: 10
hivemind_state.add_option(option_hash=option.multihash(), address=address, signature=signature)
assert hivemind_state.options[i] == option.multihash()
print('')
print('')
hivemind_hash = hivemind_issue.save()
print('Hivemind hash:', hivemind_hash)
hivemind_state = HivemindState()
hivemind_state.set_hivemind_issue(issue_hash=hivemind_hash)
assert hivemind_state.options == []
option_hashes = {}
option_values = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten']
for option_value in option_values:
print('adding option %s' % option_value)
option = HivemindOption()
option.set_hivemind_issue(hivemind_issue_hash=hivemind_hash)
option.answer_type = option_type
option.set(value=option_value)
option_hashes[option_value] = option.save()
print('saved with ipfs hash %s' % option.multihash())
hivemind_state.add_option(option_hash=option.multihash())
print('')
print('All options:')
print(hivemind_state.options)
assert len(hivemind_state.options) == len(option_values)
print('')
hivemind_state_hash = hivemind_state.save()
print('Hivemind state hash:', hivemind_state_hash)
def test_initializing_with_option_hash(self):
option = HivemindOption()
print(option.__dict__)
option.set_hivemind_issue(hivemind_issue_hash=STRING_QUESTION_HASH)
option.set('42')
print(option.__dict__)
option_hash = option.save()
print(option_hash)
option2 = HivemindOption(multihash=option_hash)
assert option2.hivemind_issue_hash == option.hivemind_issue_hash
assert option2.value == option.value
assert option2.answer_type == option.answer_type
def test_initializing_with_option_hash(self):
option = HivemindOption()
print(option.__dict__)
option.set_hivemind_issue(hivemind_issue_hash=STRING_QUESTION_HASH)
option.set('42')
print(option.__dict__)
option_hash = option.save()
print(option_hash)
option2 = HivemindOption(multihash=option_hash)
assert option2.hivemind_issue_hash == option.hivemind_issue_hash
assert option2.value == option.value
assert option2.answer_type == option.answer_type
def test_is_valid_integer_option(self, value, expected):
option = HivemindOption()
option.set_hivemind_issue(hivemind_issue_hash=INTEGER_QUESTION_HASH)
option.value = value
assert option.is_valid_integer_option() is expected
print('\nAuto_completing using opinion as MAX value')
opinion.auto_complete = 'MAX'
for option_hash in opinion.ranking():
print(HivemindOption(multihash=option_hash).value, option_hash)
assert opinion.ranking() == [INTEGER_OPTION5_HASH, INTEGER_OPTION2_HASH, INTEGER_OPTION3_HASH]
print('\nAuto_completing using opinion as MIN value')
opinion.auto_complete = 'MIN'
for option_hash in opinion.ranking():
print(HivemindOption(multihash=option_hash).value, option_hash)
assert opinion.ranking() == [INTEGER_OPTION3_HASH, INTEGER_OPTION4_HASH, INTEGER_OPTION1_HASH]
print('\nAuto_completing using opinion as CLOSEST value if 2 other options are equally close, the order of adding them to the hivemind is used')
opinion.auto_complete = 'CLOSEST'
for option_hash in opinion.ranking():
print(HivemindOption(multihash=option_hash).value, option_hash)
assert opinion.ranking() == [INTEGER_OPTION3_HASH, INTEGER_OPTION2_HASH, INTEGER_OPTION4_HASH, INTEGER_OPTION1_HASH, INTEGER_OPTION5_HASH]
print('\nAuto_completing using opinion as CLOSEST value with preference for higher values if equally close')
opinion.auto_complete = 'CLOSEST_HIGH'
for option_hash in opinion.ranking():
print(HivemindOption(multihash=option_hash).value, option_hash)
assert opinion.ranking() == [INTEGER_OPTION3_HASH, INTEGER_OPTION4_HASH, INTEGER_OPTION2_HASH, INTEGER_OPTION1_HASH, INTEGER_OPTION5_HASH]
print('\nAuto_completing using opinion as CLOSEST value with preference for lower values if equally close')
opinion.auto_complete = 'CLOSEST_LOW'
for option_hash in opinion.ranking():
print(HivemindOption(multihash=option_hash).value, option_hash)
assert opinion.ranking() == [INTEGER_OPTION3_HASH, INTEGER_OPTION2_HASH, INTEGER_OPTION4_HASH, INTEGER_OPTION5_HASH, INTEGER_OPTION1_HASH]
def test_is_valid_float_option(self, value, expected):
option = HivemindOption()
option.set_hivemind_issue(hivemind_issue_hash=FLOAT_QUESTION_HASH)
option.value = value
assert option.is_valid_float_option() is expected
def test_initialization(self):
option = HivemindOption()
assert isinstance(option, HivemindOption)
def options_info(self):
"""
Get detailed information about the options as a formatted string
:return: A string containing all information about the options
"""
ret = "Options"
ret += "\n======="
for i, option_hash in enumerate(self.options):
ret += '\nOption %s:' % (i + 1)
option = HivemindOption(multihash=option_hash)
ret += '\n' + option.info()
ret += '\n'
return ret
def get_options(self, question_index=0):
"""
Get the list of Options as sorted by the hivemind
:return: A list of Option objects sorted by highest score
"""
return [HivemindOption(multihash=option[0]) for option in sorted(self.results[question_index].items(), key=lambda x: x[1]['score'], reverse=True)]