import sys
sys.version
'3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 12:22:00) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]'
import sys
print(sys.path)
['', '/root/anaconda3/lib/python35.zip', '/root/anaconda3/lib/python3.5', '/root/anaconda3/lib/python3.5/plat-linux', '/root/anaconda3/lib/python3.5/lib-dynload', '/root/anaconda3/lib/python3.5/site-packages', '/root/anaconda3/lib/python3.5/site-packages/Sphinx-1.4.1-py3.5.egg', '/root/anaconda3/lib/python3.5/site-packages/setuptools-23.0.0-py3.5.egg', '/root/anaconda3/lib/python3.5/site-packages/IPython/extensions', '/root/.ipython']
import psutil
psutil.virtual_memory()
svmem(total=10196127744, available=4518469632, percent=55.7, used=5262598144, free=163364864, active=4818018304, inactive=4189945856, buffers=1291341824, cached=3478822912, shared=23580672)
print("CPU count - Pysical: " + str( psutil.cpu_count(logical=False) ) + " / Logical: " + str( psutil.cpu_count() ) )
CPU count - Pysical: 2 / Logical: 2
import time
time.sleep(0.2)
import sys
import argparse
parser = argparse.ArgumentParser(description='export vCard FILE to REST URL')
parser.add_argument('-f','--file', nargs=1, help='vCard file') # nargs= * + ? ...
if len(sys.argv)==1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
import os
os.rename(path_ori, path_bak)
os.makedirs(path_ori)
import os.path
os.path.isfile("/tmp")
os.path.exists("/tmp")
import glob,os
os.chdir("/etc")
os.getcwd()
glob.glob('host*')
['hostname', 'hosts', 'hosts.allow', 'hosts.deny', 'host.conf']
import sys
sys.argv[0]
'/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py'
print(os.path.dirname(os.path.realpath('__file__')))
/etc
f=open(path,'r',encoding='utf-8')
content=f.read()
f.close()
exec(open("executable.sh",encoding='utf-8').read())
datetime.datetime.fromtimestamp( os.path.getmtime('/tmp') ,tz)
datetime.datetime(2015, 6, 1, 0, 45, 2, 318563, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)
from IPython.display import Image
Image(filename='temp.png') #Display image
import math
print(math.ceil(10.0))
print(math.ceil(10.1))
print(type(math.ceil(10.1)))
10 11 <class 'int'>
("hue" * 3).upper()
'HUEHUEHUE'
str = "is is is";
print(str.replace("is", "was"))
print(str.replace("is", "was", 2))
was was was was was is
" \tworld\n ".strip() # strip, lstrip, rstrip
'world'
print(str(b'946809'), end = " ### " )
print(b'946809'.decode('utf8') )
b'946809' ### 946809
"%.2f" % float("3.5555")
'3.56'
from urllib.parse import urlparse
def get_filename_from_url(url):
return urlparse(url).path.split('/')[-1]
url = "http://imgqn.xxx.com/upload_files/2015/05/29/yyy.jpg!730x0.jpg"
urlparse(url)
ParseResult(scheme='http', netloc='imgqn.xxx.com', path='/upload_files/2015/05/29/yyy.jpg!730x0.jpg', params='', query='', fragment='')
from bs4 import BeautifulSoup # analyze html
#http://www.crummy.com/software/BeautifulSoup/bs4/doc/
soup = BeautifulSoup(html_doc)
soup.p['class']
soup.find_all('a')
soup.find_all('img', src=True):
soup.find_all("div", { "class" : "xxx"})
soup.find(id="link3")
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
import re
re.sub(r'(?i)\b(u|you+)\b', "your name", 'u YOU')
'your name your name'
re.match("c", "abcdef") # checks for a match only at the beginning of the string, No match
re.search("c", "abcdef")
<_sre.SRE_Match object; span=(2, 3), match='c'>
m = re.search("\w",'1dfsde2') # \w match as well as numbers and the underscore
if m:
print(m.group(0))
m
1
<_sre.SRE_Match object; span=(0, 1), match='1'>
import re
pattern="BEGIN:VCARD.*?END:VCARD"
result = re.findall(pattern,content,re.DOTALL)
print( 'Positive Lookbehind:\t' + re.sub(u'(?<=a)b', "*", 'abc a b c') )
print( 'Negative Lookbehind:\t' + re.sub(u'(?<!a)b', "*", 'abc a b c') )
print( 'Positive Lookahead:\t' + re.sub(u'b(?=c)', "*", 'abc a b c') )
print( 'Negative Lookahead:\t' + re.sub(u'b(?!c)', "*", 'abc a b c') )
Positive Lookbehind: a*c a b c Negative Lookbehind: abc a * c Positive Lookahead: a*c a b c Negative Lookahead: abc a * c
[1,]
[1]
(1)
1
(1,)
(1,)
(1,2,)
(1, 2)
#conditional operator
'y' if True else "x" # don't use: True and "y" or "x"
'y'
import requests
r = requests.get('')
r = requests.post('', data={})
r = requests.put('')
r = requests.delete('')
import requests
o = {
"name": v.fn.value,
"vcard": s
}
r = requests.post(args.url, auth = (args.user[0], args.password[0]), data =o )
print(r.reason)
b'{"count":2,"next":null,"previous":null,"results":[{"url":"http://127.0.0.1:8000/api/contacts/1/","name":"n1-admin","vcard":"v1-admin"},{"url":"http://127.0.0.1:8000/api/contacts/2/","name":"n2-api","vcard":"v2-api"}]}'
from urllib import request
url = 'https://gist.githubusercontent.com/omz/3823483/raw/3fbae981237897406db6a47984384082ab3360cf/FileTransfer.py'
request.urlretrieve(url, "/tmp/file_manager.py")
('/tmp/file_manager.py', <http.client.HTTPMessage at 0x7fb1934a2400>)
spam = 'hue'.__mul__ #a function
spam(4)
'huehuehuehue'
import string
sentence = "The quick brown fox jumps over the lazy dog" #pangram
len( [s for s in list(string.ascii_lowercase) if s in str.lower(sentence)] )
26
all(letter in sentence for letter in string.ascii_lowercase)
True
any(l == 't' for l in 'python') # Returns True. Same as: 't' in 'python
True
all(l == 't' for l in 'python') # Returns False. Not all of the letters are 't'.
False
from random import randint,seed
seed(1)
randint(1,100) #always the same
18
for i, j in enumerate('abc'): print(str(i) + ":" + j)
0:a 1:b 2:c
from functools import reduce
a = ['1', '2', '3']
reduce(lambda x, y: x + '-' + y, a)
'1-2-3'
a,b = [1,2,2], [2]
list(filter( lambda x: x not in b ,a))
[x for x in a if x not in b]
[1]
list(map(lambda x: x*2,[1,2,3]))
[2, 4, 6]
sorted(['ad','abd','b'])
['abd', 'ad', 'b']
sorted({'ad','abd','b','b'})
['abd', 'ad', 'b']
[{x for x in range(8)}]
[{0, 1, 2, 3, 4, 5, 6, 7}]
a1 = ["live", "arp", "strong"]
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
[s1 for s1 in a1 for s2 in a2 if s1 in s2]
['live', 'live', 'arp', 'arp', 'strong']
students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10),]
sorted(students, key=lambda s : s[2]) # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
{1,3,2} - {1}
{2, 3}
{1,3,2} & {1}
{1}
a = {'i': 30,'u': 20,'she':44}
b = {'i': 31,'u': 21,'he': 55}
c = a.copy()
c.update(b)
c
{'he': 55, 'i': 31, 'she': 44, 'u': 21}
[1, 2, 3, 4, 1, 4, 1].count(1)
3
[1,2,3][::-1]
[3, 2, 1]
a = [1,2,3,2,1]
a.remove(1) #insert, remove or sort only modify the list have no return value printed
a.insert(2, -1) # 2 is index, -1 is value
b = [1,2,3,2,1]
b.pop(1) # 1 is index
print(a)
a.reverse()
print(a)
print(b)
a+b
[2, 3, -1, 2, 1] [1, 2, -1, 3, 2] [1, 3, 2, 1]
[1, 2, -1, 3, 2, 1, 3, 2, 1]
delete_nth = lambda order, max_e: [x for i, x in enumerate(order) if order[:i].count(x) < max_e]
delete_nth([20,37,20,21],1) #item appears at most 1 times
[20, 37, 21]
from collections import defaultdict
d = defaultdict(list)
d[1] = 3 # int, not default list
d[1] = [4]
d[1].append(5)
d[2] #set to default []
d
defaultdict(<class 'list'>, {1: [4, 5], 2: []})
i = defaultdict(int)
i[1]
0
#class to json obj, from: http://stackoverflow.com/questions/3768895
from json import JSONEncoder
class MyEncoder(JSONEncoder):
def default(self, o):
return o.__dict__
j = MyEncoder().encode(p)
json.loads(j)
with open('allgoods.json', 'w') as outfile:
json.dump(map_goods, outfile, sort_keys=True, indent=4 )
from lxml import etree
root = etree.fromstring(b'<root>\n <child1/>\n <child2/>\n</root>\n')
print(etree.tostring(root,pretty_print=True).decode())
<root> <child1/> <child2/> </root>
import datetime
import pytz
tz = pytz.timezone('Asia/Shanghai')
print(datetime.datetime.now(tz).strftime("%Y-%m-%d %X") )
import time
filename = '_' + str(time.strftime('%Y-%m-%d_%H%M%S', time.localtime(time.time()))) + '.ext'
print(filename)
2017-08-06 17:03:17 _2017-08-06_090317.ext
def get_current_time():
return time.strftime('%Y-%m-%d %X ', time.localtime(time.time()))
try:
s="Warning: n你好 during api"
end = None
print( get_current_time() + s.encode('utf-8'), end = end)
except TypeError:
print('type_error')
else:
print("else")
print("x")
type_error x
float('Inf')
inf
recip = float('Inf')
try:
recip = 1 / f(x)
except ZeroDivisionError:
logging.info('Infinite result')
else:
logging.info('Finite result')
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-40-19798dae900f> in <module>() 1 recip = float('Inf') 2 try: ----> 3 recip = 1 / f(x) 4 except ZeroDivisionError: 5 logging.info('Infinite result') NameError: name 'f' is not defined
type(s.encode('utf-8'))
bytes
https://pexpect.readthedocs.io/en/stable/install.html
Pexpect is a pure Python module for spawning child applications; controlling them; and responding to expected patterns in their output.
import string
import unittest
def str_len(data):
return len(data)
class MyTest(unittest.TestCase):
def test_StrLen(self):
self.assertEqual(str_len(""), 0)
self.assertEqual(str_len("Hello World"), 11)
if __name__ == '__main__':
unittest.main(argv=['ignored', '-v'], exit=False)
test_StrLen (__main__.MyTest) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.003s OK
class Test:
Key1 = 1
def addkey():
setattr(Test, 'Key2', 2)
Test.Key3 = 3
print(Test.Key1)
print(Test.Key3)
Test.addkey()
print(Test.Key2)
1 3 2