Queer European MD passionate about IT
Explorar o código

custom_join function added

It joins elements of a list using a joiner and a final custom joiner
Davte %!s(int64=5) %!d(string=hai) anos
pai
achega
9fccdcc51c
Modificáronse 2 ficheiros con 15 adicións e 1 borrados
  1. 1 1
      davtelepot/__init__.py
  2. 14 0
      davtelepot/utilities.py

+ 1 - 1
davtelepot/__init__.py

@@ -7,7 +7,7 @@ __author__ = "Davide Testa"
 __email__ = "davide@davte.it"
 __credits__ = "Marco Origlia"
 __license__ = "GNU General Public License v3.0"
-__version__ = "1.5.7"
+__version__ = "1.5.9"
 __maintainer__ = "Davide Testa"
 __contact__ = "t.me/davte"
 

+ 14 - 0
davtelepot/utilities.py

@@ -1468,3 +1468,17 @@ def run_aiohttp_server(app, *args, **kwargs):
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     web.run_app(app, *args, **kwargs)
+
+
+def custom_join(_list, joiner, final=None):
+    """Join elements of `_list` using `joiner` (`final` as last joiner)."""
+    _list = list(map(str, _list))
+    if final is None:
+        final = joiner
+    if len(_list) == 0:
+        return ''
+    if len(_list) == 1:
+        return _list[0]
+    if len(_list) == 2:
+        return final.join(_list)
+    return joiner.join(_list[:-1]) + final + _list[-1]