def new_data(data):
# 这里传入字典数据或json数据都可以
required_fields = ["source", "recorder"]
for field in required_fields:
if not data.get(field):
return "Invlaid data"
data["timestamp"] = time.time()
bc.add_new_data(data)
return "Success"
bc = Blockchain()
a = new_data({"source": "香肠加工", "recorder": "小詹"})
print(a)
添加成功:
F:\push\20191027>python block.py
Success
现在数据是在 unconfirmed_data 中,我们需要挖矿,让它成功添加到区块链上:
def new_data(data):
# 这里传入字典数据或json数据都可以
required_fields = ["source", "recorder"]
for field in required_fields:
if not data.get(field):
return "Invlaid data"
data["timestamp"] = time.time()
bc.add_new_data(data)
return "Success"
def get_chain(blockchain):
chain_data = []
for block in blockchain.chain:
chain_data.append(block.__dict__)
return json.dumps({"length": len(chain_data),
"chain": chain_data})
def mine_unconfirmed_transactions(blockchain):
result = blockchain.mine()
if not result:
print("No data need to mine")
print("Block #{} is mined.".format(result))
bc = Blockchain()
a = new_data({"source": "香肠加工", "recorder": "小詹"})
print(a)
mine_unconfirmed_transactions(bc)
输出结果:
F:\push\20191027>python block.py
Success
Block #1 is mined.
现在来看看区块链上是不是有两个区块了(一个创始块,一个我们新增的区块)显示数据:
def get_chain(blockchain):
chain_data = []
for block in blockchain.chain:
chain_data.append(block.__dict__)
return json.dumps({"length": len(chain_data),
"chain": chain_data})
print(get_chain(bc))
class Point:
__slots__ = 'x', 'y', 'z'
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
>>> ob = Point(1,2,3)
>>> print(sys.getsizeof(ob))
64
import marvel
PUBLIC_KEY = '你的公钥'
PRIVATE_KEY = '你的私钥'
m = marvel.Marvel(PUBLIC_KEY, PRIVATE_KEY)
characters = m.characters
def get_hero_id(characters, name):
all_characters = characters.all(nameStartsWith=name)
# 根据名字获得角色信息,仅支持英文
ids = [i['id'] for i in all_characters['data']['results']]
names = [i['name'] for i in all_characters['data']['results']]
return ids,names
ids, names = get_hero_id(characters, 'thor')
comics = characters.comics(ids[0])
# ids[0]即1009664
print([i['title'] for i in comics['data']['results']])
结果如下:
(base) ckenddeMacBook-Pro:20190925 ckend$ python 1.py
['THOR VOL. 2: ROAD TO WAR OF THE REALMS TPB (Trade Paperback)', 'Marvel Masterworks: The Mighty Thor Vol. 18 (Hardcover)', 'King Thor (2019) #1', 'Thor Epic Collection: The Black Galaxy (Trade Paperback)', 'Thor (2018) #16', 'THOR & LOKI: BLOOD BROTHERS GALLERY EDITION HC (Hardcover)', 'Thor Of The Realms (Trade Paperback)', 'War Of The Realms Omega (2019) #1', 'Thor (2018) #15', 'The Unbeatable Squirrel Girl (2015) #46', 'Kirby Is… Mighty! King-Size (Hardcover)', 'Thor (2018) #14', 'War of the Realms (2019) #5', 'MARVEL ACTION CLASSICS: SPIDER-MAN TWO-IN-ONE 1 (2019) #1', 'Thor (2018) #13', 'Moon Girl and Devil Dinosaur (2015) #43', "Decades: Marvel in The '80s - Awesome Evolutions (Trade Paperback)", 'War of the Realms (2019) #3', 'The Art of War of the Realms (Trade Paperback)', 'Mighty Thor 3D (2019) #1']
In [66]: def f1(x):
....: return sum(c in '02468' for c in str(x))
....:
In [68]: x = int('1234567890'*50)
In [69]: %timeit f1(x)
10000 loops, best of 5: 52.2 µs per loop
生成器这么用其实是速度最慢的一种做法,花费了52微秒。我们来看看如果我改成列表解析式会怎么样:
In [67]: def f2(x):
....: return sum([c in '02468' for c in str(x)])
....:
In [70]: %timeit f2(x)
10000 loops, best of 5: 40.5 µs per loop