项目作者: alvarofpp

项目描述 :
Python abstract data structure (ADT) extension
高级语言: Python
项目地址: git://github.com/alvarofpp/python-adt-extension.git
创建时间: 2019-09-03T00:19:06Z
项目社区:https://github.com/alvarofpp/python-adt-extension

开源协议:

下载


adt-extension


latest release

Python abstract data structure (ADT) extension.

Install:

  1. pip install adt-extension

Import:

  1. from adt_extension import Set, SwitchDict

Extensions

Currently the package have the ADT extensions:

Class Extension of Description
Set set Set with element type and validation rule.
SwitchDict dict Dictionary with the possibility of behavior of a switch case.

Set

Set with element type and validation rule for the elements that will be inserted into the set.

Example:

  1. from adt_extension import Set
  2. # A set with only even integers
  3. set_int_even = Set(element_type=int, rule=lambda x: (x % 2 == 0))
  4. # Elements that satisfies the element type and validation rule
  5. set_int_even.update({4, 6})
  6. # Elements that satisfies the element type, but not validation rule
  7. set_int_even.update({5})
  8. # Elements that not satisfies the element type
  9. set_int_even.update({"qwe", True})
  10. print(set_int_even)
  11. # Output:
  12. # Set({4, 6})
  13. # Remove element type
  14. set_int_even.element_type = None
  15. # Remove validation rule
  16. set_int_even.rule = None

SwitchDict

Dictionary with the possibility to perform a function or return a value when trying to access a nonexistent index in the dictionary class.

Example:

  1. from adt_extension import SwitchDict
  2. # Same behavior of a normal dictionary
  3. switch_dict = SwitchDict({
  4. 'Apartament': 125,
  5. 'House': 250,
  6. 'Condominium': 300,
  7. })
  8. # Add default case
  9. switch_dict.default_case = 999
  10. # List example
  11. properties_list = [
  12. 'Apartament',
  13. 'House',
  14. 'Condominium',
  15. 'Treehouse',
  16. 'Hotel',
  17. ]
  18. # Get values
  19. properties_values = [ switch_dict[ x ] for x in properties_list ]
  20. print(properties_values)
  21. # Output:
  22. # [ 125, 250, 300, 999, 999 ]
  23. # Remove default case, becomes a normal dictionary
  24. switch_dict.default_case = None