Guest User

#1Zl-Avwx (Python)

a guest
Jan 12th, 2023
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. from collections import UserString
  2.  
  3.  
  4. class MyString(UserString):
  5.     def __init__(self, s):
  6.         super().__init__(s)
  7.         if len(self) > 30:
  8.             raise ValueError(f"str length is larger than 30: {s}")
  9.  
  10.     def __eq__(self, other):
  11.         if not isinstance(other, (str, self.__class__)):
  12.             return False
  13.         return str(self).lower().__eq__(str(other).lower())
  14.  
  15.     def __ne__(self, other):
  16.         return not self.__eq__(other)
  17.  
  18.     def __lt__(self, other):
  19.         if not isinstance(other, (str, self.__class__)):
  20.             raise TypeError(
  21.                 f"not supported between instances of '{self.__class__.__name__}' and '{other.__class__.__name__}'"
  22.             )
  23.         return str(self).lower().__lt__(str(other).lower())
  24.  
  25.     def __le__(self, other):
  26.         if not isinstance(other, (str, self.__class__)):
  27.             raise TypeError(
  28.                 f"not supported between instances of '{self.__class__.__name__}' and '{other.__class__.__name__}'"
  29.             )
  30.         return str(self).lower().__le__(str(other).lower())
  31.  
  32.     def __gt__(self, other):
  33.         if not isinstance(other, (str, self.__class__)):
  34.             raise TypeError(
  35.                 f"not supported between instances of '{self.__class__.__name__}' and '{other.__class__.__name__}'"
  36.             )
  37.         return str(self).lower().__gt__(str(other).lower())
  38.  
  39.     def __ge__(self, other):
  40.         if not isinstance(other, (str, self.__class__)):
  41.             raise TypeError(
  42.                 f"not supported between instances of '{self.__class__.__name__}' and '{other.__class__.__name__}'"
  43.             )
  44.         return str(self).lower().__ge__(str(other).lower())
  45.  
  46.  
  47. # testing
  48. # create instance
  49. foo_int = MyString(123)
  50. print(foo_int)
  51. foo_float = MyString(123.0)
  52. print(foo_float)
  53.  
  54. foo = MyString("Foo")
  55.  
  56. # concat
  57. foobar = foo + "bar"
  58. assert isinstance(foobar, MyString)
  59. foobar = "foo" + MyString("bar")
  60. assert isinstance(foobar, MyString)
  61. assert foobar == "foobar"
  62.  
  63. # concat error
  64. try:
  65.     MyString("1" * 29) + "11"
  66. except ValueError as e:
  67.     print(e)
  68.  
  69. # multiply
  70. foo3 = foo * 10
  71.  
  72. # multiply error
  73. try:
  74.     foo * 11
  75. except ValueError as e:
  76.     print(e)
  77.  
  78. # total ordering
  79. assert foo == "foo"
  80. assert foo == "Foo"
  81. assert foo >= "foo"
  82. assert foo >= "Foo"
  83. assert foo <= "foo"
  84. assert foo <= "Foo"
  85.  
  86. assert foo != "bar"
  87. assert foo != "BAR"
  88.  
  89. assert foo > "foa"
  90. assert foo < "foz"
  91. assert foo <= "foz"
  92. assert foo >= "foa"
  93.  
  94.  
  95. assert "foo" == foo
  96. assert "Foo" == foo
  97. assert "foo" >= foo
  98. assert "Foo" >= foo
  99. assert "foo" <= foo
  100. assert "Foo" <= foo
  101.  
  102. assert "bar" != foo
  103. assert "BAR" != foo
  104.  
  105. assert "foa" < foo
  106. assert "foz" > foo
  107. assert "foz" >= foo
  108. assert "foa" <= foo
  109.  
Add Comment
Please, Sign In to add comment