| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,17 +21,20 @@ def get_args(): | |||
| 21 | 21 | description='Compile my book', | |
| 22 | 22 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
| 23 | 23 | ||
| 24 | - # parser.add_argument('positional', | ||
| 25 | - # metavar='str', | ||
| 26 | - # help='A positional argument') | ||
| 27 | - | ||
| 28 | 24 | parser.add_argument('-i', | |
| 29 | 25 | '--dir', | |
| 30 | 26 | help='Input dir', | |
| 31 | 27 | metavar='str', | |
| 32 | 28 | type=str, | |
| 33 | 29 | default=os.getcwd()) | |
| 34 | 30 | ||
| 31 | + parser.add_argument('-f', | ||
| 32 | + '--outfile', | ||
| 33 | + help='Output filename', | ||
| 34 | + metavar='str', | ||
| 35 | + type=str, | ||
| 36 | + default='playful_python.pdf') | ||
| 37 | + | ||
| 35 | 38 | parser.add_argument('-o', | |
| 36 | 39 | '--outdir', | |
| 37 | 40 | help='Output dir', | |
@@ -46,18 +49,6 @@ def get_args(): | |||
| 46 | 49 | type=str, | |
| 47 | 50 | default=None) | |
| 48 | 51 | ||
| 49 | - # parser.add_argument('-i', | ||
| 50 | - # '--int', | ||
| 51 | - # help='A named integer argument', | ||
| 52 | - # metavar='int', | ||
| 53 | - # type=int, | ||
| 54 | - # default=0) | ||
| 55 | - | ||
| 56 | - # parser.add_argument('-f', | ||
| 57 | - # '--flag', | ||
| 58 | - # help='A boolean flag', | ||
| 59 | - # action='store_true') | ||
| 60 | - | ||
| 61 | 52 | return parser.parse_args() | |
| 62 | 53 | ||
| 63 | 54 | ||
@@ -80,9 +71,6 @@ def main(): | |||
| 80 | 71 | book_file = os.path.join(out_dir, 'book.md') | |
| 81 | 72 | ||
| 82 | 73 | with open(book_file, 'wt') as fh: | |
| 83 | - # fh.write('\n'.join( | ||
| 84 | - # ['\\documentclass{article}', '\\begin{document}', | ||
| 85 | - # '\\tableofcontents', '\\newpage', ''])) | ||
| 86 | 74 | fh.write('\\setcounter{tocdepth}{1}\\tableofcontents\n\\newpage\n\n') | |
| 87 | 75 | ||
| 88 | 76 | top_readme = 'README.md' | |
@@ -102,25 +90,26 @@ def main(): | |||
| 102 | 90 | print('\tREADME') | |
| 103 | 91 | chapter = 'Chapter {}: '.format(i) | |
| 104 | 92 | text = open(readme).read() | |
| 105 | - text = re.sub('^#\s+', '# ' + chapter, text) | ||
| 93 | + text = re.sub(r'^#\s+', '# ' + chapter, text) | ||
| 106 | 94 | fh.write(text + '\n\\newpage\n\n') | |
| 107 | 95 | ||
| 108 | 96 | solution = os.path.join(in_dir, dir_name, 'solution.py') | |
| 109 | 97 | if os.path.isfile(solution): | |
| 110 | 98 | print('\tSOLUTION') | |
| 111 | - fh.write('## Solution\n\n'.format(dir_name)) | ||
| 99 | + fh.write('## Solution\n\n') | ||
| 112 | 100 | fh.write('````\n') | |
| 113 | 101 | numbered = getoutput('cat -n {}'.format(solution)) | |
| 114 | 102 | fh.write(numbered) | |
| 115 | 103 | fh.write('\n````\n') | |
| 116 | 104 | fh.write('\n\\newpage\n\n') | |
| 117 | 105 | ||
| 118 | - cmd = 'pandoc {} --pdf-engine=xelatex -o book.pdf' | ||
| 119 | - rv, out = getstatusoutput(cmd.format(book_file)) | ||
| 106 | + cmd = 'pandoc {} --pdf-engine=xelatex -o {}' | ||
| 107 | + rv, out = getstatusoutput(cmd.format(book_file, args.outfile)) | ||
| 120 | 108 | ||
| 121 | 109 | if rv != 0: | |
| 122 | 110 | die('Error: {}'.format(out)) | |
| 123 | 111 | ||
| 112 | + | ||
| 124 | 113 | # -------------------------------------------------- | |
| 125 | 114 | if __name__ == '__main__': | |
| 126 | 115 | main() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,7 +18,7 @@ family_tree | |||
| 18 | 18 | gematria | |
| 19 | 19 | histy | |
| 20 | 20 | guess | |
| 21 | - kentucky_fryer | ||
| 21 | + kentucky_friar | ||
| 22 | 22 | mad_libs | |
| 23 | 23 | license_plates | |
| 24 | 24 | mad_libs | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,114 @@ | |||
| 1 | + #!/usr/bin/env python3 | ||
| 2 | + """ | ||
| 3 | + Author : Ken Youens-Clark <kyclark@gmail.com> | ||
| 4 | + Date : 2019-05-09 | ||
| 5 | + Purpose: Diplay delimited text files as ASCII tables | ||
| 6 | + """ | ||
| 7 | + | ||
| 8 | + import argparse | ||
| 9 | + import csv | ||
| 10 | + import os | ||
| 11 | + import sys | ||
| 12 | + from tabulate import tabulate | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + # -------------------------------------------------- | ||
| 16 | + def get_args(): | ||
| 17 | + """get command-line arguments""" | ||
| 18 | + parser = argparse.ArgumentParser( | ||
| 19 | + description='Diplay delimited text files as ASCII tables', | ||
| 20 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
| 21 | + | ||
| 22 | + parser.add_argument( | ||
| 23 | + 'file', | ||
| 24 | + metavar='FILE', | ||
| 25 | + nargs='+', | ||
| 26 | + type=argparse.FileType('r', encoding='UTF-8'), | ||
| 27 | + help='Input file(s)') | ||
| 28 | + | ||
| 29 | + parser.add_argument( | ||
| 30 | + '-d', | ||
| 31 | + '--delimiter', | ||
| 32 | + help='Delimiter', | ||
| 33 | + metavar='str', | ||
| 34 | + type=str, | ||
| 35 | + default='') | ||
| 36 | + | ||
| 37 | + parser.add_argument( | ||
| 38 | + '-s', | ||
| 39 | + '--style', | ||
| 40 | + help='Tabulate table style', | ||
| 41 | + metavar='str', | ||
| 42 | + type=str, | ||
| 43 | + choices=[ | ||
| 44 | + 'plain', 'simple', 'github', 'grid', 'fancy_grid', 'pipe', | ||
| 45 | + 'orgtbl', 'jira', 'presto', 'psql', 'rst', 'mediawiki', 'moinmoin', | ||
| 46 | + 'youtrack', 'html', 'latex', 'latex_raw', 'latex_booktabs', | ||
| 47 | + 'textile' | ||
| 48 | + ], | ||
| 49 | + default='psql') | ||
| 50 | + | ||
| 51 | + parser.add_argument( | ||
| 52 | + '-l', | ||
| 53 | + '--limit', | ||
| 54 | + help='Limit display to number records', | ||
| 55 | + metavar='int', | ||
| 56 | + type=int, | ||
| 57 | + default=0) | ||
| 58 | + | ||
| 59 | + parser.add_argument( | ||
| 60 | + '-n', '--no_headers', help='No headers in file', action='store_true') | ||
| 61 | + | ||
| 62 | + return parser.parse_args() | ||
| 63 | + | ||
| 64 | + | ||
| 65 | + # -------------------------------------------------- | ||
| 66 | + def warn(msg): | ||
| 67 | + """Print a message to STDERR""" | ||
| 68 | + print(msg, file=sys.stderr) | ||
| 69 | + | ||
| 70 | + | ||
| 71 | + # -------------------------------------------------- | ||
| 72 | + def die(msg='Something bad happened'): | ||
| 73 | + """warn() and exit with error""" | ||
| 74 | + warn(msg) | ||
| 75 | + sys.exit(1) | ||
| 76 | + | ||
| 77 | + | ||
| 78 | + # -------------------------------------------------- | ||
| 79 | + def main(): | ||
| 80 | + """Make a jazz noise here""" | ||
| 81 | + args = get_args() | ||
| 82 | + | ||
| 83 | + file_hdr = '/ ' + ('*' * 10) + ' {} ' + ('*' * 10) + ' /' | ||
| 84 | + | ||
| 85 | + for fh in args.file: | ||
| 86 | + if len(args.file) > 1: | ||
| 87 | + print(file_hdr.format(os.path.basename(fh.name))) | ||
| 88 | + | ||
| 89 | + _, ext = os.path.splitext(fh.name) | ||
| 90 | + delimiter = args.delimiter if args.delimiter else ',' \ | ||
| 91 | + if ext == '.csv' else '\t' | ||
| 92 | + data = list(csv.reader(fh, delimiter=delimiter)) | ||
| 93 | + | ||
| 94 | + if not data: | ||
| 95 | + warn('No data in "{}"'.format(fh.name)) | ||
| 96 | + continue | ||
| 97 | + | ||
| 98 | + headers = [] | ||
| 99 | + if args.no_headers: | ||
| 100 | + num_fields = len(data[0]) | ||
| 101 | + headers = map(lambda i: 'Field {}'.format(i + 1), | ||
| 102 | + range(num_fields)) | ||
| 103 | + else: | ||
| 104 | + headers = data.pop(0) | ||
| 105 | + | ||
| 106 | + if args.limit: | ||
| 107 | + data = data[:args.limit] | ||
| 108 | + | ||
| 109 | + print(tabulate(data, headers=headers, tablefmt=args.style)) | ||
| 110 | + | ||
| 111 | + | ||
| 112 | + # -------------------------------------------------- | ||
| 113 | + if __name__ == '__main__': | ||
| 114 | + main() | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments