本文整理汇总了Python中xrootdpyfs.XRootDPyFile.write方法的典型用法代码示例。如果您正苦于以下问题:Python XRootDPyFile.write方法的具体用法?Python XRootDPyFile.write怎么用?Python XRootDPyFile.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xrootdpyfs.XRootDPyFile
的用法示例。
在下文中一共展示了XRootDPyFile.write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_init_append
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_init_append(tmppath):
"""Test for files opened 'a'"""
fd = get_tsta_file(tmppath)
fp, fc = fd['full_path'], fd['contents']
xfile = XRootDPyFile(mkurl(fp), 'a')
assert xfile.mode == 'a'
pytest.raises(IOError, xfile.read)
assert xfile.tell() == len(fc)
# Seeking is allowed, but writes still go on the end.
xfile.seek(0)
assert xfile.tell() == 0
newcont = u'butterflies'
xfile.write(newcont)
assert xfile.tell() == len(fc) + len(newcont)
# Can't read in this mode.
xfile.close()
xfile = XRootDPyFile(mkurl(fp), 'r')
assert xfile.read() == fc + newcont
xfile.close()
xfile = XRootDPyFile(mkurl(fp), 'a')
xfile.write(fc)
xfile.seek(0)
pytest.raises(IOError, xfile.read)
示例2: test_readwrite_unicode
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_readwrite_unicode(tmppath):
"""Test read/write unicode."""
if sys.getdefaultencoding() != "ascii":
# Python 2 only problem
raise AssertionError(
"Default system encoding is not ascii. This is likely due to some"
" imported module changing it using sys.setdefaultencoding."
)
fd = get_tsta_file(tmppath)
fb = get_copy_file(fd)
fp, dummy = fd["full_path"], fd["contents"]
fp2 = fb["full_path"]
unicodestr = u"æøå"
pfile = open(fp2, "w")
xfile = XRootDPyFile(mkurl(fp), "w")
pytest.raises(UnicodeEncodeError, pfile.write, unicodestr)
pytest.raises(UnicodeEncodeError, xfile.write, unicodestr)
xfile.close()
xfile = XRootDPyFile(mkurl(fp), "w+", encoding="utf-8")
xfile.write(unicodestr)
xfile.flush()
xfile.seek(0)
assert unicodestr.encode("utf8") == xfile.read()
xfile.close()
xfile = XRootDPyFile(mkurl(fp), "w+", errors="ignore")
xfile.write(unicodestr)
xfile.flush()
xfile.seek(0)
assert unicodestr.encode("ascii", "ignore") == xfile.read()
xfile.close()
示例3: test_read_and_write
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_read_and_write(tmppath):
"""Tests that the XRDFile behaves like a regular python file."""
fd = get_tsta_file(tmppath)
fb = get_copy_file(fd)
fp, fc = fd['full_path'], fd['contents']
fp2 = fb['full_path']
seekpoint = len(fc)//2
writestr = "Come what may in May this day says Ray all gay like Jay"
pfile = open(fp2, 'r+')
xfile = XRootDPyFile(mkurl(fp), 'r+')
assert xfile.tell() == pfile.tell()
assert xfile.read() == pfile.read()
assert xfile.tell() == pfile.tell()
xfile.seek(seekpoint), pfile.seek(seekpoint)
assert xfile.tell() == pfile.tell()
xfile.write(writestr), pfile.write(writestr)
assert xfile.tell() == pfile.tell()
assert xfile.read() == pfile.read()
xfile.seek(0), pfile.seek(0)
assert xfile.tell() == pfile.tell()
assert xfile.read() == pfile.read()
示例4: test_write_binary
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_write_binary(tmppath):
"""Tests for writing binary data to file."""
fd = get_bin_testfile(tmppath)
fp, fc = fd['full_path'], fd['contents']
# Test w/ confirmed binary data read from a binary file
xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'wb+')
xf_new.write(fc), xf_new.seek(0)
assert xf_new.read() == fc
xf_new.close()
# Verify persistence.
xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'r+')
assert xf_new.read() == fc
# Test truncate
xf_new.truncate()
xf_new.seek(0)
assert xf_new.read() == fc
xf_new.close()
# Test with bytearray
xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'wb+')
barr = bytearray(range(0, 5))
xf_new.write(barr), xf_new.seek(0)
assert xf_new.read() == barr
xf_new.close()
# Verify persistence.
xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'r')
assert xf_new.read() == barr
xf_new.close()
示例5: test_write_and_read
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_write_and_read(tmppath):
"""Tests that the XRootDPyFile behaves like a regular python file in w+."""
fd = get_tsta_file(tmppath)
fb = get_copy_file(fd)
fp, fc = fd['full_path'], fd['contents']
fp2 = fb['full_path']
writestr = "Hello fair mare what fine stairs."
seekpoint = len(writestr)//2
# In 'w' (and variant modes) the file's contents are deleted upon opening.
pfile = open(fp2, 'w+')
xfile = XRootDPyFile(mkurl(fp), 'w+')
assert xfile.tell() == pfile.tell()
assert xfile.read() == pfile.read()
assert xfile.tell() == pfile.tell()
xfile.write(writestr), pfile.write(writestr)
assert xfile.tell() == pfile.tell()
assert xfile.read() == pfile.read()
xfile.seek(0), pfile.seek(0)
assert xfile.read() == pfile.read()
assert xfile.tell() == pfile.tell()
xfile.seek(seekpoint), pfile.seek(seekpoint)
assert xfile.tell() == pfile.tell()
assert xfile.read() == pfile.read()
assert xfile.tell() == pfile.tell()
示例6: test_truncate_read_write2
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_truncate_read_write2(tmppath):
"""Tests behaviour of writing after seek(0) after
reading after truncating."""
fd = get_tsta_file(tmppath)
fb = get_copy_file(fd)
fp, fc = fd['full_path'], fd['contents']
fp2 = fb['full_path']
sp = len(fc)//2
wstr = "I am the string"
pfile = open(fp2, 'r+')
xfile = XRootDPyFile(mkurl(fp), 'r+')
xfile.truncate(sp), pfile.truncate(sp)
assert xfile.tell() == pfile.tell()
assert xfile.read() == pfile.read()
assert xfile.tell() == pfile.tell()
xfile.seek(0), pfile.seek(0)
assert xfile.tell() == pfile.tell()
assert xfile.read() == pfile.read()
xfile.seek(0), pfile.seek(0)
xfile.write(wstr), pfile.write(wstr)
assert xfile.tell() == pfile.tell()
assert xfile.read() == pfile.read()
xfile.seek(0), pfile.seek(0)
assert xfile.read() == pfile.read()
示例7: test_flush
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_flush(tmppath):
"""Tests for flush()"""
# Mostly it just ensures calling it doesn't crash the program.
fd = get_tsta_file(tmppath)
full_path, fc = fd['full_path'], fd['contents']
xfile = XRootDPyFile(mkurl(full_path), 'w')
writestr = 'whut'
xfile.flush()
xfile.seek(0, SEEK_END)
xfile.write(writestr)
xfile.flush()
xfile.close()
xfile = XRootDPyFile(mkurl(full_path), 'r')
assert xfile.read() == writestr
# Fake/mock an error response
fake_status = {
"status": 3,
"code": 0,
"ok": False,
"errno": errno.EREMOTE,
"error": True,
"message": '[FATAL] Remote I/O Error',
"fatal": True,
"shellcode": 51
}
# Assign mock return value to the file's sync() function
# (which is called by flush())
xfile._file.sync = Mock(return_value=(XRootDStatus(fake_status), None))
pytest.raises(IOError, xfile.flush)
示例8: test_write
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_write(tmppath):
"""Test write()."""
# With a new file.
xfile = XRootDPyFile(mkurl(join(tmppath, 'data/nuts')), 'w+')
assert xfile.size == 0
conts = xfile.read()
assert not conts
nconts = 'Write.'
xfile.write(nconts)
assert xfile.tell() == len(nconts)
assert not xfile.closed
xfile.seek(0)
assert xfile.size == len(nconts)
assert xfile.read() == nconts
xfile.close()
# Verify persistence after closing.
xfile = XRootDPyFile(mkurl(join(tmppath, 'data/nuts')), 'r+')
assert xfile.size == len(nconts)
assert xfile.read() == nconts
# Seek(x>0) followed by a write
nc2 = 'hello'
cntr = len(nconts)//2
xfile.seek(cntr)
xfile.write(nc2)
assert xfile.tell() == len(nc2) + cntr
xfile.seek(0)
assert xfile.read() == nconts[:cntr] + nc2
xfile.close()
# Seek(x>0) followed by a write of len < size-x
fd = get_tsta_file(tmppath)
fp, fc = fd['full_path'], fd['contents']
xfile = XRootDPyFile(mkurl(fp), 'r+')
assert xfile.read() == fc
xfile.seek(2)
nc = 'yo'
xfile.write(nc)
assert xfile.tell() == len(nc) + 2
assert xfile.read() == fc[2+len(nc):]
# run w/ flushing == true
xfile.write('', True)
# Mock an error, yayy!
fake_status = {
"status": 3,
"code": 0,
"ok": False,
"errno": errno.EREMOTE,
"error": True,
"message": '[FATAL] Remote I/O Error',
"fatal": True,
"shellcode": 51
}
xfile._file.write = Mock(return_value=(XRootDStatus(fake_status), None))
pytest.raises(IOError, xfile.write, '')
示例9: test_init_writemode
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_init_writemode(tmppath):
"""Tests for opening files in 'w(+)'"""
fd = get_tsta_file(tmppath)
fp, fc = fd['full_path'], fd['contents']
xfile = XRootDPyFile(mkurl(fp), 'w')
pytest.raises(IOError, xfile.read)
xfile.seek(1)
conts = 'what'
xfile.write(conts)
assert xfile.tell() == 1 + len(conts)
assert xfile.size == 1 + len(conts)
xfile.close()
xfile = XRootDPyFile(mkurl(fp), 'r')
fc = xfile.read()
assert fc == '\x00'+conts
assert not fc == conts
示例10: test_init_writemode
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_init_writemode(tmppath):
"""Tests for opening files in 'w(+)'"""
fd = get_tsta_file(tmppath)
fp, fc = fd["full_path"], fd["contents"]
xfile = XRootDPyFile(mkurl(fp), "w")
pytest.raises(IOError, xfile.read)
xfile.seek(1)
conts = "what"
xfile.write(conts)
assert xfile.tell() == 1 + len(conts)
assert xfile.size == 1 + len(conts)
xfile.close()
xfile = XRootDPyFile(mkurl(fp), "r")
fc = xfile.read()
assert fc == "\x00" + conts
assert not fc == conts
示例11: test_init_appendread
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_init_appendread(tmppath):
"""Test for files opened in mode 'a+'."""
fd = get_tsta_file(tmppath)
fp, fc = fd['full_path'], fd['contents']
xfile = XRootDPyFile(mkurl(fp), 'a+')
assert xfile.mode == 'a+'
assert xfile.tell() == len(fc)
assert xfile.read() == u''
# Seeking is allowed, but writes still go on the end.
xfile.seek(0)
assert xfile.tell() == 0
newcont = u'butterflies'
xfile.write(newcont)
assert xfile.tell() == len(fc) + len(newcont)
xfile.seek(0)
assert xfile.read() == fc + newcont
xfile.write(fc)
xfile.seek(0)
xfile.read() == fc + newcont + fc
示例12: test_init_streammodes
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_init_streammodes(tmppath):
fd = get_tsta_file(tmppath)
fp, fc = fd['full_path'], fd['contents']
xfile = XRootDPyFile(mkurl(fp), 'r-')
pytest.raises(IOError, xfile.seek, 3)
assert xfile.size == len(fc)
assert xfile.tell() == 0
assert xfile.read() == fc
assert xfile.tell() == len(fc)
xfile.close()
xfile = XRootDPyFile(mkurl(fp), 'w-')
pytest.raises(IOError, xfile.read)
pytest.raises(IOError, xfile.seek, 3)
assert xfile.tell() == 0
assert xfile.size == 0
conts = 'hugs are delightful'
xfile.write(conts)
assert xfile.tell() == len(conts)
xfile.close()
xfile = XRootDPyFile(mkurl(fp), 'r')
assert xfile.read() == conts
示例13: test_seek_past_eof_wr
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_seek_past_eof_wr(tmppath):
"""Tests read/write/truncate behaviour after seeking past the EOF, 'w+'"""
fd = get_tsta_file(tmppath)
fb = get_copy_file(fd)
fp, fc = fd['full_path'], u''
fp2 = fb['full_path']
wstr = "www"
eof = len(fc)
skpnt = len(fc)+4
pfile = open(fp2, 'w+')
xfile = XRootDPyFile(mkurl(fp), 'w+')
xfile.seek(skpnt), pfile.seek(skpnt)
assert xfile.tell() == pfile.tell()
assert xfile.read() == pfile.read()
assert xfile.tell() == pfile.tell()
assert xfile.tell() == skpnt
xfile.write(wstr), pfile.write(wstr)
assert xfile.tell() == pfile.tell()
xfile.seek(eof), pfile.seek(eof)
assert xfile.read() == pfile.read() == '\x00'*(skpnt-eof) + wstr
assert xfile.tell() == pfile.tell()
xfile.seek(0), pfile.seek(0)
assert xfile.read() == pfile.read()
xfile.truncate(skpnt), pfile.truncate(skpnt)
assert xfile.tell() == pfile.tell() == skpnt + len(wstr)
xfile.write(wstr), pfile.write(wstr)
expected = fc + '\x00'*(skpnt-eof+len(wstr)) + wstr
xfile.seek(0), pfile.seek(0)
assert xfile.read() == pfile.read() == expected
示例14: test_seek_past_eof_rw
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_seek_past_eof_rw(tmppath):
"""Tests read/write/truncate behaviour after seeking past the EOF, 'r+'."""
fd = get_tsta_file(tmppath)
fb = get_copy_file(fd)
fp, fc = fd["full_path"], fd["contents"]
fp2 = fb["full_path"]
wstr = "www"
eof = len(fc)
skpnt = len(fc) + 4
pfile = open(fp2, "r+")
xfile = XRootDPyFile(mkurl(fp), "r+")
xfile.seek(skpnt), pfile.seek(skpnt)
assert xfile.tell() == pfile.tell()
assert xfile.read() == pfile.read()
assert xfile.tell() == pfile.tell()
assert xfile.tell() == skpnt
xfile.write(wstr), pfile.write(wstr)
assert xfile.tell() == pfile.tell()
xfile.seek(eof), pfile.seek(eof)
assert xfile.read() == pfile.read() == "\x00" * (skpnt - eof) + wstr
assert xfile.tell() == pfile.tell()
xfile.seek(0), pfile.seek(0)
assert xfile.read() == pfile.read()
xfile.truncate(skpnt), pfile.truncate(skpnt)
assert xfile.tell() == pfile.tell() == skpnt + len(wstr)
xfile.write(wstr), pfile.write(wstr)
expected = fc + "\x00" * (skpnt - eof + len(wstr)) + wstr
xfile.seek(0), pfile.seek(0)
assert xfile.read() == pfile.read() == expected
示例15: test_readline
# 需要导入模块: from xrootdpyfs import XRootDPyFile [as 别名]
# 或者: from xrootdpyfs.XRootDPyFile import write [as 别名]
def test_readline(tmppath):
"""Tests for readline()."""
fd = get_mltl_file(tmppath)
fb = get_copy_file(fd)
fp, fc = fd['full_path'], fd['contents']
fp2 = fb['full_path']
xfile, pfile = XRootDPyFile(mkurl(fp), 'r'), opener.open(fp2, 'r')
assert xfile.readline() == pfile.readline()
assert xfile.readline() == pfile.readline()
assert xfile.readline() == pfile.readline()
xfile.close(), pfile.close()
xfile, pfile = XRootDPyFile(mkurl(fp), 'r'), opener.open(fp2, 'r')
assert xfile.readline() == pfile.readline()
xfile.seek(0), pfile.seek(0)
assert xfile.readline() == pfile.readline()
assert xfile.tell(), pfile.tell()
xfile.close(), pfile.close()
xfile = XRootDPyFile(mkurl(fp), 'w+')
str1 = 'hello\n'
str2 = 'bye\n'
xfile.write(str1+str2)
xfile.seek(0)
assert xfile.readline() == str1
assert xfile.readline() == str2
assert xfile.readline() == ''
assert xfile.readline() == ''
xfile.seek(100)
assert xfile.readline() == ''
xfile.close()
xfile = XRootDPyFile(mkurl(fp), 'w+')
xfile.write(str2)
xfile.seek(len(str2)+1)
xfile.write(str2)
xfile.seek(0)
assert xfile.readline() == str2
assert xfile.readline() == u'\x00'+str2