Python collections.defaultdict() Examples

The following are code examples for showing how to use collections.defaultdict(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: fs_image   Author: facebookincubator   File: extents_to_chunks.py    MIT License 6 votes vote down vote up
def _id_to_leaf_idx_to_chunk_clones(
    ids_and_extents: Iterable[Tuple[InodeID, Extent]],
):
    'Aggregates newly created ChunkClones per InodeID, and per "trimmed leaf"'
    id_to_leaf_idx_to_chunk_clones = defaultdict(dict)
    for extent_id, clone_ops in _leaf_extent_id_to_clone_ops(
        ids_and_extents
    ).items():
        leaf_ref_to_chunk_clones = _leaf_ref_to_chunk_clones_from_clone_ops(
            extent_id, clone_ops
        )
        for leaf_ref, offsets_clones in leaf_ref_to_chunk_clones.items():
            d = id_to_leaf_idx_to_chunk_clones[leaf_ref.clone.inode_id]
            # A `leaf_idx` from a specific inode ID refers to one extent,
            # and each extent is handled in one iteration, so it cannot be
            # that two iterations contribute to the same `leaf_idx` key.
            assert leaf_ref.leaf_idx not in d
            # `leaf_idx` is the position in `gen_trimmed_leaves` of the
            # chunk, whose clones we computed.  That fully specifies where
            #  `extents_to_chunks_with_clones` should put the clones.
            d[leaf_ref.leaf_idx] = offsets_clones

    return id_to_leaf_idx_to_chunk_clones 
Example 2
Project: fs_image   Author: facebookincubator   File: inode_id.py    MIT License 6 votes vote down vote up
def new(cls, *, description: Any=''):
        inner = _InnerInodeIDMap(
            description=description,
            id_to_reverse_entries=defaultdict(set),
        )
        counter = itertools.count()
        self = cls(
            inode_id_counter=counter,
            root=_PathEntry(
                id=InodeID(id=next(counter), inner_id_map=inner),
                name_to_child={},
            ),
            inner=inner,
        )
        self.inner.id_to_reverse_entries[self.root.id.id].add(
            _ROOT_REVERSE_ENTRY
        )
        return self 
Example 3
Project: fs_image   Author: facebookincubator   File: parse_repodata.py    MIT License 6 votes vote down vote up
def pick_primary_repodata(repodatas: Repodata) -> Repodata:
    primaries = defaultdict(list)
    for rd in repodatas:
        if rd.is_primary_sqlite():
            primaries['sqlite'].append(rd)
        elif rd.is_primary_xml():
            primaries['xml'].append(rd)

    primaries = primaries.get('sqlite', primaries.get('xml'))  # Prefer SQLite

    if not primaries:
        raise RuntimeError(f'{repodatas} has no known primary file.')

    if len(primaries) > 1:
        raise RuntimeError(f'More than one primary of one type: {primaries}')

    return primaries[0] 
Example 4
Project: rhodonite   Author: nestauk   File: misc.py    MIT License 6 votes vote down vote up
def reverse_index_communities(nested_list):
    """reverse_mapping
    Creates a mapping from elements to container index.

    Parameters
    ----------
        nested_list : :obj:`list` 
            Iterable of iterables.

    Returns
    -------
        d : :obj:`dict` 
            A dictionary mapping elements in the nested iterables to the index 
            of the iterable that they appeared within.
    """
    d = defaultdict(set)
    for i, l in enumerate(nested_list):
        for element in l:
            d[element].add(i)
    return d 
Example 5
Project: rhodonite   Author: nestauk   File: misc.py    MIT License 6 votes vote down vote up
def reverse_index(sequences):
    '''reverse_index
    Maps elements in a set of sequences to the index of the sets that they 
    appear in.

    Parameters
    ----------
    sequences : :obj:`iter` of :obj:`iter`

    Returns
    -------
    mapping : :obj:`dict`
    '''
    mapping = defaultdict(list)
    for i, seq in enumerate(sequences):
        for e in seq:
            mapping[e].append(i)
    return mapping 
Example 6
Project: rhodonite   Author: nestauk   File: phylomemetic.py    MIT License 6 votes vote down vote up
def reverse_mapping(g):
    """_reverse_mapping
    Returns the reverse mapping of items to vertices for the graph. If it
    does not exist, it is created.

    Returns
    -------
        self.reverse_mapping (dict): A mapping of community items to sets
            of vertices.
    """
    if not hasattr(g, 'reverse_mapping'):
        reverse_mapping = defaultdict(set)

        for v in g.vertices():
            items = g.vp['item'][v]
            for item in items:
                reverse_mapping[item].add(v)
        g.reverse_mapping = reverse_mapping

    return g.reverse_mapping 
Example 7
Project: pyblish-win   Author: pyblish   File: refactor.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _get_headnode_dict(fixer_list):
    """ Accepts a list of fixers and returns a dictionary
        of head node type --> fixer list.  """
    head_nodes = collections.defaultdict(list)
    every = []
    for fixer in fixer_list:
        if fixer.pattern:
            try:
                heads = _get_head_types(fixer.pattern)
            except _EveryNode:
                every.append(fixer)
            else:
                for node_type in heads:
                    head_nodes[node_type].append(fixer)
        else:
            if fixer._accept_type is not None:
                head_nodes[fixer._accept_type].append(fixer)
            else:
                every.append(fixer)
    for node_type in chain(pygram.python_grammar.symbol2number.itervalues(),
                           pygram.python_grammar.tokens):
        head_nodes[node_type].extend(every)
    return dict(head_nodes) 
Example 8
Project: pyblish-win   Author: pyblish   File: _pslinux.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_proc_inodes(self, pid):
        inodes = defaultdict(list)
        for fd in os.listdir("/proc/%s/fd" % pid):
            try:
                inode = os.readlink("/proc/%s/fd/%s" % (pid, fd))
            except OSError as err:
                # ENOENT == file which is gone in the meantime;
                # os.stat('/proc/%s' % self.pid) will be done later
                # to force NSP (if it's the case)
                if err.errno in (errno.ENOENT, errno.ESRCH):
                    continue
                elif err.errno == errno.EINVAL:
                    # not a link
                    continue
                else:
                    raise
            else:
                if inode.startswith('socket:['):
                    # the process is using a socket
                    inode = inode[8:][:-1]
                    inodes[inode].append((pid, int(fd)))
        return inodes 
Example 9
Project: pyblish-win   Author: pyblish   File: test_defaultdict.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_repr(self):
        d1 = defaultdict()
        self.assertEqual(d1.default_factory, None)
        self.assertEqual(repr(d1), "defaultdict(None, {})")
        self.assertEqual(eval(repr(d1)), d1)
        d1[11] = 41
        self.assertEqual(repr(d1), "defaultdict(None, {11: 41})")
        d2 = defaultdict(int)
        self.assertEqual(d2.default_factory, int)
        d2[12] = 42
        self.assertEqual(repr(d2), "defaultdict(<type 'int'>, {12: 42})")
        def foo(): return 43
        d3 = defaultdict(foo)
        self.assertTrue(d3.default_factory is foo)
        d3[13]
        self.assertEqual(repr(d3), "defaultdict(%s, {13: 43})" % repr(foo)) 
Example 10
Project: pyblish-win   Author: pyblish   File: test_defaultdict.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_print(self):
        d1 = defaultdict()
        def foo(): return 42
        d2 = defaultdict(foo, {1: 2})
        # NOTE: We can't use tempfile.[Named]TemporaryFile since this
        # code must exercise the tp_print C code, which only gets
        # invoked for *real* files.
        tfn = tempfile.mktemp()
        try:
            f = open(tfn, "w+")
            try:
                print >>f, d1
                print >>f, d2
                f.seek(0)
                self.assertEqual(f.readline(), repr(d1) + "\n")
                self.assertEqual(f.readline(), repr(d2) + "\n")
            finally:
                f.close()
        finally:
            os.remove(tfn) 
Example 11
Project: pyblish-win   Author: pyblish   File: test_defaultdict.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_copy(self):
        d1 = defaultdict()
        d2 = d1.copy()
        self.assertEqual(type(d2), defaultdict)
        self.assertEqual(d2.default_factory, None)
        self.assertEqual(d2, {})
        d1.default_factory = list
        d3 = d1.copy()
        self.assertEqual(type(d3), defaultdict)
        self.assertEqual(d3.default_factory, list)
        self.assertEqual(d3, {})
        d1[42]
        d4 = d1.copy()
        self.assertEqual(type(d4), defaultdict)
        self.assertEqual(d4.default_factory, list)
        self.assertEqual(d4, {42: []})
        d4[12]
        self.assertEqual(d4, {42: [], 12: []})

        # Issue 6637: Copy fails for empty default dict
        d = defaultdict()
        d['a'] = 42
        e = d.copy()
        self.assertEqual(e['a'], 42) 
Example 12
Project: pyblish-win   Author: pyblish   File: test_defaultdict.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_recursive_repr(self):
        # Issue2045: stack overflow when default_factory is a bound method
        class sub(defaultdict):
            def __init__(self):
                self.default_factory = self._factory
            def _factory(self):
                return []
        d = sub()
        self.assertTrue(repr(d).startswith(
            "defaultdict(<bound method sub._factory of defaultdict(..."))

        # NOTE: printing a subclass of a builtin type does not call its
        # tp_print slot. So this part is essentially the same test as above.
        tfn = tempfile.mktemp()
        try:
            f = open(tfn, "w+")
            try:
                print >>f, d
            finally:
                f.close()
        finally:
            os.remove(tfn) 
Example 13
Project: AutoDL   Author: tanguofu   File: blueprints.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, name,
                 url_prefix=None,
                 host=None, version=None,
                 strict_slashes=False):
        """Create a new blueprint

        :param name: unique name of the blueprint
        :param url_prefix: URL to be prefixed before all route URLs
        :param strict_slashes: strict to trailing slash
        """
        self.name = name
        self.url_prefix = url_prefix
        self.host = host

        self.routes = []
        self.websocket_routes = []
        self.exceptions = []
        self.listeners = defaultdict(list)
        self.middlewares = []
        self.statics = []
        self.version = version
        self.strict_slashes = strict_slashes 
Example 14
Project: AutoDL   Author: tanguofu   File: convnet_builder.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self,
               input_op,
               input_nchan,
               phase_train,
               use_tf_layers,
               data_format='NCHW',
               dtype=tf.float32,
               variable_dtype=tf.float32):
    self.top_layer = input_op
    self.top_size = input_nchan
    self.phase_train = phase_train
    self.use_tf_layers = use_tf_layers
    self.data_format = data_format
    self.dtype = dtype
    self.variable_dtype = variable_dtype
    self.counts = defaultdict(lambda: 0)
    self.use_batch_norm = False
    self.batch_norm_config = {}  # 'decay': 0.997, 'scale': True}
    self.channel_pos = ('channels_last'
                        if data_format == 'NHWC' else 'channels_first')
    self.aux_top_layer = None
    self.aux_top_size = 0 
Example 15
Project: AutoDL   Author: tanguofu   File: convnet_builder.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self,
               input_op,
               input_nchan,
               phase_train,
               use_tf_layers,
               data_format='NCHW',
               dtype=tf.float32,
               variable_dtype=tf.float32):
    self.top_layer = input_op
    self.top_size = input_nchan
    self.phase_train = phase_train
    self.use_tf_layers = use_tf_layers
    self.data_format = data_format
    self.dtype = dtype
    self.variable_dtype = variable_dtype
    self.counts = defaultdict(lambda: 0)
    self.use_batch_norm = False
    self.batch_norm_config = {}  # 'decay': 0.997, 'scale': True}
    self.channel_pos = ('channels_last'
                        if data_format == 'NHWC' else 'channels_first')
    self.aux_top_layer = None
    self.aux_top_size = 0 
Example 16
Project: slidoc   Author: mitotic   File: slidoc.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, http_post_func=None, return_html=False, error_exit=False):
        self.http_post = http_post_func or sliauth.http_post
        self.return_html = return_html
        self.error_exit = error_exit
        self.primary_tags = defaultdict(OrderedDict)
        self.sec_tags = defaultdict(OrderedDict)
        self.primary_qtags = defaultdict(OrderedDict)
        self.sec_qtags = defaultdict(OrderedDict)

        self.all_tags = {}

        self.questions = OrderedDict()
        self.concept_questions = defaultdict(list)

        self.ref_tracker = dict()
        self.ref_counter = defaultdict(int)
        self.chapter_ref_counter = defaultdict(int)

        self.dup_ref_tracker = set() 
Example 17
Project: ieml   Author: IEMLdev   File: lexicon.py    GNU General Public License v3.0 6 votes vote down vote up
def define_singular_sequences(self, dictionary, descriptors):
        parser = IEMLParser(dictionary=dictionary)

        paradigms_to_ss = defaultdict(list)
        paradigms_to_domain = {}

        singular_sequences = set()
        for (ieml), (domain) in self.structure:
            paradigm = parser.parse(ieml)
            assert len(paradigm) != 1

            paradigms_to_domain[paradigm] = domain

            for ss in paradigm.singular_sequences:
                if descriptors.is_defined(ss):
                    if ss not in singular_sequences:
                        singular_sequences.add(ss)

                    paradigms_to_ss[paradigm].append(ss)

        return paradigms_to_ss, singular_sequences, paradigms_to_domain 
Example 18
Project: iSDX   Author: sdn-ixp   File: replay.py    Apache License 2.0 6 votes vote down vote up
def __init__(self, config, flows_dir, ports_dir, num_timesteps, debug=False):
        self.logger = logging.getLogger("LogHistory")
        if debug:
            self.logger.setLevel(logging.DEBUG)

        self.log_entry = namedtuple("LogEntry", "source destination type")
        self.ports = defaultdict(list)
        self.flows = defaultdict(list)

        self.data = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))
        self.current_timestep = 0
        self.total_timesteps = num_timesteps

        self.parse_config(config)
        self.parse_logs(num_timesteps, flows_dir, ports_dir)
        self.info()

        pretty(self.data) 
Example 19
Project: QA   Author: shuaihuaiyi   File: qaData.py    GNU General Public License v3.0 6 votes vote down vote up
def loadEmbedding(filename):
    """
    加载词向量文件

    :param filename: 文件名
    :return: embeddings列表和它对应的索引
    """
    embeddings = []
    word2idx = defaultdict(list)
    with open(filename, mode="r", encoding="utf-8") as rf:
        for line in rf:
            arr = line.split(" ")
            embedding = [float(val) for val in arr[1: -1]]
            word2idx[arr[0]] = len(word2idx)
            embeddings.append(embedding)
    return embeddings, word2idx 
Example 20
Project: neural-fingerprinting   Author: StephanZheng   File: fingerprint.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, tau=0, name="", ds_name=""):
        self.name = name
        self.ds_name = ds_name
        self.tau = tau

        self.ids = set()
        self.ids_correct = set()
        self.ids_correct_fp = set()
        self.ids_agree = set()

        # Legal = there is a fingerprint match below threshold tau
        self.ids_legal = set()

        self.counts = defaultdict(lambda: 0)
        self.counts_legal = defaultdict(lambda: 0)
        self.counts_correct = defaultdict(lambda: 0)

        # Total number of examples
        self.i = 0 
Example 21
Project: neural-fingerprinting   Author: StephanZheng   File: master.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _save_sorted_results(self, run_stats, scores, image_count, filename):
    """Saves sorted (by score) results of the evaluation.

    Args:
      run_stats: dictionary with runtime statistics for submissions,
        can be generated by WorkPiecesBase.compute_work_statistics
      scores: dictionary mapping submission ids to scores
      image_count: dictionary with number of images processed by submission
      filename: output filename
    """
    with open(filename, 'w') as f:
      writer = csv.writer(f)
      writer.writerow(['SubmissionID', 'ExternalTeamId', 'Score',
                       'MedianTime', 'ImageCount'])
      get_second = lambda x: x[1]
      for s_id, score in sorted(iteritems(scores),
                                key=get_second, reverse=True):
        external_id = self.submissions.get_external_id(s_id)
        stat = run_stats.get(
            s_id, collections.defaultdict(lambda: float('NaN')))
        writer.writerow([s_id, external_id, score,
                         stat['median_eval_time'],
                         image_count[s_id]]) 
Example 22
Project: GreenGuard   Author: D3-AI   File: pipeline.py    MIT License 6 votes vote down vote up
def _to_dicts(self, hyperparameters):
        params_tree = defaultdict(dict)
        for (block, hyperparameter), value in hyperparameters.items():
            if isinstance(value, np.integer):
                value = int(value)

            elif isinstance(value, np.floating):
                value = float(value)

            elif isinstance(value, np.ndarray):
                value = value.tolist()

            elif value == 'None':
                value = None

            params_tree[block][hyperparameter] = value

        return params_tree 
Example 23
Project: google-tech-dev-guide   Author: alvinctk   File: hangman.py    Apache License 2.0 6 votes vote down vote up
def __init__(self, all_secret_word, number_of_guess):

        # To store number of guess allowed for the game
        self.number_of_guess = number_of_guess

        # To used to determine whether to print user's previous guesses
        self.start = number_of_guess

        # To store the final word to be shown
        self.random_word = random.choice(all_secret_word).lower()

        # To store user's previous guesses
        self.user_guess = set()

        # To used in determing which characters are incorrect
        self.fill_in_blank = "_"

        # Build guess word from secret word
        self.guess_word = [self.fill_in_blank] * len(self.random_word)

        # Preprocess the secret word to store the indices for a faster search
        self.secret_word = defaultdict(list)
        for i, character in enumerate(self.random_word):
            self.secret_word[character].append(i) 
Example 24
Project: fs_image   Author: facebookincubator   File: extents_to_chunks.py    MIT License 5 votes vote down vote up
def _leaf_extent_id_to_clone_ops(
    ids_and_extents: Iterable[Tuple[InodeID, Extent]]
):
    '''
    To collect the parts of a Chunk that are cloned, we will run a variation
    on the standard interval-overlap algorithm.  We first sort the starts &
    ends of each interval, and then do a sequential scan that uses starts to
    add, and ends to remove, a tracking object from a "current intervals"
    structure.

    This function simply prepares the set of interval starts & ends for each
    InodeID, the computation is in `_leaf_ref_to_chunk_clones_from_clone_ops`.
    '''
    leaf_extent_id_to_clone_ops = defaultdict(list)
    for ino_id, extent in ids_and_extents:
        file_offset = 0
        for leaf_idx, (offset, length, leaf_extent) in enumerate(
            extent.gen_trimmed_leaves()
        ):
            ref = _CloneExtentRef(
                clone=Clone(
                    inode_id=ino_id, offset=file_offset, length=length,
                ),
                extent=leaf_extent,
                offset=offset,
                leaf_idx=leaf_idx,
            )
            leaf_extent_id_to_clone_ops[id(leaf_extent)].extend([
                _CloneOp(pos=offset, action=_CloneOp.PUSH, ref=ref),
                _CloneOp(pos=offset + length, action=_CloneOp.POP, ref=ref),
            ])
            file_offset += length
    return leaf_extent_id_to_clone_ops 
Example 25
Project: PEAKachu   Author: tbischler   File: bam_to_bed.py    ISC License 5 votes vote down vote up
def generate_bed_format(self, bam_file):
        self._bam_fh = pysam.AlignmentFile(bam_file, "rb")
        for replicon in self._bam_fh.references:
            self._bed_read_dict = defaultdict(int)
            if self._paired_end:
                self._cache_read2(replicon)
            self._calc_bed_reads(replicon)
            yield(replicon, self._bed_read_dict)
        self._bam_fh.close() 
Example 26
Project: PEAKachu   Author: tbischler   File: bam_to_bed.py    ISC License 5 votes vote down vote up
def _cache_read2(self, replicon):
        self._read2_dict = defaultdict(dict)
        for aligned_read in self._bam_fh.fetch(replicon):
            if not aligned_read.is_read2:
                continue
            if not (aligned_read.is_paired and aligned_read.is_proper_pair):
                continue
            if aligned_read.mate_is_unmapped:
                continue
            self._read2_dict[aligned_read.query_name.split()[0]][
                aligned_read.reference_start] = aligned_read
        self._bam_fh.seek(0) 
Example 27
Project: PEAKachu   Author: tbischler   File: coverage.py    ISC License 5 votes vote down vote up
def _cache_read2(self, replicon, bam_fh):
        self._read2_dict = defaultdict(dict)
        for aligned_read in bam_fh.fetch(replicon):
            if not aligned_read.is_read2:
                continue
            if not (aligned_read.is_paired and aligned_read.is_proper_pair):
                continue
            if aligned_read.mate_is_unmapped:
                continue
            self._read2_dict[aligned_read.query_name.split()[0]][
                aligned_read.reference_start] = aligned_read
        bam_fh.seek(0) 
Example 28
Project: PEAKachu   Author: tbischler   File: count.py    ISC License 5 votes vote down vote up
def _cache_read2(self, replicon):
        self._read2_dict = defaultdict(dict)
        for aligned_read in self._bam_fh.fetch(replicon):
            if not aligned_read.is_read2:
                continue
            if not (aligned_read.is_paired and aligned_read.is_proper_pair):
                continue
            if aligned_read.mate_is_unmapped:
                continue
            self._read2_dict[aligned_read.query_name.split()[0]][
                aligned_read.reference_start] = aligned_read
        self._bam_fh.seek(0) 
Example 29
Project: PEAKachu   Author: tbischler   File: consensus_peak.py    ISC License 5 votes vote down vote up
def __init__(self, project_folder, consensus_length):
        self._project_folder = project_folder
        self._consensus_length = consensus_length
        self._replicon_peak_dict = defaultdict(lambda: defaultdict(set)) 
Example 30
Project: autolims   Author: scottbecker   File: custom_protocol.py    MIT License 5 votes vote down vote up
def __init__(self,parent_protocol=None,
                 mammalian_cell_mode=False):
        #catalog inventory
        self.transcriptic_inv = get_transcriptic_inventory()
        
        
        self.trash_plate_size = DEFAULT_TRASH_PLATE_SIZE
        
        #default to test mode (users need to change this after creating the protocol before using it)
        self.set_test_mode(True)
        
        #self.trash_tube_count = 0
        
        self._trash_plate = None
        self._trash_count = 0
        self._reagant_plate_cache = {}
        
        self.parent_protocol = parent_protocol
        self._last_incubated = []
        
        def get_zero():
            return ul(0)
        
        
        #changes default incubation w/ CO2
        self.mammalian_cell_mode = mammalian_cell_mode
        
        self.unprovisioned_stock_reagent_volumes = defaultdict(get_zero)
        
        self.absorbance_measurement_count= 0
        
        #used for measuring optical density
        self.absorbance_plate = None
        self.next_absorbance_plate_index = 0
        
        super(CustomProtocol,self).__init__() 
Example 31
Project: incubator-spot   Author: apache   File: proxy.py    Apache License 2.0 5 votes vote down vote up
def create_storyboard(uri,date,title,text,expanded_search,top_results):

    clientips  = defaultdict(int)
    reqmethods = defaultdict(int)
    rescontype = defaultdict(int)
    referers   = defaultdict(int)
    refered    = defaultdict(int)
    requests   = []


    for row in expanded_search:
        clientips[row['clientIp']]+=1
        reqmethods[row['requestMethod']]+=1
        rescontype[row['responseContentType']]+=1
        if row['uri'] == uri:
           #Source URI's that refered the user to the threat
           referers[row['referer']]+=1
           requests += [{'clientip':row['clientIp'], 'referer':row['referer'],'reqmethod':row['requestMethod'], 'resconttype':row['responseContentType']}]

        else:
            #Destination URI's refered by the threat
            refered[row['uri']]+=1

    create_incident_progression(uri,requests,refered,date)
    create_timeline(uri,clientips,date,top_results)
    save_comments(uri,title,text,date)

    return True 
Example 32
Project: pyblish-win   Author: pyblish   File: __init__.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def net_if_addrs():
    """Return the addresses associated to each NIC (network interface
    card) installed on the system as a dictionary whose keys are the
    NIC names and value is a list of namedtuples for each address
    assigned to the NIC. Each namedtuple includes 4 fields:

     - family
     - address
     - netmask
     - broadcast

    'family' can be either socket.AF_INET, socket.AF_INET6 or
    psutil.AF_LINK, which refers to a MAC address.
    'address' is the primary address, 'netmask' and 'broadcast'
    may be None.
    Note: you can have more than one address of the same family
    associated with each interface.
    """
    has_enums = sys.version_info >= (3, 4)
    if has_enums:
        import socket
    rawlist = _psplatform.net_if_addrs()
    rawlist.sort(key=lambda x: x[1])  # sort by family
    ret = collections.defaultdict(list)
    for name, fam, addr, mask, broadcast in rawlist:
        if has_enums:
            try:
                fam = socket.AddressFamily(fam)
            except ValueError:
                if os.name == 'nt' and fam == -1:
                    fam = _psplatform.AF_LINK
                elif (hasattr(_psplatform, "AF_LINK") and
                        _psplatform.AF_LINK == fam):
                    # Linux defines AF_LINK as an alias for AF_PACKET.
                    # We re-set the family here so that repr(family)
                    # will show AF_LINK rather than AF_PACKET
                    fam = _psplatform.AF_LINK
        ret[name].append(_common.snic(fam, addr, mask, broadcast))
    return dict(ret) 
Example 33
Project: pyblish-win   Author: pyblish   File: test_defaultdict.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_basic(self):
        d1 = defaultdict()
        self.assertEqual(d1.default_factory, None)
        d1.default_factory = list
        d1[12].append(42)
        self.assertEqual(d1, {12: [42]})
        d1[12].append(24)
        self.assertEqual(d1, {12: [42, 24]})
        d1[13]
        d1[14]
        self.assertEqual(d1, {12: [42, 24], 13: [], 14: []})
        self.assertTrue(d1[12] is not d1[13] is not d1[14])
        d2 = defaultdict(list, foo=1, bar=2)
        self.assertEqual(d2.default_factory, list)
        self.assertEqual(d2, {"foo": 1, "bar": 2})
        self.assertEqual(d2["foo"], 1)
        self.assertEqual(d2["bar"], 2)
        self.assertEqual(d2[42], [])
        self.assertIn("foo", d2)
        self.assertIn("foo", d2.keys())
        self.assertIn("bar", d2)
        self.assertIn("bar", d2.keys())
        self.assertIn(42, d2)
        self.assertIn(42, d2.keys())
        self.assertNotIn(12, d2)
        self.assertNotIn(12, d2.keys())
        d2.default_factory = None
        self.assertEqual(d2.default_factory, None)
        try:
            d2[15]
        except KeyError, err:
            self.assertEqual(err.args, (15,)) 
Example 34
Project: pyblish-win   Author: pyblish   File: test_defaultdict.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_shallow_copy(self):
        d1 = defaultdict(foobar, {1: 1})
        d2 = copy.copy(d1)
        self.assertEqual(d2.default_factory, foobar)
        self.assertEqual(d2, d1)
        d1.default_factory = list
        d2 = copy.copy(d1)
        self.assertEqual(d2.default_factory, list)
        self.assertEqual(d2, d1) 
Example 35
Project: pyblish-win   Author: pyblish   File: test_defaultdict.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_deep_copy(self):
        d1 = defaultdict(foobar, {1: [1]})
        d2 = copy.deepcopy(d1)
        self.assertEqual(d2.default_factory, foobar)
        self.assertEqual(d2, d1)
        self.assertTrue(d1[1] is not d2[1])
        d1.default_factory = list
        d2 = copy.deepcopy(d1)
        self.assertEqual(d2.default_factory, list)
        self.assertEqual(d2, d1) 
Example 36
Project: pyblish-win   Author: pyblish   File: test_defaultdict.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_callable_arg(self):
        self.assertRaises(TypeError, defaultdict, {}) 
Example 37
Project: pyblish-win   Author: pyblish   File: test_set.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_cuboctahedron(self):

        # http://en.wikipedia.org/wiki/Cuboctahedron
        # 8 triangular faces and 6 square faces
        # 12 indentical vertices each connecting a triangle and square

        g = cube(3)
        cuboctahedron = linegraph(g)            # V( --> {V1, V2, V3, V4}
        self.assertEqual(len(cuboctahedron), 12)# twelve vertices

        vertices = set(cuboctahedron)
        for edges in cuboctahedron.values():
            self.assertEqual(len(edges), 4)     # each vertex connects to four other vertices
        othervertices = set(edge for edges in cuboctahedron.values() for edge in edges)
        self.assertEqual(vertices, othervertices)   # edge vertices in original set

        cubofaces = faces(cuboctahedron)
        facesizes = collections.defaultdict(int)
        for face in cubofaces:
            facesizes[len(face)] += 1
        self.assertEqual(facesizes[3], 8)       # eight triangular faces
        self.assertEqual(facesizes[4], 6)       # six square faces

        for vertex in cuboctahedron:
            edge = vertex                       # Cuboctahedron vertices are edges in Cube
            self.assertEqual(len(edge), 2)      # Two cube vertices define an edge
            for cubevert in edge:
                self.assertIn(cubevert, g)


#============================================================================== 
Example 38
Project: arm_now   Author: nongiach   File: download.py    MIT License 5 votes vote down vote up
def scrawl_kernel(arch):
    re_href = re.compile('href="?({arch}[^ <>"]*)"?'.format(arch=arch))
    url = "https://toolchains.bootlin.com/downloads/releases/toolchains/{arch}/test-system/".format(arch=arch)
    response = requests.get(url + "?C=M;O=D")
    text = response.text
    links = re_href.findall(text)
    links_dict = defaultdict(lambda: defaultdict(dict))
    for link in links:
        version = get_link_version(link)
        libc = get_link_libc(link)
        filetype = get_link_filetype(link)

        # TODO: make sure they have been compiled at the same time
        if filetype not in links_dict[version][libc]:
            if filetype is None:
                return None, None, None
            links_dict[version][libc][filetype] = url + link

    state = "bleeding-edge"
    if "stable" in links_dict:
        state = "stable"

    for libc in ["glibc", "uclibc", "musl"]:
        if libc in links_dict[state]:
            break
    else:
        libc = None

    target = links_dict[state][libc]

    dtb = target.get("dtb", None)
    rootfs = target.get("rootfs", None)
    kernel = target.get("kernel", None)

    return kernel, dtb, rootfs 
Example 39
Project: broadway   Author: greencase   File: eventbus.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._subscribers = defaultdict(set) 
Example 40
Project: BASS   Author: Cisco-Talos   File: binary_database.py    GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, data):
        self.data = data
        self.callees = defaultdict(set)
        for func in self.data["functions"]:
            for call in func["called_from"]:
                self.callees[call].add(func["entry_point"]) 
Example 41
Project: BASS   Author: Cisco-Talos   File: binary_database.py    GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, data):
        self.data = data
        self.callees = defaultdict(set)
        for func in self.data["functions"]:
            for call in func["called_from"]:
                self.callees[call].add(func["entry_point"]) 
Example 42
Project: Att-ChemdNER   Author: lingluodlut   File: common.py    Apache License 2.0 5 votes vote down vote up
def reset_uids():
    global _UID_PREFIXES
    _UID_PREFIXES = defaultdict(int) 
Example 43
Project: wikilinks   Author: trovdimi   File: compare_hypothesis.py    MIT License 5 votes vote down vote up
def dd():
    return defaultdict(float) 
Example 44
Project: wikilinks   Author: trovdimi   File: pickle_data.py    MIT License 5 votes vote down vote up
def dd():
    return defaultdict(float) 
Example 45
Project: wikilinks   Author: trovdimi   File: check.py    MIT License 5 votes vote down vote up
def get_article2outlinks(self, clickstream_date):
        print('get_article2outlinks...')
        article2outlinks = collections.defaultdict(set)
        fname = '/home/ddimitrov/data/enwiki20150304_plus_clickstream/' + clickstream_date + '_clickstream.tsv'
        regex = re.compile(r'^([\d]+)\t([\d]+)')
        with open(fname) as infile:
            for lindex, line in enumerate(infile):
                if (lindex % 10000) == 0:
                    print('\r', lindex, end='')
                line = line.decode('utf-8')
                for left, right in regex.findall(line):
                    article2outlinks[int(left)].add(int(right))
        print()
        return article2outlinks 
Example 46
Project: everyclass-server   Author: everyclass   File: query.py    Mozilla Public License 2.0 5 votes vote down vote up
def get_teacher(url_tid, url_semester):
    """老师查询"""

    # decrypt identifier in URL
    try:
        _, teacher_id = decrypt(url_tid, resource_type='teacher')
    except ValueError:
        return render_template("common/error.html", message=MSG_INVALID_IDENTIFIER)

    # RPC to get teacher timetable
    with tracer.trace('rpc_get_teacher_timetable'):
        try:
            teacher = Entity.get_teacher_timetable(teacher_id, url_semester)
        except Exception as e:
            return handle_exception_with_error_page(e)

    with tracer.trace('process_rpc_result'):
        cards = defaultdict(list)
        for card in teacher.cards:
            day, time = lesson_string_to_tuple(card.lesson)
            if (day, time) not in cards:
                cards[(day, time)] = list()
            cards[(day, time)].append(card)

    empty_5, empty_6, empty_sat, empty_sun = _empty_column_check(cards)

    available_semesters = semester_calculate(url_semester, teacher.semesters)

    return render_template('query/teacher.html',
                           teacher=teacher,
                           cards=cards,
                           empty_sat=empty_sat,
                           empty_sun=empty_sun,
                           empty_6=empty_6,
                           empty_5=empty_5,
                           available_semesters=available_semesters,
                           current_semester=url_semester) 
Example 47
Project: everyclass-server   Author: everyclass   File: query.py    Mozilla Public License 2.0 5 votes vote down vote up
def get_classroom(url_rid, url_semester):
    """教室查询"""
    # decrypt identifier in URL
    try:
        _, room_id = decrypt(url_rid, resource_type='room')
    except ValueError:
        return render_template("common/error.html", message=MSG_INVALID_IDENTIFIER)

    # RPC to get classroom timetable
    with tracer.trace('rpc_get_classroom_timetable'):
        try:
            room = Entity.get_classroom_timetable(url_semester, room_id)
        except Exception as e:
            return handle_exception_with_error_page(e)

    with tracer.trace('process_rpc_result'):
        cards = defaultdict(list)
        for card in room.cards:
            day, time = lesson_string_to_tuple(card.lesson)
            cards[(day, time)].append(card)

    empty_5, empty_6, empty_sat, empty_sun = _empty_column_check(cards)

    available_semesters = semester_calculate(url_semester, room.semesters)

    return render_template('query/room.html',
                           room=room,
                           cards=cards,
                           empty_sat=empty_sat,
                           empty_sun=empty_sun,
                           empty_6=empty_6,
                           empty_5=empty_5,
                           available_semesters=available_semesters,
                           current_semester=url_semester) 
Example 48
Project: pywarp   Author: pyauth   File: test.py    Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.users = collections.defaultdict(collections.defaultdict) 
Example 49
Project: AutoDL   Author: tanguofu   File: app.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, name=None, router=None, error_handler=None,
                 load_env=True, request_class=None,
                 strict_slashes=False, log_config=None,
                 configure_logging=True):

        # Get name from previous stack frame
        if name is None:
            frame_records = stack()[1]
            name = getmodulename(frame_records[1])

        # logging
        if configure_logging:
            logging.config.dictConfig(log_config or LOGGING_CONFIG_DEFAULTS)

        self.name = name
        self.router = router or Router()
        self.request_class = request_class
        self.error_handler = error_handler or ErrorHandler()
        self.config = Config(load_env=load_env)
        self.request_middleware = deque()
        self.response_middleware = deque()
        self.blueprints = {}
        self._blueprint_order = []
        self.configure_logging = configure_logging
        self.debug = None
        self.sock = None
        self.strict_slashes = strict_slashes
        self.listeners = defaultdict(list)
        self.is_running = False
        self.is_request_stream = False
        self.websocket_enabled = False
        self.websocket_tasks = set()

        # Register alternative method names
        self.go_fast = self.run 
Example 50
Project: AutoDL   Author: tanguofu   File: router.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self):
        self.routes_all = {}
        self.routes_names = {}
        self.routes_static_files = {}
        self.routes_static = {}
        self.routes_dynamic = defaultdict(list)
        self.routes_always_check = []
        self.hosts = set()