.


:




:

































 

 

 

 


import




, , . ( import), Python . , , sys.path. , . ( .)

>>> import sys ①
>>> sys.path ②
['',
'/usr/lib/python31.zip',
'/usr/lib/python3.1',
'/usr/lib/python3.1/plat-linux2@EXTRAMACHDEPPATH@',
'/usr/lib/python3.1/lib-dynload',
'/usr/lib/python3.1/dist-packages',
'/usr/local/lib/python3.1/dist-packages']
>>> sys ③
<module 'sys' (built-in)>
>>> sys.path.insert(0, '/home/mark/diveintopython3/examples') ④
>>> sys.path ⑤
['/home/mark/diveintopython3/examples',
'',
'/usr/lib/python31.zip',
'/usr/lib/python3.1',
'/usr/lib/python3.1/plat-linux2@EXTRAMACHDEPPATH@',
'/usr/lib/python3.1/lib-dynload',
'/usr/lib/python3.1/dist-packages',
'/usr/local/lib/python3.1/dist-packages']

sys .
sys.path , . ( , , Python , .) Python ( ) .py, , .
- ; , .py. , , , sys, ; Python. , , , Python! ( sys .)
, sys.path, Python, Python , . Python.
sys.path.insert(0, _), sys.path, , , . , . (, Python 2- , 3) , , , Python.

, , Python , . , Python, .

Python :

>>> import humansize ①
>>> print(humansize.approximate_size(4096, True)) ②
4.0 KiB
>>> print(humansize.approximate_size.__doc__) ③
.

:
size --
a_kilobyte_is_1024_bytes -- True ( ), 1024
False, 1000

: (string)

humansize , Python-. , , , . , , Python. , .
, , . approximate_size, humansize.approximate_size. Java, .
, ( , , ), __doc__.

 

import Python require Perl. import Python, .; require Perl, ::.

?

Python , . __doc__, , . sys , ( ) path. .

: ? -. , . , . Python . , , . . , .

, . Python . . . . , .

, , : Python . . . . . . .

Python begin end, , , , . (:) .

def approximate_size(size, a_kilobyte_is_1024_bytes=True): ①
if size < 0: ②
raise ValueError(' ') ③

multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
for suffix in SUFFIXES[multiple]: ⑤
size /= multiple
if size < multiple:
return '{0:.1f} {1}'.format(size, suffix)

raise ValueError(' ')

. , if, for while . . , . , . , , . . , . .
if . , , , else ( ). , .
if. raise ( ValueError), size < 0.
. . , . .
for . , , . for . . , !

, , . , Python , , . Python, .

Python , . C++ Java .

(. exceptions , , ) Python. Python , Python . .

? , , - . ( , .) , . Python , .

Python, - , , . . , , , Python, . , , , . , , , .

Java, Python , . , .

. . - (, , ), , . , . , . , . , - , try...except.

Python try...except raise . Java C++ try...catch throw .

approximate_size() : (size) , , .

if size < 0:
raise ValueError(' ')

. raise, , . . ( , , raise ValueError ' '. !)

, . , , , , , . . . , , Python (. traceback) . , , , , , .

Python ImportError ( ), , . , , import. . , chardet . , , , , . try...except.

try: import chardet except ImportError: chardet = None

chardet if:

if chardet:
# -
else:
#

ImportError , (API), (, ). , , . , XML , API, ElementTree API. lxml , . xml.etree.ElementTree , Python 3.

try:
from lxml import etree
except ImportError:
import xml.etree.ElementTree as etree

try...except etree. API, , . etree, if .

approximate_size():

multiple = 1024 if a_kilobyte_is_1024_bytes else 1000

multiple (), . , Python . , , . , NameError ( ).

>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> x = 1
>>> x
1

:

( ):

"<stdin>", 1, <>

NameError: 'x'

Python .





:


: 2016-11-18; !; : 443 |


:

:

.
==> ...

1273 - | 1256 -


© 2015-2024 lektsii.org - -

: 0.02 .