From 9e47c19fa312b44a6b61aab9993753920258863f Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Fri, 21 Dec 2018 08:17:08 +0100
Subject: [PATCH] Add strtools.strip_prefix()
---
src/imcflibs/strtools.py | 21 +++++++++++++++++++++
tests/test_strtools.py | 8 +++++++-
2 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/src/imcflibs/strtools.py b/src/imcflibs/strtools.py
index 49db1d5..2f1c626 100644
--- a/src/imcflibs/strtools.py
+++ b/src/imcflibs/strtools.py
@@ -76,3 +76,24 @@ def flatten(lst):
for line in lst:
flat += line
return flat
+
+
+def strip_prefix(string, prefix):
+ """Remove a given prefix from a string.
+
+ Parameters
+ ----------
+ string : str
+ The original string from which the prefix should be removed.
+ prefix : str
+ The prefix to be removed.
+
+ Returns
+ -------
+ str
+ The original string without the given prefix. In case the original
+ string doesn't start with the prefix, it is returned unchanged.
+ """
+ if string.startswith(prefix):
+ string = string[len(prefix):]
+ return string
diff --git a/tests/test_strtools.py b/tests/test_strtools.py
index c49a762..823b561 100644
--- a/tests/test_strtools.py
+++ b/tests/test_strtools.py
@@ -7,6 +7,7 @@ import os
from imcflibs.strtools import _is_string_like
from imcflibs.strtools import filename
from imcflibs.strtools import flatten
+from imcflibs.strtools import strip_prefix
__author__ = "Niko Ehrenfeuchter"
__copyright__ = "Niko Ehrenfeuchter"
@@ -29,4 +30,9 @@ def test_filename_from_handle(tmpdir):
def test_flatten():
- assert flatten(('foo', 'bar')) == 'foobar'
\ No newline at end of file
+ assert flatten(('foo', 'bar')) == 'foobar'
+
+
+def test_strip_prefix():
+ assert strip_prefix('foobar', 'foo') == 'bar'
+ assert strip_prefix('foobar', 'bar') == 'foobar'
\ No newline at end of file
--
GitLab