Python學習第十四天

#!/usr/bin/pythonpython

#filename:test14.pygit


#f = open('test.txt','wb+')app


#from urllib.request import urlopenurl

#with urlopen('http://www.baidu.com') as response:spa

#for line in response:code

# f.write(line)orm

#line = line.decode('utf-8')utf-8

#print(line)ci


#f.closeinput


from datetime import date

now = date.today()

print(now.strftime("%Y-%m-%d"))


birthday = date(1988,11,21)

age = now - birthday

print(age.days)


import zlib

s = b'witch which has which witches wrist watch'

print(len(s))

t= zlib.compress(s)

print(len(t))

print(zlib.decompress(t))

print(zlib.crc32(s))


from timeit import Timer

print(Timer('t=a;a=b;b=t','a=1;b=2').timeit())

print(Timer('a,b=b,a','a=1;b=2').timeit())


def average(values):

return sum(values)/len(values)



import unittest

class TestStatisticalFunctions(unittest.TestCase):

def test_average(self):

self.assertEqual(average([20,30,70]),40)

self.assertEqual(round(average([1,5,7]),1),4.3)

with self.assertRaises(ZeroDivisionError):

average([])

with self.assertRaises(TypeError):

average(20,30,70)


#unittest.main()


import reprlib

ww = reprlib.repr(set('supercalifragilisticexpialidocious'))

print(ww)



import pprint

t = [[[['black','cyan'],'white',['green','red']],[['magenta','yellow'],'blue']]]

pprint.pprint(t,width=40)


import textwrap

doc = """The wrap() method is just like fill() except that it returns a list of strings instead of one big string with newlines to separate the wrapped lines."""

print(textwrap.fill(doc,width=40))


import locale

print(locale.setlocale(locale.LC_ALL,'English_United States.1252'))

conv = locale.localeconv()

print(conv)

x = 1234567.8

print(locale.format("%d",x,grouping=True))

print(locale.format_string("%s%.*f",(conv['currency_symbol'],conv['frac_digits'],x),grouping=True))


from string import Template

t = Template('${village}folk send $$10 to $cause.')

tt= t.substitute(village='Mottingham',cause='the ditch fund')

print(tt)


t = Template('Return the $item to $ower.')

d = dict(item='unladen swallow')

#t.substitute(d) 

t.safe_substitute(d)


import time,os.path

photofiles = ['img_1074.jpg','img_1076.jpg','img_1077.jpg']


class BatchRename(Template):

delimiter = '%'

fmt = input('Enter rename style (%d-date %n-seqnum %f-format):')

t = BatchRename(fmt)

date = time.strftime('%d%b%y')

for i,filename in enumerate(photofiles):

base,ext = os.path.splitext(filename)

newname = t.substitute(d=date,n=i,f=ext)

print('{0} --> {1}'.format(filename,newname))

相關文章
相關標籤/搜索