Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e61fa0a

Browse files
committed
* profile.py, pdb.py: added help() function
* builtin.py: b/w compat for builtin -> __builtin__ name change * string.py: added atof() and atol() and corresponding exceptions * test_types.py: added test for list sort with user comparison function
1 parent 3bb8a05 commit e61fa0a

5 files changed

Lines changed: 93 additions & 2 deletions

File tree

Lib/pdb.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,15 @@ def test():
312312
import linecache
313313
linecache.checkcache()
314314
run(TESTCMD)
315+
316+
# print help
317+
def help():
318+
for dirname in sys.path:
319+
fullname = os.path.join(dirname, 'pdb.doc')
320+
if os.path.exists(fullname):
321+
sts = os.system('${PAGER-more} '+fullname)
322+
if sts: print '*** Pager exit status:', sts
323+
break
324+
else:
325+
print 'Sorry, can\'t find the help file "pdb.doc"',
326+
print 'along the Python search path'

Lib/profile.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,15 @@ def debug():
377377
# test command
378378
def test():
379379
run('import x; x.main()')
380+
381+
# print help
382+
def help():
383+
for dirname in sys.path:
384+
fullname = os.path.join(dirname, 'profile.doc')
385+
if os.path.exists(fullname):
386+
sts = os.system('${PAGER-more} '+fullname)
387+
if sts: print '*** Pager exit status:', sts
388+
break
389+
else:
390+
print 'Sorry, can\'t find the help file "profile.doc"',
391+
print 'along the Python search path'

Lib/string.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,26 @@ def find(*args):
116116
except index_error:
117117
return -1
118118

119+
# Convert string to float
120+
atof_error = 'non-float argument to string.atof'
121+
def atof(str):
122+
import regex
123+
sign = ''
124+
s = str
125+
if s and s[0] in '+-':
126+
sign = s[0]
127+
s = s[1:]
128+
if not s: raise atof_error, str
129+
while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:]
130+
if regex.match('[0-9]*\(\.[0-9]*\)?\([eE][-+]?[0-9]+\)?', s) != len(s):
131+
raise atof_error, str
132+
try:
133+
return eval(sign + s)
134+
except SyntaxError:
135+
raise atof_error, str
136+
119137
# Convert string to integer
120-
atoi_error = 'non-numeric argument to string.atoi'
138+
atoi_error = 'non-integer argument to string.atoi'
121139
def atoi(str):
122140
sign = ''
123141
s = str
@@ -130,6 +148,20 @@ def atoi(str):
130148
if c not in digits: raise atoi_error, str
131149
return eval(sign + s)
132150

151+
# Convert string to long integer
152+
atol_error = 'non-integer argument to string.atol'
153+
def atol(str):
154+
sign = ''
155+
s = str
156+
if s and s[0] in '+-':
157+
sign = s[0]
158+
s = s[1:]
159+
if not s: raise atoi_error, str
160+
while s[0] == '0' and len(s) > 1: s = s[1:]
161+
for c in s:
162+
if c not in digits: raise atoi_error, str
163+
return eval(sign + s + 'L')
164+
133165
# Left-justify a string
134166
def ljust(s, width):
135167
n = width - len(s)

Lib/stringold.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,26 @@ def find(*args):
116116
except index_error:
117117
return -1
118118

119+
# Convert string to float
120+
atof_error = 'non-float argument to string.atof'
121+
def atof(str):
122+
import regex
123+
sign = ''
124+
s = str
125+
if s and s[0] in '+-':
126+
sign = s[0]
127+
s = s[1:]
128+
if not s: raise atof_error, str
129+
while s[0] == '0' and len(s) > 1 and s[1] in digits: s = s[1:]
130+
if regex.match('[0-9]*\(\.[0-9]*\)?\([eE][-+]?[0-9]+\)?', s) != len(s):
131+
raise atof_error, str
132+
try:
133+
return eval(sign + s)
134+
except SyntaxError:
135+
raise atof_error, str
136+
119137
# Convert string to integer
120-
atoi_error = 'non-numeric argument to string.atoi'
138+
atoi_error = 'non-integer argument to string.atoi'
121139
def atoi(str):
122140
sign = ''
123141
s = str
@@ -130,6 +148,20 @@ def atoi(str):
130148
if c not in digits: raise atoi_error, str
131149
return eval(sign + s)
132150

151+
# Convert string to long integer
152+
atol_error = 'non-integer argument to string.atol'
153+
def atol(str):
154+
sign = ''
155+
s = str
156+
if s and s[0] in '+-':
157+
sign = s[0]
158+
s = s[1:]
159+
if not s: raise atoi_error, str
160+
while s[0] == '0' and len(s) > 1: s = s[1:]
161+
for c in s:
162+
if c not in digits: raise atoi_error, str
163+
return eval(sign + s + 'L')
164+
133165
# Left-justify a string
134166
def ljust(s, width):
135167
n = width - len(s)

Lib/test/test_types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ class C: pass
152152
if a <> [2,1,0,-1,-2]: raise TestFailed, 'list reverse'
153153
a.sort()
154154
if a <> [-2,-1,0,1,2]: raise TestFailed, 'list sort'
155+
def revcmp(a, b): return cmp(b, a)
156+
a.sort(revcmp)
157+
if a <> [2,1,0,-1,-2]: raise TestFailed, 'list sort with cmp func'
155158

156159
print '6.6 Mappings == Dictionaries'
157160
d = {}

0 commit comments

Comments
 (0)