修訂 | 540e3e292b79c48d37e4b99650f67ebc6fc6c8d7 (tree) |
---|---|
時間 | 2019-03-20 13:39:47 |
作者 | ![]() |
Commiter | Dreas Nielsen |
Modified to run under both Python 2 and Python 3.
@@ -1,6 +1,7 @@ | ||
1 | 1 | ========== ========== ================================================================================= |
2 | 2 | Date Version Features |
3 | 3 | ========== ========== ================================================================================= |
4 | +2019-03-19 1.0.0 Modified to run under both Python 2 and Python 3. | |
4 | 5 | 2018-08-26 0.10.2 Documentation revisions. |
5 | 6 | 2018-08-21 0.10.1 Modified to delete the error message file if it was created but no errors occurred. |
6 | 7 | 2018-03-05 0.10.0 Modified so that sorting is controlled by a new option, "-s". |
@@ -54,9 +54,9 @@ | ||
54 | 54 | # built documents. |
55 | 55 | # |
56 | 56 | # The short X.Y version. |
57 | -version = u'0.10.1' | |
57 | +version = u'1.0.0' | |
58 | 58 | # The full version, including alpha/beta/rc tags. |
59 | -release = u'0.10.1' | |
59 | +release = u'1.0.0' | |
60 | 60 | |
61 | 61 | # The language for content autogenerated by Sphinx. Refer to documentation |
62 | 62 | # for a list of supported languages. |
@@ -3,7 +3,7 @@ | ||
3 | 3 | Copyright and License |
4 | 4 | ================================ |
5 | 5 | |
6 | -Copyright (c) 2008-2018 R.Dreas Nielsen | |
6 | +Copyright (c) 2008-2019 R.Dreas Nielsen | |
7 | 7 | |
8 | 8 | This program is free software: you can redistribute it and/or modify it |
9 | 9 | under the terms of the GNU General Public License as published by the |
@@ -1,20 +1,21 @@ | ||
1 | 1 | from distutils.core import setup |
2 | 2 | |
3 | 3 | setup(name='xtab', |
4 | - version='0.10.2', | |
4 | + version='1.0.0', | |
5 | 5 | description="Crosstabulates data in a text file.", |
6 | 6 | author='Dreas Nielsen', |
7 | 7 | author_email='dreas.nielsen@gmail.com', |
8 | 8 | url='https://bitbucket.org/rdnielsen/xtab/', |
9 | 9 | scripts=['xtab/xtab.py'], |
10 | 10 | license='GPL', |
11 | - python_requires = '>=2.6, <3', | |
11 | + python_requires = '>=2.7', | |
12 | 12 | classifiers=[ |
13 | 13 | 'Environment :: Console', |
14 | 14 | 'Intended Audience :: End Users/Desktop', |
15 | 15 | 'License :: OSI Approved :: GNU General Public License (GPL)', |
16 | 16 | 'Operating System :: OS Independent', |
17 | 17 | 'Programming Language :: Python :: 2.7', |
18 | + 'Programming Language :: Python :: 3', | |
18 | 19 | 'Topic :: Office/Business' |
19 | 20 | ], |
20 | 21 | keywords=['CSV', 'crosstab', 'normalized', 'SQLite', 'table', 'data'], |
@@ -43,8 +43,8 @@ | ||
43 | 43 | #===================================================================================== |
44 | 44 | |
45 | 45 | |
46 | -_version = "0.10.1" | |
47 | -_vdate = "2018-08-21" | |
46 | +_version = "1.0.0" | |
47 | +_vdate = "2019-03-19" | |
48 | 48 | |
49 | 49 | import sys |
50 | 50 | import os |
@@ -185,7 +185,10 @@ | ||
185 | 185 | any subsequent parsing to be done by other programs (e.g., R). |
186 | 186 | """ |
187 | 187 | multiple_vals = False # A flag indicating whether or not multiple values were found for a single crosstab cell |
188 | - outfile = open(outfilename, "wb") # The csv module adds an extra <CR> if "wt" is specified | |
188 | + if sys.version_info < (3,0,0): | |
189 | + outfile = open(outfilename, "wb") # The Py2 csv module adds an extra <CR> if "wt" is specified | |
190 | + else: | |
191 | + outfile = open(outfilename, "w") | |
189 | 192 | csvout = csv.writer(outfile) |
190 | 193 | reportable_errors = 0 |
191 | 194 |
@@ -304,10 +307,6 @@ | ||
304 | 307 | sqlcmd = "SELECT %s FROM %s WHERE %s" % (",".join(xtab_datanames), tablename, " AND ".join(selcond)) |
305 | 308 | if sql_reporter: |
306 | 309 | sql_reporter.log(logging.INFO, "%s" % sqlcmd) |
307 | - # <Debugging> | |
308 | - #print(sqlcmd) | |
309 | - #return | |
310 | - # </Debugging> | |
311 | 310 | data_vals = sqldb.execute(sqlcmd).fetchall() |
312 | 311 | if sql_reporter: |
313 | 312 | for r in data_vals: |
@@ -397,7 +396,7 @@ | ||
397 | 396 | """ |
398 | 397 | dialect = csv.Sniffer().sniff(open(data_fn, "rt").readline()) |
399 | 398 | inf = csv.reader(open(data_fn, "rt"), dialect) |
400 | - column_names = inf.next() | |
399 | + column_names = next(inf) | |
401 | 400 | if sqlite_fn == None: |
402 | 401 | conn = sqlite3.connect(":memory:") |
403 | 402 | else: |
@@ -423,8 +422,8 @@ | ||
423 | 422 | |
424 | 423 | def print_help(): |
425 | 424 | """Print a program description and brief usage instructions to the console.""" |
426 | - print "xtab %s %s -- Cross-tabulates data." % (_version, _vdate) | |
427 | - print __help_msg | |
425 | + print("xtab %s %s -- Cross-tabulates data." % (_version, _vdate)) | |
426 | + print(__help_msg) | |
428 | 427 | |
429 | 428 | |
430 | 429 | def get_opts(arglist): |
@@ -460,90 +459,90 @@ | ||
460 | 459 | """Read and interpret the command-line arguments and options, and carry out |
461 | 460 | the appropriate actions.""" |
462 | 461 | args = get_opts(sys.argv) |
463 | - if len(args) == 0 or args.has_key('-h') or args.has_key('--help'): | |
462 | + if len(args) == 0 or '-h' in args or '--help' in args: | |
464 | 463 | print_help() |
465 | 464 | sys.exit(0) |
466 | 465 | badopts = [ o for o in args.keys() if o not in ['-i', '-o', '-r', |
467 | 466 | '-c', '-v', '-d', '-d1', '-d2', '-d3', '-d4', '-f', '-k', '-s', '-t', |
468 | 467 | '-n', '-e', '-q'] ] |
469 | 468 | if len(badopts) > 0: |
470 | - raise ValueError, "Unrecognized option(s): %s" % ", ".join(badopts) | |
471 | - if args.has_key('-i'): | |
469 | + raise ValueError("Unrecognized option(s): %s" % ", ".join(badopts)) | |
470 | + if '-i' in args: | |
472 | 471 | if len(args['-i']) == 0: |
473 | - raise ValueError, _errmsg_noinfile | |
472 | + raise ValueError(_errmsg_noinfile) | |
474 | 473 | infilename = args['-i'][0] |
475 | 474 | if not os.path.exists(infilename): |
476 | - raise ValueError, "%s (%s)" % (_errmsg_badinfile, infilename) | |
475 | + raise ValueError("%s (%s)" % (_errmsg_badinfile, infilename)) | |
477 | 476 | else: |
478 | - raise ValueError, _errmsg_noinfile | |
477 | + raise ValueError(_errmsg_noinfile) | |
479 | 478 | # |
480 | - if args.has_key('-o'): | |
479 | + if '-o' in args: | |
481 | 480 | if len(args['-o']) == 0: |
482 | - raise ValueError, _errmsg_nooutfile | |
481 | + raise ValueError(_errmsg_nooutfile) | |
483 | 482 | outfilename = args['-o'][0] |
484 | 483 | else: |
485 | - raise ValueError, _errmsg_nooutfile | |
484 | + raise ValueError(_errmsg_nooutfile) | |
486 | 485 | # |
487 | - if args.has_key('-r'): | |
486 | + if '-r' in args: | |
488 | 487 | if len(args['-r']) == 0: |
489 | - raise ValueError, _errmsg_norowheaders | |
488 | + raise ValueError(_errmsg_norowheaders) | |
490 | 489 | rowheaders = args['-r'] |
491 | 490 | else: |
492 | - raise ValueError, _errmsg_norowheaders | |
491 | + raise ValueError(_errmsg_norowheaders) | |
493 | 492 | # |
494 | - if args.has_key('-c'): | |
493 | + if '-c' in args: | |
495 | 494 | if len(args['-c']) == 0: |
496 | - raise ValueError, _errmsg_nocolumheaders | |
495 | + raise ValueError(_errmsg_nocolumheaders) | |
497 | 496 | columnheaders = args['-c'] |
498 | 497 | else: |
499 | - raise ValueError, _errmsg_nocolumheaders | |
498 | + raise ValueError(_errmsg_nocolumheaders) | |
500 | 499 | # |
501 | - if args.has_key('-v'): | |
500 | + if '-v' in args: | |
502 | 501 | if len(args['-v']) == 0: |
503 | - raise ValueError, _errmsg_nocellcolumns | |
502 | + raise ValueError(_errmsg_nocellcolumns) | |
504 | 503 | cellvalues = args['-v'] |
505 | 504 | else: |
506 | - raise ValueError, _errmsg_nocellcolumns | |
505 | + raise ValueError(_errmsg_nocellcolumns) | |
507 | 506 | # |
508 | 507 | hdr_opt = hdrs_1 |
509 | - if args.has_key('-d') or args.has_key('-d2'): | |
508 | + if '-d' in args or '-d2' in args: | |
510 | 509 | hdr_opt = hdrs_2 |
511 | - if args.has_key('-d3'): | |
510 | + if '-d3' in args: | |
512 | 511 | hdr_opt = hdrs_many |
513 | - if args.has_key('-d4'): | |
512 | + if '-d4' in args: | |
514 | 513 | hdr_opt = hdrs_labeled |
515 | - file_db = args.has_key('-f') | |
516 | - keep_file_db = args.has_key('-k') | |
514 | + file_db = '-f' in args | |
515 | + keep_file_db = '-k' in args | |
517 | 516 | tablename = 'src' |
518 | - if args.has_key('-t'): | |
517 | + if '-t' in args: | |
519 | 518 | if len(args['-t']) == 1: |
520 | 519 | tablename = args['-t'][0] |
521 | 520 | nullfill = None |
522 | - if args.has_key('-n'): | |
521 | + if '-n' in args: | |
523 | 522 | if len(args['-n']) == 1: |
524 | 523 | nullfill = args['-n'][0] |
525 | - sort_alpha = args.has_key('-s') | |
524 | + sort_alpha = '-s' in args | |
526 | 525 | # |
527 | 526 | # Set up logging |
528 | 527 | #logging.basicConfig(level=logging.INFO, filemode="w", filename='') |
529 | 528 | err_logger = None |
530 | 529 | sql_logger = None |
531 | 530 | error_file = None |
532 | - if args.has_key('-e'): | |
531 | + if '-e' in args: | |
533 | 532 | err_logger = logging.getLogger("err") |
534 | 533 | err_logger.setLevel(logging.WARNING) |
535 | 534 | if len(args['-e']) == 0: |
536 | 535 | err_logger.addHandler(logging.StreamHandler()) |
537 | 536 | else: |
538 | 537 | if len(args['-e']) > 1: |
539 | - raise ValueError, _errmsg_baderrlogfile | |
538 | + raise ValueError(_errmsg_baderrlogfile) | |
540 | 539 | error_file = args['-e'][0] |
541 | 540 | del_file(error_file) |
542 | 541 | file_logger = logging.FileHandler(error_file, "w") |
543 | 542 | err_logger.addHandler(file_logger) |
544 | - if args.has_key('-q'): | |
545 | - if len(args['-q']) <> 1: | |
546 | - raise ValueError, _errmsg_badsqllogfile | |
543 | + if '-q' in args: | |
544 | + if len(args['-q']) != 1: | |
545 | + raise ValueError(_errmsg_badsqllogfile) | |
547 | 546 | sql_logger = logging.getLogger("sql") |
548 | 547 | sql_logger.setLevel(logging.INFO) |
549 | 548 | sql_logger.addHandler(logging.FileHandler(args['-q'][0], "w")) |
@@ -561,9 +560,9 @@ | ||
561 | 560 | if __name__=='__main__': |
562 | 561 | try: |
563 | 562 | main() |
564 | - except SystemExit, x: | |
563 | + except SystemExit as x: | |
565 | 564 | sys.exit(x) |
566 | - except ValueError, e: | |
565 | + except ValueError as e: | |
567 | 566 | sys.stderr.write("%s\n" % str(e)) |
568 | 567 | sys.exit(1) |
569 | 568 | except Exception: |