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

r""" 

Converting Dictionary 

 

At the moment, the only class contained in this model is a key 

converting dictionary, which applies some function (e.g. type 

conversion function) to all arguments used as keys. 

 

.. It is conceivable that a other dicts might be added later on. 

 

AUTHORS: 

 

- Martin von Gagern (2015-01-31): initial version 

 

EXAMPLES: 

 

A ``KeyConvertingDict`` will apply a conversion function to all method 

arguments which are keys:: 

 

sage: from sage.misc.converting_dict import KeyConvertingDict 

sage: d = KeyConvertingDict(int) 

sage: d["3"] = 42 

sage: list(d.items()) 

[(3, 42)] 

 

This is used e.g. in the result of a variety, to allow access to the 

result no matter how a generator is identified:: 

 

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

sage: I = ideal([x^2+2*y-5,x+y+3]) 

sage: v = I.variety(AA)[0]; v 

{x: 4.464101615137755?, y: -7.464101615137755?} 

sage: list(v)[0].parent() 

Multivariate Polynomial Ring in x, y over Algebraic Real Field 

sage: v[x] 

4.464101615137755? 

sage: v["y"] 

-7.464101615137755? 

""" 

 

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

# Copyright (C) 2015 Martin von Gagern <Martin.vGagern@gmx.net> 

# 

# 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 absolute_import 

from six import iteritems 

 

import collections 

 

 

class KeyConvertingDict(dict): 

r""" 

A dictionary which automatically applies a conversions to its keys. 

 

The most common application is the case where the conversion 

function is the object representing some category, so that key 

conversion means a type conversion to adapt keys to that 

category. This allows different representations for keys which in 

turn makes accessing the correct element easier. 

 

INPUT: 

 

- ``key_conversion_function`` -- a function which will be 

applied to all method arguments which represent keys. 

- ``data`` -- optional dictionary or sequence of key-value pairs 

to initialize this mapping. 

 

EXAMPLES:: 

 

sage: from sage.misc.converting_dict import KeyConvertingDict 

sage: d = KeyConvertingDict(int) 

sage: d["3"] = 42 

sage: list(d.items()) 

[(3, 42)] 

sage: d[5.0] = 64 

sage: d["05"] 

64 

 

""" 

 

def __init__(self, key_conversion_function, data=None): 

r""" 

Construct a dictionary with a given conversion function. 

 

EXAMPLES:: 

 

sage: from sage.misc.converting_dict import KeyConvertingDict 

sage: d = KeyConvertingDict(int) 

sage: d["3"] = 42 

sage: list(d.items()) 

[(3, 42)] 

sage: list(KeyConvertingDict(int, {"5": 7}).items()) 

[(5, 7)] 

sage: list(KeyConvertingDict(int, [("9", 99)]).items()) 

[(9, 99)] 

""" 

super(KeyConvertingDict, self).__init__() 

self.key_conversion_function = key_conversion_function 

if data: 

self.update(data) 

 

def __getitem__(self, key): 

r""" 

Retrieve an element from the dictionary. 

 

INPUT: 

 

- ``key`` -- A value identifying the element, will be converted. 

 

EXAMPLES:: 

 

sage: from sage.misc.converting_dict import KeyConvertingDict 

sage: d = KeyConvertingDict(int) 

sage: d[3] = 42 

sage: d["3"] 

42 

""" 

key = self.key_conversion_function(key) 

return super(KeyConvertingDict, self).__getitem__(key) 

 

def __setitem__(self, key, value): 

r""" 

Assign an element in the dictionary. 

 

INPUT: 

 

- ``key`` -- A value identifying the element, will be converted. 

- ``value`` -- The associated value, will be left unmodified. 

 

EXAMPLES:: 

 

sage: from sage.misc.converting_dict import KeyConvertingDict 

sage: d = KeyConvertingDict(int) 

sage: d["3"] = 42 

sage: list(d.items()) 

[(3, 42)] 

""" 

key = self.key_conversion_function(key) 

return super(KeyConvertingDict, self).__setitem__(key, value) 

 

def __delitem__(self, key): 

r""" 

Remove a mapping from the dictionary. 

 

INPUT: 

 

- ``key`` -- A value identifying the element, will be converted. 

 

EXAMPLES:: 

 

sage: from sage.misc.converting_dict import KeyConvertingDict 

sage: d = KeyConvertingDict(int) 

sage: d[3] = 42 

sage: del d["3"] 

sage: len(d) 

0 

""" 

key = self.key_conversion_function(key) 

return super(KeyConvertingDict, self).__delitem__(key) 

 

def __contains__(self, key): 

r""" 

Test whether a given key is contained in the mapping. 

 

INPUT: 

 

- ``key`` -- A value identifying the element, will be converted. 

 

EXAMPLES:: 

 

sage: from sage.misc.converting_dict import KeyConvertingDict 

sage: d = KeyConvertingDict(int) 

sage: d[3] = 42 

sage: "3" in d 

True 

sage: 4 in d 

False 

""" 

key = self.key_conversion_function(key) 

return super(KeyConvertingDict, self).__contains__(key) 

 

def has_key(self, key): 

r""" 

Deprecated; present just for the sake of compatibility. 

Use ``key in self`` instead. 

 

INPUT: 

 

- ``key`` -- A value identifying the element, will be converted. 

 

EXAMPLES:: 

 

sage: from sage.misc.converting_dict import KeyConvertingDict 

sage: d = KeyConvertingDict(int) 

sage: d[3] = 42 

sage: d.has_key("3") 

True 

sage: d.has_key(4) 

False 

""" 

return key in self 

 

def pop(self, key, *args): 

r""" 

Remove and retrieve a given element from the dictionary. 

 

INPUT: 

 

- ``key`` -- A value identifying the element, will be converted. 

- ``default`` -- The value to return if the element is not mapped, optional. 

 

EXAMPLES:: 

 

sage: from sage.misc.converting_dict import KeyConvertingDict 

sage: d = KeyConvertingDict(int) 

sage: d[3] = 42 

sage: d.pop("3") 

42 

sage: d.pop("3", 33) 

33 

sage: d.pop("3") 

Traceback (most recent call last): 

... 

KeyError: ... 

""" 

key = self.key_conversion_function(key) 

return super(KeyConvertingDict, self).pop(key, *args) 

 

def setdefault(self, key, default=None): 

r""" 

Create a given mapping unless there already exists a mapping 

for that key. 

 

INPUT: 

 

- ``key`` -- A value identifying the element, will be converted. 

- ``default`` -- The value to associate with the key. 

 

EXAMPLES:: 

 

sage: from sage.misc.converting_dict import KeyConvertingDict 

sage: d = KeyConvertingDict(int) 

sage: d.setdefault("3") 

sage: list(d.items()) 

[(3, None)] 

""" 

key = self.key_conversion_function(key) 

return super(KeyConvertingDict, self).setdefault(key, default) 

 

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

r""" 

Update the dictionary with key-value pairs from another dictionary, 

sequence of key-value pairs, or keyword arguments. 

 

INPUT: 

 

- ``key`` -- A value identifying the element, will be converted. 

- ``args`` -- A single dict or sequence of pairs. 

- ``kwds`` -- Named elements require that the conversion 

function accept strings. 

 

EXAMPLES:: 

 

sage: from sage.misc.converting_dict import KeyConvertingDict 

sage: d = KeyConvertingDict(int) 

sage: d.update([("3",1),(4,2)]) 

sage: d[3] 

1 

sage: d.update({"5": 7, "9": 12}) 

sage: d[9] 

12 

sage: d = KeyConvertingDict(QQ['x']) 

sage: d.update(x=42) 

sage: d 

{x: 42} 

""" 

f = self.key_conversion_function 

u = super(KeyConvertingDict, self).update 

if args: 

if len(args) != 1: 

raise TypeError("update expected at most 1 argument") 

arg = args[0] 

if isinstance(arg, collections.Mapping): 

seq = ((f(k), arg[k]) for k in arg) 

else: 

seq = ((f(k), v) for k, v in arg) 

u(seq) 

if kwds: 

seq = ((f(k), v) for k, v in iteritems(kwds)) 

u(seq)