Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | # ========== Copyright Header Begin ========================================== |
2 | # | |
3 | # OpenSPARC T2 Processor File: Pfe_Slicer.py | |
4 | # Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
5 | # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
6 | # | |
7 | # The above named program is free software; you can redistribute it and/or | |
8 | # modify it under the terms of the GNU General Public | |
9 | # License version 2 as published by the Free Software Foundation. | |
10 | # | |
11 | # The above named program is distributed in the hope that it will be | |
12 | # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | # General Public License for more details. | |
15 | # | |
16 | # You should have received a copy of the GNU General Public | |
17 | # License along with this work; if not, write to the Free Software | |
18 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 | # | |
20 | # ========== Copyright Header End ============================================ | |
21 | ||
22 | ||
23 | class Slicer: | |
24 | """ | |
25 | Slicer(list) takes a list as argument. When a Slicer | |
26 | object is subscribted it returns a sublist of the list. | |
27 | For example: | |
28 | ||
29 | s = Slicer(range(0,64)) | |
30 | s[0] => [0] | |
31 | s[0,2] => [0,2] | |
32 | s[0:2] => [0,1] | |
33 | s[0:2,4] => [0,1,4] | |
34 | s[0:4:2] => [0,2,4] | |
35 | s[0:2,4:6] => [0,1,4,5] | |
36 | ||
37 | """ | |
38 | def __init__(self,list): | |
39 | self.list = list | |
40 | ||
41 | def __getitem__(self,index): | |
42 | if type(index) == tuple: | |
43 | l = [] | |
44 | for i in index: | |
45 | if type(i) == slice: | |
46 | l.extend(self.list[i]) | |
47 | else: | |
48 | l.append(self.list[i]) | |
49 | return l | |
50 | elif type(index) == slice: | |
51 | return self.list[index] | |
52 | else: | |
53 | return [self.list[index]] | |
54 | ||
55 | ||
56 | if __name__ == '__main__': | |
57 | s=Slicer(range(0,64)) | |
58 | ||
59 | print s[0] | |
60 | print s[0:32] | |
61 | print s[0,8,16,56,64] | |
62 | print s[0:32:8] | |
63 | print s[0:8,8:16] | |
64 | print s[0:8,8:16,32,56:64] | |
65 | ||
66 | ||
67 | ||
68 |