Python---class and iter

import itertools
import subprocess
import util_m
'''it is for shell command'''
import builtins
'''it is loaded into memory when python module starts'''
class myclass1:
	pass

'''pass means {} in java or c'''

class myclass:
	def __init__(self, max):
		self.max=max

	def __next__(self):
		flib = self.a
		if flib > self.max:
			raise StopIteration
		self.a,self.b= self.b, self.a+self.b
		return flib

	def __iter__(self):
		self.a = 0
		self.b=1
		return self

'''
__init__(self)  means contruction function
self means this in c++
__iter__ is used when for loop start, and return interator of self
__next__ is used when for loop next, when StopIteration happen , for loop quit
'''

'''generator expression=====================, use () , it means generator iterator, not to use others'''
gen1 =  (ord(c) for c in {'E', 'B','S','W'})

'''generator function=========================, return ord(c) every time, it is good use'''
def getord(string1):
	for c in string1:
		yield ord(c)

def per1():
	perm1=itertools.permutations([1,2,3],2)
	list1=list(perm1)
	for item in list1:
		yield item
'''
permutations return iterator
list(iterator) returns list, list is many data
use for loop to get each item
because per1 is util app, it not good to print info here, so return generator object
in caller, we can use for loop to print generator object, no need to call next() because for loop is smart
'''

def group1():
	name=['candy', 'mandy','hebe']
	print(list(itertools.groupby(name, len)))
'''return key, value_list'''

def chain1():
	print(list(range(0,5)))
	print(list(itertools.chain(range(0,5), range(6,10))))
	print(list(zip(range(0,5), range(6,10))))
	'''match util 3, because 9 is the last in latter list'''
	print(list(itertools.zip_longest(range(0,5), range(6,10))))
	'''range(0,5)------0,1,2,3,4. match util 4'''

def trans1():
	table1={ord('A'):ord('B')}
	print('Able'.translate(table1))

def eval1():
	x=5
	eval('1+1')
	print(eval('x*5'))
	print(eval('"A"+"B"'))
	print(eval('__import__("subprocess").getoutput("ls ~/ -l")'))
	subprocess.call(['df','-h'])
	subprocess.call(['uname', '-a'])
'''
eval can calculate the expression in string
but it is not safe, it will be attacked by hacker
'''

def subp1():
	print(subprocess.getoutput('ls ~/ -l'))

def builthello():
	print('hello__builtins__')


if  __name__ == '__main__':
	c1 = myclass(6)
	print(c1.max)
	for n in c1:
		print(n)
	try:
		assert 1>2, 'a is not bigger than b'
	except AssertionError:
		print('I got exception')
	print(next(gen1))
	print(next(gen1))
	gen2=getord({'E','B','S','W'})
	print(next(gen2))
	print(next(gen2))
	for item in per1():
		print(item)

	for item2 in list(itertools.permutations([1,2,3], 2)):
		print(item2)

	print(list(itertools.product('ABC','123')))
	print(list(itertools.combinations('ABC',2)))
	group1()
	chain1()
	trans1()
	eval1()
	subp1()
	__builtins__.__dict__['hello']=builthello
	'''add hello api to __builtins__, to load this api when python module starts'''
	hello()
	util_m.built()
	'''for loop is smart, it can call next automatically'''



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值