forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtf_utils.cs
More file actions
80 lines (69 loc) · 1.98 KB
/
tf_utils.cs
File metadata and controls
80 lines (69 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.IO;
namespace Tensorflow.Hub
{
internal class tf_utils
{
public static string bytes_to_readable_str(long? numBytes, bool includeB = false)
{
if (numBytes == null) return numBytes.ToString();
var num = (double)numBytes;
if (num < 1024)
{
return $"{(long)num}{(includeB ? "B" : "")}";
}
num /= 1 << 10;
if (num < 1024)
{
return $"{num:F2}k{(includeB ? "B" : "")}";
}
num /= 1 << 10;
if (num < 1024)
{
return $"{num:F2}M{(includeB ? "B" : "")}";
}
num /= 1 << 10;
return $"{num:F2}G{(includeB ? "B" : "")}";
}
public static void atomic_write_string_to_file(string filename, string contents, bool overwrite)
{
var tempPath = $"{filename}.tmp.{Guid.NewGuid():N}";
using (var fileStream = new FileStream(tempPath, FileMode.Create))
{
using (var writer = new StreamWriter(fileStream))
{
writer.Write(contents);
writer.Flush();
}
}
try
{
if (File.Exists(filename))
{
if (overwrite)
{
File.Delete(filename);
File.Move(tempPath, filename);
}
}
else
{
File.Move(tempPath, filename);
}
}
catch
{
File.Delete(tempPath);
throw;
}
}
public static string absolute_path(string path)
{
if (path.Contains("://"))
{
return path;
}
return Path.GetFullPath(path);
}
}
}