You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tf_utils.cs 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.IO;
  3. namespace Tensorflow.Hub
  4. {
  5. internal class tf_utils
  6. {
  7. public static string bytes_to_readable_str(long? numBytes, bool includeB = false)
  8. {
  9. if (numBytes == null) return numBytes.ToString();
  10. var num = (double)numBytes;
  11. if (num < 1024)
  12. {
  13. return $"{(long)num}{(includeB ? "B" : "")}";
  14. }
  15. num /= 1 << 10;
  16. if (num < 1024)
  17. {
  18. return $"{num:F2}k{(includeB ? "B" : "")}";
  19. }
  20. num /= 1 << 10;
  21. if (num < 1024)
  22. {
  23. return $"{num:F2}M{(includeB ? "B" : "")}";
  24. }
  25. num /= 1 << 10;
  26. return $"{num:F2}G{(includeB ? "B" : "")}";
  27. }
  28. public static void atomic_write_string_to_file(string filename, string contents, bool overwrite)
  29. {
  30. var tempPath = $"{filename}.tmp.{Guid.NewGuid():N}";
  31. using (var fileStream = new FileStream(tempPath, FileMode.Create))
  32. {
  33. using (var writer = new StreamWriter(fileStream))
  34. {
  35. writer.Write(contents);
  36. writer.Flush();
  37. }
  38. }
  39. try
  40. {
  41. if (File.Exists(filename))
  42. {
  43. if (overwrite)
  44. {
  45. File.Delete(filename);
  46. File.Move(tempPath, filename);
  47. }
  48. }
  49. else
  50. {
  51. File.Move(tempPath, filename);
  52. }
  53. }
  54. catch
  55. {
  56. File.Delete(tempPath);
  57. throw;
  58. }
  59. }
  60. public static string absolute_path(string path)
  61. {
  62. if (path.Contains("://"))
  63. {
  64. return path;
  65. }
  66. return Path.GetFullPath(path);
  67. }
  68. }
  69. }