diff options
author | Elena ``of Valhalla'' Grandi <elena.valhalla@gmail.com> | 2011-06-10 00:08:24 +0200 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <elena.valhalla@gmail.com> | 2011-06-10 00:08:24 +0200 |
commit | a5e97e3f0cf2466086f31046d47deb32be1685b9 (patch) | |
tree | 7b47a6d4a3008eed2b77519084cdaea9965fe9c6 /namegen.py | |
parent | e7d05b331b7dbd2a7041ee6e4f17c226d125660d (diff) |
namegen.py: use json instead of yaml to save stats.
yaml is slow and json is faster and available in the standard library.
Diffstat (limited to 'namegen.py')
-rwxr-xr-x | namegen.py | 26 |
1 files changed, 9 insertions, 17 deletions
@@ -43,16 +43,10 @@ LICENSE """ +import json import sys import random -try: - import yaml - skip_yaml = False -except ImportError: - sys.stderr.write("Could not find module YAML: will not be able to save / load statistics\n") - skip_yaml = True - class Stats: def __init__(self,context): @@ -73,6 +67,7 @@ class Stats: """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 xrange(min(self.context,len(name))): self._increment_stats(name[0:i],name[i]) for i in xrange(self.context,len(name)): @@ -80,17 +75,11 @@ class Stats: def load_stats(self,fp): """Load stats from a yaml in a string or file-like object""" - if skip_yaml: - return False - self.stats = yaml.safe_load(fp) + self.stats = json.load(fp) def save_stats(self,fp): """Save stats in yaml to a file-like object""" - if skip_yaml: - return False - yaml.safe_dump(self.stats,fp) - return True - + json.dump(self.stats,fp) def generate_name(self): """Generate a name following the stats""" @@ -110,8 +99,11 @@ class Stats: def main(): mystats = Stats(3) - fp = open('data/nomif2.txt') - mystats.read_names(fp) + #fp = open('data/nomif2.txt') + #mystats.read_names(fp) + #fp.close() + fp = open('it_female.stats') + mystats.load_stats(fp) fp.close() print mystats.generate_name(), |