import os
import sys
import re
from UserDict import UserDict
_WHITESPACE_REGEXP = re.compile('\s+')
if os.sep == '\\':
_CASE_INSENSITIVE_FILESYSTEM = True
else:
try:
_CASE_INSENSITIVE_FILESYSTEM = os.listdir('/tmp') == os.listdir('/TMP')
except OSError:
_CASE_INSENSITIVE_FILESYSTEM = False
def normalize(string, ignore=[], caseless=True, spaceless=True):
if spaceless:
string = _WHITESPACE_REGEXP.sub('', string)
if caseless:
string = string.lower()
ignore = [ ign.lower() for ign in ignore ]
for ign in ignore:
string = string.replace(ign, '')
return string
def normalize_tags(tags):
"""Removes duplicates (normalized) and empty tags and sorts tags"""
ret = []
dupes = NormalizedDict({'': 1})
for tag in tags:
if not dupes.has_key(tag):
ret.append(tag)
dupes[tag] = 1
ret.sort(lambda x, y: cmp(normalize(x), normalize(y)))
return ret
def normpath(path, normcase=True):
"""Returns path in normalized and absolute format.
On case-insensitive file systems the path is also casenormalized
(if normcase is True).
"""
path = _abspath(path)
if normcase and _CASE_INSENSITIVE_FILESYSTEM:
path = path.lower()
return path
def _abspath(path):
pathlen = len(path)
# Return 'x:\' both when the given path is 'x:\' and 'x:'. Notice that
# os.path.abspath('x:') returns the current dir (we don't want that) and
# with Jython os.path.abspath('x:\') returns 'x:' (don't want that either)
if os.sep == '\\' and pathlen > 1 and path[1] == ':':
if pathlen == 2:
return path + '\\'
if pathlen == 3:
return path
return os.path.abspath(path)
class NormalizedDict(UserDict):
def __init__(self, initial={}, ignore=[], caseless=True, spaceless=True):
UserDict.__init__(self)
self._keys = {}
self._normalize = lambda s: normalize(s, ignore, caseless, spaceless)
for key, value in initial.items():
self[key] = value
def __setitem__(self, key, value):
nkey = self._normalize(key)
self._keys.setdefault(nkey, key)
self.data[nkey] = value
set = __setitem__
def __getitem__(self, key):
return self.data[self._normalize(key)]
def __delitem__(self, key):
nkey = self._normalize(key)
del self.data[nkey]
del self._keys[nkey]
def get(self, key, default=None):
try:
return self.__getitem__(key)
except KeyError:
return default
def has_key(self, key):
return self.data.has_key(self._normalize(key))
__contains__ = has_key
def keys(self):
return self._keys.values()
def items(self):
return [ (key, self[key]) for key in self.keys() ]
def copy(self):
copy = UserDict.copy(self)
copy._keys = self._keys.copy()
return copy
文件路径:C:\Python26\lib\site-packages\robot\normalizing.py
功能:去掉多余字符的正则表达式匹配;字典的构建。此函数中包含了lambda,比较晦涩,尚未完全理解。
_WHITESPACE_REGEXP = re.compile('\s+'):编译去掉空白字符的匹配模型。
根据操作系统决定是否需要区分大小写。
normalize函数:把列表ignore中的所有内容替换为空格。先根据条件去掉空格,决定是否区分大小写。
normalize_tags函数:移除重复的tag并排序。ret.sort(lambda x, y: cmp(normalize(x), normalize(y)))中的x,y是从哪里传入的?
normpath函数返回标准路径,如果不区分大小写,全部转为小写。
_abspath函数为路径名的处理,针对windiws平台为主,暂不深究。
类NormalizedDict:构建了一个字典,这个地方待深入的是为什么不使用默认的字典。
阅读(20699) | 评论(0) | 转发(0) |