def test_newline_empty(): sio = StringIO(u"a\nb\r\nc\rd", newline="") res = list(sio) assert res == [u"a\n", u"b\r\n", u"c\r", u"d"] sio.seek(0) res = sio.read(4) assert res == u"a\nb\r" res = sio.read(2) assert res == u"\nc" res = sio.read(1) assert res == u"\r" sio = StringIO(newline="") res = sio.write(u"a\n") assert res == 2 res = sio.write(u"b\r") assert res == 2 res = sio.write(u"\nc") assert res == 2 res = sio.write(u"\rd") assert res == 2 sio.seek(0) res = list(sio) assert res == [u"a\n", u"b\r\n", u"c\r", u"d"]
def test_read(): buf = u"1234567890" sio = StringIO(buf) assert sio.read(0) == '' assert buf[:1] == sio.read(1) assert buf[1:5] == sio.read(4) assert buf[5:] == sio.read(900) assert u"" == sio.read()
def test_newline_crlf(): sio = StringIO(u"a\nb\r\nc\rd", newline="\r\n") res = sio.read() assert res == u"a\r\nb\r\r\nc\rd" sio.seek(0) res = list(sio) assert res == [u"a\r\n", u"b\r\r\n", u"c\rd"]
def test_read_binary(): # data is from a test_imghdr test for a GIF file buf_in = (u'\x47\x49\x46\x38\x39\x61\x10\x00\x10\x00\xf6\x64\x00\xeb' u'\xbb\x18\xeb\xbe\x21\xf3\xc1\x1a\xfa\xc7\x19\xfd\xcb\x1b' u'\xff\xcc\x1c\xeb') assert len(buf_in) == 32 sio = StringIO(buf_in) buf_out = sio.read(32) assert buf_in == buf_out
def test_newline_none(): sio = StringIO(u"a\nb\r\nc\rd", newline=None) res = list(sio) assert res == [u"a\n", u"b\n", u"c\n", u"d"] sio.seek(0) res = sio.read(1) assert res == u"a" res = sio.read(2) assert res == u"\nb" res = sio.read(2) assert res == u"\nc" res = sio.read(1) assert res == u"\n" sio = StringIO(newline=None) res = sio.write(u"a\n") assert res == 2 res = sio.write(u"b\r\n") assert res == 3 res = sio.write(u"c\rd") assert res == 3 sio.seek(0) res = sio.read() assert res == u"a\nb\nc\nd" sio = StringIO(u"a\r\nb", newline=None) res = sio.read(3) assert res == u"a\nb"
def test_seek(): s = u"1234567890" sio = StringIO(s) sio.read(5) sio.seek(0) r = sio.read() assert r == s sio.seek(3) r = sio.read() assert r == s[3:] raises(TypeError, sio.seek, 0.0) exc_info = raises(ValueError, sio.seek, -3) assert exc_info.value.args[0] == "Negative seek position -3" raises(ValueError, sio.seek, 3, -1) raises(ValueError, sio.seek, 3, -3) sio.close() raises(ValueError, sio.seek, 0)
def test_overseek(): s = u"1234567890" sio = StringIO(s) res = sio.seek(11) assert res == 11 res = sio.read() assert res == u"" assert sio.tell() == 11 assert sio.getvalue() == s sio.write(u"") assert sio.getvalue() == s sio.write(s) assert sio.getvalue() == s + u"\0" + s
def submitWait(self,jcl,wait=30): """ :param jcl: dataset or pds(member) containting JCL to submit :param wait: wait time in seconds until function is to return :return Job: Job object containing information on Job submitted or None >>> fz=ftptoolz.Ftpzos('zibm','mm','pw',verbose=2) >>> j=fz.submitWait("'mm.jobs(copyy)'") >>> x.cc 'AE37' """ j=None f=StringIO() # py2/3 for line in self.getlines(jcl): f.write(line+'\n') f.seek(0) self.ftp.sendcmd('SITE file=jes') # switch to Spool mode try: if PY3: # convert to latin1 (iso-8859-1) byte string f = BytesIO(f.read().encode('latin1')) fresp = self.ftp.storlines('STOR myjob.seq', f) if fresp.startswith('250-'): jobid = fresp.split()[6] if jobid.startswith('JOB') and len(jobid)==8: j=Job(jobid,jcl) finally: self.ftp.sendcmd('SITE file=seq') # switch to File mode if not j: return j for i in range(wait): js,sp = self.listjob(j.jobid) j.status=js.status j.jobstatus=js if js.status=='OUTPUT': j.cc=js.cc j.spoolfiles=sp break elif js.status=='ACTIVE': j.cputime=js.cputime j.elapsed=js.elapsed time.sleep(1.) return j
def touchmembers(self, pds, membertimes, touchtemplate): """Submit TOUCH job to set modification times in members of a partitioned dataset. :param pds: partitioned dataset name :param membertimes: list of (membername, modtime, uid, size) tuples modtime is of datetime type or of string 'yyyymmdd.HHMMSS' :param touchtemplate: Touch template job skeleton (see touchtemplate_sample for further details """ if len(membertimes)==0: return f=StringIO() # py2/3 f.write(touchtemplate % pds.upper().strip()) for m, t, u, s in membertimes: # if touchuid/touchdate given as parameter asmdate will only count lines if self.verbose: print(m, t, u) if t: if isinstance(t,datetime.datetime): ttime = t.strftime('%Y%m%d.%H%M%S') else: ttime = t # 'yyyymmdd.HHMMSS' f.write('SET DATE=%s\n' % ttime) if u: f.write('SET USER=%s\n' % u.upper()) if s: f.write('SET LINES=%d\n' % s) f.write(m.upper()+'\n') f.write('//\n') # end of job f.seek(0) # rewind if self.test: print('\nThe following generated TOUCH job is not submitted in test mode:)') for line in f: print(line[:-1]) else: if PY3: # convert to latin1 (iso-8859-1) byte string f = BytesIO(f.read().encode('latin1')) self.ftp.sendcmd('SITE file=jes') # switch to Spool mode self.ftp.storlines('STOR touch.seq', f) self.ftp.sendcmd('SITE file=seq') # switch to File mode
def excepthook(excType, excValue, tracebackobj): """ Global function to catch unhandled exceptions. @param excType exception type @param excValue exception value @param tracebackobj traceback object """ separator = '-' * 80 tbinfofile = StringIO() traceback.print_tb(tracebackobj, None, tbinfofile) tbinfofile.seek(0) tbinfo = tbinfofile.read() errmsg = '%s: \n%s' % (str(excType), str(excValue)) sections = [separator, errmsg, separator, tbinfo] msg = '\n'.join(sections) sys.exit(str(msg))
def create_thumbnail(self): if not self.M.image: THUMBNAIL_SIZE = (200, 200) DJANGO_TYPE = self.M_image.file.content_type if DJANGO_TYPE == 'image/jpeg': PIL_TYPE = 'jpeg' FILE_EXTENSION = 'jpg' elif DJANGO_TYPE == 'image/png': PIL_TYPE = 'png' FILE_EXTENSION = 'png' image = Image.open(StringIO(self.M_image.read())) image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS) temp_handle = StringIO() image.save(temp_handle, PIL_TYPE) temp_handle.seek(0) suf = SimpleUploadedFile(os.path.split(self.M_image.name)[-1], temp_handle.read(), content_type=DJANGO_TYPE) self.thumb_pic.save( '%s.%s' % (os.path.splitext(suf.name)[0], FILE_EXTENSION), suf, save=False) return
from _io import StringIO except ImportError: from io import StringIO f = StringIO() f.write('hello world!!!') print(f.getvalue(), len(f.getvalue())) f.write('hello world!!!') print(f.getvalue(), len(f.getvalue())) f.truncate(0) f.write('start write!') print(f.tell()) print("next:\t", f.getvalue(), len(f.getvalue())) f_test = StringIO('Hello! Hi! Goodbye!') print("1:\t", f_test.read()) print(f_test.getvalue()) print(f_test.getvalue()) print("2:\t", f_test.read()) print("3:\t", f_test.read()) print("4:\t", f_test.read()) print("next:\t", f_test.getvalue(), len(f_test.getvalue())) f_test.close() f.close()
''' @author: xilh @since: 20200128 ''' from _io import StringIO from demo.tools.tool import pline # 创建对象 f = StringIO() f.write('hello') f.write(' ') f.write('world') ret = f.getvalue() print(ret) pline() f = StringIO("abcdefghijk") print(f.read(2)) print(f.readline())