Ejemplo n.º 1
0
    def __init__(self, params):
        """
        `__init__()` initializes the necessary objects for using the
        Constraint.

        Attributes:
        `height`                         --     Board heigh.
        `widt`                           --    Board width.
        `count`         int              --     Number of solutions found.
        `board`         []position       --     List of currently allocated pieces.
        `board_index`   int              --     Index in the list of allocated pieces.
        `pieces`        []int            --     List of given pieces to allocate.
        `last_xy`       [][]coordinate   --     List of last used position
                                         --     per every type of piece.
        `last_index`    []int            --     Index in the list of last positions.
        `used_cols`     []int            --     Attacked columns.
        `used_rows`     []int            --     Attacked rows.
        `used_diag_l`   []int            --     Attacked diagonals (left)
        `used_diag_r`   []int            --     Attacked diagonals (right)
        `used_cells`    []int            --     Map of cells "under attack".
        `king_rules`    [8]coordinate    --     Permitted moves for king
        `knight_rules`  [8]coordinate    --     Permitted moves for knight
        """
        position = recordtype("position", ["x", "y", "kind"])
        coordinate = recordtype("coordinate", ["x", "y"])

        self.width = params["m"]
        self.height = params["n"]
        self.count = 0
        self.pieces = params["pieces"]

        self.board = []
        for _ in self.pieces:
            self.board.append(position(0, 0, 0))

        self.board_index = 0
        self.last_xy = []
        self.last_index = [0] * number_of_types

        for _ in range(number_of_types):
            coord_list = []
            for _ in range(len(self.pieces) + 1):
                coord_list.append(coordinate(0, 0))
            self.last_xy.append(coord_list)

        self.attacked_cols = [0] * self.width
        self.attacked_rows = [0] * self.height
        self.attacked_diag_l = [0] * (self.width + self.height)
        self.attacked_diag_r = [0] * (self.width + self.height)
        self.attacked_cells = [0] * ((self.width+4) * (self.height+4))

        self.king_rules = [
            coordinate(-1, 0), coordinate(1, 0), coordinate(0, -1), coordinate(0, 1),
            coordinate(-1, -1), coordinate(1, 1), coordinate(1, -1), coordinate(-1, 1)
        ]

        self.knight_rules = [
            coordinate(-2, -1), coordinate(-2, 1), coordinate(2, -1), coordinate(2, 1),
            coordinate(-1, -2), coordinate(-1, 2), coordinate(1, -2), coordinate(1, 2)
        ]
def train(model, optimizer, train_loader, model_evaluator, detection_loss, params, verbose, start_epoch=0,
          use_amp=False):
    """
    args: model - nn.Module CNN to train
          optimizer - torch.optim
          params - json config
    trains model, saves best model by validation
    """

    losses = [0] * 4

    model.train()

    print("Total number of parameters trained this epoch: ",
          sum(p.numel() for pg in optimizer.param_groups for p in pg['params'] if p.requires_grad))

    average_time = recordtype('average_time', ['inference_time', 'backprop_time', 'optimizer_time'])
    total_time = recordtype('total_time', ['inference_time', 'backprop_time', 'optimizer_time'])

    average = average_time(inference_time=0, backprop_time=0, optimizer_time=0)
    total = total_time(inference_time=0, backprop_time=0, optimizer_time=0)

    WARM_UP = 2
    nr_batches = len(train_loader)
    counted_batches = nr_batches - WARM_UP
    for batch_idx, (input_, label, _) in enumerate(train_loader):
        if verbose:
            print("Batch id: ", batch_idx)
        now1 = time.time()
        batch_time = train_step(model, input_, label, optimizer,
                                losses, detection_loss, params, verbose, use_amp)
        now2 = time.time()

        if verbose:
            print("=================")

        if batch_idx >= WARM_UP:
            average.inference_time += batch_time.inference_time / counted_batches
            average.backprop_time += batch_time.backprop_time / counted_batches
            average.optimizer_time += batch_time.optimizer_time / counted_batches

            total.inference_time += batch_time.inference_time
            total.backprop_time += batch_time.backprop_time
            total.optimizer_time += batch_time.optimizer_time

        if verbose:
            print("Total time: {}\n\n".format(now2-now1))

    print("Times on {} batches of size {}:".format(counted_batches, params.batch_size))
    print(average)
    print(total)
    print("Total time: ", total.inference_time + total.backprop_time + total.optimizer_time)
Ejemplo n.º 3
0
    def setUpClass(cls):
        # Create test's data
        cls.test_data_list = []
        cls.TestDataDevelop = recordtype("TestDataDevelop",
                                         "type data host port database user \
                                         password schema")
        cls.TestDataTest = recordtype("TestDataTest",
                                      "type data host port virtualhost user \
                                      password")
        cls.test_data_list.append(
            cls.TestDataDevelop("Develop.mr_robot", "test_data", "test_host",
                                1111, "test_database", "test_user",
                                "test_password", "test_schema"))
        cls.test_data_list.append(
            cls.TestDataTest("Test.vpn", "test_data", "test_host", 2222,
                             "test_virtualhost", "test_user", "test_password"))

        # add new test's data to table develop_mr_robot_configs
        with psql.open(host="localhost", user="******", database="db",
                       password="******") as db:
            if not db.query(
                    f"SELECT data FROM develop_mr_robot_configs\
                    WHERE data='{cls.test_data_list[0].data}'"):
                db.query(f"INSERT INTO develop_mr_robot_configs (data, host,\
                                  port, database, \"user\", password, schema)\
                                  VALUES \
                                  ('{cls.test_data_list[0].data}',\
                                   '{cls.test_data_list[0].host}',\
                                   '{cls.test_data_list[0].port}',\
                                   '{cls.test_data_list[0].database}',\
                                   '{cls.test_data_list[0].user}',\
                                   '{cls.test_data_list[0].password}',\
                                   '{cls.test_data_list[0].schema}')")

        # add new test's data to table test_vpn_configs
        with psql.open(host="localhost", user="******", database="db",
                       password="******") as db:
            if not db.query(
                    f"SELECT data FROM test_vpn_configs\
                    WHERE data='{cls.test_data_list[1].data}'"):
                db.query(f"INSERT INTO test_vpn_configs (data, host, port,\
                                  virtualhost, \"user\", password) VALUES \
                                  ('{cls.test_data_list[1].data}',\
                                   '{cls.test_data_list[1].host}',\
                                   '{cls.test_data_list[1].port}',\
                                   '{cls.test_data_list[1].virtualhost}',\
                                   '{cls.test_data_list[1].user}',\
                                   '{cls.test_data_list[1].password}')")
Ejemplo n.º 4
0
class NACopy(Edit,
             recordtype.recordtype("NACopy", ["copy", ("uncertain", False)])):
    """Represent copy number variants (Invitae-specific use)

    This class is intended for Invitae use only and does not represent
    a standard HGVS concept. The class may be changed, moved, or
    removed without notice.

    """
    def __str__(self):
        s = "copy{}".format(self.copy)
        return "(" + s + ")" if self.uncertain else s

    def _set_uncertain(self):
        """sets the uncertain flag to True; used primarily by the HGVS grammar

        :returns: self
        """
        self.uncertain = True
        return self

    @property
    def type(self):
        """return the type of this Edit

        :returns: edit type (str)
        """
        return "copy"

    def _del_ins_lengths(self, ilen):
        """returns (del_len, ins_len).
        Unspecified ref or alt returns None for del_len or ins_len respectively.
        """
        return (0, ilen * self.copy)
Ejemplo n.º 5
0
        class RefTranscriptData(recordtype.recordtype("RefTranscriptData",
                                                      ["transcript_sequence", "aa_sequence", "cds_start", "cds_stop",
                                                       "protein_accession"])):
            @classmethod
            def setup_transcript_data(cls, ac, ac_p, db, ref="GRCh37.p10"):
                """helper for generating RefTranscriptData from for c_to_p"""
                tx_info = db.get_tx_info(ac)
                tx_seq = db.get_tx_seq(ac)

                if tx_info is None or tx_seq is None:
                    raise hgvs.exceptions.HGVSError("Missing transcript data for accession: {}".format(ac))

                # use 1-based hgvs coords
                cds_start = tx_info["cds_start_i"] + 1
                cds_stop = tx_info["cds_end_i"]

                # padding list so biopython won't complain during the conversion
                tx_seq_to_translate = tx_seq[cds_start - 1:cds_stop]
                if len(tx_seq_to_translate) % 3 != 0:
                    "".join(list(tx_seq_to_translate).extend(["N"] * ((3 - len(tx_seq_to_translate) % 3) % 3)))

                tx_seq_cds = Seq(tx_seq_to_translate)
                protein_seq = str(tx_seq_cds.translate())

                transcript_data = RefTranscriptData(tx_seq, protein_seq, cds_start, cds_stop, ac_p)

                return transcript_data
Ejemplo n.º 6
0
def make_game_object(game_tag):
    """
    :param game_tag: game_container from beautiful soup, to be parsed
    :return: game recordtype containing all information parsed, ready to be added to DB
    """
    # main parser wrapper.... for now
    game_datetime = get_game_datetime(game_tag)
    away_team, home_team, away_rank, home_rank = get_team_names(game_tag)
    game_lines = check_has_lines(game_tag)
    away_spread, home_spread = get_game_spread(game_lines)
    away_ml, home_ml = get_moneyline(game_lines)
    over, under = get_game_total(game_lines)

    game_id = (game_datetime, home_team, away_team)
    # TODO: move this out of function
    Game = recordtype(
        'Game',
        'game_id away_spread home_spread away_ml home_ml over under ranks')
    game = Game(game_id=game_id,
                away_spread=away_spread,
                home_spread=home_spread,
                away_ml=away_ml,
                home_ml=home_ml,
                over=over,
                under=under,
                ranks=[home_rank, away_rank])
    return game
Ejemplo n.º 7
0
    def __init__(self, rows, cols, scope=10, num_food=5, max_pad=10):
        self.rows = rows
        self.cols = cols
        self.scope = scope
        self.num_food = num_food
        self.consumed_count = 0
        self.max_pad = max_pad
        self.boxes = []

        self.pos = recordtype('pos', 'rownum colnum')

        for i in range(rows):
            row_box = []
            for j in range(cols):
                row_box.append(self.pos(i, j))
            self.boxes.append(row_box)

        self.current_pos = self.pos(random.randint(0, self.rows - 1),
                                    random.randint(0, self.cols - 1))

        self.foods = [
            self.pos(random.randint(0, self.rows - 1),
                     random.randint(0, self.cols - 1))
            for _ in range(self.num_food)
        ]
Ejemplo n.º 8
0
def recordtype_row_strategy(column_names):
    """ Recordtype row strategy, rows returned as recordtypes

    Column names that are not valid Python identifiers will be replaced
    with col<number>_
    """
    import recordtype  # optional dependency
    # replace empty column names with placeholders
    column_names = [
        name if is_valid_identifier(name) else 'col%s_' % idx
        for idx, name in enumerate(column_names)
    ]
    recordtype_row_class = recordtype.recordtype('Row', column_names)

    # custom extension class that supports indexing
    class Row(recordtype_row_class):
        def __getitem__(self, index):
            if isinstance(index, slice):
                return tuple(getattr(self, x) for x in self.__slots__[index])
            return getattr(self, self.__slots__[index])

        def __setitem__(self, index, value):
            setattr(self, self.__slots__[index], value)

    def row_factory(row):
        return Row(*row)

    return row_factory
 def __init__(self, value_dic, vector_name='Vector'):
     value_tuple = tuple(value_dic.values())
     self.len_tuple = len(value_tuple)
     self.validate(value_tuple)
     self.vector_nt = recordtype(vector_name,
                                 ' '.join([k for k in value_dic.keys()]))
     self._vector = value_tuple
Ejemplo n.º 10
0
def forward(input_parameter_name):
    """
    Forwards a Task's input as an output

    :param input_parameter_name: The name of this cmd_fxn's input parameter to forward
    """
    return recordtype('Forward', 'input_parameter_name', default=None)
Ejemplo n.º 11
0
def out_dir(basename=''):
    """
    Essentially will perform os.path.join(Task.output_dir, basename)

    :param str basename: The basename of the output_file
    """
    return recordtype('OutputDir', 'basename', default=None)
Ejemplo n.º 12
0
def out_dir(basename=""):
    """
    Essentially will perform os.path.join(Task.output_dir, basename)

    :param str basename: The basename of the output_file
    """
    return recordtype("OutputDir", "basename", default=None)
Ejemplo n.º 13
0
def forward(input_parameter_name):
    """
    Forwards a Task's input as an output

    :param input_parameter_name: The name of this cmd_fxn's input parameter to forward
    """
    return recordtype("Forward", "input_parameter_name", default=None)
Ejemplo n.º 14
0
def recordtype_row_strategy(column_names):
    """ Recordtype row strategy, rows returned as recordtypes

    Column names that are not valid Python identifiers will be replaced
    with col<number>_
    """
    import recordtype  # optional dependency
    # replace empty column names with placeholders
    column_names = [name if is_valid_identifier(name) else 'col%s_' % idx for idx, name in enumerate(column_names)]
    recordtype_row_class = recordtype.recordtype('Row', column_names)

    # custom extension class that supports indexing
    class Row(recordtype_row_class):
        def __getitem__(self, index):
            if isinstance(index, slice):
                return tuple(getattr(self, x) for x in self.__slots__[index])
            return getattr(self, self.__slots__[index])

        def __setitem__(self, index, value):
            setattr(self, self.__slots__[index], value)

    def row_factory(row):
        return Row(*row)

    return row_factory
Ejemplo n.º 15
0
class Repeat(Edit,
             recordtype.recordtype('Repeat', [('ref', None), ('min', None),
                                              ('max', None),
                                              ('uncertain', False)])):
    def __str__(self):
        if self.min > self.max:
            raise HGVSError(
                "Repeat min count must be less than or equal to max count")
        if self.min == self.max:
            return "{self.ref}[{self.min}]".format(self=self)
        return "{self.ref}({self.min}_{self.max})".format(self=self)

    def _set_uncertain(self):
        """sets the uncertain flag to True; used primarily by the HGVS grammar

        :returns: self
        """
        self.uncertain = True
        return self

    @property
    def type(self):
        """return the type of this Edit

        :returns: edit type (str)
        """
        return "repeat"
def train_step(model, input_, label, optimizer, losses, detection_loss, params, verbose, use_amp=False):
    input_ = input_.to(device)
    label[0] = label[0].to(device)
    label[1] = label[1].to(device)

    optimizer.zero_grad()

    # =================
    # START INFERENCE

    time_before_inference = time.time()

    output = model(input_)

    time_after_inference = time.time()
    inference_duration = time_after_inference - time_before_inference

    if verbose:
        print("Forward propagation time: {}".format(inference_duration))

    # END INFERENCE
    # =================

    # =================
    # START BACKPROPAGATION

    time_before_backprop = time.time()

    l_loss, c_loss = detection_loss.ssd_loss(output, label)
    loss = l_loss + c_loss

    update_losses(losses, l_loss.item(), c_loss.item())
    if use_amp:
        with amp.scale_loss(loss, optimizer) as scaled_loss:
            scaled_loss.backward()
    else:
        loss.backward()

    time_after_backprop = time.time()
    backprop_duration = time_after_backprop - time_before_backprop

    if verbose:
        print("Backward propagation time: {}".format(backprop_duration))

    # END BACKPROPAGATION
    # =================

    a = time.time()
    optimizer.step()
    b = time.time()
    optimizer_duration = b-a

    if verbose:
        print("Optimizer step time: ", optimizer_duration)

    Times = recordtype('Times', ['inference_time', 'backprop_time', 'optimizer_time'])
    batch_time = Times(inference_time=inference_duration,
                       backprop_time=backprop_duration, optimizer_time=optimizer_duration)
    return batch_time
Ejemplo n.º 17
0
class ParseResult(
        recordtype('ParseResult', 'scheme netloc path params query fragment'),
        urlparse1.ResultMixin):

    __slots__ = ()

    def geturl(self):
        return urlparse1.urlunparse(self)
Ejemplo n.º 18
0
class SplitResult(
        recordtype('SplitResult', 'scheme netloc path query fragment'),
        urlparse1.ResultMixin):

    __slots__ = ()

    def geturl(self):
        return urlparse1.urlunsplit(self)
Ejemplo n.º 19
0
def check_has_lines(
        game_tag
):  # TODO: This is ugly.  Come up with better way to handle this?
    """
    :param game_tag: game_container from beautiful soup, to be parsed
    :return: recordtpe containing all parsed line information
    """
    # check whether all lines are there or not and return the found tag if so
    # TODO: move this out of function
    Lines = recordtype(
        'Lines',
        'spread moneyline total prices has_spread has_moneyline has_total')
    lines = Lines(spread=None,
                  moneyline=None,
                  total=None,
                  prices=None,
                  has_spread=False,
                  has_moneyline=False,
                  has_total=False)

    lines.spread = game_tag.find_all('span', class_='market-line bet-handicap')
    lines.total = game_tag.find_all(
        'span', class_='market-line bet-handicap both-handicaps')
    lines.prices = game_tag.find_all('span', class_='bet-price')
    # TODO: more elegant solution for this?
    for k in range(len(lines.prices)):
        price = lines.prices[k].text
        if '(' in price:
            for char in ['(', ')']:
                price = price.replace(char, '')
        lines.prices[k] = price

    num_prices = len(lines.prices)
    if num_prices == 6:
        lines.has_spread, lines.has_moneyline, lines.has_total = True, True, True
    elif num_prices == 4:
        if lines.spread:
            lines.has_spread = True
        if lines.total:
            lines.has_total = True
        if not lines.has_total or not lines.has_spread:
            lines.has_moneyline = True
    elif num_prices == 2:
        if lines.spread:
            lines.has_spread = True
        if lines.total:
            lines.has_total = True
        if not lines.has_total and not lines.has_spread:
            lines.has_moneyline = True

    # Pad lines.prices for indexing purposes
    if not lines.has_spread:
        lines.prices.insert(0, '')
        lines.prices.insert(0, '')
    if not lines.has_moneyline:
        lines.prices.insert(2, '')
        lines.prices.insert(2, '')
    return lines
Ejemplo n.º 20
0
def find(regex, n="==1", tags=None):
    """
    Used to set an input_file's default behavior to finds output_files from a Task's parents using a regex

    :param str regex: a regex to match the file path
    :param str n: (cardinality) the number of files to find
    :param dict tags: filter parent search space using these tags
    """
    return recordtype("FindFromParents", "regex n tags", default=None)
Ejemplo n.º 21
0
def dijkstra():
    tests = [(1, 0, 3, 'R'), (-1, 0, 2, 'L'), (0, -1, 0, 'U'), (0, 1, 1, 'D')]

    def get_neighbors(x, y, path):
        k = key(x, y)
        ns = []

        h = gethash(passw, path)

        for t in tests:
            tx = x + t[0]
            ty = y + t[1]
            if tx < 0 or ty < 0 or tx >= max_x or ty >= max_y:
                continue
            tk = key(tx, ty)
            if h[t[2]] in 'bcdef':
                ns.append((1, tx, ty, tk + path + t[3], path + t[3]))

        return ns

    QEntry = recordtype.recordtype('QEntry', 'x y k valid path')

    dist['0,0,'] = 0

    prev['0,0,'] = None

    finder = {}

    inq = set()
    h = []
    heapq.heappush(h,
                   (0, 0, '', QEntry(x=0, y=0, k='0,0,', path='', valid=True)))
    finder['0,0,'] = h[0]
    inq.add('0,0,')

    while len(h) > 0:
        #print_map(dist);
        u = heapq.heappop(h)
        item = u[3]
        if not item.valid:
            continue
        inq.remove(item.k)
        if item.x == target_y and item.y == target_y:
            continue
        uk = item.k
        for v in get_neighbors(item.x, item.y, item.path):
            alt = dist[uk] + v[0]
            if alt > dist[v[3]]:
                dist[v[3]] = alt
                #prev[v[3]] = (uk, v[0], v[4], v[1], v[2])
                entry = QEntry(x=v[1], y=v[2], k=v[3], path=v[4], valid=True)
                if v[3] in inq:
                    finder[v[3]].valid = False
                inq.add(v[3])
                finder[v[3]] = entry

                heapq.heappush(h, (-alt, -len(entry.path), entry.path, entry))
Ejemplo n.º 22
0
def Convert():
    for file in files:
        #print("converting")
        f = open(file, 'r', encoding="utf-8")
        _file = f.read()
        f.close()
        text = []
        _file = re.sub('\t*\n', '\n', _file)
        _file = re.sub(' *\n', '\n', _file)
        _file = re.sub('{', '{\n', _file)
        _file = re.sub('}', '\n}', _file)
        _file = re.sub('\"', '', _file)

        # Remove Comments
        for line in _file.split("\n"):
            if "#" in line:
                line = line.split("#")[0]
            text.append(line + "\n")
        # Format Input
        #print(text)
        out = "{"
        layer = 0
        for line in text:
            if "{" in line:
                layer += 1
                #print(line)
                temp = line.split("=")
                out += "\"{}\"".format(Replacer(temp[0])) + ": {"
                #print(temp)
            elif "}" in line:
                layer -= 1
                out += "},"
            elif "=" in line:
                temp = line.split("=")
                out += "\"{}\" : \"{}\",".format(Replacer(temp[0]),
                                                 Replacer(temp[1]))
            elif not line.isspace():
                #print(line)
                out += "\"KEY_LESS_VALUE\":\"{}\",".format(line)
        out = out + "}"

        out = re.sub('\n', '', out)
        out = re.sub('\t', '', out)
        out = re.sub(',}', '}', out)

        #out = re.sub(' ','STL2PY_REPLACE_SPACE_',out)

        #print(out)
        try:
            x = json.loads(out,
                           object_hook=lambda d: recordtype('X', d.keys())
                           (*d.values()))
        except Exception as e:
            print("\n-----")
            print(e)
            print(out)
            print(file)
Ejemplo n.º 23
0
def edge_expansion(edge, threshold, cursor):
    EDC = []

    # added for avoid move back in network during searching
    visited_edges = [edge.edge_id]

    for node_geom in [edge.start_node, edge.end_node]:
        # for n in NODES:
        #   n['dis'] = 0
        visited_nodes = []

        # TODO: maybe it should be copy of this node
        Node = recordtype('Node', 'geom dis')
        node = Node(node_geom, 0)
        H = []
        heapq.heappush(H, (node.dis, node))

        while H:
            node_min = heapq.heappop(H)[1]
            visited_nodes.append(node_min.geom)

            for edge_prim_record in get_node_edges(node_min.geom, cursor):
                edge_prim_id = edge_prim_record.edge_id

                if not edge_prim_id in visited_edges:
                    edge_prim = get_edge_by_id(edge_prim_id, cursor)
                    visited_edges.append(edge_prim_id)

                    if node_geom == edge.start_node and node_min.geom == edge_prim.start_node:
                        EDC.append((edge.edge_id, edge_prim.edge_id, 'SS',
                                    node_min.dis))
                        other_edge_prim_node = Node(edge_prim.end_node, 0)

                    if node_geom == edge.start_node and node_min.geom == edge_prim.end_node:
                        EDC.append((edge.edge_id, edge_prim.edge_id, 'SE',
                                    node_min.dis))
                        other_edge_prim_node = Node(edge_prim.start_node, 0)

                    if node_geom == edge.end_node and node_min.geom == edge_prim.start_node:
                        EDC.append((edge.edge_id, edge_prim.edge_id, 'ES',
                                    node_min.dis))
                        other_edge_prim_node = Node(edge_prim.end_node, 0)

                    if node_geom == edge.end_node and node_min.geom == edge_prim.end_node:
                        EDC.append((edge.edge_id, edge_prim.edge_id, 'EE',
                                    node_min.dis))
                        other_edge_prim_node = Node(edge_prim.start_node, 0)

                    # find other node_primes of edge_prime
                    if other_edge_prim_node.geom not in visited_nodes:
                        # other_edge_prim_node['dis'] > node_min['dis'] + edge_prim['weight'] omitted
                        if threshold >= node_min.dis + edge_prim.weight:
                            other_edge_prim_node.dis = node_min.dis + edge_prim.weight
                            heapq.heappush(H, (other_edge_prim_node.dis,
                                               other_edge_prim_node))

    return EDC
Ejemplo n.º 24
0
def find(regex, n='==1', tags=None):
    """
    Used to set an input_file's default behavior to finds output_files from a Task's parents using a regex

    :param str regex: a regex to match the file path
    :param str n: (cardinality) the number of files to find
    :param dict tags: filter parent search space using these tags
    """
    return recordtype('FindFromParents', 'regex n tags', default=None)
Ejemplo n.º 25
0
def Options(optionsList=None, mutable=False):
    """Construct an o
    from proteus.LinearAlgebraToptions object (named tuple)
    
    :param optionsList: An iterable of options tuples. Each option is
                        a 3-tuple, with the first entry the option
                        name string, the second entry the default
                        value, and the third entry a help string

    Example::

      opts=Context.Options([("nnx", 11, "number of mesh nodes in x-direction"),
                            ("nny", 21, "number of mesh nodes in y-direction"])
      nnx = opts.nnx
      nny = opts.nny

    """
    import ast, sys
    global contextOptionsString
    contextOptionsDict = {}
    help = "Context input options:\n"
    for optTuple in optionsList:
        help += """{0}[{1}] {2}\n\n""".format(optTuple[0], optTuple[1],
                                              optTuple[2])
        contextOptionsDict[optTuple[0]] = optTuple[1]
    logEvent(help)
    if contextOptionsString == "?":
        print(help)
        contextOptionsString = None
        sys.exit(0)
    if contextOptionsString is not None:
        option_overides = contextOptionsString.split(" ")
        for option in option_overides:
            lvalue, rvalue = option.split("=")
            if lvalue in contextOptionsDict:
                logEvent("Processing context input options from commandline")
                try:
                    contextOptionsDict[lvalue] = ast.literal_eval(rvalue)
                    logEvent(lvalue + " = " + rvalue)
                except:
                    sys.stderr.write(
                        "Failed setting context options from command line string."
                    )
                    raise
            else:
                logEvent("IGNORING CONTEXT OPTION; DECLARE " + lvalue +
                         " IF YOU WANT TO SET IT")
    #now set named tuple merging optionsList and opts_cli_dihelpct
    if mutable:
        ContextOptions = recordtype("ContextOptions",
                                    iter(contextOptionsDict.items()))
        return ContextOptions()
    else:
        ContextOptions = namedtuple("ContextOptions",
                                    list(contextOptionsDict.keys()))
        return ContextOptions._make(list(contextOptionsDict.values()))
Ejemplo n.º 26
0
def get_record(recordtype, record_url):
    """
    Given a record url and a recordtype, fetch the record from the and build the record.
    Assume there's just one record at the location and everything goes well
    """

    resp = session.get(record_url)

    new_record = recordtype(**resp.json())
    return new_record
Ejemplo n.º 27
0
    def __init__(self, sensor_id, stream_names):
        """
        sensor_id: the ID of the sensor that generates the stream
        stream_names: list of the stream names that the sensor generates 
        """

        Model = recordtype("Stream", "sensor_id timestamp streams")

        streams = dict((stream_names[i], 0) for i in range(0, len(stream_names)))

        self.stream = Model(sensor_id=sensor_id, timestamp=0, streams=streams)
Ejemplo n.º 28
0
class FunctionStats(recordtype('FunctionStatsRecord', 'cc nc tt ct callers')):
    def __getitem__(self, index):
        return getattr(self, self.__slots__[index])

    @property
    def percall(self):
        return float(self.tt) / self.nc if self.nc else 0.0

    @property
    def cumpercall(self):
        return float(self.ct) / self.cc if self.cc else 0.0
Ejemplo n.º 29
0
class AAExt(Edit,
            recordtype.recordtype("AAExt", [("ref", None), ("alt", None),
                                            ("aaterm", None), ("length", None),
                                            ("uncertain", False)])):
    def __init__(self, ref, alt, aaterm=None, length=None, uncertain=False):
        super(AAExt, self).__init__(ref=aa_to_aa1(ref),
                                    alt=aa_to_aa1(alt),
                                    aaterm=aa_to_aa1(aaterm),
                                    length=length,
                                    uncertain=uncertain)

    def format(self, conf=None):
        p_3_letter, p_term_asterisk = self._format_config(conf)

        st_alt = self.alt or ""
        st_aaterm = self.aaterm or ""
        st_length = self.length or ""
        if p_3_letter:
            st_alt = aa1_to_aa3(st_alt)
            st_aaterm = aa1_to_aa3(st_aaterm)
            if p_term_asterisk and st_alt == "Ter":
                st_alt = "*"
            if p_term_asterisk and st_aaterm == "Ter":
                st_aaterm = "*"

        s = "{alt}ext{term}{length}".format(alt=st_alt,
                                            term=st_aaterm,
                                            length=st_length)
        return "(" + s + ")" if self.uncertain else s

    __str__ = format

    def _set_uncertain(self):
        """sets the uncertain flag to True; used primarily by the HGVS grammar

        :returns: self
        """
        self.uncertain = True
        return self

    @property
    def type(self):
        """return the type of this Edit

        :returns: edit type (str)
        """
        return "ext"

    def _del_ins_lengths(self, ilen):
        """returns (del_len, ins_len).
        Unspecified ref or alt returns None for del_len or ins_len respectively.
        """
        return (0, abs(self.length))
Ejemplo n.º 30
0
class HGVSPosition(recordtype.recordtype("HGVSPosition",
                                         ["ac", "type", "pos"])):
    """
    HGVSPosition -- Represent partial HGVS tags that refer to a position without alleles

    :param str ac: sequence accession
    :param str type: type of sequence and coordinate
    :param str pos: sequence position

    """
    def __str__(self):
        return "{self.ac}:{self.type}.{self.pos}".format(self=self)
Ejemplo n.º 31
0
def setFromModule(moduleIn,mutable=False):
    """Construct the global context object from a module"""
    global context
    fields = {}
    for key, value in moduleIn.__dict__.iteritems():
        if  key[:2] != "__":
            fields[key]=value
    if mutable:
        Context = recordtype(moduleIn.__name__.split('.')[-1], fields.iteritems())
        context = Context()
    else:
        Context = namedtuple(moduleIn.__name__.split('.')[-1], fields.keys())
        context = Context._make(fields.values())
Ejemplo n.º 32
0
def perceptron_algorithm_iter(vector,
                              init_w=None,
                              is_offset=False,
                              max_iter=20):
    Run = recordtype('Run', 'init_w final_w nb_iter')
    Weight = recordtype('Weight', 'theta theta_0')
    if not init_w:
        w = Weight(np.array([0, 0]), 0)
    else:
        init_w_copy = init_w.theta.copy()
        w = init_w
    iter = 0
    while not is_all_right(vector) and iter < max_iter:
        for point in vector.points:
            if is_not_right(point, w):
                update_weight(point, w)
            elif is_right(point, w):
                update_point_status(point)
        else:
            iter += 1

    return Run(Weight(init_w_copy, 0), w, iter)
Ejemplo n.º 33
0
def setFromModule(moduleIn,mutable=False):
    """Construct the global context object from a module"""
    global context
    fields = {}
    for key, value in moduleIn.__dict__.iteritems():
        if  key[:2] != "__":
            fields[key]=value
    if mutable:
        Context = recordtype(moduleIn.__name__.split('.')[-1], fields.iteritems())
        context = Context()
    else:
        Context = namedtuple(moduleIn.__name__.split('.')[-1], fields.keys())
        context = Context._make(fields.values())
Ejemplo n.º 34
0
class Inv(Edit,
          recordtype.recordtype('Inv', [('ref', None), ('uncertain', False)])):
    """Inversion
    """
    def __init__(self, ref=None, uncertain=False, edit=None):
        if edit:
            ref = edit.ref
            uncertain = edit.uncertain
        super(Inv, self).__init__(ref=ref, uncertain=uncertain)

    def __str__(self):
        return "inv"

    def _set_uncertain(self):
        """sets the uncertain flag to True; used primarily by the HGVS grammar

        :returns: self
        """
        self.uncertain = True
        return self

    @property
    def ref_s(self):
        return self.ref if (isinstance(self.ref, basestring) and self.ref
                            and self.ref[0] in "ACGTUN") else None

    @property
    def ref_n(self):
        """
        returns an integer, either from the `seq` instance variable if it's a number,
        or None otherwise
        """
        try:
            return int(self.ref)
        except ValueError:
            return None

    @property
    def type(self):
        """return the type of this Edit

        :returns: edit type (str)
        """
        return "inv"

    def _del_ins_lengths(self, ilen):
        """returns (del_len, ins_len).
        Unspecified ref or alt returns None for del_len or ins_len respectively.
        """
        return (ilen, ilen)
Ejemplo n.º 35
0
def Options(optionsList=None,mutable=False):
    """Construct an o
    from proteus.LinearAlgebraToptions object (named tuple)
    
    :param optionsList: An iterable of options tuples. Each option is
                        a 3-tuple, with the first entry the option
                        name string, the second entry the default
                        value, and the third entry a help string

    Example::

      opts=Context.Options([("nnx", 11, "number of mesh nodes in x-direction"),
                            ("nny", 21, "number of mesh nodes in y-direction"])
      nnx = opts.nnx
      nny = opts.nny

    """
    import ast, sys
    global contextOptionsString
    contextOptionsDict = {}
    help="Context input options:\n"
    for optTuple  in optionsList:
        help += """{0}[{1}] {2}\n\n""".format(optTuple[0], optTuple[1], optTuple[2])
        contextOptionsDict[optTuple[0]] = optTuple[1]
    logEvent(help)
    if contextOptionsString=="?":
        print(help)
        contextOptionsString=None
    if contextOptionsString is not None:
        option_overides=contextOptionsString.split(" ")
        for option in option_overides:
            lvalue, rvalue = option.split("=")
            if contextOptionsDict.has_key(lvalue):
                logEvent("Processing context input options from commandline")
                try:
                    contextOptionsDict[lvalue] = ast.literal_eval(rvalue)
                    logEvent(lvalue+" = "+rvalue)
                except:
                    sys.stderr.write("Failed setting context options from command line string.")
                    raise 
            else:
                logEvent("IGNORING CONTEXT OPTION; DECLARE "+lvalue+" IF YOU WANT TO SET IT")
    #now set named tuple merging optionsList and opts_cli_dihelpct
    if mutable:
        ContextOptions = recordtype("ContextOptions", contextOptionsDict.iteritems())
        return ContextOptions()
    else:
        ContextOptions = namedtuple("ContextOptions", contextOptionsDict.keys())
        return ContextOptions._make(contextOptionsDict.values())
Ejemplo n.º 36
0
    def __init__(self, buffer_size, batch_size, device='cpu'):
        """Initialize a ReplayBuffer object.

        Params
        ======        
            buffer_size (int): maximum size of buffer
            batch_size (int): size of each training batch
        """

        self.DEVICE = device

        self.memory = deque(maxlen=buffer_size)
        self.batch_size = batch_size
        self.experience = recordtype(
            'Experience', ["state", "action", "reward", "next_state", "done"])
Ejemplo n.º 37
0
def dicts_to_namedrecords(dicts, class_name=None):
    """ Takes list of dictionaries and converts to list of recordtype objects"""
    from recordtype import recordtype
    from random import randint
    from sets import Set
    all_keys = []
    for dic in dicts:
        all_keys += dic.keys()
    unique_keys = list(Set(all_keys))
    key_defaults = [(key, None) for key in unique_keys]
    if not class_name:
        class_name = 'FB_Record_Type_'
    Record = recordtype(class_name + str(randint(1,9999999)), key_defaults)
    data = [Record(**dic) for dic in dicts]
    return data
Ejemplo n.º 38
0
def attributes_class_constructor(name, fields, identity=True, *args, **kwargs):
    if not identity:
        return recordtype.recordtype(name, fields, *args, **kwargs)

    # Create the new class, inheriting from the record type and from this
    # class
    class IdentityClass(recordtype.recordtype(name, fields, *args, **kwargs)):
        def __init__(self, id=None, created_at=None, updated_at=None,
                     *init_args, **init_kwargs):
            super(IdentityClass, self).__init__(*init_args, **init_kwargs)
            self.id = id
            self.created_at = created_at
            self.updated_at = updated_at

        @property
        def id(self):
            return self._id

        @id.setter
        def id(self, value):
            if value is not None:
                value = int(value)

            self._id = value

        @property
        def created_at(self):
            return self._created_at

        @created_at.setter
        def created_at(self, value):
            if value is not None and not isinstance(value, datetime):
                value = dateutil.parser.parse(value)

            self._created_at = value

        @property
        def updated_at(self):
            return self._updated_at

        @updated_at.setter
        def updated_at(self, value):
            if value is not None and not isinstance(value, datetime):
                value = dateutil.parser.parse(value)

            self._updated_at = value

    return IdentityClass
Ejemplo n.º 39
0
class SimplePosition(
        recordtype.recordtype("SimplePosition",
                              field_names=[("base", None),
                                           ("uncertain", False)])):
    def __str__(self):
        self.validate()
        s = "?" if self.base is None else str(self.base)
        return "(" + s + ")" if self.uncertain else s

    def format(self, conf):
        return str(self)

    @property
    def is_uncertain(self):
        """return True if the position is marked uncertain or undefined"""
        return self.uncertain or self.base in None

    def _set_uncertain(self):
        "mark this location as uncertain and return reference to self; this is called during parsing (see hgvs.ometa)"
        self.uncertain = True
        return self

    def validate(self):
        if self.base is not None and self.base < 1:
            return (ValidationLevel.ERROR, "Position base must be >= 1")
        return (ValidationLevel.VALID, None)

    def __sub__(lhs, rhs):
        assert type(lhs) == type(
            rhs), "Cannot substract coordinates of different representations"
        return lhs.base - rhs.base

    def __eq__(lhs, rhs):
        assert type(lhs) == type(
            rhs), "Cannot compare coordinates of different representations"
        if lhs.uncertain or rhs.uncertain:
            raise HGVSUnsupportedOperationError(
                "Cannot compare coordinates of uncertain positions")
        return lhs.base == rhs.base

    def __lt__(lhs, rhs):
        assert type(lhs) == type(
            rhs), "Cannot compare coordinates of different representations"
        if lhs.uncertain or rhs.uncertain:
            raise HGVSUnsupportedOperationError(
                "Cannot compare coordinates of uncertain positions")
        return lhs.base < rhs.base
Ejemplo n.º 40
0
class Conv(Edit,
           recordtype.recordtype('Conv', [('from_ac', None),
                                          ('from_type', None),
                                          ('from_pos', None),
                                          ('uncertain', False)])):
    """Conversion
    """
    def __init__(self,
                 from_ac=None,
                 from_type=None,
                 from_pos=None,
                 uncertain=False,
                 edit=None):
        if edit:
            from_ac = edit.from_ac
            from_type = edit.from_type
            from_pos = edit.from_pos
            uncertain = edit.uncertain
        super(Conv, self).__init__(from_ac=from_ac,
                                   from_type=from_type,
                                   from_pos=from_pos,
                                   uncertain=uncertain)

    def __str__(self):
        if self.from_ac and self.from_type and self.from_pos:
            s = "con{self.from_ac}:{self.from_type}.{self.from_pos}".format(
                self=self)
        else:
            s = "con"
        return "(" + s + ")" if self.uncertain else s

    def _set_uncertain(self):
        """sets the uncertain flag to True; used primarily by the HGVS grammar

        :returns: self
        """
        self.uncertain = True
        return self

    @property
    def type(self):
        """return the type of this Edit

        :returns: edit type (str)
        """
        return "con"
Ejemplo n.º 41
0
from .tools import StringIO, icmp, list, target_from_compact, tuple

__all__ = [
    'ChainParameters',
    'Output',
    'Input',
    'Transaction',
    'Block',
    'ConnectedBlockInfo',
]

# ===----------------------------------------------------------------------===

ChainParameters = recordtype('ChainParameters',
    ['magic', 'port', 'genesis', 'testnet', 'pubkey_hash_prefix',
     'script_hash_prefix', 'secret_prefix', 'max_value', 'transient_reward',
     'transient_budget', 'perpetual_reward', 'perpetual_budget', 'fee_budget',
     'maximum_target', 'next_target', 'alert_keys','checkpoints', 'features'])

# ===----------------------------------------------------------------------===

class Output(SerializableMixin):
    def __init__(self, amount=0, contract=None, *args, **kwargs):
        if contract is None:
            contract = self.get_script_class()()
        super(Output, self).__init__(*args, **kwargs)
        self.amount = amount
        self.contract = contract

    @classmethod
    def get_script_class(cls):
Ejemplo n.º 42
0
@event.listens_for(Engine, 'before_cursor_execute')
def before_cursor_execute(conn, cursor, statement, 
                          parameters, context, executemany):
    global query_stats
    query_stats._begin = datetime.now()

@event.listens_for(Engine, 'after_cursor_execute')
def after_cursor_execute(conn, cursor, statement, 
                         parameters, context, executemany):
    global query_stats
    query_stats.num_queries += 1
    query_stats.time_database += (datetime.now() - query_stats._begin).total_seconds()

from recordtype import recordtype
TableStatistics = recordtype('TableStatistics',
    'num_inserts num_updates num_deletes'.split(), default=0)

@event.listens_for(Mapper, 'after_insert')
def after_insert(mapper, connection, target):
    global query_stats
    query_stats.table_stats.setdefault(target.__table__.name, TableStatistics())
    query_stats.table_stats[target.__table__.name].num_inserts += 1

@event.listens_for(Mapper, 'after_update')
def after_update(mapper, connection, target):
    global query_stats
    query_stats.table_stats.setdefault(target.__table__.name, TableStatistics())
    query_stats.table_stats[target.__table__.name].num_updates += 1

@event.listens_for(Mapper, 'after_delete')
def after_delete(mapper, connection, target):
Ejemplo n.º 43
0
import boto.ec2
from recordtype import recordtype

from .util import AMI_MAP

Network = recordtype('Network', [
    "net_name",
    "root_folder",
    "AWS_region",
    "AWS_ACCESS_KEY",
    "AWS_SECRET_KEY",
    "AWS_keypair",
    "AWS_security_group",
    "sync_node",
    "sync_node_user",
    "monitor_node",
    "bridge_node",
    "config_node",
    "mailgun_domain",
    "mailgun_key",
    "alert_recipients",
    "system_alert_recipients",
    "code_repo",
    "config_repo",
    "blobs_repo",
], default=None)

def _(s): print(textwrap.dedent(s).strip())

def ask(msg, default): return raw_input("%s [%s]: " % (msg, default)) or default
Ejemplo n.º 44
0
    def _work(self):
        def update_connected_routers_info():
            new_connected_routers = \
                connected_routers - prev_connected_routers
            new_disconnected_routers = \
                prev_connected_routers - connected_routers

            if new_connected_routers:
                self._logger.debug("Connected to routers: {0}".format(
                    list(new_connected_routers)))
            if new_disconnected_routers:
                self._logger.debug("Disconnected from routers: {0}".format(
                    list(new_disconnected_routers)))

            # Remove disconnected routers from list.
            for router_name in new_disconnected_routers:
                assert router_name in connected_rrs_info
                del connected_rrs_info[router_name]

            # Set distance to infinity for destination routers route to which
            # leaded through disconnected routers.
            for to_router, dest_router_info in dest_routers_info.iteritems():
                if dest_router_info.next_router in new_disconnected_routers:
                    dest_router_info.dist = RIPService.inf_distance
                    self._logger.debug(
                        "Remove route: Due to disconnection: "
                        "{dest}:(d={dist}, n={next})".format(
                            dest=to_router, dist=dest_router_info.dist,
                            next=dest_router_info.next_router))

            # Add connected hosts to according list.
            for router_name in new_connected_routers:
                router_name not in connected_rrs_info
                connected_rrs_info[router_name] = \
                    ConnectedRouterInfo(Timer(self._update_period,
                        start_time=time.time() - self._update_period - 1.0))
                assert connected_rrs_info[router_name].timer.is_expired()

            # Update routing information for directly connected destination
            # routers.
            for router_name in new_connected_routers:
                dest_routers_info[router_name] = DestRouterInfo(
                    dist=1, next_router=router_name,
                    timer=DummyTimer())
                self._logger.debug(
                    "Add route: Directly connected: "
                    "{dest}:(d={dist}, n={next})".format(
                        dest=router_name,
                        dist=dest_routers_info[router_name].dist,
                        next=dest_routers_info[router_name].next_router))

            table_changed = new_connected_routers or new_disconnected_routers
            if table_changed:
                update_routing_table()

        def connected_routers_with_timeout():
            for rr_name, connected_rr_info in connected_rrs_info.iteritems():
                if connected_rr_info.timer.is_expired():
                    yield rr_name

        def set_infinite_timeout_distances():
            routing_table_updated = False
            for dest, dest_router_info in dest_routers_info.iteritems():
                if (dest_router_info.dist < RIPService.inf_distance and
                        dest_router_info.timer.is_expired()):
                    dest_router_info.dist = RIPService.inf_distance
                    dest_router_info.timer = Timer(self._remove_timeout)
                    routing_table_updated = True

                    self._logger.debug(
                        "Remove route: Due to timeout: "
                        "{dest}:(d={dist}, n={next})".format(
                            dest=dest, dist=dest_router_info.dist,
                            next=dest_router_info.next_router))

            if routing_table_updated:
                update_routing_table()

        def remove_timeout_distances():
            routing_table_updated = False
            for dest, dest_router_info in dest_routers_info.items():
                if (dest_router_info.dist == RIPService.inf_distance and
                        dest_router_info.timer.is_expired()):
                    del dest_routers_info[dest]

                    routing_table_updated = True

                    self._logger.debug(
                        "Due to big timeout removing target: {dest}".format(
                            dest=dest))

            # TODO: Not actually needed.
            if routing_table_updated:
                update_routing_table()

        def distances_for_sending(to_router):
            distances = []
            for dest, dest_router_info in dest_routers_info.iteritems():
                if dest_router_info.next_router == to_router:
                    # Rule 1A from [vasilev04netsoft]:
                    # For router R: if packets to destination router X are sent
                    # through router G, then distance to router X that being
                    # sent to router G is infinity.
                    #
                    #   R ----------------- G - ... - X
                    #       X:(inf, G) ->
                    #
                    d = (RIPService.inf_distance, dest)
                    distances.append(d)
                else:
                    d = (dest_router_info.dist, dest)
                    distances.append(d)
            return distances

        def send_distances(routers):
            for to_router in routers:
                # Prepare data to send.
                distances = distances_for_sending(to_router)
                raw_data = RIPData(distances).serialize()

                # Send data.
                self._service_transmitter.send_data(to_router, raw_data)

                # Mark time data was sent.
                connected_rrs_info[to_router].timer.restart()

                # TODO: Assume that computer is not slow.
                assert not connected_rrs_info[to_router].timer.is_expired()

        def handle_receive():
            while True:
                result = self._service_transmitter.receive_data(block=False)
                if result is None:
                    break
                src, raw_data = result

                rip_data = RIPData.deserialize(raw_data)

                self._logger.debug(
                    "Received vector from {0}:\n  {1}".format(
                        src,
                        pprint.pformat(rip_data.distances)))

                routing_table_updated = False
                for dist, dest in rip_data.distances:
                    dist = min(dist + 1, RIPService.inf_distance)

                    if dest not in dest_routers_info:
                        # Route to new router.

                        if dist < RIPService.inf_distance:
                            dest_routers_info[dest] = DestRouterInfo(
                                dist=dist, next_router=src,
                                timer=Timer(self._inf_timeout))

                            routing_table_updated = True

                            self._logger.debug(
                                "Received route to new router: "
                                "{dest}:(d={dist}, n={next})".format(
                                    dest=dest, dist=dist,
                                    next=src))
                        else:
                            # Ignore.
                            pass

                    elif dist < dest_routers_info[dest].dist:
                        # Received shorter then all known path to router.
                        
                        dest_routers_info[dest].dist = dist
                        dest_routers_info[dest].next_router = src
                        dest_routers_info[dest].timer = \
                            Timer(self._inf_timeout)

                        routing_table_updated = True

                        self._logger.debug(
                            "Found shorter path: "
                            "{dest}:(d={dist}, n={next})".format(
                                dest=dest, dist=dist,
                                next=src))

                    elif (dest_routers_info[dest].next_router == src and
                            dest_routers_info[dest].dist != dist):
                        # Received route update from source.

                        dest_routers_info[dest].dist = dist

                        timer = Timer(self._inf_timeout) \
                            if dist < RIPService.inf_distance \
                            else Timer(self._remove_timeout)
                        dest_routers_info[dest].timer = timer

                        routing_table_updated = True

                        self._logger.debug(
                            "Received route update from source: "
                            "{dest}:(d={dist}, n={next})".format(
                                dest=dest, dist=dist,
                                next=src))
                    else:
                        if dist < RIPService.inf_distance:
                            # Update timer.
                            dest_routers_info[dest].timer.restart()
                        else:
                            # Don't update timer for infinite paths.
                            pass

                if routing_table_updated:
                    update_routing_table()

        def update_routing_table():
            old_routing_table = self._dynamic_routing_table.table().copy()

            new_routing_table = {}

            for dest, dest_rr_info in dest_routers_info.iteritems():
                if dest_rr_info.dist < RIPService.inf_distance:
                    assert dest not in new_routing_table
                    new_routing_table[dest] = \
                        RIPService.RIPRouteToDestination(
                            dest_rr_info.next_router, dest_rr_info.dist)

            if old_routing_table != new_routing_table:
                self._logger.debug("New routing table:\n  {0}".format(
                    pprint.pformat(new_routing_table)))

                self._dynamic_routing_table.update(new_routing_table)

        self._logger.info("Working thread started")

        DestRouterInfo = recordtype(
            'DestRouterInfo', 'dist next_router timer')
        ConnectedRouterInfo = recordtype('ConnectedRouterInfo', 'timer')

        # {destination router name: DestRouterInfo()}
        # `timer' member is for last time information about destination router
        # was updated,
        dest_routers_info = {self._router_name:
            DestRouterInfo(dist=0, next_router=self._router_name,
                timer=DummyTimer())}

        # {connected router: ConnectedRouterInfo()}
        # `timer' member if for last time information packet was sent to
        # router}
        connected_rrs_info = {}

        connected_routers = frozenset()
        while True:
            if self._exit_lock.acquire(False):
                # Obtained exit lock. Terminate.

                self._exit_lock.release()
                self._logger.info("Exit working thread")
                return

            # Prepare data for detecting changes in current router connectivity.
            prev_connected_routers = connected_routers
            connected_routers = \
                frozenset(self._link_manager.connected_routers())
                
            # Remove information about disconnected routers and add information
            # about newly connected routers.
            update_connected_routers_info()

            # Send distances information to all who needs it.
            send_distances(list(connected_routers_with_timeout()))

            # Update distances according to received packets.
            handle_receive()

            set_infinite_timeout_distances()
            remove_timeout_distances()

            time.sleep(config.thread_sleep_time)
Ejemplo n.º 45
0
            '<%s type=%s message=%s mark=%s>' % (
                str(self.__class__).split('.')[-1][:-2],
                self.type,
                self.message,
                self.mark)
        )

    def __str__(self):
        return (
            super(MarkedIssue, self).__str__() +
            (' (source "%s" line %d column %d)' %
                (self.mark.source, self.mark.line + 1, self.mark.column + 1))
        )


Rule = recordtype('Rule', ['name', 'description'])


class Inspection(object):

    @classmethod
    def all_inspections(klass):
        return [c for c in all_subclasses(klass)]

    @classmethod
    def rules(klass):
        if hasattr(klass, 'name') and hasattr(klass, 'description'):
            return [Rule(klass.name, klass.description)]
        else:
            return []
Ejemplo n.º 46
0
from collections import namedtuple
from os import access, path, W_OK

from flask import current_app
from recordtype import recordtype
from werkzeug.security import generate_password_hash, check_password_hash
from flask.ext.login import UserMixin

import models_jsonapi as mj
from . import db, login_manager, date_util
from datetime import timedelta


StateWarning = namedtuple('StateWarning', ['limit', 'value', 'created_ts', 'sensor', 'alarming_measurements'])

RRDDef = recordtype('RRDDef', 'name step path mmin mmax')


class Measurement(object):
    def __init__(self, sensor_code):
        self.sensor_code = sensor_code
        self.sensor = None

        self.value = None
        self.read_ts = None
        self.has_warning = False
        self.has_notification = False

    def init_sensor(self):
        self.sensor = db.session.query(Sensor).filter(Sensor.sensor_code == self.sensor_code).first()
Ejemplo n.º 47
0
SIL_LABEL = 'SIL'
NOISES = set(['SIL', 'SPN'])

# FILE1 = 's0101a'
# FILE2 = FILE1
# FILE2 = 's2001a'

def silentremove(filename):
    try:
        os.remove(filename)
    except OSError as e:
        if e.errno != errno.ENOENT:
            # errno.ENOENT = no such file or directory
            raise

LabelledInterval = recordtype('LabelledInterval' ,'label interval')


def load_gold(goldfile):
    res = {}
    current_file = ''
    prev_end = -1
    with open(goldfile) as fin:
        for line in fin:
            splitted = line.strip().split()
            if not splitted:
                # empty line
                continue
            elif len(splitted) == 1:
                # New file
                current_file = splitted[0]
Ejemplo n.º 48
0
        assert 1 <= secret.exponent < SECP256k1.order, (u"encoded exponent "
            u"must be an integer x such that 1 <= x < 0x%x" % SECP256k1.order)

        # Return the newly generated Secret
        return secret

    # The exponent is stored as a 256-bit big endian integer, the first field
    # of the payload:
    exponent = property(lambda self:BigInteger.deserialize(BytesIO(self.payload[:32]), 32))
    # A compressed key is requested by suffixing 0x01 to the payload field. Note
    # that the following comparison only returns true if payload is exactly 33
    # bytes in length and the final byte is '\x01'.
    compressed = property(lambda self:self.payload[32:] == six.int2byte(1))

from recordtype import recordtype
BaseSignature = recordtype('BaseSignature', 'r s'.split())

class Signature(SerializableMixin, BaseSignature):
    def serialize(self):
        def _serialize_derint(n):
            n_str = BigNum(n).serialize()
            return b''.join([
                six.int2byte(0x02),
                six.int2byte(len(n_str)),
                n_str,
            ])
        r_str = _serialize_derint(self.r)
        s_str = _serialize_derint(self.s)
        return b''.join([
            six.int2byte(0x30),
            six.int2byte(len(r_str + s_str)),
Ejemplo n.º 49
0
    def run(self, command, *args, **kwargs):
        if self.use_sudo:
            command = ['sudo'] + command
        result = self.shell.run(command, allow_error=True, *args, **kwargs)
        self.logger.debug('Executed command: %s, '
                          'result code %d, output:\n%s' % (' '.join(command),
                                                           result.return_code,
                                                           result.output))
        return result

    def open(self, path, mode='r'):
        self.logger.debug('Opening file %s mode %s' % (path, mode))
        return self.shell.open(path, mode)


ProcessInfo = recordtype('ProcessInfo', ['pid', 'command'])


class ExtendedNodeClient(object):
    def __init__(self, client):
        super(ExtendedNodeClient, self).__init__()
        self._client = client

    def run(self, command, *args, **kwargs):
        return self._client.run(command, *args, **kwargs)

    def open(self, path, mode='r'):
        return self._client.open(path, mode)

    def get_processes(self, reload=False):
        if not hasattr(self, '_processes') or reload:
Ejemplo n.º 50
0
# Tolerance on convergence test
tol = 1e-2

NUM_PRINCIPLE_COMPONENTS = 5

from recordtype import recordtype  #for mutable namedtuple (dict might also work)

SettingsStruct = recordtype('SettingsStruct',
                            ['shape_learning',  #String representing the shape which the object is learning
                             'initDatasetFile',  #Path to the dataset file that will be used to initialize the matrix for PCA
                             'updateDatasetFiles',  #List of path -- or single path-- to dataset that will be updated with demo shapes
                             'paramFile', #Path to the dataset file 'params.dat' inside which we save the learned parameters
                             'paramsToVary',
                             #Natural number between 1 and number of parameters in the associated ShapeModeler, representing the parameter to learn
                             'doGroupwiseComparison',  #instead of pairwise comparison with most recent two shapes
                             'initialBounds',
                             #Initial acceptable parameter range (if a value is NaN, the initialBounds_stdDevMultiples setting will be used to set that value)
                             'initialBounds_stdDevMultiples',
                             #Initial acceptable parameter range in terms of the standard deviation of the parameter
                             'initialParamValue',
                             #Initial parameter value (NaN if to be drawn uniformly from initialBounds)
                             'minParamDiff']) #How different two shapes' parameters need to be to be published for comparison
#@todo: make groupwise comparison/pairwise comparison different implementations of shapeLearner class

class ShapeLearner:
    def __init__(self, settings):

        self.paramsToVary = settings.paramsToVary
        #self.numPrincipleComponents = max(self.paramsToVary)
        self.numPrincipleComponents = NUM_PRINCIPLE_COMPONENTS
Ejemplo n.º 51
0
from recordtype import recordtype

Review = recordtype(
    "Review", ["review_id", "business_id", "user_id", "date", "type", "text", "stars", "votes"], default=None
)
Ejemplo n.º 52
0
from inspect import isfunction
from collections import OrderedDict
from recordtype import recordtype
from six import iteritems

import numpy as np

import theano 
import theano.tensor as T

_logger = logging.getLogger(__name__)


#------------------------------------------------------------------------------

HyperParam = recordtype('HyperParam', 'value name default help')

#------------------------------------------------------------------------------

class HyperBase(object):
    initialized = False

    def __init__(self, **hyper_params):
        self._hyper_params = OrderedDict()
        self.initialized = True

    def _ensure_init(self):
        if not self.initialized:
            raise ArgumentError("HyperBase base class not initialized yet!"
                    "Call yperBase.__init__()  before doing anything else!")
Ejemplo n.º 53
0
import random
import numpy as np
from numpy.linalg import norm
from collections import namedtuple
from recordtype import recordtype

Cluster = recordtype('Cluster', ('center', 'members', 'converged'))

class KMeans:
    def __init__(self, X, k, iters = 1):
        self.X = X
        self.k = k
        self.iters = iters
        self.cost = None
        self.clusters = []

    def fit(self):
        self.clusters = self.__find_centers(self.X, self.k, self.clusters)
        self.cost = self.__cost(self.clusters)

        for _ in xrange(self.iters - 1):
            new_clusters = self.__find_centers(self.X, self.k, self.clusters)
            new_cost = self.__cost(self.clusters)

            if (new_cost < self.cost):
                self.clusters = new_clusters
                self.cost = new_cost

        return self.clusters

    def __find_centers(self, X, k, clusters):
Ejemplo n.º 54
0

def attribute_repr(self, indent_level=0):
    """Pretty print for an attribute."""

    indent = '\t' * indent_level
    return '%s{Attribute: %s = %s}' % (indent, self.key, str(self.value))


def widget_repr(self, indent_level=0):
    """Pretty print for a widget."""

    indent = '\t' * indent_level
    attrs = '\n'.join([a.repr(indent_level + 1) for a in self.attributes])
    child_widgets = '\n\n'.join([c.repr(indent_level + 1) for c in self.child_widgets])
    if attrs and child_widgets:
        attrs += '\n\n'
    return '%s{Widget: %s\n%s%s\n%s}' % (indent, self.name, attrs, child_widgets, indent)


# AST nodes

# TODO: Make your changes here ################################################

Attribute = recordtype('Attribute', 'key, value')
Widget = recordtype('Widget', 'name, attributes, child_widgets')

# Give them a pretty print method
Attribute.repr = attribute_repr
Widget.repr = widget_repr
Ejemplo n.º 55
0
using different strategies and different metrics
"""

import logging; shapeLogger = logging.getLogger("shape_logger")
import os.path
from recordtype import recordtype
import glob
import numpy as np
from allograph import stroke
from allograph.stroke import Stroke
from ast import literal_eval

# global variables :
#-------------------

Shape = recordtype('Shape', [('path', None), ('shapeID', None), ('shapeType', None), ('shapeType_code', None), ('paramsToVary', None), ('paramValues', None)])

alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"


# learning_manager class :
#-------------------------

class LearningManager():

    def __init__(self, generate_path, demo_path, robot_path, ref_path):
        self.generate_path = generate_path
        self.demo_path = demo_path
        self.robot_path = robot_path
        self.ref_path = ref_path
        self.robot_data = read_data(self.robot_path,0)
Ejemplo n.º 56
0
system.

Part of 'Adaptor' framework.

Author: Michael Pankov, 2012-2013.

Please do not redistribute.
"""

import recordtype as rt
import collections as cl

from printable_structure import PrintableStructure


CPUInfoBase = rt.recordtype('CPUInfo',
    'cpu_name cpu_mhz cache_size flags')


class CPUInfo(PrintableStructure, CPUInfoBase):
    pass


HardwareInfoBase = rt.recordtype('HardwareInfo',
    'cpu_info')


class HardwareInfo(PrintableStructure, HardwareInfoBase):
    pass


BuildSettingsBase = rt.recordtype('BuildSettings',
Ejemplo n.º 57
0
GHIBLI_URL = "https://ghibliapi.herokuapp.com/"
session = requests.session()
# We don't want to panic when we see charles custom SSL cert.
# I've turned ssl verification off, but you can also pass a file path to charles's cert
# There are several ways to handle this.
session.verify = False
# Here's what passing the cert file would look like.
# You can ask charles to save the file somewhere on your machine
# session.verify = "/Users/Christopher/charles_sessions/charles-ssl-proxying-certificate.pem"


Person = recordtype("Person", ["id",
                               "name",
                               "gender",
                               "age",
                               "eye_color",
                               "hair_color",
                               "films",
                               "species",
                               "url"])

Species = recordtype("Species", ["id",
                                 "name",
                                 "classification",
                                 "eye_colors",
                                 "hair_colors",
                                 "url",
                                 "people",
                                 "films", ])

Ejemplo n.º 58
0
#!/usr/bin/env python

import sys
from os.path import dirname, realpath

# Add the funcparserlib_talk directory to the path to import recordtype
sys.path.insert(0, dirname(dirname(realpath(__file__))))
from recordtype import recordtype


# AST nodes
Calculation = recordtype('Calculation', 'left, operator, right')
Operator = recordtype('Operator', 'sign')
Number = recordtype('Number', 'value')
Ejemplo n.º 59
0
from .util import GeoRange
import simplejson as json
from lxml import etree
from lxml.etree import ParseError
from markupsafe import Markup

RPC_URL = "http://webservices.nextbus.com/service/publicXMLFeed?"
# cache durations
SHORT = 20
HOUR = 3600
DAY = 86400

resource_uri = Markup("resource_uri")
template_id = Markup("{{id}}")

Point = recordtype("Point", ['lat', 'lng'])

AgencyRecord = recordtype("Agency", ['id', 'title', 'region',
    ('short_title', None), ('route_ids', [])])
class Agency(AgencyRecord):
    @property
    def url(self):
        return url_for('agency_detail', agency_id=self.id)

    def _as_url_dict(self):
        d = self._asdict()
        d[resource_uri] = self.url
        detail_uri_template = url_for('route_detail', agency_id=self.id, route_id=template_id)
        # unescape mustaches
        detail_uri_template = detail_uri_template.replace("%7B", "{").replace("%7D", "}")
        d['routes'] = dict(