示例#1
0
    def read(self, path, size, offset, force_reload=False):
        """Read the given data from the given path on the filesystem.

        Any parts which are requested and are not in the cache are read
        from the underlying filesystem
        """
        debug('Cacher.read', path, size, offset)

        self.init_cached_data(path)

        if force_reload:
            self.remove_cached_blocks(path)

        cached_blocks = self.get_cached_blocks(path)
        debug('Cacher.cached_blocks', cached_blocks)

        blocks_needed = cached_blocks.get_uncovered_portions(
            Range(offset, offset + size))
        debug('Cacher.blocks_needed', blocks_needed)

        blocks_to_read = Ranges()
        for block in blocks_needed:
            # make sure this block isn't already included in a previous block + read_ahead
            if not blocks_to_read.contains(block):
                blocks_to_read.add_range(
                    Range(block.start,
                          max(block.end, block.start + self.read_ahead)))

        debug('Cacher.blocks_to_read', blocks_to_read)
        self.update_cached_data(path, blocks_to_read.ranges)
        self.update_cached_blocks(
            path, cached_blocks.add_ranges(blocks_to_read.ranges))

        return self.get_cached_data(path, size, offset)
示例#2
0
    def process_entry(self, entry: str):
        """
        Process a whole entry
        :param entry:
        :return:
        """

        doc = self.nlp(entry)
        analysis = Analysis()

        for sent in doc.sents:

            if self.do_general_analysis:
                self.count_tokens(sent)
            for noun_phrase in sent.noun_chunks:
                covered = RangeSet()

                for trigger in self.all_triggers:

                    if trigger in noun_phrase.text.lower():

                        start = noun_phrase.text.lower().index(trigger)

                        if not Range(start,
                                     len(noun_phrase.text) +
                                     start).intersection(covered):

                            covered.add(
                                Range(noun_phrase.start, noun_phrase.end))
                            self.__process_np(trigger, noun_phrase, doc,
                                              analysis)

        return analysis
示例#3
0
 def expand_star(self, max_line: int, other_split_files):
     source_file_range = Range(1, max_line, include_end=True)
     union_of_splits = RangeSet()
     for split in other_split_files:
         lines = split._lines  # pylint: disable=protected-access
         union_of_splits = union_of_splits.union(lines)
     diff = source_file_range.symmetric_difference(union_of_splits)
     self._lines.extend(diff)
示例#4
0
def test_range_isdisjoint(rng1, rng2, isdisjoint, error_type):
    if error_type == "r2":
        assert (isdisjoint == rng1.isdisjoint(rng2))
        rng2 = Range(rng2)
        error_type = None
    if error_type is not None:
        asserterror(error_type, rng1.isdisjoint, (rng2, ))
    else:
        assert (rng1.isdisjoint(rng2) == rng2.isdisjoint(rng1))
        assert (isdisjoint == rng1.isdisjoint(rng2))
示例#5
0
def test_range_intersect(rng1, rng2, intersect, error_type):
    if error_type == "r2":
        assert (intersect == rng1.intersection(rng2))
        rng2 = Range(rng2)
        error_type = None
    if error_type is not None:
        asserterror(error_type, rng1.intersection, (rng2, ))
    else:
        assert (rng1.intersection(rng2) == rng2.intersection(rng1))
        assert (rng1 & rng2 == rng1.intersection(rng2))
        assert (intersect == rng1.intersection(rng2))
示例#6
0
def test_range_union(rng1, rng2, union, error_type):
    if error_type == "r2":
        assert (union == rng1.union(rng2))
        rng2 = Range(rng2)
        error_type = None
    if error_type is not None:
        asserterror(error_type, rng1.union, (rng2, ))
    else:
        assert (rng1.union(rng2) == rng2.union(rng1))
        assert (rng1.union(rng2) == rng1 | rng2)
        assert (union == rng1.union(rng2))
示例#7
0
def test_range_difference(rng1, rng2, forward_diff, backward_diff, error_type):
    if error_type == "r2":
        assert (rng1.difference(rng2) == rng1 - rng2)
        rng2 = Range(rng2)
        error_type = None
    if error_type is not None:
        asserterror(error_type, rng1.difference, (rng2, ))
    else:
        assert (rng1.difference(rng2) == rng1 - rng2)
        assert (rng2.difference(rng1) == rng2 - rng1)
        assert (forward_diff == rng1.difference(rng2))
        assert (backward_diff == rng2.difference(rng1))
示例#8
0
def test_issue4():
    # issue: a Range that exactly overlaps one exclusive border of a key in a RangeDict
    # does not register as contains
    # cause: Range._above_start() and ._below_end() were disregarding the other Range's inclusivity
    # by not treating the other Range as a Range
    rd = RangeDict({Range(0, 5, include_end=True): 'zero to five inclusive'})
    assert (Range(0, 5, include_end=True) in rd)
    assert (Range(0, 4) in rd)
    assert (Range(1, 4) in rd)
    assert (Range(1, 5, include_end=True) in rd)
    rd2 = RangeDict(
        {Range(0, 5, include_start=False): 'zero to five exclusive'})
    assert (Range(0, 5, include_start=False) in rd2)
    assert (Range(0, 4, include_start=False) in rd2)
    assert (Range(1, 4) in rd2)
    assert (Range(1, 5) in rd2)
示例#9
0
def test_issue6():
    # issue: cannot use unhashable types as the value in a RangeDict
    try:
        x = RangeDict({Range(0, 1): ["A", "B"]})
        assert (str(x) == "{{[0, 1)}: ['A', 'B']}")
    except TypeError:
        fail("RangeDict should not raise an error when value is unhashable")
示例#10
0
def test_rangeset_docstring():
    a = RangeSet()
    b = RangeSet([Range(0, 1), Range(2, 3), Range(4, 5)])
    c = RangeSet(Range(0, 1), Range(2, 3), Range(4, 5))
    d = RangeSet("[0, 1)", ["[1.5, 2)", "[2.5, 3)"], "[4, 5]")
    assert (str(a) == "{}")
    assert (str(b) == "{[0, 1), [2, 3), [4, 5)}")
    assert (str(c) == "{[0, 1), [2, 3), [4, 5)}")
    assert (b == c)
    assert (str(d) == "{[0, 1), [1.5, 2), [2.5, 3), [4, 5]}")

    asserterror(
        ValueError, RangeSet,
        ([[Range(0, 1), Range(2, 3)], [Range(4, 5), Range(6, 7)]], ))

    f = RangeSet("[0, 3]", "[2, 4)", "[5, 6]")
    assert (str(f) == "{[0, 4), [5, 6]}")
示例#11
0
def test_issue12():
    # issue: mutating a mutable RangeDict value also affected all keys set to equivalent values.
    # In other words, the RangeDict was compressing equal but not identical values into the same
    # rangekey values. To fix, added a toggle to use identity instead of equality.
    # The code in this test is now also contained in the docstring.
    f = RangeDict({Range(1, 2): {3}, Range(4, 5): {3}})
    assert (str(f) == '{{[1, 2), [4, 5)}: {3}}')
    f[Range(1, 2)] |= {4}
    assert (str(f) == '{{[1, 2), [4, 5)}: {3, 4}}')

    g = RangeDict({Range(1, 2): {3}, Range(4, 5): {3}}, identity=True)
    assert (str(g) == '{{[1, 2)}: {3}, {[4, 5)}: {3}}')

    h = RangeDict({Range(1, 2): {3}, Range(4, 5): {3}})
    assert (str(h) == '{{[1, 2), [4, 5)}: {3}}')
    h[Range(1, 2)] = h[Range(1, 2)] | {4}
    assert (str(h) == '{{[4, 5)}: {3}, {[1, 2)}: {3, 4}}')
示例#12
0
def gather_symbols(symbol_addrs_path, undefined_syms_path):
    symbols = {}
    special_labels = {}
    labels_to_add = set()
    ranges = RangeDict()

    # Manual list of func name / addrs
    if os.path.exists(symbol_addrs_path):
        with open(symbol_addrs_path) as f:
            func_addrs_lines = f.readlines()

        for line in func_addrs_lines:
            line = line.strip()
            if not line == "" and not line.startswith("//"):
                comment_loc = line.find("//")
                line_ext = ""

                if comment_loc != -1:
                    line_ext = line[comment_loc + 2:].strip()
                    line = line[:comment_loc].strip()

                line_split = line.split("=")
                name = line_split[0].strip()
                addr = int(line_split[1].strip()[:-1], 0)
                symbols[addr] = name

                if line_ext:
                    for info in line_ext.split(" "):
                        if info == "!":
                            labels_to_add.add(name)
                            special_labels[addr] = name
                        if info.startswith("size:"):
                            size = int(info.split(":")[1], 0)
                            ranges.add(Range(addr, addr + size), name)

    if os.path.exists(undefined_syms_path):
        with open(undefined_syms_path) as f:
            us_lines = f.readlines()

        for line in us_lines:
            line = line.strip()
            if not line == "" and not line.startswith("//"):
                line_split = line.split("=")
                name = line_split[0].strip()
                addr = int(line_split[1].strip()[:-1], 0)
                symbols[addr] = name

    return symbols, labels_to_add, special_labels, ranges
示例#13
0
    def read(self, path, size, offset, force_reload=False):
        """Read the given data from the given path on the filesystem.

        Any parts which are requested and are not in the cache are read
        from the underlying filesystem
        """
        debug('Cacher.read', path, size, offset)

        self.init_cached_data(path)

        if force_reload:
            self.remove_cached_blocks(path)

        cached_blocks = self.get_cached_blocks(path)
        blocks_to_read = cached_blocks.get_uncovered_portions(
            Range(offset, offset + size))

        self.update_cached_data(path, blocks_to_read)
        self.update_cached_blocks(path,
                                  cached_blocks.add_ranges(blocks_to_read))

        return self.get_cached_data(path, size, offset)
示例#14
0
    def _create_line_ranges(self, split_data: collections.abc.Mapping,
                            max_line: int):
        lines = split_data.get("lines")
        if not lines or not lines.strip():
            raise ConfigError(
                f'No lines specified for split file "{self._path}".')

        range_set = RangeSet()
        line_ranges = lines.split(",")
        for line_range in line_ranges:
            start, _, end = line_range.partition("-")
            if start.strip() == "*":
                self._has_star = True
                continue
            try:
                start = int(start)
                end = int(end) if end else start
                if not 0 < start <= max_line or not 0 < end <= max_line:
                    raise ValueError(f"Out of range (1-{max_line})")
                range_set.add(Range(start, end, include_end=True))
            except ValueError as ex:
                raise ConfigError(
                    f'Invalid lines for split file "{self._path}": {ex}')
        return range_set
示例#15
0
def test_issue8():
    # issue: adding a Range to a RangeSet containing two non-overlapping ranges, such that the new range overlaps
    # with one but not the other, leads to a TypeError being raised.
    # cause: code was passing a Linked List Node instead of the node's value (a range)
    try:
        a = RangeSet()
        a.add(Range(100, 300))
        a.add(Range(400, 500))
        a.add(Range(500, 600))
        assert (str(a) == "{[100, 300), [400, 600)}")
        b = RangeSet()
        b.add(Range(400, 600))
        b.add(Range(200, 300))
        b.add(Range(100, 200))
        assert (str(b) == "{[100, 300), [400, 600)}")
    except TypeError:
        fail(
            "RangeSet should not have an issue concatenating to the second range of two in a RangeSet"
        )
 def setUp(self):
     self.ranges = Range()
class TestAddRange(unittest.TestCase):
    def setUp(self):
        self.ranges = Range()
    def assertEqualRange(self, range):
    	self.assertEquals(self.ranges.ranges, range)
    def test_add_none_intersect(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(20, 25)
        self.assertEqualRange([(10, 15), (20, 25)])
    def test_add_intersect_simple_left(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(9, 12)
        self.assertEqualRange([(9, 15)])
    def test_add_intersect_simple_right(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(11, 16)
        self.assertEqualRange([(10, 16)])
    def test_add_intersect_simple_inner(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(11, 12)
        self.assertEqualRange([(10, 15)])
    def test_add_intersect_simple_outer(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(9, 16)
        self.assertEqualRange([(9, 16)])
    def test_add_intersect_complex_1(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(20, 25)
        self.ranges.addRange(30, 35)
        self.ranges.addRange(10, 20)
        self.assertEqualRange([(10, 25),(30, 35)])
    def test_add_intersect_complex_2(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(20, 25)
        self.ranges.addRange(30, 35)
        self.ranges.addRange(16, 19)
        self.assertEqualRange([(10, 25),(30, 35)])
    def test_add_intersect_complex_3(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(20, 25)
        self.ranges.addRange(30, 35)
        self.ranges.addRange(17, 18)
        self.assertEqualRange([(10, 15),(17,18),(20, 25),(30, 35)])
    def test_add_intersect_complex_4(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(20, 25)
        self.ranges.addRange(30, 35)
        self.ranges.addRange(17, 19)
        self.assertEqualRange([(10, 15),(17, 25),(30, 35)])
    def test_add_intersect_complex_5(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(20, 25)
        self.ranges.addRange(30, 35)
        self.ranges.addRange(17, 29)
        self.assertEqualRange([(10, 15),(17, 35)])
    def test_add_intersect_complex_6(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(20, 25)
        self.ranges.addRange(30, 35)
        self.ranges.addRange(0, 50)
        self.assertEqualRange([(0, 50)])
    def test_add_intersect_complex_7(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(20, 25)
        self.ranges.addRange(30, 35)
        self.ranges.addRange(20, 50)
    def test_query_1(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(20, 25)
        self.assertTrue(self.ranges.queryRange(10, 15))
    def test_query_2(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(20, 25)
        self.assertTrue(self.ranges.queryRange(11, 14))
    def test_query_3(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(20, 25)
        self.assertFalse(self.ranges.queryRange(9, 14))
    def test_query_4(self):
        self.ranges.addRange(10, 15)
        self.ranges.addRange(20, 25)
        self.assertFalse(self.ranges.queryRange(13, 16))
示例#18
0
    def read(self, path, size, offset):
        debug('cacher.read', path, str(size), str(offset))
        cache_data = self._get_cache_dir(path, 'cache.data')
        data_cache_range = self._get_cache_dir(path, 'cache.data.range')

        # list of Range objects indicating which chunks of the requested data
        # we have not yet cached and will need to get from the underlying fs
        blocks_to_read = []

        # Ranges object indicating which chunks of the file we have cached
        cached_blocks = None
        if os.path.exists(data_cache_range):
            with __builtin__.open(data_cache_range, 'rb') as f:
                debug('  loading cached_blocks from file')
                cached_blocks = pickle.load(f)
        else:
            cached_blocks = Ranges()

        requested_range = Range(offset, offset + size)

        debug('   read', 'path=' + path, 'size=' + str(size),
              'offset=' + str(offset))
        debug('   requested_range', requested_range)
        debug('   cached_blocks', cached_blocks)

        blocks_to_read = cached_blocks.get_uncovered_portions(requested_range)

        debug('   blocks_to_read', blocks_to_read)

        # First, create the cache file if it does not exist already
        if not os.path.exists(cache_data):
            # We create a file full of zeroes the same size as the real file
            file_stat = self.getattr(path)
            self._create_cache_dir(path)

            with __builtin__.open(cache_data, 'wb') as f:
                debug('  creating blank file, size', str(file_stat.st_size))
                f.seek(file_stat.st_size - 1)
                f.write('\0')

                #for i in range(1, file_stat.st_size):
                #	f.write('\0')

        # If there are no blocks_to_read, then don't bother opening
        # the cache_data file for updates or dumping our cached_blocks.
        # This will slightly improve performance when getting data which
        # is already in the cache.
        if len(blocks_to_read) > 0:

            # Now open it up in update mode so we can add data to it as
            # we read the data from the underlying filesystem
            with __builtin__.open(cache_data, 'r+b') as cache_data_file:

                # Now loop through all the blocks we need to get
                # and append them to the cached file as we go
                for block in blocks_to_read:
                    block_data = self.underlying_fs.read(
                        path, block.size, block.start)

                    cached_blocks.add_range(block)

                    cache_data_file.seek(block.start)
                    cache_data_file.write(
                        block_data)  # overwrites existing data in the file

            # update our cached_blocks file
            with __builtin__.open(data_cache_range, 'wb') as f:
                pickle.dump(cached_blocks, f)

        # Now we have loaded all the data we need to into the cache, we do the read
        # from the cached file
        result = None
        with __builtin__.open(cache_data, 'rb') as f:
            f.seek(offset)
            result = f.read(size)

        debug('  returning result from cache', type(result), len(result))
        return result
示例#19
0
def test_range_docstring():
    a = Range()
    b = Range()
    assert (a == b)
    assert (str(a) == "[-inf, inf)")

    c = Range("(-3, 5.5)")
    d = Range("[-3, 5.5)")
    assert (c != d)
    assert (c > d)
    assert (c.start == d.start and c.end == d.end)
    assert (c.start == -3 and c.end == 5.5)

    e = Range(3, 5)
    f = Range(3, 5, include_start=False, include_end=True)
    assert (e != f)
    assert (e < f)
    assert (str(e) == "[3, 5)")
    assert (str(f) == "(3, 5]")
    assert (e.start == f.start and e.end == f.end)

    g = Range(start=3, end=5)
    h = Range(start=3)
    i = Range(end=5)
    j = Range(start=3, end=5, include_start=False, include_end=True)
    k = Range(start=datetime.date(1969, 10, 5))
    l = Range(end="ni", include_end=True)
    assert (str(g) == "[3, 5)")
    assert (str(h) == "[3, inf)")
    assert (str(i) == "[-inf, 5)")
    assert (str(j) == "(3, 5]")
    assert (str(k) == "[1969-10-05, inf)")
    assert (repr(k) == "Range[datetime.date(1969, 10, 5), inf)")
    assert (str(l) == "[-inf, ni]")
    assert (repr(l) == "Range[-inf, 'ni']")

    m = Range(datetime.date(1478, 11, 1), datetime.date(1834, 7, 15))
    assert (datetime.date(1492, 8, 3) in m)
    assert (datetime.date(1979, 8, 17) not in m)

    n = Range("killer", "rabbit")
    assert ("grenade" not in n)
    assert ("pin" in n)
    assert ("three" not in n)

    o = Range(include_end=True)
    assert (str(o) == "[-inf, inf]")
    assert (0 in o)
    assert (1 in o)
    assert (-99e99 in o)
    assert ("one" in o)
    assert (datetime.date(1975, 3, 14) in o)
    assert (None in o)
    assert (float('nan') not in o)

    r = Range(include_start=True, include_end=False)
    assert (str(r) == "[-inf, inf)")
    assert (float('-inf') in r)
    assert (float('inf') not in r)
示例#20
0
def test_range_constructor_valid(start, end, include_start, include_end,
                                 isempty):
    """
    Tests all possible permutations of the Range() constructor to make sure that they produce valid ranges.
    Also tests is_empty().
    """
    fakestart = 5 if start == 4 else 4
    fakeend = 9 if start == 8 else 8
    test_ranges = [
        (Range(start, end), {'check_include_start', 'check_include_end'}),
        (Range(start=start,
               end=end), {'check_include_start', 'check_include_end'}),
        (Range(start, end,
               include_start=include_start), {'check_include_end'}),
        (Range(start, end, include_end=include_end), {'check_include_start'}),
        (Range(start,
               end,
               include_start=include_start,
               include_end=include_end), {}),
        (Range(start=start, end=end,
               include_start=include_start), {'check_include_end'}),
        (Range(start=start, end=end,
               include_end=include_end), {'check_include_start'}),
        (Range(start=start,
               end=end,
               include_start=include_start,
               include_end=include_end), {}),
        (Range(start, end, start=fakestart,
               end=fakeend), {'check_include_start', 'check_include_end'}),
        (Range(start,
               end,
               start=fakestart,
               end=fakeend,
               include_start=include_start), {'check_include_end'}),
        (Range(start,
               end,
               start=fakestart,
               end=fakeend,
               include_end=include_end), {'check_include_start'}),
        (Range(start,
               end,
               start=fakestart,
               end=fakeend,
               include_start=include_start,
               include_end=include_end), {}),
    ]
    if isinstance(start, numbers.Number) and isinstance(end, numbers.Number):
        test_ranges += [
            (Range(start=start, include_start=include_start),
             {'infinite_end', 'check_include_end'}),
            (Range(start=start, include_end=include_end),
             {'infinite_end', 'check_include_start'}),
            (Range(start=start,
                   include_start=include_start,
                   include_end=include_end), {'infinite_end'}),
            (Range(end=end, include_start=include_start),
             {'infinite_start', 'check_include_end'}),
            (Range(end=end, include_end=include_end),
             {'infinite_start', 'check_include_start'}),
            (Range(end=end,
                   include_start=include_start,
                   include_end=include_end), {'infinite_start'}),
            (Range(start=start),
             {'infinite_end', 'check_include_start', 'check_include_end'}),
            (Range(end=end),
             {'infinite_start', 'check_include_start', 'check_include_end'}),
        ]
    numstr = ""
    if (isinstance(start, int)
            and isinstance(end, int)) or (isinstance(start, float)
                                          and isinstance(end, float)):
        numstr = f"{'[' if include_start else '('}{start}, {end}{']' if include_end else ')'}"
        numstr2 = numstr.replace(", ", "..")
        test_ranges += [
            (Range(numstr), {}),
            (Range(numstr, start=fakestart), {}),
            (Range(numstr, end=fakeend), {}),
            (Range(numstr, include_start=not include_start), {}),
            (Range(numstr, include_end=not include_end), {}),
            (Range(numstr, start=fakestart, end=fakeend), {}),
            (Range(numstr, start=fakestart,
                   include_start=not include_start), {}),
            (Range(numstr, start=fakestart, include_end=not include_end), {}),
            (Range(numstr, end=fakeend, include_start=not include_start), {}),
            (Range(numstr, end=fakeend, include_end=not include_end), {}),
            (Range(numstr,
                   include_start=not include_start,
                   include_end=not include_end), {}),
            (Range(numstr,
                   start=fakestart,
                   end=fakeend,
                   include_start=not include_start), {}),
            (Range(numstr,
                   start=fakestart,
                   end=fakeend,
                   include_end=not include_end), {}),
            (Range(numstr,
                   start=fakestart,
                   include_start=not include_start,
                   include_end=not include_end), {}),
            (Range(numstr,
                   end=fakeend,
                   include_start=not include_start,
                   include_end=not include_end), {}),
            (Range(numstr,
                   start=fakestart,
                   end=fakeend,
                   include_start=not include_start,
                   include_end=not include_end), {}),
            (Range(numstr2), {}),
            (Range(numstr2, start=fakestart), {}),
            (Range(numstr2, end=fakeend), {}),
            (Range(numstr2, include_start=not include_start), {}),
            (Range(numstr2, include_end=not include_end), {}),
            (Range(numstr2, start=fakestart, end=fakeend), {}),
            (Range(numstr2, start=fakestart,
                   include_start=not include_start), {}),
            (Range(numstr2, start=fakestart, include_end=not include_end), {}),
            (Range(numstr2, end=fakeend, include_start=not include_start), {}),
            (Range(numstr2, end=fakeend, include_end=not include_end), {}),
            (Range(numstr2,
                   include_start=not include_start,
                   include_end=not include_end), {}),
            (Range(numstr2,
                   start=fakestart,
                   end=fakeend,
                   include_start=not include_start), {}),
            (Range(numstr2,
                   start=fakestart,
                   end=fakeend,
                   include_end=not include_end), {}),
            (Range(numstr2,
                   start=fakestart,
                   include_start=not include_start,
                   include_end=not include_end), {}),
            (Range(numstr2,
                   end=fakeend,
                   include_start=not include_start,
                   include_end=not include_end), {}),
            (Range(numstr2,
                   start=fakestart,
                   end=fakeend,
                   include_start=not include_start,
                   include_end=not include_end), {}),
        ]
    for idx, test in enumerate(test_ranges):
        # print(idx, test)
        assert (test[0].start == start if 'infinite_start' not in test[1] else
                test[0].start == float('-inf'))
        assert (test[0].end == end if 'infinite_end' not in test[1] else
                test[0].end == float('inf'))
        assert (test[0].include_start == include_start if 'check_include_start'
                not in test[1] else test[0].include_start)
        assert (test[0].include_end == include_end if 'check_include_end'
                not in test[1] else not test[0].include_end)
        if 'check_include_start' not in test[1] and 'check_include_end' not in test[1] \
                and 'infinite_start' not in test[1] and 'infinite_end' not in test[1]:
            assert (test[0].isempty() == isempty)
            assert (bool(test[0]) != isempty)
        if (isinstance(start, int)
                and isinstance(end, int)) or (isinstance(start, float)
                                              and isinstance(end, float)):
            assert str(test[0] == numstr)
示例#21
0
"""
Tests for RangeSet
"""
import pytest
from ranges import Range, RangeSet
import datetime
from .test_base import asserterror


@pytest.mark.parametrize(
    "args,ranges,strr,reprr,isempty",
    [
        ([], [], "{}", "RangeSet{}", True),  # empty RangeSet
        (["[1..1]"], [Range("[1..1]")], "{[1, 1]}", "RangeSet{Range[1, 1]}",
         False),  # contains one nonempty Range
        (["(1..1)"], [Range("(1..1)")], "{(1, 1)}", "RangeSet{Range(1, 1)}",
         True),  # contains one empty Range
        (["[1, 2)", "[3, 4)", "[5, 6)"],
         [Range(1, 2), Range(3, 4), Range(5, 6)], "{[1, 2), [3, 4), [5, 6)}",
         "RangeSet{Range[1, 2), Range[3, 4), Range[5, 6)}", False),
        ([[]], [], "{}", "RangeSet{}", True),  # once-nested empty list
        (["[0, 1)", ["[1.5, 2)", "[2.5, 3)"], "[4, 5)"],
         [Range(0, 1), Range(1.5, 2),
          Range(2.5, 3),
          Range(4, 5)], "{[0, 1), [1.5, 2), [2.5, 3), [4, 5)}",
         "RangeSet{Range[0, 1), Range[1.5, 2), Range[2.5, 3), Range[4, 5)}",
         False),  # mix Rangelike, iterable args
        (["[0, 3]", "[2, 4)", "[5, 6]"], [Range(0, 4),
                                          Range("[5, 6]")], "{[0, 4), [5, 6]}",
         "RangeSet{Range[0, 4), Range[5, 6]}", False),  # overlapping
        (["[0, 4)", "(1, 3)"], [Range(0, 4)], "{[0, 4)}",
示例#22
0
        print('File does not exist or file type is not CSV.')
        sys.exit(1)


#list path and file name for data to import
original_data = dataset('Credit_Data.csv')
#original_data

#make copy of data
od = original_data

# In[3]:

#make groups for continuous data (Amount, Duration, Age)
AMT_DICT = RangeDict({
    Range(0, 1360): 1,
    Range(1360, 2300): 2,
    Range(2300, 3900): 3,
    Range(3900,
          max(od.AMOUNT) + 1): 4
})
od['AMOUNT_GROUP'] = [AMT_DICT[i] for i in od.AMOUNT]

DUR_DICT = RangeDict({
    Range(0, 12): 1,
    Range(12, 18): 2,
    Range(18, 24): 3,
    Range(24,
          max(od.DURATION) + 1): 4
})
od['DURATION_GROUP'] = [DUR_DICT[i] for i in od.DURATION]
示例#23
0
        (("[]", ), {}),
        (("", ), {}),
        (("{1, 2}", ), {}),
        (("(1 2)", ), {}),
        (("(one, two)", ), {}),
        (("[1, 2", ), {}),
    ])
def test_range_constructor_invalid(args, kwargs):
    """ Tests invalid calls to `Range.__init__()`, asserting that they yield the correct error. """
    asserterror(ValueError, Range, args, kwargs)


@pytest.mark.parametrize(
    "rng, item, contains, strr, reprr",
    [
        (Range(1, 2), 1, True, "[1, 2)", "Range[1, 2)"),
        (Range(1, 2), 2, False, "[1, 2)", "Range[1, 2)"),
        (Range(1, 2), 1.5, True, "[1, 2)", "Range[1, 2)"),
        (Range("(0.3, 0.4)"), 0.1 + 0.2, True, "(0.3, 0.4)",
         "Range(0.3, 0.4)"),
        (Range("(0.3, 0.4)"), 0.3, False, "(0.3, 0.4)", "Range(0.3, 0.4)"),
        (Range(), 99e99, True, "[-inf, inf)", "Range[-inf, inf)"),
        (Range(), -99e99, True, "[-inf, inf)", "Range[-inf, inf)"),
        (Range(), float('inf'), False, "[-inf, inf)",
         "Range[-inf, inf)"),  # inclusive Infinity is deliberate
        (Range(include_end=True), float('inf'), True, "[-inf, inf]",
         "Range[-inf, inf]"),  # (see IEEE 754)
        (Range(-3, 3), Range(1, 2), True, "[-3, 3)", "Range[-3, 3)"),
        (Range(-3, 3), Range(
            1, 3), True, "[-3, 3)", "Range[-3, 3)"),  # changed, see issue #4
        (Range(-3, 3), Range(-4, 4), False, "[-3, 3)", "Range[-3, 3)"),