File tree 2 files changed +54
-16
lines changed
2 files changed +54
-16
lines changed Original file line number Diff line number Diff line change 1
1
import nose
2
2
3
- from contextlib import contextmanager
4
3
import os
5
4
import datetime
6
5
import numpy as np
16
15
import pandas .util .testing as tm
17
16
from pandas .util .testing import (ensure_clean , assert_index_equal ,
18
17
assert_series_equal ,
19
- assert_frame_equal )
18
+ assert_frame_equal ,
19
+ patch )
20
20
from pandas .tests .test_panel import assert_panel_equal
21
21
22
22
import pandas
@@ -59,20 +59,6 @@ def check_arbitrary(a, b):
59
59
assert (a == b )
60
60
61
61
62
- @contextmanager
63
- def patch (ob , attr , value ):
64
- noattr = object () # mark that the attribute never existed
65
- old = getattr (ob , attr , noattr )
66
- setattr (ob , attr , value )
67
- try :
68
- yield
69
- finally :
70
- if old is noattr :
71
- delattr (ob , attr )
72
- else :
73
- setattr (ob , attr , old )
74
-
75
-
76
62
class TestPackers (tm .TestCase ):
77
63
78
64
def setUp (self ):
Original file line number Diff line number Diff line change @@ -2313,3 +2313,55 @@ class SubclassedDataFrame(DataFrame):
2313
2313
@property
2314
2314
def _constructor (self ):
2315
2315
return SubclassedDataFrame
2316
+
2317
+
2318
+ @contextmanager
2319
+ def patch (ob , attr , value ):
2320
+ """Temporarily patch an attribute of an object.
2321
+
2322
+ Parameters
2323
+ ----------
2324
+ ob : any
2325
+ The object to patch. This must support attribute assignment for `attr`.
2326
+ attr : str
2327
+ The name of the attribute to patch.
2328
+ value : any
2329
+ The temporary attribute to assign.
2330
+
2331
+ Examples
2332
+ --------
2333
+ >>> class C(object):
2334
+ ... attribute = 'original'
2335
+ ...
2336
+ >>> C.attribute
2337
+ 'original'
2338
+ >>> with patch(C, 'attribute', 'patched'):
2339
+ ... in_context = C.attribute
2340
+ ...
2341
+ >>> in_context
2342
+ 'patched'
2343
+ >>> C.attribute # the value is reset when the context manager exists
2344
+ 'original'
2345
+
2346
+ Correctly replaces attribute when the manager exits with an exception.
2347
+ >>> with patch(C, 'attribute', 'patched'):
2348
+ ... in_context = C.attribute
2349
+ ... raise ValueError()
2350
+ Traceback (most recent call last):
2351
+ ...
2352
+ ValueError
2353
+ >>> in_context
2354
+ 'patched'
2355
+ >>> C.attribute
2356
+ 'original'
2357
+ """
2358
+ noattr = object () # mark that the attribute never existed
2359
+ old = getattr (ob , attr , noattr )
2360
+ setattr (ob , attr , value )
2361
+ try :
2362
+ yield
2363
+ finally :
2364
+ if old is noattr :
2365
+ delattr (ob , attr )
2366
+ else :
2367
+ setattr (ob , attr , old )
You can’t perform that action at this time.
0 commit comments