当前位置: 首页>>代码示例>>Python>>正文


Python array.byteswap方法代码示例

本文整理汇总了Python中array.array.byteswap方法的典型用法代码示例。如果您正苦于以下问题:Python array.byteswap方法的具体用法?Python array.byteswap怎么用?Python array.byteswap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在array.array的用法示例。


在下文中一共展示了array.byteswap方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_flashdevice_long_rw

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import byteswap [as 别名]
 def test_flashdevice_long_rw(self):
     # Fill in the whole flash with a monotonic increasing value, that is
     # the current flash 32-bit address, then verify the sequence has been
     # properly read back
     from hashlib import sha1
     buf = Array('I')
     length = len(self.flash)
     #length = 4096
     print "Build sequence"
     for address in range(0, length, 4):
         buf.append(address)
     # Expect to run on x86 or ARM (little endian), so swap the values
     # to ease debugging
     # A cleaner test would verify the host endianess, or use struct module
     print "Swap sequence"
     buf.byteswap()
     print "Erase flash (may take a while...)"
     self.flash.erase(0, length)
     # Cannot use buf, as it's an I-array, and SPI expects a B-array
     bufstr = buf.tostring()
     print "Write flash", len(bufstr)
     self.flash.write(0, bufstr)
     wmd = sha1()
     wmd.update(buf.tostring())
     refdigest = wmd.hexdigest()
     print "Read flash"
     data = self.flash.read(0, length)
     #print "Dump flash"
     #print hexdump(data.tostring())
     print "Verify flash"
     rmd = sha1()
     rmd.update(data.tostring())
     newdigest = rmd.hexdigest()
     print "Reference:", refdigest
     print "Retrieved:", newdigest
     if refdigest != newdigest:
         raise AssertionError('Data comparison mismatch')
开发者ID:etihwnad,项目名称:pyftdi,代码行数:39,代码来源:serialflash.py

示例2: test_rw_sector_1

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import byteswap [as 别名]
	def test_rw_sector_1(self):
		from hashlib import sha1
		buf = Array('I')
#		length = 65536
		length = len(self.flash)
		print "length: " + str(length)
		print "Build Sequence"
		for address in range (0, length):
			buf.append(address)
		print "Swap sequence"
		buf = buf.byteswap()
		#print "Erase flash from %08X to %08X" % (0, length)
		print "Erase all of the flash"
		self.flash.erase(0, len(self.flash))
		bufstr = buf.tostring()
		dout = Array('B')
		dout.fromstring(bufstr)
		self.flash.write(0, bufstr)
		print "Verify Flash"
		wmd = sha1()
		wmd.update(buf.tostring())
		refdigest = wmd.hexdigest()
		print "Read Flash"
		din = self.flash.read(0, length)
		print "Dump Flash"
		print hexdump(din.tostring())
		print "Verify Flash"
		rmd = sha1()
		rmd.update(din.tostring())
		newdigest = rmd.hexdigest()
		print "Reference: ", refdigest
		print "Retrieved: ", newdigest

		try:
			f = open("din.hex", "w")
			din.tofile(f)
			f.close()
		except IOError, err:
			print "Error writing to din file"
开发者ID:cospan,项目名称:sycamore,代码行数:41,代码来源:test_spi_flash.py

示例3: test_flashdevice_4_long_rw

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import byteswap [as 别名]
 def test_flashdevice_4_long_rw(self):
     """Long R/W test
     """
     # Max size to perform the test on
     size = 1<<20
     # Whether to test with random value, or contiguous values to ease debug
     randomize = True
     # Fill in the whole flash with a monotonic increasing value, that is
     # the current flash 32-bit address, then verify the sequence has been
     # properly read back
     from hashlib import sha1
     # limit the test to 1MiB to keep the test duration short, but performs
     # test at the end of the flash to verify that high addresses may be
     # reached
     length = min(len(self.flash), size)
     start = len(self.flash)-length
     print "Erase %s from flash @ 0x%06x(may take a while...)" % \
         (pretty_size(length), start)
     delta = time.time()
     self.flash.unlock()
     self.flash.erase(start, length, True)
     delta = time.time()-delta
     self._report_bw('Erased', length, delta)
     if str(self.flash).startswith('SST'):
         # SST25 flash devices are tremendously slow at writing (one or two
         # bytes per SPI request MAX...). So keep the test sequence short
         # enough
         length = 16<<10
     print "Build test sequence"
     if not randomize:
         buf = Array('I')
         back = Array('I')
         for address in range(0, length, 4):
             buf.append(address)
         # Expect to run on x86 or ARM (little endian), so swap the values
         # to ease debugging
         # A cleaner test would verify the host endianess, or use struct
         # module
         buf.byteswap()
         # Cannot use buf directly, as it's an I-array,
         # and SPI expects a B-array
     else:
         from random import seed
         seed(0)
         buf = Array('B')
         back = Array('B')
         buf.extend((randint(0, 255) for x in range(0, length)))
     bufstr = buf.tostring()
     print "Writing %s to flash (may take a while...)" % \
         pretty_size(len(bufstr))
     delta = time.time()
     self.flash.write(start, bufstr)
     delta = time.time()-delta
     length = len(bufstr)
     self._report_bw('Wrote', length, delta)
     wmd = sha1()
     wmd.update(buf.tostring())
     refdigest = wmd.hexdigest()
     print "Reading %s from flash" % pretty_size(length)
     delta = time.time()
     data = self.flash.read(start, length)
     delta = time.time()-delta
     self._report_bw('Read', length, delta)
     #print "Dump flash"
     #print hexdump(data.tostring())
     print "Verify flash"
     rmd = sha1()
     rmd.update(data.tostring())
     newdigest = rmd.hexdigest()
     print "Reference:", refdigest
     print "Retrieved:", newdigest
     if refdigest != newdigest:
         errcount = 0
         back.fromstring(data)
         for pos in xrange(len(buf)):
             if buf[pos] != data[pos]:
                 print 'Invalid byte @ offset 0x%06x: 0x%02x / 0x%02x' % \
                     (pos, buf[pos], back[pos])
                 errcount += 1
                 # Stop report after 16 errors
                 if errcount >= 32:
                     break
         raise AssertionError('Data comparison mismatch')
开发者ID:0xd3d0,项目名称:pyspiflash,代码行数:85,代码来源:serialflash.py


注:本文中的array.array.byteswap方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。