BagObj(obj) Convert attribute look-ups to getitems on the object passed in. Parameters ---------- obj : class instance Object on which attribute look-up is performed. Examples -------- >>> from numpy.lib.npyio import BagObj as BO >>> class BagDemo:
| 40 | |
| 41 | |
| 42 | class BagObj: |
| 43 | """ |
| 44 | BagObj(obj) |
| 45 | |
| 46 | Convert attribute look-ups to getitems on the object passed in. |
| 47 | |
| 48 | Parameters |
| 49 | ---------- |
| 50 | obj : class instance |
| 51 | Object on which attribute look-up is performed. |
| 52 | |
| 53 | Examples |
| 54 | -------- |
| 55 | >>> from numpy.lib.npyio import BagObj as BO |
| 56 | >>> class BagDemo: |
| 57 | ... def __getitem__(self, key): # An instance of BagObj(BagDemo) |
| 58 | ... # will call this method when any |
| 59 | ... # attribute look-up is required |
| 60 | ... result = "Doesn't matter what you want, " |
| 61 | ... return result + "you're gonna get this" |
| 62 | ... |
| 63 | >>> demo_obj = BagDemo() |
| 64 | >>> bagobj = BO(demo_obj) |
| 65 | >>> bagobj.hello_there |
| 66 | "Doesn't matter what you want, you're gonna get this" |
| 67 | >>> bagobj.I_can_be_anything |
| 68 | "Doesn't matter what you want, you're gonna get this" |
| 69 | |
| 70 | """ |
| 71 | |
| 72 | def __init__(self, obj): |
| 73 | # Use weakref to make NpzFile objects collectable by refcount |
| 74 | self._obj = weakref.proxy(obj) |
| 75 | |
| 76 | def __getattribute__(self, key): |
| 77 | try: |
| 78 | return object.__getattribute__(self, '_obj')[key] |
| 79 | except KeyError: |
| 80 | raise AttributeError(key) from None |
| 81 | |
| 82 | def __dir__(self): |
| 83 | """ |
| 84 | Enables dir(bagobj) to list the files in an NpzFile. |
| 85 | |
| 86 | This also enables tab-completion in an interpreter or IPython. |
| 87 | """ |
| 88 | return list(object.__getattribute__(self, '_obj').keys()) |
| 89 | |
| 90 | |
| 91 | def zipfile_factory(file, *args, **kwargs): |