All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
PythonDictConverter Class Reference
Inheritance diagram for PythonDictConverter:

Public Types

using key_t = std::string
 
using any_t = std::any
 

Public Member Functions

 PythonDictConverter ()
 
PyObjectresult () const
 

Private Member Functions

void enter_table (key_t const &key, any_t const &any)
 
void enter_sequence (key_t const &key, any_t const &any)
 
void atom (key_t const &key, any_t const &any)
 
void exit_table (key_t const &key, any_t const &any)
 
void exit_sequence (key_t const &key, any_t const &any)
 
void add_object (key_t const &key, PyObject *pyobj)
 

Private Attributes

std::vector< PyObject * > _stack
 

Detailed Description

Definition at line 115 of file fclmodule.cxx.

Member Typedef Documentation

using PythonDictConverter::any_t = std::any

Definition at line 120 of file fclmodule.cxx.

using PythonDictConverter::key_t = std::string

Definition at line 119 of file fclmodule.cxx.

Constructor & Destructor Documentation

PythonDictConverter::PythonDictConverter ( )

Definition at line 151 of file fclmodule.cxx.

155 {
156  // Initialize result stack with an empty python dictionary.
157  // This dictionary will represent the parameter set.
158 
159  _stack.emplace_back(PyDict_New());
160 }
std::vector< PyObject * > _stack
Definition: fclmodule.cxx:148

Member Function Documentation

void PythonDictConverter::add_object ( key_t const &  key,
PyObject pyobj 
)
private

Definition at line 335 of file fclmodule.cxx.

344 {
345  // Get the current parent object.
346 
347  if(_stack.size() == 0)
348  throw cet::exception("fclmodule") << "No parent object." << std::endl;
349  PyObject* parent = _stack.back();
350 
351  if(PyDict_Check(parent)) {
352 
353  // Insert object into dicionary.
354 
355  PyObject* keyobj = cxx_str_to_python(key);
356  PyDict_SetItem(parent, keyobj, pyobj);
357  Py_DECREF(pyobj);
358  }
359  else if(PyList_Check(parent)) {
360 
361  // Append object to list.
362 
363  PyList_Append(parent, pyobj);
364  Py_DECREF(pyobj);
365  }
366  else {
367 
368  // Oops.
369 
370  throw cet::exception("fclmodule") << "Parent object is not dictionary or list." << std::endl;
371  }
372 }
_object PyObject
Definition: PyUtils.h:4
std::vector< PyObject * > _stack
Definition: fclmodule.cxx:148
void PythonDictConverter::atom ( key_t const &  key,
any_t const &  any 
)
private

Definition at line 226 of file fclmodule.cxx.

233 {
234  PyObject* pyval = 0;
235 
236  // Extract atom as string.
237 
238  const std::string& atom = std::any_cast<const std::string&>(any);
239 
240  // Get lower case version of argument string.
241 
242  std::string lcatom(atom);
243  std::transform(lcatom.begin(), lcatom.end(), lcatom.begin(),
244  [](unsigned char c){return std::tolower(c);});
245 
246  // Check for boolean.
247 
248  if(lcatom == std::string("true") || lcatom == std::string("\"true\"")) {
249  pyval = Py_True;
250  Py_INCREF(pyval);
251  }
252  else if(lcatom == std::string("false") || lcatom == std::string("\"false\"")) {
253  pyval = Py_False;
254  Py_INCREF(pyval);
255  }
256 
257  // Check for quoted string.
258 
259  size_t n = atom.size();
260  if(pyval == 0 && n >= 2 && atom[0] == '"' && atom[n-1] == '"') {
261  std::string s = atom.substr(1, n-2);
262  pyval = cxx_str_to_python(s);
263  }
264 
265  // Check for int.
266 
267  if(pyval == 0) {
268  std::istringstream iss(atom);
269  long i;
270  iss >> std::noskipws >> i;
271  if(iss.eof() and !iss.fail())
272  pyval = PyLong_FromLong(i);
273  }
274 
275  // Check for float.
276 
277  if(pyval == 0) {
278  std::istringstream iss(atom);
279  double x;
280  iss >> std::noskipws >> x;
281  if(iss.eof() and !iss.fail())
282  pyval = PyFloat_FromDouble(x);
283  }
284 
285  // Last resort store a copy of the original string (unquoted string).
286 
287  if(pyval == 0)
288  pyval = cxx_str_to_python(atom);
289 
290  // Done converting atom to python.
291  // Add python object to parent container.
292 
293  add_object(key, pyval);
294 }
void atom(key_t const &key, any_t const &any)
Definition: fclmodule.cxx:226
static constexpr Sample_t transform(Sample_t sample)
_object PyObject
Definition: PyUtils.h:4
process_name opflash particleana ie x
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
void add_object(key_t const &key, PyObject *pyobj)
Definition: fclmodule.cxx:335
void PythonDictConverter::enter_sequence ( key_t const &  key,
any_t const &  any 
)
private

Definition at line 205 of file fclmodule.cxx.

212 {
213  // Make a new empty python list for this sequence.
214 
215  PyObject* seq = PyList_New(0);
216 
217  // Insert the list into the current parent container.
218 
219  add_object(key, seq);
220 
221  // Make the newly created python dictionary the current parent container.
222 
223  _stack.emplace_back(seq);
224 }
_object PyObject
Definition: PyUtils.h:4
std::vector< PyObject * > _stack
Definition: fclmodule.cxx:148
void add_object(key_t const &key, PyObject *pyobj)
Definition: fclmodule.cxx:335
void PythonDictConverter::enter_table ( key_t const &  key,
any_t const &  any 
)
private

Definition at line 184 of file fclmodule.cxx.

191 {
192  // Make a new empty python dictionary for this table.
193 
194  PyObject* dict = PyDict_New();
195 
196  // Insert this dictionary into the current parent container.
197 
198  add_object(key, dict);
199 
200  // Make the newly created python dictionary the current parent container.
201 
202  _stack.emplace_back(dict);
203 }
_object PyObject
Definition: PyUtils.h:4
std::vector< PyObject * > _stack
Definition: fclmodule.cxx:148
void add_object(key_t const &key, PyObject *pyobj)
Definition: fclmodule.cxx:335
void PythonDictConverter::exit_sequence ( key_t const &  key,
any_t const &  any 
)
private

Definition at line 314 of file fclmodule.cxx.

321 {
322  // Do consistency checks.
323 
324  if(_stack.size() < 2)
325  throw cet::exception("fclmodule") << "Result stack has wrong size: "
326  << _stack.size() << std::endl;
327  if(!PyList_Check(_stack.back()))
328  throw cet::exception("fclmodule") << "Result stack has wrong type." << std::endl;
329 
330  // Pop the current parent (this sequence) off the result stack.
331 
332  _stack.pop_back();
333 }
std::vector< PyObject * > _stack
Definition: fclmodule.cxx:148
void PythonDictConverter::exit_table ( key_t const &  key,
any_t const &  any 
)
private

Definition at line 296 of file fclmodule.cxx.

300 {
301  // Do consistency checks.
302 
303  if(_stack.size() < 2)
304  throw cet::exception("fclmodule") << "Result stack has wrong size: "
305  << _stack.size() << std::endl;
306  if(!PyDict_Check(_stack.back()))
307  throw cet::exception("fclmodule") << "Result stack has wrong type." << std::endl;
308 
309  // Pop the current parent (this table) off the result stack.
310 
311  _stack.pop_back();
312 }
std::vector< PyObject * > _stack
Definition: fclmodule.cxx:148
PyObject * PythonDictConverter::result ( ) const

Definition at line 162 of file fclmodule.cxx.

170 {
171  // Do consistency checks.
172 
173  if(_stack.size() != 1)
174  throw cet::exception("fclmodule") << "Result stack has wrong size: "
175  << _stack.size() << std::endl;
176  if(!PyDict_Check(_stack[0]))
177  throw cet::exception("fclmodule") << "Result stack has wrong type." << std::endl;
178 
179  // Everything OK.
180 
181  return _stack[0];
182 }
std::vector< PyObject * > _stack
Definition: fclmodule.cxx:148

Member Data Documentation

std::vector<PyObject*> PythonDictConverter::_stack
private

Definition at line 148 of file fclmodule.cxx.


The documentation for this class was generated from the following file: