def make_multiple_choice(self, chapter_index, cumulative=False, question_type=0): self.__chapter_index = chapter_index self.__answer_choices = [] if cumulative: if randint(0, 1) == 0: chapter_index = randint(1, chapter_index) question = (Prompt.select().where( Prompt.chapter_index == chapter_index, Prompt.type == question_type).order_by(fn.Rand()).get()) self.__question_index = question.get_index() self.__question_text = question.get_prompt_text() correct_choice = Choice.get(Choice.question_index == question, Choice.correct == True) self.__answer_choices.append(correct_choice.get_json_min()) self.__correct_choice_index = correct_choice.get_index() incorrect_choices = (Choice.select().where( Choice.question_index == question, Choice.correct == False).order_by( fn.Rand()).limit(number_of_multiple_choices - 1)) for incorrect_choice in incorrect_choices: self.__answer_choices.append(incorrect_choice.get_json_min()) shuffle(self.__answer_choices) return self
def make_definition_question( self, chapter_index, question_type=None, cumulative=False, ): # if no question type is requested, flip a coin to determine the question type self.__chapter_index = chapter_index if question_type not in [0, 1]: self.__question_type = randint(0, 1) else: self.__question_type = question_type if cumulative: chapter_index = randint(1, chapter_index) query = (Word.select().where( Word.chapter_index == chapter_index).order_by( fn.Rand()).limit(number_of_multiple_choices)) if self.__question_type == 1: definition_list = [] for word in query: raw_definition = (Definition.select().where( Definition.word_index == word.get_question_index).order_by( fn.Rand()).limit(1))[0] definition = { "text": raw_definition.definition, "index": word.get_question_index } definition_list.append(definition) self.__answer_choices = definition_list self.__question_text = query[0].word # question type 0 is definition prompt with word choices elif self.__question_type == 0: word_list = [] for element in query: word = { "text": element.word, "index": element.get_question_index } word_list.append(word) raw_definition = (Definition.select().where( Definition.word_index == query[0].get_question_index).order_by( fn.Rand()).limit(1))[0] self.__answer_choices = word_list self.__question_text = raw_definition.definition self.__word_index = query[0].get_question_index shuffle(self.__answer_choices) return self
class ArticleFactory(PeeweeModelFactory): class Meta: model = operational.Article type = factory.LazyFunction( lambda: operational.Type.select().order_by(fn.Rand()).get()) size = factory.LazyFunction( lambda: operational.Size.select().order_by(fn.Rand()).get()) color = factory.LazyFunction( lambda: operational.Color.select().order_by(fn.Rand()).get()) name = factory.Faker('bs')
def get_stat_raw_by_hour(self, read_hour, window=0, limit=1): if window > 0: query = Stats_Raw.select().where((Stats_Raw.read_hour.between( read_hour - window, read_hour + window))).order_by(fn.Rand()).limit(limit) else: query = Stats_Raw.select().where( Stats_Raw.read_hour == read_hour).order_by( fn.Rand()).limit(limit) results = self.transform_query_to_list(query) if len(results) > 0: uris = [o.uri for o in results] return uris
class OfferFactory(PeeweeModelFactory): class Meta: model = operational.Offer article = factory.LazyFunction( lambda: operational.Article.select().order_by(fn.Rand()).get()) seller = factory.LazyFunction( lambda: operational.Seller.select().order_by(fn.Rand()).get()) price = FuzzyDecimal(200, 50000) created_at = FuzzyDateTime( start_dt=(datetime.now(pytz.timezone('Europe/Belgrade')) - relativedelta(years=1)), end_dt=datetime.now(pytz.timezone('Europe/Belgrade')) ) #factory.Faker('date_time_this_year', before_now=True, after_now=False, tzinfo=None)
def FirstFetch(self): try: last_domain = DarkNet_Domain.select().order_by(DarkNet_Domain.datetime).limit(1) self.domain = self.domain if not last_domain else last_domain[0].domain self.warn(f"[{self.domain}]Getting PHPSESSID") self.session.cookies.clear() self.info(f"Already Cleaned Session Cookies.") self.RefreshNewDomain(self.session.get(f"http://{self.domain}")) resp = self.session.get(f"http://{self.domain}/index.php") self.info(f"Current Cookie Nums: {len(self.session.cookies)}") user = ( DarkNet_User.select() .where(DarkNet_User.useful == True) .order_by(fn.Rand()) .limit(1) ) if not bool(user): self.Reg() else: self.usr = user[0].user self.pwd = user[0].pwd if random.choice([1, 0, 0]): # 佛系注册堆积账号池 self.Reg() return True except KeyboardInterrupt: pass except Exception as e: raise
def FirstFetch(self): targets = DarkNetWebSites.select().where( DarkNetWebSites.ismaster == self.usemaster) if not targets: return target = targets[0] try: self.warn(f'[{target.domain}]Getting PHPSESSID') resp = self.session.get(f'http://{target.domain}') target.ismaster = True target.title = jq(resp.text)('title').text() self.usemaster = True self.master = target self.domain = target.domain user = DarkNet_User.select().where( DarkNet_User.useful == True).order_by(fn.Rand()).limit(1) if not bool(user): self.Reg() else: self.usr = user[0].user self.pwd = user[0].pwd if random.choice([1, 0]): # 佛系注册堆积账号池 self.Reg() return True except KeyboardInterrupt: pass except requests.Timeout: target.alive = False target.ismaster = False self.usemaster = False except Exception as e: raise finally: target.save()
def seed_addresses(generator, number_of_addresses): for _ in range(0, number_of_addresses): # get an address, split on newlines fake_address = generator.address() address_list = fake_address.split("\n") address1 = address_list[0] zipcode = random.randrange(10000, 99999) # if more than 2 items in list, assume list[1] can be address2 if len(address_list) > 2: address2 = address_list[1] else: address2 = "" result = City.select(City.cityId).order_by(fn.Rand()).limit(1) city = result[0].cityId user = random_user() Address.create( address=address1, address2=address2, cityId=city, postalCode=zipcode, phone=generator.phone_number(), createdBy=user, lastUpdateBy=user )
def get_all_products_from_db(self): """this method is to access all product name from db.""" for product in (Product.select().order_by( fn.Rand()).limit(10)): # get randomly 10 products from the db print(product.product_name)
class DistrictFactory(PeeweeModelFactory): class Meta: model = operational.District name = Faker('bothify', text="## ??") city = factory.LazyFunction( lambda: operational.City.select().order_by(fn.Rand()).get())
async def get_recommend_4_courses(): course_list = [] sq = Course.select().order_by(fn.Rand()).limit(4) courses = await manager.execute(sq) for c in courses: course_list.append(c.asDict()) return course_list
class OrderFactory(PeeweeModelFactory): class Meta: model = operational.Order customer = factory.LazyFunction( lambda: operational.Customer.select().order_by(fn.Rand()).get()) created_at = FuzzyDateTime( start_dt=(datetime.now(pytz.timezone('Europe/Belgrade')) - relativedelta(years=1)), end_dt=datetime.now(pytz.timezone('Europe/Belgrade')) ) #factory.Faker('date_time_this_year', before_now=True, after_now=False, tzinfo=None) status = FuzzyChoice([status[0] for status in Order.OrderStatus]) @factory.post_generation def articles(self, create, extracted, **kwargs): if not create: return n_offers = randrange(1, 5) offers = [ offer for offer in Offer.select().order_by(fn.Rand()).limit(n_offers) ] for i in range(n_offers): offer_order = OfferOrder() offer_order.order_id = self.id offer_order.offer = offers[i] offer_order.amount = randrange(1, 2) offer_order.save(force_insert=True)
def record_rerandomize(): if not isinstance(g.identity, identity.Super): response.json(response.Forbidden) else: ApplyInfo.update(random_id=fn.Rand()).execute() db_transaction_succeeded() response.json(response.Success)
def __proxies(self): '''构造代理IP,需要提供代理IP保存的数据库名称和表名''' ip = model.ProxyIp.select().where(model.ProxyIp.status == 1).order_by( fn.Rand()).limit(1).get() if ip: return ip.getProxies() else: return False
def get_products_from_category(self, category): """this method is to get all products from a category.""" self.query = category return list(Product.select().join(Category).where( Category.category_name == self.query).order_by( fn.Rand()).limit(10))
def get_random_user(last_days=30): try: return (DarkNet_User.select().where( DarkNet_User.useful == True, DarkNet_User.intime >= moment.now().add( days=-last_days).format("YYYY-MM-DD HH:mm:ss"), ).order_by(fn.Rand()).limit(10)[0]) except Exception as e: error(f"[Cursor->get_random_user]: {e}")
def seed_appointments(generator, number_of_appointments): utc_convert = datetime.timedelta(hours=settings.UTC_OFFSET) start = datetime.datetime.now() start = start.replace(hour=8, minute=0, second=0, microsecond=0) end = start + datetime.timedelta(minutes=15) for _ in range(0, number_of_appointments): customer = (Customer.select(Customer.customerId, Customer.customerName) .order_by(fn.Rand()).limit(1)[0]) cityName = City.select(City.city).order_by(fn.Rand()).limit(1)[0].city user = random_user() if start.hour > 16: # don't schedule appointments after 5pm, set hours to 8am start = start.replace(hour=8) start += datetime.timedelta(days=1) end = end.replace(hour=8, minute=15) end += datetime.timedelta(days=1) skip_period = 0 # if meeting start time on weekend, set start to the following monday if start.weekday() == 5: skip_period = 2 elif start.weekday() == 6: skip_period = 1 skip_weekend = datetime.timedelta(days=skip_period) start += skip_weekend end += skip_weekend Appointment.create( customerId=customer.customerId, title=generator.sentence(), description=generator.text(), location=cityName, contact=customer.customerName, url=generator.url(), start=start - utc_convert, end=end - utc_convert, createdBy=user, lastUpdateBy=user ) start = start + datetime.timedelta(hours=1) end = end + datetime.timedelta(hours=1)
def get_random_user(): try: return ( DarkNet_User.select() .where(DarkNet_User.useful == True) .order_by(fn.Rand()) .limit(1)[0] ) except Exception as e: error(f"[Cursor->get_random_user]: {e}")
class SellerFactory(PeeweeModelFactory): class Meta: model = operational.Seller PIB = FuzzyText(chars="0123456789", length=9) name = Faker('company') street = Faker('street_name') number = Faker('building_number') district = factory.LazyFunction( lambda: operational.District.select().order_by(fn.Rand()).get())
def seed_cities(generator, number_of_cities): for _ in range(0, number_of_cities): result = Country.select(Country.countryId).order_by(fn.Rand()).limit(1) user = random_user() City.create( city=generator.city(), countryId=result[0].countryId, createdBy=user, lastUpdateBy=user, )
def __init__(self): """ The __init__ method send the query and save it in self.data SQL : SELECT * FROM Categories ORDER BY RAND () LIMIT 5; """ self.data = tables.Categories.select().order_by(fn.Rand()).limit(5)
def get_healthy_products(self, category): """this method is to get all healthy product from a category from the database.""" self.query = category return list( Product.select(Product.product_name, Product.category, Product.nutrition_grade_fr).join(Category).where( (Category.category_name == self.query) & (Product.nutrition_grade_fr <= "b")).order_by( fn.Rand()).limit(10))
def post_index(): all_posts = Post.get_posts(index_only=True)\ .order_by(Post.date_posted.desc())\ .limit(config.POSTS_PER_PAGE) for item in all_posts: item.post_text = shorten_text(item.post_text) random_banner = Banner.select()\ .where(Banner.disabled == False)\ .order_by(fn.Rand())\ .first() quote = Quote.select().order_by(fn.Rand()).first() messages = StreamMessage.select() template = env.get_template('post/index.html') return template.render(posts=all_posts, banner=random_banner, stream_messages=messages, quote=quote, link_what='pstlink')
def seed_customers(generator, number_of_customers): for _ in range(0, number_of_customers): result = Address.select(Address.addressId).order_by(fn.Rand()).limit(1) address = result[0].addressId active = random.choice([0, 1]) user = random_user() Customer.create( customerName=generator.name(), addressId=address, active=active, createdBy=user, lastUpdateBy=user )
def get(self): logger.log_python_api_get(SuggestedProfilesResource.api_url) current_user_profile = Profile.get(id=current_user.id) skills_list = safe_split_strip_remove_empty( current_user_profile.skills) location_part_list = safe_split_strip_remove_empty( current_user_profile.location) position_title_list = [p.title for p in current_user_profile.positions] clauses = [Profile.id != current_user.id] or_clauses = [] for skill in skills_list: or_clauses.append(Profile.skills.contains(skill)) for location_part in location_part_list: or_clauses.append(Profile.location.contains(location_part)) if any(position_title_list): subquery = Position.select(Param('1')).where( Position.profile == Profile.id, Position.title << position_title_list) or_clauses.append(Clause(SQL('EXISTS'), subquery)) if any(or_clauses): clauses.append(reduce(operator.or_, or_clauses)) friends = Friend.select( Friend.friend).where(Friend.user == current_user.id).execute() clauses.append(~(Profile.id << [f.friend.id for f in friends])) profiles = Profile.select().where(reduce( operator.and_, clauses)).order_by(fn.Rand()).limit(100) for profile in profiles: profile.score = 0 for skill in skills_list: if profile.skills and skill in profile.skills: profile.score += 10 for part in location_part_list: if profile.location and part in profile.location: profile.score += 10 if any(position_title_list): profile.position_fetch = profile.positions.execute() for position_title in position_title_list: if any(position.title == position_title for position in profile.position_fetch): profile.score += 10 suggested_profiles = sorted(profiles, key=lambda profile: -profile.score)[:2] return list(map(lambda p: self.profile_to_dict(p), suggested_profiles))
def return_name(self): # Connect to db models.db.connect(reuse_if_open=True) result = "" resultDic = {} # Append to result based on what names the monster has if self._First_Name: FName = self._First_Name_Model.select().order_by( fn.Rand()).limit(1) fullName = (FName[0].firstName) resultDic["firstName"] = FName[0].firstName if self._Last_Name: LName = self._Last_Name_Model.select().order_by(fn.Rand()).limit(1) resultDic["lastName"] = LName[0].lastName if len(fullName) != 0: fullName += " " + (LName[0].lastName) else: fullName = (LName[0].lastName) resultDic["fullName"] = fullName print(resultDic) # Disconnect from DB models.db.close() # Return json data return make_response(json.dumps(resultDic), 200)
def get(): speed = request.args.get('speed') query = model.ProxyIp.select().where(model.ProxyIp.status == 1) if speed: speed = int(speed) query = query.where(model.ProxyIp.speed < speed) proxy = query.order_by(fn.Rand()).limit(1).first() # if ip: # return ip.getProxies() # else: # return False if proxy: return proxy.getProxies() return {"code": 1, "msg": 'no proxy!'}
def articles(self, create, extracted, **kwargs): if not create: return n_offers = randrange(1, 5) offers = [ offer for offer in Offer.select().order_by(fn.Rand()).limit(n_offers) ] for i in range(n_offers): offer_order = OfferOrder() offer_order.order_id = self.id offer_order.offer = offers[i] offer_order.amount = randrange(1, 2) offer_order.save(force_insert=True)
def get_html(session, url, delay_time=0, headers=None): time.sleep(delay_time) try: if USE_PROXY: proxy = Proxys.select().where(Proxys.score > 0).order_by( fn.Rand()).limit(1) if proxy: proxy_ip = "http://{0}:{1}".format(proxy[0].ip, proxy[0].port) response = session.get(url, headers=headers, proxies={"https": proxy_ip}) else: response = session.get(url, headers=headers) else: response = session.get(url, headers=headers) response.encoding = "gbk" except Exception as e: raise RuntimeError(f"{ERR_PREFIX} get_html({url}) fail, error:{e}") return response.text
def proposed_product(product_choice, c_category, user_name): """ Def to propose, print, and store the products. """ query_product = (Product.select().where( Product.product_name_fr == product_choice)) c_product = query_product[0] print( str(c_product.product_name_fr).upper(), "!\n Son identifiant est:\n", c_product._id, "\n Ses ingredients :\n", c_product.ingredients_text_fr, "\n Son site internet est :\n", c_product.url) print("Je vous propose le produit suivant! ") query_proposed_product = ( Product.select().join(ProductCategory).join(Category).where( (Product.nutrition_grade_fr == c_product.nutrition_grade_fr) & (Category.categories == c_category)).order_by( fn.Rand()).limit(1)) proposed_product = query_proposed_product[0] print( str(proposed_product.product_name_fr).upper(), "!\n Son identifiant est:\n", proposed_product._id, "\n Ses ingredients sont:\n", proposed_product.ingredients_text_fr, "\n Son site internet est :\n", proposed_product.url) rec_current_products(c_product, proposed_product, user_name) while True: web_page_ask2 = input("Voulez-vous voir leur page internet?\n" + "[O] = Oui [N] = Non (Retour au menu)") if web_page_ask2 == "O" or web_page_ask2 == "o": webbrowser.open_new(c_product.url) webbrowser.open_new(proposed_product.url) break if web_page_ask2 == "N" or web_page_ask2 == "n": break else: continue