本文共 11916 字,大约阅读时间需要 39 分钟。
删除属性时调用
描述器实现前提是描述器类实例作为类属性
注意所谓的类属性不仅仅类似与x=A()的属性,类中定义的函数也是类属性
from functools import partialclass StaticMethod: def __init__(self, fn): self.fn = fn def __get__(self, instance, owner): return self.fnclass ClassMethod: def __init__(self, fn): self.fn = fn def __get__(self, instance, owner): return partial(self.fn, owner)class Test: @StaticMethod def s_mtd(): # s_mtd = StaticMethod(s_mtd) print('s_mtd') @ClassMethod def c_mtd(cls): # c_mtd = ClassMethod(c_mtd) print('c_mtd', cls)if __name__ == '__main__': Test.s_mtd() Test.c_mtd()
class Property: def __init__(self, fget=None, fset=None): self.fget = fget self.fset = fset def __get__(self, instance, owner): return self.fget(instance) def __set__(self, instance, value): self.fset(instance, value) def getter(self): pass def setter(self, fset): self.fset = fset return selfclass Person: def __init__(self, name, age): self._name = name self._age = age @Property def name(self): # name=Property(name) return self._name @name.setter def name(self, value): # name=Property(name).setter(name) (value) self._name = value @Property def age(self): # name=Property(name) return self._age @age.setter def age(self, value): # name=Property(name).setter(name) (value) self._age = value
import inspectclass TypeCheck: def __init__(self, key, type): print('TC init') self.key = key self.type = type def __get__(self, instance, owner): print('TC get') if instance is not None: return instance.__dict__[self.key] return self def __set__(self, instance, value): print('TC set') if not isinstance(value, self.type): raise TypeError instance.__dict__[self.key] = valuedef typeassert(cls): params = inspect.signature(cls).parameters for name, type in params.items(): if type != type.empty: setattr(cls, name, type.annotation) return cls@typeassertclass Person: name = TypeCheck('name', str) age = TypeCheck('age', int) def __init__(self, name: str, age: int): self.name = name self.age = agetom = Person('tom', 12)print(tom.name)
import inspectclass TypeCheck: def __init__(self, key, type): print('TC init') self.key = key self.type = type def __get__(self, instance, owner): print('TC get') if instance is not None: return instance.__dict__[self.key] return self def __set__(self, instance, value): print('TC set') if not isinstance(value, self.type): raise TypeError instance.__dict__[self.key] = valueclass TypeAssert: def __init__(self, cls): self.cls = cls def __call__(self, name, age): params = inspect.signature(self.cls).parameters for key, type in params.items(): if type != type.empty: setattr(self.cls, key, TypeCheck(key, type.annotation)) return self.cls(name, age)@TypeAssertclass Person: # Person = TypeAssert(Person) name = TypeCheck('name', str) age = TypeCheck('age', int) def __init__(self, name: str, age: int): self.name = name self.age = agetom = Person('tom', '12')print(tom.name)
class Node: """ Description: Node Class attr item: current Node`s data attr next: points to the next Node attr past: points to the last Node """ def __init__(self, item: object): self.__item = item self.__next = None self.__past = None @property def item(self): return self.__item @item.setter def item(self, value): self.__item = value @property def next(self): return self.__next @next.setter def next(self, value: object): self.__next = value @property def past(self): return self.__past @past.setter def past(self, value: object): self.__past = valueclass LinkedList: """ Description: Base class LinkedList """ def __init__(self): self.cur = None self.head = None self.length = 0 def append(self, no: object): raise Exception('Base Method') def iternodes(self): raise Exception('Base Method') def pop(self): raise Exception('Base Method') def insert(self, position: int, value: object): raise Exception('Base Method') def remove(self, value: object): raise Exception('Base Method')class SingleLinkedList(LinkedList): """ Description: attr head: head Node attr cur: current Node method append(): append Node """ def __init__(self): super().__init__() def __iter__(self): cur_node = self.head while True: yield cur_node.item if not cur_node.next: break cur_node = cur_node.next def __getitem__(self, item): cur_node = self.head if isinstance(item, slice): pass else: for _ in range(item): cur_node = cur_node.next return cur_node.item def __setitem__(self, key, value): cur_node = self.head for _ in range(key): cur_node = cur_node.next cur_node.item = value def append(self, no: object): if self.length == 0: self.cur = Node(no) self.head = self.cur else: self.cur.next = Node(no) self.cur = self.cur.next self.length += 1sl = SingleLinkedList()sl.append(1)sl.append(2)for i in sl: print(i)sl[1] = 999sl[0] = 234for i in sl: print(i)class DoubleLinkedList(LinkedList): """ Description: attr head: attr cur: method append: method pop: method insert: method remove: method iternodes: """ def __init__(self): super().__init__() def __iter__(self): cur_node = self.head while True: yield cur_node.item if not cur_node.next: break cur_node = cur_node.next def __reversed__(self): cur_node = self.cur while True: yield cur_node.item if not cur_node.past: break cur_node = cur_node.past def __getitem__(self, item): cur_node = self.head if isinstance(item, slice): pass else: for _ in range(item): cur_node = cur_node.next return cur_node.item def __setitem__(self, key, value): cur_node = self.head for _ in range(key): cur_node = cur_node.next cur_node.item = value def append(self, no: object): if self.length == 0: self.cur = Node(no) self.head = self.cur else: temp = self.cur self.cur.next = Node(no) self.cur = self.cur.next self.cur.past = temp self.length += 1 def pop(self): pop_node = self.cur pop_node.past.next = None self.cur = self.cur.past self.length -= 1 return pop_node def insert(self, position: int, value: object): cur_node = self.head new_node = Node(value) for _ in range(position - 1): cur_node = cur_node.next next_node = cur_node.next cur_node.next = new_node new_node.past = cur_node new_node.next = next_node next_node.past = new_node def remove(self, value: object): cur_node = self.head while True: if cur_node.item == value: cur_node.past.next = cur_node.next cur_node.next.past = cur_node.past break elif not cur_node.next: raise Exception('NodeNotFound') cur_node = cur_node.next def iternodes(self, *, reverse=False): if not reverse: cur_node = self.head while True: yield cur_node.item if not cur_node.next: break cur_node = cur_node.next else: cur_node = self.cur while True: yield cur_node.item if not cur_node.past: break cur_node = cur_node.past
raise 异常实例
try:待捕获异常的代码块except [异常类型] as e:异常的处理代码块else:...finally:...
在import所在的作用域的局部命名空间中,增加名称和上一步创建的对象关联
from ... import *
setuptools
pip
pip install wheel
# from distutils.core import setup # 可能失败from setuptools import setupsetup(name='Distutils', version='1.0', description='Python Distribution Utilities', author='Greg Ward', author_email='gward@python.net', url='https://www.python.org/sigs/distutils-sig/', packages=['distutils', 'distutils.command'], )
python setup.py cmd -help
python setup.py build
python setup.py install
python setup.py sdist
Mysql
useradd gitsu - gittar xf gogs*cd gogsmysql -uroot -p < scripts/mysql.sql
grant all on gogs.* to 'gogs'@'%' identified by 'gogs';flush privileges;
mkdir -p custom/confcd custom/conftouch app.ini
./gogs web
gogs下需要建立log目录
root用户操作
cp /home/git/gogs/scripts/init/centos/gogs /etc/init.d/chmod +x /etc/init.d/gogschkconfig gogs onservice gogs start
首次登陆
转载于:https://blog.51cto.com/11281400/2118365