The wisdomBank connects strings to their NumExpr objects, so if the same expression pattern is called, it will be retrieved from the bank. Also this permits serialization via pickle.
| 1433 | |
| 1434 | |
| 1435 | class _WisdomBankSingleton(dict): |
| 1436 | ''' |
| 1437 | The wisdomBank connects strings to their NumExpr objects, so if the same |
| 1438 | expression pattern is called, it will be retrieved from the bank. |
| 1439 | Also this permits serialization via pickle. |
| 1440 | ''' |
| 1441 | |
| 1442 | def __init__(self, wisdomFile: str='', maxEntries: int=256): |
| 1443 | # Call super |
| 1444 | super(_WisdomBankSingleton, self).__init__(self) |
| 1445 | # attribute dictionary breaks a lot of things in the intepreter |
| 1446 | # dict.__init__(self) |
| 1447 | self.__wisdomFile = wisdomFile |
| 1448 | self.maxEntries = maxEntries |
| 1449 | pass |
| 1450 | |
| 1451 | @property |
| 1452 | def wisdomFile(self) -> str: |
| 1453 | if not bool(self.__wisdomFile): |
| 1454 | if not os.access('ne3_wisdom.pkl', os.W_OK): |
| 1455 | raise OSError('insufficient permissions to write to {}'.format('ne3_wisdom.pkl')) |
| 1456 | self.__wisdomFile = 'ne3_wisdom.pkl' |
| 1457 | return self.__wisdomFile |
| 1458 | |
| 1459 | @wisdomFile.setter |
| 1460 | def wisdomFile(self, newName: str) -> None: |
| 1461 | '''Check to see if the user has write permisions on the file.''' |
| 1462 | dirName = os.path.dirname(newName) |
| 1463 | if not os.access(dirName, os.W_OK): |
| 1464 | raise OSError('do not have write perimission for directory {}'.format(dirName)) |
| 1465 | self.__wisdomFile = newName |
| 1466 | |
| 1467 | def __setitem__(self, key, value): |
| 1468 | # Protection against growing the cache too much |
| 1469 | if len(self) > self.maxEntries: |
| 1470 | # Remove a 10% of random elements from the cache |
| 1471 | entries_to_remove = self.maxEntries // 10 |
| 1472 | |
| 1473 | keysView = list(self.keys()) |
| 1474 | for I, cull in enumerate(keysView): |
| 1475 | self.pop(cull) |
| 1476 | if I >= entries_to_remove: |
| 1477 | break |
| 1478 | |
| 1479 | super(_WisdomBankSingleton, self).__setitem__(key, value) |
| 1480 | |
| 1481 | |
| 1482 | def load(self, wisdomFile: Optional[str]=None) -> None: |
| 1483 | ''' |
| 1484 | Load the wisdom from a file on disk (or otherwise file-like object). |
| 1485 | |
| 1486 | wisdomFile should support the :code:`io.IOBase` or similar interface. |
| 1487 | ''' |
| 1488 | if wisdomFile == None: |
| 1489 | wisdomFile = self.wisdomFile |
| 1490 | |
| 1491 | with open(wisdomFile, 'rb') as fh: |
| 1492 | self = pickle.load(fh) |
no outgoing calls
no test coverage detected
searching dependent graphs…