From 58c35285f898518bd902232bf6e811236de02ecc Mon Sep 17 00:00:00 2001 From: Christof Schulze Date: Mon, 28 Oct 2019 21:52:42 +0100 Subject: [PATCH] some cleanups and headers --- collections.py | 71 ++++++++++++++++++++++++++++++++++++++++++ display.py | 3 +- git.py | 17 ++++++++++ parsing/__init__.py | 1 - parsing/yaml/dumper.py | 19 ++++++++++- singleton.py | 19 +++++++++++ six.py | 33 -------------------- 7 files changed, 126 insertions(+), 37 deletions(-) create mode 100644 collections.py delete mode 100644 six.py diff --git a/collections.py b/collections.py new file mode 100644 index 0000000..091a1d4 --- /dev/null +++ b/collections.py @@ -0,0 +1,71 @@ +# GNU Lesser General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/lgpl-3.0.txt) +# (c) 2019, Christof Schulze +# +# This file is part of Ammsml +# +# Ammsml is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ammsml is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Ammsml. If not, see . + +from collections.abc import Sequence + + +def is_string(seq): + """Identify whether the input has a string-like type.""" + return isinstance(seq, str) + + +def is_iterable(seq, include_strings=False): + """Identify whether the input is an iterable.""" + if not include_strings and is_string(seq): + return False + + try: + iter(seq) + return True + except TypeError: + return False + + +def is_sequence(seq, include_strings=False): + """ + Identify whether the input is a sequence. + Strings are not sequences here, + unless ``include_string`` is ``True``. + Non-indexable things are never of a sequence type. + """ + if not include_strings and is_string(seq): + return False + + return isinstance(seq, Sequence) + + +def count(seq): + """ + Returns a dictionary with the number of appearances of each element of the iterable. + + Resembles the `collections.Counter` class functionality, which is deprecated + since version 3.3, will be removed in version 3.9 + It is meant to be replaced when the we find something equivalent in maybe collections.abc + + this code also runs on Python 2.6.* where collections.Counter is not available. + + """ + if not is_iterable(seq): + raise Exception('Argument provided is not an iterable') + + if not is_iterable(seq): + raise Exception('Argument provided is not an iterable') + counters = dict() + for elem in seq: + counters[elem] = counters.get(elem, 0) + 1 + return counters diff --git a/display.py b/display.py index 064a40c..f0563b6 100644 --- a/display.py +++ b/display.py @@ -31,9 +31,8 @@ from termios import TIOCGWINSZ from ammsml.utils.color import stringc from ammsml.utils.parsing import to_text from ammsml.utils.errors import AMMSMLError -from ammsml.utils.singleton import Singleton +from ammsml.utils.singleton import Singleton, with_metaclass from ammsml.config import constants as C -from ammsml.utils.six import with_metaclass from ammsml.utils.wrap import wrap_var diff --git a/git.py b/git.py index 11f4651..e973da3 100644 --- a/git.py +++ b/git.py @@ -1,3 +1,20 @@ +# GNU Lesser General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/lgpl-3.0.txt) +# (c) 2019, Christof Schulze +# +# This file is part of Ammsml +# +# Ammsml is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ammsml is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Ammsml. If not, see . import os import time diff --git a/parsing/__init__.py b/parsing/__init__.py index d356b39..cdfd95f 100644 --- a/parsing/__init__.py +++ b/parsing/__init__.py @@ -16,7 +16,6 @@ # You should have received a copy of the GNU Lesser General Public License # along with Ammsml. If not, see . - BOOLEANS_TRUE = frozenset(('y', 'yes', 'on', '1', 'true', 't', 1, 1.0, True)) BOOLEANS_FALSE = frozenset(('n', 'no', 'off', '0', 'false', 'f', 0, 0.0, False)) BOOLEANS = BOOLEANS_TRUE.union(BOOLEANS_FALSE) diff --git a/parsing/yaml/dumper.py b/parsing/yaml/dumper.py index 947c8a5..8940196 100644 --- a/parsing/yaml/dumper.py +++ b/parsing/yaml/dumper.py @@ -1,5 +1,22 @@ -import yaml +# GNU Lesser General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/lgpl-3.0.txt) +# (c) 2019, Christof Schulze +# +# This file is part of Ammsml +# +# Ammsml is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ammsml is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Ammsml. If not, see . +import yaml # from ammsml.vars.hostvars import HostVars, HostVarsVars from ammsml.utils.parsing.yaml.objects import AMMSMLUnicode, AMMSMLSequence, AMMSMLMapping diff --git a/singleton.py b/singleton.py index 36bdc6d..ce9f97f 100644 --- a/singleton.py +++ b/singleton.py @@ -38,3 +38,22 @@ class Singleton(type): cls.__instance = super(Singleton, cls).__call__(*args, **kw) return cls.__instance + + +# +# metaclass handling +# +def with_metaclass(meta, *bases): + """Create a base class with a metaclass.""" + # This requires a bit of explanation: the basic idea is to make a dummy + # metaclass for one level of class instantiation that replaces itself with + # the actual metaclass. + class metaclass(type): + + def __new__(cls, name, this_bases, d): + return meta(name, bases, d) + + @classmethod + def __prepare__(cls, name, this_bases): + return meta.__prepare__(name, bases) + return type.__new__(metaclass, 'temporary_class', (), {}) diff --git a/six.py b/six.py deleted file mode 100644 index 916f939..0000000 --- a/six.py +++ /dev/null @@ -1,33 +0,0 @@ -# GNU Lesser General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/lgpl-3.0.txt) -# (c) 2019, Christof Schulze -# -# This file is part of Ammsml -# -# Ammsml is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ammsml is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with Ammsml. If not, see . - - -def with_metaclass(meta, *bases): - """Create a base class with a metaclass.""" - # This requires a bit of explanation: the basic idea is to make a dummy - # metaclass for one level of class instantiation that replaces itself with - # the actual metaclass. - class metaclass(type): - - def __new__(cls, name, this_bases, d): - return meta(name, bases, d) - - @classmethod - def __prepare__(cls, name, this_bases): - return meta.__prepare__(name, bases) - return type.__new__(metaclass, 'temporary_class', (), {}) -- GitLab