xboa
_line_factory_base.py
Go to the documentation of this file.
1 
2 # This file is a part of xboa
3 #
4 # xboa is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # xboa is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with xboa in the doc folder. If not, see
16 # <http://www.gnu.org/licenses/>.
17 
18 import xboa.common as common
19 import xboa.hit
20 from xboa.hit.factory import HitFactoryBase
21 
22 class LineFactoryBase(HitFactoryBase):
23  """
24  Base factory class for line by line reads
25 
26  LineFactoryBase defines the interface for line-by-line IO of a text file to
27  make hits. LineFactoryBase is a factory class i.e. it is used for generating
28  new hits from a file
29  """
30  def __init__(self):
31  """
32  Initialise the base class
33  """
34  super(LineFactoryBase, self).__init__()
35 
36  def make_hit(self):
37  """
38  Read a new hit
39  """
40  raise NotImplementedError("read_hit not implemented")
41 
42  @classmethod
43  def _read_formatted(cls, format_list, format_units_dict, file_handle, mass_shell_condition):
44  """
45  Read a line and parse according to some pre-defined format
46  """
47  try:
48  line = file_handle.next()
49  except StopIteration:
50  raise EOFError("End of file reached")
51  words = line.split()
52  words.reverse()
53  if not(len(words) == len(format_list)):
54  raise xboa.hit.BadEventError("Read operation failed with line "+str(line))
55  a_hit = xboa.hit.Hit()
56  for key in format_list:
57  value = words.pop()
58  if key != '':
59  try:
60  value = xboa.hit.Hit._default_var_types[key](value)
61  # I ran into problems with int > 1000000 written in scientific
62  # notation by G4BL (urk)
63  except ValueError:
64  value = float(value)
65  value = xboa.hit.Hit._default_var_types[key](value)
66  if xboa.hit.Hit._default_var_types[key] == float:
67  value *= common.units[format_units_dict[key]]
68  a_hit.set(key, value)
69  return a_hit
70 
The hit factory module defines a number of factory classes used for generating hit objects...
Definition: __init__.py:1
Base factory class for line by line reads.
common module defines common utility data and functions that are used elsewhere
Definition: __init__.py:1
Implemented within this module:
Definition: __init__.py:1
def _read_formatted
Read a line and parse according to some pre-defined format.