|
20 | 20 | import copy |
21 | 21 | import keyword |
22 | 22 | import os |
| 23 | +import re |
23 | 24 |
|
24 | 25 | # 3rd party |
25 | 26 | import numpy as np |
@@ -48,6 +49,112 @@ def normpath(path): |
48 | 49 | return out |
49 | 50 |
|
50 | 51 |
|
| 52 | +def fileparts(path): |
| 53 | + """split a file path into its directory, name, and extension. |
| 54 | + |
| 55 | + Parameters |
| 56 | + ---------- |
| 57 | + path : str |
| 58 | + Input file path. |
| 59 | + |
| 60 | + Returns |
| 61 | + ------- |
| 62 | + dirname : str |
| 63 | + File directory. |
| 64 | + fname : str |
| 65 | + File name. |
| 66 | + ext : str |
| 67 | + File extension. |
| 68 | + |
| 69 | + Notes |
| 70 | + ----- |
| 71 | + * Removes the dot ('.') from the extension. |
| 72 | + |
| 73 | + """ |
| 74 | + |
| 75 | + dirname, fname = os.path.split(path) |
| 76 | + fname, ext = os.path.splitext(fname) |
| 77 | + ext = ext.replace('.', '') |
| 78 | + |
| 79 | + return dirname, fname, ext |
| 80 | + |
| 81 | + |
| 82 | +def fullfile(*args): |
| 83 | + """Join one or more file path components, assuming the last is |
| 84 | + the extension. |
| 85 | + |
| 86 | + Parameters |
| 87 | + ---------- |
| 88 | + ``*args`` : list, optional |
| 89 | + Components to concatenate. |
| 90 | + |
| 91 | + Returns |
| 92 | + ------- |
| 93 | + fpath : str |
| 94 | + The concatenated file path. |
| 95 | + |
| 96 | + """ |
| 97 | + |
| 98 | + nb = len(args) |
| 99 | + if nb == 0: |
| 100 | + return '' |
| 101 | + elif nb == 1: |
| 102 | + return args[0] |
| 103 | + elif nb == 2: |
| 104 | + return os.path.join(*args) |
| 105 | + |
| 106 | + fpath = os.path.join(*args[:-1]) + '.' + args[-1] |
| 107 | + |
| 108 | + return fpath |
| 109 | + |
| 110 | + |
| 111 | +def walktree(top=None, spec=None): |
| 112 | + """Iterator to recursively descend a directory and return all files |
| 113 | + matching the spec. |
| 114 | + |
| 115 | + Parameters |
| 116 | + ---------- |
| 117 | + top : str, optional |
| 118 | + Starting directory; if None, defaults to the current working directoty. |
| 119 | + spec : str, optional |
| 120 | + Regular expression to match the desired files; |
| 121 | + if None, matches all files. Typical patterns: |
| 122 | + * `r'\.txt$'` - matches files with '.txt' extension; |
| 123 | + * `r'^File_'` - matches files starting with 'File_' |
| 124 | + * `r'^File_.+\.txt$'` - matches files starting with 'File_' and ending |
| 125 | + with the '.txt' extension. |
| 126 | + |
| 127 | + Yields |
| 128 | + ------ |
| 129 | + fpath : str |
| 130 | + Absolute file path. |
| 131 | + |
| 132 | + Notes |
| 133 | + ----- |
| 134 | + * Partial matches are also selected. |
| 135 | + |
| 136 | + See Also |
| 137 | + -------- |
| 138 | + https://docs.python.org/3/library/re.html |
| 139 | + https://regex101.com/ |
| 140 | + |
| 141 | + """ |
| 142 | + |
| 143 | + if top is None: |
| 144 | + top = os.getcwd() |
| 145 | + |
| 146 | + if spec is None: |
| 147 | + spec = r'.*?' |
| 148 | + |
| 149 | + prog = re.compile(spec) |
| 150 | + |
| 151 | + for root, _, files in os.walk(top): |
| 152 | + for name in files: |
| 153 | + if prog.search(name): |
| 154 | + fname = os.path.join(root, name) |
| 155 | + yield fname |
| 156 | + |
| 157 | + |
51 | 158 | def remainderAllocator(votes, k, reverse=True, check=False): |
52 | 159 | """Allocate k seats proportionally using the Remainder Method. |
53 | 160 |
|
|
0 commit comments