def create_session(name): print('-- No saved session found, loading new.') session = Session( name='demo', mask={ # Each entry goes like this: 'Genre': pairup( # Pratice: Go lookup what this Providers does. GenreTreeProvider(), # Practice: Same for the DistanceFunction. GenreTreeDistance(), # This has the highest rating of the three attributes: 8 ), 'Title': pairup( # We can also compose Provider, so that the left one # gets the input value, and the right one the value # the left one processed. # In this case we first split the title in words, # then we stem each word. WordlistProvider() | StemProvider(), WordlistDistance(), 1 ), 'Artist': pairup( # If no Provider (None) is given the value is forwarded as-is. # Here we just use the default provider, but enable # compression. Values are saved once and are givean an ID. # Duplicate items get the same ID always. # You can trade off memory vs. speed with this. ArtistNormalizeProvider(compress=True), # If not DistanceFunctions is given, all values are # compare with __eq__ - which might give bad results. None, 1 ) } ) # As in our first example we fill the session, but we dont insert the full # database, we leave out the last song: with session.transaction(): for idx, (artist, title, genre) in enumerate(MY_DATABASE[:3]): # Notice how we use the uppercase keys like above: session.mapping[session.add({ 'Genre': genre, 'Title': title, 'Artist': artist, })] = idx return session
def create_session(name): print('-- No saved session found, loading new.') session = Session( name='demo', mask={ # Each entry goes like this: 'Genre': pairup( # Pratice: Go lookup what this Providers does. GenreTreeProvider(), # Practice: Same for the DistanceFunction. GenreTreeDistance(), # This has the highest rating of the three attributes: 8), 'Title': pairup( # We can also compose Provider, so that the left one # gets the input value, and the right one the value # the left one processed. # In this case we first split the title in words, # then we stem each word. WordlistProvider() | StemProvider(), WordlistDistance(), 1), 'Artist': pairup( # If no Provider (None) is given the value is forwarded as-is. # Here we just use the default provider, but enable # compression. Values are saved once and are givean an ID. # Duplicate items get the same ID always. # You can trade off memory vs. speed with this. ArtistNormalizeProvider(compress=True), # If not DistanceFunctions is given, all values are # compare with __eq__ - which might give bad results. None, 1) }) # As in our first example we fill the session, but we dont insert the full # database, we leave out the last song: with session.transaction(): for idx, (artist, title, genre) in enumerate(MY_DATABASE[:3]): # Notice how we use the uppercase keys like above: session.mapping[session.add({ 'Genre': genre, 'Title': title, 'Artist': artist, })] = idx return session
def test_find_matching_attributes_generic(self): from munin.provider import GenreTreeProvider from munin.distance import GenreTreeDistance from munin.helper import pairup session = Session('session_find_test', { 'genre': pairup(GenreTreeProvider(), GenreTreeDistance(), 5), 'artist': pairup(None, None, 1) }) session.add({ 'artist': 'Berta', 'genre': 'death metal' }) session.add({ 'artist': 'Hans', 'genre': 'metal' }) session.add({ 'artist': 'Berta', 'genre': 'pop' }) found = list(session.find_matching_attributes({'genre': 'metal'})) self.assertEqual(len(found), 1) self.assertEqual(found[0], session[1]) found = list(session.find_matching_attributes( {'genre': 'metal', 'artist': 'Berta'} )) self.assertEqual(len(found), 0) found = list(session.find_matching_attributes( {'genre': 'metal', 'artist': 'Hans'} )) self.assertEqual(len(found), 1) self.assertEqual(found[0], session[1]) found = list(session.find_matching_attributes( {'genre': 'pop', 'artist': 'Berta'} )) self.assertEqual(len(found), 1) self.assertEqual(found[0], session[2]) found = list(session.find_matching_attributes({'artist': 'Berta'})) self.assertEqual(len(found), 2) self.assertEqual(found[0], session[0]) self.assertEqual(found[1], session[2])
def test_find_matching_attributes_numeric(self): from munin.provider import GenreTreeProvider from munin.distance import GenreTreeDistance from munin.helper import pairup session = Session('session_find_test', { 'x': pairup(None, None, 1), 'y': pairup(None, None, 1) }) a = session[session.add({ 'x': 21, 'y': 42, })] b = session[session.add({ 'x': 0, 'y': 100, })] session[session.add({ 'x': 51, 'y': 50, })] self.assertEqual(list(session.database.find_matching_attributes_numeric( {'x': 10}, 20 )), [a, b] ) self.assertEqual(list(session.database.find_matching_attributes_numeric( {'y': 100}, 0 )), [b] ) self.assertEqual(list(session.database.find_matching_attributes_numeric( {'x': 10, 'y': 40}, 20 )), [a] ) self.assertEqual(list(session.database.find_matching_attributes_numeric( {'x': 10, 'y': 10}, 0, )), [] )
def test_find_matching_attributes_generic(self): from munin.provider import GenreTreeProvider from munin.distance import GenreTreeDistance from munin.helper import pairup session = Session( 'session_find_test', { 'genre': pairup(GenreTreeProvider(), GenreTreeDistance(), 5), 'artist': pairup(None, None, 1) }) session.add({'artist': 'Berta', 'genre': 'death metal'}) session.add({'artist': 'Hans', 'genre': 'metal'}) session.add({'artist': 'Berta', 'genre': 'pop'}) found = list(session.find_matching_attributes({'genre': 'metal'})) self.assertEqual(len(found), 1) self.assertEqual(found[0], session[1]) found = list( session.find_matching_attributes({ 'genre': 'metal', 'artist': 'Berta' })) self.assertEqual(len(found), 0) found = list( session.find_matching_attributes({ 'genre': 'metal', 'artist': 'Hans' })) self.assertEqual(len(found), 1) self.assertEqual(found[0], session[1]) found = list( session.find_matching_attributes({ 'genre': 'pop', 'artist': 'Berta' })) self.assertEqual(len(found), 1) self.assertEqual(found[0], session[2]) found = list(session.find_matching_attributes({'artist': 'Berta'})) self.assertEqual(len(found), 2) self.assertEqual(found[0], session[0]) self.assertEqual(found[1], session[2])
def test_find_matching_attributes_numeric(self): from munin.provider import GenreTreeProvider from munin.distance import GenreTreeDistance from munin.helper import pairup session = Session('session_find_test', { 'x': pairup(None, None, 1), 'y': pairup(None, None, 1) }) a = session[session.add({ 'x': 21, 'y': 42, })] b = session[session.add({ 'x': 0, 'y': 100, })] session[session.add({ 'x': 51, 'y': 50, })] self.assertEqual( list( session.database.find_matching_attributes_numeric( {'x': 10}, 20)), [a, b]) self.assertEqual( list( session.database.find_matching_attributes_numeric( {'y': 100}, 0)), [b]) self.assertEqual( list( session.database.find_matching_attributes_numeric( { 'x': 10, 'y': 40 }, 20)), [a]) self.assertEqual( list( session.database.find_matching_attributes_numeric( { 'x': 10, 'y': 10 }, 0, )), [])
def __init__(self, name='EasySession', disabled_attrs=None): mask = { 'artist': pairup( ArtistNormalizeProvider(compress=True), None, 0.5, ), 'album': pairup( AlbumNormalizeProvider(compress=True), None, 0.5, ), 'title': pairup( TitleNormalizeProvider(compress=False) | StemProvider(), LevenshteinDistance(), 1, ), 'date': pairup(DateProvider(), DateDistance(), 2), 'bpm': pairup(BPMCachedProvider(), BPMDistance(), 3), 'lyrics': pairup(KeywordsProvider(), KeywordsDistance(), 3), 'rating': pairup(None, RatingDistance(), 2), 'genre': pairup(GenreTreeProvider(), GenreTreeAvgDistance(), 4), 'moodbar': pairup(MoodbarAudioFileProvider(), MoodbarDistance(), 5) } if not check_for_moodbar(): logging.warning('Disabling moodbar attr, no binary found in PATH.') del mask['moodbar'] if not check_for_bpmtools(): logging.warning("Disabling bpm attr, no binary found in PATH.") del mask['bpm'] for disabled_attr in disabled_attrs or []: try: del mask[disabled_attr] except KeyError: pass Session.__init__(self, name, mask)
def __init__(self, name='EasySession', disabled_attrs=None): mask = { 'artist': pairup( ArtistNormalizeProvider(compress=True), None, 0.5, ), 'album': pairup( AlbumNormalizeProvider(compress=True), None, 0.5, ), 'title': pairup( TitleNormalizeProvider(compress=False) | StemProvider(), LevenshteinDistance(), 1, ), 'date': pairup( DateProvider(), DateDistance(), 2 ), 'bpm': pairup( BPMCachedProvider(), BPMDistance(), 3 ), 'lyrics': pairup( KeywordsProvider(), KeywordsDistance(), 3 ), 'rating': pairup( None, RatingDistance(), 2 ), 'genre': pairup( GenreTreeProvider(), GenreTreeAvgDistance(), 4 ), 'moodbar': pairup( MoodbarAudioFileProvider(), MoodbarDistance(), 5 ) } if not check_for_moodbar(): logging.warning('Disabling moodbar attr, no binary found in PATH.') del mask['moodbar'] if not check_for_bpmtools(): logging.warning("Disabling bpm attr, no binary found in PATH.") del mask['bpm'] for disabled_attr in disabled_attrs or []: try: del mask[disabled_attr] except KeyError: pass Session.__init__(self, name, mask)