Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: A fun python CLI program for all to enjoy! Date: Sat, 07 May 2016 15:59 +0200 Organization: None Lines: 63 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de p9BYtrg+BNBiq3GUW1ii1Q8XgYZiMN44T8RQ6tJ25rJA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'args': 0.04; '"__main__":': 0.07; '__name__': 0.07; 'linear': 0.07; 'main()': 0.07; 'welcome.': 0.07; 'enhancements': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sqlite': 0.09; 'bug': 0.10; 'output': 0.13; 'def': 0.13; 'subject:python': 0.14; 'explicitly': 0.15; 'variables': 0.15; 'accidental': 0.16; 'dfs': 0.16; 'globals.': 0.16; 'labels,': 0.16; 'main():': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:program': 0.16; 'verbose': 0.16; 'wrote:': 0.16; 'directory.': 0.18; 'typing': 0.18; 'creates': 0.18; "aren't": 0.22; 'arguments': 0.22; 'button,': 0.22; 'code,': 0.23; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'module.': 0.27; 'function': 0.28; 'too.': 0.30; 'code': 0.30; 'writes': 0.30; 'skip:s 30': 0.31; 'another': 0.32; 'addresses': 0.32; 'operate': 0.32; 'subject:all': 0.32; 'structure': 0.34; 'add': 0.34; 'text': 0.35; 'reports,': 0.35; 'skip:i 20': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'there,': 0.37; 'received:org': 0.37; 'mailing': 0.38; 'files': 0.38; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'address': 0.61; 'skip:u 10': 0.61; 'avoid': 0.61; 'further': 0.62; 'flat': 0.63; 'reviews': 0.63; 'more': 0.63; 'within': 0.64; 'food': 0.64; 'places': 0.64; 'note:': 0.66; 'records': 0.70; 'bells': 0.84; 'yellow': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd9e4e.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:108268 DFS wrote: > getAddresses.py > > Scrapes addresses from www.usdirectory.com and stores them in a SQLite > database, or writes them to text files for mailing labels, etc > > Now, just by typing 'fast food Taco Bell 10 db all' you can find > out how many Taco Bells are within 10 miles of you, and store all the > addresses in your own address database. > > No more convoluted Googling, or hitting the 'Next Page' button, or > fumbling with the Yellow Pages... > > Note: the db structure is flat on purpose, and the .csv files aren't > quote delimited. > > Put the program in its own directory. It creates the SQLite database > there, and writes files there, too. > > Reviews of code, bug reports, criticisms, suggestions for improvement, > etc are all welcome. - Avoid module-level code and global variables - Use functions that do one thing and operate on explicitly passed arguments - You have if store == ...: ... sprinkled across your module. You will have to change your code in many places if you want to add another output format. With a linear structure like STORE_FUNCS = { "db": store_in_db, "txt": store_as_text, "csv": store_as_csv, } def main(): args = read_arguments() records = read_records(args) records = unique(records) if args.limit: records = itertools.islice(records, args.limit) STORE_FUNCS[args.storage_format](args, records) if __name__ == "__main__": main() further enhancements will be a lot easier to implement. The main() function avoids accidental uncontrolled globals. If you want one you have to declare it: def main(): global verbose args = read_arguments() verbose = args.verbose ...