Posted By

alejandrombernardis on 04/21/11


Tagged

utils


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Priestd09


Py | Utils


 / Published in: Python
 

URL: http://www.alejandrob.com.ar/

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (c) 2011 The Octopus Apps Inc.
  5. # Licensed under the Apache License, Version 2.0 (the "License")
  6. #
  7. # Author: Alejandro M. Bernardis
  8. # Email: alejandro.m.bernardis at gmail.com
  9. # Created: Apr 5, 2011 2:35:29 AM
  10. #
  11. import os
  12. import re
  13. import hashlib
  14. import datetime
  15. import shutil
  16. import types
  17.  
  18. # Misc
  19. # ------------------------------------------------------------------------------
  20. def isPrimitive(value):
  21. """doc here..."""
  22. primitive_type = type(value);
  23. primitive_list = [types.IntType, types.FloatType, types.LongType,
  24. types.ComplexType, types.NoneType, types.StringType,
  25. types.BooleanType];
  26.  
  27. if primitive_type in primitive_list:
  28. return True;
  29.  
  30. return False;
  31.  
  32. def toStringAttributes(cls):
  33. """doc here..."""
  34. result = '';
  35.  
  36. for i in cls.__dict__:
  37. attribute_name = re.sub(r'^_(.+)__', '', i)
  38. attribute_value = cls.__getattribute__(i)
  39. attribute_class = attribute_value.__class__.__name__
  40. attribute_string = (result+'\n\t%s: "%s"')
  41.  
  42. if isPrimitive(attribute_value):
  43. result = attribute_string % (attribute_name, attribute_value);
  44.  
  45. else:
  46. result = attribute_string % (attribute_name, attribute_class);
  47.  
  48. return (cls.__class__.__name__+': %s') % result;
  49.  
  50. # File
  51. # ------------------------------------------------------------------------------
  52. def file_generate_name(prefix='', extension='.txt', length=32):
  53. """doc here..."""
  54. temp = hashlib.md5(str(datetime.datetime.now()))
  55. return '%s%s%s' % (prefix, temp.hexdigest()[0:length], extension)
  56.  
  57. def file_create(path, name=None):
  58. """doc here..."""
  59. if not name:
  60. name = file_generate_name()
  61.  
  62. path = os.path.join(path, name)
  63.  
  64. if not os.path.exists(path):
  65. try:
  66. f = open(path, 'wb')
  67. f.write('')
  68.  
  69. finally:
  70. f.close()
  71.  
  72. else:
  73. print 'File %s already exists' % path
  74.  
  75. def file_remove(path, name):
  76. """doc here..."""
  77. path = os.path.join(path, name)
  78.  
  79. if os.path.exists(path):
  80. os.unlink(path)
  81.  
  82. else:
  83. print 'File %s does not exists' % path
  84.  
  85. # Dir
  86. # ------------------------------------------------------------------------------
  87.  
  88. def mkdir(path, mode=0777, recursive=False):
  89. """doc here..."""
  90. if not os.path.exists(path):
  91. if not recursive:
  92. os.mkdir(path, mode)
  93.  
  94. else:
  95. os.makedirs(path, mode)
  96.  
  97. else:
  98. print 'Directory %s already exists' % path
  99.  
  100. def rmdir(path, recursive=False):
  101. """doc here..."""
  102. if not os.path.exists(path):
  103. if not recursive:
  104. os.rmdir(path)
  105.  
  106. else:
  107. shutil.rmtree(path)
  108.  
  109. else:
  110. print 'Directory %s does not exists' % path
  111.  
  112. # ------------------------------------------------------------------------------

Report this snippet  

You need to login to post a comment.