git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 4/6] git-p4: Decode byte strings before printing

From
Fabian Stelzer <fs@gigacodes.de>
Date
Dec 10, 2021, 08:40 UTC
Message-ID
<20211210084021.k4pzkckrmocoqfgg@fs>
In-Reply-To
<xmqqczm5o0pa.fsf@gitster.g>
On 09.12.2021 14:47, Junio C Hamano wrote:
Show 11 quoted lines
>Joel Holdsworth <jholdsworth@nvidia.com> writes:
>
>> Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>
>> ---
>>  git-p4.py | 8 +++++---
>>  1 file changed, 5 insertions(+), 3 deletions(-)
>
>Is the use of strings with {} placeholders and their .format() method
>integral part of "decoding byte strings before printing", or it is just
>a new/better/improved/subjectively-preferred/whatever style?
>

If the new minimum python version will be 3.6 or above I'd vote for using f-Strings instead of .format() which I think are more readable and are also supposed to be faster.

So: sys.stdout.write(f'\r{file_path} --> {rel_path} ({size/1024/1024} MB)\n')

instead of one of these: sys.stdout.write('\r%s --> %s (%i MB)\n' % (file_path, relPath, size/1024/1024)) sys.stdout.write('\r{} --> {} ({} MB)\n'.format(file_path.decode(), relPath, size/1024/1024))

Show 33 quoted lines
>> diff --git a/git-p4.py b/git-p4.py
>> index b5d4fc1176..b5945a0306 100755
>> --- a/git-p4.py
>> +++ b/git-p4.py
>> @@ -2917,7 +2917,8 @@ def streamOneP4File(self, file, contents):
>>                  size = int(self.stream_file['fileSize'])
>>              else:
>>                  size = 0 # deleted files don't get a fileSize apparently
>> -            sys.stdout.write('\r%s --> %s (%i MB)\n' % (file_path, relPath, size/1024/1024))
>> +            sys.stdout.write('\r{} --> {} ({} MB)\n'.format(
>> +                file_path.decode(), relPath, size/1024/1024))
>>              sys.stdout.flush()
>>
>>          (type_base, type_mods) = split_p4_type(file["type"])
>> @@ -3061,7 +3062,8 @@ def streamP4FilesCb(self, marshalled):
>>              size = int(self.stream_file["fileSize"])
>>              if size > 0:
>>                  progress = 100*self.stream_file['streamContentSize']/size
>> -                sys.stdout.write('\r%s %d%% (%i MB)' % (self.stream_file['depotFile'], progress, int(size/1024/1024)))
>> +                sys.stdout.write('\r{} {}% ({} MB)'.format(
>> +                    self.stream_file['depotFile'].decode(), progress, int(size/1024/1024)))
>>                  sys.stdout.flush()
>>
>>          self.stream_have_file_info = True
>> @@ -3803,7 +3805,7 @@ def closeStreams(self):
>>              return
>>          self.gitStream.close()
>>          if self.importProcess.wait() != 0:
>> -            die("fast-import failed: %s" % self.gitError.read())
>> +            die("fast-import failed: {}".format(self.gitError.read().decode()))
>>          self.gitOutput.close()
>>          self.gitError.close()
>>          self.gitStream = None
Previous: Junio C HamanoNext: Joel Holdsworth
Message 12 of 31 in “Transition git-p4.py to support Python 3 only”
  1. 0/6 Transition git-p4.py to support Python 3 onlyJoel Holdsworth, Dec 9, 2021
  2. 1/6 git-p4: Always pass cmd arguments to subprocess as a python listsJoel Holdsworth, Dec 9, 2021
  3. Junio C HamanoDec 9, 2021
  4. 2/6 git-p4: Don't print shell commands as python listsJoel Holdsworth, Dec 9, 2021
  5. 3/6 git-p4: Removed support for Python 2Joel Holdsworth, Dec 9, 2021
  6. Junio C HamanoDec 9, 2021
  7. rsbecker@nexbridge.comDec 9, 2021
  8. David AguilarDec 10, 2021
  9. Joel HoldsworthDec 10, 2021
  10. 4/6 git-p4: Decode byte strings before printingJoel Holdsworth, Dec 9, 2021
  11. Junio C HamanoDec 9, 2021
  12. Fabian StelzerDec 10, 2021
  13. Joel HoldsworthDec 10, 2021
  14. Joel HoldsworthDec 10, 2021
  15. 5/6 git-p4: Eliminate decode_stream and encode_streamJoel Holdsworth, Dec 9, 2021
  16. 6/6 git-p4: Resolve RCS keywords in binaryJoel Holdsworth, Dec 9, 2021
  17. Luke DiamandDec 10, 2021
  18. Joel HoldsworthDec 10, 2021
  19. Ævar Arnfjörð BjarmasonDec 10, 2021
  20. Joel HoldsworthDec 10, 2021
  21. Ævar Arnfjörð BjarmasonDec 10, 2021
  22. Junio C HamanoDec 10, 2021
  23. rsbecker@nexbridge.comDec 10, 2021
  24. Elijah NewrenDec 11, 2021
  25. Luke DiamandDec 12, 2021
  26. Luke DiamandDec 10, 2021
  27. Joel HoldsworthDec 10, 2021
  28. Luke DiamandDec 11, 2021
  29. Joel HoldsworthDec 13, 2021
  30. Junio C HamanoDec 13, 2021
  31. Joel HoldsworthDec 13, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.