[elbe-devel] [PATCH 10/25] py3: avoid using has_key

Torben Hohn torben.hohn at linutronix.de
Wed Dec 6 11:40:21 CET 2017


On Fri, Dec 01, 2017 at 04:51:07PM +0100, Manuel Traut wrote:
> dictionaries no longer have the has_key function in python3.
> Instead 'in' can be used in py3 and py2.

I assume, that this patch is completetly automatic.

I was a big fan of has_key(), so this patch makes me a bit sad.

Anyways, we have to move on. But i am not happy with these constructs:

> +    if ptype != parted.PARTITION_EXTENDED and part.text("label") in fslabel and fslabel[part.text("label")].fstype == "vfat":

can we agree, that we "mandate" parenthesis in such expressions ? even
if not needed ?

if (ptype != parted.PARTITION_EXTENDED) and (part.text("label") in fslabel) and (fslabel[part.text("label")].fstype == "vfat"):

This is a good discussion object, because it was not havin parenthesis in the
first place. And i now think, it really should have some, or be extended
to multiple lines.

We can do this later on, when we touch code. This is only cosmetics
after all. 

So i give 

Reviewed-by: Torben Hohn <torbenh at linutronix.de>

anyways.


> 
> Signed-off-by: Manuel Traut <manut at linutronix.de>
> ---
>  elbepack/commands/parselicence.py | 10 +++++-----
>  elbepack/config.py                | 10 +++++-----
>  elbepack/dump.py                  | 16 ++++++++--------
>  elbepack/finetuning.py            |  2 +-
>  elbepack/fstab.py                 |  2 +-
>  elbepack/hdimg.py                 |  8 ++++----
>  elbepack/pkgutils.py              |  2 +-
>  elbepack/treeutils.py             |  4 ++--
>  elbepack/xmldefaults.py           |  6 +++---
>  9 files changed, 30 insertions(+), 30 deletions(-)
> 
> diff --git a/elbepack/commands/parselicence.py b/elbepack/commands/parselicence.py
> index 708d5aa4..c9d215de 100644
> --- a/elbepack/commands/parselicence.py
> +++ b/elbepack/commands/parselicence.py
> @@ -56,16 +56,16 @@ class license_dep5_to_spdx (dict):
>  
>  
>      def have_override (self, pkgname):
> -        return self.perpackage_override.has_key (pkgname)
> +        return pkgname in self.perpackage_override
>  
>      def get_override (self, pkgname):
>          return self.perpackage_override[pkgname]
>  
>      def map_one_license (self, pkgname, l, errors):
> -        if self.perpackage_mapping.has_key(pkgname):
> -            if self.perpackage_mapping[pkgname].has_key(l):
> +        if pkgname in self.perpackage_mapping:
> +            if l in self.perpackage_mapping[pkgname]:
>                  return self.perpackage_mapping[pkgname][l]
> -        if self.has_key(l):
> +        if l in self:
>              return self[l]
>          errors.append ('no mapping for "%s" for pkg "%s"' % (l, pkgname))
>          return None
> @@ -92,7 +92,7 @@ class license_dep5_to_spdx (dict):
>  
>  
>      def map_lic (self, pkgname, licenses, errors):
> -        if self.perpackage_override.has_key (pkgname):
> +        if pkgname in self.perpackage_override:
>              if len(self.perpackage_override[pkgname]) > 0:
>                  return self.perpackage_override[pkgname]
>  
> diff --git a/elbepack/config.py b/elbepack/config.py
> index 39ce2a61..f7262c7d 100644
> --- a/elbepack/config.py
> +++ b/elbepack/config.py
> @@ -27,19 +27,19 @@ class Config(dict):
>          self['elbepass'] = "foo"
>          self['pbuilder_jobs'] = "auto"
>  
> -        if os.environ.has_key('ELBE_SOAPPORT'):
> +        if 'ELBE_SOAPPORT' in os.environ:
>              self['soapport'] = os.environ['ELBE_SOAPPORT']
>  
> -        if os.environ.has_key('ELBE_SOAPHOST'):
> +        if 'ELBE_SOAPHOST' in os.environ:
>              self['soaphost'] = os.environ['ELBE_SOAPHOST']
>  
> -        if os.environ.has_key('ELBE_USER'):
> +        if 'ELBE_USER' in os.environ:
>              self['elbeuser'] = os.environ['ELBE_USER']
>  
> -        if os.environ.has_key('ELBE_PASS'):
> +        if 'ELBE_PASS' in os.environ:
>              self['elbepass'] = os.environ['ELBE_PASS']
>  
> -        if os.environ.has_key('ELBE_PBUILDER_JOBS'):
> +        if 'ELBE_PBUILDER_JOBS' in os.environ:
>              self['pbuilder_jobs'] = os.environ['ELBE_PBUILDER_JOBS']
>  
>  
> diff --git a/elbepack/dump.py b/elbepack/dump.py
> index 45669ec1..14cb0226 100644
> --- a/elbepack/dump.py
> +++ b/elbepack/dump.py
> @@ -153,7 +153,7 @@ def check_full_pkgs(pkgs, fullpkgs, errorname, cache):
>              errors += 1
>  
>      for cp in cache.get_installed_pkgs():
> -        if not pindex.has_key(cp.name):
> +        if cp.name not in pindex:
>              elog.printo( "additional package %s installed, that was not requested" % cp.name )
>              errors += 1
>  
> @@ -234,19 +234,19 @@ def elbe_report( xml, buildenv, cache, reportname, targetfs ):
>      tgt_pkg_list = set()
>  
>      for fpath, realpath in targetfs.walk_files():
> -        if index.has_key(fpath):
> +        if fpath in index:
>              pkg = index[fpath]
>              tgt_pkg_list.add(pkg)
>          else:
>              pkg = "postinst generated"
>  
> -        if mt_index_post_fine.has_key(fpath) and mt_index.has_key(fpath):
> +        if fpath in mt_index_post_fine and fpath in mt_index:
>              if mt_index_post_fine[fpath] > mt_index[fpath]:
>                  pkg = "modified finetuning"
> -        if mt_index_post_fine.has_key(fpath):
> +        if fpath in mt_index_post_fine:
>              if mt_index_post_arch[fpath] > mt_index_post_fine[fpath]:
>                  pkg = "from archive"
> -            elif not mt_index.has_key(fpath):
> +            elif fpath not in mt_index:
>                  pkg = "added in finetuning"
>          else:
>              pkg = "added in archive"
> @@ -257,9 +257,9 @@ def elbe_report( xml, buildenv, cache, reportname, targetfs ):
>  
>      outf.h2( "Deleted Files" )
>      outf.table()
> -    for fpath in mt_index.keys():
> -        if not mt_index_post_arch.has_key(fpath):
> -            if index.has_key(fpath):
> +    for fpath in list(mt_index.keys()):
> +        if fpath not in mt_index_post_arch:
> +            if fpath in index:
>                  pkg = index[fpath]
>              else:
>                  pkg = "postinst generated"
> diff --git a/elbepack/finetuning.py b/elbepack/finetuning.py
> index d209e1b3..0020e020 100644
> --- a/elbepack/finetuning.py
> +++ b/elbepack/finetuning.py
> @@ -53,7 +53,7 @@ class RmAction(FinetuningAction):
>      def execute(self, log, buildenv, target):
>          files = target.glob( self.node.et.text )
>  
> -        if self.node.et.attrib.has_key ('exclude'):
> +        if 'exclude' in self.node.et.attrib:
>              exclude = self.node.et.attrib['exclude'].split (' ')
>          else:
>              exclude = []
> diff --git a/elbepack/fstab.py b/elbepack/fstab.py
> index d86bc23f..47633002 100644
> --- a/elbepack/fstab.py
> +++ b/elbepack/fstab.py
> @@ -53,7 +53,7 @@ class mountpoint_dict (dict):
>      def register (self, fstabentry):
>          mp = fstabentry.mountpoint
>  
> -        if self.has_key (mp):
> +        if mp in self:
>              fstabentry.id = self[mp].id
>          else:
>              fstabentry.id = str(self.id_count)
> diff --git a/elbepack/hdimg.py b/elbepack/hdimg.py
> index e2834e51..872b0ded 100644
> --- a/elbepack/hdimg.py
> +++ b/elbepack/hdimg.py
> @@ -45,7 +45,7 @@ def mkfs_mtd( outf, mtd, fslabel, rfs, target ):
>              continue
>  
>          label = v.text("label")
> -        if not fslabel.has_key(label):
> +        if label not in fslabel:
>              continue
>  
>          try:
> @@ -301,7 +301,7 @@ def create_partition(disk, part, ptype, fslabel, size_in_sectors, current_sector
>          sz = size_to_int(part.text("size"))/sector_size
>  
>      g = parted.Geometry(device=disk.device, start=current_sector, length=sz)
> -    if ptype != parted.PARTITION_EXTENDED and fslabel.has_key(part.text("label")) and fslabel[part.text("label")].fstype == "vfat":
> +    if ptype != parted.PARTITION_EXTENDED and part.text("label") in fslabel and fslabel[part.text("label")].fstype == "vfat":
>          fs = simple_fstype("fat32")
>          ppart = parted.Partition(disk, ptype, fs, geometry=g)
>          ppart.setFlag(_ped.PARTITION_LBA)
> @@ -357,7 +357,7 @@ def create_logical_partitions(outf, disk, extended, epart, fslabel, target, grub
>  
>          current_sector += 2048
>          lpart = create_partition(disk, logical, parted.PARTITION_LOGICAL, fslabel, size_in_sectors, current_sector)
> -        if logical.has("label") and fslabel.has_key(logical.text("label")):
> +        if logical.has("label") and logical.text("label") in fslabel:
>              create_label(outf, disk, logical, lpart, fslabel, target, grub)
>  
>          current_sector += lpart.getLength();
> @@ -392,7 +392,7 @@ def do_image_hd( outf, hd, fslabel, target, grub_version ):
>  
>          if part.tag == "partition":
>              ppart = create_partition(disk, part, parted.PARTITION_NORMAL, fslabel, size_in_sectors, current_sector)
> -            if fslabel.has_key(part.text("label")):
> +            if part.text("label") in fslabel:
>                  create_label(outf, disk, part, ppart, fslabel, target, grub)
>          elif part.tag == "extended":
>              ppart = create_partition(disk, part, parted.PARTITION_EXTENDED, fslabel, size_in_sectors, current_sector)
> diff --git a/elbepack/pkgutils.py b/elbepack/pkgutils.py
> index b8e82a68..d97baff7 100644
> --- a/elbepack/pkgutils.py
> +++ b/elbepack/pkgutils.py
> @@ -165,7 +165,7 @@ def get_dsc_size( fname ):
>  
>      sz = os.path.getsize(fname)
>      for sect in tf:
> -        if sect.has_key('Files'):
> +        if 'Files' in sect:
>              files = sect['Files'].split('\n')
>              files = [ f.strip().split(' ') for f in files ]
>              for f in files:
> diff --git a/elbepack/treeutils.py b/elbepack/treeutils.py
> index c998bde9..bcbd415e 100644
> --- a/elbepack/treeutils.py
> +++ b/elbepack/treeutils.py
> @@ -48,9 +48,9 @@ class ebase(object):
>  
>      def text( self, path, **args ):
>          el = self.et.find("./"+path)
> -        if (el is None) and not args.has_key("default"):
> +        if (el is None) and "default" not in args:
>              raise Exception( "Cant find path %s" % path )
> -        elif (el is None) and args.has_key("default"):
> +        elif (el is None) and "default" in args:
>              default = args["default"]
>              if type(default) == str:
>                  return default
> diff --git a/elbepack/xmldefaults.py b/elbepack/xmldefaults.py
> index 147d4be6..738d3199 100644
> --- a/elbepack/xmldefaults.py
> +++ b/elbepack/xmldefaults.py
> @@ -185,7 +185,7 @@ class ElbeDefaults(object):
>  
>      def __init__(self, build_type):
>  
> -        if not defaults.has_key(build_type):
> +        if build_type not in defaults:
>              print "Please specify a valid buildtype."
>              print "Valid buildtypes:"
>              print defaults.keys()
> @@ -197,9 +197,9 @@ class ElbeDefaults(object):
>          self.generic_defaults = archindep_defaults
>  
>      def __getitem__( self, key ):
> -        if self.defaults.has_key( key ):
> +        if key in self.defaults:
>              return self.defaults[key]
> -        if self.generic_defaults.has_key( key ):
> +        if key in self.generic_defaults:
>              return self.generic_defaults[key]
>  
>          return None
> -- 
> 2.15.1
> 
> 
> _______________________________________________
> elbe-devel mailing list
> elbe-devel at linutronix.de
> https://lists.linutronix.de/mailman/listinfo/elbe-devel

-- 
Mit freundlichen Grüßen
Torben Hohn

Linutronix GmbH

Standort: Bremen

Phone: +49 7556 25 999 18; Fax.: +49 7556 25 999 99

Firmensitz / Registered Office: D-88690 Uhldingen, Bahnhofstr. 3
Registergericht / Local District Court: Amtsgericht Freiburg i. Br.; HRB
Nr. / Trade register no.: 700 806

Geschäftsführer / Managing Directors: Heinz Egger, Thomas Gleixner

Eine Bitte von uns: Sollten Sie diese E-Mail irrtümlich erhalten haben,
benachrichtigen Sie uns in diesem Falle bitte sobald wie es Ihnen
möglich ist, durch Antwort-Mail. Vielen Dank!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: Digital signature
URL: <http://lists.linutronix.de/pipermail/elbe-devel/attachments/20171206/b05d3986/attachment.sig>


More information about the elbe-devel mailing list