ATLAS Offline Software
Public Member Functions | List of all members
Py::Store Class Reference

#include <PyStore.h>

Collaboration diagram for Py::Store:

Public Member Functions

 Store ()
 Constructor with parameters: More...
 
 Store (const std::size_t bufSize)
 Constructor with parameters: size of buffers. More...
 
 ~Store ()
 Destructor: More...
 
const PyObjectpydict () const
 retrieve the underlying python dictionary More...
 
std::size_t bufferSize () const
 retrieve the current buffer size More...
 
std::string repr ()
 python representation More...
 
void setBufferSize (std::size_t bufferSize)
 reset the buffer size. More...
 
template<typename T >
void book (const std::string &key, const std::string &dataName)
 retrieve the underlying python dictionary More...
 
template<typename T >
void fill (const std::string &key, const std::string &dataName, const T &data)
 fill some already registered node with data More...
 
void clear_store ()
 clear buckets (but keeps registered/booked keys around) More...
 
std::size_t m_bufSize
 the buffer size of all buckets (ie: the pre-reserved length of all PyArrays) More...
 
PyObjectm_store
 data store: a python dictionary More...
 
void py_book (const std::string &key, const std::string &dataName, char dataType='d')
 python API More...
 
void py_fill (const std::string &key, const std::string &dataName, PyObject *data)
 
void book_impl (const std::string &key, const std::string &dataName, char dataType='d')
 register a variable with the store More...
 
void fill_impl (const std::string &key, const std::string &dataName, PyObject *data)
 fill some already registered node with data More...
 

Detailed Description

Definition at line 36 of file PyStore.h.

Constructor & Destructor Documentation

◆ Store() [1/2]

Py::Store::Store ( )

Constructor with parameters:

Definition at line 46 of file PyStore.cxx.

46  :
47  m_bufSize( defaultBufferSize ),
48  m_store ( PyDict_New() )
49 {}

◆ Store() [2/2]

Py::Store::Store ( const std::size_t  bufSize)

Constructor with parameters: size of buffers.

Definition at line 51 of file PyStore.cxx.

51  :
52  m_bufSize( bufSize ),
53  m_store ( PyDict_New() )
54 {}

◆ ~Store()

Py::Store::~Store ( )

Destructor:

Definition at line 58 of file PyStore.cxx.

59 {
60  Py_DECREF( m_store );
61 }

Member Function Documentation

◆ book()

template<typename T >
void Py::Store::book ( const std::string &  key,
const std::string &  dataName 
)
inline

retrieve the underlying python dictionary

register a variable with the store

Definition at line 79 of file PyStore.h.

81  {
82  //PyObject* buf = Py::cnv<T>::toPy( T() );
83  //PyObject* buf = PyList_New(0);
84  //PyObject* buf = Py::cnv<T>::to( T() );
85  this->book_impl( key, dataName, 'd' );
86  //Py_XDECREF(buf);
87  }

◆ book_impl()

void Py::Store::book_impl ( const std::string &  key,
const std::string &  dataName,
char  dataType = 'd' 
)
private

register a variable with the store

Definition at line 123 of file PyStore.cxx.

126 {
127  PyObject* buf = PyList_New( m_bufSize );
128  if ( !buf ) {
129  // error handling
130  std::cerr << "Store::book : Could not allocate a buffer for ["
131  << key << s_uri << dataName << "] !!\n";
132  Py_XDECREF(buf);
133  return;
134  }
135 
136  // resize to 0: >>> del buf[:]
137  if ( PyList_SetSlice( buf, 0, m_bufSize, (PyObject*)NULL ) != 0 ) {
138  // error handling
139  std::cerr << "Store::book : Could not reset allocated buffer ["
140  << key << s_uri << dataName << "] !!\n"
141  << " buf: " << Py::repr( buf ) << "\n";
142 
143  Py_DECREF(buf);
144  return;
145  }
146 
147  if ( PyDict_SetItemString( m_store,
148  const_cast<char*>((key+s_uri+dataName).c_str()),
149  buf ) != 0 ) {
150  // error handling
151  std::cerr << "Store::book : Could not create buffer at ["
152  << key << s_uri << dataName << "]\n"
153  << " buf: " << Py::repr( buf ) << "\n";
154  Py_DECREF(buf);
155  return;
156  }
157 }

◆ bufferSize()

std::size_t Py::Store::bufferSize ( ) const
inline

retrieve the current buffer size

Definition at line 61 of file PyStore.h.

61 { return m_bufSize; }

◆ clear_store()

void Py::Store::clear_store ( )

clear buckets (but keeps registered/booked keys around)

Definition at line 89 of file PyStore.cxx.

90 {
91  PyObject *buffers = PyDict_Values( m_store );
92  if ( !buffers ) {
93  // error handling
94  Py_XDECREF( buffers );
95  return;
96  }
97 
98  for ( int i = 0, iMax = PyList_GET_SIZE( buffers ); i < iMax; ++i ) {
99  PyObject* buf = PyList_GET_ITEM( buffers, i );
100  Py_INCREF( buf );
101 
102  const int len = PyList_GET_SIZE( buf );
103 
104  // delete the whole slice
105  if ( PyList_SetSlice( buf, 0, len, (PyObject *) NULL ) != 0 ) {
106  // error handling
107 
108  Py_DECREF( buf );
109  continue;
110  }
111 
112  Py_DECREF( buf );
113  }
114 
115  Py_DECREF( buffers );
116  return;
117 }

◆ fill()

template<typename T >
void Py::Store::fill ( const std::string &  key,
const std::string &  dataName,
const T &  data 
)
inline

fill some already registered node with data

Definition at line 91 of file PyStore.h.

94  {
95 // std::cerr << "PyStore::fill( " << key << ", " << dataName
96 // << ", " << typeid(data).name() << " => "
97 // << ClassName<T>::name() << "\n";
98  PyObject* pyData = Py::cnv<T>::toPy( data );
99  this->fill_impl( key, dataName, pyData );
100  // fill_impl steals pyData reference...
101  return;
102  }

◆ fill_impl()

void Py::Store::fill_impl ( const std::string &  key,
const std::string &  dataName,
PyObject data 
)
private

fill some already registered node with data

Definition at line 159 of file PyStore.cxx.

162 {
163  PyObject* buf =
164  PyDict_GetItemString( m_store,
165  const_cast<char*>((key+s_uri+dataName).c_str()) );
166  Py_XINCREF( buf );
167  if ( ! buf ) {
168  std::cerr << "Store::fill : Could not retrieve key ["
169  << key << s_uri << dataName << "] from store !!\n";
170 
171  // error handling
172  PyErr_Clear();
173  Py_DECREF ( data );
174  Py_XDECREF( buf );
175  return;
176  }
177 
178  if ( !PyList_Check( buf ) ) {
179  std::cerr << "Store::fill : buffer at ["
180  << key << s_uri << dataName << "] is NOT a python array !!\n"
181  << " buf: " << Py::repr( buf ) << "\n";
182 
183  // error handling
184  PyErr_Clear();
185  Py_DECREF( data );
186  Py_DECREF( buf );
187  return;
188  }
189 
190  if ( 0 != PyList_Append( buf, data ) ) {
191 
192  std::cerr << "Store::fill : could not fill buffer at ["
193  << key << s_uri << dataName << "] !!\n"
194  << " buf: " << Py::repr( buf ) << "\n"
195  << " data: " << Py::repr( data ) << "\n";
196 
197  // error handling
198  PyErr_Clear();
199  Py_DECREF( data );
200  Py_DECREF( buf );
201  return;
202  }
203 
204  Py_DECREF( data );
205  Py_DECREF( buf );
206  return;
207 }

◆ py_book()

void Py::Store::py_book ( const std::string &  key,
const std::string &  dataName,
char  dataType = 'd' 
)
inline

python API

Definition at line 108 of file PyStore.h.

111  { return this->book_impl( key, dataName, dataType ); }

◆ py_fill()

void Py::Store::py_fill ( const std::string &  key,
const std::string &  dataName,
PyObject data 
)
inline

Definition at line 113 of file PyStore.h.

116  { return this->fill_impl( key, dataName, data ); }

◆ pydict()

const PyObject* Py::Store::pydict ( ) const
inline

retrieve the underlying python dictionary

Definition at line 58 of file PyStore.h.

58 { Py_INCREF(m_store); return m_store; }

◆ repr()

std::string Py::Store::repr ( )

python representation

Definition at line 67 of file PyStore.cxx.

68 {
69  return Py::repr( m_store );
70 }

◆ setBufferSize()

void Py::Store::setBufferSize ( std::size_t  bufferSize)

reset the buffer size.

A no-op if new buffer size is same as old or smaller than old

Definition at line 76 of file PyStore.cxx.

77 {
78  if ( bufferSize <= m_bufSize ) {
80  return;
81  }
82 
84  // loop over buffers and extend their size
85 
86  return;
87 }

Member Data Documentation

◆ m_bufSize

std::size_t Py::Store::m_bufSize
private

the buffer size of all buckets (ie: the pre-reserved length of all PyArrays)

Definition at line 141 of file PyStore.h.

◆ m_store

PyObject* Py::Store::m_store
private

data store: a python dictionary

Definition at line 144 of file PyStore.h.


The documentation for this class was generated from the following files:
Py::Store::book_impl
void book_impl(const std::string &key, const std::string &dataName, char dataType='d')
register a variable with the store
Definition: PyStore.cxx:123
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
Py::Store::m_bufSize
std::size_t m_bufSize
the buffer size of all buckets (ie: the pre-reserved length of all PyArrays)
Definition: PyStore.h:141
Py::Store::fill_impl
void fill_impl(const std::string &key, const std::string &dataName, PyObject *data)
fill some already registered node with data
Definition: PyStore.cxx:159
Py::cnv
helper method to convert a type to a Py::Object
Definition: PyDataStore.h:28
downloadSingle.dataType
string dataType
Definition: downloadSingle.py:18
Py::Store::m_store
PyObject * m_store
data store: a python dictionary
Definition: PyStore.h:144
Py::Store::bufferSize
std::size_t bufferSize() const
retrieve the current buffer size
Definition: PyStore.h:61
lumiFormat.i
int i
Definition: lumiFormat.py:92
python.DecayParser.buf
buf
print ("=> [%s]"cmd)
Definition: DecayParser.py:27
Py::repr
std::string repr(PyObject *o)
Definition: PyStore.cxx:25
PyObject
_object PyObject
Definition: IPyComponent.h:26
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37