Example #1
1
    def write(self, fileobj):
        fileobj = fileview(fileobj, self.offset, self.size)
        fileobj.seek(0)

        # serialize all the mach-o commands
        self.synchronize_size()

        self.header.to_fileobj(fileobj)
        for lc, cmd, data in self.commands:
            lc.to_fileobj(fileobj)
            cmd.to_fileobj(fileobj)

            if isinstance(data, unicode):
                fileobj.write(data.encode(sys.getfilesystemencoding()))

            elif isinstance(data, (bytes, str)):
                fileobj.write(data)
            else:
                # segments..
                for obj in data:
                    obj.to_fileobj(fileobj)

        # zero out the unused space, doubt this is strictly necessary
        # and is generally probably already the case
        fileobj.write(B("\x00") * (self.low_offset - fileobj.tell()))
 def _iterencode(o, _current_indent_level):
     if isinstance(o, basestring):
         yield _encoder(o)
     elif o is None:
         yield "null"
     elif o is True:
         yield "true"
     elif o is False:
         yield "false"
     elif isinstance(o, (int, long)):
         yield str(o)
     elif isinstance(o, float):
         yield _floatstr(o)
     elif isinstance(o, (list, tuple)):
         for chunk in _iterencode_list(o, _current_indent_level):
             yield chunk
     elif isinstance(o, dict):
         for chunk in _iterencode_dict(o, _current_indent_level):
             yield chunk
     else:
         if markers is not None:
             markerid = id(o)
             if markerid in markers:
                 raise ValueError("Circular reference detected")
             markers[markerid] = o
         o = _default(o)
         for chunk in _iterencode(o, _current_indent_level):
             yield chunk
         if markers is not None:
             del markers[markerid]
Example #3
1
 def html_format_question(q):
     if "setcounter" in q:
         gui_compose.counter = int(q["setcounter"])
     res = "<strong>Вопрос {}.</strong> {}".format(
         gui_compose.counter if not "number" in q else q["number"],
         yapper(q["question"]) + ("\n<lj-spoiler>" if not args.nospoilers else ""),
     )
     if not "number" in q:
         gui_compose.counter += 1
     res += "\n<strong>Ответ: </strong>{}".format(yapper(q["answer"]))
     if "zachet" in q:
         res += "\n<strong>Зачёт: </strong>{}".format(yapper(q["zachet"]))
     if "nezachet" in q:
         res += "\n<strong>Незачёт: </strong>{}".format(yapper(q["nezachet"]))
     if "source" in q:
         res += "\n<strong>Источник{}: </strong>{}".format(
             "и" if isinstance(q["source"], list) else "", yapper(q["source"])
         )
     if "comment" in q:
         res += "\n<strong>Комментарий: </strong>{}".format(yapper(q["comment"]))
     if "author" in q:
         res += "\n<strong>Автор{}: </strong>{}".format(
             "ы" if isinstance(q["author"], list) else "", yapper(q["author"])
         )
     if not args.nospoilers:
         res += "</lj-spoiler>"
     return res
Example #4
1
 def add_variable(self, name, dims, data=None, **kwargs):
     if data is None:
         char = "d"
     else:
         if isinstance(data, np.ndarray):
             char = data.dtype.char
         elif isinstance(data, float):
             char = "d"
         elif isinstance(data, int):
             char = "i"
         else:
             char = "c"
     print(("%-34s %s%s" % (name, char, tuple([self.nc.dimensions[dim] for dim in dims]))))
     var = self.nc.createVariable(name, char, dims)
     for attr, value in kwargs.items():
         setattr(var, attr, value)
     if data is not None:
         if len(dims) == 0:
             var.assignValue(data)
         else:
             if char == "c":
                 if len(dims) == 1:
                     var[: len(data)] = data
                 else:
                     for i, x in enumerate(data):
                         var[i, : len(x)] = x
             else:
                 var[:] = data
     return var
Example #5
1
def detect_nan(i, node, fn):
    for output in fn.outputs:
        if not isinstance(output[0], np.random.RandomState) and not (
            hasattr(node, "op")
            or isinstance(
                node.op, (theano.sandbox.rng_mrg.GPU_mrg_uniform, theano.sandbox.cuda.basic_ops.GpuAllocEmpty)
            )
        ):
            try:
                has_nans = np.isnan(output[0]).any() or np.isinf(output[0]).any()
            except TypeError:
                has_nans = False
            if not has_nans:
                continue
            print ("*** NaN detected ***")
            theano.printing.debugprint(node, depth=3)
            print (type(node), node.op, type(node.op))
            print ("Inputs : %s" % [input[0] for input in fn.inputs])
            print "Input shape", [input[0].shape for input in fn.inputs]
            print ("Outputs: %s" % [output[0] for output in fn.outputs])
            print "Output shape", [output[0].shape for output in fn.outputs]
            print "NaN # :", [np.sum(np.isnan(output[0])) for output in fn.outputs]
            print "Inf # :", [np.sum(np.isinf(output[0])) for output in fn.outputs]
            print "NaN location: ", np.argwhere(np.isnan(output[0])), ", Inf location: ", np.argwhere(
                np.isinf(output[0])
            )
            import pdb

            pdb.set_trace()
            raise ValueError
Example #6
1
    def test_crash_several_sub_task(self):
        tempDirPath = mkdtemp()
        tempDirPathInTmp = os.path.split(tempDirPath)[1]
        self.add_file_to_remove(tempDirPath)

        fake_file = FakeFile(dir=tempDirPath)
        self.add_file_to_close(fake_file)
        path_in_container = os.path.join(tempDirPathInTmp, fake_file.filename)
        self.local_container.inject_hash(path_in_container, "local", "remote")

        fake_file = FakeFile(dir=tempDirPath)
        self.add_file_to_close(fake_file)
        path_in_container = os.path.join(tempDirPathInTmp, fake_file.filename)
        self.local_container.inject_hash(path_in_container, "local", "remote")

        fake_file = FakeFile(dir=tempDirPath)
        self.add_file_to_close(fake_file)
        path_in_container = os.path.join(tempDirPathInTmp, fake_file.filename)
        self.local_container.inject_hash(path_in_container, "local", "remote")

        self.execute_task(generate_fake_task(self, tempDirPathInTmp))

        assert self.result is not None
        assert len(self.result) == 3
        assert len(set(self.result)) == 3

        assert isinstance(self.result[0], AddedLocalFilesTask)
        assert isinstance(self.result[1], AddedLocalFilesTask)
        assert isinstance(self.result[2], AddedLocalFilesTask)
Example #7
1
def get_tractor_fits_values(T, cat, pat):
    typearray = np.array([fits_typemap[type(src)] for src in cat])
    # If there are no "COMP" sources, the type will be 'S3' rather than 'S4'...
    typearray = typearray.astype("S4")
    T.set(pat % "type", typearray)

    T.set(pat % "ra", np.array([src is not None and src.getPosition().ra for src in cat]))
    T.set(pat % "dec", np.array([src is not None and src.getPosition().dec for src in cat]))

    shapeExp = np.zeros((len(T), 3))
    shapeDev = np.zeros((len(T), 3))
    fracDev = np.zeros(len(T))

    for i, src in enumerate(cat):
        if isinstance(src, ExpGalaxy):
            shapeExp[i, :] = src.shape.getAllParams()
        elif isinstance(src, DevGalaxy):
            shapeDev[i, :] = src.shape.getAllParams()
            fracDev[i] = 1.0
        elif isinstance(src, FixedCompositeGalaxy):
            shapeExp[i, :] = src.shapeExp.getAllParams()
            shapeDev[i, :] = src.shapeDev.getAllParams()
            fracDev[i] = src.fracDev.getValue()

    T.set(pat % "shapeExp", shapeExp.astype(np.float32))
    T.set(pat % "shapeDev", shapeDev.astype(np.float32))
    T.set(pat % "fracDev", fracDev.astype(np.float32))
    return
Example #8
1
def expect_model_param(models, attribute_name, expected_values):
    print "param: {0}".format(attribute_name)
    actual_values = list(
        set(
            [
                m.params[attribute_name]["actual"]
                if type(m.params[attribute_name]["actual"]) != list
                else m.params[attribute_name]["actual"][0]
                for m in models.models
            ]
        )
    )
    # possible for actual to be a list (GLM)
    if type(expected_values) != list:
        expected_values = [expected_values]
    # limit precision. Rounding happens in some models like RF
    actual_values = [x if isinstance(x, basestring) else round(float(x), 5) for x in actual_values]
    expected_values = [x if isinstance(x, basestring) else round(float(x), 5) for x in expected_values]
    print "actual values: {0}".format(actual_values)
    print "expected values: {0}".format(expected_values)
    actual_values_len = len(actual_values)
    expected_values_len = len(expected_values)
    assert actual_values_len == expected_values_len, "Expected values len: {0}. Actual values len: " "{1}".format(
        expected_values_len, actual_values_len
    )
    diff = set(actual_values) - set(expected_values)
    assert len(diff) == 0, "Difference between actual and expected values: {0}".format(diff)
Example #9
0
def test_transformation():
    """ Test transforming images.
    """
    N = 10
    v2w_mapping = Transform("voxels", "world1", mapping, inverse_mapping)
    identity = Transform("world1", "world2", id, id)
    data = np.random.random((N, N, N))
    img1 = VolumeGrid(data=data, transform=v2w_mapping)
    img2 = img1.composed_with_transform(identity)

    yield nose.tools.assert_equal, img2.world_space, "world2"

    x, y, z = N * np.random.random(size=(3, 10))
    yield np.testing.assert_almost_equal, img1.values_in_world(x, y, z), img2.values_in_world(x, y, z)

    yield nose.tools.assert_raises, CompositionError, img1.composed_with_transform, identity.get_inverse()

    yield nose.tools.assert_raises, CompositionError, img1.resampled_to_img, img2

    # Resample an image on itself: it shouldn't change much:
    img = img1.resampled_to_img(img1)
    yield np.testing.assert_almost_equal, data, img.get_data()

    # Check that if I 'resampled_to_img' on an VolumeImg, I get an
    # VolumeImg, and vice versa
    volume_image = VolumeImg(data, np.eye(4), "world")
    identity = Transform("voxels", "world", id, id)
    image = VolumeGrid(data, identity)
    image2 = image.resampled_to_img(volume_image)
    yield nose.tools.assert_true, isinstance(image2, VolumeImg)
    volume_image2 = volume_image.resampled_to_img(image)
    yield nose.tools.assert_true, isinstance(image2, VolumeGrid)
    # Check that the data are all the same: we have been playing only
    # with identity mappings
    yield np.testing.assert_array_equal, volume_image2.get_data(), image2.get_data()
Example #10
0
    def _demarshal(self, body):
        """Demarshal the request payload.

        :param str body: The string response body
        :rtype: dict or str

        """
        if self.status_code == 200:
            if body is None:
                return None
            try:
                if utils.PYTHON3 and isinstance(body, bytes):
                    body = body.decode("utf-8")
                value = json.loads(body, encoding="utf-8")
            except (TypeError, ValueError):
                return body
            if isinstance(value, bool):
                return value
            if "error" not in value:
                for row in value:
                    if "Value" in row:
                        try:
                            row["Value"] = base64.b64decode(row["Value"])
                            if isinstance(row["Value"], bytes):
                                row["Value"] = row["Value"].decode("utf-8")
                        except TypeError:
                            pass
            if isinstance(value, list) and len(value) == 1:
                return value[0]
            return value
        return body
Example #11
0
    def add_queue(
        self, queue_name, routing_keys, callback, exchange_name=None, no_ack=True, exclusive=False, auto_delete=True
    ):

        c_routing_keys = []

        if not isinstance(routing_keys, list):
            if isinstance(routing_keys, basestring):
                c_routing_keys = [routing_keys]
        else:
            c_routing_keys = routing_keys

        if not exchange_name:
            exchange_name = self.exchange_name

        self.queues[queue_name] = {
            "queue": False,
            "consumer": False,
            "queue_name": queue_name,
            "routing_keys": c_routing_keys,
            "callback": callback,
            "exchange_name": exchange_name,
            "no_ack": no_ack,
            "exclusive": exclusive,
            "auto_delete": auto_delete,
        }
Example #12
0
def check_dims_values(python_obj, h2o_frame, rows, cols):
    """
    Check that the dimensions and values of the python object and H2OFrame are equivalent. Assumes that the python object
    conforms to the rules specified in the h2o frame documentation.

    :param python_obj: a (nested) list, tuple, dictionary, numpy.ndarray, ,or pandas.DataFrame
    :param h2o_frame: an H2OFrame
    :param rows: number of rows
    :param cols: number of columns
    :return: None
    """
    h2o_rows, h2o_cols = h2o_frame.dim
    assert (
        h2o_rows == rows and h2o_cols == cols
    ), "failed dim check! h2o_rows:{0} rows:{1} h2o_cols:{2} cols:{3}" "".format(h2o_rows, rows, h2o_cols, cols)
    if isinstance(python_obj, (list, tuple)):
        for c in range(cols):
            for r in range(rows):
                pval = python_obj[c][r] if cols > 1 else python_obj[r]
                hval = h2o_frame[r, c]
                assert pval == hval, (
                    "expected H2OFrame to have the same values as the python object for row {0} and column "
                    "{1}, but h2o got {2} and python got {3}.".format(r, c, hval, pval)
                )
    elif isinstance(python_obj, dict):
        for r in range(rows):
            for k in python_obj.keys():
                pval = python_obj[k][r] if hasattr(python_obj[k], "__iter__") else python_obj[k]
                hval = h2o_frame[r, k]
                assert pval == hval, (
                    "expected H2OFrame to have the same values as the python object for row {0} and column "
                    "{1}, but h2o got {2} and python got {3}.".format(r, k, hval, pval)
                )
Example #13
0
    def _from_dict_value(cls, class_, value, validator):
        # validate raw input
        if validator is cls.SOFT_VALIDATION:
            if issubclass(class_, Unicode) and not isinstance(value, unicode):
                if not (issubclass(class_, String) and isinstance(value, str)):
                    raise ValidationError(value)

            elif issubclass(class_, Decimal) and not isinstance(value, (int, long, float)):
                raise ValidationError(value)

            elif issubclass(class_, DateTime) and not (
                isinstance(value, unicode) and class_.validate_string(class_, value)
            ):
                raise ValidationError(value)

        # get native type
        if issubclass(class_, ComplexModelBase):
            retval = cls._doc_to_object(class_, value, validator)

        elif issubclass(class_, DateTime):
            retval = class_.from_string(value)

        else:
            retval = value

        # validate native type
        if validator is cls.SOFT_VALIDATION and not class_.validate_native(class_, retval):
            raise ValidationError(retval)

        return retval
Example #14
0
    def register_foreign_device(self, addr, ttl):
        """Add a foreign device to the FDT."""
        if _debug:
            BIPBBMD._debug("register_foreign_device %r %r", addr, ttl)

        # see if it is an address or make it one
        if isinstance(addr, Address):
            pass
        elif isinstance(addr, str):
            addr = LocalStation(addr)
        else:
            raise TypeError("addr must be a string or an Address")

        for fdte in self.bbmdFDT:
            if addr == fdte.fdAddress:
                break
        else:
            fdte = FDTEntry()
            fdte.fdAddress = addr
            self.bbmdFDT.append(fdte)

        fdte.fdTTL = ttl
        fdte.fdRemain = ttl + 5

        # return success
        return 0
Example #15
0
    def to_python(self, value):
        """
        Passes embedded model fields' values through embedded fields
        to_python methods and reinstiatates the embedded instance.

        We expect to receive a field.attname => value dict together
        with a model class from back-end database deconversion (which
        needs to know fields of the model beforehand).
        """

        # Either the model class has already been determined during
        # deconverting values from the database or we've got a dict
        # from a deserializer that may contain model class info.
        if isinstance(value, tuple):
            embedded_model, attribute_values = value
        elif isinstance(value, dict):
            embedded_model = self.stored_model(value)
            attribute_values = value
        else:
            return value

        # Pass values through respective fields' to_python, leaving
        # fields for which no value is specified uninitialized.
        attribute_values = dict(
            (field.attname, field.to_python(attribute_values[field.attname]))
            for field in embedded_model._meta.fields
            if field.attname in attribute_values
        )

        # Create the model instance.
        instance = embedded_model(**attribute_values)
        instance._state.adding = False
        return instance
Example #16
0
 def transform_incoming(self, son, collection):
     for (key, value) in son.items():
         if isinstance(value, CustomRepo):
             son[key] = encode_custom(value)
         elif isinstance(value, dict):  # Make sure we recurse into sub-docs
             son[key] = self.transform_incoming(value, collection)
     return son
Example #17
0
    def validate_filter(filter):
        for key, value in filter.items():
            if "*" not in allowed and key not in allowed:
                return "filter on '%s' not allowed" % key

            if key in ("$or", "$and", "$nor"):
                if not isinstance(value, list):
                    return "operator '%s' expects a list of sub-queries" % key
                for v in value:
                    if not isinstance(v, dict):
                        return "operator '%s' expects a list of sub-queries" % key
                    r = validate_filter(v)
                    if r:
                        return r
            else:
                if config.VALIDATE_FILTERS:
                    res_schema = config.DOMAIN[resource]["schema"]
                    if key not in res_schema:
                        return "filter on '%s' is invalid"
                    else:
                        field_schema = res_schema.get(key)
                        v = Validator({key: field_schema})
                        if not v.validate({key: value}):
                            return "filter on '%s' is invalid"
                        else:
                            return None
Example #18
0
    def get_config_list(self, key, default="", sep=",", keep_empty=False):
        """
        A convenience method which coerces the key to a list of string.

        A different separator can be specified using the `sep` parameter. The
        `sep` parameter can specify multiple values using a list or a tuple.
        If the `keep_empty` parameter is set to `True`, empty elements are
        included in the list.

        Valid default input is a string or a list. Returns a string.
        """
        value = self.get_config(key, default)
        if not value:
            return []
        if isinstance(value, basestring):
            if isinstance(sep, (list, tuple)):
                splitted = re.split("|".join(map(re.escape, sep)), value)
            else:
                splitted = value.split(sep)
            items = [item.strip() for item in splitted]
        else:
            items = list(value)
        if not keep_empty:
            items = [item for item in items if item not in (None, "")]
        return items
Example #19
0
def round_up(n, k):
    """Return the smallest integer >= n, where the lowest k digits are 0.

    @param n: a positive integer to round up.
    @param k: a positive integer indicating how many digits should be 0.
    @return the round-up number.
    @exception AssertError if either n or k is not a positive integer.
    @example
      - round_up(3140, 1) = 3140:  no change because the lowest digit is already 0.
      - round_up(3140, 2) = 3200:  the lowest two digits are 0.
      - round_up(3140, 3) = 4000:  the lowest three digits are 0.
      - round_up(3140, k>=4) = 1 followed by k zeros.
    """
    assert isinstance(n, (int, long)) and isinstance(k, (int, long)) and n > 0 and k > 0

    # how many digits does n have?
    num_digits = len(str(abs(n)))

    # 1 followed by k zeros.
    k_zeros = int(math.pow(10, k))

    if k >= num_digits:  # if k >= num_digits, return 1 followed by k zeros.
        return k_zeros
    elif n % k_zeros == 0:  # if the last k digits are already zeros, just return n.
        return n
    else:
        return (n // k_zeros + 1) * k_zeros  # increase the (k+1)'th digit by 1.
Example #20
0
def run_function_list(fns, predicate=None):
    """
    Runs a list of functions.
    Tolerant of passing in a single function

    :param fns: list of dicts with three keys: fn (function object),
                                               args (list of args),
                                               kwargs (dict of kwargs)
    :param predicate: if this function returns false, given the current function, it will not be ran
    """

    if not fns:
        return

    # Allow for sending in a single function
    if not isinstance(fns, (list, tuple, set)):
        fns = [fns]

    for fn in fns:
        if predicate and not predicate(fn):
            continue

        if isinstance(fn, dict) and "fn" in fn:
            fn["fn"](*fn.get("args", []), **fn.get("kwargs", {}))
        elif callable(fn):
            fn()
        else:
            raise Exception("Invalid function")
Example #21
0
def migrate_base_class_to_new_class(
    obj, indexes=["is_folderish", "object_provides"], old_class_name="", new_class_name="", migrate_to_folderish=False
):
    if not old_class_name:
        old_class_name = get_old_class_name_string(obj)
    if not new_class_name:
        new_class_name = get_portal_type_name_string(obj)
        if not new_class_name:
            logger.warning("The type {0} has no fti!".format(obj.portal_type))
            return False

    was_item = not isinstance(obj, BTreeFolder2Base)
    if old_class_name != new_class_name:
        obj_id = obj.getId()
        module_name, class_name = new_class_name.rsplit(".", 1)
        module = importlib.import_module(module_name)
        new_class = getattr(module, class_name)

        # update obj class
        parent = obj.__parent__
        parent._delOb(obj_id)
        obj.__class__ = new_class
        parent._setOb(obj_id, obj)

    is_container = isinstance(obj, BTreeFolder2Base)

    if was_item and is_container or migrate_to_folderish and is_container:
        #  If Itemish becomes Folderish we have to update obj _tree
        BTreeFolder2Base._initBTrees(obj)

    # reindex
    obj.reindexObject(indexes)

    return True
Example #22
0
 def tolist(x):
     x = x or []
     if isinstance(x, basestring):
         x = [x]
     if not isinstance(x, list):
         raise Exception("Invalid configuration - Must be list or string")
     return x
Example #23
0
 def compare_output(self, input, output, expected):
     """`input`, `output`, and `expected` should all be strings."""
     if isinstance(input, unicode):
         input = input.encode("raw_unicode_escape")
     if sys.version_info > (3,):
         # API difference: Python 3's node.__str__ doesn't escape
         # assert expected is None or isinstance(expected, unicode)
         if isinstance(expected, bytes):
             expected = expected.decode("utf-8")
         if isinstance(output, bytes):
             output = output.decode("utf-8")
     else:
         if isinstance(expected, unicode):
             expected = expected.encode("raw_unicode_escape")
         if isinstance(output, unicode):
             output = output.encode("raw_unicode_escape")
     # Normalize line endings:
     if expected:
         expected = "\n".join(expected.splitlines())
     if output:
         output = "\n".join(output.splitlines())
     try:
         self.assertEquals(output, expected)
     except AssertionError, error:
         print >>sys.stderr, "\n%s\ninput:" % (self,)
         print >>sys.stderr, input
         try:
             comparison = "".join(self.compare(expected.splitlines(1), output.splitlines(1)))
             print >>sys.stderr, "-: expected\n+: output"
             print >>sys.stderr, comparison
         except AttributeError:  # expected or output not a string
             # alternative output for non-strings:
             print >>sys.stderr, "expected: %r" % expected
             print >>sys.stderr, "output:   %r" % output
         raise error
Example #24
0
 def test_connect_timeout(self):
     try:
         requests.get(TARPIT, timeout=(0.1, None))
         assert False, "The connect() request should time out."
     except ConnectTimeout as e:
         assert isinstance(e, ConnectionError)
         assert isinstance(e, Timeout)
Example #25
0
    def extract_resources(self, resources):
        """Takes a list of resources, and gets paths by type"""
        extracted = []

        for resource in resources:

            # A URL string
            if isinstance(resource, (str, unicode, Promise)):
                extracted.append(str(resource))
                continue

            # A model instance; requires get_absolute_url method
            if isinstance(resource, self.model):
                extracted.append(resource.get_absolute_url())
                continue

            # If it's a Model, we get the base Manager
            if isinstance(resource, self.model_base):
                resource = resource._default_manager

            # If it's a Manager, we get the QuerySet
            if isinstance(resource, self.manager):
                resource = resource.all()

            # Append all paths from obj.get_absolute_url() to list
            if isinstance(resource, self.queryset):
                extracted += [obj.get_absolute_url() for obj in resource]

        return extracted
Example #26
0
    def _datehist_agg(interval, bounds_min=None, bounds_max=None, min_doc_count=0, field="@timestamp"):
        """Return a date histogram aggregation that can be applied to an
        existing search object."""
        from arrow import Arrow
        from elasticsearch_dsl import A

        assert isinstance(interval, basestring), "interval must be a string"

        # if start/end were provided, let's put them in the extended bounds
        # and set min_doc_count to 0
        extended_bounds = {}
        if bounds_min is not None:
            if isinstance(bounds_min, Arrow):
                extended_bounds["min"] = bounds_min.isoformat()
            else:
                extended_bounds["min"] = bounds_min

        if bounds_max is not None:
            if isinstance(bounds_max, Arrow):
                extended_bounds["max"] = bounds_max.isoformat()
            else:
                extended_bounds["max"] = bounds_max
        if "max" in extended_bounds or "min" in extended_bounds:
            min_doc_count = 0

        return A(
            "date_histogram",
            field=field,
            interval=interval,
            min_doc_count=min_doc_count,
            extended_bounds=extended_bounds,
        )
Example #27
0
def warns(expected_warning, *args, **kwargs):
    """Assert that code raises a particular class of warning.

    Specifically, the input @expected_warning can be a warning class or
    tuple of warning classes, and the code must return that warning
    (if a single class) or one of those warnings (if a tuple).

    This helper produces a list of ``warnings.WarningMessage`` objects,
    one for each warning raised.

    This function can be used as a context manager, or any of the other ways
    ``pytest.raises`` can be used::

        >>> with warns(RuntimeWarning):
        ...    warnings.warn("my warning", RuntimeWarning)
    """
    wcheck = WarningsChecker(expected_warning)
    if not args:
        return wcheck
    elif isinstance(args[0], str):
        code, = args
        assert isinstance(code, str)
        frame = sys._getframe(1)
        loc = frame.f_locals.copy()
        loc.update(kwargs)

        with wcheck:
            code = py.code.Source(code).compile()
            py.builtin.exec_(code, frame.f_globals, loc)
    else:
        func = args[0]
        with wcheck:
            return func(*args[1:], **kwargs)
Example #28
0
    def bounded_search(cls, start=None, end=None, key_field="@timestamp"):
        """ Returns a search with time range."""
        import arrow
        from arrow import Arrow

        start = arrow.get(0) if start == "" else start
        end = arrow.utcnow() if end == "" else end

        if end is not None:
            assert isinstance(end, Arrow), "end is not an Arrow object"

        if start is not None:
            assert isinstance(start, Arrow), "start is not an Arrow object"

        search = cls.search()

        if start is not None and end is not None:
            search = search.query("range", **{key_field: {"lte": end.isoformat(), "gte": start.isoformat()}})

        elif start is not None and end is None:
            search = search.query("range", **{key_field: {"gte": start.isoformat()}})

        elif start is None and end is not None:
            search = search.query("range", **{key_field: {"lte": end.isoformat()}})

        return search
Example #29
0
 def from_args(service=None, dests=[]):
     assert isinstance(service, Service)
     assert isinstance(dests, list)
     p = Pool()
     p.service_ = service
     p.dests_ = dests
     return p
Example #30
0
    def set(self, key, value, overwrite=True):
        """
    Appends the given key/value configuration mapping, behaving the same as if
    we'd loaded this from a configuration file.

    :param str key: key for the configuration mapping
    :param str,list value: value we're setting the mapping to
    :param bool overwrite: replaces the previous value if **True**, otherwise
      the values are appended
    """

        with self._contents_lock:
            if isinstance(value, str):
                if not overwrite and key in self._contents:
                    self._contents[key].append(value)
                else:
                    self._contents[key] = [value]

                for listener in self._listeners:
                    listener(self, key)
            elif isinstance(value, (list, tuple)):
                if not overwrite and key in self._contents:
                    self._contents[key] += value
                else:
                    self._contents[key] = value

                for listener in self._listeners:
                    listener(self, key)
            else:
                raise ValueError(
                    "Config.set() only accepts str, list, or tuple. Provided value was a '%s'" % type(value)
                )