python学习之路上的一些小笔记

简述

在学习python的过程中,我积累了不少的笔记以及练习,但是疏于整理正好时至春节放假之际,再加上新型冠状病毒的肆虐。各个村、小区都已经封锁,实在无聊对我考完研究生之后学习python到现在的笔记进行记录一下,这是在家里学习的一部分,还有在学校的一部分写在这里作为提醒写在这里,以及进行时不时的回忆

进程的学习

进程对于python不可小觑,以下是我学习进程的代码,以后再做补充

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

#进程

import multiprocessing
import time
def action(a,b):
    for i in range(30):
        print(a,' ',b)
        time.sleep(0.1)
if __name__=='__main__':
    jc1=multiprocessing.Process(target=action,args=('进程一',0))
    jc2=multiprocessing.Process(target=action,args=('进程二',1))
    
    jc1.start()
    jc2.start()
    jc1.join()
    jc2.join()
    print('jc1,jc2任务都已执行完毕')
    jc1.close()
    jc2.close()


IO以及python中书写路径的格式

'''
文件路径不能用反斜杠‘\’。举个例子,如果我传入的文件路径是这样的:

sys.path.append('c:\Users\mshacxiang\VScode_project\web_ddt')

则会报错SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: tr

原因分析:在windows系统当中读取文件路径可以使用\,但是在python字符串中\有转义的含义,如\t可代表TAB,\n代表换行,所以我们需要采取一些方式使得\不被解读为转义字符。目前有3个解决方案

1、在路径前面加r,即保持字符原始值的意思。

sys.path.append(r'c:\Users\mshacxiang\VScode_project\web_ddt')

2、替换为双反斜杠

sys.path.append('c:\\Users\\mshacxiang\\VScode_project\\web_ddt')

3、替换为正斜杠

sys.path.append('c:/Users/mshacxiang/VScode_project/web_ddt')
'''



fpath = r'C:\Windows\system.ini'

with open(fpath, 'r') as f:
    s = f.read()
    print(s)





错误的捕获和调试

#错误的捕获并且记录

import logging
from functools import reduce
import json

def str2num(s):
    return json.loads(s)
#json.loads 用于解码 JSON 数据,json 类型转换到 python 的类型

def calc(exp):
    ss = exp.split('+')
    ns = map(str2num, ss)
    return reduce(lambda acc, x: acc + x, ns)

def main():
    try:
        r = calc('100 + 200 + 345')
        print('100 + 200 + 345 =', r)
        r = calc('99 + 88 + 7.6')
        print('99 + 88 + 7.6 =', r)
    except Exception as e:
        logging.exception(e)

main()

reduce

#reduce
'''
reduce把一个函数作用在一个序列[x1, x2, x3, ...]上,
这个函数必须接收两个参数,
reduce把结果继续和序列的下一个元素做累积计算,其效果就是:
'''
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

#枚举类型不可实例化,不可更改。

'''
import urllib.request
response = urllib.request.urlopen('http://placekitten.com/g/500/600')
cat_img = response.read()
with open('cat_500_600.jpg','wb') as f:
    f.write(cat_img)
    
'''
'''
response.info()
print(response.info())
'''

property

#练习 @property
class Screen(object):
    @property
    def width(self):
        return self._width
    @width.setter
    def width(self,width):
        self.__width=width
    
    @property
    def height(self):
        return self._height
    @height.setter
    def height(self,height):
        self.__height=height 
    
    
    
    @property
    def resolution(self):
        return self.__width*self.__height




# 测试:
s = Screen()
s.width = 1024
s.height = 768
print('resolution =', s.resolution)
if s.resolution == 786432:
    print('测试通过!')
else:
    print('测试失败!')

正确区分类属型和实例属性,练习


#方法一
class Student(object):
    count = 0

    def __init__(self, name):
        self.name = name
        Student.count=Student.count+1
#方法二
class Student(object):

    """Student class

    function:

    add_count

    __init__    -- Instantiate object

    _add_count  -- add Count

    """

    count = 0

    def _add_count(func):

        def wrapper(*args, **kw):

            Student.count += 1

            return func(*args, **kw)

        return wrapper

    @_add_count

    def __init__(self, name):

        self.name = name

# 测试:
if Student.count != 0:
    print('测试失败!')
else:
    bart = Student('Bart')
    if Student.count != 1:
        print('测试失败!')
    else:
        lisa = Student('Bart')
        if Student.count != 2:
            print('测试失败!')
        else:
            print('Students:', Student.count)
            print('测试通过!')

参考

上面的绝大多数代码都是我在廖雪峰的教程网站上进行学习,并摘抄参考下来的
廖雪峰老师的网站
[1]: https://www.liaoxuefeng.com/wiki/1016959663602400

发布了2 篇原创文章 · 获赞 0 · 访问量 133

猜你喜欢

转载自blog.csdn.net/m0_46073932/article/details/104088341
今日推荐