Minor cleanup, and made status class properties private

This commit is contained in:
Veronica K. B. Olsen
2020-09-18 18:50:13 +02:00
parent 4e03fdca31
commit de72d93b9c
3 changed files with 43 additions and 46 deletions
+40 -40
View File
@@ -36,12 +36,12 @@ logger = logging.getLogger(__name__)
class NWStatus(): class NWStatus():
def __init__(self): def __init__(self):
self.theLabels = [] self._theLabels = []
self.theColours = [] self._theColours = []
self.theCounts = [] self._theCounts = []
self.theMap = {} self._theMap = {}
self.theLength = 0 self._theLength = 0
self.theIndex = 0 self._theIndex = 0
return return
def addEntry(self, theLabel, theColours): def addEntry(self, theLabel, theColours):
@@ -50,11 +50,11 @@ class NWStatus():
""" """
theLabel = theLabel.strip() theLabel = theLabel.strip()
if self.lookupEntry(theLabel) is None: if self.lookupEntry(theLabel) is None:
self.theLabels.append(theLabel) self._theLabels.append(theLabel)
self.theColours.append(theColours) self._theColours.append(theColours)
self.theCounts.append(0) self._theCounts.append(0)
self.theMap[theLabel] = self.theLength self._theMap[theLabel] = self._theLength
self.theLength += 1 self._theLength += 1
return True return True
def lookupEntry(self, theLabel): def lookupEntry(self, theLabel):
@@ -64,8 +64,8 @@ class NWStatus():
if theLabel is None: if theLabel is None:
return None return None
theLabel = theLabel.strip() theLabel = theLabel.strip()
if theLabel in self.theMap.keys(): if theLabel in self._theMap.keys():
return self.theMap[theLabel] return self._theMap[theLabel]
return None return None
def checkEntry(self, theStatus): def checkEntry(self, theStatus):
@@ -77,8 +77,8 @@ class NWStatus():
if self.lookupEntry(theStatus) is not None: if self.lookupEntry(theStatus) is not None:
return theStatus return theStatus
theStatus = checkInt(theStatus, 0, False) theStatus = checkInt(theStatus, 0, False)
if theStatus >= 0 and theStatus < self.theLength: if theStatus >= 0 and theStatus < self._theLength:
return self.theLabels[theStatus] return self._theLabels[theStatus]
def setNewEntries(self, newList): def setNewEntries(self, newList):
"""Update the list of entries after they have been modified by """Update the list of entries after they have been modified by
@@ -87,12 +87,12 @@ class NWStatus():
replaceMap = {} replaceMap = {}
if newList is not None: if newList is not None:
self.theLabels = [] self._theLabels = []
self.theColours = [] self._theColours = []
self.theCounts = [] self._theCounts = []
self.theMap = {} self._theMap = {}
self.theLength = 0 self._theLength = 0
self.theIndex = 0 self._theIndex = 0
for nName, nR, nG, nB, oName in newList: for nName, nR, nG, nB, oName in newList:
self.addEntry(nName, (nR, nG, nB)) self.addEntry(nName, (nR, nG, nB))
@@ -104,7 +104,7 @@ class NWStatus():
def resetCounts(self): def resetCounts(self):
"""Clear the counts of references to the status entries. """Clear the counts of references to the status entries.
""" """
self.theCounts = [0]*self.theLength self._theCounts = [0]*self._theLength
return return
def countEntry(self, theLabel): def countEntry(self, theLabel):
@@ -112,20 +112,20 @@ class NWStatus():
""" """
theIndex = self.lookupEntry(theLabel) theIndex = self.lookupEntry(theLabel)
if theIndex is not None: if theIndex is not None:
self.theCounts[theIndex] += 1 self._theCounts[theIndex] += 1
return return
def packXML(self, xParent): def packXML(self, xParent):
"""Pack the status entries into an XML object for saving to the """Pack the status entries into an XML object for saving to the
main project file. main project file.
""" """
for n in range(self.theLength): for n in range(self._theLength):
xSub = etree.SubElement(xParent, "entry", attrib={ xSub = etree.SubElement(xParent, "entry", attrib={
"blue" : str(self.theColours[n][2]), "blue" : str(self._theColours[n][2]),
"green" : str(self.theColours[n][1]), "green" : str(self._theColours[n][1]),
"red" : str(self.theColours[n][0]), "red" : str(self._theColours[n][0]),
}) })
xSub.text = self.theLabels[n] xSub.text = self._theLabels[n]
return True return True
def unpackXML(self, xParent): def unpackXML(self, xParent):
@@ -151,12 +151,12 @@ class NWStatus():
theColours.append((cR, cG, cB)) theColours.append((cR, cG, cB))
if len(theLabels) > 0: if len(theLabels) > 0:
self.theLabels = [] self._theLabels = []
self.theColours = [] self._theColours = []
self.theCounts = [] self._theCounts = []
self.theMap = {} self._theMap = {}
self.theLength = 0 self._theLength = 0
self.theIndex = 0 self._theIndex = 0
for n in range(len(theLabels)): for n in range(len(theLabels)):
self.addEntry(theLabels[n], theColours[n]) self.addEntry(theLabels[n], theColours[n])
@@ -170,22 +170,22 @@ class NWStatus():
def __getitem__(self, n): def __getitem__(self, n):
"""Return an entry by its index. """Return an entry by its index.
""" """
if n >= 0 and n < self.theLength: if n >= 0 and n < self._theLength:
return self.theLabels[n], self.theColours[n], self.theCounts[n] return self._theLabels[n], self._theColours[n], self._theCounts[n]
return None, None, None return None, None, None
def __iter__(self): def __iter__(self):
"""Initialise the iterator. """Initialise the iterator.
""" """
self.theIndex = 0 self._theIndex = 0
return self return self
def __next__(self): def __next__(self):
"""Return the next entry for the iterator. """Return the next entry for the iterator.
""" """
if self.theIndex < self.theLength: if self._theIndex < self._theLength:
theLabel, theColour, theCount = self.__getitem__(self.theIndex) theLabel, theColour, theCount = self.__getitem__(self._theIndex)
self.theIndex += 1 self._theIndex += 1
return theLabel, theColour, theCount return theLabel, theColour, theCount
else: else:
raise StopIteration raise StopIteration
+1 -2
View File
@@ -309,8 +309,7 @@ class ToHtml(Tokenizer):
def _formatKeywords(self, tText): def _formatKeywords(self, tText):
"""Apply HTML formatting to keywords. """Apply HTML formatting to keywords.
""" """
tText = "@"+tText isValid, theBits, thePos = self.theParent.theIndex.scanThis("@"+tText)
isValid, theBits, thePos = self.theParent.theIndex.scanThis(tText)
if not isValid or not theBits: if not isValid or not theBits:
return "" return ""
+2 -4
View File
@@ -27,7 +27,6 @@
import logging import logging
import re import re
import nw
from operator import itemgetter from operator import itemgetter
from PyQt5.QtCore import QRegularExpression from PyQt5.QtCore import QRegularExpression
@@ -74,9 +73,8 @@ class Tokenizer():
def __init__(self, theProject, theParent): def __init__(self, theProject, theParent):
self.mainConf = nw.CONFIG self.theProject = theProject
self.theProject = theProject self.theParent = theParent
self.theParent = theParent
# Data Variables # Data Variables
self.theText = None # The raw text to be tokenized self.theText = None # The raw text to be tokenized