Created
December 9, 2013 18:08
-
-
Save jasonbot/7877027 to your computer and use it in GitHub Desktop.
Use to make GP tools compatible with multiprocessing (usually they replace sys.stdout with a GP version that doesn't have all the same methods). Just call fix_stdout() after importing arcpy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import arcpy | |
| import sys | |
| class WrappedStream(object): | |
| "Wraps sys.stdout to have all the usual methods of a file object" | |
| def __init__(self, wrap): | |
| self._wrap = wrap | |
| def close(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| @property | |
| def closed(self, *args, **kw): | |
| try: | |
| return getattr(self._wrap, 'closed', False) | |
| except: | |
| return False | |
| @property | |
| def encoding(self, *args, **kw): | |
| try: | |
| return getattr(self._wrap, 'encoding', 'utf-8') | |
| except: | |
| return 'utf-8' | |
| @property | |
| def errors(self, *args, **kw): | |
| try: | |
| return getattr(self._wrap, 'errors', None) | |
| except: | |
| pass | |
| def fileno(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| return -1 | |
| def flush(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| def isatty(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| @property | |
| def mode(self, *args, **kw): | |
| try: | |
| return getattr(self._wrap, 'mode', 'w') | |
| except: | |
| return 'w' | |
| @property | |
| def name(self, *args, **kw): | |
| try: | |
| return getattr(self._wrap, 'name', '<stdout>') | |
| except: | |
| return '<stdout>' | |
| @property | |
| def newlines(self, *args, **kw): | |
| try: | |
| return getattr(self._wrap, 'newlines', None) | |
| except: | |
| pass | |
| def next(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| def read(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| def readinto(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| def readline(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| def readlines(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| def seek(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| @property | |
| def softspace(self, *args, **kw): | |
| try: | |
| return getattr(self._wrap, 'softspace', 1) | |
| except: | |
| return 1 | |
| def tell(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| def truncate(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| def write(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| def writelines(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| def xreadlines(self, *args, **kw): | |
| try: | |
| self._wrap(*args, **kw) | |
| except: | |
| pass | |
| def fix_stdout(): | |
| if not isinstance(sys.stdout, (file, StreamWrapper)): | |
| sys.stdout = StreamWrapper(sys.stdout) | |
| if not isinstance(sys.stderr, (file, StreamWrapper)): | |
| sys.stderr = StreamWrapper(sys.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment