From 8eff4b8416ab3be073d9cedc397581f66397525e Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Thu, 16 Nov 2017 22:00:16 +0100 Subject: Style fixes --- namegen.py | 71 +++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 28 deletions(-) (limited to 'namegen.py') diff --git a/namegen.py b/namegen.py index 7419331..a07ca16 100755 --- a/namegen.py +++ b/namegen.py @@ -34,7 +34,7 @@ OPTIONS -n , --number= Generate names (0 to generate no name). -c , --context= - Use characters of context in the stats (ignored when loading + Use characters of context in the stats (ignored when loading existing stats). -h, --help Show an help message. @@ -75,14 +75,15 @@ import optparse import sys import random + class Stats: - def __init__(self,context): + def __init__(self, context): """""" self.stats = {} self.context = context - def _increment_stats(self,cont,key): + def _increment_stats(self, cont, key): if cont in self.stats: if key in self.stats[cont]: self.stats[cont][key] += 1 @@ -91,24 +92,24 @@ class Stats: else: self.stats[cont] = {key: 1} - def read_names(self,fp): + def read_names(self, fp): """Calculate stats from a list of names in a file-like object.""" for name in fp.readlines(): name = name.decode('utf-8') - for i in range(min(self.context,len(name))): - self._increment_stats(name[0:i],name[i]) - for i in range(self.context,len(name)): - self._increment_stats(name[i-self.context:i],name[i]) + for i in range(min(self.context, len(name))): + self._increment_stats(name[0:i], name[i]) + for i in range(self.context, len(name)): + self._increment_stats(name[i-self.context:i], name[i]) - def load_stats(self,fp): + def load_stats(self, fp): """Load stats from json in a file-like object.""" self.stats = json.load(fp) self.context = max([len(k) for k in list(self.stats.keys())]) - def save_stats(self,fp): + def save_stats(self, fp): """Save stats in json to a file-like object.""" - json.dump(self.stats,fp) + json.dump(self.stats, fp) def generate_name(self): """Generate a name.""" @@ -119,23 +120,24 @@ class Stats: cont = name else: cont = name[-self.context:] - + chars = ''.join([k*self.stats[cont][k] for k in self.stats[cont]]) c = random.choice(chars) name += c return name + def main(): parser = optparse.OptionParser() - parser.add_option('--stats','-s') - parser.add_option('--list','-l') - parser.add_option('--save-stats','-S') - parser.add_option('--number','-n') - parser.add_option('--context','-c') + parser.add_option('--stats', '-s') + parser.add_option('--list', '-l') + parser.add_option('--save-stats', '-S') + parser.add_option('--number', '-n') + parser.add_option('--context', '-c') opt, arg = parser.parse_args() - if opt.stats == None and opt.list == None: + if opt.stats is None and opt.list is None: parser.print_help() sys.exit(1) @@ -148,7 +150,8 @@ def main(): try: fp = open(opt.stats) except IOError: - sys.stderr.write("Could not open "+opt.stats+" for reading.\n") + sys.stderr.write( + "Could not open {stats} for reading.\n".format(stats=opt.stats)) except TypeError: pass else: @@ -156,7 +159,9 @@ def main(): mystats.load_stats(fp) need_list = False except ValueError: - sys.stderr.write(opt.stats+" is not a valid stats file.\n") + sys.stderr.write("{stats} is not a valid stats file.\n".format( + stats=opt.stats + )) finally: fp.close() @@ -164,7 +169,9 @@ def main(): try: fp = open(opt.list) except IOError: - sys.stderr.write("Could not open "+opt.list+" for reading.\n") + sys.stderr.write("Could not open {} for reading.\n".format( + list=opt.list + )) sys.exit(1) except TypeError: parser.print_help() @@ -173,24 +180,31 @@ def main(): try: mystats.read_names(fp) except IOError: - sys.stderr.write(opt.list+" is not a valid name list file.\n") + sys.stderr.write( + "{fname} is not a valid name list file.\n".format( + fname=opt.list + )) sys.exit(1) finally: fp.close() try: - fp = open(opt.save_stats,'wb') + fp = open(opt.save_stats, 'wb') except TypeError: pass except IOError: - sys.stderr.write("Warning: could not save stats data on file "+ - opt.save_stats+"\n") + sys.stderr.write( + "Warning: could not save stats data " + + "on file {save_stats}\n".format(save_stats=opt.save_stats) + ) else: try: mystats.save_stats(fp) except IOError: - sys.stderr.write("Warning: could not save stats data on file "+ - opt.save_stats+"\n") + sys.stderr.write( + "Warning: could not save stats data on " + + "file {save_stats}\n".format(save_stats=opt.save_stats) + ) finally: fp.close() @@ -203,4 +217,5 @@ def main(): print(mystats.generate_name(), end=' ') -if __name__ == '__main__': main() +if __name__ == '__main__': + main() -- cgit v1.2.3