blob: cbbacb279719fba3b3e0a53f23aabebb1d5f2ba4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
import os
import sys
import string
import random
import time
import socket
import re
try:
import pwd
except ImportError:
import getpass
pwd = None
using_python_3 = True if sys.version_info.major == 3 else False
def get_current_user():
if pwd:
return pwd.getpwuid(os.geteuid()).pw_name
else:
return getpass.getuser()
def user_input():
if using_python_3:
return input()
else:
# using python version 2
return raw_input()
class random_id_gen:
"""
Emulated generator for creating a random chars id of specific length
:parameters:
length : int
the desired length of the generated id
default: 8
:return:
a random id with each next() request.
"""
def __init__(self, length=8):
self.id_chars = string.ascii_lowercase + string.digits
self.length = length
def next(self):
return ''.join(random.choice(self.id_chars) for _ in range(self.length))
__next__ = next
# try to get number from input, return None in case of fail
def get_number(input):
try:
return long(input)
except:
try:
return int(input)
except:
return None
def list_intersect(l1, l2):
return list(filter(lambda x: x in l2, l1))
def list_difference (l1, l2):
return list(filter(lambda x: x not in l2, l1))
def is_sub_list (l1, l2):
return set(l1) <= set(l2)
# a simple passive timer
class PassiveTimer(object):
# timeout_sec = None means forever
def __init__ (self, timeout_sec):
if timeout_sec != None:
self.expr_sec = time.time() + timeout_sec
else:
self.expr_sec = None
def has_expired (self):
# if no timeout was set - return always false
if self.expr_sec == None:
return False
return (time.time() > self.expr_sec)
def is_valid_ipv4 (addr):
try:
socket.inet_pton(socket.AF_INET, addr)
return True
except (socket.error, TypeError):
return False
def is_valid_mac (mac):
return bool(re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", mac.lower()))
def list_remove_dup (l):
tmp = list()
for x in l:
if not x in tmp:
tmp.append(x)
return tmp
|