Beispiel #1
0
    def generateLineViews(self, grid):
        allViews = {}
        viewH = [''.join(x) for x in grid]
        allViews['h'] = viewH
        allViews['h-'] = [x[::-1] for x in viewH]

        # print(viewH)
        viewV = [''.join(x) for x in zip(*viewH)]
        allViews['v'] = viewV
        allViews['v-'] = [x[::-1] for x in viewV]

        # print(viewV)
        viewDR = defaultlist(str)
        viewDL = defaultlist(str)

        height = len(viewH)
        width = len(viewV)
        for i in range(height):
            first = height - i - 1
            for j in range(width):
                viewDR[first + j] += viewH[i][j]
                viewDL[i + j] += viewH[i][j]

        # print(viewDR)
        # print(viewDL)
        allViews['dr'] = viewDR
        allViews['dr-'] = [x[::-1] for x in viewDR]
        allViews['dl'] = viewDL
        allViews['dl-'] = [x[::-1] for x in viewDL]

        self.allViews = allViews
        if self.requestViews:
            self.views = {key: allViews[key] for key in self.requestViews}
        else:
            self.views = allViews
Beispiel #2
0
 def __init__(self, scope, stream_uuid):
     self.scope = scope
     self.uuid = stream_uuid
     self.clocks = defaultlist.defaultlist(lambda: Clock(self))
     self.activities = defaultlist.defaultlist(lambda: Activity(self))
     self.activities[0].verb = '<root>'
     self.activities[0].idx = scope.make_idx()
     self.activities[0].clock = self.clocks[0]
Beispiel #3
0
    def cleanup(self) -> None:
        if not ArgumentParser.args.cleanup:
            log.error('Cleanup called but --cleanup flag not provided at startup - ignoring call')
            return

        log.info('Notifying plugins to plan cleanup')
        dispatch_event(Event(EventType.CLEANUP_PLAN, self.graph), blocking=True)
        log.info('Running cleanup')
        dispatch_event(Event(EventType.CLEANUP_BEGIN, self.graph), blocking=True)
        with self.graph.lock.read_access:
            cleanup_nodes = [node for node in self.graph.nodes() if node.clean]
            cleanup_plan = defaultlist(lambda: [])

            for node in cleanup_nodes:
                log.debug(f'Adding {node.resource_type} {node.dname} to cleanup plan with priority {node.max_graph_depth}')
                cleanup_plan[node.max_graph_depth].append(node)

            with ThreadPoolExecutor(max_workers=ArgumentParser.args.cleanup_pool_size, thread_name_prefix='pre_cleaner') as executor:
                executor.map(self.pre_clean, cleanup_nodes)

            for nodes in reversed(cleanup_plan):
                with ThreadPoolExecutor(max_workers=ArgumentParser.args.cleanup_pool_size, thread_name_prefix='cleaner') as executor:
                    executor.map(self.clean, nodes)

        dispatch_event(Event(EventType.CLEANUP_FINISH, self.graph))
Beispiel #4
0
    def __init__(self, program):
        self.program = defaultlist(lambda: 0) + program
        self.cursor = 0
        self.relative_base = 0

        self.output_count = 0
        self.outputs = []
Beispiel #5
0
def format_lists():
    """Transforms ordered and unordered lists into markdown-syntax."""
    ordered_list_history = defaultlist.defaultlist(lambda: 1)
    sequential_empty_lines = 0
    line = ''
    while True:
        line = yield line
        list_item_match = re.match(r'^\s*([-|\+])\s', line)
        if list_item_match:
            i = list_item_match.start(1)
            if line[i] == '-':
                # Un-ordered lists have the same format in markdown.
                pass
            else:
                # Ordered lists must change to the actual number.
                line = f'{line[:i]}{ordered_list_history[i]}.{line[i + 1:]}'
                ordered_list_history[i] += 1
            # Reset numbering of sub-items.
            del ordered_list_history[i + 1:]
        elif line.strip():
            sequential_empty_lines = 0
            ordered_list_history.clear()
        else:
            sequential_empty_lines += 1
            if sequential_empty_lines >= 2:
                ordered_list_history.clear()
Beispiel #6
0
 def __init__(self, intcode: list):
     self.ic = defaultlist(lambda: 0)
     self.ic.extend(intcode)
     self.pos = 0
     self.rb = 0
     self.running = True
     self.q = Queue()
Beispiel #7
0
 def __init__(self, client, data):
     self.client = client
     self.program = client.program
     data = str(data)
     self.opcode = OpCode(int(data[-2:]))
     self.modes = defaultlist(lambda: Mode(0)) + [
         Mode(int(x)) for x in reversed(data[:-2])
     ]
Beispiel #8
0
 def __init__(self):
     self._program = defaultlist(lambda: 0)
     self._program[0] = 99
     self._status = IntCodeStatus.UNINITIALIZED
     self._input = deque()
     self._output_buffer = list()
     self._program_counter = 0
     self._relative_base_offset = 0
Beispiel #9
0
 def __init__(self, program, id_):
     self.program = defaultlist(lambda: 0) + program
     self.id = id_
     self.cursor = 0
     self.relative_base = 0
     self.input_count = 0
     self.start_count = 0
     self.running = True
def test_len():
    """Length."""
    l = defaultlist()
    eq_(len(l), 0)
    l[2] = "C"
    eq_(len(l), 3)
    l[4]
    eq_(len(l), 5)
def test_simple():
    """Simple scenario."""
    l = defaultlist()
    eq_(l, [])
    l[2] = "C"
    eq_(l, [None, None, 'C'])
    l[4]
    eq_(l, [None, None, 'C', None, None])
Beispiel #12
0
 def add_event_list(
     self, e: "List[EventType]", dashed_links: Optional[List[bool]] = None
 ):
     dash = defaultlist(lambda: self.dash_by_default)
     if dashed_links:
         for i, b in enumerate(dashed_links):
             dash[i] = b
     for j in range(len(e)):
         self.add_event(e[j], dash[j - 1], dash[j])
Beispiel #13
0
def exp3():
    tuf_table_file_name = 'all_maxs.csv'
    all_alarms = tuf_table_csv_to_df(
        os.path.join(PROJECT_ROOT, "csvs", tuf_table_file_name))
    australia_alarms = tuf_table_csv_to_df(
        os.path.join(PROJECT_ROOT, "csvs", "australia.csv"))

    m1, s1 = torch.serialization.load(os.path.join(PROJECT_ROOT, 'means.pt')), \
             torch.serialization.load(os.path.join(PROJECT_ROOT, 'stds.pt'))

    australia_ad = AlarmDataset(australia_alarms,
                                DATA_ROOT,
                                transform=transforms.Compose(
                                    [Normalize(m1, s1)]))
    australia_adl = DataLoader(australia_ad,
                               BATCH_SIZE,
                               shuffle=False,
                               num_workers=4)

    f = open(os.path.join(LOGS_PATH, "loss.csv"), "w+")

    nets = defaultlist(lambda: torch.nn.DataParallel(GPR_15_300()))
    for overtrain in range(100):
        for i, (strat_splits, _rgn_train, rgn_holdout) in enumerate(
                region_and_stratified(all_alarms,
                                      n_splits_stratified=N_STRAT_SPLITS)):

            net = nets[i]
            # main training loop
            for j, (alarm_strat_train,
                    alarm_strat_holdout) in enumerate(strat_splits):
                # train
                strat_train_ad = AlarmDataset(alarm_strat_train,
                                              DATA_ROOT,
                                              transform=transforms.Compose(
                                                  [Normalize(m1, s1)]))
                strat_train_adl = DataLoader(strat_train_ad,
                                             BATCH_SIZE,
                                             SHUFFLE_DL,
                                             num_workers=4)

                optim = OPTIMIZER(net)
                sched = SCHEDULER(optim, strat_train_adl)
                for k, _ in enumerate(
                        train(net,
                              strat_train_adl,
                              criterion=CRITERION,
                              optimizer=optim,
                              scheduler=sched,
                              epochs=EPOCHS)):
                    _roc, auc, _all_labels, _confs, loss = test(
                        net, australia_adl, CRITERION)

                    f.write(f"{i}, {j}, {k}, {auc}, {loss}\n")
                    f.flush()
                    print(f"done with {i} {j} {k}  auc: {auc} loss: {loss}")
def test_op():
    """default operations."""
    l = defaultlist()
    l[2] = "C"
    l[4]
    eq_(l, [None, None, 'C', None, None])
    l.insert(3, 'D')
    eq_(l, [None, None, 'C', 'D', None, None])
    l.remove(None)
    eq_(l, [None, 'C', 'D', None, None])
    def __init__(self, memory):
        self.memory = defaultlist(lambda: 0)
        for i, entry in enumerate(memory):
            self.memory[i] = entry
        self.pointer = 0
        self.relative_base = 0

        self.inputs = None
        self.outputs = []
        self.complete = False
def test_slice():
    """Slice selection."""
    l = defaultlist()
    l += list(range(5))
    eq_(l, [0, 1, 2, 3, 4])
    c = l[1:]
    eq_(c, [1, 2, 3, 4])
    c = l[1:-1]
    eq_(c, [1, 2, 3])
    c[5] = 'c0fe'
    eq_(c, [1, 2, 3, None, None, 'c0fe'])
def test_copy():
    """copy()."""
    l = defaultlist()
    l[2] = "C"
    l[4]
    eq_(l, [None, None, 'C', None, None])
    c = l.copy()
    eq_(c, [None, None, 'C', None, None])
    assert isinstance(c, defaultlist)
    c[6] = 'c0fe'
    eq_(c, [None, None, 'C', None, None, None, 'c0fe'])
Beispiel #18
0
def parse_table(table):
    grid = defaultlist(defaultlist)
    row_class = defaultlist()
    cur_row = 0
    cur_col = 0
    seen_cols = False
    for line in table:
        xline = line.rstrip()
        if xline.startswith('|}'):
            break
        if xline.startswith('|-'):
            cur_col = 0
            if seen_cols:
                cur_row += 1
            seen_cols = False
            m = re_attribs.search(line)
            if m:
                assert m.group(1) == 'class'
                row_class[cur_row] = m.group(2)
        elif xline.startswith('|') or xline.startswith('!'):
            seen_cols = True
            if '||' in line or '!!' in line:
                cols = re_col.split(xline[1:])
                # if len(grid) <= cur_row:
                #     grid += [None] * ((cur_row - len(grid)) + 1)

                for num, i in enumerate(cols):
                    set_grid(grid, cur_row, cur_col + num, i)
                cur_col += num
            else:
                # if len(grid) <= cur_row:
                #     grid += [None] * ((cur_row - len(grid)))

                set_grid(grid, cur_row, cur_col, line[1:])
                cur_col += 1

    return (row_class, grid)

    for i, row in enumerate(grid):
        print(len(row))
        print('grid:', i, row)
def update_rating(source_file, output_file, is_pair):
    """
    Update rating using information from results table in csv format
    """
    row_count = 0
    results = defaultlist(list)
    with open(source_file, 'r') as csvfile:
        reader = csv.reader(csvfile, delimiter=',')
        for row in reader:
            results[row_count] = row
            row_count += 1
    start_column = 3

    if is_pair:
        start_column += 3

    curr_rating_column = 1
    is_first_line = True
    for row in results:
        if is_first_line:
            is_first_line = False
            row.append("New rating")
            continue
        curr_rating = float(row[curr_rating_column])
        new_rating = curr_rating
        curr_name = row[curr_rating_column + 1]
        if is_pair:
            curr_name += '+' + row[curr_rating_column + 3]
        for i in range(start_column, row_count - 1 + start_column):
            if ':' not in row[i]:
                continue
            score = row[i]
            curr_opp_rating = float(results[i - start_column +
                                            1][curr_rating_column])
            opp_name = results[i - start_column + 1][curr_rating_column + 1]
            if is_pair:
                opp_name += '+' + results[i - start_column +
                                          1][curr_rating_column + 3]
            if '+' in row[i]:
                first, _, score = row[i].partition("+")
                delta = delta_calculation(curr_rating, curr_opp_rating, first)
                new_rating += delta
                print(curr_name + ',' + opp_name + ',' + first + ',' +
                      str(delta))
            delta = delta_calculation(curr_rating, curr_opp_rating, score)
            new_rating += delta
            print(curr_name + ',' + opp_name + ',' + score + ',' + str(delta))

        row.append("{:.1f}".format(new_rating))
    with open(output_file, 'w') as outfile:
        wr = csv.writer(outfile, delimiter=',')
        for row in results:
            wr.writerow(row)
def test_slice_ref():
    """Slice reference implementation."""
    r = list(range(10))
    l = defaultlist()
    l += r
    eq_(l[-4:9], r[-4:9])
    eq_(l[0:9], r[0:9])
    eq_(l[:9], r[:9])
    eq_(l[-4:8], r[-4:8])
    eq_(l[0:], r[0:])
    eq_(l[:-2], r[:-2])
    eq_(list(l), r)
def test_iadd():
    """iadd operator."""
    a = defaultlist()
    b = [1, 2]
    a += a
    eq_(a, [])
    a += b
    eq_(a, [1, 2])
    a[5] = 7
    eq_(b, [1, 2])
    eq_(a, [1, 2, None, None, None, 7])
    with assert_raises(TypeError, "'int' object is not iterable"):
        a += 4
Beispiel #22
0
def table_to_items(table):
    row_class, grid = parse_table(table)

    rooms = defaultlist()

    start_cell = cell_text(grid[0][0])

    if not start_cell or 'Building' not in start_cell:
        return None, None
    room_row = next(row for row in range(len(row_class))
                    if row_class[row] == 'room')
    for i in range(1, len(grid[room_row]) - 1):
        text = tidy_room_name(cell_text(grid[room_row][i]))
        rooms[i] = text

    seen = set()

    if False:
        for row in range(0, len(grid)):
            cls = row_class[row]
            print(cls)

    items = []
    for row in range(2, len(grid)):
        cls = row_class[row]
        if cls != 'items':
            continue

        row_time = cell_text(grid[row][0])
        assert len(row_time) == 5

        for col in range(1, len(grid[row])):
            cell = grid[row][col]
            text = cell_text(cell)
            if text is None:
                continue
            if not is_program_item(text) or text in seen:
                continue
            seen.add(text)
            item = parse_program_item(text)
            if '/' not in item['title']:
                continue
            assert item['title'].startswith('2019:')
            if row != cell['row']:
                assert cell_text(grid[cell['row']][col]) == text
            item['start'] = row_time
            item['duration'] = rowspan_to_duration(cell['rowspan'])
            item['room'] = rooms[col]
            items.append(item)

    return rooms, items
def test_add():
    a = defaultlist()
    b = [1, 2]
    a_a = a + a
    eq_(a_a, [])
    a_b = a + b
    eq_(a_b, [1, 2])
    a_b[5] = 7
    eq_(a, [])
    eq_(b, [1, 2])
    eq_(a_b, [1, 2, None, None, None, 7])
    with assert_raises(TypeError,
                       'can only concatenate list (not "int") to list'):
        a + 4
def test_inc():
    """Count the number of func invocations."""
    def inc():
        inc.counter += 1
        return inc.counter

    inc.counter = -1

    l = defaultlist(inc)
    eq_(l, [])
    l[2] = "C"
    eq_(l, [0, 1, 'C'])
    eq_(l[4], 4)
    eq_(l, [0, 1, 'C', 3, 4])
    eq_(l[1], 1)
    eq_(l, [0, 1, 'C', 3, 4])
def test_copy_inc():
    """copy() using inc."""
    def inc():
        inc.counter += 1
        return inc.counter

    inc.counter = -1
    l = defaultlist(inc)
    l[2] = "C"
    l[4]
    eq_(l, [0, 1, 'C', 3, 4])
    c = l.copy()
    eq_(c, [0, 1, 'C', 3, 4])
    assert isinstance(c, defaultlist)
    c[6] = 'c0fe'
    eq_(c, [0, 1, 'C', 3, 4, 5, 'c0fe'])
    l[6]
    eq_(l, [0, 1, 'C', 3, 4, 7, 8])
Beispiel #26
0
class Day14(AOCDay):
    test_input = "abc"

    THREE_SAME = re.compile(r"(.)\1\1")

    hash_cache = defaultlist()

    def has_triplet(self, s):
        return self.THREE_SAME.search(s)

    def stretch_hash(self, hash):
        for _ in range(2016):
            hash = md5(hash.encode("utf-8")).hexdigest()
        return hash

    def common(self, input_data):
        if isinstance(self.input_data, str):
            self.input_data = self.input_data.encode("utf-8")
        self.hash_cache = defaultlist()

    def part1(self, input_data):
        index = 0
        found_keys = []
        while len(found_keys) < 64:
            if self.hash_cache[index] is None:
                self.hash_cache[index] = md5(
                    self.input_data + str(index).encode("utf-8")).hexdigest()
            hash = self.hash_cache[index]
            if c := self.has_triplet(hash):
                for i in range(1, 1001):
                    if self.hash_cache[index + i] is None:
                        self.hash_cache[index + i] = md5(
                            self.input_data +
                            str(index + i).encode("utf-8")).hexdigest()
                    hash2 = self.hash_cache[index + i]
                    if f"{c.group(1)}{c.group(1)}{c.group(1)}{c.group(1)}{c.group(1)}" in hash2:
                        self.debug(f"#{len(found_keys)+1}")
                        self.debug(f"3 -- {index}: {hash}")
                        self.debug(f"5 -- {index+i}: {hash2}")
                        found_keys.append((index, index + i, hash))
                        break  # Break from for loop
            index += 1

        yield found_keys[-1][0]
Beispiel #27
0
def create_users(_input: List) -> List:
    """ Create multiple users
    """
    _output = defaultlist(dict)
    for _index, add_user in enumerate(_input):
        _output[_index] = create_user(add_user)
    return _output




# def check_userinfo(_input: dict, _check_keys:list) -> list:
#     """
#     _input = { "name": "admin","password": "******"}
#     _check_keys = ["name", "password"]
#     """
#     pass
#     # model_user = model_user_tool.get_by_name(name=_input.get("name"))
#     # isTrue, keys = check_value(_input, _demo: dict, _keys: list)
Beispiel #28
0
def intcode(input_str, input=None, output=None):
    ints = defaultlist(lambda: 0)
    for _int in map(lambda x: int(x), input_str.split(",")):
        ints.append(_int)

    if not input:
        input = Queue()

    if not output:
        output = input

    int_pointer = 0
    relative_base = 0
    while int_pointer < len(ints):
        opcode = Opcode(ints[int_pointer])
        if opcode.operation == Operation.Term:
            return list(output.queue)[-1] if output.queue else ints[0]

        int_pointer, relative_base = opcode.execute(int_pointer, ints, input,
                                                    output, relative_base)
def test_slice_step():
    """Slice Selection with step."""
    l = defaultlist()
    l[0] = 'a'
    l[1] = 'b'
    l[2] = 'c'
    l[3] = 'd'
    l[4] = 'e'
    l[5] = 'f'

    d = l[1:6:2]
    eq_(l, ['a', 'b', 'c', 'd', 'e', 'f', None])
    eq_(d, ['b', 'd', 'f'])

    d = l[:6:2]
    eq_(l, ['a', 'b', 'c', 'd', 'e', 'f', None])
    eq_(d, ['a', 'c', 'e'])

    d = l[1:6:2]
    eq_(l, ['a', 'b', 'c', 'd', 'e', 'f', None])
    eq_(d, ['b', 'd', 'f'])

    d = l[:7:2]
    eq_(l, ['a', 'b', 'c', 'd', 'e', 'f', None, None])
    eq_(d, ['a', 'c', 'e', None])

    d = l[1:7:2]
    eq_(l, ['a', 'b', 'c', 'd', 'e', 'f', None, None])
    eq_(d, ['b', 'd', 'f'])

    d = l[::2]
    eq_(l, ['a', 'b', 'c', 'd', 'e', 'f', None, None])
    eq_(d, ['a', 'c', 'e', None])

    d = l[1::2]
    eq_(l, ['a', 'b', 'c', 'd', 'e', 'f', None, None])
    eq_(d, ['b', 'd', 'f', None])

    d = l[1::]
    eq_(l, ['a', 'b', 'c', 'd', 'e', 'f', None, None])
    eq_(d, ['b', 'c', 'd', 'e', 'f', None, None])
Beispiel #30
0
    def __init__(self, c_input, input_id):
        self.current_index = 0

        self.memory = defaultlist()
        self.memory.extend(c_input)

        self.input_value = input_id
        self.outputs = []

        result = "START"
        while self.current_index < len(self.memory) and result != "HALT":
            (
                instruction,
                mode_param_1,
                mode_param_2,
                mode_param_3,
            ) = self.parse_instruction_and_modes(self.memory[self.current_index])
            result = self.run_instruction(
                opcode=instruction,
                mode_param_1=mode_param_1,
                mode_param_2=mode_param_2,
                mode_param_3=mode_param_3,
            )