python基础 -- 获取对象信息的内置方法
issubclass12345678910111213141516171819202122232425262728293031323334class A(object): passclass B(object): passclass C(object): passclass D(A): passprint(issubclass(D, A)) # Trueprint(issubclass(D, B)) # Falseprint(issubclass(D, (B, A, C))) # Trueprint(issubclass(D, (B, C))) # Falseprint(issubclass(bool, int)) # Trueprint(issubclass(bool, str)) # Falseprint(issubclass(bool, (str, int, dict))) # Trueprint(issubclass(bool, (str, list, dict))) # Falseprint(isinstance(D(), D)) # ...
python基础 -- 访问限制
123456789101112131415161718192021222324252627282930class Student: # 类属性 __count = 0 def __init__(self, name) -> None: # 实例属性 self.name = name Student.__count += 1 self.__age = 100 self._score = 1000s1 = Student('haha')print(s1._score) # 1000# print(s1.__age) # AttributeError: 'Student' object has no attribute '__age'print(dir(s1)) # ['_Student__age', '_Student__count', '__class__ ...
流畅的Python -- 文本和字节序列
[TOC]
人类使用文本,计算机使用字节序列
字符问题python3的str相当于python2中的unicode类型
str->bytes encode
bytes->str decode
但是由于编码的时候会指定编码方式,解码的时候需要使用同样的编码方式,很多时候拿到了bytes却不知道编码方式导致无法解码
流畅的Python -- 第一章 Python的模型
[toc]
Python的模型一摞Python风格的纸牌12345678910111213141516import collectionsCard = collections.namedtuple('Card', ['rank', 'suit'])class FrenchDeck: ranks = [str(n) for n in range(2, 11)] + list('JQKA') suits = 'spades diamonds clubs hearts'.split() def __init__(self): self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks] def __len__(self): return len(self._cards) def __getitem__(self, position): ...
流畅的Python -- 第三章 字典与集合
[TOC]
泛映射类型(key-value)这里提到了一个可散列的概念,可散列对象需要实现一个__hash__方法,会有一个可散列值,这个值在生命周期中是不变的,大概也就是说需要是原子不可变数据类型(str、bytes和数值类型),一般来讲用户自定义类的对象都是可散列的,散列值是他们id()函数的返回值
只有可散列的数据才能作为字典的key
dict(one=1, two=2, three=3)
{'one': 1, 'two': 2, 'three': 3}
dict(zip(['one', 'two', 'three'], [1, 2, 3]))
dict([('two', 2), ('one', 1), ('three', 3)])
dict({'three': 3, 'one': 1, 'two': 2})
构建字典的几种方式,实现方式太多也不是好 ...
流畅的Python -- 第二章 序列构成的数组
[toc]
序列构成的数组Python的作者以前参与过一个叫ABC的语言的迭代,这个语言是专为初学者准备的,所以Python身上也有很多它的特征,其中之一就是序列的泛型操作,也就是字符串、列表、数组、字节序列、xml元素以及数据库查询结果等都共用一套操作:迭代、切片、排序和拼接
比如说java中string要进行切割的话需要调用方法比如split(当然py中同样有这个方法)或者substring来指定保留的内容,而数组需要通过copyOfRange方法或者subList方法,但py中都可以通过[]来完成
Python内置序列(集合)容器序列list、tuple 和 collections.deque 这些序列能存放不同类型的数据。
扁平序列str、bytes、bytearray、memoryview 和 array.array,这类序列只能容纳一种类型。
容器序列中存储的是对象引用,扁平序列中存储的是值(基础数据类型),这里说扁平序列都是连续的内存空间更加紧凑
可变不可变序列按照是否可变进行划分
可变:list、bytearray、array.array、collections.de ...
爬虫 -- Xpath解析
抓取数据from lxml import etree
html = requests.get(url=product_url)
<!--解析数据-->
html = etree.HTML(html.content)
html_data = html.xpath('/html/body/div/ul/li/a')
使用xpath进行数据定位<!--这里列举一些语法案例-->
//button[@type='submit']
//section/span[contains(@class,'glyphsSpriteHeart')]/..
//input[@id='username']
//a[contains(@href, "/products/")]/@href
爬虫 -- 爬虫总结
问题与解决方案模拟请求
hibernate -- hibernate故事会
hibernate故事会
2016年1月29日
9:34
关于id生成策略的故事:
“assigned”–主键由外部程序负责生成,在 save() 之前指定一个。
“hilo”–通过hi/lo 算法实现的主键生成机制,需要额外的数据库表或字段提供高位值来源。
“seqhilo”–与hilo 类似,通过hi/lo 算法实现的主键生成机制,需要数据库中的 Sequence,适用于支持 Sequence 的数据库,如Oracle。
“increment”–主键按数值顺序递增。此方式的实现机制为在当前应用实例中维持一个变量,以保存着当前的最大值,之后每次需要生成主键的时候将此值加1作为主键。这种方式可能产生的问题是:不能在集群下使用。
“identity”–采用数据库提供的主键生成机制。如DB2、SQL Server、MySQL 中的主键生成机制。
“sequence”–采用数据库提供的 sequence 机制生成主键。如 Oralce 中的Sequence。
“native”–由 Hibernate 根据使用的数据库自行判断采用 identity、hilo、sequence 其中一种作 ...
hibernate -- hibernate笔记1
hibernate笔记1
2016年1月20日
13:33
hibernate配置
导入hibernate的jar包
编写hibernate配置文件
1234567891011121314151617181920
<?xml version=”1.0” encoding=”UTF-8”?><!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!– 数据库的方言,每个数据库都有自己的方言 –> <property name=”dialect”>org.hibernate.di ...