Add suffix and remove_suffix functions similar to prefix and remove_prefix

This commit is contained in:
Fatih Aşıcı
2009-08-23 04:00:51 +00:00
parent cfb43d7d51
commit 60b44c4e58
+14
View File
@@ -103,6 +103,20 @@ def remove_prefix(a,b):
assert prefix(a, b) assert prefix(a, b)
return b[len(a):] return b[len(a):]
def suffix(a, b):
"""Check if sequence a is a suffix of sequence b."""
if len(a) > len(b):
return False
for i in range(1, len(a) + 1):
if a[-i] != b[-i]:
return False
return True
def remove_suffix(a, b):
"""Remove suffix a from sequence b."""
assert suffix(a, b)
return b[:-len(a)]
def human_readable_size(size = 0): def human_readable_size(size = 0):
symbols, depth = [' B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 0 symbols, depth = [' B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], 0