/usr/lib/python2.7/dist-packages/ginga/misc/Bunch.py is in python-ginga 2.6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #
# Bunch.py -- simple classes for grouping variables
#
# See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308
# Description:
#
# TODO: make these true subclasses of dict?
#
import threading
class caselessDict(object):
"""
Case-insensitive dictionary. Adapted from
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66315
"""
def __init__(self, inDict=None):
"""Constructor: takes conventional dictionary as input (or nothing)
"""
self.dict = {}
if inDict is not None:
self.update(inDict)
def lower(self, key):
try:
return key.lower()
except AttributeError:
return key
def update(self, inDict):
for key in inDict.keys():
k = self.lower(key)
self.dict[k] = (key, inDict[key])
def store(self, inDict):
return self.update(inDict)
def setvals(self, **kwdargs):
return self.update(kwdargs)
def fetchList(self, keySeq):
res = []
for key in keySeq:
res.append(self.dict[key])
return res
def fetchDict(self, keyDict):
res = {}
for key in keyDict:
res[key] = self.dict[key]
return res
def fetch(self, keyDict):
"""Like update(), but for retrieving values.
"""
for key in keyDict:
keyDict[key] = self.dict[key]
def clear(self):
self.dict.clear()
def setdefault(self, key, val):
if key in self:
return self.__getitem__(key)
else:
self.__setitem__(key, val)
return val
def __iter__(self):
self.iterPosition = 0
self.keyList = list(self.dict.keys())
return(self)
def __next__(self):
if self.iterPosition >= len(self.keyList):
raise StopIteration
x = self.dict[self.keyList[self.iterPosition]][0]
self.iterPosition += 1
return x
def next(self):
return self.__next__()
def iteritems(self):
return iter(self.items())
def iterkeys(self):
return iter(self.keys())
def itervalues(self):
return iter(self.values())
def __getitem__(self, key):
k = self.lower(key)
return self.dict[k][1]
def __setitem__(self, key, value):
k = self.lower(key)
self.dict[k] = (key, value)
def __delitem__(self, key):
k = self.lower(key)
del self.dict[k]
def has_key(self, key):
k = self.lower(key)
return k in self.dict
def canonicalKey(self, key):
k = self.lower(key)
return self.dict[k][0]
def get(self, key, alt=None):
if key in self:
return self.__getitem__(key)
return alt
def __len__(self):
return len(self.dict)
def keys(self):
return [v[0] for v in self.dict.values()]
def values(self):
return [v[1] for v in self.dict.values()]
def items(self):
return self.dict.values()
def __contains__(self, item):
return item.lower() in self.dict
def __repr__(self):
items = ", ".join([("%r: %r" % (k,v)) for k,v in self.items()])
return "{%s}" % items
def __str__(self):
return repr(self)
def copy(self):
d={}
for k,v in self.dict.items():
d[k]=v[1]
return d
class Bunch(object):
"""Often we want to just collect a bunch of stuff together, naming each
item of the bunch; a dictionary's OK for that, but a small do-nothing
class is even handier, and prettier to use.
e.g.
point = Bunch(datum=y, squared=y*y, coord=x)
and of course you can read/write the named attributes you just created,
add others, delete some of them, e.g.
if point.squared > threshold:
point.isok = 1
This Bunch class provides both dictionary and attribute-style access,
so you can use either method for member access or even mix-and-match.
"""
def __init__(self, inDict=None, caseless=False, **kwdargs):
"""Constructor for a Bunch. If _caseless_ is True, bunch will
ignore case in looking up members; e.g.
foo = Bunch(caseless=True, Bar=4)
foo['BAR'] => 4
foo['bar'] => 4
foo['BaR'] => 4
foo.BAR => 4
foo.bar => 4
foo.BaR => 4
"""
if caseless:
self.tbl = caselessDict(inDict=inDict)
else:
self.tbl = {}
if inDict is not None:
self.tbl.update(inDict)
self.tbl.update(kwdargs)
self.iterPosition = 0
self.keyList = self.tbl.keys()
# after initialisation, setting attributes is the same as setting
# an item.
self.__initialised = True
def __getitem__(self, key):
"""Maps dictionary keys to values.
Called for dictionary style access of this object.
"""
return self.tbl[key]
def __setitem__(self, key, value):
"""Maps dictionary keys to values for assignment. Called for dictionary style
access with assignment.
"""
self.tbl[key] = value
def __delitem__(self, key):
del self.tbl[key]
def __getattr__(self, attr):
"""Maps values to attributes.
Only called if there *isn't* an attribute with this name. Called for attribute
style access of this object.
"""
return self.tbl[attr]
def __setattr__(self, attr, value):
"""Maps attributes to values for assignment. Called for attribute style access
of this object for assignment.
"""
# this test allows attributes to be set in the __init__ method
# (self.__dict__[_Bunch__initialised] same as self.__initialized)
if '_Bunch__initialised' not in self.__dict__:
self.__dict__[attr] = value
else:
# Any normal attributes are handled normally
if attr in self.__dict__:
self.__dict__[attr] = value
# Others are entries in the table
else:
self.tbl[attr] = value
def __str__(self):
return self.tbl.__str__()
def __repr__(self):
return self.tbl.__repr__()
def __getstate__(self):
return self.tbl.__repr__()
def __setstate__(self, pickled_state):
self.tbl = eval(pickled_state)
def __iter__(self):
return iter(self.tbl.keys())
def __len__(self):
return len(self.tbl)
def __contains__(self, key):
return key in self.tbl
def update(self, dict2):
return self.tbl.update(dict2)
def store(self, dict2):
return self.tbl.update(dict2)
def setvals(self, **kwdargs):
return self.tbl.update(kwdargs)
def fetch(self, keyDict):
"""Like update(), but for retrieving values.
"""
for key in keyDict.keys():
keyDict[key] = self.tbl[key]
def fetchDict(self, keyDict):
res = {}
for key in keyDict.keys():
res[key] = self.tbl[key]
return res
def fetchList(self, keySeq):
res = []
for key in keySeq:
res.append(self.tbl[key])
return res
def keys(self):
return self.tbl.keys()
def has_key(self, key):
return key in self.tbl
def get(self, key, alt=None):
if key in self:
return self.__getitem__(key)
return alt
def setdefault(self, key, val):
if key in self:
return self.__getitem__(key)
else:
self.__setitem__(key, val)
return val
def items(self):
return self.tbl.items()
def values(self):
return self.tbl.values()
def copy(self):
return self.tbl.copy()
class threadSafeBunch(object):
"""Like a Bunch, but with thread safety built-in. Multiple threads can
try to read and write the bunch concurrently and it all just works.
"""
def __init__(self, inDict=None, caseless=False, **kwdargs):
self.lock = threading.RLock()
if caseless:
self.tbl = caselessDict(inDict=inDict)
else:
self.tbl = {}
if inDict is not None:
self.tbl.update(inDict)
self.tbl.update(kwdargs)
# After initialisation, setting attributes is the same as setting
# an item.
self.__initialised = True
def enter(self):
"""Acquires the lock used for this Bunch. USE WITH EXTREME CAUTION!
"""
return self.lock.acquire()
def leave(self):
"""Releases the lock on this Bunch. USE WITH EXTREME CAUTION!
"""
return self.lock.release()
def getlock(self):
"""Returns the lock used for this Bunch. USE WITH EXTREME CAUTION!
"""
return self.lock
def getitem(self, key):
"""Maps dictionary keys to values.
Called for dictionary style access of this object.
"""
with self.lock:
return self.tbl[key]
def __getitem__(self, key):
return self.getitem(key)
def fetch(self, keyDict):
"""Like update(), but for retrieving values.
"""
with self.lock:
for key in keyDict.keys():
keyDict[key] = self.tbl[key]
def fetchDict(self, keyDict):
with self.lock:
res = {}
for key in keyDict.keys():
res[key] = self.tbl[key]
return res
def fetchList(self, keySeq):
with self.lock:
res = []
for key in keySeq:
res.append(self.tbl[key])
return res
def setitem(self, key, value):
"""Maps dictionary keys to values for assignment. Called for
dictionary style access with assignment.
"""
with self.lock:
self.tbl[key] = value
def __setitem__(self, key, value):
return self.setitem(key, value)
def setvals(self, **kwdargs):
return self.update(kwdargs)
def delitem(self, key):
"""Deletes key/value pairs from object.
"""
with self.lock:
del self.tbl[key]
def __contains__(self, key):
with self.lock:
return key in self.tbl
def __delitem__(self, key):
return self.delitem(key)
def __getattr__(self, key):
"""Maps values to attributes.
Only called if there *isn't* an attribute with this name.
Called for attribute style access of this object.
"""
return self.getitem(key)
def __setattr__(self, key, value):
"""Maps attributes to values for assignment.
Called for attribute style access of this object for assignment.
"""
# this test allows attributes to be set in the __init__ method
# (self.__dict__[_threadSafeBunch__initialised] same as
# self.__initialized)
if '_threadSafeBunch__initialised' not in self.__dict__:
self.__dict__[key] = value
else:
with self.lock:
# Any normal attributes are handled normally
if key in self.__dict__:
self.__dict__[key] = value
# Others are entries in the table
else:
self.tbl[key] = value
def __delattr__(self, key):
"""Deletes key/value pairs from object.
"""
with self.lock:
del self.tbl[key]
def __str__(self):
with self.lock:
return self.tbl.__str__()
def __len__(self):
with self.lock:
return len(self.tbl)
def __repr__(self):
with self.lock:
return self.tbl.__repr__()
def clear(self):
"""Clears all key/value pairs.
"""
with self.lock:
self.tbl.clear()
##############################################################
# the following methods are inherited by subclasses
##############################################################
def has_key(self, key):
"""Checks for membership of dictionary key.
"""
with self.lock:
return key in self.tbl
def keys(self):
"""Returns list of keys.
"""
with self.lock:
return self.tbl.keys()
def values(self):
"""Returns list of values.
"""
with self.lock:
return self.tbl.values()
def update(self, updict):
"""Updates key/value pairs in dictionary from _updict_.
"""
with self.lock:
for (key, value) in updict.items():
self.setitem(key, value)
def items(self):
"""Returns list of items.
"""
with self.lock:
return self.tbl.items()
def get(self, key, alt=None):
"""If dictionary contains _key_ return the associated value,
otherwise return _alt_.
"""
with self.lock:
if key in self:
return self.getitem(key)
else:
return alt
def setdefault(self, key, value):
"""Atomic store conditional. Stores _value_ into dictionary
at _key_, but only if _key_ does not already exist in the dictionary.
Returns the old value found or the new value.
"""
with self.lock:
if key in self:
return self.getitem(key)
else:
self.setitem(key, value)
return value
def getsetitem(self, key, klass, args=None, kwdargs=None):
"""This is similar to setdefault(), except that the new value is
created by instantiating _klass_. This prevents you from having
to create an object and initialize it and then throw it away if
there is already a dictionary item of that type.
"""
with self.lock:
if key in self:
return self.getitem(key)
# Instantiate value.
if not args:
args = []
if not kwdargs:
kwdargs = {}
value = klass(*args, **kwdargs)
self.setitem(key, value)
return value
# Undoubtedly there are more dictionary interface methods ...
class threadSafeList(object):
"""Like a list, but thread-safe.
"""
def __init__(self, *args):
self.lock = threading.RLock()
self.list = list(args)
def append(self, item):
with self.lock:
self.list.append(item)
def extend(self, list2):
with self.lock:
self.list.extend(list2)
def prepend(self, item):
with self.lock:
self.list = [item].extend(self.list)
return self.list
def cons(self, item):
return self.prepend(item)
#END Bunch.py
|