Python-基础数据测试-案例

32 阅读2分钟

数据结构混转

# 元祖 - 数据类型
print("--元祖数据类型--")
dogs = ('tom', 'jim', 'dou')
print(type(dogs))
# 元祖不允许修改
# dogs[1] = 'tom2'
for dog in dogs:
    # dog = 'xiaomi'
    print(dog)

# 列表 - 数据类型
print("--列表数据类型--")
cats = ['大众', '一汽', '伯爵']
# print(cats[1])
for cat in cats:
    print(cat)

# while循环
print("--while循环--")
count = 10
while count > 3:
    count -= 1
    print(count)

# 字典转列表
print("--字典转列表--")
array1 = {'name': '张三', 'age': '45', 'sex': '男'}
array2 = []
array3 = []
for key, value in array1.items():
    array2.append(key)
    array3.append(value)

print(array1)
print(array2)
print(array3)

# 列表赋值
print("--列表赋值--")
james_list = ['james', 'tim', 'lisa']
lisa_list = []
tim_list = []
while james_list:
    last_elem = james_list.pop()
    lisa_list.append(last_elem)
print(lisa_list)
lisa_list.reverse()
print(lisa_list)
lisa_list.sort()
print(lisa_list)

时间类操作

# 时间对象
import datetime
import time

print(time.sleep(0.1))
print(time.time())
print(time.thread_time())
print(time.asctime(time.localtime(time.time())))

print('--格式化时间--')
# time.strptime() //字符串转换为时间类型
# time.
try:
    t = time.strptime('2024-10-23 12:23:23', '%Y-%m-%d %H:%M:%S')
    print(type(t), t)
except Exception as e:
    print(e)
    pass


try:
    t = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
    print(type(t), t)
except Exception as e:
    print(e)
    pass

print(datetime.datetime.now())
print(str(datetime.datetime.now()).split(".")[0])
print(datetime.date.today())

线程类举例

import threading
import time
from concurrent.futures import thread

"""
线程类
Python通过两个标准库thread和threading提供对线程的支持。thread提供了低级别的、原始的线程以及一个简单的锁。
thread 模块提供的其他方法:
threading.currentThread(): 返回当前的线程变量。
threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
除了使用方法外,线程模块同样提供了Thread类来处理线程,Thread类提供了以下方法:
run(): 用以表示线程活动的方法。
start():启动线程活动。
join([time]): 等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。
isAlive(): 返回线程是否活动的。
getName(): 返回线程名。
setName(): 设置线程名。
"""

print ("%s:%s" % ("正在执行 ", threading.current_thread().name))
# print(threading.current_thread().start())
print(threading.current_thread().getName())

exitFlag = 0

def print_time(threadName, delay, counter):
    while counter:
        if exitFlag:
            thread.exit()
        time.sleep(delay)
        print ("%s: %s" % (threadName, time.ctime(time.time())))
        counter -= 1


class myThread (threading.Thread):   #继承父类threading.Thread
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
    def run(self):                   #把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
        print ("Starting " + self.name)
        print_time(self.name, self.counter, 5)
        print ("Exiting " + self.name)

# 创建新线程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)

# 开启线程
thread1.start()
thread2.start()

print ("Exiting Main Thread")
```
```