Exemplo n.º 1
0
Arquivo: data.py Projeto: kedz/cuttsum
 def wrapper(self, event, corpus, **kwargs):            
     print "I am here"
     is_met = \
         self.deps_met_.get((event.fs_name(), corpus.fs_name()), False)
     if is_met is False and kwargs.get('no_resolve', False) is False:
         for dc in self.dependencies():
             print self, dc
             dep = dc()
             coverage_per = dep.check_coverage(event, corpus, **kwargs)
             if coverage_per != 1.:
                 sys.stdout.write(
                     u"{}: Incomplete coverage (%{:0.2f}), "
                     u"retrieving...\n"
                         .format(dep, coverage_per * 100.))
                 print dep.get
                 if dep.get(event, corpus, **kwargs) is True:
                     sys.stdout.write('OK!\n')
                     sys.stdout.flush()
                 else:
                     sys.stdout.write('FAILED!\n')
                     sys.flush()
                     sys.stderr.write(
                         'Failed to retrieve necessary resource:\n')
                     sys.stderr.write('\t{} for {} / {}'.format(
                         dep, event.query_id, corpus.fs_name())) 
                     sys.stderr.flush()
                     sys.exit(1)
     self.deps_met_[(event.fs_name(), corpus.fs_name())] = True
     return fun(self, event, corpus, **kwargs)
Exemplo n.º 2
0
def SunState():
	UTCnow = datetime.datetime.utcnow()
	PSTnow = datetime.datetime.now()
	# Moved Lat & Long to top of script
	a = Astral()
	SunAngle = a.solar_elevation(UTCnow, latitude, longitude)
	#SunAngleMQTT = SunAngle
	# SunAngle is angle (+)above/(-)below horizon
	if SunAngle < 0.1:
		SunState = 'Night'
	else:
		SunState = 'Day'
	if ForceNight is True:
		SunState = 'Night'
	if DebugUTC is True:
		print 'PSTnow =',
		print (PSTnow),
		print 'UTCnow =',
		print (UTCnow),
		print 'SunState =',
		print (SunState), 
		print 'SunAngle =',
		print (SunAngle),
		print 'ForceNight =',
		print (ForceNight)
		sys.flush()
	return SunState
Exemplo n.º 3
0
 def wrapper(self, event, corpus, **kwargs):
     print "I am here"
     is_met = \
         self.deps_met_.get((event.fs_name(), corpus.fs_name()), False)
     if is_met is False and kwargs.get('no_resolve', False) is False:
         for dc in self.dependencies():
             print self, dc
             dep = dc()
             coverage_per = dep.check_coverage(event, corpus, **kwargs)
             if coverage_per != 1.:
                 sys.stdout.write(
                     u"{}: Incomplete coverage (%{:0.2f}), "
                     u"retrieving...\n".format(dep,
                                               coverage_per * 100.))
                 print dep.get
                 if dep.get(event, corpus, **kwargs) is True:
                     sys.stdout.write('OK!\n')
                     sys.stdout.flush()
                 else:
                     sys.stdout.write('FAILED!\n')
                     sys.flush()
                     sys.stderr.write(
                         'Failed to retrieve necessary resource:\n')
                     sys.stderr.write('\t{} for {} / {}'.format(
                         dep, event.query_id, corpus.fs_name()))
                     sys.stderr.flush()
                     sys.exit(1)
     self.deps_met_[(event.fs_name(), corpus.fs_name())] = True
     return fun(self, event, corpus, **kwargs)
 def wbyte(self, reg, value):
     # Write a single byte <value> to register <reg>.
     if self.DEBUG >= 32:
         sys.stdout.write(
             '  write  x{:02X} to chip x{:02X} register {}\n'.format(
                 value, self.address, reg))
         sys.flush()
     self.device.write_byte_data(self.address, reg, value)
     return
Exemplo n.º 5
0
# Main program:
try:
	if __name__ == '__main__':
		# Create NeoPixel object with appropriate configuration.
		strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
		# Intialize the library (must be called once before other functions).
		strip.begin()
		strip.show()
		blackout()
		SunStateMQTT = SunState()
		MQTT1()
		MQTT2("First Run")
		# Set LastOn and LastOff in event of exit before reaching either state
		if Debug is True: print 'Debug is ON'
		if Debug is False: print 'Debug is OFF'
		sys.flush()
		if Debug is True:
			print ('Press \033[1m Ctrl-C \033[0m to quit.')
			print (PBlue),
			print ('It is currently'),
			print (PYellow),
			print SunState(),
			print (PBlue),
			print ('on'),
			print (PPurple),
			print Holiday(),
			print (POff),
			sys.flush()
		while True:
			#AllTest() # Functions setup for individual testing
			while Holiday() != 'None':
Exemplo n.º 6
0
def main():
    """
    Get fact about number passed from command line
    """

    if "-t" in argumentList:
        if not update_token(argumentList[argumentList.index("-t") + 1]):
            sys.stdout.write(
                "Unable to update token, it may be invalid or file may not be written properly.\nCheck token validity again!\n"
            )
            sys.stdout.flush()
            return
        headers["x-rapidapi-key"] = argumentList[argumentList.index("-t") + 1]
        argumentList.pop(argumentList.index("-t"))
        argumentList.pop(argumentList.index(headers["x-rapidapi-key"]))

    if len(argumentList) > 2:
        if argumentList[1] == "-f":
            querystring = {"fragment": "true", "json": "true"}
            url = f"https://numbersapi.p.rapidapi.com/{argumentList[2]}/math"
            response = requests.request("GET",
                                        url,
                                        headers=headers,
                                        params=querystring)
            data = response.json()

            sys.stdout.write(f"{str(data['text'])}\n")
        elif argumentList[1] == "-ft":
            querystring = {
                "fragment": "true",
                "notfound": "floor",
                "json": "true"
            }
            url = f"https://numbersapi.p.rapidapi.com/{argumentList[2]}/trivia"
            response = requests.request("GET",
                                        url,
                                        headers=headers,
                                        params=querystring)
            data = response.json()

            sys.stdout.write(f"{str(data['text'])}\n")
        elif argumentList[1] == "-fy":
            querystring = {"fragment": "true", "json": "true"}
            url = f"https://numbersapi.p.rapidapi.com/{argumentList[2]}/year"
            response = requests.request("GET",
                                        url,
                                        headers=headers,
                                        params=querystring)
            data = response.json()

            if "date" in data:
                sys.stdout.write(
                    f"{str(data['date'])}, {str(data['number'])} - {str(data['text'])}\n"
                )
            else:
                sys.stdout.write(
                    f"{str(data['number'])} - {str(data['text'])}\n")
        elif argumentList[1] == "-fd":
            if "/" not in str(argumentList[2]):
                sys.stdout.write("Please provide date in the form of - mm/dd")
                sys.flush()
                return
            querystring = {"fragment": "true", "json": "true"}
            url = f"https://numbersapi.p.rapidapi.com/{argumentList[2]}/date"
            response = requests.request("GET",
                                        url,
                                        headers=headers,
                                        params=querystring)
            data = response.json()

            sys.stdout.write(f"{str(data['year'])} - {str(data['text'])}\n")
        else:
            sys.stdout.write(
                "Please provide options in - '-f', '-ft', '-fy' or '-fd'")
    elif len(argumentList) > 1:
        if argumentList[1] == "-h":
            sys.stdout.write("Command line options - \n")
            options = [("<none>", "get trivia fact about random number"),
                       ("-f", "get math fact about a number. Ex. -f 29"),
                       ("-ft", "get trivia fact about a number. Ex. -ft 7"),
                       ("-fy", "get fact about year. Ex. -fy 2003"),
                       ("-fd", "get fact about date. Ex. -fd 5/23")]
            for pair in options:
                sys.stdout.write(f"\t{pair[0]}\t{pair[1]}\n")
            sys.stdout.flush()
            return

        querystring = {"fragment": "true", "json": "true"}
        url = f"https://numbersapi.p.rapidapi.com/{argumentList[1]}/math"
        response = requests.request("GET",
                                    url,
                                    headers=headers,
                                    params=querystring)
        data = response.json()

        sys.stdout.write(f"{str(data['text'])}\n")
    else:
        url = "https://numbersapi.p.rapidapi.com/random/trivia"
        querystring = {
            "max": "20",
            "fragment": "true",
            "min": "10",
            "json": "true"
        }
        response = requests.request("GET",
                                    url,
                                    headers=headers,
                                    params=querystring)
        data = response.json()

        sys.stdout.write(f"{str(data['number'])} - {str(data['text'])}\n")

    sys.stdout.flush()
    return
    def calibrate(self, numsteps):
        """
            Calculates the coefficient and power terms for the sensor by recording
            voltage measurements at known distances and then performing an
            exponential curve fit on the data.
            
            numsteps: the number of measurements to take from min to max distance
                      more steps will give a more accurate curve
        """
        import numpy as np
        from scipy.optimize import curve_fit

        distances = range(self.min, self.max+1, (self.max-self.min)/numsteps)
        voltages = []
        
        def func(x, a, b):
            """
                Curve fitting function.  Used to fit data points to the curve
                y = a * x ^ b
            """
            return a * x ** b
        
        print('IR Calibration')
        print('\nThe calibration process requires you to place an object ' \
              '(a flat piece of card is good) at a variety of distances from ' \
              'the minumum distance to the maximum distance.\n\n' \
              'Voltage readings will be taken at (cm):')
        print distances
        
        for d in distances:
            print("Measure @ %d cm" % (d))
            sleep(3)
            for x in range(5):
                sys.stdout.write("%d  " % (5-x))
                sys.flush()
                sleep(1)
    
            print("\nReading @ %d cm" % (d))
            value = 0.0
            total = 0.0
            for x in range(5):
                value = ADC.read(self.AIN) * 1.8 * self.scale
                total = total + value
                print("#%d - %2.4f" % (x+1, value))
                sleep(1)
            print("%d cm Average: %2.4f" % (d, total/5))
            print"=" * 15
            voltages.append(total/5)
        
        # fits measured voltages to y = a * x ^ B curve
        popt, pcov = curve_fit(func, np.array(voltages), np.array(distances))
        self.coeff = popt[0]
        self.power = popt[1]
        print "Calibration Data"
        print "-" * 15
        print "Coefficient  :", self.coeff
        print "Power        :", self.power
        print "Err 1 std dev:", np.sqrt(np.diag(pcov))
        print "\nEquation     : distance = %2.2f * scaled_voltage ** %2.2f" % (self.coeff, self.power)
        print "-" * 15
        print "Example use:"
        print '  IR = Adafruit_SharpIR("P9_36", scale=1560.0/560.0, coeff=%2.6f, power=%2.6f)' % (self.coeff, self.power)
        print "-" * 15
Exemplo n.º 8
0
    def calibrate(self, numsteps):
        """
            Calculates the coefficient and power terms for the sensor by recording
            voltage measurements at known distances and then performing an
            exponential curve fit on the data.
            
            numsteps: the number of measurements to take from min to max distance
                      more steps will give a more accurate curve
        """
        import numpy as np
        from scipy.optimize import curve_fit

        distances = range(self.min, self.max+1, (self.max-self.min)/numsteps)
        voltages = []
        
        def func(x, a, b):
            """
                Curve fitting function.  Used to fit data points to the curve
                y = a * x ^ b
            """
            return a * x ** b
        
        print('IR Calibration')
        print('\nThe calibration process requires you to place an object ' \
              '(a flat piece of card is good) at a variety of distances from ' \
              'the minumum distance to the maximum distance.\n\n' \
              'Voltage readings will be taken at (cm):')
        print distances
        
        for d in distances:
            print("Measure @ %d cm" % (d))
            sleep(3)
            for x in range(5):
                sys.stdout.write("%d  " % (5-x))
                sys.flush()
                sleep(1)
    
            print("\nReading @ %d cm" % (d))
            value = 0.0
            total = 0.0
            for x in range(5):
                value = ADC.read(self.AIN) * 1.8 * self.scale
                total = total + value
                print("#%d - %2.4f" % (x+1, value))
                sleep(1)
            print("%d cm Average: %2.4f" % (d, total/5))
            print"=" * 15
            voltages.append(total/5)
        
        # fits measured voltages to y = a * x ^ B curve
        popt, pcov = curve_fit(func, np.array(voltages), np.array(distances))
        self.coeff = popt[0]
        self.power = popt[1]
        print "Calibration Data"
        print "-" * 15
        print "Coefficient  :", self.coeff
        print "Power        :", self.power
        print "Err 1 std dev:", np.sqrt(np.diag(pcov))
        print "\nEquation     : distance = %2.2f * scaled_voltage ** %2.2f" % (self.coeff, self.power)
        print "-" * 15
        print "Example use:"
        print '  IR = Adafruit_SharpIR("P9_36", scale=1560.0/560.0, coeff=%2.6f, power=%2.6f)' % (self.coeff, self.power)
        print "-" * 15
Exemplo n.º 9
0
def import_data(save=True,try_from_csv=False,saved_csvs=['MaleRankCount.csv','FemaleRankCount.csv',
                                                                       'MaleRankIDs.csv','FemaleRankIDs.csv',
                                                                    'male_name_ids.csv','female_name_ids.csv']):
    cwd = os.getcwd()
    subdir = 'data'
    path = os.path.join(cwd,subdir)
    dir_info = os.walk(path)
    (_,_,files) = dir_info.__next__()
    csv_files = set([file for file in files if '.csv' in file])
    txt_files = [file for file in files if '.txt' in file]
    name_id2str = {'M':{},'F':{}}
    name_str2id = {'M':{},'F':{}}
    ranks_count = {'M':None,'F':None}
    ranks_ids = {'M':None,'F':None}
    if try_from_csv and len(saved_csvs) == 6 and csv_files.issuperset(saved_csvs):
        try:
            sys.stdout.write('\nImporting from CSV files...\n')
            sys.stdout.flush()
            show_status(0./6)
            with open(os.path.join(path,saved_csvs[0])) as f:
                df = pd.read_csv(f)
                try:
                    df.columns = [int(yr) for yr in df.columns]
                except:
                    pass
                ranks_count['M'] = df
            show_status(1./6)
            with open(os.path.join(path,saved_csvs[1])) as f:
                df = pd.read_csv(f)
                try:
                    df.columns = [int(yr) for yr in df.columns]
                except:
                    pass
                ranks_count['F'] = df
            show_status(2./6)
            with open(os.path.join(path,saved_csvs[2])) as f:
                df = pd.read_csv(f)
                try:
                    df.columns = [int(yr) for yr in df.columns]
                except:
                    pass
                ranks_ids['M'] = df
            show_status(3./6)
            with open(os.path.join(path,saved_csvs[3])) as f:
                df = pd.read_csv(f)
                try:
                    df.columns = [int(yr) for yr in df.columns]
                except:
                    pass
                ranks_ids['F'] = df
            show_status(4./6)
            with open(os.path.join(path,saved_csvs[4])) as f:
                curr_reader = csv.reader(f)
                for id,name in curr_reader:
                    name_id2str['M'][int(id)] = name
                    name_str2id['M'][name] = int(id)
            show_status(5./6)
            with open(os.path.join(path,saved_csvs[5])) as f:
                curr_reader = csv.reader(f)
                for id,name in curr_reader:
                    name_id2str['F'][int(id)] = name
                    name_str2id['F'][name] = int(id)
            show_status(1)
            return {'id2str':name_id2str,'str2id':name_str2id}, ranks_count, ranks_ids
        except:
            sys.stdout.write("\nError: Could not import from CSV. Attempting import from txt files\n")
            sys.flush()
    rankings = {i:defaultdict(list) for i in range(len(txt_files))}
    mmax_len = 0; fmax_len = 0; high_prime = 104729 # 449563
    sys.stdout.write('Extracting Data From txt files...\n')
    sys.stdout.flush()
    for i in range(len(txt_files)):
        show_status(float(i+1)/len(txt_files))
        file = txt_files[i]
        with open(os.path.join(path,file)) as f:
            data = list(csv.reader(f))
            for name,gender,births in data:
                h = str2hash(name,maxval=high_prime)
                hval = name_id2str[gender].get(h,'-1')
                count = 1
                while name != hval and hval != '-1':
                    h = (h**count + ord(name[0])**count + ord(name[-1])**count) % high_prime
                    count+=1
                    hval = name_id2str[gender].get(h,'-1')
                if name != hval:
                    name_id2str[gender][h] = name
                    name_str2id[gender][name] = h
                rankings[i][gender].append((h,births))
            mmax_len = max(mmax_len,len(rankings[i]['M']))
            fmax_len = max(fmax_len,len(rankings[i]['F']))

    ranks_count = {'M':pd.DataFrame(columns=list(range(len(rankings.keys()))), index=list(range(mmax_len)))
             , 'F':pd.DataFrame(columns=list(range(len(rankings.keys()))), index=list(range(fmax_len)))}
    ranks_ids = {'M':pd.DataFrame(columns=list(range(len(rankings.keys()))), index=list(range(mmax_len)))
             , 'F':pd.DataFrame(columns=list(range(len(rankings.keys()))), index=list(range(fmax_len)))}
    sys.stdout.write('\nConstructing DataFrame...\n')
    sys.stdout.flush()
    for year in rankings.keys():
        #show_status(float(year + 1)/len(rankings.keys()))
        for gender in rankings[year].keys():
            size = len(rankings[year][gender])
            ranks_ids[gender].loc[:size-1,year] = [rankings[year][gender][i][0] for i in range(size)]
            ranks_count[gender].loc[:size-1,year] = [int(rankings[year][gender][i][1]) for i in range(size)]
    if save:
        sys.stdout.write('\nSaving Data...\n')
        sys.stdout.flush()
        with open(os.path.join('data','male_name_ids.csv'),'w',newline='') as fout:
            w = csv.writer(fout)
            w.writerows(name_id2str['M'].items())
        with open(os.path.join('data','female_name_ids.csv'),'w',newline='') as fout:
            w = csv.writer(fout)
            w.writerows(name_id2str['F'].items())
        export_data(df=ranks_count['M'],filename='MaleRankCount.csv',path='data')
        export_data(df=ranks_count['F'],filename='FemaleRankCount.csv',path='data')
        export_data(df=ranks_ids['M'],filename='MaleRankIDs.csv',path='data')
        export_data(df=ranks_ids['F'],filename='FemaleRankIDs.csv',path='data')
    return {'id2str':name_id2str,'str2id':name_str2id},ranks_count,ranks_ids