polyconfig.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """Configuration utilities for polynomial manipulation algorithms. """
  2. from contextlib import contextmanager
  3. _default_config = {
  4. 'USE_COLLINS_RESULTANT': False,
  5. 'USE_SIMPLIFY_GCD': True,
  6. 'USE_HEU_GCD': True,
  7. 'USE_IRREDUCIBLE_IN_FACTOR': False,
  8. 'USE_CYCLOTOMIC_FACTOR': True,
  9. 'EEZ_RESTART_IF_NEEDED': True,
  10. 'EEZ_NUMBER_OF_CONFIGS': 3,
  11. 'EEZ_NUMBER_OF_TRIES': 5,
  12. 'EEZ_MODULUS_STEP': 2,
  13. 'GF_IRRED_METHOD': 'rabin',
  14. 'GF_FACTOR_METHOD': 'zassenhaus',
  15. 'GROEBNER': 'buchberger',
  16. }
  17. _current_config = {}
  18. @contextmanager
  19. def using(**kwargs):
  20. for k, v in kwargs.items():
  21. setup(k, v)
  22. yield
  23. for k in kwargs.keys():
  24. setup(k)
  25. def setup(key, value=None):
  26. """Assign a value to (or reset) a configuration item. """
  27. key = key.upper()
  28. if value is not None:
  29. _current_config[key] = value
  30. else:
  31. _current_config[key] = _default_config[key]
  32. def query(key):
  33. """Ask for a value of the given configuration item. """
  34. return _current_config.get(key.upper(), None)
  35. def configure():
  36. """Initialized configuration of polys module. """
  37. from os import getenv
  38. for key, default in _default_config.items():
  39. value = getenv('SYMPY_' + key)
  40. if value is not None:
  41. try:
  42. _current_config[key] = eval(value)
  43. except NameError:
  44. _current_config[key] = value
  45. else:
  46. _current_config[key] = default
  47. configure()