Python Library Stringtools
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. stringtools (Stringtools)
  6. =========================
  7. **Author:**
  8. * Dirk Alders <sudo-dirk@mount-mockery.de>
  9. **Description:**
  10. This Module supports functionality around string operations.
  11. **Submodules:**
  12. * :mod:`stringtools.csp`
  13. * :mod:`stringtools.stp`
  14. * :func:`gzip_compress`
  15. * :func:`gzip_extract`
  16. * :func:`hexlify`
  17. **Unittest:**
  18. See also the :download:`unittest <stringtools/_testresults_/unittest.pdf>` documentation.
  19. **Module Documentation:**
  20. """
  21. from stringtools import stp
  22. from stringtools import csp
  23. __DEPENDENCIES__ = []
  24. import fractions
  25. import gzip
  26. import logging
  27. import time
  28. import sys
  29. if sys.version_info < (3, 0):
  30. from cStringIO import StringIO
  31. logger_name = 'STRINGTOOLS'
  32. logger = logging.getLogger(logger_name)
  33. __DESCRIPTION__ = """The Module {\\tt %s} is designed to support functionality for strings (e.g. transfer strings via a bytestream, compressing, extracting, ...).
  34. For more Information read the sphinx documentation.""" % __name__.replace('_', '\_')
  35. """The Module Description"""
  36. __INTERPRETER__ = (2, 3)
  37. """The Tested Interpreter-Versions"""
  38. __all__ = ['gzip_compress',
  39. 'gzip_extract',
  40. 'hexlify',
  41. 'csp',
  42. 'stp']
  43. def physical_value_repr(value, unit=''):
  44. prefix = {
  45. -4: 'p',
  46. -3: 'n',
  47. -2: 'µ',
  48. -1: 'm',
  49. 0: '',
  50. 1: 'k',
  51. 2: 'M',
  52. 3: 'G',
  53. 4: 'T',
  54. 5: 'P',
  55. }
  56. u = 0
  57. while u > -4 and u < 5 and (value >= 1000. or value < 1.):
  58. if value >= 1:
  59. u += 1
  60. value /= 1000.
  61. else:
  62. u -= 1
  63. value *= 1000.
  64. if u == 0:
  65. ext = ''
  66. else:
  67. ext = prefix[u]
  68. #
  69. if value < 100.:
  70. value = '%.2f' % (value)
  71. else:
  72. value = '%.1f' % (value)
  73. while value.find('.') > -1 and (value.endswith('0') or value.endswith('.')):
  74. value = value[:-1]
  75. return value + ext + unit
  76. def time_repr(seconds):
  77. days = seconds / (24 * 60 * 60)
  78. seconds = seconds % (24 * 60 * 60)
  79. if seconds >= 60 * 60:
  80. rv = time.strftime('%H:%M:%S', time.gmtime(seconds))
  81. else:
  82. rv = time.strftime('%M:%S', time.gmtime(seconds))
  83. if days >= 1:
  84. rv = '%dD %s' % (days, rv)
  85. if rv.endswith(' 00:00'):
  86. rv = rv[:-6]
  87. return rv
  88. def frac_repr(value):
  89. f = fractions.Fraction(value).limit_denominator()
  90. return '%s/%s' % (f.numerator, f.denominator)
  91. def gzip_compress(s, compresslevel=9):
  92. """
  93. Method to compress a stream of bytes.
  94. :param str s: The bytestream (string) to be compressed
  95. :param int compresslevel: An optional compressionn level (default is 9)
  96. :return: The compressed bytestream
  97. :rtype: str
  98. **Example:**
  99. .. literalinclude:: ../examples/gzip_compress.py
  100. Will result to the following output:
  101. .. literalinclude:: ../examples/gzip_compress.log
  102. """
  103. rv = None
  104. t = time.time()
  105. if sys.version_info >= (3, 0):
  106. rv = gzip.compress(s, compresslevel)
  107. else:
  108. buf = StringIO()
  109. f = gzip.GzipFile(mode='wb', compresslevel=compresslevel, fileobj=buf)
  110. try:
  111. f.write(s)
  112. finally:
  113. f.close()
  114. rv = buf.getvalue()
  115. buf.close()
  116. if rv is not None:
  117. logger.debug('GZIP: Finished to compress a string (compression_rate=%.3f, consumed_time=%.1fs).', len(rv) / float(len(s)), time.time() - t)
  118. return rv
  119. def gzip_extract(s):
  120. """
  121. Method to extract data from a compress stream of bytes.
  122. :param str s: The compressed bytestream (string) to be extracted
  123. :return: The extracted data
  124. :rtype: str
  125. **Example:**
  126. .. literalinclude:: ../examples/gzip_extract.py
  127. Will result to the following output:
  128. .. literalinclude:: ../examples/gzip_extract.log
  129. """
  130. t = time.time()
  131. rv = None
  132. if sys.version_info >= (3, 0):
  133. rv = gzip.decompress(s)
  134. else:
  135. inbuffer = StringIO(s)
  136. f = gzip.GzipFile(mode='rb', fileobj=inbuffer)
  137. try:
  138. rv = f.read()
  139. finally:
  140. f.close()
  141. inbuffer.close()
  142. if rv is not None:
  143. logger.debug('GZIP: Finished to extract a string (compression_rate=%.3f, consumed_time=%.1fs).', len(s) / float(len(rv)), time.time() - t)
  144. return rv
  145. def hexlify(s):
  146. """Method to hexlify a string.
  147. :param str s: A string including the bytes to be hexlified.
  148. :returns: The hexlified string
  149. :rtype: str
  150. **Example:**
  151. .. literalinclude:: ../examples/hexlify.py
  152. Will result to the following output:
  153. .. literalinclude:: ../examples/hexlify.log
  154. """
  155. rv = '(%d):' % len(s)
  156. for byte in s:
  157. if sys.version_info >= (3, 0):
  158. rv += ' %02x' % byte
  159. else:
  160. rv += ' %02x' % ord(byte)
  161. return rv
  162. def linefeed_filter(s):
  163. """Method to change linefeed and carriage return to '\\\\n' and '\\\\r'
  164. :param str s: A string including carriage return and/ or linefeed.
  165. :returns: A string with converted carriage return and/ or linefeed.
  166. :rtype: str
  167. """
  168. if sys.version_info >= (3, 0):
  169. return s.replace(b'\r', b'\\r').replace(b'\n', b'\\n')
  170. else:
  171. return s.replace('\r', '\\r').replace('\n', '\\n')