示例#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 )