File: parameterized.py

package info (click to toggle)
python-param 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,048 kB
  • sloc: python: 17,980; makefile: 3
file content (4575 lines) | stat: -rw-r--r-- 179,225 bytes parent folder | download
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
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
"""
Generic support for objects with full-featured Parameters and
messaging.

This file comes from the Param library (https://github.com/holoviz/param)
but can be taken out of the param module and used on its own if desired,
either alone (providing basic Parameter support) or with param's
__init__.py (providing specialized Parameter types).
"""

import asyncio
import copy
import datetime as dt
import html
import inspect
import logging
import numbers
import operator
import random
import re
import types
import typing
import warnings

# Allow this file to be used standalone if desired, albeit without JSON serialization
try:
    from . import serializer
except ImportError:
    serializer = None

from collections import defaultdict, namedtuple, OrderedDict
from functools import partial, wraps, reduce
from html import escape
from itertools import chain
from operator import itemgetter, attrgetter
from types import FunctionType, MethodType

from contextlib import contextmanager
from logging import DEBUG, INFO, WARNING, ERROR, CRITICAL

from ._utils import (
    DEFAULT_SIGNATURE,
    ParamDeprecationWarning as _ParamDeprecationWarning,
    ParamFutureWarning as _ParamFutureWarning,
    Skip,
    _deprecated,
    _deprecate_positional_args,
    _dict_update,
    _in_ipython,
    _is_auto_name,
    _is_mutable_container,
    _recursive_repr,
    _to_async_gen,
    _validate_error_prefix,
    accept_arguments,
    iscoroutinefunction,
    descendents,
)

# Ideally setting param_pager would be in __init__.py but param_pager is
# needed on import to create the Parameterized class, so it'd need to precede
# importing parameterized.py in __init__.py which would be a little weird.
if _in_ipython():
    # In case the optional ipython module is unavailable
    try:
        from .ipython import ParamPager, ipython_async_executor as async_executor
        param_pager = ParamPager(metaclass=True)  # Generates param description
    except ImportError:
        from ._utils import async_executor
else:
    from ._utils import async_executor
    param_pager = None


from inspect import getfullargspec

dt_types = (dt.datetime, dt.date)
_int_types = (int,)

try:
    import numpy as np
    dt_types = dt_types + (np.datetime64,)
    _int_types = _int_types + (np.integer,)
except:
    pass

VERBOSE = INFO - 1
logging.addLevelName(VERBOSE, "VERBOSE")

# Get the appropriate logging.Logger instance. If `logger` is None, a
# logger named `"param"` will be instantiated. If `name` is set, a descendant
# logger with the name ``"param.<name>"`` is returned (or
# ``logger.name + ".<name>"``)
logger = None
def get_logger(name=None):
    if logger is None:
        root_logger = logging.getLogger('param')
        if not root_logger.handlers:
            root_logger.setLevel(logging.INFO)
            formatter = logging.Formatter(
                fmt='%(levelname)s:%(name)s: %(message)s')
            handler = logging.StreamHandler()
            handler.setFormatter(formatter)
            root_logger.addHandler(handler)
    else:
        root_logger = logger
    if name is None:
        return root_logger
    else:
        return logging.getLogger(root_logger.name + '.' + name)


# Indicates whether warnings should be raised as errors, stopping
# processing.
warnings_as_exceptions = False

docstring_signature = True        # Add signature to class docstrings
docstring_describe_params = True  # Add parameter description to class
                                  # docstrings (requires ipython module)
object_count = 0
warning_count = 0

# Hook to apply to depends and bind arguments to turn them into valid parameters
_reference_transforms = []

def register_reference_transform(transform):
    """
    Appends a transform to extract potential parameter dependencies
    from an object.

    Arguments
    ---------
    transform: Callable[Any, Any]
    """
    return _reference_transforms.append(transform)

def transform_reference(arg):
    """
    Applies transforms to turn objects which should be treated like
    a parameter reference into a valid reference that can be resolved
    by Param. This is useful for adding handling for depending on objects
    that are not simple Parameters or functions with dependency
    definitions.
    """
    for transform in _reference_transforms:
        if isinstance(arg, Parameter) or hasattr(arg, '_dinfo'):
            break
        arg = transform(arg)
    return arg

def eval_function_with_deps(function):
    """Evaluates a function after resolving its dependencies.

    Calls and returns a function after resolving any dependencies
    stored on the _dinfo attribute and passing the resolved values
    as arguments.
    """
    args, kwargs = (), {}
    if hasattr(function, '_dinfo'):
        arg_deps = function._dinfo['dependencies']
        kw_deps = function._dinfo.get('kw', {})
        if kw_deps or any(isinstance(d, Parameter) for d in arg_deps):
            args = (getattr(dep.owner, dep.name) for dep in arg_deps)
            kwargs = {n: getattr(dep.owner, dep.name) for n, dep in kw_deps.items()}
    return function(*args, **kwargs)

def resolve_value(value, recursive=True):
    """
    Resolves the current value of a dynamic reference.
    """
    if not recursive:
        pass
    elif isinstance(value, (list, tuple)):
        return type(value)(resolve_value(v) for v in value)
    elif isinstance(value, dict):
        return type(value)((resolve_value(k), resolve_value(v)) for k, v in value.items())
    elif isinstance(value, slice):
        return slice(
            resolve_value(value.start),
            resolve_value(value.stop),
            resolve_value(value.step)
        )
    value = transform_reference(value)
    is_gen = inspect.isgeneratorfunction(value)
    if hasattr(value, '_dinfo') or iscoroutinefunction(value) or is_gen:
        value = eval_function_with_deps(value)
        if is_gen:
            value = _to_async_gen(value)
    elif isinstance(value, Parameter):
        value = getattr(value.owner, value.name)
    return value

def resolve_ref(reference, recursive=False):
    """
    Resolves all parameters a dynamic reference depends on.
    """
    if recursive:
        if isinstance(reference, (list, tuple, set)):
            return [r for v in reference for r in resolve_ref(v, recursive)]
        elif isinstance(reference, dict):
            return [r for kv in reference.items() for o in kv for r in resolve_ref(o, recursive)]
        elif isinstance(reference, slice):
            return [r for v in (reference.start, reference.stop, reference.step) for r in resolve_ref(v, recursive)]
    reference = transform_reference(reference)
    if hasattr(reference, '_dinfo'):
        dinfo = getattr(reference, '_dinfo', {})
        args = list(dinfo.get('dependencies', []))
        kwargs = list(dinfo.get('kw', {}).values())
        refs = []
        for arg in (args + kwargs):
            if isinstance(arg, str):
                owner = get_method_owner(reference)
                if arg in owner.param:
                    arg = owner.param[arg]
                elif '.' in arg:
                    path = arg.split('.')
                    arg = owner
                    for attr in path[:-1]:
                        arg = getattr(arg, attr)
                    arg = arg.param[path[-1]]
                else:
                    arg = getattr(owner, arg)
            refs.extend(resolve_ref(arg))
        return refs
    elif isinstance(reference, Parameter):
        return [reference]
    return []

def _identity_hook(obj, val):
    """To be removed when set_hook is removed"""
    return val


class _Undefined:
    """
    Dummy value to signal completely undefined values rather than
    simple None values.
    """

    def __bool__(self):
        # Haven't defined whether Undefined is falsy or truthy,
        # so to avoid subtle bugs raise an error when it
        # is used in a comparison without `is`.
        raise RuntimeError('Use `is` to compare Undefined')

    def __repr__(self):
        return '<Undefined>'


Undefined = _Undefined()


@contextmanager
def logging_level(level):
    """
    Temporarily modify param's logging level.
    """
    level = level.upper()
    levels = [DEBUG, INFO, WARNING, ERROR, CRITICAL, VERBOSE]
    level_names = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL', 'VERBOSE']

    if level not in level_names:
        raise Exception(f"Level {level!r} not in {levels!r}")

    param_logger = get_logger()
    logging_level = param_logger.getEffectiveLevel()
    param_logger.setLevel(levels[level_names.index(level)])
    try:
        yield None
    finally:
        param_logger.setLevel(logging_level)


@contextmanager
def _batch_call_watchers(parameterized, enable=True, run=True):
    """
    Internal version of batch_call_watchers, adding control over queueing and running.
    Only actually batches events if enable=True; otherwise a no-op. Only actually
    calls the accumulated watchers on exit if run=True; otherwise they remain queued.
    """
    BATCH_WATCH = parameterized.param._BATCH_WATCH
    parameterized.param._BATCH_WATCH = enable or parameterized.param._BATCH_WATCH
    try:
        yield
    finally:
        parameterized.param._BATCH_WATCH = BATCH_WATCH
        if run and not BATCH_WATCH:
            parameterized.param._batch_call_watchers()


# PARAM3_DEPRECATION
@_deprecated(extra_msg="Use instead `batch_call_watchers`.")
@contextmanager
def batch_watch(parameterized, enable=True, run=True):
    with _batch_call_watchers(parameterized, enable, run):
        yield


@contextmanager
def batch_call_watchers(parameterized):
    """
    Context manager to batch events to provide to Watchers on a
    parameterized object.  This context manager queues any events
    triggered by setting a parameter on the supplied parameterized
    object, saving them up to dispatch them all at once when the
    context manager exits.
    """
    BATCH_WATCH = parameterized.param._BATCH_WATCH
    parameterized.param._BATCH_WATCH = True
    try:
        yield
    finally:
        parameterized.param._BATCH_WATCH = BATCH_WATCH
        if not BATCH_WATCH:
            parameterized.param._batch_call_watchers()


@contextmanager
def _syncing(parameterized, parameters):
    old = parameterized._param__private.syncing
    parameterized._param__private.syncing = set(old) | set(parameters)
    try:
        yield
    finally:
        parameterized._param__private.syncing = old


@contextmanager
def edit_constant(parameterized):
    """
    Temporarily set parameters on Parameterized object to constant=False
    to allow editing them.
    """
    params = parameterized.param.objects('existing').values()
    constants = [p.constant for p in params]
    for p in params:
        p.constant = False
    try:
        yield
    except:
        raise
    finally:
        for (p, const) in zip(params, constants):
            p.constant = const


@contextmanager
def discard_events(parameterized):
    """
    Context manager that discards any events within its scope
    triggered on the supplied parameterized object.
    """
    batch_watch = parameterized.param._BATCH_WATCH
    parameterized.param._BATCH_WATCH = True
    watchers, events = (list(parameterized.param._state_watchers),
                        list(parameterized.param._events))
    try:
        yield
    except:
        raise
    finally:
        parameterized.param._BATCH_WATCH = batch_watch
        parameterized.param._state_watchers = watchers
        parameterized.param._events = events


def classlist(class_):
    """
    Return a list of the class hierarchy above (and including) the given class.

    Same as `inspect.getmro(class_)[::-1]`
    """
    return inspect.getmro(class_)[::-1]


def get_all_slots(class_):
    """
    Return a list of slot names for slots defined in `class_` and its
    superclasses.
    """
    # A subclass's __slots__ attribute does not contain slots defined
    # in its superclass (the superclass' __slots__ end up as
    # attributes of the subclass).
    all_slots = []
    parent_param_classes = [c for c in classlist(class_)[1::]]
    for c in parent_param_classes:
        if hasattr(c,'__slots__'):
            all_slots+=c.__slots__
    return all_slots


def get_occupied_slots(instance):
    """
    Return a list of slots for which values have been set.

    (While a slot might be defined, if a value for that slot hasn't
    been set, then it's an AttributeError to request the slot's
    value.)
    """
    return [slot for slot in get_all_slots(type(instance))
            if hasattr(instance,slot)]


# PARAM3_DEPRECATION
@_deprecated()
def all_equal(arg1,arg2):
    """
    Return a single boolean for arg1==arg2, even for numpy arrays
    using element-wise comparison.

    Uses all(arg1==arg2) for sequences, and arg1==arg2 otherwise.

    If both objects have an '_infinitely_iterable' attribute, they are
    not be zipped together and are compared directly instead.
    """
    if all(hasattr(el, '_infinitely_iterable') for el in [arg1,arg2]):
        return arg1==arg2
    try:
        return all(a1 == a2 for a1, a2 in zip(arg1, arg2))
    except TypeError:
        return arg1==arg2


# PARAM3_DEPRECATION
# The syntax to use a metaclass changed incompatibly between 2 and
# 3. The add_metaclass() class decorator below creates a class using a
# specified metaclass in a way that works on both 2 and 3. For 3, can
# remove this decorator and specify metaclasses in a simpler way
# (https://docs.python.org/3/reference/datamodel.html#customizing-class-creation)
#
# Code from six (https://bitbucket.org/gutworth/six; version 1.4.1).
@_deprecated()
def add_metaclass(metaclass):
    """Class decorator for creating a class with a metaclass.

    .. deprecated:: 2.0.0
    """
    def wrapper(cls):
        orig_vars = cls.__dict__.copy()
        orig_vars.pop('__dict__', None)
        orig_vars.pop('__weakref__', None)
        for slots_var in orig_vars.get('__slots__', ()):
            orig_vars.pop(slots_var)
        return metaclass(cls.__name__, cls.__bases__, orig_vars)
    return wrapper


class bothmethod:
    """
    'optional @classmethod'

    A decorator that allows a method to receive either the class
    object (if called on the class) or the instance object
    (if called on the instance) as its first argument.
    """
    def __init__(self, method):
        self.method = method

    def __get__(self, instance, owner):
        if instance is None:
            # Class call
            return self.method.__get__(owner)
        else:
            # Instance call
            return self.method.__get__(instance, owner)


def _getattrr(obj, attr, *args):
    def _getattr(obj, attr):
        return getattr(obj, attr, *args)
    return reduce(_getattr, [obj] + attr.split('.'))


def no_instance_params(cls):
    """
    Disables instance parameters on the class
    """
    cls._param__private.disable_instance_params = True
    return cls


def _instantiate_param_obj(paramobj, owner=None):
    """Return a Parameter object suitable for instantiation given the class's Parameter object"""

    # Shallow-copy Parameter object without the watchers
    p = copy.copy(paramobj)
    p.owner = owner

    # Reset watchers since class parameter watcher should not execute
    # on instance parameters
    p.watchers = {}

    # shallow-copy any mutable slot values other than the actual default
    for s in p.__class__._all_slots_:
        v = getattr(p, s)
        if _is_mutable_container(v) and s != "default":
            setattr(p, s, copy.copy(v))
    return p


def _instantiated_parameter(parameterized, param):
    """
    Given a Parameterized object and one of its class Parameter objects,
    return the appropriate Parameter object for this instance, instantiating
    it if need be.
    """
    if (getattr(parameterized._param__private, 'initialized', False) and param.per_instance and
        not getattr(type(parameterized)._param__private, 'disable_instance_params', False)):
        key = param.name

        if key not in parameterized._param__private.params:
            parameterized._param__private.params[key] = _instantiate_param_obj(param, parameterized)

        param = parameterized._param__private.params[key]

    return param


def instance_descriptor(f):
    # If parameter has an instance Parameter, delegate setting
    def _f(self, obj, val):
        # obj is None when the metaclass is setting
        if obj is not None:
            instance_param = obj._param__private.params.get(self.name)
            if instance_param is None:
                instance_param = _instantiated_parameter(obj, self)
            if instance_param is not None and self is not instance_param:
                instance_param.__set__(obj, val)
                return
        return f(self, obj, val)
    return _f


def get_method_owner(method):
    """
    Gets the instance that owns the supplied method
    """
    if not inspect.ismethod(method):
        return None
    if isinstance(method, partial):
        method = method.func
    return method.__self__


# PARAM3_DEPRECATION
def recursive_repr(fillvalue='...'):
    """
    Decorator to make a repr function return fillvalue for a recursive call

    .. deprecated:: 1.12.0
    """
    warnings.warn(
        'recursive_repr has been deprecated and will be removed in a future version.',
        category=_ParamDeprecationWarning,
        stacklevel=2,
    )
    return _recursive_repr(fillvalue=fillvalue)


@accept_arguments
def output(func, *output, **kw):
    """
    output allows annotating a method on a Parameterized class to
    declare that it returns an output of a specific type. The outputs
    of a Parameterized class can be queried using the
    Parameterized.param.outputs method. By default the output will
    inherit the method name but a custom name can be declared by
    expressing the Parameter type using a keyword argument.

    The simplest declaration simply declares the method returns an
    object without any type guarantees, e.g.:

      @output()

    If a specific parameter type is specified this is a declaration
    that the method will return a value of that type, e.g.:

      @output(param.Number())

    To override the default name of the output the type may be declared
    as a keyword argument, e.g.:

      @output(custom_name=param.Number())

    Multiple outputs may be declared using keywords mapping from output name
    to the type or using tuples of the same format, i.e. these two declarations
    are equivalent:

      @output(number=param.Number(), string=param.String())

      @output(('number', param.Number()), ('string', param.String()))

    output also accepts Python object types which will be upgraded to
    a ClassSelector, e.g.:

      @output(int)
    """
    if output:
        outputs = []
        for i, out in enumerate(output):
            i = i if len(output) > 1 else None
            if isinstance(out, tuple) and len(out) == 2 and isinstance(out[0], str):
                outputs.append(out+(i,))
            elif isinstance(out, str):
                outputs.append((out, Parameter(), i))
            else:
                outputs.append((None, out, i))
    elif kw:
        # (requires keywords to be kept ordered, which was not true in previous versions)
        outputs = [(name, otype, i if len(kw) > 1 else None)
                   for i, (name, otype) in enumerate(kw.items())]
    else:
        outputs = [(None, Parameter(), None)]

    names, processed = [], []
    for name, otype, i in outputs:
        if isinstance(otype, type):
            if issubclass(otype, Parameter):
                otype = otype()
            else:
                from .import ClassSelector
                otype = ClassSelector(class_=otype)
        elif isinstance(otype, tuple) and all(isinstance(t, type) for t in otype):
            from .import ClassSelector
            otype = ClassSelector(class_=otype)
        if not isinstance(otype, Parameter):
            raise ValueError('output type must be declared with a Parameter class, '
                             'instance or a Python object type.')
        processed.append((name, otype, i))
        names.append(name)

    if len(set(names)) != len(names):
        raise ValueError('When declaring multiple outputs each value '
                         'must be unique.')

    _dinfo = getattr(func, '_dinfo', {})
    _dinfo.update({'outputs': processed})

    @wraps(func)
    def _output(*args,**kw):
        return func(*args,**kw)

    _output._dinfo = _dinfo

    return _output


def _parse_dependency_spec(spec):
    """
    Parses param.depends specifications into three components:

    1. The dotted path to the sub-object
    2. The attribute being depended on, i.e. either a parameter or method
    3. The parameter attribute being depended on
    """
    assert spec.count(":")<=1
    spec = spec.strip()
    m = re.match("(?P<path>[^:]*):?(?P<what>.*)", spec)
    what = m.group('what')
    path = "."+m.group('path')
    m = re.match(r"(?P<obj>.*)(\.)(?P<attr>.*)", path)
    obj = m.group('obj')
    attr = m.group("attr")
    return obj or None, attr, what or 'value'


def _params_depended_on(minfo, dynamic=True, intermediate=True):
    """
    Resolves dependencies declared on a Parameterized method.
    Dynamic dependencies, i.e. dependencies on sub-objects which may
    or may not yet be available, are only resolved if dynamic=True.
    By default intermediate dependencies, i.e. dependencies on the
    path to a sub-object are returned. For example for a dependency
    on 'a.b.c' dependencies on 'a' and 'b' are returned as long as
    intermediate=True.

    Returns lists of concrete dependencies on available parameters
    and dynamic dependencies specifications which have to resolved
    if the referenced sub-objects are defined.
    """
    deps, dynamic_deps = [], []
    dinfo = getattr(minfo.method, "_dinfo", {})
    for d in dinfo.get('dependencies', list(minfo.cls.param)):
        ddeps, ddynamic_deps = (minfo.cls if minfo.inst is None else minfo.inst).param._spec_to_obj(d, dynamic, intermediate)
        dynamic_deps += ddynamic_deps
        for dep in ddeps:
            if isinstance(dep, PInfo):
                deps.append(dep)
            else:
                method_deps, method_dynamic_deps = _params_depended_on(dep, dynamic, intermediate)
                deps += method_deps
                dynamic_deps += method_dynamic_deps
    return deps, dynamic_deps


def _resolve_mcs_deps(obj, resolved, dynamic, intermediate=True):
    """
    Resolves constant and dynamic parameter dependencies previously
    obtained using the _params_depended_on function. Existing resolved
    dependencies are updated with a supplied parameter instance while
    dynamic dependencies are resolved if possible.
    """
    dependencies = []
    for dep in resolved:
        if not issubclass(type(obj), dep.cls):
            dependencies.append(dep)
            continue
        inst = obj if dep.inst is None else dep.inst
        dep = PInfo(inst=inst, cls=dep.cls, name=dep.name,
                    pobj=inst.param[dep.name], what=dep.what)
        dependencies.append(dep)
    for dep in dynamic:
        subresolved, _ = obj.param._spec_to_obj(dep.spec, intermediate=intermediate)
        for subdep in subresolved:
            if isinstance(subdep, PInfo):
                dependencies.append(subdep)
            else:
                dependencies += _params_depended_on(subdep, intermediate=intermediate)[0]
    return dependencies


def _skip_event(*events, **kwargs):
    """
    Checks whether a subobject event should be skipped.
    Returns True if all the values on the new subobject
    match the values on the previous subobject.
    """
    what = kwargs.get('what', 'value')
    changed = kwargs.get('changed')
    if changed is None:
        return False
    for e in events:
        for p in changed:
            if what == 'value':
                old = Undefined if e.old is None else _getattrr(e.old, p, None)
                new = Undefined if e.new is None else _getattrr(e.new, p, None)
            else:
                old = Undefined if e.old is None else _getattrr(e.old.param[p], what, None)
                new = Undefined if e.new is None else _getattrr(e.new.param[p], what, None)
            if not Comparator.is_equal(old, new):
                return False
    return True


def extract_dependencies(function):
    """
    Extract references from a method or function that declares the references.
    """
    subparameters = list(function._dinfo['dependencies'])+list(function._dinfo['kw'].values())
    params = []
    for p in subparameters:
        if isinstance(p, str):
            owner = get_method_owner(function)
            *subps, p = p.split('.')
            for subp in subps:
                owner = getattr(owner, subp, None)
                if owner is None:
                    raise ValueError('Cannot depend on undefined sub-parameter {p!r}.')
            if p in owner.param:
                pobj = owner.param[p]
                if pobj not in params:
                    params.append(pobj)
            else:
                for sp in extract_dependencies(getattr(owner, p)):
                    if sp not in params:
                        params.append(sp)
        elif p not in params:
            params.append(p)
    return params


# Two callers at the module top level to support pickling.
async def _async_caller(*events, what='value', changed=None, callback=None, function=None):
    if callback:
        callback(*events)
    if not _skip_event or not _skip_event(*events, what=what, changed=changed):
        await function()


def _sync_caller(*events, what='value', changed=None, callback=None, function=None):
    if callback:
        callback(*events)
    if not _skip_event(*events, what=what, changed=changed):
        return function()


def _m_caller(self, method_name, what='value', changed=None, callback=None):
    """
    Wraps a method call adding support for scheduling a callback
    before it is executed and skipping events if a subobject has
    changed but its values have not.
    """
    function = getattr(self, method_name)
    _caller = _async_caller if iscoroutinefunction(function) else _sync_caller
    caller = partial(_caller, what=what, changed=changed, callback=callback, function=function)
    caller._watcher_name = method_name
    return caller


def _add_doc(obj, docstring):
    """Add a docstring to a namedtuple"""
    obj.__doc__ = docstring


PInfo = namedtuple("PInfo", "inst cls name pobj what")
_add_doc(PInfo,
    """
    Object describing something being watched about a Parameter.

    `inst`: Parameterized instance owning the Parameter, or None

    `cls`: Parameterized class owning the Parameter

    `name`: Name of the Parameter being watched

    `pobj`: Parameter object being watched

    `what`: What is being watched on the Parameter (either 'value' or a slot name)
    """)

MInfo = namedtuple("MInfo", "inst cls name method")
_add_doc(MInfo,
    """
    Object describing a Parameterized method being watched.

    `inst`: Parameterized instance owning the method, or None

    `cls`: Parameterized class owning the method

    `name`: Name of the method being watched

    `method`: bound method of the object being watched
    """)

DInfo = namedtuple("DInfo", "spec")
_add_doc(DInfo,
    """
    Object describing dynamic dependencies.
    `spec`: Dependency specification to resolve
    """)

Event = namedtuple("Event", "what name obj cls old new type")
_add_doc(Event,
    """
    Object representing an event that triggers a Watcher.

    `what`: What is being watched on the Parameter (either value or a slot name)

    `name`: Name of the Parameter that was set or triggered

    `obj`: Parameterized instance owning the watched Parameter, or None

    `cls`: Parameterized class owning the watched Parameter

    `old`: Previous value of the item being watched

    `new`: New value of the item being watched

    `type`: `triggered` if this event was triggered explicitly), `changed` if
    the item was set and watching for `onlychanged`, `set` if the item was set,
    or  None if type not yet known
    """)

_Watcher = namedtuple("Watcher", "inst cls fn mode onlychanged parameter_names what queued precedence")

class Watcher(_Watcher):
    """
    Object declaring a callback function to invoke when an Event is
    triggered on a watched item.

    `inst`: Parameterized instance owning the watched Parameter, or
    None

    `cls`: Parameterized class owning the watched Parameter

    `fn`: Callback function to invoke when triggered by a watched
    Parameter

    `mode`: 'args' for param.watch (call `fn` with PInfo object
    positional args), or 'kwargs' for param.watch_values (call `fn`
    with <param_name>:<new_value> keywords)

    `onlychanged`: If True, only trigger for actual changes, not
    setting to the current value

    `parameter_names`: List of Parameters to watch, by name

    `what`: What to watch on the Parameters (either 'value' or a slot
    name)

    `queued`: Immediately invoke callbacks triggered during processing
            of an Event (if False), or queue them up for processing
            later, after this event has been handled (if True)

    `precedence`: A numeric value which determines the precedence of
                  the watcher.  Lower precedence values are executed
                  with higher priority.
    """

    def __new__(cls_, *args, **kwargs):
        """
        Allows creating Watcher without explicit precedence value.
        """
        values = dict(zip(cls_._fields, args))
        values.update(kwargs)
        if 'precedence' not in values:
            values['precedence'] = 0
        return super().__new__(cls_, **values)

    def __str__(self):
        cls = type(self)
        attrs = ', '.join([f'{f}={getattr(self, f)!r}' for f in cls._fields])
        return f"{cls.__name__}({attrs})"




class ParameterMetaclass(type):
    """
    Metaclass allowing control over creation of Parameter classes.
    """
    def __new__(mcs, classname, bases, classdict):

        # store the class's docstring in __classdoc
        if '__doc__' in classdict:
            classdict['__classdoc']=classdict['__doc__']

        # when asking for help on Parameter *object*, return the doc slot
        classdict['__doc__'] = property(attrgetter('doc'))

        # Compute all slots in order, using a dict later turned into a list
        # as it's the fastest way to get an ordered set in Python
        all_slots = {}
        for bcls in set(chain(*(base.__mro__[::-1] for base in bases))):
            all_slots.update(dict.fromkeys(getattr(bcls, '__slots__', [])))

        # To get the benefit of slots, subclasses must themselves define
        # __slots__, whether or not they define attributes not present in
        # the base Parameter class.  That's because a subclass will have
        # a __dict__ unless it also defines __slots__.
        if '__slots__' not in classdict:
            classdict['__slots__'] = []
        else:
            all_slots.update(dict.fromkeys(classdict['__slots__']))

        classdict['_all_slots_'] = list(all_slots)

        # No special handling for a __dict__ slot; should there be?
        return type.__new__(mcs, classname, bases, classdict)

    def __getattribute__(mcs,name):
        if name=='__doc__':
            # when asking for help on Parameter *class*, return the
            # stored class docstring
            return type.__getattribute__(mcs,'__classdoc')
        else:
            return type.__getattribute__(mcs,name)


class _ParameterBase(metaclass=ParameterMetaclass):
    """
    Base Parameter class used to dynamically update the signature of all
    the Parameters.
    """

    @classmethod
    def _modified_slots_defaults(cls):
        defaults = cls._slot_defaults.copy()
        defaults['label'] = defaults.pop('_label')
        return defaults

    @classmethod
    def __init_subclass__(cls):
        # _update_signature has been tested against the Parameters available
        # in Param, we don't want to break the Parameters created elsewhere
        # so wrapping this in a loose try/except.
        try:
            cls._update_signature()
        except Exception:
            # The super signature has been changed so we need to get the one
            # from the class constructor directly.
            cls.__signature__ = inspect.signature(cls.__init__)

    @classmethod
    def _update_signature(cls):
        defaults = cls._modified_slots_defaults()
        new_parameters = {}

        for i, kls in enumerate(cls.mro()):
            if kls.__name__.startswith('_'):
                continue
            sig = inspect.signature(kls.__init__)
            for pname, parameter in sig.parameters.items():
                if pname == 'self':
                    continue
                if i >= 1 and parameter.default == inspect.Signature.empty:
                    continue
                if parameter.kind in (inspect.Parameter.VAR_KEYWORD, inspect.Parameter.VAR_POSITIONAL):
                    continue
                if getattr(parameter, 'default', None) is Undefined:
                    if pname not in defaults:
                        raise LookupError(
                            f'Argument {pname!r} of Parameter {cls.__name__!r} has no '
                            'entry in _slot_defaults.'
                        )
                    default = defaults[pname]
                    if callable(default) and hasattr(default, 'sig'):
                        default = default.sig
                    new_parameter = parameter.replace(default=default)
                else:
                    new_parameter = parameter
                if i >= 1:
                    new_parameter = new_parameter.replace(kind=inspect.Parameter.KEYWORD_ONLY)
                new_parameters.setdefault(pname, new_parameter)

        def _sorter(p):
            if p.default == inspect.Signature.empty:
                return 0
            else:
                return 1

        new_parameters = sorted(new_parameters.values(), key=_sorter)
        new_sig = sig.replace(parameters=new_parameters)
        cls.__signature__ = new_sig


class Parameter(_ParameterBase):
    """
    An attribute descriptor for declaring parameters.

    Parameters are a special kind of class attribute.  Setting a
    Parameterized class attribute to be a Parameter instance causes
    that attribute of the class (and the class's instances) to be
    treated as a Parameter.  This allows special behavior, including
    dynamically generated parameter values, documentation strings,
    constant and read-only parameters, and type or range checking at
    assignment time.

    For example, suppose someone wants to define two new kinds of
    objects Foo and Bar, such that Bar has a parameter delta, Foo is a
    subclass of Bar, and Foo has parameters alpha, sigma, and gamma
    (and delta inherited from Bar).  She would begin her class
    definitions with something like this::

       class Bar(Parameterized):
           delta = Parameter(default=0.6, doc='The difference between steps.')
           ...
       class Foo(Bar):
           alpha = Parameter(default=0.1, doc='The starting value.')
           sigma = Parameter(default=0.5, doc='The standard deviation.',
                           constant=True)
           gamma = Parameter(default=1.0, doc='The ending value.')
           ...

    Class Foo would then have four parameters, with delta defaulting
    to 0.6.

    Parameters have several advantages over plain attributes:

    1. Parameters can be set automatically when an instance is
       constructed: The default constructor for Foo (and Bar) will
       accept arbitrary keyword arguments, each of which can be used
       to specify the value of a Parameter of Foo (or any of Foo's
       superclasses).  E.g., if a script does this::

           myfoo = Foo(alpha=0.5)

       myfoo.alpha will return 0.5, without the Foo constructor
       needing special code to set alpha.

       If Foo implements its own constructor, keyword arguments will
       still be accepted if the constructor accepts a dictionary of
       keyword arguments (as in ``def __init__(self,**params):``), and
       then each class calls its superclass (as in
       ``super(Foo,self).__init__(**params)``) so that the
       Parameterized constructor will process the keywords.

    2. A Parameterized class need specify only the attributes of a
       Parameter whose values differ from those declared in
       superclasses; the other values will be inherited.  E.g. if Foo
       declares::

        delta = Parameter(default=0.2)

       the default value of 0.2 will override the 0.6 inherited from
       Bar, but the doc will be inherited from Bar.

    3. The Parameter descriptor class can be subclassed to provide
       more complex behavior, allowing special types of parameters
       that, for example, require their values to be numbers in
       certain ranges, generate their values dynamically from a random
       distribution, or read their values from a file or other
       external source.

    4. The attributes associated with Parameters provide enough
       information for automatically generating property sheets in
       graphical user interfaces, allowing Parameterized instances to
       be edited by users.

    Note that Parameters can only be used when set as class attributes
    of Parameterized classes. Parameters used as standalone objects,
    or as class attributes of non-Parameterized classes, will not have
    the behavior described here.
    """

    # Because they implement __get__ and __set__, Parameters are known
    # as 'descriptors' in Python; see "Implementing Descriptors" and
    # "Invoking Descriptors" in the 'Customizing attribute access'
    # section of the Python reference manual:
    # http://docs.python.org/ref/attribute-access.html
    #
    # Overview of Parameters for programmers
    # ======================================
    #
    # Consider the following code:
    #
    #
    # class A(Parameterized):
    #     p = Parameter(default=1)
    #
    # a1 = A()
    # a2 = A()
    #
    #
    # * a1 and a2 share one Parameter object (A.__dict__['p']).
    #
    # * The default (class) value of p is stored in this Parameter
    #   object (A.__dict__['p'].default).
    #
    # * If the value of p is set on a1 (e.g. a1.p=2), a1's value of p
    #   is stored in a1 itself (a1._param__private.values['p'])
    #
    # * When a1.p is requested, a1._param__private.values['p'] is
    #   returned. When a2.p is requested, 'p' is not found in
    #   a1._param__private.values, so A.__dict__['p'].default (i.e. A.p) is
    #   returned instead.
    #
    #
    # Be careful when referring to the 'name' of a Parameter:
    #
    # * A Parameterized class has a name for the attribute which is
    #   being represented by the Parameter ('p' in the example above);
    #   in the code, this is called the 'name'.
    #
    # * When a Parameterized instance has its own local value for a
    #   parameter, it is stored as 'p._param__private.values[X]' where X is the
    #   name of the Parameter


    # So that the extra features of Parameters do not require a lot of
    # overhead, Parameters are implemented using __slots__ (see
    # http://www.python.org/doc/2.4/ref/slots.html).  Instead of having
    # a full Python dictionary associated with each Parameter instance,
    # Parameter instances have an enumerated list (named __slots__) of
    # attributes, and reserve just enough space to store these
    # attributes.  Using __slots__ requires special support for
    # operations to copy and restore Parameters (e.g. for Python
    # persistent storage pickling); see __getstate__ and __setstate__.
    __slots__ = ['name', 'default', 'doc',
                 'precedence', 'instantiate', 'constant', 'readonly',
                 'pickle_default_value', 'allow_None', 'per_instance',
                 'watchers', 'owner', 'allow_refs', 'nested_refs', '_label']

    # Note: When initially created, a Parameter does not know which
    # Parameterized class owns it, nor does it know its names
    # (attribute name, internal name). Once the owning Parameterized
    # class is created, owner, and name are
    # set.

    _serializers = {'json': serializer.JSONSerialization}

    _slot_defaults = dict(
        default=None, precedence=None, doc=None, _label=None, instantiate=False,
        constant=False, readonly=False, pickle_default_value=True, allow_None=False,
        per_instance=True, allow_refs=False, nested_refs=False
    )

    # Parameters can be updated during Parameterized class creation when they
    # are defined multiple times in a class hierarchy. We have to record which
    # Parameter slots require the default value to be re-validated. Any slots
    # in this list do not have to trigger such re-validation.
    _non_validated_slots = ['_label', 'doc', 'name', 'precedence',
                            'constant', 'pickle_default_value',
                            'watchers', 'owner']

    @typing.overload
    def __init__(
        self,
        default=None, *,
        doc=None, label=None, precedence=None, instantiate=False, constant=False,
        readonly=False, pickle_default_value=True, allow_None=False, per_instance=True,
        allow_refs=False, nested_refs=False
    ):
        ...

    @_deprecate_positional_args
    def __init__(self, default=Undefined, *, doc=Undefined, # pylint: disable-msg=R0913
                 label=Undefined, precedence=Undefined,
                 instantiate=Undefined, constant=Undefined, readonly=Undefined,
                 pickle_default_value=Undefined, allow_None=Undefined,
                 per_instance=Undefined, allow_refs=Undefined, nested_refs=Undefined):

        """Initialize a new Parameter object and store the supplied attributes:

        default: the owning class's value for the attribute represented
        by this Parameter, which can be overridden in an instance.

        doc: docstring explaining what this parameter represents.

        label: optional text label to be used when this Parameter is
        shown in a listing. If no label is supplied, the attribute name
        for this parameter in the owning Parameterized object is used.

        precedence: a numeric value, usually in the range 0.0 to 1.0,
        which allows the order of Parameters in a class to be defined in
        a listing or e.g. in GUI menus. A negative precedence indicates
        a parameter that should be hidden in such listings.

        instantiate: controls whether the value of this Parameter will
        be deepcopied when a Parameterized object is instantiated (if
        True), or if the single default value will be shared by all
        Parameterized instances (if False). For an immutable Parameter
        value, it is best to leave instantiate at the default of
        False, so that a user can choose to change the value at the
        Parameterized instance level (affecting only that instance) or
        at the Parameterized class or superclass level (affecting all
        existing and future instances of that class or superclass). For
        a mutable Parameter value, the default of False is also appropriate
        if you want all instances to share the same value state, e.g. if
        they are each simply referring to a single global object like
        a singleton. If instead each Parameterized should have its own
        independently mutable value, instantiate should be set to
        True, but note that there is then no simple way to change the
        value of this Parameter at the class or superclass level,
        because each instance, once created, will then have an
        independently instantiated value.

        constant: if true, the Parameter value can be changed only at
        the class level or in a Parameterized constructor call. The
        value is otherwise constant on the Parameterized instance,
        once it has been constructed.

        readonly: if true, the Parameter value cannot ordinarily be
        changed by setting the attribute at the class or instance
        levels at all. The value can still be changed in code by
        temporarily overriding the value of this slot and then
        restoring it, which is useful for reporting values that the
        _user_ should never change but which do change during code
        execution.

        pickle_default_value: whether the default value should be
        pickled. Usually, you would want the default value to be pickled,
        but there are rare cases where that would not be the case (e.g.
        for file search paths that are specific to a certain system).

        per_instance: whether a separate Parameter instance will be
        created for every Parameterized instance. True by default.
        If False, all instances of a Parameterized class will share
        the same Parameter object, including all validation
        attributes (bounds, etc.). See also instantiate, which is
        conceptually similar but affects the Parameter value rather
        than the Parameter object.

        allow_None: if True, None is accepted as a valid value for
        this Parameter, in addition to any other values that are
        allowed. If the default value is defined as None, allow_None
        is set to True automatically.

        allow_refs: if True allows automatically linking parameter
        references to this Parameter, i.e. the parameter value will
        automatically reflect the current value of the reference that
        is passed in.

        nested_refs: if True and allow_refs=True then even nested objects
        such as dictionaries, lists, slices, tuples and sets will be
        inspected for references and will be automatically resolved.

        default, doc, and precedence all default to None, which allows
        inheritance of Parameter slots (attributes) from the owning-class'
        class hierarchy (see ParameterizedMetaclass).
        """

        self.name = None
        self.owner = None
        self.allow_refs = allow_refs
        self.nested_refs = nested_refs
        self.precedence = precedence
        self.default = default
        self.doc = doc
        self.constant = constant is True or readonly is True # readonly => constant
        self.readonly = readonly
        self._label = label
        self._set_instantiate(instantiate)
        self.pickle_default_value = pickle_default_value
        self._set_allow_None(allow_None)
        self.watchers = {}
        self.per_instance = per_instance

    @classmethod
    def serialize(cls, value):
        "Given the parameter value, return a Python value suitable for serialization"
        return value

    @classmethod
    def deserialize(cls, value):
        "Given a serializable Python value, return a value that the parameter can be set to"
        return value

    def schema(self, safe=False, subset=None, mode='json'):
        if serializer is None:
            raise ImportError('Cannot import serializer.py needed to generate schema')
        if mode not in  self._serializers:
            raise KeyError(f'Mode {mode!r} not in available serialization formats {list(self._serializers.keys())!r}')
        return self._serializers[mode].param_schema(self.__class__.__name__, self,
                                                    safe=safe, subset=subset)

    @property
    def rx(self):
        from .reactive import reactive_ops
        return reactive_ops(self)

    @property
    def label(self):
        if self.name and self._label is None:
            return label_formatter(self.name)
        else:
            return self._label

    @label.setter
    def label(self, val):
        self._label = val

    def _set_allow_None(self, allow_None):
        # allow_None is set following these rules (last takes precedence):
        # 1. to False by default
        # 2. to the value provided in the constructor, if any
        # 3. to True if default is None
        if self.default is None:
            self.allow_None = True
        elif allow_None is not Undefined:
            self.allow_None = allow_None
        else:
            self.allow_None = self._slot_defaults['allow_None']

    def _set_instantiate(self,instantiate):
        """Constant parameters must be instantiated."""
        # instantiate doesn't actually matter for read-only
        # parameters, since they can't be set even on a class.  But
        # having this code avoids needless instantiation.
        if self.readonly:
            self.instantiate = False
        elif instantiate is not Undefined:
            self.instantiate = instantiate
        else:
            # Default value
            self.instantiate = self._slot_defaults['instantiate']

    def __setattr__(self, attribute, value):
        if attribute == 'name':
            name = getattr(self, 'name', None)
            if name is not None and value != name:
                raise AttributeError("Parameter name cannot be modified after "
                                     "it has been bound to a Parameterized.")

        is_slot = attribute in self.__class__._all_slots_
        has_watcher = attribute != "default" and attribute in getattr(self, 'watchers', [])
        if not (is_slot or has_watcher):
            # Return early if attribute is not a slot
            return super().__setattr__(attribute, value)

        # Otherwise get the old value so we can call watcher/on_set
        old = getattr(self, attribute, NotImplemented)
        if is_slot:
            try:
                self._on_set(attribute, old, value)
            except AttributeError:
                pass

        super().__setattr__(attribute, value)
        if has_watcher and old is not NotImplemented:
            self._trigger_event(attribute, old, value)

    def _trigger_event(self, attribute, old, new):
        event = Event(what=attribute, name=self.name, obj=None, cls=self.owner,
                      old=old, new=new, type=None)
        for watcher in self.watchers[attribute]:
            self.owner.param._call_watcher(watcher, event)
        if not self.owner.param._BATCH_WATCH:
            self.owner.param._batch_call_watchers()

    def __getattribute__(self, key):
        """
        Allow slot values to be Undefined in an "unbound" parameter, i.e. one
        that is not (yet) owned by a Parameterized object, in which case their
        value will be retrieved from the _slot_defaults dictionary.
        """
        v = object.__getattribute__(self, key)
        # Safely checks for name (avoiding recursion) to decide if this object is unbound
        if v is Undefined and key != "name" and getattr(self, "name", None) is None:
            try:
                v = self._slot_defaults[key]
            except KeyError as e:
                raise KeyError(
                    f'Slot {key!r} on unbound parameter {self.__class__.__name__!r} '
                    'has no default value defined in `_slot_defaults`'
                ) from e
            if callable(v):
                v = v(self)
        return v

    def _on_set(self, attribute, old, value):
        """
        Can be overridden on subclasses to handle changes when parameter
        attribute is set.
        """

    def _update_state(self):
        """
        Can be overridden on subclasses to update a Parameter state, i.e. slot
        values, after the slot values have been set in the inheritance procedure.
        """

    def __get__(self, obj, objtype): # pylint: disable-msg=W0613
        """
        Return the value for this Parameter.

        If called for a Parameterized class, produce that
        class's value (i.e. this Parameter object's 'default'
        attribute).

        If called for a Parameterized instance, produce that
        instance's value, if one has been set - otherwise produce the
        class's value (default).
        """
        if obj is None: # e.g. when __get__ called for a Parameterized class
            result = self.default
        else:
            # Attribute error when .values does not exist (_ClassPrivate)
            # and KeyError when there's no cached value for this parameter.
            try:
                result = obj._param__private.values[self.name]
            except (AttributeError, KeyError):
                result = self.default
        return result

    @instance_descriptor
    def __set__(self, obj, val):
        """
        Set the value for this Parameter.

        If called for a Parameterized class, set that class's
        value (i.e. set this Parameter object's 'default' attribute).

        If called for a Parameterized instance, set the value of
        this Parameter on that instance (i.e. in the instance's
        `values` dictionary located in the private namespace `_param__private`,
        under the parameter's name).

        If the Parameter's constant attribute is True, only allows
        the value to be set for a Parameterized class or on
        uninitialized Parameterized instances.

        If the Parameter's readonly attribute is True, only allows the
        value to be specified in the Parameter declaration inside the
        Parameterized source code. A read-only parameter also
        cannot be set on a Parameterized class.

        Note that until we support some form of read-only
        object, it is still possible to change the attributes of the
        object stored in a constant or read-only Parameter (e.g. one
        item in a list).
        """
        name = self.name
        if obj is not None and self.allow_refs and obj._param__private.initialized:
            syncing = name in obj._param__private.syncing
            ref, deps, val, is_async = obj.param._resolve_ref(self, val)
            refs = obj._param__private.refs
            if ref is not None:
                self.owner.param._update_ref(name, ref)
            elif name in refs and not syncing:
                del refs[name]
                if name in obj._param__private.async_refs:
                    obj._param__private.async_refs.pop(name).cancel()
            if is_async or val is Undefined:
                return

        # Deprecated Number set_hook called here to avoid duplicating setter
        if hasattr(self, 'set_hook'):
            val = self.set_hook(obj, val)
            if self.set_hook is not _identity_hook:
                # PARAM3_DEPRECATION
                warnings.warn(
                    'Number.set_hook has been deprecated.',
                    category=_ParamDeprecationWarning,
                    stacklevel=6,
                )

        self._validate(val)

        _old = NotImplemented
        # obj can be None if __set__ is called for a Parameterized class
        if self.constant or self.readonly:
            if self.readonly:
                raise TypeError("Read-only parameter '%s' cannot be modified" % name)
            elif obj is None:
                _old = self.default
                self.default = val
            elif not obj._param__private.initialized:
                _old = obj._param__private.values.get(self.name, self.default)
                obj._param__private.values[self.name] = val
            else:
                _old = obj._param__private.values.get(self.name, self.default)
                if val is not _old:
                    raise TypeError("Constant parameter '%s' cannot be modified" % name)
        else:
            if obj is None:
                _old = self.default
                self.default = val
            else:
                # When setting a Parameter before calling super.
                if not isinstance(obj._param__private, _InstancePrivate):
                    obj._param__private = _InstancePrivate(
                        explicit_no_refs=type(obj)._param__private.explicit_no_refs
                    )
                _old = obj._param__private.values.get(name, self.default)
                obj._param__private.values[name] = val
        self._post_setter(obj, val)

        if obj is not None:
            if not hasattr(obj, '_param__private') or not getattr(obj._param__private, 'initialized', False):
                return
            obj.param._update_deps(name)

        if obj is None:
            watchers = self.watchers.get("value")
        elif name in obj._param__private.watchers:
            watchers = obj._param__private.watchers[name].get('value')
            if watchers is None:
                watchers = self.watchers.get("value")
        else:
            watchers = None

        obj = self.owner if obj is None else obj

        if obj is None or not watchers:
            return

        event = Event(what='value', name=name, obj=obj, cls=self.owner,
                      old=_old, new=val, type=None)

        # Copy watchers here since they may be modified inplace during iteration
        for watcher in sorted(watchers, key=lambda w: w.precedence):
            obj.param._call_watcher(watcher, event)
        if not obj.param._BATCH_WATCH:
            obj.param._batch_call_watchers()

    def _validate_value(self, value, allow_None):
        """Implements validation for parameter value"""

    def _validate(self, val):
        """Implements validation for the parameter value and attributes"""
        self._validate_value(val, self.allow_None)

    def _post_setter(self, obj, val):
        """Called after the parameter value has been validated and set"""

    def __delete__(self,obj):
        raise TypeError("Cannot delete '%s': Parameters deletion not allowed." % self.name)

    def _set_names(self, attrib_name):
        if None not in (self.owner, self.name) and attrib_name != self.name:
            raise AttributeError('The {} parameter {!r} has already been '
                                 'assigned a name by the {} class, '
                                 'could not assign new name {!r}. Parameters '
                                 'may not be shared by multiple classes; '
                                 'ensure that you create a new parameter '
                                 'instance for each new class.'.format(type(self).__name__, self.name,
                                    self.owner.name, attrib_name))
        self.name = attrib_name

    def __getstate__(self):
        """
        All Parameters have slots, not a dict, so we have to support
        pickle and deepcopy ourselves.
        """
        return {slot: getattr(self, slot) for slot in self.__class__._all_slots_}

    def __setstate__(self,state):
        # set values of __slots__ (instead of in non-existent __dict__)
        for k, v in state.items():
            setattr(self, k, v)


# Define one particular type of Parameter that is used in this file
class String(Parameter):
    r"""
    A String Parameter, with a default value and optional regular expression (regex) matching.

    Example of using a regex to implement IPv4 address matching::

      class IPAddress(String):
        '''IPv4 address as a string (dotted decimal notation)'''
       def __init__(self, default="0.0.0.0", allow_None=False, **kwargs):
           ip_regex = r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
           super(IPAddress, self).__init__(default=default, regex=ip_regex, **kwargs)

    """

    __slots__ = ['regex']

    _slot_defaults = _dict_update(Parameter._slot_defaults, default="", regex=None)

    @typing.overload
    def __init__(
        self,
        default="", *, regex=None,
        doc=None, label=None, precedence=None, instantiate=False, constant=False,
        readonly=False, pickle_default_value=True, allow_None=False, per_instance=True,
        allow_refs=False, nested_refs=False
    ):
        ...

    @_deprecate_positional_args
    def __init__(self, default=Undefined, *, regex=Undefined, **kwargs):
        super().__init__(default=default, **kwargs)
        self.regex = regex
        self._validate(self.default)

    def _validate_regex(self, val, regex):
        if (val is None and self.allow_None):
            return
        if regex is not None and re.match(regex, val) is None:
            raise ValueError(
                f'{_validate_error_prefix(self)} value {val!r} does not '
                f'match regex {regex!r}.'
            )

    def _validate_value(self, val, allow_None):
        if allow_None and val is None:
            return
        if not isinstance(val, str):
            raise ValueError(
                f'{_validate_error_prefix(self)} only takes a string value, '
                f'not value of {type(val)}.'
            )

    def _validate(self, val):
        self._validate_value(val, self.allow_None)
        self._validate_regex(val, self.regex)


class shared_parameters:
    """
    Context manager to share parameter instances when creating
    multiple Parameterized objects of the same type. Parameter default
    values are instantiated once and cached to be reused when another
    Parameterized object of the same type is instantiated.
    Can be useful to easily modify large collections of Parameterized
    objects at once and can provide a significant speedup.
    """

    _share = False
    _shared_cache = {}

    def __enter__(self):
        shared_parameters._share = True

    def __exit__(self, exc_type, exc_val, exc_tb):
        shared_parameters._share = False
        shared_parameters._shared_cache = {}


def as_uninitialized(fn):
    """
    Decorator: call fn with the parameterized_instance's
    initialization flag set to False, then revert the flag.

    (Used to decorate Parameterized methods that must alter
    a constant Parameter.)
    """
    @wraps(fn)
    def override_initialization(self_,*args,**kw):
        parameterized_instance = self_.self
        original_initialized = parameterized_instance._param__private.initialized
        parameterized_instance._param__private.initialized = False
        ret = fn(self_, *args, **kw)
        parameterized_instance._param__private.initialized = original_initialized
        return ret
    return override_initialization


class Comparator:
    """
    Comparator defines methods for determining whether two objects
    should be considered equal. It works by registering custom
    comparison functions, which may either be registed by type or with
    a predicate function. If no matching comparison can be found for
    the two objects the comparison will return False.

    If registered by type the Comparator will check whether both
    objects are of that type and apply the comparison. If the equality
    function is instead registered with a function it will call the
    function with each object individually to check if the comparison
    applies. This is useful for defining comparisons for objects
    without explicitly importing them.

    To use the Comparator simply call the is_equal function.
    """

    equalities = {
        numbers.Number: operator.eq,
        str: operator.eq,
        bytes: operator.eq,
        type(None): operator.eq,
        lambda o: hasattr(o, '_infinitely_iterable'): operator.eq,  # Time
    }
    equalities.update({dtt: operator.eq for dtt in dt_types})

    @classmethod
    def is_equal(cls, obj1, obj2):
        for eq_type, eq in cls.equalities.items():
            try:
                are_instances = isinstance(obj1, eq_type) and isinstance(obj2, eq_type)
            except TypeError:
                pass
            else:
                if are_instances:
                    return eq(obj1, obj2)
            if isinstance(eq_type, FunctionType) and eq_type(obj1) and eq_type(obj2):
                return eq(obj1, obj2)
        if isinstance(obj2, (list, set, tuple)):
            return cls.compare_iterator(obj1, obj2)
        elif isinstance(obj2, dict):
            return cls.compare_mapping(obj1, obj2)
        return False

    @classmethod
    def compare_iterator(cls, obj1, obj2):
        if type(obj1) != type(obj2) or len(obj1) != len(obj2):
            return False
        for o1, o2 in zip(obj1, obj2):
            if not cls.is_equal(o1, o2):
                return False
        return True

    @classmethod
    def compare_mapping(cls, obj1, obj2):
        if type(obj1) != type(obj2) or len(obj1) != len(obj2): return False
        for k in obj1:
            if k in obj2:
                if not cls.is_equal(obj1[k], obj2[k]):
                    return False
            else:
                return False
        return True


class _ParametersRestorer:
    """
    Context-manager to handle the reset of parameter values after an update.
    """

    def __init__(self, *, parameters, restore, refs=None):
        self._parameters = parameters
        self._restore = restore
        self._refs = {} if refs is None else refs

    def __enter__(self):
        return self._restore

    def __exit__(self, exc_type, exc_value, exc_tb):
        try:
            self._parameters._update(dict(self._restore, **self._refs))
        finally:
            self._restore = {}


class Parameters:
    """Object that holds the namespace and implementation of Parameterized
    methods as well as any state that is not in __slots__ or the
    Parameters themselves.

    Exists at both the metaclass level (instantiated by the metaclass)
    and at the instance level. Can contain state specific to either the
    class or the instance as necessary.
    """

    def __init__(self_, cls, self=None):
        """
        cls is the Parameterized class which is always set.
        self is the instance if set.
        """
        self_.cls = cls
        self_.self = self

    @property
    def _BATCH_WATCH(self_):
        return self_.self_or_cls._param__private.parameters_state['BATCH_WATCH']

    @_BATCH_WATCH.setter
    def _BATCH_WATCH(self_, value):
        self_.self_or_cls._param__private.parameters_state['BATCH_WATCH'] = value

    @property
    def _TRIGGER(self_):
        return self_.self_or_cls._param__private.parameters_state['TRIGGER']

    @_TRIGGER.setter
    def _TRIGGER(self_, value):
        self_.self_or_cls._param__private.parameters_state['TRIGGER'] = value

    @property
    def _events(self_):
        return self_.self_or_cls._param__private.parameters_state['events']

    @_events.setter
    def _events(self_, value):
        self_.self_or_cls._param__private.parameters_state['events'] = value

    @property
    def _state_watchers(self_):
        return self_.self_or_cls._param__private.parameters_state['watchers']

    @_state_watchers.setter
    def _state_watchers(self_, value):
        self_.self_or_cls._param__private.parameters_state['watchers'] = value

    @property
    def watchers(self_):
        """Dictionary of instance watchers."""
        if self_.self is None:
            raise TypeError('Accessing `.param.watchers` is only supported on a Parameterized instance, not class.')
        return self_.self._param__private.watchers

    @watchers.setter
    def watchers(self_, value):
        if self_.self is None:
            raise TypeError('Setting `.param.watchers` is only supported on a Parameterized instance, not class.')
        self_.self._param__private.watchers = value

    @property
    def self_or_cls(self_):
        return self_.cls if self_.self is None else self_.self

    def __setstate__(self, state):
        # Set old parameters state on Parameterized.parameters_state
        self_, cls = state.get('self'), state.get('cls')
        self_or_cls = self_ if self_ is not None else cls
        for k in self_or_cls._param__private.parameters_state:
            key = '_'+k
            if key in state:
                self_or_cls._param__private.parameters_state[k] = state.pop(key)
        for k, v in state.items():
            setattr(self, k, v)

    def __getitem__(self_, key):
        """
        Returns the class or instance parameter
        """
        inst = self_.self
        if inst is None:
            return self_._cls_parameters[key]
        p = self_.objects(instance=False)[key]
        return _instantiated_parameter(inst, p)

    def __dir__(self_):
        """
        Adds parameters to dir
        """
        return super().__dir__() + list(self_._cls_parameters)

    def __iter__(self_):
        """
        Iterates over the parameters on this object.
        """
        yield from self_._cls_parameters

    def __contains__(self_, param):
        return param in self_._cls_parameters

    def __getattr__(self_, attr):
        """
        Extends attribute access to parameter objects.
        """
        cls = self_.__dict__.get('cls')
        if cls is None: # Class not initialized
            raise AttributeError

        if attr in self_._cls_parameters:
            return self_.__getitem__(attr)
        elif self_.self is None:
            raise AttributeError(f"type object '{self_.cls.__name__}.param' has no attribute {attr!r}")
        else:
            raise AttributeError(f"'{self_.cls.__name__}.param' object has no attribute {attr!r}")

    @as_uninitialized
    def _set_name(self_, name):
        self_.self.name = name

    @as_uninitialized
    def _generate_name(self_):
        self_._set_name('%s%05d' % (self_.cls.__name__, object_count))

    @as_uninitialized
    def _setup_params(self_, **params):
        """
        Initialize default and keyword parameter values.

        First, ensures that values for all Parameters with 'instantiate=True'
        (typically used for mutable Parameters) are copied directly into each object,
        to ensure that there is an independent copy of the value (to avoid surprising
        aliasing errors). Second, ensures that Parameters with 'constant=True' are
        referenced on the instance, to make sure that setting a constant
        Parameter on the class doesn't affect already created instances. Then
        sets each of the keyword arguments, raising when any of them are not
        defined as parameters.
        """
        self = self_.self
        ## Deepcopy all 'instantiate=True' parameters
        params_to_deepcopy = {}
        params_to_ref = {}
        objects = self_._cls_parameters
        for pname, p in objects.items():
            if p.instantiate and pname != "name":
                params_to_deepcopy[pname] = p
            elif p.constant and pname != 'name':
                params_to_ref[pname] = p

        for p in params_to_deepcopy.values():
            self_._instantiate_param(p)
        for p in params_to_ref.values():
            self_._instantiate_param(p, deepcopy=False)

        ## keyword arg setting
        deps, refs = {}, {}
        for name, val in params.items():
            desc = self_.cls.get_param_descriptor(name)[0] # pylint: disable-msg=E1101
            if not desc:
                raise TypeError(
                    f"{self.__class__.__name__}.__init__() got an unexpected "
                    f"keyword argument {name!r}"
                )

            pobj = objects.get(name)
            if pobj is None or not pobj.allow_refs:
                # Until Parameter.allow_refs=True by default we have to
                # speculatively evaluate a values to check whether they
                # contain a reference and warn the user that the
                # behavior may change in future.
                if name not in self_.cls._param__private.explicit_no_refs:
                    try:
                        ref, _, resolved, _ = self_._resolve_ref(pobj, val)
                    except Exception:
                        ref = None
                    if ref:
                        warnings.warn(
                            f"Parameter {name!r} on {pobj.owner} is being given a valid parameter "
                            f"reference {val} but is implicitly allow_refs=False. "
                            "In future allow_refs will be enabled by default and "
                            f"the reference {val} will be resolved to its underlying "
                            f"value {resolved}. Please explicitly set allow_ref on the "
                            "Parameter definition to declare whether references "
                            "should be resolved or not.",
                            category=_ParamFutureWarning,
                            stacklevel=4,
                        )
                setattr(self, name, val)
                continue

            # Resolve references
            ref, ref_deps, resolved, is_async = self_._resolve_ref(pobj, val)
            if ref is not None:
                refs[name] = ref
                deps[name] = ref_deps
            if not is_async and not (resolved is Undefined or resolved is Skip):
                setattr(self, name, resolved)
        return refs, deps

    def _setup_refs(self_, refs):
        groups = defaultdict(list)
        for pname, subrefs in refs.items():
            for p in subrefs:

                if isinstance(p, Parameter):
                    groups[p.owner].append((pname, p.name))
                else:
                    for sp in extract_dependencies(p):
                        groups[sp.owner].append((pname, sp.name))
        for owner, pnames in groups.items():
            refnames, pnames = zip(*pnames)
            self_.self._param__private.ref_watchers.append((
                refnames,
                owner.param._watch(self_._sync_refs, list(set(pnames)), precedence=-1)
            ))

    def _update_ref(self_, name, ref):
        param_private = self_.self._param__private
        if name in param_private.async_refs:
            param_private.async_refs.pop(name).cancel()
        for _, watcher in param_private.ref_watchers:
            dep_obj = watcher.cls if watcher.inst is None else watcher.inst
            dep_obj.param.unwatch(watcher)
        self_.self._param__private.ref_watchers = []
        refs = dict(self_.self._param__private.refs, **{name: ref})
        deps = {name: resolve_ref(ref) for name, ref in refs.items()}
        self_._setup_refs(deps)
        self_.self._param__private.refs = refs

    def _sync_refs(self_, *events):
        updates = {}
        for pname, ref in self_.self._param__private.refs.items():
            # Skip updating value if dependency has not changed
            recursive = self_[pname].nested_refs
            deps = resolve_ref(ref, recursive)
            is_gen = inspect.isgeneratorfunction(ref)
            is_async = iscoroutinefunction(ref) or is_gen
            if not any((dep.owner is e.obj and dep.name == e.name) for dep in deps for e in events) and not is_async:
                continue

            try:
                new_val = resolve_value(ref, recursive)
            except Skip:
                new_val = Undefined
            if new_val is Skip or new_val is Undefined:
                continue
            elif is_async:
                async_executor(partial(self_._async_ref, pname, new_val))
                continue

            updates[pname] = new_val

        with edit_constant(self_.self):
            with _syncing(self_.self, updates):
                self_.update(updates)

    def _resolve_ref(self_, pobj, value):
        is_gen = inspect.isgeneratorfunction(value)
        is_async = iscoroutinefunction(value) or is_gen
        deps = resolve_ref(value, recursive=pobj.nested_refs)
        if not (deps or is_async or is_gen):
            return None, None, value, False
        ref = value
        try:
            value = resolve_value(value, recursive=pobj.nested_refs)
        except Skip:
            value = Undefined
        if is_async:
            async_executor(partial(self_._async_ref, pobj.name, value))
            value = None
        return ref, deps, value, is_async

    async def _async_ref(self_, pname, awaitable):
        if not self_.self._param__private.initialized:
            async_executor(partial(self_._async_ref, pname, awaitable))
            return

        current_task = asyncio.current_task()
        running_task = self_.self._param__private.async_refs.get(pname)
        if running_task is None:
            self_.self._param__private.async_refs[pname] = current_task
        elif current_task is not running_task:
            self_.self._param__private.async_refs[pname].cancel()
        try:
            if isinstance(awaitable, types.AsyncGeneratorType):
                async for new_obj in awaitable:
                    with _syncing(self_.self, (pname,)):
                        self_.update({pname: new_obj})
            else:
                with _syncing(self_.self, (pname,)):
                    try:
                        self_.update({pname: await awaitable})
                    except Skip:
                        pass
        finally:
            # Ensure we clean up but only if the task matches the currrent task
            if self_.self._param__private.async_refs.get(pname) is current_task:
                del self_.self._param__private.async_refs[pname]

    @classmethod
    def _changed(cls, event):
        """
        Predicate that determines whether a Event object has actually
        changed such that old != new.
        """
        return not Comparator.is_equal(event.old, event.new)

    def _instantiate_param(self_, param_obj, dict_=None, key=None, deepcopy=True):
        # deepcopy or store a reference to reference param_obj.default into
        # self._param__private.values (or dict_ if supplied) under the
        # parameter's name (or key if supplied)
        instantiator = copy.deepcopy if deepcopy else lambda o: o
        self = self_.self
        dict_ = dict_ or self._param__private.values
        key = key or param_obj.name
        if shared_parameters._share:
            param_key = (str(type(self)), param_obj.name)
            if param_key in shared_parameters._shared_cache:
                new_object = shared_parameters._shared_cache[param_key]
            else:
                new_object = instantiator(param_obj.default)
                shared_parameters._shared_cache[param_key] = new_object
        else:
            new_object = instantiator(param_obj.default)

        dict_[key] = new_object

        if isinstance(new_object, Parameterized) and deepcopy:
            global object_count
            object_count += 1
            # Writes over name given to the original object;
            # could instead have kept the same name
            new_object.param._generate_name()

    def _update_deps(self_, attribute=None, init=False):
        obj = self_.self
        init_methods = []
        for method, queued, on_init, constant, dynamic in type(obj).param._depends['watch']:
            # On initialization set up constant watchers; otherwise
            # clean up previous dynamic watchers for the updated attribute
            dynamic = [d for d in dynamic if attribute is None or d.spec.split(".")[0] == attribute]
            if init:
                constant_grouped = defaultdict(list)
                for dep in _resolve_mcs_deps(obj, constant, []):
                    constant_grouped[(id(dep.inst), id(dep.cls), dep.what)].append((None, dep))
                for group in constant_grouped.values():
                    self_._watch_group(obj, method, queued, group)
                m = getattr(self_.self, method)
                if on_init and m not in init_methods:
                    init_methods.append(m)
            elif dynamic:
                for w in obj._param__private.dynamic_watchers.pop(method, []):
                    (w.cls if w.inst is None else w.inst).param.unwatch(w)
            else:
                continue

            # Resolve dynamic dependencies one-by-one to be able to trace their watchers
            grouped = defaultdict(list)
            for ddep in dynamic:
                for dep in _resolve_mcs_deps(obj, [], [ddep]):
                    grouped[(id(dep.inst), id(dep.cls), dep.what)].append((ddep, dep))

            for group in grouped.values():
                watcher = self_._watch_group(obj, method, queued, group, attribute)
                obj._param__private.dynamic_watchers[method].append(watcher)
        for m in init_methods:
            m()

    def _resolve_dynamic_deps(self, obj, dynamic_dep, param_dep, attribute):
        """
        If a subobject whose parameters are being depended on changes
        we should only trigger events if the actual parameter values
        of the new object differ from those on the old subobject,
        therefore we accumulate parameters to compare on a subobject
        change event.

        Additionally we need to make sure to notify the parent object
        if a subobject changes so the dependencies can be
        reinitialized so we return a callback which updates the
        dependencies.
        """
        subobj = obj
        subobjs = [obj]
        for subpath in dynamic_dep.spec.split('.')[:-1]:
            subobj = getattr(subobj, subpath.split(':')[0], None)
            subobjs.append(subobj)

        dep_obj = param_dep.cls if param_dep.inst is None else param_dep.inst
        if dep_obj not in subobjs[:-1]:
            return None, None, param_dep.what

        depth = subobjs.index(dep_obj)
        callback = None
        if depth > 0:
            def callback(*events):
                """
                If a subobject changes, we need to notify the main
                object to update the dependencies.
                """
                obj.param._update_deps(attribute)

        p = '.'.join(dynamic_dep.spec.split(':')[0].split('.')[depth+1:])
        if p == 'param':
            subparams = [sp for sp in list(subobjs[-1].param)]
        else:
            subparams = [p]

        if ':' in dynamic_dep.spec:
            what = dynamic_dep.spec.split(':')[-1]
        else:
            what = param_dep.what

        return subparams, callback, what

    def _watch_group(self_, obj, name, queued, group, attribute=None):
        """
        Sets up a watcher for a group of dependencies. Ensures that
        if the dependency was dynamically generated we check whether
        a subobject change event actually causes a value change and
        that we update the existing watchers, i.e. clean up watchers
        on the old subobject and create watchers on the new subobject.
        """
        dynamic_dep, param_dep = group[0]
        dep_obj = param_dep.cls if param_dep.inst is None else param_dep.inst
        params = []
        for _, g in group:
            if g.name not in params:
                params.append(g.name)

        if dynamic_dep is None:
            subparams, callback, what = None, None, param_dep.what
        else:
            subparams, callback, what = self_._resolve_dynamic_deps(
                obj, dynamic_dep, param_dep, attribute)

        mcaller = _m_caller(obj, name, what, subparams, callback)
        return dep_obj.param._watch(
            mcaller, params, param_dep.what, queued=queued, precedence=-1)

    @_recursive_repr()
    def _repr_html_(self_, open=True):
        return _parameterized_repr_html(self_.self_or_cls, open)

    # Classmethods

    # PARAM3_DEPRECATION
    @_deprecated(extra_msg="""Use instead `for k,v in p.param.objects().items(): print(f"{p.__class__.name}.{k}={repr(v.default)}")`""")
    def print_param_defaults(self_):
        """Print the default values of all cls's Parameters.

        .. deprecated:: 1.12.0
            Use instead `for k,v in p.param.objects().items(): print(f"{p.__class__.name}.{k}={repr(v.default)}")`
        """
        cls = self_.cls
        for key,val in cls.__dict__.items():
            if isinstance(val,Parameter):
                print(cls.__name__+'.'+key+ '='+ repr(val.default))

    # PARAM3_DEPRECATION
    @_deprecated(extra_msg="Use instead `p.param.default =`")
    def set_default(self_,param_name,value):
        """
        Set the default value of param_name.

        Equivalent to setting param_name on the class.

        .. deprecated:: 1.12.0
             Use instead `p.param.default =`
        """
        cls = self_.cls
        setattr(cls,param_name,value)

    def add_parameter(self_, param_name, param_obj):
        """
        Add a new Parameter object into this object's class.

        Should result in a Parameter equivalent to one declared
        in the class's source code.
        """
        # Could have just done setattr(cls,param_name,param_obj),
        # which is supported by the metaclass's __setattr__ , but
        # would need to handle the params() cache as well
        # (which is tricky but important for startup speed).
        cls = self_.cls
        type.__setattr__(cls, param_name, param_obj)
        ParameterizedMetaclass._initialize_parameter(cls, param_name, param_obj)
        # delete cached params()
        cls._param__private.params.clear()

    # PARAM3_DEPRECATION
    @_deprecated(extra_msg="Use instead `.param.add_parameter`")
    def _add_parameter(self_,param_name, param_obj):
        """Add a new Parameter object into this object's class.

        .. deprecated :: 1.12.0
        """
        return self_.add_parameter(param_name, param_obj)

    # PARAM3_DEPRECATION
    @_deprecated(extra_msg="Use instead `.param.values()` or `.param['param']`")
    def params(self_, parameter_name=None):
        """
        Return the Parameters of this class as the
        dictionary {name: parameter_object}

        Includes Parameters from this class and its
        superclasses.

        .. deprecated:: 1.12.0
            Use instead `.param.values()` or `.param['param']`
        """
        pdict = self_.objects(instance='existing')
        if parameter_name is None:
            return pdict
        else:
            return pdict[parameter_name]

    # Bothmethods

    def update(self_, arg=Undefined, /, **kwargs):
        """
        For the given dictionary or iterable or set of param=value
        keyword arguments, sets the corresponding parameter of this
        object or class to the given value.

        May also be used as a context manager to temporarily set and
        then reset parameter values.
        """
        refs = {}
        if self_.self is not None:
            private = self_.self._param__private
            params = list(kwargs if arg is Undefined else dict(arg, **kwargs))
            for pname in params:
                if pname in refs:
                    continue
                elif pname in private.refs:
                    refs[pname] = private.refs[pname]
                elif pname in private.async_refs:
                    refs[pname] = private.async_refs[pname]
        restore = dict(self_._update(arg, **kwargs))
        return _ParametersRestorer(parameters=self_, restore=restore, refs=refs)

    def _update(self_, arg=Undefined, /, **kwargs):
        BATCH_WATCH = self_._BATCH_WATCH
        self_._BATCH_WATCH = True
        self_or_cls = self_.self_or_cls
        if arg is not Undefined:
            kwargs = dict(arg, **kwargs)

        trigger_params = [
            k for k in kwargs
            if k in self_ and hasattr(self_[k], '_autotrigger_value')
        ]

        for tp in trigger_params:
            self_[tp]._mode = 'set'

        values = self_.values()
        restore = {k: values[k] for k, v in kwargs.items() if k in values}

        for (k, v) in kwargs.items():
            if k not in self_:
                self_._BATCH_WATCH = False
                raise ValueError(f"{k!r} is not a parameter of {self_.cls.__name__}")
            try:
                setattr(self_or_cls, k, v)
            except:
                self_._BATCH_WATCH = False
                raise

        self_._BATCH_WATCH = BATCH_WATCH
        if not BATCH_WATCH:
            self_._batch_call_watchers()

        for tp in trigger_params:
            p = self_[tp]
            p._mode = 'reset'
            setattr(self_or_cls, tp, p._autotrigger_reset_value)
            p._mode = 'set-reset'
        return restore

    # PARAM3_DEPRECATION
    @_deprecated(extra_msg="Use instead `.param.update`")
    def set_param(self_, *args,**kwargs):
        """
        For each param=value keyword argument, sets the corresponding
        parameter of this object or class to the given value.

        For backwards compatibility, also accepts
        set_param("param",value) for a single parameter value using
        positional arguments, but the keyword interface is preferred
        because it is more compact and can set multiple values.

        .. deprecated:: 1.12.0
            Use instead `.param.update`
        """
        self_or_cls = self_.self_or_cls
        if args:
            if len(args) == 2 and not args[0] in kwargs and not kwargs:
                kwargs[args[0]] = args[1]
            else:
                raise ValueError("Invalid positional arguments for %s.set_param" %
                                 (self_or_cls.name))
        return self_.update(kwargs)

    @property
    def _cls_parameters(self_):
        """
        Class parameters are cached because they are accessed often,
        and parameters are rarely added (and cannot be deleted)
        """
        cls = self_.cls
        pdict = cls._param__private.params
        if pdict:
            return pdict

        paramdict = {}
        for class_ in classlist(cls):
            for name, val in class_.__dict__.items():
                if isinstance(val, Parameter):
                    paramdict[name] = val

        # We only want the cache to be visible to the cls on which
        # params() is called, so we mangle the name ourselves at
        # runtime (if we were to mangle it now, it would be
        # _Parameterized.__params for all classes).
        # cls._param__private.params[f'_{cls.__name__}__params'] = paramdict
        cls._param__private.params = paramdict
        return paramdict

    def objects(self_, instance=True):
        """
        Returns the Parameters of this instance or class

        If instance=True and called on a Parameterized instance it
        will create instance parameters for all Parameters defined on
        the class. To force class parameters to be returned use
        instance=False. Since classes avoid creating instance
        parameters unless necessary you may also request only existing
        instance parameters to be returned by setting
        instance='existing'.
        """
        if self_.self is not None and not self_.self._param__private.initialized and instance is True:
            warnings.warn(
                'Looking up instance Parameter objects (`.param.objects()`) until '
                'the Parameterized instance has been fully initialized is deprecated and will raise an error in a future version. '
                'Ensure you have called `super().__init__(**params)` in your Parameterized '
                'constructor before trying to access instance Parameter objects, or '
                'looking up the class Parameter objects with `.param.objects(instance=False)` '
                'may be enough for your use case.',
                category=_ParamFutureWarning,
                stacklevel=2,
            )

        pdict = self_._cls_parameters
        if instance and self_.self is not None:
            if instance == 'existing':
                if getattr(self_.self._param__private, 'initialized', False) and self_.self._param__private.params:
                    return dict(pdict, **self_.self._param__private.params)
                return pdict
            else:
                return {k: self_.self.param[k] for k in pdict}
        return pdict

    def trigger(self_, *param_names):
        """
        Trigger watchers for the given set of parameter names. Watchers
        will be triggered whether or not the parameter values have
        actually changed. As a special case, the value will actually be
        changed for a Parameter of type Event, setting it to True so
        that it is clear which Event parameter has been triggered.
        """
        if self_.self is not None and not self_.self._param__private.initialized:
            warnings.warn(
                'Triggering watchers on a partially initialized Parameterized instance '
                'is deprecated and will raise an error in a future version. '
                'Ensure you have called super().__init__(**params) in '
                'the Parameterized instance constructor before trying to set up a watcher.',
                category=_ParamFutureWarning,
                stacklevel=2,
            )

        trigger_params = [p for p in self_
                          if hasattr(self_[p], '_autotrigger_value')]
        triggers = {p:self_[p]._autotrigger_value
                    for p in trigger_params if p in param_names}

        events = self_._events
        watchers = self_._state_watchers
        self_._events  = []
        self_._state_watchers = []
        param_values = self_.values()
        params = {name: param_values[name] for name in param_names}
        self_._TRIGGER = True
        self_.update(dict(params, **triggers))
        self_._TRIGGER = False
        self_._events += events
        self_._state_watchers += watchers

    def _update_event_type(self_, watcher, event, triggered):
        """
        Returns an updated Event object with the type field set appropriately.
        """
        if triggered:
            event_type = 'triggered'
        else:
            event_type = 'changed' if watcher.onlychanged else 'set'
        return Event(what=event.what, name=event.name, obj=event.obj, cls=event.cls,
                     old=event.old, new=event.new, type=event_type)

    def _execute_watcher(self, watcher, events):
        if watcher.mode == 'args':
            args, kwargs = events, {}
        else:
            args, kwargs = (), {event.name: event.new for event in events}

        if iscoroutinefunction(watcher.fn):
            if async_executor is None:
                raise RuntimeError("Could not execute %s coroutine function. "
                                   "Please register a asynchronous executor on "
                                   "param.parameterized.async_executor, which "
                                   "schedules the function on an event loop." %
                                   watcher.fn)
            async_executor(partial(watcher.fn, *args, **kwargs))
        else:
            try:
                watcher.fn(*args, **kwargs)
            except Skip:
                pass

    def _call_watcher(self_, watcher, event):
        """
        Invoke the given watcher appropriately given an Event object.
        """
        if self_._TRIGGER:
            pass
        elif watcher.onlychanged and (not self_._changed(event)):
            return

        if self_._BATCH_WATCH:
            self_._events.append(event)
            if not any(watcher is w for w in self_._state_watchers):
                self_._state_watchers.append(watcher)
        else:
            event = self_._update_event_type(watcher, event, self_._TRIGGER)
            with _batch_call_watchers(self_.self_or_cls, enable=watcher.queued, run=False):
                self_._execute_watcher(watcher, (event,))

    def _batch_call_watchers(self_):
        """
        Batch call a set of watchers based on the parameter value
        settings in kwargs using the queued Event and watcher objects.
        """
        while self_._events:
            event_dict = OrderedDict([((event.name, event.what), event)
                                      for event in self_._events])
            watchers = self_._state_watchers[:]
            self_._events = []
            self_._state_watchers = []

            for watcher in sorted(watchers, key=lambda w: w.precedence):
                events = [self_._update_event_type(watcher, event_dict[(name, watcher.what)],
                                                   self_._TRIGGER)
                          for name in watcher.parameter_names
                          if (name, watcher.what) in event_dict]
                with _batch_call_watchers(self_.self_or_cls, enable=watcher.queued, run=False):
                    self_._execute_watcher(watcher, events)

    def set_dynamic_time_fn(self_,time_fn,sublistattr=None):
        """
        Set time_fn for all Dynamic Parameters of this class or
        instance object that are currently being dynamically
        generated.

        Additionally, sets _Dynamic_time_fn=time_fn on this class or
        instance object, so that any future changes to Dynamic
        Parmeters can inherit time_fn (e.g. if a Number is changed
        from a float to a number generator, the number generator will
        inherit time_fn).

        If specified, sublistattr is the name of an attribute of this
        class or instance that contains an iterable collection of
        subobjects on which set_dynamic_time_fn should be called.  If
        the attribute sublistattr is present on any of the subobjects,
        set_dynamic_time_fn() will be called for those, too.
        """
        self_or_cls = self_.self_or_cls
        self_or_cls._Dynamic_time_fn = time_fn

        if isinstance(self_or_cls,type):
            a = (None,self_or_cls)
        else:
            a = (self_or_cls,)

        for n,p in self_or_cls.param.objects('existing').items():
            if hasattr(p, '_value_is_dynamic'):
                if p._value_is_dynamic(*a):
                    g = self_or_cls.param.get_value_generator(n)
                    g._Dynamic_time_fn = time_fn

        if sublistattr:
            try:
                sublist = getattr(self_or_cls,sublistattr)
            except AttributeError:
                sublist = []

            for obj in sublist:
                obj.param.set_dynamic_time_fn(time_fn,sublistattr)

    def serialize_parameters(self_, subset=None, mode='json'):
        self_or_cls = self_.self_or_cls
        if mode not in Parameter._serializers:
            raise ValueError(f'Mode {mode!r} not in available serialization formats {list(Parameter._serializers.keys())!r}')
        serializer = Parameter._serializers[mode]
        return serializer.serialize_parameters(self_or_cls, subset=subset)

    def serialize_value(self_, pname, mode='json'):
        self_or_cls = self_.self_or_cls
        if mode not in Parameter._serializers:
            raise ValueError(f'Mode {mode!r} not in available serialization formats {list(Parameter._serializers.keys())!r}')
        serializer = Parameter._serializers[mode]
        return serializer.serialize_parameter_value(self_or_cls, pname)

    def deserialize_parameters(self_, serialization, subset=None, mode='json'):
        self_or_cls = self_.self_or_cls
        serializer = Parameter._serializers[mode]
        return serializer.deserialize_parameters(self_or_cls, serialization, subset=subset)

    def deserialize_value(self_, pname, value, mode='json'):
        self_or_cls = self_.self_or_cls
        if mode not in Parameter._serializers:
            raise ValueError(f'Mode {mode!r} not in available serialization formats {list(Parameter._serializers.keys())!r}')
        serializer = Parameter._serializers[mode]
        return serializer.deserialize_parameter_value(self_or_cls, pname, value)

    def schema(self_, safe=False, subset=None, mode='json'):
        """
        Returns a schema for the parameters on this Parameterized object.
        """
        self_or_cls = self_.self_or_cls
        if mode not in Parameter._serializers:
            raise ValueError(f'Mode {mode!r} not in available serialization formats {list(Parameter._serializers.keys())!r}')
        serializer = Parameter._serializers[mode]
        return serializer.schema(self_or_cls, safe=safe, subset=subset)

    # PARAM3_DEPRECATION
    # same as values() but returns list, not dict
    @_deprecated(extra_msg="""
        Use `.param.values().items()` instead (or `.param.values()` for the
        common case of `dict(....param.get_param_values())`)
    """)
    def get_param_values(self_, onlychanged=False):
        """
        Return a list of name,value pairs for all Parameters of this
        object.

        When called on an instance with onlychanged set to True, will
        only return values that are not equal to the default value
        (onlychanged has no effect when called on a class).

        .. deprecated:: 1.12.0
            Use `.param.values().items()` instead (or `.param.values()` for the
            common case of `dict(....param.get_param_values())`)
        """
        vals = self_.values(onlychanged)
        return [(k, v) for k, v in vals.items()]

    def values(self_, onlychanged=False):
        """
        Return a dictionary of name,value pairs for the Parameters of this
        object.

        When called on an instance with onlychanged set to True, will
        only return values that are not equal to the default value
        (onlychanged has no effect when called on a class).
        """
        self_or_cls = self_.self_or_cls
        vals = []
        for name, val in self_or_cls.param.objects('existing').items():
            value = self_or_cls.param.get_value_generator(name)
            if name == 'name' and onlychanged and _is_auto_name(self_.cls.__name__, value):
                continue
            if not onlychanged or not Comparator.is_equal(value, val.default):
                vals.append((name, value))

        vals.sort(key=itemgetter(0))
        return dict(vals)

    def force_new_dynamic_value(self_, name): # pylint: disable-msg=E0213
        """
        Force a new value to be generated for the dynamic attribute
        name, and return it.

        If name is not dynamic, its current value is returned
        (i.e. equivalent to getattr(name).
        """
        cls_or_slf = self_.self_or_cls
        param_obj = cls_or_slf.param.objects('existing').get(name)

        if not param_obj:
            return getattr(cls_or_slf, name)

        cls, slf = None, None
        if isinstance(cls_or_slf,type):
            cls = cls_or_slf
        else:
            slf = cls_or_slf

        if not hasattr(param_obj,'_force'):
            return param_obj.__get__(slf, cls)
        else:
            return param_obj._force(slf, cls)

    def get_value_generator(self_,name): # pylint: disable-msg=E0213
        """
        Return the value or value-generating object of the named
        attribute.

        For most parameters, this is simply the parameter's value
        (i.e. the same as getattr()), but Dynamic parameters have
        their value-generating object returned.
        """
        cls_or_slf = self_.self_or_cls
        param_obj = cls_or_slf.param.objects('existing').get(name)

        if not param_obj:
            value = getattr(cls_or_slf,name)

        # CompositeParameter detected by being a Parameter and having 'attribs'
        elif hasattr(param_obj,'attribs'):
            value = [cls_or_slf.param.get_value_generator(a) for a in param_obj.attribs]

        # not a Dynamic Parameter
        elif not hasattr(param_obj,'_value_is_dynamic'):
            value = getattr(cls_or_slf,name)

        # Dynamic Parameter...
        else:
            # TODO: is this always an instance?
            if isinstance(cls_or_slf, Parameterized) and name in cls_or_slf._param__private.values:
                # dealing with object and it's been set on this object
                value = cls_or_slf._param__private.values[name]
            else:
                # dealing with class or isn't set on the object
                value = param_obj.default

        return value

    def inspect_value(self_,name): # pylint: disable-msg=E0213
        """
        Return the current value of the named attribute without modifying it.

        Same as getattr() except for Dynamic parameters, which have their
        last generated value returned.
        """
        cls_or_slf = self_.self_or_cls
        param_obj = cls_or_slf.param.objects('existing').get(name)

        if not param_obj:
            value = getattr(cls_or_slf,name)
        elif hasattr(param_obj,'attribs'):
            value = [cls_or_slf.param.inspect_value(a) for a in param_obj.attribs]
        elif not hasattr(param_obj,'_inspect'):
            value = getattr(cls_or_slf,name)
        else:
            if isinstance(cls_or_slf,type):
                value = param_obj._inspect(None,cls_or_slf)
            else:
                value = param_obj._inspect(cls_or_slf,None)

        return value

    def method_dependencies(self_, name, intermediate=False):
        """
        Given the name of a method, returns a PInfo object for each dependency
        of this method. See help(PInfo) for the contents of these objects.

        By default intermediate dependencies on sub-objects are not
        returned as these are primarily useful for internal use to
        determine when a sub-object dependency has to be updated.
        """
        method = getattr(self_.self_or_cls, name)
        minfo = MInfo(cls=self_.cls, inst=self_.self, name=name,
                      method=method)
        deps, dynamic = _params_depended_on(
            minfo, dynamic=False, intermediate=intermediate)
        if self_.self is None:
            return deps
        return _resolve_mcs_deps(
            self_.self, deps, dynamic, intermediate=intermediate)

    # PARAM3_DEPRECATION
    @_deprecated(extra_msg='Use instead `.param.method_dependencies`')
    def params_depended_on(self_, *args, **kwargs):
        """
        Given the name of a method, returns a PInfo object for each dependency
        of this method. See help(PInfo) for the contents of these objects.

        By default intermediate dependencies on sub-objects are not
        returned as these are primarily useful for internal use to
        determine when a sub-object dependency has to be updated.

        .. deprecated: 2.0.0
            Use instead `.param.method_dependencies`
        """
        return self_.method_dependencies(*args, **kwargs)

    def outputs(self_):
        """
        Returns a mapping between any declared outputs and a tuple
        of the declared Parameter type, the output method, and the
        index into the output if multiple outputs are returned.
        """
        outputs = {}
        for cls in classlist(self_.cls):
            for name in dir(cls):
                if name == '_param_watchers':
                    continue
                method = getattr(self_.self_or_cls, name)
                dinfo = getattr(method, '_dinfo', {})
                if 'outputs' not in dinfo:
                    continue
                for override, otype, idx in dinfo['outputs']:
                    if override is not None:
                        name = override
                    outputs[name] = (otype, method, idx)
        return outputs

    def _spec_to_obj(self_, spec, dynamic=True, intermediate=True):
        """
        Resolves a dependency specification into lists of explicit
        parameter dependencies and dynamic dependencies.

        Dynamic dependencies are specifications to be resolved when
        the sub-object whose parameters are being depended on is
        defined.

        During class creation dynamic=False which means sub-object
        dependencies are not resolved. At instance creation and
        whenever a sub-object is set on an object this method will be
        invoked to determine whether the dependency is available.

        For sub-object dependencies we also return dependencies for
        every part of the path, e.g. for a dependency specification
        like "a.b.c" we return dependencies for sub-object "a" and the
        sub-sub-object "b" in addition to the dependency on the actual
        parameter "c" on object "b". This is to ensure that if a
        sub-object is swapped out we are notified and can update the
        dynamic dependency to the new object. Even if a sub-object
        dependency can only partially resolved, e.g. if object "a"
        does not yet have a sub-object "b" we must watch for changes
        to "b" on sub-object "a" in case such a subobject is put in "b".
        """
        if isinstance(spec, Parameter):
            inst = spec.owner if isinstance(spec.owner, Parameterized) else None
            cls = spec.owner if inst is None else type(inst)
            info = PInfo(inst=inst, cls=cls, name=spec.name,
                         pobj=spec, what='value')
            return [] if intermediate == 'only' else [info], []

        obj, attr, what = _parse_dependency_spec(spec)
        if obj is None:
            src = self_.self_or_cls
        elif not dynamic:
            return [], [DInfo(spec=spec)]
        else:
            if not hasattr(self_.self_or_cls, obj.split('.')[1]):
                raise AttributeError(
                    f'Dependency {obj[1:]!r} could not be resolved, {self_.self_or_cls} '
                    f'has no parameter or attribute {obj.split(".")[1]!r}. Ensure '
                    'the object being depended on is declared before calling the '
                    'Parameterized constructor.'
                )

            src = _getattrr(self_.self_or_cls, obj[1::], None)
            if src is None:
                path = obj[1:].split('.')
                deps = []
                # Attempt to partially resolve subobject path to ensure
                # that if a subobject is later updated making the full
                # subobject path available we have to be notified and
                # set up watchers
                if len(path) >= 1 and intermediate:
                    sub_src = None
                    subpath = path
                    while sub_src is None and subpath:
                        subpath = subpath[:-1]
                        sub_src = _getattrr(self_.self_or_cls, '.'.join(subpath), None)
                    if subpath:
                        subdeps, _ = self_._spec_to_obj(
                            '.'.join(path[:len(subpath)+1]), dynamic, intermediate)
                        deps += subdeps
                return deps, [] if intermediate == 'only' else [DInfo(spec=spec)]

        cls, inst = (src, None) if isinstance(src, type) else (type(src), src)
        if attr == 'param':
            deps, dynamic_deps = self_._spec_to_obj(obj[1:], dynamic, intermediate)
            for p in src.param:
                param_deps, param_dynamic_deps = src.param._spec_to_obj(p, dynamic, intermediate)
                deps += param_deps
                dynamic_deps += param_dynamic_deps
            return deps, dynamic_deps
        elif attr in src.param:
            info = PInfo(inst=inst, cls=cls, name=attr,
                         pobj=src.param[attr], what=what)
        elif hasattr(src, attr):
            attr_obj = getattr(src, attr)
            if isinstance(attr_obj, Parameterized):
                return [], []
            elif isinstance(attr_obj, (FunctionType, MethodType)):
                info = MInfo(inst=inst, cls=cls, name=attr,
                             method=attr_obj)
            else:
                raise AttributeError(f"Attribute {attr!r} could not be resolved on {src}.")
        elif getattr(src, "abstract", None):
            return [], [] if intermediate == 'only' else [DInfo(spec=spec)]
        else:
            raise AttributeError(f"Attribute {attr!r} could not be resolved on {src}.")

        if obj is None or not intermediate:
            return [info], []
        deps, dynamic_deps = self_._spec_to_obj(obj[1:], dynamic, intermediate)
        if intermediate != 'only':
            deps.append(info)
        return deps, dynamic_deps

    def _register_watcher(self_, action, watcher, what='value'):
        if self_.self is not None and not self_.self._param__private.initialized:
            warnings.warn(
                '(Un)registering a watcher on a partially initialized Parameterized instance '
                'is deprecated and will raise an error in a future version. Ensure '
                'you have called super().__init__(**) in the Parameterized instance '
                'constructor before trying to set up a watcher.',
                category=_ParamFutureWarning,
                stacklevel=4,
            )

        parameter_names = watcher.parameter_names
        for parameter_name in parameter_names:
            if parameter_name not in self_.cls.param:
                raise ValueError("{} parameter was not found in list of "
                                 "parameters of class {}".format(parameter_name, self_.cls.__name__))

            if self_.self is not None and what == "value":
                watchers = self_.self._param__private.watchers
                if parameter_name not in watchers:
                    watchers[parameter_name] = {}
                if what not in watchers[parameter_name]:
                    watchers[parameter_name][what] = []
                getattr(watchers[parameter_name][what], action)(watcher)
            else:
                watchers = self_[parameter_name].watchers
                if what not in watchers:
                    watchers[what] = []
                getattr(watchers[what], action)(watcher)

    def watch(self_, fn, parameter_names, what='value', onlychanged=True, queued=False, precedence=0):
        """
        Register the given callback function `fn` to be invoked for
        events on the indicated parameters.

        `what`: What to watch on each parameter; either the value (by
        default) or else the indicated slot (e.g. 'constant').

        `onlychanged`: By default, only invokes the function when the
        watched item changes, but if `onlychanged=False` also invokes
        it when the `what` item is set to its current value again.

        `queued`: By default, additional watcher events generated
        inside the callback fn are dispatched immediately, effectively
        doing depth-first processing of Watcher events. However, in
        certain scenarios, it is helpful to wait to dispatch such
        downstream events until all events that triggered this watcher
        have been processed. In such cases setting `queued=True` on
        this Watcher will queue up new downstream events generated
        during `fn` until `fn` completes and all other watchers
        invoked by that same event have finished executing),
        effectively doing breadth-first processing of Watcher events.

        `precedence`: Declares a precedence level for the Watcher that
        determines the priority with which the callback is executed.
        Lower precedence levels are executed earlier. Negative
        precedences are reserved for internal Watchers, i.e. those
        set up by param.depends.

        When the `fn` is called, it will be provided the relevant
        Event objects as positional arguments, which allows it to
        determine which of the possible triggering events occurred.

        Returns a Watcher object.

        See help(Watcher) and help(Event) for the contents of those objects.
        """
        if precedence < 0:
            raise ValueError("User-defined watch callbacks must declare "
                             "a positive precedence. Negative precedences "
                             "are reserved for internal Watchers.")
        return self_._watch(fn, parameter_names, what, onlychanged, queued, precedence)

    def _watch(self_, fn, parameter_names, what='value', onlychanged=True, queued=False, precedence=-1):
        parameter_names = tuple(parameter_names) if isinstance(parameter_names, list) else (parameter_names,)
        watcher = Watcher(inst=self_.self, cls=self_.cls, fn=fn, mode='args',
                          onlychanged=onlychanged, parameter_names=parameter_names,
                          what=what, queued=queued, precedence=precedence)
        self_._register_watcher('append', watcher, what)
        return watcher

    def unwatch(self_, watcher):
        """
        Remove the given Watcher object (from `watch` or `watch_values`) from this object's list.
        """
        try:
            self_._register_watcher('remove', watcher, what=watcher.what)
        except Exception:
            self_.warning(f'No such watcher {str(watcher)} to remove.')

    def watch_values(self_, fn, parameter_names, what='value', onlychanged=True, queued=False, precedence=0):
        """
        Easier-to-use version of `watch` specific to watching for changes in parameter values.

        Only allows `what` to be 'value', and invokes the callback `fn` using keyword
        arguments <param_name>=<new_value> rather than with a list of Event objects.
        """
        if precedence < 0:
            raise ValueError("User-defined watch callbacks must declare "
                             "a positive precedence. Negative precedences "
                             "are reserved for internal Watchers.")
        assert what == 'value'
        if isinstance(parameter_names, list):
            parameter_names = tuple(parameter_names)
        else:
            parameter_names = (parameter_names,)
        watcher = Watcher(inst=self_.self, cls=self_.cls, fn=fn,
                          mode='kwargs', onlychanged=onlychanged,
                          parameter_names=parameter_names, what=what,
                          queued=queued, precedence=precedence)
        self_._register_watcher('append', watcher, what)
        return watcher

    # Instance methods

    # PARAM3_DEPRECATION
    @_deprecated(extra_msg="Use instead `{k:v.default for k,v in p.param.objects().items()}`")
    def defaults(self_):
        """
        Return {parameter_name:parameter.default} for all non-constant
        Parameters.

        Note that a Parameter for which instantiate==True has its default
        instantiated.

        .. deprecated:: 1.12.0
            Use instead `{k:v.default for k,v in p.param.objects().items()}`
        """
        self = self_.self
        d = {}
        for param_name, param in self.param.objects('existing').items():
            if param.constant:
                pass
            if param.instantiate:
                self.param._instantiate_param(param, dict_=d, key=param_name)
            d[param_name] = param.default
        return d

    # Designed to avoid any processing unless the print
    # level is high enough, though not all callers of message(),
    # verbose(), debug(), etc are taking advantage of this.
    def __db_print(self_,level,msg,*args,**kw):
        """
        Calls the logger returned by the get_logger() function,
        prepending the result of calling dbprint_prefix() (if any).

        See python's logging module for details.
        """
        self_or_cls = self_.self_or_cls
        if get_logger(name=self_or_cls.name).isEnabledFor(level):

            if dbprint_prefix and callable(dbprint_prefix):
                msg = dbprint_prefix() + ": " + msg  # pylint: disable-msg=E1102

            get_logger(name=self_or_cls.name).log(level, msg, *args, **kw)

    # PARAM3_DEPRECATION
    @_deprecated(extra_msg="""Use instead `for k,v in p.param.objects().items(): print(f"{p.__class__.name}.{k}={repr(v.default)}")`""")
    def print_param_values(self_):
        """Print the values of all this object's Parameters.

        .. deprecated:: 1.12.0
            Use instead `for k,v in p.param.objects().items(): print(f"{p.__class__.name}.{k}={repr(v.default)}")`
        """
        self = self_.self
        for name, val in self.param.values().items():
            print(f'{self.name}.{name} = {val}')

    def warning(self_, msg,*args,**kw):
        """
        Print msg merged with args as a warning, unless module variable
        warnings_as_exceptions is True, then raise an Exception
        containing the arguments.

        See Python's logging module for details of message formatting.
        """
        self_.log(WARNING, msg, *args, **kw)

    # PARAM3_DEPRECATION
    @_deprecated(extra_msg="Use instead `.param.log(param.MESSAGE, ...)`")
    def message(self_,msg,*args,**kw):
        """
        Print msg merged with args as a message.

        See Python's logging module for details of message formatting.

        .. deprecated:: 1.12.0
            Use instead `.param.log(param.MESSAGE, ...)`
        """
        self_.__db_print(INFO,msg,*args,**kw)

    # PARAM3_DEPRECATION
    @_deprecated(extra_msg="Use instead `.param.log(param.VERBOSE, ...)`")
    def verbose(self_,msg,*args,**kw):
        """
        Print msg merged with args as a verbose message.

        See Python's logging module for details of message formatting.

        .. deprecated:: 1.12.0
            Use instead `.param.log(param.VERBOSE, ...)`
        """
        self_.__db_print(VERBOSE,msg,*args,**kw)

    # PARAM3_DEPRECATION
    @_deprecated(extra_msg="Use instead `.param.log(param.DEBUG, ...)`")
    def debug(self_,msg,*args,**kw):
        """
        Print msg merged with args as a debugging statement.

        See Python's logging module for details of message formatting.

        .. deprecated:: 1.12.0
            Use instead `.param.log(param.DEBUG, ...)`
        """
        self_.__db_print(DEBUG,msg,*args,**kw)

    def log(self_, level, msg, *args, **kw):
        """
        Print msg merged with args as a message at the indicated logging level.

        Logging levels include those provided by the Python logging module
        plus VERBOSE, either obtained directly from the logging module like
        `logging.INFO`, or from parameterized like `param.parameterized.INFO`.

        Supported logging levels include (in order of severity)
        DEBUG, VERBOSE, INFO, WARNING, ERROR, CRITICAL

        See Python's logging module for details of message formatting.
        """
        if level is WARNING:
            if warnings_as_exceptions:
                raise Exception("Warning: " + msg % args)
            else:
                global warning_count
                warning_count+=1
        self_.__db_print(level, msg, *args, **kw)

    # Note that there's no _state_push method on the class, so
    # dynamic parameters set on a class can't have state saved. This
    # is because, to do this, _state_push() would need to be a
    # @bothmethod, but that complicates inheritance in cases where we
    # already have a _state_push() method.
    # (isinstance(g,Parameterized) below is used to exclude classes.)

    def _state_push(self_):
        """
        Save this instance's state.

        For Parameterized instances, this includes the state of
        dynamically generated values.

        Subclasses that maintain short-term state should additionally
        save and restore that state using _state_push() and
        _state_pop().

        Generally, this method is used by operations that need to test
        something without permanently altering the objects' state.
        """
        self = self_.self_or_cls
        if not isinstance(self, Parameterized):
            raise NotImplementedError('_state_push is not implemented at the class level')
        for pname, p in self.param.objects('existing').items():
            g = self.param.get_value_generator(pname)
            if hasattr(g,'_Dynamic_last'):
                g._saved_Dynamic_last.append(g._Dynamic_last)
                g._saved_Dynamic_time.append(g._Dynamic_time)
                # CB: not storing the time_fn: assuming that doesn't
                # change.
            elif hasattr(g,'_state_push') and isinstance(g,Parameterized):
                g._state_push()

    def _state_pop(self_):
        """
        Restore the most recently saved state.

        See _state_push() for more details.
        """
        self = self_.self_or_cls
        if not isinstance(self, Parameterized):
            raise NotImplementedError('_state_pop is not implemented at the class level')
        for pname, p in self.param.objects('existing').items():
            g = self.param.get_value_generator(pname)
            if hasattr(g,'_Dynamic_last'):
                g._Dynamic_last = g._saved_Dynamic_last.pop()
                g._Dynamic_time = g._saved_Dynamic_time.pop()
            elif hasattr(g,'_state_pop') and isinstance(g,Parameterized):
                g._state_pop()

    def pprint(self_, imports=None, prefix=" ", unknown_value='<?>',
               qualify=False, separator=""):
        """
        (Experimental) Pretty printed representation that may be
        evaluated with eval. See pprint() function for more details.
        """
        self = self_.self_or_cls
        if not isinstance(self, Parameterized):
            raise NotImplementedError('pprint is not implemented at the class level')
        # Wrapping the staticmethod _pprint with partial to pass `self` as the `_recursive_repr`
        # decorator expects `self`` to be the pprinted object (not `self_`).
        return partial(self_._pprint, self, imports=imports, prefix=prefix,
                       unknown_value=unknown_value, qualify=qualify, separator=separator)()

    @staticmethod
    @_recursive_repr()
    def _pprint(self, imports=None, prefix=" ", unknown_value='<?>',
               qualify=False, separator=""):
        if imports is None:
            imports = [] # would have been simpler to use a set from the start
        imports[:] = list(set(imports))

        # Generate import statement
        mod = self.__module__
        bits = mod.split('.')
        imports.append("import %s"%mod)
        imports.append("import %s"%bits[0])

        changed_params = self.param.values(onlychanged=script_repr_suppress_defaults)
        values = self.param.values()
        spec = getfullargspec(type(self).__init__)
        if 'self' not in spec.args or spec.args[0] != 'self':
            raise KeyError(f"'{type(self).__name__}.__init__.__signature__' must contain 'self' as its first Parameter.")
        args = spec.args[1:]

        if spec.defaults is not None:
            posargs = spec.args[:-len(spec.defaults)]
            kwargs = dict(zip(spec.args[-len(spec.defaults):], spec.defaults))
        else:
            posargs, kwargs = args, []

        parameters = self.param.objects('existing')
        ordering = sorted(
            sorted(changed_params), # alphanumeric tie-breaker
            key=lambda k: (- float('inf')  # No precedence is lowest possible precendence
                           if parameters[k].precedence is None else
                           parameters[k].precedence))

        arglist, keywords, processed = [], [], []
        for k in args + ordering:
            if k in processed: continue

            # Suppresses automatically generated names.
            if k == 'name' and (values[k] is not None
                                and re.match('^'+self.__class__.__name__+'[0-9]+$', values[k])):
                continue

            value = pprint(values[k], imports, prefix=prefix,settings=[],
                           unknown_value=unknown_value,
                           qualify=qualify) if k in values else None

            if value is None:
                if unknown_value is False:
                    raise Exception(f"{self.name}: unknown value of {k!r}")
                elif unknown_value is None:
                    # i.e. suppress repr
                    continue
                else:
                    value = unknown_value

            # Explicit kwarg (unchanged, known value)
            if (k in kwargs) and (k in values) and kwargs[k] == values[k]: continue

            if k in posargs:
                # value will be unknown_value unless k is a parameter
                arglist.append(value)
            elif (k in kwargs or
                  (hasattr(spec, 'varkw') and (spec.varkw is not None)) or
                  (hasattr(spec, 'keywords') and (spec.keywords is not None))):
                # Explicit modified keywords or parameters in
                # precendence order (if **kwargs present)
                keywords.append(f'{k}={value}')

            processed.append(k)

        qualifier = mod + '.'  if qualify else ''
        arguments = arglist + keywords + (['**%s' % spec.varargs] if spec.varargs else [])
        return qualifier + '{}({})'.format(self.__class__.__name__,  (','+separator+prefix).join(arguments))


class ParameterizedMetaclass(type):
    """
    The metaclass of Parameterized (and all its descendents).

    The metaclass overrides type.__setattr__ to allow us to set
    Parameter values on classes without overwriting the attribute
    descriptor.  That is, for a Parameterized class of type X with a
    Parameter y, the user can type X.y=3, which sets the default value
    of Parameter y to be 3, rather than overwriting y with the
    constant value 3 (and thereby losing all other info about that
    Parameter, such as the doc string, bounds, etc.).

    The __init__ method is used when defining a Parameterized class,
    usually when the module where that class is located is imported
    for the first time.  That is, the __init__ in this metaclass
    initializes the *class* object, while the __init__ method defined
    in each Parameterized class is called for each new instance of
    that class.

    Additionally, a class can declare itself abstract by having an
    attribute __abstract set to True. The 'abstract' attribute can be
    used to find out if a class is abstract or not.
    """
    def __init__(mcs, name, bases, dict_):
        """
        Initialize the class object (not an instance of the class, but
        the class itself).

        Initializes all the Parameters by looking up appropriate
        default values (see __param_inheritance()) and setting
        attrib_names (see _set_names()).
        """
        type.__init__(mcs, name, bases, dict_)

        # Compute which parameters explicitly do not support references
        # This can be removed when Parameter.allow_refs=True by default.
        explicit_no_refs = set()
        for base in bases:
            if issubclass(base, Parameterized):
                explicit_no_refs |= set(base._param__private.explicit_no_refs)

        _param__private = _ClassPrivate(explicit_no_refs=list(explicit_no_refs))
        mcs._param__private = _param__private
        mcs.__set_name(name, dict_)
        mcs._param__parameters = Parameters(mcs)

        # All objects (with their names) of type Parameter that are
        # defined in this class
        parameters = [(n, o) for (n, o) in dict_.items()
                      if isinstance(o, Parameter)]

        for param_name,param in parameters:
            mcs._initialize_parameter(param_name, param)

        # retrieve depends info from methods and store more conveniently
        dependers = [(n, m, m._dinfo) for (n, m) in dict_.items()
                     if hasattr(m, '_dinfo')]

        # Resolve dependencies of current class
        _watch = []
        for name, method, dinfo in dependers:
            watch = dinfo.get('watch', False)
            on_init = dinfo.get('on_init', False)
            minfo = MInfo(cls=mcs, inst=None, name=name,
                          method=method)
            deps, dynamic_deps = _params_depended_on(minfo, dynamic=False)
            if watch:
                _watch.append((name, watch == 'queued', on_init, deps, dynamic_deps))

        # Resolve dependencies in class hierarchy
        _inherited = []
        for cls in classlist(mcs)[:-1][::-1]:
            if not hasattr(cls, '_param__parameters'):
                continue
            for dep in cls.param._depends['watch']:
                method = getattr(mcs, dep[0], None)
                dinfo = getattr(method, '_dinfo', {'watch': False})
                if (not any(dep[0] == w[0] for w in _watch+_inherited)
                    and dinfo.get('watch')):
                    _inherited.append(dep)

        mcs.param._depends = {'watch': _inherited+_watch}

        if docstring_signature:
            mcs.__class_docstring()

    def __set_name(mcs, name, dict_):
        """
        Give Parameterized classes a useful 'name' attribute that is by
        default the class name, unless a class in the hierarchy has defined
        a `name` String Parameter with a defined `default` value, in which case
        that value is used to set the class name.
        """
        name_param = dict_.get("name", None)
        if name_param is not None:
            if not type(name_param) is String:
                raise TypeError(
                    f"Parameterized class {name!r} cannot override "
                    f"the 'name' Parameter with type {type(name_param)}. "
                    "Overriding 'name' is only allowed with a 'String' Parameter."
                )
            if name_param.default:
                mcs.name = name_param.default
                mcs._param__private.renamed = True
            else:
                mcs.name = name
        else:
            classes = classlist(mcs)[::-1]
            found_renamed = False
            for c in classes:
                if hasattr(c, '_param__private') and c._param__private.renamed:
                    found_renamed = True
                    break
            if not found_renamed:
                mcs.name = name

    def __class_docstring(mcs):
        """
        Customize the class docstring with a Parameter table if
        `docstring_describe_params` and the `param_pager` is available.
        """
        if not docstring_describe_params or not param_pager:
            return
        class_docstr = mcs.__doc__ if mcs.__doc__ else ''
        description = param_pager(mcs)
        mcs.__doc__ = class_docstr + '\n' + description

    def _initialize_parameter(mcs, param_name, param):
        # A Parameter has no way to find out the name a
        # Parameterized class has for it
        param._set_names(param_name)
        mcs.__param_inheritance(param_name, param)

    # Should use the official Python 2.6+ abstract base classes; see
    # https://github.com/holoviz/param/issues/84
    def __is_abstract(mcs):
        """
        Return True if the class has an attribute __abstract set to True.
        Subclasses will return False unless they themselves have
        __abstract set to true.  This mechanism allows a class to
        declare itself to be abstract (e.g. to avoid it being offered
        as an option in a GUI), without the "abstract" property being
        inherited by its subclasses (at least one of which is
        presumably not abstract).
        """
        # Can't just do ".__abstract", because that is mangled to
        # _ParameterizedMetaclass__abstract before running, but
        # the actual class object will have an attribute
        # _ClassName__abstract.  So, we have to mangle it ourselves at
        # runtime. Mangling follows description in
        # https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references
        try:
            return getattr(mcs,'_%s__abstract'%mcs.__name__.lstrip("_"))
        except AttributeError:
            return False

    def __get_signature(mcs):
        """
        For classes with a constructor signature that matches the default
        Parameterized.__init__ signature (i.e. ``__init__(self, **params)``)
        this method will generate a new signature that expands the
        parameters. If the signature differs from the default the
        custom signature is returned.
        """
        if mcs._param__private.signature:
            return mcs._param__private.signature
        # allowed_signature must be the signature of Parameterized.__init__
        # Inspecting `mcs.__init__` instead of `mcs` to avoid a recursion error
        if inspect.signature(mcs.__init__) != DEFAULT_SIGNATURE:
            return None
        processed_kws, keyword_groups = set(), []
        for cls in reversed(mcs.mro()):
            keyword_group = []
            for k, v in sorted(cls.__dict__.items()):
                if isinstance(v, Parameter) and k not in processed_kws and not v.readonly:
                    keyword_group.append(k)
                    processed_kws.add(k)
            keyword_groups.append(keyword_group)

        keywords = [el for grp in reversed(keyword_groups) for el in grp]
        mcs._param__private.signature = signature = inspect.Signature([
            inspect.Parameter(k, inspect.Parameter.KEYWORD_ONLY)
            for k in keywords
        ])
        return signature

    __signature__ = property(__get_signature)

    abstract = property(__is_abstract)

    def _get_param(mcs):
        return mcs._param__parameters

    param = property(_get_param)

    def __setattr__(mcs, attribute_name, value):
        """
        Implements 'self.attribute_name=value' in a way that also supports Parameters.

        If there is already a descriptor named attribute_name, and
        that descriptor is a Parameter, and the new value is *not* a
        Parameter, then call that Parameter's __set__ method with the
        specified value.

        In all other cases set the attribute normally (i.e. overwrite
        the descriptor).  If the new value is a Parameter, once it has
        been set we make sure that the value is inherited from
        Parameterized superclasses as described in __param_inheritance().
        """
        # Find out if there's a Parameter called attribute_name as a
        # class attribute of this class - if not, parameter is None.
        parameter,owning_class = mcs.get_param_descriptor(attribute_name)

        if parameter and not isinstance(value,Parameter):
            if owning_class != mcs:
                parameter = copy.copy(parameter)
                parameter.owner = mcs
                type.__setattr__(mcs,attribute_name,parameter)
            mcs.__dict__[attribute_name].__set__(None,value)

        else:
            type.__setattr__(mcs,attribute_name,value)

            if isinstance(value,Parameter):
                mcs.__param_inheritance(attribute_name,value)

    def __param_inheritance(mcs, param_name, param):
        """
        Look for Parameter values in superclasses of this
        Parameterized class.

        Ordinarily, when a Python object is instantiated, attributes
        not given values in the constructor will inherit the value
        given in the object's class, or in its superclasses.  For
        Parameters owned by Parameterized classes, we have implemented
        an additional level of default lookup, should this ordinary
        lookup return only `Undefined`.

        In such a case, i.e. when no non-`Undefined` value was found for a
        Parameter by the usual inheritance mechanisms, we explicitly
        look for Parameters with the same name in superclasses of this
        Parameterized class, and use the first such value that we
        find.

        The goal is to be able to set the default value (or other
        slots) of a Parameter within a Parameterized class, just as we
        can set values for non-Parameter objects in Parameterized
        classes, and have the values inherited through the
        Parameterized hierarchy as usual.

        Note that instantiate is handled differently: if there is a
        parameter with the same name in one of the superclasses with
        instantiate set to True, this parameter will inherit
        instantiate=True.
        """
        # get all relevant slots (i.e. slots defined in all
        # superclasses of this parameter)
        p_type = type(param)
        slots = dict.fromkeys(p_type._all_slots_)

        # note for some eventual future: python 3.6+ descriptors grew
        # __set_name__, which could replace this and _set_names
        setattr(param, 'owner', mcs)
        del slots['owner']

        # backwards compatibility (see Composite parameter)
        if 'objtype' in slots:
            setattr(param, 'objtype', mcs)
            del slots['objtype']

        supers = classlist(mcs)[::-1]

        # Explicitly inherit instantiate from super class and
        # check if type has changed to a more specific or different
        # Parameter type, requiring extra validation
        type_change = False
        for superclass in supers:
            super_param = superclass.__dict__.get(param_name)
            if not isinstance(super_param, Parameter):
                continue
            if super_param.instantiate is True:
                param.instantiate = True
            super_type = type(super_param)
            if not issubclass(super_type, p_type):
                type_change = True
        del slots['instantiate']

        callables, slot_values = {}, {}
        slot_overridden = False
        for slot in slots.keys():
            # Search up the hierarchy until param.slot (which has to
            # be obtained using getattr(param,slot)) is not Undefined,
            # is a new value (using identity) or we run out of classes
            # to search.
            for scls in supers:
                # Class may not define parameter or slot might not be
                # there because could be a more general type of Parameter
                new_param = scls.__dict__.get(param_name)
                if new_param is None or not hasattr(new_param, slot):
                    continue

                new_value = getattr(new_param, slot)
                old_value = slot_values.get(slot, Undefined)
                if new_value is Undefined:
                    continue
                elif new_value is old_value:
                    continue
                elif old_value is Undefined:
                    slot_values[slot] = new_value
                    # If we already know we have to re-validate abort
                    # early to avoid costly lookups
                    if slot_overridden or type_change:
                        break
                else:
                    if slot not in param._non_validated_slots:
                        slot_overridden = True
                    break

            if slot_values.get(slot, Undefined) is Undefined:
                try:
                    default_val = param._slot_defaults[slot]
                except KeyError as e:
                    raise KeyError(
                        f'Slot {slot!r} of parameter {param_name!r} has no '
                        'default value defined in `_slot_defaults`'
                    ) from e
                if callable(default_val):
                    callables[slot] = default_val
                else:
                    slot_values[slot] = default_val
            elif slot == 'allow_refs':
                # Track Parameters that explicitly declared no refs
                explicit_no_refs = mcs._param__private.explicit_no_refs
                if param.allow_refs is False:
                    explicit_no_refs.append(param.name)
                elif param.allow_refs is True and param.name in explicit_no_refs:
                    explicit_no_refs.remove(param.name)

        # Now set the actual slot values
        for slot, value in slot_values.items():
            setattr(param, slot, value)

            # Avoid crosstalk between mutable slot values in different Parameter objects
            if slot != "default":
                v = getattr(param, slot)
                if _is_mutable_container(v):
                    setattr(param, slot, copy.copy(v))

        # Once all the static slots have been filled in, fill in the dynamic ones
        # (which are only allowed to use static values or results are undefined)
        for slot, fn in callables.items():
            setattr(param, slot, fn(param))

        # Once all the slot values have been set, call _update_state for Parameters
        # that need updates to make sure they're set up correctly after inheritance.
        param._update_state()

        # If the type has changed to a more specific or different type
        # or a slot value has been changed validate the default again.

        # Hack: Had to disable re-validation of None values because the
        # automatic appending of an unknown value on Selector opens a whole
        # rabbit hole in regard to the validation.
        if type_change or slot_overridden and param.default is not None:
            try:
                param._validate(param.default)
            # Param has no base validation exception class. Param Parameters raise
            # ValueError, TypeError, OSError exceptions but external Parameters
            # might raise other types of error, so we catch them all.
            except Exception as e:
                msg = f'{_validate_error_prefix(param)} failed to validate its ' \
                      'default value on class creation, this is going to raise ' \
                      'an error in the future. '
                parents = ', '.join(klass.__name__ for klass in mcs.__mro__[1:-2])
                if not type_change and slot_overridden:
                    msg += (
                        f'The Parameter is defined with attributes which when '
                        'combined with attributes inherited from its parent '
                        f'classes ({parents}) make it invalid. '
                        'Please fix the Parameter attributes.'
                    )
                elif type_change and not slot_overridden:
                    msg += (
                        f'The Parameter type changed between class {mcs.__name__!r} '
                        f'and one of its parent classes ({parents}) which '
                        f'made it invalid. Please fix the Parameter type.'
                    )
                else:
                    # type_change and slot_overriden is not possible as when
                    # the type changes checking the slots is aborted for
                    # performance reasons.
                    pass
                msg += f'\nValidation failed with:\n{e}'
                warnings.warn(
                    msg,
                    category=_ParamFutureWarning,
                    stacklevel=4,
                )

    def get_param_descriptor(mcs,param_name):
        """
        Goes up the class hierarchy (starting from the current class)
        looking for a Parameter class attribute param_name. As soon as
        one is found as a class attribute, that Parameter is returned
        along with the class in which it is declared.
        """
        classes = classlist(mcs)
        for c in classes[::-1]:
            attribute = c.__dict__.get(param_name)
            if isinstance(attribute,Parameter):
                return attribute,c
        return None,None



# Whether script_repr should avoid reporting the values of parameters
# that are just inheriting their values from the class defaults.
# Because deepcopying creates a new object, cannot detect such
# inheritance when instantiate = True, so such values will be printed
# even if they are just being copied from the default.
script_repr_suppress_defaults=True


def script_repr(val, imports=None, prefix="\n    ", settings=[],
        qualify=True, unknown_value=None, separator="\n",
        show_imports=True):
    """
    Variant of pprint() designed for generating a (nearly) runnable script.

    The output of script_repr(parameterized_obj) is meant to be a
    string suitable for running using `python file.py`. Not every
    object is guaranteed to have a runnable script_repr
    representation, but it is meant to be a good starting point for
    generating a Python script that (after minor edits) can be
    evaluated to get a newly initialized object similar to the one
    provided.

    The new object will only have the same parameter state, not the
    same internal (attribute) state; the script_repr captures only
    the state of the Parameters of that object and not any other
    attributes it may have.

    If show_imports is True (default), includes import statements
    for each of the modules required for the objects being
    instantiated. This list may not be complete, as it typically
    includes only the imports needed for the Parameterized object
    itself, not for values that may have been supplied to Parameters.

    Apart from show_imports, accepts the same arguments as pprint(),
    so see pprint() for explanations of the arguments accepted. The
    default values of each of these arguments differ from pprint() in
    ways that are more suitable for saving as a separate script than
    for e.g. pretty-printing at the Python prompt.
    """

    if imports is None:
        imports = []

    rep = pprint(val, imports, prefix, settings, unknown_value,
                 qualify, separator)

    imports = list(set(imports))
    imports_str = ("\n".join(imports) + "\n\n") if show_imports else ""

    return imports_str + rep


# PARAM2_DEPRECATION: Remove entirely unused settings argument
def pprint(val,imports=None, prefix="\n    ", settings=[],
           unknown_value='<?>', qualify=False, separator=''):
    """
    Pretty printed representation of a parameterized
    object that may be evaluated with eval.

    Similar to repr except introspection of the constructor (__init__)
    ensures a valid and succinct representation is generated.

    Only parameters are represented (whether specified as standard,
    positional, or keyword arguments). Parameters specified as
    positional arguments are always shown, followed by modified
    parameters specified as keyword arguments, sorted by precedence.

    unknown_value determines what to do where a representation cannot be
    generated for something required to recreate the object. Such things
    include non-parameter positional and keyword arguments, and certain
    values of parameters (e.g. some random state objects).

    Supplying an unknown_value of None causes unrepresentable things
    to be silently ignored. If unknown_value is a string, that
    string will appear in place of any unrepresentable things. If
    unknown_value is False, an Exception will be raised if an
    unrepresentable value is encountered.

    If supplied, imports should be a list, and it will be populated
    with the set of imports required for the object and all of its
    parameter values.

    If qualify is True, the class's path will be included (e.g. "a.b.C()"),
    otherwise only the class will appear ("C()").

    Parameters will be separated by a comma only by default, but the
    separator parameter allows an additional separator to be supplied
    (e.g. a newline could be supplied to have each Parameter appear on a
    separate line).

    Instances of types that require special handling can use the
    script_repr_reg dictionary. Using the type as a key, add a
    function that returns a suitable representation of instances of
    that type, and adds the required import statement. The repr of a
    parameter can be suppressed by returning None from the appropriate
    hook in script_repr_reg.
    """

    if imports is None:
        imports = []

    if isinstance(val,type):
        rep = type_script_repr(val,imports,prefix,settings)

    elif type(val) in script_repr_reg:
        rep = script_repr_reg[type(val)](val,imports,prefix,settings)

    elif isinstance(val, Parameterized) or (type(val) is type and issubclass(val, Parameterized)):
        rep=val.param.pprint(imports=imports, prefix=prefix+"    ",
                        qualify=qualify, unknown_value=unknown_value,
                        separator=separator)
    else:
        rep=repr(val)

    return rep


# Registry for special handling for certain types in script_repr and pprint
script_repr_reg = {}


# currently only handles list and tuple
def container_script_repr(container,imports,prefix,settings):
    result=[]
    for i in container:
        result.append(pprint(i,imports,prefix,settings))

    ## (hack to get container brackets)
    if isinstance(container,list):
        d1,d2='[',']'
    elif isinstance(container,tuple):
        d1,d2='(',')'
    else:
        raise NotImplementedError
    rep=d1+','.join(result)+d2

    # no imports to add for built-in types

    return rep


def empty_script_repr(*args): # pyflakes:ignore (unused arguments):
    return None

try:
    # Suppress scriptrepr for objects not yet having a useful string representation
    import numpy
    script_repr_reg[random.Random] = empty_script_repr
    script_repr_reg[numpy.random.RandomState] = empty_script_repr

except ImportError:
    pass # Support added only if those libraries are available


def function_script_repr(fn,imports,prefix,settings):
    name = fn.__name__
    module = fn.__module__
    imports.append('import %s'%module)
    return module+'.'+name

def type_script_repr(type_,imports,prefix,settings):
    module = type_.__module__
    if module!='__builtin__':
        imports.append('import %s'%module)
    return module+'.'+type_.__name__

script_repr_reg[list] = container_script_repr
script_repr_reg[tuple] = container_script_repr
script_repr_reg[FunctionType] = function_script_repr


#: If not None, the value of this Parameter will be called (using '()')
#: before every call to __db_print, and is expected to evaluate to a
#: string that is suitable for prefixing messages and warnings (such
#: as some indicator of the global state).
dbprint_prefix=None


def truncate(str_, maxlen = 30):
    """Return HTML-safe truncated version of given string"""
    rep = (str_[:(maxlen-2)] + '..') if (len(str_) > (maxlen-2)) else str_
    return html.escape(rep)


def _get_param_repr(key, val, p, vallen=30, doclen=40):
    """HTML representation for a single Parameter object and its value"""
    if isinstance(val, Parameterized) or (type(val) is type and issubclass(val, Parameterized)):
        value = val.param._repr_html_(open=False)
    elif hasattr(val, "_repr_html_"):
        value = val._repr_html_()
    else:
        value = truncate(repr(val), vallen)

    if hasattr(p, 'bounds'):
        if p.bounds is None:
            range_ = ''
        elif hasattr(p,'inclusive_bounds'):
            # Numeric bounds use ( and [ to indicate exclusive and inclusive
            bl,bu = p.bounds
            il,iu = p.inclusive_bounds

            lb = '' if bl is None else ('>=' if il else '>') + str(bl)
            ub = '' if bu is None else ('<=' if iu else '<') + str(bu)
            range_ = lb + (', ' if lb and bu else '') + ub
        else:
            range_ = repr(p.bounds)
    elif hasattr(p, 'objects') and p.objects:
        range_ = ', '.join(list(map(repr, p.objects)))
    elif hasattr(p, 'class_'):
        if isinstance(p.class_, tuple):
            range_ = ' | '.join(kls.__name__ for kls in p.class_)
        else:
            range_ = p.class_.__name__
    elif hasattr(p, 'regex') and p.regex is not None:
        range_ = f'regex({p.regex})'
    else:
        range_ = ''

    if p.readonly:
        range_ = ' '.join(s for s in ['<i>read-only</i>', range_] if s)
    elif p.constant:
        range_ = ' '.join(s for s in ['<i>constant</i>', range_] if s)

    if getattr(p, 'allow_None', False):
        range_ = ' '.join(s for s in ['<i>nullable</i>', range_] if s)

    tooltip = f' class="param-doc-tooltip" data-tooltip="{escape(p.doc.strip())}"' if p.doc else ''

    return (
        f'<tr>'
        f'  <td><p style="margin-bottom: 0px;"{tooltip}>{key}</p></td>'
        f'  <td style="max-width: 200px; text-align:left;">{value}</td>'
        f'  <td style="text-align:left;">{p.__class__.__name__}</td>'
        f'  <td style="max-width: 300px;">{range_}</td>'
        f'</tr>\n'
    )


def _parameterized_repr_html(p, open):
    """HTML representation for a Parameterized object"""
    if isinstance(p, Parameterized):
        cls = p.__class__
        title = cls.name + "()"
        value_field = 'Value'
    else:
        cls = p
        title = cls.name
        value_field = 'Default'

    tooltip_css = """
.param-doc-tooltip{
  position: relative;
  cursor: help;
}
.param-doc-tooltip:hover:after{
  content: attr(data-tooltip);
  background-color: black;
  color: #fff;
  border-radius: 3px;
  padding: 10px;
  position: absolute;
  z-index: 1;
  top: -5px;
  left: 100%;
  margin-left: 10px;
  min-width: 250px;
}
.param-doc-tooltip:hover:before {
  content: "";
  position: absolute;
  top: 50%;
  left: 100%;
  margin-top: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: transparent black transparent transparent;
}
"""
    openstr = " open" if open else ""
    param_values = p.param.values().items()
    contents = "".join(_get_param_repr(key, val, p.param[key])
                       for key, val in param_values)
    return (
        f'<style>{tooltip_css}</style>\n'
        f'<details {openstr}>\n'
        ' <summary style="display:list-item; outline:none;">\n'
        f'  <tt>{title}</tt>\n'
        ' </summary>\n'
        ' <div style="padding-left:10px; padding-bottom:5px;">\n'
        '  <table style="max-width:100%; border:1px solid #AAAAAA;">\n'
        f'   <tr><th style="text-align:left;">Name</th><th style="text-align:left;">{value_field}</th><th style="text-align:left;">Type</th><th>Range</th></tr>\n'
        f'{contents}\n'
        '  </table>\n </div>\n</details>\n'
    )

# _ClassPrivate and _InstancePrivate are the private namespaces of Parameterized
# classes and instance respectively, stored on the `_param__private` attribute.
# They are implemented with slots for performance reasons.

class _ClassPrivate:
    """
    parameters_state: dict
        Dict holding some transient states
    disable_instance_params: bool
        Whether to disable instance parameters
    renamed: bool
        Whethe the class has been renamed by a super class
    params: dict
        Dict of parameter_name:parameter
    """

    __slots__ = [
        'parameters_state',
        'disable_instance_params',
        'renamed',
        'params',
        'initialized',
        'signature',
        'explicit_no_refs',
    ]

    def __init__(
        self,
        parameters_state=None,
        disable_instance_params=False,
        explicit_no_refs=None,
        renamed=False,
        params=None,
    ):
        if parameters_state is None:
            parameters_state = {
                "BATCH_WATCH": False, # If true, Event and watcher objects are queued.
                "TRIGGER": False,
                "events": [], # Queue of batched events
                "watchers": [] # Queue of batched watchers
            }
        self.parameters_state = parameters_state
        self.disable_instance_params = disable_instance_params
        self.renamed = renamed
        self.params = {} if params is None else params
        self.initialized = False
        self.signature = None
        self.explicit_no_refs = [] if explicit_no_refs is None else explicit_no_refs

    def __getstate__(self):
        return {slot: getattr(self, slot) for slot in self.__slots__}

    def __setstate__(self, state):
        for k, v in state.items():
            setattr(self, k, v)


class _InstancePrivate:
    """
    initialized: bool
        Flag that can be tested to see if e.g. constant Parameters can still be set
    parameters_state: dict
        Dict holding some transient states
    dynamic_watchers: defaultdict
        Dynamic watchers
    ref_watchers: list[Watcher]
        Watchers used for internal references
    params: dict
        Dict of parameter_name:parameter
    refs: dict
        Dict of parameter name:reference
    watchers: dict
        Dict of dict:
            parameter_name:
                parameter_attribute (e.g. 'value'): list of `Watcher`s
    values: dict
        Dict of parameter name: value
    """

    __slots__ = [
        'initialized',
        'parameters_state',
        'dynamic_watchers',
        'params',
        'async_refs',
        'refs',
        'ref_watchers',
        'syncing',
        'watchers',
        'values',
        'explicit_no_refs',
    ]

    def __init__(
        self,
        initialized=False,
        parameters_state=None,
        dynamic_watchers=None,
        refs=None,
        params=None,
        watchers=None,
        values=None,
        explicit_no_refs=None
    ):
        self.initialized = initialized
        self.explicit_no_refs = [] if explicit_no_refs is None else explicit_no_refs
        self.syncing = set()
        if parameters_state is None:
            parameters_state = {
                "BATCH_WATCH": False, # If true, Event and watcher objects are queued.
                "TRIGGER": False,
                "events": [], # Queue of batched events
                "watchers": [] # Queue of batched watchers
            }
        self.ref_watchers = []
        self.async_refs = {}
        self.parameters_state = parameters_state
        self.dynamic_watchers = defaultdict(list) if dynamic_watchers is None else dynamic_watchers
        self.params = {} if params is None else params
        self.refs = {} if refs is None else refs
        self.watchers = {} if watchers is None else watchers
        self.values = {} if values is None else values

    def __getstate__(self):
        return {slot: getattr(self, slot) for slot in self.__slots__}

    def __setstate__(self, state):
        for k, v in state.items():
            setattr(self, k, v)


class Parameterized(metaclass=ParameterizedMetaclass):
    """
    Base class for named objects that support Parameters and message
    formatting.

    Automatic object naming: Every Parameterized instance has a name
    parameter.  If the user doesn't designate a name=<str> argument
    when constructing the object, the object will be given a name
    consisting of its class name followed by a unique 5-digit number.

    Automatic parameter setting: The Parameterized __init__ method
    will automatically read the list of keyword parameters.  If any
    keyword matches the name of a Parameter (see Parameter class)
    defined in the object's class or any of its superclasses, that
    parameter in the instance will get the value given as a keyword
    argument.  For example:

      class Foo(Parameterized):
         xx = Parameter(default=1)

      foo = Foo(xx=20)

    in this case foo.xx gets the value 20.

    When initializing a Parameterized instance ('foo' in the example
    above), the values of parameters can be supplied as keyword
    arguments to the constructor (using parametername=parametervalue);
    these values will override the class default values for this one
    instance.

    If no 'name' parameter is supplied, self.name defaults to the
    object's class name with a unique number appended to it.

    Message formatting: Each Parameterized instance has several
    methods for optionally printing output. This functionality is
    based on the standard Python 'logging' module; using the methods
    provided here, wraps calls to the 'logging' module's root logger
    and prepends each message with information about the instance
    from which the call was made. For more information on how to set
    the global logging level and change the default message prefix,
    see documentation for the 'logging' module.
    """

    name = String(default=None, constant=True, doc="""
        String identifier for this object.""")

    def __init__(self, **params):
        global object_count

        # Setting a Parameter value in an __init__ block before calling
        # Parameterized.__init__ (via super() generally) already sets the
        # _InstancePrivate namespace over the _ClassPrivate namespace
        # (see Parameter.__set__) so we shouldn't override it here.
        if not isinstance(self._param__private, _InstancePrivate):
            self._param__private = _InstancePrivate(
                explicit_no_refs=type(self)._param__private.explicit_no_refs
            )

        # Skip generating a custom instance name when a class in the hierarchy
        # has overriden the default of the `name` Parameter.
        if self.param.name.default == self.__class__.__name__:
            self.param._generate_name()
        refs, deps = self.param._setup_params(**params)
        object_count += 1

        self._param__private.initialized = True

        self.param._setup_refs(deps)
        self.param._update_deps(init=True)
        self._param__private.refs = refs

    @property
    def param(self):
        return Parameters(self.__class__, self=self)

    #PARAM3_DEPRECATION
    @property
    @_deprecated(extra_msg="Use `inst.param.watchers` instead.", warning_cat=_ParamFutureWarning)
    def _param_watchers(self):
        return self._param__private.watchers

    #PARAM3_DEPRECATION
    @_param_watchers.setter
    @_deprecated(extra_msg="Use `inst.param.watchers = ...` instead.", warning_cat=_ParamFutureWarning)
    def _param_watchers(self, value):
        self._param__private.watchers = value

    # 'Special' methods

    def __getstate__(self):
        """
        Save the object's state: return a dictionary that is a shallow
        copy of the object's __dict__ and that also includes the
        object's __slots__ (if it has any).
        """
        # Unclear why this is a copy and not simply state.update(self.__dict__)
        state = self.__dict__.copy()
        for slot in get_occupied_slots(self):
            state[slot] = getattr(self,slot)

        # Note that Parameterized object pickling assumes that
        # attributes to be saved are only in __dict__ or __slots__
        # (the standard Python places to store attributes, so that's a
        # reasonable assumption). (Additionally, class attributes that
        # are Parameters are also handled, even when they haven't been
        # instantiated - see PickleableClassAttributes.)

        return state

    def __setstate__(self, state):
        """
        Restore objects from the state dictionary to this object.

        During this process the object is considered uninitialized.
        """
        explicit_no_refs = type(self)._param__private.explicit_no_refs
        self._param__private = _InstancePrivate(explicit_no_refs=explicit_no_refs)
        self._param__private.initialized = False

        _param__private = state.get('_param__private', None)
        if _param__private is None:
            _param__private = _InstancePrivate(explicit_no_refs=explicit_no_refs)

        # When making a copy the internal watchers have to be
        # recreated and point to the new instance
        if _param__private.watchers:
            param_watchers = _param__private.watchers
            for p, attrs in param_watchers.items():
                for attr, watchers in attrs.items():
                    new_watchers = []
                    for watcher in watchers:
                        watcher_args = list(watcher)
                        if watcher.inst is not None:
                            watcher_args[0] = self
                        fn = watcher.fn
                        if hasattr(fn, '_watcher_name'):
                            watcher_args[2] = _m_caller(self, fn._watcher_name)
                        elif get_method_owner(fn) is watcher.inst:
                            watcher_args[2] = getattr(self, fn.__name__)
                        new_watchers.append(Watcher(*watcher_args))
                    param_watchers[p][attr] = new_watchers

        state.pop('param', None)

        for name,value in state.items():
            setattr(self,name,value)
        self._param__private.initialized = True

    @_recursive_repr()
    def __repr__(self):
        """
        Provide a nearly valid Python representation that could be used to recreate
        the item with its parameters, if executed in the appropriate environment.

        Returns 'classname(parameter1=x,parameter2=y,...)', listing
        all the parameters of this object.
        """
        try:
            settings = [f'{name}={val!r}'
                        for name, val in self.param.values().items()]
        except RuntimeError: # Handle recursion in parameter depth
            settings = []
        return self.__class__.__name__ + "(" + ", ".join(settings) + ")"

    def __str__(self):
        """Return a short representation of the name and class of this object."""
        return f"<{self.__class__.__name__} {self.name}>"


def print_all_param_defaults():
    """Print the default values for all imported Parameters."""
    print("_______________________________________________________________________________")
    print("")
    print("                           Parameter Default Values")
    print("")
    classes = descendents(Parameterized)
    classes.sort(key=lambda x:x.__name__)
    for c in classes:
        c.print_param_defaults()
    print("_______________________________________________________________________________")



# As of Python 2.6+, a fn's **args no longer has to be a
# dictionary. This might allow us to use a decorator to simplify using
# ParamOverrides (if that does indeed make them simpler to use).
# http://docs.python.org/whatsnew/2.6.html
class ParamOverrides(dict):
    """
    A dictionary that returns the attribute of a specified object if
    that attribute is not present in itself.

    Used to override the parameters of an object.
    """

    # NOTE: Attribute names of this object block parameters of the
    # same name, so all attributes of this object should have names
    # starting with an underscore (_).

    def __init__(self,overridden,dict_,allow_extra_keywords=False):
        """

        If allow_extra_keywords is False, then all keys in the
        supplied dict_ must match parameter names on the overridden
        object (otherwise a warning will be printed).

        If allow_extra_keywords is True, then any items in the
        supplied dict_ that are not also parameters of the overridden
        object will be available via the extra_keywords() method.
        """
        # This method should be fast because it's going to be
        # called a lot. This _might_ be faster (not tested):
        #  def __init__(self,overridden,**kw):
        #      ...
        #      dict.__init__(self,**kw)
        self._overridden = overridden
        dict.__init__(self,dict_)

        if allow_extra_keywords:
            self._extra_keywords=self._extract_extra_keywords(dict_)
        else:
            self._check_params(dict_)

    def extra_keywords(self):
        """
        Return a dictionary containing items from the originally
        supplied `dict_` whose names are not parameters of the
        overridden object.
        """
        return self._extra_keywords

    def param_keywords(self):
        """
        Return a dictionary containing items from the originally
        supplied `dict_` whose names are parameters of the
        overridden object (i.e. not extra keywords/parameters).
        """
        return {key: self[key] for key in self if key not in self.extra_keywords()}

    def __missing__(self,name):
        # Return 'name' from the overridden object
        return getattr(self._overridden,name)

    def __repr__(self):
        # As dict.__repr__, but indicate the overridden object
        return dict.__repr__(self)+" overriding params from %s"%repr(self._overridden)

    def __getattr__(self,name):
        # Provide 'dot' access to entries in the dictionary.
        # (This __getattr__ method is called only if 'name' isn't an
        # attribute of self.)
        return self.__getitem__(name)

    def __setattr__(self,name,val):
        # Attributes whose name starts with _ are set on self (as
        # normal), but all other attributes are inserted into the
        # dictionary.
        if not name.startswith('_'):
            self.__setitem__(name,val)
        else:
            dict.__setattr__(self,name,val)

    def get(self, key, default=None):
        try:
            return self[key]
        except KeyError:
            return default

    def __contains__(self, key):
        return key in self.__dict__ or key in self._overridden.param

    def _check_params(self,params):
        """
        Print a warning if params contains something that is not a
        Parameter of the overridden object.
        """
        overridden_object_params = list(self._overridden.param)
        for item in params:
            if item not in overridden_object_params:
                self.param.warning("'%s' will be ignored (not a Parameter).",item)

    def _extract_extra_keywords(self,params):
        """
        Return any items in params that are not also
        parameters of the overridden object.
        """
        extra_keywords = {}
        overridden_object_params = list(self._overridden.param)
        for name, val in params.items():
            if name not in overridden_object_params:
                extra_keywords[name]=val
                # Could remove name from params (i.e. del params[name])
                # so that it's only available via extra_keywords()
        return extra_keywords


# Helper function required by ParameterizedFunction.__reduce__
def _new_parameterized(cls):
    return Parameterized.__new__(cls)


class ParameterizedFunction(Parameterized):
    """
    Acts like a Python function, but with arguments that are Parameters.

    Implemented as a subclass of Parameterized that, when instantiated,
    automatically invokes __call__ and returns the result, instead of
    returning an instance of the class.

    To obtain an instance of this class, call instance().
    """
    __abstract = True

    def __str__(self):
        return self.__class__.__name__+"()"

    @bothmethod
    def instance(self_or_cls,**params):
        """
        Return an instance of this class, copying parameters from any
        existing instance provided.
        """

        if isinstance (self_or_cls,ParameterizedMetaclass):
            cls = self_or_cls
        else:
            p = params
            params = self_or_cls.param.values()
            params.update(p)
            params.pop('name')
            cls = self_or_cls.__class__

        inst=Parameterized.__new__(cls)
        Parameterized.__init__(inst,**params)
        if 'name' in params:  inst.__name__ = params['name']
        else:                 inst.__name__ = self_or_cls.name
        return inst

    def __new__(class_,*args,**params):
        # Create and __call__() an instance of this class.
        inst = class_.instance()
        inst.param._set_name(class_.__name__)
        return inst.__call__(*args,**params)

    def __call__(self,*args,**kw):
        raise NotImplementedError("Subclasses must implement __call__.")

    def __reduce__(self):
        # Control reconstruction (during unpickling and copying):
        # ensure that ParameterizedFunction.__new__ is skipped
        state = ParameterizedFunction.__getstate__(self)
        # Here it's necessary to use a function defined at the
        # module level rather than Parameterized.__new__ directly
        # because otherwise pickle will find .__new__'s module to be
        # __main__. Pretty obscure aspect of pickle.py...
        return (_new_parameterized,(self.__class__,),state)

    def _pprint(self, imports=None, prefix="\n    ",unknown_value='<?>',
                qualify=False, separator=""):
        """
        Same as self.param.pprint, except that X.classname(Y
        is replaced with X.classname.instance(Y
        """
        r = self.param.pprint(imports,prefix,
                              unknown_value=unknown_value,
                              qualify=qualify,separator=separator)
        classname=self.__class__.__name__
        return r.replace(".%s("%classname,".%s.instance("%classname)


class default_label_formatter(ParameterizedFunction):
    "Default formatter to turn parameter names into appropriate widget labels."

    capitalize = Parameter(default=True, doc="""
        Whether or not the label should be capitalized.""")

    replace_underscores = Parameter(default=True, doc="""
        Whether or not underscores should be replaced with spaces.""")

    overrides = Parameter(default={}, doc="""
        Allows custom labels to be specified for specific parameter
        names using a dictionary where key is the parameter name and the
        value is the desired label.""")

    def __call__(self, pname):
        if pname in self.overrides:
            return self.overrides[pname]
        if self.replace_underscores:
            pname = pname.replace('_',' ')
        if self.capitalize:
            pname = pname[:1].upper() + pname[1:]
        return pname


label_formatter = default_label_formatter


# PARAM3_DEPRECATION: Should be able to remove this; was originally
# adapted from OProperty from
# infinitesque.net/articles/2005/enhancing%20Python's%20property.xhtml
# but since python 2.6 the getter, setter, and deleter attributes of
# a property should provide similar functionality already.
class overridable_property:
    """
    The same as Python's "property" attribute, but allows the accessor
    methods to be overridden in subclasses.

    .. deprecated:: 2.0.0
    """
    # Delays looking up the accessors until they're needed, rather
    # than finding them when the class is first created.

    # Based on the emulation of PyProperty_Type() in Objects/descrobject.c

    def __init__(self, fget=None, fset=None, fdel=None, doc=None):
        warnings.warn(
            message="overridable_property has been deprecated.",
            category=_ParamDeprecationWarning,
            stacklevel=2,
        )
        self.fget = fget
        self.fset = fset
        self.fdel = fdel
        self.__doc__ = doc

    def __get__(self, obj, objtype=None):
        if obj is None:
            return self
        if self.fget is None:
            raise AttributeError("unreadable attribute")
        if self.fget.__name__ == '<lambda>' or not self.fget.__name__:
            return self.fget(obj)
        else:
            return getattr(obj, self.fget.__name__)()

    def __set__(self, obj, value):
        if self.fset is None:
            raise AttributeError("can't set attribute")
        if self.fset.__name__ == '<lambda>' or not self.fset.__name__:
            self.fset(obj, value)
        else:
            getattr(obj, self.fset.__name__)(value)

    def __delete__(self, obj):
        if self.fdel is None:
            raise AttributeError("can't delete attribute")
        if self.fdel.__name__ == '<lambda>' or not self.fdel.__name__:
            self.fdel(obj)
        else:
            getattr(obj, self.fdel.__name__)()