1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308
|
# pylint: disable=too-many-lines,redefined-builtin,import-outside-toplevel
from asyncio import Future
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Iterable,
List,
Optional,
Set,
Tuple,
Type,
TypeVar,
Union,
cast,
overload,
)
from reactivex import (
ConnectableObservable,
GroupedObservable,
Notification,
Observable,
abc,
compose,
typing,
)
from reactivex.internal.basic import identity
from reactivex.internal.utils import NotSet
from reactivex.subject import Subject
from reactivex.typing import (
Accumulator,
Comparer,
Mapper,
MapperIndexed,
Predicate,
PredicateIndexed,
)
_T = TypeVar("_T")
_T1 = TypeVar("_T1")
_T2 = TypeVar("_T2")
_TKey = TypeVar("_TKey")
_TState = TypeVar("_TState")
_TValue = TypeVar("_TValue")
_TRight = TypeVar("_TRight")
_TLeft = TypeVar("_TLeft")
_A = TypeVar("_A")
_B = TypeVar("_B")
_C = TypeVar("_C")
_D = TypeVar("_D")
def all(predicate: Predicate[_T]) -> Callable[[Observable[_T]], Observable[bool]]:
"""Determines whether all elements of an observable sequence satisfy
a condition.
.. marble::
:alt: all
--1--2--3--4--5-|
[ all(i: i<10) ]
----------------true-|
Example:
>>> op = all(lambda value: value.length > 3)
Args:
predicate: A function to test each element for a condition.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing a single element
determining whether all elements in the source sequence pass
the test in the specified predicate.
"""
from ._all import all_
return all_(predicate)
def amb(right_source: Observable[_T]) -> Callable[[Observable[_T]], Observable[_T]]:
"""Propagates the observable sequence that reacts first.
.. marble::
:alt: amb
---8--6--9-----------|
--1--2--3---5--------|
----------10-20-30---|
[ amb() ]
--1--2--3---5--------|
Example:
>>> op = amb(ys)
Returns:
An operator function that takes an observable source and
returns an observable sequence that surfaces any of the given
sequences, whichever reacted first.
"""
from ._amb import amb_
return amb_(right_source)
def as_observable() -> Callable[[Observable[_T]], Observable[_T]]:
"""Hides the identity of an observable sequence.
Returns:
An operator function that takes an observable source and
returns and observable sequence that hides the identity of the
source sequence.
"""
from ._asobservable import as_observable_
return as_observable_()
def average(
key_mapper: Optional[Mapper[_T, float]] = None
) -> Callable[[Observable[_T]], Observable[float]]:
"""The average operator.
Computes the average of an observable sequence of values that
are in the sequence or obtained by invoking a transform function on
each element of the input sequence if present.
.. marble::
:alt: average
---1--2--3--4----|
[ average() ]
-----------------2.5-|
Examples:
>>> op = average()
>>> op = average(lambda x: x.value)
Args:
key_mapper: [Optional] A transform function to apply to each element.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing a single element with
the average of the sequence of values.
"""
from ._average import average_
return average_(key_mapper)
def buffer(
boundaries: Observable[Any],
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""Projects each element of an observable sequence into zero or
more buffers.
.. marble::
:alt: buffer
---a-----b-----c--------|
--1--2--3--4--5--6--7---|
[ buffer() ]
---1-----2,3---4,5------|
Examples:
>>> res = buffer(reactivex.interval(1.0))
Args:
boundaries: Observable sequence whose elements denote the
creation and completion of buffers.
Returns:
A function that takes an observable source and returns an
observable sequence of buffers.
"""
from ._buffer import buffer_
return buffer_(boundaries)
def buffer_when(
closing_mapper: Callable[[], Observable[Any]]
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""Projects each element of an observable sequence into zero or
more buffers.
.. marble::
:alt: buffer_when
--------c-|
--------c-|
--------c-|
---1--2--3--4--5--6-------|
[ buffer_when() ]
+-------1,2-----3,4,5---6-|
Examples:
>>> res = buffer_when(lambda: reactivex.timer(0.5))
Args:
closing_mapper: A function invoked to define the closing of each
produced buffer. A buffer is started when the previous one is
closed, resulting in non-overlapping buffers. The buffer is closed
when one item is emitted or when the observable completes.
Returns:
A function that takes an observable source and returns an
observable sequence of windows.
"""
from ._buffer import buffer_when_
return buffer_when_(closing_mapper)
def buffer_toggle(
openings: Observable[Any], closing_mapper: Callable[[Any], Observable[Any]]
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""Projects each element of an observable sequence into zero or
more buffers.
.. marble::
:alt: buffer_toggle
---a-----------b--------------|
---d--|
--------e--|
----1--2--3--4--5--6--7--8----|
[ buffer_toggle() ]
------1----------------5,6,7--|
>>> res = buffer_toggle(reactivex.interval(0.5), lambda i: reactivex.timer(i))
Args:
openings: Observable sequence whose elements denote the
creation of buffers.
closing_mapper: A function invoked to define the closing of each
produced buffer. Value from openings Observable that initiated
the associated buffer is provided as argument to the function. The
buffer is closed when one item is emitted or when the observable
completes.
Returns:
A function that takes an observable source and returns an
observable sequence of windows.
"""
from ._buffer import buffer_toggle_
return buffer_toggle_(openings, closing_mapper)
def buffer_with_count(
count: int, skip: Optional[int] = None
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""Projects each element of an observable sequence into zero or more
buffers which are produced based on element count information.
.. marble::
:alt: buffer_with_count
----1-2-3-4-5-6------|
[buffer_with_count(3)]
--------1,2,3-4,5,6--|
Examples:
>>> res = buffer_with_count(10)(xs)
>>> res = buffer_with_count(10, 1)(xs)
Args:
count: Length of each buffer.
skip: [Optional] Number of elements to skip between
creation of consecutive buffers. If not provided, defaults to
the count.
Returns:
A function that takes an observable source and returns an
observable sequence of buffers.
"""
from ._buffer import buffer_with_count_
return buffer_with_count_(count, skip)
def buffer_with_time(
timespan: typing.RelativeTime,
timeshift: Optional[typing.RelativeTime] = None,
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""Projects each element of an observable sequence into zero or more
buffers which are produced based on timing information.
.. marble::
:alt: buffer_with_time
---1-2-3-4-5-6-----|
[buffer_with_time()]
-------1,2,3-4,5,6-|
Examples:
>>> # non-overlapping segments of 1 second
>>> res = buffer_with_time(1.0)
>>> # segments of 1 second with time shift 0.5 seconds
>>> res = buffer_with_time(1.0, 0.5)
Args:
timespan: Length of each buffer (specified as a float denoting seconds
or an instance of timedelta).
timeshift: [Optional] Interval between creation of consecutive buffers
(specified as a float denoting seconds or an instance of timedelta).
If not specified, the timeshift will be the same as the timespan
argument, resulting in non-overlapping adjacent buffers.
scheduler: [Optional] Scheduler to run the timer on. If not specified,
the timeout scheduler is used
Returns:
An operator function that takes an observable source and
returns an observable sequence of buffers.
"""
from ._bufferwithtime import buffer_with_time_
return buffer_with_time_(timespan, timeshift, scheduler)
def buffer_with_time_or_count(
timespan: typing.RelativeTime,
count: int,
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""Projects each element of an observable sequence into a buffer
that is completed when either it's full or a given amount of time
has elapsed.
.. marble::
:alt: buffer_with_time_or_count
--1-2-3-4-5-6------|
[ buffer() ]
------1,2,3-4,5,6--|
Examples:
>>> # 5s or 50 items in an array
>>> res = source._buffer_with_time_or_count(5.0, 50)
>>> # 5s or 50 items in an array
>>> res = source._buffer_with_time_or_count(5.0, 50, Scheduler.timeout)
Args:
timespan: Maximum time length of a buffer.
count: Maximum element count of a buffer.
scheduler: [Optional] Scheduler to run buffering timers on. If
not specified, the timeout scheduler is used.
Returns:
An operator function that takes an observable source and
returns an observable sequence of buffers.
"""
from ._bufferwithtimeorcount import buffer_with_time_or_count_
return buffer_with_time_or_count_(timespan, count, scheduler)
def catch(
handler: Union[
Observable[_T], Callable[[Exception, Observable[_T]], Observable[_T]]
]
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Continues an observable sequence that is terminated by an
exception with the next observable sequence.
.. marble::
:alt: catch
---1---2---3-*
a-7-8-|
[ catch(a) ]
---1---2---3---7-8-|
Examples:
>>> op = catch(ys)
>>> op = catch(lambda ex, src: ys(ex))
Args:
handler: Second observable sequence used to produce
results when an error occurred in the first sequence, or an
exception handler function that returns an observable sequence
given the error and source observable that occurred in the
first sequence.
Returns:
A function taking an observable source and returns an
observable sequence containing the first sequence's elements,
followed by the elements of the handler sequence in case an
exception occurred.
"""
from ._catch import catch_
return catch_(handler)
def combine_latest(
*others: Observable[Any],
) -> Callable[[Observable[Any]], Observable[Any]]:
"""Merges the specified observable sequences into one observable
sequence by creating a tuple whenever any of the
observable sequences produces an element.
.. marble::
:alt: combine_latest
---a-----b--c------|
--1---2--------3---|
[ combine_latest() ]
---a1-a2-b2-c2-c3--|
Examples:
>>> obs = combine_latest(other)
>>> obs = combine_latest(obs1, obs2, obs3)
Returns:
An operator function that takes an observable sources and
returns an observable sequence containing the result of
combining elements of the sources into a tuple.
"""
from ._combinelatest import combine_latest_
return combine_latest_(*others)
def concat(*sources: Observable[_T]) -> Callable[[Observable[_T]], Observable[_T]]:
"""Concatenates all the observable sequences.
.. marble::
:alt: concat
---1--2--3--|
--6--8--|
[ concat() ]
---1--2--3----6--8-|
Examples:
>>> op = concat(xs, ys, zs)
Returns:
An operator function that takes one or more observable sources and
returns an observable sequence that contains the elements of
each given sequence, in sequential order.
"""
from ._concat import concat_
return concat_(*sources)
def contains(
value: _T, comparer: Optional[typing.Comparer[_T]] = None
) -> Callable[[Observable[_T]], Observable[bool]]:
"""Determines whether an observable sequence contains a specified
element with an optional equality comparer.
.. marble::
:alt: contains
--1--2--3--4--|
[ contains(3) ]
--------------true-|
Examples:
>>> op = contains(42)
>>> op = contains({ "value": 42 }, lambda x, y: x["value"] == y["value"])
Args:
value: The value to locate in the source sequence.
comparer: [Optional] An equality comparer to compare elements.
Returns:
A function that takes a source observable that returns an
observable sequence containing a single element determining
whether the source sequence contains an element that has the
specified value.
"""
from ._contains import contains_
return contains_(value, comparer)
def count(
predicate: Optional[typing.Predicate[_T]] = None,
) -> Callable[[Observable[_T]], Observable[int]]:
"""Returns an observable sequence containing a value that
represents how many elements in the specified observable sequence
satisfy a condition if provided, else the count of items.
.. marble::
:alt: count
--1--2--3--4--|
[ count(i: i>2) ]
--------------2-|
Examples:
>>> op = count()
>>> op = count(lambda x: x > 3)
Args:
predicate: A function to test each element for a condition.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing a single element with
a number that represents how many elements in the input
sequence satisfy the condition in the predicate function if
provided, else the count of items in the sequence.
"""
from ._count import count_
return count_(predicate)
def debounce(
duetime: typing.RelativeTime, scheduler: Optional[abc.SchedulerBase] = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Ignores values from an observable sequence which are followed by
another value before duetime.
.. marble::
:alt: debounce
--1--2-3-4--5------|
[ debounce() ]
----1------4---5---|
Example:
>>> res = debounce(5.0) # 5 seconds
Args:
duetime: Duration of the throttle period for each value
(specified as a float denoting seconds or an instance of timedelta).
scheduler: Scheduler to debounce values on.
Returns:
An operator function that takes the source observable and
returns the debounced observable sequence.
"""
from ._debounce import debounce_
return debounce_(duetime, scheduler)
throttle_with_timeout = debounce
@overload
def default_if_empty(
default_value: _T,
) -> Callable[[Observable[_T]], Observable[_T]]:
...
@overload
def default_if_empty() -> Callable[[Observable[_T]], Observable[Optional[_T]]]:
...
def default_if_empty(
default_value: Any = None,
) -> Callable[[Observable[Any]], Observable[Any]]:
"""Returns the elements of the specified sequence or the specified
value in a singleton sequence if the sequence is empty.
.. marble::
:alt: default_if_empty
----------|
[default_if_empty(42)]
----------42-|
Examples:
>>> res = obs = default_if_empty()
>>> obs = default_if_empty(False)
Args:
default_value: The value to return if the sequence is empty. If
not provided, this defaults to None.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains the specified
default value if the source is empty otherwise, the elements of
the source.
"""
from ._defaultifempty import default_if_empty_
return default_if_empty_(default_value)
def delay_subscription(
duetime: typing.AbsoluteOrRelativeTime,
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Time shifts the observable sequence by delaying the
subscription.
.. marble::
:alt: delay_subscription
----1--2--3--4-----|
[ delay() ]
--------1--2--3--4-|
Example:
>>> res = delay_subscription(5.0) # 5s
Args:
duetime: Absolute or relative time to perform the subscription
at.
scheduler: Scheduler to delay subscription on.
Returns:
A function that take a source observable and returns a
time-shifted observable sequence.
"""
from ._delaysubscription import delay_subscription_
return delay_subscription_(duetime, scheduler=scheduler)
def delay_with_mapper(
subscription_delay: Union[
Observable[Any],
typing.Mapper[Any, Observable[Any]],
None,
] = None,
delay_duration_mapper: Optional[typing.Mapper[_T, Observable[Any]]] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Time shifts the observable sequence based on a subscription
delay and a delay mapper function for each element.
.. marble::
:alt: delay_with_mapper
----1--2--3--4-----|
[ delay() ]
--------1--2--3--4-|
Examples:
>>> # with mapper only
>>> res = source.delay_with_mapper(lambda x: Scheduler.timer(5.0))
>>> # with delay and mapper
>>> res = source.delay_with_mapper(
reactivex.timer(2.0), lambda x: reactivex.timer(x)
)
Args:
subscription_delay: [Optional] Sequence indicating the delay
for the subscription to the source.
delay_duration_mapper: [Optional] Selector function to retrieve
a sequence indicating the delay for each given element.
Returns:
A function that takes an observable source and returns a
time-shifted observable sequence.
"""
from ._delaywithmapper import delay_with_mapper_
return delay_with_mapper_(subscription_delay, delay_duration_mapper)
def dematerialize() -> Callable[[Observable[Notification[_T]]], Observable[_T]]:
"""Dematerialize operator.
Dematerializes the explicit notification values of an
observable sequence as implicit notifications.
Returns:
An observable sequence exhibiting the behavior
corresponding to the source sequence's notification values.
"""
from ._dematerialize import dematerialize_
return dematerialize_()
def delay(
duetime: typing.RelativeTime, scheduler: Optional[abc.SchedulerBase] = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""The delay operator.
.. marble::
:alt: delay
----1--2--3--4-----|
[ delay() ]
--------1--2--3--4-|
Time shifts the observable sequence by duetime. The relative time
intervals between the values are preserved.
Examples:
>>> res = delay(timedelta(seconds=10))
>>> res = delay(5.0)
Args:
duetime: Relative time, specified as a float denoting seconds or an
instance of timedelta, by which to shift the observable sequence.
scheduler: [Optional] Scheduler to run the delay timers on.
If not specified, the timeout scheduler is used.
Returns:
A partially applied operator function that takes the source
observable and returns a time-shifted sequence.
"""
from ._delay import delay_
return delay_(duetime, scheduler)
def distinct(
key_mapper: Optional[Mapper[_T, _TKey]] = None,
comparer: Optional[Comparer[_TKey]] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns an observable sequence that contains only distinct
elements according to the key_mapper and the comparer. Usage of
this operator should be considered carefully due to the maintenance
of an internal lookup structure which can grow large.
.. marble::
:alt: distinct
-0-1-2-1-3-4-2-0---|
[ distinct() ]
-0-1-2---3-4-------|
Examples:
>>> res = obs = xs.distinct()
>>> obs = xs.distinct(lambda x: x.id)
>>> obs = xs.distinct(lambda x: x.id, lambda a,b: a == b)
Args:
key_mapper: [Optional] A function to compute the comparison
key for each element.
comparer: [Optional] Used to compare items in the collection.
Returns:
An operator function that takes an observable source and
returns an observable sequence only containing the distinct
elements, based on a computed key value, from the source
sequence.
"""
from ._distinct import distinct_
return distinct_(key_mapper, comparer)
def distinct_until_changed(
key_mapper: Optional[Mapper[_T, _TKey]] = None,
comparer: Optional[Comparer[_TKey]] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns an observable sequence that contains only distinct
contiguous elements according to the key_mapper and the comparer.
.. marble::
:alt: distinct_until_changed
-0-1-1-2-3-1-2-2-3-|
[ distinct() ]
-0-1---2-3-1-2---3-|
Examples:
>>> op = distinct_until_changed();
>>> op = distinct_until_changed(lambda x: x.id)
>>> op = distinct_until_changed(lambda x: x.id, lambda x, y: x == y)
Args:
key_mapper: [Optional] A function to compute the comparison key
for each element. If not provided, it projects the value.
comparer: [Optional] Equality comparer for computed key values.
If not provided, defaults to an equality comparer function.
Returns:
An operator function that takes an observable source and
returns an observable sequence only containing the distinct
contiguous elements, based on a computed key value, from the
source sequence.
"""
from ._distinctuntilchanged import distinct_until_changed_
return distinct_until_changed_(key_mapper, comparer)
def do(observer: abc.ObserverBase[_T]) -> Callable[[Observable[_T]], Observable[_T]]:
"""Invokes an action for each element in the observable sequence
and invokes an action on graceful or exceptional termination of the
observable sequence. This method can be used for debugging,
logging, etc. of query behavior by intercepting the message stream
to run arbitrary actions for messages on the pipeline.
.. marble::
:alt: do
----1---2---3---4---|
[ do(i: foo()) ]
----1---2---3---4---|
>>> do(observer)
Args:
observer: Observer
Returns:
An operator function that takes the source observable and
returns the source sequence with the side-effecting behavior
applied.
"""
from ._do import do_
return do_(observer)
def do_action(
on_next: Optional[typing.OnNext[_T]] = None,
on_error: Optional[typing.OnError] = None,
on_completed: Optional[typing.OnCompleted] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Invokes an action for each element in the observable sequence
and invokes an action on graceful or exceptional termination of the
observable sequence. This method can be used for debugging,
logging, etc. of query behavior by intercepting the message stream
to run arbitrary actions for messages on the pipeline.
.. marble::
:alt: do_action
----1---2---3---4---|
[do_action(i: foo())]
----1---2---3---4---|
Examples:
>>> do_action(send)
>>> do_action(on_next, on_error)
>>> do_action(on_next, on_error, on_completed)
Args:
on_next: [Optional] Action to invoke for each element in the
observable sequence.
on_error: [Optional] Action to invoke on exceptional
termination of the observable sequence.
on_completed: [Optional] Action to invoke on graceful
termination of the observable sequence.
Returns:
An operator function that takes the source observable an
returns the source sequence with the side-effecting behavior
applied.
"""
from ._do import do_action_
return do_action_(on_next, on_error, on_completed)
def do_while(
condition: Predicate[Observable[_T]],
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Repeats source as long as condition holds emulating a do while
loop.
.. marble::
:alt: do_while
--1--2--|
[ do_while() ]
--1--2--1--2--1--2--|
Args:
condition: The condition which determines if the source will be
repeated.
Returns:
An observable sequence which is repeated as long
as the condition holds.
"""
from ._dowhile import do_while_
return do_while_(condition)
def element_at(index: int) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns the element at a specified index in a sequence.
.. marble::
:alt: element_at
----1---2---3---4---|
[ element_at(2) ]
------------3-|
Example:
>>> res = source.element_at(5)
Args:
index: The zero-based index of the element to retrieve.
Returns:
An operator function that takes an observable source and
returns an observable sequence that produces the element at
the specified position in the source sequence.
"""
from ._elementatordefault import element_at_or_default_
return element_at_or_default_(index, False)
def element_at_or_default(
index: int, default_value: Optional[_T] = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns the element at a specified index in a sequence or a
default value if the index is out of range.
.. marble::
:alt: element_at_or_default
--1---2---3---4-|
[ element_at(6, a) ]
----------------a-|
Example:
>>> res = source.element_at_or_default(5)
>>> res = source.element_at_or_default(5, 0)
Args:
index: The zero-based index of the element to retrieve.
default_value: [Optional] The default value if the index is
outside the bounds of the source sequence.
Returns:
A function that takes an observable source and returns an
observable sequence that produces the element at the
specified position in the source sequence, or a default value if
the index is outside the bounds of the source sequence.
"""
from ._elementatordefault import element_at_or_default_
return element_at_or_default_(index, True, default_value)
def exclusive() -> Callable[[Observable[Observable[_T]]], Observable[_T]]:
"""Performs a exclusive waiting for the first to finish before
subscribing to another observable. Observables that come in between
subscriptions will be dropped on the floor.
.. marble::
:alt: exclusive
-+---+-----+-------|
+-7-8-9-|
+-4-5-6-|
+-1-2-3-|
[ exclusive() ]
---1-2-3-----7-8-9-|
Returns:
An exclusive observable with only the results that
happen when subscribed.
"""
from ._exclusive import exclusive_
return exclusive_()
def expand(
mapper: typing.Mapper[_T, Observable[_T]]
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Expands an observable sequence by recursively invoking mapper.
Args:
mapper: Mapper function to invoke for each produced element,
resulting in another sequence to which the mapper will be
invoked recursively again.
Returns:
An observable sequence containing all the elements produced
by the recursive expansion.
"""
from ._expand import expand_
return expand_(mapper)
def filter(predicate: Predicate[_T]) -> Callable[[Observable[_T]], Observable[_T]]:
"""Filters the elements of an observable sequence based on a
predicate.
.. marble::
:alt: filter
----1---2---3---4---|
[ filter(i: i>2) ]
------------3---4---|
Example:
>>> op = filter(lambda value: value < 10)
Args:
predicate: A function to test each source element for a
condition.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains elements from the
input sequence that satisfy the condition.
"""
from ._filter import filter_
return filter_(predicate)
def filter_indexed(
predicate_indexed: Optional[PredicateIndexed[_T]] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Filters the elements of an observable sequence based on a
predicate by incorporating the element's index.
.. marble::
:alt: filter_indexed
----1---2---3---4---|
[ filter(i,id: id>2)]
----------------4---|
Example:
>>> op = filter_indexed(lambda value, index: (value + index) < 10)
Args:
predicate: A function to test each source element for a
condition; the second parameter of the function represents
the index of the source element.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains elements from the
input sequence that satisfy the condition.
"""
from ._filter import filter_indexed_
return filter_indexed_(predicate_indexed)
def finally_action(action: typing.Action) -> Callable[[Observable[_T]], Observable[_T]]:
"""Invokes a specified action after the source observable sequence
terminates gracefully or exceptionally.
.. marble::
:alt: finally_action
--1--2--3--4--|
a-6-7-|
[finally_action(a)]
--1--2--3--4--6-7-|
Example:
>>> res = finally_action(lambda: print('sequence ended')
Args:
action: Action to invoke after the source observable sequence
terminates.
Returns:
An operator function that takes an observable source and
returns an observable sequence with the action-invoking
termination behavior applied.
"""
from ._finallyaction import finally_action_
return finally_action_(action)
def find(
predicate: Callable[[_T, int, Observable[_T]], bool]
) -> Callable[[Observable[_T]], Observable[Union[_T, None]]]:
"""Searches for an element that matches the conditions defined by
the specified predicate, and returns the first occurrence within
the entire Observable sequence.
.. marble::
:alt: find
--1--2--3--4--3--2--|
[ find(3) ]
--------3-|
Args:
predicate: The predicate that defines the conditions of the
element to search for.
Returns:
An operator function that takes an observable source and
returns an observable sequence with the first element that
matches the conditions defined by the specified predicate, if
found otherwise, None.
"""
from ._find import find_value_
return cast(
Callable[[Observable[_T]], Observable[Union[_T, None]]],
find_value_(predicate, False),
)
def find_index(
predicate: Callable[[_T, int, Observable[_T]], bool]
) -> Callable[[Observable[_T]], Observable[Union[int, None]]]:
"""Searches for an element that matches the conditions defined by
the specified predicate, and returns an Observable sequence with the
zero-based index of the first occurrence within the entire
Observable sequence.
.. marble::
:alt: find_index
--1--2--3--4--3--2--|
[ find_index(3) ]
--------2-|
Args:
predicate: The predicate that defines the conditions of the
element to search for.
Returns:
An operator function that takes an observable source and
returns an observable sequence with the zero-based index of the
first occurrence of an element that matches the conditions
defined by match, if found; otherwise, -1.
"""
from ._find import find_value_
return cast(
Callable[[Observable[_T]], Observable[Union[int, None]]],
find_value_(predicate, True),
)
def first(
predicate: Optional[Predicate[_T]] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns the first element of an observable sequence that
satisfies the condition in the predicate if present else the first
item in the sequence.
.. marble::
:alt: first
---1---2---3---4----|
[ first(i: i>1) ]
-------2-|
Examples:
>>> res = res = first()
>>> res = res = first(lambda x: x > 3)
Args:
predicate: [Optional] A predicate function to evaluate for
elements in the source sequence.
Returns:
A function that takes an observable source and returns an
observable sequence containing the first element in the
observable sequence that satisfies the condition in the
predicate if provided, else the first item in the sequence.
"""
from ._first import first_
return first_(predicate)
def first_or_default(
predicate: Optional[Predicate[_T]] = None, default_value: Optional[_T] = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns the first element of an observable sequence that
satisfies the condition in the predicate, or a default value if no
such element exists.
.. marble::
:alt: first_or_default
--1--2--3--4-|
[first(i: i>10, 42)]
-------------42-|
Examples:
>>> res = first_or_default()
>>> res = first_or_default(lambda x: x > 3)
>>> res = first_or_default(lambda x: x > 3, 0)
>>> res = first_or_default(None, 0)
Args:
predicate: [optional] A predicate function to evaluate for
elements in the source sequence.
default_value: [Optional] The default value if no such element
exists. If not specified, defaults to None.
Returns:
A function that takes an observable source and returns an
observable sequence containing the first element in the
observable sequence that satisfies the condition in the
predicate, or a default value if no such element exists.
"""
from ._firstordefault import first_or_default_
return first_or_default_(predicate, default_value)
@overload
def flat_map(
mapper: Optional[Iterable[_T2]] = None,
) -> Callable[[Observable[Any]], Observable[_T2]]:
...
@overload
def flat_map(
mapper: Optional[Observable[_T2]] = None,
) -> Callable[[Observable[Any]], Observable[_T2]]:
...
@overload
def flat_map(
mapper: Optional[Mapper[_T1, Iterable[_T2]]] = None
) -> Callable[[Observable[_T1]], Observable[_T2]]:
...
@overload
def flat_map(
mapper: Optional[Mapper[_T1, Observable[_T2]]] = None
) -> Callable[[Observable[_T1]], Observable[_T2]]:
...
def flat_map(
mapper: Optional[Any] = None,
) -> Callable[[Observable[Any]], Observable[Any]]:
"""The flat_map operator.
.. marble::
:alt: flat_map
--1-2-3-|
[ flat_map(range) ]
--0-0-1-0-1-2-|
One of the Following:
Projects each element of an observable sequence to an observable
sequence and merges the resulting observable sequences into one
observable sequence.
Example:
>>> flat_map(lambda x: Observable.range(0, x))
Or:
Projects each element of the source observable sequence to the
other observable sequence and merges the resulting observable
sequences into one observable sequence.
Example:
>>> flat_map(Observable.of(1, 2, 3))
Args:
mapper: A transform function to apply to each element or an
observable sequence to project each element from the source
sequence onto.
Returns:
An operator function that takes a source observable and returns
an observable sequence whose elements are the result of
invoking the one-to-many transform function on each element of
the input sequence.
"""
from ._flatmap import flat_map_
return flat_map_(mapper)
@overload
def flat_map_indexed(
mapper_indexed: Optional[Iterable[_T2]] = None,
) -> Callable[[Observable[Any]], Observable[_T2]]:
...
@overload
def flat_map_indexed(
mapper_indexed: Optional[Observable[_T2]] = None,
) -> Callable[[Observable[Any]], Observable[_T2]]:
...
@overload
def flat_map_indexed(
mapper_indexed: Optional[MapperIndexed[_T1, Iterable[_T2]]] = None
) -> Callable[[Observable[_T1]], Observable[_T2]]:
...
@overload
def flat_map_indexed(
mapper_indexed: Optional[MapperIndexed[_T1, Observable[_T2]]] = None
) -> Callable[[Observable[_T1]], Observable[_T2]]:
...
def flat_map_indexed(
mapper_indexed: Any = None,
) -> Callable[[Observable[Any]], Observable[Any]]:
"""The `flat_map_indexed` operator.
One of the Following:
Projects each element of an observable sequence to an observable
sequence and merges the resulting observable sequences into one
observable sequence.
.. marble::
:alt: flat_map_indexed
--1-2-3-|
[ flat_map(range) ]
--0-0-1-0-1-2-|
Example:
>>> source.flat_map_indexed(lambda x, i: Observable.range(0, x))
Or:
Projects each element of the source observable sequence to the
other observable sequence and merges the resulting observable
sequences into one observable sequence.
Example:
>>> source.flat_map_indexed(Observable.of(1, 2, 3))
Args:
mapper_indexed: [Optional] A transform function to apply to
each element or an observable sequence to project each
element from the source sequence onto.
Returns:
An operator function that takes an observable source and
returns an observable sequence whose elements are the result of
invoking the one-to-many transform function on each element of
the input sequence.
"""
from ._flatmap import flat_map_indexed_
return flat_map_indexed_(mapper_indexed)
def flat_map_latest(
mapper: Mapper[_T1, Union[Observable[_T2], "Future[_T2]"]]
) -> Callable[[Observable[_T1]], Observable[_T2]]:
"""Projects each element of an observable sequence into a new
sequence of observable sequences by incorporating the element's
index and then transforms an observable sequence of observable
sequences into an observable sequence producing values only from
the most recent observable sequence.
Args:
mapper: A transform function to apply to each source element.
The second parameter of the function represents the index
of the source element.
Returns:
An operator function that takes an observable source and
returns an observable sequence whose elements are the result of
invoking the transform function on each element of source
producing an observable of Observable sequences and that at any
point in time produces the elements of the most recent inner
observable sequence that has been received.
"""
from ._flatmap import flat_map_latest_
return flat_map_latest_(mapper)
def fork_join(
*others: Observable[Any],
) -> Callable[[Observable[Any]], Observable[Tuple[Any, ...]]]:
"""Wait for observables to complete and then combine last values
they emitted into a tuple. Whenever any of that observables completes
without emitting any value, result sequence will complete at that moment as well.
.. marble::
:alt: fork_join
---a-----b--c---d-|
--1---2------3-4---|
-a---------b---|
[ fork_join() ]
--------------------d4b|
Examples:
>>> res = fork_join(obs1)
>>> res = fork_join(obs1, obs2, obs3)
Returns:
An operator function that takes an observable source and
return an observable sequence containing the result
of combining last element from each source in given sequence.
"""
from ._forkjoin import fork_join_
return fork_join_(*others)
def group_by(
key_mapper: Mapper[_T, _TKey],
element_mapper: Optional[Mapper[_T, _TValue]] = None,
subject_mapper: Optional[Callable[[], Subject[_TValue]]] = None,
) -> Callable[[Observable[_T]], Observable[GroupedObservable[_TKey, _TValue]]]:
"""Groups the elements of an observable sequence according to a
specified key mapper function and comparer and selects the
resulting elements by using a specified function.
.. marble::
:alt: group_by
--1--2--a--3--b--c-|
[ group_by() ]
-+-----+-----------|
+a-----b--c-|
+1--2-----3-------|
Examples:
>>> group_by(lambda x: x.id)
>>> group_by(lambda x: x.id, lambda x: x.name)
>>> group_by(lambda x: x.id, lambda x: x.name, lambda: ReplaySubject())
Keyword arguments:
key_mapper: A function to extract the key for each element.
element_mapper: [Optional] A function to map each source
element to an element in an observable group.
subject_mapper: A function that returns a subject used to initiate
a grouped observable. Default mapper returns a Subject object.
Returns:
An operator function that takes an observable source and
returns a sequence of observable groups, each of which
corresponds to a unique key value, containing all elements that
share that same key value.
"""
from ._groupby import group_by_
return group_by_(key_mapper, element_mapper, subject_mapper)
def group_by_until(
key_mapper: Mapper[_T, _TKey],
element_mapper: Optional[Mapper[_T, _TValue]],
duration_mapper: Callable[[GroupedObservable[_TKey, _TValue]], Observable[Any]],
subject_mapper: Optional[Callable[[], Subject[_TValue]]] = None,
) -> Callable[[Observable[_T]], Observable[GroupedObservable[_TKey, _TValue]]]:
"""Groups the elements of an observable sequence according to a
specified key mapper function. A duration mapper function is used
to control the lifetime of groups. When a group expires, it
receives an OnCompleted notification. When a new element with the
same key value as a reclaimed group occurs, the group will be
reborn with a new lifetime request.
.. marble::
:alt: group_by_until
--1--2--a--3--b--c-|
[ group_by_until() ]
-+-----+-----------|
+a-----b--c-|
+1--2-----3-------|
Examples:
>>> group_by_until(lambda x: x.id, None, lambda : reactivex.never())
>>> group_by_until(
lambda x: x.id, lambda x: x.name, lambda grp: reactivex.never()
)
>>> group_by_until(
lambda x: x.id,
lambda x: x.name,
lambda grp: reactivex.never(),
lambda: ReplaySubject()
)
Args:
key_mapper: A function to extract the key for each element.
element_mapper: A function to map each source element to an element in
an observable group.
duration_mapper: A function to signal the expiration of a group.
subject_mapper: A function that returns a subject used to initiate
a grouped observable. Default mapper returns a Subject object.
Returns:
An operator function that takes an observable source and
returns a sequence of observable groups, each of which
corresponds to a unique key value, containing all elements that
share that same key value. If a group's lifetime expires, a new
group with the same key value can be created once an element
with such a key value is encountered.
"""
from ._groupbyuntil import group_by_until_
return group_by_until_(key_mapper, element_mapper, duration_mapper, subject_mapper)
def group_join(
right: Observable[_TRight],
left_duration_mapper: Callable[[_TLeft], Observable[Any]],
right_duration_mapper: Callable[[_TRight], Observable[Any]],
) -> Callable[[Observable[_TLeft]], Observable[Tuple[_TLeft, Observable[_TRight]]]]:
"""Correlates the elements of two sequences based on overlapping
durations, and groups the results.
.. marble::
:alt: group_join
-1---2----3---4---->
--a--------b-----c->
[ group_join() ]
--a1-a2----b3-b4-c4|
Args:
right: The right observable sequence to join elements for.
left_duration_mapper: A function to select the duration
(expressed as an observable sequence) of each element of
the left observable sequence, used to determine overlap.
right_duration_mapper: A function to select the duration
(expressed as an observable sequence) of each element of
the right observable sequence, used to determine overlap.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains elements combined into
a tuple from source elements that have an overlapping
duration.
"""
from ._groupjoin import group_join_
return group_join_(right, left_duration_mapper, right_duration_mapper)
def ignore_elements() -> Callable[[Observable[_T]], Observable[_T]]:
"""Ignores all elements in an observable sequence leaving only the
termination messages.
.. marble::
:alt: ignore_elements
---1---2---3---4---|
[ ignore_elements()]
-------------------|
Returns:
An operator function that takes an observable source and
returns an empty observable sequence that signals termination,
successful or exceptional, of the source sequence.
"""
from ._ignoreelements import ignore_elements_
return ignore_elements_()
def is_empty() -> Callable[[Observable[Any]], Observable[bool]]:
"""Determines whether an observable sequence is empty.
.. marble::
:alt: is_empty
-------|
[ is_empty() ]
-------True-|
Returns:
An operator function that takes an observable source and
returns an observable sequence containing a single element
determining whether the source sequence is empty.
"""
from ._isempty import is_empty_
return is_empty_()
def join(
right: Observable[_T2],
left_duration_mapper: Callable[[Any], Observable[Any]],
right_duration_mapper: Callable[[Any], Observable[Any]],
) -> Callable[[Observable[_T1]], Observable[Tuple[_T1, _T2]]]:
"""Correlates the elements of two sequences based on overlapping
durations.
.. marble::
:alt: join
-1---2----3---4---->
--a--------b-----c->
[ join() ]
--a1-a2----b3-b4-c4|
Args:
right: The right observable sequence to join elements for.
left_duration_mapper: A function to select the duration
(expressed as an observable sequence) of each element of
the left observable sequence, used to determine overlap.
right_duration_mapper: A function to select the duration
(expressed as an observable sequence) of each element of
the right observable sequence, used to determine overlap.
Return:
An operator function that takes an observable source and
returns an observable sequence that contains elements combined
into a tuple from source elements that have an overlapping
duration.
"""
from ._join import join_
return join_(right, left_duration_mapper, right_duration_mapper)
def last(
predicate: Optional[Predicate[_T]] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""The last operator.
Returns the last element of an observable sequence that satisfies
the condition in the predicate if specified, else the last element.
.. marble::
:alt: last
---1--2--3--4-|
[ last() ]
------------4-|
Examples:
>>> op = last()
>>> op = last(lambda x: x > 3)
Args:
predicate: [Optional] A predicate function to evaluate for
elements in the source sequence.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing the last element in
the observable sequence that satisfies the condition in the
predicate.
"""
from ._last import last_
return last_(predicate)
@overload
def last_or_default() -> Callable[[Observable[_T]], Observable[Optional[_T]]]:
...
@overload
def last_or_default(
default_value: _T,
) -> Callable[[Observable[_T]], Observable[_T]]:
...
@overload
def last_or_default(
default_value: _T,
predicate: Predicate[_T],
) -> Callable[[Observable[_T]], Observable[_T]]:
...
def last_or_default(
default_value: Any = None,
predicate: Optional[Predicate[_T]] = None,
) -> Callable[[Observable[_T]], Observable[Any]]:
"""The last_or_default operator.
Returns the last element of an observable sequence that satisfies
the condition in the predicate, or a default value if no such
element exists.
.. marble::
:alt: last
---1--2--3--4-|
[last_or_default(8)]
--------------8-|
Examples:
>>> res = last_or_default()
>>> res = last_or_default(lambda x: x > 3)
>>> res = last_or_default(lambda x: x > 3, 0)
>>> res = last_or_default(None, 0)
Args:
predicate: [Optional] A predicate function to evaluate for
elements in the source sequence.
default_value: [Optional] The default value if no such element
exists. If not specified, defaults to None.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing the last element in
the observable sequence that satisfies the condition in the
predicate, or a default value if no such element exists.
"""
from ._lastordefault import last_or_default
return last_or_default(default_value, predicate)
def map(
mapper: Optional[Mapper[_T1, _T2]] = None
) -> Callable[[Observable[_T1]], Observable[_T2]]:
"""The map operator.
Project each element of an observable sequence into a new form.
.. marble::
:alt: map
---1---2---3---4--->
[ map(i: i*2) ]
---2---4---6---8--->
Example:
>>> map(lambda value: value * 10)
Args:
mapper: A transform function to apply to each source element.
Returns:
A partially applied operator function that takes an observable
source and returns an observable sequence whose elements are
the result of invoking the transform function on each element
of the source.
"""
from ._map import map_
return map_(mapper)
def map_indexed(
mapper_indexed: Optional[MapperIndexed[_T1, _T2]] = None
) -> Callable[[Observable[_T1]], Observable[_T2]]:
"""Project each element of an observable sequence into a new form
by incorporating the element's index.
.. marble::
:alt: map_indexed
---1---2---3---4--->
[ map(i,id: i*2) ]
---2---4---6---8--->
Example:
>>> ret = map_indexed(lambda value, index: value * value + index)
Args:
mapper_indexed: A transform function to apply to each source
element. The second parameter of the function represents
the index of the source element.
Returns:
A partially applied operator function that takes an observable
source and returns an observable sequence whose elements are
the result of invoking the transform function on each element
of the source.
"""
from ._map import map_indexed_
return map_indexed_(mapper_indexed)
def materialize() -> Callable[[Observable[_T]], Observable[Notification[_T]]]:
"""Materializes the implicit notifications of an observable
sequence as explicit notification values.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing the materialized
notification values from the source sequence.
"""
from ._materialize import materialize
return materialize()
def max(
comparer: Optional[Comparer[_T]] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns the maximum value in an observable sequence according to
the specified comparer.
.. marble::
:alt: max
---1--2--3--4-|
[ max() ]
--------------4-|
Examples:
>>> op = max()
>>> op = max(lambda x, y: x.value - y.value)
Args:
comparer: [Optional] Comparer used to compare elements.
Returns:
A partially applied operator function that takes an observable
source and returns an observable sequence containing a single
element with the maximum element in the source sequence.
"""
from ._max import max_
return max_(comparer)
def max_by(
key_mapper: Mapper[_T, _TKey], comparer: Optional[Comparer[_TKey]] = None
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""The max_by operator.
Returns the elements in an observable sequence with the maximum
key value according to the specified comparer.
.. marble::
:alt: max_by
---1--2--3--4-|
[ max_by() ]
--------------4-|
Examples:
>>> res = max_by(lambda x: x.value)
>>> res = max_by(lambda x: x.value, lambda x, y: x - y)
Args:
key_mapper: Key mapper function.
comparer: [Optional] Comparer used to compare key values.
Returns:
A partially applied operator function that takes an observable
source and return an observable sequence containing a list of
zero or more elements that have a maximum key value.
"""
from ._maxby import max_by_
return max_by_(key_mapper, comparer)
def merge(
*sources: Observable[Any], max_concurrent: Optional[int] = None
) -> Callable[[Observable[Any]], Observable[Any]]:
"""Merges an observable sequence of observable sequences into an
observable sequence, limiting the number of concurrent
subscriptions to inner sequences. Or merges two observable
sequences into a single observable sequence.
.. marble::
:alt: merge
---1---2---3---4-|
-a---b---c---d--|
[ merge() ]
-a-1-b-2-c-3-d-4-|
Examples:
>>> op = merge(max_concurrent=1)
>>> op = merge(other_source)
Args:
max_concurrent: [Optional] Maximum number of inner observable
sequences being subscribed to concurrently or the second
observable sequence.
Returns:
An operator function that takes an observable source and
returns the observable sequence that merges the elements of the
inner sequences.
"""
from ._merge import merge_
return merge_(*sources, max_concurrent=max_concurrent)
def merge_all() -> Callable[[Observable[Observable[_T]]], Observable[_T]]:
"""The merge_all operator.
Merges an observable sequence of observable sequences into an
observable sequence.
.. marble::
:alt: merge_all
---1---2---3---4-|
-a---b---c---d--|
[ merge_all() ]
-a-1-b-2-c-3-d-4-|
Returns:
A partially applied operator function that takes an observable
source and returns the observable sequence that merges the
elements of the inner sequences.
"""
from ._merge import merge_all_
return merge_all_()
def min(
comparer: Optional[Comparer[_T]] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""The `min` operator.
Returns the minimum element in an observable sequence according to
the optional comparer else a default greater than less than check.
.. marble::
:alt: min
---1--2--3--4-|
[ min() ]
--------------1-|
Examples:
>>> res = source.min()
>>> res = source.min(lambda x, y: x.value - y.value)
Args:
comparer: [Optional] Comparer used to compare elements.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing a single element
with the minimum element in the source sequence.
"""
from ._min import min_
return min_(comparer)
def min_by(
key_mapper: Mapper[_T, _TKey], comparer: Optional[Comparer[_TKey]] = None
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""The `min_by` operator.
Returns the elements in an observable sequence with the minimum key
value according to the specified comparer.
.. marble::
:alt: min_by
---1--2--3--4-|
[ min_by() ]
--------------1-|
Examples:
>>> res = min_by(lambda x: x.value)
>>> res = min_by(lambda x: x.value, lambda x, y: x - y)
Args:
key_mapper: Key mapper function.
comparer: [Optional] Comparer used to compare key values.
Returns:
An operator function that takes an observable source and
reuturns an observable sequence containing a list of zero
or more elements that have a minimum key value.
"""
from ._minby import min_by_
return min_by_(key_mapper, comparer)
@overload
def multicast() -> Callable[[Observable[_T]], ConnectableObservable[_T]]:
...
@overload
def multicast(
subject: abc.SubjectBase[_T],
) -> Callable[[Observable[_T]], ConnectableObservable[_T]]:
...
@overload
def multicast(
*,
subject_factory: Callable[[Optional[abc.SchedulerBase]], abc.SubjectBase[_T]],
mapper: Optional[Callable[[Observable[_T]], Observable[_T2]]] = None,
) -> Callable[[Observable[_T]], Observable[_T2]]:
...
def multicast(
subject: Optional[abc.SubjectBase[_T]] = None,
*,
subject_factory: Optional[
Callable[[Optional[abc.SchedulerBase]], abc.SubjectBase[_T]]
] = None,
mapper: Optional[Callable[[Observable[_T]], Observable[_T2]]] = None,
) -> Callable[[Observable[_T]], Union[Observable[_T2], ConnectableObservable[_T]]]:
"""Multicasts the source sequence notifications through an
instantiated subject into all uses of the sequence within a mapper
function. Each subscription to the resulting sequence causes a
separate multicast invocation, exposing the sequence resulting from
the mapper function's invocation. For specializations with fixed
subject types, see Publish, PublishLast, and Replay.
Examples:
>>> res = multicast(observable)
>>> res = multicast(
subject_factory=lambda scheduler: Subject(), mapper=lambda x: x
)
Args:
subject_factory: Factory function to create an intermediate
subject through which the source sequence's elements will
be multicast to the mapper function.
subject: Subject to push source elements into.
mapper: [Optional] Mapper function which can use the
multicasted source sequence subject to the policies
enforced by the created subject. Specified only if
subject_factory" is a factory function.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains the elements of a
sequence produced by multicasting the source sequence within a
mapper function.
"""
from ._multicast import multicast_
return multicast_(subject, subject_factory=subject_factory, mapper=mapper)
def observe_on(
scheduler: abc.SchedulerBase,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Wraps the source sequence in order to run its observer callbacks
on the specified scheduler.
Args:
scheduler: Scheduler to notify observers on.
This only invokes observer callbacks on a scheduler. In case the
subscription and/or unsubscription actions have side-effects
that require to be run on a scheduler, use subscribe_on.
Returns:
An operator function that takes an observable source and
returns the source sequence whose observations happen on the
specified scheduler.
"""
from ._observeon import observe_on_
return observe_on_(scheduler)
def on_error_resume_next(
second: Observable[_T],
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Continues an observable sequence that is terminated normally
or by an exception with the next observable sequence.
.. marble::
:alt: on_error
---1--2--3--4-*
e-a--b-|
[ on_error(e) ]
-1--2--3--4-a--b-|
Keyword arguments:
second: Second observable sequence used to produce results
after the first sequence terminates.
Returns:
An observable sequence that concatenates the first and
second sequence, even if the first sequence terminates
exceptionally.
"""
from ._onerrorresumenext import on_error_resume_next_
return on_error_resume_next_(second)
def pairwise() -> Callable[[Observable[_T]], Observable[Tuple[_T, _T]]]:
"""The pairwise operator.
Returns a new observable that triggers on the second and subsequent
triggerings of the input observable. The Nth triggering of the
input observable passes the arguments from the N-1th and Nth
triggering as a pair. The argument passed to the N-1th triggering
is held in hidden internal state until the Nth triggering occurs.
Returns:
An operator function that takes an observable source and
returns an observable that triggers on successive pairs of
observations from the input observable as an array.
"""
from ._pairwise import pairwise_
return pairwise_()
def partition(
predicate: Predicate[_T],
) -> Callable[[Observable[_T]], List[Observable[_T]]]:
"""Returns two observables which partition the observations of the
source by the given function. The first will trigger observations
for those values for which the predicate returns true. The second
will trigger observations for those values where the predicate
returns false. The predicate is executed once for each subscribed
observer. Both also propagate all error observations arising from
the source and each completes when the source completes.
.. marble::
:alt: partition
---1--2--3--4--|
[ partition(even) ]
---1-----3-----|
------2-----4--|
Args:
predicate: The function to determine which output Observable
will trigger a particular observation.
Returns:
An operator function that takes an observable source and
returns a list of observables. The first triggers when the
predicate returns True, and the second triggers when the
predicate returns False.
"""
from ._partition import partition_
return partition_(predicate)
def partition_indexed(
predicate_indexed: PredicateIndexed[_T],
) -> Callable[[Observable[_T]], List[Observable[_T]]]:
"""The indexed partition operator.
Returns two observables which partition the observations of the
source by the given function. The first will trigger observations
for those values for which the predicate returns true. The second
will trigger observations for those values where the predicate
returns false. The predicate is executed once for each subscribed
observer. Both also propagate all error observations arising from
the source and each completes when the source completes.
.. marble::
:alt: partition_indexed
---1--2--3--4--|
[ partition(even) ]
---1-----3-----|
------2-----4--|
Args:
predicate: The function to determine which output Observable
will trigger a particular observation.
Returns:
A list of observables. The first triggers when the predicate
returns True, and the second triggers when the predicate
returns False.
"""
from ._partition import partition_indexed_
return partition_indexed_(predicate_indexed)
def pluck(
key: _TKey,
) -> Callable[[Observable[Dict[_TKey, _TValue]]], Observable[_TValue]]:
"""Retrieves the value of a specified key using dict-like access (as in
element[key]) from all elements in the Observable sequence.
To pluck an attribute of each element, use pluck_attr.
Args:
key: The key to pluck.
Returns:
An operator function that takes an observable source and
returns a new observable sequence of key values.
"""
from ._pluck import pluck_
return pluck_(key)
def pluck_attr(prop: str) -> Callable[[Observable[Any]], Observable[Any]]:
"""Retrieves the value of a specified property (using getattr) from
all elements in the Observable sequence.
To pluck values using dict-like access (as in element[key]) on each
element, use pluck.
Args:
property: The property to pluck.
Returns:
An operator function that takes an observable source and
returns a new observable sequence of property values.
"""
from ._pluck import pluck_attr_
return pluck_attr_(prop)
@overload
def publish() -> Callable[[Observable[_T1]], ConnectableObservable[_T1]]:
...
@overload
def publish(
mapper: Mapper[Observable[_T1], Observable[_T2]],
) -> Callable[[Observable[_T1]], Observable[_T2]]:
...
def publish(
mapper: Optional[Mapper[Observable[_T1], Observable[_T2]]] = None,
) -> Callable[[Observable[_T1]], Union[Observable[_T2], ConnectableObservable[_T1]]]:
"""The `publish` operator.
Returns an observable sequence that is the result of invoking the
mapper on a connectable observable sequence that shares a single
subscription to the underlying sequence. This operator is a
specialization of Multicast using a regular Subject.
Example:
>>> res = publish()
>>> res = publish(lambda x: x)
Args:
mapper: [Optional] Selector function which can use the
multicasted source sequence as many times as needed,
without causing multiple subscriptions to the source
sequence. Subscribers to the given source will receive all
notifications of the source from the time of the
subscription on.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains the elements of a
sequence produced by multicasting the source sequence within a
mapper function.
"""
from ._publish import publish_
return publish_(mapper)
@overload
def publish_value(
initial_value: _T1,
) -> Callable[[Observable[_T1]], ConnectableObservable[_T1]]:
...
@overload
def publish_value(
initial_value: _T1,
mapper: Mapper[Observable[_T1], Observable[_T2]],
) -> Callable[[Observable[_T1]], Observable[_T2]]:
...
def publish_value(
initial_value: _T1,
mapper: Optional[Mapper[Observable[_T1], Observable[_T2]]] = None,
) -> Callable[[Observable[_T1]], Union[Observable[_T2], ConnectableObservable[_T1]]]:
"""Returns an observable sequence that is the result of invoking
the mapper on a connectable observable sequence that shares a
single subscription to the underlying sequence and starts with
initial_value.
This operator is a specialization of Multicast using a
BehaviorSubject.
Examples:
>>> res = source.publish_value(42)
>>> res = source.publish_value(42, lambda x: x.map(lambda y: y * y))
Args:
initial_value: Initial value received by observers upon
subscription.
mapper: [Optional] Optional mapper function which can use the
multicasted source sequence as many times as needed,
without causing multiple subscriptions to the source
sequence. Subscribers to the given source will receive
immediately receive the initial value, followed by all
notifications of the source from the time of the
subscription on.
Returns:
An operator function that takes an observable source and returns
an observable sequence that contains the elements of a
sequence produced by multicasting the source sequence within a
mapper function.
"""
from ._publishvalue import publish_value_
return publish_value_(initial_value, mapper)
@overload
def reduce(
accumulator: Accumulator[_TState, _T]
) -> Callable[[Observable[_T]], Observable[_T]]:
...
@overload
def reduce(
accumulator: Accumulator[_TState, _T], seed: _TState
) -> Callable[[Observable[_T]], Observable[_TState]]:
...
def reduce(
accumulator: Accumulator[_TState, _T], seed: Union[_TState, Type[NotSet]] = NotSet
) -> Callable[[Observable[_T]], Observable[Any]]:
"""The reduce operator.
Applies an accumulator function over an observable sequence,
returning the result of the aggregation as a single element in the
result sequence. The specified seed value is used as the initial
accumulator value.
For aggregation behavior with incremental intermediate results,
see `scan`.
.. marble::
:alt: reduce
---1--2--3--4--|
[reduce(acc,i: acc+i)]
---------------10-|
Examples:
>>> res = reduce(lambda acc, x: acc + x)
>>> res = reduce(lambda acc, x: acc + x, 0)
Args:
accumulator: An accumulator function to be invoked on each
element.
seed: Optional initial accumulator value.
Returns:
A partially applied operator function that takes an observable
source and returns an observable sequence containing a single
element with the final accumulator value.
"""
from ._reduce import reduce_
return reduce_(accumulator, seed)
def ref_count() -> Callable[[ConnectableObservable[_T]], Observable[_T]]:
"""Returns an observable sequence that stays connected to the
source as long as there is at least one subscription to the
observable sequence.
"""
from .connectable._refcount import ref_count_
return ref_count_()
def repeat(
repeat_count: Optional[int] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Repeats the observable sequence a specified number of times.
If the repeat count is not specified, the sequence repeats
indefinitely.
.. marble::
:alt: repeat
-1--2-|
[ repeat(3) ]
-1--2--1--2--1--2-|
Examples:
>>> repeated = repeat()
>>> repeated = repeat(42)
Args:
repeat_count: Number of times to repeat the sequence. If not
provided, repeats the sequence indefinitely.
Returns:
An operator function that takes an observable sources and
returns an observable sequence producing the elements of the
given sequence repeatedly.
"""
from ._repeat import repeat_
return repeat_(repeat_count)
@overload
def replay(
buffer_size: Optional[int] = None,
window: Optional[typing.RelativeTime] = None,
*,
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T1]], ConnectableObservable[_T1]]:
...
@overload
def replay(
buffer_size: Optional[int] = None,
window: Optional[typing.RelativeTime] = None,
*,
mapper: Optional[Mapper[Observable[_T1], Observable[_T2]]],
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T1]], Observable[_T2]]:
...
def replay(
buffer_size: Optional[int] = None,
window: Optional[typing.RelativeTime] = None,
*,
mapper: Optional[Mapper[Observable[_T1], Observable[_T2]]] = None,
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T1]], Union[Observable[_T2], ConnectableObservable[_T1]]]:
"""The `replay` operator.
Returns an observable sequence that is the result of invoking the
mapper on a connectable observable sequence that shares a single
subscription to the underlying sequence replaying notifications
subject to a maximum time length for the replay buffer.
This operator is a specialization of Multicast using a
ReplaySubject.
Examples:
>>> res = replay(buffer_size=3)
>>> res = replay(buffer_size=3, window=0.5)
>>> res = replay(None, 3, 0.5)
>>> res = replay(lambda x: x.take(6).repeat(), 3, 0.5)
Args:
mapper: [Optional] Selector function which can use the
multicasted source sequence as many times as needed,
without causing multiple subscriptions to the source
sequence. Subscribers to the given source will receive all
the notifications of the source subject to the specified
replay buffer trimming policy.
buffer_size: [Optional] Maximum element count of the replay
buffer.
window: [Optional] Maximum time length of the replay buffer.
scheduler: [Optional] Scheduler the observers are invoked on.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains the elements of a
sequence produced by multicasting the source sequence within a
mapper function.
"""
from ._replay import replay_
return replay_(mapper, buffer_size, window, scheduler=scheduler)
def retry(
retry_count: Optional[int] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Repeats the source observable sequence the specified number of
times or until it successfully terminates. If the retry count is
not specified, it retries indefinitely.
Examples:
>>> retried = retry()
>>> retried = retry(42)
Args:
retry_count: [Optional] Number of times to retry the sequence.
If not provided, retry the sequence indefinitely.
Returns:
An observable sequence producing the elements of the given
sequence repeatedly until it terminates successfully.
"""
from ._retry import retry_
return retry_(retry_count)
def sample(
sampler: Union[typing.RelativeTime, Observable[Any]],
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Samples the observable sequence at each interval.
.. marble::
:alt: sample
---1-2-3-4------|
[ sample(4) ]
----1---3---4---|
Examples:
>>> res = sample(sample_observable) # Sampler tick sequence
>>> res = sample(5.0) # 5 seconds
Args:
sampler: Observable used to sample the source observable **or** time
interval at which to sample (specified as a float denoting
seconds or an instance of timedelta).
scheduler: Scheduler to use only when a time interval is given.
Returns:
An operator function that takes an observable source and
returns a sampled observable sequence.
"""
from ._sample import sample_
return sample_(sampler, scheduler)
@overload
def scan(
accumulator: Accumulator[_T, _T]
) -> Callable[[Observable[_T]], Observable[_T]]:
...
@overload
def scan(
accumulator: Accumulator[_TState, _T], seed: Union[_TState, Type[NotSet]]
) -> Callable[[Observable[_T]], Observable[_TState]]:
...
def scan(
accumulator: Accumulator[_TState, _T], seed: Union[_TState, Type[NotSet]] = NotSet
) -> Callable[[Observable[_T]], Observable[_TState]]:
"""The scan operator.
Applies an accumulator function over an observable sequence and
returns each intermediate result. The optional seed value is used
as the initial accumulator value. For aggregation behavior with no
intermediate results, see `aggregate()` or `Observable()`.
.. marble::
:alt: scan
----1--2--3--4-----|
[scan(acc,i: acc+i)]
----1--3--6--10----|
Examples:
>>> scanned = source.scan(lambda acc, x: acc + x)
>>> scanned = source.scan(lambda acc, x: acc + x, 0)
Args:
accumulator: An accumulator function to be invoked on each
element.
seed: [Optional] The initial accumulator value.
Returns:
A partially applied operator function that takes an observable
source and returns an observable sequence containing the
accumulated values.
"""
from ._scan import scan_
return scan_(accumulator, seed)
def sequence_equal(
second: Union[Observable[_T], Iterable[_T]], comparer: Optional[Comparer[_T]] = None
) -> Callable[[Observable[_T]], Observable[bool]]:
"""Determines whether two sequences are equal by comparing the
elements pairwise using a specified equality comparer.
.. marble::
:alt: scan
-1--2--3--4----|
----1--2--3--4-|
[ sequence_equal() ]
---------------True|
Examples:
>>> res = sequence_equal([1,2,3])
>>> res = sequence_equal([{ "value": 42 }], lambda x, y: x.value == y.value)
>>> res = sequence_equal(reactivex.return_value(42))
>>> res = sequence_equal(
reactivex.return_value({ "value": 42 }), lambda x, y: x.value == y.value)
Args:
second: Second observable sequence or iterable to compare.
comparer: [Optional] Comparer used to compare elements of both
sequences. No guarantees on order of comparer arguments.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains a single element
which indicates whether both sequences are of equal length and
their corresponding elements are equal according to the
specified equality comparer.
"""
from ._sequenceequal import sequence_equal_
return sequence_equal_(second, comparer)
def share() -> Callable[[Observable[_T]], Observable[_T]]:
"""Share a single subscription among multiple observers.
This is an alias for a composed publish() and ref_count().
Returns:
An operator function that takes an observable source and
returns a new Observable that multicasts (shares) the original
Observable. As long as there is at least one Subscriber this
Observable will be subscribed and emitting data. When all
subscribers have unsubscribed it will unsubscribe from the
source
Observable.
"""
from ._publish import share_
return share_()
def single(
predicate: Optional[Predicate[_T]] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""The single operator.
Returns the only element of an observable sequence that satisfies
the condition in the optional predicate, and reports an exception
if there is not exactly one element in the observable sequence.
.. marble::
:alt: single
----1--2--3--4-----|
[ single(3) ]
----------3--------|
Example:
>>> res = single()
>>> res = single(lambda x: x == 42)
Args:
predicate: [Optional] A predicate function to evaluate for
elements in the source sequence.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing the single element in
the observable sequence that satisfies the condition in the
predicate.
"""
from ._single import single_
return single_(predicate)
def single_or_default(
predicate: Optional[Predicate[_T]] = None, default_value: Any = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns the only element of an observable sequence that matches
the predicate, or a default value if no such element exists this
method reports an exception if there is more than one element in
the observable sequence.
.. marble::
:alt: single_or_default
----1--2--3--4--|
[ single(8,42) ]
----------------42-|
Examples:
>>> res = single_or_default()
>>> res = single_or_default(lambda x: x == 42)
>>> res = single_or_default(lambda x: x == 42, 0)
>>> res = single_or_default(None, 0)
Args:
predicate: [Optional] A predicate function to evaluate for
elements in the source sequence.
default_value: [Optional] The default value if the index is
outside the bounds of the source sequence.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing the single element in
the observable sequence that satisfies the condition in the
predicate, or a default value if no such element exists.
"""
from ._singleordefault import single_or_default_
return single_or_default_(predicate, default_value)
def single_or_default_async(
has_default: bool = False, default_value: _T = None
) -> Callable[[Observable[_T]], Observable[_T]]:
from ._singleordefault import single_or_default_async_
return single_or_default_async_(has_default, default_value)
def skip(count: int) -> Callable[[Observable[_T]], Observable[_T]]:
"""The skip operator.
Bypasses a specified number of elements in an observable sequence
and then returns the remaining elements.
.. marble::
:alt: skip
----1--2--3--4-----|
[ skip(2) ]
----------3--4-----|
Args:
count: The number of elements to skip before returning the
remaining elements.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains the elements that
occur after the specified index in the input sequence.
"""
from ._skip import skip_
return skip_(count)
def skip_last(count: int) -> Callable[[Observable[_T]], Observable[_T]]:
"""The skip_last operator.
.. marble::
:alt: skip_last
----1--2--3--4-----|
[ skip_last(1) ]
-------1--2--3-----|
Bypasses a specified number of elements at the end of an observable
sequence.
This operator accumulates a queue with a length enough to store the
first `count` elements. As more elements are received, elements are
taken from the front of the queue and produced on the result
sequence. This causes elements to be delayed.
Args:
count: Number of elements to bypass at the end of the source
sequence.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing the source sequence
elements except for the bypassed ones at the end.
"""
from ._skiplast import skip_last_
return skip_last_(count)
def skip_last_with_time(
duration: typing.RelativeTime, scheduler: Optional[abc.SchedulerBase] = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Skips elements for the specified duration from the end of the
observable source sequence.
Example:
>>> res = skip_last_with_time(5.0)
This operator accumulates a queue with a length enough to store
elements received during the initial duration window. As more
elements are received, elements older than the specified duration
are taken from the queue and produced on the result sequence. This
causes elements to be delayed with duration.
Args:
duration: Duration for skipping elements from the end of the
sequence.
scheduler: Scheduler to use for time handling.
Returns:
An observable sequence with the elements skipped during the
specified duration from the end of the source sequence.
"""
from ._skiplastwithtime import skip_last_with_time_
return skip_last_with_time_(duration, scheduler=scheduler)
def skip_until(
other: Union[Observable[Any], "Future[Any]"]
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns the values from the source observable sequence only
after the other observable sequence produces a value.
.. marble::
:alt: skip_until
----1--2--3--4-----|
---------1---------|
[ skip_until() ]
----------3--4-----|
Args:
other: The observable sequence that triggers propagation of
elements of the source sequence.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing the elements of the
source sequence starting from the point the other sequence
triggered propagation.
"""
from ._skipuntil import skip_until_
return skip_until_(other)
def skip_until_with_time(
start_time: typing.AbsoluteOrRelativeTime,
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Skips elements from the observable source sequence until the
specified start time.
Errors produced by the source sequence are always forwarded to the
result sequence, even if the error occurs before the start time.
.. marble::
:alt: skip_until
------1--2--3--4-------|
[skip_until_with_time()]
------------3--4-------|
Examples:
>>> res = skip_until_with_time(datetime())
>>> res = skip_until_with_time(5.0)
Args:
start_time: Time to start taking elements from the source
sequence. If this value is less than or equal to
`datetime.utcnow()`, no elements will be skipped.
Returns:
An operator function that takes an observable source and
returns an observable sequence with the elements skipped
until the specified start time.
"""
from ._skipuntilwithtime import skip_until_with_time_
return skip_until_with_time_(start_time, scheduler=scheduler)
def skip_while(
predicate: typing.Predicate[_T],
) -> Callable[[Observable[_T]], Observable[_T]]:
"""The `skip_while` operator.
Bypasses elements in an observable sequence as long as a specified
condition is true and then returns the remaining elements. The
element's index is used in the logic of the predicate function.
.. marble::
:alt: skip_while
----1--2--3--4-----|
[skip_while(i: i<3)]
----------3--4-----|
Example:
>>> skip_while(lambda value: value < 10)
Args:
predicate: A function to test each element for a condition; the
second parameter of the function represents the index of
the source element.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains the elements from
the input sequence starting at the first element in the linear
series that does not pass the test specified by predicate.
"""
from ._skipwhile import skip_while_
return skip_while_(predicate)
def skip_while_indexed(
predicate: typing.PredicateIndexed[_T],
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Bypasses elements in an observable sequence as long as a
specified condition is true and then returns the remaining
elements. The element's index is used in the logic of the predicate
function.
.. marble::
:alt: skip_while_indexed
----1--2--3--4-----|
[skip_while(i: i<3)]
----------3--4-----|
Example:
>>> skip_while(lambda value, index: value < 10 or index < 10)
Args:
predicate: A function to test each element for a condition; the
second parameter of the function represents the index of
the source element.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains the elements from
the input sequence starting at the first element in the linear
series that does not pass the test specified by predicate.
"""
from ._skipwhile import skip_while_indexed_
return skip_while_indexed_(predicate)
def skip_with_time(
duration: typing.RelativeTime, scheduler: Optional[abc.SchedulerBase] = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Skips elements for the specified duration from the start of the
observable source sequence.
.. marble::
:alt: skip_with_time
----1--2--3--4-----|
[ skip_with_time() ]
----------3--4-----|
Args:
>>> res = skip_with_time(5.0)
Specifying a zero value for duration doesn't guarantee no elements
will be dropped from the start of the source sequence. This is a
side-effect of the asynchrony introduced by the scheduler, where
the action that causes callbacks from the source sequence to be
forwarded may not execute immediately, despite the zero due time.
Errors produced by the source sequence are always forwarded to the
result sequence, even if the error occurs before the duration.
Args:
duration: Duration for skipping elements from the start of the
sequence.
Returns:
An operator function that takes an observable source and
returns an observable sequence with the elements skipped during
the specified duration from the start of the source sequence.
"""
from ._skipwithtime import skip_with_time_
return skip_with_time_(duration, scheduler=scheduler)
def slice(
start: Optional[int] = None, stop: Optional[int] = None, step: Optional[int] = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""The slice operator.
Slices the given observable. It is basically a wrapper around the operators
:func:`skip <reactivex.operators.skip>`,
:func:`skip_last <reactivex.operators.skip_last>`,
:func:`take <reactivex.operators.take>`,
:func:`take_last <reactivex.operators.take_last>` and
:func:`filter <reactivex.operators.filter>`.
.. marble::
:alt: slice
----1--2--3--4-----|
[ slice(1, 2) ]
-------2--3--------|
Examples:
>>> result = source.slice(1, 10)
>>> result = source.slice(1, -2)
>>> result = source.slice(1, -1, 2)
Args:
start: First element to take of skip last
stop: Last element to take of skip last
step: Takes every step element. Must be larger than zero
Returns:
An operator function that takes an observable source and
returns a sliced observable sequence.
"""
from ._slice import slice_
return slice_(start, stop, step)
def some(
predicate: Optional[Predicate[_T]] = None,
) -> Callable[[Observable[_T]], Observable[bool]]:
"""The some operator.
Determines whether some element of an observable sequence
satisfies a condition if present, else if some items are in the
sequence.
.. marble::
:alt: some
----1--2--3--4-----|
[ some(i: i>3) ]
-------------True--|
Examples:
>>> result = source.some()
>>> result = source.some(lambda x: x > 3)
Args:
predicate: A function to test each element for a condition.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing a single element
determining whether some elements in the source sequence
pass the test in the specified predicate if given, else if some
items are in the sequence.
"""
from ._some import some_
return some_(predicate)
@overload
def starmap(
mapper: Callable[[_A, _B], _T]
) -> Callable[[Observable[Tuple[_A, _B]]], Observable[_T]]:
...
@overload
def starmap(
mapper: Callable[[_A, _B, _C], _T]
) -> Callable[[Observable[Tuple[_A, _B, _C]]], Observable[_T]]:
...
@overload
def starmap(
mapper: Callable[[_A, _B, _C, _D], _T]
) -> Callable[[Observable[Tuple[_A, _B, _C, _D]]], Observable[_T]]:
...
def starmap(
mapper: Optional[Callable[..., Any]] = None
) -> Callable[[Observable[Any]], Observable[Any]]:
"""The starmap operator.
Unpack arguments grouped as tuple elements of an observable
sequence and return an observable sequence of values by invoking
the mapper function with star applied unpacked elements as
positional arguments.
Use instead of `map()` when the the arguments to the mapper is
grouped as tuples and the mapper function takes multiple arguments.
.. marble::
:alt: starmap
-----1,2---3,4-----|
[ starmap(add) ]
-----3-----7-------|
Example:
>>> starmap(lambda x, y: x + y)
Args:
mapper: A transform function to invoke with unpacked elements
as arguments.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing the results of
invoking the mapper function with unpacked elements of the
source.
"""
if mapper is None:
return compose(identity)
def starred(values: Tuple[Any, ...]) -> Any:
assert mapper # mypy is paranoid
return mapper(*values)
return compose(map(starred))
@overload
def starmap_indexed(
mapper: Callable[[_A, int], _T]
) -> Callable[[Observable[_A]], Observable[_T]]:
...
@overload
def starmap_indexed(
mapper: Callable[[_A, _B, int], _T]
) -> Callable[[Observable[Tuple[_A, _B]]], Observable[_T]]:
...
@overload
def starmap_indexed(
mapper: Callable[[_A, _B, _C, int], _T]
) -> Callable[[Observable[Tuple[_A, _B, _C]]], Observable[_T]]:
...
@overload
def starmap_indexed(
mapper: Callable[[_A, _B, _C, _D, int], _T]
) -> Callable[[Observable[Tuple[_A, _B, _C, _D]]], Observable[_T]]:
...
def starmap_indexed(
mapper: Optional[Callable[..., Any]] = None
) -> Callable[[Observable[Any]], Observable[Any]]:
"""Variant of :func:`starmap` which accepts an indexed mapper.
.. marble::
:alt: starmap_indexed
---------1,2---3,4---------|
[ starmap_indexed(sum) ]
---------3-----8-----------|
Example:
>>> starmap_indexed(lambda x, y, i: x + y + i)
Args:
mapper: A transform function to invoke with unpacked elements
as arguments.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing the results of
invoking the indexed mapper function with unpacked elements
of the source.
"""
from ._map import map_
if mapper is None:
return compose(identity)
def starred(values: Tuple[Any, ...]) -> Any:
assert mapper # mypy is paranoid
return mapper(*values)
return compose(map_(starred))
def start_with(*args: _T) -> Callable[[Observable[_T]], Observable[_T]]:
"""Prepends a sequence of values to an observable sequence.
.. marble::
:alt: start_with
-----1--2--3--4----|
[ start_with(7,8) ]
-7-8-1--2--3--4----|
Example:
>>> start_with(1, 2, 3)
Returns:
An operator function that takes a source observable and returns
the source sequence prepended with the specified values.
"""
from ._startswith import start_with_
return start_with_(*args)
def subscribe_on(
scheduler: abc.SchedulerBase,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Subscribe on the specified scheduler.
Wrap the source sequence in order to run its subscription and
unsubscription logic on the specified scheduler. This operation is
not commonly used; see the remarks section for more information on
the distinction between subscribe_on and observe_on.
This only performs the side-effects of subscription and
unsubscription on the specified scheduler. In order to invoke
observer callbacks on a scheduler, use observe_on.
Args:
scheduler: Scheduler to perform subscription and unsubscription
actions on.
Returns:
An operator function that takes an observable source and
returns the source sequence whose subscriptions and
un-subscriptions happen on the specified scheduler.
"""
from ._subscribeon import subscribe_on_
return subscribe_on_(scheduler)
@overload
def sum() -> Callable[[Observable[float]], Observable[float]]:
...
@overload
def sum(key_mapper: Mapper[_T, float]) -> Callable[[Observable[_T]], Observable[float]]:
...
def sum(
key_mapper: Optional[Mapper[Any, float]] = None
) -> Callable[[Observable[Any]], Observable[float]]:
"""Computes the sum of a sequence of values that are obtained by
invoking an optional transform function on each element of the
input sequence, else if not specified computes the sum on each item
in the sequence.
.. marble::
:alt: sum
-----1--2--3--4-|
[ sum() ]
----------------10-|
Examples:
>>> res = sum()
>>> res = sum(lambda x: x.value)
Args:
key_mapper: [Optional] A transform function to apply to each
element.
Returns:
An operator function that takes a source observable and returns
an observable sequence containing a single element with the sum
of the values in the source sequence.
"""
from ._sum import sum_
return sum_(key_mapper)
def switch_latest() -> Callable[
[Observable[Union[Observable[_T], "Future[_T]"]]], Observable[_T]
]:
"""The switch_latest operator.
Transforms an observable sequence of observable sequences into an
observable sequence producing values only from the most recent
observable sequence.
.. marble::
:alt: switch_latest
-+------+----------|
+--a--b--c-|
+--1--2--3--4--|
[ switch_latest() ]
----1--2---a--b--c-|
Returns:
A partially applied operator function that takes an observable
source and returns the observable sequence that at any point in
time produces the elements of the most recent inner observable
sequence that has been received.
"""
from ._switchlatest import switch_latest_
return switch_latest_()
def take(count: int) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns a specified number of contiguous elements from the start
of an observable sequence.
.. marble::
:alt: take
-----1--2--3--4----|
[ take(2) ]
-----1--2-|
Example:
>>> op = take(5)
Args:
count: The number of elements to return.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains the specified
number of elements from the start of the input sequence.
"""
from ._take import take_
return take_(count)
def take_last(count: int) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns a specified number of contiguous elements from the end
of an observable sequence.
.. marble::
:alt: take_last
-1--2--3--4-|
[ take_last(2) ]
------------3--4-|
Example:
>>> res = take_last(5)
This operator accumulates a buffer with a length enough to store
elements count elements. Upon completion of the source sequence,
this buffer is drained on the result sequence. This causes the
elements to be delayed.
Args:
count: Number of elements to take from the end of the source
sequence.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing the specified number
of elements from the end of the source sequence.
"""
from ._takelast import take_last_
return take_last_(count)
def take_last_buffer(count: int) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""The `take_last_buffer` operator.
Returns an array with the specified number of contiguous elements
from the end of an observable sequence.
.. marble::
:alt: take_last_buffer
-----1--2--3--4-|
[take_last_buffer(2)]
----------------3,4-|
Example:
>>> res = source.take_last(5)
This operator accumulates a buffer with a length enough to store
elements count elements. Upon completion of the source sequence,
this buffer is drained on the result sequence. This causes the
elements to be delayed.
Args:
count: Number of elements to take from the end of the source
sequence.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing a single list with
the specified number of elements from the end of the source
sequence.
"""
from ._takelastbuffer import take_last_buffer_
return take_last_buffer_(count)
def take_last_with_time(
duration: typing.RelativeTime, scheduler: Optional[abc.SchedulerBase] = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns elements within the specified duration from the end of
the observable source sequence.
.. marble::
:alt: take_last_with_time
-----1--2--3--4-|
[take_last_with_time(3)]
----------------4-|
Example:
>>> res = take_last_with_time(5.0)
This operator accumulates a queue with a length enough to store
elements received during the initial duration window. As more
elements are received, elements older than the specified duration
are taken from the queue and produced on the result sequence. This
causes elements to be delayed with duration.
Args:
duration: Duration for taking elements from the end of the
sequence.
Returns:
An operator function that takes an observable source and
returns an observable sequence with the elements taken
during the specified duration from the end of the source
sequence.
"""
from ._takelastwithtime import take_last_with_time_
return take_last_with_time_(duration, scheduler=scheduler)
def take_until(other: Observable[Any]) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns the values from the source observable sequence until the
other observable sequence produces a value.
.. marble::
:alt: take_until
-----1--2--3--4----|
-------------a-|
[ take_until(2) ]
-----1--2--3-------|
Args:
other: Observable sequence that terminates propagation of
elements of the source sequence.
Returns:
An operator function that takes an observable source and
returns as observable sequence containing the elements of the
source sequence up to the point the other sequence interrupted
further propagation.
"""
from ._takeuntil import take_until_
return take_until_(other)
def take_until_with_time(
end_time: typing.AbsoluteOrRelativeTime,
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Takes elements for the specified duration until the specified
end time, using the specified scheduler to run timers.
.. marble::
:alt: take_until_with_time
-----1--2--3--4--------|
[take_until_with_time()]
-----1--2--3-----------|
Examples:
>>> res = take_until_with_time(dt, [optional scheduler])
>>> res = take_until_with_time(5.0, [optional scheduler])
Args:
end_time: Time to stop taking elements from the source
sequence. If this value is less than or equal to
`datetime.utcnow()`, the result stream will complete
immediately.
scheduler: Scheduler to run the timer on.
Returns:
An operator function that takes an observable source and
returns an observable sequence with the elements taken until
the specified end time.
"""
from ._takeuntilwithtime import take_until_with_time_
return take_until_with_time_(end_time, scheduler=scheduler)
def take_while(
predicate: Predicate[_T], inclusive: bool = False
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns elements from an observable sequence as long as a
specified condition is true.
.. marble::
:alt: take_while
-----1--2--3--4----|
[take_while(i: i<3)]
-----1--2----------|
Example:
>>> take_while(lambda value: value < 10)
Args:
predicate: A function to test each element for a condition.
inclusive: [Optional] When set to True the value that caused
the predicate function to return False will also be emitted.
If not specified, defaults to False.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains the elements from
the input sequence that occur before the element at which the
test no longer passes.
"""
from ._takewhile import take_while_
return take_while_(predicate, inclusive)
def take_while_indexed(
predicate: PredicateIndexed[_T], inclusive: bool = False
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns elements from an observable sequence as long as a
specified condition is true. The element's index is used in the
logic of the predicate function.
.. marble::
:alt: take_while_indexed
--------1------2------3------4-------|
[take_while_indexed(v, i: v<4 or i<3)]
--------1------2---------------------|
Example:
>>> take_while_indexed(lambda value, index: value < 10 or index < 10)
Args:
predicate: A function to test each element for a condition; the
second parameter of the function represents the index of the
source element.
inclusive: [Optional] When set to True the value that caused
the predicate function to return False will also be emitted.
If not specified, defaults to False.
Returns:
An observable sequence that contains the elements from the
input sequence that occur before the element at which the test no
longer passes.
"""
from ._takewhile import take_while_indexed_
return take_while_indexed_(predicate, inclusive)
def take_with_time(
duration: typing.RelativeTime, scheduler: Optional[abc.SchedulerBase] = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Takes elements for the specified duration from the start of the
observable source sequence.
.. marble::
:alt: take_with_time
-----1--2--3--4----|
[ take_with_time() ]
-----1--2----------|
Example:
>>> res = take_with_time(5.0)
This operator accumulates a queue with a length enough to store
elements received during the initial duration window. As more
elements are received, elements older than the specified duration
are taken from the queue and produced on the result sequence. This
causes elements to be delayed with duration.
Args:
duration: Duration for taking elements from the start of the
sequence.
Returns:
An operator function that takes an observable source and
returns an observable sequence with the elements taken during
the specified duration from the start of the source sequence.
"""
from ._takewithtime import take_with_time_
return take_with_time_(duration, scheduler=scheduler)
def throttle_first(
window_duration: typing.RelativeTime, scheduler: Optional[abc.SchedulerBase] = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns an Observable that emits only the first item emitted by
the source Observable during sequential time windows of a specified
duration.
Args:
window_duration: time to wait before emitting another item
after emitting the last item.
Returns:
An operator function that takes an observable source and
returns an observable that performs the throttle operation.
"""
from ._throttlefirst import throttle_first_
return throttle_first_(window_duration, scheduler)
def throttle_with_mapper(
throttle_duration_mapper: Callable[[Any], Observable[Any]]
) -> Callable[[Observable[_T]], Observable[_T]]:
"""The throttle_with_mapper operator.
Ignores values from an observable sequence which are followed by
another value within a computed throttle duration.
Example:
>>> op = throttle_with_mapper(lambda x: rx.Scheduler.timer(x+x))
Args:
throttle_duration_mapper: Mapper function to retrieve an
observable sequence indicating the throttle duration for each
given element.
Returns:
A partially applied operator function that takes an observable
source and returns the throttled observable sequence.
"""
from ._debounce import throttle_with_mapper_
return throttle_with_mapper_(throttle_duration_mapper)
if TYPE_CHECKING:
from ._timestamp import Timestamp
def timestamp(
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable["Timestamp[_T]"]]:
"""The timestamp operator.
Records the timestamp for each value in an observable sequence.
Examples:
>>> timestamp()
Produces objects with attributes `value` and `timestamp`, where
value is the original value.
Returns:
A partially applied operator function that takes an observable
source and returns an observable sequence with timestamp
information on values.
"""
from ._timestamp import timestamp_
return timestamp_(scheduler=scheduler)
def timeout(
duetime: typing.AbsoluteOrRelativeTime,
other: Optional[Observable[_T]] = None,
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns the source observable sequence or the other observable
sequence if duetime elapses.
.. marble::
:alt: timeout
-1--2--------3--4--|
o-6--7-|
[ timeout(3,o) ]
-1--2---6--7----------|
Examples:
>>> res = timeout(5.0)
>>> res = timeout(datetime(), return_value(42))
>>> res = timeout(5.0, return_value(42))
Args:
duetime: Absolute (specified as a datetime object) or relative time
(specified as a float denoting seconds or an instance of timedetla)
when a timeout occurs.
other: Sequence to return in case of a timeout. If not
specified, a timeout error throwing sequence will be used.
scheduler:
Returns:
An operator function that takes and observable source and
returns the source sequence switching to the other sequence in
case of a timeout.
"""
from ._timeout import timeout_
return timeout_(duetime, other, scheduler)
def timeout_with_mapper(
first_timeout: Optional[Observable[Any]] = None,
timeout_duration_mapper: Optional[Callable[[_T], Observable[Any]]] = None,
other: Optional[Observable[_T]] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns the source observable sequence, switching to the other
observable sequence if a timeout is signaled.
Examples:
>>> res = timeout_with_mapper(reactivex.timer(0.5))
>>> res = timeout_with_mapper(
reactivex.timer(0.5), lambda x: reactivex.timer(0.2)
)
>>> res = timeout_with_mapper(
reactivex.timer(0.5),
lambda x: reactivex.timer(0.2),
reactivex.return_value(42)
)
Args:
first_timeout: [Optional] Observable sequence that represents
the timeout for the first element. If not provided, this
defaults to reactivex.never().
timeout_duration_mapper: [Optional] Selector to retrieve an
observable sequence that represents the timeout between the
current element and the next element.
other: [Optional] Sequence to return in case of a timeout. If
not provided, this is set to reactivex.throw().
Returns:
An operator function that takes an observable source and
returns the source sequence switching to the other sequence in
case of a timeout.
"""
from ._timeoutwithmapper import timeout_with_mapper_
return timeout_with_mapper_(first_timeout, timeout_duration_mapper, other)
if TYPE_CHECKING:
from ._timeinterval import TimeInterval
def time_interval(
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable["TimeInterval[_T]"]]:
"""Records the time interval between consecutive values in an
observable sequence.
.. marble::
:alt: time_interval
--1--2-----3---4--|
[ time_interval() ]
-----2-----5---5---|
Examples:
>>> res = time_interval()
Return:
An operator function that takes an observable source and
returns an observable sequence with time interval information
on values.
"""
from ._timeinterval import time_interval_
return time_interval_(scheduler=scheduler)
def to_dict(
key_mapper: Mapper[_T, _TKey], element_mapper: Optional[Mapper[_T, _TValue]] = None
) -> Callable[[Observable[_T]], Observable[Dict[_TKey, _TValue]]]:
"""Converts the observable sequence to a Map if it exists.
Args:
key_mapper: A function which produces the key for the
dictionary.
element_mapper: [Optional] An optional function which produces
the element for the dictionary. If not present, defaults to
the value from the observable sequence.
Returns:
An operator function that takes an observable source and
returns an observable sequence with a single value of a
dictionary containing the values from the observable sequence.
"""
from ._todict import to_dict_
return to_dict_(key_mapper, element_mapper)
def to_future(
future_ctor: Optional[Callable[[], "Future[_T]"]] = None
) -> Callable[[Observable[_T]], "Future[_T]"]:
"""Converts an existing observable sequence to a Future.
Example:
op = to_future(asyncio.Future);
Args:
future_ctor: [Optional] The constructor of the future.
Returns:
An operator function that takes an observable source and returns
a future with the last value from the observable sequence.
"""
from ._tofuture import to_future_
return to_future_(future_ctor)
def to_iterable() -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""Creates an iterable from an observable sequence.
There is also an alias called ``to_list``.
Returns:
An operator function that takes an obserable source and
returns an observable sequence containing a single element with
an iterable containing all the elements of the source sequence.
"""
from ._toiterable import to_iterable_
return to_iterable_()
to_list = to_iterable
def to_marbles(
timespan: typing.RelativeTime = 0.1, scheduler: Optional[abc.SchedulerBase] = None
) -> Callable[[Observable[Any]], Observable[str]]:
"""Convert an observable sequence into a marble diagram string.
Args:
timespan: [Optional] duration of each character in second.
If not specified, defaults to 0.1s.
scheduler: [Optional] The scheduler used to run the the input
sequence on.
Returns:
Observable stream.
"""
from ._tomarbles import to_marbles
return to_marbles(scheduler=scheduler, timespan=timespan)
def to_set() -> Callable[[Observable[_T]], Observable[Set[_T]]]:
"""Converts the observable sequence to a set.
Returns:
An operator function that takes an observable source and
returns an observable sequence with a single value of a set
containing the values from the observable sequence.
"""
from ._toset import to_set_
return to_set_()
def while_do(
condition: Predicate[Observable[_T]],
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Repeats source as long as condition holds emulating a while
loop.
Args:
condition: The condition which determines if the source will be
repeated.
Returns:
An operator function that takes an observable source and
returns an observable sequence which is repeated as long as the
condition holds.
"""
from ._whiledo import while_do_
return while_do_(condition)
def window(
boundaries: Observable[Any],
) -> Callable[[Observable[_T]], Observable[Observable[_T]]]:
"""Projects each element of an observable sequence into zero or
more windows.
.. marble::
:alt: window
---a-----b-----c--------|
----1--2--3--4--5--6--7-|
[ window(open) ]
+--+-----+-----+--------|
+5--6--7-|
+3--4-|
+1--2-|
+--|
Examples:
>>> res = window(reactivex.interval(1.0))
Args:
boundaries: Observable sequence whose elements denote the
creation and completion of non-overlapping windows.
Returns:
An operator function that takes an observable source and
returns an observable sequence of windows.
"""
from ._window import window_
return window_(boundaries)
def window_when(
closing_mapper: Callable[[], Observable[Any]]
) -> Callable[[Observable[_T]], Observable[Observable[_T]]]:
"""Projects each element of an observable sequence into zero or
more windows.
.. marble::
:alt: window
------c|
------c|
------c|
----1--2--3--4--5-|
[ window(close) ]
+-----+-----+-----+|
+4--5-|
+2--3-|
+----1|
Examples:
>>> res = window(lambda: reactivex.timer(0.5))
Args:
closing_mapper: A function invoked to define
the closing of each produced window. It defines the
boundaries of the produced windows (a window is started
when the previous one is closed, resulting in
non-overlapping windows).
Returns:
An operator function that takes an observable source and
returns an observable sequence of windows.
"""
from ._window import window_when_
return window_when_(closing_mapper)
def window_toggle(
openings: Observable[Any], closing_mapper: Callable[[Any], Observable[Any]]
) -> Callable[[Observable[_T]], Observable[Observable[_T]]]:
"""Projects each element of an observable sequence into zero or
more windows.
.. marble::
:alt: window
---a-----------b------------|
---d--|
--------e-|
----1--2--3--4--5--6--7--8--|
[ window(open, close) ]
---+-----------+------------|
+5--6--7|
+1-|
>>> res = window(reactivex.interval(0.5), lambda i: reactivex.timer(i))
Args:
openings: Observable sequence whose elements denote the
creation of windows.
closing_mapper: A function invoked to define the closing of each
produced window. Value from openings Observable that initiated
the associated window is provided as argument to the function.
Returns:
An operator function that takes an observable source and
returns an observable sequence of windows.
"""
from ._window import window_toggle_
return window_toggle_(openings, closing_mapper)
def window_with_count(
count: int, skip: Optional[int] = None
) -> Callable[[Observable[_T]], Observable[Observable[_T]]]:
"""Projects each element of an observable sequence into zero or more
windows which are produced based on element count information.
.. marble::
:alt: window_with_count
---1-2-3---4-5-6--->
[ window(3) ]
--+-------+-------->
+4-5-6-|
+1-2-3-|
Examples:
>>> window_with_count(10)
>>> window_with_count(10, 1)
Args:
count: Length of each window.
skip: [Optional] Number of elements to skip between creation of
consecutive windows. If not specified, defaults to the
count.
Returns:
An observable sequence of windows.
"""
from ._windowwithcount import window_with_count_
return window_with_count_(count, skip)
def window_with_time(
timespan: typing.RelativeTime,
timeshift: Optional[typing.RelativeTime] = None,
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable[Observable[_T]]]:
from ._windowwithtime import window_with_time_
return window_with_time_(timespan, timeshift, scheduler)
def window_with_time_or_count(
timespan: typing.RelativeTime,
count: int,
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable[Observable[_T]]]:
from ._windowwithtimeorcount import window_with_time_or_count_
return window_with_time_or_count_(timespan, count, scheduler)
def with_latest_from(
*sources: Observable[Any],
) -> Callable[[Observable[Any]], Observable[Any]]:
"""The `with_latest_from` operator.
Merges the specified observable sequences into one observable
sequence by creating a tuple only when the first
observable sequence produces an element. The observables can be
passed either as separate arguments or as a list.
.. marble::
:alt: with_latest_from
---1---2---3----4-|
--a-----b----c-d----|
[with_latest_from() ]
---1,a-2,a-3,b--4,d-|
Examples:
>>> op = with_latest_from(obs1)
>>> op = with_latest_from([obs1, obs2, obs3])
Returns:
An operator function that takes an observable source and
returns an observable sequence containing the result of
combining elements of the sources into a tuple.
"""
from ._withlatestfrom import with_latest_from_
return with_latest_from_(*sources)
def zip(*args: Observable[Any]) -> Callable[[Observable[Any]], Observable[Any]]:
"""Merges the specified observable sequences into one observable
sequence by creating a tuple whenever all of the
observable sequences have produced an element at a corresponding
index.
.. marble::
:alt: zip
--1--2---3-----4---|
-a----b----c-d------|
[ zip() ]
--1,a-2,b--3,c-4,d-|
Example:
>>> res = zip(obs1, obs2)
Args:
args: Observable sources to zip.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing the result of
combining elements of the sources as a tuple.
"""
from ._zip import zip_
return zip_(*args)
def zip_with_iterable(
second: Iterable[_T2],
) -> Callable[[Observable[_T1]], Observable[Tuple[_T1, _T2]]]:
"""Merges the specified observable sequence and list into one
observable sequence by creating a tuple whenever all of
the observable sequences have produced an element at a
corresponding index.
.. marble::
:alt: zip_with_iterable
--1---2----3---4---|
[ zip(a,b,c,b) ]
--1,a-2,b--3,c-4,d-|
Example
>>> res = zip([1,2,3])
Args:
second: Iterable to zip with the source observable..
Returns:
An operator function that takes and observable source and
returns an observable sequence containing the result of
combining elements of the sources as a tuple.
"""
from ._zip import zip_with_iterable_
return zip_with_iterable_(second)
zip_with_list = zip_with_iterable
__all__ = [
"all",
"amb",
"as_observable",
"average",
"buffer",
"buffer_when",
"buffer_toggle",
"buffer_with_count",
"buffer_with_time",
"buffer_with_time_or_count",
"catch",
"combine_latest",
"concat",
"contains",
"count",
"debounce",
"throttle_with_timeout",
"default_if_empty",
"delay_subscription",
"delay_with_mapper",
"dematerialize",
"delay",
"distinct",
"distinct_until_changed",
"do",
"do_action",
"do_while",
"element_at",
"element_at_or_default",
"exclusive",
"expand",
"filter",
"filter_indexed",
"finally_action",
"find",
"find_index",
"first",
"first_or_default",
"flat_map",
"flat_map_indexed",
"flat_map_latest",
"fork_join",
"group_by",
"group_by_until",
"group_join",
"ignore_elements",
"is_empty",
"join",
"last",
"last_or_default",
"map",
"map_indexed",
"materialize",
"max",
"max_by",
"merge",
"merge_all",
"min",
"min_by",
"multicast",
"observe_on",
"on_error_resume_next",
"pairwise",
"partition",
"partition_indexed",
"pluck",
"pluck_attr",
"publish",
"publish_value",
"reduce",
"ref_count",
"repeat",
"replay",
"retry",
"sample",
"scan",
"sequence_equal",
"share",
"single",
"single_or_default",
"single_or_default_async",
"skip",
"skip_last",
"skip_last_with_time",
"skip_until",
"skip_until_with_time",
"skip_while",
"skip_while_indexed",
"skip_with_time",
"slice",
"some",
"starmap",
"starmap_indexed",
"start_with",
"subscribe_on",
"sum",
"switch_latest",
"take",
"take_last",
"take_last_buffer",
"take_last_with_time",
"take_until",
"take_until_with_time",
"take_while",
"take_while_indexed",
"take_with_time",
"throttle_first",
"throttle_with_mapper",
"timestamp",
"timeout",
"timeout_with_mapper",
"time_interval",
"to_dict",
"to_future",
"to_iterable",
"to_list",
"to_marbles",
"to_set",
"while_do",
"window",
"window_when",
"window_toggle",
"window_with_count",
"window_with_time",
"window_with_time_or_count",
"with_latest_from",
"zip",
"zip_with_list",
"zip_with_iterable",
"zip_with_list",
]
|