def test_estimate_block_number_for_period(): timestamp = maya.now().epoch period = timestamp // SECONDS_PER_PERIOD three_periods_back = period - 3 ten_periods_back = period - 10 latest_block_number = BlockNumber(12345678) now = maya.now() now_epoch = now.epoch # ensure the same time is used in method and in test with patch.object(maya, 'now', return_value=maya.MayaDT(epoch=now_epoch)): block_number_for_three_periods_back = estimate_block_number_for_period(period=three_periods_back, seconds_per_period=SECONDS_PER_PERIOD, latest_block=latest_block_number) block_number_for_ten_periods_back = estimate_block_number_for_period(period=ten_periods_back, seconds_per_period=SECONDS_PER_PERIOD, latest_block=latest_block_number) for past_period, block_number_for_past_period in ((three_periods_back, block_number_for_three_periods_back), (ten_periods_back, block_number_for_ten_periods_back)): start_of_past_period = maya.MayaDT(epoch=(past_period * SECONDS_PER_PERIOD)) diff_in_seconds = int((now - start_of_past_period).total_seconds()) diff_in_blocks = diff_in_seconds // AVERAGE_BLOCK_TIME_IN_SECONDS assert block_number_for_past_period < latest_block_number assert block_number_for_past_period == (latest_block_number - diff_in_blocks)
async def demo(hostname: str, show_config=False): async with IPFabricClient() as ipf: rec, config_text = await fetch_most_recent_config(ipf, hostname) change_dt = maya.MayaDT(rec["lastChange"] / 1000) check_dt = maya.MayaDT(rec["lastCheck"] / 1000) print("Last Changed:", change_dt.slang_date(), ",", change_dt.slang_time()) print("Last Checked", check_dt.slang_date(), ",", check_dt.slang_time()) if show_config: print(config_text) print(rec)
def process_description(process): # TODO status = process['statename'] if status == 'FATAL': return process['description'] elif process['running']: start = maya.MayaDT(process['start']) desc = 'pid {pid}, started {start} ({delta})' \ .format(pid=process['pid'], start=start.rfc2822(), delta=start.slang_time()) else: stop = maya.MayaDT(process['stop']) desc = 'stopped on {stop} ({delta} ago)' \ .format(stop=stop.rfc2822(), delta=stop.slang_time()) return desc
def batch_from_bytes( cls, ursulas_as_bytes: Iterable[bytes], federated_only: bool = False, ) -> List['Ursula']: # TODO: Make a better splitter for this. This is a workaround until bytestringSplitter #8 is closed. stranger_ursulas = [] ursulas_attrs = cls._internal_splitter.repeat(ursulas_as_bytes) for (timestamp, signature, identity_evidence, verifying_key, encrypting_key, public_address, certificate_vbytes, rest_info) in ursulas_attrs: certificate = load_pem_x509_certificate( certificate_vbytes.message_as_bytes, default_backend()) timestamp = maya.MayaDT(timestamp) stranger_ursula_from_public_keys = cls.from_public_keys( { SigningPower: verifying_key, EncryptingPower: encrypting_key, }, interface_signature=signature, timestamp=timestamp, checksum_address=to_checksum_address(public_address), certificate=certificate, rest_host=rest_info.host, rest_port=rest_info.port, federated_only=federated_only # TODO: 289 ) stranger_ursulas.append(stranger_ursula_from_public_keys) return stranger_ursulas
def from_bytes( cls, ursula_as_bytes: bytes, federated_only: bool = False, ) -> 'Ursula': (timestamp, signature, identity_evidence, verifying_key, encrypting_key, public_address, certificate_vbytes, rest_info) = cls._internal_splitter(ursula_as_bytes) certificate = load_pem_x509_certificate( certificate_vbytes.message_as_bytes, default_backend()) timestamp = maya.MayaDT(timestamp) stranger_ursula_from_public_keys = cls.from_public_keys( { SigningPower: verifying_key, EncryptingPower: encrypting_key }, timestamp=timestamp, interface_signature=signature, checksum_address=to_checksum_address(public_address), rest_host=rest_info.host, rest_port=rest_info.port, certificate=certificate, federated_only=federated_only, # TODO: 289 ) return stranger_ursula_from_public_keys
def _parse_transaction(self, tx: Dict, tx_type: TxType) -> Transaction: maya_dt = maya.MayaDT(float(tx['timestamp_created'])) timestamp = maya_dt.epoch datetime = maya_dt.datetime() status = tx.get('status') if status is not None: status_mapping = { 'CANCELED': TxStatus.CANCELED, 'ZEROCONFIRMED': TxStatus.FAILED, 'COMPLETED': TxStatus.OK, } status = status_mapping.get(status) fee = tx.get('fee') if fee is not None: fee = abs(Money(fee, self.currency)) return Transaction( id=tx.get('id'), type=tx_type, currency=self.currency, amount=Money(tx['amount'], self.currency), status=status, address=tx.get('address'), tx_hash=tx.get('txid'), fee=fee, timestamp=timestamp, datetime=datetime, info=tx, )
def write_events_to_csv_file(csv_file: str, agent: EthereumContractAgent, event_name: str, argument_filters: Dict = None, from_block: Optional[BlockIdentifier] = 0, to_block: Optional[BlockIdentifier] = 'latest') -> bool: """ Write events to csv file. :return: True if data written to file, False if there was no event data to write """ event_type = agent.contract.events[event_name] entries = event_type.getLogs(fromBlock=from_block, toBlock=to_block, argument_filters=argument_filters) if not entries: return False with open(csv_file, mode='w') as events_file: events_writer = None for event_record in entries: event_record = EventRecord(event_record) event_row = OrderedDict() event_row['event_name'] = event_name event_row['block_number'] = event_record.block_number event_row['unix_timestamp'] = event_record.timestamp event_row['date'] = maya.MayaDT(event_record.timestamp).iso8601() event_row.update(dict(event_record.args.items())) if events_writer is None: events_writer = csv.DictWriter(events_file, fieldnames=event_row.keys()) events_writer.writeheader() events_writer.writerow(event_row) return True
def _ticker(self) -> Ticker: ticker = self.client.ticker(self.market_id) maya_dt = maya.MayaDT(int(ticker['timestamp'])) currency = self.market.quote last = Money(ticker['last'], currency) bid = Money(ticker['bid'], currency) ask = Money(ticker['ask'], currency) return Ticker( market=self.market, bid=bid, ask=ask, mid=(bid + ask) / 2, last=last, open=Money(ticker['open'], currency), high=Money(ticker['high'], currency), low=Money(ticker['low'], currency), close=last, change=None, percentage=None, average=None, vwap=Money(ticker['vwap'], currency), info=ticker, timestamp=maya_dt.epoch, datetime=maya_dt.datetime(), )
def process_description(process): # TODO status = process["statename"] if status == "FATAL": return process["description"] elif process["running"]: start = maya.MayaDT(process["start"]) desc = "pid {pid}, started {start} ({delta})".format( pid=process["pid"], start=start.rfc2822(), delta=start.slang_time()) else: stop = maya.MayaDT(process["stop"]) desc = "stopped on {stop} ({delta} ago)".format( stop=stop.rfc2822(), delta=stop.slang_time()) return desc
def _ticker(self) -> Ticker: ticker = self.client.ticker(self.market_id) maya_dt = maya.MayaDT(int(ticker["timestamp"])) currency = self.market.quote last = Money(ticker["last"], currency) bid = Money(ticker["bid"], currency) ask = Money(ticker["ask"], currency) return Ticker( market=self.market, bid=bid, ask=ask, mid=(bid + ask) / 2, last=last, open=Money(ticker["open"], currency), high=Money(ticker["high"], currency), low=Money(ticker["low"], currency), close=last, change=None, percentage=None, average=None, vwap=Money(ticker["vwap"], currency), info=ticker, timestamp=maya_dt.epoch, datetime=maya_dt.datetime(), )
def _parse_trade(self, trade: Dict) -> Trade: trade_id = trade.get("id", trade.get("tid")) maya_dt = None if "date" in trade: maya_dt = maya.MayaDT(int(trade["date"])) elif "datetime" in trade: maya_dt = maya.when(trade["datetime"]) side = trade.get("type") if side: side = side_mapping[side] price = trade.get("price", trade.get(self.market.base.lower())) if price: price = Money(price, self.market.quote) amount = trade.get("amount", trade.get(self.market.code.lower())) if amount: amount = Money(amount, self.market.base) fee = trade.get("fee") if fee: fee = Money(fee, self.market.quote) cost = None if price and amount: cost = price * amount.amount return Trade( id=trade_id, market=self.market, type=None, side=side, amount=amount, price=price, cost=cost, fee=fee, info=trade, timestamp=maya_dt.epoch, datetime=maya_dt.datetime(), )
def test_read_timestamp(agent): timestamp = agent.end_timestamp end_locktime = maya.MayaDT(timestamp) assert end_locktime.slang_time() now = maya.now() assert now < end_locktime, '{} is not in the future!'.format( end_locktime.slang_date())
def _transform(self, value, then): if isinstance(value, maya.Datetime): value = maya.MayaDT(mktime(value.timetuple())) if isinstance(value, maya.MayaDT): value = value.iso8601() return then(str(value))
def decrypt(self, bob, item_cid, pol, sig, lab): policy_pubkey = UmbralPublicKey.from_bytes(bytes.fromhex(pol)) alices_sig_pubkey = UmbralPublicKey.from_bytes(bytes.fromhex(sig)) label = lab.encode() dat = self.ipfs_gateway_api.cat(item_cid) doctor = bob doctor.join_policy(label, alices_sig_pubkey) data = msgpack.loads(dat, raw=False) message_kits = (UmbralMessageKit.from_bytes(k) for k in data['kits']) data_source = Enrico.from_public_keys( {SigningPower: data['data_source']}, policy_encrypting_key=policy_pubkey ) message_kit = next(message_kits) start = timer() retrieved_plaintexts = doctor.retrieve( label=label, message_kit=message_kit, data_source=data_source, alice_verifying_key=alices_sig_pubkey ) end = timer() plaintext = msgpack.loads(retrieved_plaintexts[0], raw=False) heart_rate = plaintext['heart_rate'] timestamp = maya.MayaDT(plaintext['timestamp']) terminal_size = shutil.get_terminal_size().columns max_width = min(terminal_size, 120) columns = max_width - 12 - 27 scale = columns / 40 retrieval_time = "Retrieval time: {:8.2f} ms".format(1000 * (end - start)) line = heart_rate# + " " + retrieval_time return line
def decrypting_msg(data, policy_pubkey, label, arjuns_sig_pubkey, mayank): data = msgpack.loads(data, raw=False) print("afterjson", data) message_kits = (UmbralMessageKit.from_bytes(k) for k in data['kits']) # The mayank also needs to create a view of the Data Source from its public keys data_source = DataSource.from_public_keys( policy_public_key=policy_pubkey, datasource_public_key=data['data_source'], label=label ) # NuCypher network to get a re-encrypted version of each MessageKit. for message_kit in message_kits: try: start = timer() retrieved_plaintexts = mayank.retrieve( message_kit=message_kit, data_source=data_source, alice_verifying_key=arjuns_sig_pubkey ) end = timer() plaintext = msgpack.loads(retrieved_plaintexts[0], raw=False) msg = plaintext['msg'] name = plaintext['name'] timestamp = maya.MayaDT(plaintext['timestamp']) return (name+": "+msg+" ("+str(timestamp)+")") except Exception as e: traceback.print_exc()
def from_python_type(cls, value, strict: bool = True): evaluated_type = type(value) if evaluated_type is str: try: if cls.valid_year[0] < maya.parse(value).year < cls.valid_year[1]: return KustoType.DATETIME except: pass if not strict: try: json.loads(value) return KustoType.DYNAMIC except: pass if evaluated_type is int: # if not strict: try: length_in_plausible_epochtime = 8 <= len(str(evaluated_type // 1)) <= 11 if length_in_plausible_epochtime and cls.valid_year[0] < maya.MayaDT(value).year < cls.valid_year[1]: return KustoType.DATETIME except: pass if value > 2 ** 31 or value < -1 * (2 ** 31): return KustoType.LONG return KustoType.INT if evaluated_type is float: # TODO: need to handle decimal properly return KustoType.REAL if evaluated_type in [dict, list, tuple, set]: return KustoType.DYNAMIC return KustoType.STRING
def unpack_snapshot(data) -> Tuple[str, maya.MayaDT, bytes]: checksum_bytes, timestamp_bytes, remainder = FleetState.snapshot_splitter( data, return_remainder=True) checksum = checksum_bytes.hex() timestamp = maya.MayaDT( int.from_bytes(timestamp_bytes, byteorder="big")) return checksum, timestamp, remainder
def _parse_transaction(self, tx: Dict, tx_type: TxType) -> Transaction: maya_dt = maya.MayaDT(float(tx["timestamp_created"])) timestamp = maya_dt.epoch datetime = maya_dt.datetime() status = tx.get("status") if status is not None: status_mapping = { "CANCELED": TxStatus.CANCELED, "ZEROCONFIRMED": TxStatus.FAILED, "COMPLETED": TxStatus.OK, } status = status_mapping.get(status) fee = tx.get("fee") if fee is not None: fee = abs(Money(fee, self.currency)) return Transaction( id=tx.get("id"), type=tx_type, currency=self.currency, amount=Money(tx["amount"], self.currency), status=status, address=tx.get("address"), tx_hash=tx.get("txid"), fee=fee, timestamp=timestamp, datetime=datetime, info=tx, )
def decode_format_timestamp(timestamp): """Convert unix timestamp (millis) into date & time we use in logs output. :param timestamp: unix timestamp in millis :return: date, time in UTC """ dt = maya.MayaDT(timestamp / 1000).datetime(naive=True) return dt.strftime('%Y-%m-%d'), dt.strftime('%H:%M:%S')
def run(self, params={}): epoch = params.get(Input.EPOCH) if re.search("([0-9]){21,}", epoch): raise PluginException( cause="The given epoch is out of range.", assistance="This action supports seconds, milliseconds, microseconds, and nanoseconds. Please check " "that the given epoch is correct.", ) return {Output.DATE: maya.MayaDT(float(epoch) / self.seconds_division_number(epoch)).rfc3339()}
def __post_init__(self): if self.timestamp is None: maya_dt = maya.now() self.timestamp = maya_dt.epoch else: maya_dt = maya.MayaDT(self.timestamp) if self.datetime is None: self.datetime = maya_dt.datetime() self.iso8601 = maya_dt.iso8601()
def paint_bidding_notice(emitter, bidder): message = WORKLOCK_AGREEMENT.format( refund_rate=prettify_eth_amount( bidder.worklock_agent.get_bonus_refund_rate()), end_date=maya.MayaDT( bidder.economics.bidding_end_date).local_datetime(), bidder_address=bidder.checksum_address, duration=bidder.economics.worklock_commitment_duration) emitter.echo(message) return
def everything_after(self, start, return_type="epoch", field_name="timestamp"): # both start and stop should be datetime or a string start_maya = None start_timestamp = None if isinstance(start, str): start_maya = maya.when(start) start_timestamp = start_maya.datetime().timestamp() if isinstance(start, float): start_maya = maya.MayaDT(start) start_timestamp = start_maya.datetime().timestamp() if isinstance(start, dt): start_maya = maya.MayaDT(start) start_timestamp = start_maya.datetime().timestamp() return {'{}'.format(field_name):{'$gt': start_timestamp}}
def paint_staking_rewards(stakeholder, blockchain, emitter, past_periods, staking_address, staking_agent): if not past_periods: reward_amount = stakeholder.staker.calculate_staking_reward() emitter.echo(message=TOKEN_REWARD_CURRENT.format( reward_amount=round(reward_amount, TOKEN_DECIMAL_PLACE))) return economics = stakeholder.staker.economics seconds_per_period = economics.seconds_per_period current_period = staking_agent.get_current_period() from_period = current_period - past_periods latest_block = blockchain.client.block_number from_block = estimate_block_number_for_period( period=from_period, seconds_per_period=seconds_per_period, latest_block=latest_block) argument_filters = {'staker': staking_address} event_type = staking_agent.contract.events['Minted'] entries = event_type.getLogs(fromBlock=from_block, toBlock='latest', argument_filters=argument_filters) rows = [] rewards_total = NU(0, 'NU') for event_record in entries: event_block_number = int(event_record['blockNumber']) event_period = event_record['args']['period'] event_reward = NU(event_record['args']['value'], 'NuNit') timestamp = blockchain.client.get_block(event_block_number).timestamp event_date = maya.MayaDT( epoch=timestamp).local_datetime().strftime("%b %d %Y") rows.append([ event_date, event_block_number, int(event_period), round(event_reward, TOKEN_DECIMAL_PLACE), ]) rewards_total += event_reward if not rows: emitter.echo(TOKEN_REWARD_NOT_FOUND) return periods_as_days = economics.days_per_period * past_periods emitter.echo(message=TOKEN_REWARD_PAST_HEADER.format(periods=past_periods, days=periods_as_days)) emitter.echo( tabulate.tabulate(rows, headers=REWARDS_TABLE_COLUMNS, tablefmt="fancy_grid")) emitter.echo(message=TOKEN_REWARD_PAST.format( reward_amount=round(rewards_total, TOKEN_DECIMAL_PLACE)))
def check_bonding_requirements(emitter, agent: PREApplicationAgent, staking_provider: ChecksumAddress) -> None: blockchain = agent.blockchain now = blockchain.get_blocktime() commencement = agent.get_staking_provider_info( staking_provider=staking_provider).operator_start_timestamp min_seconds = agent.get_min_operator_seconds() termination = (commencement + min_seconds) if now < termination: emitter.error(BONDING_TIME.format(date=maya.MayaDT(termination))) raise click.Abort()
def get_iso_time_str(timestamp: Union[int, float, str, datetime]=None) -> str: """Get the ISO time string from a timestamp or date obj. Returns current time str if no timestamp is passed""" if isinstance(timestamp, (int, float)): maya_dt = maya.MayaDT(timestamp) elif isinstance(timestamp, str): maya_dt = maya.when(timestamp) elif timestamp is None: maya_dt = maya.now() else: raise ValueError(f'`{type(timestamp)}` is not supported') return maya_dt.iso8601()
def peak_back_far(self) -> float: """ # PEAK FAR --- Peak far into the future. One step head + one lookahead_params """ head = self.head step_params = self.stepsize_params lookahead_params = self.lookback_params new_head = (maya.MayaDT(head).subtract(**step_params).subtract( **lookahead_params)._epoch) return new_head
def from_bytes( cls, ursula_as_bytes: bytes, version: int = INCLUDED_IN_BYTESTRING, federated_only: bool = False, ) -> 'Ursula': if version is INCLUDED_IN_BYTESTRING: version, payload = cls.version_splitter(ursula_as_bytes, return_remainder=True) else: payload = ursula_as_bytes # Check version and raise IsFromTheFuture if this node is... you guessed it... if version > cls.LEARNER_VERSION: # TODO: Some auto-updater logic? try: canonical_address, _ = BytestringSplitter( PUBLIC_ADDRESS_LENGTH)(payload, return_remainder=True) checksum_address = to_checksum_address(canonical_address) nickname, _ = nickname_from_seed(checksum_address) display_name = "⇀{}↽ ({})".format(nickname, checksum_address) message = cls.unknown_version_message.format( display_name, version, cls.LEARNER_VERSION) except BytestringSplittingError: message = cls.really_unknown_version_message.format( version, cls.LEARNER_VERSION) raise cls.IsFromTheFuture(message) # Version stuff checked out. Moving on. node_info = cls.internal_splitter(payload) powers_and_material = { SigningPower: node_info.pop("verifying_key"), DecryptingPower: node_info.pop("encrypting_key") } interface_info = node_info.pop("rest_interface") node_info['rest_host'] = interface_info.host node_info['rest_port'] = interface_info.port node_info['timestamp'] = maya.MayaDT(node_info.pop("timestamp")) node_info['checksum_public_address'] = to_checksum_address( node_info.pop("public_address")) domains_vbytes = VariableLengthBytestring.dispense( node_info['domains']) node_info['domains'] = [constant_or_bytes(d) for d in domains_vbytes] ursula = cls.from_public_keys(powers_and_material, federated_only=federated_only, **node_info) return ursula
def __to_maya(date): mapping = { str: lambda date: maya.when(date), int: lambda date: maya.MayaDT(epoch=date), maya.MayaDT: lambda date: date, datetime: lambda date: maya.MayaDT.from_datetime(date) } try: return mapping[type(date)](date) except KeyError: return maya.now()
def __init__(self, path): html_soup = utils.html_open(path, 'soup') try: self.raw_meta = html_soup.find_all('code', class_='meta')[0].get_text() if config['Config']['Hide_meta']: html_soup.find_all('code', class_='meta')[0].parent.decompose() except IndexError: #no meta data found print( utils.print_style(' **No raw meta found in ' + path, 'yellow', 'bold')) self.raw_meta = '' meta = yaml.safe_load(self.raw_meta) if meta is None: meta = {} if 'Title' in meta: self.title = meta['Title'] else: try: self.title = html_soup.h1.get_text() meta['Title'] = self.title html_soup.h1.decompose() except AttributeError: self.title = 'Untitled' meta['Title'] = self.title if 'Author' in meta: self.author = meta['Author'] else: self.author = config['Site']['Author'] meta['Author'] = self.author if 'Date' in meta: self.maya = maya.parse(meta['Date']) else: self.maya = maya.MayaDT(utils.get_time(path, 'modify')) self.date_epoch = self.maya.epoch #for data comparason self.date_human = self.maya.datetime().strftime( config['Site']['Time_style']) meta['Date'] = self.date_human if 'Category' in meta: self.category = meta['Category'].capitalize() else: self.category = 'Default' meta['Category'] = self.category if 'Tag' in meta: meta['Tag'].sort(key=str.lower) for i in range(len(meta['Tag'])): meta['Tag'][i] = meta['Tag'][i].capitalize() self.tag = meta['Tag'] else: self.tag = [] meta['Tag'] = self.tag self.dict = meta self.body = html_soup.body