File: rexx.py

package info (click to toggle)
bcnc 0.9.14.318%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,072 kB
  • sloc: python: 39,148; sh: 45; makefile: 44
file content (330 lines) | stat: -rw-r--r-- 8,399 bytes parent folder | download | duplicates (2)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#
# Copyright and User License
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
# Copyright Vasilis.Vlachoudis@cern.ch for the
# European Organization for Nuclear Research (CERN)
#
# Please consult the flair documentation for the license
#
# DISCLAIMER
# ~~~~~~~~~~
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY, OF
# SATISFACTORY QUALITY, AND FITNESS FOR A PARTICULAR PURPOSE
# OR USE ARE DISCLAIMED. THE COPYRIGHT HOLDERS AND THE
# AUTHORS MAKE NO REPRESENTATION THAT THE SOFTWARE AND
# MODIFICATIONS THEREOF, WILL NOT INFRINGE ANY PATENT,
# COPYRIGHT, TRADE SECRET OR OTHER PROPRIETARY RIGHT.
#
# LIMITATION OF LIABILITY
# ~~~~~~~~~~~~~~~~~~~~~~~
# THE COPYRIGHT HOLDERS AND THE AUTHORS SHALL HAVE NO
# LIABILITY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
# CONSEQUENTIAL, EXEMPLARY, OR PUNITIVE DAMAGES OF ANY
# CHARACTER INCLUDING, WITHOUT LIMITATION, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES, LOSS OF USE, DATA OR PROFITS,
# OR BUSINESS INTERRUPTION, HOWEVER CAUSED AND ON ANY THEORY
# OF CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE), PRODUCT
# LIABILITY OR OTHERWISE, ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
#
# Author:	Vasilis.Vlachoudis@cern.ch
# Date:	14-May-2004

from __future__ import absolute_import
__author__ = "Vasilis Vlachoudis"
__email__  = "Vasilis.Vlachoudis@cern.ch"

import string

_letters_digits = string.ascii_letters + string.digits
_letters_digits_symbol = _letters_digits + "_."


# abbrev
def abbrev(information, info, l=0):
	"""
	return true if the info is an abbreviation of information
	with minimum length l
	"""
	if l>0:
		length = l
	else:
		length = len(info)

	cond1 = (len(information) >= len(info))
	cond2 = (len(info) >= length)
	cond3 = (information[:len(info)] == info)
	return cond1 and cond2 and cond3


# center
def center(s, l, pad=' '):
	if l<=0: return ""

	i = l - len(s);
	if i==0:
		return s
	elif i < 0:
		i = -i
		a = i // 2
		return s[a:a+l]
	else:
		a = i // 2
		return "%s%s%s"%(pad*a, s, pad*(i-a))


# datatype
def datatype(str, check="N"):
	"""rexx datatype function"""

	try:
		if len(str)==0:
			return check=="X" or check=="B"
	except:
		return check=="X" or check=="B"

	if check=="N":
		return _isnum(str)

	if check=="A":
		return verify(str, _letters_digits)==-1
	elif check=="L":
		return verify(str, string.ascii_lowercase)==-1
	elif check=="M":
		return verify(str, string.ascii_letters)==-1
	elif check=="U":
		return verify(str, string.ascii_uppercase)==-1
	elif check=="O":
		return verify(str, string.octdigits)==-1
	elif check=="X":
		return verify(str, string.hexdigits)==-1
	elif check=="S":
		return (str[0] in string.ascii_letters) and \
			(verify(str[1:], _letters_digits_symbol)==-1)
	else:
		return _isnum(str)


# insert
def insert(new, target, n, pad=" "):
	"""
	insert new string to target as position n padded with pad characters
	"""
	if n==0:
		return new+target
	elif n>len(target):
		return target + pad*(n-len(target)) + new

	return target[0:n] + new + target[n:]


# left
def left(str, length, pad=" "):
	"""return left of string str of length padded with pad chars"""
	if length<len(str):
		return str[0:length]
	else:
		return str + (pad*(length-len(str)))


# translate
def translate(str, tableo=None, tablei=None, pad=" "):
	"""translate string"""
	# If neither input nor output tables, uppercase.
	if tableo==None and tablei==None:
		return str.upper()

	if tableo==None:
		tableo = xrange(0,255)

	if tablei==None:
		tablei = xrange(0,255)

	# The input table defaults to all characters.
	dl = len(tablei)-len(tableo)
	if dl>0:
		tableo += pad*dl
	else:
		tablei += pad*(-dl)

	tbl = string.maketrans(tablei,tableo)
	return str.translate(tbl)


# reverse
def reverse(str):
	"""reverse string"""
	return str[::-1]


# verify
def verify(str,ref,match=0,start=0):
	"""
	return the index of the first character in string that
	is not also in reference. if "Match" is given, then return
	the result index of the first character in string that is in reference
	"""

	if start<0: start = 0
	if start>=len(str): return -1

	for i in range(start,len(str)):
		found = ref.find(str[i])==-1
		if found ^ match:
			return i
	return -1


# xrange
def xrange(start,stop):
	return "".join([chr(x) for x in range(start, stop+1)])


# isnum - return true if string is number
def _isnum(str):
	str = str.strip()

	# accept one sign
	i = 0
	l = len(str)

	if l==0: return False

	if str[i]=='-' or str[i]=='+': i += 1

	# skip spaces after sign
	while i<l and str[i].isspace(): i += 1

	# accept many digits
	if i<l and '0'<=str[i]<='9':
		i += 1
		F  = 1
		while i<l and '0'<=str[i]<='9': i += 1
	else:
		F = 0

	# accept one dot
	if i<l and str[i]=='.':
		i+=1

		# accept many digits
		if i<l and '0'<=str[i]<='9':
			while i<l and '0'<=str[i]<='9': i += 1
		else:
			if not F: return False
	else:
		if not F: return False

	# accept one e/E/d/D
	if i<l and (str[i]=='e' or str[i]=='E' or str[i]=='d' or str[i]=='D'):
		i+=1
		# accept one sign
		if i<l and (str[i]=='-' or str[i]=='+'): i += 1

		# accept many digits
		if i<l and '0'<=str[i]<='9':
			while i<l and '0'<=str[i]<='9': i += 1
		else:
			return False

	if i != l: return False

	return True

if __name__=="__main__":
	from .log import say

	say("abbrev")
	assert     abbrev('information','info',4)
	assert     abbrev('information','',0)
	assert not abbrev('information','Info',4)
	assert not abbrev('information','info',5)
	assert not abbrev('information','info ')
	assert     abbrev('information','info',3)
	assert not abbrev('info','information',3)
	assert not abbrev('info','info',5)

	say("center")
	assert center('****',0,'-')      == ''
	assert center('****',8,'-')      == '--****--'
	assert center('****',7,'-')      == '-****--'
	assert center('*****',8,'-')     == '-*****--'
	assert center('*****',7,'-')     == '-*****-'
	assert center('12345678',4,'-')  == '3456'
	assert center('12345678',5,'-')  == '23456'
	assert center('1234567',4,'-')   == '2345'
	assert center('1234567',5,'-')   == '23456'

	say("datatype")
	assert not datatype("")
	assert not datatype("foobar")
	assert not datatype("foo bar")
	assert not datatype("123.456.789")
	assert     datatype("123.456")
	assert not datatype("DeadBeef")
	assert not datatype("Dead Beef")
	assert not datatype("1234ABCD")
	assert     datatype("01001101")
	assert not datatype("0110 1101")
	assert not datatype("0110 101")
	assert     datatype("1324.1234")
	assert     datatype("123")
	assert     datatype("12.3")
	assert     datatype('123.123')
	assert     datatype('123.123E3')
	assert     datatype('123.0000003')
	assert     datatype('123.0000004')
	assert     datatype('123.0000005')
	assert     datatype('123.0000006')
	assert     datatype(' 23')
	assert     datatype(' 23 ')
	assert     datatype('23 ')
	assert     datatype('123.00')
	assert     datatype('123000E-2')
	assert     datatype('123000E+2')
	assert not datatype("A B C")
	assert not datatype("123ABC")
	assert not datatype("123AHC")
	assert     datatype('0.000E-2')
	assert     datatype('0.000E-1')
	assert     datatype('0.000E0')
	assert     datatype('0.000E1')
	assert     datatype('0.000E2')
	assert     datatype('0.000E3')
	assert     datatype('0.000E4')
	assert     datatype('0.000E5')
	assert     datatype('0.000E6')
	assert     datatype('0E-1')
	assert     datatype('0E0')
	assert     datatype('0E1')
	assert     datatype('0E2')
	assert not datatype('+.')
	assert not datatype('++0')

	say("insert")
	assert insert("abc","def",2) == "deabcf"
	assert insert("abc","def",3) == "defabc"
	assert insert("abc","def",5) == "def  abc"
	assert insert("abc","def",5,'*') == "def**abc"

	say("translate")
#	assert translate("Foo Bar"), "FOO BAR"
	assert translate("Foo Bar","","") == "Foo Bar"
#	assert translate("Foo Bar","") == "       "
#	assert translate("Foo Bar",None,None,'*') == "*******"
	assert translate("Foo Bar",xrange(1,255)) == "Gpp!Cbs"
	assert translate("","klasjdf","woieruw") == ""
	assert translate("foobar","abcdef","fedcba") == "aooefr"

	say("verify")
	assert verify('foobar', 'barfo', 0, 0)==-1
	assert verify('foobar', 'barfo', 1, 0)==0
	assert verify('', 'barfo')==-1
	assert verify('foobar', '')==0
	assert verify('foobar', 'barf', 0, 2)==2
	assert verify('foobar', 'barf', 0, 3)==-1
	assert verify('', '')==-1

	say("All Test passed")