Exemplo n.º 1
0
        def codename2server(codename):
            aliases = DictTool.filter(
                lambda k, v: v, {
                    "en": h_codename2aliases_en.get(codename),
                    "ko": h_codename2aliases_ko.get(codename),
                })

            culture = {
                Server.Field.CODENAME: codename,
                Server.Field.ALIASES: aliases,
            }
            return DictTool.filter(lambda k, v: v, culture)
Exemplo n.º 2
0
        def codename2culture(codename):
            aliases = DictTool.filter(
                lambda k, v: v, {
                    "en": h_codename2aliases_en.get(codename),
                    "ko": h_codename2aliases_ko.get(codename),
                })

            culture = {
                Culture.Field.CODENAME: codename,
                Culture.Field.ALIASES: aliases,
                Culture.Field.PREFERS: h_codename2prefers.get(codename) or [],
            }
            return DictTool.filter(lambda k, v: v, culture)
Exemplo n.º 3
0
        def codename2port(codename):
            aliases = DictTool.filter(lambda k, v: v,
                                      {"en": h_codename2aliases_en.get(codename),
                                       "ko": h_codename2aliases_ko.get(codename),
                                       })
            tradegoodtype = h_codename2tradegoodtype.get(codename)

            port = DictTool.filter(lambda k, v: bool(v),
                                   {Tradegood.Field.CODENAME: codename,
                                    Tradegood.Field.TRADEGOODTYPE: tradegoodtype,
                                    Tradegood.Field.ALIASES: aliases,
                                    }
                                   )
            return DictTool.filter(lambda k, v: v, port)
Exemplo n.º 4
0
        def codename2port(codename):
            aliases = DictTool.filter(
                lambda k, v: v, {
                    "en": h_codename2aliases_en.get(codename),
                    "ko": h_codename2aliases_ko.get(codename),
                })
            category = h_codename2category.get(codename)

            port = DictTool.filter(
                lambda k, v: bool(v), {
                    Tradegoodtype.Field.CODENAME: codename,
                    Tradegoodtype.Field.CATEGORY: category,
                    Tradegoodtype.Field.ALIASES: aliases,
                })
            return DictTool.filter(lambda k, v: v, port)
Exemplo n.º 5
0
    def server2datetime_nanban(
        cls,
        server_codename,
        dt_pivot,
    ):
        server_doc = ServerDoc.codename2doc(server_codename)
        if not server_doc:
            return None

        nanban_prev = ServerDoc.doc2nanban(server_doc)
        nanban_datetime_prev = ServerNanban.nanban2datetime(nanban_prev)
        # dt_nanban_raw
        # utc_now = datetime.now(tz=pytz.utc)

        nanban_datetime_this = DatetimeTool.from_pivot_period2next(
            nanban_datetime_prev, dt_pivot, NanbanTimedelta.period())

        if nanban_datetime_this != nanban_datetime_prev:
            nanban = DictTool.filter(
                lambda k, v: v is not None, {
                    ServerNanban.Field.DATETIME:
                    nanban_datetime_this,
                    ServerNanban.Field.COMMAND_IN:
                    ServerNanban.nanban2command_in(nanban_prev),
                })
            doc = {
                ServerDoc.Field.CODENAME: server_codename,
                ServerDoc.Field.NANBAN: nanban,
            }
            ServerDoc.docs2upsert([doc])

        return nanban_datetime_this
Exemplo n.º 6
0
    def index_init_unittest(cls, settings=None, mappings=None):
        logger = FoxylibLogger.func_level2logger(cls.index_init_unittest, logging.DEBUG)

        client = FoxylibElasticsearch.client()
        index = UnittestIndex.env2name(FoxylibEnv.env())
        logger.debug({"index_unittest": index})

        # ElasticsearchTool.delete_or_skip(client, index_unittest)

        # settings = UnittestIndex.settings()
        # mappings = UnittestIndex.mappings()

        body = DictTool.filter(lambda k, v: bool(v),
                               {"settings": settings if settings else None,
                                "mappings": mappings if mappings else None,
                                }
                               )
        ElasticsearchTool.create_or_skip(client, index, body=body)

        query_all = {"match_all": {}}
        client.delete_by_query(index, {"query": query_all}, refresh=True)

        result = client.search(index, body={"query": query_all, })
        assert_false(ElasticsearchTool.j_result2j_hit_list(result))

        return client, index
Exemplo n.º 7
0
        def codename2port(codename):
            aliases = DictTool.filter(lambda k, v: v,
                                      {"en": h_codename2aliases_en.get(codename),
                                       "ko": h_codename2aliases_ko.get(codename),
                                       })

            comments = DictTool.filter(lambda k, v: v,
                                      {"ko": h_codename2comments_ko.get(codename),
                                       })

            port = {Port.Field.CODENAME: codename,
                    Port.Field.CULTURE: h_codename2culture[codename],
                    Port.Field.ALIASES: aliases,
                    Port.Field.PRODUCTS: h_codename2product_list.get(codename),
                    Port.Field.COMMENTS: comments,
                    }
            return DictTool.filter(lambda k, v: v, port)
Exemplo n.º 8
0
 def row2product(row):
     port, tradegood = row[0], row[1]
     price = row[2] if len(row) >= 3 else None
     raw = {Product.Field.PORT: port,
            Product.Field.TRADEGOOD: tradegood,
            Product.Field.PRICE: price,
            }
     product = DictTool.filter(lambda k,v:v, raw)
     return product
Exemplo n.º 9
0
        def codename2chatroomuser(codename):
            aliases = h_codename2aliases.get(codename) or []
            comments = h_codename2comments.get(codename)

            chatroomuser = {
                Chatroomuser.Field.CODENAME: codename,
                Chatroomuser.Field.COMMENTS: comments,
                Chatroomuser.Field.ALIASES: aliases,
            }
            return DictTool.filter(lambda k, v: v, chatroomuser)
Exemplo n.º 10
0
    def attach_cachedmethod(
        cls,
        func=None,
        cachedmethod=None,
        self2cache=None,
        key=None,
        lock=None,
    ):
        logger = FoxylibLogger.func_level2logger(cls.attach_cachedmethod,
                                                 logging.DEBUG)

        assert_is_not_none(self2cache)

        cachedmethod = cachedmethod or cachetools.cachedmethod

        config = DictTool.filter(
            lambda k, v: v is not None, {
                cls.Config.Field.SELF2CACHE: self2cache,
                cls.Config.Field.KEY: key or hashkey,
                cls.Config.Field.LOCK: lock,
            })
        kwargs = cls.Config.config2kwargs(config)

        def wrapper(f):
            # types_valid = {CallableTool.Type.INSTANCEMETHOD,CallableTool.Type.CLASSMETHOD_BEFORE_DECORATOR,}
            # assert_in(CallableTool.callable2type(f), types_valid,
            #           "For functions, use attach_cached() instead")

            assert_false(hasattr(f, cls.Constant.ATTRIBUTE_NAME))
            setattr(f, cls.Constant.ATTRIBUTE_NAME, config)

            _self2cache = cls.Config.config2self2cache(config)

            @wraps(f)
            def wrapped(self, *_, **__):
                # config = cls.callable2config(f)
                # _self2cache = cls.Config.config2cache(config)

                # f.__self__ doesn't work here because classmethod() is not called yet
                hideout = cls.Hideout.method2hideout(self, f)
                cache = cls.Hideout.get_or_lazyinit_cache(
                    hideout, lambda: _self2cache(self))
                # logger.debug({"f":f,"cache":cache})

                f_with_cache = cachedmethod(lambda x: cache, **kwargs)(f)
                result = f_with_cache(self, *_, **__)
                # raise Exception({"self":self,"result":result,"cache":cache})
                return result

            return wrapped

        return wrapper(func) if func else wrapper
Exemplo n.º 11
0
    def sheet_ranges2data_lll(cls, credentials, spreadsheet_id, ranges=None):
        logger = FoxylibLogger.func_level2logger(cls.sheet_ranges2data_lll,
                                                 logging.DEBUG)
        logger.debug({"spreadsheet_id": spreadsheet_id, "ranges": ranges})

        # service = build('sheets', 'v4', http=credentials.authorize(Http()))
        service = build('sheets', 'v4', credentials=credentials)

        h = DictTool.filter(lambda k, v: v, {
            "spreadsheetId": spreadsheet_id,
            "ranges": ranges,
        })
        result = service.spreadsheets().values().batchGet(**h).execute()
        # pprint({"result":result})
        values = lmap(lambda h: h.get("values") or [],
                      result.get('valueRanges', []))

        logger.debug({"len(values)": len(values)})

        return values
Exemplo n.º 12
0
    def attach_cached(
        cls,
        func=None,
        cached=None,
        cache=None,
        key=None,
        lock=None,
    ):
        logger = FoxylibLogger.func_level2logger(cls.attach_cached,
                                                 logging.DEBUG)

        assert_is_not_none(cache)
        cached = cached or cachetools.cached

        config = DictTool.filter(
            lambda k, v: v is not None, {
                cls.Config.Field.CACHE: cache,
                cls.Config.Field.KEY: key or hashkey,
                cls.Config.Field.LOCK: lock,
            })
        kwargs = cls.Config.config2kwargs(config)

        def wrapper(f):
            """
            CLASSMETHOD can used cached() too.
            But for simplicity of the system, CLASSMETHOD is forced to use cachedmethod
            """
            # types_valid = {CallableTool.Type.FUNCTION, }
            # assert_in(CallableTool.callable2type(f), types_valid,
            #           "For instancemethod, use attach_cachedmethod() instead")

            assert_false(hasattr(f, cls.Constant.ATTRIBUTE_NAME))
            setattr(f, cls.Constant.ATTRIBUTE_NAME, config)

            f_cached = cached(cache, **kwargs)(f)
            return f_cached

        return wrapper(func) if func else wrapper
Exemplo n.º 13
0
        def i2entity(i):
            entity = entity_list_in[i]
            assert_equal(FoxylibEntity.entity2type(entity),
                         TimeEntity.entity_type())

            if i not in h_i2j:
                return entity_list_in[i]

            j = h_i2j[i]

            m_ampm = m_list_ampm[j]
            span = (span_list_in[i][0], span_list_ampm[j][1])

            v_entity = FoxylibEntity.entity2value(entity)
            hour, minute, second = TimeEntity.Value.value2hms(v_entity)
            ampm = AMPM.match2value(m_ampm)
            hour_adjusted, ampm_adjusted = AMPM.hour_ampm2normalized(
                hour, ampm)

            # logger.debug({"hour":hour, "ampm":ampm,
            #               "hour_adjusted":hour_adjusted, "ampm_adjusted":ampm_adjusted})

            value = DictTool.filter(
                lambda k, v: v is not None, {
                    TimeEntity.Value.Field.HOUR: hour_adjusted,
                    TimeEntity.Value.Field.MINUTE: minute,
                    TimeEntity.Value.Field.SECOND: second,
                    TimeEntity.Value.Field.AMPM: ampm_adjusted,
                })

            entity = {
                FoxylibEntity.Field.TYPE: FoxylibEntity.entity2type(entity),
                FoxylibEntity.Field.FULLTEXT: text_in,
                FoxylibEntity.Field.SPAN: span,
                FoxylibEntity.Field.VALUE: value,
            }
            return entity
Exemplo n.º 14
0
 def dict2none_excluded(cls, h):
     return DictTool.filter(lambda k, v: v is not None, h)
Exemplo n.º 15
0
    def form2j_form(cls, form):
        if not form:
            return form

        return DictTool.filter(lambda k, v: v, form.patch_data)
Exemplo n.º 16
0
    def dict_codename2comments(cls):
        data_ll = PortGooglesheets.sheetname2data_ll(cls.NAME)

        h = merge_dicts([{row[0]: lfilter(bool,row[1:])} for row in data_ll[1:]],
                        vwrite=vwrite_no_duplicate_key)
        return DictTool.filter(lambda k,v:bool(v), h)
Exemplo n.º 17
0
 def data2dict_noempty(cls, data):
     return DictTool.filter(lambda k, v: not cls.value2is_empty(v),
                            asdict(data))
Exemplo n.º 18
0
 def dict2empty_excluded(cls, h):
     return DictTool.filter(lambda k, v: not cls.value2is_empty(v), h)