互动型网站模板,天水网站开发,网站建设找天宇智能,ios应用程序开发回眸那年#xff0c;懵懂的我以为学会了if判断#xff0c;就能轻易判断世间所有逻辑进行处理。时至今日#xff0c;我依然为我当年的想法所骄傲#x1f602;。工作中多重if...elif...经常会遇到#xff0c;我也像如下的方式写了N1次#xff0c;内容如下#xff1a;
use…回眸那年懵懂的我以为学会了if判断就能轻易判断世间所有逻辑进行处理。时至今日我依然为我当年的想法所骄傲。工作中多重if...elif...经常会遇到我也像如下的方式写了N1次内容如下
use_standing diamond_userif use_standing normal_user: # 普通用户discount 0.95# 大量的逻辑代码print(normal_user)
elif use_standing gold_user: # 黄金用户discount 0.9# 大量的逻辑代码print(gold_user)
elif use_standing platinum_user: # 铂金用户discount 0.85# 大量的逻辑代码print(platinum_user)
elif use_standing diamond_user: # 钻石用户discount 0.8# 大量的逻辑代码print(diamond_user)
elif use_standing starlight_user: # 星耀用户discount 0.7# 大量的逻辑代码print(starlight_user)
elif use_standing superior_user: # 至尊用户discount 0.6# 大量的逻辑代码print(superior_user)
else:print(The user type does not exist。)
通过判断不同的用户类型进而针对不同的用户要进行大量的业务逻辑处理这样的代码经常会有几百行乃至更多。慢慢的我开始厌烦这种代码哪怕之前是我自己写的过段时间再修改里面的业务逻辑我都很难受更何况看到别人写这种心里就只有MMP。
于是以后再遇到多重判断逻辑必须优化。
方式1使用字典的方式。
# 普通用户
def normal_user(type):大量的逻辑代码return type# 黄金用户
def gold_user(type):大量的逻辑代码return type# 铂金用户
def platinum_user(type):大量的逻辑代码return type# 钻石用户
def diamond_user(type):大量的逻辑代码return type# 星耀用户
def starlight_user(type):大量的逻辑代码return type# 至尊用户
def superior_user(type):大量的逻辑代码return typedef inexistence_user(type):return fThe user type {type} does not exist。user_dict {normal_user: normal_user,gold_user: gold_user,platinum_user: platinum_user,diamond_user: diamond_user,starlight_user: starlight_user,superior_user: superior_user,
}user_type platinum_user
print(user_dict.get(user_type, inexistence_user)(user_type))
通过将不同的用户类型封装到不同的函数中进而使用字典键值对获取调用。
方式2使用EdgeDB中的轮子
代码地址https://github.com/edgedb/edgedb/blob/master/edb/common/value_dispatch.py
我们直接拿来使用即可示例如下
源码文件名为value_dispatch.py直接放在同目录或指定目录下调用就源码如下
#
# This source file is part of the EdgeDB open source project.
#
# Copyright 2021-present MagicStack Inc. and the EdgeDB authors.
#
# Licensed under the Apache License, Version 2.0 (the License);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an AS IS BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#from __future__ import annotations
from typing import *import functools
import inspect
import types_T TypeVar(_T)class _ValueDispatchCallable(Generic[_T], Protocol):registry: types.MappingProxyType[Any, Callable[..., _T]]def register(self,val: Any,) - Callable[[Callable[..., _T]], Callable[..., _T]]:...def register_for_all(self,val: Iterable[Any],) - Callable[[Callable[..., _T]], Callable[..., _T]]:...def __call__(__self, *args: Any, **kwargs: Any) - _T:...def value_dispatch(func: Callable[..., _T]) - _ValueDispatchCallable[_T]:Like singledispatch() but dispatches by value of the first arg.Example:value_dispatchdef eat(fruit):return fI dont want a {fruit}...eat.register(apple)def _eat_apple(fruit):return I love apples!eat.register(eggplant)eat.register(squash)def _eat_what(fruit):return fI didnt know {fruit} is a fruit!An alternative to applying multuple register decorators is touse the register_for_all helper:eat.register_for_all({eggplant, squash})def _eat_what(fruit):return fI didnt know {fruit} is a fruit!registry: dict[Any, Callable[..., _T]] {}functools.wraps(func)def wrapper(arg0: Any, *args: Any, **kwargs: Any) - _T:try:delegate registry[arg0]except KeyError:passelse:return delegate(arg0, *args, **kwargs)return func(arg0, *args, **kwargs)def register(value: Any,) - Callable[[Callable[..., _T]], Callable[..., _T]]:if inspect.isfunction(value):raise TypeError(value_dispatch.register() decorator requires a value)def wrap(func: Callable[..., _T]) - Callable[..., _T]:if value in registry:raise ValueError(fvalue_dispatch: there is already a handler fregistered for {value!r})registry[value] funcreturn funcreturn wrapdef register_for_all(values: Iterable[Any],) - Callable[[Callable[..., _T]], Callable[..., _T]]:def wrap(func: Callable[..., _T]) - Callable[..., _T]:for value in values:if value in registry:raise ValueError(fvalue_dispatch: there is already a handler fregistered for {value!r})registry[value] funcreturn funcreturn wrapwrapper.register registerwrapper.register_for_all register_for_allreturn wrapper使用如下
from value_dispatch import value_dispatchvalue_dispatch
def get_user_type(type):处理不存在的情况return f等级 {type} 不存在# 普通用户
get_user_type.register(normal_user)
def normal_user(type):大量的逻辑代码return type# 黄金用户
get_user_type.register(gold_user)
def gold_user(type):大量的逻辑代码return type# 铂金用户
get_user_type.register(platinum_user)
def platinum_user(type):大量的逻辑代码return type# 钻石用户
get_user_type.register(diamond_user)
def diamond_user(type):大量的逻辑代码return type# 星耀用户
get_user_type.register(starlight_user)
def starlight_user(type):大量的逻辑代码return type# 至尊用户
get_user_type.register(superior_user)
def superior_user(type):大量的逻辑代码return typeif __name__ __main__:print(get_user_type(diamond_user))
至此是不是觉得自己的编码逻辑又进了一步拜拜