def sort_prioritized_configs(backend_configs, master_config): resolved_backend_configs = tuple( ( backend_name, resolve_config(backend_configs.get_config(backend_name), master_config), ) for backend_name in backend_configs ) backends_with_conflicting_priorities = tuple(( backend_name for backend_name, count in collections.Counter(( (backend_name, config['priority']) for backend_name, config in resolved_backend_configs )).items() if count > 1 )) if backends_with_conflicting_priorities: raise ValueError( "The following package backends have conflicting priority " "values. '{0}'. Ensure that all priority values are unique " "across all backends.".format( ', '.join((backends_with_conflicting_priorities)) ) ) return sorted( resolved_backend_configs, key=compose(*( operator.itemgetter(1), operator.itemgetter('priority'), )), )
def apply_formatters_to_return(*formatters): formatter = compose(*formatters) def outer(fn): @functools.wraps(fn) def inner(*args, **kwargs): value = fn(*args, **kwargs) return formatter(value) return inner return outer
def upgrade_config(config, to_version=LATEST_VERSION): try: current_version = config['version'] except KeyError: raise KeyError("No version key found in config file:\n\n{0}".format( pprint.pformat(config), )) upgrade_sequence = get_upgrade_sequence(current_version, to_version) upgrade_fn = compose(*(UPGRADE_FUNCTIONS[version] for version in upgrade_sequence)) upgraded_config = upgrade_fn(config) return upgraded_config
def link_bytecode(bytecode, link_reference_values): """ Given the bytecode for a contract, and it's dependencies in the form of {contract_name: address} this functino returns the bytecode with all of the link references replaced with the dependency addresses. """ linker_fn = compose(*(functools.partial( insert_link_value, value=value, offset=link_reference.offset, ) for link_reference, value in link_reference_values)) linked_bytecode = linker_fn(bytecode) return linked_bytecode
def pop_nested_key(config, key): key_head, _, key_tail = key.rpartition('.') head_getters = ( operator.itemgetter(key_part) for key_part in key_head.split('.') if key_part ) tail_popper = operator.methodcaller('pop', key_tail) popper_fn = compose(*itertools.chain(head_getters, (tail_popper,))) return popper_fn(config)
def set_nested_key(config, key, value): key_head, _, key_tail = key.rpartition('.') head_setters = ( operator.methodcaller('setdefault', key_part, {}) for key_part in key_head.split('.') if key_part ) tail_setter = operator.methodcaller('__setitem__', key_tail, value) setter_fn = compose(*itertools.chain(head_setters, (tail_setter,))) # must write to both the config_for_read and config_for_write return setter_fn(config)
def _find_matching_event_abi(cls, event_name=None, argument_names=None): filters = [ functools.partial(filter_by_type, 'event'), ] if event_name is not None: filters.append(functools.partial(filter_by_name, event_name)) if argument_names is not None: filters.append( functools.partial(filter_by_argument_name, argument_names)) filter_fn = compose(*filters) event_abi_candidates = filter_fn(cls.abi) if len(event_abi_candidates) == 1: return event_abi_candidates[0] elif not event_abi_candidates: raise ValueError("No matching functions found") else: raise ValueError("Multiple functions found")
def _find_matching_event_abi(cls, event_name=None, argument_names=None): filters = [ functools.partial(filter_by_type, 'event'), ] if event_name is not None: filters.append(functools.partial(filter_by_name, event_name)) if argument_names is not None: filters.append( functools.partial(filter_by_argument_name, argument_names) ) filter_fn = compose(*filters) event_abi_candidates = filter_fn(cls.abi) if len(event_abi_candidates) == 1: return event_abi_candidates[0] elif not event_abi_candidates: raise ValueError("No matching functions found") else: raise ValueError("Multiple functions found")
def get_nested_key(config, key): key_head, _, key_tail = key.rpartition('.') head_getters = ( operator.itemgetter(key_part) for key_part in key_head.split('.') if key_part ) tail_getter = operator.itemgetter(key_tail) getter_fn = compose(*itertools.chain(head_getters, (tail_getter,))) try: return getter_fn(config) except TypeError as err: raise KeyError( "Error getting nested key {0} from {1}: {2}".format( key, force_text(repr(config)), str(err), ) )
def apply_to_array(formatter_fn): return compose( functools.partial(map, formatter_fn), list, )
Constructs a decorator that will cause the underlying function to only be applied to the given value if the `test_fn` returns true for that value. """ def outer_fn(fn): @functools.wraps(fn) def inner(value): if test_fn(value): return fn(value) return value return inner return outer_fn apply_if_not_null = apply_if_passes_test(compose(is_null, operator.not_)) apply_if_string = apply_if_passes_test(is_string) apply_if_array = apply_if_passes_test(is_list_like) apply_if_dict = apply_if_passes_test(is_dict) apply_if_integer = apply_if_passes_test(is_integer) def apply_to_array(formatter_fn): return compose( functools.partial(map, formatter_fn), list, ) @coerce_args_to_text @coerce_return_to_text
class Web3(object): # Providers HTTPProvider = HTTPProvider RPCProvider = RPCProvider KeepAliveRPCProvider = KeepAliveRPCProvider IPCProvider = IPCProvider TestRPCProvider = TestRPCProvider EthereumTesterProvider = EthereumTesterProvider # Managers RequestManager = RequestManager DelegatedSigningManager = DelegatedSigningManager PrivateKeySigningManager = PrivateKeySigningManager # Iban Iban = Iban # Encoding and Decoding toHex = staticmethod(to_hex) toAscii = staticmethod(decode_hex) toUtf8 = staticmethod(compose(decode_hex, force_text)) fromAscii = staticmethod(encode_hex) fromUtf8 = staticmethod(encode_hex) toDecimal = staticmethod(to_decimal) fromDecimal = staticmethod(from_decimal) # Currency Utility toWei = staticmethod(to_wei) fromWei = staticmethod(from_wei) # Address Utility isAddress = staticmethod(is_address) isChecksumAddress = staticmethod(is_checksum_address) toChecksumAddress = staticmethod(to_checksum_address) def __init__(self, provider): self._requestManager = RequestManager(provider) self.eth = Eth(self) self.db = Db(self) self.shh = Shh(self) self.net = Net(self) self.personal = Personal(self) self.version = Version(self) self.txpool = TxPool(self) self.miner = Miner(self) self.admin = Admin(self) self.testing = Testing(self) def setProvider(self, provider): self._requestManager.setProvider(provider) def setManager(self, manager): self._requestManager = manager @property def currentProvider(self): return self._requestManager.provider @coerce_return_to_text def sha3(self, value, encoding="hex"): if encoding == 'hex': hex_string = value else: hex_string = encode_hex(value) return self._requestManager.request_blocking('web3_sha3', [hex_string]) def isConnected(self): return self.currentProvider is not None and self.currentProvider.isConnected() def createBatch(self): raise NotImplementedError("Not Implemented") def receive(self, requestid, timeout=0, keep=False): return self._requestManager.receive(requestid, timeout, keep)
def apply_if_passes_test(test_fn): """ Constructs a decorator that will cause the underlying function to only be applied to the given value if the `test_fn` returns true for that value. """ def outer_fn(fn): @functools.wraps(fn) def inner(value): if test_fn(value): return fn(value) return value return inner return outer_fn apply_if_not_null = apply_if_passes_test(compose(is_null, operator.not_)) apply_if_string = apply_if_passes_test(is_string) apply_if_array = apply_if_passes_test(is_list_like) apply_if_dict = apply_if_passes_test(is_dict) apply_if_integer = apply_if_passes_test(is_integer) def apply_to_array(formatter_fn): return compose( functools.partial(map, formatter_fn), list, ) @coerce_args_to_text @coerce_return_to_text