Debian Codegraph @

docs/gettingstarted.md

raw symbols SCIP
1 # Getting started
2
3 The following examples will cover what you can do with seedir. We can start by importing the package with the `sd` alias:
4
5 ```python
6 >>> import seedir as sd
7
8 ```
9
10 ## Displaying folder trees
11
12 The primary function of seedir is to create plain text diagrams of folders, for use in blogs, examples, Q&As, etc. The GitHub repo for seedir includes an example folder (`seedir/seedir/exampledir`) which will be used here.
13
14 To printout out the structure of this folder, you can use the primary `seedir.realdir.seedir()` function:
15
16 ```python
17 >>> path = 'exampledir'
18 >>> sd.seedir(path)
19 exampledir/
20 ├─jowly.pdf
21 ├─monkish.txt
22 ├─pedantic/
23 │ └─cataclysmic.txt
24 ├─scrooge/
25 │ ├─light.pdf
26 │ ├─paycheck/
27 │ │ └─electrophoresis.txt
28 │ └─reliquary.pdf
29 └─Vogel.txt
30
31 ```
32
33 By default, **the output is printed**. To return a string instead, use the `printout` argument:
34
35 ```python
36 >>> s = sd.seedir(path, printout=False)
37 >>> print(type(s))
38 <class 'str'>
39
40 ```
41
42 ## Trimming folder trees
43
44 Sometimes a directory is too large, is private, or contains certain irrelevant files. To handle this, there are a few arguments which allow you to edit the items included in the output.
45
46 ### Including & excluding folders & files
47
48 One way is to call out specific folders of files to include or exclude:
49
50 ```python
51 >>> sd.seedir(path, include_folders=['scrooge','paycheck'], exclude_files='reliquary.pdf')
52 exampledir/
53 ├─jowly.pdf
54 ├─monkish.txt
55 ├─scrooge/
56 │ ├─light.pdf
57 │ └─paycheck/
58 │ └─electrophoresis.txt
59 └─Vogel.txt
60
61 ```
62
63 By passing `regex=True`, these "include" and "exclude" arguments also support regular expressions, :
64
65 ```python
66 >>> sd.seedir(path, include_files='.*\.pdf$', regex=True) # all PDFs
67 exampledir/
68 ├─jowly.pdf
69 ├─pedantic/
70 └─scrooge/
71 ├─light.pdf
72 ├─paycheck/
73 └─reliquary.pdf
74
75 ```
76
77 ### Masking
78
79 You can also use the `mask` parameter to functionally filter out items in the folder. Pathnames are passed to the mask function, and items are kept if `True` is returned:
80
81 ```python
82 >>> import os
83
84 >>> def foo(x): # omits folders with more than 2 items
85 ... if os.path.isdir(x) and len(os.listdir(x)) > 2:
86 ... return False
87 ... return True
88
89 >>> sd.seedir(path, mask=foo)
90 exampledir/
91 ├─jowly.pdf
92 ├─monkish.txt
93 ├─pedantic/
94 │ └─cataclysmic.txt
95 └─Vogel.txt
96
97 ```
98
99 ### Limiting the depth or number of items
100
101 You can also wholly limit the output by providing the `depthlimit` or `itemlimit` arguments. Respectively, these arguments limit the depth of folders to enter and the number of items to include per folder.
102
103 ```python
104 >>> sd.seedir(path, depthlimit=1)
105 exampledir/
106 ├─jowly.pdf
107 ├─monkish.txt
108 ├─pedantic/
109 ├─scrooge/
110 └─Vogel.txt
111
112 >>> sd.seedir(path, itemlimit=3)
113 exampledir/
114 ├─jowly.pdf
115 ├─monkish.txt
116 └─pedantic/
117 └─cataclysmic.txt
118
119 ```
120
121 **New in v0.5.0!** You can also pass a 2-tuple to `itemlimit` to have separate limits for folders and files, respectively:
122
123 ```python
124 >>> sd.seedir(path, itemlimit=(None, 1))
125 exampledir/
126 ├─scrooge/
127 │ ├─light.pdf
128 │ └─paycheck/
129 │ └─electrophoresis.txt
130 ├─jowly.pdf
131 └─pedantic/
132 └─cataclysmic.txt
133
134 ```
135
136
137
138 As `seedir.realdir.seedir()` uses recursion, these arguments can hedge the traversal of deep, complicated folders.
139
140 When limiting the tree, using the `beyond` argument can be helpful to show what is being cut. The special value `'content'` shows the number of folders and files:
141
142 ```python
143 >>> sd.seedir(path, depthlimit=1, beyond='content')
144 exampledir/
145 ├─jowly.pdf
146 ├─monkish.txt
147 ├─pedantic/
148 │ └─0 folder(s), 1 file(s)
149 ├─scrooge/
150 │ └─1 folder(s), 2 file(s)
151 └─Vogel.txt
152
153 ```
154
155 ### Sorting
156
157 Especially when using the `itemlimit`, but also generally, you may want to sort the output to determine which items appear first. You can apply a general sort using `sort=True`:
158
159 ```python
160 >>> sd.seedir(path, itemlimit=4, sort=True)
161 exampledir/
162 ├─Vogel.txt
163 ├─jowly.pdf
164 ├─monkish.txt
165 └─pedantic/
166 └─cataclysmic.txt
167
168 ```
169
170 There are additional reverse and key arguments (akin to `sorted()` or `list.sort()`) which allow you to customize the sorting:
171
172 ```python
173 >>> sd.seedir(path, sort=True, sort_reverse=True, sort_key=lambda s : len(s))
174 exampledir/
175 ├─monkish.txt
176 ├─jowly.pdf
177 ├─Vogel.txt
178 ├─pedantic/
179 │ └─cataclysmic.txt
180 └─scrooge/
181 ├─reliquary.pdf
182 ├─light.pdf
183 └─paycheck/
184 └─electrophoresis.txt
185
186 ```
187
188 The `first` argument allows you to select whether files or folders appear first:
189
190 ```python
191 >>> sd.seedir(path, first='files')
192 exampledir/
193 ├─Vogel.txt
194 ├─jowly.pdf
195 ├─monkish.txt
196 ├─pedantic/
197 │ └─cataclysmic.txt
198 └─scrooge/
199 ├─light.pdf
200 ├─reliquary.pdf
201 └─paycheck/
202 └─electrophoresis.txt
203
204 ```
205
206 ## Styles 💅
207
208 `seedir.realdir.seedir()` has a few builtin styles for formatting the output of the folder tree:
209
210 ```python
211 >>> sd.seedir(path, style='emoji')
212 📁 exampledir/
213 ├─📄 jowly.pdf
214 ├─📄 monkish.txt
215 ├─📁 pedantic/
216 │ └─📄 cataclysmic.txt
217 ├─📁 scrooge/
218 │ ├─📄 light.pdf
219 │ ├─📁 paycheck/
220 │ │ └─📄 electrophoresis.txt
221 │ └─📄 reliquary.pdf
222 └─📄 Vogel.txt
223
224 ```
225
226 To make use of the 'emoji' style, install [emoji](https://pypi.org/project/emoji/) (`pip install emoji`) or use `pip install seedir[emoji]`.
227
228 For any builtin style, you can customize the indent size:
229
230 ```python
231 >>> sd.seedir(path, style='spaces', indent=4)
232 exampledir/
233 jowly.pdf
234 monkish.txt
235 pedantic/
236 cataclysmic.txt
237 scrooge/
238 light.pdf
239 paycheck/
240 electrophoresis.txt
241 reliquary.pdf
242 Vogel.txt
243
244 >>> sd.seedir(path, style='plus', indent=6)
245 exampledir/
246 +-----jowly.pdf
247 +-----monkish.txt
248 +-----pedantic/
249 | +-----cataclysmic.txt
250 +-----scrooge/
251 | +-----light.pdf
252 | +-----paycheck/
253 | | +-----electrophoresis.txt
254 | +-----reliquary.pdf
255 +-----Vogel.txt
256
257 ```
258
259 Each style is basically a collection of string "tokens" which are combined to form the header of each printed line (based on the depth and folder structure). You can see these tokens using `seedir.printing.get_styleargs()`:
260
261 ```python
262 >>> sd.get_styleargs('emoji')
263 {'split': '├─', 'extend': '│ ', 'space': ' ', 'final': '└─', 'folderstart': '📁 ', 'filestart': '📄 ', 'folderend': '/', 'fileend': ''}
264
265 ```
266
267 You can pass any of these tokens as `**kwargs` to explicitly customize styles with new symbols (note that passed tokens will not be affected by the `indent` parameter; it assumes you know how long you want them to be):
268
269 ```python
270 >>> sd.seedir(path, space=' ', extend='||', split='-}', final='\\\\', folderstart=' ', filestart=' ', folderend='%')
271 exampledir%
272 -} jowly.pdf
273 -} monkish.txt
274 -} pedantic%
275 ||\\ cataclysmic.txt
276 -} scrooge%
277 ||-} light.pdf
278 ||-} paycheck%
279 ||||\\ electrophoresis.txt
280 ||\\ reliquary.pdf
281 \\ Vogel.txt
282
283 ```
284
285 There are also `uniform` and `anystart` arguments for customizing multiple tokens at once:
286
287 ```python
288 >>> sd.seedir(path, uniform='----', anystart='>')
289 >exampledir/
290 ---->jowly.pdf
291 ---->monkish.txt
292 ---->pedantic/
293 -------->cataclysmic.txt
294 ---->scrooge/
295 -------->light.pdf
296 -------->paycheck/
297 ------------>electrophoresis.txt
298 -------->reliquary.pdf
299 ---->Vogel.txt
300
301 ```
302
303 ## Programmatic formatting
304
305 [Following a user-raised issue](https://github.com/earnestt1234/seedir/issues/4), seedir has added a `formatter` parameter for enabling some programmatic editing of the folder tree. This can be useful when you want to alter the style of the diagram based on things like the depth, item name, file extension, etc. The path of each item (relative to the root) is passed to `formatter`, which returns new settings to use for that item.
306
307 The following example edits the style tokens for items with particular names or file extensions:
308
309 ```python
310 >>> import os
311
312 >>> def my_style(item):
313 ...
314 ... outdict = {}
315 ...
316 ... # get the extension
317 ... ext = os.path.splitext(item)[1]
318 ...
319 ... if ext == '.txt':
320 ... outdict['filestart'] = '✏️'
321 ...
322 ... if os.path.basename(item) == 'scrooge':
323 ... outdict['folderstart'] = '👉'
324 ...
325 ... return outdict
326
327 >>> sd.seedir(path, formatter=my_style)
328 exampledir/
329 ├─jowly.pdf
330 ├─✏️monkish.txt
331 ├─pedantic/
332 │ └─✏️cataclysmic.txt
333 ├─👉scrooge/
334 │ ├─light.pdf
335 │ ├─paycheck/
336 │ │ └─✏️electrophoresis.txt
337 │ └─reliquary.pdf
338 └─✏️Vogel.txt
339
340 ```
341
342 The `formatter` parameter can also dynamically set other options, such as the filtering arguments outlined above. The following example sets a mask that removes files, but only in the root directory:
343
344 ```python
345 >>> import os
346 >>> def no_root_files(item):
347 ... if os.path.basename(item) == 'exampledir':
348 ... mask = lambda x: os.path.isdir(x)
349 ... return {'mask': mask}
350
351 >>> sd.seedir(path, formatter=no_root_files)
352 exampledir/
353 ├─pedantic/
354 │ └─cataclysmic.txt
355 └─scrooge/
356 ├─light.pdf
357 ├─paycheck/
358 │ └─electrophoresis.txt
359 └─reliquary.pdf
360
361 ```
362
363 Note that by default, any changes set by the `formatter` are unset for sub-directories. You can turn on the `sticky_formatter` option to make changes persist:
364
365 ```
366 >>> def mark(item):
367 ... d = {}
368 ... parent = os.path.basename(os.path.dirname(item))
369 ... if parent == 'pedantic':
370 ... d['folderstart'] = '✔️ '
371 ... d['filestart'] = '✔️ '
372 ... if parent == 'scrooge':
373 ... d['folderstart'] = '❌ '
374 ... d['filestart'] = '❌ '
375 ...
376 ... return d
377
378 # The function only makes changes to items in the 'pedantic' and 'scrooge'
379 # folders explicilty.
380 >>> sd.seedir(path, formatter=mark)
381 exampledir/
382 ├─jowly.pdf
383 ├─monkish.txt
384 ├─pedantic/
385 │ └─✔️ cataclysmic.txt
386 ├─scrooge/
387 │ ├─❌ light.pdf
388 │ ├─❌ paycheck/
389 │ │ └─electrophoresis.txt
390 │ └─❌ reliquary.pdf
391 └─Vogel.txt
392
393 # By passing `sticky_formatter`, changes are also applied to subdirectories
394 >>> sd.seedir(path, formatter=mark, sticky_formatter=True)
395 exampledir/
396 ├─jowly.pdf
397 ├─monkish.txt
398 ├─pedantic/
399 │ └─✔️ cataclysmic.txt
400 ├─scrooge/
401 │ ├─❌ light.pdf
402 │ ├─❌ paycheck/
403 │ │ └─❌ electrophoresis.txt
404 │ └─❌ reliquary.pdf
405 └─Vogel.txt
406
407 ```
408
409
410
411 Some properties (e.g., the depth of the folder relative to the parent) are a little more difficult to determine with a system paths. However, switching to object-oriented FakeDirs (introduced in the following section) make accessing interfolder relations a little easier:
412
413 ```python
414 >>> # now items passed are FakeDir / FakeFile, not system path
415 >>> def formatter(item):
416 ... if item.depth > 1: # we can access the depth attribute
417 ... return sd.get_styleargs('plus')
418
419 >>> f = sd.fakedir(path)
420 >>> f.seedir(formatter=formatter)
421 exampledir/
422 ├─jowly.pdf
423 ├─monkish.txt
424 ├─pedantic/
425 | +-cataclysmic.txt
426 ├─scrooge/
427 | +-light.pdf
428 | +-paycheck/
429 | | +-electrophoresis.txt
430 | +-reliquary.pdf
431 └─Vogel.txt
432
433 ```
434
435 ## Pathlib
436
437 As of version 0.4.0, `sd.realdir.seedir()` also accepts [pathlib](https://docs.python.org/3/library/pathlib.html) objects:
438
439 ```python
440 >>> import pathlib
441 >>> p = pathlib.Path(path)
442 >>> p
443 PosixPath('exampledir')
444
445 >>> sd.seedir(p)
446 exampledir/
447 ├─jowly.pdf
448 ├─monkish.txt
449 ├─pedantic/
450 │ └─cataclysmic.txt
451 ├─scrooge/
452 │ ├─light.pdf
453 │ ├─paycheck/
454 │ │ └─electrophoresis.txt
455 │ └─reliquary.pdf
456 └─Vogel.txt
457
458 ```
459
460 Most of the features demonstrated above apply as usual. The only major difference is that arguments accepting callables (`mask` & `fromatter`) will now be passed `pathlib.Path` objects, instead of strings:
461
462 ```python
463 >>> mask = lambda x: x.is_dir() or x.suffix == '.txt'
464 >>> sd.seedir(p, mask=mask)
465 exampledir/
466 ├─scrooge/
467 │ └─paycheck/
468 │ └─electrophoresis.txt
469 ├─monkish.txt
470 ├─pedantic/
471 │ └─cataclysmic.txt
472 └─Vogel.txt
473
474 ```
475
476
477
478
479
480
481
482
483
484 ## Fake directories
485
486 You can also create or edit directory examples by using "fake directories" in `seedir`. These are Python representations of folders and files, which can be manipulated in ways similar to real directories.
487
488 ### Making from scratch
489
490 `seedir.fakedir.FakeDir` is a folder class; you can be used to initialize an example folder tree. `FakeDir` objects have a `seedir.fakedir.FakeDir.seedir()` method, which is fundamentally similar to `seedir.realdir.seedir()` for real system paths:
491
492 ```python
493 >>> x = sd.FakeDir('myfakedir')
494 >>> x.seedir()
495 myfakedir/
496
497 ```
498
499 To add items to `x`, you can create subitems, initialize new directories, or move existing ones:
500
501 ```python
502 >>> x.create_file(['__init__.py', 'main.py', 'styles.txt'])
503 [FakeFile(myfakedir/__init__.py), FakeFile(myfakedir/main.py), FakeFile(myfakedir/styles.txt)]
504
505 >>> y = sd.FakeDir('resources', parent=x)
506
507 >>> z = sd.FakeDir('images')
508 >>> z.parent = y
509
510 >>> for n in ['a', 'b', 'c']:
511 ... z.create_file(n + '.png')
512 FakeFile(myfakedir/resources/images/a.png)
513 FakeFile(myfakedir/resources/images/b.png)
514 FakeFile(myfakedir/resources/images/c.png)
515
516 >>> x.seedir(sort=True, first='folders')
517 myfakedir/
518 ├─resources/
519 │ └─images/
520 │ ├─a.png
521 │ ├─b.png
522 │ └─c.png
523 ├─__init__.py
524 ├─main.py
525 └─styles.txt
526
527 ```
528
529 You can use path-like strings to index fake directories:
530
531 ```python
532 >>> x['resources/images/a.png']
533 FakeFile(myfakedir/resources/images/a.png)
534
535 ```
536
537 You can also use the `listdir` or `get_child_names` methods to get the children of a folder (or their names):
538
539 ```python
540 >>> x['resources/images'].listdir()
541 [FakeFile(myfakedir/resources/images/a.png), FakeFile(myfakedir/resources/images/b.png), FakeFile(myfakedir/resources/images/c.png)]
542
543 >>> x['resources/images'].get_child_names()
544 ['a.png', 'b.png', 'c.png']
545
546 ```
547
548 You can use the `delete` method to remove items by name or by object:
549
550 ```python
551 >>> x['resources/images'].delete([x['resources/images/a.png'], 'b.png'])
552 >>> x.seedir()
553 myfakedir/
554 ├─__init__.py
555 ├─main.py
556 ├─styles.txt
557 └─resources/
558 └─images/
559 └─c.png
560
561 ```
562
563 You can also move items within the tree:
564
565 ```python
566 >>> x['styles.txt'].parent = x['resources']
567 >>> x.seedir()
568 myfakedir/
569 ├─__init__.py
570 ├─main.py
571 └─resources/
572 ├─images/
573 │ └─c.png
574 └─styles.txt
575
576 ```
577
578 ### Turn existing directories into fakes
579
580 The `seedir.fakedir.fakedir()` function allows you to convert real directories into `seedir.fakedir.FakeDir` objects:
581
582 ```python
583 >>> f = sd.fakedir(path) # using path from above
584 >>> f.seedir()
585 exampledir/
586 ├─jowly.pdf
587 ├─monkish.txt
588 ├─pedantic/
589 │ └─cataclysmic.txt
590 ├─scrooge/
591 │ ├─light.pdf
592 │ ├─paycheck/
593 │ │ └─electrophoresis.txt
594 │ └─reliquary.pdf
595 └─Vogel.txt
596
597 >>> type(f)
598 <class 'seedir.fakedir.FakeDir'>
599
600 ```
601
602 Similar to `seedir.realdir.seedir()`, `seedir.fakedir.fakedir()` has options to limit the incoming folders and files:
603
604 ```python
605 >>> f = sd.fakedir(path, exclude_folders='scrooge', exclude_files='Vogel.txt')
606 >>> f.seedir()
607 exampledir/
608 ├─jowly.pdf
609 ├─monkish.txt
610 └─pedantic/
611 └─cataclysmic.txt
612
613 ```
614
615 Fake directories created from your system directories can be combined with other ones created from scratch:
616
617 ```python
618 >>> f.parent = x
619 >>> x.seedir()
620 myfakedir/
621 ├─__init__.py
622 ├─main.py
623 ├─resources/
624 │ ├─images/
625 │ │ └─c.png
626 │ └─styles.txt
627 └─exampledir/
628 ├─jowly.pdf
629 ├─monkish.txt
630 └─pedantic/
631 └─cataclysmic.txt
632
633 ```
634
635 ### Creating folder trees from text
636
637 The `seedir.fakedir.fakedir_fromstring()` method allows you to read in existing folder tree diagrams. Given this:
638
639 ```python
640 somedir/
641 - folder1
642 - folder2
643 - folder3
644 - file1
645 - file2
646 - subfolder
647 - subfolder
648 - deepfile
649 - folder4
650 ```
651
652 You can do:
653
654 ```python
655 >>> s = '''somedir/
656 ... - folder1
657 ... - folder2
658 ... - folder3
659 ... - file1
660 ... - file2
661 ... - subfolder
662 ... - subfolder
663 ... - deepfile
664 ... - folder4'''
665
666 >>> g = sd.fakedir_fromstring(s)
667 >>> g.seedir()
668 somedir/
669 ├─folder1
670 ├─folder2
671 ├─folder3/
672 │ ├─file1
673 │ ├─file2
674 │ └─subfolder/
675 │ └─subfolder/
676 │ └─deepfile
677 └─folder4
678
679 ```
680
681 This can be used to read in examples found online. [For instance](https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html):
682
683 ```python
684 >>> s = '''test/ # root folder
685 ... packA/ # package packA
686 ... subA/ # subpackage subA
687 ... __init__.py
688 ... sa1.py
689 ... sa2.py
690 ... __init__.py
691 ... a1.py
692 ... a2.py
693 ... packB/ # package packB (implicit namespace package)
694 ... b1.py
695 ... b2.py
696 ... math.py
697 ... random.py
698 ... other.py
699 ... start.py'''
700
701 >>> h = sd.fakedir_fromstring(s, parse_comments=True) # handling the added comments
702 >>> h.seedir(style='emoji')
703 📁 test/
704 ├─📁 packA/
705 │ ├─📁 subA/
706 │ │ ├─📄 __init__.py
707 │ │ ├─📄 sa1.py
708 │ │ └─📄 sa2.py
709 │ ├─📄 __init__.py
710 │ ├─📄 a1.py
711 │ └─📄 a2.py
712 ├─📁 packB/
713 │ ├─📄 b1.py
714 │ └─📄 b2.py
715 ├─📄 math.py
716 ├─📄 random.py
717 ├─📄 other.py
718 └─📄 start.py
719
720 ```
721
722 This function has not been extensively tested, so expect failures/misreadings!
723
724 ### Creating random directories
725
726 You can also use seedir to create randomly generated directories.
727
728 ```python
729 >>> r = sd.randomdir(seed=4.21, extensions=['png', 'jpg', 'pdf'], name='sorandom')
730 >>> r.seedir()
731 sorandom/
732 ├─plethora.pdf
733 ├─randy.jpg
734 ├─giddap.pdf
735 ├─adage/
736 └─Gobi/
737 ├─inventive.pdf
738 ├─Noel.jpg
739 ├─archaic/
740 │ └─bream.pdf
741 ├─salve/
742 └─NY/
743 └─Bahrein.jpg
744
745 ```
746
747 Additional parameters dictate the size of the tree created:
748
749 ```python
750 >>> sd.randomdir(files=range(5), # making an unnecessarily large tree
751 ... folders=[1,2],
752 ... stopchance=.3,
753 ... depth=5,
754 ... extensions=['png', 'jpg', 'pdf'],
755 ... seed=4.21).seedir()
756 MyFakeDir/
757 ├─plethora.pdf
758 ├─randy.jpg
759 ├─giddap.pdf
760 ├─adage.jpg
761 ├─oint/
762 │ ├─inventive.pdf
763 │ ├─Noel.jpg
764 │ ├─archaic.pdf
765 │ ├─NY/
766 │ │ ├─neonatal.png
767 │ │ ├─pyknotic.png
768 │ │ ├─wife.png
769 │ │ ├─Mickelson/
770 │ │ │ └─Caruso/
771 │ │ │ ├─royal.pdf
772 │ │ │ ├─aurora.pdf
773 │ │ │ ├─stampede.pdf
774 │ │ │ ├─divan.pdf
775 │ │ │ └─Coffey/
776 │ │ │ ├─sad.pdf
777 │ │ │ ├─cob.pdf
778 │ │ │ ├─natural.jpg
779 │ │ │ ├─skinflint.pdf
780 │ │ │ └─exploration/
781 │ │ └─Westchester/
782 │ │ ├─metamorphism.jpg
783 │ │ ├─Pulitzer.jpg
784 │ │ ├─permissive.png
785 │ │ ├─wakerobin/
786 │ │ │ ├─talon.jpg
787 │ │ │ ├─peace.pdf
788 │ │ │ ├─drowse.jpg
789 │ │ │ ├─rutabaga/
790 │ │ │ └─silage/
791 │ │ │ ├─pert.png
792 │ │ │ ├─defensible.png
793 │ │ │ ├─lexicon.png
794 │ │ │ ├─eureka.pdf
795 │ │ │ └─impasse/
796 │ │ └─calve/
797 │ │ ├─superstitious.png
798 │ │ └─embroider/
799 │ │ ├─safekeeping.pdf
800 │ │ ├─brig.png
801 │ │ ├─systemic.pdf
802 │ │ ├─caddis.png
803 │ │ └─borosilicate/
804 │ └─Pentecost/
805 │ ├─visceral.pdf
806 │ ├─replica.png
807 │ ├─rockaway.jpg
808 │ ├─luxe.jpg
809 │ └─breath/
810 │ ├─contain.jpg
811 │ └─homophobia/
812 │ ├─crewman.jpg
813 │ └─restroom/
814 └─joss/
815 ├─Technion.jpg
816 ├─Mynheer.png
817 ├─begonia/
818 └─Djakarta/
819 ├─Lionel.png
820 ├─Cantonese.pdf
821 ├─drafty.jpg
822 ├─half/
823 │ └─builtin/
824 │ ├─ermine.jpg
825 │ └─Cretan/
826 │ ├─happenstance.pdf
827 │ ├─insoluble.jpg
828 │ ├─Audrey/
829 │ └─octennial/
830 └─recipient/
831 ├─splintery.jpg
832 ├─bottom.png
833 ├─stunk.png
834 ├─Polyhymnia/
835 └─Holocene/
836 ├─plaque.pdf
837 ├─westerly.png
838 ├─glassine.jpg
839 ├─titanium.pdf
840 └─square/
841
842 ```
843
844 ### Turning fake directories into real ones
845
846 Finally, there is a `seedir.fakedir.FakeDir.realize()` method which can covert fake directories into real folders on your computer.
847
848 ```
849 x = sd.randomdir()
850 x.realize(path='where/to/create/it/')
851 ```
852
853 All files created will be empty.
854
855 ## Extending seedir
856
857 It is also possible to use seedir to generate folder tree diagrams for other types of Python objects that have directory-like structures. This can be done by **subclassing the `seedir.folderstructure.FolderStructure` class**, which implements the main algorithm:
858
859 ```python
860 >>> from seedir.folderstructure import FolderStructure
861
862 ```
863
864 FolderStructure is an abstract class, which requires three arguments to be implemented:
865
866 1. `seedir.folderstructure.FolderStructure.getname()`: returns a string name for the object
867 2. ``seedir.folderstructure.FolderStructure.isdir()`: returns a boolean indicating whether the object passed is a folder or not.
868 3. `seedir.folderstructure.FolderStructure.listdir()`: when called on the folder-like object, returns a list of child objects
869
870 Once these methods are implemented, your custom FolderStructure can be called on inputs to create folder structures. The following example demonstrates creating a "folder" structure for factorizing numbers:
871
872 ```python
873 >>> def factors(n):
874 ... return [i for i in range(2, n) if n % i == 0]
875
876 # note that the isdir_func always returns true, treating all numbers as "folders"
877 >>> class FactorStructure(FolderStructure):
878 ...
879 ... def getname(self, item):
880 ... return str(item)
881 ...
882 ... def isdir(self, item):
883 ... return True
884 ...
885 ... def listdir(self, item):
886 ... return factors(item)
887
888 >>> factorizer = FactorStructure()
889 >>> factorizer(36, folderend='')
890 36
891 ├─2
892 ├─3
893 ├─4
894 │ └─2
895 ├─6
896 │ ├─2
897 │ └─3
898 ├─9
899 │ └─3
900 ├─12
901 │ ├─2
902 │ ├─3
903 │ ├─4
904 │ │ └─2
905 │ └─6
906 │ ├─2
907 │ └─3
908 └─18
909 ├─2
910 ├─3
911 ├─6
912 │ ├─2
913 │ └─3
914 └─9
915 └─3
916
917 ```
918