def __init__(self, *components, type, from_type=False, update=False):
        if update == True:
            from warnings import warn
            warn(
                "updating the Type with non-conformant values is not yet implemented",
                Warning)

        self.type = type
        from builtins import type
        if type(self.type) is not Type:
            self.type = Type(self.type)
            #raise TypeError("the type must be of class %s"%Type.__name__)
        if len(components) == 1: components = components[0]
        if not (hasattr(components, '__iter__')
                or hasattr(components, '__len__')):
            raise TypeError("must have at least 2 components")
        from collections.abc import Sequence
        if any(
                isinstance(o, Sequence) and not isinstance(o, str)
                for o in components):
            raise TypeError("Iterable components not allowed")

        if from_type == True:
            from builtins import tuple
            components = tuple(self.type.cats[i][ix]
                               for i, ix in enumerate(components))

        if not self.type.conforms(components):
            raise TypeError("Must conform to %s" % self.type.__str__())
        from builtins import tuple
        self._cats = tuple(
            cat.index(c) for c, cat in zip(components, self.type.cats))
        assert len(self._cats) == len(self.type.cats), "lengths do not match"
def RRT(startpos, endpos, obstacles, n_iter, radius, stepSize):
    # Insert kinematics to translate start position cartesian coordinates to angles of the arm

    startpos = tuple(np.concatenate((forward_kin(startpos), startpos)))
    endpos = tuple(np.concatenate((forward_kin(endpos), endpos)))

    G = Graph(startpos, endpos)

    for _ in range(n_iter):
        randangles = random_angle_config(endpos, 1, G)
        randvex = tuple(np.concatenate((forward_kin(randangles), randangles)))
        if isInObstacle(randvex, obstacles, radius):
            continue

        nearvex, nearidx = nearest(G, randvex, obstacles, radius)
        if nearvex is None:
            continue

        newvex = steer(randvex, nearvex, stepSize)

        newidx = G.add_vex(newvex)
        dist = distance(newvex, nearvex)
        G.add_edge(newidx, nearidx, dist)

        dist = distance(newvex, G.endpos)
        if dist < 2 * radius:
            endidx = G.add_vex(G.endpos)
            G.add_edge(newidx, endidx, dist)
            G.success = True
            # print('success')
            # break
    return G
def main():
    pass
    tp = Type((1, 2, 3), ('a', 'b'), (10, 20, 999), (1, float, int, list, str),
              eq_strict=True)
    tp2 = Type((1, 2, 3), ('a', 'b'), (10, 20, int(999.0)),
               (1, float, int, list, str))
    tp3 = Type((1, 2, 3), ('a', 'b'), [10, 20, 999])
    print(tp)

    b = tp.conforms([2, 'a', 10, int])
    print("conforms?:", b)

    b = tp == tp2
    print(b)

    t = Tuple([3, 'a', 10, list], type=tp)
    t2 = Tuple(3, 'a', int(999.0), list, type=tp)

    b = t == t2
    print("these Tuples are equal:", b)

    b = t < t2
    print(b)

    #TEST
    from calendar import month_abbr, day_abbr
    tp = Type(
        tuple(range(97, 100)) + tuple(['00', '01', '02', '03']),
        month_abbr[1:], day_abbr[:])
    print(repr(tp))

    t1 = Tuple(99, 'Feb', 'Mon', type=tp)
    t2 = Tuple(0, 1, 2, type=tp, from_type=True)

    from random import randint
    l = [
        Tuple([randint(0, n) for n in (len(cat) - 1 for cat in tp)],
              type=tp,
              from_type=True) for _ in range(10)
    ]
    l = sorted(l)
    mn, mx = min(l), max(l)
    print(l)
    print(mn, mx)
    print(l[0] == l[0], mn == mx)

    tp = [(10, 20, 30), (True, False), (None, str, list, int), day_abbr[:]]
    t = Tuple(10, True, str, 'Mon', type=tp)
    t = Tuple(0, 1, 2, 3, from_type=True, type=tp)
    print(t, t.type)

    tp = [[1, 2, 3, 10], [2, 3, 20], [5.0, 30]]

    l = [(3, 2, 5.0), (3, 2, 5), (10, 3, 5), (2, 20, 30)]
    print(id(l), l)
    l = sort_categorical_tuples(l)
    print(id(l), l)
Пример #4
0
def ts_arrays_to_bits(ts_xss: Tuple) -> Tuple:
    """
    Convert a list of sseqs of atoms to a tuple of tuple of bits.
    Each inner tuple of bits is one clock cycle
    Note: will be a list of atoms if each sseq is length 1. Should also work.
    """
    ts_flattened = []
    for s_xss in ts_xss:
        ts_flattened.append(builtins.tuple(atom_or_sseq_to_bits(s_xss)))
    return builtins.tuple(ts_flattened)
 def __repr__(self):
     length = max(len(cat) for cat in self.cats)
     n = max(len(str(e)) for cat in self.cats for e in cat)
     cats = (tuple(str(e).ljust(n)
                   for e in cat) + (' '.ljust(n), ) * (length - len(cat))
             for cat in self.cats)
     t = tuple(str.join(' | ', t) for t in zip(*cats))
     s = str.join('\n', t)
     s = "Type(\n" + s + " )"
     return s
Пример #6
0
def test_lut_11_13():
    width = 11
    numOut = 13
    T = Array[width, BitOut]
    init = [builtins.tuple(int2seq(i, width)) for i in range(4, numOut + 4)]

    testcircuit = DefineLUTAnyType(T, numOut, builtins.tuple(init))

    tester = fault.Tester(testcircuit, testcircuit.CLK)

    for i in range(numOut):
        tester.circuit.addr = i
        tester.eval()
        tester.circuit.data.expect(i + 4)
    compile_and_run(tester)
Пример #7
0
def tuple(arg, default=None):
    val_list = list(arg, default=default)

    if val_list is not None:
        return builtins.tuple(val_list)
    else:
        return val_list
Пример #8
0
def crawling_pelicana():
    results = []
    for page in count(start=113):
        url = 'https://pelicana.co.kr/store/stroe_search.html?branch_name=&gu=&si=&page=%d' % page
        html = crawler.crawling(url)
        # html = get_html(url)

        bs = BeautifulSoup(html, 'html.parser')

        # # ---- 1
        # trs = bs.table.findAll("tr")
        # for i in range(1, len(trs) - 1):
        #     td = trs[i].findAll('td')
        #     title = td[0].text
        #     addr = td[1].text
        #     call = td[2].text.strip()
        #     sidogu = addr.split()[:2]

        # ---- 2
        tag_table = bs.find('table', attrs={'class': 'table mt20'})
        tag_tbody = tag_table.find('tbody')
        tags_tr = tag_tbody.findAll('tr')

        # 끝 검출
        if len(tags_tr) == 0:
            break
        for tag_tr in tags_tr:
            strings = list(tag_tr.strings)
            name = strings[1]
            address = strings[3]
            sidogu = address.split()[:2]
            results.append((name, address) + tuple(sidogu))
Пример #9
0
def atom_or_sseq_to_bits(atom: Union[int, bool, Tuple]) -> Tuple:
    """
    This converts atoms or SSeqs of atoms to flat bits
    :return:
    """
    if type(atom) == int:
        return builtins.tuple(int2seq(atom, int_width))
    elif type(atom) == bool:
        if atom == True:
            return builtins.tuple([1])
        else:
            return builtins.tuple([0])
    elif isinstance(atom, Iterable):
        return builtins.tuple(ae_flatten([atom_or_sseq_to_bits(subatom) for subatom in atom]))
    else:
        raise NotImplementedError("Type {} not supported".format(str(type(atom))))
Пример #10
0
 def forgive(self, arg, level):
     if level == 1:
         if NotPassed(arg):
             return ()
         if not isinstance(arg, six.string_types):
             return builtins.tuple(arg)
     if level == 2:
         return (arg,)
Пример #11
0
def tuple(
    env_var: builtins.str,
    default: Optional[Tuple[Any, ...]] = None,
) -> Optional[Tuple[Any, ...]]:
    if env_var == "":
        return default

    return builtins.tuple(env_var.split(","))
Пример #12
0
    def enhance_contrast(self, img, method='HE', level=256, window_size=32, affect_size=16, blocks=8, threshold=10.0):
        """
        equalize the histogram
        :param img: Image type
        :param method: Histogram Equalization Method
        :param level: color or gray scale
        :param window_size: in AHE, the window to calculate Histogram CDF size
        :param affect_size: in AHE, the affected pixels size
        :param blocks: in CLAHE, split how many times in row and col
        :param threshold: in CLAHE, if threshold times higher than the mean, clip
        :return: equalized result
        """
        # choose algorithms
        if method in ['HE', 'FHE', 'he', 'fhe']:
            he_func = self.histogram_equalization  # HE
        elif method in ['AHE', 'ahe']:
            he_func = self.adaptive_histogram_equalization  # AHE
        elif method in ['CLANE', 'clane']:
            he_func = self.contrast_limited_adaptive_histogram_equalization  # CLAHE
        elif method in ['standard', 'STANDARD', 'Standard']:
            he_func = self.standard_histogram_equalization  # ImageOps HE
        elif method in ['Bright', 'bright', 'bright_level']:
            he_func = self.bright_wise_histogram_equalization  # Local Region Stretch
        else:
            he_func = self.histogram_equalization
            raise Exception("unknown method: ", method)

        # process gray and color images
        img_arr = np.array(img)
        if len(img_arr.shape) == 2:
            channel_num = 1
        elif len(img_arr.shape) == 3:
            channel_num = img_arr.shape[2]
        else:
            channel_num = 1
            raise Exception("image shape wrong")

        if channel_num == 1:
            # gray image
            arr = he_func(img_arr, level=level, window_size=window_size,
                          affect_size=affect_size, blocks=blocks, threshold=threshold)
            img_res = Image.fromarray(arr)
        elif channel_num == 3 or channel_num == 4:
            # RGB image or RGBA image(such as png)
            rgb_arr = [None] * 3
            rgb_img = [None] * 3
            # process dividely
            for k in range(3):
                rgb_arr[k] = he_func(img_arr[:, :, k], level=level, window_size=window_size,
                                     affect_size=affect_size, blocks=blocks, threshold=threshold)
                rgb_img[k] = Image.fromarray(rgb_arr[k])
            img_res = Image.merge("RGB", tuple(rgb_img))
        else:
            img_res = img
            raise Exception("The channel_num must be 1, 3 or 4")

        return img_res
Пример #13
0
def test_rom_11_13():
    width = 11
    numOut = 13
    T = Array[width, BitOut]
    init = [builtins.tuple(int2seq(i)) for i in range(4, numOut+4)]

    return
    testcircuit = DefineROMAnyType(T, width, builtins.tuple(init))

    tester = fault.Tester(testcircuit, testcircuit.CLK)

    tester.circuit.RE = True

    for i in range(numOut):
        tester.circuit.RADDR = i
        tester.step(2)
        tester.circuit.RDATA.expect(i + 4)
    compile_and_run(tester)
Пример #14
0
 def _validate(self, arg):
     if not self.valid(arg):
         raise ValidationError('Not a tuple')
     if self.value_spec:
         l = [validate(entry, self.value_spec) for entry in arg]
         if any(list_entry != tuple_entry for (list_entry, tuple_entry) in zip(l, arg)):
             return builtins.tuple(l)
         else:
             return arg
     else:
         return arg
Пример #15
0
 def __init__(self, *args: ty.Optional[int], elements: int = 1):
     s = builtins.slice(*args)
     start, stop, step = (
         s.start or 0,
         s.stop or builtins.min(s.stop, MAX_RANGE),
         s.step or 1,
     )
     self._it = builtins.tuple(
         builtins.iter(builtins.range(start, stop, step))
         for _ in builtins.range(elements)
     )
    def __init__(self, *args, **kwargs):
        if len(args) == 1: args = args[0]
        from collections.abc import Sequence
        if not all(
                isinstance(o, Sequence) and  # must be sequences
                not isinstance(o, str) and  # but not strings
                len(o) > 1 and  # of length greater than 1
                hasattr(o, '__getitem__') and  # must be subscribable
                len(o) == len(frozenset(o)) and  # must be all unique
                all(
                    isinstance(e, str) or not isinstance(e, Sequence)
                    for e in o) for o in args):
            raise TypeError(
                "all must be subscribable iterables of length > 1 with unique values"
            )

        self.cats = tuple(tuple(cat) for cat in args)  # unique categoricals
        key = (
            [key for key in kwargs.keys() if ('strict' in str(key).lower())]
            or ['nosuchkey'])[0]
        self.strict_eq = bool(kwargs.get(key, False))
 def __lt__(self, other):
     if not (isinstance(other, self.__class__) and
             (self.type == other.type)):
         raise TypeError(
             "Both must be of class {} and have the same type".format(
                 self.__class__.__name__))
     assert len(self) == len(other), "lengths do not match"
     g = zip(self._cats, other._cats)
     from builtins import filter
     fn = lambda t: t[0] != t[1]
     t = tuple(filter(fn, g))
     b = (not bool(t)) or (t[0][1] < t[0][0])
     return not b
Пример #18
0
def tuple(
    env_var: builtins.str,
    default: Optional[Tuple[Any, ...]] = None,
) -> Optional[Tuple[Any, ...]]:
    """
    Parse environment variable value into a tuple.

    Args:
        env_var (str): Name of desired environment variable.
        default (tuple, optional): Optional fallback value.

    Returns:
        tuple (optional): Environment variable typecast into a tuple.

    """
    if env_var == "":
        return default

    return builtins.tuple(env_var.split(","))
Пример #19
0
def crawling_goobne():
    url = 'http://www.goobne.co.kr/store/search_store.jsp'

    # 첫 페이지 로딩
    path = 'D:/bin/chromedriver/chromedriver.exe'
    wd = webdriver.Chrome(path)
    wd.get(url)
    time.sleep(3)

    results = []
    for page in count(start=1):
        # 자바스크립트 실행
        script = 'store.getList(%d)' % page
        wd.execute_script(script)
        print(f'{datetime.now()}: success for request [{script}]')
        time.sleep(1)

        # 실행 결과 HTML(동적으로 렌더링 된 HTML) 가져오기
        html = wd.page_source
        bs = BeautifulSoup(html, 'html.parser')
        tag_tbody = bs.find('tbody', attrs={'id': 'store_list'})
        tags_tr = tag_tbody.findAll('tr')

        # 끝 검출
        if tags_tr[0].get('class') is None:
            break

        for tag_tr in tags_tr:
            strings = list(tag_tr.strings)
            name = strings[1]
            addrs = strings[6]
            sidogu = addrs.split()[:2]

            results.append((name, addrs) + tuple(sidogu))

    # store
    # table = pd.DataFrame(results, columns=['name', 'address', 'sido', 'gu'])
    # table.to_csv('__results__/goobne.csv', encoding='utf-8', mode='w', index=0)
    wd.quit()
Пример #20
0
def crawling_kyochon():
    results = []
    for sido1 in range(1, 18):
        for sido2 in count(start=1):
            url = 'http://www.kyochon.com/shop/domestic.asp?sido1=%d&sido2=%d' % (
                sido1, sido2)
            html = crawler.crawling(url)

            # 끝 검출
            if html is None:
                break

            bs = BeautifulSoup(html, 'html.parser')
            tag_ul = bs.find('ul', attrs={'class': 'list'})
            tags_span = tag_ul.findAll('span', attrs={'class': 'store_item'})

            for tag_span in tags_span:
                strings = list(tag_span.strings)
                name = strings[1]
                addrs = strings[3].strip()
                sidogu = addrs.split()[:2]
                results.append((name, addrs) + tuple(sidogu))
Пример #21
0
def tuple(name, default=None, allow_none=False, separator=","):
    """Get a tuple of strings or the default.

    The individual list elements are whitespace-stripped.

    Args:
        name: The environment variable name
        default: The default value to use if no environment variable is found
        allow_none: If the return value can be `None` (i.e. optional)
        separator: The list item separator character or pattern
    """
    try:
        value = read(name, default, allow_none)
        if isinstance(value, builtins.tuple):
            return value
        elif isinstance(value, builtins.str):
            return builtins.tuple(_str_to_list(value, separator))
        elif value is None and allow_none:
            return None
        else:
            return (builtins.str(value),)
    except ValueError:
        raise ValueError("Invalid tuple varible.")
Пример #22
0
    def __init__(self, low, high, shape=None, dtype=np.float32):
        # type casting low and high parameters (first two parameters of box) with float32
        low = np.float32(low)
        high = np.float32(high)
        assert dtype is not None, 'dtype must be explicitly provided. '
        self.dtype = np.dtype(dtype)

        if shape is None:
            assert low.shape == high.shape, 'box dimension mismatch. '
            self.shape = low.shape
            self.low = low
            self.high = high
        else:
            assert np.isscalar(low) and np.isscalar(high), 'box requires scalar bounds. '
            self.shape = tuple(shape)
            self.low = np.full(self.shape, low)
            self.high = np.full(self.shape, high)

        def _get_precision(dtype):
            if np.issubdtype(dtype, np.floating):
                return np.finfo(dtype).precision
            else:
                return np.inf
        low_precision = _get_precision(self.low.dtype)
        high_precision = _get_precision(self.high.dtype)
        dtype_precision = _get_precision(self.dtype)
        if min(low_precision, high_precision)> dtype_precision:
            logger.warn("Box bound precision lowered by casting to {}".format(self.dtype))
        self.low = self.low.astype(self.dtype)
        self.high = self.high.astype(self.dtype)

        # Boolean arrays which indicate the interval type for each coordinate
        self.bounded_below = -np.inf < self.low
        self.bounded_above = np.inf > self.high

        super(Box, self).__init__(self.shape, self.dtype)
Пример #23
0
def crawling_nene():
    results = []
    cnt = 0
    # for page in count(start=1):
    for page in range(1, 3):
        url = 'https://nenechicken.com/17_new/sub_shop01.asp?ex_select=1&ex_select2=&IndexSword=&GUBUN=A&page=%d' % page
        html = get_html(url)

        bs = BeautifulSoup(html, 'html.parser')

        divs = bs.select('.shopWrap > .shop')
        for info in divs:
            info_list = info.select('td')
            name = info_list[0].select('.shopName')[0].text
            addr = info_list[0].select('.shopAdd')[0].text
            sidogu = addr.split()[:2]
            call = info_list[1].a['href'].split(':')[1]

            results.append((name, addr, call) + tuple(sidogu))

        if page == 1:
            cnt = len(divs)
        elif len(divs) != cnt:
            break
Пример #24
0
from builtins import tuple

if __name__ == '__main__':
    n = int(input())
    integer_list = map(int, input().split())
    t = tuple(integer_list)
    print(hash(t))
Пример #25
0
 def constructor(value):
   return builtins.tuple(map(itemtype, value))
Пример #26
0
import codecs
import sys
from builtins import int, sorted, tuple, str, set

file_path = sys.argv[1]
maps_sort_path = file_path.replace('.txt', '_sort')
maps = set()
with codecs.open(file_path, encoding='utf-8') as f1:
    for line in f1:
        fileds = line.strip().split('\t')
        if str(fileds[1].strip()) is not "":
            # if line.strip() not in maps:
            maps.add((fileds[0], fileds[1], fileds[2]))
with codecs.open(maps_sort_path, 'w', encoding='utf-8') as f_false:
    for f in sorted(maps, key=lambda x: (x[0], -int(x[2]))):
        s2 = "\t".join(tuple(f))
        f_false.write(s2)
        f_false.write('\n')
print("Finish Line")
Пример #27
0
#tuple
from builtins import tuple

a=()
b=tuple()
print("type a",type(a),"type b",type(b))
a=(1,2,4,5,6,7)
print(a)
print(a[1])
#a[1]=1 ->tuple da değer ataması yapılmaz
yazi="selam nası gidiyo  "
yeniyazi=tuple(yazi)
print(yeniyazi)
print(yeniyazi.index("l"))
print(yeniyazi.count(" "))
print(yeniyazi.__contains__("x")) # x elemeanını bulundurmuyor
Пример #28
0
        def definition(cls):
            # first section creates the RAMs and LUTs that set values in them and the sorting network
            shared_and_diff_subtypes = get_shared_and_diff_subtypes(
                t_in, t_out)
            t_in_diff = shared_and_diff_subtypes.diff_input
            t_out_diff = shared_and_diff_subtypes.diff_output
            graph = build_permutation_graph(ST_TSeq(2, 0, t_in_diff),
                                            ST_TSeq(2, 0, t_out_diff))
            banks_write_addr_per_input_lane = get_banks_addr_per_lane(
                graph.input_nodes)
            input_lane_write_addr_per_bank = get_lane_addr_per_banks(
                graph.input_nodes)
            output_lane_read_addr_per_bank = get_lane_addr_per_banks(
                graph.output_nodes)

            # each ram only needs to be large enough to handle the number of addresses assigned to it
            # all rams receive the same number of writes
            # but some of those writes don't happen as the data is invalid, so don't need storage for them
            max_ram_addrs = [
                max([bank_clock_data.addr for bank_clock_data in bank_data])
                for bank_data in output_lane_read_addr_per_bank
            ]
            # rams also handle parallelism from outer_shared type as this affects all banks the same
            outer_shared_sseqs = remove_tseqs(
                shared_and_diff_subtypes.shared_outer)
            if outer_shared_sseqs == ST_Tombstone():
                ram_element_type = shared_and_diff_subtypes.shared_inner
            else:
                ram_element_type = replace_tombstone(
                    outer_shared_sseqs, shared_and_diff_subtypes.shared_inner)
            # can use wider rams rather than duplicate for outer_shared_sseqs because will
            # transpose dimenions of input wires below to wire up as if outer, shared dimensions
            # were on the inside
            rams = [
                DefineRAM_ST(ram_element_type, ram_max_addr + 1)()
                for ram_max_addr in max_ram_addrs
            ]
            rams_addr_widths = [ram.WADDR.N for ram in rams]

            # for bank, the addresses to write to each clock
            write_addr_for_bank_luts = []
            for bank_idx in range(len(rams)):
                ram_addr_width = rams_addr_widths[bank_idx]
                num_addrs = len(input_lane_write_addr_per_bank[bank_idx])
                #assert num_addrs == t_in_diff.time()
                write_addrs = [
                    builtins.tuple(
                        int2seq(write_data_per_bank_per_clock.addr,
                                ram_addr_width))
                    for write_data_per_bank_per_clock in
                    input_lane_write_addr_per_bank[bank_idx]
                ]
                write_addr_for_bank_luts.append(
                    DefineLUTAnyType(Array[ram_addr_width, Bit], num_addrs,
                                     builtins.tuple(write_addrs))())

            # for bank, whether to actually write this clock
            write_valid_for_bank_luts = []
            for bank_idx in range(len(rams)):
                num_valids = len(input_lane_write_addr_per_bank[bank_idx])
                #assert num_valids == t_in_diff.time()
                valids = [
                    builtins.tuple([write_data_per_bank_per_clock.valid])
                    for write_data_per_bank_per_clock in
                    input_lane_write_addr_per_bank[bank_idx]
                ]
                write_valid_for_bank_luts.append(
                    DefineLUTAnyType(Bit, num_valids,
                                     builtins.tuple(valids))())

            # for each input lane, the bank to write to each clock
            write_bank_for_input_lane_luts = []
            bank_idx_width = getRAMAddrWidth(len(rams))
            for lane_idx in range(len(banks_write_addr_per_input_lane)):
                num_bank_idxs = len(banks_write_addr_per_input_lane[lane_idx])
                #assert num_bank_idxs == t_in_diff.time()
                bank_idxs = [
                    builtins.tuple(
                        int2seq(write_data_per_lane_per_clock.bank,
                                bank_idx_width))
                    for write_data_per_lane_per_clock in
                    banks_write_addr_per_input_lane[lane_idx]
                ]
                write_bank_for_input_lane_luts.append(
                    DefineLUTAnyType(Array[bank_idx_width, Bit], num_bank_idxs,
                                     builtins.tuple(bank_idxs))())

            # for each bank, the address to read from each clock
            read_addr_for_bank_luts = []
            for bank_idx in range(len(rams)):
                ram_addr_width = rams_addr_widths[bank_idx]
                num_addrs = len(output_lane_read_addr_per_bank[bank_idx])
                #assert num_addrs == t_in_diff.time()
                read_addrs = [
                    builtins.tuple(
                        int2seq(read_data_per_bank_per_clock.addr,
                                ram_addr_width))
                    for read_data_per_bank_per_clock in
                    output_lane_read_addr_per_bank[bank_idx]
                ]
                read_addr_for_bank_luts.append(
                    DefineLUTAnyType(Array[ram_addr_width, Bit], num_addrs,
                                     builtins.tuple(read_addrs))())

            # for each bank, the lane to send each read to
            output_lane_for_bank_luts = []
            # number of lanes equals number of banks
            # some the lanes are just always invalid, added so input lane width equals output lane width
            lane_idx_width = getRAMAddrWidth(len(rams))
            for bank_idx in range(len(rams)):
                num_lane_idxs = len(output_lane_read_addr_per_bank[bank_idx])
                #assert num_lane_idxs == t_in_diff.time()
                lane_idxs = [
                    builtins.tuple(
                        int2seq(read_data_per_bank_per_clock.s,
                                lane_idx_width))
                    for read_data_per_bank_per_clock in
                    output_lane_read_addr_per_bank[bank_idx]
                ]
                output_lane_for_bank_luts.append(
                    DefineLUTAnyType(Array[lane_idx_width, Bit], num_lane_idxs,
                                     builtins.tuple(lane_idxs))())

            # second part creates the counters that index into the LUTs
            # elem_per counts time per element of the reshape
            elem_per_reshape_counter = AESizedCounterModM(
                ram_element_type.time(), has_ce=True)
            end_cur_elem = Decode(ram_element_type.time() - 1,
                                  elem_per_reshape_counter.O.N)(
                                      elem_per_reshape_counter.O)
            # reshape counts which element in the reshape
            num_clocks = len(output_lane_read_addr_per_bank[0])
            reshape_write_counter = AESizedCounterModM(num_clocks,
                                                       has_ce=True,
                                                       has_reset=has_reset)
            reshape_read_counter = AESizedCounterModM(num_clocks,
                                                      has_ce=True,
                                                      has_reset=has_reset)

            output_delay = (
                get_output_latencies(graph)[0]) * ram_element_type.time()
            # this is present so testing knows the delay
            cls.output_delay = output_delay
            reshape_read_delay_counter = DefineInitialDelayCounter(
                output_delay, has_ce=True, has_reset=has_reset)()
            # outer counter the repeats the reshape
            #wire(reshape_write_counter.O, cls.reshape_write_counter)

            enabled = DefineCoreirConst(1, 1)().O[0]
            if has_valid:
                enabled = cls.valid_up & enabled
                wire(reshape_read_delay_counter.valid, cls.valid_down)
            if has_ce:
                enabled = bit(cls.CE) & enabled
            wire(enabled, elem_per_reshape_counter.CE)
            wire(enabled, reshape_read_delay_counter.CE)
            wire(enabled & end_cur_elem, reshape_write_counter.CE)
            wire(enabled & end_cur_elem & reshape_read_delay_counter.valid,
                 reshape_read_counter.CE)

            if has_reset:
                wire(cls.RESET, elem_per_reshape_counter.RESET)
                wire(cls.RESET, reshape_read_delay_counter.RESET)
                wire(cls.RESET, reshape_write_counter.RESET)
                wire(cls.RESET, reshape_read_counter.RESET)

            # wire read and write counters to all LUTs
            for lut in write_bank_for_input_lane_luts:
                wire(reshape_write_counter.O, lut.addr)

            for lut in write_addr_for_bank_luts:
                wire(reshape_write_counter.O, lut.addr)

            for lut in write_valid_for_bank_luts:
                wire(reshape_write_counter.O, lut.addr)

            for lut in read_addr_for_bank_luts:
                wire(reshape_read_counter.O, lut.addr)

            for lut in output_lane_for_bank_luts:
                wire(reshape_read_counter.O, lut.addr)

            # third and final instance creation part creates the sorting networks that map lanes to banks
            input_sorting_network_t = Tuple(
                bank=Array[write_bank_for_input_lane_luts[0].data.N, Bit],
                val=ram_element_type.magma_repr())
            input_sorting_network = DefineBitonicSort(input_sorting_network_t,
                                                      len(rams),
                                                      lambda x: x.bank)()

            output_sorting_network_t = Tuple(
                lane=Array[output_lane_for_bank_luts[0].data.N, Bit],
                val=ram_element_type.magma_repr())
            output_sorting_network = DefineBitonicSort(
                output_sorting_network_t, len(rams), lambda x: x.lane)()

            # wire luts, sorting networks, inputs, and rams
            # flatten all the sseq_layers to get flat magma type of inputs and outputs
            # tseqs don't affect magma types
            num_sseq_layers_inputs = num_nested_layers(
                remove_tseqs(shared_and_diff_subtypes.diff_input))
            num_sseq_layers_to_remove_inputs = max(0,
                                                   num_sseq_layers_inputs - 1)
            num_sseq_layers_outputs = num_nested_layers(
                remove_tseqs(shared_and_diff_subtypes.diff_output))
            num_sseq_layers_to_remove_outputs = max(
                0, num_sseq_layers_outputs - 1)
            if remove_tseqs(
                    shared_and_diff_subtypes.shared_outer) != ST_Tombstone():
                #num_sseq_layers_inputs += num_nested_layers(remove_tseqs(shared_and_diff_subtypes.shared_outer))
                #num_sseq_layers_outputs += num_nested_layers(remove_tseqs(shared_and_diff_subtypes.shared_outer))
                input_ports = flatten_ports(
                    transpose_outer_dimensions(
                        shared_and_diff_subtypes.shared_outer,
                        shared_and_diff_subtypes.diff_input, cls.I),
                    num_sseq_layers_to_remove_inputs)
                output_ports = flatten_ports(
                    transpose_outer_dimensions(
                        shared_and_diff_subtypes.shared_outer,
                        shared_and_diff_subtypes.diff_output, cls.O),
                    num_sseq_layers_to_remove_outputs)
            else:
                input_ports = flatten_ports(cls.I,
                                            num_sseq_layers_to_remove_inputs)
                output_ports = flatten_ports(
                    cls.O, num_sseq_layers_to_remove_outputs)
            # this is only used if the shared outer layers contains any sseqs
            sseq_layers_to_flatten = max(
                num_nested_layers(
                    remove_tseqs(shared_and_diff_subtypes.shared_outer)) - 1,
                0)
            for idx in range(len(rams)):
                # wire input and bank to input sorting network
                wire(write_bank_for_input_lane_luts[idx].data,
                     input_sorting_network.I[idx].bank)
                #if idx == 0:
                #    wire(cls.first_valid, write_valid_for_bank_luts[idx].data)
                if idx < t_in_diff.port_width():
                    # since the input_ports are lists, need to wire them individually to the sorting ports
                    if remove_tseqs(shared_and_diff_subtypes.shared_outer
                                    ) != ST_Tombstone():
                        cur_input_port = flatten_ports(input_ports[idx],
                                                       sseq_layers_to_flatten)
                        cur_sort_port = flatten_ports(
                            input_sorting_network.I[idx].val,
                            sseq_layers_to_flatten)
                        for i in range(len(cur_input_port)):
                            wire(cur_input_port[i], cur_sort_port[i])
                    else:
                        if num_sseq_layers_inputs == 0:
                            # input_ports will be an array of bits for 1 element
                            # if no sseq in t_in
                            wire(input_ports, input_sorting_network.I[idx].val)
                        else:
                            wire(input_ports[idx],
                                 input_sorting_network.I[idx].val)
                    #wire(cls.ram_wr, input_sorting_network.O[idx].val)
                    #wire(cls.ram_rd, rams[idx].RDATA)
                else:
                    zero_const = DefineCoreirConst(
                        ram_element_type.magma_repr().size(), 0)().O
                    cur_sn_input = input_sorting_network.I[idx].val
                    while len(cur_sn_input) != len(zero_const):
                        cur_sn_input = cur_sn_input[0]
                    wire(zero_const, cur_sn_input)

                # wire input sorting network, write addr, and write valid luts to banks
                wire(input_sorting_network.O[idx].val, rams[idx].WDATA)
                wire(write_addr_for_bank_luts[idx].data, rams[idx].WADDR)
                #wire(write_addr_for_bank_luts[idx].data[0], cls.addr_wr[idx])
                if has_ce:
                    wire(write_valid_for_bank_luts[idx].data & bit(cls.CE),
                         rams[idx].WE)
                else:
                    wire(write_valid_for_bank_luts[idx].data, rams[idx].WE)

                # wire output sorting network, read addr, read bank, and read enable
                wire(rams[idx].RDATA, output_sorting_network.I[idx].val)
                wire(output_lane_for_bank_luts[idx].data,
                     output_sorting_network.I[idx].lane)
                wire(read_addr_for_bank_luts[idx].data, rams[idx].RADDR)
                #wire(read_addr_for_bank_luts[idx].data[0], cls.addr_rd[idx])
                # ok to read invalid things, so in read value LUT
                if has_ce:
                    wire(bit(cls.CE), rams[idx].RE)
                else:
                    wire(DefineCoreirConst(1, 1)().O[0], rams[idx].RE)
                if has_reset:
                    wire(cls.RESET, rams[idx].RESET)

                # wire output sorting network value to output or term
                if idx < t_out_diff.port_width():
                    # since the output_ports are lists, need to wire them individually to the sorting ports
                    if remove_tseqs(shared_and_diff_subtypes.shared_outer
                                    ) != ST_Tombstone():
                        cur_output_port = flatten_ports(
                            output_ports[idx], sseq_layers_to_flatten)
                        cur_sort_port = flatten_ports(
                            output_sorting_network.O[idx].val,
                            sseq_layers_to_flatten)
                        for i in range(len(cur_output_port)):
                            wire(cur_output_port[i], cur_sort_port[i])
                    else:
                        if num_sseq_layers_outputs == 0:
                            # output_ports will be an array of bits for 1 element
                            # if no sseq in t_out
                            wire(output_sorting_network.O[idx].val,
                                 output_ports)
                        else:
                            wire(output_sorting_network.O[idx].val,
                                 output_ports[idx])
                else:
                    wire(output_sorting_network.O[idx].val,
                         TermAnyType(type(output_sorting_network.O[idx].val)))

                # wire sorting networks bank/lane to term as not used on outputs, just used for sorting
                wire(input_sorting_network.O[idx].bank,
                     TermAnyType(type(input_sorting_network.O[idx].bank)))
                wire(output_sorting_network.O[idx].lane,
                     TermAnyType(type(output_sorting_network.O[idx].lane)))
Пример #29
0
 async def __anext__(self) -> ty.Tuple[int, ...]:
     try:
         return builtins.tuple([builtins.next(x) for x in self._it])
     except StopIteration:
         raise StopAsyncIteration
Пример #30
0
 def __call__(*args, **kwargs):
   return builtins.tuple(*args, **kwargs)
Пример #31
0
class BasePlugin(oa.conf.Conf, object):
    """Abstract class for plugins. All plugins must inherit from this class.

    This exposes methods to methods to store data and configuration options
    in the "global" context and the "local" context.

     * The "global" context is loaded once when the configuration is parsed
       and persists throughout until the plugin is reloaded.
     * The "local" context is stored per message and each new message parsed
       has its one context.

    The methods automatically stores the data under the plugin names to ensure
    that there are no name clashes between plugins.

    The plugin can also define eval rules by implementing a method and adding
    it to the eval_rules list. These will be registered after the plugin has
    been initialized.
    """
    eval_rules = tuple()
    # Defines any new rules that the plugins implements.
    cmds = None
    # See oa.conf.Conf for details on options.
    options = None
    # The name of the DSN options
    dsn_name = None

    def __init__(self, ctxt):
        if self.dsn_name:
            self.options[self.dsn_name + "_dsn"] = ("str", "")
            self.options[self.dsn_name + "_sql_username"] = ("str", "")
            self.options[self.dsn_name + "_sql_password"] = ("str", "")
        self.path_to_plugin = None
        super(BasePlugin, self).__init__(ctxt)

    def finish_parsing_start(self, results):
        """Called when the configuration parsing has finished but before
        the has actually been initialized from the parsed data.

        This can be used to insert new data after parsing.

        :param results: A dictionary that maps the rule names to the
          rest of the data extracted from the configuration (e.g. the
          score, description etc.)
        :return: Nothing

        """

    # XXX The name method for this is horrible, but it's likely better to have
    # XXX it the same as SA.
    def finish_parsing_end(self, ruleset):
        """Called when the configuration parsing has finished, but before the
        post-parsing. This hook can be used for e.g. to add rules to the
        ruleset.

        By default this prepares the SQLAlchemy engine if the plugin has any
        set.
        """
        connect_string = None
        self["engine"] = None
        if self.dsn_name:
            dsn = self[self.dsn_name + "_dsn"]
            if dsn.upper().startswith("DBI"):
                # Convert from SA format.
                user = self[self.dsn_name + "_sql_username"]
                password = self[self.dsn_name + "_sql_password"]
                if not create_engine:
                    self["engine"] = dbi_to_mysql(dsn, user, password)
                else:
                    connect_string = dbi_to_alchemy(dsn, user, password)
            elif dsn:
                # The connect string is already in the correct format
                connect_string = dsn
        if connect_string is not None:
            self["engine"] = create_engine(connect_string)

    def get_engine(self):
        return self["engine"]

    def get_session(self):
        """Open a new SQLAlchemy session."""
        engine = self["engine"]
        return sessionmaker(bind=engine)()

    def check_start(self, msg):
        """Called before the metadata is extracted from the message. The
        message object passed will only have raw_msg and msg available.

        May be overridden.
        """

    def extract_metadata(self, msg, payload, text, part):
        """Called while the message metadata is extracted for every message
        part. If the part contains text, corresponding payload is provided,
        else it will be None.

        May be overridden.
        """

    def parsed_metadata(self, msg):
        """The message has been parsed and all the information can be accessed
        by the plugin.

        May be overridden.
        """

    def check_end(self, ruleset, msg):
        """The message check operation has just finished, and the results are
        about to be returned to the caller

        May be overridden.
        """

    def auto_learn_discriminator(self, ruleset, msg):
        """All message operations have finished and it can be checked for
        submission to autolearning systems

        May be overridden.
        """

    def plugin_report(self, msg):
        """Called when a message should be reported as spam.

        May be overridden.
        """

    def plugin_revoke(self, msg):
        """Called when a message should be reported as ham.

        May be overridden.
        """

    def parse_config(self, key, value):
        """Parse a config line that the normal parses doesn't know how to
        interpret.

        Use self.inhibit_further_callbacks to stop other plugins from
        processing this line.

        May be overridden.
        """
        super(BasePlugin, self).parse_config(key, value)