Python six.moves.intern() Examples

The following are 18 code examples of six.moves.intern(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module six.moves , or try the search function .
Example #1
Source File: translator.py    From loopy with MIT License 6 votes vote down vote up
def add_expression_instruction(self, lhs, rhs):
        scope = self.scope_stack[-1]

        new_id = intern("insn%d" % self.insn_id_counter)
        self.insn_id_counter += 1

        from loopy.kernel.data import Assignment
        insn = Assignment(
                lhs, rhs,
                within_inames=frozenset(
                    scope.active_loopy_inames),
                id=new_id,
                predicates=frozenset(self.conditions),
                tags=tuple(self.instruction_tags))

        scope.previous_instruction_id = new_id
        scope.instructions.append(insn)

    # {{{ map_XXX functions 
Example #2
Source File: translator.py    From loopy with MIT License 6 votes vote down vote up
def realize_conditional(self, node, context_cond=None):
        scope = self.scope_stack[-1]

        cond_name = intern("loopy_cond%d" % self.condition_id_counter)
        self.condition_id_counter += 1
        assert cond_name not in scope.type_map

        scope.type_map[cond_name] = np.int32

        from pymbolic import var
        cond_var = var(cond_name)

        self.add_expression_instruction(
                cond_var, self.parse_expr(node, node.expr))

        cond_expr = cond_var
        if context_cond is not None:
            from pymbolic.primitives import LogicalAnd
            cond_expr = LogicalAnd((cond_var, context_cond))

            self.conditions_data.append((context_cond, cond_var))
        else:
            self.conditions_data.append((None, cond_var))

        self.conditions.append(cond_expr) 
Example #3
Source File: probe.py    From Xpedite with Apache License 2.0 6 votes vote down vote up
def __init__(self, name, filePath, lineNo, attributes, isActive, sysName=None):
    """
    Constructs an anchored probe

    :param name: User friendly name for use in reports
    :param filePath: Path of the source file containing the probe
    :param lineNo: Line number of the statement containing the probe
    :param attributes: Attributes associated with this probe
    :param sysName: Name of the probe, as defined in the instrumented source file
    """
    AbstractProbe.__init__(self, name)
    if not (filePath and lineNo):
      errMsg = """Argument exception - failed to build anchored probe {}.
        must have valid value for <filePath> and <lineNo>""".format(name)
      raise Exception(errMsg)

    self.sysName = sysName
    self.filePath = intern(filePath)
    self.path = intern(os.path.dirname(filePath))
    self.fileName = intern(os.path.basename(filePath))
    self.lineNo = int(lineNo)
    self.attributes = attributes
    self.isActive = isActive
    self.initAttributes() 
Example #4
Source File: normalization.py    From pyensembl with Apache License 2.0 5 votes vote down vote up
def normalize_chromosome(c):
    try:
        return NORMALIZE_CHROMOSOME_CACHE[c]
    except KeyError:
        pass

    if not (is_string(c) or is_integer(c)):
        raise TypeError("Chromosome cannot be '%s' : %s" % (c, type(c)))

    result = str(c)

    if result == "0":
        raise ValueError("Chromosome name cannot be 0")
    elif result == "":
        raise ValueError("Chromosome name cannot be empty")

    if result.startswith("chr") and "_" not in result:
        # excluding "_" for names like "chrUn_gl000212"
        # capitalize "chrx" -> "chrX"
        result = "chr" + result[3:].upper()
    elif result.isalpha():
        # capitalize e.g. "x" -> "X"
        result = result.upper()

    # interning strings since the chromosome names probably get constructed
    # or parsed millions of times, can save memory in tight situations
    # (such as parsing GTF files)
    result = intern(result)

    NORMALIZE_CHROMOSOME_CACHE[c] = result

    return result 
Example #5
Source File: sniffer.py    From thrift-tools with Apache License 2.0 5 votes vote down vote up
def run(self, *args, **kwargs):
        """ Deal with the incoming packets """
        while True:
            try:
                timestamp, ip_p = self._queue.popleft()

                src_ip = get_ip(ip_p, ip_p.src)
                dst_ip = get_ip(ip_p, ip_p.dst)

                src = intern('%s:%s' % (src_ip, ip_p.data.sport))
                dst = intern('%s:%s' % (dst_ip, ip_p.data.dport))
                key = intern('%s<->%s' % (src, dst))

                stream = self._streams.get(key)
                if stream is None:
                    stream = Stream(src, dst)
                    self._streams[key] = stream

                # HACK: save the timestamp
                setattr(ip_p, 'timestamp', timestamp)
                pushed = stream.push(ip_p)

                if not pushed:
                    continue

                # let listeners know about the updated stream
                for handler in self._handlers:
                    try:
                        handler(stream)
                    except Exception as ex:
                        print('handler exception: %s' % ex)
            except Exception as ex:
                time.sleep(0.00001) 
Example #6
Source File: loaders.py    From zktraffic with Apache License 2.0 5 votes vote down vote up
def __init__(self, max_reqs=400000, max_reps=400000, max_events=400000, timer=None):
    self._accumulators = {}
    self._cv = Condition()
    self._stopped = True
    self._requests = Deque(maxlen=max_reqs)
    self._replies = Deque(maxlen=max_reps)
    self._events = Deque(maxlen=max_events)
    self._request_handlers = set()
    self._reply_handlers = set()
    self._event_handlers = set()
    self._auth_by_client = defaultdict(lambda: intern("noauth"))
    self._timer = timer if timer else Timer()
    super(QueueStatsLoader, self).__init__()
    self.setDaemon(True) 
Example #7
Source File: accumulators.py    From zktraffic with Apache License 2.0 5 votes vote down vote up
def get_path(self, message, suffix=None):
    if self._aggregation_depth > 0 and message.path:
      path = message.parent_path(self._aggregation_depth)
    else:
      path = message.path

    return intern(path if suffix is None else ':'.join((path, suffix))) 
Example #8
Source File: client_message.py    From zktraffic with Apache License 2.0 5 votes vote down vote up
def with_params(cls, xid, path, watch, data, offset, size, client, server):
    auth_type, offset = read_number(data, offset)
    scheme = "very-long-scheme"
    credential = "very-long-credential"

    try:
      scheme, offset = read_string(data, offset)
      credential, offset = read_string(data, offset)
    except StringTooLong: pass

    return cls(auth_type, intern(scheme), intern(credential), size, client, server) 
Example #9
Source File: client_message.py    From zktraffic with Apache License 2.0 5 votes vote down vote up
def __init__(self, size, xid, path, client, watch, server):
    self.size = size
    self.xid = xid
    self.path = intern(path)
    self.client = client
    self.server = server
    self.watch = watch
    self.timestamp = 0  # this will be set by caller later on
    self.auth = ""      # ditto 
Example #10
Source File: circlist.py    From ryu with Apache License 2.0 5 votes vote down vote up
def __init__(self, next_attr_name=None, prev_attr_name=None):
        """Initializes this list.

        next_attr_name: The name of the attribute that holds a reference
                        to the next item in the list.

        prev_attr_name: the name of the attribute that holds a reference
                        to the previous item in the list.
        """

        # Keep an interned version of the attribute names. This should
        # speed up the process of looking up the attributes.
        self.next_name = intern(next_attr_name)
        self.prev_name = intern(prev_attr_name) 
Example #11
Source File: internable.py    From ryu with Apache License 2.0 5 votes vote down vote up
def intern(self):
        """Returns either itself or a canonical copy of itself."""

        # If this is an interned object, return it
        if hasattr(self, '_interned'):
            return self._internable_stats.incr('self')

        #
        # Got to find or create an interned object identical to this
        # one. Auto-initialize the class if need be.
        #
        kls = self.__class__

        if not hasattr(kls, dict_name):
            kls._internable_init()

        obj = kls._internable_dict.get(self)
        if (obj):
            # Found an interned copy.
            kls._internable_stats.incr('found')
            return obj

        # Create an interned copy. Take care to only keep a weak
        # reference to the object itself.
        def object_collected(obj):
            kls._internable_stats.incr('collected')
            # print("Object %s garbage collected" % obj)
            pass

        ref = weakref.ref(self, object_collected)
        kls._internable_dict[self] = ref
        self._interned = True
        kls._internable_stats.incr('inserted')
        return self 
Example #12
Source File: instruction.py    From loopy with MIT License 5 votes vote down vote up
def __setstate__(self, val):
        super(InstructionBase, self).__setstate__(val)

        from loopy.tools import intern_frozenset_of_ids

        if self.id is not None:  # pylint:disable=access-member-before-definition
            self.id = intern(self.id)
        self.depends_on = intern_frozenset_of_ids(self.depends_on)
        self.groups = intern_frozenset_of_ids(self.groups)
        self.conflicts_with_groups = (
                intern_frozenset_of_ids(self.conflicts_with_groups))
        self.within_inames = (
                intern_frozenset_of_ids(self.within_inames))

# }}} 
Example #13
Source File: data.py    From loopy with MIT License 5 votes vote down vote up
def __init__(self, **kwargs):
        kwargs["name"] = intern(kwargs.pop("name"))

        target = kwargs.pop("target", None)

        dtype = kwargs.pop("dtype", None)

        if 'for_atomic' in kwargs:
            for_atomic = kwargs['for_atomic']
        else:
            for_atomic = False

        from loopy.types import to_loopy_type
        dtype = to_loopy_type(
                dtype, allow_auto=True, allow_none=True, for_atomic=for_atomic,
                target=target)

        import loopy as lp
        if dtype is lp.auto:
            warn("Argument/temporary data type for '%s' should be None if "
                   "unspecified, not auto. This usage will be disallowed in 2018."
                    % kwargs["name"],
                    DeprecationWarning, stacklevel=2)

            dtype = None

        kwargs["dtype"] = dtype

        ImmutableRecord.__init__(self, **kwargs) 
Example #14
Source File: __init__.py    From loopy with MIT License 5 votes vote down vote up
def all_inames(self):
        result = set()
        for dom in self.domains:
            result.update(
                    intern(n) for n in dom.get_var_names(dim_type.set))
        return frozenset(result) 
Example #15
Source File: creation.py    From loopy with MIT License 5 votes vote down vote up
def parse_special_insn(groups, insn_options):
    insn_options = parse_insn_options(
            insn_options.copy(),
            groups["options"],
            assignee_names=())

    del insn_options["atomicity"]

    insn_id = insn_options.pop("insn_id", None)
    inames_to_dup = insn_options.pop("inames_to_dup", [])

    kwargs = dict(
                id=(
                    intern(insn_id)
                    if isinstance(insn_id, str)
                    else insn_id),
                **insn_options)

    from loopy.kernel.instruction import NoOpInstruction, BarrierInstruction
    special_insn_kind = groups["kind"]
    # check for bad options
    check_illegal_options(insn_options, special_insn_kind)

    if special_insn_kind == "gbarrier":
        cls = BarrierInstruction
        kwargs["synchronization_kind"] = "global"
    elif special_insn_kind == "lbarrier":
        cls = BarrierInstruction
        kwargs["synchronization_kind"] = "local"
    elif special_insn_kind == "nop":
        cls = NoOpInstruction
    else:
        raise LoopyError(
            "invalid kind of special instruction: '%s'" % special_insn_kind)

    return cls(**kwargs), inames_to_dup

# }}}


# {{{ parse_instructions 
Example #16
Source File: tools.py    From loopy with MIT License 5 votes vote down vote up
def intern_frozenset_of_ids(fs):
    return frozenset(intern(s) for s in fs)

# vim: foldmethod=marker 
Example #17
Source File: tools.py    From loopy with MIT License 5 votes vote down vote up
def is_interned(s):
    return s is None or intern(s) is s 
Example #18
Source File: sniffer.py    From zktraffic with Apache License 2.0 4 votes vote down vote up
def message_from_packet(self, packet):
    """
    :returns: Returns an instance of ClientMessage or ServerMessage (or a subclass)
    :raises:
      :exc:`BadPacket` if the packet is for a client we are not tracking
      :exc:`DeserializationError` if deserialization failed
      :exc:`struct.error` if deserialization failed
    """
    client_port = self.config.client_port
    zk_port = self.config.zookeeper_port
    ip_p = get_ip_packet(packet.load, client_port, zk_port, self.config.is_loopback)

    if 0 == len(ip_p.data.data):
      return None

    if ip_p.data.dport == zk_port:
      data = ip_p.data.data
      src = intern("%s:%s" % (get_ip(ip_p, ip_p.src), ip_p.data.sport))
      dst = intern("%s:%s" % (get_ip(ip_p, ip_p.dst), ip_p.data.dport))
      client, server = src, dst
      if data.startswith(FOUR_LETTER_WORDS):
        self._set_four_letter_mode(client, data[0:4])
        raise BadPacket("Four letter request %s" % data[0:4])
      client_message = ClientMessage.from_payload(data, client, server)
      client_message.timestamp = packet.time
      self._track_client_message(client_message)
      return client_message

    if ip_p.data.sport == zk_port:
      data = ip_p.data.data
      src = intern("%s:%s" % (get_ip(ip_p, ip_p.src), ip_p.data.sport))
      dst = intern("%s:%s" % (get_ip(ip_p, ip_p.dst), ip_p.data.dport))
      client, server = dst, src
      four_letter = self._get_four_letter_mode(client)
      if four_letter:
        self._set_four_letter_mode(client, None)
        raise BadPacket("Four letter response %s" % four_letter)
      requests_xids = self._requests_xids.get(client, {})
      server_message = ServerMessage.from_payload(data, client, server, requests_xids)
      server_message.timestamp = packet.time
      return server_message

    raise BadPacket("Packet to the wrong port?")