# Path: prefixes/models.py
# class Prefix(db.Expando):
#     """
#     Count all domains in the datastore that start with the same
#     prefix. The key name is the prefix, with a leading dot and
#     optional property 'resume' if incomplete.
#     """
#     length = db.IntegerProperty(required=True)
#     count = db.IntegerProperty(required=True, default=0)
#     com = db.IntegerProperty()
#     percentage = db.FloatProperty()
#     timestamp = db.DateTimeProperty()
# 
#     @classmethod
#     def name_cut(cls, name, length=None):
#         """
#         >>> Prefix.name_cut('123456789')
#         '123456789'
#         >>> Prefix.name_cut('123456789', 3)
#         '123'
#         """
#         if length is None:
#             return name
#         else:
#             return name[:length]
# 
# class Suffix(db.Expando):
#     """
#     Count all domains in the datastore that end with the same suffix.
#     The key name is the suffix backwards, with a leading dot
#     and optional property 'resume' if incomplete.
#     """
#     length = db.IntegerProperty(required=True)
#     count = db.IntegerProperty(required=True, default=0)
#     com = db.IntegerProperty()
#     percentage = db.FloatProperty()
#     timestamp = db.DateTimeProperty()
# 
#     @classmethod
#     def name_cut(cls, name, length=None):
#         """
#         >>> Suffix.name_cut('123456789')
#         '987654321'
#         >>> Suffix.name_cut('123456789', 3)
#         '987'
#         """
#         if length is None:
#             return name[::-1]
#         else:
#             return name[::-1][:length]


import os
import sys
from common.appenginepatch.aecmd import setup_env
from prefixes.models import Prefix, Suffix
        print '""".split()'
        max_score = float(length) / max(lengths)
        print 'for index, name in enumerate(POPULAR_%sES[%d]):' % (
            upper, length)
        print '    %s_SCORES[name] = (%d - index) / %.1f' % (
            upper, len(names), len(names) / max_score)


def score_function(variable, slice):
    func = """
def prefix_score(name):
    best_score = 0.0
    best_prefix = ''
    for length in range(2, min(6, len(name)) + 1):
        prefix = name[slice]
        score = PREFIX_SCORES.get(prefix, None)
        if score > best_score:
            best_score = score
            best_prefix = prefix
    return best_score, best_prefix
""".strip()
    func = func.replace('PREFIX', variable)
    func = func.replace('prefix', variable.lower())
    func = func.replace('[slice]', slice)
    print '\n\n' + func


def main():
    all_lengths(Prefix, LENGTHS)
    print

# Path: prefixes/models.py
# class Prefix(db.Expando):
#     """
#     Count all domains in the datastore that start with the same
#     prefix. The key name is the prefix, with a leading dot and
#     optional property 'resume' if incomplete.
#     """
#     length = db.IntegerProperty(required=True)
#     count = db.IntegerProperty(required=True, default=0)
#     com = db.IntegerProperty()
#     percentage = db.FloatProperty()
#     timestamp = db.DateTimeProperty()
# 
#     @classmethod
#     def name_cut(cls, name, length=None):
#         """
#         >>> Prefix.name_cut('123456789')
#         '123456789'
#         >>> Prefix.name_cut('123456789', 3)
#         '123'
#         """
#         if length is None:
#             return name
#         else:
#             return name[:length]
# 
# class Suffix(db.Expando):
#     """
#     Count all domains in the datastore that end with the same suffix.
#     The key name is the suffix backwards, with a leading dot
#     and optional property 'resume' if incomplete.
#     """
#     length = db.IntegerProperty(required=True)
#     count = db.IntegerProperty(required=True, default=0)
#     com = db.IntegerProperty()
#     percentage = db.FloatProperty()
#     timestamp = db.DateTimeProperty()
# 
#     @classmethod
#     def name_cut(cls, name, length=None):
#         """
#         >>> Suffix.name_cut('123456789')
#         '987654321'
#         >>> Suffix.name_cut('123456789', 3)
#         '987'
#         """
#         if length is None:
#             return name[::-1]
#         else:
#             return name[::-1][:length]


# Path: tools/prefixes_popular.py
import os
import sys
from common.appenginepatch.aecmd import setup_env
from prefixes.models import Prefix, Suffix

        print '""".split()'
        max_score = float(length) / max(lengths)
        print 'for index, name in enumerate(POPULAR_%sES[%d]):' % (
            upper, length)
        print '    %s_SCORES[name] = (%d - index) / %.1f' % (
            upper, len(names), len(names) / max_score)


def score_function(variable, slice):
    func = """
def prefix_score(name):
    best_score = 0.0
    best_prefix = ''
    for length in range(2, min(6, len(name)) + 1):
        prefix = name[slice]
        score = PREFIX_SCORES.get(prefix, None)
        if score > best_score:
            best_score = score
            best_prefix = prefix
    return best_score, best_prefix
""".strip()
    func = func.replace('PREFIX', variable)
    func = func.replace('prefix', variable.lower())
    func = func.replace('[slice]', slice)
    print '\n\n' + func


def main():
    all_lengths(Prefix, LENGTHS)
    print

    all_lengths(Suffix, LENGTHS)