def _entities2dict_part(cls, entities): Param = PriceSkillParameter param_type = Param.Type.entity_group2parameter_type(entities) field = cls.parameter_type2field(param_type) if param_type == Param.Type.PORTLIKE: port_codenames = lchain( *map(lambda x: Portlike.entity_portlike2port_codenames(x), entities)) return {field: port_codenames} if param_type == Param.Type.TRADEGOOD: tradegood_codenames = lmap(FoxylibEntity.entity2value, entities) return {field: tradegood_codenames} if param_type == Param.Type.RATE: entity = l_singleton2obj(entities) rate = FoxylibEntity.entity2value(entity) return {field: rate} if param_type == Param.Type.TREND: entity = l_singleton2obj(entities) trend = FoxylibEntity.entity2value(entity) return {field: trend} raise Exception({ "param_type": param_type, "entities": entities, })
def iter2duplicate_docs( cls, iterable, key=None, ): """ Basically identical to dict_group_by, but in iterable form to be able to return as soon as duplicate found """ from foxylib.tools.collections.collections_tool import l_singleton2obj h_key2docs = defaultdict(list) for doc in cls.Doc.iterable2docs(iterable, key=key): k = cls.Doc.doc2key(doc) docs_prev = h_key2docs.get(k) # duplicate found for the first time. yield previous duplicate if len(docs_prev) == 1: yield l_singleton2obj(docs_prev) # duplicate existed beforehand. yield me if docs_prev: yield doc docs_prev.append(doc)
def video_id2live_streaming_data(cls, video_id, credentials): logger = FoxylibLogger.func_level2logger( cls.video_id2live_streaming_data, logging.DEBUG) # https://stackoverflow.com/questions/36683878/youtube-api-how-do-i-get-the-livechatid # https://developers.google.com/youtube/v3/docs/videos/list from foxylib.tools.googleapi.foxylib_googleapi import FoxylibGoogleapi # credentials = FoxylibGoogleapi.ServiceAccount.credentials() service = YoutubeapiTool.credentials2service(credentials) request = service.videos().list( id=video_id, part='liveStreamingDetails', ) response = request.execute() logger.debug({"response": response}) items = response.get("items") if not items: return None item = l_singleton2obj(items) return item.get("liveStreamingDetails")
def price_lang2text(cls, price, lang): logger = HenriqueLogger.func_level2logger(cls.price_lang2text, logging.DEBUG) if price is None: return cls.lang2text_idk(lang) rate = MarketpriceDoc.price2rate(price) trend = MarketpriceDoc.price2trend(price) # raise Exception({"price":price}) channel_user_codename = MarketpriceDoc.price2channel_user(price) channel_user = l_singleton2obj( ChannelUser.codenames2channel_users([channel_user_codename])) logger.debug({"price": price, "channel_user": channel_user}) # raise Exception({"price":price}) created_at = MarketpriceDoc.price2created_at( price) or MarketpriceDoc.created_at_backoff() td = datetime.now(tz=pytz.utc) - created_at str_timedelta = HenriqueDatetime.timedelta_lang2str(td, lang) arrow = Trend.trend2arrow(trend) text_out_base = format_str("{}{} @ {}", str(rate), arrow, str_timedelta) if td >= HenriqueDatetime.Constant.TIMEDELTA_OUTDATED: return text_out_base user_alias = ChannelUser.channel_user2alias(channel_user) str_user_alias = "[by {}]".format(user_alias) text_out = " ".join([text_out_base, str_user_alias]) return text_out
def j_chat2role(cls, chat): l_matched = lfilter(lambda x: x.chat2is_role_matched(chat), cls.role_class_list()) if not l_matched: return None role_class = l_singleton2obj(l_matched) return role_class.NAME
def url_param2value(cls, url, param): h = cls.url2h_query(url) l = h.get(param) if not l: return None return l_singleton2obj(l)
def test_01(self): youtube_id = "4VYAaLh3XZg" scopes = ["https://www.googleapis.com/auth/youtube.readonly"] cachefile = os.path.join(FILE_DIR, "token.pickle") cachefuncs = CredentialCache.filepath2cachefuncs_pickle(cachefile) flow = InstalledAppFlow.from_client_secrets_file( FoxylibGoogleapi.Oauth.filepath_credentials(), scopes) credentials = GoogleAPITool.cache_or_func2cred( cachefuncs, partial(flow.run_local_server, port=0)) youtube = googleapiclient.discovery.build("youtube", "v3", credentials=credentials, cache_discovery=False) request = youtube.videos().list( part="snippet,contentDetails,statistics", id=youtube_id) response = request.execute() hyp_01 = JsonTool.down(response, ["kind"]) self.assertEqual(hyp_01, "youtube#videoListResponse") item = l_singleton2obj(JsonTool.down(response, ["items"])) hyp_02 = JsonTool.down(item, ["id"]) self.assertEqual(hyp_02, youtube_id) hyp_03 = JsonTool.down(item, ["kind"]) self.assertEqual(hyp_03, "youtube#video")
def test_01(self): logger = FoxylibLogger.func_level2logger(self.test_01, logging.DEBUG) app, auth0 = FoxylibAuth0.app_auth0() c = app.test_client() response_login = c.get("/auth0/login/", follow_redirects=False) # logger.debug({"response.data": response.data, # "response.location": response.location, # }) self.assertEqual(response_login.status_code, 302) url_auth0 = response_login.location self.assertTrue( url_auth0.startswith("https://dev-8gnjw0rn.auth0.com/authorize")) h_next = URLTool.url2h_query(url_auth0) redirect_uri = l_singleton2obj(h_next.get("redirect_uri")) self.assertEqual(redirect_uri, "http://localhost:5000/auth0/callback/") response_auth0 = requests.get(url_auth0) logger.debug({ "response_auth0": response_auth0, # "response_auth0.text": response_auth0.text, # "response_auth0.content": response_auth0.content, }) self.assertEqual(response_auth0.status_code, 200) self.assertIn("Log in to foxylib", response_auth0.text) self.assertIn("Log in to dev-8gnjw0rn to continue to foxylib", response_auth0.text)
def func_list2buffered_result_iter(cls, func_list, buffer_size): if len(func_list) == 1: f = l_singleton2obj(func_list) yield f() else: result_iter = cls.func_iter2buffered_result_iter(func_list, buffer_size) yield from result_iter
def test_01(self): config = {HenriqueEntity.Config.Field.LOCALE: "ko"} entity_list = RelativeTimedeltaEntity.text2entity_list("+3일", config=config) ref = [{ 'span': (0, 3), 'text': '+3일', 'type': 'henrique.main.tool.entity.datetime.timedelta.timedelta_entity.RelativeTimedeltaEntity', 'value': { 'sign': '+', 'timedelta': { 'span': (1, 3), 'text': '3일', 'type': 'henrique.main.tool.entity.datetime.timedelta.timedelta_entity.TimedeltaEntity', 'value': [{ 'quantity': 3, 'span': (1, 3), 'unit': 'day' }] } } }] # pprint(hyp) self.assertEqual(entity_list, ref) self.assertEqual( RelativeTimedeltaEntity.entity2relativedelta( l_singleton2obj(entity_list)), relativedelta.relativedelta(days=3), )
def test_01(self): price_list = MarkettrendDoc.ports_tradegoods2price_list_latest( ["Lisbon"], ["Nutmeg"]) price = l_singleton2obj(price_list) self.assertEqual(MarketpriceDoc.price2port(price), "Lisbon") self.assertEqual(MarketpriceDoc.price2tradegood(price), "Nutmeg") self.assertEqual(MarketpriceDoc.Field.set(), set(price.keys()))
def down2uniq(cls, root, tags): node = root for tag in tags: children = node.findall(tag) if not children: return None node = l_singleton2obj(children) return node
def codename2chatroom(cls, codename): logger = KhalaLogger.func_level2logger(cls.codename2chatroom, logging.DEBUG) # logger.debug({"codename": codename}) chatrooms = cls.codenames2chatrooms([codename]) logger.debug({"chatrooms": chatrooms}) return l_singleton2obj(chatrooms)
def test_04(self): config = {HenriqueEntity.Config.Field.LOCALE: "ko"} entity_list = RelativeTimedeltaEntity.text2entity_list("-10 mins", config=config) # pprint(entity_list) hyp = RelativeTimedeltaEntity.entity2relativedelta( l_singleton2obj(entity_list)) ref = -1 * relativedelta.relativedelta(minutes=10) self.assertEqual(hyp, ref)
def test_02(self): config = {HenriqueEntity.Config.Field.LOCALE: "ko"} entity_list = RelativeTimedeltaEntity.text2entity_list("+20 초", config=config) # pprint(entity_list) hyp = RelativeTimedeltaEntity.entity2relativedelta( l_singleton2obj(entity_list)) ref = relativedelta.relativedelta(seconds=20) self.assertEqual(hyp, ref)
def j2plaintext(cls, j_event): # https://api.slack.com/changelog/2019-09-what-they-see-is-what-you-get-and-more-and-less j_rich_text_list = j_event.get("blocks") if not j_rich_text_list: return None j_rich_text = l_singleton2obj(j_rich_text_list) j_rich_text_section_list = j_rich_text.get("elements") if not j_rich_text_section_list: return None j_rich_text_section = l_singleton2obj(j_rich_text_section_list) j_element_list = j_rich_text_section.get("elements") if not j_element_list: return None text_list = lmap(lambda j:j["text"], filter(lambda j:j["type"]=="text", j_element_list)) return "".join(text_list)
def immutablemultidict2h(cls, imd, key_list_singleton=None): h_k2l = imd.to_dict(flat=False) if not key_list_singleton: return h_k2l h_k2v = { k: l if k not in key_list_singleton else l_singleton2obj(l) for k, l in h_k2l.items() } return h_k2v
def on_file_shared(**kwargs): j_event = kwargs.get("data") j_file_list = FileSharedEvent.j_event2j_file_list(j_event) self.assertEqual(len(j_file_list), 1) j_file = l_singleton2obj(j_file_list) filename = SlackFile.j_file2filename(j_file) self.assertEqual(filename, "test_01.txt") raise Exception()
def ids2query(cls, id_iterable): id_list = list(id_iterable) if not id_list: return {} objectid_list = lmap(cls.id2ObjectId, id_list) if len(id_list) == 1: oid = l_singleton2obj(objectid_list) return {cls.Field._ID: oid} query = {cls.Field._ID: {"$in": objectid_list}} return query
def j_page2text(cls, j_page): logger = FoxylibLogger.func_level2logger(cls.j_page2text, logging.DEBUG) j_responspan_list = jdown(j_page, ["responses"]) if j_responspan_list is None: return None assert_true(j_responspan_list, j_page) j_response = l_singleton2obj(j_responspan_list) str_text = jdown(j_response, ["fullTextAnnotation", "text"]) return str_text
def clique2doc_insert(cls, packet, clique): ports = cls.clique2ports(clique) tradegoods = cls.clique2tradegoods(clique) rate = cls.clique2rate(clique) trend = cls.clique2trend(clique) port_codename = l_singleton2obj(ports) tradegood_codename = l_singleton2obj(tradegoods) server_codename = HenriquePacket.packet2server(packet) channel_user = KhalaPacket.packet2channel_user(packet) doc = { MarketpriceDoc.Field.CREATED_AT: datetime.now(pytz.utc), MarketpriceDoc.Field.PORT: port_codename, MarketpriceDoc.Field.TRADEGOOD: tradegood_codename, MarketpriceDoc.Field.RATE: rate, MarketpriceDoc.Field.TREND: trend, MarketpriceDoc.Field.SERVER: server_codename, MarketpriceDoc.Field.CHANNEL_USER: channel_user, } return doc
def parse_str2reldelta(cls, s): logger = FoxylibLogger.func2logger(cls.parse_str2reldelta) p = cls.pattern_timedelta() m_list = list(p.finditer(s)) if not m_list: return None m = l_singleton2obj(m_list) int_sign = IntToolkit.parse_sign2int(m.group("sign")) kv_list = [(k, int_sign*int(m.group(k))) for k in cls.reldelta_name_list() if m.group(k)] logger.debug({"kv_list":kv_list}) reldelta = relativedelta.relativedelta(**dict(kv_list)) return reldelta
def test_03(self): config = {HenriqueEntity.Config.Field.LOCALE: "ko"} entity_list = RelativeTimedeltaEntity.text2entity_list("-1개월 6일", config=config) # pprint(entity_list) ref = [{ 'span': (0, 7), 'text': '-1개월 6일', 'type': 'henrique.main.tool.entity.datetime.timedelta.timedelta_entity.RelativeTimedeltaEntity', 'value': { 'sign': '-', 'timedelta': { 'span': (1, 7), 'text': '1개월 6일', 'type': 'henrique.main.tool.entity.datetime.timedelta.timedelta_entity.TimedeltaEntity', 'value': [{ 'quantity': 1, 'span': (1, 4), 'unit': 'month' }, { 'quantity': 6, 'span': (5, 7), 'unit': 'day' }] } } }] self.assertEqual(entity_list, ref) reldelta = RelativeTimedeltaEntity.entity2relativedelta( l_singleton2obj(entity_list)) reldelta_ref = -1 * relativedelta.relativedelta(months=1, days=6) self.assertEqual(reldelta, reldelta_ref)
def _text_config2skill_code(cls, text_in, config): pattern_prefix = cls.pattern_prefix() match_list_prefix = list(pattern_prefix.finditer(text_in)) if not match_list_prefix: return None entity_list = SkillEntity.text2entity_list(text_in, config=config) if not entity_list: return None spans_list = [lmap(lambda m: m.span(), match_list_prefix), lmap(FoxylibEntity.entity2span, entity_list) ] gap2is_valid = partial(StringTool.str_span2match_blank_or_nullstr, text_in) indextuple_list = ContextfreeTool.spans_list2reducible_indextuple_list(spans_list, gap2is_valid) assert_in(len(indextuple_list), [0, 1]) if not indextuple_list: return None index_entity = l_singleton2obj(indextuple_list)[1] entity = entity_list[index_entity] return SkillEntity.entity2skill_codename(entity)
def match2str_group(cls, m): l = cls.match2str_group_list(m) return l_singleton2obj(l)
def p_str2m_uniq(cls, pattern, s): m_list = list(pattern.finditer(s)) if not m_list: return None m = l_singleton2obj(m_list) return m
def packet2response(cls, packet): logger = HenriqueLogger.func_level2logger(cls.packet2response, logging.DEBUG) logger.debug({"packet": packet}) server_codename = HenriquePacket.packet2server(packet) chatroom = Chatroom.codename2chatroom( KhalaPacket.packet2chatroom(packet)) locale = Chatroom.chatroom2locale(chatroom) or HenriqueLocale.DEFAULT lang = LocaleTool.locale2lang(locale) tz = pytz.timezone(HenriqueLocale.lang2tzdb(lang)) dt_now = datetime.now(tz) text_in = KhalaPacket.packet2text(packet) config = {HenriqueEntity.Config.Field.LOCALE: locale} # entity_list = RelativeTimedeltaEntity.text2entity_list(text_in, config=config) entity_list = HenriqueEntity.text_extractors2entity_list( text_in, cls.config2extractors(config), ) logger.debug({ "len(entity_list)": len(entity_list), "entity_list": entity_list, }) if not entity_list: return cls.server_lang2lookup(server_codename, lang) if len(entity_list) != 1: return # Invalid request entity = l_singleton2obj(entity_list) if FoxylibEntity.entity2type( entity) == RelativeTimedeltaEntity.entity_type(): reldelta = RelativeTimedeltaEntity.entity2relativedelta(entity) dt_in = cls.relativedelta2nanban_datetime( server_codename, reldelta, ) # raise Exception({"dt_in":dt_in, "reldelta":reldelta}) if dt_in is None: msg_error = NanbanSkillError.codename_lang2text( NanbanSkillError.Codename.NO_PREV_NANBAN_ERROR, lang) raise HenriqueCommandError(msg_error) logger.debug({ "reldelta": reldelta, }) elif FoxylibEntity.entity2type(entity) == TimeEntity.entity_type(): time_in = TimeEntity.value2datetime_time( FoxylibEntity.entity2value(entity)) dt_in = PytzTool.localize(datetime.combine(dt_now.date(), time_in), tz) logger.debug({ "time_in": time_in, "dt_in": dt_in, }) else: raise RuntimeError({ "Invalid entity type: {}".format( FoxylibEntity.entity2type(entity)) }) dt_nearest = DatetimeTool.datetime2nearest(dt_in, dt_now, NanbanTimedelta.period(), Nearest.COMING) logger.debug({ "text_in": text_in, "dt_now": dt_now, "dt_in": dt_in, "dt_nearest": dt_nearest, }) cls.nanban_datetime2upsert_mongo(packet, dt_nearest) return cls.server_lang2lookup(server_codename, lang)
def codename2doc(cls, codename): return l_singleton2obj(cls.codenames2docs([codename]))
def immutablemultidict_key2v_singleton(cls, imd, key): l = cls.immutablemultidict_key2list(imd, key) if not l: return None return l_singleton2obj(l)
def variable2name(cls, var): callers_local_vars = inspect.currentframe().f_back.f_locals.items() l = [var_name for var_name, var_val in callers_local_vars if var_val is var] return l_singleton2obj(l)