utkit 0.16.0__tar.gz → 0.17.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. {utkit-0.16.0 → utkit-0.17.0}/PKG-INFO +1 -1
  2. {utkit-0.16.0 → utkit-0.17.0}/pyproject.toml +1 -1
  3. utkit-0.17.0/src/utkit/documentation/.cache/1187733482332471048 +307 -0
  4. utkit-0.17.0/src/utkit/documentation/.cache/12954085884195152434 +4 -0
  5. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/3476900567878811119 +2 -2
  6. utkit-0.17.0/src/utkit/documentation/.cache/3541202890535126406 +259 -0
  7. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/index.md +2 -1
  8. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/utils/file.md +52 -3
  9. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/index.html +2 -1
  10. utkit-0.17.0/src/utkit/documentation/site/search.json +1 -0
  11. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/utils/file/index.html +165 -7
  12. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/utils/file.py +30 -1
  13. utkit-0.16.0/src/utkit/documentation/.cache/1187733482332471048 +0 -242
  14. utkit-0.16.0/src/utkit/documentation/.cache/12954085884195152434 +0 -4
  15. utkit-0.16.0/src/utkit/documentation/.cache/3541202890535126406 +0 -259
  16. utkit-0.16.0/src/utkit/documentation/site/search.json +0 -1
  17. {utkit-0.16.0 → utkit-0.17.0}/README.md +0 -0
  18. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/__init__.py +0 -0
  19. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/api/rate_limit/__init__.py +0 -0
  20. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/api/rate_limit/error.py +0 -0
  21. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/api/rate_limit/middleware.py +0 -0
  22. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/api/schema/query.py +0 -0
  23. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/auth/__init__.py +0 -0
  24. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/cli/__init__.py +0 -0
  25. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/communication/__init__.py +0 -0
  26. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/communication/mail/__init__.py +0 -0
  27. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/communication/mail/smtp.py +0 -0
  28. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/core/logging.py +0 -0
  29. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/.gitignore +0 -0
  30. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/10042715452264249772 +0 -0
  31. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/10986014347042203044 +0 -0
  32. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/11038628458439117958 +0 -0
  33. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/11306067049371264785 +0 -0
  34. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/13386297405578974681 +0 -0
  35. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/14061902412565698710 +0 -0
  36. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/14675097771134305488 +0 -0
  37. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/14685679530430393680 +0 -0
  38. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/15066256594918372991 +0 -0
  39. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/15312684519617069940 +0 -0
  40. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/15724107283881488306 +0 -0
  41. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/16166828772222643061 +0 -0
  42. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/17695336329032716848 +0 -0
  43. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/17985671397626253671 +0 -0
  44. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/2940665334038842907 +0 -0
  45. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/326316130724058699 +0 -0
  46. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/360188204203718330 +0 -0
  47. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/403326901327389294 +0 -0
  48. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/4969546423033644587 +0 -0
  49. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/5530722400779903482 +0 -0
  50. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/5696925338617156769 +0 -0
  51. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/5859600648538783245 +0 -0
  52. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/5974781288462474643 +0 -0
  53. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/5976522500417319979 +0 -0
  54. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/6210600303880993421 +0 -0
  55. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/6916343365748484443 +0 -0
  56. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/7345008268279881772 +0 -0
  57. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/9033698058957892258 +0 -0
  58. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/9102534330981319844 +0 -0
  59. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/9811225078116513287 +0 -0
  60. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/autorefs.json +0 -0
  61. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.cache/objects.inv +0 -0
  62. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/.gitignore +0 -0
  63. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/api/rate-limit.md +0 -0
  64. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/api/schema.md +0 -0
  65. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/auth.md +0 -0
  66. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/communication/mail.md +0 -0
  67. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/core/logging.md +0 -0
  68. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/images/logo-old.png +0 -0
  69. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/images/logo.png +0 -0
  70. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/privacy/mask.md +0 -0
  71. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/privacy/security.md +0 -0
  72. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/store/mongo.md +0 -0
  73. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/store/r2.md +0 -0
  74. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/store/redis.md +0 -0
  75. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/store/s3.md +0 -0
  76. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/stylesheets/extra.css +0 -0
  77. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/template/render.md +0 -0
  78. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/utils/pdf.md +0 -0
  79. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/utils/performance.md +0 -0
  80. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/docs/validators/data.md +0 -0
  81. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/404.html +0 -0
  82. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/api/rate-limit/index.html +0 -0
  83. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/api/schema/index.html +0 -0
  84. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/assets/images/favicon.png +0 -0
  85. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/assets/javascripts/LICENSE +0 -0
  86. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/assets/javascripts/bundle.d7f30b55.min.js +0 -0
  87. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/assets/javascripts/workers/search.b6b7e04f.min.js +0 -0
  88. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/assets/stylesheets/classic/main.f62f0af6.min.css +0 -0
  89. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/assets/stylesheets/classic/palette.7dc9a0ad.min.css +0 -0
  90. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/assets/stylesheets/modern/main.62c9b9cc.min.css +0 -0
  91. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/assets/stylesheets/modern/palette.dfe2e883.min.css +0 -0
  92. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/auth/index.html +0 -0
  93. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/communication/mail/index.html +0 -0
  94. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/core/logging/index.html +0 -0
  95. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/images/logo-old.png +0 -0
  96. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/images/logo.png +0 -0
  97. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/objects.inv +0 -0
  98. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/privacy/mask/index.html +0 -0
  99. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/privacy/security/index.html +0 -0
  100. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/sitemap.xml +0 -0
  101. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/store/mongo/index.html +0 -0
  102. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/store/r2/index.html +0 -0
  103. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/store/redis/index.html +0 -0
  104. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/store/s3/index.html +0 -0
  105. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/stylesheets/extra.css +0 -0
  106. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/template/render/index.html +0 -0
  107. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/utils/pdf/index.html +0 -0
  108. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/utils/performance/index.html +0 -0
  109. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/site/validators/data/index.html +0 -0
  110. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/documentation/zensical.toml +0 -0
  111. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/privacy/__init__.py +0 -0
  112. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/privacy/mask.py +0 -0
  113. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/privacy/security.py +0 -0
  114. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/store/aws/__init__.py +0 -0
  115. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/store/aws/s3.py +0 -0
  116. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/store/cloudflare/r2.py +0 -0
  117. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/store/mongo/__init__.py +0 -0
  118. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/store/redis/__init__.py +0 -0
  119. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/template/__init__.py +0 -0
  120. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/template/render.py +0 -0
  121. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/utils/pdf.py +0 -0
  122. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/utils/performance.py +0 -0
  123. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/validators/__init__.py +0 -0
  124. {utkit-0.16.0 → utkit-0.17.0}/src/utkit/validators/data.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: utkit
3
- Version: 0.16.0
3
+ Version: 0.17.0
4
4
  Summary: core libraries for development
5
5
  Author: TINS PJ
6
6
  Author-email: TINS PJ <tinspj1997@gmail.com>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "utkit"
3
- version = "0.16.0"
3
+ version = "0.17.0"
4
4
  description = "core libraries for development"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -0,0 +1,307 @@
1
+ {
2
+ "data": {
3
+ "meta": {
4
+ "icon": "lucide/file"
5
+ },
6
+ "content": "<h1 id=\"file-utilities\">File utilities<a class=\"headerlink\" href=\"#file-utilities\" title=\"Permanent link\">&para;</a></h1>\n<p>The <code>utkit.utils.file</code> module provides lightweight helpers for working with files: computing content checksums, identifying file types from binary content, getting file sizes in human-readable units, and encoding files for transfer or display.</p>\n<hr />\n<h2 id=\"installation\">Installation<a class=\"headerlink\" href=\"#installation\" title=\"Permanent link\">&para;</a></h2>\n<p><code>python-magic</code> is required for content-based file type detection. Install the <code>standard</code> extra:</p>\n<div class=\"language-bash highlight\"><pre><span></span><code><span id=\"__span-0-1\"><a id=\"__codelineno-0-1\" name=\"__codelineno-0-1\" href=\"#__codelineno-0-1\"></a>pip<span class=\"w\"> </span>install<span class=\"w\"> </span><span class=\"s2\">&quot;utkit[standard]&quot;</span>\n</span></code></pre></div>\n<p>Or with <a href=\"https://docs.astral.sh/uv/\">uv</a>:</p>\n<div class=\"language-bash highlight\"><pre><span></span><code><span id=\"__span-1-1\"><a id=\"__codelineno-1-1\" name=\"__codelineno-1-1\" href=\"#__codelineno-1-1\"></a>uv<span class=\"w\"> </span>add<span class=\"w\"> </span><span class=\"s2\">&quot;utkit[standard]&quot;</span>\n</span></code></pre></div>\n<blockquote>\n<p><code>get_file_checksum</code>, <code>get_file_size</code> and <code>encode_file</code> use only the Python standard library. <code>get_file_type</code> requires <code>python-magic</code>.</p>\n</blockquote>\n<hr />\n<h2 id=\"quick-start\">Quick start<a class=\"headerlink\" href=\"#quick-start\" title=\"Permanent link\">&para;</a></h2>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-2-1\"><a id=\"__codelineno-2-1\" name=\"__codelineno-2-1\" href=\"#__codelineno-2-1\"></a><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">utkit.utils.file</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">encode_file</span><span class=\"p\">,</span> <span class=\"n\">get_file_checksum</span><span class=\"p\">,</span> <span class=\"n\">get_file_size</span><span class=\"p\">,</span> <span class=\"n\">get_file_type</span>\n</span><span id=\"__span-2-2\"><a id=\"__codelineno-2-2\" name=\"__codelineno-2-2\" href=\"#__codelineno-2-2\"></a>\n</span><span id=\"__span-2-3\"><a id=\"__codelineno-2-3\" name=\"__codelineno-2-3\" href=\"#__codelineno-2-3\"></a><span class=\"c1\"># Compute a SHA-256 checksum</span>\n</span><span id=\"__span-2-4\"><a id=\"__codelineno-2-4\" name=\"__codelineno-2-4\" href=\"#__codelineno-2-4\"></a><span class=\"n\">checksum</span> <span class=\"o\">=</span> <span class=\"n\">get_file_checksum</span><span class=\"p\">(</span><span class=\"s2\">&quot;report.pdf&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-2-5\"><a id=\"__codelineno-2-5\" name=\"__codelineno-2-5\" href=\"#__codelineno-2-5\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">checksum</span><span class=\"p\">)</span> <span class=\"c1\"># &quot;9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08&quot;</span>\n</span><span id=\"__span-2-6\"><a id=\"__codelineno-2-6\" name=\"__codelineno-2-6\" href=\"#__codelineno-2-6\"></a>\n</span><span id=\"__span-2-7\"><a id=\"__codelineno-2-7\" name=\"__codelineno-2-7\" href=\"#__codelineno-2-7\"></a><span class=\"c1\"># Identify a file type from its content</span>\n</span><span id=\"__span-2-8\"><a id=\"__codelineno-2-8\" name=\"__codelineno-2-8\" href=\"#__codelineno-2-8\"></a><span class=\"n\">file_type</span> <span class=\"o\">=</span> <span class=\"n\">get_file_type</span><span class=\"p\">(</span><span class=\"s2\">&quot;report.pdf&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-2-9\"><a id=\"__codelineno-2-9\" name=\"__codelineno-2-9\" href=\"#__codelineno-2-9\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">file_type</span><span class=\"p\">)</span> <span class=\"c1\"># &quot;pdf&quot;</span>\n</span><span id=\"__span-2-10\"><a id=\"__codelineno-2-10\" name=\"__codelineno-2-10\" href=\"#__codelineno-2-10\"></a>\n</span><span id=\"__span-2-11\"><a id=\"__codelineno-2-11\" name=\"__codelineno-2-11\" href=\"#__codelineno-2-11\"></a><span class=\"c1\"># Get the file size in kilobytes</span>\n</span><span id=\"__span-2-12\"><a id=\"__codelineno-2-12\" name=\"__codelineno-2-12\" href=\"#__codelineno-2-12\"></a><span class=\"n\">size_kb</span> <span class=\"o\">=</span> <span class=\"n\">get_file_size</span><span class=\"p\">(</span><span class=\"s2\">&quot;report.pdf&quot;</span><span class=\"p\">,</span> <span class=\"n\">unit</span><span class=\"o\">=</span><span class=\"s2\">&quot;KB&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-2-13\"><a id=\"__codelineno-2-13\" name=\"__codelineno-2-13\" href=\"#__codelineno-2-13\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">size_kb</span><span class=\"p\">)</span> <span class=\"c1\"># 245.83</span>\n</span><span id=\"__span-2-14\"><a id=\"__codelineno-2-14\" name=\"__codelineno-2-14\" href=\"#__codelineno-2-14\"></a>\n</span><span id=\"__span-2-15\"><a id=\"__codelineno-2-15\" name=\"__codelineno-2-15\" href=\"#__codelineno-2-15\"></a><span class=\"c1\"># Encode a file as Base64 with MIME type detection</span>\n</span><span id=\"__span-2-16\"><a id=\"__codelineno-2-16\" name=\"__codelineno-2-16\" href=\"#__codelineno-2-16\"></a><span class=\"n\">encoded</span> <span class=\"o\">=</span> <span class=\"n\">encode_file</span><span class=\"p\">(</span><span class=\"s2\">&quot;report.pdf&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-2-17\"><a id=\"__codelineno-2-17\" name=\"__codelineno-2-17\" href=\"#__codelineno-2-17\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">encoded</span><span class=\"p\">[</span><span class=\"s2\">&quot;mime_type&quot;</span><span class=\"p\">])</span> <span class=\"c1\"># &quot;application/pdf&quot;</span>\n</span><span id=\"__span-2-18\"><a id=\"__codelineno-2-18\" name=\"__codelineno-2-18\" href=\"#__codelineno-2-18\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">encoded</span><span class=\"p\">[</span><span class=\"s2\">&quot;url&quot;</span><span class=\"p\">])</span> <span class=\"c1\"># &quot;data:application/pdf;base64,...&quot;</span>\n</span></code></pre></div>\n<hr />\n<h2 id=\"get_file_checksum\"><code>get_file_checksum</code><a class=\"headerlink\" href=\"#get_file_checksum\" title=\"Permanent link\">&para;</a></h2>\n<p>Calculate a file checksum by reading the file in chunks. Reading in chunks keeps memory usage low even for very large files.</p>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-3-1\"><a id=\"__codelineno-3-1\" name=\"__codelineno-3-1\" href=\"#__codelineno-3-1\"></a><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">get_file_checksum</span><span class=\"p\">(</span><span class=\"n\">file_path</span><span class=\"p\">:</span> <span class=\"nb\">str</span><span class=\"p\">,</span> <span class=\"n\">algorithm</span><span class=\"p\">:</span> <span class=\"nb\">str</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;sha256&quot;</span><span class=\"p\">,</span> <span class=\"n\">chunk_size</span><span class=\"p\">:</span> <span class=\"nb\">int</span> <span class=\"o\">=</span> <span class=\"mi\">8192</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">str</span>\n</span></code></pre></div>\n<table>\n<thead>\n<tr>\n<th>Parameter</th>\n<th>Type</th>\n<th>Default</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>file_path</code></td>\n<td><code>str</code></td>\n<td>—</td>\n<td>Path to the file to checksum.</td>\n</tr>\n<tr>\n<td><code>algorithm</code></td>\n<td><code>str</code></td>\n<td><code>\"sha256\"</code></td>\n<td>Hash algorithm supported by <code>hashlib</code> (e.g. <code>\"md5\"</code>, <code>\"sha1\"</code>, <code>\"sha256\"</code>, <code>\"sha512\"</code>).</td>\n</tr>\n<tr>\n<td><code>chunk_size</code></td>\n<td><code>int</code></td>\n<td><code>8192</code></td>\n<td>Number of bytes to read at a time.</td>\n</tr>\n</tbody>\n</table>\n<p><strong>Returns:</strong> <code>str</code> — Hex digest of the file checksum.</p>\n<h3 id=\"default-algorithm-sha-256\">Default algorithm (SHA-256)<a class=\"headerlink\" href=\"#default-algorithm-sha-256\" title=\"Permanent link\">&para;</a></h3>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-4-1\"><a id=\"__codelineno-4-1\" name=\"__codelineno-4-1\" href=\"#__codelineno-4-1\"></a><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">utkit.utils.file</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_file_checksum</span>\n</span><span id=\"__span-4-2\"><a id=\"__codelineno-4-2\" name=\"__codelineno-4-2\" href=\"#__codelineno-4-2\"></a>\n</span><span id=\"__span-4-3\"><a id=\"__codelineno-4-3\" name=\"__codelineno-4-3\" href=\"#__codelineno-4-3\"></a><span class=\"n\">checksum</span> <span class=\"o\">=</span> <span class=\"n\">get_file_checksum</span><span class=\"p\">(</span><span class=\"s2\">&quot;data.csv&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-4-4\"><a id=\"__codelineno-4-4\" name=\"__codelineno-4-4\" href=\"#__codelineno-4-4\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">checksum</span><span class=\"p\">)</span> <span class=\"c1\"># &quot;a1b2c3...64-hex-characters&quot;</span>\n</span></code></pre></div>\n<h3 id=\"custom-algorithm\">Custom algorithm<a class=\"headerlink\" href=\"#custom-algorithm\" title=\"Permanent link\">&para;</a></h3>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-5-1\"><a id=\"__codelineno-5-1\" name=\"__codelineno-5-1\" href=\"#__codelineno-5-1\"></a><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">utkit.utils.file</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_file_checksum</span>\n</span><span id=\"__span-5-2\"><a id=\"__codelineno-5-2\" name=\"__codelineno-5-2\" href=\"#__codelineno-5-2\"></a>\n</span><span id=\"__span-5-3\"><a id=\"__codelineno-5-3\" name=\"__codelineno-5-3\" href=\"#__codelineno-5-3\"></a><span class=\"c1\"># Use SHA-1 instead of the default SHA-256</span>\n</span><span id=\"__span-5-4\"><a id=\"__codelineno-5-4\" name=\"__codelineno-5-4\" href=\"#__codelineno-5-4\"></a><span class=\"n\">checksum</span> <span class=\"o\">=</span> <span class=\"n\">get_file_checksum</span><span class=\"p\">(</span><span class=\"s2\">&quot;data.csv&quot;</span><span class=\"p\">,</span> <span class=\"n\">algorithm</span><span class=\"o\">=</span><span class=\"s2\">&quot;sha1&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-5-5\"><a id=\"__codelineno-5-5\" name=\"__codelineno-5-5\" href=\"#__codelineno-5-5\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">checksum</span><span class=\"p\">)</span> <span class=\"c1\"># &quot;da39a3ee5e6b4b0d3255bfef95601890afd80709&quot;</span>\n</span></code></pre></div>\n<h3 id=\"verifying-file-integrity\">Verifying file integrity<a class=\"headerlink\" href=\"#verifying-file-integrity\" title=\"Permanent link\">&para;</a></h3>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-6-1\"><a id=\"__codelineno-6-1\" name=\"__codelineno-6-1\" href=\"#__codelineno-6-1\"></a><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">utkit.utils.file</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_file_checksum</span>\n</span><span id=\"__span-6-2\"><a id=\"__codelineno-6-2\" name=\"__codelineno-6-2\" href=\"#__codelineno-6-2\"></a>\n</span><span id=\"__span-6-3\"><a id=\"__codelineno-6-3\" name=\"__codelineno-6-3\" href=\"#__codelineno-6-3\"></a><span class=\"n\">expected</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08&quot;</span>\n</span><span id=\"__span-6-4\"><a id=\"__codelineno-6-4\" name=\"__codelineno-6-4\" href=\"#__codelineno-6-4\"></a>\n</span><span id=\"__span-6-5\"><a id=\"__codelineno-6-5\" name=\"__codelineno-6-5\" href=\"#__codelineno-6-5\"></a><span class=\"k\">if</span> <span class=\"n\">get_file_checksum</span><span class=\"p\">(</span><span class=\"s2\">&quot;report.pdf&quot;</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"n\">expected</span><span class=\"p\">:</span>\n</span><span id=\"__span-6-6\"><a id=\"__codelineno-6-6\" name=\"__codelineno-6-6\" href=\"#__codelineno-6-6\"></a> <span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;File is intact&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-6-7\"><a id=\"__codelineno-6-7\" name=\"__codelineno-6-7\" href=\"#__codelineno-6-7\"></a><span class=\"k\">else</span><span class=\"p\">:</span>\n</span><span id=\"__span-6-8\"><a id=\"__codelineno-6-8\" name=\"__codelineno-6-8\" href=\"#__codelineno-6-8\"></a> <span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;File has been modified or corrupted&quot;</span><span class=\"p\">)</span>\n</span></code></pre></div>\n<hr />\n<h2 id=\"get_file_type\"><code>get_file_type</code><a class=\"headerlink\" href=\"#get_file_type\" title=\"Permanent link\">&para;</a></h2>\n<p>Identify a file's type based on its content using magic numbers, rather than its extension. This is more reliable than trusting a file suffix.</p>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-7-1\"><a id=\"__codelineno-7-1\" name=\"__codelineno-7-1\" href=\"#__codelineno-7-1\"></a><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">get_file_type</span><span class=\"p\">(</span><span class=\"n\">file_path</span><span class=\"p\">:</span> <span class=\"nb\">str</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">str</span>\n</span></code></pre></div>\n<table>\n<thead>\n<tr>\n<th>Parameter</th>\n<th>Type</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>file_path</code></td>\n<td><code>str</code></td>\n<td>Path to the file to identify.</td>\n</tr>\n</tbody>\n</table>\n<p><strong>Returns:</strong> <code>str</code> — File type identifier. One of: <code>\"pdf\"</code>, <code>\"docx\"</code>, <code>\"csv\"</code>, <code>\"xlsx\"</code>, <code>\"xls\"</code>, <code>\"png\"</code>, <code>\"jpeg\"</code>, <code>\"jpg\"</code>. Returns <code>\"unknown\"</code> for any unrecognised type.</p>\n<h3 id=\"identifying-a-document\">Identifying a document<a class=\"headerlink\" href=\"#identifying-a-document\" title=\"Permanent link\">&para;</a></h3>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-8-1\"><a id=\"__codelineno-8-1\" name=\"__codelineno-8-1\" href=\"#__codelineno-8-1\"></a><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">utkit.utils.file</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_file_type</span>\n</span><span id=\"__span-8-2\"><a id=\"__codelineno-8-2\" name=\"__codelineno-8-2\" href=\"#__codelineno-8-2\"></a>\n</span><span id=\"__span-8-3\"><a id=\"__codelineno-8-3\" name=\"__codelineno-8-3\" href=\"#__codelineno-8-3\"></a><span class=\"n\">file_type</span> <span class=\"o\">=</span> <span class=\"n\">get_file_type</span><span class=\"p\">(</span><span class=\"s2\">&quot;document.docx&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-8-4\"><a id=\"__codelineno-8-4\" name=\"__codelineno-8-4\" href=\"#__codelineno-8-4\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">file_type</span><span class=\"p\">)</span> <span class=\"c1\"># &quot;docx&quot;</span>\n</span></code></pre></div>\n<h3 id=\"identifying-an-image\">Identifying an image<a class=\"headerlink\" href=\"#identifying-an-image\" title=\"Permanent link\">&para;</a></h3>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-9-1\"><a id=\"__codelineno-9-1\" name=\"__codelineno-9-1\" href=\"#__codelineno-9-1\"></a><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">utkit.utils.file</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_file_type</span>\n</span><span id=\"__span-9-2\"><a id=\"__codelineno-9-2\" name=\"__codelineno-9-2\" href=\"#__codelineno-9-2\"></a>\n</span><span id=\"__span-9-3\"><a id=\"__codelineno-9-3\" name=\"__codelineno-9-3\" href=\"#__codelineno-9-3\"></a><span class=\"n\">file_type</span> <span class=\"o\">=</span> <span class=\"n\">get_file_type</span><span class=\"p\">(</span><span class=\"s2\">&quot;photo.jpg&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-9-4\"><a id=\"__codelineno-9-4\" name=\"__codelineno-9-4\" href=\"#__codelineno-9-4\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">file_type</span><span class=\"p\">)</span> <span class=\"c1\"># &quot;jpg&quot;</span>\n</span></code></pre></div>\n<h3 id=\"handling-unknown-types\">Handling unknown types<a class=\"headerlink\" href=\"#handling-unknown-types\" title=\"Permanent link\">&para;</a></h3>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-10-1\"><a id=\"__codelineno-10-1\" name=\"__codelineno-10-1\" href=\"#__codelineno-10-1\"></a><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">utkit.utils.file</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_file_type</span>\n</span><span id=\"__span-10-2\"><a id=\"__codelineno-10-2\" name=\"__codelineno-10-2\" href=\"#__codelineno-10-2\"></a>\n</span><span id=\"__span-10-3\"><a id=\"__codelineno-10-3\" name=\"__codelineno-10-3\" href=\"#__codelineno-10-3\"></a><span class=\"n\">file_type</span> <span class=\"o\">=</span> <span class=\"n\">get_file_type</span><span class=\"p\">(</span><span class=\"s2\">&quot;notes.txt&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-10-4\"><a id=\"__codelineno-10-4\" name=\"__codelineno-10-4\" href=\"#__codelineno-10-4\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">file_type</span><span class=\"p\">)</span> <span class=\"c1\"># &quot;unknown&quot; (plain text is not in the recognised set)</span>\n</span></code></pre></div>\n<hr />\n<h2 id=\"encode_file\"><code>encode_file</code><a class=\"headerlink\" href=\"#encode_file\" title=\"Permanent link\">&para;</a></h2>\n<p>Encode a file as Base64 and return a dictionary containing the MIME type, encoded content, and a data URL.</p>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-11-1\"><a id=\"__codelineno-11-1\" name=\"__codelineno-11-1\" href=\"#__codelineno-11-1\"></a><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">encode_file</span><span class=\"p\">(</span><span class=\"n\">file_path</span><span class=\"p\">:</span> <span class=\"nb\">str</span> <span class=\"o\">|</span> <span class=\"n\">Path</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">dict</span><span class=\"p\">[</span><span class=\"nb\">str</span><span class=\"p\">,</span> <span class=\"nb\">str</span><span class=\"p\">]</span>\n</span></code></pre></div>\n<table>\n<thead>\n<tr>\n<th>Parameter</th>\n<th>Type</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>file_path</code></td>\n<td><code>str \\| Path</code></td>\n<td>Path to the file to encode.</td>\n</tr>\n</tbody>\n</table>\n<p><strong>Returns:</strong> <code>dict[str, str]</code> — A dictionary with three keys:</p>\n<table>\n<thead>\n<tr>\n<th>Key</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>mime_type</code></td>\n<td>MIME type detected from the file extension</td>\n</tr>\n<tr>\n<td><code>data</code></td>\n<td>Base64-encoded file content</td>\n</tr>\n<tr>\n<td><code>url</code></td>\n<td>Data URL combining MIME type and encoded content</td>\n</tr>\n</tbody>\n</table>\n<h3 id=\"basic-encoding\">Basic encoding<a class=\"headerlink\" href=\"#basic-encoding\" title=\"Permanent link\">&para;</a></h3>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-12-1\"><a id=\"__codelineno-12-1\" name=\"__codelineno-12-1\" href=\"#__codelineno-12-1\"></a><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">utkit.utils.file</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">encode_file</span>\n</span><span id=\"__span-12-2\"><a id=\"__codelineno-12-2\" name=\"__codelineno-12-2\" href=\"#__codelineno-12-2\"></a>\n</span><span id=\"__span-12-3\"><a id=\"__codelineno-12-3\" name=\"__codelineno-12-3\" href=\"#__codelineno-12-3\"></a><span class=\"n\">result</span> <span class=\"o\">=</span> <span class=\"n\">encode_file</span><span class=\"p\">(</span><span class=\"s2\">&quot;document.pdf&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-12-4\"><a id=\"__codelineno-12-4\" name=\"__codelineno-12-4\" href=\"#__codelineno-12-4\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">result</span><span class=\"p\">)</span>\n</span><span id=\"__span-12-5\"><a id=\"__codelineno-12-5\" name=\"__codelineno-12-5\" href=\"#__codelineno-12-5\"></a><span class=\"c1\"># {</span>\n</span><span id=\"__span-12-6\"><a id=\"__codelineno-12-6\" name=\"__codelineno-12-6\" href=\"#__codelineno-12-6\"></a><span class=\"c1\"># &quot;mime_type&quot;: &quot;application/pdf&quot;,</span>\n</span><span id=\"__span-12-7\"><a id=\"__codelineno-12-7\" name=\"__codelineno-12-7\" href=\"#__codelineno-12-7\"></a><span class=\"c1\"># &quot;data&quot;: &quot;...base64-encoded-content...&quot;,</span>\n</span><span id=\"__span-12-8\"><a id=\"__codelineno-12-8\" name=\"__codelineno-12-8\" href=\"#__codelineno-12-8\"></a><span class=\"c1\"># &quot;url&quot;: &quot;data:application/pdf;base64,...&quot;</span>\n</span><span id=\"__span-12-9\"><a id=\"__codelineno-12-9\" name=\"__codelineno-12-9\" href=\"#__codelineno-12-9\"></a><span class=\"c1\"># }</span>\n</span></code></pre></div>\n<h3 id=\"using-the-data-url\">Using the data URL<a class=\"headerlink\" href=\"#using-the-data-url\" title=\"Permanent link\">&para;</a></h3>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-13-1\"><a id=\"__codelineno-13-1\" name=\"__codelineno-13-1\" href=\"#__codelineno-13-1\"></a><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">utkit.utils.file</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">encode_file</span>\n</span><span id=\"__span-13-2\"><a id=\"__codelineno-13-2\" name=\"__codelineno-13-2\" href=\"#__codelineno-13-2\"></a>\n</span><span id=\"__span-13-3\"><a id=\"__codelineno-13-3\" name=\"__codelineno-13-3\" href=\"#__codelineno-13-3\"></a><span class=\"n\">result</span> <span class=\"o\">=</span> <span class=\"n\">encode_file</span><span class=\"p\">(</span><span class=\"s2\">&quot;image.png&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-13-4\"><a id=\"__codelineno-13-4\" name=\"__codelineno-13-4\" href=\"#__codelineno-13-4\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"sa\">f</span><span class=\"s2\">&quot;Data URL: </span><span class=\"si\">{</span><span class=\"n\">result</span><span class=\"p\">[</span><span class=\"s1\">&#39;url&#39;</span><span class=\"p\">]</span><span class=\"si\">}</span><span class=\"s2\">&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-13-5\"><a id=\"__codelineno-13-5\" name=\"__codelineno-13-5\" href=\"#__codelineno-13-5\"></a><span class=\"c1\"># Data URL can be used directly in HTML img tags or other applications</span>\n</span></code></pre></div>\n<hr />\n<h2 id=\"get_file_size\"><code>get_file_size</code><a class=\"headerlink\" href=\"#get_file_size\" title=\"Permanent link\">&para;</a></h2>\n<p>Return the size of a file in the requested unit. Supports bytes (B), kilobytes (KB), megabytes (MB), gigabytes (GB), and terabytes (TB).</p>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-14-1\"><a id=\"__codelineno-14-1\" name=\"__codelineno-14-1\" href=\"#__codelineno-14-1\"></a><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">get_file_size</span><span class=\"p\">(</span><span class=\"n\">path</span><span class=\"p\">:</span> <span class=\"nb\">str</span> <span class=\"o\">|</span> <span class=\"n\">Path</span><span class=\"p\">,</span> <span class=\"n\">unit</span><span class=\"p\">:</span> <span class=\"n\">SizeUnit</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;B&quot;</span><span class=\"p\">,</span> <span class=\"n\">precision</span><span class=\"p\">:</span> <span class=\"nb\">int</span> <span class=\"o\">=</span> <span class=\"mi\">2</span><span class=\"p\">)</span> <span class=\"o\">-&gt;</span> <span class=\"nb\">float</span>\n</span></code></pre></div>\n<table>\n<thead>\n<tr>\n<th>Parameter</th>\n<th>Type</th>\n<th>Default</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>path</code></td>\n<td><code>str \\| Path</code></td>\n<td>—</td>\n<td>Path to the file.</td>\n</tr>\n<tr>\n<td><code>unit</code></td>\n<td><code>SizeUnit</code></td>\n<td><code>\"B\"</code></td>\n<td>Unit of measurement: <code>\"B\"</code>, <code>\"KB\"</code>, <code>\"MB\"</code>, <code>\"GB\"</code>, or <code>\"TB\"</code>.</td>\n</tr>\n<tr>\n<td><code>precision</code></td>\n<td><code>int</code></td>\n<td><code>2</code></td>\n<td>Number of decimal places to round to.</td>\n</tr>\n</tbody>\n</table>\n<p><strong>Returns:</strong> <code>float</code> — File size in the requested unit.</p>\n<h3 id=\"default-unit-b\">Default unit (B)<a class=\"headerlink\" href=\"#default-unit-b\" title=\"Permanent link\">&para;</a></h3>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-15-1\"><a id=\"__codelineno-15-1\" name=\"__codelineno-15-1\" href=\"#__codelineno-15-1\"></a><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">utkit.utils.file</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_file_size</span>\n</span><span id=\"__span-15-2\"><a id=\"__codelineno-15-2\" name=\"__codelineno-15-2\" href=\"#__codelineno-15-2\"></a>\n</span><span id=\"__span-15-3\"><a id=\"__codelineno-15-3\" name=\"__codelineno-15-3\" href=\"#__codelineno-15-3\"></a><span class=\"n\">size</span> <span class=\"o\">=</span> <span class=\"n\">get_file_size</span><span class=\"p\">(</span><span class=\"s2\">&quot;chart.png&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-15-4\"><a id=\"__codelineno-15-4\" name=\"__codelineno-15-4\" href=\"#__codelineno-15-4\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">size</span><span class=\"p\">)</span> <span class=\"c1\"># 145062.83</span>\n</span></code></pre></div>\n<h3 id=\"custom-unit\">Custom unit<a class=\"headerlink\" href=\"#custom-unit\" title=\"Permanent link\">&para;</a></h3>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-16-1\"><a id=\"__codelineno-16-1\" name=\"__codelineno-16-1\" href=\"#__codelineno-16-1\"></a><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">utkit.utils.file</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_file_size</span>\n</span><span id=\"__span-16-2\"><a id=\"__codelineno-16-2\" name=\"__codelineno-16-2\" href=\"#__codelineno-16-2\"></a>\n</span><span id=\"__span-16-3\"><a id=\"__codelineno-16-3\" name=\"__codelineno-16-3\" href=\"#__codelineno-16-3\"></a><span class=\"n\">size</span> <span class=\"o\">=</span> <span class=\"n\">get_file_size</span><span class=\"p\">(</span><span class=\"s2\">&quot;chart.png&quot;</span><span class=\"p\">,</span> <span class=\"n\">unit</span><span class=\"o\">=</span><span class=\"s2\">&quot;KB&quot;</span><span class=\"p\">)</span>\n</span><span id=\"__span-16-4\"><a id=\"__codelineno-16-4\" name=\"__codelineno-16-4\" href=\"#__codelineno-16-4\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">size</span><span class=\"p\">)</span> <span class=\"c1\"># 1450.63</span>\n</span></code></pre></div>\n<h3 id=\"custom-precision\">Custom precision<a class=\"headerlink\" href=\"#custom-precision\" title=\"Permanent link\">&para;</a></h3>\n<div class=\"language-python highlight\"><pre><span></span><code><span id=\"__span-17-1\"><a id=\"__codelineno-17-1\" name=\"__codelineno-17-1\" href=\"#__codelineno-17-1\"></a><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">utkit.utils.file</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_file_size</span>\n</span><span id=\"__span-17-2\"><a id=\"__codelineno-17-2\" name=\"__codelineno-17-2\" href=\"#__codelineno-17-2\"></a>\n</span><span id=\"__span-17-3\"><a id=\"__codelineno-17-3\" name=\"__codelineno-17-3\" href=\"#__codelineno-17-3\"></a><span class=\"n\">size</span> <span class=\"o\">=</span> <span class=\"n\">get_file_size</span><span class=\"p\">(</span><span class=\"s2\">&quot;chart.png&quot;</span><span class=\"p\">,</span> <span class=\"n\">unit</span><span class=\"o\">=</span><span class=\"s2\">&quot;KB&quot;</span><span class=\"p\">,</span> <span class=\"n\">precision</span><span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">)</span>\n</span><span id=\"__span-17-4\"><a id=\"__codelineno-17-4\" name=\"__codelineno-17-4\" href=\"#__codelineno-17-4\"></a><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">size</span><span class=\"p\">)</span> <span class=\"c1\"># 1451.0</span>\n</span></code></pre></div>",
7
+ "search": [
8
+ {
9
+ "location": null,
10
+ "level": 1,
11
+ "title": "File utilities",
12
+ "text": "<p>The <code>utkit.utils.file</code> module provides lightweight helpers for working with files: computing content checksums, identifying file types from binary content, getting file sizes in human-readable units, and encoding files for transfer or display.</p>",
13
+ "path": [],
14
+ "tags": []
15
+ },
16
+ {
17
+ "location": "installation",
18
+ "level": 2,
19
+ "title": "Installation",
20
+ "text": "<p><code>python-magic</code> is required for content-based file type detection. Install the <code>standard</code> extra:</p> <pre><code>pip install \"utkit[standard]\"\n</code></pre> <p>Or with uv:</p> <pre><code>uv add \"utkit[standard]\"\n</code></pre> <p><code>get_file_checksum</code>, <code>get_file_size</code> and <code>encode_file</code> use only the Python standard library. <code>get_file_type</code> requires <code>python-magic</code>.</p>",
21
+ "path": [],
22
+ "tags": []
23
+ },
24
+ {
25
+ "location": "quick-start",
26
+ "level": 2,
27
+ "title": "Quick start",
28
+ "text": "<pre><code>from utkit.utils.file import encode_file, get_file_checksum, get_file_size, get_file_type\n\n# Compute a SHA-256 checksum\nchecksum = get_file_checksum(\"report.pdf\")\nprint(checksum) # \"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08\"\n\n# Identify a file type from its content\nfile_type = get_file_type(\"report.pdf\")\nprint(file_type) # \"pdf\"\n\n# Get the file size in kilobytes\nsize_kb = get_file_size(\"report.pdf\", unit=\"KB\")\nprint(size_kb) # 245.83\n\n# Encode a file as Base64 with MIME type detection\nencoded = encode_file(\"report.pdf\")\nprint(encoded[\"mime_type\"]) # \"application/pdf\"\nprint(encoded[\"url\"]) # \"data:application/pdf;base64,...\"\n</code></pre>",
29
+ "path": [],
30
+ "tags": []
31
+ },
32
+ {
33
+ "location": "get_file_checksum",
34
+ "level": 2,
35
+ "title": "<code>get_file_checksum</code>",
36
+ "text": "<p>Calculate a file checksum by reading the file in chunks. Reading in chunks keeps memory usage low even for very large files.</p> <pre><code>def get_file_checksum(file_path: str, algorithm: str = \"sha256\", chunk_size: int = 8192) -&gt; str\n</code></pre> Parameter Type Default Description <code>file_path</code> <code>str</code> — Path to the file to checksum. <code>algorithm</code> <code>str</code> <code>\"sha256\"</code> Hash algorithm supported by <code>hashlib</code> (e.g. <code>\"md5\"</code>, <code>\"sha1\"</code>, <code>\"sha256\"</code>, <code>\"sha512\"</code>). <code>chunk_size</code> <code>int</code> <code>8192</code> Number of bytes to read at a time. <p>Returns: <code>str</code> — Hex digest of the file checksum.</p>",
37
+ "path": [],
38
+ "tags": []
39
+ },
40
+ {
41
+ "location": "default-algorithm-sha-256",
42
+ "level": 3,
43
+ "title": "Default algorithm (SHA-256)",
44
+ "text": "<pre><code>from utkit.utils.file import get_file_checksum\n\nchecksum = get_file_checksum(\"data.csv\")\nprint(checksum) # \"a1b2c3...64-hex-characters\"\n</code></pre>",
45
+ "path": [],
46
+ "tags": []
47
+ },
48
+ {
49
+ "location": "custom-algorithm",
50
+ "level": 3,
51
+ "title": "Custom algorithm",
52
+ "text": "<pre><code>from utkit.utils.file import get_file_checksum\n\n# Use SHA-1 instead of the default SHA-256\nchecksum = get_file_checksum(\"data.csv\", algorithm=\"sha1\")\nprint(checksum) # \"da39a3ee5e6b4b0d3255bfef95601890afd80709\"\n</code></pre>",
53
+ "path": [],
54
+ "tags": []
55
+ },
56
+ {
57
+ "location": "verifying-file-integrity",
58
+ "level": 3,
59
+ "title": "Verifying file integrity",
60
+ "text": "<pre><code>from utkit.utils.file import get_file_checksum\n\nexpected = \"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08\"\n\nif get_file_checksum(\"report.pdf\") == expected:\n print(\"File is intact\")\nelse:\n print(\"File has been modified or corrupted\")\n</code></pre>",
61
+ "path": [],
62
+ "tags": []
63
+ },
64
+ {
65
+ "location": "get_file_type",
66
+ "level": 2,
67
+ "title": "<code>get_file_type</code>",
68
+ "text": "<p>Identify a file's type based on its content using magic numbers, rather than its extension. This is more reliable than trusting a file suffix.</p> <pre><code>def get_file_type(file_path: str) -&gt; str\n</code></pre> Parameter Type Description <code>file_path</code> <code>str</code> Path to the file to identify. <p>Returns: <code>str</code> — File type identifier. One of: <code>\"pdf\"</code>, <code>\"docx\"</code>, <code>\"csv\"</code>, <code>\"xlsx\"</code>, <code>\"xls\"</code>, <code>\"png\"</code>, <code>\"jpeg\"</code>, <code>\"jpg\"</code>. Returns <code>\"unknown\"</code> for any unrecognised type.</p>",
69
+ "path": [],
70
+ "tags": []
71
+ },
72
+ {
73
+ "location": "identifying-a-document",
74
+ "level": 3,
75
+ "title": "Identifying a document",
76
+ "text": "<pre><code>from utkit.utils.file import get_file_type\n\nfile_type = get_file_type(\"document.docx\")\nprint(file_type) # \"docx\"\n</code></pre>",
77
+ "path": [],
78
+ "tags": []
79
+ },
80
+ {
81
+ "location": "identifying-an-image",
82
+ "level": 3,
83
+ "title": "Identifying an image",
84
+ "text": "<pre><code>from utkit.utils.file import get_file_type\n\nfile_type = get_file_type(\"photo.jpg\")\nprint(file_type) # \"jpg\"\n</code></pre>",
85
+ "path": [],
86
+ "tags": []
87
+ },
88
+ {
89
+ "location": "handling-unknown-types",
90
+ "level": 3,
91
+ "title": "Handling unknown types",
92
+ "text": "<pre><code>from utkit.utils.file import get_file_type\n\nfile_type = get_file_type(\"notes.txt\")\nprint(file_type) # \"unknown\" (plain text is not in the recognised set)\n</code></pre>",
93
+ "path": [],
94
+ "tags": []
95
+ },
96
+ {
97
+ "location": "encode_file",
98
+ "level": 2,
99
+ "title": "<code>encode_file</code>",
100
+ "text": "<p>Encode a file as Base64 and return a dictionary containing the MIME type, encoded content, and a data URL.</p> <pre><code>def encode_file(file_path: str | Path) -&gt; dict[str, str]\n</code></pre> Parameter Type Description <code>file_path</code> <code>str \\| Path</code> Path to the file to encode. <p>Returns: <code>dict[str, str]</code> — A dictionary with three keys:</p> Key Description <code>mime_type</code> MIME type detected from the file extension <code>data</code> Base64-encoded file content <code>url</code> Data URL combining MIME type and encoded content",
101
+ "path": [],
102
+ "tags": []
103
+ },
104
+ {
105
+ "location": "basic-encoding",
106
+ "level": 3,
107
+ "title": "Basic encoding",
108
+ "text": "<pre><code>from utkit.utils.file import encode_file\n\nresult = encode_file(\"document.pdf\")\nprint(result)\n# {\n# \"mime_type\": \"application/pdf\",\n# \"data\": \"...base64-encoded-content...\",\n# \"url\": \"data:application/pdf;base64,...\"\n# }\n</code></pre>",
109
+ "path": [],
110
+ "tags": []
111
+ },
112
+ {
113
+ "location": "using-the-data-url",
114
+ "level": 3,
115
+ "title": "Using the data URL",
116
+ "text": "<pre><code>from utkit.utils.file import encode_file\n\nresult = encode_file(\"image.png\")\nprint(f\"Data URL: {result['url']}\")\n# Data URL can be used directly in HTML img tags or other applications\n</code></pre>",
117
+ "path": [],
118
+ "tags": []
119
+ },
120
+ {
121
+ "location": "get_file_size",
122
+ "level": 2,
123
+ "title": "<code>get_file_size</code>",
124
+ "text": "<p>Return the size of a file in the requested unit. Supports bytes (B), kilobytes (KB), megabytes (MB), gigabytes (GB), and terabytes (TB).</p> <pre><code>def get_file_size(path: str | Path, unit: SizeUnit = \"B\", precision: int = 2) -&gt; float\n</code></pre> Parameter Type Default Description <code>path</code> <code>str \\| Path</code> — Path to the file. <code>unit</code> <code>SizeUnit</code> <code>\"B\"</code> Unit of measurement: <code>\"B\"</code>, <code>\"KB\"</code>, <code>\"MB\"</code>, <code>\"GB\"</code>, or <code>\"TB\"</code>. <code>precision</code> <code>int</code> <code>2</code> Number of decimal places to round to. <p>Returns: <code>float</code> — File size in the requested unit.</p>",
125
+ "path": [],
126
+ "tags": []
127
+ },
128
+ {
129
+ "location": "default-unit-b",
130
+ "level": 3,
131
+ "title": "Default unit (B)",
132
+ "text": "<pre><code>from utkit.utils.file import get_file_size\n\nsize = get_file_size(\"chart.png\")\nprint(size) # 145062.83\n</code></pre>",
133
+ "path": [],
134
+ "tags": []
135
+ },
136
+ {
137
+ "location": "custom-unit",
138
+ "level": 3,
139
+ "title": "Custom unit",
140
+ "text": "<pre><code>from utkit.utils.file import get_file_size\n\nsize = get_file_size(\"chart.png\", unit=\"KB\")\nprint(size) # 1450.63\n</code></pre>",
141
+ "path": [],
142
+ "tags": []
143
+ },
144
+ {
145
+ "location": "custom-precision",
146
+ "level": 3,
147
+ "title": "Custom precision",
148
+ "text": "<pre><code>from utkit.utils.file import get_file_size\n\nsize = get_file_size(\"chart.png\", unit=\"KB\", precision=0)\nprint(size) # 1451.0\n</code></pre>",
149
+ "path": [],
150
+ "tags": []
151
+ }
152
+ ],
153
+ "title": "File utilities",
154
+ "toc": [
155
+ {
156
+ "title": "File utilities",
157
+ "content": "File utilities",
158
+ "id": "file-utilities",
159
+ "url": "#file-utilities",
160
+ "children": [
161
+ {
162
+ "title": "Installation",
163
+ "content": "Installation",
164
+ "id": "installation",
165
+ "url": "#installation",
166
+ "children": [],
167
+ "level": 2
168
+ },
169
+ {
170
+ "title": "Quick start",
171
+ "content": "Quick start",
172
+ "id": "quick-start",
173
+ "url": "#quick-start",
174
+ "children": [],
175
+ "level": 2
176
+ },
177
+ {
178
+ "title": "get_file_checksum",
179
+ "content": "<code>get_file_checksum</code>",
180
+ "id": "get_file_checksum",
181
+ "url": "#get_file_checksum",
182
+ "children": [
183
+ {
184
+ "title": "Default algorithm (SHA-256)",
185
+ "content": "Default algorithm (SHA-256)",
186
+ "id": "default-algorithm-sha-256",
187
+ "url": "#default-algorithm-sha-256",
188
+ "children": [],
189
+ "level": 3
190
+ },
191
+ {
192
+ "title": "Custom algorithm",
193
+ "content": "Custom algorithm",
194
+ "id": "custom-algorithm",
195
+ "url": "#custom-algorithm",
196
+ "children": [],
197
+ "level": 3
198
+ },
199
+ {
200
+ "title": "Verifying file integrity",
201
+ "content": "Verifying file integrity",
202
+ "id": "verifying-file-integrity",
203
+ "url": "#verifying-file-integrity",
204
+ "children": [],
205
+ "level": 3
206
+ }
207
+ ],
208
+ "level": 2
209
+ },
210
+ {
211
+ "title": "get_file_type",
212
+ "content": "<code>get_file_type</code>",
213
+ "id": "get_file_type",
214
+ "url": "#get_file_type",
215
+ "children": [
216
+ {
217
+ "title": "Identifying a document",
218
+ "content": "Identifying a document",
219
+ "id": "identifying-a-document",
220
+ "url": "#identifying-a-document",
221
+ "children": [],
222
+ "level": 3
223
+ },
224
+ {
225
+ "title": "Identifying an image",
226
+ "content": "Identifying an image",
227
+ "id": "identifying-an-image",
228
+ "url": "#identifying-an-image",
229
+ "children": [],
230
+ "level": 3
231
+ },
232
+ {
233
+ "title": "Handling unknown types",
234
+ "content": "Handling unknown types",
235
+ "id": "handling-unknown-types",
236
+ "url": "#handling-unknown-types",
237
+ "children": [],
238
+ "level": 3
239
+ }
240
+ ],
241
+ "level": 2
242
+ },
243
+ {
244
+ "title": "encode_file",
245
+ "content": "<code>encode_file</code>",
246
+ "id": "encode_file",
247
+ "url": "#encode_file",
248
+ "children": [
249
+ {
250
+ "title": "Basic encoding",
251
+ "content": "Basic encoding",
252
+ "id": "basic-encoding",
253
+ "url": "#basic-encoding",
254
+ "children": [],
255
+ "level": 3
256
+ },
257
+ {
258
+ "title": "Using the data URL",
259
+ "content": "Using the data URL",
260
+ "id": "using-the-data-url",
261
+ "url": "#using-the-data-url",
262
+ "children": [],
263
+ "level": 3
264
+ }
265
+ ],
266
+ "level": 2
267
+ },
268
+ {
269
+ "title": "get_file_size",
270
+ "content": "<code>get_file_size</code>",
271
+ "id": "get_file_size",
272
+ "url": "#get_file_size",
273
+ "children": [
274
+ {
275
+ "title": "Default unit (B)",
276
+ "content": "Default unit (B)",
277
+ "id": "default-unit-b",
278
+ "url": "#default-unit-b",
279
+ "children": [],
280
+ "level": 3
281
+ },
282
+ {
283
+ "title": "Custom unit",
284
+ "content": "Custom unit",
285
+ "id": "custom-unit",
286
+ "url": "#custom-unit",
287
+ "children": [],
288
+ "level": 3
289
+ },
290
+ {
291
+ "title": "Custom precision",
292
+ "content": "Custom precision",
293
+ "id": "custom-precision",
294
+ "url": "#custom-precision",
295
+ "children": [],
296
+ "level": 3
297
+ }
298
+ ],
299
+ "level": 2
300
+ }
301
+ ],
302
+ "level": 1
303
+ }
304
+ ]
305
+ },
306
+ "hash": 13685378743168212704
307
+ }