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

"""Python Enumerations""" 

 

import sys as _sys 

 

__all__ = ['Enum', 'IntEnum', 'unique'] 

 

pyver = float('%s.%s' % _sys.version_info[:2]) 

 

try: 

any 

except NameError: 

def any(iterable): 

for element in iterable: 

if element: 

return True 

return False 

 

 

class _RouteClassAttributeToGetattr(object): 

"""Route attribute access on a class to __getattr__. 

 

This is a descriptor, used to define attributes that act differently when 

accessed through an instance and through a class. Instance access remains 

normal, but access to an attribute through a class will be routed to the 

class's __getattr__ method; this is done by raising AttributeError. 

 

""" 

def __init__(self, fget=None): 

self.fget = fget 

 

def __get__(self, instance, ownerclass=None): 

32 ↛ 33line 32 didn't jump to line 33, because the condition on line 32 was never true if instance is None: 

raise AttributeError() 

return self.fget(instance) 

 

def __set__(self, instance, value): 

raise AttributeError("can't set attribute") 

 

def __delete__(self, instance): 

raise AttributeError("can't delete attribute") 

 

 

def _is_descriptor(obj): 

"""Returns True if obj is a descriptor, False otherwise.""" 

return ( 

hasattr(obj, '__get__') or 

hasattr(obj, '__set__') or 

hasattr(obj, '__delete__')) 

 

 

def _is_dunder(name): 

"""Returns True if a __dunder__ name, False otherwise.""" 

return (name[:2] == name[-2:] == '__' and 

name[2:3] != '_' and 

name[-3:-2] != '_' and 

len(name) > 4) 

 

 

def _is_sunder(name): 

"""Returns True if a _sunder_ name, False otherwise.""" 

return (name[0] == name[-1] == '_' and 

name[1:2] != '_' and 

name[-2:-1] != '_' and 

len(name) > 2) 

 

 

def _make_class_unpicklable(cls): 

"""Make the given class un-picklable.""" 

def _break_on_call_reduce(self): 

raise TypeError('%r cannot be pickled' % self) 

cls.__reduce__ = _break_on_call_reduce 

cls.__module__ = '<unknown>' 

 

 

class _EnumDict(dict): 

"""Track enum member order and ensure member names are not reused. 

 

EnumMeta will use the names found in self._member_names as the 

enumeration member names. 

 

""" 

def __init__(self): 

super(_EnumDict, self).__init__() 

self._member_names = [] 

 

def __setitem__(self, key, value): 

"""Changes anything not dundered or not a descriptor. 

 

If a descriptor is added with the same name as an enum member, the name 

is removed from _member_names (this may leave a hole in the numerical 

sequence of values). 

 

If an enum member name is used twice, an error is raised; duplicate 

values are not checked for. 

 

Single underscore (sunder) names are reserved. 

 

Note: in 3.x __order__ is simply discarded as a not necessary piece 

leftover from 2.x 

 

""" 

102 ↛ 103line 102 didn't jump to line 103, because the condition on line 102 was never true if pyver >= 3.0 and key == '__order__': 

return 

104 ↛ 105line 104 didn't jump to line 105, because the condition on line 104 was never true if _is_sunder(key): 

raise ValueError('_names_ are reserved for future Enum use') 

elif _is_dunder(key): 

pass 

108 ↛ 110line 108 didn't jump to line 110, because the condition on line 108 was never true elif key in self._member_names: 

# descriptor overwriting an enum? 

raise TypeError('Attempted to reuse key: %r' % key) 

elif not _is_descriptor(value): 

112 ↛ 114line 112 didn't jump to line 114, because the condition on line 112 was never true if key in self: 

# enum overwriting a descriptor? 

raise TypeError('Key already defined as: %r' % self[key]) 

self._member_names.append(key) 

super(_EnumDict, self).__setitem__(key, value) 

 

 

# Dummy value for Enum as EnumMeta explicitly checks for it, but of course until 

# EnumMeta finishes running the first time the Enum class doesn't exist. This 

# is also why there are checks in EnumMeta like `if Enum is not None` 

Enum = None 

 

 

class EnumMeta(type): 

"""Metaclass for Enum""" 

@classmethod 

def __prepare__(metacls, cls, bases): 

return _EnumDict() 

 

def __new__(metacls, cls, bases, classdict): 

# an Enum class is final once enumeration items have been defined; it 

# cannot be mixed with other types (int, float, etc.) if it has an 

# inherited __new__ unless a new __new__ is defined (or the resulting 

# class will fail). 

if type(classdict) is dict: 

original_dict = classdict 

classdict = _EnumDict() 

for k, v in original_dict.items(): 

classdict[k] = v 

 

member_type, first_enum = metacls._get_mixins_(bases) 

#if member_type is object: 

# use_args = False 

#else: 

# use_args = True 

__new__, save_new, use_args = metacls._find_new_(classdict, member_type, 

first_enum) 

# save enum items into separate mapping so they don't get baked into 

# the new class 

members = dict((k, classdict[k]) for k in classdict._member_names) 

for name in classdict._member_names: 

del classdict[name] 

 

# py2 support for definition order 

__order__ = classdict.get('__order__') 

157 ↛ 164line 157 didn't jump to line 164, because the condition on line 157 was never false if __order__ is None: 

__order__ = classdict._member_names 

159 ↛ 160line 159 didn't jump to line 160, because the condition on line 159 was never true if pyver < 3.0: 

order_specified = False 

else: 

order_specified = True 

else: 

del classdict['__order__'] 

order_specified = True 

if pyver < 3.0: 

__order__ = __order__.replace(',', ' ').split() 

aliases = [name for name in members if name not in __order__] 

__order__ += aliases 

 

# check for illegal enum names (any others?) 

invalid_names = set(members) & set(['mro']) 

173 ↛ 174line 173 didn't jump to line 174, because the condition on line 173 was never true if invalid_names: 

raise ValueError('Invalid enum member name(s): %s' % ( 

', '.join(invalid_names), )) 

 

# create our new Enum type 

enum_class = super(EnumMeta, metacls).__new__(metacls, cls, bases, classdict) 

enum_class._member_names_ = [] # names in random order 

enum_class._member_map_ = {} # name->value map 

enum_class._member_type_ = member_type 

 

# Reverse value->name map for hashable values. 

enum_class._value2member_map_ = {} 

 

# check for a __getnewargs__, and if not present sabotage 

# pickling, since it won't work anyway 

188 ↛ 191line 188 didn't jump to line 191, because the condition on line 188 was never true if (member_type is not object and 

member_type.__dict__.get('__getnewargs__') is None 

): 

_make_class_unpicklable(enum_class) 

 

# instantiate them, checking for duplicates as we go 

# we instantiate first instead of checking for duplicates first in case 

# a custom __new__ is doing something funky with the values -- such as 

# auto-numbering ;) 

197 ↛ 198line 197 didn't jump to line 198, because the condition on line 197 was never true if __new__ is None: 

__new__ = enum_class.__new__ 

for member_name in __order__: 

value = members[member_name] 

201 ↛ 204line 201 didn't jump to line 204, because the condition on line 201 was never false if not isinstance(value, tuple): 

args = (value, ) 

else: 

args = value 

205 ↛ 206line 205 didn't jump to line 206, because the condition on line 205 was never true if member_type is tuple: # special case for tuple enums 

args = (args, ) # wrap it one more time 

207 ↛ 212line 207 didn't jump to line 212, because the condition on line 207 was never false if not use_args or not args: 

enum_member = __new__(enum_class) 

209 ↛ 215line 209 didn't jump to line 215, because the condition on line 209 was never false if not hasattr(enum_member, '_value_'): 

enum_member._value_ = value 

else: 

enum_member = __new__(enum_class, *args) 

if not hasattr(enum_member, '_value_'): 

enum_member._value_ = member_type(*args) 

value = enum_member._value_ 

enum_member._name_ = member_name 

enum_member.__objclass__ = enum_class 

enum_member.__init__(*args) 

# If another member with the same value was already defined, the 

# new member becomes an alias to the existing one. 

for name, canonical_member in enum_class._member_map_.items(): 

if canonical_member.value == enum_member._value_: 

enum_member = canonical_member 

break 

else: 

# Aliases don't appear in member names (only in __members__). 

enum_class._member_names_.append(member_name) 

enum_class._member_map_[member_name] = enum_member 

try: 

# This may fail if value is not hashable. We can't add the value 

# to the map, and by-value lookups for this value will be 

# linear. 

enum_class._value2member_map_[value] = enum_member 

except TypeError: 

pass 

 

# in Python2.x we cannot know definition order, so go with value order 

# unless __order__ was specified in the class definition 

239 ↛ 240line 239 didn't jump to line 240 if not order_specified: 

enum_class._member_names_ = [ 

e[0] for e in sorted( 

[(name, enum_class._member_map_[name]) for name in enum_class._member_names_], 

key=lambda t: t[1]._value_ 

)] 

 

# double check that repr and friends are not the mixin's or various 

# things break (such as pickle) 

if Enum is not None: 

setattr(enum_class, '__getnewargs__', Enum.__getnewargs__) 

for name in ('__repr__', '__str__', '__format__'): 

class_method = getattr(enum_class, name) 

obj_method = getattr(member_type, name, None) 

enum_method = getattr(first_enum, name, None) 

if obj_method is not None and obj_method is class_method: 

setattr(enum_class, name, enum_method) 

 

# method resolution and int's are not playing nice 

# Python's less than 2.6 use __cmp__ 

 

260 ↛ 262line 260 didn't jump to line 262, because the condition on line 260 was never true if pyver < 2.6: 

 

if issubclass(enum_class, int): 

setattr(enum_class, '__cmp__', getattr(int, '__cmp__')) 

 

265 ↛ 267line 265 didn't jump to line 267, because the condition on line 265 was never true elif pyver < 3.0: 

 

if issubclass(enum_class, int): 

for method in ( 

'__le__', 

'__lt__', 

'__gt__', 

'__ge__', 

'__eq__', 

'__ne__', 

'__hash__', 

): 

setattr(enum_class, method, getattr(int, method)) 

 

# replace any other __new__ with our own (as long as Enum is not None, 

# anyway) -- again, this is to support pickle 

if Enum is not None: 

# if the user defined their own __new__, save it before it gets 

# clobbered in case they subclass later 

284 ↛ 285line 284 didn't jump to line 285, because the condition on line 284 was never true if save_new: 

setattr(enum_class, '__member_new__', enum_class.__dict__['__new__']) 

setattr(enum_class, '__new__', Enum.__dict__['__new__']) 

return enum_class 

 

def __call__(cls, value, names=None, module=None, type=None): 

"""Either returns an existing member, or creates a new enum class. 

 

This method is used both when an enum class is given a value to match 

to an enumeration member (i.e. Color(3)) and for the functional API 

(i.e. Color = Enum('Color', names='red green blue')). 

 

When used for the functional API: `module`, if set, will be stored in 

the new class' __module__ attribute; `type`, if set, will be mixed in 

as the first base class. 

 

Note: if `module` is not set this routine will attempt to discover the 

calling module by walking the frame stack; if this is unsuccessful 

the resulting class will not be pickleable. 

 

""" 

305 ↛ 308line 305 didn't jump to line 308, because the condition on line 305 was never false if names is None: # simple value lookup 

return cls.__new__(cls, value) 

# otherwise, functional API: we're creating a new Enum type 

return cls._create_(value, names, module=module, type=type) 

 

def __contains__(cls, member): 

return isinstance(member, cls) and member.name in cls._member_map_ 

 

def __delattr__(cls, attr): 

# nicer error message when someone tries to delete an attribute 

# (see issue19025). 

if attr in cls._member_map_: 

raise AttributeError( 

"%s: cannot delete Enum member." % cls.__name__) 

super(EnumMeta, cls).__delattr__(attr) 

 

def __dir__(self): 

return (['__class__', '__doc__', '__members__', '__module__'] + 

self._member_names_) 

 

@property 

def __members__(cls): 

"""Returns a mapping of member name->value. 

 

This mapping lists all enum members, including aliases. Note that this 

is a copy of the internal mapping. 

 

""" 

return cls._member_map_.copy() 

 

def __getattr__(cls, name): 

"""Return the enum member matching `name` 

 

We use __getattr__ instead of descriptors or inserting into the enum 

class' __dict__ in order to support `name` and `value` being both 

properties for enum members (which live in the class' __dict__) and 

enum members themselves. 

 

""" 

if _is_dunder(name): 

raise AttributeError(name) 

try: 

return cls._member_map_[name] 

except KeyError: 

raise AttributeError(name) 

 

def __getitem__(cls, name): 

return cls._member_map_[name] 

 

def __iter__(cls): 

return (cls._member_map_[name] for name in cls._member_names_) 

 

def __reversed__(cls): 

return (cls._member_map_[name] for name in reversed(cls._member_names_)) 

 

def __len__(cls): 

return len(cls._member_names_) 

 

def __repr__(cls): 

return "<enum %r>" % cls.__name__ 

 

def __setattr__(cls, name, value): 

"""Block attempts to reassign Enum members. 

 

A simple assignment to the class namespace only changes one of the 

several possible ways to get an Enum member from the Enum class, 

resulting in an inconsistent Enumeration. 

 

""" 

member_map = cls.__dict__.get('_member_map_', {}) 

375 ↛ 376line 375 didn't jump to line 376, because the condition on line 375 was never true if name in member_map: 

raise AttributeError('Cannot reassign members.') 

super(EnumMeta, cls).__setattr__(name, value) 

 

def _create_(cls, class_name, names=None, module=None, type=None): 

"""Convenience method to create a new Enum class. 

 

`names` can be: 

 

* A string containing member names, separated either with spaces or 

commas. Values are auto-numbered from 1. 

* An iterable of member names. Values are auto-numbered from 1. 

* An iterable of (member name, value) pairs. 

* A mapping of member name -> value. 

 

""" 

metacls = cls.__class__ 

if type is None: 

bases = (cls, ) 

else: 

bases = (type, cls) 

classdict = metacls.__prepare__(class_name, bases) 

__order__ = [] 

 

# special processing needed for names? 

if isinstance(names, str): 

names = names.replace(',', ' ').split() 

if isinstance(names, (tuple, list)) and isinstance(names[0], str): 

names = [(e, i+1) for (i, e) in enumerate(names)] 

 

# Here, names is either an iterable of (name, value) or a mapping. 

for item in names: 

if isinstance(item, str): 

member_name, member_value = item, names[item] 

else: 

member_name, member_value = item 

classdict[member_name] = member_value 

__order__.append(member_name) 

# only set __order__ in classdict if name/value was not from a mapping 

if not isinstance(item, str): 

classdict['__order__'] = ' '.join(__order__) 

enum_class = metacls.__new__(metacls, class_name, bases, classdict) 

 

# TODO: replace the frame hack if a blessed way to know the calling 

# module is ever developed 

if module is None: 

try: 

module = _sys._getframe(2).f_globals['__name__'] 

except (AttributeError, ValueError): 

pass 

if module is None: 

_make_class_unpicklable(enum_class) 

else: 

enum_class.__module__ = module 

 

return enum_class 

 

@staticmethod 

def _get_mixins_(bases): 

"""Returns the type for creating enum members, and the first inherited 

enum class. 

 

bases: the tuple of bases that was given to __new__ 

 

""" 

if not bases or Enum is None: 

return object, Enum 

 

 

# double check that we are not subclassing a class with existing 

# enumeration members; while we're at it, see if any other data 

# type has been mixed in so we can use the correct __new__ 

member_type = first_enum = None 

for base in bases: 

449 ↛ 452line 449 didn't jump to line 452, because the condition on line 449 was never true if (base is not Enum and 

issubclass(base, Enum) and 

base._member_names_): 

raise TypeError("Cannot extend enumerations") 

# base is now the last base in bases 

454 ↛ 455line 454 didn't jump to line 455, because the condition on line 454 was never true if not issubclass(base, Enum): 

raise TypeError("new enumerations must be created as " 

"`ClassName([mixin_type,] enum_type)`") 

 

# get correct mix-in type (either mix-in type of Enum subclass, or 

# first base if last base is Enum) 

if not issubclass(bases[0], Enum): 

member_type = bases[0] # first data type 

first_enum = bases[-1] # enum type 

else: 

for base in bases[0].__mro__: 

# most common: (IntEnum, int, Enum, object) 

# possible: (<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>, 

# <class 'int'>, <Enum 'Enum'>, 

# <class 'object'>) 

if issubclass(base, Enum): 

470 ↛ 464line 470 didn't jump to line 464, because the condition on line 470 was never false if first_enum is None: 

first_enum = base 

else: 

473 ↛ 464line 473 didn't jump to line 464, because the condition on line 473 was never false if member_type is None: 

member_type = base 

 

return member_type, first_enum 

 

478 ↛ 479line 478 didn't jump to line 479, because the condition on line 478 was never true if pyver < 3.0: 

@staticmethod 

def _find_new_(classdict, member_type, first_enum): 

"""Returns the __new__ to be used for creating the enum members. 

 

classdict: the class dictionary given to __new__ 

member_type: the data type whose __new__ will be used by default 

first_enum: enumeration to check for an overriding __new__ 

 

""" 

# now find the correct __new__, checking to see of one was defined 

# by the user; also check earlier enum classes in case a __new__ was 

# saved as __member_new__ 

__new__ = classdict.get('__new__', None) 

if __new__: 

return None, True, True # __new__, save_new, use_args 

 

N__new__ = getattr(None, '__new__') 

O__new__ = getattr(object, '__new__') 

if Enum is None: 

E__new__ = N__new__ 

else: 

E__new__ = Enum.__dict__['__new__'] 

# check all possibles for __member_new__ before falling back to 

# __new__ 

for method in ('__member_new__', '__new__'): 

for possible in (member_type, first_enum): 

try: 

target = possible.__dict__[method] 

except (AttributeError, KeyError): 

target = getattr(possible, method, None) 

if target not in [ 

None, 

N__new__, 

O__new__, 

E__new__, 

]: 

if method == '__member_new__': 

classdict['__new__'] = target 

return None, False, True 

if isinstance(target, staticmethod): 

target = target.__get__(member_type) 

__new__ = target 

break 

if __new__ is not None: 

break 

else: 

__new__ = object.__new__ 

 

# if a non-object.__new__ is used then whatever value/tuple was 

# assigned to the enum member name will be passed to __new__ and to the 

# new enum member's __init__ 

if __new__ is object.__new__: 

use_args = False 

else: 

use_args = True 

 

return __new__, False, use_args 

else: 

@staticmethod 

def _find_new_(classdict, member_type, first_enum): 

"""Returns the __new__ to be used for creating the enum members. 

 

classdict: the class dictionary given to __new__ 

member_type: the data type whose __new__ will be used by default 

first_enum: enumeration to check for an overriding __new__ 

 

""" 

# now find the correct __new__, checking to see of one was defined 

# by the user; also check earlier enum classes in case a __new__ was 

# saved as __member_new__ 

__new__ = classdict.get('__new__', None) 

 

# should __new__ be saved as __member_new__ later? 

save_new = __new__ is not None 

 

if __new__ is None: 

# check all possibles for __member_new__ before falling back to 

# __new__ 

for method in ('__member_new__', '__new__'): 

for possible in (member_type, first_enum): 

target = getattr(possible, method, None) 

if target not in ( 

None, 

None.__new__, 

object.__new__, 

Enum.__new__, 

): 

__new__ = target 

break 

if __new__ is not None: 

break 

else: 

__new__ = object.__new__ 

 

# if a non-object.__new__ is used then whatever value/tuple was 

# assigned to the enum member name will be passed to __new__ and to the 

# new enum member's __init__ 

if __new__ is object.__new__: 

use_args = False 

else: 

use_args = True 

 

return __new__, save_new, use_args 

 

 

######################################################## 

# In order to support Python 2 and 3 with a single 

# codebase we have to create the Enum methods separately 

# and then use the `type(name, bases, dict)` method to 

# create the class. 

######################################################## 

temp_enum_dict = {} 

temp_enum_dict['__doc__'] = "Generic enumeration.\n\n Derive from this class to define new enumerations.\n\n" 

 

def __new__(cls, value): 

# all enum instances are actually created during class construction 

# without calling this method; this method is called by the metaclass' 

# __call__ (i.e. Color(3) ), and by pickle 

597 ↛ 599line 597 didn't jump to line 599, because the condition on line 597 was never true if type(value) is cls: 

# For lookups like Color(Color.red) 

value = value.value 

#return value 

# by-value search for a matching enum member 

# see if it's in the reverse mapping (for hashable values) 

try: 

if value in cls._value2member_map_: 

return cls._value2member_map_[value] 

except TypeError: 

# not there, now do long search -- O(n) behavior 

for member in cls._member_map_.values(): 

if member.value == value: 

return member 

raise ValueError("%s is not a valid %s" % (value, cls.__name__)) 

temp_enum_dict['__new__'] = __new__ 

del __new__ 

 

def __repr__(self): 

return "<%s.%s: %r>" % ( 

self.__class__.__name__, self._name_, self._value_) 

temp_enum_dict['__repr__'] = __repr__ 

del __repr__ 

 

def __str__(self): 

return "%s.%s" % (self.__class__.__name__, self._name_) 

temp_enum_dict['__str__'] = __str__ 

del __str__ 

 

def __dir__(self): 

added_behavior = [m for m in self.__class__.__dict__ if m[0] != '_'] 

return (['__class__', '__doc__', '__module__', 'name', 'value'] + added_behavior) 

temp_enum_dict['__dir__'] = __dir__ 

del __dir__ 

 

def __format__(self, format_spec): 

# mixed-in Enums should use the mixed-in type's __format__, otherwise 

# we can get strange results with the Enum name showing up instead of 

# the value 

 

# pure Enum branch 

if self._member_type_ is object: 

cls = str 

val = str(self) 

# mix-in branch 

else: 

cls = self._member_type_ 

val = self.value 

return cls.__format__(val, format_spec) 

temp_enum_dict['__format__'] = __format__ 

del __format__ 

 

 

#################################### 

# Python's less than 2.6 use __cmp__ 

 

653 ↛ 655line 653 didn't jump to line 655, because the condition on line 653 was never trueif pyver < 2.6: 

 

def __cmp__(self, other): 

if type(other) is self.__class__: 

if self is other: 

return 0 

return -1 

return NotImplemented 

raise TypeError("unorderable types: %s() and %s()" % (self.__class__.__name__, other.__class__.__name__)) 

temp_enum_dict['__cmp__'] = __cmp__ 

del __cmp__ 

 

else: 

 

def __le__(self, other): 

raise TypeError("unorderable types: %s() <= %s()" % (self.__class__.__name__, other.__class__.__name__)) 

temp_enum_dict['__le__'] = __le__ 

del __le__ 

 

def __lt__(self, other): 

raise TypeError("unorderable types: %s() < %s()" % (self.__class__.__name__, other.__class__.__name__)) 

temp_enum_dict['__lt__'] = __lt__ 

del __lt__ 

 

def __ge__(self, other): 

raise TypeError("unorderable types: %s() >= %s()" % (self.__class__.__name__, other.__class__.__name__)) 

temp_enum_dict['__ge__'] = __ge__ 

del __ge__ 

 

def __gt__(self, other): 

raise TypeError("unorderable types: %s() > %s()" % (self.__class__.__name__, other.__class__.__name__)) 

temp_enum_dict['__gt__'] = __gt__ 

del __gt__ 

 

 

def __eq__(self, other): 

if type(other) is self.__class__: 

return self is other 

return NotImplemented 

temp_enum_dict['__eq__'] = __eq__ 

del __eq__ 

 

def __ne__(self, other): 

if type(other) is self.__class__: 

return self is not other 

return NotImplemented 

temp_enum_dict['__ne__'] = __ne__ 

del __ne__ 

 

def __getnewargs__(self): 

return (self._value_, ) 

temp_enum_dict['__getnewargs__'] = __getnewargs__ 

del __getnewargs__ 

 

def __hash__(self): 

return hash(self._name_) 

temp_enum_dict['__hash__'] = __hash__ 

del __hash__ 

 

# _RouteClassAttributeToGetattr is used to provide access to the `name` 

# and `value` properties of enum members while keeping some measure of 

# protection from modification, while still allowing for an enumeration 

# to have members named `name` and `value`. This works because enumeration 

# members are not set directly on the enum class -- __getattr__ is 

# used to look them up. 

 

@_RouteClassAttributeToGetattr 

def name(self): 

return self._name_ 

temp_enum_dict['name'] = name 

del name 

 

@_RouteClassAttributeToGetattr 

def value(self): 

return self._value_ 

temp_enum_dict['value'] = value 

del value 

 

Enum = EnumMeta('Enum', (object, ), temp_enum_dict) 

del temp_enum_dict 

 

# Enum has now been created 

########################### 

 

class IntEnum(int, Enum): 

"""Enum where members are also (and must be) ints""" 

 

 

def unique(enumeration): 

"""Class decorator that ensures only unique members exist in an enumeration.""" 

duplicates = [] 

for name, member in enumeration.__members__.items(): 

if name != member.name: 

duplicates.append((name, member.name)) 

if duplicates: 

duplicate_names = ', '.join( 

["%s -> %s" % (alias, name) for (alias, name) in duplicates] 

) 

raise ValueError('duplicate names found in %r: %s' % 

(enumeration, duplicate_names) 

) 

return enumeration