Python sys.stderr.write() Examples

The following are 30 code examples of sys.stderr.write(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sys.stderr , or try the search function .
Example #1
Source File: hevector.py    From vector-homomorphic-encryption with MIT License 7 votes vote down vote up
def evaluate(operations, DEBUG=False):
    from subprocess import Popen, PIPE

    if DEBUG:
        print
        print operations
        print
    inp = send(operations)
    if DEBUG:
        print inp
        print
    with open('vhe.in', 'w') as f:
        f.write(inp)
    output, error = Popen(['./vhe'], stdin=PIPE, stdout=PIPE, shell=True).communicate('')
    if DEBUG:
        print output
        print
    
    if error:
        from sys import stderr
        stderr.write(error + '\n')
        stderr.flush()

    return recv(output) 
Example #2
Source File: wcrawler.py    From sina-weibo-crawler with GNU General Public License v2.0 7 votes vote down vote up
def __get_request(self, url, max_try=3):
        stderr.write(url + '\n')
        cnt = 0
        while cnt < max_try:
            try:
                req = requests.get(url, headers=self.headers)
            except:
                cnt += 1
                continue
            if req.status_code != requests.codes.ok:
                break
            return req
        # Should not reach here if everything is ok.
        stderr.write(json.dumps(self.data, ensure_ascii=False, sort_keys=True, indent=4).encode('utf-8', 'replace'))
        stderr.write('Error: %s\n' % url)
        exit(1) 
Example #3
Source File: wcrawler.py    From sina-weibo-crawler with GNU General Public License v2.0 7 votes vote down vote up
def __parse_weibo(self, soup):
        # return all weibo in a page as a list
        ret = []
        for block in soup.find_all('div', 'c'):
            divs = block.find_all('div')
            if len(divs) == 1:
                text = self.__trim_like(divs[0].get_text())
                ret.append(text)
            elif len(divs) == 2 or len(divs) == 3:
                text = self.__trim_like(divs[0].get_text())
                comment = self.__trim_like(divs[-1].get_text())
                ret.append(text + comment)
            elif len(divs) == 0:
                continue
            else:
                stderr.write('Error: invalid weibo page')
                exit(1)
        return ret 
Example #4
Source File: textio.py    From PyDev.Debugger with Eclipse Public License 1.0 7 votes vote down vote up
def integer_list_file(cls, filename, values, bits = None):
        """
        Write a list of integers to a file.
        If a file of the same name exists, it's contents are replaced.

        See L{HexInput.integer_list_file} for a description of the file format.

        @type  filename: str
        @param filename: Name of the file to write.

        @type  values: list( int )
        @param values: List of integers to write to the file.

        @type  bits: int
        @param bits:
            (Optional) Number of bits of the target architecture.
            The default is platform dependent. See: L{HexOutput.integer_size}
        """
        fd = open(filename, 'w')
        for integer in values:
            print >> fd, cls.integer(integer, bits)
        fd.close() 
Example #5
Source File: textio.py    From PyDev.Debugger with Eclipse Public License 1.0 7 votes vote down vote up
def string_list_file(cls, filename, values):
        """
        Write a list of strings to a file.
        If a file of the same name exists, it's contents are replaced.

        See L{HexInput.string_list_file} for a description of the file format.

        @type  filename: str
        @param filename: Name of the file to write.

        @type  values: list( int )
        @param values: List of strings to write to the file.
        """
        fd = open(filename, 'w')
        for string in values:
            print >> fd, string
        fd.close() 
Example #6
Source File: dfxml.py    From IFIscripts with MIT License 7 votes vote down vote up
def tempfile(self,calcMD5=False,calcSHA1=False,calcSHA256=False):
        """Return the contents of imagefile in a named temporary file. If
        calcMD5, calcSHA1, or calcSHA256 are set TRUE, then the object
        returned has a hashlib object as self.md5 or self.sha1 with the
        requested hash."""
        import tempfile
        tf = tempfile.NamedTemporaryFile()
        if calcMD5: tf.md5 = hashlib.md5()
        if calcSHA1: tf.sha1 = hashlib.sha1()
        if calcSHA256: tf.sha256 = hashlib.sha256()
        for run in self.byte_runs():
            self.imagefile.seek(run.img_offset)
            count = run.len
            while count>0:
                xfer_len = min(count,1024*1024)        # transfer up to a megabyte at a time
                buf = self.imagefile.read(xfer_len)
                if len(buf)==0: break
                tf.write(buf)
                if calcMD5: tf.md5.update(buf)
                if calcSHA1: tf.sha1.update(buf)
                if calcSHA256: tf.sha256.update(buf)
                count -= xfer_len
        tf.flush()
        return tf 
Example #7
Source File: textio.py    From PyDev.Debugger with Eclipse Public License 1.0 7 votes vote down vote up
def __logfile_error(self, e):
        """
        Shows an error message to standard error
        if the log file can't be written to.

        Used internally.

        @type  e: Exception
        @param e: Exception raised when trying to write to the log file.
        """
        from sys import stderr
        msg = "Warning, error writing log file %s: %s\n"
        msg = msg % (self.logfile, str(e))
        stderr.write(DebugLog.log_text(msg))
        self.logfile = None
        self.fd      = None 
Example #8
Source File: lsof.py    From volafox with GNU General Public License v2.0 7 votes vote down vote up
def getnode(self):

        if self.xnode == None:
            x_node_ptr = unpacktype(self.smem, self.template['v_data'], INT)

            if self.tag == None:
                self.tag = unpacktype(self.smem, self.template['v_tag'], SHT)

            if self.tag == 16:  # VT_HFS
                self.xnode = Cnode(x_node_ptr)

            elif self.tag == 18:  # VT_DEVFS
                self.xnode = Devnode(x_node_ptr)

            else:
                if self.tag < len(Vnode.VNODE_TAG):
                    s_tag = Vnode.VNODE_TAG[self.tag]
                else:
                    s_tag = str(self.tag)
                stderr.write("WARNING Vnode.getnode(): unsupported FS tag %s, returning %d.\n" % (s_tag, ECODE['node']))
                return ECODE['node']

        return self.xnode.getnode() 
Example #9
Source File: lsof.py    From volafox with GNU General Public License v2.0 7 votes vote down vote up
def getdev(self):

        if self.tag == None:
            self.tag = unpacktype(self.smem, self.template['v_tag'], SHT)

        if self.tag == 18:  # CHR
            vu_specinfo = unpacktype(self.smem, self.template['v_un'], INT)

            # this pointer is invalid for /dev (special case DIR using VT_DEVFS)
            if not (vu_specinfo == 0) and Struct.mem.is_valid_address(vu_specinfo):
                specinfo = Specinfo(vu_specinfo)
                return specinfo.getdev()

        # default return for REG/DIR/LINK
        if self.mount == None:
            mount_ptr = unpacktype(self.smem, self.template['v_mount'], INT)

            if mount_ptr == 0 or not (Struct.mem.is_valid_address(mount_ptr)):
                stderr.write("WARNING Vnode.getdev(): v_mount pointer invalid, returning %d.\n" % ECODE['device'])
                return ECODE['device']

            self.mount = Mount(mount_ptr)

        return self.mount.getdev() 
Example #10
Source File: recipe-52278.py    From code with MIT License 7 votes vote down vote up
def printexpr(expr_string):
    """ printexpr(expr) - 
        print the value of the expression, along with linenumber and filename.
    """

    stack = extract_stack ( )[-2:][0]
    actualCall = stack[3]
    left = string.find ( actualCall, '(' )
    right = string.rfind ( actualCall, ')' )
    caller_globals,caller_locals = _caller_symbols()
    expr = eval(expr_string,caller_globals,caller_locals)
    varType = type( expr )
    stderr.write("%s:%d>  %s == %s  (%s)\n" % (
        stack[0], stack[1],
        string.strip( actualCall[left+1:right] )[1:-1],
        repr(expr), str(varType)[7:-2])) 
Example #11
Source File: data_io.py    From automl-phase-2 with MIT License 7 votes vote down vote up
def zipdir(archivename, basedir, ignoredirs=None):
    '''Zip directory, from J.F. Sebastian http://stackoverflow.com/'''
    assert os.path.isdir(basedir)
    if not ignoredirs:
        ignoredirs = []
    with closing(ZipFile(archivename, "w", ZIP_DEFLATED)) as z:
        for root, dirs, files in os.walk(basedir):
            for folder in copy.copy(dirs):
                abspath = os.path.abspath(os.path.join(root, folder))
                if abspath in ignoredirs:
                    dirs.remove(folder)

            # NOTE: ignore empty directories
            for fn in files:
                if fn[-4:]!='.zip':
                    absfn = os.path.join(root, fn)
                    zfn = absfn[len(basedir)+len(os.sep):]  # XXX: relative path
                    z.write(absfn, zfn)
                    
# ================ Inventory input data and create data structure ================= 
Example #12
Source File: pdfdoc.py    From Fluid-Designer with GNU General Public License v3.0 7 votes vote down vote up
def SaveToFile(self, filename, canvas):
        if hasattr(getattr(filename, "write",None),'__call__'):
            myfile = 0
            f = filename
            filename = makeFileName(getattr(filename,'name',''))
        else :
            myfile = 1
            filename = makeFileName(filename)
            f = open(filename, "wb")
        data = self.GetPDFData(canvas)
        if isUnicode(data):
            data = data.encode('latin1')
        f.write(data)
        if myfile:
            f.close()
            import os
            if os.name=='mac':
                from reportlab.lib.utils import markfilename
                markfilename(filename) # do platform specific file junk
        if getattr(canvas,'_verbosity',None): print('saved %s' % (filename,)) 
Example #13
Source File: dumpcomppage.py    From volafox with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, addr):
        self.smem = None

        if self.__class__.template == None:

            # configure template based on architecture and kernel version
            if Struct.arch in self.__class__.TEMPLATES:
                if Struct.kvers in self.__class__.TEMPLATES[Struct.arch]:
                    self.__class__.template = self.__class__.TEMPLATES[Struct.arch][Struct.kvers]
                else:
                    stderr.write("ERROR %s has no template for x%d Darwin %d.x.\n" % (
                    self.__class__.__name__, Struct.arch, Struct.kvers))
                    sys.exit()
            else:
                stderr.write(
                    "ERROR %s does not support %s architecture.\n" % (self.__class__.__name__, str(Struct.arch)))
                sys.exit()

            # set size of the structure by iterating over template
            for item in self.__class__.template.values():
                if ( item[1] + item[2] ) > self.__class__.ssize:
                    self.__class__.ssize = item[1] + item[2]

        if self.validaddr(addr):
            self.smem = Struct.mem.read(addr, self.__class__.ssize);
        else:
            stderr.write(
                "ERROR instance of %s failed to construct with address %.8x.\n" % (self.__class__.__name__, addr)) 
Example #14
Source File: boopstrike.py    From BoopSuite with MIT License 6 votes vote down vote up
def set_size(height, width):
    stdout.write("\x1b[8;{rows};{cols}t".format(rows=height, cols=width))
    return 
Example #15
Source File: debug.py    From asynq with Apache License 2.0 6 votes vote down vote up
def attach_exception_hook():
    """Injects async exception hook into the sys.excepthook."""
    try:
        # detect whether we're running in IPython
        __IPYTHON__
    except NameError:
        shell = None
    else:
        # override ipython's exception handler if in a shell.
        # we need to do this because ipython overrides sys.excepthook
        # so just the else block doesn't cover that case.
        from IPython.core.getipython import get_ipython

        # this may be None if __IPYTHON__ is somehow defined, but we are not
        # in fact in a shell
        shell = get_ipython()

    if shell is not None:
        shell.set_custom_exc((BaseException,), ipython_custom_exception_handler)
    else:
        global is_attached, original_hook
        if is_attached:
            sys.stderr.write("Warning: async exception hook was already attached.\n")
            return
        original_hook = sys.excepthook
        sys.excepthook = async_exception_hook
        is_attached = True 
Example #16
Source File: wcrawler.py    From sina-weibo-crawler with GNU General Public License v2.0 6 votes vote down vote up
def __parse_info_url(self, soup):
        table = soup.find_all('table')[0]
        for e in table.find_all('a'):
            if e.get_text() == u'资料':
                assert(e['href'][0] == '/')
                ret = 'https://weibo.cn' + e['href']
                return self.__remove_qmark(ret)
        # Should not reach here in normal case
        stderr.write('Error: can not find info tag.\n')
        exit(1) 
Example #17
Source File: debug.py    From asynq with Apache License 2.0 6 votes vote down vote up
def write(text, indent=0):
    if indent > 0:
        indent_str = "  " * indent
        text = text.replace("\n", "\n" + indent_str)
        if not text.startswith("\n"):
            text = indent_str + text
    stdout.write(text + "\n") 
Example #18
Source File: debug.py    From asynq with Apache License 2.0 6 votes vote down vote up
def dump(state):
    if not options.DUMP_PRE_ERROR_STATE:
        return
    stdout.flush()
    stderr.flush()
    stdout.write(
        "\n--- Pre-error state dump: --------------------------------------------\n"
    )
    try:
        state.dump()
    finally:
        stdout.write(
            "----------------------------------------------------------------------\n"
        )
        stderr.write("\n")
        stdout.flush()
        stderr.flush() 
Example #19
Source File: debug.py    From asynq with Apache License 2.0 6 votes vote down vote up
def dump_error(error, tb=None):
    """Dumps errors w/async stack traces."""
    try:
        stderr.write("\n" + (format_error(error, tb=tb) or "No error"))
    finally:
        stdout.flush()
        stderr.flush() 
Example #20
Source File: hdfs_client.py    From incubator-spot with Apache License 2.0 6 votes vote down vote up
def put_file_json(hdfs_file_content,hdfs_path,hdfs_file_name,append_file=False,overwrite_file=False, client=None):
    if not client:
        client = get_client()

    try:
        hdfs_full_name = "{0}/{1}".format(hdfs_path,hdfs_file_name)
        with client.write(hdfs_full_name,append=append_file,overwrite=overwrite_file,encoding='utf-8') as writer:
            dump(hdfs_file_content, writer)
        return True
    except HdfsError:
        return False 
Example #21
Source File: hdfs_client.py    From incubator-spot with Apache License 2.0 6 votes vote down vote up
def __call__(self):
        with self._lock:
            if self._nbytes >= 0:
                self._data[self._hpath] = self._nbytes
            else:
                stderr.write('%s\n' % (sum(self._data.values()), )) 
Example #22
Source File: hdfs_client.py    From incubator-spot with Apache License 2.0 6 votes vote down vote up
def put_file_csv(hdfs_file_content,hdfs_path,hdfs_file_name,append_file=False,overwrite_file=False, client=None):
    if not client:
        client = get_client()

    try:
        hdfs_full_name = "{0}/{1}".format(hdfs_path,hdfs_file_name)
        with client.write(hdfs_full_name,append=append_file,overwrite=overwrite_file) as writer:
            for item in hdfs_file_content:
                data = ','.join(str(d) for d in item)
                writer.write("{0}\n".format(data))
        return True

    except HdfsError:
        return False 
Example #23
Source File: utilities.py    From pytim with GNU General Public License v3.0 6 votes vote down vote up
def lap(show=False):
    """ Timer function

        :param bool show: (optional) print timer information to stderr
    """

    if not hasattr(lap, "tic"):
        lap.tic = timer()
    else:
        toc = timer()
        dt = toc - lap.tic
        lap.tic = toc
        if show:
            stderr.write("LAP >>> " + str(dt) + "\n")
        return dt 
Example #24
Source File: wcrawler.py    From sina-weibo-crawler with GNU General Public License v2.0 6 votes vote down vote up
def __parse_info_list(self, soup):
        arr = soup.find_all('div')
        table = None
        for i in xrange(len(arr)):
            if arr[i]['class'][0] == 'tip' and arr[i].get_text() == u'基本信息':
                assert(i + 1 < len(arr))
                table = arr[i + 1]
        assert(table != None)
        for c in table.children:
            try:
                if c['href'].find('keyword') > 0:
                    self.data['tags'].append(c.get_text())
            except:
                pass
            pos = c.find(':')
            if pos < 0 or pos + 1 == len(c):
                try:
                    pos = c.find(u':')
                except:
                    continue
            if pos < 0 or pos + 1 == len(c):
                continue
            key, val = c[:pos], c[pos + 1:]
            assert(len(key) > 0 and len(val) > 0)
            if key == u'昵称': self.data['nickname'] = val
            elif key == u'性别': self.data['gender'] = val
            elif key == u'地区': self.data['location'] = val
            elif key == u'生日': self.data['birthday'] = val
            elif key == u'简介': self.data['self-intro'] = val
            elif key == u'性取向': self.data['sexual_orientation'] = val
            elif key == u'认证' or key == u'认证信息': self.data['verify_info'] = val
            elif key == u'感情状况': self.data['relationship_status'] = val
            elif key == u'达人': self.data['good_at'] = val
            else: stderr.write('Not include attribute: %s %s\n' % (key, val)) 
Example #25
Source File: hdfs_client.py    From incubator-spot with Apache License 2.0 6 votes vote down vote up
def put_file_csv(hdfs_file_content,hdfs_path,hdfs_file_name,append_file=False,overwrite_file=False, client=None):
    if not client:
        client = get_client()

    try:
        hdfs_full_name = "{0}/{1}".format(hdfs_path,hdfs_file_name)
        with client.write(hdfs_full_name,append=append_file,overwrite=overwrite_file) as writer:
            for item in hdfs_file_content:
                data = ','.join(str(d) for d in item)
                writer.write("{0}\n".format(data))
        return True

    except HdfsError:
        return False 
Example #26
Source File: hdfs_client.py    From incubator-spot with Apache License 2.0 6 votes vote down vote up
def __call__(self):
        with self._lock:
            if self._nbytes >= 0:
                self._data[self._hpath] = self._nbytes
            else:
                stderr.write('%s\n' % (sum(self._data.values()), )) 
Example #27
Source File: textio.py    From PyDev.Debugger with Eclipse Public License 1.0 6 votes vote down vote up
def mixed_list_file(cls, filename, values, bits):
        """
        Write a list of mixed values to a file.
        If a file of the same name exists, it's contents are replaced.

        See L{HexInput.mixed_list_file} for a description of the file format.

        @type  filename: str
        @param filename: Name of the file to write.

        @type  values: list( int )
        @param values: List of mixed values to write to the file.

        @type  bits: int
        @param bits:
            (Optional) Number of bits of the target architecture.
            The default is platform dependent. See: L{HexOutput.integer_size}
        """
        fd = open(filename, 'w')
        for original in values:
            try:
                parsed = cls.integer(original, bits)
            except TypeError:
                parsed = repr(original)
            print >> fd, parsed
        fd.close()

#------------------------------------------------------------------------------ 
Example #28
Source File: dumpcomppage.py    From volafox with GNU General Public License v2.0 6 votes vote down vote up
def unpacktype(binstr, member, mtype):
    offset = member[1]
    size = member[2]
    fmt = ''

    if mtype == STR:
        fmt = str(size) + 's'
    elif mtype == INT:
        fmt = 'I' if size == 4 else 'Q'
    elif mtype == SHT:
        fmt = 'H'
    else:
        calling_fxn = sys._getframe(1)
        stderr.write("ERROR %s.%s tried to unpack the unknown type %d.\n" % (
        callingclass(calling_fxn), calling_fxn.f_code.co_name, mtype))
        return None

    if struct.calcsize(fmt) != len(binstr[offset:size + offset]):
        calling_fxn = sys._getframe(1)
        stderr.write("ERROR %s.%s tried to unpack '%s' (fmt size: %d) from %d bytes.\n" % (
        callingclass(calling_fxn), calling_fxn.f_code.co_name, fmt, struct.calcsize(fmt),
        len(binstr[offset:size + offset])))
        return None

    return struct.unpack(fmt, binstr[offset:size + offset])[0]

#################################### PRIVATE CLASSES #####################################

# return the enclosing class when called inside a function (error reporting) 
Example #29
Source File: dumpcomppage.py    From volafox with GNU General Public License v2.0 6 votes vote down vote up
def validaddr(self, addr):
        if addr == 0:
            calling_fxn = sys._getframe(1)
            stderr.write(
                "WARNING %s.%s was passed a NULL address.\n" % (callingclass(calling_fxn), calling_fxn.f_code.co_name))
            return False
        elif not (Struct.mem.is_valid_address(addr)):
            calling_fxn = sys._getframe(1)
            stderr.write("WARNING %s.%s was passed the invalid address %.8x.\n" % (
            callingclass(calling_fxn), calling_fxn.f_code.co_name, addr))
            return False
        return True 
Example #30
Source File: data_io.py    From automl-phase-2 with MIT License 5 votes vote down vote up
def write(filename, predictions):
    ''' Write prediction scores in prescribed format'''
    with open(filename, "w") as output_file:
		for row in predictions:
			if type(row) is not np.ndarray and type(row) is not list:
				row = [row]
			for val in row:
				output_file.write('{:g} '.format(float(val)))
			output_file.write('\n')