Last active
October 24, 2020 16:44
-
-
Save havron/6f4e41300de57aa4847e8693cd5af466 to your computer and use it in GitHub Desktop.
random_sample.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import random | |
def main(path, samplesize): | |
""" | |
Prints random samples in order of selection. | |
:param path: file path of newline-separated els. | |
:param samplesize: size of sample to select. bounded by (1, len(els)). | |
""" | |
with open(path, "r") as f: | |
els = f.read().splitlines() | |
# Cap samples to els length, no negatives | |
samplesize = max(1, min(len(els), samplesize)) | |
sample = random.sample(els, k=samplesize) | |
for i, s in enumerate(sample): | |
print(f"Selection {i+1}: {s}") | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Random sample from a file") | |
parser.add_argument( | |
"-f", | |
"--file", | |
type=str, | |
dest="path", | |
help="Newline separated file of elements", | |
default=None, | |
required=True, | |
) | |
parser.add_argument( | |
"-s", | |
"--size", | |
type=int, | |
dest="samplesize", | |
help="Sample size", | |
default=1, | |
required=False, | |
) | |
main(**parser.parse_args().__dict__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment