yapping-filorithm 0.1.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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ghetea-patrick
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,459 @@
1
+ Metadata-Version: 2.4
2
+ Name: yapping-filorithm
3
+ Version: 0.1.0
4
+ Author-email: Patrick Ghetea <ghetea_patrick@outlook.com>
5
+ Classifier: Programming Language :: Python :: 3
6
+ Classifier: Programming Language :: Python :: 3.12
7
+ Classifier: Programming Language :: Python :: 3 :: Only
8
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
9
+ Requires-Python: >=3.12
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Dynamic: license-file
13
+
14
+ # Filorithm
15
+
16
+ **Find the files. Do the thing. Retire a few nested loops.**
17
+
18
+ Filorithm is a lightweight Python utility for files, directories, filtering, and batch storage operations. It builds on `pathlib` and `shutil`, giving you a readable way to select items and then copy, move, or delete them together.
19
+
20
+ If a simple file task keeps turning into three loops and an `os.path.join()` expedition, this is the shorter route through the same filesystem.
21
+
22
+ > **IMPORTANT**
23
+ > **Now on PyPI!** You no longer need to clone the repository, sacrifice a weekend, and manually summon the source code. Just install it:
24
+ >
25
+ > ```bash
26
+ > pip install yapping-filorithm
27
+ > ```
28
+ >
29
+ > Then import it in Python using the module name, minus the `yapping-` prefix:
30
+ >
31
+ > ```python
32
+ > import filorithm
33
+ > ```
34
+ >
35
+ > Remember: `yapping-filorithm` is the package name; `filorithm` is the import name. Two names, one project. Don't overthink it. Python already does enough of that.
36
+ >
37
+ > Happy hacking! May your files be found and your paths be relative only when intended.
38
+
39
+ [What it does](#what-it-does) · [Get started](#get-started) · [A quick example](#a-quick-example) · [How it fits together](#how-it-fits-together) · [Reference](#reference) · [Examples](#examples) · [Good to know](#good-to-know) · [Design notes](#design-notes)
40
+
41
+ > **IMPORTANT**
42
+ > The file-family module has been renamed from **`file_types.py`** to **`families.py`** as part of the project's cleanup and standardization. The examples in this README use `filorithm.families`. Update older imports that still point to `filorithm.file_types`.
43
+
44
+ ## What it does
45
+
46
+ - Supplies predefined extension families for archives, media, code, data, documents, executables, fonts, presentations, spreadsheets, system files, and web files.
47
+ - Collects files with `Files` and directories with `Folders`.
48
+ - Builds chainable filters for names, prefixes, suffixes, regex patterns, and modification dates, with extension and size filtering for files.
49
+ - Supports readable size amounts and units, including kilobytes, megabytes, gigabytes, and terabytes.
50
+ - Copies, moves, and deletes selected items through the `@`, `>>`, and `~` operators.
51
+ - Resolves paths and sanitizes directory inputs automatically.
52
+ - Handles individual files, multiple files, and entire directories through the collection and storage interfaces.
53
+ - Works with standard Python `Path` and `datetime` objects.
54
+
55
+ ## Get started
56
+
57
+ > **NOTE**
58
+ > Filorithm uses only Python's standard library. Replace `filorithm` in the imports if your installation uses another package path.
59
+
60
+ ```python
61
+ from filorithm.files import Files
62
+ from filorithm.folders import Folders
63
+ from filorithm.families import IMAGES, VIDEOS
64
+ ```
65
+
66
+ For an older checkout, the relevant import change is:
67
+
68
+ | Older path | Current path |
69
+ | --- | --- |
70
+ | `filorithm.file_types` | `filorithm.families` |
71
+
72
+ The examples use ordinary filesystem paths. Replace the sample paths with directories appropriate to your own environment.
73
+
74
+ ## A quick example
75
+
76
+ Collect interpreted-code files and copy them to a backup directory:
77
+
78
+ ```python
79
+ from filorithm.files import Files
80
+ from filorithm.families import CODE_INTERPRETED
81
+
82
+ scripts = Files("/scripts").filter().with_extensions(CODE_INTERPRETED).collect()
83
+
84
+ scripts @ "/backup/scripts"
85
+ ```
86
+
87
+ `Files("/scripts")` selects the source directory. `filter()` starts a builder, `with_extensions()` narrows the selection, and `collect()` returns a new `Files` collection. The `@` operator then copies that selection.
88
+
89
+ No matrix algebra is involved. Python simply lent the operator to another department.
90
+
91
+ ## How it fits together
92
+
93
+ ### Collect files
94
+
95
+ Constructing `Files` reads the files in the specified directory:
96
+
97
+ ```python
98
+ source_files = Files("/path/to/directory")
99
+ ```
100
+
101
+ You can also supply raw paths directly:
102
+
103
+ ```python
104
+ source_files = Files(
105
+ "/path/to/directory",
106
+ raw=["file1.txt", "file2.txt"]
107
+ )
108
+ ```
109
+
110
+ The collection is iterable and supports indexing. For example, iterate over it to inspect each filename:
111
+
112
+ ```python
113
+ for file in source_files:
114
+ print(file.name)
115
+ ```
116
+
117
+ ### Build a file filter
118
+
119
+ `Files.filter()` returns a `FilterFiles` builder. Chain criteria to narrow your selection, then collect the results.
120
+
121
+ #### Include or exclude extensions
122
+
123
+ Use a list or a predefined extension tuple:
124
+
125
+ ```python
126
+ images = Files("/downloads").filter().with_extensions(IMAGES).collect()
127
+ ```
128
+
129
+ To exclude a family, import that family and use `without_extensions()`:
130
+
131
+ ```python
132
+ from filorithm.families import EXECUTABLES
133
+ ```
134
+
135
+ ```python
136
+ safe_files = Files("/downloads").filter().without_extensions(EXECUTABLES).collect()
137
+ ```
138
+
139
+ The variable name `safe_files` here only describes the extension-based selection. The filter excludes the listed executable extensions; it does not inspect file contents.
140
+
141
+ #### Match names
142
+
143
+ Name filters support prefixes, suffixes, regex patterns, and text matching. These examples show prefix matching, substring matching, and keeping an explicit filename:
144
+
145
+ ```python
146
+ reports = Files("/docs").filter().has_prefix("report_").collect()
147
+
148
+ old_files = Files("/docs").filter().name_contains("backup").collect()
149
+
150
+ specific = Files("/docs").filter().keep_only(["config.json"]).collect()
151
+ ```
152
+
153
+ `has_prefix("report_")` keeps names beginning with `report_`. `name_contains("backup")` keeps names containing that text, and `keep_only(["config.json"])` restricts the selection to the given name.
154
+
155
+ #### Filter by modification date
156
+
157
+ Date filtering accepts standard Python `datetime` values:
158
+
159
+ ```python
160
+ from datetime import datetime
161
+
162
+ cutoff = datetime(2026, 1, 1)
163
+
164
+ recent = Files("/docs").filter().modified_after(cutoff).collect()
165
+ ```
166
+
167
+ Here, `modified_after(cutoff)` selects files modified after January 1, 2026. `modified_before(cutoff)` is used in the complete example below to select older files.
168
+
169
+ #### Filter by size
170
+
171
+ You can filter by size on disk using an amount and unit:
172
+
173
+ ```python
174
+ from filorithm.storage import Unit
175
+
176
+ large = Files("/media").filter().bigger_than(500, "mb").collect()
177
+ ```
178
+
179
+ This selects files bigger than 500 MB. `Unit` is available from the storage module; the example passes the unit as the string `"mb"` and does not need to reference `Unit` directly.
180
+
181
+ You can also select the largest or smallest files. For example, collect the ten largest:
182
+
183
+ ```python
184
+ top_ten = Files("/media").filter().largest(10).collect()
185
+ ```
186
+
187
+ > **IMPORTANT**
188
+ > Finish a filter chain with **`collect()`**. The chain builds conditions; `collect()` returns a new `Files` object containing the selected results.
189
+
190
+ ### Collect and filter folders
191
+
192
+ `Folders` follows the same collection pattern, targeting directories instead of files:
193
+
194
+ ```python
195
+ folders = Folders("/var/log")
196
+ ```
197
+
198
+ Its `filter()` method returns a `FilterFolders` builder:
199
+
200
+ ```python
201
+ backups = Folders("/var/log").filter().name_contains("backup").collect()
202
+ ```
203
+
204
+ Folder filtering supports prefixes, suffixes, name content, regex patterns, and modification dates. Use `collect()` to finish the selection here too.
205
+
206
+ ### Apply a batch operation
207
+
208
+ The same storage operators work with both `Files` and `Folders`:
209
+
210
+ | Operator | Action | Example form |
211
+ | --- | --- | --- |
212
+ | `>>` | Move the collected items to a destination directory. | `items >> "/destination"` |
213
+ | `@` | Copy the collected items to a destination directory. | `items @ "/destination"` |
214
+ | `~` | Delete the collected items from disk. | `~items` |
215
+
216
+ #### Move items with `>>`
217
+
218
+ ```python
219
+ images = Files("/downloads").filter().with_extensions(IMAGES).collect()
220
+
221
+ images >> "/pictures/memes"
222
+ ```
223
+
224
+ All selected images move into the memes directory. Their journey is your business; Filorithm handles the moving.
225
+
226
+ #### Copy items with `@`
227
+
228
+ ```python
229
+ documents = Files("/work")
230
+
231
+ documents @ "/backup/work"
232
+ ```
233
+
234
+ This copies the files collected from `/work` into `/backup/work`. The collection determines the scope of the operation.
235
+
236
+ #### Delete items with `~`
237
+
238
+ > **CAUTION**
239
+ > **`~items` permanently removes the selected files or folders from disk.** Inspect the selection before applying it. This is a filesystem deletion, not a move to a recycle bin.
240
+
241
+ ```python
242
+ temp_files = Files("/tmp").filter().name_contains("cache").collect()
243
+
244
+ ~temp_files
245
+ ```
246
+
247
+ In this example, the deletion applies to files in `/tmp` whose names contain `cache`.
248
+
249
+ #### Choose overwrite behavior explicitly
250
+
251
+ By default, a storage operation raises `FileExistsError` when a destination file already exists. Enable overwriting when constructing the collection if that is the behavior you intend:
252
+
253
+ ```python
254
+ Files("/downloads", overwrite=True) >> "/pictures"
255
+ ```
256
+
257
+ > **WARNING**
258
+ > `overwrite=True` opts into replacing existing destination files. Choose the destination and collection deliberately; the default protection is there for a reason.
259
+
260
+ ## Reference
261
+
262
+ ### File families
263
+
264
+ The family constants are tuples of common filename extensions, grouped by category. The examples in the table describe their contents; entries followed by “etc.” are representative rather than exhaustive lists of tuple members.
265
+
266
+ | Category | Contains |
267
+ |---|---|
268
+ | `ARCHIVES` | 7z, zip, tar, gz, rar, etc. |
269
+ | `AUDIO` | mp3, wav, flac, ogg, etc. |
270
+ | `CODE_ASSEMBLY` | asm, s, nasm, etc. |
271
+ | `CODE_COMPILED` | c, cpp, java, rs, go, etc. |
272
+ | `CODE_INTERPRETED` | py, js, php, rb, ts, etc. |
273
+ | `DATA` | json, csv, xml, yaml, sql, etc. |
274
+ | `DOCUMENTS` | pdf, docx, txt, md, epub, etc. |
275
+ | `EXECUTABLES` | exe, sh, bat, apk, bin, etc. |
276
+ | `FONTS` | ttf, otf, woff, etc. |
277
+ | `IMAGES` | png, jpg, jpeg, svg, gif, etc. |
278
+ | `PRESENTATIONS` | pptx, key, odp, etc. |
279
+ | `SPREADSHEETS` | xlsx, csv, ods, etc. |
280
+ | `SYSTEM` | dll, sys, log, ini, etc. |
281
+ | `VIDEOS` | mp4, mkv, avi, mov, etc. |
282
+ | `WEB` | html, css, jsx, wasm, etc. |
283
+
284
+ For example:
285
+
286
+ ```python
287
+ from filorithm.families import IMAGES
288
+
289
+ print("png" in IMAGES)
290
+ ```
291
+
292
+ The membership check shows the documented extension form: `"png"` appears without a leading dot. These collections let you reuse a family instead of remembering every spelling variation of a JPEG extension.
293
+
294
+ ### Collections and filter methods
295
+
296
+ | Interface | Purpose |
297
+ | --- | --- |
298
+ | `Files(path)` | Collect files from the source directory. |
299
+ | `Files(path, raw=[...])` | Construct a file collection using supplied raw paths. |
300
+ | `Folders(path)` | Collect directories from the source directory. |
301
+ | `.filter()` | Start a `FilterFiles` or `FilterFolders` builder. |
302
+ | `.collect()` | Finish the builder and return a new collection containing the results. |
303
+ | `.with_extensions(extensions)` | Keep files whose extensions belong to the supplied collection. |
304
+ | `.without_extensions(extensions)` | Exclude files whose extensions belong to the supplied collection. |
305
+ | `.has_prefix(text)` | Filter names by prefix. |
306
+ | `.name_contains(text)` | Filter names by included text. |
307
+ | `.keep_only(names)` | Restrict the selection to the supplied names. |
308
+ | `.modified_after(date)` | Filter by modification time after a `datetime` cutoff. |
309
+ | `.modified_before(date)` | Filter by modification time before a `datetime` cutoff. |
310
+ | `.bigger_than(amount, unit)` | Filter files by size on disk. |
311
+ | `.largest(count)` | Keep the requested number of largest files. |
312
+
313
+ Additional supported filter categories include suffix matching, regex matching, and selecting the smallest files.
314
+
315
+ ### Standalone storage functions
316
+
317
+ If you prefer calling functions directly, import the storage utilities:
318
+
319
+ ```python
320
+ from filorithm.storage import (
321
+ to_bytes,
322
+ remove,
323
+ copy_items,
324
+ move_items,
325
+ delete_items,
326
+ )
327
+ ```
328
+
329
+ `to_bytes()` converts a readable amount and unit into bytes:
330
+
331
+ ```python
332
+ bytes = to_bytes(5, "gb")
333
+ ```
334
+
335
+ For manual batch work, supply a list or tuple of `pathlib.Path` objects. First import `Path`:
336
+
337
+ ```python
338
+ from pathlib import Path
339
+ ```
340
+
341
+ Then call the batch functions as needed:
342
+
343
+ ```python
344
+ paths = [Path("file1.txt"), Path("file2.txt")]
345
+
346
+ copy_items(paths, "/destination")
347
+ move_items(paths, "/destination")
348
+ delete_items(paths)
349
+ ```
350
+
351
+ The snippet shows the call shapes for `copy_items()`, `move_items()`, and `delete_items()`. Choose the operation you need; running all three sequentially against the same paths changes which source paths still exist.
352
+
353
+ These functions supply the storage mechanics used by `Files` and `Folders`. The module also exposes `remove`, as shown in the import list above.
354
+
355
+ ## Examples
356
+
357
+ ### Select large videos and old images
358
+
359
+ > **CAUTION**
360
+ > This example performs real changes: it enables overwriting, moves selected videos, and permanently deletes selected images. Read through the filters and replace the paths before running it.
361
+
362
+ ```python
363
+ from datetime import datetime
364
+ from filorithm.files import Files
365
+ from filorithm.families import VIDEOS, IMAGES
366
+
367
+ # Define a cutoff date
368
+ cutoff = datetime(2026, 1, 1)
369
+
370
+ # Collect all files in the downloads folder
371
+ downloads = Files("/users/home/downloads", overwrite=True)
372
+
373
+ # Find large videos
374
+ large_videos = (
375
+ downloads.filter()
376
+ .with_extensions(VIDEOS)
377
+ .bigger_than(1, "gb")
378
+ .collect()
379
+ )
380
+
381
+ # Find old images
382
+ old_images = (
383
+ downloads.filter()
384
+ .with_extensions(IMAGES)
385
+ .modified_before(cutoff)
386
+ .collect()
387
+ )
388
+
389
+ # Move the videos to an external drive
390
+ large_videos >> "/media/external/movies"
391
+
392
+ # Delete the old images
393
+ ~old_images
394
+ ```
395
+
396
+ The workflow has three steps:
397
+
398
+ 1. Target the downloads directory with `Files`.
399
+ 2. Build separate selections: videos bigger than 1 GB, and images modified before January 1, 2026.
400
+ 3. Move the video selection to an external drive and delete the old-image selection.
401
+
402
+ The two collections let you express different criteria and actions from the same starting directory without writing the traversal and batch loops yourself.
403
+
404
+ ## Good to know
405
+
406
+ Filorithm stays close to Python's standard filesystem utilities. The current implementation does not provide:
407
+
408
+ - Asynchronous file operations.
409
+ - A toggle for automatic symlink resolution.
410
+ - Deep recursive filtering through the filter interface, equivalent to an `os.walk()` traversal.
411
+ - File-content inspection.
412
+ - Advanced permission management through `chmod` or `chown`.
413
+ - Archiving or unzipping directly from `Files`.
414
+
415
+ > **NOTE**
416
+ > Both `Files` and `Folders` implement `__str__()` and `__repr__()` to provide readable collection output.
417
+
418
+ Use `print()` to display the collection's string representation:
419
+
420
+ ```python
421
+ files = Files("/path/to/files")
422
+ folders = Folders("/path/to/folders")
423
+
424
+ print(files)
425
+ print(folders)
426
+ ```
427
+
428
+ `__str__()` returns the string representation of the underlying list of `Path` objects. The output includes their paths.
429
+
430
+ Use `repr()` to display a shorter summary of the collection:
431
+
432
+ ```python
433
+ print(repr(files))
434
+ print(repr(folders))
435
+ ```
436
+
437
+ `__repr__()` displays the collection's class name, total item count, and item names. It shows up to five names; when the collection contains more than five items, the remaining count is shown instead.
438
+
439
+ For example, on a POSIX system:
440
+
441
+ ```text
442
+ [PosixPath('/path/to/files/report.txt'), PosixPath('/path/to/files/data.csv')]
443
+ <Files count=2 items=['report.txt', 'data.csv']>
444
+
445
+ [PosixPath('/path/to/folders/archive'), PosixPath('/path/to/folders/images')]
446
+ <Folders count=2 items=['archive', 'images']>
447
+ ```
448
+
449
+ The path representation may differ between operating systems. The collection summary, however, keeps the same format.
450
+
451
+ ## Design notes
452
+
453
+ Filorithm puts a readable interface over the standard `os`, `shutil`, and `pathlib` operations. The work is split into three explicit stages:
454
+
455
+ 1. **Select a source:** `Files` or `Folders` collects the items.
456
+ 2. **Build conditions:** `FilterFiles` or `FilterFolders` narrows the collection.
457
+ 3. **Execute an action:** storage operators or standalone functions perform the operation.
458
+
459
+ The overloaded operators make batch actions concise and visually distinct from normal method calls. The underlying filesystem and storage behavior remain ordinary Python operations; Filorithm handles the orchestration. If you have been repeating `shutil.move()` or `os.remove(os.path.join(root, file))` inside traversal loops, this gives that work a common interface. Fewer nested loops, same files, slightly more afternoon left.