def __next__(self): # nlive_points has to tell how many points were used when # the last sample was returned self.nlive_points = len(self.live_points) # select worst point self.live_points.sort(key=itemgetter(0)) Li, ui, xi = self.live_points.pop() Lsecond = self.live_points[-1][0] #if (Lsecond - Li) < (self.Lmax - Li) * self.expected_steps: # current iteration of nested integrator # it = self.samples - self.nlive_points r = (1 - exp(Lsecond - Li)) / (1 - exp(self.Lmax - Li)) / (1 - exp(-1./self.nlive_points)) if r < 1e-3: # there is a very flat curve # do not add a point (contract) print('Adaption: contracting') pass elif r > 1: # currently have a steep curve and maximum not exceeded # then fill up (expand) # this step can be parallelized print('Adaption: expanding') while len(self.live_points) < self.nlive_points_max: self.add_point(Lmin=Li) else: # reasonably flat, keep steady state if len(self.live_points) < self.nlive_points_max: self.add_point(Lmin=Li) # make sure we have at least the minimum number of points while len(self.live_points) < self.nlive_points_min: self.add_point(Lmin=Li) return ui, xi, Li
books = [b.replace('-', ' ') for b in books] books = [b.replace('_', ' ') for b in books] books = [b.replace(' ', '+') for b in books] # Getting exact title and rating of books from Goodreads def book_search(q, page=1, search_field='all'): auth_client = client.GoodreadsClient(key, secret) books_list = auth_client.search_books(str(q), page, search_field) return books_list[0].title, books_list[0].average_rating ratings = [] for book in books: try: print(book) ratings.append(book_search(book)) print(book_search(book)) print('-----') except: print('error!') continue # Sorting books by rating ratings.sort(key=itemgetter(1), reverse=True) # Exporting sorted list in a CSV file with open('output.csv', 'w') as f: wr = csv.writer(f) wr.writerows(ratings) print(ratings)
def schema_extract(x, **types_dict): # each kwarg is a a key, value pair # the key is the name of the attribute and the field in the database # value is the type to be used internally in the database return {key : value(itemgetter(key)(x)) for key, value in types_dict}