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

Skip to content

Commit ae3b3a3

Browse files
committed
* test_*.py: new lambda syntax (also affects tests for filter, map,
reduce) * ftplib.py: added default callback for retrlines; added dir() method * ftplib.py: don't return self in self.connect(); added hack so that if 'CDUP' is not understood, 'CWD ..' is tried. * ftplib.py: second method called init() should have been called connect(); if __init__ sees more than one argument, it will also try to login().
1 parent 590baa4 commit ae3b3a3

7 files changed

Lines changed: 112 additions & 32 deletions

File tree

Lib/UserDict.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# A more or less complete user-defined wrapper around dictionary objects
2+
3+
class UserDict:
4+
def __init__(self): self.data = {}
5+
def __repr__(self): return repr(self.data)
6+
def __cmp__(self, dict):
7+
if type(dict) == type(self.data):
8+
return cmp(self.data, dict)
9+
else:
10+
return cmp(self.data, dict.data)
11+
def __len__(self): return len(self.data)
12+
def __getitem__(self, key): return self.data[key]
13+
def __setitem__(self, key, item): self.data[key] = item
14+
def __delitem__(self, key): del self.data[key]
15+
def keys(self): return self.data.keys()
16+
def items(self): return self.data.items()
17+
def values(self): return self.data.values()
18+
def has_key(self, key): return self.data.has_key(key)

Lib/UserList.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# A more or less complete user-defined wrapper around list objects
2+
3+
class UserList:
4+
def __init__(self, *args):
5+
if len(args) > 1: raise TypeError, 'too many args'
6+
self.data = []
7+
if args:
8+
list = args[0]
9+
if type(list) == type(self.data):
10+
self.data[:] = list
11+
else:
12+
self.data[:] = list.data[:]
13+
def __repr__(self): return repr(self.data)
14+
def __cmp__(self, list):
15+
if type(list) == type(self.data):
16+
return cmp(self.data, list)
17+
else:
18+
return cmp(self.data, list.data)
19+
def __len__(self): return len(self.data)
20+
def __getitem__(self, i): return self.data[i]
21+
def __setitem__(self, i, item): self.data[i] = item
22+
def __delitem__(self, i): del self.data[i]
23+
def __getslice__(self, i, j):
24+
userlist = UserList()
25+
userlist.data[:] = self.data[i:j]
26+
return userlist
27+
def __setslice__(self, i, j, list):
28+
if type(list) == type(self.data):
29+
self.data[i:j] = list
30+
else:
31+
self.data[i:j] = list.data
32+
def __delslice__(self, i, j): del self.data[i:j]
33+
def append(self, item): self.data.append(item)
34+
def insert(self, i, item): self.data.insert(i, item)
35+
def remove(self, item): self.data.remove(item)
36+
def count(self, item): return self.data.count(item)
37+
def index(self, item): return self.data.index(item)
38+
def reverse(self): self.data.reverse()
39+
def sort(self, *args): apply(self.data.sort, args)

Lib/ftplib.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# An FTP client class. Based on RFC 959: File Transfer Protocol
22
# (FTP), by J. Postel and J. Reynolds
33

4+
# Changes and improvements suggested by Steve Majewski
5+
46

57
# Example:
68
#
@@ -70,6 +72,8 @@ class FTP:
7072

7173
# New initialization method (called by class instantiation)
7274
# Initialize host to localhost, port to standard ftp port
75+
# Optional arguments are host (for connect()),
76+
# and user, passwd, acct (for login())
7377
def __init__(self, *args):
7478
# Initialize the instance to something mostly harmless
7579
self.debugging = 0
@@ -79,7 +83,9 @@ def __init__(self, *args):
7983
self.file = None
8084
self.welcome = None
8185
if args:
82-
apply(self.connect, args)
86+
self.connect(args[0])
87+
if args[1:]:
88+
apply(self.login, args[1:])
8389

8490
# Old init method (explicitly called by caller)
8591
def init(self, *args):
@@ -89,15 +95,14 @@ def init(self, *args):
8995
# Connect to host. Arguments:
9096
# - host: hostname to connect to (default previous host)
9197
# - port: port to connect to (default previous port)
92-
def init(self, *args):
98+
def connect(self, *args):
9399
if args: self.host = args[0]
94100
if args[1:]: self.port = args[1]
95101
if args[2:]: raise TypeError, 'too many args'
96102
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
97103
self.sock.connect(self.host, self.port)
98104
self.file = self.sock.makefile('r')
99105
self.welcome = self.getresp()
100-
return self
101106

102107
# Get the welcome message from the server
103108
# (this is read and squirreled away by init())
@@ -276,8 +281,14 @@ def retrbinary(self, cmd, callback, blocksize):
276281
# Retrieve data in line mode.
277282
# The argument is a RETR or LIST command.
278283
# The callback function is called for each line, with trailing
279-
# CRLF stripped. This creates a new port for you
280-
def retrlines(self, cmd, callback):
284+
# CRLF stripped. This creates a new port for you.
285+
# print_lines is the default callback
286+
def retrlines(self, cmd, args):
287+
callback = None
288+
if args:
289+
callback = args[0]
290+
if args[1:]: raise TypeError, 'too many args'
291+
if not callback: callback = print_line
281292
resp = self.sendcmd('TYPE A')
282293
conn = self.transfercmd(cmd)
283294
fp = conn.makefile('r')
@@ -328,6 +339,20 @@ def nlst(self, *args):
328339
self.retrlines(cmd, files.append)
329340
return files
330341

342+
# List a directory in long form. By default list current directory
343+
# to stdout. Optional last argument is callback function;
344+
# all non-empty arguments before it are concatenated to the
345+
# LIST command. (This *should* only be used for a pathname.)
346+
def dir(self, *args):
347+
cmd = 'LIST'
348+
func = None
349+
if args[-1:] and type(args[-1]) != type(''):
350+
args, func = args[:-1], args[-1]
351+
for arg in args:
352+
if arg:
353+
cmd = cmd + (' ' + arg)
354+
self.retrlines(cmd, func)
355+
331356
# Rename a file
332357
def rename(self, fromname, toname):
333358
resp = self.sendcmd('RNFR ' + fromname)
@@ -338,9 +363,13 @@ def rename(self, fromname, toname):
338363
# Change to a directory
339364
def cwd(self, dirname):
340365
if dirname == '..':
341-
cmd = 'CDUP'
342-
else:
343-
cmd = 'CWD ' + dirname
366+
try:
367+
self.voidcmd('CDUP')
368+
return
369+
except error_perm, msg:
370+
if msg[:3] != '500':
371+
raise error_perm, msg
372+
cmd = 'CWD ' + dirname
344373
self.voidcmd(cmd)
345374

346375
# Retrieve the size of a file
@@ -390,6 +419,10 @@ def parse257(resp):
390419
dirname = dirname + c
391420
return dirname
392421

422+
# Default retrlines callback to print a line
423+
def print_line(line):
424+
print line
425+
393426

394427
# Test program.
395428
# Usage: ftp [-d] host [-l[dir]] [-d[dir]] [file] ...
@@ -409,12 +442,9 @@ def test():
409442
ftp = FTP(host)
410443
ftp.set_debuglevel(debugging)
411444
ftp.login()
412-
def writeln(line): print line
413445
for file in sys.argv[2:]:
414446
if file[:2] == '-l':
415-
cmd = 'LIST'
416-
if file[2:]: cmd = cmd + ' ' + file[2:]
417-
ftp.retrlines(cmd, writeln)
447+
ftp.dir(file[2:])
418448
elif file[:2] == '-d':
419449
cmd = 'CWD'
420450
if file[2:]: cmd = cmd + ' ' + file[2:]

Lib/test/test_b1.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ def f3(a1, a2, a3):
9191
unlink(TESTFN)
9292

9393
print 'filter'
94-
if filter("c: 'a' <= c <= 'z'", 'Hello World') <> 'elloorld':
94+
if filter(lambda c: 'a' <= c <= 'z', 'Hello World') <> 'elloorld':
9595
raise TestFailed, 'filter (filter a string)'
9696
if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) <> [1, 'hello', [3], 9]:
9797
raise TestFailed, 'filter (remove false values)'
98-
if filter('x: x > 0', [1, -3, 9, 0, 2]) <> [1, 9, 2]:
98+
if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) <> [1, 9, 2]:
9999
raise TestFailed, 'filter (keep positives)'
100100

101101
print 'float'
@@ -120,11 +120,6 @@ def f3(a1, a2, a3):
120120
if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
121121
if int(314L) <> 314: raise TestFailed, 'int(314L)'
122122

123-
print 'lambda'
124-
binary_plus = lambda('x, y: x+y')
125-
if binary_plus(2, 10) <> 12:
126-
raise TestFailed, 'binary_plus(2, 10)'
127-
128123
print 'len'
129124
if len('123') <> 3: raise TestFailed, 'len(\'123\')'
130125
if len(()) <> 0: raise TestFailed, 'len(())'
@@ -146,13 +141,13 @@ def f3(a1, a2, a3):
146141
raise TestFailed, 'map(None, \'abcd\', \'efg\')'
147142
if map(None, range(10)) <> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
148143
raise TestFailed, 'map(None, range(10))'
149-
if map('x: x*x', range(1,4)) <> [1, 4, 9]:
150-
raise TestFailed, 'map(\'x: x*x\', range(1,4))'
144+
if map(lambda x: x*x, range(1,4)) <> [1, 4, 9]:
145+
raise TestFailed, 'map(lambda x: x*x, range(1,4))'
151146
from math import sqrt
152-
if map('x: map(sqrt,x)', [[16, 4], [81, 9]]) <> [[4.0, 2.0], [9.0, 3.0]]:
153-
raise TestFailed, map('x: map(sqrt,x)', [[16, 4], [81, 9]])
154-
if map('x,y: x+y', [1,3,2], [9,1,4]) <> [10, 4, 6]:
155-
raise TestFailed, 'map(\'x,y: x+y\', [1,3,2], [9,1,4])'
147+
if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) <> [[4.0, 2.0], [9.0, 3.0]]:
148+
raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
149+
if map(lambda x, y: x+y, [1,3,2], [9,1,4]) <> [10, 4, 6]:
150+
raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
156151
def plus(*v):
157152
accu = 0
158153
for i in v: accu = accu + i

Lib/test/test_b2.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,14 @@
112112
fp.close()
113113

114114
print 'reduce'
115-
if reduce('x,y:x+y', ['a', 'b', 'c'], '') <> 'abc':
115+
if reduce(lambda x, y: x+y, ['a', 'b', 'c'], '') <> 'abc':
116116
raise TestFailed, 'reduce(): implode a string'
117-
if reduce('x,y:x+y', [['a', 'c'], [], ['d', 'w']], []) <> ['a','c','d','w']:
117+
if reduce(lambda x, y: x+y,
118+
[['a', 'c'], [], ['d', 'w']], []) <> ['a','c','d','w']:
118119
raise TestFailed, 'reduce(): append'
119-
if reduce('x,y: x*y', range(2,8), 1) <> 5040:
120+
if reduce(lambda x, y: x*y, range(2,8), 1) <> 5040:
120121
raise TestFailed, 'reduce(): compute 7!'
121-
if reduce('x,y:x*y', range(2,21), 1L) <> 2432902008176640000L:
122+
if reduce(lambda x, y: x*y, range(2,21), 1L) <> 2432902008176640000L:
122123
raise TestFailed, 'reduce(): compute 20!, use long'
123124

124125
print 'reload'

Lib/test/test_select.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Testing select module
22

3-
from test_support import *
4-
53
def test():
64
import select
75
import os

Lib/test/testall.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ float
7979
getattr
8080
hex
8181
int
82-
lambda
8382
len
8483
long
8584
map

0 commit comments

Comments
 (0)