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
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)
def checkForPrimeData( ): primeFile = Path( getUserDataPath( ) + os.sep + 'large_primes.cache' ) if primeFile.is_file( ): g.primeDataAvailable = True else: g.primeDataAvailable = False
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
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
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
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.' )
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 )
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
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' )
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
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
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
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
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
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
def getUserFunctionsFileName(): return getUserDataPath() + os.sep + PROGRAM_NAME + '_user_functions.cfg'
def getUserConfigurationFileName(): return getUserDataPath() + os.sep + PROGRAM_NAME + '_user_config.cfg'
def loadFactorCache(): g.factorCache = PersistentDict(getUserDataPath() + os.sep + 'factors.cache')
def getUserVariablesFileName(): return getUserDataPath() + os.sep + PROGRAM_NAME + '_user_variables.cfg'
def saveConstants(constants): with DelayedKeyboardInterrupt(): with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'constants.pckl.bz2', 'wb' ) ) \ as pickleFile: pickle.dump(constants, pickleFile)
def getPrimeCacheFileName(name): return getUserDataPath() + os.sep + name + '.cache'
def saveConstants( constants ): with DelayedKeyboardInterrupt( ): with contextlib.closing( bz2.BZ2File( getUserDataPath( ) + os.sep + 'constants.pckl.bz2', 'wb' ) ) as pickleFile: pickle.dump( constants, pickleFile )
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 )
def getUserVariablesFileName( ): return getUserDataPath( ) + os.sep + PROGRAM_NAME + '_user_variables.cfg'
def getPrimeCacheFileName( name ): return getUserDataPath( ) + os.sep + name + '.cache'
def getUserFunctionsFileName( ): return getUserDataPath( ) + os.sep + PROGRAM_NAME + '_user_functions.cfg'
def loadFactorCache( ): g.factorCache = PersistentDict( getUserDataPath( ) + os.sep + 'factors.cache' )
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 ) ) )
def saveLocationCache(locationCache): with DelayedKeyboardInterrupt(): with contextlib.closing( bz2.BZ2File(getUserDataPath() + os.sep + 'locations.pckl.bz2', 'wb')) as pickleFile: pickle.dump(locationCache, pickleFile)
def getUserConfigurationFileName( ): return getUserDataPath( ) + os.sep + PROGRAM_NAME + '_user_config.cfg'
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.' )
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)))
def initializeAlpha(): with open(getUserDataPath() + os.sep + 'wolframalpha.key', "r") as input: key = input.read().replace('\n', '') return wolframalpha.Client(key)
def initializeAlpha( ): with open( getUserDataPath( ) + os.sep + 'wolframalpha.key', "r" ) as input: key = input.read( ).replace( '\n', '' ) return wolframalpha.Client( key )