From 0ff9d49111497c74714d9afb9bf518b331601051 Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Sun, 16 Apr 2017 02:40:55 -0700 Subject: [PATCH] Further Pythonicity and flake8 cleanup. NFC. --- copycat/codeletMethods.py | 4 ++-- copycat/coderack.py | 14 +++++++------- copycat/correspondence.py | 9 ++++----- copycat/grouprun.py | 1 + copycat/slipnet.py | 10 +++++----- copycat/slipnode.py | 13 ++++--------- copycat/temperature.py | 1 + copycat/workspaceFormulas.py | 1 + 8 files changed, 25 insertions(+), 28 deletions(-) diff --git a/copycat/codeletMethods.py b/copycat/codeletMethods.py index 11df779..ba78098 100644 --- a/copycat/codeletMethods.py +++ b/copycat/codeletMethods.py @@ -710,7 +710,7 @@ def group_builder(codelet): incompatible.break_the_structure() # create new bonds group.bondList = [] - for i in range(1, len(group.objectList)): + for i in xrange(1, len(group.objectList)): #print 803 object1 = group.objectList[i - 1] object2 = group.objectList[i] @@ -761,7 +761,7 @@ def __getCutOff(density): distribution = [1.0, 1.0, 1.0, 2.0, 5.0, 150.0, 5.0, 2.0, 1.0, 1.0] stop = sum(distribution) * random.random() total = 0.0 - for i in range(0, len(distribution)): + for i in xrange(len(distribution)): total += distribution[i] if total >= stop: return i + 1 diff --git a/copycat/coderack.py b/copycat/coderack.py index 9445169..92dac8f 100644 --- a/copycat/coderack.py +++ b/copycat/coderack.py @@ -71,7 +71,7 @@ class CodeRack(object): probability = workspaceFormulas.probabilityOfPosting( codeletName) howMany = workspaceFormulas.howManyToPost(codeletName) - for _ in range(0, howMany): + for _ in xrange(howMany): if random.random() >= probability: continue urgency = getUrgencyBin( @@ -108,7 +108,7 @@ class CodeRack(object): urgency = 1 if formulas.Temperature < 25.0 and 'translator' in codeletName: urgency = 5 - for _ in range(0, howMany): + for _ in xrange(howMany): if random.random() < probability: codelet = Codelet(codeletName, urgency, self.codeletsRun) self.post(codelet) @@ -219,7 +219,7 @@ class CodeRack(object): urgencies += [urgency] threshold = random.random() * sum(urgencies) sumOfUrgencies = 0.0 - for i in range(0, len(self.codelets)): + for i in xrange(len(self.codelets)): sumOfUrgencies += urgencies[i] if sumOfUrgencies > threshold: return self.codelets[i] @@ -227,11 +227,9 @@ class CodeRack(object): def postInitialCodelets(self): for name in self.initialCodeletNames: - for _ in range(0, workspaceFormulas.numberOfObjects()): + for _ in xrange(2 * workspaceFormulas.numberOfObjects()): codelet = Codelet(name, 1, self.codeletsRun) self.post(codelet) - codelet2 = Codelet(name, 1, self.codeletsRun) - self.post(codelet2) def tryRun(self, newCodelet): if self.removeTerracedScan: @@ -267,7 +265,8 @@ class CodeRack(object): 'bottom-up-correspondence-scout', 'important-object-correspondence-scout', 'correspondence-strength-tester', - 'correspondence-builder',) + 'correspondence-builder', + ) self.methods = {} for codeletName in knownCodeletNames: methodName = re.sub('[ -]', '_', codeletName) @@ -352,4 +351,5 @@ class CodeRack(object): except AssertionError: pass + coderack = CodeRack() diff --git a/copycat/correspondence.py b/copycat/correspondence.py index 7e592c9..15309f7 100644 --- a/copycat/correspondence.py +++ b/copycat/correspondence.py @@ -109,9 +109,8 @@ class Correspondence(WorkspaceStructure): if self.objectFromTarget.spansString(): return 100.0 total = sum(c.totalStrength for c in workspace.correspondences() - if self.supporting(c)) - total = min(total, 100.0) - return total + if self.supporting(c)) + return min(total, 100.0) def updateInternalStrength(self): """A function of how many concept mappings there are @@ -144,8 +143,8 @@ class Correspondence(WorkspaceStructure): def internallyCoherent(self): """Whether any pair of distinguishing mappings support each other""" mappings = self.relevantDistinguishingConceptMappings() - for i in range(0, len(mappings)): - for j in range(0, len(mappings)): + for i in xrange(len(mappings)): + for j in xrange(len(mappings)): if i != j: if mappings[i].supports(mappings[j]): return True diff --git a/copycat/grouprun.py b/copycat/grouprun.py index 2b37fd1..ea4aec1 100644 --- a/copycat/grouprun.py +++ b/copycat/grouprun.py @@ -12,4 +12,5 @@ class GroupRun(object): self.modified = workspace.modified self.target = workspace.target + groupRun = GroupRun() diff --git a/copycat/slipnet.py b/copycat/slipnet.py index 2f892a1..0da9916 100644 --- a/copycat/slipnet.py +++ b/copycat/slipnet.py @@ -10,9 +10,8 @@ def distinguishingDescriptor(descriptor): return False if descriptor == slipnet.group: return False - for number in slipnet.numbers: - if number == descriptor: - return False + if descriptor in slipnet.numbers: + return False return True @@ -43,8 +42,8 @@ class SlipNet(object): self.numberOfUpdates = 0 for node in self.slipnodes: node.reset() - if node in self.initiallyClampedSlipnodes: - node.clampHigh() + for node in self.initiallyClampedSlipnodes: + node.clampHigh() def update(self): logging.debug('slipnet.update()') @@ -279,4 +278,5 @@ class SlipNet(object): self.__addNonSlipLink(item, previous, label=self.predecessor) previous = item + slipnet = SlipNet() diff --git a/copycat/slipnode.py b/copycat/slipnode.py index c457bab..ddb0a02 100644 --- a/copycat/slipnode.py +++ b/copycat/slipnode.py @@ -1,6 +1,6 @@ import math import logging -from random import random +import random def full_activation(): @@ -11,11 +11,6 @@ def jump_threshold(): return 55.0 -def points_at(links, other): - """Whether any of the links points at the other""" - return any(l.points_at(other) for l in links) - - class Slipnode(object): # pylint: disable=too-many-instance-attributes def __init__(self, name, depth, length=0.0): @@ -95,11 +90,11 @@ class Slipnode(object): def linked(self, other): """Whether the other is among the outgoing links""" - return points_at(self.outgoingLinks, other) + return any(l.points_at(other) for l in self.outgoingLinks) def slipLinked(self, other): """Whether the other is among the lateral links""" - return points_at(self.lateralSlipLinks, other) + return any(l.points_at(other) for l in self.lateralSlipLinks) def related(self, other): """Same or linked""" @@ -163,7 +158,7 @@ class Slipnode(object): if self.clamped: return False value = (self.activation / 100.0) ** 3 - return random() < value + return random.random() < value def jump(self): if self.can_jump(): diff --git a/copycat/temperature.py b/copycat/temperature.py index d144301..e4bae78 100644 --- a/copycat/temperature.py +++ b/copycat/temperature.py @@ -21,4 +21,5 @@ class Temperature(object): def log(self): logging.debug('temperature.value: %f', self.value) + temperature = Temperature() diff --git a/copycat/workspaceFormulas.py b/copycat/workspaceFormulas.py index f24c504..43330fe 100644 --- a/copycat/workspaceFormulas.py +++ b/copycat/workspaceFormulas.py @@ -33,6 +33,7 @@ class WorkspaceFormulas(object): formulas.Temperature = formulas.actualTemperature temperature.update(formulas.Temperature) + workspaceFormulas = WorkspaceFormulas()