1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-09 06:01:28 +00:00

Replace key existence tests with dict.get()

Replaced instances of this pattern:

if 'key' in somedict:
    var = somedict['key']
else:
    var = ""

...with this much simpler pattern:

var = somedict.get('key', "")

Some variations, like returning None if the key is not found were also
replaced.
This commit is contained in:
dmiller
2014-01-08 20:28:12 +00:00
parent 5c662fffdc
commit b254234118
5 changed files with 10 additions and 42 deletions

View File

@@ -179,10 +179,7 @@ class XMLNode:
def get_attr(self, attr): def get_attr(self, attr):
""" """
""" """
if attr in self.__attrs: return self.__attrs.get(attr)
return self.__attrs[attr]
return None
def get_attrs(self): def get_attrs(self):
""" """

View File

@@ -254,17 +254,9 @@ class ServicesPage(gtk.Notebook):
color = get_service_color(port['state']['state']) color = get_service_color(port['state']['state'])
if 'name' in port['service']: service_name = port['service'].get('name', _('<unknown>'))
service_name = port['service']['name']
else: service_method = port['service'].get('method', _('<none>'))
service_name = _('<unknown>')
if 'method' in port['service']:
service_method = port['service']['method']
else:
service_method = _('<none>')
reference = self.__ports_store.append(None, reference = self.__ports_store.append(None,
[port['id'], [port['id'],
@@ -576,10 +568,7 @@ class SystemPage(BWScrolledWindow):
for os_class in os['classes']: for os_class in os['classes']:
os_gen = '' os_gen = os_class.get('os_gen', '')
if 'os_gen' in os_class:
os_gen = os_class['os_gen']
self.__class_store.append([os_class['accuracy'], self.__class_store.append([os_class['accuracy'],
os_class['vendor'], os_class['vendor'],

View File

@@ -2042,10 +2042,7 @@ class NetNode(Node):
if info == None: if info == None:
return self.__draw_info return self.__draw_info
if info in self.__draw_info: return self.__draw_info.get(info)
return self.__draw_info[info]
return None
def set_draw_info(self, info): def set_draw_info(self, info):
""" """

View File

@@ -293,18 +293,12 @@ class ScanChooser(HIGVBox):
"""Return the currently selected scan's parsed output as an NmapParser """Return the currently selected scan's parsed output as an NmapParser
object, or None if no valid scan is selected.""" object, or None if no valid scan is selected."""
selected_scan = self.combo_scan.child.get_text() selected_scan = self.combo_scan.child.get_text()
if selected_scan in self.scan_dict: return self.scan_dict.get(selected_scan)
return self.scan_dict[selected_scan]
# What's typed in the entry doesn't match a registered scan.
return None
def get_nmap_output(self): def get_nmap_output(self):
"""Return the currently selected scan's output as a string, or None if """Return the currently selected scan's output as a string, or None if
no valid scan is selected.""" no valid scan is selected."""
parsed = self.parsed_scan return self.parsed_scan
if parsed is not None:
return parsed.nmap_output
return None
nmap_output = property(get_nmap_output) nmap_output = property(get_nmap_output)
parsed_scan = property(get_parsed_scan) parsed_scan = property(get_parsed_scan)

View File

@@ -137,28 +137,19 @@ class ProfileHelp:
self.labels[option_name] = text self.labels[option_name] = text
def get_label(self): def get_label(self):
if self.currentstate in self.labels.keys(): return self.labels.get(self.currentstate, "")
return self.labels[self.currentstate]
else:
return "" # blank
def add_shortdesc(self, option_name, text): def add_shortdesc(self, option_name, text):
self.descs[option_name] = text self.descs[option_name] = text
def get_shortdesc(self): def get_shortdesc(self):
if self.currentstate in self.descs.keys(): return self.descs.get(self.currentstate, "")
return self.descs[self.currentstate]
else:
return "" # blank
def add_example(self, option_name, text): def add_example(self, option_name, text):
self.examples[option_name] = text self.examples[option_name] = text
def get_example(self): def get_example(self):
if self.currentstate in self.examples.keys(): return self.examples.get(self.currentstate, "")
return self.examples[self.currentstate]
else:
return "" # blank
def handler(self, whichLabel): def handler(self, whichLabel):
log.debug("whichLabel: %s" % whichLabel) log.debug("whichLabel: %s" % whichLabel)