【python中sys.argv参数用法实例分析】教程文章相关的互联网学习教程文章

pythontime模块用法实例详解

本文详细讲述了python的内嵌time模块的用法。分享给大家供大家参考之用。具体分析如下: 一、简介 time模块提供各种操作时间的函数 说明:一般有两种表示时间的方式: 第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的 第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同 year (four digits, e.g. 1998) month (1-12) day (1-31) hours ...

Python中unittest用法实例

本文实例讲述了Python中unittest的用法,分享给大家供大家参考。具体用法分析如下: 1. unittest module包含了编写运行unittest的功能,自定义的test class都要集成unitest.TestCase类,test method要以test开头,运行顺序根据test method的名字排序,特殊方法: ① setup():每个测试函数运行前运行 ② teardown():每个测试函数运行完后执行 ③ setUpClass():必须使用@classmethod 装饰器,所有test运行前运行一次 ④ tearDownClass(...

python中pycurl库的用法实例

本文实例讲述了python中pycurl库的用法,分享给大家供大家参考。 该实例代码实现从指定网址读取网页,主要是pycurl库的使用。 具体实现方法如下:#定义一个类 class CallBack:"""for pycurl """def __init__(self):"""Constructor"""self.data = ""def func(self, data):self.data = self.data + datadef urls(md5, location="", option={}):c = pycurl.Curl()f = CallBack()c.setopt(pycurl.URL, "http://XXXXXX/getUrl.php?key=...

python中sets模块的用法实例

本文实例简单讲述了python中sets模块的用法,分享给大家供大家参考。 具体方法如下:import sets magic_chars = sets.Set(abracadabra) print magic_chars poping_chars = sets.Set(supercalifragilisticeexpialidocious) print poping_chars print "".join(magic_chars & poping_chars)程序运行结果如下:Set([a, r, b, c, d]) Set([a, c, e, d, g, f, i, l, o, p, s, r, u, t, x]) acrd希望本文所述对大家的Python程序设计有...

Python中logging模块的用法实例

本文实例讲述了logging模块的用法实例,分享给大家供大家参考。具体方法如下:import logging import os log = logging.getLogger() formatter = logging.Formatter([%(asctime)s] [%(name)s] %(levelname)s: %(message)s) stream_handler = logging.StreamHandler() file_handler = logging.FileHandler(os.path.join("c:\\", "analysis.log")) file_handler.setFormatter(formatter) stream_handler.setFormatter(formatter)...

python中pygame模块用法实例【图】

本文实例讲述了python中pygame模块用法,分享给大家供大家参考。具体方法如下:import pygame, sys from pygame.locals import * #set up pygame pygame.init() windowSurface = pygame.display.set_mode((500, 400), 0, 32) pygame.display.set_caption("hello, world") BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) basicFont = pygame.font.SysFont(None, 48) ...

Python中asyncore的用法实例

本文实例讲述了python中asyncore模块的用法,分享给大家供大家参考。具体方法如下: 实例代码如下:##asyncore import asyncore,socket ######################################################################## class AsyncGet(asyncore.dispatcher): """ the defined class """ #---------------------------------------------------------------------- def __init__(self, host): """Constructor""" asyncore.dispatcher._...

python中元类用法实例

本文实例讲述了python中元类用法,分享给大家供大家参考。具体方法分析如下: 1.元类(metaclass)是用来创建类的类 2.type(object):返回一个对象的类型,与object.__class__的值相同,type(name,bases,dict):创建一个新的type类型,name就是新class的name,值存到__name__属性中,bases是tuple类型,值会存到__bases__中,dict的值存到__dict__中代码如下:class X: ... a = 1 ... X = type(X, (object,), dict(a=1)) 3.类默认是...

Python列表list数组array用法实例解析

本文以实例形式详细讲述了Python列表list数组array用法。分享给大家供大家参考。具体如下:Python中的列表(list)类似于C#中的可变数组(ArrayList),用于顺序存储结构。 创建列表 代码如下:sample_list = [a,1,(a,b)] Python 列表操作代码如下:sample_list = [a,b,0,1,3] 得到列表中的某一个值 代码如下:value_start = sample_list[0] end_value = sample_list[-1] 删除列表的第一个值 代码如下:del sample_list[0] 在列表中插入...

python中MySQLdb模块用法实例

本文实例讲述了python中MySQLdb模块用法。分享给大家供大家参考。具体用法分析如下: MySQLdb其实有点像php或asp中连接数据库的一个模式了,只是MySQLdb是针对mysql连接了接口,我们可以在python中连接MySQLdb来实现数据的各种操作。 python连接mysql的方案有oursql、PyMySQL、 myconnpy、MySQL Connector 等,不过本篇要说的确是另外一个类库MySQLdb,MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范...

Python装饰器decorator用法实例

本文实例讲述了Python装饰器decorator用法。分享给大家供大家参考。具体分析如下: 1. 闭包(closure) 闭包是Python所支持的一种特性,它让在非global scope定义的函数可以引用其外围空间中的变量,这些外围空间中被引用的变量叫做这个函数的环境变量。环境变量和这个非全局函数一起构成了闭包。代码如下:def outer(x):y = [1,2,3]def inner():print xprint yreturn inner x = 5 #这个x没有被引用 f = outer(2) f() print f.__...

python中list循环语句用法实例

本文实例讲述了python中list循环语句用法。分享给大家供大家参考。具体用法分析如下: Python 的强大特性之一就是其对 list 的解析,它提供一种紧凑的方法,可以通过对 list 中的每个元素应用一个函数,从而将一个 list 映射为另一个 list。 实例代码如下:a = [cat, window, defenestrate] for x in a:print x, len(x) for x in [1, 2, 3]: print x, # iteration Loop through a list: for in a = [cat, window, defenestrat...

python多线程threading.Lock锁用法实例

本文实例讲述了python多线程threading.Lock锁的用法实例,分享给大家供大家参考。具体分析如下: python的锁可以独立提取出来代码如下:mutex = threading.Lock() #锁的使用 #创建锁 mutex = threading.Lock() #锁定 mutex.acquire([timeout]) #释放 mutex.release() 锁定方法acquire可以有一个超时时间的可选参数timeout。如果设定了timeout,则在超时后通过返回值可以判断是否得到了锁,从而可以进行一些其他的处理。代码如下:#!/...

python中urllib模块用法实例详解

本文实例讲述了python中urllib模块用法。分享给大家供大家参考。具体分析如下: 一、问题: 近期公司项目的需求是根据客户提供的api,我们定时去获取数据, 之前的方案是用php收集任务存入到redis队列,然后在linux下做一个常驻进程跑某一个php文件, 该php文件就一个无限循环,判断redis队列,有就执行,没有就break. 二、解决方法: 最近刚好学了一下python, python的urllib模块或许比php的curl更快,而且简单. 贴一下代码代码如...

Python的批量远程管理和部署工具Fabric用法实例

本文实例讲述了Python的批量远程管理和部署工具Fabric用法。分享给大家供大家参考。具体如下: Fabric是Python中一个非常强大的批量远程管理和部署工具,常用于在多个远程PC上批量执行SSH任务. 常见的使用方法大概总结如下: 1. 首先,要将批量执行的任务写入到一个fabfile.py中,代码如下:# -*- coding:utf-8 -*- from fabric.api import run, local, roles, env, cd env.hosts=[ 192.168.1.110, 192.168.1.111, 192.168.1.112...