def find(self, name): all_commands = list(self._commands.keys()) expr = re.sub('([^:]+|)', lambda m: re.escape(m.group(1)) + '[^:]*', name) commands = collect(all_commands).filter( lambda x: re.findall('^%s' % expr, x)).sort() if not commands or commands.filter( lambda x: re.findall('^%s$' % expr, x)).count() < 1: pos = name.find(':') if pos >= 0: # Check if a namespace exists and contains commands self.find_namespace(name[:pos]) alternatives = self.find_alternatives(name, all_commands) raise CommandNotFound(name, alternatives) # Filter out aliases for commands which are already on the list if len(commands) > 1: command_list = self._commands def f(name_or_alias): command_name = command_list[name_or_alias].get_name() return command_name == name_or_alias or (command_name not in commands) commands = commands.filter(f).sort() exact = name in commands if len(commands) > 1 and not exact: raise AmbiguousCommand(name, commands) return self.get(name if exact else commands[0])
def next_drafter(self): # ordering 0 --> # ordering 1 <-- # change the drafter to the next drafter current_drafter = self.current_id draft_order = collect(self.draftorder.split(',')) next_drafter = None i = 0 for drafter in draft_order: if self.ordering == 0: if drafter == str(current_drafter): try: next_drafter = draft_order[i+1] break except IndexError: next_drafter = draft_order[i] self.ordering = 1 break else: if drafter == str(current_drafter): if drafter == draft_order[0]: # user if the first one next_drafter = draft_order[i] self.ordering = 0 else: next_drafter = draft_order[i-1] i += 1 self.current_id = next_drafter self.save()
def dot(self, search, dictionary, default=None): """The search string in dot notation to look into the dictionary for. Arguments: search {string} -- This should be a string in dot notation like 'key.key.value'. dictionary {dict} -- A normal dictionary which will be searched using the search string in dot notation. Keyword Arguments: default {string} -- The default value if nothing is found in the dictionary. (default: {None}) Returns: string -- Returns the value found the dictionary or the default value specified above if nothing is found. """ if "." not in search: if search == "": return dictionary try: return dictionary[search] except KeyError: return default searching = search.split(".") possible = None while searching: dic = dictionary for search in searching: if not dic: if "*" in searching: return [] return default if isinstance(dic, list): try: return (collect(dic).pluck( searching[searching.index("*") + 1]).serialize()) except IndexError: return dic except KeyError: return [] dic = dic.get(search) if isinstance(dic, str) and dic.isnumeric(): continue if (dic and not isinstance(dic, int) and len(dic) == 1 and not isinstance(dic[list(dic)[0]], dict)): possible = dic if not isinstance(dic, dict): return dic del searching[-1] return possible
def get_namespaces(self): namespaces = [] for command in self._commands.values(): namespaces.append(self.extract_namespace(command.get_name())) for alias in command.get_aliases(): namespaces.append(self.extract_namespace(alias)) return collect(namespaces).filter(lambda n: n).unique()
def find_namespace(self, namespace): all_namespaces = self.get_namespaces() expr = re.sub('([^:]+|)', lambda m: re.escape(m.group(1)) + '[^:]*', namespace) namespaces = (collect(all_namespaces).filter( lambda x: re.findall('^%s' % expr, x)).sort()) if not namespaces: alternatives = self.find_alternatives(namespace, all_namespaces) raise NamespaceNotFound(namespace, alternatives) exact = namespace in namespaces if len(namespaces) > 1 and not exact: raise AmbiguousNamespace(namespace, namespaces) return namespace if exact else namespaces[0]
def test_columns_listing(self): column_names = (collect(self.schema().get_column_listing( OratorTestUser().get_table())).sort().all()) self.assertEqual(['created_at', 'email', 'id', 'updated_at'], column_names)
def get(self): if self._library is not None: return self._library library = self.cache.remember(self.cache_key('library'), 24 * 60, lambda: self.api.get_all_songs()) self._library = [] artists_dict = defaultdict(list) albums_dict = defaultdict(list) artists = [] albums = [] for t in library: artist = t.get('artist', 'Unknown Artist') artists_dict[artist].append(t) album = t.get('album', 'Untitled Album') albums_dict[album].append(t) # With our dictionary of artists we can now loop through and partition in to albums for artist_, songs_of_artist in artists_dict.items(): # Create a new default dict for each album in the same way that the artists were partitioned artist = Artist(self, artist_, artist_) albums_of_artist_dict = defaultdict(list) for i in songs_of_artist: album_name = i.get('album', 'Untitled Album') albums_of_artist_dict[album_name].append(i) for album, tracks_ in albums_of_artist_dict.items(): album_name = album if album == "": album_name = "Untitled Album" album = Album(self, album_name, album_name) tracks = [] for track_ in tracks_: tracks.append( Track(self, track_['id'], track_['title'], track_.get('artist', 'Unknwown Artist'), album_name, track_.get('trackNumber', 1), track_.get('discNumber', 1), metadata=track_)) for track in collect(tracks).sort(lambda x: (x.disc_number, x.number)): album.add_track(track) artist.add_album(album) artists.append(artist) for album_name, tracks_ in albums_dict.items(): album = Album(self, album_name, album_name) tracks = [] for track_ in tracks_: tracks.append( Track(self, track_['id'], track_['title'], track_.get('artist', 'Unknwown Artist'), album_name, track_.get('trackNumber', 1), track_.get('discNumber', 1), metadata=track_)) for track in collect(tracks).sort(lambda x: (x.disc_number, x.number)): album.add_track(track) albums.append(album) self._library = { 'artists': collect(artists).sort(lambda a: a.name), 'albums': collect(albums).sort(lambda a: a.name), '_default': library } return self._library
def test_columns_listing(self): column_names = (collect(self.schema().get_column_listing( OratorTestUser().get_table())).sort().all()) self.assertEqual(["created_at", "email", "id", "updated_at"], column_names)