Exemple #1
0
def _unbool(element, true=object(), false=object()):
    """A hack to make True and 1 and False and 0 unique for _uniq."""
    if element is True:
        return true
    elif element is False:
        return false
    return element
Exemple #2
0
class CounterName(_CounterName):
    """Naming information for a counter."""
    SYSTEM = object()
    USER = object()

    def __new__(cls,
                name,
                stage_name=None,
                step_name=None,
                system_name=None,
                namespace=None,
                origin=None,
                output_index=None,
                io_target=None):
        origin = origin or CounterName.SYSTEM
        return super(CounterName,
                     cls).__new__(cls, name, stage_name, step_name,
                                  system_name, namespace, origin, output_index,
                                  io_target)

    def __repr__(self):
        return '<CounterName<%s> at %s>' % (self._str_internal(), hex(
            id(self)))

    def __str__(self):
        return self._str_internal()

    def _str_internal(self):
        if self.origin == CounterName.USER:
            return 'user-%s-%s' % (self.step_name, self.name)
        elif self.origin == CounterName.SYSTEM and self.output_index:
            return '%s-out%s-%s' % (self.step_name, self.output_index,
                                    self.name)
        else:
            return '%s-%s-%s' % (self.stage_name, self.step_name, self.name)
Exemple #3
0
class SourceRelationMeta(ClassMeta):
    """The meta data for a source object in a relationship"""

    # Cardinality constants
    ONE_TO_ONE = object()
    ONE_TO_M = object()
    N_TO_ONE = object()
    N_TO_M = object()

    def __init__(self, className, targetClassName):
        """Initialize a source relationship meta object
        
        Args:
          className (str): The source Mo class name for the relationship
          targetClassName (str): The target class name for the relationship
        """
        ClassMeta.__init__(self, className)
        self.targetClassName = targetClassName
        self.cardinality = None
        self.isRelation = True
        self.isSource = True
        self.isExplicit = True

    def getTargetClass(self):
        """Imports and returns the target class for a relationship

        Returns:
          cobra.mit.mo.Mo: The target class
        """
        return ClassLoader.loadClass(self.targetClassName)
Exemple #4
0
 def test_remove_tracer(self):
     """The C{remote_tracer} function removes a specific tracer."""
     tracer1 = object()
     tracer2 = object()
     install_tracer(tracer1)
     install_tracer(tracer2)
     remove_tracer(tracer1)
     self.assertEquals(get_tracers(), [tracer2])
        def execute(self, slot, *args, **kwargs):
            if slot == self.OutputList:
                return [1, 2, 3]

            elif slot == self.OutputOpaque:
                return object()

            elif slot == self.OutputUnsupportedType:
                return object()

            else:
                return numpy.ones((2, 2), dtype=int)
Exemple #6
0
def all_equal(iter1, iter2):
    # Sentinel object used to check that both iterators are the same length
    diff_length_sentinel = object()

    try:
        if iter1 == iter2:
            return True
    except ValueError:
        pass

    if iter1 is None and iter2 is None:
        return True

    try:
        i1 = iter(iter1)
        i2 = iter(iter2)
    except TypeError:
        return iter1 == iter2

    for [ip1, ip2] in zip_longest(i1, i2,
                                  fillvalue=diff_length_sentinel):
        # Verify that none of the lists has ended (then they are not the
        # same size)
        if ip1 is diff_length_sentinel or ip2 is diff_length_sentinel:
            return False

        if not all_equal(ip1, ip2):
            return False

    return True
Exemple #7
0
    def test_auto_remove_change_notification_with_arg(self):
        changes1 = []
        changes2 = []

        def object_changed1(obj_info, variable, old_value, new_value, fromdb,
                            arg):
            changes1.append(
                (1, obj_info, variable, old_value, new_value, fromdb, arg))
            return False

        def object_changed2(obj_info, variable, old_value, new_value, fromdb,
                            arg):
            changes2.append(
                (2, obj_info, variable, old_value, new_value, fromdb, arg))
            return False

        self.obj_info.checkpoint()

        obj = object()

        self.obj_info.event.hook("changed", object_changed1, obj)
        self.obj_info.event.hook("changed", object_changed2, obj)

        self.obj.prop2 = 20
        self.obj.prop1 = 10

        self.assertEquals(
            changes1,
            [(1, self.obj_info, self.variable2, Undef, 20, False, obj)])
        self.assertEquals(
            changes2,
            [(2, self.obj_info, self.variable2, Undef, 20, False, obj)])
Exemple #8
0
    def test_timedelta(self):
        self.setup(TimeDelta,
                   default=timedelta(days=1, seconds=2, microseconds=3),
                   allow_none=False)

        self.assertTrue(isinstance(self.column1, Column))
        self.assertTrue(isinstance(self.column2, Column))
        self.assertEquals(self.column1.name, "column1")
        self.assertEquals(self.column1.table, self.SubClass)
        self.assertEquals(self.column2.name, "prop2")
        self.assertEquals(self.column2.table, self.SubClass)
        self.assertTrue(isinstance(self.variable1, TimeDeltaVariable))
        self.assertTrue(isinstance(self.variable2, TimeDeltaVariable))

        self.assertEquals(self.obj.prop1,
                          timedelta(days=1, seconds=2, microseconds=3))
        self.assertRaises(NoneError, setattr, self.obj, "prop1", None)
        self.obj.prop2 = None
        self.assertEquals(self.obj.prop2, None)

        self.obj.prop1 = timedelta(days=42, seconds=42, microseconds=42)
        self.assertEquals(self.obj.prop1,
                          timedelta(days=42, seconds=42, microseconds=42))

        self.assertRaises(TypeError, setattr, self.obj, "prop1", object())
Exemple #9
0
def all_almost_equal(iter1, iter2, places=None):
    """`True` if all elements in ``a`` and ``b`` are almost equal."""
    try:
        if iter1 is iter2 or iter1 == iter2:
            return True
    except ValueError:
        pass

    if iter1 is None and iter2 is None:
        return True

    if places is None:
        places = _places(iter1, iter2, None)

    if hasattr(iter1, '__array__') and hasattr(iter2, '__array__'):
        return all_almost_equal_array(iter1, iter2, places)

    try:
        it1 = iter(iter1)
        it2 = iter(iter2)
    except TypeError:
        return almost_equal(iter1, iter2, places)

    diff_length_sentinel = object()
    for [ip1, ip2] in zip_longest(it1, it2,
                                  fillvalue=diff_length_sentinel):
        # Verify that none of the lists has ended (then they are not the
        # same size)
        if ip1 is diff_length_sentinel or ip2 is diff_length_sentinel:
            return False

        if not all_almost_equal(ip1, ip2, places):
            return False

    return True
Exemple #10
0
 def __eq__(self, other):
     if isinstance(other, collections.Iterable):
         return all(
             a == b
             for a, b in zip_longest(self, other, fillvalue=object()))
     else:
         return NotImplemented
Exemple #11
0
 def __eq__(self, other):
   if isinstance(other, collections.Iterable):
     return all(
         a == b
         for a, b in zip_longest(self, other, fillvalue=object()))
   else:
     return NotImplemented
Exemple #12
0
    def resize(self, maxsize):
        """ Resize the cache.  Increasing the size of the cache is non-destructive, i.e.,
        previously cached inputs remain in the cache.  Decreasing the size of the cache will
        necessarily remove items from the cache if the cache is already filled.  Items are removed
        in least recently used order.

        @param maxsize  The new maximum number of inputs to cache.
        """
        oldsize = len(self.cache)
        if maxsize == oldsize:
            return
        else:
            root = self.root
            cache = self.cache
            if maxsize < oldsize:
                for i in range(oldsize - maxsize):
                    # Delete root.next
                    current_next_link = root[1]
                    new_next_link = root[1] = root[1][1]
                    new_next_link[0] = root
                    del cache[current_next_link[2]]
            elif maxsize > oldsize:
                for i in range(maxsize - oldsize):
                    # Insert between root and root.next
                    key = object()
                    cache[key] = link = [root, root[1], key, None]
                    root[1][0] = link
                    root[1] = link
            else:
                raise ValueError("Invalid maxsize: {0:}".format(maxsize))
Exemple #13
0
def all_almost_equal(iter1, iter2, places=None):
    """Return ``True`` if all elements in ``a`` and ``b`` are almost equal."""
    try:
        if iter1 is iter2 or iter1 == iter2:
            return True
    except ValueError:
        pass

    if iter1 is None and iter2 is None:
        return True

    if hasattr(iter1, '__array__') and hasattr(iter2, '__array__'):
        # Only get default places if comparing arrays, need to keep `None`
        # otherwise for recursive calls.
        if places is None:
            places = _places(iter1, iter2, None)
        return all_almost_equal_array(iter1, iter2, places)

    try:
        it1 = iter(iter1)
        it2 = iter(iter2)
    except TypeError:
        return almost_equal(iter1, iter2, places)

    diff_length_sentinel = object()
    for [ip1, ip2] in zip_longest(it1, it2, fillvalue=diff_length_sentinel):
        # Verify that none of the lists has ended (then they are not the
        # same size)
        if ip1 is diff_length_sentinel or ip2 is diff_length_sentinel:
            return False

        if not all_almost_equal(ip1, ip2, places):
            return False

    return True
Exemple #14
0
    def run(self):
        control_stub = beam_fn_api_pb2_grpc.BeamFnControlStub(
            self._control_channel)
        no_more_work = object()

        # Create workers
        bundle_processor_cache = BundleProcessorCache(
            state_handler_factory=self._state_handler_factory,
            data_channel_factory=self._data_channel_factory,
            fns=self._fns)
        for _ in range(self._worker_count):
            # SdkHarness manage function registration and share self._fns with all
            # the workers. This is needed because function registration (register)
            # and exceution(process_bundle) are send over different request and we
            # do not really know which woker is going to process bundle
            # for a function till we get process_bundle request. Moreover
            # same function is reused by different process bundle calls and
            # potentially get executed by different worker. Hence we need a
            # centralized function list shared among all the workers.
            self.workers.put(
                SdkWorker(bundle_processor_cache,
                          profiler_factory=self._profiler_factory))

        def get_responses():
            while True:
                response = self._responses.get()
                if response is no_more_work:
                    return
                yield response

        self._alive = True
        monitoring_thread = threading.Thread(
            target=self._monitor_process_bundle)
        monitoring_thread.daemon = True
        monitoring_thread.start()

        try:
            for work_request in control_stub.Control(get_responses()):
                logging.debug('Got work %s', work_request.instruction_id)
                request_type = work_request.WhichOneof('request')
                # Name spacing the request method with 'request_'. The called method
                # will be like self.request_register(request)
                getattr(self, SdkHarness.REQUEST_METHOD_PREFIX +
                        request_type)(work_request)
        finally:
            self._alive = False

        logging.info('No more requests from control plane')
        logging.info('SDK Harness waiting for in-flight requests to complete')
        # Wait until existing requests are processed.
        self._progress_thread_pool.shutdown()
        self._process_thread_pool.shutdown()
        # get_responses may be blocked on responses.get(), but we need to return
        # control to its caller.
        self._responses.put(no_more_work)
        # Stop all the workers and clean all the associated resources
        self._data_channel_factory.close()
        self._state_handler_factory.close()
        logging.info('Done consuming work.')
 def test_should_fail_if_service_is_bad(self):
     testfs = [
         lambda: self._get_registry(),
         lambda: service.MethodRegistry(None),
         lambda: service.MethodRegistry(object()),
     ]
     for f in testfs:
         expect(f).to(raise_error(ValueError))
Exemple #16
0
 def test_sign_invalid_pubkey(self):
     '''
     signing data using an invalid key-object fails
     '''
     priv, pub = rsa.create_signing_keypair(1024)
     with self.assertRaises(ValueError) as ctx:
         rsa.sign_data(object(), b"data")
     self.assertIn("must be an RSAPrivateKey", str(ctx.exception))
 def test_should_fail_if_service_is_bad(self):
     testfs = [
         lambda: self._get_registry(),
         lambda: service.MethodRegistry(None),
         lambda: service.MethodRegistry(object()),
     ]
     for f in testfs:
         expect(f).to(raise_error(ValueError))
Exemple #18
0
 def test_verify_invalid_pubkey(self):
     '''
     pubkey must be correct kind of object
     '''
     priv, pub = ed25519.create_signing_keypair()
     with self.assertRaises(ValueError) as ctx:
         ed25519.verify_signature(object(), b"signature", b"data")
     self.assertIn("must be an Ed25519PublicKey", str(ctx.exception))
Exemple #19
0
 def test_verify_invalid_pubkey(self):
     '''
     verifying a signature using an invalid key-object fails
     '''
     priv, pub = rsa.create_signing_keypair(1024)
     with self.assertRaises(ValueError) as ctx:
         rsa.verify_signature(object(), b"signature", b"data")
     self.assertIn("must be an RSAPublicKey", str(ctx.exception))
Exemple #20
0
class SourceRelationMeta(ClassMeta):
    # Cardinality constants
    ONE_TO_ONE = object()
    ONE_TO_M = object()
    N_TO_ONE = object()
    N_TO_M = object()

    def __init__(self, className, targetClassName):
        ClassMeta.__init__(self, className)
        self.targetClassName = targetClassName
        self.cardinality = None
        self.isRelation = True
        self.isSource = True
        self.isExplicit = True

    def getTargetClass(self):
        return ClassLoader.loadClass(self.targetClassName)
def dict_diff(left, right):
    u"""
    Computes a dictionary with the elements that are in the right but
    not or different in the left.
    """
    dummy = object()
    return dict([
        k_v for k_v in iter(right.items()) if left.get(k_v[0], dummy) != k_v[1]
    ])
Exemple #22
0
  def run(self):
    control_stub = beam_fn_api_pb2_grpc.BeamFnControlStub(self._control_channel)
    no_more_work = object()

    # Create workers
    for _ in range(self._worker_count):
      # SdkHarness manage function registration and share self._fns with all
      # the workers. This is needed because function registration (register)
      # and exceution(process_bundle) are send over different request and we
      # do not really know which woker is going to process bundle
      # for a function till we get process_bundle request. Moreover
      # same function is reused by different process bundle calls and
      # potentially get executed by different worker. Hence we need a
      # centralized function list shared among all the workers.
      self.workers.put(
          SdkWorker(
              state_handler_factory=self._state_handler_factory,
              data_channel_factory=self._data_channel_factory,
              fns=self._fns,
              profiler_factory=self._profiler_factory))

    def get_responses():
      while True:
        response = self._responses.get()
        if response is no_more_work:
          return
        yield response

    self._alive = True
    monitoring_thread = threading.Thread(target=self._monitor_process_bundle)
    monitoring_thread.daemon = True
    monitoring_thread.start()

    try:
      for work_request in control_stub.Control(get_responses()):
        logging.debug('Got work %s', work_request.instruction_id)
        request_type = work_request.WhichOneof('request')
        # Name spacing the request method with 'request_'. The called method
        # will be like self.request_register(request)
        getattr(self, SdkHarness.REQUEST_METHOD_PREFIX + request_type)(
            work_request)
    finally:
      self._alive = False

    logging.info('No more requests from control plane')
    logging.info('SDK Harness waiting for in-flight requests to complete')
    # Wait until existing requests are processed.
    self._progress_thread_pool.shutdown()
    self._process_thread_pool.shutdown()
    # get_responses may be blocked on responses.get(), but we need to return
    # control to its caller.
    self._responses.put(no_more_work)
    # Stop all the workers and clean all the associated resources
    self._data_channel_factory.close()
    self._state_handler_factory.close()
    logging.info('Done consuming work.')
Exemple #23
0
    def test_successResultOf_ok(self):
        """
        successResultOf will not fail if deferred has a result.
        """
        value = object()
        deferred = defer.succeed(value)

        result = self.successResultOf(deferred)

        self.assertEqual(value, result)
def memoize(func):
  cache = {}
  missing = object()

  def wrapper(*args):
    result = cache.get(args, missing)
    if result is missing:
      result = cache[args] = func(*args)
    return result
  return wrapper
Exemple #25
0
 def test_get_patch_module_no_sub_level(self):
     """
     The C{get_patch_module} method returns a dummy patch module if no
     sub-level file exists in the patch directory for the given version.
     """
     patch_1_dir = os.path.join(self.package_dir, "patch_1")
     os.makedirs(patch_1_dir)
     patch_module = self.patch_package.get_patch_module(1)
     store = object()
     self.assertIsNone(patch_module.apply(store))
Exemple #26
0
def memoize(func):
  cache = {}
  missing = object()

  def wrapper(*args):
    result = cache.get(args, missing)
    if result is missing:
      result = cache[args] = func(*args)
    return result
  return wrapper
Exemple #27
0
    def test_scoped_to_request(self):
        """Test that the cache is scoped to requests."""
        c = CacheClass()
        self.assertEqual(2, c.foo(1))
        self.mock.get_request.return_value = object()
        self.assertEqual(2, c.foo(1))

        self.assertListEqual([
            (1, ),
            (1, ),
        ], c.called)
Exemple #28
0
    def __init__(self,
                 channel,
                 queue_capacity=100,
                 batch_size=10,
                 flush_interval=DEFAULT_FLUSH_INTERVAL,
                 io_loop=None,
                 error_reporter=None,
                 metrics=None,
                 metrics_factory=None,
                 **kwargs):
        """
        :param channel: a communication channel to jaeger-agent
        :param queue_capacity: how many spans we can hold in memory before
            starting to drop spans
        :param batch_size: how many spans we can submit at once to Collector
        :param flush_interval: how often the auto-flush is called (in seconds)
        :param io_loop: which IOLoop to use. If None, try to get it from
            channel (only works if channel is tchannel.sync)
        :param error_reporter:
        :param metrics: an instance of Metrics class, or None. This parameter
            has been deprecated, please use metrics_factory instead.
        :param metrics_factory: an instance of MetricsFactory class, or None.
        :param kwargs:
            'logger'
        :return:
        """
        from threading import Lock

        self._channel = channel
        self.queue_capacity = queue_capacity
        self.batch_size = batch_size
        self.metrics_factory = metrics_factory or LegacyMetricsFactory(
            metrics or Metrics())
        self.metrics = ReporterMetrics(self.metrics_factory)
        self.error_reporter = error_reporter or ErrorReporter(Metrics())
        self.logger = kwargs.get('logger', default_logger)
        self.agent = Agent.Client(self._channel, self)

        if queue_capacity < batch_size:
            raise ValueError('Queue capacity cannot be less than batch size')

        self.io_loop = io_loop or channel.io_loop
        if self.io_loop is None:
            self.logger.error('Jaeger Reporter has no IOLoop')
        else:
            self.queue = tornado.queues.Queue(maxsize=queue_capacity)
            self.stop = object()
            self.stopped = False
            self.stop_lock = Lock()
            self.flush_interval = flush_interval or None
            self.io_loop.spawn_callback(self._consume_queue)

        self._process_lock = Lock()
        self._process = None
Exemple #29
0
class BeamFnControlServicer(beam_fn_api_pb2_grpc.BeamFnControlServicer):

  UNSTARTED_STATE = 'unstarted'
  STARTED_STATE = 'started'
  DONE_STATE = 'done'

  _DONE_MARKER = object()

  def __init__(self):
    self._push_queue = queue.Queue()
    self._futures_by_id = dict()
    self._read_thread = threading.Thread(
        name='beam_control_read', target=self._read)
    self._uid_counter = 0
    self._state = self.UNSTARTED_STATE
    self._lock = threading.Lock()

  def Control(self, iterator, context):
    with self._lock:
      if self._state == self.DONE_STATE:
        return
      else:
        self._state = self.STARTED_STATE
    self._inputs = iterator
    # Note: We only support one client for now.
    self._read_thread.start()
    while True:
      to_push = self._push_queue.get()
      if to_push is self._DONE_MARKER:
        return
      yield to_push

  def _read(self):
    for data in self._inputs:
      self._futures_by_id.pop(data.instruction_id).set(data)

  def push(self, item):
    if item is self._DONE_MARKER:
      future = None
    else:
      if not item.instruction_id:
        self._uid_counter += 1
        item.instruction_id = 'control_%s' % self._uid_counter
      future = ControlFuture(item.instruction_id)
      self._futures_by_id[item.instruction_id] = future
    self._push_queue.put(item)
    return future

  def done(self):
    with self._lock:
      if self._state == self.STARTED_STATE:
        self.push(self._DONE_MARKER)
        self._read_thread.join()
      self._state = self.DONE_STATE
Exemple #30
0
    def test_index_doc_callback_discriminator(self):
        OTHER_FACETS = ['foo', 'foo:bar', 'foo:baz']

        def _discrimintator(obj, default):
            return ['foo:bar']

        index = self._makeOne(_discrimintator, OTHER_FACETS)
        index.index_doc(1, object())
        self.assertEqual(list(index._fwd_index['foo']), [1])
        self.assertEqual(list(index._fwd_index['foo:bar']), [1])
        self.assertEqual(list(index._rev_index[1]), ['foo', 'foo:bar'])
Exemple #31
0
    def __init__(self, user_function, maxsize=1024):
        # Link layout:     [PREV, NEXT, KEY, RESULT]
        self.root = root = [None, None, None, None]
        self.user_function = user_function
        self.cache = cache = {}

        last = root
        for i in range(maxsize):
            key = object()
            cache[key] = last[1] = last = [last, root, key, None]
        root[0] = last
Exemple #32
0
 def test_notify(self):
     """
     The notify() API allows getting all events passed to the Discovery instance.
     """
     disco = create_disco()
     messages = [object(), NodeActive(create_node("hello"))]
     result = []
     disco.notify(result.append)
     for m in messages:
         disco.onMessage(None, m)
     self.assertEqual(messages, result)
Exemple #33
0
    def setUp(self):
        super(ClusterScalerTest, self).setUp()
        self.config = Config()
        self.config.targetTime = 1800
        self.config.nodeTypes = ['r3.8xlarge', 'c4.8xlarge:0.6']
        # Set up a stub provisioner with some nodeTypes and nodeShapes.
        self.provisioner = object()
        self.provisioner.nodeTypes = ['r3.8xlarge', 'c4.8xlarge']
        self.provisioner.nodeShapes = [r3_8xlarge, c4_8xlarge_preemptable]
        self.provisioner.setStaticNodes = lambda _, __: None
        self.provisioner.retryPredicate = lambda _: False

        self.leader = MockBatchSystemAndProvisioner(self.config, 1)
 def initiate_checkpoint():
     with self._checkpoint_lock:
         if checkpoint_state.checkpointed:
             return
         checkpoint_state.checkpointed = object()
     split = sdf_invoker.try_split(0)
     if split:
         _, checkpoint_state.residual_restriction = split
     else:
         # Clear the checkpoint if the split didn't happen. This counters
         # a very unlikely race condition that the Timer attempted to initiate
         # a checkpoint before invoke_process set the current element allowing
         # for another attempt to checkpoint.
         checkpoint_state.checkpointed = None
Exemple #35
0
def memoize_on_instance(f):
  missing = object()

  def wrapper(self, *args):
    try:
      cache = getattr(self, '_cache_%s' % f.__name__)
    except AttributeError:
      cache = {}
      setattr(self, '_cache_%s' % f.__name__, cache)
    result = cache.get(args, missing)
    if result is missing:
      result = cache[args] = f(self, *args)
    return result

  return wrapper
Exemple #36
0
    def test_commit_already_committed(self):
        from google.cloud.spanner_v1.keyset import KeySet

        keys = [[0], [1], [2]]
        keyset = KeySet(keys=keys)
        database = _Database()
        session = _Session(database)
        batch = self._make_one(session)
        batch.committed = object()
        batch.delete(TABLE_NAME, keyset=keyset)

        with self.assertRaises(ValueError):
            batch.commit()

        self.assertNoSpans()
Exemple #37
0
    def test_container(self):
        """
        base test for Container class
        """
        foo = object()
        foo.id = TEST_ID
        foo.name = TEST_NAME
        foo.addrs = TEST_ADDRS

        container = Container(foo)

        self.assertEquals(container.id, TEST_ID)
        self.assertEquals(container.name, TEST_NAME)
        self.assertEquals(container.addrs, TEST_ADDRS)
        self.assertIsNotNone(str(container))
def memoize_on_instance(f):
    missing = object()

    def wrapper(self, *args):
        try:
            cache = getattr(self, '_cache_%s' % f.__name__)
        except AttributeError:
            cache = {}
            setattr(self, '_cache_%s' % f.__name__, cache)
        result = cache.get(args, missing)
        if result is missing:
            result = cache[args] = f(self, *args)
        return result

    return wrapper
Exemple #39
0
    def testOsFunctionMapper(self):
        from Exscript.util.decorator import os_function_mapper
        cb_map = {'ios': self.ios_cb, 'junos': self.junos_cb}
        mapper = os_function_mapper(cb_map)
        job = FakeJob()
        host = object()

        # Test with 'ios'.
        conn = FakeConnection(os='ios')
        result = mapper(job, host, conn)
        self.assertEqual(result, 'hello ios')

        # Test with 'junos'.
        conn = FakeConnection(os='junos')
        result = mapper(job, host, conn)
        self.assertEqual(result, 'hello junos')

        # Test with unsupported OS.
        conn = FakeConnection(os='unknown')
        self.assertRaises(Exception, mapper, job, host, conn)
Exemple #40
0
def all_equal(iter1, iter2):
    """`True` if all elements in ``a`` and ``b`` are equal."""
    # Direct comparison for scalars, tuples or lists
    try:
        if iter1 == iter2:
            return True
    except ValueError:  # Raised by NumPy when comparing arrays
        pass

    # Special case for None
    if iter1 is None and iter2 is None:
        return True

    # If one nested iterator is exhausted, go to direct comparison
    try:
        it1 = iter(iter1)
        it2 = iter(iter2)
    except TypeError:
        try:
            return iter1 == iter2
        except ValueError:  # Raised by NumPy when comparing arrays
            return False

    diff_length_sentinel = object()

    # Compare element by element and return False if the sequences have
    # different lengths
    for [ip1, ip2] in zip_longest(it1, it2,
                                  fillvalue=diff_length_sentinel):
        # Verify that none of the lists has ended (then they are not the
        # same size)
        if ip1 is diff_length_sentinel or ip2 is diff_length_sentinel:
            return False

        if not all_equal(ip1, ip2):
            return False

    return True
Exemple #41
0
def test_kwargs():
    """
    Verify that kwargs can be adapted
    """
    class Test(object):
        @anticipate(foo=int, bar=str)
        def get_args(self, arg, **kwargs):
            return arg, kwargs

    @anticipate(foo=int, bar=str)
    def get_args(arg, **kwargs):
        return arg, kwargs

    t = Test()

    obj = object()

    r = t.get_args(obj, foo='2', bar=3)
    assert r[0] is obj
    assert r[1]['foo'] == 2
    assert r[1]['bar'] == '3'

    r = get_args(obj, foo='2', bar=3)
    assert r[0] is obj
    assert r[1]['foo'] == 2
    assert r[1]['bar'] == '3'

    r = t.get_args(arg=obj, foo='2', bar=3)
    assert r[0] is obj
    assert r[1]['foo'] == 2
    assert r[1]['bar'] == '3'

    r = get_args(arg=obj, foo='2', bar=3)
    assert r[0] is obj
    assert r[1]['foo'] == 2
    assert r[1]['bar'] == '3'
Exemple #42
0
def roundrobin(*iterables):
    sentinel = object()
    return (x for x in chain(*izip_longest(fillvalue=sentinel, *iterables)) if x is not sentinel)
Exemple #43
0
    def _recurse(o, dct, depth):
        if max_depth >= 0 and depth > max_depth:
            return
        for ref in get_referents(o):
            idr = id(ref)
            if not idr in dct:
                dct[idr] = (ref, getsizeof(ref, default=0))
                _recurse(ref, dct, depth+1)
    sizedict = {}
    _recurse(obj, sizedict, 0)
    #count = len(sizedict) + 1
    size = getsizeof(obj) + sum([p[1] for p in sizedict.values()])
    return size

# lazy load handler
_missing = object()
class lazy_property(object):
    """
    Delays loading of property until first access. Credit goes to the
    Implementation in the werkzeug suite:
    http://werkzeug.pocoo.org/docs/utils/#werkzeug.utils.cached_property

    This should be used as a decorator in a class and in Evennia is
    mainly used to lazy-load handlers:

        ```python
        @lazy_property
        def attributes(self):
            return AttributeHandler(self)
        ```
 def test_should_raise_if_constructed_with_a_bad_protocol(self):
     testf = lambda: report_request.Info(protocol=object())
     # not a report_request.ReportedProtocols
     expect(testf).to(raise_error(ValueError))
Exemple #45
0
import struct
import sys
import termios

t = gettext.translation('okaara', fallback=True)
if sys.version_info[0] < 3:
    _ = t.ugettext
else:
    _ = t.gettext

# -- constants ----------------------------------------------------------------

LOG = logging.getLogger(__name__)

# Returned to indicate the user has interrupted the input
ABORT = object()

# Indicates the automatic wrap should use the current width of the screen,
# calculated at the time of rendering
WIDTH_TERMINAL = object()

COLOR_WHITE = '\033[0m'
COLOR_BRIGHT_WHITE = '\033[1m'

COLOR_GRAY = '\033[30m'
COLOR_RED = '\033[31m'
COLOR_GREEN = '\033[32m'
COLOR_YELLOW = '\033[33m'
COLOR_BLUE = '\033[34m'
COLOR_PURPLE = '\033[35m'
COLOR_CYAN = '\033[36m'
        def __exit__(self, exc_type, exc_value, traceback):
            pass


try: # Python 2.7+
    from collections import OrderedDict
except ImportError:
    from .packages.ordered_dict import OrderedDict
from .packages.six import itervalues


__all__ = ['RecentlyUsedContainer', 'HTTPHeaderDict']


_Null = object()


class RecentlyUsedContainer(MutableMapping):
    """
    Provides a thread-safe dict-like container which maintains up to
    ``maxsize`` keys while throwing away the least-recently-used keys beyond
    ``maxsize``.

    :param maxsize:
        Maximum number of recent elements to retain.

    :param dispose_func:
        Every time an item is evicted from the container,
        ``dispose_func(value)`` is called.  Callback which will get called
    """
Exemple #47
0
""" Copyright (c) 2002-2003 LOGILAB S.A. (Paris, FRANCE).
 http://www.logilab.fr/ -- mailto:[email protected]

 Cache module, with a least recently used algorithm for the management of the
 deletion of entries.
"""
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future import standard_library
standard_library.install_aliases()
from builtins import *
from builtins import object

_marker = object()

class Cache(object):
    """ a dictionnary like cache

    inv:
        len(self._usage) <= self.size
        len(self.data) <= self.size
    """
    
    def __init__(self, size=100):
        self.data = {}
        self.size = size
        self._usage = []

    def __repr__(self):
 def test_should_raise_if_constructed_with_a_bad_error_cause(self):
     testf = lambda: report_request.Info(error_cause=object())
     expect(testf).to(raise_error(ValueError))
Exemple #49
0
 def __init__(self):
   self.objects = FakeGcsObjects()
   # Referenced in GcsIO.copy_batch() and GcsIO.delete_batch().
   self._http = object()
Exemple #50
0
    FlavorError,
    ImageError,
    InstanceNotFoundError,
    KeypairError,
    SecurityGroupError,
)


# these defaults should be kept in sync w/ `conf.py`
DEFAULT_OS_COMPUTE_API_VERSION='2'
DEFAULT_OS_IDENTITY_API_VERSION='3'
DEFAULT_OS_IMAGE_API_VERSION='2'
DEFAULT_OS_NETWORK_API_VERSION='2.0'  # no choice as of Aug. 2017
DEFAULT_OS_VOLUME_API_VERSION='2'

_NO_DEFAULT = object()
"""
Special value used in `_get_os_config_value` to indicate that a
value *must* be provided.
"""



class OpenStackCloudProvider(AbstractCloudProvider):
    """
    This implementation of
    :py:class:`elasticluster.providers.AbstractCloudProvider` uses the
    OpenStack native python bindings to connect to OpenStack clouds
    and manage instances.

    :param str username: username of the keystone user
Exemple #51
0
 def test_get_namespace_error(self):
   with self.assertRaises(ValueError):
     Metrics.get_namespace(object())
 def __init__(self):
   self.objects = FakeGcsObjects()
   # Referenced in GcsIO.batch_copy() and GcsIO.batch_delete().
   self._http = object()
 def test_should_fail_on_invalid_input(self):
     testf = lambda: check_request.sign(None)
     expect(testf).to(raise_error(ValueError))
     testf = lambda: check_request.sign(object())
     expect(testf).to(raise_error(ValueError))
 def test_should_fail_if_req_is_bad(self):
     testf = lambda: self.agg.report(object())
     expect(testf).to(raise_error(ValueError))
     testf = lambda: self.agg.report(None)
     expect(testf).to(raise_error(ValueError))
# Internal country indexes
_by_alpha2 = _build_index(1)
_by_alpha3 = _build_index(2)
_by_numeric = _build_index(3)
_by_name = _build_index(0)


# Documented accessors for the country indexes
countries_by_alpha2 = _by_alpha2
countries_by_alpha3 = _by_alpha3
countries_by_numeric = _by_numeric
countries_by_name = _by_name


NOT_FOUND = object()


class _CountryLookup(object):

    def get(self, key, default=NOT_FOUND):
        if isinstance(key, Integral):
            r = _by_numeric.get("%03d" % key, default)
        else:
            k = key.upper()
            if len(k) == 2:
                r = _by_alpha2.get(k, default)
            elif len(k) == 3 and re.match(r"[0-9]{3}", k):
                r = _by_numeric.get(k, default)
            elif len(k) == 3:
                r = _by_alpha3.get(k, default)
Exemple #56
0
    else:
      raise ValueError('Invalid state spec: %s' % state_spec)

  def _encode(self, value):
    return self._state_spec.coder.encode(value)

  def _decode(self, value):
    return self._state_spec.coder.decode(value)

  def prefetch(self):
    # The default implementation here does nothing.
    pass


# Sentinel designating an unread value.
UNREAD_VALUE = object()


class BagRuntimeState(RuntimeState):
  """Bag state interface object passed to user code."""

  def __init__(self, state_spec, state_tag, current_value_accessor):
    super(BagRuntimeState, self).__init__(
        state_spec, state_tag, current_value_accessor)
    self._cached_value = UNREAD_VALUE
    self._cleared = False
    self._new_values = []

  def read(self):
    if self._cached_value is UNREAD_VALUE:
      self._cached_value = self._current_value_accessor()
 def initiate_checkpoint():
   with self._checkpoint_lock:
     if checkpoint_state.checkpointed:
       return
   checkpoint_state.residual_restriction = tracker.checkpoint()
   checkpoint_state.checkpointed = object()
    def __gt__(s, o): return (s.time, s.priority) >  (o.time, o.priority)
    def __ge__(s, o): return (s.time, s.priority) >= (o.time, o.priority)

if PY3:
    Event.time.__doc__ = ('''Numeric type compatible with the return value of the
    timefunc function passed to the constructor.''')
    Event.priority.__doc__ = ('''Events scheduled for the same time will be executed
    in the order of their priority.''')
    Event.action.__doc__ = ('''Executing the event means executing
    action(*argument, **kwargs)''')
    Event.argument.__doc__ = ('''argument is a sequence holding the positional
    arguments for the action.''')
    Event.kwargs.__doc__ = ('''kwargs is a dictionary holding the keyword
    arguments for the action.''')

_sentinel = object()

class scheduler(object):

    def __init__(self, timefunc=_time, delayfunc=time.sleep):
        """Initialize a new instance, passing the time and delay
        functions"""
        self._queue = []
        self._lock = threading.RLock()
        self.timefunc = timefunc
        self.delayfunc = delayfunc

    def enterabs(self, time, priority, action, argument=(), kwargs=_sentinel):
        """Enter a new event in the queue at an absolute time.

        Returns an ID for the event which can be used to remove it,