This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Разбор XML формата | |
""" | |
from xml.dom.minidom import parse, parseString | |
dom1 = parse( "example.xml" ) # parse an XML file | |
dom2 = parseString( "<myxml>Some data <empty/> some more data</myxml>" ) | |
print dom1.toxml() | |
print dom2.toxml() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Вычисление хэша | |
""" | |
import hashlib | |
m = hashlib.md5() | |
m.update("Nobody inspects") | |
m.update(" the spammish repetition") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Постоянное хранилище языковых объектов | |
""" | |
import shelve | |
d = shelve.open(filename) # open -- file may get suffix added by low-level library | |
d[key] = data # store data at key (overwrites old data if using an existing key) | |
data = d[key] # retrieve a COPY of data at key (raise KeyError if no such key) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Сравнение текстов | |
""" | |
from difflib import get_close_matches | |
get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy']) | |
import keyword | |
get_close_matches('wheel', keyword.kwlist) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Пути к файлам и каталогам | |
""" | |
import os.path | |
for path in [ '/one/two/three', '/one/two/three/', '/', '.', '']: | |
print '"%s" : "%s"' % (path, os.path.split(path)) | |
print '"%s" : "%s"' % (path, os.path.basename(path)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Аргументы командной строки | |
""" | |
import sys, os | |
print 'sys.argv[0] =', sys.argv[0] | |
pathname = os.path.dirname(sys.argv[0]) | |
print 'path =', pathname | |
print 'full path =', os.path.abspath(pathname) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Поверхностное и глубокое копирование | |
""" | |
import copy | |
listOne = [{"name": "Willie", "city": "Providence, RI"}, 1, "tomato", 3.0] | |
listTwo = listOne[:] | |
listTwo=copy.copy(listOne) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Использование атрибутов, методов и классов модуля | |
""" | |
import library | |
library.sys | |
print library.name | |
print library.func, library.klass | |
print library.__dict__.keys() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Импортирование модуля | |
""" | |
import printer # get module as a whole | |
printer.printer('Hello world!') # qualify to get names |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Перегрузка операторов | |
""" | |
class defaultdict(dict): | |
def __init__(self, default=None): | |
dict.__init__(self) | |
self.default = default |