Python目录及正则表达式练习

import re
p = re.compile('(\d+)-(\d+)-(\d+)')
print(p.match('2018-05-10'))
print(p.match('2018-05-10').group())
print(p.match('2018-05-10').group(1))
print(p.match('2018-05-10').groups())
print(p.search('aa2018-05-10bb'))

phone = '123-456-789 #这是电话号码'
p2 = re.sub(r'#.*$','',phone)
print(p2)
p3 = re.sub(r'\D','',p2)
print(p3)

regular_v4 = re.findall(r'[t,w]h','https://docs.python.org/3/whatsnew/3.6.htm')
print (regular_v4)


import os
print(os.path.abspath('.')) #获取绝对路径
print(os.path.abspath('..'))
print(os.path.exists('E:/python_study'))

print(os.path.isfile('E:/python_study'))
print(os.path.isdir('E:/python_study'))

print(os.path.join('E:/python_study/','b/c'))


from pathlib import Path
p = Path('.')
print(p.resolve())

p.is_dir()
q = Path('E:/python_study/a/b/c')
Path.mkdir(q,parents=True)
发表在 Python | 留下评论

Python多线程编程

函数的方法

import threading
import time
from threading import current_thread

def myThread(arg1,arg2):
    print(current_thread().getName(),'start')
    print('%s %s' %(arg1,arg2))
    time.sleep(1)
    print(current_thread().getName(),'stop')

for i in range(1,6,1):
    # t1 = myThread(i,i+1)
    t1 = threading.Thread(target=myThread,args=(i,i+1))
    t1.start()

print(current_thread().getName(),'end')

类的方法

import threading
from threading import current_thread

class Mythread(threading.Thread):
    def run(self):
        print(current_thread().getName(),'start')
        print('run')
        print(current_thread().getName(),'stop')

t1 = Mythread()
t1.start()
t1.join()

print(current_thread().getName()

生产者与消费者练习

from threading import Thread,current_thread
import time
import random
from queue import Queue

queue = Queue(5)

class ProducerThread(Thread):
    def run(self):
        name = current_thread().getName()
        nums = range(100)
        global queue
        while True:
            num = random.choice(nums)
            queue.put(num)
            print('生产者 %s 生产了数据 %s' %(name,num))
            t = random.randint(1,3)
            time.sleep(t)
            print('生产者 %s 睡眠了 %s' %(name,t))

class ConsumerThread(Thread):
    def run(self):
        name = current_thread().getName()
        global queue
        while True:
            num = queue.get()
            queue.task_done()
            print('消费者 %s 消耗了数据 %s' %(name,num))
            t = random.randint(1,5)
            time.sleep(t)
            print('消费者 %s 睡眠了 %s' %(name,t))

p1 = ProducerThread()
p1.start()
p2=ProducerThread()
p2.start()
p3=ProducerThread()
p3.start()
c1 = ConsumerThread()
c1.start()
c2 = ConsumerThread()
c2.start()
发表在 Python | 留下评论

Python类练习及类的使用-自定义with语句

class Player():            
    def __init__(self,name,hp,occu):
        # self.name=name   
        self.__name = name 
        self.hp=hp
        self.occu=occu
    def print_role(self):  
        # print('%s: %s %s' %(self.name,self.hp,self.occu))
        print('%s: %s %s' % (self.__name, self.hp, self.occu))
    def updateName(self,newname):
        self.name=newname



class Monster():
    '定义怪物类'
    def __init__(self,hp=100):
        self.hp = hp
    def run(self):
        print('移动到某个位置')
    def whoami(self):
        print('我是怪物父类')

class Animials(Monster):
    '普通怪物'
    def __init__(self,hp=10):
        #self.hp = hp
        super().__init__(hp)

class Boss(Monster):
    'Boss类怪物'
    def __init__(self,hp=1000):
        super().__init__(hp)
    def whoami(self):
        print('我是怪物我怕谁')

a1 = Monster(200)
print(a1.hp)
print(a1.run())
a2 = Animials(1)
print(a2.hp)
print(a2.run())

a3 = Boss(800)
a3.whoami()

print('a1的类型 %s' %type(a1))
print('a2的类型 %s' %type(a2))
print('a3的类型 %s' %type(a3))

print(isinstance(a2,Monster)) 



user1 = Player('tom',100,'war')   
user2 = Player('jerry',80,'master')
user1.print_role()
user2.print_role()

user1.updateName('wilson')
user1.print_role()
user1.name='aaa'
user1.print_role()

自定义with语句

class Testwith():
    def __enter__(self):
        print('run')
    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_tb is None:
            print('正常结束')
        else:
            print('has error %s' %exc_tb)


with Testwith():
    print('Test is runing')
    raise NameError('testNameError')
发表在 Python | 留下评论

Pycharm 配置autopep8

pip install autopep8

Settings–>Tools–>External Tools 点击添加按钮

 Name:Autopep8
 Tools settings: 
     Programs:autopep8
     Parameters: --in-place --aggressive --aggressive $FilePath$
     Working directory: $ProjectFileDir$
 Output Filters中输入:$FILE_PATH$\:$LINE$\:$COLUMN$\:.*
发表在 Python | 留下评论

python闭包及装饰器练习

def tips(func):
    def nei(a,b):
        print('start')
        func(a,b)
        print('stop')

    return nei

@tips
def add(a,b):
    print(a+b)

@tips
def sun(a,b):
    print(a-b)

print(add(4,5))
print(sun(5,1))


def new_tips(argv):
    def tips(func):
        def nei(a,b):
            print('start %s %s' %(argv,func.__name__))
            func(a,b)
            print('stop')

        return nei
    return tips

@new_tips('add_module')
def add(a,b):
    print(a+b)

@new_tips('sub_module')
def sub(a,b):
    print(a-b)

print(add(4,5))
print(sub(5,4))
发表在 Python | 留下评论

递归累加

vim test.c
#include<stdio.h>

long sum(int n)
{
        if(1==n)
        {
                return 1;
        }
        else
        {
                return n+sum(n-1);
        }
}



int main(void)
{
        int n=100,a=0;
        a=sum(100);
        printf("sum=%d\n",a);
        return 0;
}
gcc test.c
./a.out
发表在 C | 留下评论