Last active
December 8, 2015 05:17
-
-
Save bencharb/2d8f7a4c430b6a5f57af to your computer and use it in GitHub Desktop.
make_glob
This file contains hidden or 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
| import re | |
| def make_glob(*parts): | |
| """ | |
| Reverse glob: make a 'glob_pattern_string*' from a list of strings ['glob_pattern_string_1', 'not_a_match', 'glob_pattern_string_1', 'glob_pattern_string_3'] | |
| """ | |
| def match_parts(partial_string): | |
| rx = re.compile(partial_string+'.*') | |
| for part in parts: | |
| try: | |
| rx.match(part).group() | |
| yield part | |
| except AttributeError: | |
| continue | |
| def get_match(): | |
| avg_len = sum(map(len,parts))/len(parts) | |
| half_count = len(parts)/2 | |
| for x in xrange(avg_len): | |
| for part in parts: | |
| amt = avg_len-x | |
| if amt > len(part): | |
| continue | |
| part_string = part[:amt] | |
| if len(list(match_parts(part_string))) > half_count: | |
| return part_string | |
| match = get_match() | |
| if not match: | |
| raise Exception('Cannot find glob for parts %s' % format(parts)) | |
| return match + '*' | |
| def test_make_glob(): | |
| vals = ['test_strings_part-0.csv', | |
| 'test_strings_nomatch-0', | |
| 'no_match', | |
| 'test_strings_part-1.csv', | |
| 'test_strings_part-2.csv', | |
| 'test_strings_part-3.csv', | |
| 'test_strings_nomatch-3', | |
| 'test_strings_part-4.csv', | |
| 'test_strings_part-5.csv', | |
| 'test_strings_part-6.csv', | |
| 'test_strings_nomatch-6', | |
| 'test_strings_part-7.csv', | |
| 'test_strings_part-8.csv', | |
| 'no_match', | |
| 'test_strings_part-9.csv', | |
| 'test_strings_nomatch-9'] | |
| assert make_glob(*vals) == 'test_strings_part-*' | |
| test_make_glob() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment