Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

r""" 

Cached Functions and Methods 

  

AUTHORS: 

  

- William Stein: initial version, (inspired by conversation with Justin Walker) 

- Mike Hansen: added doctests and made it work with class methods. 

- Willem Jan Palenstijn: add CachedMethodCaller for binding cached methods to 

instances. 

- Tom Boothby: added DiskCachedFunction. 

- Simon King: improved performance, more doctests, cython version, 

CachedMethodCallerNoArgs, weak cached function, cached special methods. 

- Julian Rueth (2014-03-19, 2014-05-09, 2014-05-12): added ``key`` parameter, allow caching 

for unhashable elements, added ``do_pickle`` parameter 

  

EXAMPLES: 

  

By :trac:`11115`, cached functions and methods are now also 

available in Cython code. The following examples cover various ways 

of usage. 

  

Python functions:: 

  

sage: @cached_function 

....: def test_pfunc(x): 

....: ''' 

....: Some documentation 

....: ''' 

....: return -x 

sage: test_pfunc(5) is test_pfunc(5) 

True 

  

In some cases, one would only want to keep the result in cache as long 

as there is any other reference to the result. By :trac:`12215`, this is 

enabled for :class:`~sage.structure.unique_representation.UniqueRepresentation`, 

which is used to create unique parents: If an algebraic structure, such 

as a finite field, is only temporarily used, then it will not stay in 

cache forever. That behaviour is implemented using ``weak_cached_function``, 

that behaves the same as ``cached_function``, except that it uses a 

:class:`~sage.misc.weak_dict.WeakValueDictionary` for storing the results. 

:: 

  

sage: from sage.misc.cachefunc import weak_cached_function 

sage: class A: pass 

sage: @weak_cached_function 

....: def f(): 

....: print("doing a computation") 

....: return A() 

sage: a = f() 

doing a computation 

  

The result is cached:: 

  

sage: b = f() 

sage: a is b 

True 

  

However, if there are no strong references left, the result 

may be garbage collected, and thus a new computation would 

take place:: 

  

sage: del a 

sage: del b 

sage: import gc 

sage: n = gc.collect() 

sage: a = f() 

doing a computation 

  

Cython cdef functions do not allow arbitrary decorators. 

However, one can wrap a Cython function and turn it into 

a cached function, by :trac:`11115`. We need to provide 

the name that the wrapped method or function should have, 

since otherwise the name of the original function would 

be used:: 

  

sage: cython('''cpdef test_funct(x): return -x''') 

sage: wrapped_funct = cached_function(test_funct, name='wrapped_funct') 

sage: wrapped_funct 

Cached version of <built-in function test_funct> 

sage: wrapped_funct.__name__ 

'wrapped_funct' 

sage: wrapped_funct(5) 

-5 

sage: wrapped_funct(5) is wrapped_funct(5) 

True 

  

We can proceed similarly for cached methods of Cython classes, 

provided that they allow attribute assignment or have a public 

attribute ``__cached_methods`` of type ``<dict>``. Since 

:trac:`11115`, this is the case for all classes inheriting from 

:class:`~sage.structure.parent.Parent`. See below for a more explicit 

example. By :trac:`12951`, cached methods of extension classes can 

be defined by simply using the decorator. However, an indirect 

approach is still needed for cpdef methods:: 

  

sage: cython_code = ['cpdef test_meth(self,x):', 

....: ' "some doc for a wrapped cython method"', 

....: ' return -x', 

....: 'from sage.all import cached_method', 

....: 'from sage.structure.parent cimport Parent', 

....: 'cdef class MyClass(Parent):', 

....: ' @cached_method', 

....: ' def direct_method(self, x):', 

....: ' "Some doc for direct method"', 

....: ' return 2*x', 

....: ' wrapped_method = cached_method(test_meth,name="wrapped_method")'] 

sage: cython(os.linesep.join(cython_code)) 

sage: O = MyClass() 

sage: O.direct_method 

Cached version of <method 'direct_method' of '...MyClass' objects> 

sage: O.wrapped_method 

Cached version of <built-in function test_meth> 

sage: O.wrapped_method.__name__ 

'wrapped_method' 

sage: O.wrapped_method(5) 

-5 

sage: O.wrapped_method(5) is O.wrapped_method(5) 

True 

sage: O.direct_method(5) 

10 

sage: O.direct_method(5) is O.direct_method(5) 

True 

  

In some cases, one would only want to keep the result in cache as long 

as there is any other reference to the result. By :trac:`12215`, this is 

enabled for :class:`~sage.structure.unique_representation.UniqueRepresentation`, 

which is used to create unique parents: If an algebraic structure, such 

as a finite field, is only temporarily used, then it will not stay in 

cache forever. That behaviour is implemented using ``weak_cached_function``, 

that behaves the same as ``cached_function``, except that it uses a 

:class:`~sage.misc.weak_dict.WeakValueDictionary` for storing the results. 

:: 

  

sage: from sage.misc.cachefunc import weak_cached_function 

sage: class A: pass 

sage: @weak_cached_function 

....: def f(): 

....: print("doing a computation") 

....: return A() 

sage: a = f() 

doing a computation 

  

The result is cached:: 

  

sage: b = f() 

sage: a is b 

True 

  

However, if there are no strong references left, the result 

may be garbage collected, and thus a new computation would 

take place:: 

  

sage: del a 

sage: del b 

sage: import gc 

sage: n = gc.collect() 

sage: a = f() 

doing a computation 

  

By :trac:`11115`, even if a parent does not allow attribute 

assignment, it can inherit a cached method from the parent class of a 

category (previously, the cache would have been broken):: 

  

sage: cython_code = ["from sage.all import cached_method, cached_in_parent_method, Category, Objects", 

....: "class MyCategory(Category):", 

....: " @cached_method", 

....: " def super_categories(self):", 

....: " return [Objects()]", 

....: " class ElementMethods:", 

....: " @cached_method", 

....: " def element_cache_test(self):", 

....: " return -self", 

....: " @cached_in_parent_method", 

....: " def element_via_parent_test(self):", 

....: " return -self", 

....: " class ParentMethods:", 

....: " @cached_method", 

....: " def one(self):", 

....: " return self.element_class(self,1)", 

....: " @cached_method", 

....: " def invert(self, x):", 

....: " return -x"] 

sage: cython('\n'.join(cython_code)) 

sage: C = MyCategory() 

  

In order to keep the memory footprint of elements small, it was 

decided to not support the same freedom of using cached methods 

for elements: If an instance of a class derived from 

:class:`~sage.structure.element.Element` does not allow attribute 

assignment, then a cached method inherited from the category of 

its parent will break, as in the class ``MyBrokenElement`` below. 

  

However, there is a class :class:`~sage.structure.element.ElementWithCachedMethod` 

that has generally a slower attribute access, but fully supports 

cached methods. We remark, however, that cached methods are 

*much* faster if attribute access works. So, we expect that 

:class:`~sage.structure.element.ElementWithCachedMethod` will 

hardly by used. 

:: 

  

sage: cython_code = ["from sage.structure.element cimport Element, ElementWithCachedMethod", "from cpython.object cimport PyObject_RichCompare", 

....: "cdef class MyBrokenElement(Element):", 

....: " cdef public object x", 

....: " def __init__(self,P,x):", 

....: " self.x=x", 

....: " Element.__init__(self,P)", 

....: " def __neg__(self):", 

....: " return MyBrokenElement(self.parent(),-self.x)", 

....: " def _repr_(self):", 

....: " return '<%s>'%self.x", 

....: " def __hash__(self):", 

....: " return hash(self.x)", 

....: " cpdef _richcmp_(left, right, int op):", 

....: " return PyObject_RichCompare(left.x, right.x, op)", 

....: " def raw_test(self):", 

....: " return -self", 

....: "cdef class MyElement(ElementWithCachedMethod):", 

....: " cdef public object x", 

....: " def __init__(self,P,x):", 

....: " self.x=x", 

....: " ElementWithCachedMethod.__init__(self,P)", 

....: " def __neg__(self):", 

....: " return MyElement(self.parent(),-self.x)", 

....: " def _repr_(self):", 

....: " return '<%s>'%self.x", 

....: " def __hash__(self):", 

....: " return hash(self.x)", 

....: " cpdef _richcmp_(left, right, int op):", 

....: " return PyObject_RichCompare(left.x, right.x, op)", 

....: " def raw_test(self):", 

....: " return -self", 

....: "from sage.structure.parent cimport Parent", 

....: "cdef class MyParent(Parent):", 

....: " Element = MyElement"] 

sage: cython('\n'.join(cython_code)) 

sage: P = MyParent(category=C) 

sage: ebroken = MyBrokenElement(P,5) 

sage: e = MyElement(P,5) 

  

The cached methods inherited by the parent works:: 

  

sage: P.one() 

<1> 

sage: P.one() is P.one() 

True 

sage: P.invert(e) 

<-5> 

sage: P.invert(e) is P.invert(e) 

True 

  

The cached methods inherited by ``MyElement`` works:: 

  

sage: e.element_cache_test() 

<-5> 

sage: e.element_cache_test() is e.element_cache_test() 

True 

sage: e.element_via_parent_test() 

<-5> 

sage: e.element_via_parent_test() is e.element_via_parent_test() 

True 

  

The other element class can only inherit a ``cached_in_parent_method``, since 

the cache is stored in the parent. In fact, equal elements share the cache, 

even if they are of different types:: 

  

sage: e == ebroken 

True 

sage: type(e) == type(ebroken) 

False 

sage: ebroken.element_via_parent_test() is e.element_via_parent_test() 

True 

  

However, the cache of the other inherited method breaks, although the method 

as such works:: 

  

sage: ebroken.element_cache_test() 

<-5> 

sage: ebroken.element_cache_test() is ebroken.element_cache_test() 

False 

  

The cache can be emptied:: 

  

sage: a = test_pfunc(5) 

sage: test_pfunc.clear_cache() 

sage: a is test_pfunc(5) 

False 

sage: a = P.one() 

sage: P.one.clear_cache() 

sage: a is P.one() 

False 

  

Since ``e`` and ``ebroken`` share the cache, when we empty it for one element 

it is empty for the other as well:: 

  

sage: b = ebroken.element_via_parent_test() 

sage: e.element_via_parent_test.clear_cache() 

sage: b is ebroken.element_via_parent_test() 

False 

  

Introspection works:: 

  

sage: from sage.misc.edit_module import file_and_line 

sage: from sage.misc.sageinspect import sage_getdoc, sage_getfile, sage_getsource 

sage: print(sage_getdoc(test_pfunc)) 

Some documentation 

sage: print(sage_getdoc(O.wrapped_method)) 

some doc for a wrapped cython method 

<BLANKLINE> 

sage: print(sage_getdoc(O.direct_method)) 

Some doc for direct method 

<BLANKLINE> 

sage: print(sage_getsource(O.wrapped_method)) 

cpdef test_meth(self,x): 

"some doc for a wrapped cython method" 

return -x 

sage: print(sage_getsource(O.direct_method)) 

def direct_method(self, x): 

"Some doc for direct method" 

return 2*x 

  

It is a very common special case to cache a method that has no 

arguments. In that special case, the time needed to access the cache 

can be drastically reduced by using a special implementation. The 

cached method decorator automatically determines which implementation 

ought to be chosen. A typical example is 

:meth:`sage.rings.polynomial.multi_polynomial_ideal.MPolynomialIdeal.gens` 

(no arguments) versus 

:meth:`sage.rings.polynomial.multi_polynomial_ideal.MPolynomialIdeal.groebner_basis` 

(several arguments):: 

  

sage: P.<a,b,c,d> = QQ[] 

sage: I = P*[a,b] 

sage: I.gens() 

[a, b] 

sage: I.gens() is I.gens() 

True 

sage: I.groebner_basis() 

[a, b] 

sage: I.groebner_basis() is I.groebner_basis() 

True 

sage: type(I.gens) 

<type 'sage.misc.cachefunc.CachedMethodCallerNoArgs'> 

sage: type(I.groebner_basis) 

<type 'sage.misc.cachefunc.CachedMethodCaller'> 

  

By :trac:`12951`, the cached_method decorator is also supported on non-c(p)def 

methods of extension classes, as long as they either support attribute assignment 

or have a public attribute of type ``<dict>`` called ``__cached_methods``. The 

latter is easy:: 

  

sage: cython_code = [ 

....: "from sage.misc.cachefunc import cached_method", 

....: "cdef class MyClass:", 

....: " cdef public dict __cached_methods", 

....: " @cached_method", 

....: " def f(self, a,b):", 

....: " return a*b"] 

sage: cython(os.linesep.join(cython_code)) 

sage: P = MyClass() 

sage: P.f(2,3) 

6 

sage: P.f(2,3) is P.f(2,3) 

True 

  

Providing attribute access is a bit more tricky, since it is needed that 

an attribute inherited by the instance from its class can be overridden 

on the instance. That is why providing a ``__getattr__`` would not be 

enough in the following example:: 

  

sage: cython_code = [ 

....: "from sage.misc.cachefunc import cached_method", 

....: "cdef class MyOtherClass:", 

....: " cdef dict D", 

....: " def __init__(self):", 

....: " self.D = {}", 

....: " def __setattr__(self, n,v):", 

....: " self.D[n] = v", 

....: " def __getattribute__(self, n):", 

....: " try:", 

....: " return self.D[n]", 

....: " except KeyError:", 

....: " pass", 

....: " return getattr(type(self),n).__get__(self)", 

....: " @cached_method", 

....: " def f(self, a,b):", 

....: " return a+b"] 

sage: cython(os.linesep.join(cython_code)) 

sage: Q = MyOtherClass() 

sage: Q.f(2,3) 

5 

sage: Q.f(2,3) is Q.f(2,3) 

True 

  

Note that supporting attribute access is somehow faster than the 

easier method:: 

  

sage: timeit("a = P.f(2,3)") # random 

625 loops, best of 3: 1.3 µs per loop 

sage: timeit("a = Q.f(2,3)") # random 

625 loops, best of 3: 931 ns per loop 

  

Some immutable objects (such as `p`-adic numbers) cannot implement a 

reasonable hash function because their ``==`` operator has been 

modified to return ``True`` for objects which might behave differently 

in some computations:: 

  

sage: K.<a> = Qq(9) 

sage: b = a.add_bigoh(1) 

sage: c = a + 3 

sage: b 

a + O(3) 

sage: c 

a + 3 + O(3^20) 

sage: b == c 

True 

sage: b == a 

True 

sage: c == a 

False 

  

If such objects defined a non-trivial hash function, this would break 

caching in many places. However, such objects should still be usable 

in caches. This can be achieved by defining an appropriate method 

``_cache_key``:: 

  

sage: hash(b) 

Traceback (most recent call last): 

... 

TypeError: unhashable type: 'sage.rings.padics.qadic_flint_CR.qAdicCappedRelativeElement' 

sage: @cached_method 

....: def f(x): return x == a 

sage: f(b) 

True 

sage: f(c) # if b and c were hashable, this would return True 

False 

  

sage: b._cache_key() 

(..., ((0, 1),), 0, 1) 

sage: c._cache_key() 

(..., ((0, 1), (1,)), 0, 20) 

  

.. NOTE:: 

  

This attribute will only be accessed if the object itself 

is not hashable. 

  

An implementation must make sure that for elements ``a`` and ``b``, 

if ``a != b``, then also ``a._cache_key() != b._cache_key()``. 

In practice this means that the ``_cache_key`` should always include 

the parent as its first argument:: 

  

sage: S.<a> = Qq(4) 

sage: d = a.add_bigoh(1) 

sage: b._cache_key() == d._cache_key() # this would be True if the parents were not included 

False 

""" 

  

#***************************************************************************** 

# Copyright (C) 2008 William Stein <wstein@gmail.com> 

# Mike Hansen <mhansen@gmail.com> 

# 2011 Simon King <simon.king@uni-jena.de> 

# 2014 Julian Rueth <julian.rueth@fsfe.org> 

# 2015 Jeroen Demeyer <jdemeyer@cage.ugent.be> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from __future__ import print_function, absolute_import 

  

from cpython cimport PyObject 

  

cdef extern from "methodobject.h": 

cdef int METH_NOARGS, METH_O 

cdef int PyCFunction_GetFlags(object op) except -1 

  

import os 

from os.path import relpath,normpath,commonprefix 

from sage.misc.sageinspect import sage_getfile, sage_getsourcelines, sage_getargspec 

from inspect import isfunction 

  

from sage.misc.weak_dict cimport WeakValueDictionary 

from sage.misc.decorators import decorator_keywords 

  

cdef frozenset special_method_names = frozenset(['__abs__', '__add__', 

'__and__', '__call__', '__cmp__', '__coerce__', '__complex__', '__contains__', '__del__', 

'__delattr__', '__delete__', '__delitem__', '__delslice__', '__dir__', '__div__', 

'__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__get__', '__getattr__', 

'__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__hex__', 

'__iadd__', '__iand__', '__idiv__', '__ifloordiv__', '__ilshift__', '__imod__', '__imul__', 

'__index__', '__init__', '__instancecheck__', '__int__', '__invert__', '__ior__', '__ipow__', 

'__irshift__', '__isub__', '__iter__', '__itruediv__', '__ixor__', '__le__', '__len__', 

'__length_hint__', '__long__', '__lshift__', '__lt__', '__missing__', '__mod__', '__mul__', 

'__ne__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', 

'__radd__', '__rand__', '__rdiv__', '__repr__', '__reversed__', '__rfloordiv__', '__rlshift__', 

'__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', 

'__rtruediv__', '__rxor__', '__set__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', 

'__str__', '__sub__', '__subclasscheck__', '__truediv__', '__unicode__', '__xor__', 'next']) 

  

def _cached_function_unpickle(module, name, cache=None): 

""" 

Unpickle the cache function ``name`` defined in ``module``. 

  

This function loads ``name`` from ``module`` (it does not restore the code 

of the actual function when it was pickled.) The cache is restored from 

``cache`` if present. 

  

INPUT: 

  

- ``module`` -- the name of the module to import the function from. 

- ``name`` -- the name of the cached function. 

- ``cache`` -- a list of cached key value pairs. 

  

TESTS:: 

  

sage: type(cunningham_prime_factors) 

<type 'sage.misc.cachefunc.CachedFunction'> 

sage: loads(dumps(cunningham_prime_factors)) is cunningham_prime_factors #indirect doctest 

True 

  

Verify that the ``cache`` parameter works:: 

  

sage: @cached_function(do_pickle=True) 

....: def f(n): return n 

sage: import __main__ 

sage: __main__.f = f 

sage: f(0) 

0 

sage: ((0,),()) in f.cache 

True 

  

sage: s = dumps(f) 

sage: f.clear_cache() 

sage: ((0,),()) in f.cache 

False 

sage: f = loads(s) 

sage: ((0,),()) in f.cache 

True 

sage: f(0) 

0 

  

""" 

ret = getattr(__import__(module, fromlist=['']),name) 

if cache is not None: 

ret.cache.update(cache) 

return ret 

  

cdef class NonpicklingDict(dict): 

r""" 

A special dict which does not pickle its contents. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import NonpicklingDict 

sage: d = NonpicklingDict() 

sage: d[0] = 0 

sage: loads(dumps(d)) 

{} 

  

""" 

def __reduce__(self): 

r""" 

Return data required to unpickle this dictionary. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import NonpicklingDict 

sage: d = NonpicklingDict() 

sage: d[0] = 0 

sage: d.__reduce__() 

(<type 'sage.misc.cachefunc.NonpicklingDict'>, ()) 

  

""" 

return NonpicklingDict, () 

  

  

cdef unhashable_key = object() 

  

cpdef inline dict_key(o): 

""" 

Return a key to cache object ``o`` in a dict. 

  

This is different from ``cache_key`` since the ``cache_key`` might 

get confused with the key of a hashable object. Therefore, such keys 

include ``unhashable_key`` which acts as a unique marker which is 

certainly not stored in the dictionary otherwise. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import dict_key 

sage: dict_key(42) 

42 

sage: K.<u> = Qq(9) 

sage: dict_key(u) 

(<object object at ...>, (..., 20)) 

""" 

try: 

hash(o) 

except TypeError: 

o = (unhashable_key, cache_key_unhashable(o)) 

return o 

  

  

cpdef inline cache_key(o): 

r""" 

Helper function to return a hashable key for ``o`` which can be used for 

caching. 

  

This function is intended for objects which are not hashable such as 

`p`-adic numbers. The difference from calling an object's ``_cache_key`` 

method directly, is that it also works for tuples and unpacks them 

recursively (if necessary, i.e., if they are not hashable). 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import cache_key 

sage: K.<u> = Qq(9) 

sage: a = K(1); a 

1 + O(3^20) 

sage: cache_key(a) 

(..., ((1,),), 0, 20) 

  

This function works if ``o`` is a tuple. In this case it unpacks its 

entries recursively:: 

  

sage: o = (1, 2, (3, a)) 

sage: cache_key(o) 

(1, 2, (3, (..., ((1,),), 0, 20))) 

  

Note that tuples are only partially unpacked if some of its entries are 

hashable:: 

  

sage: o = (1/2, a) 

sage: cache_key(o) 

(1/2, (..., ((1,),), 0, 20)) 

""" 

try: 

hash(o) 

except TypeError: 

o = cache_key_unhashable(o) 

return o 

  

  

cdef cache_key_unhashable(o): 

""" 

Return a key for caching an item which is unhashable. 

""" 

if isinstance(o, tuple): 

return tuple(cache_key(item) for item in o) 

try: 

k = o._cache_key() 

except AttributeError: 

raise TypeError("unhashable type: {!r}".format(type(o).__name__)) 

return cache_key(k) 

  

  

cdef class CachedFunction(object): 

""" 

Create a cached version of a function, which only recomputes 

values it hasn't already computed. Synonyme: ``cached_function`` 

  

INPUT: 

  

- ``f`` -- a function 

- ``name`` -- (optional string) name that the cached version 

of ``f`` should be provided with 

- ``key`` -- (optional callable) takes the input and returns a 

key for the cache, typically one would use this to normalize input 

- ``do_pickle`` -- (optional boolean) whether or not the contents of the 

cache should be included when pickling this function; the default is not 

to include them. 

  

If ``f`` is a function, do either ``g = CachedFunction(f)`` 

or ``g = cached_function(f)`` to make a cached version of ``f``, 

or put ``@cached_function`` right before the definition of ``f`` 

(i.e., use Python decorators):: 

  

@cached_function 

def f(...): 

.... 

  

The inputs to the function must be hashable or they must define 

:meth:`sage.structure.sage_object.SageObject._cache_key`. 

  

EXAMPLES:: 

  

sage: @cached_function 

....: def mul(x, y=2): 

....: return x*y 

sage: mul(3) 

6 

  

We demonstrate that the result is cached, and that, moreover, 

the cache takes into account the various ways of providing 

default arguments:: 

  

sage: mul(3) is mul(3,2) 

True 

sage: mul(3,y=2) is mul(3,2) 

True 

  

The user can clear the cache:: 

  

sage: a = mul(4) 

sage: mul.clear_cache() 

sage: a is mul(4) 

False 

  

It is also possible to explicitly override the cache with 

a different value:: 

  

sage: mul.set_cache('foo',5) 

sage: mul(5,2) 

'foo' 

  

The parameter ``key`` can be used to ignore parameters for 

caching. In this example we ignore the parameter ``algorithm``:: 

  

sage: @cached_function(key=lambda x,y,algorithm: (x,y)) 

....: def mul(x, y, algorithm="default"): 

....: return x*y 

sage: mul(1,1,algorithm="default") is mul(1,1,algorithm="algorithm") is mul(1,1) is mul(1,1,'default') 

True 

""" 

def __init__(self, f, *, classmethod=False, name=None, key=None, do_pickle=None): 

""" 

Create a cached version of a function, which only recomputes 

values it hasn't already computed. A custom name can be 

provided by an optional argument "name". 

  

If ``f`` is a function, do either ``g = CachedFunction(f)`` 

to make a cached version of ``f``, or put ``@CachedFunction`` 

right before the definition of ``f`` (i.e., use Python decorators):: 

  

@CachedFunction 

def f(...): 

.... 

  

The inputs to the function must be hashable or they must define 

:meth:`sage.structure.sage_object.SageObject._cache_key`. 

  

TESTS:: 

  

sage: g = CachedFunction(number_of_partitions) 

sage: g.__name__ 

'number_of_partitions' 

sage: 'partitions' in sage.misc.sageinspect.sage_getdoc(g) 

True 

sage: g(5) 

7 

sage: g.cache 

{((5, 'default'), ()): 7} 

sage: def f(t=1): print(t) 

sage: h = CachedFunction(f) 

sage: w = walltime() 

sage: h(); h(1); h(t=1) 

1 

sage: walltime(w) < 2 

True 

  

By default, the contents of the cache are not pickled:: 

  

sage: @cached_function 

....: def f(n): return None 

sage: import __main__ 

sage: __main__.f = f 

sage: for i in range(100): f(i) 

sage: len(f.cache) 

100 

  

sage: s = dumps(f) 

sage: f.clear_cache() 

sage: f = loads(s) 

sage: len(f.cache) 

0 

  

If ``do_pickle`` is set, then the cache is pickled:: 

  

sage: @cached_function(do_pickle=True) 

....: def f(n): return None 

sage: __main__.f = f 

sage: for i in range(100): f(i) 

sage: len(f.cache) 

100 

  

sage: s = dumps(f) 

sage: f.clear_cache() 

sage: f = loads(s) 

sage: len(f.cache) 

100 

  

""" 

self.is_classmethod = classmethod 

self._common_init(f, None, name=name, key=key, do_pickle=do_pickle) 

self.cache = {} if do_pickle else NonpicklingDict() 

  

def _common_init(self, f, argument_fixer, name=None, key=None, do_pickle=None): 

""" 

Perform initialization common to CachedFunction and CachedMethodCaller. 

  

TESTS:: 

  

sage: @cached_function 

....: def test_cache(x): 

....: return -x 

sage: test_cache.__name__ # indirect doctest 

'test_cache' 

""" 

self.f = f 

self.key = key 

self.do_pickle = do_pickle 

if name is not None: 

self.__name__ = name 

else: 

self.__name__ = f.__name__ 

try: 

self.__module__ = f.__module__ 

except AttributeError: 

self.__module__ = f.__objclass__.__module__ 

if argument_fixer is not None: # it is None unless the argument fixer 

# was known previously. See #15038. 

self._argument_fixer = argument_fixer 

  

cdef get_key_args_kwds(self, tuple args, dict kwds): 

""" 

Return the key in the cache to be used when ``args`` and 

``kwds`` are passed in as parameters. 

  

See ``get_key`` for the Python interface and tests. 

""" 

# The key for "no arguments" is cached in empty_key 

if not args and not kwds: 

if self.empty_key is None: 

self.empty_key = self.fix_args_kwds(args, kwds) 

return self.empty_key 

  

return self.fix_args_kwds(args, kwds) 

  

cdef int argfix_init(self) except -1: 

""" 

TESTS:: 

  

sage: @cached_function 

....: def test_cache(x): 

....: return -x 

sage: test_cache(1) 

-1 

""" 

self._argument_fixer = ArgumentFixer(self.f, 

classmethod=self.is_classmethod) 

  

cdef fix_args_kwds(self, tuple args, dict kwds): 

r""" 

Normalize parameters to obtain a key for the cache. 

  

TESTS:: 

  

sage: @cached_function(key=lambda x,y,algorithm: (x,y)) 

....: def mul(x, y, algorithm="default"): 

....: return x*y 

sage: mul.get_key(1,1,"default") # indirect doctest 

(1, 1) 

""" 

if self._argument_fixer is None: 

self.argfix_init() 

  

k = self._argument_fixer.fix_to_pos_args_kwds(args, kwds) 

if self.key is None: 

return k 

else: 

return self.key(*k[0], **dict(k[1])) 

  

def __reduce__(self): 

""" 

Pickling of cached functions. 

  

TESTS:: 

  

sage: type(cunningham_prime_factors) 

<type 'sage.misc.cachefunc.CachedFunction'> 

sage: loads(dumps(cunningham_prime_factors)) is cunningham_prime_factors #indirect doctest 

True 

  

""" 

return _cached_function_unpickle, (self.__module__, self.__name__, self.cache) 

  

######### 

## Introspection 

## 

## We provide some methods explicitly, and 

## forward other questions to the cached function. 

  

def _instancedoc_(self): 

""" 

Provide documentation for the cached function. 

  

A cached function shall inherit the documentation 

from the function that is wrapped, not from the 

documentation of the wrapper. 

  

TESTS:: 

  

sage: P.<x,y> = QQ[] 

sage: I = P*[x,y] 

sage: from sage.misc.sageinspect import sage_getdoc 

sage: print(sage_getdoc(I.groebner_basis)) # indirect doctest 

Return the reduced Groebner basis of this ideal. 

... 

ALGORITHM: Uses Singular, Magma (if available), Macaulay2 (if 

available), Giac (if available), or a toy implementation. 

  

Test that :trac:`15184` is fixed:: 

  

sage: from sage.misc.sageinspect import sage_getfile 

sage: type(I.groebner_basis) 

<type 'sage.misc.cachefunc.CachedMethodCaller'> 

sage: os.path.exists(sage_getfile(I.groebner_basis)) 

True 

  

Test that :trac:`18064` is fixed:: 

  

sage: @cached_function 

....: def f(): 

....: return 3 

sage: f.__doc__ 

'File: ... (starting at line 1)\n' 

""" 

from sage.misc.sageinspect import _extract_embedded_position 

f = self.f 

doc = f.__doc__ or '' 

if not doc or _extract_embedded_position(doc) is None: 

try: 

sourcelines = sage_getsourcelines(f) 

from sage.env import SAGE_SRC, SAGE_LIB 

filename = sage_getfile(f) 

  

#it would be nice if we could be sure that SAGE_SRC and 

#SAGE_LIB were already normalized (e.g. not end in a slash) 

S=normpath(SAGE_SRC) 

L=normpath(SAGE_LIB) 

if commonprefix([filename,S]) == S: 

filename = relpath(filename,S) 

elif commonprefix([filename,L]) == L: 

filename = relpath(filename,L) 

#this is a rather expensive way of getting the line number, because 

#retrieving the source requires reading the source file and in many 

#cases this is not required (in cython it's embedded in the docstring, 

#on code objects you'll find it in co_filename and co_firstlineno) 

#however, this hasn't been factored out yet in sageinspect 

#and the logic in sage_getsourcelines is rather intricate. 

file_info = "File: {} (starting at line {})".format(filename,sourcelines[1])+os.linesep 

  

doc = file_info+doc 

except IOError: 

pass 

return doc 

  

def _sage_src_(self): 

""" 

Returns the source code for the wrapped function. 

  

TESTS:: 

  

sage: from sage.misc.sageinspect import sage_getsource 

sage: g = CachedFunction(number_of_partitions) 

sage: 'bober' in sage_getsource(g) # indirect doctest 

True 

  

""" 

from sage.misc.sageinspect import sage_getsource 

return sage_getsource(self.f) 

  

def _sage_src_lines_(self): 

r""" 

Returns the list of source lines and the first line number 

of the wrapped function. 

  

TESTS:: 

  

sage: P.<x,y> = QQ[] 

sage: I = P*[x,y] 

sage: from sage.misc.sageinspect import sage_getsourcelines 

sage: l = " elif algorithm == 'macaulay2:gb':\n" 

sage: l in sage_getsourcelines(I.groebner_basis)[0] # indirect doctest 

True 

  

""" 

from sage.misc.sageinspect import sage_getsourcelines 

return sage_getsourcelines(self.f) 

  

def _sage_argspec_(self): 

""" 

Return the argspec of the wrapped function or method. 

  

This was implemented in :trac:`11115`. 

  

EXAMPLES:: 

  

sage: P.<x,y> = QQ[] 

sage: I = P*[x,y] 

sage: from sage.misc.sageinspect import sage_getargspec 

sage: sage_getargspec(I.groebner_basis) # indirect doctest 

ArgSpec(args=['self', 'algorithm', 'deg_bound', 'mult_bound', 'prot'], 

varargs='args', keywords='kwds', defaults=('', None, None, 

False)) 

  

""" 

return sage_getargspec(self.f) 

  

def __call__(self, *args, **kwds): 

""" 

Return value from cache or call the wrapped function, 

caching the output. 

  

TESTS:: 

  

sage: g = CachedFunction(number_of_partitions) 

sage: a = g(5) 

sage: g.cache 

{((5, 'default'), ()): 7} 

sage: a = g(10^5) # indirect doctest 

sage: a == number_of_partitions(10^5) 

True 

sage: a is g(10^5) 

True 

sage: a is number_of_partitions(10^5) 

True 

  

Check that :trac:`16316` has been fixed, i.e., caching works for 

immutable unhashable objects which define 

:meth:`sage.structure.sage_object.SageObject._cache_key`:: 

  

sage: @cached_function 

....: def f(x): return x+x 

sage: K.<u> = Qq(4) 

sage: x = K(1,1); x 

1 + O(2) 

sage: y = K(1,2); y 

1 + O(2^2) 

sage: x == y 

True 

sage: f(x) is f(x) 

True 

sage: f(y) is not f(x) 

True 

  

""" 

k = self.get_key_args_kwds(args, kwds) 

  

try: 

try: 

return self.cache[k] 

except TypeError: # k is not hashable 

k = dict_key(k) 

return self.cache[k] 

except KeyError: 

w = self.f(*args, **kwds) 

self.cache[k] = w 

return w 

  

def cached(self, *args, **kwds): 

""" 

Return the result from the cache if available. If the value is 

not cached, raise ``KeyError``. 

  

EXAMPLES:: 

  

sage: @cached_function 

....: def f(x): 

....: return x 

sage: f.cached(5) 

Traceback (most recent call last): 

... 

KeyError: ((5,), ()) 

sage: f(5) 

5 

sage: f.cached(5) 

5 

""" 

k = self.get_key_args_kwds(args, kwds) 

  

try: 

return self.cache[k] 

except TypeError: # k is not hashable 

k = dict_key(k) 

return self.cache[k] 

  

def is_in_cache(self, *args, **kwds): 

""" 

Checks if the argument list is in the cache. 

  

EXAMPLES:: 

  

sage: class Foo: 

....: def __init__(self, x): 

....: self._x = x 

....: @cached_method 

....: def f(self, z, y=0): 

....: return self._x*z+y 

sage: a = Foo(2) 

sage: a.f.is_in_cache(3) 

False 

sage: a.f(3) 

6 

sage: a.f.is_in_cache(3,y=0) 

True 

  

TESTS: 

  

Check that :trac:`16316` has been fixed, i.e., caching works for 

immutable unhashable objects which define 

:meth:`sage.structure.sage_object.SageObject._cache_key`:: 

  

sage: @cached_function 

....: def f(x): return x 

sage: K.<u> = Qq(4) 

sage: x = K(1,1); x 

1 + O(2) 

sage: f.is_in_cache(x) 

False 

sage: f(x) 

1 + O(2) 

sage: f.is_in_cache(x) 

True 

  

""" 

k = self.get_key_args_kwds(args, kwds) 

try: 

return k in self.cache 

except TypeError: # k is not hashable 

k = dict_key(k) 

return k in self.cache 

  

def set_cache(self, value, *args, **kwds): 

""" 

Set the value for those args and keyword args 

Mind the unintuitive syntax (value first). 

Any idea on how to improve that welcome! 

  

EXAMPLES:: 

  

sage: g = CachedFunction(number_of_partitions) 

sage: a = g(5) 

sage: g.cache 

{((5, 'default'), ()): 7} 

sage: g.set_cache(17, 5) 

sage: g.cache 

{((5, 'default'), ()): 17} 

sage: g(5) 

17 

  

TESTS: 

  

Check that :trac:`16316` has been fixed, i.e., caching works for 

immutable unhashable objects which define 

:meth:`sage.structure.sage_object.SageObject._cache_key`:: 

  

sage: @cached_function 

....: def f(x): return x 

sage: K.<u> = Qq(4) 

sage: x = K(1,1); x 

1 + O(2) 

sage: f.set_cache(x,x) 

sage: f.is_in_cache(x) 

True 

  

DEVELOPER NOTE: 

  

Is there a way to use the following intuitive syntax? 

  

:: 

  

sage: g(5) = 19 # todo: not implemented 

sage: g(5) # todo: not implemented 

19 

""" 

k = self.get_key_args_kwds(args, kwds) 

try: 

self.cache[k] = value 

except TypeError: # k is not hashable 

k = dict_key(k) 

self.cache[k] = value 

  

def get_key(self, *args, **kwds): 

""" 

Return the key in the cache to be used when ``args`` 

and ``kwds`` are passed in as parameters. 

  

EXAMPLES:: 

  

sage: @cached_function 

....: def foo(x): 

....: return x^2 

sage: foo(2) 

4 

sage: foo.get_key(2) 

((2,), ()) 

sage: foo.get_key(x=3) 

((3,), ()) 

  

Examples for cached methods:: 

  

sage: class Foo: 

....: def __init__(self, x): 

....: self._x = x 

....: @cached_method 

....: def f(self, y, z=0): 

....: return self._x * y + z 

sage: a = Foo(2) 

sage: z = a.f(37) 

sage: k = a.f.get_key(37); k 

((37, 0), ()) 

sage: a.f.cache[k] is z 

True 

  

Note that the method does not test whether there are 

too many arguments, or wrong argument names:: 

  

sage: a.f.get_key(1,2,3,x=4,y=5,z=6) 

((1, 2, 3), (('x', 4), ('y', 5), ('z', 6))) 

  

It does, however, take into account the different 

ways of providing named arguments, possibly with a 

default value:: 

  

sage: a.f.get_key(5) 

((5, 0), ()) 

sage: a.f.get_key(y=5) 

((5, 0), ()) 

sage: a.f.get_key(5,0) 

((5, 0), ()) 

sage: a.f.get_key(5,z=0) 

((5, 0), ()) 

sage: a.f.get_key(y=5,z=0) 

((5, 0), ()) 

""" 

return self.get_key_args_kwds(args, kwds) 

  

def __repr__(self): 

""" 

EXAMPLES:: 

  

sage: g = CachedFunction(number_of_partitions) 

sage: g # indirect doctest 

Cached version of <function number_of_partitions at 0x...> 

""" 

try: 

return "Cached version of {}".format(self.f) 

except AttributeError: 

return "Cached version of a method (pending reassignment)" 

  

def clear_cache(self): 

""" 

Clear the cache dictionary. 

  

EXAMPLES:: 

  

sage: g = CachedFunction(number_of_partitions) 

sage: a = g(5) 

sage: g.cache 

{((5, 'default'), ()): 7} 

sage: g.clear_cache() 

sage: g.cache 

{} 

""" 

self.cache.clear() 

  

def precompute(self, arglist, num_processes=1): 

""" 

Cache values for a number of inputs. Do the computation 

in parallel, and only bother to compute values that we 

haven't already cached. 

  

INPUT: 

  

- ``arglist`` -- list (or iterables) of arguments for which 

the method shall be precomputed. 

  

- ``num_processes`` -- number of processes used by 

:func:`~sage.parallel.decorate.parallel` 

  

EXAMPLES:: 

  

sage: @cached_function 

....: def oddprime_factors(n): 

....: l = [p for p,e in factor(n) if p != 2] 

....: return len(l) 

sage: oddprime_factors.precompute(range(1,100), 4) 

sage: oddprime_factors.cache[(25,),()] 

1 

""" 

from sage.parallel.decorate import parallel, normalize_input 

P = parallel(num_processes)(self.f) 

cdef list arglist2 = [] 

for a in arglist: 

ak = normalize_input(a) 

if self.get_key_args_kwds(ak[0], ak[1]) not in self.cache: 

arglist2.append(ak) 

for ((args,kwargs), val) in P(arglist2): 

self.set_cache(val, *args, **kwargs) 

  

  

cached_function = decorator_keywords(CachedFunction) 

  

  

cdef class WeakCachedFunction(CachedFunction): 

""" 

A version of :class:`CachedFunction` using weak references on the values. 

  

If ``f`` is a function, do either ``g = weak_cached_function(f)`` to make 

a cached version of ``f``, or put ``@weak_cached_function`` right before 

the definition of ``f`` (i.e., use Python decorators):: 

  

@weak_cached_function 

def f(...): 

... 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import weak_cached_function 

sage: class A: pass 

sage: @weak_cached_function 

....: def f(): 

....: print("doing a computation") 

....: return A() 

sage: a = f() 

doing a computation 

  

The result is cached:: 

  

sage: b = f() 

sage: a is b 

True 

  

However, if there are no strong references left, the result 

may be garbage collected, and thus a new computation would 

take place:: 

  

sage: del a 

sage: del b 

sage: import gc 

sage: n = gc.collect() 

sage: a = f() 

doing a computation 

  

The parameter ``key`` can be used to ignore parameters for 

caching. In this example we ignore the parameter ``algorithm``:: 

  

sage: @weak_cached_function(key=lambda x,algorithm: x) 

....: def mod_ring(x, algorithm="default"): 

....: return IntegerModRing(x) 

sage: mod_ring(1,algorithm="default") is mod_ring(1,algorithm="algorithm") is mod_ring(1) is mod_ring(1,'default') 

True 

  

TESTS:: 

  

sage: from sage.misc.cachefunc import weak_cached_function 

sage: class A: pass 

sage: @weak_cached_function 

....: def f(): 

....: print("doing a computation") 

....: return A() 

sage: a = f() # indirect doctest 

doing a computation 

  

The result is cached:: 

  

sage: b = f() 

sage: a is b 

True 

  

However, if there are no strong references left, the result 

may be garbage collected, and thus a new computation would 

take place:: 

  

sage: del a 

sage: del b 

sage: import gc 

sage: n = gc.collect() 

sage: a = f() 

doing a computation 

  

Check that :trac:`16316` has been fixed, i.e., caching works for 

immutable unhashable objects which define 

:meth:`sage.structure.sage_object.SageObject._cache_key`:: 

  

sage: from sage.misc.cachefunc import weak_cached_function 

sage: @weak_cached_function 

....: def f(x): return x+x 

sage: K.<u> = Qq(4) 

sage: R.<t> = K[] 

sage: x = t + K(1,1); x 

(1 + O(2^20))*t + 1 + O(2) 

sage: y = t + K(1,2); y 

(1 + O(2^20))*t + 1 + O(2^2) 

sage: x == y 

True 

sage: f(x) is f(x) 

True 

sage: f(y) is not f(x) 

True 

  

Examples and tests for ``is_in_cache``:: 

  

sage: from sage.misc.cachefunc import weak_cached_function 

sage: class A: 

....: def __init__(self, x): 

....: self.x = x 

sage: @weak_cached_function 

....: def f(n): 

....: return A(n) 

sage: a = f(5) 

  

The key 5 is in the cache, as long as there is a strong 

reference to the corresponding value:: 

  

sage: f.is_in_cache(5) 

True 

  

However, if there are no strong references left, the cached 

item is removed from cache after garbage collection:: 

  

sage: del a 

sage: import gc 

sage: n = gc.collect() 

sage: f.is_in_cache(5) 

False 

  

Check that :trac:`16316` has been fixed, i.e., caching works for 

immutable unhashable objects which define 

:meth:`sage.structure.sage_object.SageObject._cache_key`:: 

  

sage: from sage.misc.cachefunc import weak_cached_function 

sage: @weak_cached_function 

....: def f(x): return x 

sage: K.<u> = Qq(4) 

sage: R.<t> = K[] 

sage: f.is_in_cache(t) 

False 

sage: f(t) 

(1 + O(2^20))*t 

sage: f.is_in_cache(t) 

True 

  

Examples and tests for ``set_cache``:: 

  

sage: from sage.misc.cachefunc import weak_cached_function 

sage: @weak_cached_function 

....: def f(n): 

....: raise RuntimeError 

sage: f.set_cache(ZZ, 5) 

sage: f(5) 

Integer Ring 

  

Check that :trac:`16316` has been fixed, i.e., caching works for 

immutable unhashable objects which define 

:meth:`sage.structure.sage_object.SageObject._cache_key`:: 

  

sage: from sage.misc.cachefunc import weak_cached_function 

sage: @weak_cached_function 

....: def f(x): return x 

sage: K.<u> = Qq(4) 

sage: R.<t> = K[] 

sage: f.set_cache(t,t) 

sage: f.is_in_cache(t) 

True 

""" 

def __init__(self, f, *, classmethod=False, name=None, key=None): 

""" 

The inputs to the function must be hashable or they must define 

:meth:`sage.structure.sage_object.SageObject._cache_key`. 

The outputs to the function must be weakly referenceable. 

  

TESTS:: 

  

sage: from sage.misc.cachefunc import weak_cached_function 

sage: class A: pass 

sage: @weak_cached_function 

....: def f(): 

....: return A() 

sage: f 

Cached version of <function f at ...> 

  

We demonstrate that pickling works, provided the uncached function 

is available:: 

  

sage: import __main__ 

sage: __main__.f = f 

sage: loads(dumps(f)) 

Cached version of <function f at ...> 

sage: str(f.cache) 

'<WeakValueDictionary at 0x...>' 

  

""" 

self._common_init(f, None, name=name, key=key) 

self.cache = WeakValueDictionary() 

  

  

weak_cached_function = decorator_keywords(WeakCachedFunction) 

  

class CachedMethodPickle(object): 

""" 

This class helps to unpickle cached methods. 

  

.. NOTE:: 

  

Since :trac:`8611`, a cached method is an attribute 

of the instance (provided that it has a ``__dict__``). 

Hence, when pickling the instance, it would be attempted 

to pickle that attribute as well, but this is a problem, 

since functions can not be pickled, currently. Therefore, 

we replace the actual cached method by a place holder, 

that kills itself as soon as any attribute is requested. 

Then, the original cached attribute is reinstated. But the 

cached values are in fact saved (if `do_pickle` is set.) 

  

EXAMPLES:: 

  

sage: R.<x, y, z> = PolynomialRing(QQ, 3) 

sage: I = R*(x^3 + y^3 + z^3,x^4-y^4) 

sage: I.groebner_basis() 

[y^5*z^3 - 1/4*x^2*z^6 + 1/2*x*y*z^6 + 1/4*y^2*z^6, 

x^2*y*z^3 - x*y^2*z^3 + 2*y^3*z^3 + z^6, 

x*y^3 + y^4 + x*z^3, x^3 + y^3 + z^3] 

sage: I.groebner_basis 

Cached version of <function groebner_basis at 0x...> 

  

We now pickle and unpickle the ideal. The cached method 

``groebner_basis`` is replaced by a placeholder:: 

  

sage: J = loads(dumps(I)) 

sage: J.groebner_basis 

Pickle of the cached method "groebner_basis" 

  

But as soon as any other attribute is requested from the 

placeholder, it replaces itself by the cached method, and 

the entries of the cache are actually preserved:: 

  

sage: J.groebner_basis.is_in_cache() 

True 

sage: J.groebner_basis 

Cached version of <function groebner_basis at 0x...> 

sage: J.groebner_basis() == I.groebner_basis() 

True 

  

TESTS: 

  

Since :trac:`11115`, there is a special implementation for 

cached methods that don't take arguments:: 

  

sage: class A: 

....: @cached_method(do_pickle=True) 

....: def f(self): return 1 

....: @cached_method(do_pickle=True) 

....: def g(self, x): return x 

  

sage: import __main__ 

sage: __main__.A = A 

sage: a = A() 

sage: type(a.f) 

<type 'sage.misc.cachefunc.CachedMethodCallerNoArgs'> 

sage: type(a.g) 

<type 'sage.misc.cachefunc.CachedMethodCaller'> 

  

We demonstrate that both implementations can be pickled and 

preserve the cache. For that purpose, we assign nonsense to the 

cache. Of course, it is a very bad idea to override the cache in 

that way. So, please don't try this at home:: 

  

sage: a.f.set_cache(0) 

sage: a.f() 

0 

sage: a.g.set_cache(0,x=1) 

sage: a.g(1) 

0 

sage: b = loads(dumps(a)) 

sage: b.f() 

0 

sage: b.g(1) 

0 

  

Anyway, the cache will be automatically reconstructed after 

clearing it:: 

  

sage: a.f.clear_cache() 

sage: a.f() 

1 

  

sage: a.g.clear_cache() 

sage: a.g(1) 

1 

  

AUTHOR: 

  

- Simon King (2011-01) 

""" 

def __init__(self, inst, name, cache=None): 

""" 

INPUT: 

  

- ``inst`` - some instance. 

- ``name`` (string) - usually the name of an attribute 

of ``inst`` to which ``self`` is assigned. 

  

TESTS:: 

  

sage: from sage.misc.cachefunc import CachedMethodPickle 

sage: P = CachedMethodPickle(1, 'foo') 

sage: P 

Pickle of the cached method "foo" 

  

""" 

self._instance = inst 

self._name = name 

self._cache = cache 

  

def __repr__(self): 

""" 

TESTS:: 

  

sage: R.<x, y, z> = PolynomialRing(QQ, 3) 

sage: I = R*(x^3 + y^3 + z^3,x^4-y^4) 

sage: G = I.groebner_basis() 

sage: J = loads(dumps(I)) 

sage: J.groebner_basis #indirect doctest 

Pickle of the cached method "groebner_basis" 

""" 

return 'Pickle of the cached method "{}"'.format(self._name) 

  

def __reduce__(self): 

""" 

This class is a pickle. However, sometimes, pickles 

need to be pickled another time. 

  

TESTS:: 

  

sage: R.<x, y, z> = PolynomialRing(QQ, 3) 

sage: I = R*(x^3 + y^3 + z^3,x^4-y^4) 

sage: I.groebner_basis() 

[y^5*z^3 - 1/4*x^2*z^6 + 1/2*x*y*z^6 + 1/4*y^2*z^6, 

x^2*y*z^3 - x*y^2*z^3 + 2*y^3*z^3 + z^6, 

x*y^3 + y^4 + x*z^3, x^3 + y^3 + z^3] 

sage: J = loads(dumps(I)) 

sage: J.groebner_basis 

Pickle of the cached method "groebner_basis" 

  

When we now pickle ``J``, the pickle of the cached method 

needs to be taken care of:: 

  

sage: K = loads(dumps(J)) # indirect doctest 

sage: K.groebner_basis 

Pickle of the cached method "groebner_basis" 

sage: K.groebner_basis.cache 

{(('', None, None, False), ()): 

[y^5*z^3 - 1/4*x^2*z^6 + 1/2*x*y*z^6 + 1/4*y^2*z^6, 

x^2*y*z^3 - x*y^2*z^3 + 2*y^3*z^3 + z^6, 

x*y^3 + y^4 + x*z^3, x^3 + y^3 + z^3]} 

""" 

return CachedMethodPickle,(self._instance,self._name,self._cache) 

  

def __call__(self,*args,**kwds): 

""" 

The purpose of this call method is to kill ``self`` and to 

replace it by an actual :class:`CachedMethodCaller`. The last 

thing that ``self`` does before disappearing is to call the 

:class:`CachedMethodCaller` and return the result. 

  

EXAMPLES:: 

  

sage: P.<a,b,c,d> = QQ[] 

sage: I = P*[a,b] 

sage: I.gens 

Cached version of <function gens at 0x...> 

sage: J = loads(dumps(I)) 

sage: J.gens 

Pickle of the cached method "gens" 

sage: J.gens() # indirect doctest 

[a, b] 

sage: J.gens 

Cached version of <function gens at 0x...> 

  

""" 

self._instance.__dict__.__delitem__(self._name) 

CM = getattr(self._instance,self._name) 

if self._cache is not None: 

if isinstance(CM, CachedMethodCallerNoArgs): 

CM.cache = self._cache 

else: 

for k, v in self._cache: 

CM.cache[k] = v 

return CM(*args,**kwds) 

  

def __getattr__(self,s): 

""" 

TESTS:: 

  

sage: R.<x, y, z> = PolynomialRing(QQ, 3) 

sage: I = R*(x^3 + y^3 + z^3,x^4-y^4) 

sage: G = I.groebner_basis() 

sage: J = loads(dumps(I)) 

sage: J.groebner_basis 

Pickle of the cached method "groebner_basis" 

  

If an attribute of name ``s`` is requested (say, 

``is_in_cache``), the attribute ``self._name`` of 

``self._instance`` is deleted. Then, the attribute 

of name ``s`` of the attribute ``self._name`` of 

``self._instance`` is requested. Since ``self._name`` 

is a cached method defined for the class of 

``self._instance``, retrieving the just-deleted 

attribute ``self._name`` succeeds. 

  

In that way, the unpickling of the cached method is 

finally accomplished:: 

  

sage: J.groebner_basis.is_in_cache() #indirect doctest 

True 

sage: J.groebner_basis 

Cached version of <function groebner_basis at 0x...> 

  

""" 

self._instance.__dict__.__delitem__(self._name) 

CM = getattr(self._instance,self._name) 

if self._cache is not None: 

if isinstance(CM, CachedMethodCallerNoArgs): 

CM.cache = self._cache 

else: 

for k, v in self._cache: 

CM.cache[k] = v 

return getattr(CM,s) 

  

  

cdef class CachedMethodCaller(CachedFunction): 

""" 

Utility class that is used by :class:`CachedMethod` to bind a 

cached method to an instance. 

  

.. NOTE:: 

  

Since :trac:`11115`, there is a special implementation 

:class:`CachedMethodCallerNoArgs` for methods that do not take 

arguments. 

  

EXAMPLES:: 

  

sage: class A: 

....: @cached_method 

....: def bar(self,x): 

....: return x^2 

sage: a = A() 

sage: a.bar 

Cached version of <function bar at 0x...> 

sage: type(a.bar) 

<type 'sage.misc.cachefunc.CachedMethodCaller'> 

sage: a.bar(2) is a.bar(x=2) 

True 

  

TESTS: 

  

As of :trac:`15692` the contents of the cache are not pickled anymore:: 

  

sage: import __main__ 

sage: __main__.A = A 

sage: len(a.bar.cache) 

1 

sage: b = loads(dumps(a)) 

sage: len(b.bar.cache) 

0 

  

The parameter ``do_pickle`` can be used to change this behaviour:: 

  

sage: class A: 

....: @cached_method(do_pickle=True) 

....: def bar(self,x): 

....: return x^2 

  

sage: __main__.A = A 

sage: a = A() 

sage: a.bar(2) 

4 

sage: len(a.bar.cache) 

1 

sage: b = loads(dumps(a)) 

sage: len(b.bar.cache) 

1 

  

""" 

def __init__(self, CachedMethod cachedmethod, inst, *, cache=None, name=None, key=None, do_pickle=None): 

""" 

EXAMPLES:: 

  

sage: class Foo: 

....: def __init__(self, x): 

....: self._x = x 

....: @cached_method 

....: def f(self,*args): 

....: return self._x^2 

sage: a = Foo(2) 

sage: a.f.cache 

{} 

sage: a.f() 

4 

sage: a.f.cache 

{((), ()): 4} 

""" 

# initialize CachedFunction. Since the cached method is actually bound 

# to an instance, it now makes sense to initialise the ArgumentFixer 

# and re-use it for all bound cached method callers of the unbound 

# cached method. 

if cachedmethod._cachedfunc._argument_fixer is None: 

cachedmethod._cachedfunc.argfix_init() 

self._common_init(cachedmethod._cachedfunc.f, 

cachedmethod._cachedfunc._argument_fixer, 

name=name, 

key=key, 

do_pickle=do_pickle) 

if cache is None: 

self.cache = NonpicklingDict() if do_pickle else {} 

else: 

self.cache = cache 

self._instance = inst 

self._cachedmethod = cachedmethod 

  

def __reduce__(self): 

""" 

The pickle of a :class:`CachedMethodCaller` unpickles 

to a :class:`CachedMethodPickle`, that is able to replace 

itself by a copy of the original :class:`CachedMethodCaller`. 

  

TESTS:: 

  

sage: R.<x, y, z> = PolynomialRing(QQ, 3) 

sage: I = R*(x^3 + y^3 + z^3,x^4-y^4) 

sage: G = I.groebner_basis() 

sage: J = loads(dumps(I)) #indirect doctest 

sage: J.groebner_basis 

Pickle of the cached method "groebner_basis" 

sage: J.groebner_basis.is_in_cache() 

True 

sage: J.groebner_basis 

Cached version of <function groebner_basis at 0x...> 

""" 

if isinstance(self._cachedmethod, CachedInParentMethod) or hasattr(self._instance,self._cachedmethod._cache_name): 

return CachedMethodPickle,(self._instance,self.__name__) 

else: 

return CachedMethodPickle,(self._instance,self.__name__,self.cache) 

  

def _instance_call(self, *args, **kwds): 

""" 

Call the cached method without using the cache. 

  

EXAMPLES:: 

  

sage: P.<a,b,c,d> = QQ[] 

sage: I = P*[a,b] 

sage: I.groebner_basis() 

[a, b] 

sage: I.groebner_basis._instance_call() is I.groebner_basis() 

False 

sage: I.groebner_basis._instance_call() == I.groebner_basis() 

True 

  

:: 

  

sage: class Foo(object): 

....: def __init__(self, x): 

....: self._x = x 

....: @cached_method 

....: def f(self,n=2): 

....: return self._x^n 

sage: a = Foo(2) 

sage: a.f() 

4 

  

Usually, a cached method is indeed cached:: 

  

sage: a.f() is a.f() 

True 

  

However, when it becomes necessary, one can call it without 

using the cache:: 

  

sage: a.f._instance_call() is a.f() 

False 

sage: a.f._instance_call() == a.f() 

True 

""" 

return self.f(self._instance, *args, **kwds) 

  

cdef fix_args_kwds(self, tuple args, dict kwds): 

r""" 

Normalize parameters to obtain a key for the cache. 

  

TESTS:: 

  

sage: class A(object): 

....: def _f_normalize(self, x, algorithm): return x 

....: @cached_method(key=_f_normalize) 

....: def f(self, x, algorithm='default'): return x 

sage: a = A() 

sage: a.f(1, algorithm="default") is a.f(1) is a.f(1, algorithm="algorithm") 

True 

""" 

if self._argument_fixer is None: 

self.argfix_init() 

  

k = self._argument_fixer.fix_to_pos_args_kwds(args, kwds) 

if self.key is None: 

return k 

else: 

return self.key(self._instance, *k[0], **dict(k[1])) 

  

def __call__(self, *args, **kwds): 

""" 

Call the cached method. 

  

TESTS:: 

  

sage: from sage.misc.superseded import deprecated_function_alias 

sage: class Foo: 

....: @cached_method 

....: def f(self, x,y=1): 

....: return x+y 

....: g = deprecated_function_alias(57, f) 

sage: a = Foo() 

sage: a.f(1) #indirect doctest 

2 

  

The result is cached, taking into account 

the three ways of providing (named) arguments:: 

  

sage: a.f(5) is a.f(5,1) 

True 

sage: a.f(5) is a.f(5,y=1) 

True 

sage: a.f(5) is a.f(y=1,x=5) 

True 

  

The method can be called as a bound function using the same cache:: 

  

sage: a.f(5) is Foo.f(a, 5) 

True 

sage: a.f(5) is Foo.f(a,5,1) 

True 

sage: a.f(5) is Foo.f(a, 5,y=1) 

True 

sage: a.f(5) is Foo.f(a, y=1,x=5) 

True 

  

Cached methods are compatible with 

:meth:`sage.misc.superseded.deprecated_function_alias`:: 

  

sage: a.g(5) is a.f(5) 

doctest:...: DeprecationWarning: g is deprecated. Please use f instead. 

See http://trac.sagemath.org/57 for details. 

True 

sage: Foo.g(a, 5) is a.f(5) 

True 

sage: Foo.g(a, y=1,x=5) is a.f(5) 

True 

  

We test that :trac:`5843` is fixed:: 

  

sage: class Foo: 

....: def __init__(self, x): 

....: self._x = x 

....: @cached_method 

....: def f(self, y): 

....: return self._x 

sage: a = Foo(2) 

sage: b = Foo(3) 

sage: a.f(b.f) 

2 

  

Check that :trac:`16316` has been fixed, i.e., caching works for 

immutable unhashable objects which define 

:meth:`sage.structure.sage_object.SageObject._cache_key`:: 

  

sage: K.<u> = Qq(4) 

sage: class A(object): 

....: @cached_method 

....: def f(self, x): return x+x 

sage: a = A() 

sage: x = K(1,1); x 

1 + O(2) 

sage: y = K(1,2); y 

1 + O(2^2) 

sage: x == y 

True 

sage: a.f(x) is a.f(x) 

True 

sage: a.f(y) is not a.f(x) 

True 

  

""" 

if self._instance is None: 

# cached method bound to a class 

instance = args[0] 

args = args[1:] 

return self._cachedmethod.__get__(instance)(*args, **kwds) 

  

k = self.get_key_args_kwds(args, kwds) 

  

cdef dict cache = <dict>self.cache 

try: 

try: 

return cache[k] 

except TypeError: # k is not hashable 

k = dict_key(k) 

return cache[k] 

except KeyError: 

w = self._instance_call(*args, **kwds) 

cache[k] = w 

return w 

  

def cached(self, *args, **kwds): 

""" 

Return the result from the cache if available. If the value is 

not cached, raise ``KeyError``. 

  

EXAMPLES:: 

  

sage: class CachedMethodTest(object): 

....: @cached_method 

....: def f(self, x): 

....: return x 

sage: o = CachedMethodTest() 

sage: CachedMethodTest.f.cached(o, 5) 

Traceback (most recent call last): 

... 

KeyError: ((5,), ()) 

sage: o.f.cached(5) 

Traceback (most recent call last): 

... 

KeyError: ((5,), ()) 

sage: o.f(5) 

5 

sage: CachedMethodTest.f.cached(o, 5) 

5 

sage: o.f.cached(5) 

5 

""" 

if self._instance is None: 

# cached method bound to a class 

instance = args[0] 

args = args[1:] 

return self._cachedmethod.__get__(instance).cached(*args, **kwds) 

  

k = self.get_key_args_kwds(args, kwds) 

  

try: 

return self.cache[k] 

except TypeError: # k is not hashable 

k = dict_key(k) 

return self.cache[k] 

  

def __get__(self, inst, cls): 

r""" 

Get a :class:`CachedMethodCaller` bound to a specific 

instance of the class of the cached method. 

  

NOTE: 

  

:class:`CachedMethodCaller` has a separate ``__get__`` 

since the categories framework creates and caches the 

return value of ``CachedMethod.__get__`` with 

``inst==None``. 

  

This getter attempts to assign a bound method as an 

attribute to the given instance. If this is not 

possible (for example, for some extension classes), 

it is attempted to find an attribute ``__cached_methods``, 

and store/retrieve the bound method there. In that 

way, cached methods can be implemented for extension 

classes deriving from :class:`~sage.structure.parent.Parent` 

and :class:`~sage.structure.element.Element`. 

  

TESTS: 

  

Due to the separate ``__get__`` method, it is possible 

to define a cached method in one class and use it as 

an attribute of another class. :: 

  

sage: class Foo: 

....: @cached_method 

....: def f(self, y): 

....: return y - 1 

sage: class Bar: 

....: f = Foo.f 

sage: b1 = Bar() 

sage: b2 = Bar() 

  

The :class:`CachedMethod` is replaced by an instance 

of :class:`CachedMethodCaller` that (by :trac:`8611`) 

is set as an attribute. Hence, we have:: 

  

sage: b1.f is b1.f 

True 

  

Any instance of ``Bar`` gets its own instance of 

:class:`CachedMethodCaller``:: 

  

sage: b1.f is b2.f 

False 

  

The method caller knows the instance that it belongs 

to:: 

  

sage: Foo.f._instance is None 

True 

sage: b1.f._instance is b1 

True 

sage: b2.f._instance is b2 

True 

  

An extension class can inherit a cached method from the 

parent or element class of a category (:trac:`11115`). 

See :class:`CachedMethodCaller` for examples. 

  

Verify that :trac:`16337` has been resolved:: 

  

sage: class Foo: 

....: @cached_method(key=lambda self,y: y+1) 

....: def f(self, y): 

....: return y - 1 

sage: class Bar: 

....: f = Foo.f 

  

sage: b = Bar() 

sage: b.f(0) 

-1 

sage: b.f.cache 

{1: -1} 

  

""" 

# This is for Parents or Elements that do not allow attribute assignment 

try: 

return (<dict>inst.__cached_methods)[self._cachedmethod._cachedfunc.__name__] 

except (AttributeError, TypeError, KeyError): 

pass 

  

cls = type(self) 

Caller = cls(self._cachedmethod, inst, 

cache=self._cachedmethod._get_instance_cache(inst), 

name=self._cachedmethod._cachedfunc.__name__, key=self.key, do_pickle=self.do_pickle) 

  

try: 

setattr(inst,self._cachedmethod._cachedfunc.__name__, Caller) 

return Caller 

except AttributeError: 

pass 

try: 

if inst.__cached_methods is None: 

inst.__cached_methods = {self._cachedmethod._cachedfunc.__name__ : Caller} 

else: 

(<dict>inst.__cached_methods)[self._cachedmethod._cachedfunc.__name__] = Caller 

except AttributeError: 

pass 

return Caller 

  

def precompute(self, arglist, num_processes=1): 

""" 

Cache values for a number of inputs. Do the computation 

in parallel, and only bother to compute values that we 

haven't already cached. 

  

INPUT: 

  

- ``arglist`` -- list (or iterables) of arguments for which 

the method shall be precomputed. 

  

- ``num_processes`` -- number of processes used by 

:func:`~sage.parallel.decorate.parallel` 

  

EXAMPLES:: 

  

sage: class Foo(object): 

....: @cached_method 

....: def f(self, i): 

....: return i^2 

sage: foo = Foo() 

sage: foo.f(3) 

9 

sage: foo.f(1) 

1 

sage: foo.f.precompute(range(2), 2) 

sage: foo.f.cache == {((0,), ()): 0, ((1,), ()): 1, ((3,), ()): 9} 

True 

""" 

from sage.parallel.decorate import parallel, normalize_input 

P = parallel(num_processes)(self._instance_call) 

cdef list arglist2 = [] 

for a in arglist: 

ak = normalize_input(a) 

if self.get_key_args_kwds(ak[0], ak[1]) not in self.cache: 

arglist2.append(ak) 

for ((args,kwargs), val) in P(arglist2): 

self.set_cache(val, *args, **kwargs) 

  

  

cdef class CachedMethodCallerNoArgs(CachedFunction): 

""" 

Utility class that is used by :class:`CachedMethod` to bind a 

cached method to an instance, in the case of a method that does 

not accept any arguments except ``self``. 

  

.. NOTE:: 

  

The return value ``None`` would not be cached. So, if you have 

a method that does not accept arguments and may return ``None`` 

after a lengthy computation, then ``@cached_method`` should not 

be used. 

  

EXAMPLES:: 

  

sage: P.<a,b,c,d> = QQ[] 

sage: I = P*[a,b] 

sage: I.gens 

Cached version of <function gens at 0x...> 

sage: type(I.gens) 

<type 'sage.misc.cachefunc.CachedMethodCallerNoArgs'> 

sage: I.gens is I.gens 

True 

sage: I.gens() is I.gens() 

True 

  

TESTS: 

  

As of :trac:`15692` the contents of the cache are not pickled anymore:: 

  

sage: class A: 

....: @cached_method 

....: def bar(self): 

....: return 4 

sage: import __main__ 

sage: __main__.A = A 

sage: a = A() 

sage: a.bar() 

4 

sage: a.bar.cache 

4 

sage: b = loads(dumps(a)) 

sage: b.bar.cache 

  

The parameter ``do_pickle`` can be used to change this behaviour:: 

  

sage: class A: 

....: @cached_method(do_pickle=True) 

....: def bar(self): 

....: return 4 

  

sage: __main__.A = A 

sage: a = A() 

sage: a.bar() 

4 

sage: a.bar.cache 

4 

sage: b = loads(dumps(a)) 

sage: b.bar.cache 

4 

  

AUTHOR: 

  

- Simon King (2011-04) 

""" 

def __init__(self, inst, f, cache=None, name=None, do_pickle=None): 

""" 

EXAMPLES:: 

  

sage: class Foo: 

....: def __init__(self, x): 

....: self._x = x 

....: @cached_method 

....: def f(self): 

....: return self._x^2 

sage: a = Foo(2) 

sage: print(a.f.cache) 

None 

sage: a.f() 

4 

sage: a.f.cache 

4 

  

""" 

# initialize CachedFunction 

if isinstance(f,basestring): 

try: 

F = getattr(inst.__class__,f) 

except AttributeError: 

F = getattr(inst,f) 

if isinstance(F,CachedFunction): 

f = F.f 

else: 

f = F 

self._common_init(f, None, name=name, do_pickle=do_pickle) 

# This is for unpickling a CachedMethodCallerNoArgs out 

# of an old CachedMethodCaller: 

cachename = '_cache__' + self.__name__ 

if hasattr(inst, cachename): 

# This is for data that are pickled in an old format 

CACHE = getattr(inst, cachename) 

if len(CACHE) > 1: 

raise TypeError("Apparently you are opening a pickle in which '{}' was a method accepting arguments".format(name)) 

if len(CACHE) == 1: 

self.cache = CACHE.values()[0] 

else: 

self.cache = cache 

delattr(inst, cachename) 

else: 

self.cache = cache # None means: the underlying method will be called 

self._instance = inst 

  

def __reduce__(self): 

""" 

Since functions can not be pickled, the cached method caller 

is pickled by a :class:`CachedMethodPickle`, that replaces 

itself by an actual :class:`CachedMethodCallerNoArgs` as soon 

as it is asked to do anything. 

  

TESTS:: 

  

sage: P.<a,b,c,d> = QQ[] 

sage: I = P*[a,b] 

sage: I.gens() 

[a, b] 

sage: I.gens 

Cached version of <function gens at 0x...> 

sage: J = loads(dumps(I)) 

sage: J.gens 

Pickle of the cached method "gens" 

sage: J.gens.cache # the cache is dropped because gens is not marked with do_pickle=True 

sage: J.gens 

Cached version of <function gens at 0x...> 

  

""" 

if self.do_pickle: 

return CachedMethodPickle,(self._instance, self.__name__, self.cache) 

else: 

return CachedMethodPickle,(self._instance, self.__name__) 

  

def _instance_call(self): 

""" 

Call the cached method without using the cache. 

  

EXAMPLES:: 

  

sage: P.<a,b,c,d> = QQ[] 

sage: I = P*[a,b] 

sage: I.gens() 

[a, b] 

sage: I.gens._instance_call() is I.gens() 

False 

sage: I.gens._instance_call() == I.gens() 

True 

  

""" 

return self.f(self._instance) 

  

def __call__(self): 

""" 

Call the cached method. 

  

EXAMPLES:: 

  

sage: P.<a,b,c,d> = QQ[] 

sage: I = P*[a,b] 

sage: I.gens() # indirect doctest 

[a, b] 

sage: I.gens() is I.gens() 

True 

  

""" 

if self.cache is None: 

f = self.f 

self.cache = f(self._instance) 

return self.cache 

  

def set_cache(self, value): 

""" 

Override the cache with a specific value. 

  

.. NOTE:: 

  

``None`` is not suitable for a cached value. It would be 

interpreted as an empty cache, forcing a new computation. 

  

EXAMPLES:: 

  

sage: P.<a,b,c,d> = QQ[] 

sage: I = P*[a,b] 

sage: I.gens() 

[a, b] 

sage: I.gens.set_cache('bar') 

sage: I.gens() 

'bar' 

  

The cache can be emptied and thus the original value will 

be reconstructed:: 

  

sage: I.gens.clear_cache() 

sage: I.gens() 

[a, b] 

  

The attempt to assign ``None`` to the cache fails:: 

  

sage: I.gens.set_cache(None) 

sage: I.gens() 

[a, b] 

  

""" 

self.cache = value 

  

def clear_cache(self): 

r""" 

Clear the cache dictionary. 

  

EXAMPLES:: 

  

sage: P.<a,b,c,d> = QQ[] 

sage: I = P*[a,b] 

sage: I.gens() 

[a, b] 

sage: I.gens.set_cache('bar') 

sage: I.gens() 

'bar' 

  

The cache can be emptied and thus the original value will 

be reconstructed:: 

  

sage: I.gens.clear_cache() 

sage: I.gens() 

[a, b] 

  

""" 

self.cache = None 

  

def is_in_cache(self): 

""" 

Answers whether the return value is already in the cache. 

  

.. NOTE:: 

  

Recall that a cached method without arguments can not cache 

the return value ``None``. 

  

EXAMPLES:: 

  

sage: P.<x,y> = QQ[] 

sage: I = P*[x,y] 

sage: I.gens.is_in_cache() 

False 

sage: I.gens() 

[x, y] 

sage: I.gens.is_in_cache() 

True 

  

""" 

return self.cache is not None 

  

def __get__(self, inst, cls): 

""" 

Get a :class:`CachedMethodCallerNoArgs` bound to a specific 

instance of the class of the cached method. 

  

NOTE: 

  

:class:`CachedMethodCallerNoArgs` has a separate ``__get__`` 

since the categories framework creates and caches the 

return value of ``CachedMethod.__get__`` with 

``inst==None``. 

  

This getter attempts to assign a bound method as an 

attribute to the given instance. If this is not 

possible (for example, for some extension classes), 

it is attempted to find an attribute ``__cached_methods``, 

and store/retrieve the bound method there. In that 

way, cached methods can be implemented for extension 

classes deriving from :class:`~sage.structure.parent.Parent` 

and :class:`~sage.structure.element.Element`. 

  

TESTS: 

  

Due to the separate ``__get__`` method, it is possible 

to define a cached method in one class and use it as 

an attribute of another class. :: 

  

sage: class Foo: 

....: def __init__(self, n): 

....: self.__n = n 

....: @cached_method 

....: def f(self): 

....: return self.__n^2 

sage: class Bar: 

....: f = Foo.f 

sage: b1 = Bar() 

sage: b2 = Bar() 

  

The :class:`CachedMethod` is replaced by an instance of 

:class:`CachedMethodCallerNoArgs` that is set as an 

attribute. Hence, we have:: 

  

sage: b1.f is b1.f 

True 

sage: type(b1.f) 

<type 'sage.misc.cachefunc.CachedMethodCallerNoArgs'> 

  

Any instance of ``Bar`` gets its own instance of 

:class:`CachedMethodCaller``:: 

  

sage: b1.f is b2.f 

False 

  

The method caller knows the instance that it belongs 

to:: 

  

sage: Foo.f._instance is None 

True 

sage: b1.f._instance is b1 

True 

sage: b2.f._instance is b2 

True 

  

""" 

# This is for Parents or Elements that do not allow attribute assignment 

try: 

return (<dict>inst.__cached_methods)[self.__name__] 

except (AttributeError, TypeError, KeyError): 

pass 

Caller = CachedMethodCallerNoArgs(inst, self.f, name=self.__name__, do_pickle=self.do_pickle) 

try: 

setattr(inst,self.__name__, Caller) 

return Caller 

except AttributeError: 

pass 

try: 

if inst.__cached_methods is None: 

inst.__cached_methods = {self.__name__ : Caller} 

else: 

(<dict>inst.__cached_methods)[self.__name__] = Caller 

except AttributeError: 

pass 

return Caller 

  

  

cdef class GloballyCachedMethodCaller(CachedMethodCaller): 

""" 

Implementation of cached methods in case that the cache is not 

stored in the instance, but in some global object. In particular, 

it is used to implement :class:`CachedInParentMethod`. 

  

The only difference is that the instance is used as part of the 

key. 

""" 

cdef get_key_args_kwds(self, tuple args, dict kwds): 

""" 

Return the key in the cache to be used when ``args`` and 

``kwds`` are passed in as parameters. 

  

EXAMPLES:: 

  

sage: class MyParent(Parent): 

....: pass 

sage: class MyElement(object): 

....: def __init__(self, x): 

....: self.x = x 

....: def parent(self): 

....: return MyParent() 

....: @cached_in_parent_method #indirect doctest 

....: def f(self): 

....: return self.x^2 

sage: a = MyElement(2) 

sage: a.f.get_key() 

(<__main__.MyElement object at 0x...>, ((), ())) 

sage: a.f.get_key()[0] is a 

True 

""" 

k = self.fix_args_kwds(args, kwds) 

return (self._instance, k) 

  

  

cdef class CachedMethod(object): 

""" 

A decorator that creates a cached version of an instance 

method of a class. 

  

.. NOTE:: 

  

For proper behavior, the method must be a pure function (no side 

effects). Arguments to the method must be hashable or transformed into 

something hashable using ``key`` or they must define 

:meth:`sage.structure.sage_object.SageObject._cache_key`. 

  

EXAMPLES:: 

  

sage: class Foo(object): 

....: @cached_method 

....: def f(self, t, x=2): 

....: print('computing') 

....: return t**x 

sage: a = Foo() 

  

The example shows that the actual computation 

takes place only once, and that the result is 

identical for equivalent input:: 

  

sage: res = a.f(3, 2); res 

computing 

9 

sage: a.f(t = 3, x = 2) is res 

True 

sage: a.f(3) is res 

True 

  

Note, however, that the :class:`CachedMethod` is replaced by a 

:class:`CachedMethodCaller` or :class:`CachedMethodCallerNoArgs` 

as soon as it is bound to an instance or class:: 

  

sage: P.<a,b,c,d> = QQ[] 

sage: I = P*[a,b] 

sage: type(I.__class__.gens) 

<type 'sage.misc.cachefunc.CachedMethodCallerNoArgs'> 

  

So, you would hardly ever see an instance of this class alive. 

  

The parameter ``key`` can be used to pass a function which creates a 

custom cache key for inputs. In the following example, this parameter is 

used to ignore the ``algorithm`` keyword for caching:: 

  

sage: class A(object): 

....: def _f_normalize(self, x, algorithm): return x 

....: @cached_method(key=_f_normalize) 

....: def f(self, x, algorithm='default'): return x 

sage: a = A() 

sage: a.f(1, algorithm="default") is a.f(1) is a.f(1, algorithm="algorithm") 

True 

  

The parameter ``do_pickle`` can be used to enable pickling of the cache. 

Usually the cache is not stored when pickling:: 

  

sage: class A(object): 

....: @cached_method 

....: def f(self, x): return None 

sage: import __main__ 

sage: __main__.A = A 

sage: a = A() 

sage: a.f(1) 

sage: len(a.f.cache) 

1 

sage: b = loads(dumps(a)) 

sage: len(b.f.cache) 

0 

  

When ``do_pickle`` is set, the pickle contains the contents of the cache:: 

  

sage: class A(object): 

....: @cached_method(do_pickle=True) 

....: def f(self, x): return None 

sage: __main__.A = A 

sage: a = A() 

sage: a.f(1) 

sage: len(a.f.cache) 

1 

sage: b = loads(dumps(a)) 

sage: len(b.f.cache) 

1 

  

Cached methods can not be copied like usual methods, see :trac:`12603`. 

Copying them can lead to very surprising results:: 

  

sage: class A: 

....: @cached_method 

....: def f(self): 

....: return 1 

sage: class B: 

....: g=A.f 

....: def f(self): 

....: return 2 

  

sage: b=B() 

sage: b.f() 

2 

sage: b.g() 

1 

sage: b.f() 

1 

  

""" 

def __init__(self, f, name=None, key=None, do_pickle=None): 

""" 

EXAMPLES:: 

  

sage: class Foo(object): 

....: def __init__(self, x): 

....: self._x = x 

....: @cached_method 

....: def f(self,n): 

....: return self._x^n 

....: @cached_method 

....: def f0(self): 

....: return self._x^2 

sage: a = Foo(2) 

sage: a.f(2) 

4 

sage: a.f0() 

4 

  

The computations in method ``f`` are tried to store in a 

dictionary assigned to the instance ``a``:: 

  

sage: hasattr(a, '_cache__f') 

True 

sage: a._cache__f 

{((2,), ()): 4} 

  

As a shortcut, useful to speed up internal computations, 

the same dictionary is also available as an attribute 

of the ``CachedMethodCaller``:: 

  

sage: type(a.f) 

<type 'sage.misc.cachefunc.CachedMethodCaller'> 

sage: a.f.cache is a._cache__f 

True 

  

Note that if the instance ``a`` would not accept attribute 

assignment, the computations would still be cached in 

``a.f.cache``, and they would in fact be preserved when 

pickling. 

  

The cached method ``f0`` accepts no arguments, which allows 

for an improved way of caching: By an attribute of the cached 

method itself. This cache is *only* available in that way, i.e., 

it is not additionally stored as an attribute of ``a``:: 

  

sage: type(a.f0) 

<type 'sage.misc.cachefunc.CachedMethodCallerNoArgs'> 

sage: a.f0.cache 

4 

sage: sorted(n for n in dir(a) if not n.startswith('__')) 

['_cache__f', '_x', 'f', 'f0'] 

  

The cached method has its name and module set:: 

  

sage: f = Foo.__dict__["f"] 

sage: f.__name__ 

'f' 

sage: f.__module__ 

'__main__' 

""" 

self._cache_name = '_cache__' + (name or f.__name__) 

self._cachedfunc = CachedFunction(f, classmethod=True, name=name, key=key, do_pickle=do_pickle) 

self.__name__ = self._cachedfunc.__name__ 

self.__module__ = self._cachedfunc.__module__ 

  

def __call__(self, inst, *args, **kwds): 

""" 

Call the cached method as a function on an instance 

  

INPUT: 

  

- ``inst`` -- an instance on which the method is to be called 

- Further positional or named arguments. 

  

EXAMPLES:: 

  

  

sage: from sage.misc.superseded import deprecated_function_alias 

sage: class Foo(object): 

....: def __init__(self, x): 

....: self._x = x 

....: @cached_method 

....: def f(self,n=2): 

....: return self._x^n 

....: g = deprecated_function_alias(57, f) 

sage: a = Foo(2) 

sage: Foo.__dict__['f'](a) 

4 

  

This uses the cache as usual:: 

  

sage: Foo.__dict__['f'](a) is a.f() 

True 

  

This feature makes cached methods compatible with 

:meth:`sage.misc.superseded.deprecated_function_alias`:: 

  

sage: a.g() is a.f() 

doctest:...: DeprecationWarning: g is deprecated. Please use f instead. 

See http://trac.sagemath.org/57 for details. 

True 

sage: Foo.g(a) is a.f() 

True 

""" 

return self.__get__(inst)(*args, **kwds) 

  

cpdef _get_instance_cache(self, inst): 

""" 

Return the cache dictionary. 

  

TESTS:: 

  

sage: class Foo: 

....: def __init__(self, x): 

....: self._x = x 

....: @cached_method 

....: def f(self,n=2): 

....: return self._x^n 

sage: a = Foo(2) 

sage: a.f() 

4 

  

Note that we can not provide a direct test, since ``a.f`` is 

an instance of :class:`CachedMethodCaller`. But during its 

initialisation, this method was called in order to provide the 

cached method caller with its cache, and, if possible, assign 

it to an attribute of ``a``. So, the following is an indirect 

doctest:: 

  

sage: a.f.cache # indirect doctest 

{((2,), ()): 4} 

sage: a._cache__f 

{((2,), ()): 4} 

  

""" 

default = {} if self._cachedfunc.do_pickle else NonpicklingDict() 

try: 

return inst.__dict__.setdefault(self._cache_name, default) 

except AttributeError: 

return default 

  

def __get__(self, object inst, cls): 

""" 

Get a CachedMethodCaller bound to this specific instance of 

the class of the cached method. 

  

TESTS:: 

  

sage: class Foo: 

....: @cached_method 

....: def f(self): 

....: return 1 

....: @cached_method 

....: def g(self, x,n=3): 

....: return x^n 

sage: a = Foo() 

sage: type(a.f) 

<type 'sage.misc.cachefunc.CachedMethodCallerNoArgs'> 

sage: type(a.g) 

<type 'sage.misc.cachefunc.CachedMethodCaller'> 

  

By :trac:`8611`, it is attempted to set the 

CachedMethodCaller as an attribute of the instance ``a``, 

replacing the original cached attribute. Therefore, the 

``__get__`` method will be used only once, which saves much 

time. Hence, we have:: 

  

sage: a.f is a.f 

True 

sage: a.g is a.g 

True 

  

Verify that :trac:`16337` has been resolved:: 

  

sage: class Foo: 

....: @cached_method(key=lambda self, x:x+1) 

....: def f(self, x=0): 

....: return x 

  

sage: a = Foo() 

sage: a.f(0) 

0 

sage: a.f.cache 

{1: 0} 

  

""" 

# This is for Parents or Elements that do not allow attribute assignment: 

cdef str name 

try: 

name = self._cachedfunc.__name__ 

except AttributeError: 

name = self.__name__ 

try: 

return (<dict>inst.__cached_methods)[name] 

except (AttributeError, TypeError, KeyError): 

pass 

# Apparently we need to construct the caller. 

# Since we have an optimized version for functions that do not accept arguments, 

# we need to analyse the argspec 

f = self._cachedfunc.f 

if self.nargs == 0: 

if isinstance(f, object) and not isfunction(f): 

try: 

if METH_NOARGS&PyCFunction_GetFlags(f.__get__(inst,cls)): 

self.nargs = 1 

else: 

self.nargs = 2 

except Exception: 

pass 

if self.nargs == 0: 

args, varargs, keywords, defaults = sage_getargspec(f) 

if varargs is None and keywords is None and len(args)<=1: 

self.nargs = 1 

else: 

self.nargs = 2 # don't need the exact number 

if self.nargs == 1: 

Caller = CachedMethodCallerNoArgs(inst, f, name=name, do_pickle=self._cachedfunc.do_pickle) 

else: 

Caller = CachedMethodCaller(self, inst, 

cache=self._get_instance_cache(inst), 

name=name, 

key=self._cachedfunc.key, 

do_pickle=self._cachedfunc.do_pickle) 

try: 

setattr(inst, name, Caller) 

return Caller 

except AttributeError: 

pass 

try: 

if inst.__cached_methods is None: 

inst.__cached_methods = {name : Caller} 

else: 

(<dict>inst.__cached_methods)[name] = Caller 

except AttributeError: 

pass 

return Caller 

  

  

cdef class CachedSpecialMethod(CachedMethod): 

""" 

Cached version of *special* python methods. 

  

IMPLEMENTATION: 

  

For new style classes ``C``, it is not possible to override a special 

method, such as ``__hash__``, in the ``__dict__`` of an instance ``c`` of 

``C``, because Python will for efficiency reasons always use what is 

provided by the class, not by the instance. 

  

By consequence, if ``__hash__`` would be wrapped by using 

:class:`CachedMethod`, then ``hash(c)`` will access ``C.__hash__`` and bind 

it to ``c``, which means that the ``__get__`` method of 

:class:`CachedMethod` will be called. But there, we assume that Python has 

already inspected ``__dict__``, and thus a :class:`CachedMethodCaller` 

will be created over and over again. 

  

Here, the ``__get__`` method will explicitly access the ``__dict__``, so that 

``hash(c)`` will rely on a single :class:`CachedMethodCaller` stored in 

the ``__dict__``. 

  

EXAMPLES:: 

  

sage: class C: 

....: @cached_method 

....: def __hash__(self): 

....: print("compute hash") 

....: return int(5) 

....: 

sage: c = C() 

sage: type(C.__hash__) 

<type 'sage.misc.cachefunc.CachedMethodCallerNoArgs'> 

  

The hash is computed only once, subsequent calls will use the value from 

the cache. This was implemented in :trac:`12601`. 

  

:: 

  

sage: hash(c) # indirect doctest 

compute hash 

5 

sage: hash(c) 

5 

  

""" 

def __get__(self, object inst, cls): 

""" 

Bind a :class:`CachedMethodCaller` to a specific instance, using ``__dict__``. 

  

EXAMPLES:: 

  

sage: class C: 

....: @cached_method 

....: def __hash__(self): 

....: print("compute hash") 

....: return int(5) 

sage: c = C() 

sage: type(C.__hash__) 

<type 'sage.misc.cachefunc.CachedMethodCallerNoArgs'> 

sage: hash(c) # indirect doctest 

compute hash 

5 

sage: hash(c) 

5 

  

Verify that :trac:`16337` has been resolved:: 

  

sage: class Foo: 

....: @cached_method(key=lambda self, x:x+1) 

....: def __hash__(self, x=0): 

....: return x 

  

sage: a = Foo() 

sage: a.__hash__(0) 

0 

sage: a.__hash__.cache 

{1: 0} 

  

""" 

# This is for Parents or Elements that do not allow attribute assignment: 

cdef str name 

try: 

name = self._cachedfunc.__name__ 

except AttributeError: 

name = self.__name__ 

cdef dict D = None 

if inst is not None: 

try: 

D = inst.__dict__ 

except (TypeError, AttributeError): 

try: 

D = inst.__cached_methods 

except (TypeError, AttributeError): 

raise TypeError("For a cached special method, either attribute assignment or a public '__cached_methods' attribute of type <dict> is needed") 

if D is None: 

# This can only happen in the case of __cached_methods 

D = inst.__cached_methods = {} 

else: 

try: 

return D[name] 

except KeyError: 

pass 

# Apparently we need to construct the caller. 

# Since we have an optimized version for functions that do not accept arguments, 

# we need to analyse the argspec 

f = self._cachedfunc.f 

if self.nargs == 0: 

args, varargs, keywords, defaults = sage_getargspec(f) 

if varargs is None and keywords is None and len(args)<=1: 

self.nargs = 1 

Caller = CachedMethodCallerNoArgs(inst, f, name=name, do_pickle=self._cachedfunc.do_pickle) 

else: 

self.nargs = 2 # don't need the exact number 

Caller = CachedMethodCaller(self, inst, 

cache=self._get_instance_cache(inst), 

name=name, 

key=self._cachedfunc.key, 

do_pickle=self._cachedfunc.do_pickle) 

elif self.nargs == 1: 

Caller = CachedMethodCallerNoArgs(inst, f, name=name, do_pickle=self._cachedfunc.do_pickle) 

else: 

Caller = CachedMethodCaller(self, inst, 

cache=self._get_instance_cache(inst), 

name=name, 

key=self._cachedfunc.key, 

do_pickle=self._cachedfunc.do_pickle) 

if inst is not None: 

try: 

setattr(inst,name, Caller) 

return Caller 

except AttributeError: 

pass 

D[name] = Caller 

return Caller 

  

@decorator_keywords 

def cached_method(f, name=None, key=None, do_pickle=None): 

""" 

A decorator for cached methods. 

  

EXAMPLES: 

  

In the following examples, one can see how a cached method works 

in application. Below, we demonstrate what is done behind the scenes:: 

  

sage: class C: 

....: @cached_method 

....: def __hash__(self): 

....: print("compute hash") 

....: return int(5) 

....: @cached_method 

....: def f(self, x): 

....: print("computing cached method") 

....: return x*2 

sage: c = C() 

sage: type(C.__hash__) 

<type 'sage.misc.cachefunc.CachedMethodCallerNoArgs'> 

sage: hash(c) 

compute hash 

5 

  

When calling a cached method for the second time with the same arguments, 

the value is gotten from the cache, so that a new computation is not 

needed:: 

  

sage: hash(c) 

5 

sage: c.f(4) 

computing cached method 

8 

sage: c.f(4) is c.f(4) 

True 

  

Different instances have distinct caches:: 

  

sage: d = C() 

sage: d.f(4) is c.f(4) 

computing cached method 

False 

sage: d.f.clear_cache() 

sage: c.f(4) 

8 

sage: d.f(4) 

computing cached method 

8 

  

Using cached methods for the hash and other special methods was 

implemented in :trac:`12601`, by means of :class:`CachedSpecialMethod`. We 

show that it is used behind the scenes:: 

  

sage: cached_method(c.__hash__) 

<sage.misc.cachefunc.CachedSpecialMethod object at ...> 

sage: cached_method(c.f) 

<sage.misc.cachefunc.CachedMethod object at ...> 

  

The parameter ``do_pickle`` can be used if the contents of the cache should be 

stored in a pickle of the cached method. This can be dangerous with special 

methods such as ``__hash__``:: 

  

sage: class C: 

....: @cached_method(do_pickle=True) 

....: def __hash__(self): 

....: return id(self) 

  

sage: import __main__ 

sage: __main__.C = C 

sage: c = C() 

sage: hash(c) # random output 

sage: d = loads(dumps(c)) 

sage: hash(d) == hash(c) 

True 

  

However, the contents of a method's cache are not pickled unless ``do_pickle`` 

is set:: 

  

sage: class C: 

....: @cached_method 

....: def __hash__(self): 

....: return id(self) 

  

sage: __main__.C = C 

sage: c = C() 

sage: hash(c) # random output 

sage: d = loads(dumps(c)) 

sage: hash(d) == hash(c) 

False 

  

""" 

cdef str fname = name or f.__name__ 

if fname in special_method_names: 

return CachedSpecialMethod(f, name, key=key, do_pickle=do_pickle) 

return CachedMethod(f, name, key=key, do_pickle=do_pickle) 

  

  

cdef class CachedInParentMethod(CachedMethod): 

r""" 

A decorator that creates a cached version of an instance 

method of a class. 

  

In contrast to :class:`CachedMethod`, 

the cache dictionary is an attribute of the parent of 

the instance to which the method belongs. 

  

ASSUMPTION: 

  

This way of caching works only if 

  

- the instances *have* a parent, and 

- the instances are hashable (they are part of the cache key) or they 

define :meth:`sage.structure.sage_object.SageObject._cache_key` 

  

NOTE: 

  

For proper behavior, the method must be a pure function (no side effects). 

If this decorator is used on a method, it will have identical output on 

equal elements. This is since the element is part of the hash key. 

Arguments to the method must be hashable or define 

:meth:`sage.structure.sage_object.SageObject._cache_key`. The instance it 

is assigned to must be hashable. 

  

Examples can be found at :mod:`~sage.misc.cachefunc`. 

  

""" 

  

def __init__(self, f, name=None, key=None, do_pickle=None): 

""" 

Constructs a new method with cache stored in the parent of the instance. 

  

See also ``cached_method`` and ``cached_function``. 

  

EXAMPLES:: 

  

sage: class MyParent(Parent): 

....: pass 

sage: class Foo: 

....: def __init__(self, x): 

....: self._x = x 

....: self._parent = MyParent() 

....: def parent(self): 

....: return self._parent 

....: @cached_in_parent_method #indirect doctest 

....: def f(self): 

....: return self._x^2 

sage: a = Foo(2) 

sage: a.f() 

4 

sage: b = Foo(3) 

sage: b.f() 

9 

sage: hasattr(a.parent(), '_cache__element_f') 

True 

  

For speeding up internal computations, this dictionary 

is also accessible as an attribute of the CachedMethodCaller 

(by :trac:`8611`):: 

  

sage: a.parent()._cache__element_f is a.f.cache 

True 

  

TESTS: 

  

Test that ``key`` works:: 

  

sage: class A(object): 

....: def __init__(self): 

....: self._parent = MyParent() 

....: def parent(self): return self._parent 

....: def _f_normalize(self, x, algorithm): return x 

....: @cached_in_parent_method(key=_f_normalize) 

....: def f(self, x, algorithm='default'): return x 

sage: a = A() 

sage: a.f(1, algorithm="default") is a.f(1) is a.f(1, algorithm="algorithm") 

True 

  

Test that ``do_pickle`` works. Usually the contents of the cache are not 

pickled:: 

  

sage: class A(object): 

....: def __init__(self): 

....: self._parent = MyParent() 

....: def parent(self): return self._parent 

....: @cached_in_parent_method 

....: def f(self, x): return x 

sage: import __main__ 

sage: __main__.A = A 

sage: __main__.MyParent = MyParent 

sage: a = A() 

sage: a.f(1) 

1 

sage: len(a.f.cache) 

1 

sage: b = loads(dumps(a)) 

sage: len(b.f.cache) 

0 

  

Pickling can be enabled with ``do_pickle``:: 

  

sage: class A(object): 

....: def __init__(self): 

....: self._parent = MyParent() 

....: def parent(self): return self._parent 

....: @cached_in_parent_method(do_pickle=True) 

....: def f(self, x): return x 

sage: __main__.A = A 

sage: a = A() 

sage: a.f(1) 

1 

sage: len(a.f.cache) 

1 

sage: b = loads(dumps(a)) 

sage: len(b.f.cache) 

1 

  

""" 

self._cache_name = '_cache__' + 'element_' + (name or f.__name__) 

self._cachedfunc = CachedFunction(f, classmethod=True, name=name, key=key, do_pickle=do_pickle) 

  

cpdef _get_instance_cache(self, inst): 

""" 

Return the cache dictionary, which is stored in the parent. 

  

EXAMPLES:: 

  

sage: class MyParent(Parent): 

....: pass 

sage: class Foo: 

....: def __init__(self, x): 

....: self._x = x 

....: _parent = MyParent() 

....: def parent(self): 

....: return self._parent 

....: def __eq__(self, other): 

....: return self._x^2 == other._x^2 

....: def __hash__(self): 

....: return hash(self._x^2) 

....: def __repr__(self): 

....: return 'My %s'%self._x 

....: @cached_in_parent_method 

....: def f(self): 

....: return self._x^3 

sage: a = Foo(2) 

sage: a.f() 

8 

sage: a.f.cache #indirect doctest 

{(My 2, ((), ())): 8} 

  

Since the key for the cache depends on equality of 

the instances, we obtain *identical* result for 

*equal* instance - even though in this particular 

example the result is wrong:: 

  

sage: b = Foo(-2) 

sage: a is not b 

True 

sage: a == b 

True 

sage: b.f() is a.f() 

True 

  

Non-equal instances do not share the result of 

the cached method, but they do share the cache:: 

  

sage: c = Foo(3) 

sage: c.f() 

27 

sage: c.f.cache is a.f.cache #indirect doctest 

True 

  

Note that the cache is also available as an 

attribute of the cached method, which speeds 

up internal computations:: 

  

sage: a.f.cache is b.f.cache is c.f._cachedmethod._get_instance_cache(c) 

True 

  

""" 

default = {} if self._cachedfunc.do_pickle else NonpicklingDict() 

if inst is None: 

return default 

try: 

P = inst.parent() 

return P.__dict__.setdefault(self._cache_name, default) 

except AttributeError: 

pass 

if not hasattr(P,'__cached_methods'): 

raise TypeError("The parent of this element does not allow attribute assignment\n" + 

" and does not descend from the Parent base class.\n" + 

" Can not use CachedInParentMethod.") 

if P.__cached_methods is None: 

P.__cached_methods = {} 

return (<dict>P.__cached_methods).setdefault(self._cache_name, default) 

  

def __get__(self, inst, cls): 

""" 

Get a CachedMethodCaller bound to this specific instance of 

the class of the cached-in-parent method. 

""" 

Caller = GloballyCachedMethodCaller(self, inst, cache=self._get_instance_cache(inst), key=self._cachedfunc.key, do_pickle=self._cachedfunc.do_pickle) 

try: 

setattr(inst,self._cachedfunc.__name__, Caller) 

except AttributeError: 

pass 

return Caller 

  

  

cached_in_parent_method = decorator_keywords(CachedInParentMethod) 

  

  

class FileCache(object): 

""" 

:class:`FileCache` is a dictionary-like class which stores keys 

and values on disk. The keys take the form of a tuple ``(A,K)`` 

  

- ``A`` is a tuple of objects ``t`` where each ``t`` is an 

exact object which is uniquely identified by a short string. 

  

- ``K`` is a tuple of tuples ``(s,v)`` where ``s`` is a valid 

variable name and ``v`` is an exact object which is uniquely 

identified by a short string with letters [a-zA-Z0-9-._] 

  

The primary use case is the :class:`DiskCachedFunction`. If 

``memory_cache == True``, we maintain a cache of objects seen 

during this session in memory -- but we don't load them from 

disk until necessary. The keys and values are stored in a 

pair of files: 

  

- ``prefix-argstring.key.sobj`` contains the ``key`` only, 

- ``prefix-argstring.sobj`` contains the tuple ``(key,val)`` 

  

where ``self[key] == val``. 

  

.. NOTE:: 

  

We assume that each :class:`FileCache` lives in its own directory. 

Use **extreme** caution if you wish to break that assumption. 

""" 

def __init__(self, dir, prefix='', memory_cache=False): 

""" 

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import FileCache 

sage: dir = tmp_dir() 

sage: FC = FileCache(dir, memory_cache = True) 

sage: FC[((),())] = 1 

sage: FC[((1,2),())] = 2 

sage: FC[((),())] 

1 

""" 

from sage.misc.misc import sage_makedirs 

if len(dir) == 0 or dir[-1] != '/': 

dir += '/' 

self._dir = dir 

sage_makedirs(dir) 

  

self._prefix = prefix + '-' 

  

if memory_cache: 

self._cache = {} 

else: 

self._cache = None 

  

def file_list(self): 

""" 

Return the list of files corresponding to ``self``. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import FileCache 

sage: dir = tmp_dir() 

sage: FC = FileCache(dir, memory_cache = True, prefix='t') 

sage: FC[((),())] = 1 

sage: FC[((1,2),())] = 2 

sage: FC[((1,),(('a',1),))] = 3 

sage: for f in sorted(FC.file_list()): print(f[len(dir):]) 

t-.key.sobj 

t-.sobj 

t-1_2.key.sobj 

t-1_2.sobj 

t-a-1.1.key.sobj 

t-a-1.1.sobj 

""" 

files = [] 

prefix = self._prefix 

dir = self._dir 

l = len(prefix) 

for f in os.listdir(dir): 

if f[:l] == prefix: 

files.append( dir + f ) 

return files 

  

def items(self): 

""" 

Return a list of tuples ``(k,v)`` where ``self[k] = v``. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import FileCache 

sage: dir = tmp_dir() 

sage: FC = FileCache(dir, memory_cache = False) 

sage: FC[((),())] = 1 

sage: FC[((1,2),())] = 2 

sage: FC[((1,),(('a',1),))] = 3 

sage: I = FC.items() 

sage: I.sort(); I 

[(((), ()), 1), (((1,), (('a', 1),)), 3), (((1, 2), ()), 2)] 

""" 

return [(k, self[k]) for k in self] 

  

def values(self): 

""" 

Return a list of values that are stored in ``self``. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import FileCache 

sage: dir = tmp_dir() 

sage: FC = FileCache(dir, memory_cache = False) 

sage: FC[((),())] = 1 

sage: FC[((1,2),())] = 2 

sage: FC[((1,),(('a',1),))] = 3 

sage: FC[((),(('a',1),))] = 4 

sage: v = FC.values() 

sage: v.sort(); v 

[1, 2, 3, 4] 

""" 

return [self[k] for k in self] 

  

def __iter__(self): 

""" 

Return a list of keys of ``self``. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import FileCache 

sage: dir = tmp_dir() 

sage: FC = FileCache(dir, memory_cache = False) 

sage: FC[((),())] = 1 

sage: FC[((1,2),())] = 2 

sage: FC[((1,),(('a',1),))] = 3 

sage: for k in sorted(FC): print(k) 

((), ()) 

((1,), (('a', 1),)) 

((1, 2), ()) 

""" 

return iter(self.keys()) 

  

def keys(self): 

""" 

Return a list of keys ``k`` where ``self[k]`` is defined. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import FileCache 

sage: dir = tmp_dir() 

sage: FC = FileCache(dir, memory_cache = False) 

sage: FC[((),())] = 1 

sage: FC[((1,2),())] = 2 

sage: FC[((1,),(('a',1),))] = 3 

sage: K = FC.keys() 

sage: K.sort(); K 

[((), ()), ((1,), (('a', 1),)), ((1, 2), ())] 

""" 

cdef list K = [] 

from sage.structure.sage_object import load 

for f in self.file_list(): 

if f[-9:] == '.key.sobj': 

K.append(load(f)) 

return K 

  

def clear(self): 

""" 

Clear all key, value pairs from self and unlink the associated files 

from the file cache. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import FileCache 

sage: dir = tmp_dir() 

sage: FC1 = FileCache(dir, memory_cache=False, prefix='foo') 

sage: FC2 = FileCache(dir, memory_cache=False, prefix='foo') 

sage: k1 = ((), (('a', 1),)) 

sage: t1 = randint(0, 1000) 

sage: k2 = ((), (('b', 1),)) 

sage: t2 = randint(0, 1000) 

sage: FC1[k1] = t1 

sage: FC2[k2] = t2 

sage: FC2.clear() 

sage: k1 in FC1 

False 

sage: k2 in FC1 

False 

""" 

for k in self: 

del self[k] 

  

def _filename(self, key): 

""" 

Compute the filename associated with a certain key. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import FileCache 

sage: dir = tmp_dir() 

sage: FC = FileCache(dir, memory_cache = False, prefix='foo') 

sage: N = FC._filename(((1,2), (('a',1),('b',2)))) 

sage: print(N[len(dir):]) 

foo-a-1_b-2.1_2 

sage: N = FC._filename(((), (('a',1),('b',2)))) 

sage: print(N[len(dir):]) 

foo-a-1_b-2 

sage: N = FC._filename(((1,2), ())) 

sage: print(N[len(dir):]) 

foo-1_2 

""" 

a,k = key 

kwdstr = '_'.join(['%s-%s'%x for x in k]) 

argstr = '_'.join(['%s'%x for x in a]) 

if kwdstr and argstr: 

keystr = kwdstr + '.' + argstr 

else: 

keystr = kwdstr + argstr 

return self._dir + self._prefix + keystr 

  

def __contains__(self, key): 

""" 

Return ``True`` if ``self[key]`` is defined and ``False`` otherwise. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import FileCache 

sage: dir = tmp_dir() 

sage: FC = FileCache(dir, memory_cache = False, prefix='foo') 

sage: k = ((),(('a',1),)) 

sage: FC[k] = True 

sage: k in FC 

True 

sage: ((),()) in FC 

False 

""" 

return os.path.exists(self._filename(key) + '.key.sobj') 

  

def __getitem__(self, key): 

""" 

Return the value set by ``self[key] = val``, in this session 

or a previous one. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import FileCache 

sage: dir = tmp_dir() 

sage: FC1 = FileCache(dir, memory_cache = False, prefix='foo') 

sage: FC2 = FileCache(dir, memory_cache = False, prefix='foo') 

sage: k = ((),(('a',1),)) 

sage: t = randint(0, 1000) 

sage: FC1[k] = t 

sage: FC2[k] == FC1[k] == t 

True 

sage: FC1[(1,2),(('a',4),('b',2))] 

Traceback (most recent call last): 

... 

KeyError: ((1, 2), (('a', 4), ('b', 2))) 

  

""" 

from sage.structure.sage_object import load 

  

cache = self._cache 

if cache is not None: 

if key in cache: 

return cache[key] 

  

f = self._filename(key) + '.sobj' 

try: 

k, v = load(f) 

except IOError: 

raise KeyError(key) 

if k != key: 

raise RuntimeError("cache corrupted") 

  

if cache is not None: 

cache[key] = v 

return v 

  

def __setitem__(self, key, value): 

""" 

Sets ``self[key] = value`` and stores both key and value on 

disk. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import FileCache 

sage: dir = tmp_dir() 

sage: FC1 = FileCache(dir, memory_cache = False, prefix='foo') 

sage: FC2 = FileCache(dir, memory_cache = False, prefix='foo') 

sage: k = ((),(('a',1),)) 

sage: t = randint(0, 1000) 

sage: FC1[k] = t 

sage: FC2[k] == t 

True 

sage: FC1[k] = 2000 

sage: FC2[k]!= t 

True 

""" 

from sage.structure.sage_object import save 

  

f = self._filename(key) 

  

save(key, f+'.key.sobj') 

save((key,value), f + '.sobj') 

if self._cache is not None: 

self._cache[key] = value 

  

def __delitem__(self, key): 

""" 

Delete the key,value pair from self and unlink the associated 

files from the file cache. 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import FileCache 

sage: dir = tmp_dir() 

sage: FC1 = FileCache(dir, memory_cache = False, prefix='foo') 

sage: FC2 = FileCache(dir, memory_cache = False, prefix='foo') 

sage: k = ((),(('a',1),)) 

sage: t = randint(0, 1000) 

sage: FC1[k] = t 

sage: del FC2[k] 

sage: k in FC1 

False 

""" 

f = self._filename(key) 

cache = self._cache 

if cache is not None and key in cache: 

del self._cache[key] 

if os.path.exists(f + '.sobj'): 

os.remove(f + '.sobj') 

if os.path.exists(f + '.key.sobj'): 

os.remove(f + '.key.sobj') 

  

  

class DiskCachedFunction(CachedFunction): 

""" 

Works similar to CachedFunction, but instead, we keep the 

cache on disk (optionally, we keep it in memory too). 

  

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import DiskCachedFunction 

sage: dir = tmp_dir() 

sage: factor = DiskCachedFunction(factor, dir, memory_cache=True) 

sage: f = factor(2775); f 

3 * 5^2 * 37 

sage: f is factor(2775) 

True 

""" 

def __init__(self, f, dir, memory_cache=False, key=None): 

""" 

EXAMPLES:: 

  

sage: from sage.misc.cachefunc import DiskCachedFunction 

sage: def foo(x): sleep(x) 

sage: dir = tmp_dir() 

sage: bar = DiskCachedFunction(foo, dir, memory_cache = False) 

sage: w = walltime() 

sage: for i in range(10): bar(1) 

sage: walltime(w) < 2 

True 

""" 

CachedFunction.__init__(self, f, key=key) 

prefix = f.__name__ 

self.cache = FileCache(dir, prefix=prefix, memory_cache = memory_cache) 

  

  

class disk_cached_function: 

""" 

Decorator for :class:`DiskCachedFunction`. 

  

EXAMPLES:: 

  

sage: dir = tmp_dir() 

sage: @disk_cached_function(dir) 

....: def foo(x): return next_prime(2^x)%x 

sage: x = foo(200);x 

11 

sage: @disk_cached_function(dir) 

....: def foo(x): return 1/x 

sage: foo(200) 

11 

sage: foo.clear_cache() 

sage: foo(200) 

1/200 

""" 

def __init__(self, dir, memory_cache = False, key=None): 

""" 

EXAMPLES:: 

  

sage: dir = tmp_dir() 

sage: @disk_cached_function(dir, memory_cache=True) 

....: def foo(x): return next_prime(2^x) 

sage: x = foo(200) 

sage: x is foo(200) 

True 

sage: @disk_cached_function(dir, memory_cache=False) 

....: def foo(x): return next_prime(2^x) 

sage: x is foo(200) 

False 

""" 

self._dir = dir 

self._memory_cache = memory_cache 

self._key = key 

  

def __call__(self, f): 

""" 

EXAMPLES:: 

  

sage: dir = tmp_dir() 

sage: @disk_cached_function(dir) 

....: def foo(x): return ModularSymbols(x) 

sage: foo(389) 

Modular Symbols space of dimension 65 for Gamma_0(389) of weight 2 with sign 0 over Rational Field 

""" 

return DiskCachedFunction(f, self._dir, memory_cache=self._memory_cache, key=self._key) 

  

  

# Add support for _instancedoc_ 

from sage.docs.instancedoc import instancedoc 

instancedoc(CachedFunction) 

instancedoc(WeakCachedFunction) 

instancedoc(CachedMethodCaller) 

instancedoc(CachedMethodCallerNoArgs) 

instancedoc(GloballyCachedMethodCaller) 

instancedoc(DiskCachedFunction)