• 2020系统综合实践 第五次实践作业


    一.python镜像布置

    1.树形结构

    hello.py--python容器的测试代码
    date.py--日历代码
    op.py--opencv程序代码
    date.py--数据库代码
    test.jpg--测试图片
    test-rotated--opencv运行结果图片
    

    2.dockerfile

    FROM python:3
    MAINTAINER monster<1263199084@qq.com>
    WORKDIR /app
    COPY ./requirements.txt /requirements.txt
    RUN pip install -r /requirements.txt -i https://pypi.douban.com/simple  #修改源并安装依赖
    ENTRYPOINT ["python"]
    CMD ["hello.py"] #默认打开文件
    

    3.requirements

    PyMySQL
    opencv-python
    

    二.Hello World

    -v将本地目录挂载到python工作目录以方便代码修改

    --rm运行完毕后自动移除容器

    vim hello.py
    print('hello world')
    
    sudo docker run -it --rm -v /home/monster/python/app:/app python:test1 hello.py
    

    三.日历输出(参考菜鸟教程)

    vim date.py
    import calendar
     
    # 输入指定年月
    yy = int(input("输入年份: "))
    mm = int(input("输入月份: "))
     
    # 显示日历
    print(calendar.month(yy,mm))
    

    交互式终端需要 -it 指令

    sudo docker run -it --rm -v /home/monster/python/app:/app python:test1 date.py
    

    四.mysql数据库操作(使用实践二中的数据库

    import pymysql
    
    # 打开数据库连接
    db = pymysql.connect("mysql", "docker", "123456", "docker_mysql")
    #创建游标对象
    
    cursor = db.cursor()
    #先查询一次数据库数据
    sql = """select * FROM test"""
    cursor.execute(sql)
    results = cursor.fetchall()
    print(results)
    
    #SQL插入语句
    sql="""insert test(id,name)
    	values(2022,'DEF')"""
    cursor.execute(sql)
    db.commit()
    
    #插入完成后再读取一次数据库数据
    sql = """select * FROM test"""
    cursor.execute(sql)
    results = cursor.fetchall()
    print(results)
    
    # 关闭数据库连接
    db.close()
    
    sudo docker run -it --rm -v /home/monster/python/app:/app --link=mysql:mysql python:test1  db.py
    

    未执行py文件前的docker_mysql数据库中test表

    执行py文件后的docker_mysql数据库中test表

    五.opencv程序

    #做一个图片翻转功能
    import cv2
    img=cv2.imread('test.jpg',flags=1)
    rows,cols=img.shape[:2]
    M=cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
    dst=cv2.warpAffine(img,M,(cols,rows))
    cv2.imwrite("test-rotated.jpg", dst, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
    print('rotated and saved.')
    
     sudo docker run -it --rm -v /home/monster/python/app:/app  python:test1 op.py
    

    六.实验总结

    前后花费大概4小时的时间吧,和实践四相比起来,轻松多了

  • 相关阅读:
    第二节:简单工厂模式(静态工厂模式)
    第一节:不使用设计模式的传统方式
    第三章:设计模式概述
    第二节:类与类之间的关系
    高斯混合模型(GMM)
    随机森林
    LDA主题模型
    Adaboost算法
    线性代数
    k-means聚类
  • 原文地址:https://www.cnblogs.com/ljw1999/p/12906193.html
Copyright © 2020-2023  润新知