Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tqdm.write 'int' object has no attribute 'decode' #699

Closed
3 tasks done
thomasaarholt opened this issue Mar 20, 2019 · 3 comments
Closed
3 tasks done

tqdm.write 'int' object has no attribute 'decode' #699

thomasaarholt opened this issue Mar 20, 2019 · 3 comments
Assignees
Labels
duplicate 🗐 Seen it before

Comments

@thomasaarholt
Copy link

thomasaarholt commented Mar 20, 2019

  • I have visited the source website, and in particular
    read the known issues
  • I have searched through the issue tracker for duplicates
  • I have mentioned version numbers, operating system and
    environment, where applicable:
    import tqdm, sys
    print(tqdm.__version__, sys.version, sys.platform)
    4.31.1 3.7.1 | packaged by conda-forge | (default, Mar 13 2019, 12:57:14) 
    [GCC 7.3.0] linux

I think the following behaviour can at least be solved by improving the tqdm.write docs:

Whentqdm.write gets non-unicode input (I've tested with writing a list as well, same error), it throws the following error:

>>> from tqdm import tqdm
>>> tqdm.write(2)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-14-8f12b8905b28> in <module>
      1 from tqdm import tqdm
----> 2 tqdm.write(2)

/shared/users/thomasaar/py37/lib/python3.7/site-packages/tqdm/_tqdm.py in write(cls, s, file, end, nolock)
    524         with cls.external_write_mode(file=file, nolock=nolock):
    525             # Write the message
--> 526             fp.write(s)
    527             fp.write(end)
    528 

/shared/users/thomasaar/py37/lib/python3.7/site-packages/ipykernel/iostream.py in write(self, string)
    394             # Make sure that we're handling unicode
    395             if not isinstance(string, unicode_type):
--> 396                 string = string.decode(self.encoding, 'replace')
    397 
    398             is_child = (not self._is_master_process())

AttributeError: 'int' object has no attribute 'decode'

I have viewed tqdm.write as a tqdm replacement for print that doesn't break progress bar flow, and print can handle printing non-string types. As hinted, the docs aren't extensive, and suggest that the behaviour should imitate print.

My immediate suggestion it wrap whatever argument is fed to tqdm.write in a str(), since tqdm.write(str(non_string_object)) works fine. Alternatively, one could try the decode in the error message, and if that fails use the string representation instead. I'm not familiar with how python 2.6+ would handle this, however, and I'd gladly take other suggestions.

@casperdcl
Copy link
Sponsor Member

I think we should provide a tqdm.print() which actually behaves like print(). The current write() function has confused other people too

@thomasaarholt
Copy link
Author

That would make sense, and also not break any current behaviour with tqdm.write.

@casperdcl
Copy link
Sponsor Member

duplicate of #342

@casperdcl casperdcl self-assigned this May 9, 2019
@casperdcl casperdcl added the duplicate 🗐 Seen it before label May 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate 🗐 Seen it before
Projects
None yet
Development

No branches or pull requests

2 participants