zoukankan      html  css  js  c++  java
  • python-10 开箱即用

    # 开箱即用
    # 10.1 模块 import

    # 10.1.1 模块就是程序
    # import sys
    # sys.path.append('C:/python') # 添加路径
    # import FU_10_01_hello # 导入一次和多次效果一样

    #import hello
    # 如果要重新加载 可使用模块importlib中的函数reload
    # import importlib
    # importlib.reload(FU_10_01_hello)

    # 10.1.2 模块是用来定义下来

    # 1.在模块中定义函数
    # import FU_10_02_hello2
    # FU_10_02_hello2.hello()

    # 2.在模块中添加测试代码
    # import FU_10_03_hello3
    # FU_10_03_hello3.hello()

    # print(__name__)
    # print(FU_10_03_hello3.__name__)

    # import FU_10_04_hello4
    # FU_10_04_hello4.hello()
    # FU_10_04_hello4.test()

    # 10.1.3 让模块可用
    # 1. 将模块放在正确的位置
    # import pprint, sys
    # pprint.pprint(sys.path) # 在模块sys的变量path中找到目录列表
    #目录site-packages是最佳的选择
    #如果要打印的数据结构太大,一行容纳不下,可使用模块pprint中的函数pprint(而不是普通print语句)。pprint是个卓越的打印函数,能够更妥善地打印输出
    # 2. 告诉解释器到哪里去查找
    #标准做法是将模块所在的目录包含在环境变量PYTHONPATH中。

    # 10.1.4 包 为组织模块,可将其编组为包
    '''
    要创建一个名为drawing的包,其中包含模块shapes和colors,需要创建如表10-1所示的文件和目录(UNIX路径名)。
    ~/python/ PYTHONPATH中的目录
    ~/python/drawing/ 包目录(包drawing)
    ~/python/drawing/__init__.py 包代码(模块drawing)
    ~/python/drawing/colors.py 模块colors
    ~/python/drawing/shapes.py 模块shapes
    '''

    # 10.2 探索模块
    # 10.2.1 模块包含什么
    # import copy
    # 1、使用dir 查看对象的所有属性(包括函数、类、变量)
    # print([n for n in dir(copy) if not n.startswith('_')])
    # 变量
    # print(copy.__all__)

    # 10.2.2 使用help获取帮助
    # print(help(copy.copy))
    # print(copy.copy.__doc__)

    # 10.2.3 文档
    # print(range.__doc__)
    #“Python库参考手册”(https://docs.python.org/library)
    # 在Python网站(https://docs.python.org)
    # 10.2.4 使用源代码
    # print(copy.__file__) # 找到源代码

    # 10.3 标准库:一些深受欢迎的模块
    #10.3.1 sys
    # import sys
    # from math import *
    # print(sys.modules['sqrt'])
    # print(sys.path)
    # print(sys.platform)
    '''
    argv 命令行参数,包括脚本名
    exit([arg]) 退出当前程序,可通过可选参数指定返回值或错误消息
    modules 一个字典,将模块名映射到加载的模块
    path 一个列表,包含要在其中查找模块的目录的名称
    platform 一个平台标识符,如sunos5或win32
    stdin 标准输入流——一个类似于文件的对象
    stdout 标准输出流——一个类似于文件的对象
    stderr 标准错误流——一个类似于文件的对象
    '''
    # 10.3.2 os
    import os
    # for i in os.environ:
    # print('key : {} Values: {}'.format(i, os.environ[i]))
    # print(os.environ['PYTHONPATH'])

    # print(os.system(command= 'execv'))
    # print(os.sep) # 路径名中的分隔符
    # print(os.pathsep) # 用于分割不同的路径名
    # print(repr(os.linesep)) # 用于文本文件中行分隔符
    # print(os.urandom(2)) # 使用随系统而异的“真正”(至少是强加密)随机源
    # environ 包含环境变量的映射
    # system(command) 在子shell中执行操作系统命令
    # sep 路径中使用的分隔符
    # pathsep 分隔不同路径的分隔符
    # linesep 行分隔符(' '、' '或' ')
    # urandom(n) 返回n个字节的强加密随机数据
    # 10.3.3 fileinput

    # input([files[, inplace[, backup]]]) 帮助迭代多个输入流中的行
    # filename() 返回当前文件的名称
    # lineno() 返回(累计的)当前行号
    # filelineno() 返回在当前文件中的行号
    # isfirstline() 检查当前行是否是文件中的第一行
    # isstdin() 检查最后一行是否来自sys.stdin
    # nextfile() 关闭当前文件并移到下一个文件
    # close() 关闭序列

    # 10.3.4 集合、堆和双端队列
    # 1、集合
    # print(set(range(10)))
    # print({0, 1, 2, 0, 1})
    # a = {1, 2, 3}
    # b = {2, 3, 4}
    # print(a.union(b))
    # print(a | b)

    # 2、堆
    # from heapq import *
    # from random import shuffle
    # # print(help(shuffle))
    # data = list(range(10))
    # shuffle(data)
    # heappush(data, 5)
    # heapify(data)
    # print(data)
    # print(heappop(data))
    # print(heappop(data))
    # print(heappop(data))
    # print(data)
    # print(heapreplace(data, 10))
    # print(data)
    # print(nlargest(3,data))
    # print(nsmallest(3, data))
    # heappush(heap, x) 将x压入堆中
    # heappop(heap) 从堆中弹出最小的元素
    # heapify(heap) 让列表具备堆特征
    # heapreplace(heap, x) 弹出最小的元素,并将x压入堆中
    # nlargest(n, iter) 返回iter中n个最大的元素
    # nsmallest(n, iter) 返回iter中n个最小的元素
    # merge(*iterables, key=None, reverse=False):将多个有序的堆合并成一个大的有序堆,然后再输出。
    # heappushpop(heap, item):将item 入堆,然后弹出并返回堆中最小的元素。

    # 3. 双端队列(及其他集合)
    # from collections import deque
    # q = deque(range(5))
    # q.append(5)
    # q.appendleft(6)
    # print(q)
    # print(q.pop())
    # print(q.popleft())
    # print(q)
    # q.rotate(-1)
    # print(q)

    # 10.3.5 time
    import time
    # 1、返回当前时间的时间戳(1970纪元后经过的浮点秒数)
    print(time.time()) # 返回当前时间的时间戳(1970纪元后经过的浮点秒数)
    #2、推迟调用线程的运行秒
    # time.sleep(3)
    # print(time.time())
    #3、接收时间戳(1970纪元后经过的浮点秒数)并返回格林威治天文时间下的时间元组t
    print(time.gmtime())
    #4、 接收时间戳(1970纪元后经过的浮点秒数)并返回当地时间下的时间元组t(t.tm_isdst可取0或1,取决于当地当时是不是夏令时)
    print(time.localtime())

    #5、接收时间元组并返回时间戳
    print(time.mktime(time.gmtime()))
    print(time.mktime(time.localtime()))
    # 6 接收以时间元组,并返回以可读字符串表示当地时间
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
    print(time.strftime('%y-%m-%d %H:%M:%S', time.localtime()))

    # 7、把一个时间字符串解析为时间元组
    print(time.strptime('2019-10-09 11:11:11', '%Y-%m-%d %H:%M:%S'))

    # 8、接受一个时间元组并返回一个可读的形式
    print(time.asctime())

    # 9、作用相当于asctime(localtime(secs)),未给参数相当于asctime()
    print(time.ctime())

    # 10、当地时区(未启动夏令时)距离格林威治的偏移秒数(>0,美洲;<=0大部分欧洲,亚洲,非洲)。
    print(time.timezone)

    #Python日期元组中的字段
    # 索引 字段 值
    # 0 年 如2000、2001等
    # 1 月 范围1~12
    # 2 日 范围1~31
    # 3 时 范围0~23
    # 4 分 范围0~59
    # 5 秒 范围0~61
    # 6 星期 范围0~6,其中0表示星期一
    # 7 儒略日 范围1~366
    # 8 夏令时 0、1或-1

    # 模块time中一些重要的函数
    # asctime([tuple]) 将时间元组转换为字符串
    # localtime([secs]) 将秒数转换为表示当地时间的日期元组
    # mktime(tuple) 将时间元组转换为当地时间
    # sleep(secs) 休眠(什么都不做)secs秒
    # strptime(string[, format]) 将字符串转换为时间元组
    # time() 当前时间(从新纪元开始后的秒数,以UTC为准)



    # 10.3.6 random
    # import random
    # print(random.random()) # 返回一个0-1 的随机函数
    # print(random.getrandbits(3))
    # print(random.uniform(5, 8))
    # print(random.randrange(0, 10, 3))
    # print(range(0, 10, 3))
    # print([x for x in range(0, 10, 3)])
    # a = [x for x in range(0, 10)]
    # print(random.choice(a)) # 从a 中随机选择一个元素
    # random.shuffle(a)
    # print(a)
    # print(random.sample(a, 5)) # 从a 中随机选择五个元素

    # 10.3.7 shelve 和 json
  • 相关阅读:
    转载ORACLE批量绑定FORALL与BULK COLLECT
    Oracle Locking Survival Guide
    转载:TOAD中查看执行计划
    Oracle 9i/10g编程艺术笔记第七章 并发与多版本
    C#调用Oracle存储过程返回多结果集
    转载oracle 字符集查看与修改
    转载:Oracle的优化器(Optimizer)
    Oracle 随笔
    转载:Oracle中SQL语句执行效率的查找与解决
    当查询和设置需要输入Pn时界面的处理方法
  • 原文地址:https://www.cnblogs.com/fuyouqiang/p/11844679.html
Copyright © 2011-2022 走看看