System

In [1]:
import sys
sys.version
Out[1]:
'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)]'
In [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']
In [1]:
import psutil
psutil.virtual_memory()
Out[1]:
svmem(total=10196127744, available=4518469632, percent=55.7, used=5262598144, free=163364864, active=4818018304, inactive=4189945856, buffers=1291341824, cached=3478822912, shared=23580672)
In [5]:
print("CPU count - Pysical: " + str( psutil.cpu_count(logical=False) ) + " / Logical: " + str( psutil.cpu_count() ) )
CPU count - Pysical: 2 / Logical: 2
In [13]:
import time
time.sleep(0.2)

argparse

In [ ]:
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()

File

In [241]:
import os
os.rename(path_ori, path_bak)    
os.makedirs(path_ori)
In [ ]:
import os.path
os.path.isfile("/tmp") 
os.path.exists("/tmp")
In [12]:
import glob,os
os.chdir("/etc")
os.getcwd()

glob.glob('host*')
Out[12]:
['hostname', 'hosts', 'hosts.allow', 'hosts.deny', 'host.conf']
In [30]:
import sys
sys.argv[0]
Out[30]:
'/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py'
In [31]:
print(os.path.dirname(os.path.realpath('__file__')))
/etc
In [43]:
f=open(path,'r',encoding='utf-8')
content=f.read()
f.close()
In [ ]:
exec(open("executable.sh",encoding='utf-8').read())
In [263]:
datetime.datetime.fromtimestamp( os.path.getmtime('/tmp') ,tz)
Out[263]:
datetime.datetime(2015, 6, 1, 0, 45, 2, 318563, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)

Image

In [ ]:
from IPython.display import Image
Image(filename='temp.png')  #Display image

Math

In [5]:
import math
print(math.ceil(10.0))
print(math.ceil(10.1))
print(type(math.ceil(10.1)))
10
11
<class 'int'>

String

In [234]:
("hue" * 3).upper()
Out[234]:
'HUEHUEHUE'
In [12]:
str = "is is is";
print(str.replace("is", "was"))
print(str.replace("is", "was", 2))
was was was
was was is
In [132]:
" \tworld\n  ".strip()    # strip, lstrip, rstrip
Out[132]:
'world'
In [7]:
print(str(b'946809'), end = " ### " )
print(b'946809'.decode('utf8') )
b'946809' ### 946809
In [1]:
"%.2f" % float("3.5555")
Out[1]:
'3.56'
In [9]:
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)
Out[9]:
ParseResult(scheme='http', netloc='imgqn.xxx.com', path='/upload_files/2015/05/29/yyy.jpg!730x0.jpg', params='', query='', fragment='')
In [240]:
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>
In [3]:
import re
re.sub(r'(?i)\b(u|you+)\b', "your name", 'u YOU')
Out[3]:
'your name your name'
In [61]:
re.match("c", "abcdef") # checks for a match only at the beginning of the string, No match
In [59]:
re.search("c", "abcdef")
Out[59]:
<_sre.SRE_Match object; span=(2, 3), match='c'>
In [7]:
m = re.search("\w",'1dfsde2') # \w match  as well as numbers and the underscore
if m: 
    print(m.group(0))
m    
1
Out[7]:
<_sre.SRE_Match object; span=(0, 1), match='1'>
In [ ]:
import re
pattern="BEGIN:VCARD.*?END:VCARD"
result = re.findall(pattern,content,re.DOTALL) 
In [22]:
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

Basic

In [12]:
[1,]
Out[12]:
[1]
In [13]:
(1)
Out[13]:
1
In [14]:
(1,)
Out[14]:
(1,)
In [17]:
(1,2,)
Out[17]:
(1, 2)
In [16]:
#conditional operator 
'y' if True else "x"  # don't use: True and "y" or "x"
Out[16]:
'y'
In [ ]:
import requests
r = requests.get('')
r = requests.post('', data={})
r = requests.put('')
r = requests.delete('')
In [107]:
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)
Out[107]:
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"}]}'
In [2]:
from urllib import request
url = 'https://gist.githubusercontent.com/omz/3823483/raw/3fbae981237897406db6a47984384082ab3360cf/FileTransfer.py'
request.urlretrieve(url, "/tmp/file_manager.py")
Out[2]:
('/tmp/file_manager.py', <http.client.HTTPMessage at 0x7fb1934a2400>)
In [128]:
spam = 'hue'.__mul__  #a function
spam(4)
Out[128]:
'huehuehuehue'
In [133]:
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)]  )
Out[133]:
26
In [134]:
all(letter in sentence for letter in string.ascii_lowercase)
Out[134]:
True
In [135]:
any(l == 't' for l in 'python') # Returns True. Same as: 't' in 'python
Out[135]:
True
In [136]:
all(l == 't' for l in 'python') # Returns False. Not all of the letters are 't'.
Out[136]:
False
In [137]:
from random import randint,seed
seed(1)
randint(1,100) #always the same
Out[137]:
18
In [138]:
for i, j in enumerate('abc'): print(str(i) + ":" + j)
0:a
1:b
2:c
In [139]:
from functools import reduce 
a = ['1', '2', '3'] 
reduce(lambda x, y: x + '-' + y, a) 
Out[139]:
'1-2-3'
In [140]:
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]
Out[140]:
[1]
In [141]:
list(map(lambda x: x*2,[1,2,3]))
Out[141]:
[2, 4, 6]
In [142]:
sorted(['ad','abd','b'])
Out[142]:
['abd', 'ad', 'b']
In [143]:
sorted({'ad','abd','b','b'})
Out[143]:
['abd', 'ad', 'b']
In [144]:
[{x for x in range(8)}]
Out[144]:
[{0, 1, 2, 3, 4, 5, 6, 7}]
In [145]:
a1 = ["live", "arp", "strong"] 
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
[s1 for s1 in a1 for s2 in a2 if s1 in s2]
Out[145]:
['live', 'live', 'arp', 'arp', 'strong']
In [146]:
students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10),]  
sorted(students, key=lambda s : s[2])   # sort by age  
Out[146]:
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
In [147]:
{1,3,2} - {1}
Out[147]:
{2, 3}
In [148]:
{1,3,2} & {1}
Out[148]:
{1}
In [149]:
a = {'i': 30,'u': 20,'she':44}
b = {'i': 31,'u': 21,'he': 55}
c = a.copy()
c.update(b)
c
Out[149]:
{'he': 55, 'i': 31, 'she': 44, 'u': 21}
In [150]:
 [1, 2, 3, 4, 1, 4, 1].count(1)
Out[150]:
3
In [151]:
[1,2,3][::-1]
Out[151]:
[3, 2, 1]
In [8]:
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]
Out[8]:
[1, 2, -1, 3, 2, 1, 3, 2, 1]
In [153]:
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
Out[153]:
[20, 37, 21]
In [154]:
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
Out[154]:
defaultdict(<class 'list'>, {1: [4, 5], 2: []})
In [155]:
i = defaultdict(int)
i[1]
Out[155]:
0
In [ ]:
#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 )
In [37]:
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>

Time

In [11]:
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
In [44]:
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
In [41]:
float('Inf')
Out[41]:
inf
In [40]:
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
In [27]:
type(s.encode('utf-8'))
Out[27]:
bytes

code practice

http://www.codewars.com/

Packages

Pexpect

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.

In [25]:
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

Meta

In [57]:
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