Skip to content

Commit 286cb93

Browse files
author
Kevin D Smith
committed
flake8 cleanup
1 parent b163f7f commit 286cb93

File tree

14 files changed

+247
-206
lines changed

14 files changed

+247
-206
lines changed

swat/cas/actions.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -641,14 +641,14 @@ def from_reflection(cls, asname, actinfo, connection):
641641
param_names = [prm.lower() for prm in pkeys]
642642

643643
# __init__ and action methods
644-
funcargs = '**mergedefined(_self_._get_default_params(), ' + \
645-
'{%s}, kwargs)' % callargs
646-
six.exec_(('''def __init__(_self_, %s):''' +
647-
''' CASAction.__init__(_self_, %s)''')
648-
% (sig, funcargs), _globals, _locals)
649-
six.exec_(('''def __call__(_self_, %s):''' +
650-
''' return CASAction.__call__(_self_, %s)''')
651-
% (sig, funcargs), _globals, _locals)
644+
funcargs = ('**mergedefined(_self_._get_default_params(), '
645+
+ '{%s}, kwargs)') % callargs
646+
six.exec_(('''def __init__(_self_, %s):'''
647+
+ ''' CASAction.__init__(_self_, %s)''')
648+
% (sig, funcargs), _globals, _locals)
649+
six.exec_(('''def __call__(_self_, %s):'''
650+
+ ''' return CASAction.__call__(_self_, %s)''')
651+
% (sig, funcargs), _globals, _locals)
652652

653653
# Generate documentation
654654
all_params = []

swat/cas/connection.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def __init__(self, hostname=None, port=None, username=None, password=None,
227227
'exist: %s' % ', '.join(authinfo))
228228
else:
229229
raise OSError('The specified authinfo file does not '
230-
'exist: %s' % authinfo)
230+
'exist: %s' % authinfo)
231231

232232
# If a prototype exists, use it for the connection config
233233
prototype = kwargs.get('prototype')
@@ -243,14 +243,14 @@ def __init__(self, hostname=None, port=None, username=None, password=None,
243243
port = cf.get_option('cas.port')
244244

245245
# Detect protocol
246-
if (isinstance(hostname, items_types) and
247-
(hostname[0].startswith('http:') or
248-
hostname[0].startswith('https:'))):
246+
if (isinstance(hostname, items_types)
247+
and (hostname[0].startswith('http:')
248+
or hostname[0].startswith('https:'))):
249249
protocol = hostname[0].split(':', 1)[0]
250250

251-
elif (isinstance(hostname, six.string_types) and
252-
(hostname.startswith('http:') or
253-
hostname.startswith('https:'))):
251+
elif (isinstance(hostname, six.string_types)
252+
and (hostname.startswith('http:')
253+
or hostname.startswith('https:'))):
254254
protocol = hostname.split(':', 1)[0]
255255

256256
else:
@@ -279,8 +279,8 @@ def __init__(self, hostname=None, port=None, username=None, password=None,
279279
# Create a new connection
280280
else:
281281
# Set up hostnames
282-
if (protocol not in ['http', 'https'] and
283-
isinstance(hostname, items_types)):
282+
if (protocol not in ['http', 'https']
283+
and isinstance(hostname, items_types)):
284284
hostname = a2n(' '.join(a2n(x) for x in hostname if x))
285285
elif isinstance(hostname, six.string_types):
286286
hostname = a2n(hostname)
@@ -390,7 +390,9 @@ def _id_generator():
390390
num = num + 1
391391
self._id_generator = _id_generator()
392392

393-
self.server_type, self.server_version, self.server_features = self._get_server_features()
393+
(self.server_type,
394+
self.server_version,
395+
self.server_features) = self._get_server_features()
394396

395397
def _gen_id(self):
396398
''' Generate an ID unique to the session '''
@@ -455,17 +457,17 @@ def _detect_protocol(self, hostname, port, protocol=None):
455457
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
456458
sock.settimeout(10)
457459
sock.connect((host, port))
458-
sock.send(('GET /cas HTTP/1.1\r\n' +
459-
('Host: %s\r\n' % host) +
460-
'Connection: close\r\n' +
461-
'User-Agent: Python-SWAT\r\n' +
462-
'Cache-Control: no-cache\r\n\r\n').encode('utf8'))
460+
sock.send(('GET /cas HTTP/1.1\r\n'
461+
+ ('Host: %s\r\n' % host)
462+
+ 'Connection: close\r\n'
463+
+ 'User-Agent: Python-SWAT\r\n'
464+
+ 'Cache-Control: no-cache\r\n\r\n').encode('utf8'))
463465

464466
if sock.recv(4).decode('utf-8').lower() == 'http':
465467
protocol = ptype
466468
break
467469

468-
except Exception as err:
470+
except Exception:
469471
pass
470472

471473
finally:
@@ -1096,8 +1098,7 @@ def _merge_param_args(self, parmlist, kwargs, action=None):
10961098
if isinstance(kwargs['table'], CASTable):
10971099
kwargs['table'] = kwargs['table'].to_table_params()
10981100
if isinstance(kwargs['table'], dict):
1099-
if caslib and 'caslib' not in kwargs and \
1100-
kwargs['table'].get('caslib'):
1101+
if caslib and 'caslib' not in kwargs and kwargs['table'].get('caslib'):
11011102
kwargs['caslib'] = kwargs['table']['caslib']
11021103
kwargs['table'] = kwargs['table']['name']
11031104

@@ -1355,8 +1356,10 @@ def upload(self, data, importoptions=None, casout=None, **kwargs):
13551356
delete = True
13561357
filename = tmp.name
13571358
name = os.path.splitext(os.path.basename(filename))[0]
1358-
data.to_csv(filename, encoding='utf-8', index=False, sep=a2n(',', 'utf-8'),
1359-
decimal=a2n('.', 'utf-8'), line_terminator=a2n('\r\n', 'utf-8'))
1359+
data.to_csv(filename, encoding='utf-8',
1360+
index=False, sep=a2n(',', 'utf-8'),
1361+
decimal=a2n('.', 'utf-8'),
1362+
line_terminator=a2n('\r\n', 'utf-8'))
13601363
df_dtypes = self._extract_dtypes(data)
13611364
importoptions['locale'] = 'EN-us'
13621365

@@ -1423,7 +1426,7 @@ def upload(self, data, importoptions=None, casout=None, **kwargs):
14231426
if delete:
14241427
try:
14251428
os.remove(filename)
1426-
except:
1429+
except Exception:
14271430
pass
14281431

14291432
return self._get_results([(CASResponse(resp, connection=self), self)])
@@ -1867,12 +1870,12 @@ def __getattr__(self, name, atype=None):
18671870
name = name.lower()
18681871

18691872
# Check cache for actionset and action classes
1870-
if (atype in [None, 'actionset'] and name in self._actionset_classes and
1871-
self._actionset_classes[name] is not None):
1873+
if (atype in [None, 'actionset'] and name in self._actionset_classes
1874+
and self._actionset_classes[name] is not None):
18721875
return self._actionset_classes[name]()
18731876

1874-
if (atype in [None, 'action'] and name in self._action_classes and
1875-
self._action_classes[name] is not None):
1877+
if (atype in [None, 'action'] and name in self._action_classes
1878+
and self._action_classes[name] is not None):
18761879
if class_requested:
18771880
return self._action_classes[name]
18781881
return self._action_classes[name]()
@@ -3116,7 +3119,7 @@ def path_to_caslib(self, path, name=None, **kwargs):
31163119
31173120
'''
31183121
if not name:
3119-
name = 'Caslib_%x' % random.randint(0,1e9)
3122+
name = 'Caslib_%x' % random.randint(0, 1e9)
31203123

31213124
activeonadd_key = None
31223125
subdirectories_key = None
@@ -3168,7 +3171,7 @@ def path_to_caslib(self, path, name=None, **kwargs):
31683171
elif normpath.startswith(item):
31693172
if bool(subdirs) != bool(kwargs[subdirectories_key]):
31703173
raise SWATError('caslib exists, but subdirectories flag differs')
3171-
return libname, path[len(item)+1:]
3174+
return libname, path[len(item) + 1:]
31723175

31733176
out = self.retrieve('table.addcaslib', _messagelevel='error',
31743177
name=name, path=path, **kwargs)

swat/cas/datamsghandlers.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,10 @@ def get(arr, idx, default=0):
328328
datetime.datetime)):
329329
value = python2cas_datetime(value)
330330
if vrtype == 'CHAR' or vtype in ['VARCHAR', 'CHAR', 'BINARY', 'VARBINARY']:
331-
if vtype in ['BINARY', 'VARBINARY'] and \
332-
hasattr(self._sw_databuffer, 'setBinaryFromBase64'):
331+
if vtype in ['BINARY', 'VARBINARY'] \
332+
and hasattr(self._sw_databuffer, 'setBinaryFromBase64'):
333333
if isinstance(value, (binary_types, text_types)):
334-
errorcheck(self._sw_databuffer\
334+
errorcheck(self._sw_databuffer
335335
.setBinaryFromBase64(row, offset,
336336
a2n(base64.b64encode(
337337
a2b(transformer(value))))),
@@ -353,9 +353,9 @@ def get(arr, idx, default=0):
353353
if pd.isnull(value):
354354
value = get_option('cas.missing.%s' % vtype.lower())
355355
warnings.warn(('Missing value found in 32-bit '
356-
'integer-based column \'%s\'.\n' % v['name']) +
357-
('Substituting cas.missing.%s option value (%s).' %
358-
(vtype.lower(), value)),
356+
+ 'integer-based column \'%s\'.\n' % v['name'])
357+
+ ('Substituting cas.missing.%s option value (%s).' %
358+
(vtype.lower(), value)),
359359
RuntimeWarning)
360360
if length > 4:
361361
for i in range(int64(length / 4)):
@@ -370,9 +370,9 @@ def get(arr, idx, default=0):
370370
if pd.isnull(value):
371371
value = get_option('cas.missing.%s' % vtype.lower())
372372
warnings.warn(('Missing value found in 64-bit '
373-
'integer-based column \'%s\'.\n' % v['name']) +
374-
('Substituting cas.missing.%s option value (%s).' %
375-
(vtype.lower(), value)),
373+
+ 'integer-based column \'%s\'.\n' % v['name'])
374+
+ ('Substituting cas.missing.%s option value (%s).' %
375+
(vtype.lower(), value)),
376376
RuntimeWarning)
377377
if length > 8:
378378
for i in range(int64(length / 8)):

swat/cas/rest/connection.py

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,15 @@ def _normalize_list(items):
161161

162162

163163
class SSLContextAdapter(requests.adapters.HTTPAdapter):
164-
"""HTTPAdapter that uses the default SSL context on the machine."""
164+
''' HTTPAdapter that uses the default SSL context on the machine '''
165165

166-
def init_poolmanager(self, connections, maxsize, block=requests.adapters.DEFAULT_POOLBLOCK, **pool_kwargs):
166+
def init_poolmanager(self, connections, maxsize,
167+
block=requests.adapters.DEFAULT_POOLBLOCK, **pool_kwargs):
167168
context = ssl.create_default_context()
168169
pool_kwargs['ssl_context'] = context
169-
return super(SSLContextAdapter, self).init_poolmanager(connections, maxsize, block, **pool_kwargs)
170+
return super(SSLContextAdapter, self).init_poolmanager(connections,
171+
maxsize, block,
172+
**pool_kwargs)
170173

171174

172175
class REST_CASConnection(object):
@@ -296,7 +299,13 @@ def __init__(self, hostname, port, username, password, soptions, error):
296299
if get_option('cas.debug.responses'):
297300
_print_response(res.text)
298301

299-
out = json.loads(a2u(res.text, 'utf-8'))
302+
try:
303+
txt = a2u(res.text, 'utf-8')
304+
out = json.loads(txt, strict=False)
305+
except Exception:
306+
sys.stderr.write(txt)
307+
sys.stderr.write('\n')
308+
raise
300309

301310
if out.get('error', None):
302311
if out.get('details', None):
@@ -318,7 +327,13 @@ def __init__(self, hostname, port, username, password, soptions, error):
318327
if get_option('cas.debug.responses'):
319328
_print_response(res.text)
320329

321-
out = json.loads(a2u(res.text, 'utf-8'))
330+
try:
331+
txt = a2u(res.text, 'utf-8')
332+
out = json.loads(txt, strict=False)
333+
except Exception:
334+
sys.stderr.write(txt)
335+
sys.stderr.write('\n')
336+
raise
322337

323338
if out.get('error', None):
324339
if out.get('details', None):
@@ -337,7 +352,7 @@ def __init__(self, hostname, port, username, password, soptions, error):
337352
self._results.clear()
338353
break
339354

340-
except requests.ConnectionError as exc:
355+
except requests.ConnectionError:
341356
self._set_next_connection()
342357

343358
except Exception as exc:
@@ -390,6 +405,7 @@ def invoke(self, action_name, kwargs):
390405

391406
result_id = None
392407

408+
txt = ''
393409
while True:
394410
try:
395411
url = urllib.parse.urljoin(self._current_baseurl,
@@ -407,7 +423,7 @@ def invoke(self, action_name, kwargs):
407423
res = res.text
408424
break
409425

410-
except requests.ConnectionError as exc:
426+
except requests.ConnectionError:
411427
self._set_next_connection()
412428

413429
# Get ID of results
@@ -430,7 +446,14 @@ def invoke(self, action_name, kwargs):
430446
if get_option('cas.debug.responses'):
431447
_print_response(res.text)
432448

433-
out = json.loads(a2u(res.text, 'utf-8'), strict=False)
449+
try:
450+
txt = a2u(res.text, 'utf-8')
451+
out = json.loads(txt, strict=False)
452+
except Exception:
453+
sys.stderr.write(txt)
454+
sys.stderr.write('\n')
455+
raise
456+
434457
result_id = out['results']['Queued Results']['rows'][0][0]
435458

436459
# Setup retrieval of results from ID
@@ -445,7 +468,14 @@ def invoke(self, action_name, kwargs):
445468
raise SWATError(str(exc))
446469

447470
try:
448-
self._results = json.loads(a2u(res, 'utf-8'), strict=False)
471+
txt = a2u(res, 'utf-8')
472+
self._results = json.loads(txt, strict=False)
473+
except Exception:
474+
sys.stderr.write(txt)
475+
sys.stderr.write('\n')
476+
raise
477+
478+
try:
449479
if self._results.get('disposition', None) is None:
450480
if self._results.get('error'):
451481
raise SWATError(self._results['error'])
@@ -492,8 +522,7 @@ def setZeroIndexedParameters(self):
492522

493523
def copy(self):
494524
''' Copy the connection object '''
495-
username, password = base64.b64decode(
496-
self._auth.split(b' ', 1)[-1]).split(b':', 1)
525+
username, password = base64.b64decode(self._auth.split(b' ', 1)[-1]).split(b':', 1)
497526
return type(self)(self._orig_hostname, self._orig_port,
498527
a2u(username), a2u(password),
499528
self._soptions,
@@ -562,7 +591,7 @@ def upload(self, file_name, params):
562591
res = res.text
563592
break
564593

565-
except requests.ConnectionError as exc:
594+
except requests.ConnectionError:
566595
self._set_next_connection()
567596

568597
except Exception as exc:
@@ -572,7 +601,14 @@ def upload(self, file_name, params):
572601
del self._req_sess.headers['JSON-Parameters']
573602

574603
try:
575-
out = json.loads(a2u(res, 'utf-8'), strict=False)
604+
txt = a2u(res, 'utf-8')
605+
out = json.loads(txt, strict=False)
606+
except Exception:
607+
sys.stderr.write(txt)
608+
sys.stderr.write('\n')
609+
raise
610+
611+
try:
576612
if out.get('disposition', None) is None:
577613
if out.get('error'):
578614
raise SWATError(self._results['error'])

swat/cas/rest/table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,10 @@ def toTuples(self, errors, cas2python_datetime, cas2python_date,
280280
elif isinstance(item, dict):
281281
try:
282282
outrow.append(base64.b64decode(item['data']))
283-
except:
283+
except Exception:
284284
try:
285285
outrow.append(base64.b64decode(item['data'] + '='))
286-
except:
286+
except Exception:
287287
outrow.append(base64.b64decode(item['data'] + '=='))
288288
# Check for datetime, date, time
289289
elif dtype == 'datetime':

0 commit comments

Comments
 (0)