예제 #1
0
def loadHelpData():
    if g.helpLoaded:
        return True

    try:
        with contextlib.closing(
                bz2.BZ2File(getUserDataPath() + os.sep + 'help.pckl.bz2',
                            'rb')) as pickleFile:
            g.helpVersion = pickle.load(pickleFile)
            g.helpTopics = pickle.load(pickleFile)
            g.operatorHelp = pickle.load(pickleFile)
    except FileNotFoundError:
        raise ValueError(
            'rpn:  Unable to load help.  Run "makeHelp" to generate the help data files.'
        )

    try:
        with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'unit_help.pckl.bz2', 'rb' ) ) \
                as pickleFile:
            g.unitTypeDict = pickle.load(pickleFile)
    except FileNotFoundError:
        raise ValueError(
            'rpn:  Unable to load unit help data.  Run "makeHelp" to generate the help data files.'
        )

    g.operatorCategories = set(g.operatorHelp[key][0]
                               for key in g.operatorHelp)

    g.helpLoaded = True

    return True
예제 #2
0
파일: rpn.py 프로젝트: GaelicGrime/rpn
def main():
    checkForPrimeData()

    unitsFile = Path(getUserDataPath() + os.sep + 'units.pckl.bz2')

    g.cwd = os.getcwd()

    os.chdir(getUserDataPath()
             )  # SkyField doesn't like running in the root directory

    if not unitsFile.is_file():
        print(
            'Please run "rpnMakeUnits" (or makeUnits.py) to initialize the unit conversion data files.'
        )
        sys.exit(0)

    helpFile = Path(getUserDataPath() + os.sep + 'help.pckl.bz2')

    if not helpFile.is_file():
        print('Please run "makeHelp" to initialize the help files.')
        sys.exit(0)

    try:
        #for arg in sys.argv:
        #    if arg == '-e':
        #        import profile
        #        profile.run( 'handleOutput( rpn( sys.argv[ 1 : ] ) )' )

        handleOutput(rpn(sys.argv[1:]))

        if g.userVariablesAreDirty:
            saveUserVariablesFile()

        if g.userFunctionsAreDirty:
            saveUserFunctionsFile()

        if g.userConfigurationIsDirty:
            saveUserConfigurationFile()
    except ValueError as error:
        print('\nrpn:  value error:  {0}'.format(error))

        if g.debugMode:
            raise

        sys.exit(0)
    except KeyboardInterrupt:
        print()
        print('handling ctrl-c keyboard interrupt...')
        sys.exit(0)
예제 #3
0
def checkForPrimeData( ):
    primeFile = Path( getUserDataPath( ) + os.sep + 'large_primes.cache' )

    if primeFile.is_file( ):
        g.primeDataAvailable = True
    else:
        g.primeDataAvailable = False
예제 #4
0
def loadLocationCache( ):
    try:
        with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'locations.pckl.bz2', 'rb' ) ) as pickleFile:
            locationCache = pickle.load( pickleFile )
    except FileNotFoundError:
        locationCache = { }

    return locationCache
예제 #5
0
def loadConstants( ):
    try:
        with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'constants.pckl.bz2', 'rb' ) ) as pickleFile:
            constants = pickle.load( pickleFile )
    except FileNotFoundError:
        constants = { }

    return constants
예제 #6
0
def loadConstants():
    try:
        with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'constants.pckl.bz2', 'rb' ) ) \
                as pickleFile:
            constants = pickle.load(pickleFile)
    except FileNotFoundError:
        constants = {}

    return constants
예제 #7
0
def loadUnitConversionMatrix():
    try:
        with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'unit_conversions.pckl.bz2', 'rb' ) ) \
                as pickleFile:
            g.unitConversionMatrix.update(pickle.load(pickleFile))
    except FileNotFoundError:
        print(
            'rpn:  Unable to load unit conversion data.  Run "makeUnits" to generate the unit data files.'
        )
예제 #8
0
파일: rpn.py 프로젝트: ConceptJunkie/rpn
def main( ):
    checkForPrimeData( )

    unitsFile = Path( getUserDataPath( ) + os.sep + 'units.pckl.bz2' )

    os.chdir( getUserDataPath( ) )     # SkyField doesn't like running in the root directory

    if not unitsFile.is_file( ):
        print( 'Please run "makeUnits" to initialize the unit conversion data files.' )
        sys.exit( 0 )

    helpFile = Path( getUserDataPath( ) + os.sep + 'help.pckl.bz2' )

    if not helpFile.is_file( ):
        print( 'Please run "makeHelp" to initialize the help files.' )
        sys.exit( 0 )

    try:
        #for arg in sys.argv:
        #    if arg == '-e':
        #        import profile
        #        profile.run( 'handleOutput( rpn( sys.argv[ 1 : ] ) )' )

        handleOutput( rpn( sys.argv[ 1 : ] ) )

        if g.userVariablesAreDirty:
            saveUserVariablesFile( )

        if g.userFunctionsAreDirty:
            saveUserFunctionsFile( )

        if g.userConfigurationIsDirty:
            saveUserConfigurationFile( )
    except ValueError as error:
        print( '\nrpn:  value error:  {0}'.format( error ) )

        if g.debugMode:
            raise
        else:
            sys.exit( 0 )
    except KeyboardInterrupt:
        print( )
        print( 'handling ctrl-c keyboard interrupt...' )
        sys.exit( 0 )
예제 #9
0
def loadResultOperator():
    try:
        fileName = getUserDataPath() + os.sep + 'result.pckl.bz2'

        with contextlib.closing(bz2.BZ2File(fileName, 'rb')) as pickleFile:
            result = pickle.load(pickleFile)
    except FileNotFoundError:
        result = 0

    return result
예제 #10
0
def saveResult( result ):
    fileName = getUserDataPath( ) + os.sep + 'result.pckl.bz2'

    # TODO:  handle RPNGenerator and RPNMeasurement
    try:
        with DelayedKeyboardInterrupt( ):
            with contextlib.closing( bz2.BZ2File( fileName, 'wb' ) ) as pickleFile:
                pickle.dump( result, pickleFile )
    except:
        pass #print( 'error:  failed to save result' )
예제 #11
0
def loadResult( ):
    try:
        fileName = getUserDataPath( ) + os.sep + 'result.pckl.bz2'

        with contextlib.closing( bz2.BZ2File( fileName, 'rb' ) ) as pickleFile:
            result = pickle.load( pickleFile )
    except FileNotFoundError:
        result = 0

    return result
예제 #12
0
def loadLocationCache():
    try:
        with contextlib.closing(
                bz2.BZ2File(getUserDataPath() + os.sep + 'locations.pckl.bz2',
                            'rb')) as pickleFile:
            locationCache = pickle.load(pickleFile)
    except FileNotFoundError:
        locationCache = {}

    return locationCache
예제 #13
0
def saveResult(result):
    fileName = getUserDataPath() + os.sep + 'result.pckl.bz2'

    # TODO:  handle RPNGenerator and RPNMeasurement
    try:
        with DelayedKeyboardInterrupt():
            with contextlib.closing(bz2.BZ2File(fileName, 'wb')) as pickleFile:
                pickle.dump(result, pickleFile)
    except TypeError:
        #print( 'error:  failed to save result' )
        pass
예제 #14
0
def loadHelpData( ):
    if g.helpLoaded:
        return

    try:
        with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'help.pckl.bz2', 'rb' ) ) as pickleFile:
            g.helpVersion = pickle.load( pickleFile )
            g.helpTopics = pickle.load( pickleFile )
            g.operatorHelp = pickle.load( pickleFile )
    except FileNotFoundError:
        raise ValueError( 'rpn:  Unable to load help.  Run "makeHelp" to generate the help data files.' )

    try:
        with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'unit_help.pckl.bz2', 'rb' ) ) as pickleFile:
            g.unitTypeDict = pickle.load( pickleFile )
    except FileNotFoundError:
        raise ValueError( 'rpn:  Unable to load unit help data.  Run "makeHelp" to generate the help data files.' )

    g.operatorCategories = set( g.operatorHelp[ key ][ 0 ] for key in g.operatorHelp )

    g.helpLoaded = True

    return True
예제 #15
0
def loadUnitNameData( ):
    try:
        with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'unit_names.pckl.bz2', 'rb' ) ) as pickleFile:
            unitsVersion = pickle.load( pickleFile )
            g.unitOperatorNames = pickle.load( pickleFile )
            g.constantOperatorNames = pickle.load( pickleFile )
            g.aliases.update( pickle.load( pickleFile ) )
    except IOError:
        print( 'rpn:  Unable to load unit names.  Run "makeUnits" to generate the unit data files.' )
        return False

    if unitsVersion != PROGRAM_VERSION:
        print( 'rpn:  units data file version mismatch.  Run "makeUnits" to generate the unit data files.' )

    return True
예제 #16
0
def loadUnitNameData():
    try:
        with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'unit_names.pckl.bz2', 'rb' ) ) \
                as pickleFile:
            unitsVersion = pickle.load(pickleFile)
            g.unitOperatorNames = pickle.load(pickleFile)
            g.constantOperatorNames = pickle.load(pickleFile)
            g.aliases.update(pickle.load(pickleFile))
    except IOError:
        print(
            'rpn:  Unable to load unit names.  Run "makeUnits" to generate the unit data files.'
        )
        return False

    if unitsVersion != PROGRAM_VERSION:
        print(
            'rpn:  units data file version mismatch.  Run "makeUnits" to generate the unit data files.'
        )

    return True
예제 #17
0
def getUserFunctionsFileName():
    return getUserDataPath() + os.sep + PROGRAM_NAME + '_user_functions.cfg'
예제 #18
0
def getUserConfigurationFileName():
    return getUserDataPath() + os.sep + PROGRAM_NAME + '_user_config.cfg'
예제 #19
0
def loadFactorCache():
    g.factorCache = PersistentDict(getUserDataPath() + os.sep +
                                   'factors.cache')
예제 #20
0
def getUserVariablesFileName():
    return getUserDataPath() + os.sep + PROGRAM_NAME + '_user_variables.cfg'
예제 #21
0
def saveConstants(constants):
    with DelayedKeyboardInterrupt():
        with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'constants.pckl.bz2', 'wb' ) ) \
                as pickleFile:
            pickle.dump(constants, pickleFile)
예제 #22
0
def getPrimeCacheFileName(name):
    return getUserDataPath() + os.sep + name + '.cache'
예제 #23
0
def saveConstants( constants ):
    with DelayedKeyboardInterrupt( ):
        with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'constants.pckl.bz2', 'wb' ) ) as pickleFile:
            pickle.dump( constants, pickleFile )
예제 #24
0
def saveLocationCache( locationCache ):
    from rpn.rpnKeyboard import DelayedKeyboardInterrupt

    with DelayedKeyboardInterrupt( ):
        with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'locations.pckl.bz2', 'wb' ) ) as pickleFile:
            pickle.dump( locationCache, pickleFile )
예제 #25
0
def getUserVariablesFileName( ):
    return getUserDataPath( ) + os.sep + PROGRAM_NAME + '_user_variables.cfg'
예제 #26
0
def getPrimeCacheFileName( name ):
    return getUserDataPath( ) + os.sep + name + '.cache'
예제 #27
0
def getUserFunctionsFileName( ):
    return getUserDataPath( ) + os.sep + PROGRAM_NAME + '_user_functions.cfg'
예제 #28
0
def loadFactorCache( ):
    g.factorCache = PersistentDict( getUserDataPath( ) + os.sep + 'factors.cache' )
예제 #29
0
def initializeConversionMatrix( unitConversionMatrix, validateConversions ):
    # reverse each conversion
    print( 'Reversing each conversion...' )

    newConversions = { }

    for ops, factor in unitConversionMatrix.items( ):
        conversion = fdiv( 1, factor )
        newConversions[ ( ops[ 1 ], ops[ 0 ] ) ] = conversion

    unitConversionMatrix.update( newConversions )

    print( 'Expanding metric units against the list of SI prefixes...' )

    metricConversions, metricAliases = expandMetricUnits( )

    unitConversionMatrix.update( metricConversions )
    newAliases = metricAliases

    print( 'Expanding data units against the list of SI and binary prefixes...' )
    unitConversionMatrix.update( expandDataUnits( ) )

    # add new operators for compound time units
    print( 'Expanding compound time units...' )

    # extrapolate transitive conversions
    print( )
    print( 'Extrapolating transitive conversions for', len( unitOperators ), 'units...' )

    unitTypeTable = makeUnitTypeTable( unitOperators )

    for unitType in sorted( basicUnitTypes ):
        if unitType != '_null_type':
            print( '\r     ', unitType, '({} units)'.format( len( unitTypeTable[ unitType ] ) ) )

        while True:
            newConversions = { }

            for op1, op2 in itertools.combinations( unitTypeTable[ unitType ], 2 ):
                if ( op1, op2 ) in unitConversionMatrix:
                    # print( )
                    # print( ( op1, op2 ), ': ', unitConversionMatrix[ ( op1, op2 ) ] )
                    newConversions.update( \
                        extrapolateTransitiveConversions( op1, op2, unitTypeTable, unitType, \
                                                          unitConversionMatrix ) )

            if not newConversions:
                break

            unitConversionMatrix.update( newConversions )

            print( '\r' + '{:,}'.format( len( unitConversionMatrix ) ), end='' )

    print( '\r' + '{:,} conversions'.format( len( unitConversionMatrix ) ) )

    # make some more aliases
    print( '        ' )
    print( 'Making some more aliases...' )

    newAliases.update( makeAliases( ) )

    print( 'Stringifying conversion matrix values...' )
    for ops, factor in unitConversionMatrix.items( ):
        unitConversionMatrix[ ( ops[ 0 ], ops[ 1 ] ) ] = str( factor )

    print( 'Saving everything...' )

    # save the list of unit operator names and aliases
    fileName = getUserDataPath( ) + os.sep + 'unit_names.pckl.bz2'

    with contextlib.closing( bz2.BZ2File( fileName, 'wb' ) ) as pickleFile:
        pickle.dump( PROGRAM_VERSION, pickleFile )
        pickle.dump( list( unitOperators.keys( ) ), pickleFile )
        pickle.dump( list( constantOperators.keys( ) ), pickleFile )
        pickle.dump( newAliases, pickleFile )

    # save the actual unit data
    fileName = getUserDataPath( ) + os.sep + 'units.pckl.bz2'

    testAllCombinations( unitTypeTable, unitConversionMatrix )

    if validateConversions:
        testAllConversions( unitTypeTable, unitConversionMatrix )

    with contextlib.closing( bz2.BZ2File( fileName, 'wb' ) ) as pickleFile:
        pickle.dump( PROGRAM_VERSION, pickleFile )
        pickle.dump( basicUnitTypes, pickleFile )
        pickle.dump( unitOperators, pickleFile )
        pickle.dump( constantOperators, pickleFile )

    fileName = getUserDataPath( ) + os.sep + 'unit_conversions.pckl.bz2'

    with contextlib.closing( bz2.BZ2File( fileName, 'wb' ) ) as pickleFile:
        pickle.dump( unitConversionMatrix, pickleFile )

    unitTypeDict = { }

    for unitType in basicUnitTypes.keys( ):
        unitTypeDict[ unitType ] = list( )

    for unit, unitInfo in unitOperators.items( ):
        unitTypeDict[ unitInfo.unitType ].append( unit )

    fileName = getUserDataPath( ) + os.sep + 'unit_help.pckl.bz2'

    with contextlib.closing( bz2.BZ2File( fileName, 'wb' ) ) as pickleFile:
        pickle.dump( unitTypeDict, pickleFile )

    print( )
    print( '{:,} unit operators'.format( len( unitOperators ) ) )
    print( '{:,} unit conversions'.format( len( unitConversionMatrix ) ) )
    print( '{:,} aliases'.format( len( newAliases ) ) )
예제 #30
0
def saveLocationCache(locationCache):
    with DelayedKeyboardInterrupt():
        with contextlib.closing(
                bz2.BZ2File(getUserDataPath() + os.sep + 'locations.pckl.bz2',
                            'wb')) as pickleFile:
            pickle.dump(locationCache, pickleFile)
예제 #31
0
def getUserConfigurationFileName( ):
    return getUserDataPath( ) + os.sep + PROGRAM_NAME + '_user_config.cfg'
예제 #32
0
def loadUnitConversionMatrix( ):
    try:
        with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'unit_conversions.pckl.bz2', 'rb' ) ) as pickleFile:
            g.unitConversionMatrix.update( pickle.load( pickleFile ) )
    except FileNotFoundError:
        print( 'rpn:  Unable to load unit conversion data.  Run "makeUnits" to generate the unit data files.' )
예제 #33
0
def initializeConversionMatrix(unitConversionMatrix, validateConversions):
    # reverse each conversion
    print('Reversing each conversion...')

    newConversions = {}

    for ops, factor in unitConversionMatrix.items():
        conversion = fdiv(1, factor)
        newConversions[(ops[1], ops[0])] = conversion

    unitConversionMatrix.update(newConversions)

    print('Expanding metric units against the list of SI prefixes...')

    metricConversions, metricAliases = expandMetricUnits()

    unitConversionMatrix.update(metricConversions)
    newAliases = metricAliases

    print('Expanding data units against the list of SI and binary prefixes...')
    unitConversionMatrix.update(expandDataUnits())

    # add new operators for compound time units
    print('Expanding compound time units...')

    # extrapolate transitive conversions
    print()
    print('Extrapolating transitive conversions for', len(unitOperators),
          'units...')

    unitTypeTable = makeUnitTypeTable(unitOperators)

    for unitType in sorted(basicUnitTypes):
        if unitType != '_null_type':
            print('\r     ', unitType,
                  '({} units)'.format(len(unitTypeTable[unitType])))

        while True:
            newConversions = {}

            for op1, op2 in itertools.combinations(unitTypeTable[unitType], 2):
                if (op1, op2) in unitConversionMatrix:
                    # print( )
                    # print( ( op1, op2 ), ': ', unitConversionMatrix[ ( op1, op2 ) ] )
                    newConversions.update(
                        extrapolateTransitiveConversions(
                            op1, op2, unitTypeTable, unitType,
                            unitConversionMatrix))

            if not newConversions:
                break

            unitConversionMatrix.update(newConversions)

            print('\r' + '{:,}'.format(len(unitConversionMatrix)), end='')

    print('\r' + '{:,} conversions'.format(len(unitConversionMatrix)))

    # make some more aliases
    print('        ')
    print('Making some more aliases...')

    newAliases.update(makeAliases())

    print('Stringifying conversion matrix values...')
    for ops, factor in unitConversionMatrix.items():
        unitConversionMatrix[(ops[0], ops[1])] = str(factor)

    print('Saving everything...')

    # save the list of unit operator names and aliases
    fileName = getUserDataPath() + os.sep + 'unit_names.pckl.bz2'

    with contextlib.closing(bz2.BZ2File(fileName, 'wb')) as pickleFile:
        pickle.dump(PROGRAM_VERSION, pickleFile)
        pickle.dump(list(unitOperators.keys()), pickleFile)
        pickle.dump(list(constantOperators.keys()), pickleFile)
        pickle.dump(newAliases, pickleFile)

    # save the actual unit data
    fileName = getUserDataPath() + os.sep + 'units.pckl.bz2'

    testAllCombinations(unitTypeTable, unitConversionMatrix)

    if validateConversions:
        testAllConversions(unitTypeTable, unitConversionMatrix)

    with contextlib.closing(bz2.BZ2File(fileName, 'wb')) as pickleFile:
        pickle.dump(PROGRAM_VERSION, pickleFile)
        pickle.dump(basicUnitTypes, pickleFile)
        pickle.dump(unitOperators, pickleFile)
        pickle.dump(constantOperators, pickleFile)

    fileName = getUserDataPath() + os.sep + 'unit_conversions.pckl.bz2'

    with contextlib.closing(bz2.BZ2File(fileName, 'wb')) as pickleFile:
        pickle.dump(unitConversionMatrix, pickleFile)

    unitTypeDict = {}

    for unitType in basicUnitTypes:
        unitTypeDict[unitType] = list()

    for unit, unitInfo in unitOperators.items():
        unitTypeDict[unitInfo.unitType].append(unit)

    fileName = getUserDataPath() + os.sep + 'unit_help.pckl.bz2'

    with contextlib.closing(bz2.BZ2File(fileName, 'wb')) as pickleFile:
        pickle.dump(unitTypeDict, pickleFile)

    print()
    print('{:,} unit operators'.format(len(unitOperators)))
    print('{:,} unit conversions'.format(len(unitConversionMatrix)))
    print('{:,} aliases'.format(len(newAliases)))

    missingHelp = []

    for unit, unitInfo in unitOperators.items():
        found = False

        for i in unitInfo.helpText:
            if i.isalnum():
                found = True
                break

        if not found:
            missingHelp.append(unit)

    if missingHelp:
        print()
        print('The following {} units do not have help text:'.format(
            len(missingHelp)))
        print()
        printParagraph(', '.join(sorted(missingHelp)))

    missingHelp = []

    for constant, constantInfo in constantOperators.items():
        found = False

        for i in constantInfo.helpText:
            if i.isalnum():
                found = True
                break

        if not found:
            missingHelp.append(constant)

    if missingHelp:
        print()
        print('The following {} constants do not have help text:'.format(
            len(missingHelp)))
        print()
        printParagraph(', '.join(sorted(missingHelp)))
예제 #34
0
def initializeAlpha():
    with open(getUserDataPath() + os.sep + 'wolframalpha.key', "r") as input:
        key = input.read().replace('\n', '')

    return wolframalpha.Client(key)
예제 #35
0
def initializeAlpha( ):
    with open( getUserDataPath( ) + os.sep + 'wolframalpha.key', "r" ) as input:
        key = input.read( ).replace( '\n', '' )

    return wolframalpha.Client( key )