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

""" 

Utility functions 

 

This module contains various utility functions and classes used in doctesting. 

 

AUTHORS: 

 

- David Roe (2012-03-27) -- initial version, based on Robert Bradshaw's code. 

""" 

 

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

# Copyright (C) 2012 David Roe <roed.math@gmail.com> 

# Robert Bradshaw <robertwb@gmail.com> 

# William Stein <wstein@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 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 

from six import iteritems 

 

from sage.misc.misc import walltime, cputime 

 

def count_noun(number, noun, plural=None, pad_number=False, pad_noun=False): 

""" 

EXAMPLES:: 

 

sage: from sage.doctest.util import count_noun 

sage: count_noun(1, "apple") 

'1 apple' 

sage: count_noun(1, "apple", pad_noun=True) 

'1 apple ' 

sage: count_noun(1, "apple", pad_number=3) 

' 1 apple' 

sage: count_noun(2, "orange") 

'2 oranges' 

sage: count_noun(3, "peach", "peaches") 

'3 peaches' 

sage: count_noun(1, "peach", plural="peaches", pad_noun=True) 

'1 peach ' 

""" 

if plural is None: 

plural = noun + "s" 

if pad_noun: 

# We assume that the plural is never shorter than the noun.... 

pad_noun = " " * (len(plural) - len(noun)) 

else: 

pad_noun = "" 

if pad_number: 

number_str = ("%%%sd"%pad_number)%number 

else: 

number_str = "%d"%number 

if number == 1: 

return "%s %s%s"%(number_str, noun, pad_noun) 

else: 

return "%s %s"%(number_str, plural) 

 

 

def dict_difference(self, other): 

""" 

Return a dict with all key-value pairs occuring in ``self`` but not 

in ``other``. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import dict_difference 

sage: d1 = {1: 'a', 2: 'b', 3: 'c'} 

sage: d2 = {1: 'a', 2: 'x', 4: 'c'} 

sage: dict_difference(d2, d1) 

{2: 'x', 4: 'c'} 

 

:: 

 

sage: from sage.doctest.control import DocTestDefaults 

sage: D1 = DocTestDefaults() 

sage: D2 = DocTestDefaults(foobar="hello", timeout=100) 

sage: dict_difference(D2.__dict__, D1.__dict__) 

{'foobar': 'hello', 'timeout': 100} 

""" 

D = dict() 

for k, v in iteritems(self): 

try: 

if other[k] == v: 

continue 

except KeyError: 

pass 

D[k] = v 

return D 

 

 

class Timer: 

""" 

A simple timer. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import Timer 

sage: Timer() 

{} 

sage: TestSuite(Timer()).run() 

""" 

def start(self): 

""" 

Start the timer. 

 

Can be called multiple times to reset the timer. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import Timer 

sage: Timer().start() 

{'cputime': ..., 'walltime': ...} 

""" 

self.cputime = cputime() 

self.walltime = walltime() 

return self 

 

def stop(self): 

""" 

Stops the timer, recording the time that has passed since it 

was started. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import Timer 

sage: import time 

sage: timer = Timer().start() 

sage: time.sleep(0.5) 

sage: timer.stop() 

{'cputime': ..., 'walltime': ...} 

""" 

self.cputime = cputime(self.cputime) 

self.walltime = walltime(self.walltime) 

return self 

 

def annotate(self, object): 

""" 

Annotates the given object with the cputime and walltime 

stored in this timer. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import Timer 

sage: Timer().start().annotate(EllipticCurve) 

sage: EllipticCurve.cputime # random 

2.817255 

sage: EllipticCurve.walltime # random 

1332649288.410404 

""" 

object.cputime = self.cputime 

object.walltime = self.walltime 

 

def __repr__(self): 

""" 

String representation is from the dictionary. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import Timer 

sage: repr(Timer().start()) # indirect doctest 

"{'cputime': ..., 'walltime': ...}" 

""" 

return str(self) 

 

def __str__(self): 

""" 

String representation is from the dictionary. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import Timer 

sage: str(Timer().start()) # indirect doctest 

"{'cputime': ..., 'walltime': ...}" 

""" 

return str(self.__dict__) 

 

def __eq__(self, other): 

""" 

Comparison. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import Timer 

sage: Timer() == Timer() 

True 

sage: t = Timer().start() 

sage: loads(dumps(t)) == t 

True 

""" 

if not isinstance(other, Timer): 

return False 

return self.__dict__ == other.__dict__ 

 

def __ne__(self, other): 

""" 

Test for unequality 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import Timer 

sage: Timer() == Timer() 

True 

sage: t = Timer().start() 

sage: loads(dumps(t)) != t 

False 

""" 

return not (self == other) 

 

 

# Inheritance rather then delegation as globals() must be a dict 

class RecordingDict(dict): 

""" 

This dictionary is used for tracking the dependencies of an example. 

 

This feature allows examples in different doctests to be grouped 

for better timing data. It's obtained by recording whenever 

anything is set or retrieved from this dictionary. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import RecordingDict 

sage: D = RecordingDict(test=17) 

sage: D.got 

set() 

sage: D['test'] 

17 

sage: D.got 

{'test'} 

sage: D.set 

set() 

sage: D['a'] = 1 

sage: D['a'] 

1 

sage: D.set 

{'a'} 

sage: D.got 

{'test'} 

 

TESTS:: 

 

sage: TestSuite(D).run() 

""" 

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

""" 

Initialization arguments are the same as for a normal dictionary. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import RecordingDict 

sage: D = RecordingDict(d = 42) 

sage: D.got 

set() 

""" 

dict.__init__(self, *args, **kwds) 

self.start() 

 

def start(self): 

""" 

We track which variables have been set or retrieved. 

This function initializes these lists to be empty. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import RecordingDict 

sage: D = RecordingDict(d = 42) 

sage: D.set 

set() 

sage: D['a'] = 4 

sage: D.set 

{'a'} 

sage: D.start(); D.set 

set() 

""" 

self.set = set([]) 

self.got = set([]) 

 

def __getitem__(self, name): 

""" 

EXAMPLES:: 

 

sage: from sage.doctest.util import RecordingDict 

sage: D = RecordingDict(d = 42) 

sage: D['a'] = 4 

sage: D.got 

set() 

sage: D['a'] # indirect doctest 

4 

sage: D.got 

set() 

sage: D['d'] 

42 

sage: D.got 

{'d'} 

""" 

if name not in self.set: 

self.got.add(name) 

return dict.__getitem__(self, name) 

 

def __setitem__(self, name, value): 

""" 

EXAMPLES:: 

 

sage: from sage.doctest.util import RecordingDict 

sage: D = RecordingDict(d = 42) 

sage: D['a'] = 4 # indirect doctest 

sage: D.set 

{'a'} 

""" 

self.set.add(name) 

dict.__setitem__(self, name, value) 

 

def __delitem__(self, name): 

""" 

EXAMPLES:: 

 

sage: from sage.doctest.util import RecordingDict 

sage: D = RecordingDict(d = 42) 

sage: del D['d'] # indirect doctest 

sage: D.set 

{'d'} 

""" 

self.set.add(name) 

dict.__delitem__(self, name) 

 

def get(self, name, default=None): 

""" 

EXAMPLES:: 

 

sage: from sage.doctest.util import RecordingDict 

sage: D = RecordingDict(d = 42) 

sage: D.get('d') 

42 

sage: D.got 

{'d'} 

sage: D.get('not_here') 

sage: sorted(list(D.got)) 

['d', 'not_here'] 

""" 

if name not in self.set: 

self.got.add(name) 

return dict.get(self, name, default) 

 

def copy(self): 

""" 

Note that set and got are not copied. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import RecordingDict 

sage: D = RecordingDict(d = 42) 

sage: D['a'] = 4 

sage: D.set 

{'a'} 

sage: E = D.copy() 

sage: E.set 

set() 

sage: sorted(E.keys()) 

['a', 'd'] 

""" 

return RecordingDict(dict.copy(self)) 

 

def __reduce__(self): 

""" 

Pickling. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import RecordingDict 

sage: D = RecordingDict(d = 42) 

sage: D['a'] = 4 

sage: D.get('not_here') 

sage: E = loads(dumps(D)) 

sage: E.got 

{'not_here'} 

""" 

return make_recording_dict, (dict(self), self.set, self.got) 

 

def make_recording_dict(D, st, gt): 

""" 

Auxilliary function for pickling. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import make_recording_dict 

sage: D = make_recording_dict({'a':4,'d':42},set([]),set(['not_here'])) 

sage: sorted(D.items()) 

[('a', 4), ('d', 42)] 

sage: D.got 

{'not_here'} 

""" 

ans = RecordingDict(D) 

ans.set = st 

ans.got = gt 

return ans 

 

class NestedName: 

""" 

Class used to construct fully qualified names based on indentation level. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import NestedName 

sage: qname = NestedName('sage.categories.algebras') 

sage: qname[0] = 'Algebras'; qname 

sage.categories.algebras.Algebras 

sage: qname[4] = '__contains__'; qname 

sage.categories.algebras.Algebras.__contains__ 

sage: qname[4] = 'ParentMethods' 

sage: qname[8] = 'from_base_ring'; qname 

sage.categories.algebras.Algebras.ParentMethods.from_base_ring 

 

TESTS:: 

 

sage: TestSuite(qname).run() 

""" 

def __init__(self, base): 

""" 

INPUT: 

 

- base -- a string: the name of the module. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import NestedName 

sage: qname = NestedName('sage.categories.algebras') 

sage: qname 

sage.categories.algebras 

""" 

self.all = [base] 

 

def __setitem__(self, index, value): 

""" 

Sets the value at a given indentation level. 

 

INPUT: 

 

- index -- a positive integer, the indentation level (often a multiple of 4, but not necessarily) 

- value -- a string, the name of the class or function at that indentation level. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import NestedName 

sage: qname = NestedName('sage.categories.algebras') 

sage: qname[1] = 'Algebras' # indirect doctest 

sage: qname 

sage.categories.algebras.Algebras 

sage: qname.all 

['sage.categories.algebras', None, 'Algebras'] 

""" 

if index < 0: 

raise ValueError 

while len(self.all) <= index: 

self.all.append(None) 

self.all[index+1:] = [value] 

 

def __str__(self): 

""" 

Returns a .-separated string giving the full name. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import NestedName 

sage: qname = NestedName('sage.categories.algebras') 

sage: qname[1] = 'Algebras' 

sage: qname[44] = 'at_the_end_of_the_universe' 

sage: str(qname) # indirect doctest 

'sage.categories.algebras.Algebras.at_the_end_of_the_universe' 

""" 

return repr(self) 

 

def __repr__(self): 

""" 

Returns a .-separated string giving the full name. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import NestedName 

sage: qname = NestedName('sage.categories.algebras') 

sage: qname[1] = 'Algebras' 

sage: qname[44] = 'at_the_end_of_the_universe' 

sage: print(qname) # indirect doctest 

sage.categories.algebras.Algebras.at_the_end_of_the_universe 

""" 

return '.'.join(a for a in self.all if a is not None) 

 

def __eq__(self, other): 

""" 

Comparison is just comparison of the underlying lists. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import NestedName 

sage: qname = NestedName('sage.categories.algebras') 

sage: qname2 = NestedName('sage.categories.algebras') 

sage: qname == qname2 

True 

sage: qname[0] = 'Algebras' 

sage: qname2[2] = 'Algebras' 

sage: repr(qname) == repr(qname2) 

True 

sage: qname == qname2 

False 

""" 

if not isinstance(other, NestedName): 

return False 

return self.all == other.all 

 

def __ne__(self, other): 

""" 

Test for unequality. 

 

EXAMPLES:: 

 

sage: from sage.doctest.util import NestedName 

sage: qname = NestedName('sage.categories.algebras') 

sage: qname2 = NestedName('sage.categories.algebras') 

sage: qname != qname2 

False 

sage: qname[0] = 'Algebras' 

sage: qname2[2] = 'Algebras' 

sage: repr(qname) == repr(qname2) 

True 

sage: qname != qname2 

True 

""" 

return not (self == other)