Initialization of NomadNet project
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
#! /usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
@@ -0,0 +1,26 @@
|
||||
#! /usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
|
||||
import enum
|
||||
|
||||
|
||||
class MODIFIER_KEY(enum.Enum):
|
||||
"""Represents modifier keys such as 'ctrl', 'shift' and so on.
|
||||
Not every combination of modifier and input is useful."""
|
||||
|
||||
NONE = ""
|
||||
SHIFT = "shift"
|
||||
ALT = "meta"
|
||||
CTRL = "ctrl"
|
||||
SHIFT_ALT = "shift meta"
|
||||
SHIFT_CTRL = "shift ctrl"
|
||||
ALT_CTRL = "meta ctrl"
|
||||
SHIFT_ALT_CTRL = "shift meta ctrl"
|
||||
|
||||
def append_to(self, text, separator=" "):
|
||||
return (text + separator + self.value) if (self != self.__class__.NONE) else text
|
||||
|
||||
def prepend_to(self, text, separator=" "):
|
||||
return (self.value + separator + text) if (self != self.__class__.NONE) else text
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
#! /usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""A non-thematic collection of useful functions."""
|
||||
|
||||
|
||||
def recursively_replace(original, replacements, include_original_keys=False):
|
||||
"""Clones an iterable and recursively replaces specific values."""
|
||||
|
||||
# If this function would be called recursively, the parameters 'replacements' and 'include_original_keys' would have to be
|
||||
# passed each time. Therefore, a helper function with a reduced parameter list is used for the recursion, which nevertheless
|
||||
# can access the said parameters.
|
||||
|
||||
def _recursion_helper(obj):
|
||||
#Determine if the object should be replaced. If it is not hashable, the search will throw a TypeError.
|
||||
try:
|
||||
if obj in replacements:
|
||||
return replacements[obj]
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
# An iterable is recursively processed depending on its class.
|
||||
if hasattr(obj, "__iter__") and not isinstance(obj, (str, bytes, bytearray)):
|
||||
if isinstance(obj, dict):
|
||||
contents = {}
|
||||
|
||||
for key, val in obj.items():
|
||||
new_key = _recursion_helper(key) if include_original_keys else key
|
||||
new_val = _recursion_helper(val)
|
||||
|
||||
contents[new_key] = new_val
|
||||
|
||||
else:
|
||||
contents = []
|
||||
|
||||
for element in obj:
|
||||
new_element = _recursion_helper(element)
|
||||
|
||||
contents.append(new_element)
|
||||
|
||||
# Use the same class as the original.
|
||||
return obj.__class__(contents)
|
||||
|
||||
# If it is not replaced and it is not an iterable, return it.
|
||||
return obj
|
||||
|
||||
return _recursion_helper(original)
|
||||
Reference in New Issue
Block a user