File: testpickle.py

package info (click to toggle)
python-param 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,048 kB
  • sloc: python: 17,980; makefile: 3
file content (212 lines) | stat: -rw-r--r-- 6,167 bytes parent folder | download
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
import pickle

import param
import pytest

try:
    import cloudpickle
except ImportError:
    cloudpickle = None
try:
    import numpy as np
except:
    np = None
try:
    import pandas as pd
except:
    pd = None


def eq(o1, o2):
    if not sorted(o1.param) == sorted(o2.param):
        return False

    for pname in o1.param:
        if getattr(o1, pname) != getattr(o2, pname):
            return False
    return True


@pytest.fixture
def pickler(request):
    if request.param is None:
        pytest.skip('cloudpickle not available')
    return request.param


class P1(param.Parameterized):
    x = param.Parameter()

@pytest.mark.parametrize('pickler', [cloudpickle, pickle], indirect=True)
@pytest.mark.parametrize('protocol', [0, pickle.DEFAULT_PROTOCOL, pickle.HIGHEST_PROTOCOL])
def test_pickle_simple_class(pickler, protocol):
    s = pickler.dumps(P1, protocol=protocol)
    cls = pickler.loads(s)
    assert cls is P1


@pytest.mark.parametrize('pickler', [cloudpickle, pickle], indirect=True)
@pytest.mark.parametrize('protocol', [0, pickle.DEFAULT_PROTOCOL, pickle.HIGHEST_PROTOCOL])
def test_pickle_simple_instance(pickler, protocol):
    p = P1()
    s = pickler.dumps(p, protocol=protocol)
    inst = pickler.loads(s)
    assert eq(p, inst)


@pytest.mark.parametrize('pickler', [cloudpickle, pickle], indirect=True)
@pytest.mark.parametrize('protocol', [0, pickle.DEFAULT_PROTOCOL, pickle.HIGHEST_PROTOCOL])
def test_pickle_simple_instance_modif_after(pickler, protocol):
    p = P1()
    s = pickler.dumps(p, protocol=protocol)
    p.x = 'modified'
    inst = pickler.loads(s)
    assert not eq(p, inst)
    assert inst.x is None


class P2(param.Parameterized):
    a = param.Parameter()
    b = param.String()
    c = param.Dynamic()
    d = param.Number()
    e = param.Integer()
    f = param.Action()
    g = param.Event()
    h = param.Callable()
    i = param.Tuple()
    k = param.NumericTuple()
    l = param.Range()
    m = param.XYCoordinates()
    n = param.CalendarDateRange()
    o = param.DateRange()
    p = param.List()
    q = param.HookList()
    r = param.Path()
    s = param.Filename()
    t = param.Foldername()
    u = param.Date()
    v = param.CalendarDate()
    w = param.Selector()
    x = param.ObjectSelector()
    y = param.FileSelector()
    z = param.ListSelector()
    aa = param.MultiFileSelector()
    ab = param.ClassSelector(class_=type(None))
    ac = None if pd is None else param.Series()
    ad = param.Dict()
    ae = None if pd is None else param.DataFrame()
    af = None if np is None else param.Array()


@pytest.mark.parametrize('pickler', [cloudpickle, pickle], indirect=True)
@pytest.mark.parametrize('protocol', [0, pickle.DEFAULT_PROTOCOL, pickle.HIGHEST_PROTOCOL])
def test_pickle_all_parameters_class(pickler, protocol):
    s = pickler.dumps(P2, protocol=protocol)
    cls = pickler.loads(s)
    assert cls is P2


@pytest.mark.parametrize('pickler', [cloudpickle, pickle], indirect=True)
@pytest.mark.parametrize('protocol', [0, pickle.DEFAULT_PROTOCOL, pickle.HIGHEST_PROTOCOL])
def test_pickle_all_parameters_instance(pickler, protocol):
    p = P2()
    s = pickler.dumps(p, protocol=protocol)
    inst = pickler.loads(s)
    assert eq(p, inst)


class P3(param.Parameterized):
    a = param.Integer(0)
    count = param.Integer(0)

    @param.depends("a", watch=True)
    def cb(self):
        self.count += 1


@pytest.mark.parametrize('pickler', [cloudpickle, pickle], indirect=True)
@pytest.mark.parametrize('protocol', [0, pickle.DEFAULT_PROTOCOL, pickle.HIGHEST_PROTOCOL])
def test_pickle_depends_watch_class(pickler, protocol):
    s = pickler.dumps(P3, protocol=protocol)
    cls = pickler.loads(s)
    assert cls is P3


@pytest.mark.parametrize('pickler', [cloudpickle, pickle], indirect=True)
@pytest.mark.parametrize('protocol', [0, pickle.DEFAULT_PROTOCOL, pickle.HIGHEST_PROTOCOL])
def test_pickle_depends_watch_instance(pickler, protocol):
    # https://github.com/holoviz/param/issues/757
    p = P3()
    s = pickler.dumps(p, protocol=protocol)
    inst = pickler.loads(s)
    assert eq(p, inst)

    inst.a += 1
    assert inst.count == 1


class P4(param.Parameterized):
    a = param.Parameter()
    b = param.Parameter()

    single_count = param.Integer()
    attr_count = param.Integer()
    single_nested_count = param.Integer()
    double_nested_count = param.Integer()
    nested_attr_count = param.Integer()
    nested_count = param.Integer()

    @param.depends('a', watch=True)
    def single_parameter(self):
        self.single_count += 1

    @param.depends('a:constant', watch=True)
    def constant(self):
        self.attr_count += 1

    @param.depends('b.a', watch=True)
    def single_nested(self):
        self.single_nested_count += 1

    @param.depends('b.b.a', watch=True)
    def double_nested(self):
        self.double_nested_count += 1

    @param.depends('b.a:constant', watch=True)
    def nested_attribute(self):
        self.nested_attr_count += 1

    @param.depends('b.param', watch=True)
    def nested(self):
        self.nested_count += 1


@pytest.mark.parametrize('pickler', [cloudpickle, pickle], indirect=True)
@pytest.mark.parametrize('protocol', [0, pickle.DEFAULT_PROTOCOL, pickle.HIGHEST_PROTOCOL])
def test_pickle_complex_depends_class(pickler, protocol):
    s = pickler.dumps(P4, protocol=protocol)
    cls = pickler.loads(s)
    assert cls is P4


@pytest.mark.parametrize('pickler', [cloudpickle, pickle], indirect=True)
@pytest.mark.parametrize('protocol', [0, pickle.DEFAULT_PROTOCOL, pickle.HIGHEST_PROTOCOL])
def test_pickle_complex_depends_instance(pickler, protocol):
    p = P4()
    s = pickler.dumps(p, protocol=protocol)
    inst = pickler.loads(s)
    assert eq(p, inst)


@pytest.mark.skipif(cloudpickle is None, reason='cloudpickle not available')
@pytest.mark.parametrize('protocol', [0, pickle.DEFAULT_PROTOCOL, pickle.HIGHEST_PROTOCOL])
def test_issue_757(protocol):
    # https://github.com/holoviz/param/issues/759
    class P(param.Parameterized):
        a = param.Parameter()

    p = P()
    s = cloudpickle.dumps(p, protocol=protocol)
    inst = cloudpickle.loads(s)
    assert eq(p, inst)