-
Notifications
You must be signed in to change notification settings - Fork 36
/
Utilities.py
96 lines (74 loc) · 2.42 KB
/
Utilities.py
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
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
import math
import random
import string
utility_builtins = {}
utility_builtins['string'] = string
utility_builtins['math'] = math
utility_builtins['random'] = random
utility_builtins['whrandom'] = random
utility_builtins['set'] = set
utility_builtins['frozenset'] = frozenset
try:
import DateTime
utility_builtins['DateTime'] = DateTime.DateTime
except ImportError:
pass
def same_type(arg1, *args):
"""Compares the class or type of two or more objects."""
t = getattr(arg1, '__class__', type(arg1))
for arg in args:
if getattr(arg, '__class__', type(arg)) is not t:
return 0
return 1
utility_builtins['same_type'] = same_type
def test(*args):
length = len(args)
for i in range(1, length, 2):
if args[i - 1]:
return args[i]
if length % 2:
return args[-1]
utility_builtins['test'] = test
def reorder(s, with_=None, without=()):
# s, with_, and without are sequences treated as sets.
# The result is subtract(intersect(s, with_), without),
# unless with_ is None, in which case it is subtract(s, without).
if with_ is None:
with_ = s
orig = {}
for item in s:
if isinstance(item, tuple) and len(item) == 2:
key, value = item
else:
key = value = item
orig[key] = value
result = []
for item in without:
if isinstance(item, tuple) and len(item) == 2:
key, ignored = item
else:
key = item
if key in orig:
del orig[key]
for item in with_:
if isinstance(item, tuple) and len(item) == 2:
key, ignored = item
else:
key = item
if key in orig:
result.append((key, orig[key]))
del orig[key]
return result
utility_builtins['reorder'] = reorder