28.01.2015 Views

Tutorial Python - Starship

Tutorial Python - Starship

Tutorial Python - Starship

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

# datele sunt usor de construit si formatat<br />

>>> from datetime import date<br />

>>> now = date.today()<br />

>>> now<br />

datetime.date(2003, 12, 2)<br />

>>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")<br />

’12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.’<br />

# datele suporta artmetica de tip calendar<br />

>>> birthday = date(1964, 7, 31)<br />

>>> age = now - birthday<br />

>>> age.days<br />

14368<br />

10.9 Compresarea datelor<br />

Formatele de arhivăre şi compresare obişnuite sunt accesate direct prin includerea modululelor zlib, gzip,<br />

bz2, zipfile şi tarfile.<br />

>>> import zlib<br />

>>> s = ’witch which has which witches wrist watch’<br />

>>> len(s)<br />

41<br />

>>> t = zlib.compress(s)<br />

>>> len(t)<br />

37<br />

>>> zlib.decompress(t)<br />

’witch which has which witches wrist watch’<br />

>>> zlib.crc32(t)<br />

-1438085031<br />

10.10 Măsurarea performanţei<br />

Unii utilizatoru <strong>Python</strong> sunt interesaţi de cunoaşterea diferenţelor de performanţă între diferite abordări ale<br />

aceleiaşi probleme.<strong>Python</strong> ofera instrumente de măsură, care răspunde rapid cererilor.<br />

Exista tentaţia foloseriii impachetării şi a despachetării perechilor (tuple) in locul obişnuitei schimbări intre argumentelor.<br />

Modulul timeit evidenţiază imediat un mic avantaj de performanţă:<br />

>>> from timeit import Timer<br />

>>> Timer(’t=a; a=b; b=t’, ’a=1; b=2’).timeit()<br />

0.57535828626024577<br />

>>> Timer(’a,b = b,a’, ’a=1; b=2’).timeit()<br />

0.54962537085770791<br />

În contrast cu timeit, care operează pe coduri de foarte mici dimensiuni, modulele profile si pstats au<br />

instrumente pentru identificarea secţinii de timp critic pentru aplicatii de mari dimensiuni.<br />

72 Capitolul 10. Pe scurt despre Standard Library - partea I

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!