说说 Python çå ç¼ç¨
æå°å è¿ä¸ªåï¼ä½ ä¹è®¸ä¼æ³å°å æ°æ®ï¼å æ°æ®å°±æ¯æè¿°æ°æ®æ¬èº«çæ°æ®ï¼å 类就æ¯ç±»çç±»ï¼ç¸åºçå ç¼ç¨å°±æ¯æ述代ç æ¬èº«ç代ç ï¼å ç¼ç¨å°±æ¯å ³äºå建æä½æºä»£ç (æ¯å¦ä¿®æ¹ãçææå è£ åæ¥ç代ç )çå½æ°åç±»ã主è¦ææ¯æ¯ä½¿ç¨è£ 饰å¨ãå ç±»ãæ述符类ãæ¬æç主è¦ç®çæ¯å大家ä»ç»è¿äºå ç¼ç¨ææ¯ï¼å¹¶ä¸ç»åºå®ä¾æ¥æ¼ç¤ºå®ä»¬æ¯ææ ·å®å¶åæºä»£ç çè¡ä¸ºã
è£ é¥°å¨ è£ é¥°å¨å°±æ¯å½æ°çå½æ°ï¼å®æ¥åä¸ä¸ªå½æ°ä½ä¸ºåæ°å¹¶è¿åä¸ä¸ªæ°çå½æ°ï¼å¨ä¸æ¹ååæ¥å½æ°ä»£ç çæ åµä¸ä¸ºå ¶å¢å æ°çåè½ï¼æ¯å¦æ常ç¨ç计æ¶è£ 饰å¨ï¼
from functools import wrapsdef timeit(logger=None):"""èæ¶ç»è®¡è£ 饰å¨ï¼åä½æ¯ç§ï¼ä¿ç 4 ä½å°æ°"""def decorator(func):@wraps(func)def wrapper(*args, **kwargs):start = time.time()result = func(*args, **kwargs)end = time.time()if logger:logger.info(f"{ func.__name__} cost { end - start :.4f} seconds")else:print(f"{ func.__name__} cost { end - start :.4f} seconds")return resultreturn wrapperreturn decorator(注ï¼æ¯å¦ä¸é¢ä½¿ç¨ @wraps(func) 注解æ¯å¾éè¦çï¼ å®è½ä¿çåå§å½æ°çå æ°æ®) åªéè¦å¨åæ¥çå½æ°ä¸é¢å ä¸ @timeit() å³å¯ä¸ºå ¶å¢å æ°çåè½ï¼
@timeit()def test_timeit():time.sleep(1)test_timeit()#test_timeit cost 1. secondsä¸é¢ç代ç è·ä¸é¢è¿æ ·åçæææ¯ä¸æ ·çï¼
test_timeit = timeit(test_timeit)test_timeit()è£ é¥°å¨çæ§è¡é¡ºåº å½æå¤ä¸ªè£ 饰å¨çæ¶åï¼ä»ä»¬çè°ç¨é¡ºåºæ¯æä¹æ ·çï¼
åå¦æè¿æ ·ç代ç ï¼è¯·é®æ¯å æå° Decorator1 è¿æ¯ Decorator2 ?
from functools import wrapsdef decorator1(func):@wraps(func)def wrapper(*args, **kwargs):print('Decorator 1')return func(*args, **kwargs)return wrapperdef decorator2(func):@wraps(func)def wrapper(*args, **kwargs):print('Decorator 2')return func(*args, **kwargs)return wrapper@decorator1@decorator2def add(x, y):return x + yadd(1,2)# Decorator 1# Decorator 2åçè¿ä¸ªé®é¢ä¹åï¼æå ç»ä½ æ个形象çæ¯å»ï¼è£ 饰å¨å°±åå½æ°å¨ç©¿è¡£æï¼ç¦»å®æè¿çæå ç©¿ï¼ç¦»å¾è¿çæåç©¿ï¼ä¸ä¾ä¸ decorator1 æ¯å¤å¥ï¼decorator2 æ¯å è¡£ã
add = decorator1(decorator2(add))
å¨è°ç¨å½æ°çæ¶åï¼å°±åè±è¡£æï¼å 解é¤æå¤é¢ç decorator1ï¼ä¹å°±æ¯å æå° Decorator1ï¼æ§è¡å° return func(
args, kwargs) çæ¶åä¼å»è§£é¤ decorator2ï¼ç¶åæå° Decorator2ï¼å次æ§è¡å° return func(
args, kwargs) æ¶ä¼çæ£æ§è¡ add() å½æ°ã
éè¦æ³¨æçæ¯æå°çä½ç½®ï¼å¦ææå°å符串ç代ç ä½äºè°ç¨å½æ°ä¹åï¼åä¸é¢è¿æ ·ï¼é£è¾åºçç»ææ£å¥½ç¸åï¼
def decorator1(func):@wraps(func)def wrapper(*args, **kwargs):result = func(*args, **kwargs)print('Decorator 1')return resultreturn wrapperdef decorator2(func):@wraps(func)def wrapper(*args, **kwargs):result = func(*args, **kwargs)print('Decorator 2')return resultreturn wrapperè£ é¥°å¨ä¸ä» å¯ä»¥å®ä¹ä¸ºå½æ°ï¼ä¹å¯ä»¥å®ä¹ä¸ºç±»ï¼åªè¦ä½ ç¡®ä¿å®å®ç°äº__call__() å __get__() æ¹æ³ã
å ç±» Python ä¸ææç±»ï¼objectï¼çå ç±»ï¼å°±æ¯ type ç±»ï¼ä¹å°±æ¯è¯´ Python ç±»çå建è¡ä¸ºç±é»è®¤ç type ç±»æ§å¶ï¼æ个æ¯å»ï¼type ç±»æ¯ææç±»çç¥å ãæ们å¯ä»¥éè¿ç¼ç¨çæ¹å¼æ¥å®ç°èªå®ä¹çä¸äºå¯¹è±¡å建è¡ä¸ºã
å®ä¸ä¸ªç±»ç»§æ¿ type ç±» Aï¼ç¶åè®©å ¶ä»ç±»çå ç±»æå Aï¼å°±å¯ä»¥æ§å¶ A çå建è¡ä¸ºãå ¸åçå°±æ¯ä½¿ç¨å ç±»å®ç°ä¸ä¸ªåä¾ï¼
class Singleton(type):def __init__(self, *args, **kwargs):self._instance = Nonesuper().__init__(*args, **kwargs)def __call__(self, *args, **kwargs):if self._instance is None:self._instance = super().__call__(*args, **kwargs)return self._instanceelse:return self._instanceclass Spam(metaclass=Singleton):def __init__(self):print("Spam!!!")å ç±» Singleton ç__init__å__new__ æ¹æ³ä¼å¨å®ä¹ Spam çæé´è¢«æ§è¡ï¼è __call__æ¹æ³ä¼å¨å®ä¾å Spam çæ¶åæ§è¡ã
descriptor ç±»ï¼æ述符类ï¼
descriptor å°±æ¯ä»»ä½ä¸ä¸ªå®ä¹äº __get__()ï¼__set__()æ __delete__()ç对象ï¼æè¿°å¨è®©å¯¹è±¡è½å¤èªå®ä¹å±æ§æ¥æ¾ãåå¨åå é¤çæä½ãè¿é举å®æ¹ææ¡£[1]ä¸ä¸ªèªå®ä¹éªè¯å¨çä¾åã
å®ä¹éªè¯å¨ç±»ï¼å®æ¯ä¸ä¸ªæ述符类ï¼åæ¶è¿æ¯ä¸ä¸ªæ½è±¡ç±»ï¼
from abc import ABC, abstractmethodclass Validator(ABC):def __set_name__(self, owner, name):self.private_name = '_' + namedef __get__(self, obj, objtype=None):return getattr(obj, self.private_name)def __set__(self, obj, value):self.validate(value)setattr(obj, self.private_name, value)@abstractmethoddef validate(self, value):passèªå®ä¹éªè¯å¨éè¦ä» Validator 继æ¿ï¼å¹¶ä¸å¿ é¡»æä¾ validate() æ¹æ³ä»¥æ ¹æ®éè¦æµè¯åç§çº¦æã
è¿æ¯ä¸ä¸ªå®ç¨çæ°æ®éªè¯å·¥å ·ï¼
OneOf éªè¯å¼æ¯ä¸ç»å约æçé项ä¹ä¸ã
class OneOf(Validator):def __init__(self, *options):self.options = set(options)def validate(self, value):if value not in self.options:raise ValueError(f'Expected { value!r} to be one of { self.options!r}')Number éªè¯å¼æ¯å¦ä¸º int æ floatãæ ¹æ®å¯éåæ°ï¼å®è¿å¯ä»¥éªè¯å¼å¨ç»å®çæå°å¼ææ大å¼ä¹é´ã
class Number(Validator):def __init__(self, minvalue=None, maxvalue=None):self.minvalue = minvalueself.maxvalue = maxvaluedef validate(self, value):if not isinstance(value, (int, float)):raise TypeError(f'Expected { value!r} to be an int or float')if self.minvalue is not None and value < self.minvalue:raise ValueError(f'Expected { value!r} to be at least { self.minvalue!r}')if self.maxvalue is not None and value > self.maxvalue:raise ValueError(f'Expected { value!r} to be no more than { self.maxvalue!r}')String éªè¯å¼æ¯å¦ä¸º strãæ ¹æ®å¯éåæ°ï¼å®å¯ä»¥éªè¯ç»å®çæå°ææ大é¿åº¦ãå®è¿å¯ä»¥éªè¯ç¨æ·å®ä¹ç predicateã
class String(Validator):def __init__(self, minsize=None, maxsize=None, predicate=None):self.minsize = minsizeself.maxsize = maxsizeself.predicate = predicatedef validate(self, value):if not isinstance(value, str):raise TypeError(f'Expected { value!r} to be an str')if self.minsize is not None and len(value) < self.minsize:raise ValueError(f'Expected { value!r} to be no smaller than { self.minsize!r}')if self.maxsize is not None and len(value) > self.maxsize:raise ValueError(f'Expected { value!r} to be no bigger than { self.maxsize!r}')if self.predicate is not None and not self.predicate(value):raise ValueError(f'Expected { self.predicate} to be true for { value!r}')å®é åºç¨æ¶è¿æ ·åï¼
@timeit()def test_timeit():time.sleep(1)test_timeit()#test_timeit cost 1. seconds0æè¿°å¨é»æ¢æ æå®ä¾çå建ï¼
@timeit()def test_timeit():time.sleep(1)test_timeit()#test_timeit cost 1. seconds1æåçè¯ å ³äº Python çå ç¼ç¨ï¼æ»ç»å¦ä¸ï¼
å¦æå¸ææäºå½æ°æ¥æç¸åçåè½ï¼å¸æä¸æ¹ååæçè°ç¨æ¹å¼ãä¸åéå¤ä»£ç ãæç»´æ¤ï¼å¯ä»¥ä½¿ç¨è£ 饰å¨æ¥å®ç°ã
å¦æå¸ææä¸äºç±»æ¥ææäºç¸åçç¹æ§ï¼æè å¨ç±»å®ä¹å®ç°å¯¹å ¶çæ§å¶ï¼æ们å¯ä»¥èªå®ä¹ä¸ä¸ªå ç±»ï¼ç¶å让å®ç±»çå ç±»æå该类ã
å¦æå¸æå®ä¾çå±æ§æ¥ææäºå ±åçç¹ç¹ï¼å°±å¯ä»¥èªå®ä¹ä¸ä¸ªæ述符类ã
以ä¸å°±æ¯æ¬æ¬¡å享çææå 容ï¼å¦æä½ è§å¾æç« è¿ä¸éï¼æ¬¢è¿å ³æ³¨å ¬ä¼å·ï¼Pythonç¼ç¨å¦ä¹ åï¼æ¯æ¥å¹²è´§å享ï¼å 容è¦çPythonçµå书ãæç¨ãæ°æ®åºç¼ç¨ãDjangoï¼ç¬è«ï¼äºè®¡ç®ççãææ¯åå¾ç¼ç¨å¦ä¹ ç½ï¼äºè§£æ´å¤ç¼ç¨ææ¯ç¥è¯ã
åæï¼/post/如何把标题上面的网站隐藏
要将网站标题上面的新浪博客隐藏,需针对首页不同元素进行代码修改。源码
首先,源码隐藏最底部文字,源码代码为:.foot{ display:none;}
接着,源码蜜蜂源码去掉最上面新浪3.0代码,源码glbuffersubdata 源码代码为:.bodyTop{ display:none;}
去掉横幅代码,源码代码为:.banner{ display:none;}
去除顶部白条,源码代码为:.bodyTop{ background:#transparent}
隐藏特定元素,源码代码包括:.nka{ display:none;}.nktd{ display:none;},源码去除顶部菜单部分(登录、源码注册、源码搜索、源码androidvudroid源码帮助,源码无HOME),源码代码为:.menu .text a{ display:none;}.menu .home a{ display:none;},去除顶部菜单部分(HOME┆登录┆注册┆搜索┆帮助),tangram源码代码为:.menu a{ display:none;}.menu .text{ display:none;},去掉正文阅读处(固定链接┆评论┆引用┆阅读),代码为:.feeds .function a{ display:none;}.feeds .function td{ display:none;},去掉正文底部翻页处文字(第(1/)页 1 2 3 4 5 6 >>),flyio源码代码为:.gea{ display:none;},隐藏题图处的博客名字,代码为:.banner .title td{ display:none;},去掉新浪计数器的代码,代码为:#labelFM{ display:none;}
实现这些操作的具体步骤如下:点击控制面板中的个人首页维护,选择自定义空白面板,若已有模块则打开该模块显示源代码,粘贴隐藏代码并保存。若新增面板,先敲空格键,显示源代码区域粘贴代码,确保隐藏效果,保存面板后在个人首页定制界面选取并保存。
欢迎访问我的博客。
2024-11-19 12:16
2024-11-19 12:11
2024-11-19 11:21
2024-11-19 10:13
2024-11-19 09:58