aboutsummaryrefslogtreecommitdiffstats
path: root/netmodel/util/misc.py
diff options
context:
space:
mode:
Diffstat (limited to 'netmodel/util/misc.py')
-rw-r--r--netmodel/util/misc.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/netmodel/util/misc.py b/netmodel/util/misc.py
new file mode 100644
index 00000000..315887b3
--- /dev/null
+++ b/netmodel/util/misc.py
@@ -0,0 +1,62 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017 Cisco and/or its affiliates.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import collections
+
+def is_iterable(x):
+ return isinstance(x, collections.Iterable) and not isinstance(x, str)
+
+#------------------------------------------------------------------------------
+from itertools import tee
+
+# https://docs.python.org/3/library/itertools.html#itertools-recipes
+def pairwise(iterable):
+ "s -> (s0,s1), (s1,s2), (s2, s3), ..."
+ a, b = tee(iterable)
+ next(b, None)
+ return zip(a, b)
+
+
+#------------------------------------------------------------------------------
+# http://stackoverflow.com/questions/1630320/what-is-the-pythonic-way-to-detect-the-last-element-in-a-python-for-loop
+def lookahead(iterable):
+ it = iter(iterable)
+ last = next(it)
+ for val in it:
+ yield last, False
+ last = val
+ yield last, True
+
+#------------------------------------------------------------------------------
+# http://stackoverflow.com/questions/10840533/most-pythonic-way-to-delete-a-file-which-may-not-exist
+
+import os, errno
+
+def silentremove(filename):
+ try:
+ os.remove(filename)
+ except OSError as e: # this would be "except OSError, e:" before Python 2.6
+ if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
+ raise # re-raise exception if a different error occured
+
+#------------------------------------------------------------------------------
+import socket
+
+def is_local_host(hostname):
+ return hostname in ['localhost', '127.0.0.1'] or \
+ hostname == socket.gethostname()