#!/usr/bin/env python # coding: utf-8 # # System # In[1]: import sys sys.version # In[1]: import sys print(sys.path) # In[1]: import psutil psutil.virtual_memory() # In[5]: print("CPU count - Pysical: " + str( psutil.cpu_count(logical=False) ) + " / Logical: " + str( psutil.cpu_count() ) ) # 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*') # In[30]: import sys sys.argv[0] # In[31]: print(os.path.dirname(os.path.realpath('__file__'))) # 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) # ## 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))) # # String # In[234]: ("hue" * 3).upper() # In[12]: str = "is is is"; print(str.replace("is", "was")) print(str.replace("is", "was", 2)) # In[132]: " \tworld\n ".strip() # strip, lstrip, rstrip # In[7]: print(str(b'946809'), end = " ### " ) print(b'946809'.decode('utf8') ) # In[1]: "%.2f" % float("3.5555") # 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) # 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") # Tillie # ## Regex # https://www.regex101.com/ # http://regexr.com/ # https://docs.python.org/3/library/re.html # In[3]: import re re.sub(r'(?i)\b(u|you+)\b', "your name", 'u YOU') # 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") # In[7]: m = re.search("\w",'1dfsde2') # \w match as well as numbers and the underscore if m: print(m.group(0)) m # 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'(?\n \n \n\n') print(etree.tostring(root,pretty_print=True).decode()) # #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) # 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") # In[41]: float('Inf') # In[40]: recip = float('Inf') try: recip = 1 / f(x) except ZeroDivisionError: logging.info('Infinite result') else: logging.info('Finite result') # In[27]: type(s.encode('utf-8')) # # # 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) # # 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)