2
# -*- coding: utf-8 -*-
6
# Copyright (c) 2008 Magnun Leno da Silva
8
# Author: Magnun Leno da Silva <magnun.leno@gmail.com>
10
# This program is free software; you can redistribute it and/or
11
# modify it under the terms of the GNU Lesser General Public License
12
# as published by the Free Software Foundation; either version 2 of
13
# the License, or (at your option) any later version.
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
# GNU General Public License for more details.
20
# You should have received a copy of the GNU Lesser General Public
21
# License along with this program; if not, write to the Free Software
22
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25
# Contributor: Rodrigo Moreiro Araujo <alf.rodrigo@gmail.com>
30
NUMTYPES = (int, float, long)
31
LISTTYPES = (list, tuple)
32
STRTYPES = (str, unicode)
33
FILLING_TYPES = ['linear', 'solid', 'gradient']
34
DEFAULT_COLOR_FILLING = 'solid'
35
#TODO: Define default color list
36
DEFAULT_COLOR_LIST = None
40
Class that models the main data structure.
42
- a number type (int, float or long)
43
- a tuple, witch represents a point and can have 2 or 3 items (x,y,z)
44
- if a list is passed it will be converted to a tuple.
46
obs: In case a tuple is passed it will convert to tuple
48
def __init__(self, data=None, name=None, parent=None):
50
Starts main atributes from the Data class
51
@name - Name for each point;
52
@content - The real data, can be an int, float, long or tuple, which
53
represents a point (x,y) or (x,y,z);
54
@parent - A pointer that give the data access to it's parent.
57
>>> d = Data(name='empty'); print d
59
>>> d = Data((1,1),'point a'); print d
61
>>> d = Data((1,2,3),'point b'); print d
63
>>> d = Data([2,3],'point c'); print d
65
>>> d = Data(12, 'simple value'); print d
72
# Setting passed values
81
Name is a read/write property that controls the input of name.
82
- If passed an invalid value it cleans the name with None
85
>>> d = Data(13); d.name = 'name_test'; print d
87
>>> d.name = 11; print d
89
>>> d.name = 'other_name'; print d
91
>>> d.name = None; print d
93
>>> d.name = 'last_name'; print d
95
>>> d.name = ''; print d
100
returns the name as a string
104
def fset(self, name):
106
Sets the name of the Data
108
if type(name) in STRTYPES and len(name) > 0:
115
return property(**locals())
121
Content is a read/write property that validate the data passed
125
>>> d = Data(); d.content = 13; d.content
127
>>> d = Data(); d.content = (1,2); d.content
129
>>> d = Data(); d.content = (1,2,3); d.content
131
>>> d = Data(); d.content = [1,2,3]; d.content
133
>>> d = Data(); d.content = [1.5,.2,3.3]; d.content
134
(1.5, 0.20000000000000001, 3.2999999999999998)
138
Return the content of Data
140
return self.__content
142
def fset(self, data):
144
Ensures that data is a valid tuple/list or a number (int, float
149
self.__content = None
153
elif type(data) in NUMTYPES:
154
self.__content = data
156
# Type: List or Tuple
157
elif type(data) in LISTTYPES:
158
# Ensures the correct size
159
if len(data) not in (2, 3):
160
raise TypeError, "Data (as list/tuple) must have 2 or 3 items"
163
# Ensures that all items in list/tuple is a number
164
isnum = lambda x : type(x) not in NUMTYPES
166
if max(map(isnum, data)):
167
# An item in data isn't an int or a float
168
raise TypeError, "All content of data must be a number (int or float)"
170
# Convert the tuple to list
171
if type(data) is list:
174
# Append a copy and sets the type
175
self.__content = data[:]
179
self.__content = None
180
raise TypeError, "Data must be an int, float or a tuple with two or three items"
183
return property(**locals())
188
Clear the all Data (content, name and parent)
196
Returns a copy of the Data structure
200
if self.content is not None:
201
# If content is a point
202
if type(self.content) is tuple:
203
new_data.__content = self.content[:]
205
# If content is a number
207
new_data.__content = self.content
210
if self.name is not None:
211
new_data.__name = self.name
217
Return a string representation of the Data structure
219
if self.name is None:
220
if self.content is None:
222
return str(self.content)
224
if self.content is None:
225
return self.name+": ()"
226
return self.name+": "+str(self.content)
230
Return the length of the Data.
231
- If it's a number return 1;
232
- If it's a list return it's length;
233
- If its None return 0.
235
if self.content is None:
237
elif type(self.content) in NUMTYPES:
239
return len(self.content)
246
Class that models a group of data. Every value (int, float, long, tuple
247
or list) passed is converted to a list of Data.
249
- A single number (int, float, long);
251
- A tuple of numbers;
252
- An instance of Data;
255
Obs: If a tuple with 2 or 3 items is passed it is converted to a point.
256
If a tuple with only 1 item is passed it's converted to a number;
257
If a tuple with more than 2 items is passed it's converted to a
260
def __init__(self, group=None, name=None, parent=None):
262
Starts main atributes in Group instance.
263
@data_list - a list of data which forms the group;
264
@range - a range that represent the x axis of possible functions;
265
@name - name of the data group;
266
@parent - the Serie parent of this group.
269
>>> g = Group(13, 'simple number'); print g
271
>>> g = Group((1,2), 'simple point'); print g
272
simple point ['(1, 2)']
273
>>> g = Group([1,2,3,4], 'list of numbers'); print g
274
list of numbers ['1', '2', '3', '4']
275
>>> g = Group((1,2,3,4),'int in tuple'); print g
276
int in tuple ['1', '2', '3', '4']
277
>>> g = Group([(1,2),(2,3),(3,4)], 'list of points'); print g
278
list of points ['(1, 2)', '(2, 3)', '(3, 4)']
279
>>> g = Group([[1,2,3],[1,2,3]], '2D coordinate lists'); print g
280
2D coordinated lists ['(1, 1)', '(2, 2)', '(3, 3)']
281
>>> g = Group([[1,2],[1,2],[1,2]], '3D coordinate lists'); print g
282
3D coordinated lists ['(1, 1, 1)', '(2, 2, 2)']
285
self.__data_list = []
292
self.data_list = group
298
Name is a read/write property that controls the input of name.
299
- If passed an invalid value it cleans the name with None
302
>>> g = Group(13); g.name = 'name_test'; print g
304
>>> g.name = 11; print g
306
>>> g.name = 'other_name'; print g
308
>>> g.name = None; print g
310
>>> g.name = 'last_name'; print g
312
>>> g.name = ''; print g
317
Returns the name as a string
321
def fset(self, name):
323
Sets the name of the Group
325
if type(name) in STRTYPES and len(name) > 0:
330
return property(**locals())
336
The data_list is a read/write property that can be a list of
337
numbers, a list of points or a list of 2 or 3 coordinate lists. This
338
property uses mainly the self.add_data method.
341
>>> g = Group(); g.data_list = 13; print g
343
>>> g.data_list = (1,2); print g
345
>>> g.data_list = Data((1,2),'point a'); print g
347
>>> g.data_list = [1,2,3]; print g
349
>>> g.data_list = (1,2,3,4); print g
351
>>> g.data_list = [(1,2),(2,3),(3,4)]; print g
352
['(1, 2)', '(2, 3)', '(3, 4)']
353
>>> g.data_list = [[1,2],[1,2]]; print g
355
>>> g.data_list = [[1,2],[1,2],[1,2]]; print g
356
['(1, 1, 1)', '(2, 2, 2)']
357
>>> g.range = (10); g.data_list = lambda x:x**2; print g
358
['(0.0, 0.0)', '(1.0, 1.0)', '(2.0, 4.0)', '(3.0, 9.0)', '(4.0, 16.0)', '(5.0, 25.0)', '(6.0, 36.0)', '(7.0, 49.0)', '(8.0, 64.0)', '(9.0, 81.0)']
362
Returns the value of data_list
364
return self.__data_list
366
def fset(self, group):
368
Ensures that group is valid.
372
self.__data_list = []
374
# Int/float/long or Instance of Data
375
elif type(group) in NUMTYPES or isinstance(group, Data):
377
self.__data_list = []
381
elif type(group) is tuple and len(group) in (2,3):
382
self.__data_list = []
386
elif type(group) in LISTTYPES and type(group[0]) is not list:
388
self.__data_list = []
390
# try to append and catch an exception
394
elif callable(group):
395
# Explicit is better than implicit
398
if len(self.range) is not 0:
400
self.__data_list = []
401
# Generate values for the lambda function
403
#self.add_data((x,round(group(x),2)))
404
self.add_data((x,function(x)))
406
# Only have range in parent
407
elif self.parent is not None and len(self.parent.range) is not 0:
409
self.__range = self.parent.range[:]
411
self.__data_list = []
412
# Generate values for the lambda function
414
#self.add_data((x,round(group(x),2)))
415
self.add_data((x,function(x)))
417
# Don't have range anywhere
420
raise Exception, "Data argument is valid but to use function type please set x_range first"
423
elif type(group) in LISTTYPES and type(group[0]) is list:
425
self.__data_list = []
428
data = zip(group[0], group[1], group[2])
429
elif len(group) == 2:
430
data = zip(group[0], group[1])
432
raise TypeError, "Only one list of coordinates was received."
438
raise TypeError, "Group type not supported"
440
return property(**locals())
445
The range is a read/write property that generates a range of values
446
for the x axis of the functions. When passed a tuple it almost works
447
like the built-in range funtion:
448
- 1 item, represent the end of the range started from 0;
449
- 2 items, represents the start and the end, respectively;
450
- 3 items, the last one represents the step;
452
When passed a list the range function understands as a valid range.
455
>>> g = Group(); g.range = 10; print g.range
456
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
457
>>> g = Group(); g.range = (5); print g.range
458
[0.0, 1.0, 2.0, 3.0, 4.0]
459
>>> g = Group(); g.range = (1,7); print g.range
460
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
461
>>> g = Group(); g.range = (0,10,2); print g.range
462
[0.0, 2.0, 4.0, 6.0, 8.0]
464
>>> g = Group(); g.range = [0]; print g.range
466
>>> g = Group(); g.range = [0,10,20]; print g.range
475
def fset(self, x_range):
477
Controls the input of a valid type and generate the range
479
# if passed a simple number convert to tuple
480
if type(x_range) in NUMTYPES:
483
# A list, just convert to float
484
if type(x_range) is list and len(x_range) > 0:
485
# Convert all to float
486
x_range = map(float, x_range)
487
# Prevents repeated values and convert back to list
488
self.__range = list(set(x_range[:]))
489
# Sort the list to ascending order
492
# A tuple, must check the lengths and generate the values
493
elif type(x_range) is tuple and len(x_range) in (1,2,3):
494
# Convert all to float
495
x_range = map(float, x_range)
502
# Only the end and it can't be less or iqual to 0
503
if len(x_range) is 1 and x_range > 0:
506
# The start and the end but the start must be less then the end
507
elif len(x_range) is 2 and x_range[0] < x_range[1]:
511
# All 3, but the start must be less then the end
512
elif x_range[0] <= x_range[1]:
520
# Can't use the range function because it doesn't support float values
522
self.__range.append(start)
527
raise Exception, "x_range must be a list with one or more items or a tuple with 2 or 3 items"
529
return property(**locals())
531
def add_data(self, data, name=None):
533
Append a new data to the data_list.
534
- If data is an instance of Data, append it
535
- If it's an int, float, tuple or list create an instance of Data and append it
539
>>> g.add_data(12); print g
541
>>> g.add_data(7,'other'); print g
545
>>> g.add_data((1,1),'a'); print g
547
>>> g.add_data((2,2),'b'); print g
548
['a: (1, 1)', 'b: (2, 2)']
550
>>> g.add_data(Data((1,2),'c')); print g
551
['a: (1, 1)', 'b: (2, 2)', 'c: (1, 2)']
553
if not isinstance(data, Data):
555
data = Data(data,name,self)
557
if data.content is not None:
558
self.__data_list.append(data.copy())
559
self.__data_list[-1].parent = self
564
Returns the group as a list of numbers (int, float or long) or a
565
list of tuples (points 2D or 3D).
568
>>> g = Group([1,2,3,4],'g1'); g.to_list()
570
>>> g = Group([(1,2),(2,3),(3,4)],'g2'); g.to_list()
571
[(1, 2), (2, 3), (3, 4)]
572
>>> g = Group([(1,2,3),(3,4,5)],'g2'); g.to_list()
573
[(1, 2, 3), (3, 4, 5)]
575
return [data.content for data in self]
579
Returns a copy of this group
582
new_group.__name = self.__name
583
if self.__range is not None:
584
new_group.__range = self.__range[:]
586
new_group.add_data(data.copy())
591
Return a list with the names of all data in this group
595
if data.name is None:
596
names.append('Data '+str(data.index()+1))
598
names.append(data.name)
604
Returns a string representing the Group
607
if self.name is not None:
608
ret += self.name + " "
610
list_str = [str(item) for item in self]
616
def __getitem__(self, key):
618
Makes a Group iterable, based in the data_list property
620
return self.data_list[key]
624
Returns the length of the Group, based in the data_list property
626
return len(self.data_list)
629
class Colors(object):
631
Class that models the colors its labels (names) and its properties, RGB
635
- A list where each item is a list with 3 or 4 items. The
636
first 3 items represent the RGB values and the last argument
637
defines the filling type. The list will be converted to a dict
638
and each color will receve a name based in its position in the
640
- A dictionary where each key will be the color name and its item
641
can be a list with 3 or 4 items. The first 3 items represent
642
the RGB colors and the last argument defines the filling type.
644
def __init__(self, color_list=None):
646
Start the color_list property
647
@ color_list - the list or dict contaning the colors properties.
649
self.__color_list = None
651
self.color_list = color_list
656
>>> c = Colors([[1,1,1],[2,2,2,'linear'],[3,3,3,'gradient']])
657
>>> print c.color_list
658
{'Color 2': [2, 2, 2, 'linear'], 'Color 3': [3, 3, 3, 'gradient'], 'Color 1': [1, 1, 1, 'solid']}
659
>>> c.color_list = [[1,1,1],(2,2,2,'solid'),(3,3,3,'linear')]
660
>>> print c.color_list
661
{'Color 2': [2, 2, 2, 'solid'], 'Color 3': [3, 3, 3, 'linear'], 'Color 1': [1, 1, 1, 'solid']}
662
>>> c.color_list = {'a':[1,1,1],'b':(2,2,2,'solid'),'c':(3,3,3,'linear'), 'd':(4,4,4)}
663
>>> print c.color_list
664
{'a': [1, 1, 1, 'solid'], 'c': [3, 3, 3, 'linear'], 'b': [2, 2, 2, 'solid'], 'd': [4, 4, 4, 'solid']}
668
Return the color list
670
return self.__color_list
672
def fset(self, color_list):
674
Format the color list to a dictionary
676
if color_list is None:
677
self.__color_list = None
680
if type(color_list) in LISTTYPES and type(color_list[0]) in LISTTYPES:
681
old_color_list = color_list[:]
683
for index, color in enumerate(old_color_list):
684
if len(color) is 3 and max(map(type, color)) in NUMTYPES:
685
color_list['Color '+str(index+1)] = list(color)+[DEFAULT_COLOR_FILLING]
686
elif len(color) is 4 and max(map(type, color[:-1])) in NUMTYPES and color[-1] in FILLING_TYPES:
687
color_list['Color '+str(index+1)] = list(color)
689
raise TypeError, "Unsuported color format"
690
elif type(color_list) is not dict:
691
raise TypeError, "Unsuported color format"
693
for name, color in color_list.items():
695
if max(map(type, color)) in NUMTYPES:
696
color_list[name] = list(color)+[DEFAULT_COLOR_FILLING]
698
raise TypeError, "Unsuported color format"
699
elif len(color) is 4:
700
if max(map(type, color[:-1])) in NUMTYPES and color[-1] in FILLING_TYPES:
701
color_list[name] = list(color)
703
raise TypeError, "Unsuported color format"
704
self.__color_list = color_list.copy()
706
return property(**locals())
709
class Series(object):
711
Class that models a Series (group of groups). Every value (int, float,
712
long, tuple or list) passed is converted to a list of Group or Data.
714
- a single number or point, will be converted to a Group of one Data;
715
- a list of numbers, will be converted to a group of numbers;
716
- a list of tuples, will converted to a single Group of points;
717
- a list of lists of numbers, each 'sublist' will be converted to a
719
- a list of lists of tuples, each 'sublist' will be converted to a
721
- a list of lists of lists, the content of the 'sublist' will be
722
processed as coordinated lists and the result will be converted to
724
- a Dictionary where each item can be the same of the list: number,
725
point, list of numbers, list of points or list of lists (coordinated
727
- an instance of Data;
728
- an instance of group.
730
def __init__(self, series=None, name=None, property=[], colors=None):
732
Starts main atributes in Group instance.
733
@series - a list, dict of data of which the series is composed;
734
@name - name of the series;
735
@property - a list/dict of properties to be used in the plots of
739
>>> print Series([1,2,3,4])
740
["Group 1 ['1', '2', '3', '4']"]
741
>>> print Series([[1,2,3],[4,5,6]])
742
["Group 1 ['1', '2', '3']", "Group 2 ['4', '5', '6']"]
743
>>> print Series((1,2))
744
["Group 1 ['(1, 2)']"]
745
>>> print Series([(1,2),(2,3)])
746
["Group 1 ['(1, 2)', '(2, 3)']"]
747
>>> print Series([[(1,2),(2,3)],[(4,5),(5,6)]])
748
["Group 1 ['(1, 2)', '(2, 3)']", "Group 2 ['(4, 5)', '(5, 6)']"]
749
>>> print Series([[[1,2,3],[1,2,3],[1,2,3]]])
750
["Group 1 ['(1, 1, 1)', '(2, 2, 2)', '(3, 3, 3)']"]
751
>>> print Series({'g1':[1,2,3], 'g2':[4,5,6]})
752
["g1 ['1', '2', '3']", "g2 ['4', '5', '6']"]
753
>>> print Series({'g1':[(1,2),(2,3)], 'g2':[(4,5),(5,6)]})
754
["g1 ['(1, 2)', '(2, 3)']", "g2 ['(4, 5)', '(5, 6)']"]
755
>>> print Series({'g1':[[1,2],[1,2]], 'g2':[[4,5],[4,5]]})
756
["g1 ['(1, 1)', '(2, 2)']", "g2 ['(4, 4)', '(5, 5)']"]
757
>>> print Series(Data(1,'d1'))
758
["Group 1 ['d1: 1']"]
759
>>> print Series(Group([(1,2),(2,3)],'g1'))
760
["g1 ['(1, 2)', '(2, 3)']"]
763
self.__group_list = []
767
# TODO: Implement colors with filling
771
self.group_list = series
778
Name is a read/write property that controls the input of name.
779
- If passed an invalid value it cleans the name with None
782
>>> s = Series(13); s.name = 'name_test'; print s
783
name_test ["Group 1 ['13']"]
784
>>> s.name = 11; print s
786
>>> s.name = 'other_name'; print s
787
other_name ["Group 1 ['13']"]
788
>>> s.name = None; print s
790
>>> s.name = 'last_name'; print s
791
last_name ["Group 1 ['13']"]
792
>>> s.name = ''; print s
797
Returns the name as a string
801
def fset(self, name):
803
Sets the name of the Group
805
if type(name) in STRTYPES and len(name) > 0:
810
return property(**locals())
819
>>> s.colors = [[1,1,1],[2,2,2,'linear'],[3,3,3,'gradient']]
821
{'Color 2': [2, 2, 2, 'linear'], 'Color 3': [3, 3, 3, 'gradient'], 'Color 1': [1, 1, 1, 'solid']}
822
>>> s.colors = [[1,1,1],(2,2,2,'solid'),(3,3,3,'linear')]
824
{'Color 2': [2, 2, 2, 'solid'], 'Color 3': [3, 3, 3, 'linear'], 'Color 1': [1, 1, 1, 'solid']}
825
>>> s.colors = {'a':[1,1,1],'b':(2,2,2,'solid'),'c':(3,3,3,'linear'), 'd':(4,4,4)}
827
{'a': [1, 1, 1, 'solid'], 'c': [3, 3, 3, 'linear'], 'b': [2, 2, 2, 'solid'], 'd': [4, 4, 4, 'solid']}
831
Return the color list
833
return self.__colors.color_list
835
def fset(self, colors):
837
Format the color list to a dictionary
839
self.__colors = Colors(colors)
841
return property(**locals())
846
The range is a read/write property that generates a range of values
847
for the x axis of the functions. When passed a tuple it almost works
848
like the built-in range funtion:
849
- 1 item, represent the end of the range started from 0;
850
- 2 items, represents the start and the end, respectively;
851
- 3 items, the last one represents the step;
853
When passed a list the range function understands as a valid range.
856
>>> s = Series(); s.range = 10; print s.range
857
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
858
>>> s = Series(); s.range = (5); print s.range
859
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
860
>>> s = Series(); s.range = (1,7); print s.range
861
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
862
>>> s = Series(); s.range = (0,10,2); print s.range
863
[0.0, 2.0, 4.0, 6.0, 8.0, 10.0]
865
>>> s = Series(); s.range = [0]; print s.range
867
>>> s = Series(); s.range = [0,10,20]; print s.range
876
def fset(self, x_range):
878
Controls the input of a valid type and generate the range
880
# if passed a simple number convert to tuple
881
if type(x_range) in NUMTYPES:
884
# A list, just convert to float
885
if type(x_range) is list and len(x_range) > 0:
886
# Convert all to float
887
x_range = map(float, x_range)
888
# Prevents repeated values and convert back to list
889
self.__range = list(set(x_range[:]))
890
# Sort the list to ascending order
893
# A tuple, must check the lengths and generate the values
894
elif type(x_range) is tuple and len(x_range) in (1,2,3):
895
# Convert all to float
896
x_range = map(float, x_range)
903
# Only the end and it can't be less or iqual to 0
904
if len(x_range) is 1 and x_range > 0:
907
# The start and the end but the start must be lesser then the end
908
elif len(x_range) is 2 and x_range[0] < x_range[1]:
912
# All 3, but the start must be lesser then the end
913
elif x_range[0] < x_range[1]:
921
# Cnat use the range function becouse it don't suport float values
923
self.__range.append(start)
928
raise Exception, "x_range must be a list with one or more item or a tuple with 2 or 3 items"
930
return property(**locals())
935
The group_list is a read/write property used to pre-process the list
938
- a single number, point or lambda, will be converted to a single
940
- a list of numbers, will be converted to a group of numbers;
941
- a list of tuples, will converted to a single Group of points;
942
- a list of lists of numbers, each 'sublist' will be converted to
944
- a list of lists of tuples, each 'sublist' will be converted to a
946
- a list of lists of lists, the content of the 'sublist' will be
947
processed as coordinated lists and the result will be converted
948
to a group of points;
949
- a list of lambdas, each lambda represents a Group;
950
- a Dictionary where each item can be the same of the list: number,
951
point, list of numbers, list of points, list of lists
952
(coordinated lists) or lambdas
953
- an instance of Data;
954
- an instance of group.
958
>>> s.group_list = [1,2,3,4]; print s
959
["Group 1 ['1', '2', '3', '4']"]
960
>>> s.group_list = [[1,2,3],[4,5,6]]; print s
961
["Group 1 ['1', '2', '3']", "Group 2 ['4', '5', '6']"]
962
>>> s.group_list = (1,2); print s
963
["Group 1 ['(1, 2)']"]
964
>>> s.group_list = [(1,2),(2,3)]; print s
965
["Group 1 ['(1, 2)', '(2, 3)']"]
966
>>> s.group_list = [[(1,2),(2,3)],[(4,5),(5,6)]]; print s
967
["Group 1 ['(1, 2)', '(2, 3)']", "Group 2 ['(4, 5)', '(5, 6)']"]
968
>>> s.group_list = [[[1,2,3],[1,2,3],[1,2,3]]]; print s
969
["Group 1 ['(1, 1, 1)', '(2, 2, 2)', '(3, 3, 3)']"]
970
>>> s.group_list = [(0.5,5.5) , [(0,4),(6,8)] , (5.5,7) , (7,9)]; print s
971
["Group 1 ['(0.5, 5.5)']", "Group 2 ['(0, 4)', '(6, 8)']", "Group 3 ['(5.5, 7)']", "Group 4 ['(7, 9)']"]
972
>>> s.group_list = {'g1':[1,2,3], 'g2':[4,5,6]}; print s
973
["g1 ['1', '2', '3']", "g2 ['4', '5', '6']"]
974
>>> s.group_list = {'g1':[(1,2),(2,3)], 'g2':[(4,5),(5,6)]}; print s
975
["g1 ['(1, 2)', '(2, 3)']", "g2 ['(4, 5)', '(5, 6)']"]
976
>>> s.group_list = {'g1':[[1,2],[1,2]], 'g2':[[4,5],[4,5]]}; print s
977
["g1 ['(1, 1)', '(2, 2)']", "g2 ['(4, 4)', '(5, 5)']"]
979
>>> s.group_list = lambda x:x*2
980
>>> s.group_list = [lambda x:x*2, lambda x:x**2, lambda x:x**3]; print s
981
["Group 1 ['(0.0, 0.0)', '(1.0, 2.0)', '(2.0, 4.0)', '(3.0, 6.0)', '(4.0, 8.0)', '(5.0, 10.0)', '(6.0, 12.0)', '(7.0, 14.0)', '(8.0, 16.0)', '(9.0, 18.0)', '(10.0, 20.0)']", "Group 2 ['(0.0, 0.0)', '(1.0, 1.0)', '(2.0, 4.0)', '(3.0, 9.0)', '(4.0, 16.0)', '(5.0, 25.0)', '(6.0, 36.0)', '(7.0, 49.0)', '(8.0, 64.0)', '(9.0, 81.0)', '(10.0, 100.0)']", "Group 3 ['(0.0, 0.0)', '(1.0, 1.0)', '(2.0, 8.0)', '(3.0, 27.0)', '(4.0, 64.0)', '(5.0, 125.0)', '(6.0, 216.0)', '(7.0, 343.0)', '(8.0, 512.0)', '(9.0, 729.0)', '(10.0, 1000.0)']"]
982
>>> s.group_list = {'linear':lambda x:x*2, 'square':lambda x:x**2, 'cubic':lambda x:x**3}; print s
983
["cubic ['(0.0, 0.0)', '(1.0, 1.0)', '(2.0, 8.0)', '(3.0, 27.0)', '(4.0, 64.0)', '(5.0, 125.0)', '(6.0, 216.0)', '(7.0, 343.0)', '(8.0, 512.0)', '(9.0, 729.0)', '(10.0, 1000.0)']", "linear ['(0.0, 0.0)', '(1.0, 2.0)', '(2.0, 4.0)', '(3.0, 6.0)', '(4.0, 8.0)', '(5.0, 10.0)', '(6.0, 12.0)', '(7.0, 14.0)', '(8.0, 16.0)', '(9.0, 18.0)', '(10.0, 20.0)']", "square ['(0.0, 0.0)', '(1.0, 1.0)', '(2.0, 4.0)', '(3.0, 9.0)', '(4.0, 16.0)', '(5.0, 25.0)', '(6.0, 36.0)', '(7.0, 49.0)', '(8.0, 64.0)', '(9.0, 81.0)', '(10.0, 100.0)']"]
984
>>> s.group_list = Data(1,'d1'); print s
985
["Group 1 ['d1: 1']"]
986
>>> s.group_list = Group([(1,2),(2,3)],'g1'); print s
987
["g1 ['(1, 2)', '(2, 3)']"]
991
Return the group list.
993
return self.__group_list
995
def fset(self, series):
997
Controls the input of a valid group list.
999
#TODO: Add support to the following strem of data: [ (0.5,5.5) , [(0,4),(6,8)] , (5.5,7) , (7,9)]
1003
self.__group_list = []
1006
elif type(series) in LISTTYPES:
1007
self.__group_list = []
1009
is_function = lambda x: callable(x)
1011
if list in map(type, series) or max(map(is_function, series)):
1012
for group in series:
1013
self.add_group(group)
1017
self.add_group(series)
1021
#if type(series[0]) in NUMTYPES or type(series[0]) is tuple:
1023
# self.add_group(series)
1025
## List of anything else
1027
# for group in series:
1028
# self.add_group(group)
1030
# Dict representing series of groups
1031
elif type(series) is dict:
1032
self.__group_list = []
1033
names = series.keys()
1036
self.add_group(Group(series[name],name,self))
1039
elif callable(series):
1040
self.__group_list = []
1041
self.add_group(series)
1043
# Int/float, instance of Group or Data
1044
elif type(series) in NUMTYPES or isinstance(series, Group) or isinstance(series, Data):
1045
self.__group_list = []
1046
self.add_group(series)
1050
raise TypeError, "Serie type not supported"
1052
return property(**locals())
1054
def add_group(self, group, name=None):
1056
Append a new group in group_list
1058
if not isinstance(group, Group):
1060
group = Group(group, name, self)
1062
if len(group.data_list) is not 0:
1063
# Auto naming groups
1064
if group.name is None:
1065
group.name = "Group "+str(len(self.__group_list)+1)
1067
self.__group_list.append(group)
1068
self.__group_list[-1].parent = self
1072
Returns a copy of the Series
1074
new_series = Series()
1075
new_series.__name = self.__name
1076
if self.__range is not None:
1077
new_series.__range = self.__range[:]
1078
#Add color property in the copy method
1079
#self.__colors = None
1082
new_series.add_group(group.copy())
1086
def get_names(self):
1088
Returns a list of the names of all groups in the Serie
1092
if group.name is None:
1093
names.append('Group '+str(group.index()+1))
1095
names.append(group.name)
1101
Returns a list with the content of all groups and data
1106
if type(data.content) in NUMTYPES:
1107
big_list.append(data.content)
1109
big_list = big_list + list(data.content)
1112
def __getitem__(self, key):
1114
Makes the Series iterable, based in the group_list property
1116
return self.__group_list[key]
1120
Returns a string that represents the Series
1123
if self.name is not None:
1124
ret += self.name + " "
1126
list_str = [str(item) for item in self]
1127
ret += str(list_str)
1134
Returns the length of the Series, based in the group_lsit property
1136
return len(self.group_list)
1139
if __name__ == '__main__':