示例#1
0
 def test_do_scoping_correct(self):
     try:
         l = lambda *items: List.from_iterable(items)
         do(let(a=l(1, 2, 3)), let(b=lambda e: List.unit(e.a)),
            lambda e: List.unit(e.a * e.b))
     except AttributeError:
         assert False
示例#2
0
    def test_list_applicative_binary_func(self):
        f = lambda x, y: x + y
        v = List.from_iterable([1, 2])
        w = List.from_iterable([4, 8])

        self.assertEquals(
            pure(f).apply(v).apply(w), List.from_iterable([5, 9, 6, 10]))
示例#3
0
 def test_do_list_basic(self):
     l = lambda *items: List.from_iterable(items)
     out1 = do(let(a=l(3, 10, 6)), let(b=l(100, 200)),
               lambda e: List.unit(e.a + e.b))
     out2 = l(3, 10, 6) | (lambda a: l(100, 200) |
                           (lambda b: List.unit(a + b)))
     self.assertEqual(out1, out2)
示例#4
0
    def test_list_functor_map(self):
        # fmap f (return v) = return (f v)
        x = unit(42)
        f = lambda x: x * 10

        self.assertEqual(x.map(f), unit(420))

        y = List.from_iterable([1, 2, 3, 4])
        g = lambda x: x * 10

        self.assertEqual(y.map(g), List.from_iterable([10, 20, 30, 40]))
示例#5
0
 def test_do_list_pythag(self):
     r = lambda low, high: List.from_iterable(range(low, high))
     out1 = do(let(z=r(1, 21)), let(x=lambda e: r(1, e.z + 1)),
               let(y=lambda e: r(e.x, e.z + 1)),
               lambda e: guard(List, e.x * e.x + e.y * e.y == e.z * e.z),
               lambda e: List.unit((e.x, e.y, e.z)))
     out2 = r(1, 21) | (
         lambda z: r(1, z + 1) |
         (lambda x: r(x, z + 1) |
          (lambda y: guard(List, x * x + y * y == z * z) >> List.unit(
              (x, y, z)))))
     self.assertEqual(out1, out2)
示例#6
0
    def test_list_applicative_law_identity(self):
        # pure id <*> v = v

        # Singleton list
        x = unit(42)
        self.assertEquals(pure(identity).apply(x), x)

        # Empty list
        y = List.empty()
        self.assertEquals(pure(identity).apply(y), y)

        # Log list
        y = List.from_iterable(range(42))
        self.assertEquals(pure(identity).apply(y), y)
示例#7
0
def _get_setup_args(path):
    rtn = List.empty()
    with open(path, 'rU') as file:
        t = compile(file.read(), path, 'exec', ast.PyCF_ONLY_AST)
        for node in (n for n in t.body if isinstance(n, ast.Expr)):
            if isinstance(node.value, ast.Call):
                if hasattr(node.value.func,
                           'id') and node.value.func.id == 'setup':
                    rtn = List.from_iterable(node.value.keywords)
                    break
                else:
                    print(dir(node.value.func), node.value.func.value)

    return rtn
示例#8
0
    def test_list_functor_law_1(self):
        # fmap id = id

        # Singleton list using return
        x = unit(42)
        self.assertEqual(x.map(identity), x)

        # Empty list
        y = List()
        self.assertEqual(y.map(identity), y)

        # Long list
        z = List.from_iterable(range(42))
        self.assertEqual(z.map(identity), z)
示例#9
0
    def test_list_applicative_law_functor(self):
        # pure f <*> x = fmap f x
        f = lambda x: x * 42

        x = unit(42)
        self.assertEquals(pure(f).apply(x), x.map(f))

        # Empty list
        z = List()
        self.assertEquals(pure(f).apply(z), z.map(f))

        # Long list
        z = List.from_iterable(range(42))
        self.assertEquals(pure(f).apply(z), z.map(f))
示例#10
0
def _get_value(keyname, alias_list, kw):
    name = kw.arg
    rtn = {}

    def get_list_value(v):
        r = []
        for e in v.elts:
            if isinstance(e, ast.Str):
                r.append(e.s)
            elif isinstance(e, ast.Num):
                r.append(str(e.n))
        return r

    get_tuple_value = get_list_value

    if name in alias_list:
        v = kw.value
        if isinstance(v, ast.Str):
            rtn[keyname] = v.s
        elif isinstance(v, ast.Tuple):
            rtn[keyname] = get_tuple_value(v)
        elif isinstance(v, ast.List):
            rtn[keyname] = get_list_value(v)
        elif isinstance(v, ast.BinOp):
            if isinstance(v.left, ast.List):
                rtn[keyname] = get_list_value(v.left)
        else:
            #print(name, dir(v))
            pass

    return List.unit(rtn)
示例#11
0
    def test_list_monad_law_associativity_empty(self):
        # (m >>= f) >>= g is just like doing m >>= (\x -> f x >>= g)
        f = lambda x: unit(x + 1000)
        g = lambda y: unit(y * 42)

        # Empty list
        m = List()
        self.assertEqual(m.bind(f).bind(g), m.bind(lambda x: f(x).bind(g)))
示例#12
0
    def test_list_monad_law_associativity_range(self):
        # (m >>= f) >>= g is just like doing m >>= (\x -> f x >>= g)
        f = lambda x: unit(x + 1000)
        g = lambda y: unit(y * 42)

        # Long list
        m = List.from_iterable(range(42))
        self.assertEqual(m.bind(f).bind(g), m.bind(lambda x: f(x).bind(g)))
示例#13
0
def get_result(name, version):
    theurl = "https://pypi.python.org/pypi/%s" % name
    if version != None and version.strip() != "":
        theurl = theurl + "/" + version
    print(theurl)
    thepage = urllib.request.urlopen(theurl)
    soup = BeautifulSoup(thepage, "lxml")
    return List.unit(soup)
示例#14
0
    def test_identity_applicative_law_composition_empty(self):
        # pure (.) <*> u <*> v <*> w = u <*> (v <*> w)

        u = pure(lambda x: x * 42)
        v = pure(lambda x: x + 42)

        # Empty list
        w = List()
        self.assertEquals(
            pure(fmap).apply(u).apply(v).apply(w), u.apply(v.apply(w)))
示例#15
0
    def test_list_functor_law2(self):
        # fmap (f . g) x = fmap f (fmap g x)
        def f(x):
            return x + 10

        def g(x):
            return x * 10

        # Singleton list
        x = unit(42)
        self.assertEquals(x.map(compose(f, g)), x.map(g).map(f))

        # Empty list
        y = List.empty()
        self.assertEquals(y.map(compose(f, g)), y.map(g).map(f))

        # Long list
        z = List.from_iterable(range(42))
        self.assertEquals(z.map(compose(f, g)), z.map(g).map(f))
示例#16
0
    def test_identity_applicative_law_composition_range(self):
        # pure (.) <*> u <*> v <*> w = u <*> (v <*> w)

        u = pure(lambda x: x * 42)
        v = pure(lambda x: x + 42)

        # Long list
        w = List.from_iterable(range(42))
        self.assertEquals(
            pure(fmap).apply(u).apply(v).apply(w), u.apply(v.apply(w)))
示例#17
0
def get_index(soup):
    data = {}
    data["index"] = {}
    for div in soup("div", id="download-button"):
        lst = div.parent.find("h1").text.split()
        data["index"]['name_version'] = (lst[0], lst[1])
        data["index"]['description'] = div.parent.findNext('p').text
        data["index"]['download_url'] = div.find('a')["href"]
        break

    return List.unit(data)
示例#18
0
def get_table(soup):
    data = {}
    data["files"] = []
    for table in soup("table", class_="list")[-1:]:
        if "File" not in table("th")[0].string:
            continue

        for entry in table("tr")[1:-1]:
            fields = entry("td")

            FILE = 0
            URL = 0
            MD5 = 1

            TYPE = 1
            PYVERSION = 2
            UPLOADED = 3
            SIZE = 4

            file_inf = fields[FILE]("a")[0]["href"].split("#")
            file_url = file_inf[URL]
            file_md5 = file_inf[MD5][4:]

            file_type = fields[TYPE].string
            file_pyversion = fields[PYVERSION].string
            file_uploaded = fields[UPLOADED].string
            file_size = fields[SIZE].string

            data["files"].append({
                "url": file_url,
                "md5": file_md5,
                "type": file_type,
                "pyversion": file_pyversion,
                "uploaded": file_uploaded,
                "size": file_size
            })
            entry.decompose()
        table.decompose()
    return List.unit(data)
示例#19
0
def get_ul(soup):
    data = {}
    data["info"] = {}
    uls = soup("ul", class_="nodot")
    if uls:
        if "Downloads (All Versions):" in uls[0]("strong")[0].string:
            ul = uls[1]
        else:
            ul = uls[0]

        for entry in ul.contents:
            if not hasattr(entry, "name") or entry.name != "li":
                continue
            entry_name = entry("strong")[0].string
            if not entry_name:
                continue

            if entry_name == "Categories":
                data["info"][entry_name] = {}
                for cat_entry in entry("a"):
                    cat_data = cat_entry.string.split(" :: ")
                    if not cat_data[0] in data["info"][entry_name]:
                        data["info"][entry_name][cat_data[0]] = cat_data[1:]
                    else:
                        data["info"][entry_name][cat_data[0]].extend(
                            cat_data[1:])
                continue

            if entry("span"):
                data["info"][entry_name] = entry("span")[0].string
                continue

            if entry("a"):
                data["info"][entry_name] = entry("a")[0]["href"]
                continue
            entry.decompose()
        ul.decompose()
    return List.unit(data)
示例#20
0
 def test_list_length_empty(self):
     xs = List()
     self.assertEqual(0, len(xs))
示例#21
0
 def test_list_list(self):
     xs = List().cons(List().cons(42))
     self.assertEqual(42, xs.head().head())
示例#22
0
 def test_list_tail_tail_null(self):
     xs = List().cons("b").cons("a")
     assert (xs.tail().tail().null())
示例#23
0
 def test_list_applicative_empty_arg_2(self):
     a = List.pure(lambda x, y: x+y).apply(List(42)).apply(List([]))
     self.assertEquals(a, List([]))
示例#24
0
 def test_list_append_non_empty(self):
     xs = List.from_iterable(range(5))
     ys = List.from_iterable(range(5, 10))
     zs = xs.append(ys)
     self.assertEqual(List.from_iterable(range(10)), zs)
示例#25
0
 def test_list_append_empty(self):
     xs = List()
     ys = List.unit(42)
     zs = xs.append(ys)
     self.assertEqual(ys, zs)
示例#26
0
 def test_list_head(self):
     x = List().cons(42).head()
     self.assertEqual(42, x)
示例#27
0
 def test_list_applicative_1(self):
     a = List.pure(lambda x, y: x+y).apply(List(2)).apply(List(40))
     self.assertEquals(a, List(42))
示例#28
0
 def test_list_applicative_2(self):
     a = List.pure(lambda x: x * 2).apply(List([1, 2]))  #.apply(List([3, 4]))
     self.assertEquals(a, List([2, 4]))
示例#29
0
 def test_list_applicative_3(self):
     a = List.pure(lambda x, y: x+y).apply(List([1, 2])).apply(List([4, 8]))
     self.assertEquals(a, List([5, 9, 6, 10]))
示例#30
0
 def test_list_length_non_empty(self):
     xs = List.unit(42)
     self.assertEqual(1, len(xs))
示例#31
0
 def test_list_length_multiple(self):
     xs = List.from_iterable(range(42))
     self.assertEqual(42, len(xs))
示例#32
0
    def test_list_monad_empty_bind(self):
        m = List()
        f = lambda x: unit(x * 10)

        self.assertEqual(m.bind(f), List())
示例#33
0
 def test_list_append_empty_other(self):
     xs = List.unit(42)
     ys = List()
     zs = xs.append(ys)
     self.assertEqual(xs, zs)
示例#34
0
 def test_list_append_empty_other(self):
     xs = List.unit(42)
     ys = List()
     zs = xs.append(ys)
     self.assertEqual(xs, zs)
示例#35
0
 def test_list_length_non_empty(self):
     xs = List.unit(42)
     self.assertEqual(1, len(xs))
示例#36
0
 def test_list_tail_head(self):
     xs = List().cons("b").cons("a")
     self.assertEqual("a", xs.head())
示例#37
0
 def test_list_append_empty(self):
     xs = List()
     ys = List.unit(42)
     zs = xs.append(ys)
     self.assertEqual(ys, zs)
示例#38
0
 def test_list_applicative_empty_func(self):
     a = List.pure([]).apply(List(42)).apply(List([1, 2, 3]))
     self.assertEquals(a, List([]))