Python Library Stringtools
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

__init__.py 5.5KB

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