stoichiograph

Spell words with elemental symbols from the periodic table.
git clone git://git.amin.space/stoichiograph.git
Log | Files | Refs | LICENSE

commit 25b007e43a45b13b655c582786d544ab25eb5529
parent d2dcd88d5a9cefc71c89bedd87045fceabb58e47
Author: amin <dev@aminmesbah.com>
Date:   Tue, 17 Jan 2017 07:15:38 +0000

Change name to stoichiograph.

FossilOrigin-Name: aa588613eca60419e298a3c72242dafe07a83676c1eb3c4a5bc5188bc93327de
Diffstat:
MREADME.rst | 32++++++++++++++++----------------
Dspellement.py | 133-------------------------------------------------------------------------------
Astoichiograph.py | 136+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 152 insertions(+), 149 deletions(-)

diff --git a/README.rst b/README.rst @@ -1,5 +1,5 @@ -Spellement -========== +Stoichiograph +============= Spell words with elemental symbols from the periodic table ("He", "Cu", etc). @@ -14,25 +14,25 @@ Usage .. code-block:: - usage: spellement.py [-h] [-b BATCH_FILE] [-c] [--debug] [--list-elements] - [-o OUTPUT_FILE] [-s] [-t] [-v] [-V] - [words [words ...]] + usage: stoichiograph [-h] [-b BATCH_FILE] [-c] [--debug] [--list-elements] + [-o OUTPUT_FILE] [-s] [-t] [-v] [-V] + [words [words ...]] Spell words with elemental symbols from the periodic table. positional arguments: - words word(s) for which to find elemental spellings + words word(s) for which to find elemental spellings optional arguments: - -h, --help show this help message and exit - -b BATCH_FILE, --batch-file BATCH_FILE + -h, --help show this help message and exit + -b BATCH_FILE, --batch-file BATCH_FILE text file containing one word per line - -c, --clobber overwrite output file if it exists - --debug print debug log - --list-elements print list of elemental symbols and exit - -o OUTPUT_FILE, --output_file OUTPUT_FILE + -c, --clobber overwrite output file if it exists + --debug print debug log + --list-elements print list of elemental symbols and exit + -o OUTPUT_FILE, --output_file OUTPUT_FILE path of output json file - -s, --sort sort words by length - -t, --tuples display spellings as tuples - -v, --verbose print a detailed log - -V, --version print version info and exit + -s, --sort sort words by length + -t, --tuples display spellings as tuples + -v, --verbose print a detailed log + -V, --version print version info and exit diff --git a/spellement.py b/spellement.py @@ -1,133 +0,0 @@ -#!/usr/bin/python3 - -import argparse -import collections -import json -import logging -import pathlib - -import speller - -__title__ = 'Elemental Speller' -__author__ = 'Amin Mesbah' -__version__ = '0.0.1' -__description__='Spell words with elemental symbols from the periodic table.' - - -def get_args(): - parser = argparse.ArgumentParser(description=__description__) - parser.add_argument( - 'words', - help='word(s) for which to find elemental spellings', - type=str, - nargs='*' - ) - parser.add_argument( - '-b', '--batch-file', - help='text file containing one word per line' - ) - parser.add_argument( - '-c', '--clobber', action='store_true', - help='overwrite output file if it exists' - ) - parser.add_argument( - '--debug', action='store_true', - help='print debug log' - ) - parser.add_argument( - '--list-elements', action='store_true', - help='print list of elemental symbols and exit' - ) - parser.add_argument( - '-o', '--output_file', - help='path of output json file' - ) - parser.add_argument( - '-s', '--sort', action='store_true', - help='sort words by length' - ) - parser.add_argument( - '-t', '--tuples', action='store_true', - help='display spellings as tuples' - ) - parser.add_argument( - '-v', '--verbose', action='store_true', - help='print a detailed log' - ) - parser.add_argument( - '-V', '--version', action='store_true', - help='print version info and exit' - ) - - return parser.parse_args() - - -def main(): - args = get_args() - - if args.version: - print('{} {}'.format(__title__, __version__)) - raise SystemExit - - if args.list_elements: - print('{} Elements:'.format(len(speller.ELEMENTS))) - print(sorted(list(speller.ELEMENTS))) - raise SystemExit - - if args.debug: - CONSOLE_LOG_LEVEL = logging.DEBUG - elif args.verbose: - CONSOLE_LOG_LEVEL = logging.INFO - else: - CONSOLE_LOG_LEVEL = logging.WARNING - - logging.basicConfig(level=CONSOLE_LOG_LEVEL) - logging.debug('{} {}'.format(__title__, __version__)) - - SORT_WORDS = args.sort - TUPLES = args.tuples - - if args.output_file: - OUTPUT_FILE = pathlib.Path(args.output_file) - CLOBBER = args.clobber - - if not CLOBBER and OUTPUT_FILE.exists(): - logging.warning( - "{} exists. To overwrite, use '--clobber'.".format(OUTPUT_FILE) - ) - raise SystemExit - - if args.batch_file: - words_file = pathlib.Path(args.batch_file) - with words_file.open('r') as f: - dictionary = f.readlines() - - words = [word.rstrip('\n') for word in dictionary if "'" not in word] - else: - words = args.words - - if SORT_WORDS: - words.sort(key=len, reverse=True) - - spellable = collections.OrderedDict() - - for word in words: - if TUPLES: - spellings = speller.spell(word) - else: - spellings = [''.join(s) for s in speller.spell(word)] - - if spellings: - spellable[word] = spellings - for spelling in spellings: - print(spelling) - - if args.output_file: - with OUTPUT_FILE.open('w') as f: - json.dump(spellable, f, indent=4, sort_keys=False) - - logging.debug('Done!') - - -if __name__ == '__main__': - main() diff --git a/stoichiograph.py b/stoichiograph.py @@ -0,0 +1,136 @@ +#!/usr/bin/python3 + +import argparse +import collections +import json +import logging +import pathlib + +import speller + +__title__ = 'stoichiograph' +__author__ = 'Amin Mesbah' +__version__ = '0.0.1' +__description__='Spell words with elemental symbols from the periodic table.' + + +def get_args(): + parser = argparse.ArgumentParser( + prog=__title__, + description=__description__ + ) + parser.add_argument( + 'words', + help='word(s) for which to find elemental spellings', + type=str, + nargs='*' + ) + parser.add_argument( + '-b', '--batch-file', + help='text file containing one word per line' + ) + parser.add_argument( + '-c', '--clobber', action='store_true', + help='overwrite output file if it exists' + ) + parser.add_argument( + '--debug', action='store_true', + help='print debug log' + ) + parser.add_argument( + '--list-elements', action='store_true', + help='print list of elemental symbols and exit' + ) + parser.add_argument( + '-o', '--output_file', + help='path of output json file' + ) + parser.add_argument( + '-s', '--sort', action='store_true', + help='sort words by length' + ) + parser.add_argument( + '-t', '--tuples', action='store_true', + help='display spellings as tuples' + ) + parser.add_argument( + '-v', '--verbose', action='store_true', + help='print a detailed log' + ) + parser.add_argument( + '-V', '--version', action='store_true', + help='print version info and exit' + ) + + return parser.parse_args() + + +def main(): + args = get_args() + + if args.version: + print('{} {}'.format(__title__, __version__)) + raise SystemExit + + if args.list_elements: + print('{} Elements:'.format(len(speller.ELEMENTS))) + print(sorted(list(speller.ELEMENTS))) + raise SystemExit + + if args.debug: + CONSOLE_LOG_LEVEL = logging.DEBUG + elif args.verbose: + CONSOLE_LOG_LEVEL = logging.INFO + else: + CONSOLE_LOG_LEVEL = logging.WARNING + + logging.basicConfig(level=CONSOLE_LOG_LEVEL) + logging.debug('{} {}'.format(__title__, __version__)) + + SORT_WORDS = args.sort + TUPLES = args.tuples + + if args.output_file: + OUTPUT_FILE = pathlib.Path(args.output_file) + CLOBBER = args.clobber + + if not CLOBBER and OUTPUT_FILE.exists(): + logging.warning( + "{} exists. To overwrite, use '--clobber'.".format(OUTPUT_FILE) + ) + raise SystemExit + + if args.batch_file: + words_file = pathlib.Path(args.batch_file) + with words_file.open('r') as f: + dictionary = f.readlines() + + words = [word.rstrip('\n') for word in dictionary if "'" not in word] + else: + words = args.words + + if SORT_WORDS: + words.sort(key=len, reverse=True) + + spellable = collections.OrderedDict() + + for word in words: + if TUPLES: + spellings = speller.spell(word) + else: + spellings = [''.join(s) for s in speller.spell(word)] + + if spellings: + spellable[word] = spellings + for spelling in spellings: + print(spelling) + + if args.output_file: + with OUTPUT_FILE.open('w') as f: + json.dump(spellable, f, indent=4, sort_keys=False) + + logging.debug('Done!') + + +if __name__ == '__main__': + main()