1 | # class Loader to load any king of file |
---|

2 | #import wx |
---|

3 | #import string |
---|

4 | import numpy |
---|

5 | |
---|

6 | class Load: |
---|

7 | """ |
---|

8 | This class is loading values from given file or value giving by the user |
---|

9 | """ |
---|

10 | def __init__(self, x=None, y=None, dx=None, dy=None): |
---|

11 | raise NotImplementedError("a code search shows that this code is not active, and you are not seeing this message") |
---|

12 | # variable to store loaded values |
---|

13 | self.x = x |
---|

14 | self.y = y |
---|

15 | self.dx = dx |
---|

16 | self.dy = dy |
---|

17 | self.filename = None |
---|

18 | |
---|

19 | def set_filename(self, path=None): |
---|

20 | """ |
---|

21 | Store path into a variable.If the user doesn't give |
---|

22 | a path as a parameter a pop-up |
---|

23 | window appears to select the file. |
---|

24 | |
---|

25 | :param path: the path given by the user |
---|

26 | |
---|

27 | """ |
---|

28 | self.filename = path |
---|

29 | |
---|

30 | def get_filename(self): |
---|

31 | """ return the file's path""" |
---|

32 | return self.filename |
---|

33 | |
---|

34 | def set_values(self): |
---|

35 | """ Store the values loaded from file in local variables""" |
---|

36 | if not self.filename == None: |
---|

37 | input_f = open(self.filename, 'r') |
---|

38 | buff = input_f.read() |
---|

39 | lines = buff.split('\n') |
---|

40 | self.x = [] |
---|

41 | self.y = [] |
---|

42 | self.dx = [] |
---|

43 | self.dy = [] |
---|

44 | for line in lines: |
---|

45 | try: |
---|

46 | toks = line.split() |
---|

47 | x = float(toks[0]) |
---|

48 | y = float(toks[1]) |
---|

49 | dy = float(toks[2]) |
---|

50 | |
---|

51 | self.x.append(x) |
---|

52 | self.y.append(y) |
---|

53 | self.dy.append(dy) |
---|

54 | self.dx = numpy.zeros(len(self.x)) |
---|

55 | except: |
---|

56 | print "READ ERROR", line |
---|

57 | # Sanity check |
---|

58 | if not len(self.x) == len(self.dx): |
---|

59 | raise ValueError, "x and dx have different length" |
---|

60 | if not len(self.y) == len(self.dy): |
---|

61 | raise ValueError, "y and dy have different length" |
---|

62 | |
---|

63 | |
---|

64 | def get_values(self): |
---|

65 | """ Return x, y, dx, dy""" |
---|

66 | return self.x, self.y, self.dx, self.dy |
---|

67 | |
---|

68 | def load_data(self, data): |
---|

69 | """ Return plottable""" |
---|

70 | #load data |
---|

71 | data.x = self.x |
---|

72 | data.y = self.y |
---|

73 | data.dx = self.dx |
---|

74 | data.dy = self.dy |
---|

75 | #Load its View class |
---|

76 | #plottable.reset_view() |
---|

77 | |
---|

78 | |
---|

79 | if __name__ == "__main__": |
---|

80 | load = Load() |
---|

81 | load.set_filename("testdata_line.txt") |
---|

82 | print load.get_filename() |
---|

83 | load.set_values() |
---|

84 | print load.get_values() |
---|

85 | |
---|

86 | |
---|