Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168698
Image: ubuntu2004
def tokenizeGLM(GLM): import re file = open(DATA+GLM) data = file.read() # Get rid of http for stylesheets because we don't need it and it conflicts with comment syntax. data = re.sub(r'http:\/\/', '', data) # Strip comments. data = re.sub(r'\/\/.*\n', '', data) # TODO: If the .glm creator has been lax with semicolons, add them back. # Also strip non-single whitespace because it's only for humans: data = data.replace('\n','').replace('\r','').replace('\t',' ') # Tokenize around semicolons and whitespace. tokenized = re.split(r'(;|\s)',data) # Get rid of whitespace strings. basicList = filter(lambda x:x!='' and x!=' ', tokenized) return basicList
staticToken = tokenizeGLM('Simple_System.glm')
def gatherDeepestNest(tokenizedGLM): #Helper function: backtrack from an open brace to get all the input stuff. def grabHeader(x, tokenizedGLM): pointer = x while tokenizedGLM[pointer] != ';': if pointer == 0: return pointer else: pointer += -1 return pointer + 1 # Function starts here. maxLen = len(tokenizedGLM) startRange = None accum = [] for x in xrange(0, maxLen): if tokenizedGLM[x] == '{': startRange = grabHeader(x, tokenizedGLM) if tokenizedGLM[x] == '}' and startRange != None: accum += [[startRange, x+2]] startRange = None return accum
def objectToDict(tokenList): stack = list(tokenList) bracket = tokenList.index('{') head = tokenList[0:bracket] tail = tokenList[bracket+1:-2] accumDict = {} # Process the head. if len(head) == 1: accumDict['type'] = head[0] elif len(head) == 2: accumDict['type'] = head[1] else: accumDict['type'] = head[2] # Process the tail. # TODO: enhance this to cover the case where we have already dictified something. tailStack = [] # We need to keep track of how many objects of each type so we don't clobber their keys. typeDict = {} while len(tail) > 0: current = tail.pop(0) if type(current) is dict: key = current['type'] # Update our type dictionary. if key in typeDict: typeDict[key] += 1 # We've seen this object before, so append a version number to our outKey. outKey = key + str(typeDict[key]) else: typeDict[key] = 0 outKey = key # Write out our dictionary. accumDict[outKey] = current elif current != ';': tailStack += [current] else: accumDict[tailStack.pop(0)] = reduce(lambda x,y:str(x)+' '+str(y), tailStack) tailStack = [] return accumDict
# TODO: Debug. def dictifyDeepest(tokenizedGLM): outList = list(tokenizedGLM) deepCoords = gatherDeepestNest(tokenizedGLM) adjustment = 0 for x in deepCoords: start = x[0] - adjustment end = x[1] - adjustment outList[start] = objectToDict(outList[start:end]) del outList[start+1:end] adjustment += end - start - 1 return outList
objectToDict(['object', 'stubauction', '{', 'name', 'Market_1', ';', 'period', '300', ';', {'type': 'player', 'loop': '365', 'file': 'Price.player'}, '}', ';'])
{'player': {'type': 'player', 'file': 'Price.player', 'loop': '365'}, 'type': 'stubauction', 'name': 'name Market_1', 'period': 'period 300'}
gatherDeepestNest(staticToken)
[[0, 15], [42, 50], [50, 61], [73, 87], [89, 104], [104, 154], [160, 172], [172, 184], [184, 196], [204, 239], [239, 268], [268, 294], [294, 314], [314, 337], [352, 380], [400, 428], [433, 447], [447, 461], [461, 484], [484, 507], [507, 521], [521, 535], [595, 628], [628, 657], [657, 686], [686, 715], [715, 744], [803, 836], [836, 865], [865, 894], [894, 923], [923, 952], [954, 974], [974, 994], [994, 1014], [1014, 1034]]
firstLevObjects = map(lambda x:objectToDict(staticToken[x[0]:x[1]]),gatherDeepestNest(staticToken)) map(lambda x:x['type'],firstLevObjects)
['clock', 'residential', 'powerflow', 'player', 'climate', 'regulator_configuration', 'triplex_line_conductor', 'triplex_line_conductor', 'triplex_line_conductor', 'transformer_configuration', 'transformer_configuration', 'node', 'regulator', 'node', 'transformer_configuration', 'transformer_configuration', 'triplex_node', 'triplex_meter', 'triplex_line', 'triplex_line', 'triplex_meter', 'triplex_meter', 'waterheater', 'ZIPload', 'ZIPload', 'ZIPload', 'ZIPload', 'waterheater', 'ZIPload', 'ZIPload', 'ZIPload', 'ZIPload', 'recorder', 'recorder', 'recorder', 'recorder']