Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -*- coding: utf-8 -*- 

2""" 

3@file 

4@brief Creates custom classes to interpret Python expression as column operations. 

5""" 

6 

7 

8class ColumnOperator: 

9 

10 """ 

11 defines an operation between two columns 

12 """ 

13 

14 def __init__(self): 

15 """ 

16 initiates the operator 

17 

18 @param name name of the column 

19 """ 

20 pass 

21 

22 def __str__(self): 

23 """ 

24 usual 

25 """ 

26 raise NotImplementedError() 

27 

28 def __call__(self, columns): 

29 """ 

30 returns the results of this operation between a list of columns 

31 """ 

32 raise NotImplementedError() 

33 

34 

35class OperatorId(ColumnOperator): 

36 

37 """ 

38 defines a constant 

39 """ 

40 

41 def __str__(self): 

42 """ 

43 usual 

44 """ 

45 return "=" 

46 

47 def __call__(self, columns): 

48 """ 

49 returns the results of this operation between a list of columns 

50 """ 

51 if len(columns) != 1: 

52 raise ValueError( 

53 "we expect a single value in a array here: {0}".format( 

54 str(columns))) 

55 if not isinstance(columns, tuple): 

56 raise TypeError("we expect a tuple here") 

57 for c in columns: 

58 c.IsColumnType() 

59 return columns[0]() 

60 

61 

62class OperatorMul(ColumnOperator): 

63 

64 """ 

65 defines the multiplication 

66 """ 

67 

68 def __str__(self): 

69 """ 

70 usual 

71 """ 

72 return "*" 

73 

74 def __call__(self, columns): 

75 """ 

76 returns the results of this operation between a list of columns 

77 """ 

78 if len(columns) == 0: 

79 raise ValueError( 

80 "we expect at least a value in a array here: {0}".format( 

81 str(columns))) 

82 if not isinstance(columns, tuple): 

83 raise TypeError("we expect a tuple here") 

84 for c in columns: 

85 c.IsColumnType() 

86 

87 r = columns[0]() 

88 for c in columns[1:]: 

89 r *= c() 

90 return r 

91 

92 

93class OperatorAdd(ColumnOperator): 

94 

95 """ 

96 defines the addition 

97 """ 

98 

99 def __str__(self): 

100 """ 

101 usual 

102 """ 

103 return "+" 

104 

105 def __call__(self, columns): 

106 """ 

107 returns the results of this operation between a list of columns 

108 """ 

109 if len(columns) == 0: 

110 raise ValueError( 

111 "we expect at least a value in a array here: {0}".format( 

112 str(columns))) 

113 if not isinstance(columns, tuple): 

114 raise TypeError("we expect a tuple here") 

115 for c in columns: 

116 c.IsColumnType() 

117 

118 r = columns[0]() 

119 for c in columns[1:]: 

120 r += c() 

121 return r 

122 

123 

124class OperatorDiv(ColumnOperator): 

125 

126 """ 

127 defines the division 

128 """ 

129 

130 def __str__(self): 

131 """ 

132 usual 

133 """ 

134 return "/" 

135 

136 def __call__(self, columns): 

137 """ 

138 returns the results of this operation between a list of columns 

139 """ 

140 if len(columns) != 2: 

141 raise ValueError( 

142 "we expect two values in a array here: {0}".format( 

143 str(columns))) 

144 if not isinstance(columns, tuple): 

145 raise TypeError("we expect a tuple here") 

146 for c in columns: 

147 c.IsColumnType() 

148 return columns[0]() / columns[1]() 

149 

150 

151class OperatorSub(ColumnOperator): 

152 

153 """ 

154 defines the subtraction 

155 """ 

156 

157 def __str__(self): 

158 """ 

159 usual 

160 """ 

161 return "-" 

162 

163 def __call__(self, columns): 

164 """ 

165 returns the results of this operation between a list of columns 

166 """ 

167 if len(columns) != 2: 

168 raise ValueError( 

169 "we expect two values in a array here: {0}".format( 

170 str(columns))) 

171 if not isinstance(columns, tuple): 

172 raise TypeError("we expect a tuple here") 

173 for c in columns: 

174 c.IsColumnType() 

175 return columns[0]() - columns[1]() 

176 

177 

178class OperatorPow(ColumnOperator): 

179 

180 """ 

181 defines the power 

182 """ 

183 

184 def __str__(self): 

185 """ 

186 usual 

187 """ 

188 return "**" 

189 

190 def __call__(self, columns): 

191 """ 

192 returns the results of this operation between a list of columns 

193 """ 

194 if len(columns) != 2: 

195 raise ValueError( 

196 "we expect two values in a array here: {0}".format( 

197 str(columns))) 

198 if not isinstance(columns, tuple): 

199 raise TypeError("we expect a tuple here") 

200 for c in columns: 

201 c.IsColumnType() 

202 

203 return columns[0]() ** columns[1]() 

204 

205 

206class OperatorMod(ColumnOperator): 

207 

208 """ 

209 defines the operator mod 

210 """ 

211 

212 def __str__(self): 

213 """ 

214 usual 

215 """ 

216 return "%" 

217 

218 def __call__(self, columns): 

219 """ 

220 returns the results of this operation between a list of columns 

221 """ 

222 if len(columns) != 2: 

223 raise ValueError( 

224 "we expect two values in a array here: {0}".format( 

225 str(columns))) 

226 if not isinstance(columns, tuple): 

227 raise TypeError("we expect a tuple here") 

228 for c in columns: 

229 c.IsColumnType() 

230 

231 return columns[0]() % columns[1]() 

232 

233 

234class OperatorDivN(ColumnOperator): 

235 

236 """ 

237 defines the division // 

238 """ 

239 

240 def __str__(self): 

241 """ 

242 usual 

243 """ 

244 return "//" 

245 

246 def __call__(self, columns): 

247 """ 

248 returns the results of this operation between a list of columns 

249 """ 

250 if len(columns) != 2: 

251 raise ValueError( 

252 "we expect two values in a array here: {0}".format( 

253 str(columns))) 

254 if not isinstance(columns, tuple): 

255 raise TypeError("we expect a tuple here") 

256 for c in columns: 

257 c.IsColumnType() 

258 

259 return columns[0]() // columns[1]() 

260 

261 

262class OperatorEq(ColumnOperator): 

263 

264 """ 

265 defines == 

266 """ 

267 

268 def __str__(self): 

269 """ 

270 usual 

271 """ 

272 return "==" 

273 

274 def __call__(self, columns): 

275 """ 

276 returns the results of this operation between a list of columns 

277 """ 

278 if len(columns) != 2: 

279 raise ValueError( 

280 "we expect two values in a array here: {0}".format( 

281 str(columns))) 

282 if not isinstance(columns, tuple): 

283 raise TypeError("we expect a tuple here") 

284 for c in columns: 

285 c.IsColumnType() 

286 return columns[0]() == columns[1]() 

287 

288 

289class OperatorNe(ColumnOperator): 

290 

291 """ 

292 defines != 

293 """ 

294 

295 def __str__(self): 

296 """ 

297 usual 

298 """ 

299 return "!=" 

300 

301 def __call__(self, columns): 

302 """ 

303 returns the results of this operation between a list of columns 

304 """ 

305 if len(columns) != 2: 

306 raise ValueError( 

307 "we expect two values in a array here: {0}".format( 

308 str(columns))) 

309 if not isinstance(columns, tuple): 

310 raise TypeError("we expect a tuple here") 

311 for c in columns: 

312 c.IsColumnType() 

313 return columns[0]() != columns[1]() 

314 

315 

316class OperatorLt(ColumnOperator): 

317 

318 """ 

319 defines < 

320 """ 

321 

322 def __str__(self): 

323 """ 

324 usual 

325 """ 

326 return "<" 

327 

328 def __call__(self, columns): 

329 """ 

330 returns the results of this operation between a list of columns 

331 """ 

332 if len(columns) != 2: 

333 raise ValueError( 

334 "we expect two values in a array here: {0}".format( 

335 str(columns))) 

336 if not isinstance(columns, tuple): 

337 raise TypeError("we expect a tuple here") 

338 for c in columns: 

339 c.IsColumnType() 

340 return columns[0]() < columns[1]() 

341 

342 

343class OperatorGt(ColumnOperator): 

344 

345 """ 

346 defines > 

347 """ 

348 

349 def __str__(self): 

350 """ 

351 usual 

352 """ 

353 return ">" 

354 

355 def __call__(self, columns): 

356 """ 

357 returns the results of this operation between a list of columns 

358 """ 

359 if len(columns) != 2: 

360 raise ValueError( 

361 "we expect two values in a array here: {0}".format( 

362 str(columns))) 

363 if not isinstance(columns, tuple): 

364 raise TypeError("we expect a tuple here") 

365 for c in columns: 

366 c.IsColumnType() 

367 return columns[0]() > columns[1]() 

368 

369 

370class OperatorLe(ColumnOperator): 

371 

372 """ 

373 defines <= 

374 """ 

375 

376 def __str__(self): 

377 """ 

378 usual 

379 """ 

380 return "<=" 

381 

382 def __call__(self, columns): 

383 """ 

384 returns the results of this operation between a list of columns 

385 """ 

386 if len(columns) != 2: 

387 raise ValueError( 

388 "we expect two values in a array here: {0}".format( 

389 str(columns))) 

390 if not isinstance(columns, tuple): 

391 raise TypeError("we expect a tuple here") 

392 for c in columns: 

393 c.IsColumnType() 

394 return columns[0]() <= columns[1]() 

395 

396 

397class OperatorGe(ColumnOperator): 

398 

399 """ 

400 defines >= 

401 """ 

402 

403 def __str__(self): 

404 """ 

405 usual 

406 """ 

407 return ">=" 

408 

409 def __call__(self, columns): 

410 """ 

411 returns the results of this operation between a list of columns 

412 """ 

413 if len(columns) != 2: 

414 raise ValueError( 

415 "we expect two values in a array here: {0}".format( 

416 str(columns))) 

417 if not isinstance(columns, tuple): 

418 raise TypeError("we expect a tuple here") 

419 for c in columns: 

420 c.IsColumnType() 

421 return columns[0]() >= columns[1]() 

422 

423 

424class OperatorOr(ColumnOperator): 

425 

426 """ 

427 defines ``or`` 

428 """ 

429 

430 def __str__(self): 

431 """ 

432 usual 

433 """ 

434 return "or" 

435 

436 def __call__(self, columns): 

437 """ 

438 returns the results of this operation between a list of columns 

439 """ 

440 if len(columns) != 2: 

441 raise ValueError( 

442 "we expect two values in a array here: {0}".format( 

443 str(columns))) 

444 if not isinstance(columns, tuple): 

445 raise TypeError("we expect a tuple here") 

446 for c in columns: 

447 c.IsColumnType() 

448 return columns[0]() or columns[1]() 

449 

450 

451class OperatorAnd(ColumnOperator): 

452 

453 """ 

454 defines ``and`` 

455 """ 

456 

457 def __str__(self): 

458 """ 

459 usual 

460 """ 

461 return "and" 

462 

463 def __call__(self, columns): 

464 """ 

465 returns the results of this operation between a list of columns 

466 """ 

467 if len(columns) != 2: 

468 raise ValueError( 

469 "we expect two values in a array here: {0}".format( 

470 str(columns))) 

471 if not isinstance(columns, tuple): 

472 raise TypeError("we expect a tuple here") 

473 for c in columns: 

474 c.IsColumnType() 

475 return columns[0]() and columns[1]() 

476 

477 

478class OperatorNot(ColumnOperator): 

479 

480 """ 

481 defines ``not`` 

482 """ 

483 

484 def __str__(self): 

485 """ 

486 usual 

487 """ 

488 return "not" 

489 

490 def __call__(self, columns): 

491 """ 

492 returns the results of this operation between a list of columns 

493 """ 

494 if len(columns) != 1: 

495 raise ValueError( 

496 "we expect one value in a array here: {0}".format( 

497 str(columns))) 

498 if not isinstance(columns, tuple): 

499 raise TypeError("we expect a tuple here") 

500 for c in columns: 

501 c.IsColumnType() 

502 return not columns[0]() 

503 

504 

505class OperatorFunc(ColumnOperator): 

506 

507 """ 

508 defines a function call 

509 """ 

510 

511 def __init__(self, func): 

512 """ 

513 constructor 

514 """ 

515 self._func = func 

516 

517 def __str__(self): 

518 """ 

519 usual 

520 """ 

521 return "func" 

522 

523 def __call__(self, columns): 

524 """ 

525 returns the results of this operation between a list of columns 

526 """ 

527 if not isinstance(columns, tuple): 

528 raise TypeError("we expect a tuple here") 

529 for c in columns: 

530 c.IsColumnType() 

531 return self._func(* [c() for c in columns])