Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cidict.py 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """
  2. This is a convenience wrapper for dictionaries
  3. returned from LDAP servers containing attribute
  4. names of variable case.
  5. See https://www.python-ldap.org/ for details.
  6. """
  7. from ldap import __version__
  8. from ldap.compat import IterableUserDict
  9. class cidict(IterableUserDict):
  10. """
  11. Case-insensitive but case-respecting dictionary.
  12. """
  13. def __init__(self,default=None):
  14. self._keys = {}
  15. IterableUserDict.__init__(self,{})
  16. self.update(default or {})
  17. def __getitem__(self,key):
  18. return self.data[key.lower()]
  19. def __setitem__(self,key,value):
  20. lower_key = key.lower()
  21. self._keys[lower_key] = key
  22. self.data[lower_key] = value
  23. def __delitem__(self,key):
  24. lower_key = key.lower()
  25. del self._keys[lower_key]
  26. del self.data[lower_key]
  27. def update(self,dict):
  28. for key, value in dict.items():
  29. self[key] = value
  30. def has_key(self,key):
  31. return key in self
  32. def __contains__(self,key):
  33. return IterableUserDict.__contains__(self, key.lower())
  34. def __iter__(self):
  35. return iter(self.keys())
  36. def keys(self):
  37. return self._keys.values()
  38. def items(self):
  39. result = []
  40. for k in self._keys.values():
  41. result.append((k,self[k]))
  42. return result
  43. def strlist_minus(a,b):
  44. """
  45. Return list of all items in a which are not in b (a - b).
  46. a,b are supposed to be lists of case-insensitive strings.
  47. """
  48. temp = cidict()
  49. for elt in b:
  50. temp[elt] = elt
  51. result = [
  52. elt
  53. for elt in a
  54. if elt not in temp
  55. ]
  56. return result
  57. def strlist_intersection(a,b):
  58. """
  59. Return intersection of two lists of case-insensitive strings a,b.
  60. """
  61. temp = cidict()
  62. for elt in a:
  63. temp[elt] = elt
  64. result = [
  65. temp[elt]
  66. for elt in b
  67. if elt in temp
  68. ]
  69. return result
  70. def strlist_union(a,b):
  71. """
  72. Return union of two lists of case-insensitive strings a,b.
  73. """
  74. temp = cidict()
  75. for elt in a:
  76. temp[elt] = elt
  77. for elt in b:
  78. temp[elt] = elt
  79. return temp.values()