<commit_before>'''
Created on 25.10.2010

@author: michi
'''
from ems.converter.tag import Tag,AttributeException

class ValueOf(Tag):
    '''
    classdocs
    '''
    parsedXPaths = []
    
    def interpret(self,xmlDict,inputReader,outputWriter):
        
        if xmlDict['attributes'].has_key('select'):
            select = xmlDict['attributes']['select']
            untypedValue = None
            #self.parsedXPaths.append(select)
            #variable
            if select.startswith('$'):
                untypedValue = self.converter.getVar(select[1:])
            #constant
            elif select.startswith("'"):
                untypedValue = select[1:-1]
            #ask writer
            elif select.startswith('writer:'):
                untypedValue = outputWriter.select(select[7:])
            #nested xpath query
            elif select.startswith('{') and select.endswith('}'):
                tmpDict = {'attributes':{'select':select[1:-1]}}
                value = self.interpret(tmpDict, inputReader, outputWriter)
                newDict = {'attributes':{'select':value}}
                untypedValue =  self.interpret(newDict, inputReader, outputWriter)
            else:
                untypedValue = inputReader.select(select)
            
            if not xmlDict['attributes'].has_key('type'):
                return untypedValue
            
            if xmlDict['attributes']['type'] == 'bool':
                try:
                    return bool(untypedValue)
                except ValueError:
                    return False
            elif xmlDict['attributes']['type'] == 'int':
                try:
                    return int(untypedValue)
                except ValueError:
                    return 0
            elif xmlDict['attributes']['type'] in ('float','double'):
                try:
                    return float(untypedValue)
                except ValueError:
                    return 0.0
            elif xmlDict['attributes']['type'] == 'string':
                return unicode(untypedValue)
            return untypedValue
            
        else:
            raise AttributeException(
                    "The tag value-of needs a select attribute")
    
    def __str__(self):
        return "value-of"
    
    def notify(self,eventType):
        if eventType == self.startProcess:
#            print "startProcess"
            self.parsedXPaths = []<commit_msg>Float akzeptiert nun auch Kommas als Nachstellentrenner<commit_after>'''
Created on 25.10.2010

@author: michi
'''
from ems.converter.tag import Tag,AttributeException

class ValueOf(Tag):
    '''
    classdocs
    '''
    parsedXPaths = []
    
    def interpret(self,xmlDict,inputReader,outputWriter):
        
        if xmlDict['attributes'].has_key('select'):
            select = xmlDict['attributes']['select']
            untypedValue = None
            #self.parsedXPaths.append(select)
            #variable
            if select.startswith('$'):
                untypedValue = self.converter.getVar(select[1:])
            #constant
            elif select.startswith("'"):
                untypedValue = select[1:-1]
            #ask writer
            elif select.startswith('writer:'):
                untypedValue = outputWriter.select(select[7:])
            #nested xpath query
            elif select.startswith('{') and select.endswith('}'):
                tmpDict = {'attributes':{'select':select[1:-1]}}
                value = self.interpret(tmpDict, inputReader, outputWriter)
                newDict = {'attributes':{'select':value}}
                untypedValue =  self.interpret(newDict, inputReader, outputWriter)
            else:
                untypedValue = inputReader.select(select)
            
            if not xmlDict['attributes'].has_key('type'):
                return untypedValue
            
            if xmlDict['attributes']['type'] == 'bool':
                try:
                    return bool(int(untypedValue)) # bool('0') == True
                except ValueError:
                    return False
            elif xmlDict['attributes']['type'] == 'int':
                try:
                    return int(untypedValue)
                except ValueError:
                    return 0
            elif xmlDict['attributes']['type'] in ('float','double'):
                try:
                    if isinstance(untypedValue, basestring):
                        untypedValue = untypedValue.replace(',','.')
                    return float(untypedValue)
                except ValueError:
                    return 0.0
            elif xmlDict['attributes']['type'] == 'string':
                return unicode(untypedValue)
            return untypedValue
            
        else:
            raise AttributeException(
                    "The tag value-of needs a select attribute")
    
    def __str__(self):
        return "value-of"
    
    def notify(self,eventType):
        if eventType == self.startProcess:
#            print "startProcess"
            self.parsedXPaths = []
{'fraction_non_alphanumeric': 0.10960518562168532, 'fraction_numerical': 0.006874877234335101, 'mean_word_length': 1.60460358056266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '740288', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1260, 'n_words': 265}