Re: [PATCH] Use Python's "print" as a function, not as a keyword

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Dec 21, 2011 at 3:19 AM, Sebastian Morr <sebastian@xxxxxxx> wrote:

> This has changed from Version 2.6 to Version 3.0. Replace all occurrences of
>
>    print ...
>
> with
>
>    print(...)
>
> and all occurrences of
>
>    print >> output, ...
>
> with
>
>    output.write(... + "\n")

While it's good to look forward, you shouldn't forget about testing on
python 2.6. Lots of people still stick to that and maybe even to
earlier versions.


>  if len(argv) < 2:
> -       print 'Usage:', argv[0], '<zipfile>...'
> +       print('Usage:', argv[0], '<zipfile>...')
>        exit(1)
>

Here I would use the % notation:
print('Usage: %s <zipfile>...' % argv[0])

Python 2.x would print a tuple:

>>> argv = ('import-zips.py',)
>>> print("Usage:", argv[0], '<zipfile>...')
('Usage:', 'import-zips.py', '<zipfile>...')

You could probably get away with

print('Usage: ' + argv[0] + ' <zipfile>...')

But that could probably become a readability issue. I would even
wonder if it's considered pythonic.

It happens a few times more:

>  if verbose:
> -    print 'tip is', tip
> +    print('tip is', tip)

> @@ -176,27 +176,27 @@ for cset in range(int(tip) + 1):
>     os.write(fdcomment, csetcomment)
>     os.close(fdcomment)
>
> -    print '-----------------------------------------'
> -    print 'cset:', cset
> -    print 'branch:', hgbranch[str(cset)]
> -    print 'user:', user
> -    print 'date:', date
> -    print 'comment:', csetcomment
> +    print('-----------------------------------------')
> +    print('cset:', cset)
> +    print('branch:', hgbranch[str(cset)])
> +    print('user:', user)
> +    print('date:', date)
> +    print('comment:', csetcomment)
>     if parent:
> -       print 'parent:', parent
> +       print('parent:', parent)
>     if mparent:
> -        print 'mparent:', mparent
> +        print('mparent:', mparent)
>     if tag:
> -        print 'tag:', tag
> -    print '-----------------------------------------'
> +        print('tag:', tag)
> +    print('-----------------------------------------')


>
>     # checkout the parent if necessary
>     if cset != 0:
>         if hgbranch[str(cset)] == "branch-" + str(cset):
> -            print 'creating new branch', hgbranch[str(cset)]
> +            print('creating new branch', hgbranch[str(cset)])
>             os.system('git checkout -b %s %s' % (hgbranch[str(cset)], hgvers[parent]))
>         else:
> -            print 'checking out branch', hgbranch[str(cset)]
> +            print('checking out branch', hgbranch[str(cset)])
>             os.system('git checkout %s' % hgbranch[str(cset)])
>
>     # merge
> @@ -205,7 +205,7 @@ for cset in range(int(tip) + 1):
>             otherbranch = hgbranch[mparent]
>         else:
>             otherbranch = hgbranch[parent]
> -        print 'merging', otherbranch, 'into', hgbranch[str(cset)]
> +        print('merging', otherbranch, 'into', hgbranch[str(cset)])
>         os.system(getgitenv(user, date) + 'git merge --no-commit -s ours "" %s %s' % (hgbranch[str(cset)], otherbranch))
>
>     # remove everything except .git and .hg directories
> @@ -229,12 +229,12 @@ for cset in range(int(tip) + 1):
>
>     # delete branch if not used anymore...
>     if mparent and len(hgchildren[str(cset)]):
> -        print "Deleting unused branch:", otherbranch
> +        print("Deleting unused branch:", otherbranch)
>         os.system('git branch -d %s' % otherbranch)
>
>     # retrieve and record the version
>     vvv = os.popen('git show --quiet --pretty=format:%H').read()
> -    print 'record', cset, '->', vvv
> +    print('record', cset, '->', vvv)
>     hgvers[str(cset)] = vvv
>
>  if hgnewcsets >= opt_nrepack and opt_nrepack != -1:
> @@ -243,7 +243,7 @@ if hgnewcsets >= opt_nrepack and opt_nrepack != -1:
>  # write the state for incrementals
>  if state:
>     if verbose:
> -        print 'Writing state'
> +        print('Writing state')
>     f = open(state, 'w')
>     pickle.dump(hgvers, f)
>
> diff --git a/contrib/p4import/git-p4import.py b/contrib/p4import/git-p4import.py
> index b6e534b..144fafc 100644
> --- a/contrib/p4import/git-p4import.py
> +++ b/contrib/p4import/git-p4import.py
> @@ -26,11 +26,11 @@ if s != default_int_handler:
>  def die(msg, *args):
>     for a in args:
>         msg = "%s %s" % (msg, a)
> -    print "git-p4import fatal error:", msg
> +    print("git-p4import fatal error:", msg)
>     sys.exit(1)
>

I think that's it for the print(...,...) stuff. I might have misssed
one or two though.


> diff --git a/git-remote-testgit.py b/git-remote-testgit.py
> index 3dc4851..9803214 100644
> --- a/git-remote-testgit.py
> +++ b/git-remote-testgit.py
> @@ -81,9 +81,9 @@ def do_capabilities(repo, args):
>     """Prints the supported capabilities.
>     """
>
> -    print "import"
> -    print "export"
> -    print "refspec refs/heads/*:%s*" % repo.prefix
> +    print("import")
> +    print("export")
> +    print("refspec refs/heads/*:%s*" % repo.prefix)
>
>     dirname = repo.get_base_path(repo.gitdir)
>
> @@ -92,11 +92,11 @@ def do_capabilities(repo, args):
>
>     path = os.path.join(dirname, 'testgit.marks')
>
> -    print "*export-marks %s" % path
> +    print("*export-marks %s" % path)
>     if os.path.exists(path):
> -        print "*import-marks %s" % path
> +        print("*import-marks %s" % path)
>
> -    print # end capabilities
> +    print() # end capabilities

print("") here. 2.x:

>>> print()
()



>
>
>  def do_list(repo, args):
> @@ -109,16 +109,16 @@ def do_list(repo, args):
>
>     for ref in repo.revs:
>         debug("? refs/heads/%s", ref)
> -        print "? refs/heads/%s" % ref
> +        print("? refs/heads/%s" % ref)
>
>     if repo.head:
>         debug("@refs/heads/%s HEAD" % repo.head)
> -        print "@refs/heads/%s HEAD" % repo.head
> +        print("@refs/heads/%s HEAD" % repo.head)
>     else:
>         debug("@refs/heads/master HEAD")
> -        print "@refs/heads/master HEAD"
> +        print("@refs/heads/master HEAD")
>
> -    print # end list
> +    print() # end list

print("")

Lots more to do, I'm afraid.

Cheers,
Frans
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]