def with_bcc(self, *args): if args and isinstance(args[0], str): return self.with_bcc_( [MimeEmailAddress(args[0], nth(args, 1, None))]) else: return self.with_bcc_( [MimeEmailAddress(t[0], nth(t, 1, None)) for t in args])
def pairwise_diffs(self, from_cur, to_cur, snapshot=None) -> Tuple[Dict, Dict]: """Returns pairwise absolute and % differences between exchanges for a currency pair. Values represent profit from buying on the row, selling on the column.""" snapshot = snapshot or self.copy() # Prevent changes midway. absdiffs = {} pctdiffs = {} for exchange1 in snapshot.keys(): for exchange2 in snapshot.keys(): if absdiffs.get(exchange1, {}).get(exchange2, {}): continue e1pair = RateTable._synget(snapshot, exchange1, from_cur, to_cur) e2pair = RateTable._synget(snapshot, exchange2, from_cur, to_cur) if not e1pair or not e2pair: continue buyone = e2pair[0][0] - e1pair[0][0] if exchange1 not in absdiffs: absdiffs[exchange1] = {} pctdiffs[exchange1] = {} if exchange2 not in absdiffs: absdiffs[exchange2] = {} pctdiffs[exchange2] = {} absdiffs[exchange1][exchange2] = buyone pctdiffs[exchange1][exchange2] = buyone / e1pair if exchange1 in absdiffs: absdiffs[exchange1] = OrderedDict( sorted(absdiffs[exchange1].items(), key=lambda x: x[1] or float("-inf"), reverse=True)) pctdiffs[exchange1] = OrderedDict( sorted(pctdiffs[exchange1].items(), key=lambda x: x[1] or float("-inf"), reverse=True)) absdiffs = OrderedDict( sorted(absdiffs.items(), key=lambda x: nth(x[1].values(), 1, None) or float("-inf"), reverse=True)) pctdiffs = OrderedDict( sorted(pctdiffs.items(), key=lambda x: nth(x[1].values(), 1, None) or float("-inf"), reverse=True)) return absdiffs, pctdiffs
def main(): input_dir = mit.nth(sys.argv, 1, 'test_data') output_dir = mit.nth(sys.argv, 2, 'out/test_data') planning = Planning(input_dir, output_dir) planning.import_example_data() planning.setup_solver() planning.print_solver() planning.solve() planning.output_result() planning.export_results()
def main(): # starting_numbers = [0, 3, 6] start_numbers = [5, 2, 8, 16, 18, 0, 1] # Part 1 target_pos = 2020 with timer(message_prefix="timer: part 1 "): p1_answer = more_itertools.nth(speak_numbers(start_numbers), n=target_pos - 1) print(p1_answer) # Part 2 target_pos = 30_000_000 with timer(message_prefix="timer: part 2 "): p2_answer = more_itertools.nth(speak_numbers(start_numbers), n=target_pos - 1) print(p2_answer)
def test_long(self): iterable = tuple(range(180)) r = 4 index = 1000000 actual = mi.nth_permutation(iterable, r, index) expected = mi.nth(permutations(iterable, r), index) self.assertEqual(actual, expected)
def invert_sbox(sbox_key): sbox = [i | (i << 4) for i in range(0x10)] for k in sbox_key: for x in sbox[:]: sbox.append(k ^ x) sbox_inv = nth(zip(*sorted(enumerate(sbox), key=lambda x: x[1])), 0) return bytes(sbox_inv)
def pcap_show_one_scan(source: client.PacketSource, metadata: client.SensorInfo, num: int = 0, destagger: bool = True) -> None: """Plot all channels of one scan in 2D using matplotlib.""" import matplotlib.pyplot as plt # type: ignore scan = nth(client.Scans(source), num) if not scan: print(f"ERROR: Scan # {num} in not present in pcap file") exit(1) # [doc-stag-pcap-show-one] fig = plt.figure(constrained_layout=True) axs = fig.subplots(len(client.ChanField), 1, sharey=True) for ax, field in zip(axs, client.ChanField): img = normalize(scan.field(field)) if destagger: img = client.destagger(metadata, img) ax.set_title(str(field), fontdict={'fontsize': 10}) ax.imshow(img, cmap='gray', resample=False) ax.set_yticklabels([]) ax.set_yticks([]) ax.set_xticks([0, scan.w]) plt.show()
def pcap_display_xyz_points(source: client.PacketSource, metadata: client.SensorInfo, num: int = 0) -> None: """Plot point cloud using matplotlib.""" import matplotlib.pyplot as plt # type: ignore # [doc-stag-pcap-plot-xyz-points] from more_itertools import nth scan = nth(client.Scans(source), num) if not scan: print(f"ERROR: Scan # {num} in not present in pcap file") exit(1) # set up figure plt.figure() ax = plt.axes(projection='3d') r = 6 ax.set_xlim3d([-r, r]) ax.set_ylim3d([-r, r]) ax.set_zlim3d([-r, r]) plt.title("3D Points XYZ for scan") # transform data to 3d points and graph xyzlut = client.XYZLut(metadata) xyz = xyzlut(scan) key = scan.field(client.ChanField.SIGNAL) [x, y, z] = [c.flatten() for c in np.dsplit(xyz, 3)] ax.scatter(x, y, z, c=normalize(key.flatten()), s=0.2) plt.show()
def select(self, k: int) -> int: """ Return k-th integer stored in this Elias-Fano structure. :param k: index of integer to be reconstructed. :return: k-th stored integer """ # if we are not at the last level if bool(self._level_2): # determine the 'h'-th lvl-2 bucket_id containing the k-th element's lower half # note that 'h' is a list index - NOT a prefix label h = first(locate(accumulate(map(lambda e: len(e), self._level_2.values())), lambda popcnt: popcnt > k)) # determine number 'l' of elements contained in lvl-2 buckets [0..h) l = sum(map(lambda e: len(e), islice(self._level_2.values(), 0, h))) # the '(k-l)'-th element from lvl-2 index with prefix_label['h'] must be the 'k'-th element's lower half inf = self._level_2[nth(self._level_2.keys(), h)].select(k - l) # get the 'k'-th element's upper half from lvl-1 and left_shift appropriately sup = self._level_1.select(h) << (int(math.log2(self._u)) - self._b) # return the combined upper and lower halves return sup + inf # if at last level else: # we don't have a lvl-2 index and therefore return the 'k-th element return self._level_1.select(k)
async def get_or_create_airtable_record( airtable: Airtable, search_field: str, search_value: Any, new_fields: dict[str, Any], ) -> str: results: list[AirtableRecordReadModel] = \ airtable.search( # type: ignore search_field, search_value, ) if len(results) > 1: raise RuntimeError(( f"{len(results)} records in {airtable.table_name} " # type: ignore f"found for {search_field} equal to '{search_value}'")) result = nth(results, 0) if result is not None: return result["id"] record: AirtableRecordReadModel = airtable.insert( # type: ignore new_fields, ) return record["id"]
def right_arrow(vis, action, mods): nonlocal scan if action == 1: print("Skipping forward 10 frames") scan = nth(scans_iter, 10) if scan is None: raise StopIteration update_data(vis)
def pcap_3d_one_scan(source: client.PacketSource, metadata: client.SensorInfo, num: int = 0) -> None: """Render one scan from a pcap file in the Open3D viewer. Args: source: PacketSource from pcap metadata: associated SensorInfo for PacketSource num: scan number in a given pcap file (satrs from *0*) """ try: import open3d as o3d # type: ignore except ModuleNotFoundError: print( "This example requires open3d, which may not be available on all " "platforms. Try running `pip3 install open3d` first.") exit(1) from more_itertools import nth # get single scan by index scan = nth(client.Scans(source), num) if not scan: print(f"ERROR: Scan # {num} in not present in pcap file") exit(1) # [doc-stag-open3d-one-scan] # compute point cloud using client.SensorInfo and client.LidarScan xyz = client.XYZLut(metadata)(scan) # create point cloud and coordinate axes geometries cloud = o3d.geometry.PointCloud( o3d.utility.Vector3dVector(xyz.reshape((-1, 3)))) # type: ignore axes = o3d.geometry.TriangleMesh.create_coordinate_frame( 1.0) # type: ignore # [doc-etag-open3d-one-scan] # initialize visualizer and rendering options vis = o3d.visualization.Visualizer() # type: ignore vis.create_window() vis.add_geometry(cloud) vis.add_geometry(axes) ropt = vis.get_render_option() ropt.point_size = 1.0 ropt.background_color = np.asarray([0, 0, 0]) # initialize camera settings ctr = vis.get_view_control() ctr.set_zoom(0.1) ctr.set_lookat([0, 0, 0]) ctr.set_up([1, 0, 0]) # run visualizer main loop print("Press Q or Excape to exit") vis.run() vis.destroy_window()
def part_1(data): r''' >>> part_1("""\ ... .#. ... ..# ... ###""") 112 ''' grid = GridPart1.parse(data) final = mit.nth(mit.iterate(GridPart1.next, grid), 6) return len(final)
def part_2(data): r''' >>> part_2("""\ ... .#. ... ..# ... ###""") 848 ''' grid = GridPart2.parse(data) final = mit.nth(mit.iterate(GridPart2.next, grid), 6) return len(final)
def count_trees(iter, dx, dy): """Count the trees along a given slope""" trees = 0 y = 0 start = next(iter) max_y = len(start) while y < max_y - dy: y += dy col = nth(iter, dx - 1) if col[y] == "#": trees += 1 print(f"Encountered {trees} trees using this slope") return trees
def pcap_3d_one_scan(source: client.PacketSource, metadata: client.SensorInfo, num: int = 0) -> None: """Render one scan from a pcap file in the Open3D viewer. Args: pcap_path: path to the pcap file metadata_path: path to the .json with metadata (aka :class:`.SensorInfo`) num: scan number in a given pcap file (satrs from *0*) """ import open3d as o3d # get single scan by index scan = nth(client.Scans(source), num) if not scan: print(f"ERROR: Scan # {num} in not present in pcap file") exit(1) # [doc-stag-open3d-one-scan] # compute point cloud using client.SensorInfo and client.LidarScan xyz = client.XYZLut(metadata)(scan) # create point cloud and coordinate axes geometries cloud = o3d.geometry.PointCloud( o3d.utility.Vector3dVector(xyz.reshape((-1, 3)))) axes = o3d.geometry.TriangleMesh.create_coordinate_frame(1.0) # [doc-etag-open3d-one-scan] # initialize visualizer and rendering options vis = o3d.visualization.Visualizer() vis.create_window() vis.add_geometry(cloud) vis.add_geometry(axes) ropt = vis.get_render_option() ropt.point_size = 1.0 ropt.background_color = np.asarray([0, 0, 0]) # initialize camera settings ctr = vis.get_view_control() ctr.set_zoom(0.1) ctr.set_lookat([0, 0, 0]) ctr.set_up([1, 0, 0]) # run visualizer main loop print("Press Q or Excape to exit") vis.run() vis.destroy_window()
def select(self, k: int) -> int: """ Returns the sequence element 'x' such that 'k' sequence predecessors are smaller or equal to 'x'. :param k: index of integer to be reconstructed. :return: k-th stored integer """ if not (0 <= k < self._n): raise IndexError(f"Index %s ∉ [0,..,{self._n - 1}]." % k) # for lower part simply take self._inferiors[k], defaults to 0 in case self._lower_bits == 0 inferior = nth(iter(self._inferiors), k, 0) # the higher part is index of the bucket with accumulated popCount >= k, defaults to 0 iff self._upper_bits == 0 superior = first(locate(iter(self._superiors_prefixSums), lambda cnt: cnt > k), 0) return (superior << self._lower_bits) | inferior
def pcap_show_one_scan(pcap_path: str, metadata_path: str, num: int = 0, destagger: bool = True) -> None: """Show all 4 channels of one scan (*num*) form pcap file (*pcap_path*) Args: pcap_path: path to the pcap file metadata_path: path to the .json with metadata (aka :class:`.SensorInfo`) num: scan number in a given pcap file (satrs from *0*) """ import matplotlib.pyplot as plt # type: ignore # [doc-stag-pcap-show-one] metadata = read_metadata(metadata_path) def prepare_field_image(scan, key, metadata, destagger=True): f = ae(scan.field(key)) if destagger: return client.destagger(metadata, f) return f show_fields = [('range', client.ChanField.RANGE), ('signal', client.ChanField.SIGNAL), ('near_ir', client.ChanField.NEAR_IR), ('reflectivity', client.ChanField.REFLECTIVITY)] with closing(pcap.Pcap(pcap_path, metadata)) as source: scan = nth(client.Scans(source), num) if not scan: return fields_images = [(sf[0], prepare_field_image(scan, sf[1], source.metadata)) for sf in show_fields] fig = plt.figure(constrained_layout=True) axs = fig.subplots(len(fields_images), 1, sharey=True) for ax, field in zip(axs, fields_images): ax.set_title(field[0], fontdict={'fontsize': 10}) ax.imshow(field[1], cmap='gray', resample=False) ax.set_yticklabels([]) ax.set_yticks([]) ax.set_xticks([0, scan.w]) plt.show()
async def validator(): cache_data = validator_TTCache.get(VALIDATOR_CACHE_KEY) if cache_data: resp: ValidatorsResponse = cache_data else: async with lock: cache_data = validator_TTCache.get(VALIDATOR_CACHE_KEY) if cache_data: return cache_data else: latest_block_number_tasks = [] for validator in setting.validator_list: latest_block_number_tasks.append(get_latest_block(validator)) latest_infos = await asyncio.gather(*latest_block_number_tasks, return_exceptions=True) latest_infos_no_exception = list(filter(lambda x: x.block_number != NO_LATEST_BLOCK, latest_infos)) latest_num_dict: Dict[str, LatestInfo] = {i.validator.host: i for i in latest_infos} # get latest blocks from all the validators failed then randomly return the `nextToPropose` if len(latest_infos_no_exception) == 0: best = random.choice(setting.validator_list) max_block_numbers = NO_LATEST_BLOCK else: max_block_numbers = max([i.block_number for i in latest_infos_no_exception]) latest = first_true(latest_infos_no_exception, lambda x: x.block_number == max_block_numbers) index = one(locate(setting.validator_list, lambda x: x.pub_key == latest.sender)) # why +2 ? # actually index validator should be the latest proposed validator # but it is possible that at this moment, the next validator is already trying # to propose a new block. So choosing the +2 validator is more reliable best = nth(ncycles(setting.validator_list, 2), index + 2) split_validators = list(split_before(setting.validator_list, lambda x: x.host == best.host)) if len(split_validators) == 1: sorted_validators = one(split_validators) else: sorted_validators = last(split_validators) + first(split_validators) validators = list(map(lambda x: Validator(host=x.host, grpc_port=x.grpc_port, http_port=x.http_port, latestBlockNumber=latest_num_dict.get(x.host).block_number, timestamp=latest_num_dict.get(x.host).timestamp), sorted_validators)) nextToPropose = NextToPropose(host=best.host, grpcPort=best.grpc_port, httpPort=best.http_port, latestBlockNumber=max_block_numbers) resp = ValidatorsResponse(nextToPropose=nextToPropose, validators=validators) validator_TTCache[VALIDATOR_CACHE_KEY] = resp return resp.dict()
def pcap_display_xyz_points(pcap_path: str, metadata_path: str, num: int = 0) -> None: """Display range from a specified scan number (*num*) as 3D points from pcap file located at *pcap_path* Args: pcap_path: path to the pcap file metadata_path: path to the .json with metadata (aka :class:`.SensorInfo`) num: scan number in a given pcap file (satrs from *0*) """ import matplotlib.pyplot as plt # type: ignore # [doc-stag-pcap-plot-xyz-points] metadata = read_metadata(metadata_path) source = pcap.Pcap(pcap_path, metadata) # get single scan scans = client.Scans(source) scan = nth(scans, num) if not scan: print(f'ERROR: Scan # {num} in not present in pcap file: {pcap_path}') return # set up figure plt.figure() ax = plt.axes(projection='3d') r = 6 ax.set_xlim3d([-r, r]) ax.set_ylim3d([-r, r]) ax.set_zlim3d([-r, r]) plt.title("3D Points XYZ for scan") # transform data to 3d points and graph xyzlut = client.XYZLut(metadata) xyz = xyzlut(scan) key = scan.field(client.ChanField.SIGNAL) [x, y, z] = [c.flatten() for c in np.dsplit(xyz, 3)] ax.scatter(x, y, z, c=ae(key.flatten()), s=0.2) plt.show()
async def get_track_spotify_id( spotify_client: SpotifyClient, airtable_track: AirtableTrack, ) -> Optional[str]: airtable_fields = airtable_track["fields"] try: track_id_from_url = parse_track_id(airtable_fields["Spotify"]) except Exception: track_description = " - ".join([ airtable_fields["Artist"], airtable_fields["Title"], ]) tracks = await spotify_client.search_for_tracks(track_description) maybe_track = nth(tracks, 0) track_id_from_url = \ maybe_track["id"] if maybe_track is not None else None return track_id_from_url
async def handle(e: rnd.ChooseCardToPlay) -> rnd.ChooseCardToPlay: choice = await async_ask_valid_input( "What card do you want to play?", choices=MoveChoice) e.choice = mitt.nth(game.current_round.current_player.hand, choice.value) return e
def part2(asteroids, laser_position): x, y = nth(vaporization_order(laser_position, asteroids), 199) return x * 100 + y
def lattice_paths(n): level = nth(euler.lib.sequences.pascals_triangle(), 2 * n) assert not is_even(len(level)) return level[len(level) // 2]
def test_negative_item_raises(self): """Ensure asking for a negative item raises an exception""" self.assertRaises(ValueError, lambda: mi.nth(range(10), -3))
def test_default(self): """Ensure a default value is returned when nth item not found""" l = range(3) self.assertEqual(mi.nth(l, 100, "zebra"), "zebra")
def test_basic(self): """Make sure the nth item is returned""" l = range(10) for i, v in enumerate(l): self.assertEqual(mi.nth(l, i), v)
def get_incomplete_data_splits( X_complete, X_incomplete, y, fold=0, num_folds=10, stratify=True, random_state=8675309, identities=None): """ Split the datasets for use with cross-validation Parameters ---------- X_complete: data matrix A data matrix suitable for sklearn, without missing values X_incomplete: data matrix A data matrix suitable for sklearn, with missing values represented as `np.nan` y: target variables The target variables corresponding to X fold: int The cv fold to return num_folds: int The number of cv folds to create stratify: bool Whether to use stratified splits (True) or random (False). In particular, stratified splits will not work for regression. random_state: int An attempt to make things reproducible identities: list-like of ints or None Optionally, the identities of the nodes. If present, they will be added as the first column of all of the respective X matrices. Returns (as a named tuple) ------- X_train_complete: data matrix The complete training data for this fold X_train_incomplete: data matrix The incomplete training data for this fold X_test_complete: data matrix The complete testing data for this fold X_test_incomplete: data matrix The incomplete testing data for this fold y_train: target variables The (complete) training target data for this fold y_test: target variables The (complete) testing target data for this fold """ if stratify: cv = sklearn.model_selection.StratifiedKFold( num_folds, random_state=random_state ) else: cv = sklearn.model_selection.KFold( num_folds, random_state=random_state, shuffle=True ) splits = cv.split(X_complete, y) train, test = more_itertools.nth(splits, fold) X_train_complete, y_train = X_complete[train], y[train] X_test_complete, y_test = X_complete[test], y[test] X_train_incomplete = X_incomplete[train] X_test_incomplete = X_incomplete[test] # check on our node identities if identities is not None: train_identities = identities[train] test_identities = identities[test] X_train_complete = add_identities(X_train_complete, train_identities) X_train_incomplete = add_identities(X_train_incomplete, train_identities) X_test_complete = add_identities(X_test_complete, test_identities) X_test_incomplete = add_identities(X_test_incomplete, test_identities) ret = IncompleteDataset( X_train_complete, X_train_incomplete, X_test_complete, X_test_incomplete, y_train, y_test ) return ret
def part_two(): input_lines = get_input() coordinates = create_coordinates(input_lines, 4) cycles = iterate(generate_cycle, coordinates) return len(nth(cycles, 6))
while True: last_turns = turns[last] if len(last_turns) >= 2: last = last_turns[-1] - last_turns[-2] else: last = 0 yield last turns[last].append(turn) turn += 1 test = "0,3,6" result = [-1, 0, 3, 6, 0, 3, 3, 1, 0, 4, 0] assert take(11, generate(test)) == result assert nth(generate("1,3,2"), 2020) == 1 assert nth(generate("2,1,3"), 2020) == 10 assert nth(generate("1,2,3"), 2020) == 27 assert nth(generate("2,3,1"), 2020) == 78 assert nth(generate("3,2,1"), 2020) == 438 assert nth(generate("3,1,2"), 2020) == 1836 task = "2,1,10,11,0,6" print(nth(generate(task), 2020)) # very slow... # assert nth(generate("0,3,6"), 30000000) == 175594 # assert nth(generate("1,3,2"), 30000000) == 2578 # assert nth(generate("2,1,3"), 30000000) == 3544142 # assert nth(generate("1,2,3"), 30000000) == 261214 # assert nth(generate("2,3,1"), 30000000) == 6895259
def nth_prime(n): return nth(primes(), n - 1)
def super_ugly(n, primes): return nth(super_uglies(primes), n - 1)