Skip to content

Commit 723ae70

Browse files
committed
inital flake8 cleanups
Replaced some calls to re.compile with module level variable. Renamed some variables.
1 parent 67cc910 commit 723ae70

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

roundup/hyperdb.py

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
__docformat__ = 'restructuredtext'
2222

2323
# standard python modules
24-
import os, re, shutil, sys, weakref
25-
import traceback
2624
import logging
25+
import os
26+
import re
27+
import shutil
28+
import sys
29+
import traceback
30+
import weakref
2731

2832
# roundup modules
2933
from . import date, password
@@ -58,13 +62,13 @@ def get_default_value(self):
5862
"""The default value when creating a new instance of this property."""
5963
return self.__default_value
6064

61-
def register (self, cls, propname):
65+
def register(self, cls, propname):
6266
"""Register myself to the class of which we are a property
6367
the given propname is the name we have in our class.
6468
"""
6569
assert not getattr(self, 'cls', None)
6670
self.name = propname
67-
self.cls = cls
71+
self.cls = cls
6872

6973
def sort_repr(self, cls, val, name):
7074
"""Representation used for sorting. This should be a python
@@ -281,23 +285,23 @@ def __init__(self, classname, do_journal='yes', required=False,
281285
default_value=[], quiet=quiet,
282286
try_id_parsing=try_id_parsing,
283287
rev_multilink=rev_multilink)
284-
self.rev_property = rev_property
288+
self.rev_property = rev_property
285289
self.rev_classname = None
286-
self.rev_propname = None
287-
self.table_name = None # computed in 'register' below
288-
self.linkid_name = 'linkid'
289-
self.nodeid_name = 'nodeid'
290+
self.rev_propname = None
291+
self.table_name = None # computed in 'register' below
292+
self.linkid_name = 'linkid'
293+
self.nodeid_name = 'nodeid'
290294
if self.rev_property:
291295
# Do not allow updates if this is a reverse multilink
292296
self.computed = True
293297
self.rev_classname = rev_property.cls.classname
294-
self.rev_propname = rev_property.name
298+
self.rev_propname = rev_property.name
295299
if isinstance(self.rev_property, Link):
296-
self.table_name = '_' + self.rev_classname
300+
self.table_name = '_' + self.rev_classname
297301
self.linkid_name = 'id'
298302
self.nodeid_name = '_' + self.rev_propname
299303
else:
300-
self.table_name = self.rev_classname + '_' + self.rev_propname
304+
self.table_name = self.rev_classname + '_' + self.rev_propname
301305
self.linkid_name = 'nodeid'
302306
self.nodeid_name = 'linkid'
303307

@@ -326,7 +330,7 @@ def from_raw(self, value, db, klass, propname, itemid, **kw):
326330
item = item.strip()
327331

328332
# skip blanks
329-
if not item: continue
333+
if not item: continue # noqa: E701
330334

331335
# handle +/-
332336
remove = 0
@@ -438,8 +442,11 @@ class DesignatorError(ValueError):
438442
pass
439443

440444

445+
dre = re.compile(r'^([A-Za-z](?:[A-Za-z_0-9]*[A-Za-z_]+)?)(\d+)$')
446+
447+
441448
def splitDesignator(designator,
442-
dre=re.compile(r'^([A-Za-z](?:[A-Za-z_0-9]*[A-Za-z_]+)?)(\d+)$')):
449+
dre=dre):
443450
""" Take a foo123 and return ('foo', 123)
444451
"""
445452
m = dre.match(designator)
@@ -520,7 +527,7 @@ def append(self, name, need_for='search', retr=False):
520527
pt = self.propdict[name]
521528
pt.need_for[need_for] = True
522529
# For now we do not recursively retrieve Link properties
523-
#if retr and isinstance(pt.propclass, Link):
530+
# if retr and isinstance(pt.propclass, Link):
524531
# pt.append_retr_props()
525532
return pt
526533
propclass = self.props[name]
@@ -540,7 +547,7 @@ def append(self, name, need_for='search', retr=False):
540547
self.children.append(child)
541548
self.propdict[name] = child
542549
# For now we do not recursively retrieve Link properties
543-
#if retr and isinstance(child.propclass, Link):
550+
# if retr and isinstance(child.propclass, Link):
544551
# child.append_retr_props()
545552
return child
546553

@@ -590,7 +597,7 @@ def search(self, search_matches=None, sort=True, retired=False):
590597
x = [c for c in p.children if 'search' in c.need_for]
591598
if x:
592599
p.search(sort=False)
593-
if getattr(p.propclass,'rev_property',None):
600+
if getattr(p.propclass, 'rev_property', None):
594601
pn = p.propclass.rev_property.name
595602
cl = p.propclass.rev_property.cls
596603
if not isinstance(p.val, type([])):
@@ -612,10 +619,10 @@ def search(self, search_matches=None, sort=True, retired=False):
612619
items |= s1.difference(s2)
613620
if isinstance(p.propclass.rev_property, Link):
614621
items |= set(cl.get(x, pn) for x in pval
615-
if not cl.is_retired(x))
622+
if not cl.is_retired(x))
616623
else:
617624
items |= set().union(*(cl.get(x, pn) for x in pval
618-
if not cl.is_retired(x)))
625+
if not cl.is_retired(x)))
619626
else:
620627
# Expression: materialize rev multilinks and run
621628
# expression on them
@@ -656,14 +663,14 @@ def search(self, search_matches=None, sort=True, retired=False):
656663
exact_match_spec[p.name] = exact
657664
if subst:
658665
filterspec[p.name] = subst
659-
elif not exact: # don't set if we have exact criteria
666+
elif not exact: # don't set if we have exact criteria
660667
if p.has_result:
661668
# A subquery already has found nothing. So
662669
# it doesn't make sense to search further.
663670
self.set_val([], force=True)
664671
return self.val
665672
else:
666-
filterspec[p.name] = ['-1'] # no match was found
673+
filterspec[p.name] = ['-1'] # no match was found
667674
else:
668675
assert not isinstance(p.val, Exact_Match)
669676
filterspec[p.name] = p.val
@@ -810,7 +817,7 @@ def set_val(self, val, force=False, result=True):
810817
if is_expression:
811818
# Tag on the ORed values with an AND
812819
l = val
813-
for i in range(len(val)-1):
820+
for _i in range(len(val)-1):
814821
l.append('-4')
815822
l.append('-3')
816823
self.val = self.val + l
@@ -958,14 +965,14 @@ def post_init(self):
958965
# from .keys()
959966
for p in list(cl.properties.keys()):
960967
prop = cl.properties[p]
961-
if not isinstance (prop, (Link, Multilink)):
968+
if not isinstance(prop, (Link, Multilink)):
962969
continue
963970
if prop.rev_multilink:
964971
linkcls = self.getclass(prop.classname)
965972
if prop.rev_multilink in linkcls.properties:
966973
if not done:
967974
raise ValueError(
968-
"%s already a property of class %s"%
975+
"%s already a property of class %s" %
969976
(prop.rev_multilink, linkcls.classname))
970977
else:
971978
linkcls.properties[prop.rev_multilink] = Multilink(
@@ -1105,6 +1112,7 @@ def close(self):
11051112
"""
11061113
raise NotImplementedError
11071114

1115+
11081116
def iter_roles(roles):
11091117
''' handle the text processing of turning the roles list
11101118
into something python can use more easily
@@ -1173,7 +1181,7 @@ def __repr__(self):
11731181
where self.classname isn't known yet if the error
11741182
occurs during schema parsing.
11751183
"""
1176-
cn = getattr (self, 'classname', 'Unknown')
1184+
cn = getattr(self, 'classname', 'Unknown')
11771185
return '<hyperdb.Class "%s">' % cn
11781186

11791187
# Editing nodes:
@@ -1564,7 +1572,7 @@ def _filter(self, search_matches, filterspec, sort=(None, None),
15641572
"""
15651573
raise NotImplementedError
15661574

1567-
def _proptree(self, filterspec, exact_match_spec={}, sortattr=[],
1575+
def _proptree(self, filterspec, exact_match_spec=None, sortattr=None,
15681576
retr=False):
15691577
"""Build a tree of all transitive properties in the given
15701578
exact_match_spec/filterspec.
@@ -1573,6 +1581,10 @@ def _proptree(self, filterspec, exact_match_spec={}, sortattr=[],
15731581
"""
15741582
if filterspec is None:
15751583
filterspec = {}
1584+
if exact_match_spec is None:
1585+
exact_match_spec = {}
1586+
if sortattr is None:
1587+
sortattr = []
15761588

15771589
proptree = Proptree(self.db, self, '', self.getprops(), retr=retr)
15781590
for exact, spec in enumerate((filterspec, exact_match_spec)):
@@ -1651,7 +1663,7 @@ def _sortattr(self, sort=[], group=[]):
16511663
sort last by id -- if id is not already in sortattr.
16521664
"""
16531665
if sort is None:
1654-
sort = [(None,None)]
1666+
sort = [(None, None)]
16551667
if group is None:
16561668
group = [(None, None)]
16571669

@@ -1783,12 +1795,14 @@ def getprops(self, protected=1):
17831795
"""
17841796
raise NotImplementedError
17851797

1786-
def get_required_props(self, propnames=[]):
1798+
def get_required_props(self, propnames=None):
17871799
"""Return a dict of property names mapping to property objects.
17881800
All properties that have the "required" flag set will be
17891801
returned in addition to all properties in the propnames
17901802
parameter.
17911803
"""
1804+
if propnames is None:
1805+
propnames = []
17921806
props = self.getprops(protected=False)
17931807
pdict = dict([(p, props[p]) for p in propnames])
17941808
pdict.update([(k, v) for k, v in props.items() if v.required])
@@ -1865,10 +1879,10 @@ def import_journals(self, entries):
18651879
id and then proceeded to import journals for each id."""
18661880
properties = self.getprops()
18671881
a = []
1868-
for l in entries:
1882+
for entry in entries:
18691883
# first element in sorted list is the (numeric) id
18701884
# in python2.4 and up we would use sorted with a key...
1871-
a.append((int(l[0].strip("'")), l))
1885+
a.append((int(entry[0].strip("'")), entry))
18721886
a.sort()
18731887

18741888
last = 0
@@ -1937,7 +1951,10 @@ class HyperdbValueError(ValueError):
19371951
pass
19381952

19391953

1940-
def convertLinkValue(db, propname, prop, value, idre=re.compile(r'^\d+$')):
1954+
id_regex = re.compile(r'^\d+$')
1955+
1956+
1957+
def convertLinkValue(db, propname, prop, value, idre=id_regex):
19411958
""" Convert the link value (may be id or key value) to an id value. """
19421959
linkcl = db.classes[prop.classname]
19431960
if not idre or not idre.match(value):
@@ -2072,16 +2089,16 @@ def keys(self, protected=1):
20722089
return list(self.cl.getprops(protected=protected).keys())
20732090

20742091
def values(self, protected=1):
2075-
l = []
2092+
value_list = []
20762093
for name in self.cl.getprops(protected=protected).keys():
2077-
l.append(self.cl.get(self.nodeid, name))
2078-
return l
2094+
value_list.append(self.cl.get(self.nodeid, name))
2095+
return value_list
20792096

20802097
def items(self, protected=1):
2081-
l = []
2098+
item_list = []
20822099
for name in self.cl.getprops(protected=protected).keys():
2083-
l.append((name, self.cl.get(self.nodeid, name)))
2084-
return l
2100+
item_list.append((name, self.cl.get(self.nodeid, name)))
2101+
return item_list
20852102

20862103
def has_key(self, name):
20872104
return name in self.cl.getprops()
@@ -2132,4 +2149,3 @@ def Choice(name, db, *options):
21322149
for i in range(len(options)):
21332150
cl.create(name=options[i], order=i)
21342151
return Link(name)
2135-

0 commit comments

Comments
 (0)