コード例 #1
0
def tryCache(filename, methodArgs=[], methodParams={}, dependencies=[]):
    """
    Determines whether the cached object is still fresh (if one exists),
    and if so returns that object. Otherwise returns None.
    
    @param filename: The filename to look for a cached entry in.
    @param methodArgs: The arguments passed to the method we're trying to
        cache.
    @param methodParams: As for methodArgs, but dictionary arguments. 
    @return: None or a stored value
    """
    if needsUpdate(filename, dependencies):
        return None

    try:
        iStream = common.sopen(filename, 'r', encoding=None)
        storedArgs = pickle.load(iStream)
        storedParams = pickle.load(iStream)

        if storedArgs == methodArgs and storedParams == methodParams:
            obj = pickle.load(iStream)
            iStream.close()
            return obj
        else:
            iStream.close()
            return None
    except:
        # could get several errors here:
        # - badly pickled file
        # - changed local modules when loading pickled value
        # - filesystem permissions or problems
        return None
コード例 #2
0
def writeList(outList, filename):
    """ write the list to file
    """
    outFile = sopen(filename, 'w')
    for word in outList:
        print >> outFile, word
    outFile.close()
コード例 #3
0
def writeDict(outDict, filename, sep=' '):
    """ write the dict to file
    """
    outFile = sopen(filename)
    for word in outDict.keys():
        print >> outFile, word + sep + outDict[word]
    outFile.close()
コード例 #4
0
def write_list(line_list, fname, enc):
    """ 把列表写入文件
    """
    ostream = sopen(fname, 'wb', enc)
    for line in line_list:
        print >> ostream, line
    ostream.close()
コード例 #5
0
ファイル: xmlTools.py プロジェクト: larsyencken/code-library
    def __init__(self, filename, baseTag, keyPath):
        """
        Constructor. Takes the given document, and reads in the index for
        that document. If no index exists, then one is created. The
        tagSpec gives the path that should be indexed.
        
        For example, a tagSpec of 'kanji.midashi' indicates that <kanji>
        entries should be indexed, using the contents of the <midashi>
        sub-element as a key.  A tagSpec of 'strokegr:element' indicates
        that <strokegr> entries should be indexed via their element
        attribute.

        @param filename: The document to access via index.
        @param baseTag: The elements to index.
        @param keyPath: The path from within a base tag to use for an
            indexing key.
        """
        self._fileStream = common.sopen(filename, encoding=None)
        self._baseTag = baseTag
        self._keyPath = keyPath
        self._xmlHeader = '<?xml version="1.0" encoding="UTF-8"?>'

        indexFile = '%s.index' % filename
        if os.path.exists(indexFile):
            self._index = pickle.load(open(indexFile, 'r'))
        else:
            self._index = self._generateIndex(filename)
            oStream = open(indexFile, 'w')
            pickle.dump(self._index, oStream)
            oStream.close()

        return
コード例 #6
0
ファイル: xmlTools.py プロジェクト: larsyencken/code-library
    def _generateIndex(self, filename):
        """
        Generates a new index file, using the tagPath and indexType to
        determine what should be indexed and what key should be used.

        @param filename: The file to index.
        @return: A dictionary mapping key to xml snippet.
        """
        index = {}

        data = common.sopen(filename, encoding=None).read()
        tagPattern = re.compile(
                '<%s[^>]*>.*?</%s>' % (self._baseTag, self._baseTag),
                re.UNICODE | re.MULTILINE | re.DOTALL,
            )

        for match in tagPattern.finditer(data):
            # Create a small XML document containing just this tag's data.
            xmlData = match.group()
            doc = minidom.parseString(self._xmlHeader + xmlData)

            # Fetch the key to index this section by.
            keyValue = EvaluateLeaf(self._baseTag + '/' + self._keyPath, doc)

            # Reclaim the memory.
            doc.unlink()

            # Store the location of this entry for later.
            index[keyValue] = match.span()

        return index
コード例 #7
0
def readDict(filename, sep=' '):
    """ read the dict from file
    """
    dictObj = {}
    for key, value in parseLines(sopen(filename), sep, n=2):
        dictObj[key] = value

    return dictObj
コード例 #8
0
ファイル: hanziTable.py プロジェクト: poseidon1214/Neptune
def getRelation(table, fromName, toName):
    """ 读取关系对照表
    """
    relation = Relation(fromName, toName)
    for line in sopen(table):
        if line.startswith('#'):
            continue

        flds = line.rstrip().split()
        chs = flds[0]
        for cht in flds[1:]:
            relation.add(chs, cht)

    return relation
コード例 #9
0
def storeCacheObject(obj, filename, methodArgs=[], methodParams={}):
    """
    Creates a smart cache object in the file.
    
    @param obj: The object to cache.
    @param filename: The location of the cache file.
    @param methodArgs: Any arguments which were passed to the cached
        method.
    @param methodParams: Any keyword parameters passed to the cached
        method. 
    """
    oStream = common.sopen(filename, 'w', encoding=None)
    pickle.dump(methodArgs, oStream, pickle.HIGHEST_PROTOCOL)
    pickle.dump(methodParams, oStream, pickle.HIGHEST_PROTOCOL)
    pickle.dump(obj, oStream, pickle.HIGHEST_PROTOCOL)
    oStream.close()
    
    return
コード例 #10
0
def readList(filename):
    """ return the character list in file
    """
    list = [line.rstrip() for line in filterComments(sopen(filename))]
    return list