zoukankan      html  css  js  c++  java
  • 包及简单模块

    包是一种通过使用.模块名来组织Python模块名称空间的方式

    包就是一个包含有__init__.py文件的文件夹,所以我们创建包的目的就是为了用文件夹将文件/模块组织起来

    在Python3中,即使包下没有__init__文件,import包仍然不会报错,而在Python2中,包下一定要有该文件否则报错

    2.创建包的目的不是为了运行而是被导入,包只是模块的一种形式,包的本质就是一种模块

    包的本质是一个文件夹,功能越多,模块越多,我们需要文件夹将模块组织起来,提高程序的结构性和可维护性

    单独导入包名称时不会导入包中所有包含的所有子模块

    绝对导入,以执行文件的路径为起始点开始导入称之为绝对导入

      优点:执行文件与被导入的模块都可以使用

      缺点:所有导入都是以sys.path为起始点导入麻烦

    相对导入:以.为起始代替路径只能在一个包中使用不能用于不同目录中

      优点:导入更加简单

      缺点:只能在导入包中的模块才能使用

    1.什么是序列化

      序列化就是将内存中的数据类型转成另外一种格式

      即字典----->序列化------>其他的格式--------------->存到硬盘

      硬盘--------->读取---------->其他格式-------------->反序列化---------------->字典

    2.为什么要序列化?

    持久保存程序的运行状态

    数据的跨平台交互

    2.如何序列化?

    json:

      优点:这种格式是一种通用的格式,所有的编程语言都能识别

      缺点:不能是被所有python类型

      json格式不能识别单引号

    piclke:

      优点:能识别所有Python类型

      缺点;只能被Python这门语言识别

    json使用dumps()方法将数据序列化参数为序列化对象,

    也可以使用dump()方法直接写入文件并序列化,需要的参数是序列化对象及文件对象

    读取json序列化的字符,可以使用load()方法与loads()方法

    其中load()方法参数为文件对象,loads()参数类型字符串

    pickle可以识别python类型

    方法与json相同,不同点是读写都是bytes类型

    时间分为三种模式:

    1.时间戳

    time.time()方法可以得到

    2.格式化的字符串

    time.strftime('%Y-%m-%d %H :%M;%s %p')

    3.结构化的时间对象:

    使用time.localtime()

    时间转换

    时间戳=====>struct_time=========>格式化的字符串

    1 struct_time=time.localtime(time.time())
    2 res=time.strftime('%Y-%m-%d',struct_time)

    格式化的字符串======>struct_time=======>时间戳

    1 struct_time=time.strptime('2017-03-11','%Y-%m-%d')
    2 res=mktime(struct_time))

     1 localtime()
     2 将一个时间戳转换为当前时区的struct_time,参数未提供时默认为当前时间
     3 gmtime()类似于localtime()将时间戳转换为0时区
     4 mktime()将一个struct_time转化为时间戳
     5 time()返回当前时间戳
     6 strftime('%Y-%m-%d %H-%M%S')格式化时间
     7 time.strptime(string[, format])
     8  把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
     9 print(time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X'))
    10 time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6,
    11 tm_wday=3, tm_yday=125, tm_isdst=-1)
    12 在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"13 asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'14  如果没有参数,将会将time.localtime()作为参数传入。
    15 print(time.asctime())#Sun Sep 11 00:43:43 2016
    16 ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为
    17 None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
    18 print(time.ctime())  # Sun Sep 11 00:46:38 2016
    19 print(time.ctime(time.time()))  # Sun Sep 11 00:46:38 2016
    20 import datetime
    万恶的十五字
     1  import random
     2 
     3 print(random.random())#(0,1)----float    大于0且小于1之间的小数
     4 print(random.randint(1,3))  #[1,3]    大于等于1且小于等于3之间的整数
     5 print(random.randrange(1,3)) #[1,3)    大于等于1且小于3之间的整数
     6 print(random.choice([1,'23',[4,5]]))#1或者23或者[4,5]
     7  print(random.sample([1,'23',[4,5]],2))#列表元素任意2个组合
     8 print(random.uniform(1,3))#大于1小于3的小数,如1.927109612082716 
     9 item=[1,3,5,7,9]
    10 random.shuffle(item) #打乱item的顺序,相当于"洗牌"
    11  print(item)
    12 复制代码
    13 import random
    14 def make_code(n):
    15     res=''
    16     for i in range(n):
    17         s1=chr(random.randint(65,90))
    18         s2=str(random.randint(0,9))
    19         res+=random.choice([s1,s2])
    20     return res
    21 
    22 print(make_code(9))
    random
     1 import random
     2  
     3 print(random.random())#(0,1)----float    大于0且小于1之间的小数
     4 
     5 print(random.randint(1,3))  #[1,3]    大于等于1且小于等于3之间的整数
     6  
     7 print(random.randrange(1,3)) #[1,3)    大于等于1且小于3之间的整数
     8  
     9 print(random.choice([1,'23',[4,5]]))#1或者23或者[4,5]
    10  print(random.sample([1,'23',[4,5]],2))#列表元素任意2个组合
    11  
    12 print(random.uniform(1,3))#大于1小于3的小数,如1.927109612082716 
    13 
    14  item=[1,3,5,7,9]
    15 random.shuffle(item) #打乱item的顺序,相当于"洗牌"
    16 print(item)
    17 复制代码
    18 import random
    19 def make_code(n):
    20     res=''
    21     for i in range(n):
    22         s1=chr(random.randint(65,90))
    23         s2=str(random.randint(0,9))
    24         res+=random.choice([s1,s2])
    25     return res
    26 
    27 print(make_code(9))
    这里什么都没有
  • 相关阅读:
    [POI2014]FarmCraft
    [POI2014]Solar Panels
    Luogu P2824 [HEOI2016/TJOI2016]排序
    CF903G Yet Another Maxflow Problem
    CF901C Bipartite Segments
    CF749E Inversions After Shuffle
    ARC068C Snuke Line
    BZOJ3747 [POI2015]Kinoman
    SA-IS
    简单字符串
  • 原文地址:https://www.cnblogs.com/suncunxu/p/10071248.html
Copyright © 2011-2022 走看看