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

r""" 

Enumerated set from iterator 

 

EXAMPLES: 

 

We build a set from the iterator ``graphs`` that returns a canonical 

representative for each isomorphism class of graphs:: 

 

sage: from sage.sets.set_from_iterator import EnumeratedSetFromIterator 

sage: E = EnumeratedSetFromIterator( 

....: graphs, 

....: name = "Graphs", 

....: category = InfiniteEnumeratedSets(), 

....: cache = True) 

sage: E 

Graphs 

sage: E.unrank(0) 

Graph on 0 vertices 

sage: E.unrank(4) 

Graph on 3 vertices 

sage: E.cardinality() 

+Infinity 

sage: E.category() 

Category of facade infinite enumerated sets 

 

The module also provides decorator for functions and methods:: 

 

sage: from sage.sets.set_from_iterator import set_from_function 

sage: @set_from_function 

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

sage: f(3) 

{0, 1, 2} 

sage: f(5) 

{0, 1, 2, 3, 4} 

sage: f(100) 

{0, 1, 2, 3, 4, ...} 

 

sage: from sage.sets.set_from_iterator import set_from_method 

sage: class A: 

....: @set_from_method 

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

....: return xsrange(n) 

sage: a = A() 

sage: a.f(3) 

{0, 1, 2} 

sage: f(100) 

{0, 1, 2, 3, 4, ...} 

""" 

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

# Copyright (C) 2012 Vincent Delecroix <vincent.delecroix@gmail.com> 

# 

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

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

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

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

from __future__ import print_function 

from six.moves import range 

 

from sage.structure.parent import Parent 

from sage.categories.enumerated_sets import EnumeratedSets 

from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets 

from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets 

from sage.categories.sets_cat import EmptySetError 

 

import os 

from sage.misc.function_mangling import ArgumentFixer 

from sage.misc.lazy_list import lazy_list 

from sage.docs.instancedoc import instancedoc 

 

 

class EnumeratedSetFromIterator(Parent): 

""" 

A class for enumerated set built from an iterator. 

 

INPUT: 

 

- ``f`` -- a function that returns an iterable from which the set is built from 

 

- ``args`` -- tuple -- arguments to be sent to the function ``f`` 

 

- ``kwds`` -- dictionary -- keywords to be sent to the function ``f`` 

 

- ``name`` -- an optional name for the set 

 

- ``category`` -- (default: ``None``) an optional category for that 

enumerated set. If you know that your iterator will stop after a finite 

number of steps you should set it as :class:`FiniteEnumeratedSets`, conversely if 

you know that your iterator will run over and over you should set it as 

:class:`InfiniteEnumeratedSets`. 

 

- ``cache`` -- boolean (default: ``False``) -- Whether or not use a cache 

mechanism for the iterator. If ``True``, then the function ``f`` is called 

only once. 

 

 

EXAMPLES:: 

 

sage: from sage.sets.set_from_iterator import EnumeratedSetFromIterator 

sage: E = EnumeratedSetFromIterator(graphs, args = (7,)) 

sage: E 

{Graph on 7 vertices, Graph on 7 vertices, Graph on 7 vertices, Graph on 7 vertices, Graph on 7 vertices, ...} 

sage: E.category() 

Category of facade enumerated sets 

 

The same example with a cache and a custom name:: 

 

sage: E = EnumeratedSetFromIterator( 

....: graphs, 

....: args = (8,), 

....: category = FiniteEnumeratedSets(), 

....: name = "Graphs with 8 vertices", 

....: cache = True) 

sage: E 

Graphs with 8 vertices 

sage: E.unrank(3) 

Graph on 8 vertices 

sage: E.category() 

Category of facade finite enumerated sets 

 

TESTS: 

 

The cache is compatible with multiple call to ``__iter__``:: 

 

sage: from itertools import count 

sage: E = EnumeratedSetFromIterator(count, args=(0,), category=InfiniteEnumeratedSets(), cache=True) 

sage: e1 = iter(E) 

sage: e2 = iter(E) 

sage: next(e1), next(e1) 

(0, 1) 

sage: next(e2), next(e2), next(e2) 

(0, 1, 2) 

sage: next(e1), next(e1) 

(2, 3) 

sage: next(e2) 

3 

 

The following warning is due to ``E`` being a facade parent. For more, 

see the discussion on :trac:`16239`:: 

 

sage: TestSuite(E).run() 

doctest:...: UserWarning: Testing equality of infinite sets which will not end in case of equality 

 

sage: E = EnumeratedSetFromIterator(xsrange, args=(10,), category=FiniteEnumeratedSets(), cache=True) 

sage: TestSuite(E).run() 

 

.. NOTE:: 

 

In order to make the ``TestSuite`` works, the elements of the set 

should have parents. 

""" 

def __init__(self, f, args=None, kwds=None, name=None, category=None, cache=False): 

""" 

TESTS:: 

 

sage: from sage.sets.set_from_iterator import EnumeratedSetFromIterator 

sage: S = EnumeratedSetFromIterator(xsrange, (1,200,-1), category=FiniteEnumeratedSets()) 

sage: TestSuite(S).run() 

""" 

if category is not None: 

Parent.__init__(self, facade = True, category = category) 

else: 

Parent.__init__(self, facade = True, category = EnumeratedSets()) 

 

 

if name is not None: 

self.rename(name) 

 

self._func = f 

 

if args is not None: 

self._args = args 

if kwds is not None: 

self._kwds = kwds 

 

if cache: 

self._cache = lazy_list(iter(self._func( 

*getattr(self, '_args', ()), 

**getattr(self, '_kwds', {})))) 

 

def __hash__(self): 

r""" 

A simple hash using the first elements of the set. 

 

EXAMPLES:: 

 

sage: from sage.sets.set_from_iterator import EnumeratedSetFromIterator 

sage: E = EnumeratedSetFromIterator(xsrange, (1,200)) 

sage: hash(E) 

4600916458883504074 # 64-bit 

-2063607862 # 32-bit 

""" 

try: 

return hash(self._cache[:13]) 

except AttributeError: 

from itertools import islice 

return hash(tuple(islice(self, 13))) 

 

def __reduce__(self): 

r""" 

Support for pickle. 

 

TESTS:: 

 

sage: from sage.sets.set_from_iterator import EnumeratedSetFromIterator 

sage: from sage.graphs.graph_generators import graphs 

sage: E = EnumeratedSetFromIterator(graphs, 

....: args=(3,), 

....: category=FiniteEnumeratedSets(), 

....: name="Graphs on 3 vertices") 

sage: E 

Graphs on 3 vertices 

sage: F = loads(dumps(E)); F 

Graphs on 3 vertices 

sage: E == F 

True 

""" 

return (EnumeratedSetFromIterator, 

(self._func, # func 

getattr(self, '_args', None), # args 

getattr(self, '_kwds', None), # kwds 

getattr(self, '__custom_name', None), # name 

self.category(), # category 

hasattr(self, '_cache')) # cache 

) 

 

def _repr_(self): 

r""" 

Return a string representation of ``self``. 

 

TESTS:: 

 

sage: from sage.sets.set_from_iterator import EnumeratedSetFromIterator 

sage: E = EnumeratedSetFromIterator(Partitions(7,min_part=2).__iter__) 

sage: repr(E) # indirect doctest 

'{[7], [5, 2], [4, 3], [3, 2, 2]}' 

sage: E = EnumeratedSetFromIterator(Partitions(9,min_part=2).__iter__) 

sage: repr(E) # indirect doctest 

'{[9], [7, 2], [6, 3], [5, 4], [5, 2, 2], ...}' 

sage: E = EnumeratedSetFromIterator(Partitions(9,min_part=2).__iter__, name="Some partitions") 

sage: repr(E) # indirect doctest 

'Some partitions' 

""" 

l = [] 

i = iter(self) 

for _ in range(6): 

try: 

l.append(next(i)) 

except StopIteration: 

break 

if len(l) < 6: 

return '{' + ', '.join(repr(x) for x in l) + '}' 

l.pop(-1) 

return '{' + ', '.join(repr(x) for x in l) + ', ...}' 

 

def __contains__(self, x): 

r""" 

Test whether ``x`` is in ``self``. 

 

If the set is infinite, only the answer ``True`` should be expected in 

finite time. 

 

EXAMPLES:: 

 

sage: from sage.sets.set_from_iterator import EnumeratedSetFromIterator 

sage: P = Partitions(12,min_part=2,max_part=5) 

sage: E = EnumeratedSetFromIterator(P.__iter__) 

sage: P([5,5,2]) in E 

True 

""" 

return any(x == y for y in self) 

 

is_parent_of = __contains__ 

 

#TODO: what should we do for comparisons of infinite sets 

def __eq__(self, other): 

r""" 

Equality test. 

 

The function returns ``True`` if and only if other is an enumerated 

set and has the same element as ``self``. 

 

TESTS:: 

 

sage: from sage.sets.set_from_iterator import EnumeratedSetFromIterator 

sage: E4 = EnumeratedSetFromIterator(graphs, args=(4,), category=FiniteEnumeratedSets()) 

sage: F4 = EnumeratedSetFromIterator(graphs, args=(4,), category=FiniteEnumeratedSets()) 

sage: E5 = EnumeratedSetFromIterator(graphs, args=(5,), category=FiniteEnumeratedSets()) 

sage: E4 == E4 

True 

sage: E4 == F4 

True 

sage: E4 == E5 

False 

sage: E5 == E4 

False 

sage: E5 == E5 

True 

""" 

if isinstance(other, EnumeratedSetFromIterator): 

# trick to allow equality between infinite sets 

# this assume that the function does not return randomized data! 

if (self._func == other._func and 

getattr(self, '_args', None) == getattr(other, '_args', None) and 

getattr(self, '_kwds', None) == getattr(other, '_kwds', None)): 

return True 

 

if other in EnumeratedSets(): 

#TODO: think about what should be done at that point 

if self not in FiniteEnumeratedSets() and other not in FiniteEnumeratedSets(): 

import warnings 

warnings.warn("Testing equality of infinite sets which will not end in case of equality") 

 

i1 = iter(self) 

i2 = iter(other) 

while True: 

try: 

x = next(i1) 

except StopIteration: 

try: 

next(i2) 

return False 

except StopIteration: 

return True 

try: 

y = next(i2) 

except StopIteration: 

return False 

if x != y: 

return False 

 

def __ne__(self,other): 

r""" 

Difference test. 

 

The function calls the ``__eq__`` test. 

 

TESTS:: 

 

sage: from sage.sets.set_from_iterator import EnumeratedSetFromIterator 

sage: E4 = EnumeratedSetFromIterator(graphs, args=(4,), category=FiniteEnumeratedSets()) 

sage: F4 = EnumeratedSetFromIterator(graphs, args=(4,), category=FiniteEnumeratedSets()) 

sage: E5 = EnumeratedSetFromIterator(graphs, args=(5,), category=FiniteEnumeratedSets()) 

sage: E4 != E4 

False 

sage: E4 != F4 

False 

sage: E4 != E5 

True 

sage: E5 != E4 

True 

sage: E5 != E5 

False 

""" 

return not self == other 

 

def __iter__(self): 

r""" 

Returns an iterator over the element of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.sets.set_from_iterator import EnumeratedSetFromIterator 

sage: E = EnumeratedSetFromIterator(graphs, args=(8,)) 

sage: g1 = next(iter(E)); g1 

Graph on 8 vertices 

sage: E = EnumeratedSetFromIterator(graphs, args=(8,), cache=True) 

sage: g2 = next(iter(E)); g2 

Graph on 8 vertices 

sage: g1 == g2 

True 

""" 

if hasattr(self, '_cache'): 

return iter(self._cache) 

return iter(self._func(*getattr(self, '_args', ()), **getattr(self, '_kwds', {}))) 

 

def unrank(self, i): 

r""" 

Returns the element at position ``i``. 

 

EXAMPLES:: 

 

sage: from sage.sets.set_from_iterator import EnumeratedSetFromIterator 

sage: E = EnumeratedSetFromIterator(graphs, args=(8,), cache=True) 

sage: F = EnumeratedSetFromIterator(graphs, args=(8,), cache=False) 

sage: E.unrank(2) 

Graph on 8 vertices 

sage: E.unrank(2) == F.unrank(2) 

True 

""" 

if hasattr(self, '_cache'): 

return self._cache[i] 

return super(EnumeratedSetFromIterator,self).unrank(i) 

 

def _element_constructor_(self, el): 

""" 

Construct an element from ``el``. 

 

TESTS:: 

 

sage: from sage.sets.set_from_iterator import EnumeratedSetFromIterator 

sage: from six.moves import range 

sage: S = EnumeratedSetFromIterator(range, args=(1,4)) 

sage: S(1) # indirect doctest 

1 

sage: S(0) # indirect doctest 

Traceback (most recent call last): 

... 

ValueError: 0 not in {1, 2, 3} 

""" 

if el in self: 

return el 

else: 

raise ValueError("%s not in %s"%(el, self)) 

 

def clear_cache(self): 

r""" 

Clear the cache. 

 

EXAMPLES:: 

 

sage: from itertools import count 

sage: from sage.sets.set_from_iterator import EnumeratedSetFromIterator 

sage: E = EnumeratedSetFromIterator(count, args=(1,), cache=True) 

sage: e1 = E._cache 

sage: e1 

lazy list [1, 2, 3, ...] 

sage: E.clear_cache() 

sage: E._cache 

lazy list [1, 2, 3, ...] 

sage: e1 is E._cache 

False 

""" 

if hasattr(self, '_cache'): 

self._cache = lazy_list(iter(self._func( 

*getattr(self, '_args', ()), 

**getattr(self, '_kwds', {})))) 

 

# 

# Decorators 

# 

 

#TODO: move it in sage.misc ? 

@instancedoc 

class Decorator(object): 

r""" 

Abstract class that manage documentation and sources of the wrapped object. 

 

The method needs to be stored in the attribute ``self.f`` 

""" 

def _instancedoc_(self): 

""" 

Provide documentation for the wrapped function. 

 

TESTS:: 

 

sage: from sage.misc.sageinspect import sage_getdoc 

sage: from sage.sets.set_from_iterator import Decorator 

sage: d = Decorator() 

sage: d.f = Integer.is_prime 

sage: print(sage_getdoc(d)) # indirect doctest 

Test whether "self" is prime. 

... 

Calls the PARI "isprime" function. 

""" 

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

f = self.f 

doc = f.__doc__ or '' 

if _extract_embedded_position(doc) is None: 

try: 

sourcelines = sage_getsourcelines(f) 

from sage.env import SAGE_LIB, SAGE_SRC 

filename = sage_getfile(f) 

# The following is a heuristics to get 

# the file name of the cached function 

# or method 

if filename.startswith(SAGE_SRC): 

filename = filename[len(SAGE_SRC):] 

elif filename.startswith(SAGE_LIB): 

filename = filename[len(SAGE_LIB):] 

file_info = "File: %s (starting at line %d)\n"%(filename,sourcelines[1]) 

doc = file_info+doc 

except IOError: 

pass 

return doc 

 

def _sage_src_(self): 

r""" 

Returns the source code for the wrapped function. 

 

TESTS:: 

 

sage: from sage.misc.sageinspect import sage_getsource 

sage: from sage.sets.set_from_iterator import Decorator 

sage: d = Decorator() 

sage: d.f = Rational.is_square 

sage: print(sage_getsource(d.f)) # indirect doctest 

def is_square(self): 

... 

return mpq_sgn(self.value) >= 0 and mpz_perfect_square_p(mpq_numref(self.value)) and mpz_perfect_square_p(mpq_denref(self.value)) 

""" 

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: from sage.misc.sageinspect import sage_getsourcelines 

sage: from sage.sets.set_from_iterator import Decorator 

sage: d = Decorator() 

sage: d.f = MathieuGroup.order 

sage: S = sage_getsourcelines(d) # indirect doctest 

sage: S[0][2] 

' Return the number of elements of this group.\n' 

sage: S[0][25] 

' return Integer(1)\n' 

""" 

from sage.misc.sageinspect import sage_getsourcelines 

return sage_getsourcelines(self.f) 

 

def _sage_argspec_(self): 

""" 

Return the argument specification of the wrapped function or method. 

 

TESTS:: 

 

sage: from sage.misc.sageinspect import sage_getargspec 

sage: from sage.sets.set_from_iterator import Decorator 

sage: d = Decorator() 

sage: d.f = find_local_minimum 

sage: sage_getargspec(d) # indirect doctest 

ArgSpec(args=['f', 'a', 'b', 'tol', 'maxfun'], varargs=None, keywords=None, defaults=(1.48e-08, 500)) 

""" 

from sage.misc.sageinspect import sage_getargspec 

return sage_getargspec(self.f) 

 

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

r""" 

Call function. 

 

Needs to be implemented in derived subclass. 

 

TESTS:: 

 

sage: from sage.sets.set_from_iterator import Decorator 

sage: d = Decorator() 

sage: d() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

 

 

@instancedoc 

class EnumeratedSetFromIterator_function_decorator(Decorator): 

r""" 

Decorator for :class:`EnumeratedSetFromIterator`. 

 

Name could be string or a function ``(args,kwds) -> string``. 

 

.. WARNING:: 

 

If you are going to use this with the decorator ``cached_function``, 

you must place the ``cached_function`` first. See the example below. 

 

EXAMPLES:: 

 

sage: from sage.sets.set_from_iterator import set_from_function 

sage: from six.moves import range 

sage: @set_from_function 

....: def f(n): 

....: for i in range(n): 

....: yield i**2 + i + 1 

sage: f(3) 

{1, 3, 7} 

sage: f(100) 

{1, 3, 7, 13, 21, ...} 

 

To avoid ambiguity, it is always better to use it with a call which 

provides optional global initialization for the call to 

:class:`EnumeratedSetFromIterator`:: 

 

sage: @set_from_function(category=InfiniteEnumeratedSets()) 

....: def Fibonacci(): 

....: a = 1; b = 2 

....: while True: 

....: yield a 

....: a, b = b, a + b 

sage: F = Fibonacci() 

sage: F 

{1, 2, 3, 5, 8, ...} 

sage: F.cardinality() 

+Infinity 

 

A simple example with many options:: 

 

sage: @set_from_function( 

....: name = "From %(m)d to %(n)d", 

....: category = FiniteEnumeratedSets()) 

....: def f(m, n): return xsrange(m,n+1) 

sage: E = f(3,10); E 

From 3 to 10 

sage: E.list() 

[3, 4, 5, 6, 7, 8, 9, 10] 

sage: E = f(1,100); E 

From 1 to 100 

sage: E.cardinality() 

100 

sage: f(n=100,m=1) == E 

True 

 

An example which mixes together ``set_from_function`` and 

``cached_method``:: 

 

sage: @cached_function 

....: @set_from_function( 

....: name = "Graphs on %(n)d vertices", 

....: category = FiniteEnumeratedSets(), 

....: cache = True) 

....: def Graphs(n): return graphs(n) 

sage: Graphs(10) 

Graphs on 10 vertices 

sage: Graphs(10).unrank(0) 

Graph on 10 vertices 

sage: Graphs(10) is Graphs(10) 

True 

 

The ``cached_function`` must go first:: 

 

sage: @set_from_function( 

....: name = "Graphs on %(n)d vertices", 

....: category = FiniteEnumeratedSets(), 

....: cache = True) 

....: @cached_function 

....: def Graphs(n): return graphs(n) 

sage: Graphs(10) 

Graphs on 10 vertices 

sage: Graphs(10).unrank(0) 

Graph on 10 vertices 

sage: Graphs(10) is Graphs(10) 

False 

""" 

def __init__(self, f=None, name=None, **options): 

r""" 

Initialize ``self``. 

 

TESTS:: 

 

sage: from sage.sets.set_from_iterator import set_from_function 

sage: F = set_from_function(category=FiniteEnumeratedSets())(xsrange) 

sage: TestSuite(F(100)).run() 

sage: TestSuite(F(1,5,2)).run() 

sage: TestSuite(F(0)).run() 

""" 

if f is not None: 

self.f = f 

if hasattr(f, "__name__"): 

self.__name__ = f.__name__ 

else: 

self.__name__ = f.__name__ 

self.__module__ = f.__module__ 

self.af = ArgumentFixer(f) 

if name is not None: 

self.name = name 

self.options = options 

 

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

r""" 

Build a new :class:`EnumeratedSet` by calling ``self.f`` with 

apropriate argument. If ``f`` is ``None``, then returns a new instance 

of :class:`EnumeratedSetFromIterator`. 

 

EXAMPLES:: 

 

sage: from sage.sets.set_from_iterator import set_from_function 

sage: F = set_from_function(category=FiniteEnumeratedSets())(xsrange) 

sage: F(3) 

{0, 1, 2} 

sage: F(end=7,start=3) 

{3, 4, 5, 6} 

sage: F(10).cardinality() 

10 

""" 

options = self.options 

 

if hasattr(self, 'f'): # yet initialized 

if hasattr(self,'name'): 

if isinstance(self.name,str): 

if args or kwds: 

_, kk = self.af.fix_to_named(*args,**kwds) 

name = self.name % dict(kk) 

else: 

name = self.name 

else: 

name = self.name(*args,**kwds) 

return EnumeratedSetFromIterator(self.f, args, kwds, name=name, **self.options) 

return EnumeratedSetFromIterator(self.f, args, kwds, **self.options) 

 

else: # potential global options 

if args == (): 

f, = kwds.values() 

else: 

assert len(args) == 1 

f = args[0] 

return EnumeratedSetFromIterator_function_decorator( 

f, 

name=getattr(self,'name',None), 

**self.options) 

 

set_from_function = EnumeratedSetFromIterator_function_decorator 

 

 

@instancedoc 

class EnumeratedSetFromIterator_method_caller(Decorator): 

r""" 

Caller for decorated method in class. 

 

INPUT: 

 

- ``inst`` -- an instance of a class 

 

- ``f`` -- a method of a class of ``inst`` (and not of the instance itself) 

 

- ``name`` -- optional -- either a string (which may contains substitution 

rules from argument or a function args,kwds -> string. 

 

- ``options`` -- any option accepted by :class:`EnumeratedSetFromIterator` 

""" 

def __init__(self, inst, f, name=None, **options): 

r""" 

Initialize ``self``. 

 

TESTS:: 

 

sage: from sage.sets.set_from_iterator import DummyExampleForPicklingTest 

sage: d = DummyExampleForPicklingTest() 

sage: d.f() 

{10, 11, 12, 13, 14, ...} 

 

It is possible to pickle/unpickle the class and the instance:: 

 

sage: loads(dumps(DummyExampleForPicklingTest))().f() 

{10, 11, 12, 13, 14, ...} 

sage: loads(dumps(d)).f() 

{10, 11, 12, 13, 14, ...} 

 

But not the enumerated set:: 

 

sage: loads(dumps(d.f())) 

Traceback (most recent call last): 

... 

PicklingError: Can't pickle <... 'function'>: attribute lookup __builtin__.function failed 

""" 

self.inst = inst 

self.f = f 

self.af = ArgumentFixer(self.f) 

if hasattr(f, "__name__"): 

self.__name__ = f.__name__ 

else: 

self.__name__ = f.__name__ 

self.__module__ = f.__module__ 

 

self.name = name 

self.options = options 

 

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

r""" 

Returns an instance of :class:`EnumeratedSetFromIterator` with 

proper argument. 

 

TESTS:: 

 

sage: from sage.sets.set_from_iterator import set_from_method 

sage: class A: 

....: @set_from_method(name = lambda self,n: str(self)*n) 

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

....: return xsrange(n) 

....: def __repr__(self): 

....: return "A" 

sage: a = A() 

sage: a.f(3) # indirect doctest 

AAA 

sage: A.f(a,3) # indirect doctest 

AAA 

sage: [x for x in a.f(6)] # indirect doctest 

[0, 1, 2, 3, 4, 5] 

""" 

if self.inst is not None: 

args = (self.inst,) + args 

if self.name: 

if isinstance(self.name,str): 

aa,kk = self.af.fix_to_named(*args,**kwds) 

name = self.name%dict(kk) 

else: 

name = self.name(*args, **kwds) 

return EnumeratedSetFromIterator(self.f, args, kwds, name, **self.options) 

return EnumeratedSetFromIterator(self.f, args, kwds, **self.options) 

 

def __get__(self, inst, cls): 

r""" 

Get a :class:`EnumeratedSetFromIterator_method_caller` bound to a 

specific instance of the class of the cached method. 

 

.. NOTE:: 

 

:class:`EnumeratedSetFromIterator_method_caller` has a separate 

``__get__`` because of the special behavior of category framework 

for element classes which are not of extension type (see 

:meth:`sage.structure.element.Element.__get__`). 

 

TESTS:: 

 

sage: from sage.sets.set_from_iterator import set_from_method 

sage: class A: 

....: stop = 10000 

....: @set_from_method 

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

....: return xsrange(start,self.stop) 

sage: a = A() 

sage: A.f(a,4) 

{4, 5, 6, 7, 8, ...} 

 

sage: class B: 

....: stop = 10000 

....: @set_from_method(category=FiniteEnumeratedSets()) 

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

....: return xsrange(start,self.stop) 

sage: b = B() 

sage: B.f(b,2) 

{2, 3, 4, 5, 6, ...} 

""" 

return EnumeratedSetFromIterator_method_caller( 

inst, self.f, 

self.name, 

**self.options) 

 

class EnumeratedSetFromIterator_method_decorator(object): 

r""" 

Decorator for enumerated set built from a method. 

 

INPUT: 

 

- ``f`` -- Optional function from which are built the enumerated sets at 

each call 

 

- ``name`` -- Optional string (which may contains substitution rules from 

argument) or a function ``(args,kwds) -> string``. 

 

- any option accepted by :class:`EnumeratedSetFromIterator`. 

 

EXAMPLES:: 

 

sage: from sage.sets.set_from_iterator import set_from_method 

sage: class A(): 

....: def n(self): return 12 

....: @set_from_method 

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

sage: a = A() 

sage: print(a.f.__class__) 

<class 'sage.sets.set_from_iterator.EnumeratedSetFromIterator_method_caller'> 

sage: a.f() 

{0, 1, 2, 3, 4, ...} 

sage: A.f(a) 

{0, 1, 2, 3, 4, ...} 

 

A more complicated example with a parametrized name:: 

 

sage: class B(): 

....: @set_from_method( 

....: name = "Graphs(%(n)d)", 

....: category = FiniteEnumeratedSets()) 

....: def graphs(self, n): return graphs(n) 

sage: b = B() 

sage: G3 = b.graphs(3) 

sage: G3 

Graphs(3) 

sage: G3.cardinality() 

4 

sage: G3.category() 

Category of facade finite enumerated sets 

sage: B.graphs(b,3) 

Graphs(3) 

 

And a last example with a name parametrized by a function:: 

 

sage: class D(): 

....: def __init__(self, name): self.name = str(name) 

....: def __str__(self): return self.name 

....: @set_from_method( 

....: name = lambda self,n: str(self)*n, 

....: category = FiniteEnumeratedSets()) 

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

....: return xsrange(n) 

sage: d = D('a') 

sage: E = d.subset(3); E 

aaa 

sage: E.list() 

[0, 1, 2] 

sage: F = d.subset(n=10); F 

aaaaaaaaaa 

sage: F.list() 

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

 

.. TODO:: 

 

It is not yet possible to use ``set_from_method`` in conjunction with 

``cached_method``. 

""" 

def __init__(self, f=None, **options): 

r""" 

Initialize ``self``. 

 

TESTS: 

 

We test if pickling works correctly on the Permutation class (in 

:mod:`sage.combinat.permutation`) because its method ``bruhat_succ`` 

and ``bruhat_pred`` are decorated with ``set_from_method``:: 

 

sage: from sage.combinat.permutation import Permutation 

sage: loads(dumps(Permutation)) 

<class 'sage.combinat.permutation.Permutation'> 

sage: p = Permutation([3,2,1]) 

sage: loads(dumps(p)) == p 

True 

""" 

if f is not None: 

import types 

self.f = f 

if hasattr(f,"__name__"): 

self.__name__ = f.__name__ 

self.__module__ = f.__module__ 

 

else: 

if hasattr(f, '__module__'): 

self.__module__ = f.__module__ 

elif hasattr(f, '__func__'): 

self.__module__ = f.__func__.__module__ 

 

if hasattr(f, '__name__'): 

self.__name__ = f.__name__ 

elif hasattr(f, '__func__'): 

self.__name__ = f.__func__.__name__ 

 

self.options = options 

 

def __call__(self, f): 

r""" 

Trick if :class:`EnumeratedSetFromIterator_method` was created with 

some options and is called with a function as argument. 

 

TESTS:: 

 

sage: from sage.sets.set_from_iterator import set_from_method 

sage: class A: 

....: @set_from_method() # indirect doctest 

....: def f(self): 

....: return xsrange(3) 

sage: a = A() 

sage: a.f() 

{0, 1, 2} 

""" 

return EnumeratedSetFromIterator_method_decorator(f,**self.options) 

 

def __get__(self, inst, cls): 

r""" 

TESTS:: 

 

sage: from sage.sets.set_from_iterator import set_from_method 

sage: class A(): 

....: def n(self): return 12 

....: @set_from_method 

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

sage: a = A() 

sage: print(A.f.__class__) 

<class 'sage.sets.set_from_iterator.EnumeratedSetFromIterator_method_caller'> 

sage: print(a.f.__class__) 

<class 'sage.sets.set_from_iterator.EnumeratedSetFromIterator_method_caller'> 

""" 

# You would hardly ever see an instance of this class alive. 

return EnumeratedSetFromIterator_method_caller(inst, self.f, **self.options) 

 

set_from_method = EnumeratedSetFromIterator_method_decorator 

 

class DummyExampleForPicklingTest: 

r""" 

Class example to test pickling with the decorator :class:`set_from_method`. 

 

.. WARNING:: 

 

This class is intended to be used in doctest only. 

 

EXAMPLES:: 

 

sage: from sage.sets.set_from_iterator import DummyExampleForPicklingTest 

sage: DummyExampleForPicklingTest().f() 

{10, 11, 12, 13, 14, ...} 

""" 

start = 10 

stop = 100 

@set_from_method 

def f(self): 

r""" 

Returns the set between ``self.start`` and ``self.stop``. 

 

EXAMPLES:: 

 

sage: from sage.sets.set_from_iterator import DummyExampleForPicklingTest 

sage: d = DummyExampleForPicklingTest() 

sage: d.f() 

{10, 11, 12, 13, 14, ...} 

sage: d.start = 4 

sage: d.stop = 200 

sage: d.f() 

{4, 5, 6, 7, 8, ...} 

""" 

from sage.arith.srange import xsrange 

return xsrange(self.start, self.stop)