Skip to content

Commit

Permalink
Fixes issue wxWidgets#1556
Browse files Browse the repository at this point in the history
Replaced XXX.keys() calls with idiomatic python 3 calls that are compatible with python 2 such as eg:
 * replaced "y = xxx.keys()" or "y = list(xxx.keys())" with just "y = list(xxx)"
 * replaced "sorted(xxx.keys())" or "sorted(list(xxx.keys()))" with just "sorted(xxx)"
 * replaced "if not A in B.keys():" with "if A not in B:"
 * replaced "for A in B.keys():"  with "for A in B:"

See also https://python-future.org/compatible_idioms.html
https://python-future.org/compatible_idioms.html#dict-keys-values-items-as-a-list
  • Loading branch information
pbrod committed Mar 20, 2020
1 parent ef1edac commit 033c18f
Show file tree
Hide file tree
Showing 54 changed files with 164 additions and 193 deletions.
3 changes: 1 addition & 2 deletions buildtools/build_wxwidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ def main(wxDir, args):

parser = optparse.OptionParser(usage="usage: %prog [options]", version="%prog 1.0")

keys = option_dict.keys()
for opt in sorted(keys):
for opt in sorted(option_dict):
default = option_dict[opt][0]
action = "store"
if type(default) == bool:
Expand Down
3 changes: 1 addition & 2 deletions demo/Cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ def __init__(self, parent, log):
wx.Panel.__init__(self, parent, -1)

# Create a list of choices from the dictionary above.
choices = cursors.keys()
choices = sorted(choices)
choices = sorted(cursors)

# Create the controls.
self.cb = wx.ComboBox(self, -1, "wx.CURSOR_DEFAULT", choices=choices,
Expand Down
3 changes: 1 addition & 2 deletions demo/DVC_IndexListModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ def Compare(self, item1, item2, col, ascending):

def DeleteRows(self, rows):
# make a copy since we'll be sorting(mutating) the list
rows = list(rows)
# use reverse order so the indexes don't change as we remove items
rows.sort(reverse=True)
rows = sorted(rows, reverse=True)

for row in rows:
# remove it from our data structure
Expand Down
4 changes: 2 additions & 2 deletions demo/DrawXXXList.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def makeRandomPens(num, cache):
c = random.choice(colours)
t = random.randint(1, 4)

if not (c, t) in cache.keys():
if (c, t) not in cache:
cache[(c, t)] = wx.Pen(c, t)

pens.append( cache[(c, t)] )
Expand All @@ -120,7 +120,7 @@ def makeRandomBrushes(num, cache):
for i in range(num):
c = random.choice(colours)

if not c in cache.keys():
if c not in cache:
cache[c] = wx.Brush(c)

brushes.append( cache[c] )
Expand Down
4 changes: 2 additions & 2 deletions demo/FloatCanvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,11 +1514,11 @@ def ChangeProperties(self, Object = None):
Object.SetFillColor(colors[random.randint(0,len(colors)-1)])
Object.SetLineColor(colors[random.randint(0,len(colors)-1)])
Object.SetLineWidth(random.randint(1,7))
Object.SetLineStyle(list(FloatCanvas.DrawObject.LineStyleList.keys())[random.randint(0,5)])
Object.SetLineStyle(list(FloatCanvas.DrawObject.LineStyleList)[random.randint(0,5)])
for Object in self.ColorObjectsLine:
Object.SetLineColor(colors[random.randint(0,len(colors)-1)])
Object.SetLineWidth(random.randint(1,7))
Object.SetLineStyle(list(FloatCanvas.DrawObject.LineStyleList.keys())[random.randint(0,5)])
Object.SetLineStyle(list(FloatCanvas.DrawObject.LineStyleList)[random.randint(0,5)])
for Object in self.ColorObjectsColor:
Object.SetColor(colors[random.randint(0,len(colors)-1)])
for Object in self.ColorObjectsText:
Expand Down
2 changes: 1 addition & 1 deletion demo/GLCanvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, parent, log):

box = wx.BoxSizer(wx.VERTICAL)
box.Add((20, 30))
keys = sorted(buttonDefs.keys())
keys = sorted(buttonDefs)
for k in keys:
text = buttonDefs[k][1]
btn = wx.Button(self, k, text)
Expand Down
3 changes: 1 addition & 2 deletions demo/Grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ def __init__(self, parent, log):

box = wx.BoxSizer(wx.VERTICAL)
box.Add((20, 20))
keys = sorted(buttonDefs.keys())

for k in keys:
for k in sorted(buttonDefs):
text = buttonDefs[k][1]
btn = wx.Button(self, k, text)
box.Add(btn, 0, wx.ALIGN_CENTER|wx.ALL, 10)
Expand Down
11 changes: 3 additions & 8 deletions demo/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ def RemoveHTMLTags(data):

def FormatDocs(keyword, values, num):

names = list(values.keys())
names.sort()
names = sorted(values)

headers = (num == 2 and [_eventHeaders] or [_styleHeaders])[0]
table = (num == 2 and [_eventTable] or [_styleTable])[0]
Expand Down Expand Up @@ -1014,10 +1013,7 @@ def HuntExternalDemos():

# Sort and reverse the external demos keys so that they
# come back in alphabetical order
keys = list(externalDemos.keys())
keys.sort()
keys.reverse()

keys = sorted(externalDemos, reverse=True)
# Loop over all external packages
for extern in keys:
package = externalDemos[extern]
Expand Down Expand Up @@ -1733,8 +1729,7 @@ def BuildMenuBar(self):
item.Check(self.allowAuiFloating)
self.Bind(wx.EVT_MENU, self.OnAllowAuiFloating, item)

auiPerspectives = list(self.auiConfigurations.keys())
auiPerspectives.sort()
auiPerspectives = sorted(self.auiConfigurations)
perspectivesMenu = wx.Menu()
item = wx.MenuItem(perspectivesMenu, -1, DEFAULT_PERSPECTIVE, "Load startup default perspective", wx.ITEM_RADIO)
self.Bind(wx.EVT_MENU, self.OnAUIPerspectives, item)
Expand Down
3 changes: 1 addition & 2 deletions demo/TablePrint.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def __init__(self, parent, log, frame):

box = wx.BoxSizer(wx.VERTICAL)
box.Add((20, 30))
keys = list(buttonDefs.keys())
keys.sort()
keys = sorted(buttonDefs)

for k in keys:
text = buttonDefs[k][1]
Expand Down
8 changes: 4 additions & 4 deletions demo/Throbber.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def __init__(self, parent, log):
sizer.SetSizeHints(self)
sizer.Fit(self)

for t in self.throbbers.keys():
for t in self.throbbers:
self.throbbers[t]['throbber'].Start()

self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
Expand All @@ -190,11 +190,11 @@ def OnDestroy(self, event):
event.Skip()

def OnStartAnimation(self, event):
for t in self.throbbers.keys():
for t in self.throbbers:
self.throbbers[t]['throbber'].Start()

def OnStopAnimation(self, event):
for t in self.throbbers.keys():
for t in self.throbbers:
self.throbbers[t]['throbber'].Rest()

def OnNext(self, event):
Expand Down Expand Up @@ -222,7 +222,7 @@ def OnStop(self, event):
self.customThrobber.Stop()

def ShutdownDemo(self):
for t in self.throbbers.keys():
for t in self.throbbers:
self.throbbers[t]['throbber'].Rest()


Expand Down
3 changes: 1 addition & 2 deletions demo/agw/AGWInfoBar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@

def GetValidImages():

keys = catalog.keys()
valid_images = []
counter = 0

for key in keys:
for key in catalog:
bmp = catalog[key].GetBitmap()
if bmp.GetWidth() == 16 and bmp.GetHeight() == 16:
valid_images.append(bmp)
Expand Down
7 changes: 3 additions & 4 deletions demo/agw/ShortcutEditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,10 @@

def GetValidMenuImages():

keys = catalog.keys()
valid_images = []
counter = 0

for key in keys:
for key in catalog:
bmp = catalog[key].GetBitmap()
if bmp.GetWidth() == 16 and bmp.GetHeight() == 16:
valid_images.append(bmp)
Expand Down Expand Up @@ -204,7 +203,7 @@ def MakeAcceleratorTable(self):

for i in range(6):
name = 'Accelerator %d'%(i+1)
choice = random.choice(list(SE.ACCELERATORS.keys()))
choice = random.choice(list(SE.ACCELERATORS))

if choice == wx.ACCEL_ALT:
letter = random.choice(COMBINATIONS)
Expand All @@ -216,7 +215,7 @@ def MakeAcceleratorTable(self):
wxk = ord(letter)

else:
wxk = random.choice(list(SE.KEYMAP.keys()))
wxk = random.choice(list(SE.KEYMAP))

accel = (choice, wxk, ACCEL_IDS[i])
saved_accel = (name, choice, wxk, ACCEL_IDS[i])
Expand Down
3 changes: 1 addition & 2 deletions demo/agw/UltimateListCtrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def __init__(self, parent, log):

box = wx.BoxSizer(wx.VERTICAL)
box.Add((20, 20))
keys = list(buttonDefs.keys())
keys.sort()
keys = sorted(buttonDefs)

for k in keys:
text = buttonDefs[k][1]
Expand Down
3 changes: 1 addition & 2 deletions demo/agw/UltimateListIconDemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,7 @@ def __init__(self, parent, log):

# load some images into an image list
il = wx.ImageList(64, 64, True)
imgs = list(catalog.keys())
imgs.sort()
imgs = sorted(catalog)

for img in imgs:
bmp = catalog[img].GetBitmap()
Expand Down
3 changes: 1 addition & 2 deletions demo/agw/UltimateListListDemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ def __init__(self, parent, log):

# load some images into an image list
il = wx.ImageList(16, 16, True)
imgs = list(catalog.keys())
imgs.sort()
imgs = sorted(catalog)

for img in imgs:
bmp = catalog[img].GetBitmap()
Expand Down
2 changes: 1 addition & 1 deletion etgtools/sphinx_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def Join(self, with_tail=True):
text = Node.Join(self, with_tail)

# Health check
existing_sections = list(self.sections.keys())
existing_sections = list(self.sections)

for section_name, dummy in SECTIONS:
if section_name not in self.sections:
Expand Down
4 changes: 1 addition & 3 deletions samples/doodle/doodle.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ def SetLinesData(self, lines):
def MakeMenu(self):
"""Make a menu that can be popped up later"""
menu = wx.Menu()
keys = list(self.menuColours.keys())
keys.sort()
for k in keys:
for k in sorted(self.menuColours):
text = self.menuColours[k]
menu.Append(k, text, kind=wx.ITEM_CHECK)
self.Bind(wx.EVT_MENU_RANGE, self.OnMenuSetColour, id=100, id2=200)
Expand Down
2 changes: 1 addition & 1 deletion samples/floatcanvas/BB_HitTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def BB_HitTest(self, event, HitEvent):
object_index_list = [] #Create list for holding the indexes
xy_p = event.GetPosition()
xy = self.PixelToWorld( xy_p ) #Convert to the correct coords
for key2 in self.HitDict[HitEvent].keys():
for key2 in self.HitDict[HitEvent]:
#Get Mouse Event Position
bb = self.HitDict[HitEvent][key2].BoundingBox
if bb.PointInside(xy):
Expand Down
2 changes: 1 addition & 1 deletion samples/floatcanvas/GroupDeleteDemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, *args, **kwargs):
tb = NC.ToolBar
# tb.AddSeparator()

for Group in self.Groups.keys():
for Group in self.Groups:
Button = wx.Button(tb, wx.ID_ANY, "Remove %s"%Group)
tb.AddControl(Button)
Button.Bind(wx.EVT_BUTTON, lambda evt, group=Group: self.RemoveGroup(evt, group))
Expand Down
2 changes: 1 addition & 1 deletion samples/floatcanvas/GroupDemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, *args, **kwargs):
tb = NC.ToolBar
# tb.AddSeparator()

for Group in self.Groups.keys():
for Group in self.Groups:
Button = wx.Button(tb, wx.ID_ANY, "Hide/Show%s"%Group)
tb.AddControl(Button)
print(Group)
Expand Down
2 changes: 1 addition & 1 deletion samples/floatcanvas/MiniDemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(self, *args, **kwargs):
wx.lib.colourdb.updateColourDB()
self.colors = wx.lib.colourdb.getColourList()

self.LineStyles = FloatCanvas.DrawObject.LineStyleList.keys()
self.LineStyles = list(FloatCanvas.DrawObject.LineStyleList)

return None

Expand Down
4 changes: 2 additions & 2 deletions samples/roses/wxroses.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,6 @@ def OnSpinback(self, name, value):
app = wx.App(False)
MyFrame()
if verbose:
print_('spin_panels', spin_panels.keys())
print_('ctrl_buttons', ctrl_buttons.keys())
print_('spin_panels', list(spin_panels))
print_('ctrl_buttons', list(ctrl_buttons))
app.MainLoop()
29 changes: 9 additions & 20 deletions sphinxtools/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,20 @@ def buildEnumsAndMethods(sphinxDir):
'the text file "unreferenced_classes.inc" together with the ReST file names where they\n' \
'appear.\n\n'

keys = list(unreferenced_classes.keys())
keys.sort()

fid = textfile_open(os.path.join(SPHINXROOT, 'unreferenced_classes.inc'), 'wt')
fid.write('\n')
fid.write('='*50 + ' ' + '='*50 + '\n')
fid.write('%-50s %-50s\n'%('Reference', 'File Name(s)'))
fid.write('='*50 + ' ' + '='*50 + '\n')

for key in keys:
for key in sorted(unreferenced_classes):
fid.write('%-50s %-50s\n'%(key, ', '.join(unreferenced_classes[key])))

fid.write('='*50 + ' ' + '='*50 + '\n')
fid.close()

print((warn%(len(keys))))
print((warn%(len(unreferenced_classes))))


# ----------------------------------------------------------------------- #
Expand Down Expand Up @@ -372,9 +370,7 @@ def reformatFunctions(file):
else:
label = '.'.join(local_file.split('.')[0:2])

names = list(functions.keys())
names = [name.lower() for name in names]
names.sort()
names = sorted([name.lower() for name in functions])

text = templates.TEMPLATE_FUNCTION_SUMMARY % (label, label)

Expand All @@ -387,8 +383,7 @@ def reformatFunctions(file):
text += ' | '.join([':ref:`%s <%s %s>`'%(letter, label, letter) for letter in letters])
text += '\n\n\n'

names = list(functions.keys())
names = sorted(names, key=str.lower)
names = sorted(functions, key=str.lower)
imm = ItemModuleMap()

for letter in letters:
Expand Down Expand Up @@ -443,13 +438,10 @@ def makeModuleIndex(sphinxDir, file):
enum_base = [os.path.split(os.path.splitext(enum)[0])[1] for enum in enum_files]

imm = ItemModuleMap()
names = list(classes.keys())
names.sort(key=lambda n: imm.get_fullname(n))
names = sorted(classes, key=lambda n: imm.get_fullname(n))

# Workaround to sort names in a case-insensitive way
lower_to_name = {}
for name in names:
lower_to_name[name.lower()] = name
lower_to_name = {name.lower(): name for name in names}

text = ''
if module:
Expand All @@ -461,8 +453,7 @@ def makeModuleIndex(sphinxDir, file):
text += '%-80s **Short Description**\n' % '**Class**'
text += 80*'=' + ' ' + 80*'=' + '\n'

lower_names = list(lower_to_name.keys())
lower_names.sort()
lower_names = sorted(lower_to_name)

for lower in lower_names:
cls = lower_to_name[lower]
Expand All @@ -488,8 +479,7 @@ def makeModuleIndex(sphinxDir, file):
functionsFile = os.path.join(sphinxDir, module + '.functions.pkl')
if os.path.exists(functionsFile):
pf = PickleFile(functionsFile)
functions = list(pf.read().keys())
functions.sort(key=lambda n: imm.get_fullname(n))
functions = sorted(pf.read(), key=lambda n: imm.get_fullname(n))

pf = PickleFile(os.path.join(SPHINXROOT, 'function_summary.pkl'))
function_summaries = pf.read()
Expand Down Expand Up @@ -555,8 +545,7 @@ def genGallery():
possible = simple.lower()
html_files[possible + '.png'] = simple + '.html'

keys = list(html_files.keys())
keys.sort()
keys = sorted(html_files)

text = ''

Expand Down
2 changes: 1 addition & 1 deletion sphinxtools/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def setdefault(self, key, failobj = None):

def update(self, dict):
UserDict.update(self, dict)
for key in list(dict.keys()):
for key in dict:
if key not in self._keys: self._keys.append(key)

def values(self):
Expand Down
Loading

0 comments on commit 033c18f

Please sign in to comment.