Пример #1
0
    def style(self, ws: Worksheet):
        # Give columns a fixed width so each sheet can print onto a single
        #  A4 in landscape mode.
        for col_name, col_index in self._column_locations.items():
            # column_dimensions requires a column name, not an index
            ws.column_dimensions[
                get_column_letter(col_index)
            ].width = self.DATE_TYPE_COLUMN_WIDTH

        # Then override the first column width (it's like a header)
        ws.column_dimensions["A"].width = self.FIRST_COLUMN_WIDTH
        # Style the first column in a header-like way
        for cell in first(ws.columns):
            cell.style = "40 % - Accent1"

        # Style header row (note the overlap with the name column... we're
        #  intentionally overwriting the style of A1 to be what is below)
        for cell in first(ws.rows):
            cell.style = "Accent1"
            cell.alignment = Alignment(wrap_text=True, horizontal="center")

        # Intended to be double height, with text wrap set in the loop below
        ws.row_dimensions[1].height = self.HEADER_ROW_HEIGHT

        # Style the data cells (non-header cells)
        for row in ws.iter_rows(min_row=2, min_col=2):
            for cell in row:
                cell.alignment = Alignment(horizontal="center")
                cell.border = self.THIN_BORDER
Пример #2
0
Файл: hmm1.py Проект: numpde/cbb
def viterbi_path_dp(hmm: HMM, O: pd.Series):
    """
    Find one of the most likely sequence of hidden states
    for the sequence of observations O using dynamic programming.
    """

    norm = (lambda s: s / s.sum())

    d = pd.DataFrame(index=hmm.Q, columns=O.index)
    m = pd.DataFrame(index=hmm.Q, columns=O.index)
    d[first(O.index)] = hmm.b[first(O)] * hmm.e
    for ((s, __), (t, ot)) in pairwise(O.items()):
        x: pd.DataFrame
        x = hmm.a * np.outer(d[s], hmm.b[ot])
        m[t] = x.idxmax(axis=0)
        d[t] = norm(x.max(axis=0))

    # Inferred sequence of hidden states
    qq = pd.Series(index=O.index, dtype=object)

    q = d[last(d)].idxmax()
    qq[last(d)] = q
    for (s, t) in reversed(list(pairwise(O.index))):
        q = m[t][q]
        qq[s] = q

    return qq
Пример #3
0
def wait_for_public_ip(v1: CoreV1Api, namespace: str) -> str:
    """
    Wait for LoadBalancer to get the public ip.

    :param v1: CoreV1Api
    :param namespace: namespace
    :return: str
    """
    resp = v1.list_namespaced_service(namespace)
    counter = 0
    svc_item = first(x for x in resp.items
                     if x.metadata.name == "nginx-ingress")
    while str(
            svc_item.status.load_balancer.ingress) == "None" and counter < 20:
        time.sleep(5)
        resp = v1.list_namespaced_service(namespace)
        svc_item = first(x for x in resp.items
                         if x.metadata.name == "nginx-ingress")
        counter = counter + 1
    if counter == 20:
        pytest.fail(
            "After 100 seconds the LB still doesn't have a Public IP. Exiting..."
        )
    print(f"Public IP ='{svc_item.status.load_balancer.ingress[0].ip}'")
    return str(svc_item.status.load_balancer.ingress[0].ip)
Пример #4
0
def extraction_func(code):
    lines = more_itertools.peekable(iter(code.split("\n")))

    match = directive_re.fullmatch(more_itertools.first(lines))
    if not match:
        raise RuntimeError(f"misformatted code block:\n{code}")

    directive = match.groupdict()
    directive.pop("indent")

    directive["options"] = tuple(
        line.strip() for line in take_while(lines, lambda line: line.strip()))

    line = more_itertools.first(lines)
    if line.strip():
        raise RuntimeError(
            f"misformatted code block: newline after options required but found: {line}"
        )

    lines_ = tuple(lines)
    indent = len(lines_[0]) - len(lines_[0].lstrip())
    directive["prompt_length"] = indent
    directive["n_header_lines"] = len(directive["options"]) + 2

    code_ = hide_magic(textwrap.dedent("\n".join(lines_)))

    return directive, code_
Пример #5
0
    def parse_list(self, response):
        if response.status == 521:
            yield from self.handle_521(response, self.parse_list)
            return
        parent = response.meta.get('parent')
        rows = response.xpath('//table[@id="product_table"]/tbody/tr')
        for row in rows:
            sub_brand = row.xpath('./td[10]/div/text()').get()
            if sub_brand and not ('坛墨' in sub_brand
                                  or 'tm' in sub_brand.lower()):
                continue
            url = row.xpath('./td[1]//a/@href').get()
            yield Request(url,
                          callback=self.parse_detail,
                          meta={'parent': parent})

        cur_page = first(re.findall(r'pno: (\d+),', response.text), None)
        total_page = first(re.findall(r'total: (\d+),', response.text), None)
        if cur_page is None or total_page is None:
            return
        if int(cur_page) >= int(total_page):
            return
        next_page = re.sub(r'\d+(?=\.html)', str(int(cur_page) + 1),
                           response.url)
        yield Request(next_page,
                      callback=self.parse_list,
                      meta={'parent': parent})
Пример #6
0
    def parse(self, response):
        rel_urls = response.xpath('//a[@class="Stkno"]/@href').extract()
        for rel_url in rel_urls:
            yield Request(
                urljoin(self.base_url, rel_url), callback=self.detail_parse,
                meta={'sub_brand': response.meta.get('sub_brand')}
            )

        next_page = response.xpath('//a[contains(@style,"background-color:#0088FF;")]/../following-sibling::td/a')
        if not next_page:
            return
        target = next_page.xpath('./@id').get()
        to_page = next_page.xpath('./text()').get()

        view_state = response.xpath('//input[@id="__VIEWSTATE"]/@value').get('')
        view_state = view_state or first(re.findall(r'__VIEWSTATE\|([^|]+)', response.text))

        view_state_generator = response.xpath('//input[@id="__VIEWSTATEGENERATOR"]/@value').get('')
        view_state_generator = view_state_generator or first(
            re.findall(r'__VIEWSTATEGENERATOR\|([^|]+)', response.text))

        event_validation = response.xpath('//input[@id="__EVENTVALIDATION"]/@value').get('')
        event_validation = event_validation or first(re.findall(r'__EVENTVALIDATION\|([^|]+)', response.text))

        post_data = gen_post_data(target, to_page, view_state, view_state_generator, event_validation)
        yield FormRequest(
            response.url, formdata=post_data, callback=self.parse, meta={'sub_brand': response.meta.get('sub_brand')}
        )
Пример #7
0
def StableMatching(df,
                   male_label='male',
                   female_label='female',
                   pref_male_label='pref_male',
                   pref_female_label='pref_female',
                   **kwargs):
    """
    安定マッチング問題
    入力
        df: 選好のDataFrameもしくはCSVファイル名
        male_label: 男性の属性文字
        female_label: 女性の属性文字
        pref_male_label: 男性から見た順番の属性文字
        pref_female_label: 女性から見た順番の属性文字
    出力
        マッチングのDataFrame(男性優先)
    """
    df = graph_from_table(df, None, no_graph=True, **kwargs)[1]
    m = range(df[male_label].max() + 1)
    f = range(df[female_label].max() + 1)
    prefm = [[
        first(
            df[(df[male_label] == i)
               & (df[pref_male_label] == j)][female_label], 1e6) for j in f
    ] for i in m]
    preff = [[
        first(
            df[(df[female_label] == i)
               & (df[pref_female_label] == j)][male_label], 1e6) for j in m
    ] for i in f]
    t = stable_matching(prefm, preff)
    return pd.concat([
        df[(df[female_label] == i) & (df[male_label] == j)]
        for i, j in t.items()
    ]).sort_values(male_label)
Пример #8
0
    def get_project_uuids_from_azul(self) -> List[Dict[str, Any]]:
        """Get all project UUIDs from Azul by using the service APIs search_after query parameter."""
        url = endpoints[self.deployment]['azul_projects_url']
        project_uuids = []
        search_after_string = ''
        first_iter = True
        while True:
            if first_iter:
                response = requests.get(url)
                first_iter = False
            else:
                response = requests.get(url + '?' + search_after_string)
            try:
                hits = response.json()['hits']
            except KeyError:

                return project_uuids

            search_after_string = self.make_string_url_compliant_for_search_after(
                project_title=first(hits[-1]['projects'])['projectTitle'],
                document_id=hits[-1]['entryId'])
            project_uuids = project_uuids + [{
                'project_title':
                first(hits[i]['projects'])['projectTitle'],
                'project_UUID':
                hits[i]['entryId']
            } for i in range(len(hits))]
def test_compound_rows():
    pane = Pane.parse("_ Tag\t${lemma}\n")
    row = one(pane.rows)

    multiple_forms = ("form", "longer-form")
    filled_row = row.fill({"${lemma}": multiple_forms})
    assert isinstance(filled_row, CompoundRow)

    # Ensure all generated forms are in there:
    for form in multiple_forms:
        assert filled_row.contains_wordform(form)

    assert (filled_row.num_subrows == ilen(filled_row.subrows) ==
            len(multiple_forms)), "expected as many subrows as there are forms"

    first_row_cells = tuple(first(filled_row.subrows).cells)
    assert first(row.cells).fst_tags == first_row_cells[0].fst_tags
    assert first_row_cells[0].row_span == len(multiple_forms)

    first_form = first_row_cells[-1]
    assert isinstance(first_form, WordformCell)
    assert first_form.inflection == multiple_forms[0]

    last_row_cells = tuple(last(filled_row.subrows).cells)
    assert isinstance(last_row_cells[0], SuppressOutputCell)

    last_form = last_row_cells[-1]
    assert isinstance(last_form, WordformCell)
    assert last_form.inflection == multiple_forms[-1]
Пример #10
0
def draw(context):
    queues = context[more_itertools.first(var for var in iter(context) if var.name == 'queues')]

    root = tk.Tk()

    root.title('Чат Майнкрафтера')

    root_frame = tk.Frame()
    root_frame.pack(fill="both", expand=True)

    status_labels = create_status_panel(root_frame)

    input_frame = tk.Frame(root_frame)
    input_frame.pack(side="bottom", fill=tk.X)

    input_field = ttk.Entry(input_frame)
    input_field.pack(side="left", fill=tk.X, expand=True)

    input_field.bind("<Return>", lambda event: process_new_message(input_field, queues['sending_queue']))

    send_button = ttk.Button(input_frame)
    send_button["text"] = "Отправить"
    send_button["command"] = lambda: process_new_message(input_field, queues['sending_queue'])
    send_button.pack(side="left")

    conversation_panel = ScrolledText(root_frame, wrap='none')
    conversation_panel.pack(side="top", fill="both", expand=True)

    widgets = more_itertools.first(var for var in iter(context) if var.name == 'widgets')
    context.run(widgets.set, (root_frame, conversation_panel, status_labels))
Пример #11
0
    def parse_price(self, response):
        t = first(re.findall(r'({.+});', response.text))
        if not t:
            return
        obj = json.loads(t)
        obj = json.loads(obj.get('ObjResult'))
        d = response.meta.get('product', {})

        if not obj.items():
            yield HongmengItem(**d)
            return

        _, prds = zip(*obj.items())
        for prd in prds:
            prd = first(prd)
            for inventory in prd.get('Inventores', []):
                goods_info = json.loads(inventory.get('Goods_Info', '{}')).get('goodsinfo', {})

                d_prd = {
                    'sub_cat_no': inventory.get('Goods_no'),
                    'place_code': inventory.get('Placecode'),
                    'amount': inventory.get('Amount'),
                    'package': goods_info.get('packaging'),
                    'sub_brand': goods_info.get('brand'),
                    'purity': goods_info.get('purity'),
                    'price': f"{inventory.get('MoneyUnit')} {inventory.get('Price')}"
                }
                d_prd.update(d)
                yield HongmengItem(**d_prd)
Пример #12
0
def test_successfully_extending_partial_match():
    """
    Tests whether we can match a perception pattern against a perception graph
    when initializing the search from a partial match.
    """

    target_object = BOX
    # Create train and test templates for the target objects
    train_obj_object = object_variable("obj-with-color", target_object)

    obj_template = Phase1SituationTemplate(
        "colored-obj-object", salient_object_variables=[train_obj_object])
    template = all_possible(obj_template,
                            chooser=PHASE1_CHOOSER_FACTORY(),
                            ontology=GAILA_PHASE_1_ONTOLOGY)

    train_curriculum = phase1_instances("all obj situations",
                                        situations=template)

    perceptual_representation = only(train_curriculum.instances())[2]

    # Original perception graph
    perception = PerceptionGraph.from_frame(
        perceptual_representation.frames[0])

    # Create a perception pattern for the whole thing
    # and also a perception pattern for a subset of the whole pattern
    whole_perception_pattern = PerceptionGraphPattern.from_graph(
        perception).perception_graph_pattern

    partial_digraph = whole_perception_pattern.copy_as_digraph()
    partial_digraph.remove_nodes_from([
        node for node in partial_digraph.nodes
        if isinstance(node, IsColorNodePredicate)
    ])
    partial_perception_pattern = PerceptionGraphPattern(partial_digraph)

    # get our initial match by matching the partial pattern
    matcher = partial_perception_pattern.matcher(
        perception, match_mode=MatchMode.NON_OBJECT)

    partial_match: PerceptionGraphPatternMatch = first(
        matcher.matches(use_lookahead_pruning=True))
    partial_mapping = partial_match.pattern_node_to_matched_graph_node

    # Try to extend the partial mapping, to create a complete mapping
    matcher_2 = whole_perception_pattern.matcher(
        perception, match_mode=MatchMode.NON_OBJECT)
    complete_match: PerceptionGraphPatternMatch = first(
        matcher_2.matches(initial_partial_match=partial_mapping,
                          use_lookahead_pruning=True),
        None,
    )
    complete_mapping = complete_match.pattern_node_to_matched_graph_node
    assert len(complete_mapping) == len(perception.copy_as_digraph().nodes)
    assert len(complete_mapping) == len(
        whole_perception_pattern.copy_as_digraph().nodes)
def ensure_descriptions_are_equal(
    expected_case_description_dict, obtained_description_dict, ignored_properties
):
    """
    Check that two cases description are equals.
    """
    from more_itertools import first

    for key, expected_value in expected_case_description_dict.items():
        if key in ignored_properties:
            continue
        if isinstance(expected_value, dict):
            ensure_descriptions_are_equal(
                expected_case_description_dict=expected_value,
                obtained_description_dict=obtained_description_dict[key],
                ignored_properties=ignored_properties,
            )
            continue

        is_list = isinstance(expected_value, list)
        if is_list and isinstance(first(expected_value, None), dict):
            for index, value in enumerate(expected_value):
                ensure_descriptions_are_equal(
                    expected_case_description_dict=value,
                    obtained_description_dict=obtained_description_dict[key][index],
                    ignored_properties=ignored_properties,
                )
            continue

        is_ndarray = isinstance(expected_value, np.ndarray)
        if (
            is_ndarray
            or is_list
            and isinstance(first(expected_value, None), np.ndarray)
        ):
            assert np.array_equal(obtained_description_dict[key], expected_value)
            continue  # pragma no cover [bug on coverage.py: https://github.com/nedbat/coveragepy/issues/198]

        is_array = isinstance(expected_value, Array)
        if is_array:
            unit = expected_value.GetUnit()
            obtained_values = np.array(obtained_description_dict[key].GetValues(unit))
            expected_values = np.array(expected_value.GetValues(unit))
            assert np.allclose(
                obtained_values, expected_values
            ), f"Obtained={obtained_values} != {expected_values}"
            continue  # pragma no cover [bug on coverage.py

        # Skip the check when materials or walls only has defaults values
        if key in ("materials", "walls") and first(expected_value, None) is None:
            continue

        assert (
            obtained_description_dict[key] == expected_value
        ), f'attribute "{key}" doesn\'t match, obtained "{obtained_description_dict[key]}" while "{expected_value}" was expected.'
Пример #14
0
def extract_code(line_unit, code_format):
    dedented = textwrap.dedent(line_unit)
    indentation_depth = len(more_itertools.first(line_unit.split("\n"))) - len(
        more_itertools.first(dedented.split("\n")))

    func = extraction_funcs.get(code_format, None)
    if func is None:
        raise RuntimeError(f"unknown code format: {code_format}")

    parameters, extracted = func(dedented)
    return indentation_depth, parameters, extracted
Пример #15
0
 def get_cover_image(self):
     if not (
         cover := more_itertools.first(
             self.epub.get_items_of_type(ebooklib.ITEM_COVER), None
         )
     ):
         cover = more_itertools.first(
             filter(
                 lambda item: "cover" in item.file_name.lower(),
                 self.epub.get_items_of_type(ebooklib.ITEM_IMAGE),
             ),
             None,
         )
Пример #16
0
    def parse(self, response):
        xml = XML(response.body)
        prds = xml.xpath('//Reference')
        for prd in prds:
            cat_no = first(prd.xpath('./Order_Code/text()'), None)
            d = {
                "brand":
                self.brand,
                "cat_no":
                cat_no,
                "cas":
                first(prd.xpath('./CAS_Registry_Number/text()'), None),
                "en_name":
                first(prd.xpath('./Reference_Standard/text()'), None),
                "info2":
                first(prd.xpath('./Storage/text()'), None),
                "info3":
                first(prd.xpath('./Quantity_per_vial/text()'), None),
                "info4":
                first(prd.xpath('./Price/text()'), None),
                "prd_url":
                f"https://crs.edqm.eu/db/4DCGI/View={first(prd.xpath('./Order_Code/text()'), '')}",
            }
            yield RawData(**d)

            price = first(prd.xpath('./Price/text()'), None)
            yield ProductPackage(
                brand=self.brand,
                cat_no=cat_no,
                package=first(prd.xpath('./Quantity_per_vial/text()'), None),
                price=price and price.replace('€', ''),
                currency='EUR',
            )
Пример #17
0
 def export(self, db: Database):
     idx = 1
     entities = {}
     identities = []
     songs = []
     playlists = []
     pairings = []
     for sig in self.entities:
         if sig in self.playlists:
             identities.append({
                 'id': idx,
                 'uuid4': uuid.uuid4(),
                 'model': 'Playlist',
             })
             playlists.append({
                 'id': idx,
                 'title': first(self.playlists[sig]),
             })
             entities[sig] = idx
             idx += 1
         elif sig in self.songs:
             candidate = first([json.loads(s) for s in self.songs[sig]])
             tmpl = {
                 'title': None,
                 'artist': None,
                 'album': None,
                 'lyrics': None,
                 'stats': None,
                 'id_musicbrainz': None,
                 'id_genius': None,
             }
             identities.append({
                 'id': idx,
                 'uuid4': uuid.uuid4(),
                 'model': 'Song',
             })
             songs.append({'id': idx, **tmpl, **candidate})
             entities[sig] = idx
             idx += 1
     for k, v in self.song_playlists.items():
         for p in v:
             ida = entities.get(k)
             idb = entities.get(p)
             if ida and idb:
                 pairings.append({'src': idb, 'dst': ida})
     db.execute(Identity.__table__.insert(), identities)
     db.execute(Song.__table__.insert(), songs)
     db.execute(Playlist.__table__.insert(), playlists)
     db.execute(Relationship.cached['_g_playlists_songs'].insert(),
                pairings)
Пример #18
0
 def login(self, username, password):
     s = HTMLSession()
     r = s.get(self.base_url)
     btl_return = first(r.html.xpath('//input[@id="btl-return"]/@value'))
     secret_name = first(r.html.xpath('//input[@id="btl-return"]/following-sibling::input/@name'))
     d = {
         'bttask': 'login',
         'username': username,
         'passwd': password,
         secret_name: 1,
         'return': btl_return,
     }
     _ = s.post(self.base_url, data=d)
     self.cookies = dict(s.cookies)
Пример #19
0
def pick_toggled(values):
    bases, toggles = partition(
        lambda x: isinstance(x, Toggle),
        values,
    )

    toggle = first(toggles, Toggle(False))

    try:
        base = first(bases)
    except ValueError:
        raise NoValueFound("can't find base value to toggle", values.log)

    return toggle.value != base
Пример #20
0
Файл: apple.py Проект: tg-z/hpi
def _parse_calendar_recents(f: Path) -> Iterator[Location]:
    tr = etree.parse(str(f))
    for location in _parse_apple_xml_val(tr.find("array")):
        loc_data: Dict[str, Any] = first(list(location.values()))
        if "map location" in loc_data:
            if "t" in loc_data:
                for tstamp in loc_data["t"]:
                    yield Location(
                        lng=loc_data["map location"]["longitude"],
                        lat=loc_data["map location"]["latitude"],
                        name=loc_data["display name"],
                        address=first(loc_data.get("addressArray", []), None),
                        dt=tstamp,
                    )
Пример #21
0
def tick(carts: Dict[Coord, Cart], track: Track, avoid_accidents=False):
    for c in count():
        new_carts = {}
        while carts:
            cart = carts.pop(coord := first(carts))
            coord += _update_cart(coord, cart, track)
            if avoid_accidents and not _avoid_accident(coord, carts, new_carts):
                continue
            elif coord in new_carts or coord in carts:
                return coord
            new_carts[coord] = cart
        if len(new_carts) == 1:
            return first(new_carts)
        carts = dict(sorted(new_carts.items(), key=lambda kv: (kv[0][1], kv[0][0])))
Пример #22
0
def QuadAssign(
    dfqu,
    dfdi,
    from_label="from",
    to_label="to",
    quant_label="quant",
    dist_label="dist",
    target_label="target",
    pos_label="pos",
    **kwargs,
):
    """
    2次割当問題
        全探索
    入力
        dfqu: 対象間の輸送量のDataFrameもしくはCSVファイル名
        dfdi: 割当先間の距離のDataFrameもしくはCSVファイル名
        from_label: 輸送元番号の属性文字
        to_label: X輸送先番号の属性文字
        quant_label: 必要輸送量の属性文字
        dist_label: 距離の属性文字
        target_label: 対象の属性文字(結果)
        pos_label: 位置の属性文字(結果)
    出力
        評価値と割当
    """
    dfqu = graph_from_table(dfqu, None, no_graph=True, **kwargs)[1]
    dfdi = graph_from_table(dfdi, None, no_graph=True, **kwargs)[1]
    tmp = dfdi.copy()
    tmp[[to_label, from_label]] = tmp[[from_label, to_label]]
    dfdi = pd.concat([dfdi, tmp]).drop_duplicates([from_label, to_label])
    r = range(
        max(
            dfqu[[from_label, to_label]].max().max(),
            dfdi[[from_label, to_label]].max().max(),
        ) + 1)
    q = [[
        first(
            dfqu[(dfqu[from_label] == i) & (dfqu[to_label] == j)][quant_label],
            0) for j in r
    ] for i in r]
    d = [[
        first(
            dfdi[(dfdi[from_label] == i) & (dfdi[to_label] == j)][dist_label],
            np.inf,
        ) for j in r
    ] for i in r]
    r, t = quad_assign(q, d)
    return r, pd.DataFrame([i for i in enumerate(t)],
                           columns=[target_label, pos_label])
Пример #23
0
def part_2(data):
    '''
    >>> part_2(sample_tiles)
    273

    '''
    parsed = list(parse_all('Tile: {:n}:\n{}', data.split('\n\n')))
    tiles = {
        int(num): Tile.parse(int(num), tile)
        for num, tile in parse_all('Tile {:d}:\n{}', data.split('\n\n'))
    }

    Tile.all_tiles = tiles
    calculate_neighbours(tiles)

    # We don't know the orientation of the picture yet, so just
    # pick any corner piece and declare it to be the top left
    top_left = mit.first(tiles[c] for c in tiles if tiles[c].neighbours == 2)

    # and put it around the right way
    oriented_top_left = mit.first(o for o in top_left.orientations
                                  if None not in (o.bottom_neighbour,
                                                  o.right_neighbour))

    side_length = int(math.sqrt(len(tiles)))

    rows = []
    start = oriented_top_left
    while len(rows) < side_length:
        row = [start]
        while len(row) < side_length:
            row.append(row[-1].right_neighbour)

        start = row[0].bottom_neighbour
        # Strip borders of placed tiles
        rows.append(np.hstack([r.data[1:-1, 1:-1] for r in row]))

    picture = Tile('picture', np.vstack(rows))

    sea_monster = Tile.parse('AHHH!', sea_monster_string)
    n_seamonsters = sum(
        ((sea_monster.data == ' ') | (sea_monster.data == window)).all()
        for opicture in picture.orientations
        for window in np_windows(opicture.data, sea_monster.data.shape))

    # The above seems to reliably double count all the sea monsters and I don't really
    # know why..
    return (picture.data == '#').sum() - (sea_monster.data == '#').sum() * int(
        n_seamonsters / 2)
Пример #24
0
Файл: hmm1.py Проект: numpde/cbb
def learn_baum_welch(hmm: HMM, O: pd.Series, niter=5, delay=0.9):
    for i in range(niter):
        # Forward variable
        a = pd.DataFrame(index=hmm.Q, columns=O.index)
        a[first(a)] = hmm.b[O[first(a)]] * hmm.e
        for (s, t) in pairwise(a):
            a[t] = hmm.b[O[t]] * (a[s] @ hmm.a)

        # Baum-Welch score Pr(O|M), based on remark in [1, p.179]:
        prom = sum(a[last(a.columns)])
        assert (prom > 0)

        print(F"Model likelihood before training step #{i + 1}: {prom}")

        # Backward variable (includes the hmm.b factor)
        b = pd.DataFrame(index=hmm.Q, columns=O.index)
        b[last(b)] = hmm.b[O[last(b)]] * 1
        for (s, t) in reversed(list(pairwise(b))):
            b[s] = hmm.b[O[s]] * (hmm.a @ b[t])

        # Remark [1, p.182]:
        if not np.isclose(prom, sum(b[first(b.columns)] * hmm.e), atol=0, rtol=1e-3):
            print("ERROR:", prom, "should equal", sum(b[first(b.columns)] * hmm.e))
            exit()

        # Expected number of transitions state i -> state j [1, Claim 5.12 and p.183]:
        n = pd.Series(
            data={
                s: hmm.a * np.outer(a[s], b[t]) / prom
                for (s, t) in pairwise(O.index)
            },
        )

        # From [1, Claim 5.9 on p.181]:
        # g = a * b / prom  # Not correct with the redefinition of b
        # Use [1, Claim 5.12 and Note on p.183]:
        g = n.apply(lambda x: x.sum(axis=1)).append(a[last(a.columns)] * 1 / prom, verify_integrity=True).T
        assert all(np.isclose(g.sum(axis=0), 1))
        assert all(np.isclose(n.sum().sum(axis=1), g[n.index].sum(axis=1)))
        assert all(np.isclose(g.groupby(O, axis=1).sum().sum(axis=1), g.sum(axis=1)))

        norm_rows = (lambda df: df.apply(lambda s: s / s.sum(), axis=1))
        hmm.e = delay * hmm.e + (1 - delay) * np.sum(first(n), axis=1)
        hmm.a = delay * hmm.a + (1 - delay) * norm_rows(n.sum())
        hmm.b = delay * hmm.b + (1 - delay) * norm_rows(pd.DataFrame(columns=hmm.S, data=g.groupby(O, axis=1).sum()).fillna(0))
        assert np.isclose(hmm.e.sum(), 1)
        assert all(np.isclose(hmm.a.sum(axis=1), 1))
        assert all(np.isclose(hmm.b.sum(axis=1), 1))
Пример #25
0
def test_scope_of_initialized_variable():
    js = """function smoo() {
                var a = 0;
            }"""
    ast = parse(js)
    assignment = first(assignments(ast))
    eq_(scope_of(assignment["id"]["name"], assignment)["id"]["name"], "smoo")
Пример #26
0
def detection_func(lines):
    try:
        _, line = lines.peek()
    except StopIteration:
        line = ""

    match = prompt_re.match(line)
    if not match:
        return None

    groups = match.groupdict()
    indent = len(groups["indent"])
    prompt_length = len(groups["prompt"])

    detected_lines = list(
        itertools.chain(
            [more_itertools.first(lines)],
            continuation_lines(lines, indent, prompt_length),
        ))
    line_numbers, lines = map(tuple, more_itertools.unzip(detected_lines))

    line_range = min(line_numbers), max(line_numbers) + 1
    if line_numbers != tuple(range(line_range[0], line_range[1])):
        raise RuntimeError("line numbers are not contiguous")

    return line_range, name, "\n".join(lines)
Пример #27
0
def sanitize(html, plaintext=False):
    soup = BeautifulSoup(html, 'html.parser')
    articles = soup.select("article.article")

    if len(articles) != 1:
        raise ArticleNotFound()

    article = articles[0]
    article.attrs = {}

    buzz_blocks = [
        *article.select('.article-disclaimer'),
        *article.select('footer.article-footer'),
        *article.select('aside'),
    ]
    for el in buzz_blocks:
        el.decompose()

    article_title = first(article.select('.article-header__title'))
    article_title_text = article_title.get_text()

    remove_buzz_attrs(article)
    remove_buzz_tags(article)

    if not plaintext:
        text = article.prettify()
    else:
        remove_all_tags(article)
        text = article.get_text()
    return article_title_text.strip(), text.strip()
Пример #28
0
def magic_fill(it, *, name: Optional[str] = None) -> None:
    if name is None:
        assert callable(it)  # generators have no name/module
        name = f'{it.__module__}:{it.__name__}'
    assert name is not None

    if callable(it):
        it = it()

    from itertools import tee
    from more_itertools import first, one
    it, x = tee(it)
    f = first(x, default=None)
    if f is None:
        logger.warning('%s has no data', name)
        return

    # TODO can we reuse pandas code or something?
    #
    from .pandas import _as_columns
    schema = _as_columns(type(f))

    from datetime import datetime
    dtex = RuntimeError(f'expected single datetime field. schema: {schema}')
    dtf = one((f for f, t in schema.items() if t == datetime),
              too_short=dtex,
              too_long=dtex)

    fill(it, measurement=name, reset=True, dt_col=dtf)
Пример #29
0
        async def handle(e: rnd.RoundEnd) -> None:
            get_username = lambda p: game.get_player(p).username  # noqa

            await pause()
            print_header("Round end", filler="—")
            draw_game(game, reveal=True)

            print(">>>>> The round has ended! <<<<<")
            if e.reason == rnd.RoundEnd.Reason.EMPTY_DECK:
                print("There are no cards remaining in the deck.")
                if len(e.tie_contenders) == 1:
                    print(
                        f"{get_username(e.winner)} wins with a {e.winner.hand.card}, "
                        f"which is the highest card among those remaining.")
                else:
                    card = mitt.first(p.hand.card for p in e.tie_contenders)
                    contenders = list(map(get_username, e.tie_contenders))
                    contenders_str = (
                        f"Both {contenders[0]} and {contenders[1]}"
                        if len(contenders) == 2 else
                        f"Each of {', '.join(contenders[:-1])} and {contenders[-1]}"
                    )
                    print(f"{contenders_str} have the highest card: a {card}.")
                    print(
                        f"But {get_username(e.winner)} has a higher sum of discarded"
                        f" values, so they win." if len(e.winners) == 1 else
                        f"And they each have the same sum of discarded values,"
                        f" so they {'both' if len(contenders) == 2 else 'all'} win"
                        f" in a tie.")
            elif e.reason == rnd.RoundEnd.Reason.ONE_PLAYER_STANDING:
                print(
                    f"{get_username(e.winner)} is the only player still alive, "
                    f"so they win the round.")
Пример #30
0
    def minimum_spanning_tree(self) -> Iterator[Node]:
        """
        For the same edge/node insertion order, output is guaranteed to be deterministic.

        The output is ordered in such a way that, except the first one, the smallest candidate edge is always greedily picked first.

        For disconnected graph, ValueError is raised.

        Every connected graph has a minimum spanning tree. (may not be unique)
        """

        if len(self._adjacency_list) == 1:
            yield first(self._adjacency_list)
            return

        edges = sorted(self._weight_table, key=self._weight_table.__getitem__)

        res = OrderedSet()
        uf = UnionFind(self._adjacency_list)
        cnt = 0
        for edge in edges:
            u, v = edge
            if uf.find(u) == uf.find(v):
                continue
            uf.union(u, v)
            res.update(edge)
            cnt += 1
            if cnt == len(self._adjacency_list) - 1:
                break

        if cnt < len(self._adjacency_list) - 1:
            raise ValueError("Disconnected graph has no minimum spanning tree")

        yield from res
Пример #31
0
def normalize_cluster_name(cluster_name):
    """Normalizes the cluster name to avoid from duplicated E-Zone prefix."""
    fragments = cluster_name.split(u'-')
    prefix = first(fragments)
    if prefix and prefix in settings.ROUTE_EZONE_LIST:
        return u'-'.join(dedupleft(fragments, prefix))
    return unicode(cluster_name)
Пример #32
0
def test_scope_of_initialized_variable():
    js = """function smoo() {
                var a = 0;
            }"""
    ast = parse(js)
    assignment = first(assignments(ast))
    eq_(assignment.scope_of(assignment['id']['name'])['id']['name'], 'smoo')
Пример #33
0
def test_scope_of_global_function():
    js = """function smoo() {
                var a;
                a = 0;
            }"""
    ast = parse(js)
    assignment = first(assignments(ast))
    eq_(assignment.scope_of(assignment['left']['name'])['type'], 'FunctionDeclaration')
Пример #34
0
def test_scope_of_global_function():
    js = """function smoo() {
                var a;
                a = 0;
            }"""
    ast = parse(js)
    assignment = first(assignments(ast))
    eq_(scope_of(assignment["left"]["name"], assignment)["type"], "FunctionDeclaration")
Пример #35
0
    def nearest_scope_holder(self):
        """Return the nearest node that can have its own scope, potentially
        including myself.

        This will be either a FunctionDeclaration or a Program (for now).

        """
        return first(n for n in self.walk_up() if n['type'] in
                     ['FunctionDeclaration', 'Program'])
Пример #36
0
def test_scope_of_inner_function():
    js = """function smoo() {
                function bar() {
                    a = 0;
                }
            }"""
    ast = parse(js)
    assignment = first(assignments(ast))
    eq_(scope_of(assignment["left"]["name"], assignment)["type"], "Program")
Пример #37
0
def test_scope_of_inner_function():
    js = """function smoo() {
                function bar() {
                    a = 0;
                }
            }"""
    ast = parse(js)
    assignment = first(assignments(ast))
    eq_(assignment.scope_of(assignment['left']['name'])['type'], 'Program')
Пример #38
0
def test_scope_of_inner_reference():
    js = """function smoo() {
                var a;

                function bar() {
                    a = 0;
                }
            }"""
    ast = parse(js)
    assignment = first(assignments(ast))
    eq_(assignment.scope_of(assignment['left']['name'])['id']['name'], 'smoo')
Пример #39
0
def api_latest_deaths(world: minecraft.World):
    """Returns JSON containing information about the most recent death of each player"""
    def newest_timestamp(item):
        player_id, deaths = item
        return datetime.datetime.strptime(deaths[-1]['timestamp'], '%Y-%m-%d %H:%M:%S').replace(tzinfo=datetime.timezone.utc)

    all_deaths = api_deaths(world)
    return {
        'deaths': {player_id: deaths[-1] for player_id, deaths in all_deaths.items()},
        'lastPerson': more_itertools.first(sorted(all_deaths.items(), key=newest_timestamp, reverse=True), (None, []))[0]
    }
Пример #40
0
def test_simple_html_line():
    """See if the offsets are right in simple HTML stitching."""
    a = Region('a')
    b = Region('b')
    line = LINE
    text = 'hello'
    eq_(html_line(text, first(tags_per_line([(0, True, line),
                                             (0, True, a), (3, False, a),
                                             (3, True, b), (5, False, b),
                                             (5, False, line)])), 0),
        '<span class="a">hel</span><span class="b">lo</span>')
Пример #41
0
def test_scope_of_inner_reference():
    js = """function smoo() {
                var a;

                function bar() {
                    a = 0;
                }
            }"""
    ast = parse(js)
    assignment = first(assignments(ast))
    eq_(scope_of(assignment["left"]["name"], assignment)["id"]["name"], "smoo")
Пример #42
0
def test_scope_building():
    """Make sure we find all the declarations within a function but don't stray
    into inner functions."""
    js = """function smoo() {
                var w, x;
                
                if (true) {
                    var y;
                }
                
                function bar() {
                    var z;
                }
            }"""
    ast = parse(js)
    function = first(node for node in ast.walk_down() if
                     node['type'] == 'FunctionDeclaration')
    eq_(function.scope(), set(['w', 'x', 'y']))
Пример #43
0
def test_scope_building():
    """Make sure we find all the declarations within a function but don't stray
    into inner functions."""
    js = """
    function smoo() {
        var w, x;
        if (true) {
            var y;
        }
        function bar() {
            var z;
        }
    }
    function barbar() {

    }
    """
    ast = parse(js)
    function = first(node for node in ast.walk_down() if
                     isinstance(node, FunctionDeclaration))
    eq_(set(function.scope().keys()), set(['w', 'x', 'y', 'smoo', 'bar']))

    eq_(set(ast.scope().keys()), set(['smoo', 'barbar']))
Пример #44
0
def test_scope_of_global():
    """Make sure the scope of a global is the entire program."""
    js = """a = 0;"""
    ast = parse(js)
    assignment = first(assignments(ast))
    eq_(scope_of(assignment["left"]["name"], assignment)["type"], "Program")
Пример #45
0
    def __init__(self, input, relative_to=None):
        """Pull in and validate a config file.

        :arg input: A string or dict from which to populate the config
        :arg relative_to: The dir relative to which to interpret relative paths

        Raise ConfigError if the configuration is invalid.

        """
        schema = Schema({
            'DXR': {
                Optional('temp_folder', default=abspath('dxr-temp-{tree}')):
                    AbsPath,
                Optional('default_tree', default=None): basestring,
                Optional('disabled_plugins', default=plugin_list('')): Plugins,
                Optional('enabled_plugins', default=plugin_list('*')): Plugins,
                Optional('generated_date',
                         default=datetime.utcnow()
                                         .strftime("%a, %d %b %Y %H:%M:%S +0000")):
                    basestring,
                Optional('log_folder', default=abspath('dxr-logs-{tree}')):
                    AbsPath,
                Optional('workers', default=if_raises(NotImplementedError,
                                                      cpu_count,
                                                      1)):
                    WORKERS_VALIDATOR,
                Optional('skip_stages', default=[]): WhitespaceList,
                Optional('www_root', default=''): Use(lambda v: v.rstrip('/')),
                Optional('google_analytics_key', default=''): basestring,
                Optional('es_hosts', default='http://127.0.0.1:9200/'):
                    WhitespaceList,
                # A semi-random name, having the tree name and format version in it.
                Optional('es_index', default='dxr_{format}_{tree}_{unique}'):
                    basestring,
                Optional('es_alias', default='dxr_{format}_{tree}'):
                    basestring,
                Optional('es_catalog_index', default='dxr_catalog'):
                    basestring,
                Optional('es_catalog_replicas', default=1):
                    Use(int, error='"es_catalog_replicas" must be an integer.'),
                Optional('max_thumbnail_size', default=20000):
                    And(Use(int),
                        lambda v: v >= 0,
                        error='"max_thumbnail_size" must be a non-negative '
                              'integer.'),
                Optional('es_indexing_timeout', default=60):
                    And(Use(int),
                        lambda v: v >= 0,
                        error='"es_indexing_timeout" must be a non-negative '
                              'integer.'),
                Optional('es_indexing_retries', default=0):
                    And(Use(int),
                        lambda v: v >= 0,
                        error='"es_indexing_retries" must be a non-negative '
                              'integer.'),
                Optional('es_refresh_interval', default=60):
                    Use(int, error='"es_refresh_interval" must be an integer.')
            },
            basestring: dict
        })

        # Parse the ini into nested dicts:
        config_obj = ConfigObj(input.splitlines() if isinstance(input,
                                                                basestring)
                               else input,
                               list_values=False)

        if not relative_to:
            relative_to = getcwd()
        with cd(relative_to):
            try:
                config = schema.validate(config_obj.dict())
            except SchemaError as exc:
                raise ConfigError(exc.code, ['DXR'])

            self._section = config['DXR']

            # Normalize enabled_plugins:
            if self.enabled_plugins.is_all:
                # Then explicitly enable anything that isn't explicitly
                # disabled:
                self._section['enabled_plugins'] = [
                        p for p in all_plugins_but_core().values()
                        if p not in self.disabled_plugins]

            # Now that enabled_plugins and the other keys that TreeConfig
            # depends on are filled out, make some TreeConfigs:
            self.trees = OrderedDict()  # name -> TreeConfig
            for section in config_obj.sections:
                if section != 'DXR':
                    try:
                        self.trees[section] = TreeConfig(section,
                                                         config[section],
                                                         config_obj[section].sections,
                                                         self)
                    except SchemaError as exc:
                        raise ConfigError(exc.code, [section])

        # Make sure default_tree is defined:
        if not self.default_tree:
            self._section['default_tree'] = first(self.trees.iterkeys())

        # These aren't intended for actual use; they're just to influence
        # enabled_plugins of trees, and now we're done with them:
        del self._section['enabled_plugins']
        del self._section['disabled_plugins']
Пример #46
0
def test_scope_of_global():
    """Make sure the scope of a global is the entire program."""
    js = """a = 0;"""
    ast = parse(js)
    assignment = first(assignments(ast))
    eq_(assignment.scope_of(assignment['left']['name'])['type'], 'Program')