jaraco.util / jaraco / util / dictlib.py

  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
# -*- coding: utf-8 -*-

from __future__ import absolute_import, unicode_literals

import re
import operator
import collections
import itertools

import jaraco.util.string
from jaraco.util.properties import NonDataProperty

class DictFilter(object):
	"""
	Takes a dict, and simulates a sub-dict based on the keys.

	>>> sample = {'a': 1, 'b': 2, 'c': 3}
	>>> filtered = DictFilter(sample, ['a', 'c'])
	>>> filtered == {'a': 1, 'c': 3}
	True

	One can also filter by a regular expression pattern

	>>> sample['d'] = 4
	>>> sample['ef'] = 5

	Here we filter for only single-character keys

	>>> filtered = DictFilter(sample, include_pattern='.$')
	>>> filtered == {'a': 1, 'b': 2, 'c': 3, 'd': 4}
	True

	Also note that DictFilter keeps a reference to the original dict, so
	if you modify the original dict, that could modify the filtered dict.

	>>> del sample['d']
	>>> del sample['a']
	>>> filtered == {'b': 2, 'c': 3}
	True

	"""
	def __init__(self, dict, include_keys=[], include_pattern=None):
		self.dict = dict
		self.specified_keys = set(include_keys)
		if include_pattern is not None:
			self.include_pattern = re.compile(include_pattern)
		else:
			# for performance, replace the pattern_keys property
			self.pattern_keys = set()

	def get_pattern_keys(self):
		#key_matches = lambda k, v: self.include_pattern.match(k)
		keys = filter(self.include_pattern.match, self.dict.keys())
		return set(keys)
	pattern_keys = NonDataProperty(get_pattern_keys)

	@property
	def include_keys(self):
		return self.specified_keys.union(self.pattern_keys)

	def keys(self):
		return self.include_keys.intersection(self.dict.keys())

	def values(self):
		keys = self.keys()
		values = map(self.dict.get, keys)
		return values

	def __getitem__(self, i):
		if not i in self.include_keys:
			return KeyError, i
		return self.dict[i]

	def items(self):
		keys = self.keys()
		values = map(self.dict.get, keys)
		return zip(keys, values)

	def __eq__(self, other):
		return dict(self) == other

	def __ne__(self, other):
		return dict(self) != other

def dict_map(function, dictionary):
	"""
	dict_map is much like the built-in function map.  It takes a dictionary
	and applys a function to the values of that dictionary, returning a
	new dictionary with the mapped values in the original keys.

	>>> d = dict_map(lambda x:x+1, dict(a=1, b=2))
	>>> d == dict(a=2,b=3)
	True
	"""
	return dict((key, function(value)) for key, value in dictionary.items())

class RangeMap(dict):
	"""
	A dictionary-like object that uses the keys as bounds for a range.
	Inclusion of the value for that range is determined by the
	key_match_comparator, which defaults to less-than-or-equal.
	A value is returned for a key if it is the first key that matches in
	the sorted list of keys.

	One may supply keyword parameters to be passed to the sort function used
	to sort keys (i.e. cmp [python 2 only], keys, reverse) as sort_params.

	Let's create a map that maps 1-3 -> 'a', 4-6 -> 'b'
	>>> r = RangeMap({3: 'a', 6: 'b'})  # boy, that was easy
	>>> r[1], r[2], r[3], r[4], r[5], r[6]
	(u'a', u'a', u'a', u'b', u'b', u'b')

	Even float values should work so long as the comparison operator
	supports it.
	>>> r[4.5]
	u'b'

	But you'll notice that the way rangemap is defined, it must be open-ended on one side.
	>>> r[0]
	u'a'
	>>> r[-1]
	u'a'

	One can close the open-end of the RangeMap by using undefined_value
	>>> r = RangeMap({0: RangeMap.undefined_value, 3: 'a', 6: 'b'})
	>>> r[0]
	Traceback (most recent call last):
	...
	KeyError: 0

	One can get the first or last elements in the range by using RangeMap.Item
	>>> last_item = RangeMap.Item(-1)
	>>> r[last_item]
	u'b'

	.last_item is a shortcut for Item(-1)
	>>> r[RangeMap.last_item]
	u'b'

	Sometimes it's useful to find the bounds for a RangeMap
	>>> r.bounds()
	(0, 6)

	RangeMap supports .get(key, default)
	>>> r.get(0, 'not found')
	u'not found'

	>>> r.get(7, 'not found')
	u'not found'

	"""
	def __init__(self, source, sort_params = {}, key_match_comparator = operator.le):
		dict.__init__(self, source)
		self.sort_params = sort_params
		self.match = key_match_comparator

	def __getitem__(self, item):
		sorted_keys = sorted(self.keys(), **self.sort_params)
		if isinstance(item, RangeMap.Item):
			result = self.__getitem__(sorted_keys[item])
		else:
			key = self._find_first_match_(sorted_keys, item)
			result = dict.__getitem__(self, key)
			if result is RangeMap.undefined_value:
				raise KeyError(key)
		return result

	def get(self, key, default=None):
		"""
		Return the value for key if key is in the dictionary, else default.
		If default is not given, it defaults to None, so that this method
		never raises a KeyError.
		"""
		try:
			return self[key]
		except KeyError:
			return default

	def _find_first_match_(self, keys, item):
		is_match = lambda k: self.match(item, k)
		matches = list(filter(is_match, keys))
		if matches:
			return matches[0]
		raise KeyError(item)

	def bounds(self):
		sorted_keys = sorted(self.keys(), **self.sort_params)
		return (
			sorted_keys[RangeMap.first_item],
			sorted_keys[RangeMap.last_item],
		)

	# some special values for the RangeMap
	undefined_value = type(str('RangeValueUndefined'), (object,), {})()
	class Item(int): pass
	first_item = Item(0)
	last_item = Item(-1)

__identity = lambda x: x

def sorted_items(d, key=__identity, reverse=False):
	"""
	Return the items of the dictionary sorted by the keys

	>>> sample = dict(foo=20, bar=42, baz=10)
	>>> tuple(sorted_items(sample))
	(('bar', 42), ('baz', 10), ('foo', 20))

	>>> reverse_string = lambda s: ''.join(reversed(s))
	>>> tuple(sorted_items(sample, key=reverse_string))
	(('foo', 20), ('bar', 42), ('baz', 10))

	>>> tuple(sorted_items(sample, reverse=True))
	(('foo', 20), ('baz', 10), ('bar', 42))
	"""
	# wrap the key func so it operates on the first element of each item
	pairkey_key = lambda item: key(item[0])
	return sorted(d.items(), key=pairkey_key, reverse=reverse)

class KeyTransformingDict(dict):
	"""
	A dict subclass that transforms the keys before they're used.
	Subclasses may override the default transform_key to customize behavior.
	"""
	@staticmethod
	def transform_key(key):
		return key

	def __init__(self, *args, **kargs):
		super(KeyTransformingDict, self).__init__()
		# build a dictionary using the default constructs
		d = dict(*args, **kargs)
		# build this dictionary using transformed keys.
		for item in d.items():
			self.__setitem__(*item)

	def __setitem__(self, key, val):
		key = self.transform_key(key)
		super(KeyTransformingDict, self).__setitem__(key, val)

	def __getitem__(self, key):
		key = self.transform_key(key)
		return super(KeyTransformingDict, self).__getitem__(key)

	def __contains__(self, key):
		key = self.transform_key(key)
		return super(KeyTransformingDict, self).__contains__(key)

	def __delitem__(self, key):
		key = self.transform_key(key)
		return super(KeyTransformingDict, self).__delitem__(key)

	def setdefault(self, key, *args, **kwargs):
		key = self.transform_key(key)
		return super(KeyTransformingDict, self).setdefault(key, *args, **kwargs)

	def pop(self, key, *args, **kwargs):
		key = self.transform_key(key)
		return super(KeyTransformingDict, self).pop(key, *args, **kwargs)

class FoldedCaseKeyedDict(KeyTransformingDict):
	"""A case-insensitive dictionary (keys are compared as insensitive
	if they are strings).
	>>> d = FoldedCaseKeyedDict()
	>>> d['heLlo'] = 'world'
	>>> d
	{u'heLlo': u'world'}
	>>> d['hello']
	u'world'
	>>> 'hello' in d
	True
	>>> 'HELLO' in d
	True
	>>> FoldedCaseKeyedDict({'heLlo': 'world'})
	{u'heLlo': u'world'}
	>>> d = FoldedCaseKeyedDict({'heLlo': 'world'})
	>>> d['hello']
	u'world'
	>>> d['Hello']
	u'world'
	>>> d
	{u'heLlo': u'world'}
	>>> d = FoldedCaseKeyedDict({'heLlo': 'world', 'Hello': 'world'})
	>>> d
	{u'heLlo': u'world'}
	>>> del d['HELLO']
	>>> d
	{}

	setdefault should also work
	>>> d['This'] = 'that'
	>>> d.setdefault('this', 'other')
	u'that'
	>>> len(d)
	1
	>>> d['this']
	u'that'
	>>> d.setdefault('That', 'other')
	u'other'
	>>> d['THAT']
	u'other'

	Make it pop!
	>>> d.pop('THAT')
	u'other'
	"""
	@staticmethod
	def transform_key(key):
		return jaraco.util.string.FoldedCase(key)

class DictAdapter(object):
	"""
	Provide a getitem interface for attributes of an object.

	Let's say you want to get at the string.lowercase property in a formatted
	string. It's easy with DictAdapter.

	>>> import string
	>>> "lowercase is %(lowercase)s" % DictAdapter(string)
	u'lowercase is abcdefghijklmnopqrstuvwxyz'
	"""
	def __init__(self, wrapped_ob):
		self.object = wrapped_ob

	def __getitem__(self, name):
		return getattr(self.object, name)

class ItemsAsAttributes(object):
	"""
	Mix-in class to enable a mapping object to provide items as
	attributes.

	>>> C = type(str('C'), (dict, ItemsAsAttributes), dict())
	>>> i = C()
	>>> i['foo'] = 'bar'
	>>> i.foo
	u'bar'

	# natural attribute access takes precedence
	>>> i.foo = 'henry'
	>>> i.foo
	u'henry'

	# but as you might expect, the mapping functionality is preserved.
	>>> i['foo']
	u'bar'

	# A normal attribute error should be raised if an attribute is
	#  requested that doesn't exist.
	>>> i.missing
	Traceback (most recent call last):
	...
	AttributeError: 'C' object has no attribute 'missing'

	It also works on dicts that customize __getitem__
	>>> missing_func = lambda self, key: 'missing item'
	>>> C = type(str('C'), (dict, ItemsAsAttributes), dict(__missing__ = missing_func))
	>>> i = C()
	>>> i.missing
	u'missing item'
	>>> i.foo
	u'missing item'
	"""
	def __getattr__(self, key):
		try:
			return getattr(super(ItemsAsAttributes, self), key)
		except AttributeError as e:
			# attempt to get the value from the mapping (return self[key])
			#  but be careful not to lose the original exception context.
			noval = object()
			def _safe_getitem(cont, key, missing_result):
				try:
					return cont[key]
				except KeyError:
					return missing_result
			result = _safe_getitem(self, key, noval)
			if result is not noval:
				return result
			# raise the original exception, but use the original class
			#  name, not 'super'.
			e.message = e.message.replace('super',
				self.__class__.__name__, 1)
			e.args = (e.message,)
			raise

def invert_map(map):
	"""
	Given a dictionary, return another dictionary with keys and values
	switched. If any of the values resolve to the same key, raises
	a ValueError.

	>>> numbers = dict(a=1, b=2, c=3)
	>>> letters = invert_map(numbers)
	>>> letters[1]
	'a'
	>>> numbers['d'] = 3
	>>> invert_map(numbers)
	Traceback (most recent call last):
	...
	ValueError: Key conflict in inverted mapping
	"""
	res = dict((v,k) for k, v in map.iteritems())
	if not len(res) == len(map):
		raise ValueError('Key conflict in inverted mapping')
	return res


class IdentityOverrideMap(dict):
	"""
	A dictionary that by default maps each key to itself, but otherwise
	acts like a normal dictionary.

	>>> d = IdentityOverrideMap()
	>>> d[42]
	42
	>>> d['speed'] = 'speedo'
	>>> d['speed']
	u'speedo'
	"""
	def __missing__(self, key):
		return key

class DictStack(list, collections.Mapping):
	"""
	A stack of dictionaries that behaves as a view on those dictionaries,
	giving preference to the last.

	>>> stack = DictStack([dict(a=1, c=2), dict(b=2, a=2)])
	>>> stack['a']
	2
	>>> stack['b']
	2
	>>> stack['c']
	2
	>>> stack.push(dict(a=3))
	>>> stack['a']
	3
	>>> stack.keys()
	['a', 'c', 'b']
	>>> d = stack.pop()
	>>> stack['a']
	2
	>>> d = stack.pop()
	>>> stack['a']
	1
	"""
	def keys(self):
		return list(set(itertools.chain.from_iterable(c.keys() for c in self)))
	def __getitem__(self, key):
		for scope in reversed(self):
			if key in scope: return scope[key]
		raise KeyError(key)
	push = list.append
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.