compat.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # -*- coding: utf-8 -*-
  2. #
  3. # progressbar - Text progress bar library for Python.
  4. # Copyright (c) 2005 Nilton Volpato
  5. #
  6. # SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause-Clear
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. """Compatibility methods and classes for the progressbar module."""
  22. # Python 3.x (and backports) use a modified iterator syntax
  23. # This will allow 2.x to behave with 3.x iterators
  24. try:
  25. next
  26. except NameError:
  27. def next(iter):
  28. try:
  29. # Try new style iterators
  30. return iter.__next__()
  31. except AttributeError:
  32. # Fallback in case of a "native" iterator
  33. return iter.next()
  34. # Python < 2.5 does not have "any"
  35. try:
  36. any
  37. except NameError:
  38. def any(iterator):
  39. for item in iterator:
  40. if item: return True
  41. return False