summaryrefslogtreecommitdiff
path: root/python-objsize.spec
blob: 810e73d01c07f020785f69e2164bf8e38e5a471a (plain)
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
%global _empty_manifest_terminate_build 0
Name:		python-objsize
Version:	0.6.1
Release:	1
Summary:	Traversal over Python's objects subtree and calculate the total size of the subtree in bytes (deep size).
License:	BSD-3-Clause
URL:		https://github.com/liran-funaro/objsize
Source0:	https://mirrors.nju.edu.cn/pypi/web/packages/59/12/f62869f8c5f436cc7beedaccc8bfc97113943e0541c22c8505c3b2555616/objsize-0.6.1.tar.gz
BuildArch:	noarch

Requires:	python3-black
Requires:	python3-bumpver
Requires:	python3-isort
Requires:	python3-pip-tools
Requires:	python3-pytest
Requires:	python3-pytest-cov
Requires:	python3-coveralls

%description
# objsize
[![Coverage Status](https://coveralls.io/repos/github/liran-funaro/objsize/badge.svg?branch=master)](https://coveralls.io/github/liran-funaro/objsize?branch=master)
Traversal over Python's objects subtree and calculate the total size of the subtree in bytes (deep size).
This module traverses all child objects using Python's internal GC implementation.
It attempts to ignore shared objects (i.e., `None`, types, modules, classes, functions, lambdas), as they are common
among all objects.
It is implemented without recursive calls for high performance.
# Features
- Traverse objects' subtree
- Calculate objects' (deep) size in bytes
- Exclude non-exclusive objects
- Exclude specified objects subtree
- Allow the user to specify unique handlers for:
    - Object's size calculation
    - Object's referents (i.e., its children)
    - Object filter (skip specific objects)
[Pympler](https://pythonhosted.org/Pympler/) also supports determining an object deep size via `pympler.asizeof()`.
There are two main differences between `objsize` and `pympler`.
1. `objsize` has additional features:
    * Traversing the object subtree: iterating all the object's descendants one by one.
    * Excluding non-exclusive objects. That is, objects that are also referenced from somewhere else in the program.
      This is true for calculating the object's deep size and for traversing its descendants.
2. `objsize` has a simple and robust implementation with significantly fewer lines of code, compared to `pympler`.
   The Pympler implementation uses recursion, and thus have to use a maximal depth argument to avoid reaching Python's
   max depth.
   `objsize`, however, uses BFS which is more efficient and simple to follow.
   Moreover, the Pympler implementation carefully takes care of any object type.
   `objsize` archives the same goal with a simple and generic implementation, which has fewer lines of code.
# Install
```bash
pip install objsize==0.6.1
```
# Basic Usage
Calculate the size of the object including all its members in bytes.
```pycon
>>> import objsize
>>> objsize.get_deep_size(dict(arg1='hello', arg2='world'))
340
```
It is possible to calculate the deep size of multiple objects by passing multiple arguments:
```pycon
>>> objsize.get_deep_size(['hello', 'world'], dict(arg1='hello', arg2='world'), {'hello', 'world'})
628
```
# Complex Data
`objsize` can calculate the size of an object's entire subtree in bytes regardless of the type of objects in it, and its
depth.
Here is a complex data structure, for example, that include a self reference:
```python
my_data = (list(range(3)), list(range(3, 6)))
class MyClass:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.d = {'x': x, 'y': y, 'self': self}
    def __repr__(self):
        return "MyClass"
my_obj = MyClass(*my_data)
```
We can calculate `my_obj` deep size, including its stored data.
```pycon
>>> objsize.get_deep_size(my_obj)
708
```
We might want to ignore non-exclusive objects such as the ones stored in `my_data`.
```pycon
>>> objsize.get_deep_size(my_obj, exclude=[my_data])
384
```
Or simply let `objsize` detect that automatically:
```pycon
>>> objsize.get_exclusive_deep_size(my_obj)
384
```
# Non Shared Functions or Classes
`objsize` filters functions, lambdas, and classes by default since they are usually shared among many objects.
For example:
```pycon
>>> method_dict = {"identity": lambda x: x, "double": lambda x: x*2}
>>> objsize.get_deep_size(method_dict)
232
```
Some objects, however, as illustrated in the above example, have unique functions not shared by other objects.
Due to this, it may be useful to count their sizes.
You can achieve this by providing an alternative filter function.
```pycon
>>> objsize.get_deep_size(method_dict, filter_func=objsize.shared_object_filter)
986
```
Notes:
* The default filter function is `objsize.shared_object_or_function_filter`.
* When using `objsize.shared_object_filter`, shared functions and lambdas are also counted, but builtin functions are
  still excluded.
# Special Cases
Some objects handle their data in a way that prevents Python's GC from detecting it.
The user can supply a special way to calculate the actual size of these objects.
## Case 1: `torch`
Using a simple calculation of the object size won't work for `torch.Tensor`.
```pycon
>>> import torch
>>> objsize.get_deep_size(torch.rand(200))
72
```
So the user can define its own size calculation handler for such cases:
```python
import objsize
import sys
import torch
def get_size_of_torch(o):
    # `objsize.safe_is_instance` catches `ReferenceError` caused by `weakref` objects
    if objsize.safe_is_instance(o, torch.Tensor):
        return sys.getsizeof(o.storage())
    else:
        return sys.getsizeof(o)
```
Then use it as follows:
```pycon
>>> import torch
>>> objsize.get_deep_size(
848
```
However, this neglects the object's internal structure.
The user can help `objsize` to find the object's hidden storage by supplying it with its own referent and filter
functions:
```python
import objsize
import gc
import torch
def get_referents_torch(*objs):
    # Yield all native referents
    yield from gc.get_referents(*objs)
    for o in objs:
        # If the object is a torch tensor, then also yield its storage
        if type(o) == torch.Tensor:
            yield o.storage()
def filter_func(o):
    # Torch storage points to another meta storage that is
    # already included in the outer storage calculation, 
    # so we need to filter it.
    # Also, `torch.dtype` is a common object like Python's types.
    return not objsize.safe_is_instance(o, (
        *objsize.SharedObjectOrFunctionType, torch.storage._UntypedStorage, torch.dtype
    ))
```
Then use these as follows:
```pycon
>>> import torch
>>> objsize.get_deep_size(
1024
```
## Case 2: `weakref`
Using a simple calculation of the object size won't work for `weakref.proxy`.
```pycon
>>> import weakref
>>> class Foo(list):
>>> o = Foo([0]*100)
>>> objsize.get_deep_size(o)
896
>>> o_ref = weakref.proxy(o)
>>> objsize.get_deep_size(o_ref)
72
```
To mitigate this, you can provide a method that attempts to fetch the proxy's referents:
```python
import weakref
import gc
def get_weakref_referents(*objs):
    yield from gc.get_referents(*objs)
    for o in objs:
        if type(o) in weakref.ProxyTypes:
            try:
                yield o.__repr__.__self__
            except ReferenceError:
                pass
```
Then use it as follows:
```pycon
>>> objsize.get_deep_size(o_ref, get_referents_func=get_weakref_referents)
968
```
After the referenced object will be collected, then the size of the proxy object will be reduced.
```pycon
>>> del o
>>> gc.collect()
>>> # Wait for the object to be collected 
>>> objsize.get_deep_size(o_ref, get_referents_func=get_weakref_referents)
72
```
# Traversal
A user can implement its own function over the entire subtree using the traversal method, which traverses all the
objects in the subtree.
```pycon
>>> for o in objsize.traverse_bfs(my_obj):
MyClass
{'x': [0, 1, 2], 'y': [3, 4, 5], 'd': {'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}}
[0, 1, 2]
[3, 4, 5]
{'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}
2
1
0
5
4
3
```
Similar to before, non-exclusive objects can be ignored.
```pycon
>>> for o in objsize.traverse_exclusive_bfs(my_obj):
MyClass
{'x': [0, 1, 2], 'y': [3, 4, 5], 'd': {'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}}
{'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}
```
# License
[BSD-3](LICENSE)

%package -n python3-objsize
Summary:	Traversal over Python's objects subtree and calculate the total size of the subtree in bytes (deep size).
Provides:	python-objsize
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
BuildRequires:	python3-pip
%description -n python3-objsize
# objsize
[![Coverage Status](https://coveralls.io/repos/github/liran-funaro/objsize/badge.svg?branch=master)](https://coveralls.io/github/liran-funaro/objsize?branch=master)
Traversal over Python's objects subtree and calculate the total size of the subtree in bytes (deep size).
This module traverses all child objects using Python's internal GC implementation.
It attempts to ignore shared objects (i.e., `None`, types, modules, classes, functions, lambdas), as they are common
among all objects.
It is implemented without recursive calls for high performance.
# Features
- Traverse objects' subtree
- Calculate objects' (deep) size in bytes
- Exclude non-exclusive objects
- Exclude specified objects subtree
- Allow the user to specify unique handlers for:
    - Object's size calculation
    - Object's referents (i.e., its children)
    - Object filter (skip specific objects)
[Pympler](https://pythonhosted.org/Pympler/) also supports determining an object deep size via `pympler.asizeof()`.
There are two main differences between `objsize` and `pympler`.
1. `objsize` has additional features:
    * Traversing the object subtree: iterating all the object's descendants one by one.
    * Excluding non-exclusive objects. That is, objects that are also referenced from somewhere else in the program.
      This is true for calculating the object's deep size and for traversing its descendants.
2. `objsize` has a simple and robust implementation with significantly fewer lines of code, compared to `pympler`.
   The Pympler implementation uses recursion, and thus have to use a maximal depth argument to avoid reaching Python's
   max depth.
   `objsize`, however, uses BFS which is more efficient and simple to follow.
   Moreover, the Pympler implementation carefully takes care of any object type.
   `objsize` archives the same goal with a simple and generic implementation, which has fewer lines of code.
# Install
```bash
pip install objsize==0.6.1
```
# Basic Usage
Calculate the size of the object including all its members in bytes.
```pycon
>>> import objsize
>>> objsize.get_deep_size(dict(arg1='hello', arg2='world'))
340
```
It is possible to calculate the deep size of multiple objects by passing multiple arguments:
```pycon
>>> objsize.get_deep_size(['hello', 'world'], dict(arg1='hello', arg2='world'), {'hello', 'world'})
628
```
# Complex Data
`objsize` can calculate the size of an object's entire subtree in bytes regardless of the type of objects in it, and its
depth.
Here is a complex data structure, for example, that include a self reference:
```python
my_data = (list(range(3)), list(range(3, 6)))
class MyClass:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.d = {'x': x, 'y': y, 'self': self}
    def __repr__(self):
        return "MyClass"
my_obj = MyClass(*my_data)
```
We can calculate `my_obj` deep size, including its stored data.
```pycon
>>> objsize.get_deep_size(my_obj)
708
```
We might want to ignore non-exclusive objects such as the ones stored in `my_data`.
```pycon
>>> objsize.get_deep_size(my_obj, exclude=[my_data])
384
```
Or simply let `objsize` detect that automatically:
```pycon
>>> objsize.get_exclusive_deep_size(my_obj)
384
```
# Non Shared Functions or Classes
`objsize` filters functions, lambdas, and classes by default since they are usually shared among many objects.
For example:
```pycon
>>> method_dict = {"identity": lambda x: x, "double": lambda x: x*2}
>>> objsize.get_deep_size(method_dict)
232
```
Some objects, however, as illustrated in the above example, have unique functions not shared by other objects.
Due to this, it may be useful to count their sizes.
You can achieve this by providing an alternative filter function.
```pycon
>>> objsize.get_deep_size(method_dict, filter_func=objsize.shared_object_filter)
986
```
Notes:
* The default filter function is `objsize.shared_object_or_function_filter`.
* When using `objsize.shared_object_filter`, shared functions and lambdas are also counted, but builtin functions are
  still excluded.
# Special Cases
Some objects handle their data in a way that prevents Python's GC from detecting it.
The user can supply a special way to calculate the actual size of these objects.
## Case 1: `torch`
Using a simple calculation of the object size won't work for `torch.Tensor`.
```pycon
>>> import torch
>>> objsize.get_deep_size(torch.rand(200))
72
```
So the user can define its own size calculation handler for such cases:
```python
import objsize
import sys
import torch
def get_size_of_torch(o):
    # `objsize.safe_is_instance` catches `ReferenceError` caused by `weakref` objects
    if objsize.safe_is_instance(o, torch.Tensor):
        return sys.getsizeof(o.storage())
    else:
        return sys.getsizeof(o)
```
Then use it as follows:
```pycon
>>> import torch
>>> objsize.get_deep_size(
848
```
However, this neglects the object's internal structure.
The user can help `objsize` to find the object's hidden storage by supplying it with its own referent and filter
functions:
```python
import objsize
import gc
import torch
def get_referents_torch(*objs):
    # Yield all native referents
    yield from gc.get_referents(*objs)
    for o in objs:
        # If the object is a torch tensor, then also yield its storage
        if type(o) == torch.Tensor:
            yield o.storage()
def filter_func(o):
    # Torch storage points to another meta storage that is
    # already included in the outer storage calculation, 
    # so we need to filter it.
    # Also, `torch.dtype` is a common object like Python's types.
    return not objsize.safe_is_instance(o, (
        *objsize.SharedObjectOrFunctionType, torch.storage._UntypedStorage, torch.dtype
    ))
```
Then use these as follows:
```pycon
>>> import torch
>>> objsize.get_deep_size(
1024
```
## Case 2: `weakref`
Using a simple calculation of the object size won't work for `weakref.proxy`.
```pycon
>>> import weakref
>>> class Foo(list):
>>> o = Foo([0]*100)
>>> objsize.get_deep_size(o)
896
>>> o_ref = weakref.proxy(o)
>>> objsize.get_deep_size(o_ref)
72
```
To mitigate this, you can provide a method that attempts to fetch the proxy's referents:
```python
import weakref
import gc
def get_weakref_referents(*objs):
    yield from gc.get_referents(*objs)
    for o in objs:
        if type(o) in weakref.ProxyTypes:
            try:
                yield o.__repr__.__self__
            except ReferenceError:
                pass
```
Then use it as follows:
```pycon
>>> objsize.get_deep_size(o_ref, get_referents_func=get_weakref_referents)
968
```
After the referenced object will be collected, then the size of the proxy object will be reduced.
```pycon
>>> del o
>>> gc.collect()
>>> # Wait for the object to be collected 
>>> objsize.get_deep_size(o_ref, get_referents_func=get_weakref_referents)
72
```
# Traversal
A user can implement its own function over the entire subtree using the traversal method, which traverses all the
objects in the subtree.
```pycon
>>> for o in objsize.traverse_bfs(my_obj):
MyClass
{'x': [0, 1, 2], 'y': [3, 4, 5], 'd': {'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}}
[0, 1, 2]
[3, 4, 5]
{'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}
2
1
0
5
4
3
```
Similar to before, non-exclusive objects can be ignored.
```pycon
>>> for o in objsize.traverse_exclusive_bfs(my_obj):
MyClass
{'x': [0, 1, 2], 'y': [3, 4, 5], 'd': {'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}}
{'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}
```
# License
[BSD-3](LICENSE)

%package help
Summary:	Development documents and examples for objsize
Provides:	python3-objsize-doc
%description help
# objsize
[![Coverage Status](https://coveralls.io/repos/github/liran-funaro/objsize/badge.svg?branch=master)](https://coveralls.io/github/liran-funaro/objsize?branch=master)
Traversal over Python's objects subtree and calculate the total size of the subtree in bytes (deep size).
This module traverses all child objects using Python's internal GC implementation.
It attempts to ignore shared objects (i.e., `None`, types, modules, classes, functions, lambdas), as they are common
among all objects.
It is implemented without recursive calls for high performance.
# Features
- Traverse objects' subtree
- Calculate objects' (deep) size in bytes
- Exclude non-exclusive objects
- Exclude specified objects subtree
- Allow the user to specify unique handlers for:
    - Object's size calculation
    - Object's referents (i.e., its children)
    - Object filter (skip specific objects)
[Pympler](https://pythonhosted.org/Pympler/) also supports determining an object deep size via `pympler.asizeof()`.
There are two main differences between `objsize` and `pympler`.
1. `objsize` has additional features:
    * Traversing the object subtree: iterating all the object's descendants one by one.
    * Excluding non-exclusive objects. That is, objects that are also referenced from somewhere else in the program.
      This is true for calculating the object's deep size and for traversing its descendants.
2. `objsize` has a simple and robust implementation with significantly fewer lines of code, compared to `pympler`.
   The Pympler implementation uses recursion, and thus have to use a maximal depth argument to avoid reaching Python's
   max depth.
   `objsize`, however, uses BFS which is more efficient and simple to follow.
   Moreover, the Pympler implementation carefully takes care of any object type.
   `objsize` archives the same goal with a simple and generic implementation, which has fewer lines of code.
# Install
```bash
pip install objsize==0.6.1
```
# Basic Usage
Calculate the size of the object including all its members in bytes.
```pycon
>>> import objsize
>>> objsize.get_deep_size(dict(arg1='hello', arg2='world'))
340
```
It is possible to calculate the deep size of multiple objects by passing multiple arguments:
```pycon
>>> objsize.get_deep_size(['hello', 'world'], dict(arg1='hello', arg2='world'), {'hello', 'world'})
628
```
# Complex Data
`objsize` can calculate the size of an object's entire subtree in bytes regardless of the type of objects in it, and its
depth.
Here is a complex data structure, for example, that include a self reference:
```python
my_data = (list(range(3)), list(range(3, 6)))
class MyClass:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.d = {'x': x, 'y': y, 'self': self}
    def __repr__(self):
        return "MyClass"
my_obj = MyClass(*my_data)
```
We can calculate `my_obj` deep size, including its stored data.
```pycon
>>> objsize.get_deep_size(my_obj)
708
```
We might want to ignore non-exclusive objects such as the ones stored in `my_data`.
```pycon
>>> objsize.get_deep_size(my_obj, exclude=[my_data])
384
```
Or simply let `objsize` detect that automatically:
```pycon
>>> objsize.get_exclusive_deep_size(my_obj)
384
```
# Non Shared Functions or Classes
`objsize` filters functions, lambdas, and classes by default since they are usually shared among many objects.
For example:
```pycon
>>> method_dict = {"identity": lambda x: x, "double": lambda x: x*2}
>>> objsize.get_deep_size(method_dict)
232
```
Some objects, however, as illustrated in the above example, have unique functions not shared by other objects.
Due to this, it may be useful to count their sizes.
You can achieve this by providing an alternative filter function.
```pycon
>>> objsize.get_deep_size(method_dict, filter_func=objsize.shared_object_filter)
986
```
Notes:
* The default filter function is `objsize.shared_object_or_function_filter`.
* When using `objsize.shared_object_filter`, shared functions and lambdas are also counted, but builtin functions are
  still excluded.
# Special Cases
Some objects handle their data in a way that prevents Python's GC from detecting it.
The user can supply a special way to calculate the actual size of these objects.
## Case 1: `torch`
Using a simple calculation of the object size won't work for `torch.Tensor`.
```pycon
>>> import torch
>>> objsize.get_deep_size(torch.rand(200))
72
```
So the user can define its own size calculation handler for such cases:
```python
import objsize
import sys
import torch
def get_size_of_torch(o):
    # `objsize.safe_is_instance` catches `ReferenceError` caused by `weakref` objects
    if objsize.safe_is_instance(o, torch.Tensor):
        return sys.getsizeof(o.storage())
    else:
        return sys.getsizeof(o)
```
Then use it as follows:
```pycon
>>> import torch
>>> objsize.get_deep_size(
848
```
However, this neglects the object's internal structure.
The user can help `objsize` to find the object's hidden storage by supplying it with its own referent and filter
functions:
```python
import objsize
import gc
import torch
def get_referents_torch(*objs):
    # Yield all native referents
    yield from gc.get_referents(*objs)
    for o in objs:
        # If the object is a torch tensor, then also yield its storage
        if type(o) == torch.Tensor:
            yield o.storage()
def filter_func(o):
    # Torch storage points to another meta storage that is
    # already included in the outer storage calculation, 
    # so we need to filter it.
    # Also, `torch.dtype` is a common object like Python's types.
    return not objsize.safe_is_instance(o, (
        *objsize.SharedObjectOrFunctionType, torch.storage._UntypedStorage, torch.dtype
    ))
```
Then use these as follows:
```pycon
>>> import torch
>>> objsize.get_deep_size(
1024
```
## Case 2: `weakref`
Using a simple calculation of the object size won't work for `weakref.proxy`.
```pycon
>>> import weakref
>>> class Foo(list):
>>> o = Foo([0]*100)
>>> objsize.get_deep_size(o)
896
>>> o_ref = weakref.proxy(o)
>>> objsize.get_deep_size(o_ref)
72
```
To mitigate this, you can provide a method that attempts to fetch the proxy's referents:
```python
import weakref
import gc
def get_weakref_referents(*objs):
    yield from gc.get_referents(*objs)
    for o in objs:
        if type(o) in weakref.ProxyTypes:
            try:
                yield o.__repr__.__self__
            except ReferenceError:
                pass
```
Then use it as follows:
```pycon
>>> objsize.get_deep_size(o_ref, get_referents_func=get_weakref_referents)
968
```
After the referenced object will be collected, then the size of the proxy object will be reduced.
```pycon
>>> del o
>>> gc.collect()
>>> # Wait for the object to be collected 
>>> objsize.get_deep_size(o_ref, get_referents_func=get_weakref_referents)
72
```
# Traversal
A user can implement its own function over the entire subtree using the traversal method, which traverses all the
objects in the subtree.
```pycon
>>> for o in objsize.traverse_bfs(my_obj):
MyClass
{'x': [0, 1, 2], 'y': [3, 4, 5], 'd': {'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}}
[0, 1, 2]
[3, 4, 5]
{'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}
2
1
0
5
4
3
```
Similar to before, non-exclusive objects can be ignored.
```pycon
>>> for o in objsize.traverse_exclusive_bfs(my_obj):
MyClass
{'x': [0, 1, 2], 'y': [3, 4, 5], 'd': {'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}}
{'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}
```
# License
[BSD-3](LICENSE)

%prep
%autosetup -n objsize-0.6.1

%build
%py3_build

%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
	find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
	find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
	find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
	find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
	find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .

%files -n python3-objsize -f filelist.lst
%dir %{python3_sitelib}/*

%files help -f doclist.lst
%{_docdir}/*

%changelog
* Mon Apr 10 2023 Python_Bot <Python_Bot@openeuler.org> - 0.6.1-1
- Package Spec generated