Example #1
0
def song_from_string(song_string):
    """
    >>> gene1 = NoteGene(1,1,1,1,1)
    >>> gene2 = NoteGene(2,2,2,2,2)
    >>> gene3 = NoteGene(3,3,3,3,3)
    >>> gene4 = NoteGene(4,4,4,4,4)
    >>> nc1 = NoteChromosome(gene1,gene2,track_id=0,volume=1)
    >>> nc2 = NoteChromosome(gene3,gene4,track_id=2,volume=3)
    >>> song1 = GeneticSong(nc1, nc2)
    >>> song2 = song_from_string(song1.__repr__())
    >>> song1 == song2
    True
    """
    meta_data_list = song_string.split('\n')[0:4]  
    song_id, score, crossover_chance, mutation_chance = map(int, meta_data_list)
    chromo_strings = song_string.split('\n', 4)[-1].replace('\n\n', '*').split('*')

    chromo_list = map(chromosome_from_string, chromo_strings)

    rv = GeneticSong(*chromo_list)
    rv.score = score
    rv.crossover_chance = crossover_chance
    rv.mutation_chance = mutation_chance
    rv.song_id = song_id
    
    return rv
def song_from_string(song_string, config_file):
    meta_data_list = song_string.split('\n')[0:2]  
    song_id, tempo = map(int, meta_data_list)
    chromo_strings = song_string.split('\n', 2)[-1].replace('\n\n', '*').split('*')

    chromo_list = [chromosome_from_string(string, config_file) for string in chromo_strings]
    t_range = range(config_file.min_tempo, config_file.max_tempo + 1)
    rv = GeneticSong(*chromo_list, tempo=tempo, tempo_range=t_range)
    rv.song_id = song_id
    
    return rv