2 if sys
.path
[0] != "../..":
3 sys
.path
.insert(0, "../..")
8 from pyx
.graph
import data
10 class DataTestCase(unittest
.TestCase
):
13 mydata
= data
.points([[1, 2, 3], [4, 5, 6]], a
=1, b
=2)
14 self
.failUnlessEqual(mydata
.columndata
[0], [1, 2])
15 self
.failUnlessEqual(mydata
.columns
["a"], [1, 4])
16 self
.failUnlessEqual(mydata
.columndata
[2], [2, 5])
17 self
.failUnlessEqual("c" in mydata
.columns
.keys(), 0)
20 mydata
= data
.values(a
=[1, 4])
21 self
.failUnlessEqual(mydata
.columns
["a"], [1, 4])
22 self
.failUnlessEqual("c" in mydata
.columns
.keys(), 0)
25 mydata
= data
.points([[1], [2]], a
=1)
26 mydata2
= data
.data(mydata
, a
="2*a", b
="2*$1*a", c
="4*$(i)*a*$(-1)", context
={"i":1})
27 self
.failUnlessEqual(mydata
.columns
["a"], [1, 2])
28 self
.failUnlessAlmostEqual(mydata2
.columns
["a"][0], 2.0)
29 self
.failUnlessAlmostEqual(mydata2
.columns
["a"][1], 4.0)
30 self
.failUnlessAlmostEqual(mydata2
.columns
["b"][0], 2.0)
31 self
.failUnlessAlmostEqual(mydata2
.columns
["b"][1], 8.0)
32 self
.failUnlessAlmostEqual(mydata2
.columns
["c"][0], 4.0)
33 self
.failUnlessAlmostEqual(mydata2
.columns
["c"][1], 32.0)
34 mydata3
= data
.data(mydata2
, a
="b", b
="2*c")
35 self
.failUnlessEqual(mydata3
.columns
["a"], mydata2
.columns
["b"])
36 self
.failUnlessAlmostEqual(mydata3
.columns
["b"][0], 2*mydata2
.columns
["c"][0])
37 self
.failUnlessAlmostEqual(mydata3
.columns
["b"][1], 2*mydata2
.columns
["c"][1])
42 mydata
= data
.points([[1], [2]], a
=1)
43 mydata2
= data
.data(mydata
, b
="two*a", c
="two*$1*a", d
="f($1)", context
=locals())
44 self
.failUnlessEqual(mydata
.columndata
[0], [1, 2])
45 self
.failUnlessAlmostEqual(mydata2
.columns
["b"][0], 2.0)
46 self
.failUnlessAlmostEqual(mydata2
.columns
["b"][1], 4.0)
47 self
.failUnlessAlmostEqual(mydata2
.columns
["c"][0], 2.0)
48 self
.failUnlessAlmostEqual(mydata2
.columns
["c"][1], 8.0)
49 self
.failUnlessAlmostEqual(mydata2
.columns
["d"][0], 1.0)
50 self
.failUnlessAlmostEqual(mydata2
.columns
["d"][1], 4.0)
53 testfile
= StringIO
.StringIO("""#a
58 mydata
= data
.file(testfile
, row
=0, a
="a", b
=2)
59 self
.failUnlessEqual(mydata
.columns
["row"], [1, 2, 3, 4])
60 self
.failUnlessAlmostEqual(mydata
.columns
["a"][0], 0.0)
61 self
.failUnlessAlmostEqual(mydata
.columns
["a"][1], 1.0)
62 self
.failUnlessAlmostEqual(mydata
.columns
["a"][2], 2.0)
63 self
.failUnlessAlmostEqual(mydata
.columns
["a"][3], 3.0)
64 self
.failUnlessEqual(mydata
.columns
["b"][0], None)
65 self
.failUnlessEqual(mydata
.columns
["b"][1], "eins")
66 self
.failUnlessEqual(mydata
.columns
["b"][2], "2")
67 self
.failUnlessEqual(mydata
.columns
["b"][3], "x\"x")
68 testfile
= StringIO
.StringIO("""#a
79 mydata
= data
.file(testfile
, title
="title", skiphead
=3, skiptail
=2, every
=2, row
=0)
80 self
.failUnlessEqual(mydata
.columns
["row"], [4, 6, 8])
81 self
.failUnlessEqual(mydata
.title
, "title")
84 testfile
= StringIO
.StringIO("""[sec1]
98 mydata
= data
.conffile(testfile
, sec
=0, a
="opt1", b
="opt2", c
="opt3", d
="val")
99 self
.failUnlessEqual(mydata
.columns
["sec"], ["sec1", "sec2"])
100 self
.failUnlessEqual(mydata
.columns
["a"], ["a1", "a4"])
101 self
.failUnlessEqual(mydata
.columns
["b"], ["a2", "a5"])
102 self
.failUnlessEqual(mydata
.columns
["c"], ["a3", None])
103 self
.failUnlessAlmostEqual(mydata
.columns
["d"][0], 2.0)
104 self
.failUnlessAlmostEqual(mydata
.columns
["d"][1], 1.0)
106 def testParamfunction(self
):
107 mydata
= data
.paramfunction("k", 0, 9, "x, y = k, -k", points
=10)
109 self
.failUnlessEqual(mydata
.columns
["x"][i
], i
)
110 self
.failUnlessEqual(mydata
.columns
["y"][i
], -i
)
113 if __name__
== "__main__":