Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/klieret/randomfiletree
Create random file structures in python for testing purposes.
https://github.com/klieret/randomfiletree
hacktoberfest python3 testing unit-testing unittest
Last synced: 3 days ago
JSON representation
Create random file structures in python for testing purposes.
- Host: GitHub
- URL: https://github.com/klieret/randomfiletree
- Owner: klieret
- License: mit
- Created: 2019-04-19T06:54:23.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2025-01-07T01:44:45.000Z (26 days ago)
- Last Synced: 2025-01-22T09:07:00.848Z (11 days ago)
- Topics: hacktoberfest, python3, testing, unit-testing, unittest
- Language: Python
- Homepage: https://randomfiletree.rtfd.io/
- Size: 106 KB
- Stars: 32
- Watchers: 3
- Forks: 9
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
## Description
Create a random file and directory tree/structure for testing purposes.
This is done in an iterative fashion: For every iteration, files and folders
are created based on set probabilities in all subfolders of the target folder.## Installation
`randomfiletree` can be installed with the python package manager:
```sh
pip3 install randomfiletree
```For a local installation, you might want to use the `--user` switch of
`pip`. You can also update your current installation with
`pip3 install --upgrade randomfiletree`.For the latest development version you can also work from a cloned
version of this repository:```sh
git clone https://github.com/klieret/randomfiletree/
cd randomfiletree
pip3 install --user .
```## Usage
Take a look at the
[documentation](https://randomfiletree.readthedocs.io/) for the
complete picture.### Command line interface
Simple command line interface:
```sh
randomfiletree \
-f \
-d \
-r
```For example, using the options `-f 3 -d 2 -r 2`, on average 2 folders and 3
files are created in the first iteration, and another 2 folders and 3 files are
added to the output directory and each of its subdirectories in the second
iteration.Type `randomfiletree -h` to see all supported arguments.
## Python API
```python
import randomfiletreerandomfiletree.iterative_gaussian_tree(
"/path/to/basedir",
nfiles=2.0,
nfolders=0.5,
maxdepth=5,
repeat=4
)
```Randomfiletree will now crawl through all directories in
`/path/to/basedir` and create new files with the probabilities given in
the arguments.### Advanced examples
It is possible to pass an optional function to generate the random
filenames oneself:```python
import random
import stringdef fname():
length = random.randint(5, 10)
return "".join(
random.choice(string.ascii_uppercase + string.digits)
for _ in range(length)
) + '.docx'randomfiletree.core.iterative_gaussian_tree(
"/path/to/basedir",
nfiles=100,
nfolders=10,
maxdepth=2,
filename=fname
)
```The `payload` optional argument can be used to generate file contents
together with their names. For example, it can be used to replicate some
template files with randomized names:```python
import itertools
import pathlib
import randomfiletreedef callback(target_dir: pathlib.Path) -> pathlib.Path:
sourcedir = pathlib.Path("/path/to/templates/")
sources = []
for srcfile in sourcedir.iterdir():
with open(srcfile, 'rb') as f:
content = f.read()
sources.append((srcfile.suffix, content))
for srcfile in itertools.cycle(sources):
path = target_dir / (randomfiletree.core.random_string() + srcfile[0])
with path.open('wb') as f:
f.write(srcfile[1])
yield pathrandomfiletree.core.iterative_gaussian_tree(
"/path/to/basedir",
nfiles=10,
nfolders=10,
maxdepth=5,
repeat=4,
payload=callback
)
```if both `filename` and `payload` passed, the first option is ignored.
## Contributors ✨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Heshanthaka
💻
Nicholas Bollweg
💻
Vadym Markov
💻
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!