예제 #1
0
파일: main.py 프로젝트: irvingprog/WITS
class GameCounter():
    def __init__(self):
        self.background = pygame.image.load('resources/background_pause.png').convert_alpha()

        self.counter = 10000
        self.state = True
        self.timer = Timer()

        self.font = pygame.font.Font('resources/ThrowMyHandsUpintheAirBold.ttf',300)
        self.text_counter = Text(self.font,self.counter,(255,255,255),SCREEN_WIDTH/2,SCREEN_HEIGHT/2)

        self.timer.start()

    def update(self,screen):
        while self.state:
            self.counter -= self.timer.time() * 100

            if self.counter < 0:
                self.state = False
                self.timer.stop()
                scene.timer.start()

            #make text in update, why is a object that renew
            self.text_counter = Text(self.font,int(math.ceil(self.counter/4000)),(255,255,255),SCREEN_WIDTH/2,SCREEN_HEIGHT/2)
            self._draw(screen)

    def _draw(self,screen):
        #Doesnt loss images of previous scene
        screen.blit(scene.background,(0,0))
        screen.blit(scene.title_shadow,(0,0))
        scene.level_title.draw(screen)

        screen.blit(self.background,(0,0))
        self.text_counter.draw(screen)
        pygame.display.flip()
예제 #2
0
파일: auto.py 프로젝트: prestocore/browser
 def widget(self, d):
     LOGFP       = open("log.create-test-case.txt", "a")
     LOGFP.write("\n")
     LOGFP.write("-----------------------------------------------\n")
     LOGFP.write("%s/tools/create-test-case.sh ocsp-1 %s\n" % (self._pwd, d))
     LOGFP.write("-----------------------------------------------\n")
     LOGFP.close()
     LOGFP       = open("log.sign-widget.txt", "a")
     
     handle      = Popen("%s/tools/create-test-case-modified.sh ocsp-1 %s" % (self._pwd, d), shell=True, stdout=LOGFP, stderr=LOGFP, cwd="%s/tools" % (self._pwd), close_fds=True)
     runTimer    = Timer()
     
     while True:
         if (runTimer.elapsed() > 60):
             handle.terminate()
         # if
         
         handle.poll()
         
         if (handle.returncode != None):
             break
         # if
     # while
     
     LOGFP.write("-------------------- done --------------------\n")
     LOGFP.close()
     print " - created widget [returncode: %d]" % (handle.returncode)
예제 #3
0
class Muerta(Sprite):

    def __init__(self, pos, escala, TIME):

        Sprite.__init__(self)

        path = os.path.join(BASE_PATH, "Imagenes", "muerta.png")
        imagen = pygame.image.load(path)
        imagen_escalada = pygame.transform.scale(imagen, escala)
        self.image = imagen_escalada.convert_alpha()
        self.rect = self.image.get_rect()

        self.image = pygame.transform.rotate(self.image, -pos[0])
        self.rect.centerx = pos[1]
        self.rect.centery = pos[2]

        self.timer = Timer(TIME)
        self.edad = {
            "Años": 0,
            "Dias": 0,
            "Horas": 0}
        self.timer.connect("new-time", self.__update_time)

    def __update_time(self, widget, _dict):
        self.edad = dict(_dict)
        if self.edad["Dias"] >= 3:
            self.morir()

    def morir(self):
        self.timer.salir()
        self.kill()
예제 #4
0
def edit_main(screen):
    #pygame.init()
    #screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption('LevelEdit')
    pygame.mouse.set_visible(1)
    clock = pygame.time.Clock()
    level = EditLevel(2000, 800, 'objects1.ini', DATA_PATH)
    pygame.display.flip()
    screen.blit(level.background, (0, 0))

    while 1:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            if event.type == QUIT_EVENT:
                return
            if event.type == pygame.KEYUP:
                level.key_input(event.key)
            if event.type == pygame.MOUSEBUTTONUP:
                level.mouse_input(event)
            if event.type > pygame.USEREVENT:
                Timer.handle_event(event.type)

        level.update()
        level.draw(screen)
        level.gui_manager.draw(screen)
        pygame.display.flip()

    pygame.display.quit()
    pygame.quit()
예제 #5
0
def runGrid(passCount, filename='lists/binaries2.dat'):
	'''
	Wrapper function to run the binary mofel fitting grid.

	:param passCount: [in] The maximum amount of passes to run on the grid
	:param filename: [in] The filename of the file containing a list of targets field and APOGEE ID's to use
	'''
	timer = Timer()
	timer.start()
	# Prep Grid
	locationIDs, apogeeIDs = np.loadtxt(filename, unpack=True, delimiter=',', dtype=str)
	targetCount = len(locationIDs)
	gridParams = [GridParam(locationIDs[i], apogeeIDs[i]) for i in range(targetCount)]
	minimizedVisitParams = [np.array([]) for i in range(targetCount)]

	# Use past results
	# checkPreviousData(gridParams)

	grid(passCount, gridParams, minimizedVisitParams)
	writeGridToFile(gridParams)
	for i in range(len(minimizedVisitParams)):
		filename = 'lists/chi2/' + minimizedVisitParams[i][0].locationID + '/' + minimizedVisitParams[i][0].apogeeID + '.lis'
		if not os.path.exists('lists/chi2/' + minimizedVisitParams[i][0].locationID + '/'):
			os.makedirs('lists/chi2/' + minimizedVisitParams[i][0].locationID + '/')
		writeGridToFile(minimizedVisitParams[i], filename=filename)

	print('Total run time: ' + str(round(timer.end(), 2)) + str('s'))
예제 #6
0
    def run(self, options):
        config = Config()
        config.keep_results = options['keep_results']
        config.create_diffs = options['create_diffs']
        config.update_refs = options['update_refs']

        t = Timer()
        docs = options['tests']
        docs_dir = options['docs_dir']

        if len(docs) == 1:
            if os.path.isdir(docs[0]):
                if docs_dir is None:
                    docs_dir = docs[0]
                if docs_dir == docs[0]:
                    docs = []
            else:
                if docs_dir is None:
                    docs_dir = os.path.dirname(docs[0])
        else:
            if docs_dir is None:
                docs_dir = os.path.commonprefix(docs).rpartition(os.path.sep)[0]

        tests = TestRun(docs_dir, options['refs_dir'], options['out_dir'])
        status = tests.run_tests(docs)
        tests.summary()
        get_printer().printout_ln("Tests run in %s" % (t.elapsed_str()))

        return status
예제 #7
0
def monitor():
    """Send discovery and monitor messages, and capture any responses"""

    # Define the schedule of message polling
    sendSwitchTimer    = Timer(5, 1)   # every n seconds offset by initial 1
    switch_state       = 0             # OFF
    radio.receiver()
    decoded            = None

    while True:
        # See if there is a payload, and if there is, process it
        if radio.isReceiveWaiting():
            #trace("receiving payload")
            payload = radio.receive()
            try:
                decoded = OpenHEMS.decode(payload)
            except OpenHEMS.OpenHEMSException as e:
                warning("Can't decode payload:" + str(e))
                continue
                      
            OpenHEMS.showMessage(decoded)
            updateDirectory(decoded)
            logMessage(decoded)
            
            #TODO: Should remember report time of each device,
            #and reschedule command messages to avoid their transmit slot
            #making it less likely to miss an incoming message due to
            #the radio being in transmit mode

            # handle messages with zero recs in them silently
            #trace(decoded)
            if len(decoded["recs"]) == 0:
                print("Empty record:%s" % decoded)
            else:
                # assume only 1 rec in a join, for now
                if decoded["recs"][0]["paramid"] == OpenHEMS.PARAM_JOIN:
                    #TODO: write OpenHEMS.getFromMessage("header_mfrid")
                    # send back a JOIN ACK, so that join light stops flashing
                    response = OpenHEMS.alterMessage(JOIN_ACK_MESSAGE,
                        header_mfrid=decoded["header"]["mfrid"],
                        header_productid=decoded["header"]["productid"],
                        header_sensorid=decoded["header"]["sensorid"])
                    p = OpenHEMS.encode(response)
                    radio.transmitter()
                    radio.transmit(p)
                    radio.receiver()

        if sendSwitchTimer.check() and decoded != None:
            request = OpenHEMS.alterMessage(SWITCH_MESSAGE,
                header_sensorid=decoded["header"]["sensorid"],
                recs_0_value=switch_state)
            p = OpenHEMS.encode(request)
            radio.transmitter()
            radio.transmit(p)
            radio.receiver()
            switch_state = (switch_state+1) % 2 # toggle
예제 #8
0
    def testFrameLimiter(self):
        t = Timer(fps = 60)

        fps = []
        while t.frame < 100:
            ticks = list(t.advanceFrame())
            fps.append(t.fpsEstimate)

        fps = fps[30:]
        avgFps = sum(fps) / len(fps)
        assert 0.8 * t.fps < avgFps < 1.2 * t.fps
예제 #9
0
def test1():
    """Test Timer class."""
    from time import sleep
    from datetime import timedelta
    from Timer import Timer

    timer = Timer()
    sleep(0.001)
    assert timer.get() > timedelta(seconds=0.001)
    sleep(0.001)
    assert timer.get() > timedelta(seconds=0.001)
    assert timer.getTotal() > timedelta(seconds=0.002)
예제 #10
0
    def __init__(self, var_dict):
        BaseObject.__init__(self, var_dict)
        self.type = PLAYER
        self.xvel = 0
        self.yvel = 0
        self.jumpvel = 1
        self.walkvel = 0.05
        self.maxXvel = 2.3
        self.maxYvel = 8
        self.direction = DIR_RIGHT
        self.iswalking = False
        self.on_object = None
        self.col_object = None
        self.jump_state = NOT_JUMPING
        self.airborne = True
        self.j_delay_timer = Timer(50, self.__next_jump_state, False)
        self.jump_timer = Timer(240, self.__next_jump_state, False)
        self._layer = 10
        #Array of objects currently colliding with rect - Tracks objects that are possible to interact with
        self.pos_interact = []
        self.held_item = None
        self.drop_item = None
        self.collidable = True
        self.held_ofs_x = 0.0
        self.held_ofs_y = 0.0
        self.obey_gravity = True
        self.visible = True

        self.idle_files = var_dict['idle_files'].split(',')
        self.idle_times = var_dict['idle_times'].split(',')
        self.jump_files = var_dict['jump_files'].split(',')
        self.jump_times = var_dict['jump_times'].split(',')
        self.walk_files = var_dict['walk_files'].split(',')
        self.walk_times = var_dict['walk_times'].split(',')
        self.idle_anim = Animation(self.idle_files, self.idle_times)
        self.jumping_anim = Animation(self.jump_files, self.jump_times)
        self.walking_anim = Animation(self.walk_files, self.walk_times)
        self.walking_anim.set_colorkey((255, 255, 255))

        if var_dict['w'] == 0 or var_dict['h'] == 0:
            self.rect.w = self.idle_anim.getRect().w
            self.rect.h = self.idle_anim.getRect().h
        else:
            pass
            #Needs to scale all animations while keeping aspect ratio.
            #self.walking_anim.scale((var_dict['w'], var_dict['h']))
            #self.jumping_anim.scale((var_dict['w'], var_dict['h']))
        self.anim_player = Animation_Player()
        self.anim_player.add(self.walking_anim, WALK_ANIM)
        self.anim_player.add(self.jumping_anim, JUMP_ANIM)
        self.anim_player.add(self.idle_anim, IDLE_ANIM)
        self.anim_player.set(IDLE_ANIM, True)
예제 #11
0
파일: monitor.py 프로젝트: sjhx/pyenergenie
def monitor():
    """Send discovery and monitor messages, and capture any responses"""

    # Define the schedule of message polling
    sendSwitchTimer    = Timer(60, 1)   # every n seconds offset by initial 1
    switch_state       = 0             # OFF
    radio.receiver()
    decoded            = None

    while True:
        # See if there is a payload, and if there is, process it
        if radio.isReceiveWaiting():
            trace("receiving payload")
            payload = radio.receive()
            try:
                decoded = OpenHEMS.decode(payload)
            except OpenHEMS.OpenHEMSException as e:
                print("Can't decode payload:" + str(e))
                continue
                      
            OpenHEMS.showMessage(decoded)
            updateDirectory(decoded)
            logMessage(decoded)
            
            #TODO: Should remember report time of each device,
            #and reschedule command messages to avoid their transmit slot
            #making it less likely to miss an incoming message due to
            #the radio being in transmit mode

            # assume only 1 rec in a join, for now
            if len(decoded["recs"])>0 and decoded["recs"][0]["paramid"] == OpenHEMS.PARAM_JOIN:
                #TODO: write OpenHEMS.getFromMessage("header_mfrid")
                response = OpenHEMS.alterMessage(MESSAGE_JOIN_ACK,
                    header_mfrid=decoded["header"]["mfrid"],
                    header_productid=decoded["header"]["productid"],
                    header_sensorid=decoded["header"]["sensorid"])
                p = OpenHEMS.encode(response)
                radio.transmitter()
                radio.transmit(p)
                radio.receiver()

        if sendSwitchTimer.check() and decoded != None and decoded["header"]["productid"] in [Devices.PRODUCTID_C1_MONITOR, Devices.PRODUCTID_R1_MONITOR_AND_CONTROL]:
            request = OpenHEMS.alterMessage(MESSAGE_SWITCH,
                header_sensorid=decoded["header"]["sensorid"],
                recs_0_value=switch_state)
            p = OpenHEMS.encode(request)
            radio.transmitter()
            radio.transmit(p)
            radio.receiver()
            switch_state = (switch_state+1) % 2 # toggle
예제 #12
0
 def __init__(self, var_dict):
     LevelObject.__init__(self, var_dict)
     self.type = BUILD_PROC
     str_input = var_dict['input']
     str_output = var_dict['output']
     self.input_items = [x.strip() for x in str_input.split(',')]
     self.output_items = [x.strip() for x in str_output.split(',')]
     self.length = to_num(var_dict['time'])
     self.delay_timer = Timer(5, self._ready)
     self.build_timer = Timer(self.length, self._finish)
     self.ready = True
     self.built = []  # list of output items that need to be spawned into the level
     self.input_area = self.rect  # required input parts must collide with this rect to start build
     self.output_area = pygame.Rect(self.rect.right, self.rect.bottom, 200, 100)  # items with output in this rect
    def updateK(self):
        [fro, to, pts] = eval(self.animationKEntry.get())

        self.K = np.linspace(fro, to, pts)
        t = np.linspace(0, float(self.timeEntry.get()), TIME_PTS)

        self.x_main = np.zeros((len(self.K), TIME_PTS, len(self.inpt.PlantFrame.getA())))
        self.u_main = np.zeros((len(self.K), TIME_PTS, self.inpt.PlantFrame.getB().shape[1]))

        for i in range(len(self.K)):
            Timer.reset(t)
            (x, u) = self.inpt.getX(t, self.Kset + [self.K[i]])

            self.u_main[i, :, :] = u
            self.x_main[i, :, :] = x
예제 #14
0
 def __init__(self):
     # Set state
     self.status['state'] = self.STATE_STARTUP
     
     # Setup MPD client
     self.mpd = MPDClient()
     self.mpd.timeout = 10  # network timeout in seconds (floats allowed), default: None
     self.mpd.idletimeout = None
     
     # Setup GPIO
     self.GPIO = GPIO
     self.GPIO.setwarnings(False)
     self.GPIO.setmode(GPIO.BOARD)
     
      # Add button callbacks
     GPIO.setup(PIN_PAUZE_PLAY, GPIO.IN)
     GPIO.setup(PIN_VOLUME_UP, GPIO.IN)
     GPIO.setup(PIN_VOLUME_DOWN, GPIO.IN)
     GPIO.add_event_detect(PIN_PAUZE_PLAY, GPIO.RISING, callback=self.pauseOrPlay, bouncetime=BUTTON_BOUNCETIME)
     GPIO.add_event_detect(PIN_VOLUME_UP, GPIO.RISING, callback=self.volumeUp, bouncetime=BUTTON_BOUNCETIME)
     GPIO.add_event_detect(PIN_VOLUME_DOWN, GPIO.RISING, callback=self.volumeDown, bouncetime=BUTTON_BOUNCETIME)
     
     # Start LCD
     self.screen = Screen(self)
     self.screen.onStatusChange()
     
     print(self.getIpAddress())
     
     # Start timer
     self.timer = Timer()
     self.timer.addRepeatingTask(POLLING_INTERVAL, self.updateMpdStatus)
     #self.timer.addRepeatingTask(SCREEN_UPDATE_INTERVAL, self.screen.tick)
     self.timer.start()
예제 #15
0
파일: Teacher.py 프로젝트: uonele/2016
    def stopCheckin(self):
        timedev = 90 * 60
        if Timer.timeCheck(timedev)==False :
            leaveTime = Timer.List[0][3]   #??
            leaveTimeArray=leaveTime.split('-| | :')

            beginTime = Timer.List[1][3]
            beginTimeArray = beginTime.split('-| | :')

            # 获得时间的差值 并将其赋给计时器
            timedev=(int(beginTimeArray[3])-int(leaveTime[3]))*3600+(int(beginTimeArray[4])-int(leaveTime[4]))*60+ \
                    (int(beginTimeArray[5]) - int(leaveTime[5]))

            Timer.timeCheck(timedev)  #传递一个参数给计时器

            Timer.List.pop(0)  #将教师踢出队列
예제 #16
0
class MainView(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.label = QLabel("Alarm!", self)
        
        
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.label)
        
        self.timer = Timer()
        
        self.add_button_timer(10)
        
        self.add_button("&Quit", SLOT("quit()"))
        
        self.setLayout(self.layout)
        
    def add_button_timer(self, m):
        button = QPushButton("%dmin"%m, self)
        button.clicked.connect(lambda: self.timer.reset(m))
        self.layout.addWidget(button)
        
        
    def add_button(self, msg, slot):
        button = QPushButton(msg, self)
        self.connect(button, SIGNAL("clicked()"), QCoreApplication.instance(), slot)
        self.layout.addWidget(button)
예제 #17
0
	def __init__(self, fname):

		self.tdl = ToDoList(fname)
		self.taskList = self.tdl.getTaskList()

		random.shuffle(self.taskList)
		self.timer = Timer()
예제 #18
0
 def __init__(self, fps = 60, tickrate = 1.0):
     self.tasks = []
     self.frameTasks = []
     self.timer = Timer(fps = fps, tickrate = tickrate)
     self.currentTask = None
     self.paused = []
     self.running = True
 def __init__(self, tank_index, target_color, state):
     self.state = state
     self.target_color = target_color
     self.tank_index = tank_index
     self.pdc = PDController()
     self.timer_id = Timer.add_task(self.update)
     self.kvis = KalmanVisualizer(800, 800)
     self.kfilter = KalmanFilter()
예제 #20
0
파일: Snake.py 프로젝트: adamkulpa/snake
 def __init__(self):
     self._direction = Snake.SNAKE_DIR_NONE
     self._last_direction = Snake.SNAKE_DIR_NONE
     self._length = 3
     self._part_position = [i for i in range(self._length)]
     self._speed = 100
     self._speed_timer = Timer()
     self._speed_timer.start()
예제 #21
0
  def main():
    # Get User Argument Input
    parser = argparse.ArgumentParser()
    parser.add_argument('-n', '--hour', type=int, required=True, dest='hour',
			help='hour for the timer')
    parser.add_argument('-m', '--minute', type=int, required=True, dest='minute',
			help='min for the timer')
    args = parser.parse_args()
    
    # Convert Hour and Minute to Seconds
    TimeStuff = Timer()
    TimeToWait = TimeStuff.converter(args.minute, args.hour)
    
    # Run The Base of the Main Reminder Window
    Reminder = Base()
    time.sleep(TimeToWait)
    Reminder.main()
예제 #22
0
파일: Bully.py 프로젝트: schoonc/wifite2
 def get_status(self):
     result = self.state
     result += " ({C}runtime:%s{W}" % Timer.secs_to_str(self.running_time())
     result += " {G}tries:%d{W}" % self.pins_attempted
     result += " {O}failures:%d{W}" % (self.consecutive_timeouts + self.consecutive_noassoc)
     result += " {R}lockouts:%d{W}" % self.consecutive_lockouts
     result += ")"
     return result
예제 #23
0
파일: Game.py 프로젝트: jgardner8/Bounce
    def __init__(self):
        self.timer = Timer(self.TIMER_FONT, self.TIMER_COLOR, self.TIMER_POSITION)
        self.time_scale = self.DEF_TIME_SCALE

        self.current_level = 0
        self.load_next_level()

        self.player.POPCORN_SOUND.set_volume(self.POPCORN_VOLUME)
예제 #24
0
def main(players, **settings):
    def On(key):
        return key in settings and settings[key] != 0
    if On('timers'):
        timer2 = Timer()
    #printAIData(aidata)
    statdata = [0, 0, 0, []]
    games = []
    if On('times'):
        print "Running", (settings['times']), "games"
    if On('progressbar'):
        bar = ProgressProcess(settings['times'], settings['progressbar'])
        #bar.setNewline()
    if On('timers'):
        timer = Timer(settings['times'])
    try:
        if On('times'):
            for a in range(0, settings['times']):
                play(players, statdata, games, On)
                if On('progressbar'):
                    bar.update(a)
            if On('progressbar'):
                bar.success()
                #del bar
        else:
            while (1):
                play(players, statdata, games, On, **settings)
    except UserError:
        print "\nUser quit."
    except KeyboardInterrupt:
        print
        if On('timers'):
            timer.setItter(a)
    except:
        if On('timers'):
            timer.setItter(a)
        handleError()
    finally:
        if On('times'):
            print "Ran", a + 1, " times."
    if On('timers'):
        del timer
    if On('lastfifteen'):
        printGames(games)
    if On('stats'):
        printStats(statdata)
예제 #25
0
    def run(self, options):
        config = Config()
        config.src_dir = options['src_dir']
        config.builder = options['builder']
        config.prefix = options['prefix']
        config.good = options['good']
        config.bad = options['bad']

        doc = options['test']
        if not os.path.isfile(doc):
            print("Invalid test %s: not a regulat file" % (doc))
            return

        t = Timer()
        bisect = Bisect(options['test'], options['refs_dir'], options['out_dir'])
        bisect.run()
        print("Tests run in %s" % (t.elapsed_str()))
예제 #26
0
    def run(self, options):
        config = Config()
        config.src_dir = options["src_dir"]
        config.builder = options["builder"]
        config.prefix = options["prefix"]
        config.good = options["good"]
        config.bad = options["bad"]

        doc = options["test"]
        if not os.path.isfile(doc):
            get_printer().printerr("Invalid test %s: not a regulat file" % (doc))
            return

        t = Timer()
        bisect = Bisect(options["test"], options["refs_dir"], options["out_dir"])
        bisect.run()
        get_printer().printout_ln("Tests run in %s" % (t.elapsed_str()))
예제 #27
0
파일: Huevos.py 프로젝트: fdanesse/Bichos
class Huevo(Sprite, GObject.GObject):

    __gsignals__ = {
    "nacer": (GObject.SignalFlags.RUN_LAST,
        None, (GObject.TYPE_PYOBJECT,
        GObject.TYPE_PYOBJECT))}

    def __init__(self, pos, TIME):

        Sprite.__init__(self)
        GObject.GObject.__init__(self)

        path = os.path.join(BASE_PATH, "Imagenes", "huevos.png")
        self.imagen = pygame.image.load(path)
        self.imagen_original = self.imagen.convert_alpha()

        self.image = self.imagen_original.copy()
        self.image = pygame.transform.rotate(
            self.imagen_original, -pos[0])
        self.rect = self.image.get_bounding_rect()

        self.rect.centerx = pos[1]
        self.rect.centery = pos[2]

        self.timer = Timer(TIME)
        self.edad = {
            "Años": 0,
            "Dias": 0,
            "Horas": 0}
        self.timer.connect("new-time", self.__update_time)

    def __update_time(self, widget, _dict):
        self.edad = dict(_dict)
        if self.edad["Dias"] >= 9:
            random.seed()
            huevos = random.randrange(10, 41, 1)
            hembras = random.randrange(0, huevos, 1)
            machos = huevos - hembras
            self.emit("nacer", (hembras, machos),
                (self.rect.centerx, self.rect.centery))
            self.morir()

    def morir(self):
        self.timer.salir()
        self.kill()
예제 #28
0
    def run(self, options):
        config = Config()
        config.force = options['force']
        config.checksums_only = options['checksums_only']

        t = Timer()
        doc = options['tests']
        if os.path.isdir(doc):
            docs_dir = doc
        else:
            docs_dir = os.path.dirname(doc)

        refs = TestReferences(docs_dir, options['refs_dir'])
        if doc == docs_dir:
            refs.create_refs()
        else:
            refs.create_refs_for_file(os.path.basename(doc))
        print("Refs created in %s" % (t.elapsed_str()))
예제 #29
0
 def slideshow(self):
     """Start slideshow"""
     self.logger.debug("Start slideshow")
     self.text_but.erase_sprite()
     self.actives.remove(self.text_but)
     self.timer = Timer(self.timerpause,self.next_photo,\
                             lock=self.SPG.get_thread_lock(), startnow=False)
     self.timer.start()
     self.state = 'play'
예제 #30
0
 def addToTable(self, table, cut):
     body = '%s ' %cut
     k = self.effwjet.keys()
     k.sort()
     for i in k:
         qcd = Timer.formatErrorPair(self.effqcd[i][0], self.effqcd[i][1])
         body += '& %s ' % (qcd)
     table += body + "\\\\ \n"
     return table
예제 #31
0
##################################        

#def main():
if sys.platform =='win32':
    #fname = 'Z:\\Pybehav\\VoltageRecording-04042019-1055-001_Cycle00001_VoltageRecording_001.csv'
    fname = 'Z:\\Pybehav\\VoltageRecording-04042019-1055-002_Cycle00001_VoltageRecording_001.csv'
else:
    fname = '/home/slee/data/Pybehav/VoltageRecording-04042019-1055-001_Cycle00001_VoltageRecording_001.csv'
    
   
D = pd.read_csv(fname, delimiter = ',')
dty = D.dtypes
#X = D.to_numpy()
X = D.values
X1 = X[:,1:]
timer = Timer()
params = {'rawdata':X1, 'scan_rate':5000, 'acq_dur':0.1,\
          'timer':timer}
daq = fakeDAQ(params)
whparam ={'channels':[2,3], 'daq':daq,'timer':timer}
wheel =  Wheel(whparam)
daq.worker.start()  
sleep(0.005)          
wheel.worker.start() 
    
    
    
    
    
#    plt.plot(wheel.whspeed)
#    t= X[:,0]
예제 #32
0
class fakeDAQ:
    """ DAQ"""

    def __init__(self, params):     
        try:			  
            self.rawdata = params['rawdata']	             
            self.scan_rate = params['scan_rate']           
            self.acq_dur = params['acq_dur'] # acquistion segment duration
        except KeyError:
            raise            
        self.ch = list(range(0,self.rawdata.shape[1])) 
        self.read_request_size =int(self.scan_rate*self.acq_dur)
       
        
        self.data =[] # segment data
        self.t =[]  # acquisition relative time for segment
        self.acq_counter = 0 # segment counter
        self.total_nsample_perchannel =0  # total number of samples per channel
        
        if 'timer' in params:
            self.timer = params['timer']            
        else:
            self.timer = Timer()            
            
        self.data_acqtime ={} #relative time of data-segment acquisition
        self.data_len = {} # sample number per each segment
   
        self.worker = Threadworker(self.acq_start)
       
       
        
        

    def reset_timer(self):
        """
        def reset_timer(self):
            reset timer
        """
        self.timer.start()
        
    def record_cont(self):
        """
        def record_cont(self):
        recording continously while scan_status is running
        """
        
        nsample = int(self.scan_rate*self.acq_dur)
        nch  = len(self.ch)            
        datalen = self.rawdata.shape[0]    
        
        while self.worker.running() : 
            try:
                
                sleep(self.acq_dur)
                #sleep(0.001)
                if self.total_nsample_perchannel>datalen:
                    break
             
                self.data_acqtime[self.acq_counter] = self.timer.elapsed_time()
               
                inx = range(self.acq_counter*nsample,(self.acq_counter+1)*nsample)
                self.data = self.rawdata[inx,:]
                timeoff = self.total_nsample_perchannel/self.scan_rate
                self.t = timeoff + np.array(range(0,nsample))/self.scan_rate
                self.data_len[self.acq_counter] =nsample
            
                
                workername = 'fakeDAQ'              
                #logging.debug('{}: counter:{}, nsample:{}, abstime:{}'.format(workername,self.acq_counter, self.total_nsample_perchannel, self.data_acqtime[self.acq_counter]))
                
                self.worker.set_datflag()
                
                self.total_nsample_perchannel += nsample
                self.acq_counter +=1
                
            except KeyboardInterrupt:
                logging.info('\nExit from DAQ\n')                                
                break
            
        self.acq_stop()
                
    def acq_start(self):
        """
        def acq_start(self):
            acqusition start
        """   
        self.record_cont()
        self.worker.clear_datflag()
            
    def acq_stop(self):       
        self.worker.stop()
예제 #33
0
def webbrowser(url):
    """Download the http/https resource given by the URL"""
    response = urllib.request.urlopen(url)
    if response.getcode() == 200:
        contents = response.read()
    return contents.decode("utf8")


if __package__ is None or __package__ == "":
    from Timer import Timer
else:
    from .Timer import Timer

if __name__ == "__main__":
    with Timer() as webbrowser_timer:
        fuzzingbook_contents = webbrowser(
            "http://www.fuzzingbook.org/html/Fuzzer.html")

    print("Downloaded %d bytes in %.2f seconds" %
          (len(fuzzingbook_contents), webbrowser_timer.elapsed_time()))

if __name__ == "__main__":
    fuzzingbook_contents[:100]

from urllib.parse import urlparse

if __name__ == "__main__":
    urlparse('https://www.fuzzingbook.com/html/Carver.html')

if __name__ == "__main__":
예제 #34
0
    """
    creates and returns an instance of a dealer
    :param n: number of players in the game
    :type n: Nat
    :return: the dealer
    :rtype: Dealer
    """
    players = [Player() for i in range(n)]
    dealer = Dealer(players, Deck.make_deck())
    return dealer


def print_score(score):
    """
    Prints the score of the game
    :param score: [(id, score), ...]
    :type score:[(Nat, Nat), ...]
    :return: None
    """
    for i, s in enumerate(score):
        print("%s player id: %s info: %s score: %s" % (i + 1, s[0], s[1], s[2]))

if __name__ == "__main__":
    t = Timer()
    with t:
        if len(sys.argv) == 1:
            main(["7"])
        else:
            main(sys.argv[1:])

예제 #35
0
class Board(QtGui.QFrame):
    msg2Statusbar = QtCore.pyqtSignal(str)

    def __init__(self, parent):
        super(Board, self).__init__(parent)
        self.parent = parent
        self.initBoard()

    def initBoard(self):
        self.paused = False
        self.esquina = 30
        self.ancho = 475
        self.largo = 525
        self.puntos = 0
        # cada uno de los pacman, fantasmas.
        self.pocmon = Pocmon(self)
        self.patiwi = Mrpatiwi(self)
        self.belenciwi = Belenciwi(self)
        self.jaimiwi = Jaimiwi(self)
        self.marquiwi = Marquiwi(self)
        # se lee el mapa
        self.mapa = list(
            map(lambda l: l.strip(), [line for line in open("mapa.txt", "r")]))
        self.monedas = dict()
        for i in range(len(self.mapa)):
            for j in range(len(self.mapa[0])):
                if self.mapa[i][j] == "I":
                    self.pocmon_x = j * 25 + 30  # guardar la pos.x inicial del pocmon
                    self.pocmon_y = i * 25 + 30  # guarad la pos.y inicial del pocmon
                    self.pocmon.x = j * 25 + 30  # setea la pos del pocmon
                    self.pocmon.y = i * 25 + 30
                elif self.mapa[i][j] == " ":
                    a = random.random()
                    if a >= 0.9:
                        self.monedas[(j * 25 + 30,
                                      i * 25 + 30)] = Roja(j, i, self)
                    elif a < 0.9:
                        self.monedas[(j * 25 + 30,
                                      i * 25 + 30)] = Amarilla(j, i, self, 10)
                elif self.mapa[i][j] == "P":
                    self.patiwi_x = j * 25 + 30  # guardar la pos.x inicial de patiwi
                    self.patiwi_y = i * 25 + 30  # guarad la pos.y inicial de patiwi
                    self.patiwi.x = j * 25 + 30  # setea la pos de patiwi
                    self.patiwi.y = i * 25 + 30
                elif self.mapa[i][j] == "B":
                    self.belenciwi_x = j * 25 + 30  # guardar la pos.x inicial
                    self.belenciwi_y = i * 25 + 30  # guarad la pos.y inicial
                    self.belenciwi.x = j * 25 + 30  # setea la pos de patiwi
                    self.belenciwi.y = i * 25 + 30
                elif self.mapa[i][j] == "J":
                    self.jaimiwi_x = j * 25 + 30  # guardar la pos.x inicial
                    self.jaimiwi_y = i * 25 + 30  # guarad la pos.y inicial
                    self.jaimiwi.x = j * 25 + 30  # setea la pos de patiwi
                    self.jaimiwi.y = i * 25 + 30
                elif self.mapa[i][j] == "M":
                    self.marquiwi_x = j * 25 + 30  # guardar la pos.x inicial
                    self.marquiwi_y = i * 25 + 30  # guarad la pos.y inicial
                    self.marquiwi.x = j * 25 + 30  # setea la pos de patiwi
                    self.marquiwi.y = i * 25 + 30
                elif self.mapa[i][j] == "S":
                    self.start_x = j * 25 + 30
                    self.start_y = i * 25 + 30 - 25

        # se setean los fanstasmas + pocmon
        self.pocmon.setGeometry(self.pocmon_x, self.pocmon_y, self.pocmon.lado,
                                self.pocmon.lado)
        self.patiwi.setGeometry(self.patiwi_x, self.patiwi_x, self.patiwi.lado,
                                self.patiwi.lado)
        self.belenciwi.setGeometry(self.belenciwi_x, self.belenciwi_y,
                                   self.belenciwi.lado, self.belenciwi.lado)
        self.jaimiwi.setGeometry(self.jaimiwi_x, self.jaimiwi_y,
                                 self.jaimiwi.lado, self.jaimiwi.lado)
        self.marquiwi.setGeometry(self.marquiwi_x, self.marquiwi_y,
                                  self.marquiwi.lado, self.marquiwi.lado)
        self.pocmon.show()
        self.patiwi.show()
        self.belenciwi.show()
        self.jaimiwi.show()
        self.marquiwi.show()

        # etiquetas del tiempo, puntaje, vidas etc
        self.tiempo = QtGui.QLabel(self)
        self.tiempo.setGeometry(550, 120, 50, 30)
        self.tiempo.setText("Tiempo:")
        self.tiempo.show()

        self.timer = Timer(self)
        self.timer.setGeometry(600, 120, 30, 30)
        self.timer.show()

        self.puntaje = QtGui.QLabel(self)
        self.puntaje.setGeometry(550, 80, 50, 30)
        self.puntaje.setText("Puntaje:")
        self.contador = QtGui.QLabel(self)
        self.contador.setGeometry(600, 80, 30, 30)
        self.contador.setText(str(self.puntos))
        self.puntaje.show()
        self.contador.show()

        self.texto_vidas = QtGui.QLabel(self)
        self.texto_vidas.setGeometry(550, 160, 50, 30)
        self.texto_vidas.setText("Vidas:")
        self.vidas = QtGui.QLabel(self)
        self.vidas.setGeometry(600, 160, 50, 30)
        self.vidas.setText(str(self.pocmon.vidas))
        self.vidas.show()
        self.texto_vidas.show()

        self.setFocusPolicy(QtCore.Qt.StrongFocus)

    def keyPressEvent(self, event):

        key = event.key()
        if key == QtCore.Qt.Key_Left:
            if self.chequear_pixel(self.pocmon.x-1,self.pocmon.y+5) and \
                    self.chequear_pixel(self.pocmon.x-1, self.pocmon.y+24-5):
                self.pocmon.direccion = "Izquierda"

        if key == QtCore.Qt.Key_Right:
            if self.chequear_pixel(self.pocmon.x+24+1, self.pocmon.y+5) and \
                    self.chequear_pixel(self.pocmon.x+24+1, self.pocmon.y+24-5):
                self.pocmon.direccion = "Derecha"

        if key == QtCore.Qt.Key_Down:
            if self.chequear_pixel(self.pocmon.x+5, self.pocmon.y+24+1) and \
                    self.chequear_pixel(self.pocmon.x+24-5, self.pocmon.y+24+1):
                self.pocmon.direccion = "Abajo"

        if key == QtCore.Qt.Key_Up:
            if self.chequear_pixel(self.pocmon.x+5, self.pocmon.y-1) and \
                    self.chequear_pixel(self.pocmon.x+24-5, self.pocmon.y-1):
                self.pocmon.direccion = "Arriba"

        if key == QtCore.Qt.Key_P:
            if not self.paused:
                self.paused = True
                self.mostrar_menu_pausa()
            elif self.paused:
                self.paused = False
                self.parent.titulo.hide()
                self.parent.boton1.hide()
                self.parent.boton2.hide()
                self.parent.boton3.hide()
# menu pausa

    def mostrar_menu_pausa(self):
        self.boton5 = QtGui.QPushButton(self)
        self.boton5.setText("NUEVA PARTIDA")
        self.boton5.setGeometry(550, 300, 120, 50)
        self.boton5.show()

        self.boton6 = QtGui.QPushButton(self)
        self.boton6.setText("CARGAR PARTIDA")
        self.boton6.setGeometry(550, 400, 120, 50)
        self.boton6.show()

        self.boton7 = QtGui.QPushButton(self)
        self.boton7.setText("GUARDAR PARTIDA")
        self.boton7.setGeometry(550, 500, 120, 50)
        self.boton7.show()

        self.boton5.clicked.connect(self.nueva_partida)
        self.boton6.clicked.connect(self.cargar_partida)
        self.boton7.clicked.connect(self.parent.guardar_partida)

    def nueva_partida(self):
        self.boton5.hide()
        self.boton6.hide()
        self.boton7.hide()
        self.monedas = {}
        for i in range(len(self.mapa)):
            for j in range(len(self.mapa[0])):

                if self.mapa[i][j] == " ":
                    a = random.random()
                    if a >= 0.9:
                        self.monedas[(j * 25 + 30,
                                      i * 25 + 30)] = Roja(j, i, self)
                    elif a < 0.9:
                        self.monedas[(j * 25 + 30,
                                      i * 25 + 30)] = Amarilla(j, i, self, 10)
        self.pocmon.move(self.pocmon_x, self.pocmon_y)
        self.timer.contador = 0
        self.pocmon.vidas = 3
        self.vidas.setText(str(self.pocmon.vidas))
        self.timer.setText(str(self.timer.contador))
        self.puntos = 0
        self.contador.setText(str(self.puntos))
        self.update()
        self.paused = False

    def cargar_partida(self):
        self.boton5.hide()
        self.boton6.hide()
        self.boton7.hide()
        self.pacman.alive = False
        self.parent.cargar_partida()

    def guardar_partida(self):
        self.paren.guardar_partida()

    def paintEvent(self, event):
        # dibuja el laberinto
        painter = QtGui.QPainter()
        painter.begin(self)
        for i in range(len(self.mapa)):
            for j in range(len(self.mapa[0])):
                if self.mapa[i][j] == "#":
                    painter.fillRect(25 * j + 30, 25 * i + 30, 25, 25, 9)
                if self.mapa[i][j] == "G":
                    painter.fillRect(25 * j + 40, 25 * i + 30, 10, 25, 13)
        for n in self.monedas:
            if isinstance(self.monedas[n], Amarilla):
                painter.fillRect(self.monedas[n].x + 10,
                                 self.monedas[n].y + 10, 5, 5, 18)
            elif isinstance(self.monedas[n], Roja):
                painter.fillRect(self.monedas[n].x + 10,
                                 self.monedas[n].y + 10, 5, 5, 7)

        painter.drawLine(self.esquina, self.esquina, self.esquina,
                         self.esquina + self.largo)
        painter.drawLine(self.esquina, self.esquina, self.esquina + self.ancho,
                         self.esquina)
        painter.drawLine(self.esquina, self.esquina + self.largo,
                         self.esquina + self.ancho, self.esquina + self.largo)
        painter.drawLine(self.esquina + self.ancho, self.esquina + self.largo,
                         self.esquina + self.ancho, self.esquina)

        painter.end()

# funcion que cheuquea q tipo de pixel es segun la posicion, es decir muralla o camino

    def chequear_pixel(self, x, y):
        if self.mapa[int((y - 30) // 25)][int(
            (x - 30) // 25)] == " " or self.mapa[int((y - 30) // 25)][int(
                (x - 30) // 25)] == "I":
            return True
        else:
            return False
예제 #36
0
class Babe(King):

	def __init__(self, screen, levels):

		self.screen = screen

		self.sprites = Babe_Sprites().babe_images

		self.levels = levels

		self.level = self.levels.max_level

		self.timer = Timer()

		# Booleans

		self.isWalk = False

		self.isCrouch = False

		self.isFalling = False

		self.isKiss = False

		self.hasKissed = False

		self.collideBottom = False

		self.lastCollision = True

		# Animation

		self.walkCount = 0

		self.x, self.y = 375, 113

		self.width, self.height = 32, 32

		self.rect_x, self.rect_y = self.x + 1, self.y + 7

		self.rect_width, self.rect_height = self.width - 12, self.height - 8

		self.current_image = self.sprites["Babe_Stand1"]

		# Particles

		self.jump_particle = King_Particle("images\\particles\\jump_particle.png", 5, 1, 32)

		self.snow_jump_particle = King_Particle("images\\particles\\snow_jump_particle.png", 4, 3, 36)

		self.isJump = False

		self.isLanded = False

		# Audio

		self.channel = pygame.mixer.Channel(10)

		self.audio = Babe_Audio().audio

		# Physics

		self.physics = Physics()

		self.speed, self.angle = 0, 0

		self.maxSpeed = 11

		self.walkAngles = {"right" : math.pi/2, "left" : -math.pi/2}

		self.slip = 0

		# Ending

		self.ending_distance = 50

	def blitme(self):

		self.x = self.rect.x - 6
		self.y = self.rect.y - 9

		if self.levels.current_level == self.level:
			self.screen.blit(self.current_image, (self.x, self.y))

			# pygame.draw.rect(self.screen, (255, 0, 0), self.rect, 1)

	def update(self, king, command = None):

		if self.levels.current_level == self.level:

			self._check_events(command)

			self._update_audio1()

			self._add_gravity()

			self._move()

			self._check_collisions()

			self._update_vectors()

			self._update_sprites()

			self._update_audio2()

			self._update_particles()

			if not self.levels.ending:

				self._check_ending(king)

	def _check_ending(self, king):

		if self.rect_y - king.rect_y >= 0:

			if self.rect_x - king.rect_x <= self.ending_distance:

				self.levels.ending = True

				king.rect_x, king.rect_y = self.rect_x - self.ending_distance, self.rect_y 

				king.speed = 0

	def _check_events(self, command):

		if command:

			if command == "Crouch" and not self.isCrouch:

				self.timer.start()

				self.isCrouch = True

			elif command == "Jump":

				self._jump()

			elif command == "Kiss":

				self.isKiss = True

			elif command == "WalkLeft":

				self._walk("left")

			elif command == "WalkRight":

				self._walk("right")

			elif command == "Snatched":

				self.rect_y += 999

		else:

			self.isKiss = False
			self.hasKissed = False

	def _move(self):

		if self.speed > self.maxSpeed:
			self.speed = self.maxSpeed

		self.rect_x += math.sin(self.angle) * self.speed
		self.rect_y -= math.cos(self.angle) * self.speed

	# def _check_collisions(self):

	# 	self.isFalling = True

	# 	self.collideBottom = False
	# 	self.slip = 0

	# 	for platform in self.levels.levels[self.levels.current_level].platforms:
			
	# 		if self._collide_rect(self.rect, platform):

	# 			if self.rect.bottom >= platform.top and round(self.rect.bottom - platform.top) <= round(self.speed) and -math.cos(self.angle) > 0 and not platform.support:

	# 				self.rect.bottom = platform.top
	# 				self.isFalling = False
	# 				self.isContact = False
	# 				self.collideBottom = True

	# 				if not self.lastCollision:
	# 					self.isLanded = True

	# 				self.lastCollision = platform
	# 				self.slip = platform.slip

	# 	if not self.collideBottom:

	# 		self.lastCollision = None

	def _update_vectors(self):

		if self.collideBottom:

			self.angle, self.speed = self.physics.add_vectors(self.angle, self.speed, -self.physics.gravity[0], -self.physics.gravity[1])

			self.speed *= self.slip

	def _walk(self, direction):

		self.speed = 1
		self.angle = self.walkAngles[direction]
		self.isWalk = True

	def _jump(self):

		speed = (2 + (self.timer.elapsed_time()*2) / 150)
		angle = 0

		self.angle, self.speed = self.physics.add_vectors(self.angle, self.speed, angle, speed)

		self.isJump = True
		self.isCrouch = False
		self.isWalk = False
		self.timer.end()

	def _update_sprites(self):

		if self.isCrouch:

			self.current_image = self.sprites["Babe_Crouch"]

		if self.isFalling:

			if self.angle < math.pi/2 or self.angle > 3 * math.pi / 2:

				self.current_image = self.sprites["Babe_Jump"]

			else:

				self.current_image = self.sprites["Babe_Fall"]

		elif self.isKiss:

			self.current_image = self.sprites["Babe_Kiss"]

		elif self.lastCollision and self.isLanded:

			self.current_image = self.sprites["Babe_Land"]

		else:

			if self.walkCount <= 5:

				self.current_image = self.sprites["Babe_Stand1"]

			elif self.walkCount <= 8:

				self.current_image = self.sprites["Babe_Stand2"]

			elif self.walkCount <= 13:

				self.current_image = self.sprites["Babe_Stand3"]

			else:

				self.walkCount = 0

			self.walkCount += 1	

	def _update_audio1(self):

		if self.lastCollision:

			if self.isJump:

				self.channel.play(self.audio["babe_jump"])

	def _update_audio2(self):

		if self.lastCollision:

			if self.isLanded:

				self.channel.play(self.audio["king_land"])

				self.isLanded = False

		if self.isKiss:

			if not self.channel.get_busy() and not self.hasKissed:

				self.channel.play(self.audio["babe_kiss"])

				self.hasKissed = True
예제 #37
0
class Tower:
    
    T_TYPE = {"Blaster": {1: ( 10, 250,  15,   1, False,   0, "close", (255,  51,   0), (255, 153, 102)),
                          2: ( 20, 250,  23, 0.8, False,   0, "close", (204,   0,   0), (255, 153, 102)),
                          3: ( 35, 250,  35, 0.8, False,   0, "close", (153,   0,   0), (255, 153, 102))},
              "Blitz"  : {1: ( 15, 300,   5, 0.3, False,   0, "close", (255, 255, 153), (255, 255, 204)),
                          2: ( 25, 300,  10, 0.3, False,   0, "close", (255, 255, 102), (255, 255, 204)),
                          3: ( 40, 300,  15, 0.2, False,   0, "close", (255, 255,   0), (255, 255, 204))},
              "R. Hood": {1: ( 20, 500,  20, 1.2, False,   0,   "far", (  0, 204,   0), (204, 255,  51)),
                          2: ( 30, 550,  25,   1, False,   0,   "far", (  0, 153,  51), (204, 255,  51)),
                          3: ( 50, 600,  35, 0.8, False,   0,   "far", (  0, 102,   0), (204, 255,  51))},
              "Mortar" : {1: ( 60, 225,  40, 2.5,  True,  50, "close", (  0, 102, 204), (153, 153, 255)),
                          2: ( 80, 225,  60, 2.3,  True,  50, "close", (  0,   0, 153), (153, 153, 255)),
                          3: (120, 250, 120,   2,  True,  75, "close", (  0,   0, 103), (153, 153, 255))},
              "Bomber" : {1: ( 40, 275,  20,   2,  True,  75, "close", ( 89,  89,  89), ( 38,  38,  38)),
                          2: ( 50, 275,  50, 1.7,  True,  75, "close", ( 50,  50,  50), ( 38,  38,  38)),
                          3: ( 75, 300,  80, 1.5,  True, 100, "close", (  0,   0,   0), ( 38,  38,  38))}}
    projectiles = []
    
    def __init__(self, name, level, x, y):
        self.name = name
        self.level = level
        self.x = x
        self.y = y
        self.cost = self.T_TYPE[name][level][0]
        self.upgrade_cost = self.T_TYPE[name][self.level + 1][0] if self.level != 3 else "MAX LV"
        self.price = int(self.cost * 0.75)
        self.reach = self.T_TYPE[name][level][1]
        self.dmg = self.T_TYPE[name][level][2]
        self.shootspd = self.T_TYPE[name][level][3]
        self.is_splash = self.T_TYPE[name][level][4]
        self.radius = self.T_TYPE[name][level][5]
        self.targeting = self.T_TYPE[name][level][6]
        self.target = None
        self.coloring = self.T_TYPE[name][level][7]
        self.projectile_color = self.T_TYPE[name][level][8]
        self.timer = Timer()
        self.timer.start_timer()
    
    def shoot(self, x1, y1):
        self.projectiles.append(Projectile(self.x, self.y, self.x, self.y, x1, y1, self.projectile_color))
        self.timer.reset()
        self.timer.start_timer()
    def upgrade(self):
        self.__init__(self.name, self.level + 1, self.x, self.y)
    def show_info(self, x, y):
        # Change x, y position
        if y <= 475:
            y += 220
        elif y > 475:
            y -= 220
        if x <= 750:
            x += 150
        elif x > 750:
            x -= 150
        stroke(0)
        strokeCap(ROUND)
        strokeWeight(6)
        fill(230)
        rectMode(CENTER)
        rect(x, y, 300, 400, 15)
        fill(200)
        strokeWeight(3)
        rect(x, y + 30, 270, 315, 15)
        fill(0)
        textFont(createFont("UD Digi Kyokasho NP-B", 30))
        textAlign(CENTER)
        text("{} Level: {}".format(self.name.upper(), self.level), x, y - 150, 300, 50)
        textSize(20)
        textAlign(LEFT)
        text("Tower Cost: {}".format(self.cost), x - 120, y - 95)
        text("Selling Price: {}".format(self.price), x - 120, y - 65)
        text("Damage: {}".format(self.dmg), x - 120, y - 35)
        text("Speed: {}".format(self.shootspd), x - 120, y - 5)
        text("Range: {}".format(self.reach), x - 120, y + 25)
        if self.is_splash:
            text("Splash Radius: {}".format(self.radius), x - 120, y + 55)
            text("Upgrade Cost: {}".format(self.upgrade_cost), x - 120, y + 85)
        else:
            text("Upgrade Cost: {}".format(self.upgrade_cost), x - 120, y + 55)
        textSize(16)
        text("Press u to upgrade tower", x - 120, y + 120)
        text("Press s to sell tower", x - 120, y + 145)
        text("(While pressing tower)", x - 120, y + 170)
        rectMode(CORNER)
    def display(self):
        self.draw_tower()
        for p in self.projectiles:
            p.display()
            if dist(p.starting[0], p.starting[1], p.ending[0], p.ending[1]) <= dist(p.x, p.y, p.starting[0], p.starting[1]):
                self.projectiles.pop(self.projectiles.index(p))
                del p
    def draw_tower(self):
        noStroke()
        if self.name == "Blaster":
            fill(color(self.coloring[0], self.coloring[1], self.coloring[2]))
            triangle(self.x, self.y - 20, self.x - 15, self.y + 20 , self.x + 15, self.y + 20)
        elif self.name == "Blitz":
            fill(self.coloring[0], self.coloring[1], self.coloring[2])
            quad(self.x, self.y - 20, self.x + 15, self.y , self.x, self.y + 20, self.x - 15, self.y)
        elif self.name == "R. Hood":
            fill(self.coloring[0], self.coloring[1], self.coloring[2])
            ellipse(self.x, self.y, 45, 30)
        elif self.name == "Mortar":
            fill(self.coloring[0], self.coloring[1], self.coloring[2])
            quad(self.x - 7.5, self.y - 20, self.x + 7.5, self.y - 20, self.x + 15, self.y + 20, self.x - 15, self.y + 20)
        elif self.name == "Bomber":
            stroke(0)
            strokeWeight(1)
            fill(self.coloring[0], self.coloring[1], self.coloring[2])
            ellipse(self.x, self.y, 40, 40)
            noStroke()
from queue import Empty, Full
from tqdm import tqdm
import time

from Timer import Timer
from DataController import DataController
from mod_WebsiteController import WeeklyUpdateWebsiteController
from google_search_parser import generate_parsers


if __name__ == "__main__":
    print("Generating parsers")
    parsers = generate_parsers()

    print("Setting up controllers")
    tm = Timer(interval=24*3600)
    tm2 = Timer(tm.Stop, interval=24*3600)
    dc = DataController(tm.Stop)
    wc = WeeklyUpdateWebsiteController(parsers, tm.Stop, dc.dataq, tm.Tx)

    tm.start()
    tm2.start()
    dc.start()
    wc.start()
    print("Waiting for the controllers")
    datum_count = 0
    while True:
        if not tm2.Tx.empty():
            try:
                tm2.Tx.get_nowait()
                dc.save()
예제 #39
0
    async def challenge_callback(client: Client,
                                 callback_query: CallbackQuery):
        query_data = str(callback_query.data)
        query_id = callback_query.id
        chat_id = callback_query.message.chat.id
        user_id = callback_query.from_user.id
        msg_id = callback_query.message.message_id
        chat_title = callback_query.message.chat.title
        user_name = callback_query.from_user.first_name
        group_config = _config.get(str(chat_id), _config["*"])
        if query_data in ["+", "-"]:
            admins = await client.get_chat_members(chat_id,
                                                   filter="administrators")
            if not any([
                    admin.user.id == user_id and
                (admin.status == "creator" or admin.can_restrict_members)
                    for admin in admins
            ]):
                await client.answer_callback_query(
                    query_id, group_config["msg_permission_denied"])
                return

            ch_id = "{chat}|{msg}".format(chat=chat_id, msg=msg_id)
            _cch_lock.acquire()
            # target: int = None
            timeout_event: None
            challenge, target, timeout_event = _current_challenges.get(
                ch_id, (None, None, None))
            if ch_id in _current_challenges:
                # 预防异常
                del _current_challenges[ch_id]
            _cch_lock.release()
            timeout_event.stop()
            if query_data == "+":
                try:
                    await client.restrict_chat_member(
                        chat_id,
                        target,
                        permissions=ChatPermissions(
                            can_send_stickers=True,
                            can_send_messages=True,
                            can_send_media_messages=True,
                            can_send_polls=True))
                except ChatAdminRequired:
                    await client.answer_callback_query(
                        query_id, group_config["msg_bot_no_permission"])
                    return

                await client.edit_message_text(
                    chat_id,
                    msg_id,
                    group_config["msg_approved"].format(user=user_name),
                    reply_markup=None,
                )
                _me: User = await client.get_me()
                try:
                    await client.send_message(
                        int(_channel),
                        _config["msg_passed_admin"].format(
                            botid=str(_me.id),
                            targetuser=str(target),
                            groupid=str(chat_id),
                            grouptitle=str(chat_title),
                        ),
                        parse_mode="Markdown",
                    )
                except Exception as e:
                    logging.error(str(e))
            else:
                try:
                    await client.kick_chat_member(chat_id, target)
                except ChatAdminRequired:
                    await client.answer_callback_query(
                        query_id, group_config["msg_bot_no_permission"])
                    return
                await client.edit_message_text(
                    chat_id,
                    msg_id,
                    group_config["msg_refused"].format(user=user_name),
                    reply_markup=None,
                )
                _me: User = await client.get_me()
                try:
                    await client.send_message(
                        int(_channel),
                        _config["msg_failed_admin"].format(
                            botid=str(_me.id),
                            targetuser=str(target),
                            groupid=str(chat_id),
                            grouptitle=str(chat_title),
                        ),
                        parse_mode="Markdown",
                    )
                except Exception as e:
                    logging.error(str(e))
            await client.answer_callback_query(query_id)
            return

        ch_id = "{chat}|{msg}".format(chat=chat_id, msg=msg_id)
        _cch_lock.acquire()
        challenge, target, timeout_event = _current_challenges.get(
            ch_id, (None, None, None))
        _cch_lock.release()
        if user_id != target:
            await client.answer_callback_query(
                query_id, group_config["msg_challenge_not_for_you"])
            return None
        timeout_event.stop()
        try:
            await client.restrict_chat_member(chat_id,
                                              target,
                                              permissions=ChatPermissions(
                                                  can_send_stickers=True,
                                                  can_send_messages=True,
                                                  can_send_media_messages=True,
                                                  can_send_polls=True))
        except ChatAdminRequired:
            pass

        correct = str(challenge.ans()) == query_data
        if correct:
            try:
                await client.edit_message_text(
                    chat_id,
                    msg_id,
                    group_config["msg_challenge_passed"],
                    reply_markup=None)
                _me: User = await client.get_me()
            except MessageNotModified as e:
                await client.send_message(int(_channel),
                                          'Bot 运行时发生异常: `' + str(e) + "`")
            try:
                await client.send_message(
                    int(_channel),
                    _config["msg_passed_answer"].format(
                        botid=str(_me.id),
                        targetuser=str(target),
                        groupid=str(chat_id),
                        grouptitle=str(chat_title),
                    ),
                    parse_mode="Markdown",
                )
            except Exception as e:
                logging.error(str(e))
        else:
            if not group_config["use_strict_mode"]:
                await client.edit_message_text(
                    chat_id,
                    msg_id,
                    group_config["msg_challenge_mercy_passed"],
                    reply_markup=None,
                )
                _me: User = await client.get_me()
                try:
                    await client.send_message(
                        int(_channel),
                        _config["msg_passed_mercy"].format(
                            botid=str(_me.id),
                            targetuser=str(target),
                            groupid=str(chat_id),
                            grouptitle=str(chat_title),
                        ),
                        parse_mode="Markdown",
                    )
                except Exception as e:
                    logging.error(str(e))
            else:
                try:
                    await client.edit_message_text(
                        chat_id,
                        msg_id,
                        group_config["msg_challenge_failed"],
                        reply_markup=None,
                    )
                    # await client.restrict_chat_member(chat_id, target)
                    _me: User = await client.get_me()
                    try:
                        await client.send_message(
                            int(_channel),
                            _config["msg_failed_answer"].format(
                                botid=str(_me.id),
                                targetuser=str(target),
                                groupid=str(chat_id),
                                grouptitle=str(chat_title),
                            ),
                            parse_mode="Markdown",
                        )
                    except Exception as e:
                        logging.error(str(e))
                except ChatAdminRequired:
                    return

                if group_config["challenge_timeout_action"] == "ban":
                    await client.kick_chat_member(chat_id, user_id)
                elif group_config["challenge_timeout_action"] == "kick":
                    await client.kick_chat_member(chat_id, user_id)
                    await client.unban_chat_member(chat_id, user_id)
                elif group_config["challenge_timeout_action"] == "mute":
                    await client.restrict_chat_member(
                        chat_id,
                        user_id,
                        permissions=ChatPermissions(
                            can_send_other_messages=False,
                            can_send_messages=False,
                            can_send_media_messages=False,
                            can_add_web_page_previews=False,
                            can_send_polls=False))

                else:
                    pass

                if group_config["delete_failed_challenge"]:
                    Timer(
                        client.delete_messages(chat_id, msg_id),
                        group_config["delete_failed_challenge_interval"],
                    )
        if group_config["delete_passed_challenge"]:
            Timer(
                client.delete_messages(chat_id, msg_id),
                group_config["delete_passed_challenge_interval"],
            )
예제 #40
0
    async def challenge_user(client: Client, message: Message):
        target = message.new_chat_members[0]
        if message.from_user.id != target.id:
            if target.is_self:
                group_config = _config.get(str(message.chat.id), _config["*"])
                try:
                    await client.send_message(
                        message.chat.id, group_config["msg_self_introduction"])
                    _me: User = await client.get_me()
                    try:
                        await client.send_message(
                            int(_channel),
                            _config["msg_into_group"].format(
                                botid=str(_me.id),
                                groupid=str(message.chat.id),
                                grouptitle=str(message.chat.title),
                            ),
                            parse_mode="Markdown",
                        )
                    except Exception as e:
                        logging.error(str(e))
                except ChannelPrivate:
                    return
            return
        try:
            await client.restrict_chat_member(
                chat_id=message.chat.id,
                user_id=target.id,
                permissions=ChatPermissions(can_send_stickers=False,
                                            can_send_messages=False,
                                            can_send_media_messages=False,
                                            can_add_web_page_previews=False,
                                            can_send_polls=False,
                                            can_send_animations=False))
        except ChatAdminRequired:
            return
        group_config = _config.get(str(message.chat.id), _config["*"])
        challenge = Challenge()

        def generate_challenge_button(e):
            choices = []
            answers = []
            for c in e.choices():
                answers.append(
                    InlineKeyboardButton(str(c),
                                         callback_data=bytes(
                                             str(c), encoding="utf-8")))
            choices.append(answers)
            return choices + [[
                InlineKeyboardButton(group_config["msg_approve_manually"],
                                     callback_data=b"+"),
                InlineKeyboardButton(group_config["msg_refuse_manually"],
                                     callback_data=b"-"),
            ]]

        timeout = group_config["challenge_timeout"]
        reply_message = await client.send_message(
            message.chat.id,
            group_config["msg_challenge"].format(target=target.first_name,
                                                 target_id=target.id,
                                                 timeout=timeout,
                                                 challenge=challenge.qus()),
            reply_to_message_id=message.message_id,
            reply_markup=InlineKeyboardMarkup(
                generate_challenge_button(challenge)),
        )
        _me: User = await client.get_me()
        chat_id = message.chat.id
        chat_title = message.chat.title
        target = message.from_user.id
        timeout_event = Timer(
            challenge_timeout(client, message.chat.id, message.from_user.id,
                              reply_message.message_id),
            timeout=group_config["challenge_timeout"],
        )
        _cch_lock.acquire()
        _current_challenges["{chat}|{msg}".format(
            chat=message.chat.id,
            msg=reply_message.message_id)] = (challenge, message.from_user.id,
                                              timeout_event)
        _cch_lock.release()
예제 #41
0
    # # norm_features_df.reset_index().to_json('query_features_{}_uqv.JSON'.format(corpus))

    cores = mp.cpu_count() - 1

    if generate:
        # FIXME: test and fix the fueatures creation to run in parallel
        with mp.Pool(processes=cores) as pool:
            norm_features_list = pool.starmap(run_features_process,
                                              itertools.product({'ROBUST', 'ClueWeb12B'}, NUMBER_OF_DOCS))

        testing_feat = QueryFeatureFactory(corpus)
        norm_features_df = testing_feat.generate_features()

        _path = f'{dp.set_environment_paths()[0]}/{corpus}/test/pageRank'
        _path = dp.ensure_dir(_path)
        norm_features_df.reset_index().to_json(f'{_path}/PageRank_Features.JSON')

    elif file_to_load:
        features_df = features_loader(corpus, file_to_load)
        print(features_df)
    else:
        features_df = features_loader(corpus)
        print(features_df)


if __name__ == '__main__':
    args = parser.parse_args()
    overall_timer = Timer('Total runtime')
    main(args)
    overall_timer.stop()
예제 #42
0
def spectrum_reconstruction( output, events, image, hits, lvl, tol, binsize ):
    from matplotlib import pylab as plt
    from Timer import Timer
    
    sim_E = events.get_E()
    rec_E = hits[ 'E%d' % lvl ]

    with Timer('size like fast') as t:
        minimization = hits.get_sizeLike( lvl, events.gain, events.sigma, fast = True, tol = tol )

    #with Timer('size like') as t:
        #minimization_fast = hits.get_sizeLike(1, events.gain, events.sigma, fast = False )
    
    size_like_E = minimization[0]
    size_like = minimization[2]
    rec_sigma = size_like[:,0]
    matches = Matches( hits, events, lvl=3, length = hits.border+2 )

    min_E = min( np.min(sim_E), np.min(rec_E) )
    max_E = max( np.max(sim_E), np.max(rec_E) )
    bins = np.arange( min_E, max_E+10, binsize )
    with Timer('histogram E') as t:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        
        ax.hist( sim_E, bins, histtype='step', label = 'sim' )
        ax.hist( sim_E[ matches.get_unmatched('sim') ], bins, histtype='step', label = 'sim not rec' )
        ax.hist( rec_E, bins, histtype='step', label = 'all rec' )
        ax.hist( rec_E[ matches.get_single_matched('rec')], bins, histtype='step', label = 'rec single' )
        ax.hist( rec_E[ matches.get_mult_matched('rec')], bins, histtype='step', label = 'rec mult' )
        ax.hist( rec_E[ matches.get_unmatched('rec')], bins, histtype='step', label = 'rec fake' )
        
        ax.hist( size_like_E, bins, histtype='step', label = 'size like' )
        ax.set_xlabel(r'$E$[ADU]')
        ax.axvline( hits.threshold, ymin=0, ymax=ax.get_ylim()[1], c='g' )
        legend = ax.legend( fancybox=True, framealpha=0, bbox_to_anchor=(1.,1.), loc='upper left' )
        fig.savefig( output, bbox_extra_artists = (legend,), bbox_inches='tight')

    with Timer('scatter') as t:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        
        ax.scatter( sim_E[ matches.get_single_matched('sim') ], rec_E[ matches.get_single_matched('rec') ], label = r'$E_{\rm rec}$s', s=1 )
        ax.scatter( sim_E[ matches.get_mult_matched('sim') ], rec_E[ matches.get_mult_matched('rec') ], label = r'$E_{\rm rec}$m', s=1 )
        ax.scatter( sim_E[ matches.sim ], size_like_E[ matches.rec ], label = r'$E_{\rm sl}$', s=1 )
        
        ax.plot( (ax.get_xlim()[0], ax.get_xlim()[1]), (ax.get_xlim()[0], ax.get_xlim()[1]), 'k' )
        ax.set_xlabel(r'$E_{\rm sim}$')
        legend = ax.legend( fancybox=True, framealpha=0, bbox_to_anchor=(1.,1.), loc='upper left' )
        output2 = (lambda _: _[0]+'sc.'+_[1])(output.split('.'))
        fig.savefig( output2, bbox_extra_artists = (legend,), bbox_inches='tight')
        
    sim_sigma = events.get_sigma()
    rec_std = np.sqrt( hits['xVar%d' % lvl] )
    
    with Timer('scatter') as t:
        fig = plt.figure()
        ax = fig.add_subplot(111)

        ax.scatter( sim_E, sim_sigma, label = r'$\sigma_{\rm sim}$', s=1 )
        ax.scatter( rec_E, rec_std, label = r'${\rm std}_{\rm rec}$', s=1 )
        ax.scatter( rec_E, rec_sigma, label = r'$\sigma_{\rm rec}$', s=1 )

        ax.set_xlabel(r'$E$')
        ax.set_xlim( (sim_E.min(), sim_E.max()*1.2) )
        ax.set_ylim( (sim_sigma.min(), sim_sigma.max()*1.2) )
        legend = ax.legend( fancybox=True, framealpha=0, bbox_to_anchor=(1.,1.), loc='upper left' )
        output2 = (lambda _: _[0]+'sigma.'+_[1])(output.split('.'))
        fig.savefig( output2, bbox_extra_artists = (legend,), bbox_inches='tight')

    binsize = .02
    min_sigma = np.min(sim_sigma)
    max_sigma = np.max(sim_sigma)*1.5
    bins = np.arange( min_sigma, max_sigma+1, binsize )

    with Timer('scatter') as t:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.hist( sim_sigma, bins=bins, label = r'$\sigma_{\rm sim}$', histtype = 'step' )
        ax.hist( rec_std, bins=bins, label = r'${\rm std}_{\rm rec}$', histtype = 'step' )
        ax.hist( rec_sigma, bins=bins, label = r'$\sigma_{\rm rec}$', histtype = 'step' )
        ax.set_xlabel(r'$\sigma$')
        legend = ax.legend( fancybox=True, framealpha=0, bbox_to_anchor=(1.,1.), loc='upper left' )
        output2 = (lambda _: _[0]+'sigma2.'+_[1])(output.split('.'))
        fig.savefig( output2, bbox_extra_artists = (legend,), bbox_inches='tight')
    
    with Timer('scatter') as t:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.scatter( sim_sigma[ matches.sim ], rec_std[ matches.rec ], label = r'${\rm std}_{\rm rec}$', s=1 )
        ax.scatter( sim_sigma[ matches.sim ], rec_sigma[ matches.rec ], label = r'$\sigma_{\rm rec}$', s=1 )

        ax.plot( (ax.get_xlim()[0], ax.get_xlim()[1]), (ax.get_xlim()[0], ax.get_xlim()[1]), 'k' )
        ax.set_xlabel(r'$\sigma_{\rm sim}$')
        ax.set_ylim( (sim_sigma.min(), sim_sigma.max()*1.2) )
        legend = ax.legend( fancybox=True, framealpha=0, bbox_to_anchor=(1.,1.), loc='upper left' )
        output2 = (lambda _: _[0] + 'scsigmaPair.' + _[1])(output.split('.'))
        fig.savefig( output2, bbox_extra_artists = (legend,), bbox_inches='tight')
예제 #43
0
class Screen:
    width=1200
    height = 800
    badtimer = 6
    badguys=[]
    foods = []
    badguy=None
    x=100
    y=100
    food_x=0
    food_y=105
    exitcode = 0
    count=60
    one_count=0

    background = pygame.image.load('resources/images/grass.png')
    gameover = pygame.image.load("resources/images/gameover.png")
    youwin = pygame.image.load("resources/images/youwin.png")

    player=[]
    collider=None
    wl = None
    food = []
    heallvalue=None
    timer=None

    fpsClock = pygame.time.Clock()
    FPS = 100

    screen = pygame.display.set_mode((width, height))       #화면 해상도
    bg_columns = background.get_width()                     #화면 너비 불러오기
    bg_rows = background.get_height()                       #화면 높이 불러오기

    def __init__(self):
        self.player = Player(self.screen ,self.x,self.y)
        self.collider=Collider(self.screen,self.player.arrows,self.badguys,self.player,self.foods)
        self.food = Food(self.screen,self.food_x, self.food_y)
        self.wl=WL(self.screen,self.exitcode)
        self.timer=Timer(self.screen,self.count)
        self.screen2=Screen2(self.screen,self.width,self.height)

    def Start(self):
        self.timer.timer()
        while True:
            for event in pygame.event.get():    #종료 이벤트
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit(0)
            pygame.display.update() #업데이트

            for i in range(int(self.width // self.bg_columns) + 1):     #배경 채우기
                for j in range(int(self.height // self.bg_rows) + 1):
                    self.screen.blit(self.background, (i * self.bg_columns, j * self.bg_rows))

            self.timer.print()      #타이머 그리기

            self.food.drow()        #성 그리기

            for i in range(0, 8):   #성 객체 생성 후 리스트에 넣기
                food = Food(self.screen, self.food_x, self.food_y * i)
                self.foods.append(food)

            self.player.move()      #플레이어 무브함수
            self.collider.collide() #충돌 함수
            self.healgauge = self.collider.heallgauge
            self.one_count = self.timer.count       #타이머의 count와 같은 one_count
            pygame.display.update()

            if self.one_count <= 0:     #one_count가 0보다 이하일 때
                self.badguys=[]         #배드가이 사라짐
                self.exitcode = 1
                self.wl.exitcode = self.exitcode    #wl의 exitcode를 1로 바꿈
                break
            else:
                for badguy in self.badguys: #몹의 객체만큼
                    badguy.move()   #몹 이동 함수
                pygame.display.update()

                self.badtimer -= 1
                if self.badtimer == 0:
                    badguy = Badguy(self.screen, self.width,
                                    random.randint(50, self.height - 50), 16)    #위치랜덤의 속도8인 몹 객체 생성
                    self.badguys.append(badguy)                                 #리스트에 추가
                    self.badtimer = 6

            if self.healgauge < 0:
                break
        if self.healgauge < 0:  #체력게이지가 0보다 작으면
            self.wl.print()     #win or lose 출력

    def Starting(self):
        while True:
            self.screen2.Start()    #스크린2 실행
            game = Screen()
            game.Start()            #스크린1 실행
예제 #44
0
    def __init__(self, city):

        # Get current working directory
        self.dir_path = os.path.dirname(os.path.realpath(__file__))

        with open(self.dir_path + '/config/config.json', 'r') as j:
            configs = json.loads(j.read())

        self.og_columns = configs['data']['og_columns']
        self.generated_columns = configs['data']['generated_columns']
        # When generating the output samples (Y) of the supervised problem
        # add the columns you don't want to predict
        self.dont_predict = configs['data']['dont_predict']

        self.n_days_in = configs['parameters']['lookback_days']
        self.n_in = self.len_day * self.n_days_in  # Number of previous samples used to feed the Neural Network

        self.dataset_percentage_reduction = configs['parameters'][
            'dataset_percentage_reduction']

        self.city = city

        self.timer = Timer(city=self.city)

        self.availability_db_name = "Bicis_" + self.city + "_Availability"
        self.prediction_db_name = "Bicis_" + self.city + "_Prediction"

        self.db_password = "******"

        self.plotter = Plotter()
        self.utils = Utils(city=self.city)

        self.client = InfluxDBClient(self.db_ip, '8086', 'root', "root",
                                     self.availability_db_name)

        self.utils.check_and_create(["/data/" + self.city])
        self.utils.check_and_create([
            '/data/' + self.city + '/cluster/',
            "/data/" + self.city + "/filled", "/model/" + self.city,
            "/data/utils/", "/plots/" + self.city,
            "/data/" + self.city + "/supervised",
            "/data/" + self.city + "/scaled", "/data/" + self.city + "/filled",
            "/data/" + self.city + "/encoders",
            "/data/" + self.city + "/encoded_data"
        ])

        self.list_hours = [
            "00:00", "00:10", "00:20", "00:30", "00:40", "00:50", "01:00",
            "01:10", "01:20", "01:30", "01:40", "01:50", "02:00", "02:10",
            "02:20", "02:30", "02:40", "02:50", "03:00", "03:10", "03:20",
            "03:30", "03:40", "03:50", "04:00", "04:10", "04:20", "04:30",
            "04:40", "04:50", "05:00", "05:10", "05:20", "05:30", "05:40",
            "05:50", "06:00", "06:10", "06:20", "06:30", "06:40", "06:50",
            "07:00", "07:10", "07:20", "07:30", "07:40", "07:50", "08:00",
            "08:10", "08:20", "08:30", "08:40", "08:50", "09:00", "09:10",
            "09:20", "09:30", "09:40", "09:50", "10:00", "10:10", "10:20",
            "10:30", "10:40", "10:50", "11:00", "11:10", "11:20", "11:30",
            "11:40", "11:50", "12:00", "12:10", "12:20", "12:30", "12:40",
            "12:50", "13:00", "13:10", "13:20", "13:30", "13:40", "13:50",
            "14:00", "14:10", "14:20", "14:30", "14:40", "14:50", "15:00",
            "15:10", "15:20", "15:30", "15:40", "15:50", "16:00", "16:10",
            "16:20", "16:30", "16:40", "16:50", "17:00", "17:10", "17:20",
            "17:30", "17:40", "17:50", "18:00", "18:10", "18:20", "18:30",
            "18:40", "18:50", "19:00", "19:10", "19:20", "19:30", "19:40",
            "19:50", "20:00", "20:10", "20:20", "20:30", "20:40", "20:50",
            "21:00", "21:10", "21:20", "21:30", "21:40", "21:50", "22:00",
            "22:10", "22:20", "22:30", "22:40", "22:50", "23:00", "23:10",
            "23:20", "23:30", "23:40", "23:50"
        ]

        bah = self.utils.stations_from_web(self.city)
        bah.drop(bah.columns[[2, 3]], axis=1, inplace=True)

        self.station_dict = dict(zip(bah.values[:, 1], bah.values[:, 0]))

        self.listOfStations = list(bah.values[:, 1])

        self.utils.save_array_txt(
            self.dir_path + "/data/" + self.city + "/listOfStations",
            self.listOfStations)

        self.hour_encoder.fit(self.list_hours)

        self.weekday_encoder.classes_ = self.weekdays
def main():
    # TODO 0: Measures total program runtime by collecting start time
    timer = Timer('Start Time')

    # TODO 1: Define get_input_args function within the file get_input_args.py
    # This function retrieves 3 Command Line Arugments from user as input from
    # the user running the program from a terminal window. This function returns
    # the collection of these command line arguments from the function call as
    # the variable in_arg
    in_arg = get_input_args().parse_args()

    # Function that checks command line arguments using in_arg
    check_command_line_arguments(in_arg)

    # TODO 2: Define get_pet_labels function within the file get_pet_labels.py
    # Once the get_pet_labels function has been defined replace 'None'
    # in the function call with in_arg.dir  Once you have done the replacements
    # your function call should look like this:
    #             get_pet_labels(in_arg.dir)
    # This function creates the results dictionary that contains the results,
    # this dictionary is returned from the function call as the variable results
    results = get_pet_labels(in_arg.dir)

    # Function that checks Pet Images in the results Dictionary using results
    check_creating_pet_image_labels(results)

    # TODO 3: Define classify_images function within the file classify_images.py
    # Once the classify_images function has been defined replace first 'None'
    # in the function call with in_arg.dir and replace the last 'None' in the
    # function call with in_arg.arch  Once you have done the replacements your
    # function call should look like this:
    #             classify_images(in_arg.dir, results, in_arg.arch)
    # Creates Classifier Labels with classifier function, Compares Labels,
    # and adds these results to the results dictionary - results
    classify_images(in_arg.dir, results, in_arg.arch)

    # Function that checks Results Dictionary using results
    check_classifying_images(results)

    # TODO 4: Define adjust_results4_isadog function within the file adjust_results4_isadog.py
    # Once the adjust_results4_isadog function has been defined replace 'None'
    # in the function call with in_arg.dogfile  Once you have done the
    # replacements your function call should look like this:
    #          adjust_results4_isadog(results, in_arg.dogfile)
    # Adjusts the results dictionary to determine if classifier correctly
    # classified images as 'a dog' or 'not a dog'. This demonstrates if
    # model can correctly classify dog images as dogs (regardless of breed)
    adjust_results4_isadog(results, dognames)

    # Function that checks Results Dictionary for is-a-dog adjustment using results

    check_classifying_labels_as_dogs(results)

    # TODO 5: Define calculates_results_stats function within the file calculates_results_stats.py
    # This function creates the results statistics dictionary that contains a
    # summary of the results statistics (this includes counts & percentages). This
    # dictionary is returned from the function call as the variable results_stats
    # Calculates results of run and puts statistics in the Results Statistics
    # Dictionary - called results_stats
    results_stats = calculates_results_stats(results)

    # Function that checks Results Statistics Dictionary using results_stats
    check_calculating_results(results, results_stats)

    # TODO 6: Define print_results function within the file print_results.py
    # Once the print_results function has been defined replace 'None'
    # in the function call with in_arg.arch  Once you have done the
    # replacements your function call should look like this:
    #      print_results(results, results_stats, in_arg.arch, True, True)
    # Prints summary results, incorrect classifications of dogs (if requested)
    # and incorrectly classified breeds (if requested)
    print_results(results, results_stats, in_arg.arch, True, True)

    # TODO 0: Measure total program runtime by collecting end time
    timer.stop('End Time')

    # TODO 0: Computes overall runtime in seconds & prints it in hh:mm:ss format
    print("\n** Total Elapsed Runtime:", timer.output_final())
예제 #46
0
    def outputs():
        lastSpeed = 120
        motorControl.speed(lastSpeed)

        lastX = 25

        stream = io.BytesIO()
        i = 0
        lastTurn = 0
        timer = Timer([
            'captured image',
            'got image from stream',
            'tested for end',
            'filtered image',
            'got line points',
            'transformed points',
            'filtered points',
            'did turn',
            'set speed'])

        while True:
            yield stream
            timer.reset()
            #Added part
            with PiCamera()  as camera:
                camera.start_preview()
                time.sleep()
                camera.capture(stream, format='rgb')
                timer.tick() #captured image
            # Go to the end of the stream so we can read its content
            stream.seek(2)
            timer.tick() #got image from stream
            img = Image.open(stream)

            greyImg = picUtils.imgFilter(img, threshold)
            stopData = picUtils.getImgArray(greyImg)
            if picUtils.isEnd(stopData):
                print('Is End {}'.format(i))
                servoControl.turn(0)
                sleep(0.2)
                # img.save('end.png')
                return
            timer.tick() # tested for end

            # resize image for faster path finding
            img = img.resize((50, 50), Image.ANTIALIAS)
            greyImg = picUtils.imgFilter(img, threshold)
            data = picUtils.getImgArray(greyImg)
            timer.tick() # filtered image

            # do pathfinding
            p0 = picUtils.getFirstPos(data, lastX)
            if p0 != None:
                lastX = int(p0[0] + int(10*lastTurn))
                lastX = max(0, min(49, lastX))
                # print(p0[0],lastTurn,lastX)
                points = picUtils.fillSearch(data, p0, lastTurn)
                timer.tick() # got line points
                points = pnt.transformPoints(points)
                timer.tick() # transformed points
                points = pnt.filterPoints(points, 4)
                timer.tick() # filterd points
                a = picUtils.getTurn(points, 100000)
                lastTurn = a

            # turn to most recent turn
            servoControl.turn(lastTurn)
            timer.tick() #did turn

            # set the speed to the turn
            # motorControl.speed(servoControl.getSpeed(lastTurn));
            timer.tick() #set speed

            # print(timer)

            # reset the stream
            stream.seek(0)
            stream.truncate()
            
        print('Closing Camera')
        camera.close()
        motorControl.speed(0)
예제 #47
0
    def initBoard(self):
        self.paused = False
        self.esquina = 30
        self.ancho = 475
        self.largo = 525
        self.puntos = 0
        # cada uno de los pacman, fantasmas.
        self.pocmon = Pocmon(self)
        self.patiwi = Mrpatiwi(self)
        self.belenciwi = Belenciwi(self)
        self.jaimiwi = Jaimiwi(self)
        self.marquiwi = Marquiwi(self)
        # se lee el mapa
        self.mapa = list(
            map(lambda l: l.strip(), [line for line in open("mapa.txt", "r")]))
        self.monedas = dict()
        for i in range(len(self.mapa)):
            for j in range(len(self.mapa[0])):
                if self.mapa[i][j] == "I":
                    self.pocmon_x = j * 25 + 30  # guardar la pos.x inicial del pocmon
                    self.pocmon_y = i * 25 + 30  # guarad la pos.y inicial del pocmon
                    self.pocmon.x = j * 25 + 30  # setea la pos del pocmon
                    self.pocmon.y = i * 25 + 30
                elif self.mapa[i][j] == " ":
                    a = random.random()
                    if a >= 0.9:
                        self.monedas[(j * 25 + 30,
                                      i * 25 + 30)] = Roja(j, i, self)
                    elif a < 0.9:
                        self.monedas[(j * 25 + 30,
                                      i * 25 + 30)] = Amarilla(j, i, self, 10)
                elif self.mapa[i][j] == "P":
                    self.patiwi_x = j * 25 + 30  # guardar la pos.x inicial de patiwi
                    self.patiwi_y = i * 25 + 30  # guarad la pos.y inicial de patiwi
                    self.patiwi.x = j * 25 + 30  # setea la pos de patiwi
                    self.patiwi.y = i * 25 + 30
                elif self.mapa[i][j] == "B":
                    self.belenciwi_x = j * 25 + 30  # guardar la pos.x inicial
                    self.belenciwi_y = i * 25 + 30  # guarad la pos.y inicial
                    self.belenciwi.x = j * 25 + 30  # setea la pos de patiwi
                    self.belenciwi.y = i * 25 + 30
                elif self.mapa[i][j] == "J":
                    self.jaimiwi_x = j * 25 + 30  # guardar la pos.x inicial
                    self.jaimiwi_y = i * 25 + 30  # guarad la pos.y inicial
                    self.jaimiwi.x = j * 25 + 30  # setea la pos de patiwi
                    self.jaimiwi.y = i * 25 + 30
                elif self.mapa[i][j] == "M":
                    self.marquiwi_x = j * 25 + 30  # guardar la pos.x inicial
                    self.marquiwi_y = i * 25 + 30  # guarad la pos.y inicial
                    self.marquiwi.x = j * 25 + 30  # setea la pos de patiwi
                    self.marquiwi.y = i * 25 + 30
                elif self.mapa[i][j] == "S":
                    self.start_x = j * 25 + 30
                    self.start_y = i * 25 + 30 - 25

        # se setean los fanstasmas + pocmon
        self.pocmon.setGeometry(self.pocmon_x, self.pocmon_y, self.pocmon.lado,
                                self.pocmon.lado)
        self.patiwi.setGeometry(self.patiwi_x, self.patiwi_x, self.patiwi.lado,
                                self.patiwi.lado)
        self.belenciwi.setGeometry(self.belenciwi_x, self.belenciwi_y,
                                   self.belenciwi.lado, self.belenciwi.lado)
        self.jaimiwi.setGeometry(self.jaimiwi_x, self.jaimiwi_y,
                                 self.jaimiwi.lado, self.jaimiwi.lado)
        self.marquiwi.setGeometry(self.marquiwi_x, self.marquiwi_y,
                                  self.marquiwi.lado, self.marquiwi.lado)
        self.pocmon.show()
        self.patiwi.show()
        self.belenciwi.show()
        self.jaimiwi.show()
        self.marquiwi.show()

        # etiquetas del tiempo, puntaje, vidas etc
        self.tiempo = QtGui.QLabel(self)
        self.tiempo.setGeometry(550, 120, 50, 30)
        self.tiempo.setText("Tiempo:")
        self.tiempo.show()

        self.timer = Timer(self)
        self.timer.setGeometry(600, 120, 30, 30)
        self.timer.show()

        self.puntaje = QtGui.QLabel(self)
        self.puntaje.setGeometry(550, 80, 50, 30)
        self.puntaje.setText("Puntaje:")
        self.contador = QtGui.QLabel(self)
        self.contador.setGeometry(600, 80, 30, 30)
        self.contador.setText(str(self.puntos))
        self.puntaje.show()
        self.contador.show()

        self.texto_vidas = QtGui.QLabel(self)
        self.texto_vidas.setGeometry(550, 160, 50, 30)
        self.texto_vidas.setText("Vidas:")
        self.vidas = QtGui.QLabel(self)
        self.vidas.setGeometry(600, 160, 50, 30)
        self.vidas.setText(str(self.pocmon.vidas))
        self.vidas.show()
        self.texto_vidas.show()

        self.setFocusPolicy(QtCore.Qt.StrongFocus)
예제 #48
0
    def crack_handshake(self, handshake, wordlist):
        '''Tries to crack a handshake. Returns WPA key if found, otherwise None.'''
        if wordlist is None:
            Color.pl("{!} {O}Not cracking handshake because" +
                     " wordlist ({R}--dict{O}) is not set")
            return None
        elif not os.path.exists(wordlist):
            Color.pl("{!} {O}Not cracking handshake because" +
                     " wordlist {R}%s{O} was not found" % wordlist)
            return None

        Color.pl(
            "\n{+} {C}Cracking WPA Handshake:{W} Using {C}aircrack-ng{W} via" +
            " {C}%s{W} wordlist" % os.path.split(wordlist)[-1])

        key_file = Configuration.temp('wpakey.txt')
        command = [
            "aircrack-ng", "-a", "2", "-w", wordlist, "--bssid",
            handshake.bssid, "-l", key_file, handshake.capfile
        ]
        crack_proc = Process(command)

        # Report progress of cracking
        aircrack_nums_re = re.compile(
            r"(\d+)/(\d+) keys tested.*\(([\d.]+)\s+k/s")
        aircrack_key_re = re.compile(
            r"Current passphrase:\s*([^\s].*[^\s])\s*$")
        num_tried = num_total = 0
        percent = num_kps = 0.0
        eta_str = "unknown"
        current_key = ''
        while crack_proc.poll() is None:
            line = crack_proc.pid.stdout.readline()
            match_nums = aircrack_nums_re.search(line)
            match_keys = aircrack_key_re.search(line)
            if match_nums:
                num_tried = int(match_nums.group(1))
                num_total = int(match_nums.group(2))
                num_kps = float(match_nums.group(3))
                eta_seconds = (num_total - num_tried) / num_kps
                eta_str = Timer.secs_to_str(eta_seconds)
                percent = 100.0 * float(num_tried) / float(num_total)
            elif match_keys:
                current_key = match_keys.group(1)
            else:
                continue

            status = "\r{+} {C}Cracking WPA Handshake: %0.2f%%{W}" % percent
            status += " ETA: {C}%s{W}" % eta_str
            status += " @ {C}%0.1fkps{W}" % num_kps
            #status += " ({C}%d{W}/{C}%d{W} keys)" % (num_tried, num_total)
            status += " (current key: {C}%s{W})" % current_key
            Color.clear_entire_line()
            Color.p(status)

        Color.pl("")
        # Check crack result
        if os.path.exists(key_file):
            f = open(key_file, "r")
            key = f.read().strip()
            f.close()
            os.remove(key_file)

            Color.pl("{+} {G}Cracked WPA Handshake{W} PSK: {G}%s{W}\n" % key)
            return key
        else:
            Color.pl("{!} {R}Failed to crack handshake:" +
                     " {O}%s{R} did not contain password{W}" %
                     wordlist.split(os.sep)[-1])
            return None
예제 #49
0
class Data_mgmt:

    db_ip = "192.168.86.99"
    weekdays = [
        "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY",
        "SUNDAY"
    ]
    list_hours = []
    listOfStations = []
    enable_scale = True

    hour_encoder = LabelEncoder()
    weekday_encoder = LabelEncoder()
    station_encoder = LabelEncoder()

    len_day = 144

    city = ""
    dbDaysQueryThresshold = 30
    scaler = MinMaxScaler(feature_range=(0, 1))  # Normalize values
    availability_db_name = ""
    prediction_db_name = ""
    queries_database = False
    station_dict = {}

    n_out = len_day

    def __init__(self, city):

        # Get current working directory
        self.dir_path = os.path.dirname(os.path.realpath(__file__))

        with open(self.dir_path + '/config/config.json', 'r') as j:
            configs = json.loads(j.read())

        self.og_columns = configs['data']['og_columns']
        self.generated_columns = configs['data']['generated_columns']
        # When generating the output samples (Y) of the supervised problem
        # add the columns you don't want to predict
        self.dont_predict = configs['data']['dont_predict']

        self.n_days_in = configs['parameters']['lookback_days']
        self.n_in = self.len_day * self.n_days_in  # Number of previous samples used to feed the Neural Network

        self.dataset_percentage_reduction = configs['parameters'][
            'dataset_percentage_reduction']

        self.city = city

        self.timer = Timer(city=self.city)

        self.availability_db_name = "Bicis_" + self.city + "_Availability"
        self.prediction_db_name = "Bicis_" + self.city + "_Prediction"

        self.db_password = "******"

        self.plotter = Plotter()
        self.utils = Utils(city=self.city)

        self.client = InfluxDBClient(self.db_ip, '8086', 'root', "root",
                                     self.availability_db_name)

        self.utils.check_and_create(["/data/" + self.city])
        self.utils.check_and_create([
            '/data/' + self.city + '/cluster/',
            "/data/" + self.city + "/filled", "/model/" + self.city,
            "/data/utils/", "/plots/" + self.city,
            "/data/" + self.city + "/supervised",
            "/data/" + self.city + "/scaled", "/data/" + self.city + "/filled",
            "/data/" + self.city + "/encoders",
            "/data/" + self.city + "/encoded_data"
        ])

        self.list_hours = [
            "00:00", "00:10", "00:20", "00:30", "00:40", "00:50", "01:00",
            "01:10", "01:20", "01:30", "01:40", "01:50", "02:00", "02:10",
            "02:20", "02:30", "02:40", "02:50", "03:00", "03:10", "03:20",
            "03:30", "03:40", "03:50", "04:00", "04:10", "04:20", "04:30",
            "04:40", "04:50", "05:00", "05:10", "05:20", "05:30", "05:40",
            "05:50", "06:00", "06:10", "06:20", "06:30", "06:40", "06:50",
            "07:00", "07:10", "07:20", "07:30", "07:40", "07:50", "08:00",
            "08:10", "08:20", "08:30", "08:40", "08:50", "09:00", "09:10",
            "09:20", "09:30", "09:40", "09:50", "10:00", "10:10", "10:20",
            "10:30", "10:40", "10:50", "11:00", "11:10", "11:20", "11:30",
            "11:40", "11:50", "12:00", "12:10", "12:20", "12:30", "12:40",
            "12:50", "13:00", "13:10", "13:20", "13:30", "13:40", "13:50",
            "14:00", "14:10", "14:20", "14:30", "14:40", "14:50", "15:00",
            "15:10", "15:20", "15:30", "15:40", "15:50", "16:00", "16:10",
            "16:20", "16:30", "16:40", "16:50", "17:00", "17:10", "17:20",
            "17:30", "17:40", "17:50", "18:00", "18:10", "18:20", "18:30",
            "18:40", "18:50", "19:00", "19:10", "19:20", "19:30", "19:40",
            "19:50", "20:00", "20:10", "20:20", "20:30", "20:40", "20:50",
            "21:00", "21:10", "21:20", "21:30", "21:40", "21:50", "22:00",
            "22:10", "22:20", "22:30", "22:40", "22:50", "23:00", "23:10",
            "23:20", "23:30", "23:40", "23:50"
        ]

        bah = self.utils.stations_from_web(self.city)
        bah.drop(bah.columns[[2, 3]], axis=1, inplace=True)

        self.station_dict = dict(zip(bah.values[:, 1], bah.values[:, 0]))

        self.listOfStations = list(bah.values[:, 1])

        self.utils.save_array_txt(
            self.dir_path + "/data/" + self.city + "/listOfStations",
            self.listOfStations)

        self.hour_encoder.fit(self.list_hours)

        self.weekday_encoder.classes_ = self.weekdays

    def read_dataset(self, no_date_split=False):
        """ Query the InfluxDB for all the availability data for a city.
		Data will be returnes in the form of a pandas.Dataframe and saved to disk in the
		../data/CITY/CITY.pkl cirectory
		"""

        self.timer.start()

        print("> Reading dataset")

        # If file already exists on disk check when was previously downloaded
        if os.path.isfile(self.dir_path + "/data/" + self.city + "/" +
                          self.city + ".pkl"):

            mtime = os.path.getmtime(self.dir_path + "/data/" + self.city +
                                     "/" + self.city + ".pkl")

            last_modified_date = datetime.fromtimestamp(mtime)

            timeDiff = datetime.now() - last_modified_date

            if timeDiff.days < self.dbDaysQueryThresshold:

                print("> Dataset was downloaded " + str(timeDiff.days) +
                      " days ago.")
                dataset = pd.read_pickle(self.dir_path + "/data/" + self.city +
                                         "/" + self.city + ".pkl")

                self.timer.stop("Dataset was downloaded " +
                                str(timeDiff.days) + " days ago.")

            # If the data os old enough query the server
            else:

                # Query to make to the db
                query_all = 'select * from bikes'

                dataset = pd.DataFrame(
                    self.client.query(query_all, chunked=True).get_points())

                #dataset.drop(dataset.columns[[0]], axis = 1, inplace = True)

                dataset["value"] = pd.to_numeric(dataset["value"])

                if no_date_split == False:

                    times = [
                        x.split("T")[1].replace('Z', '')[:-3]
                        for x in dataset.values[:, 1]
                    ]

                    f = lambda x: datetime.strptime(
                        x.split("T")[0], '%Y-%m-%d').timetuple().tm_yday
                    dataset["datetime"] = dataset["time"].apply(f)

                    f = lambda x: self.weekdays[datetime.strptime(
                        x.split("T")[0], '%Y-%m-%d').weekday()]
                    dataset["weekday"] = dataset["time"].apply(f)

                    dataset["time"] = times

                    # Eliminar muestras queno hayan sido recogidas correctamente a horas que no sean intervalos de 10 minutos
                    ps = [
                        "..:.1", "..:.2", "..:.3", "..:.4", "..:.5", "..:.6",
                        "..:.7", "..:.8", "..:.9"
                    ]

                    for p in ps:
                        dataset = dataset[~dataset['time'].str.contains(p)]
                        dataset = dataset[dataset['station_name'].isin(
                            self.listOfStations)]  # TODO: Debugging

                    # dataset = dataset[['datetime', 'time', 'weekday', 'station_name', 'label', 'value']]
                    dataset = dataset[self.generated_columns]

                else:
                    dataset['time'] = pd.to_datetime(dataset['time'])

                dataset = dataset.reset_index(
                    drop=True)  # Reset indexes, so they match the current row

                # Devuelve un DataFrame con las siguientes columnas
                # [ bikes, time, station_id, station_name, value ]
                # Tratar el df eliminando la primera columna y la de time dividir la fecha en day of the year (datetime) y time.
                dataset.to_pickle(
                    self.dir_path + "/data/" + self.city + "/" + self.city +
                    ".pkl")  #to save the dataframe, df to 123.pkl

                self.timer.stop("dataset downloaded from db")

        # File doesn't exist
        else:

            # Query to make to the db
            query_all = 'select * from bikes'

            dataset = pd.DataFrame(
                self.client.query(query_all, chunked=True).get_points())

            #dataset.drop(dataset.columns[[0]], axis = 1, inplace = True)

            dataset["value"] = pd.to_numeric(dataset["value"])

            if no_date_split == False:

                times = [
                    x.split("T")[1].replace('Z', '')[:-3]
                    for x in dataset.values[:, 1]
                ]

                dataset["datetime"] = dataset["time"]
                dataset["weekday"] = dataset["time"]

                f = lambda x: datetime.strptime(x.split("T")[0], '%Y-%m-%d'
                                                ).timetuple().tm_yday
                dataset["datetime"] = dataset["datetime"].apply(f)

                f = lambda x: self.weekdays[datetime.strptime(
                    x.split("T")[0], '%Y-%m-%d').weekday()]
                dataset["weekday"] = dataset["weekday"].apply(f)

                dataset["time"] = times

                # Eliminar muestras queno hayan sido recogidas correctamente a horas que no sean intervalos de 10 minutos
                ps = [
                    "..:.1", "..:.2", "..:.3", "..:.4", "..:.5", "..:.6",
                    "..:.7", "..:.8", "..:.9"
                ]

                for p in ps:
                    dataset = dataset[~dataset['time'].str.contains(p)]
                    dataset = dataset[dataset['station_name'].isin(
                        self.listOfStations)]  # TODO: Debugging

                # dataset = dataset[['datetime', 'time', 'weekday', 'station_name', 'label', 'value']]
                dataset = dataset[self.og_columns]

            else:
                dataset['time'] = pd.to_datetime(dataset['time'])

            dataset = dataset.reset_index(
                drop=True)  # Reset indexes, so they match the current row

            # Devuelve un DataFrame con las siguientes columnas
            # [ bikes, time, station_id, station_name, value ]
            # Tratar el df eliminando la primera columna y la de time dividir la fecha en day of the year (datetime) y time.
            dataset.to_pickle(self.dir_path + "/data/" + self.city + "/" +
                              self.city +
                              ".pkl")  #to save the dataframe, df to 123.pkl

            self.timer.stop("dataset downloaded from db")

        return dataset

    def encoder_helper(self, dataset):

        # Encode the columns represented by a String with an integer with LabelEncoder()
        values = dataset.values

        if "time" in self.generated_columns:
            hour_index = self.generated_columns.index("time")
            values[:, hour_index] = self.hour_encoder.transform(
                values[:, hour_index])  # Encode HOUR as an integer value

        if "weekday" in self.generated_columns:
            weekday_index = self.generated_columns.index("weekday")
            values[:, weekday_index] = self.weekday_encoder.transform(
                values[:, weekday_index])  # Encode WEEKDAY as an integer value

        if "station_name" in self.generated_columns:
            station_index = self.generated_columns.index("station_name")
            values[:, station_index] = self.station_encoder.transform(
                values[:, station_index])  # Encode STATION as an integer value

        self.save_encoders()

        return values

    def save_encoders(self):
        np.save(
            self.dir_path + '/data/' + self.city +
            '/encoders/hour_encoder.npy', self.hour_encoder.classes_)
        np.save(
            self.dir_path + '/data/' + self.city +
            '/encoders/weekday_encoder.npy', self.weekday_encoder.classes_)
        np.save(
            self.dir_path + '/data/' + self.city +
            '/encoders/station_encoder.npy', self.station_encoder.classes_)

    # Calls `series_to_supervised` and then returns a list of arrays, in each one are the values for each station
    def supervised_learning(self, scale=True):
        print("[SUPERVISED LEARNING]")
        self.timer.start()
        self.scaler = self.getMaximums()

        # Encontrar los índices de las columnas a borrar
        #################################################

        if "datetime" in self.generated_columns:
            weekday_index = self.generated_columns.index("datetime")

        list_of_indexes = []

        for to_delete in self.dont_predict:
            indices = [
                i for i, x in enumerate(self.generated_columns)
                if x == to_delete
            ]
            list_of_indexes.append(indices[0])

        # Generar los índices para todas las muestras que están a la salida

        indexesToKeep = []

        for out in range(self.n_out):
            indexesToKeep.append([
                x + len(self.generated_columns) * out for x in list_of_indexes
            ])

        # Lista `indexesToKeep` es una lista dentro de una lista [[a,b],[c,d]...], flatten para obtener una unica lista
        indexesToKeep = list(itertools.chain.from_iterable(indexesToKeep))

        # Añadir las muestras que faltan de los valores de entrada, esta desplazado hacia la derecha por eso
        indexesToKeep = [
            x + len(self.generated_columns) * self.n_in for x in indexesToKeep
        ]

        for idx, station in enumerate(self.listOfStations):

            try:
                # Load the previously processed data that has been filled with all possible holes
                dataset = np.load(self.dir_path + '/data/' + self.city +
                                  '/filled/' + self.station_dict[station] +
                                  '.npy')

                print("[" + str(idx) + "/" + str(len(self.listOfStations)) +
                      "] " + str(station),
                      end="\r")

                dataset = dataset.reshape(-1, dataset.shape[-1])

                if scale:
                    dataset = self.scaler_helper(
                        self.maximumBikesInStation[station], dataset)

                dataframe = pd.DataFrame(data=dataset,
                                         columns=self.generated_columns)

                supervised = self.series_to_supervised(self.generated_columns,
                                                       dataframe, self.n_in,
                                                       self.n_out)

                supervised = supervised.drop(supervised.columns[indexesToKeep],
                                             axis=1)

                # Eliminar cada N lineas para  no tener las muestras desplazadas
                rows_to_delete = []

                for j in range(supervised.shape[0]):
                    if j % self.n_in != 0:
                        rows_to_delete.append(j)

                supervised = supervised.drop(supervised.index[rows_to_delete])
                supervised = supervised.reset_index(drop=True)

                array_sum = np.sum(supervised.values)

                if np.isnan(array_sum):
                    print(supervised)
                    asdfasdF()

                self.utils.save_array_txt(
                    self.dir_path + "/data/" + self.city + "/supervised/" +
                    self.station_dict[station], supervised.values)
                np.save(
                    self.dir_path + "/data/" + self.city + "/supervised/" +
                    self.station_dict[station] + '.npy', supervised.values)

# 				supervised.to_excel(self.dir_path + "/data/" + self.city + "/supervised/" + self.station_dict[station] + '.xlsx')

            except (FileNotFoundError, IOError):
                print("Wrong file or file path in supervised learning (" +
                      '/data/' + self.city + '/scaled/' +
                      str(self.station_dict[station]) + ".npy)")

        aux = np.load(self.dir_path + "/data/" + self.city + "/supervised/" +
                      self.station_dict[self.listOfStations[0]] + ".npy")
        final_data = np.empty(aux.shape)

        for key, value in self.station_dict.items():

            try:
                data_read = np.load(self.dir_path + "/data/" + self.city +
                                    "/supervised/" + value + ".npy")
                os.remove(self.dir_path + "/data/" + self.city +
                          "/supervised/" + value + ".npy")
                final_data = np.append(final_data, data_read, 0)
                np.save(
                    self.dir_path + "/data/" + self.city + "/supervised/" +
                    str(value) + ".npy", final_data)
                os.remove(self.dir_path + "/data/" + self.city +
                          "/supervised/" + value + ".npy")

            except (FileNotFoundError, IOError):
                print("Wrong file or file path (" + "/data/" + self.city +
                      "/supervised/" + value + ".npy")

        self.utils.save_array_txt(
            self.dir_path + "/data/" + self.city + "/supervised/" + self.city,
            final_data)

        array_sum = np.sum(final_data)
        array_has_nan = np.isnan(array_sum)

        if array_has_nan:
            print(final_data)
            final_data = final_data[~np.isnan(final_data).any(axis=1)]
            print(final_data)

        np.save(
            self.dir_path + "/data/" + self.city + "/supervised/" + self.city +
            ".npy", final_data)

        return final_data

        self.timer.stop("Supervised learning")

    def series_to_supervised(self,
                             columns,
                             data,
                             n_in=1,
                             n_out=1,
                             dropnan=True):

        n_vars = 1 if type(data) is list else data.shape[1]
        dataset = DataFrame(data)
        cols, names = list(), list()
        # input sequence (t-n, ... t-1)
        for i in range(n_in, 0, -1):
            cols.append(dataset.shift(i))
            names += [(columns[j] + '(t-%d)' % (i)) for j in range(n_vars)]

        # forecast sequence (t, t+1, ... t+n)
        for i in range(0, n_out):
            cols.append(dataset.shift(-i))
            if i == 0:
                names += [(columns[j] + '(t)') for j in range(n_vars)]
            else:
                names += [(columns[j] + '(t+%d)' % (i)) for j in range(n_vars)]
        # put it all together
        agg = concat(cols, axis=1)
        agg.columns = names
        # drop rows with NaN values

        if dropnan:
            agg.dropna(inplace=True)

        return agg

    def iterate(self, dataset, cluster_data):
        """
		Iterate through all the stations and fill missing values.
		"""

        print("> Processing the data")

        # Crear diccionario que relacione las estaciones con su cluster
        #self.cluster_data = pd.read_csv(self.dir_path + "/data/" + self.city + "/cluster/cluster_stations.csv")
        self.cluster_data = cluster_data
        # Convert the DataFrame into a JSON
        # Key is the station_name & value is the cluster_id
        self.cluster_data = dict(
            zip(self.cluster_data.values[:, 0], self.cluster_data.values[:,
                                                                         1]))

        self.listOfStations = list(self.cluster_data.keys())
        self.station_encoder.classes_ = self.listOfStations

        path_to_save = os.path.join(self.dir_path, 'data', self.city, 'filled')

        self.timer.start()

        for idx, station in enumerate(self.listOfStations):

            # NO esta en cluster data asi que no me lo guardes
            if station not in self.cluster_data:
                self.station_dict.pop(station)

            if station not in self.station_dict:
                print("> Missing key " + station)
                self.listOfStations.remove(station)
                continue

            current_iteration = dataset[dataset['station_id'].isin(
                [self.station_dict[station]])]

            # If there aren't more than 2 weeks of data for that station discard it
            if current_iteration.shape[0] <= self.len_day * 7 * 2:
                print("> " + station + " has less than " + str(7 * 2) +
                      " days of data")
                continue

            current_iteration['time'] = pd.to_datetime(
                current_iteration['time'])

            firstSample = current_iteration['time'].iloc[0].strftime(
                '%Y-%m-%d')
            lastSample = current_iteration['time'].iloc[
                current_iteration.shape[0] - 1].strftime('%Y-%m-%d')

            print("[" + str(idx) + "/" + str(len(self.listOfStations)) + "] " +
                  station + " (" + str(firstSample) + " to " +
                  str(lastSample) + ")",
                  end='\r')

            time_range = pd.date_range(
                firstSample + 'T00:00:00Z',
                lastSample + 'T00:00:00Z',
                freq='1D').strftime('%Y-%m-%dT00:00:00Z')

            currentStationArray = np.empty(
                (0, self.len_day, len(self.generated_columns)))

            for i in range(0, (len(time_range) - 1)):

                query_all = "select * from bikes where station_id = \'" + str(
                    self.station_dict[station]) + "\' and time > \'" + str(
                        time_range[i]) + "\' and time < \'" + str(
                            time_range[i + 1]) + "\'"

                daily = pd.DataFrame(
                    self.client.query(query_all, chunked=True).get_points())

                # No proceses nada si el día no tiene más del 80% de las muestras, va a causar muchos errores
                if daily.size < int(self.len_day * 0.8): continue

                daily_range = pd.date_range(
                    time_range[i].split("T")[0] + ' 00:00:00+00:00',
                    time_range[i].split("T")[0] + ' 23:50:00+00:00',
                    freq='10T')

                daily['time'] = pd.to_datetime(daily['time'])
                daily['station_id'] = daily['station_id']
                daily['value'] = pd.to_numeric(daily['value'])

                weekday = self.weekdays[(daily_range[0]).weekday()]

                daily = daily.set_index(
                    keys=['time']).resample('10min').bfill()
                daily = daily.reindex(daily_range, fill_value=np.NaN)

                daily['value'] = daily['value'].interpolate(
                    limit_direction='both')
                daily['station_name'] = station
                daily['station_id'] = daily['station_id'].interpolate(
                    limit_direction='both')

                daily = daily.reset_index()

                daily['weekday'] = weekday
                daily['datetime'] = (daily_range[0]).timetuple().tm_yday
                daily["time"] = self.list_hours

                daily['label'] = daily['station_name']

                daily.drop(['station_id', 'index'], axis=1, inplace=True)

                daily = daily.replace({'label': self.cluster_data})

                # Reorder columns
                daily = daily[[
                    'datetime', 'time', 'weekday', 'station_name', 'label',
                    'value'
                ]]

                daily = pd.DataFrame(data=daily,
                                     columns=[
                                         'datetime', 'time', 'weekday',
                                         'station_name', 'label', 'value'
                                     ])

                daily = daily[self.generated_columns]

                # Encode columns that are strings to be numbers
                daily = self.encoder_helper(daily)

                daily = daily.reshape(
                    (1, self.len_day, len(self.generated_columns)))

                array_sum = np.sum(daily)
                array_has_nan = np.isnan(array_sum)

                if array_has_nan:
                    print(daily)
                    daily = daily[~np.isnan(daily).any(axis=1)]

                currentStationArray = np.concatenate(
                    (currentStationArray, daily), axis=0)

            aux_path = os.path.join(path_to_save, self.station_dict[station])

            np.save(aux_path, currentStationArray)

        self.timer.stop(" " + str(inspect.stack()[0][3]) + " for " + station +
                        " (" + self.station_dict[station] + ") " +
                        str(firstSample) + " to " + str(lastSample) + ")")

    maximumBikesInStation = {}

    # Read all the files and set the maximum values for each column,
    # RETURNS:
    #       · The scaler object
    def getMaximums(self):

        # Get the maximum values for
        scaler_aux = MinMaxScaler(feature_range=(0, 1))

        print("> Finding data range")

        a = np.empty((0, len(self.generated_columns)))

        for i in range(0, len(self.listOfStations)):

            try:
                dataset = np.load(self.dir_path + "/data/" + self.city +
                                  "/filled/" +
                                  self.station_dict[self.listOfStations[i]] +
                                  ".npy")

                if dataset.shape[1] == 0: continue

                dataset = dataset.reshape(-1, dataset.shape[-1])

                a = np.concatenate((a, dataset), axis=0)

                self.maximumBikesInStation[self.listOfStations[i]] = max(a[:,
                                                                           -1])

            except (FileNotFoundError, IOError):
                print("Wrong file or file path (" + self.dir_path + '/data/' +
                      self.city + '/scaled/' +
                      str(self.station_dict[self.listOfStations[i]]) + ".npy)")

        self.scaler.fit_transform(a)

        print(self.maximumBikesInStation)

        f = open(self.dir_path + "/data/" + self.city + "/Maximums.pkl", 'wb')
        pickle.dump(self.maximumBikesInStation, f)
        f.close()

        values_index = self.generated_columns.index("value")

        self.scaler.data_max_[values_index] = 100.0
        self.scaler.data_range_[values_index] = 100.0

        print("data min " + str(self.scaler.data_min_))
        print("data max " + str(self.scaler.data_max_))
        print("data rng " + str(self.scaler.data_range_))

        f = open(self.dir_path + "/data/" + self.city + "/MinMaxScaler.sav",
                 'wb')
        pickle.dump(self.scaler, f)
        f.close()

        return self.scaler

    def scaler_helper(self, maximumBikes, dataset):
        """
		Loads previously saved MinMaxScaler and scales an array.

		Parameters
		----------
		array : Numpy.ndarray((1,144,6))
				

		Returns
		-------
		no_missing_samples: Int
				Number of missing samples in the 
		missing_days: Int

		"""

        scaler = MinMaxScaler()

        f = open(self.dir_path + "/data/" + self.city + '/MinMaxScaler.sav',
                 'rb')
        scaler = pickle.load(f)
        f.close()

        values_index = self.generated_columns.index("value")

        dataset[:,
                values_index] = dataset[:, values_index] / maximumBikes * 100

        if dataset.shape[0] > 0:
            dataset = scaler.transform(dataset)

        return dataset

    def split_input_output(self, dataset, n_in, n_out):
        """
		Data has been previously shuffled
		"""

        x, y = dataset[:, range(0,
                                len(self.generated_columns) *
                                n_in)], dataset[:, -n_out:]  #dataset[:,n_out]

        x = x.reshape(
            (x.shape[0], n_in, len(self.generated_columns)))  # (...,n_in,4)

        return x, y

    def load_datasets(self):
        """
		Loads datasets used in the training from disk
		"""

        train_x = np.load(self.dir_path + '/data/' + self.city +
                          '/train_x.npy')
        train_y = np.load(self.dir_path + '/data/' + self.city +
                          '/train_y.npy')
        test_x = np.load(self.dir_path + '/data/' + self.city + '/test_x.npy')
        test_y = np.load(self.dir_path + '/data/' + self.city + '/test_y.npy')
        validation_x = np.load(self.dir_path + '/data/' + self.city +
                               '/validation_x.npy')
        validation_y = np.load(self.dir_path + '/data/' + self.city +
                               '/validation_y.npy')

        return train_x, train_y, validation_x, validation_y, test_x, test_y

    def split_sets(self, training_size, validation_size, test_size):
        """
		* Shuffle the dataset
		* Reduce (if necessary) the dataset's size
		* Create the train, validation & test datasets
		"""

        # Dataset with all the
        values = np.load(self.dir_path + "/data/" + self.city +
                         "/supervised/" + self.city + ".npy")

        # Reduce dataset's size as my computer cant handle all the dataset
        number_of_rows = values.shape[0]
        number_of_rows_trimmed = int(
            number_of_rows * (100 - self.dataset_percentage_reduction) / 100)

        print("> Datased thinned from " + str(number_of_rows) + " rows to " +
              str(number_of_rows_trimmed) + " rows")

        values = values[:number_of_rows_trimmed]

        # Calculate the number of samples for each set based on the overall dataset size
        train_size_samples = int(len(values) * training_size)
        validation_size_samples = int(len(values) * validation_size)
        test_size_samples = int(len(values) * test_size)

        # Previously the data was stored in an array the stations were contiguous, shuffle them so when splitting
        # the datasets every station is spreaded across the array
        np.random.shuffle(values)

        # Divide the dataset into the three smaller groups,
        # Each one contrains both the input and output values for the supervised problem
        train = values[0:train_size_samples, :]
        validation = values[train_size_samples:train_size_samples +
                            validation_size_samples, :]
        test = values[train_size_samples +
                      validation_size_samples:train_size_samples +
                      validation_size_samples + test_size_samples, :]

        # Get the input and output values for each subset
        train_x, train_y = self.split_input_output(train, self.n_in,
                                                   self.n_out)
        validation_x, validation_y = self.split_input_output(
            validation, self.n_in, self.n_out)
        test_x, test_y = self.split_input_output(test, self.n_in, self.n_out)

        # Save all the values to disk
        np.save(self.dir_path + '/data/' + self.city + '/train_x.npy', train_x)
        np.save(self.dir_path + '/data/' + self.city + '/train_y.npy', train_y)
        np.save(self.dir_path + '/data/' + self.city + '/test_x.npy', test_x)
        np.save(self.dir_path + '/data/' + self.city + '/test_y.npy', test_y)
        np.save(self.dir_path + '/data/' + self.city + '/validation_x.npy',
                validation_x)
        np.save(self.dir_path + '/data/' + self.city + '/validation_y.npy',
                validation_y)

        print("Train X " + str(train_x.shape))
        print("Train Y " + str(train_y.shape))
        print("Test X " + str(test_x.shape))
        print("Test Y " + str(test_y.shape))
        print("Validation X " + str(validation_x.shape))
        print("Validation Y " + str(validation_y.shape))

    def prepare_tomorrow(self, cluster_data=None):
        """
		Queries InfluxDB database for yesterday's data, fills possible holes in the dataset and saves it into a NumPy array to later be fed to the trained model.

		The predictions are for tomorrow but they have to be done on that same day so the data gathered for the day is complete. Starts querying the database for
		each station and for the availability between yesterday and today. Later it gives it the necessary format, encodes, normalizes it and then saves it for later
		use predicting tomorrow's values with the neural_model script.

		"""

        print("> Getting " + str(self.n_days_in) +
              " days of availability from the database")

        self.cluster_data = pd.read_csv(self.dir_path + "/data/" + self.city +
                                        "/cluster/cluster_stations.csv")

        # Load the dictionary that holds the maximum values per station name
        f = open(self.dir_path + "/data/" + self.city + "/Maximums.pkl", 'rb')
        self.maximumBikesInStation = pickle.load(f)
        f.close()

        print(cluster_data)

        self.cluster_data = dict(
            zip(self.cluster_data.values[:, 0], self.cluster_data.values[:,
                                                                         1]))

        self.hour_encoder.classes_ = np.load(self.dir_path + '/data/' +
                                             self.city +
                                             '/encoders/hour_encoder.npy')
        self.weekday_encoder.classes_ = np.load(
            self.dir_path + '/data/' + self.city +
            '/encoders/weekday_encoder.npy')
        self.station_encoder.classes_ = np.load(
            self.dir_path + '/data/' + self.city +
            '/encoders/station_encoder.npy')

        current_time = time.strftime('%Y-%m-%dT00:00:00Z',
                                     time.localtime(time.time()))

        d = time.strftime('%Y-%m-%dT00:00:00Z', time.localtime(time.time()))

        today = datetime.today()
        weekday = self.weekdays[(today -
                                 timedelta(days=self.n_days_in)).weekday()]
        yesterday = today - timedelta(days=self.n_days_in)
        yesterday = yesterday.strftime('%Y-%m-%dT00:00:00Z')
        today = today.strftime('%Y-%m-%dT00:00:00Z')

        informationList = {}

        for station in self.listOfStations:

            stationElement = {}

            if station not in self.station_dict:
                self.listOfStations.remove(station)
                continue

            # Occurs in cases where the station has stopped being available, therefore
            # no predictions can be made
            if station not in self.cluster_data:
                continue

            query = 'select * from bikes where time > \'' + str(
                yesterday
            ) + '\' and time < \'' + today + '\' and station_id=\'' + str(
                self.station_dict[station]) + '\''

            data = pd.DataFrame(
                self.client.query(query, chunked=True).get_points())

            # If no data is available for that station continue with the execution
            if data.size == 0: continue

            data['time'] = pd.to_datetime(data['time'])
            data['value'] = pd.to_numeric(data['value'])

            date_str = data['time'].iloc[0].strftime('%Y-%m-%d')
            date_str_end = data['time'].iloc[data.shape[0] -
                                             1].strftime('%Y-%m-%d')

            time_range = pd.date_range(date_str + ' 00:00:00+00:00',
                                       date_str_end + ' 23:50:00+00:00',
                                       freq='10T')

            data = data.set_index(keys=['time']).resample('10min').bfill()
            data = data.reindex(time_range, fill_value=np.NaN)

            data['value'] = data['value'].interpolate(limit_direction='both')
            data['station_name'] = station
            data['station_id'] = data['station_id'].interpolate(
                limit_direction='both')

            data = data.reset_index()

            data['weekday'] = weekday

            if data.shape[0] < self.n_in: continue

            data['datetime'] = (datetime.today() -
                                timedelta(days=1)).timetuple().tm_yday
            data["time"] = self.list_hours * self.n_days_in

            data.drop(['station_id', 'index'], axis=1, inplace=True)

            data['label'] = self.cluster_data[station]

            data = data[self.generated_columns]  # Sort the DataFrame's columns

            if station not in self.maximumBikesInStation:
                continue

            # Encode columns that are strings to be numbers
            data = self.encoder_helper(data)

            data = self.scaler_helper(self.maximumBikesInStation[station],
                                      data)

            # Reshape the data to be 3-Dimensional
            data = data.reshape(1, self.n_in, len(self.generated_columns))

            informationList[station] = data

        return informationList
예제 #50
0
async def cmd_lb(ctx):
    """
    Usage``:
        lb [day | week | month]
    Description:
        Display the total timer time of each guild member, within the specified period.
        The periods are rolling, i.e. `day` means the last 24h.
        Without a period specified, the all-time totals will be shown.
    Parameters::
        day: Show totals of sessions within the last 24 hours
        week: Show totals of sessions within the last 7 days
        month: Show totals of sessions within the last 31 days
    """
    out_msg = await ctx.reply("Generating leaderboard, please wait.")

    # Get the past sessions for this guild
    sessions = ctx.client.interface.registry.get_sessions_where(
        guildid=ctx.guild.id)

    if not sessions:
        return await ctx.reply(
            "This guild has no past group sessions! Please check back soon.")

    # Current utc timestamp
    now = Timer.now()

    # Determine maximum time separation allowed for sessions
    region = ctx.arg_str.lower().strip()
    if not region or region == 'all':
        max_dist = now
        head = "All-time leaderboard"
    elif region == 'day':
        max_dist = 60 * 60 * 24
        head = "Daily leaderboard"
    elif region == 'week':
        max_dist = 60 * 60 * 24 * 7
        head = "Weekly leaderboard"
    elif region == 'month':
        max_dist = 60 * 60 * 24 * 31
        head = "Monthly leaderboard"
    else:
        return await ctx.error_reply(
            "Unknown region specification `{}`.".format(ctx.arg_str))

    # Tally total session times
    total_dict = {}
    for session in sessions:
        if now - session['starttime'] > max_dist:
            continue

        if session['userid'] not in total_dict:
            total_dict[session['userid']] = 0
        total_dict[session['userid']] += session['duration']

    for guildid, userid in ctx.client.interface.subscribers:
        if guildid == ctx.guild.id:
            sub_data = ctx.client.interface.subscribers[(
                guildid, userid)].session_data()
            if userid not in total_dict:
                total_dict[userid] = 0
            total_dict[userid] += sub_data[4]

    # Reshape and sort the totals
    totals = sorted(list(total_dict.items()),
                    key=lambda tup: tup[1],
                    reverse=True)

    # Build the string pairs
    total_strs = []
    for userid, total in totals:
        # Find the user
        user = ctx.client.get_user(userid)
        if user is None:
            try:
                user = await ctx.client.fetch_user(userid)
                user_str = user.name
            except discord.NotFound:
                user_str = str(userid)
        else:
            user_str = user.name

        total_strs.append((user_str, _parse_duration(total)))

    # Build pages in groups of 20
    blocks = [total_strs[i:i + 20] for i in range(0, len(total_strs), 20)]
    max_block_lens = [
        len(max(list(zip(*block))[0], key=len)) for block in blocks
    ]
    page_blocks = [[
        "{0[0]:^{max_len}} {0[1]:>10}".format(pair, max_len=max_block_lens[i])
        for pair in block
    ] for i, block in enumerate(blocks)]

    num = len(page_blocks)
    pages = []
    for i, block in enumerate(page_blocks):
        header = head + " (Page {}/{})".format(i + 1, num) if num > 1 else head
        header_rule = "=" * len(header)
        page = "```md\n{}\n{}\n{}```".format(header, header_rule,
                                             "\n".join(block))
        pages.append(page)

    await out_msg.delete()
    if not pages:
        return await ctx.reply("No entries exist in the given range!")

    await ctx.pager(pages, locked=False)
class Process:
    DEFAULT_PORT = 37022
    ELECTION_PORT = 37023
    SYNCHRONIZE_TIME_PORT = 37024
    PING_COORDINATOR_PORT = 37025
    SYNCHRONIZATION_TIME = 4
    COORDINATOR_PING_TIME = 15

    def __init__(self):
        self.isCoordinator = False
        self.pid = randint(0, 1000)
        self.timer = Timer(increment=randint(1, 5))
        self.__initSockets()

        print('My id is: %s' % str(self.pid))
        listener = threading.Thread(target=self.__listenMessages)
        election = threading.Thread(target=self.__startElection)
        pingToCoordinator = threading.Timer(self.COORDINATOR_PING_TIME,
                                            self.__pingCoordinator)
        raw_input('Press Enter to continue...')
        listener.start()
        election.start()
        pingToCoordinator.start()
        self.__randomPing(randint(10, 20))

    def __str__(self):
        return 'pid: ' + str(self.pid)

    def __listenMessages(self):
        print('Waiting messages...')
        client = socket(AF_INET, SOCK_DGRAM)
        client.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
        client.bind(("", self.DEFAULT_PORT))

        while (True):
            print('coordinator: ' + str(self.isCoordinator))
            print('Timer: ' + str(self.timer.getTime()))
            data, addr = client.recvfrom(1024)
            message = pickle.loads(data)
            if (message.sourceId != self.pid):
                self.__handleMessage(message, addr)

    def __handleMessage(self, message, addr):
        print('Received message: %s' % message.subject)
        if (message.subject == "ping"):
            print(message.getMessage())
        elif (message.subject == "election"):
            self.isCoordinator = False
            if (self.pid > message.sourceId):
                self.__electionResponse(addr)
                election = threading.Thread(target=self.__startElection)
                election.start()
        elif (message.subject == "synchronization"):
            self.__SyncTimeRequest(addr)
        elif (message.subject == "time update"):
            print("Timer updated to " + str(message.getMessage()))
            self.timer.setTime(message.getMessage())
        elif (message.subject == "coordinator ping"):
            if (self.isCoordinator):
                print(message.getMessage())
                message = CoordinatorPingResponseMessage(
                    self.pid, message.sourceId)
                self.__sendMessage(message, addr[0],
                                   self.PING_COORDINATOR_PORT)

    def __sendBroadcastMessage(self, message):
        data = pickle.dumps(message)
        self.broadcastSocket.sendto(data, ('<broadcast>', self.DEFAULT_PORT))

    def __sendMessage(self, message, address, port):
        data = pickle.dumps(message)
        self.udpSocket.sendto(data, (address, port))

    def __initSockets(self):
        self.udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
        self.broadcastSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
        self.broadcastSocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)

    def __randomPing(self, interval):
        message = PingMessage(self.pid, 0)
        self.__sendBroadcastMessage(message)
        threading.Timer(interval, self.__randomPing, args=[interval]).start()

    def __electionResponse(self, address):
        message = ElectionResponseMessage(self.pid, 0)
        self.__sendMessage(message, address[0], self.ELECTION_PORT)

    def __startElection(self):
        print('Starting election...')
        electionSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
        electionSocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
        electionSocket.settimeout(0.5)
        try:
            electionSocket.bind(("", self.ELECTION_PORT))
        except:
            electionSocket.close()
            return

        messages = []
        message = ElectionMessage(self.pid, 0)
        self.__sendBroadcastMessage(message)

        try:
            while (True):
                data, addr = electionSocket.recvfrom(1024)
                messages.append(pickle.loads(data))
        except timeout:
            print('Received %s election messages responses' % (len(messages)))

        print('messages:')
        print(messages)
        if len(messages) == 0:
            self.isCoordinator = True
            print('I\'m the new coordinator')
            threading.Timer(self.SYNCHRONIZATION_TIME,
                            self.__synchronizeTimer).start()
        else:
            self.isCoordinator = False
            print('I lost the election')

        electionSocket.close()

    def __synchronizeTimer(self, interval=SYNCHRONIZATION_TIME):
        if self.isCoordinator:
            print('Starting time synchronization...')
            timerSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
            timerSocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
            timerSocket.settimeout(0.5)
            timerSocket.bind(("", self.SYNCHRONIZE_TIME_PORT))

            time_list = [self.timer.getTime()]
            message = SynchronizeTimeMessage(self.pid, 0)
            self.__sendBroadcastMessage(message)

            try:
                while (True):
                    data, addr = timerSocket.recvfrom(1024)
                    time_list.append(pickle.loads(data).getMessage())
            except timeout:
                print('Received %s time message responses' %
                      (len(time_list) - 1))

            updatedTime = reduce(lambda x, y: x + y,
                                 time_list) / len(time_list)
            message = UpdateTimeMessage(self.pid, 0, updatedTime)
            print("Timer updated to " + str(updatedTime))
            self.timer.setTime(updatedTime)
            self.__sendBroadcastMessage(message)
            threading.Timer(self.SYNCHRONIZATION_TIME,
                            self.__synchronizeTimer).start()
            timerSocket.close()

    def __SyncTimeRequest(self, addr):
        message = SynchronizeTimeResponseMessage(self.pid, 0,
                                                 self.timer.getTime())
        self.__sendMessage(message, addr[0], self.SYNCHRONIZE_TIME_PORT)

    def __initBroadcastSocket(self, port):
        broadcastSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
        broadcastSocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
        broadcastSocket.bind(("", port))
        return broadcastSocket

    def __pingCoordinator(self):
        if (not self.isCoordinator):
            print('Pinging coordinator...')
            pingSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
            pingSocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
            pingSocket.bind(("", self.PING_COORDINATOR_PORT))
            pingSocket.settimeout(1)
            message = CoordinatorPingMessage(self.pid, 0)
            self.__sendBroadcastMessage(message)

            try:
                data, addr = pingSocket.recvfrom(1024)
                print('Coordinator is alive')
            except timeout:
                print('Coordinator is dead')
                election = threading.Thread(target=self.__startElection)
                election.start()

            pingSocket.close()

        threading.Timer(self.COORDINATOR_PING_TIME,
                        self.__pingCoordinator).start()
예제 #52
0
	def __init__(self, screen, levels):

		self.screen = screen

		self.sprites = Babe_Sprites().babe_images

		self.levels = levels

		self.level = self.levels.max_level

		self.timer = Timer()

		# Booleans

		self.isWalk = False

		self.isCrouch = False

		self.isFalling = False

		self.isKiss = False

		self.hasKissed = False

		self.collideBottom = False

		self.lastCollision = True

		# Animation

		self.walkCount = 0

		self.x, self.y = 375, 113

		self.width, self.height = 32, 32

		self.rect_x, self.rect_y = self.x + 1, self.y + 7

		self.rect_width, self.rect_height = self.width - 12, self.height - 8

		self.current_image = self.sprites["Babe_Stand1"]

		# Particles

		self.jump_particle = King_Particle("images\\particles\\jump_particle.png", 5, 1, 32)

		self.snow_jump_particle = King_Particle("images\\particles\\snow_jump_particle.png", 4, 3, 36)

		self.isJump = False

		self.isLanded = False

		# Audio

		self.channel = pygame.mixer.Channel(10)

		self.audio = Babe_Audio().audio

		# Physics

		self.physics = Physics()

		self.speed, self.angle = 0, 0

		self.maxSpeed = 11

		self.walkAngles = {"right" : math.pi/2, "left" : -math.pi/2}

		self.slip = 0

		# Ending

		self.ending_distance = 50
예제 #53
0
if __name__ == "__main__":
    likelihood = 1 / (96**7) + 1 / (96**8)
    likelihood

if __name__ == "__main__":
    1 / likelihood

if __package__ is None or __package__ == "":
    from Timer import Timer
else:
    from .Timer import Timer

if __name__ == "__main__":
    trials = 1000
    with Timer() as t:
        for i in range(trials):
            try:
                url = fuzzer()
                result = http_program(url)
                print("Success!")
            except ValueError:
                pass

    duration_per_run_in_seconds = t.elapsed_time() / trials
    duration_per_run_in_seconds

if __name__ == "__main__":
    seconds_until_success = duration_per_run_in_seconds * (1 / likelihood)
    seconds_until_success
예제 #54
0

if __name__ == "__main__":
    long_expr_input = GrammarFuzzer(EXPR_GRAMMAR, min_nonterminals=100).fuzz()
    long_expr_input


if __package__ is None or __package__ == "":
    from Timer import Timer
else:
    from .Timer import Timer


if __name__ == "__main__":
    grammar_reducer = GrammarReducer(eval_mystery, EarleyParser(EXPR_GRAMMAR))
    with Timer() as grammar_time:
        print(grammar_reducer.reduce(long_expr_input))


if __name__ == "__main__":
    grammar_reducer.tests


if __name__ == "__main__":
    grammar_time.elapsed_time()


if __name__ == "__main__":
    dd_reducer = DeltaDebuggingReducer(eval_mystery)
    with Timer() as dd_time:
        print(dd_reducer.reduce(long_expr_input))
예제 #55
0
class Cluster:

    # Specify if the clustering has to be done
    weekday_analysis = True
    city = ""
    n_clusters = -1
    plotter = Plotter()
    dir_path = ""
    locations = ""
    position = ""

    Ks = range(1, 11)

    def __init__(self, weekday_analysis=True, city=""):
        self.weekday_analysis = weekday_analysis
        self.city = city
        self.dir_path = os.path.dirname(os.path.realpath(__file__))
        self.timer = Timer(city=self.city)

        self.utils = Utils(city=self.city)

        if weekday_analysis is True:
            self.type_of_analysis = "weekday"
        else:
            self.type_of_analysis = "weekend"

        self.utils.check_and_create([
            "/data/" + self.city + "/cluster/cluster_data", "/data/" +
            self.city + "/cluster/cluster_data/" + self.type_of_analysis,
            "/plots/" + self.city + "/cluster/"
        ])

    def do_cluster(self):
        if os.path.isfile(self.dir_path + "/data/" + self.city +
                          "/cluster/cluster_stations.csv"):

            mtime = os.path.getmtime(self.dir_path + "/data/" + self.city +
                                     "/cluster/cluster_stations.csv")

            last_modified_date = datetime.fromtimestamp(mtime)

            timeDiff = datetime.now() - last_modified_date

            return pd.read_csv(self.dir_path + "/data/" + self.city +
                               "/cluster/cluster_stations.csv")

            # if timeDiff.days < 15:
# 				return pd.read_csv(self.dir_path + "/data/" + self.city + "/cluster/cluster_stations.csv")
# 			else:
# 				self.d = Data_mgmt(city=self.city)
#
# 				print("> Reading dataset from DB")
# 				raw = self.d.read_dataset(no_date_split=True)
#
# 				self.timer.start()
# 				labels = self.cluster_analysis("weekday", raw)
# 				self.timer.stop("Cluster analysis done, found " + str(len(labels)) + " clusters/")
#
# 				return labels

        else:
            self.d = Data_mgmt(city=self.city)

            print("> Reading dataset from DB")
            raw = self.d.read_dataset(no_date_split=True)

            self.timer.start()
            labels = self.cluster_analysis("weekday", raw)
            self.timer.stop("Cluster analysis done, found " +
                            str(len(labels)) + " clusters/")

            return labels

    # Type is weekday or weekend
    def cluster_analysis(self, type, raw_data):

        self.locations = self.utils.stations_from_web(city=self.city)
        self.position = [
            self.locations['lat'].iloc[0], self.locations['lon'].iloc[0]
        ]

        max_bikes = raw_data.groupby('station_name')['value'].max()

        print("> There are " + str(max_bikes.shape[0]) + " stations")

        wrong_stations = max_bikes[max_bikes == 0].index.tolist()

        well_station_mask = np.logical_not(
            raw_data['station_name'].isin(wrong_stations))

        data = raw_data[well_station_mask]

        # Time resampling, get data every 5 minutes
        df = (data.set_index('time').groupby('station_name')['value'].resample(
            '10T').mean().bfill())

        df = df.unstack(0)

        # Daily profile getting rid out of sat and sun
        weekday = df.index.weekday

        title = "Cluster analysis for " + sys.argv[1]

        if type == "weekday":
            mask = weekday < 5
            title += " on weekdays"
            type_of_analysis = "weekday"
        else:
            mask = weekday > 4
            title += " on weekends"
            type_of_analysis = "weekend"

        df['hour'] = df.index.hour

        df = df.groupby('hour').mean()

        # normalization
        df_norm = df / df.max()

        # Some values vould be nil producing
        # Input contains NaN, infinity or a value too large for dtype('float64')

        pd.set_option('display.max_columns', None)

        df_norm = df_norm.dropna(axis=1)

        df_norm = df_norm.replace([np.inf, -np.inf], np.nan)
        df_norm = df_norm.fillna(df_norm.mean())

        df_norm.index.name = 'id'

        distortions = []

        for k in self.Ks:
            kmeanModel = KMeans(n_clusters=k)
            kmeanModel.fit(df_norm.T)
            distortions.append(kmeanModel.inertia_)

        kneedle = KneeLocator(self.Ks,
                              distortions,
                              curve='convex',
                              direction='decreasing')

        self.n_clusters = round(kneedle.knee)

        plt.figure(figsize=(15, 9))

        plt.xlabel('Hour')
        plt.xticks(np.linspace(0, 24, 13))
        plt.yticks(np.linspace(0, 100, 11))
        plt.ylabel("Available bikes (%)")

        plt.title(title)
        sns.despine()

        ax = plt.axes(frameon=True)

        # 		ax.spines["top"].set_visible(False)
        # 		ax.spines["bottom"].set_visible(False)
        # 		ax.spines["right"].set_visible(False)
        # 		ax.spines["left"].set_visible(False)

        ax.set_xlim(left=0, right=11)
        ax.xaxis.label.set_visible(False)

        plt.plot(self.Ks, distortions, 'bx-')
        plt.axvline(x=self.n_clusters, linewidth=4, color='r')
        plt.title('The Elbow Method showing the optimal k (' +
                  str(self.n_clusters) + ")")
        plt.savefig(self.dir_path + "/plots/" + self.city +
                    "/cluster/elbow_method.png")
        plt.close()

        distortions_df = pd.DataFrame(distortions)

        distortions_df.to_csv(self.dir_path + "/data/" + self.city +
                              "/cluster/distortions.csv",
                              index_label='id',
                              header=['values'])

        kmeans = KMeans(n_clusters=self.n_clusters,
                        random_state=0).fit(df_norm.T)
        label = pd.Series(kmeans.labels_)

        colors = sns.color_palette('bright', self.n_clusters)

        sns.palplot(colors)

        cluster_df = pd.DataFrame(kmeans.cluster_centers_)

        (cluster_df.T).to_csv(self.dir_path + "/data/" + self.city +
                              "/cluster/cluster_data/data.csv",
                              index_label='id')

        with sns.axes_style("darkgrid", {'xtick.major.size': 8.0}):
            fig, ax = plt.subplots(figsize=(10, 6))

        for k, label, color in zip(kmeans.cluster_centers_,
                                   range(self.n_clusters), colors):
            plt.plot(100 * k, color=color, label=label)

        plt.legend()
        plt.xlabel('Hour')
        plt.xticks(np.linspace(0, 24, 13))
        plt.yticks(np.linspace(0, 100, 11))
        plt.ylabel("Available bikes (%)")

        plt.title(title)
        sns.despine()
        plt.savefig(self.dir_path + "/plots/" + self.city + "/cluster/" +
                    str(sys.argv[1]) + "_pattern_" + type_of_analysis + ".png")

        mask = np.logical_not(self.locations['nom'].isin(wrong_stations))

        self.locations = self.locations[mask]

        dflabel = pd.DataFrame({"label": kmeans.labels_},
                               index=df_norm.columns)

        self.locations = self.locations.merge(dflabel,
                                              right_index=True,
                                              left_on='nom')

        self.locations.drop_duplicates(inplace=True)

        mp = folium.Map(location=self.position,
                        zoom_start=13,
                        tiles='cartodbpositron')

        hex_colors = colors.as_hex()

        for _, row in self.locations.iterrows():

            folium.CircleMarker(location=[row['lat'], row['lon']],
                                radius=5,
                                popup=row['nom'],
                                color=hex_colors[row['label']],
                                fill=True,
                                fill_opacity=0.5,
                                foll_color=hex_colors[row['label']]).add_to(mp)

        mp.save(self.dir_path + "/plots/" + self.city + "/cluster/" +
                str(sys.argv[1]) + "_map_" + type_of_analysis + ".html")

        dflabel = dflabel.reset_index()

        labels_dict = dict(zip(dflabel.station_name, dflabel.label))

        for label in dflabel.label.unique():

            if not os.path.exists(self.dir_path + "/data/" + self.city +
                                  "/cluster/cluster_data/" + str(label)):
                os.makedirs(self.dir_path + "/data/" + self.city +
                            "/cluster/cluster_data/" + str(label))

            result = [k for k, v in labels_dict.items() if v == label]

            plt.close()
            plt.legend()
            plt.figure(figsize=(15, 9))

            plt.xlabel('Hour')
            plt.xticks(np.linspace(0, 24, 13))
            plt.yticks(np.linspace(0, 100, 11))
            plt.ylabel("Available bikes (%)")

            plt.title(title)
            sns.despine()

            ax.spines["top"].set_visible(False)
            ax.spines["bottom"].set_visible(False)
            ax.spines["right"].set_visible(False)
            ax.spines["left"].set_visible(False)
            ax = plt.axes(frameon=False)

            ax.set_xlim(left=0, right=24)
            ax.xaxis.label.set_visible(False)

            plt.title(title + " for cluster name " + str(label))
            plt.savefig(self.dir_path + "/plots/" + self.city + "/cluster/" +
                        str(sys.argv[1]) + "_pattern_" + type_of_analysis +
                        "_cluster_" + str(label) + ".png")
            plt.close()

        dflabel.to_csv(self.dir_path + "/data/" + self.city + "/cluster/" +
                       "cluster_stations.csv",
                       index=False)

        return dflabel
예제 #56
0
class Blade(object):

    # Call constructor
    def __init__(self, rect, image, timeLength, secondsElapsed):
        self.Rect = rect
        
        self.image = image
        self.texture = self.image #self.texture is the transform of self.image
        
        self.rotation = 0
        self.rotationVel = 12

        self.active = True
        self.existenceTimer = Timer(timeLength, secondsElapsed)
        self.zombiesDamaged = []
        self.zombiesDamagedTimer = []

    # Get if the blade is active
    def getActive(self):
        return self.active

    # Set if the blade is active
    def setActive(self, activity):
        self.active = activity

    # Get the rectangle
    def getRect(self):
        return self.Rect

    # Set the rectangle
    def setRect(self, rect):
        self.Rect = rect

    # Check if you can attack a zombie
    def canAttack(self, zombie, secondsElapsed):
        for attackedZombie in range(len(self.zombiesDamaged)): # run a for loop for all attacked zombies
            if self.zombiesDamaged[attackedZombie] == zombie: # if the current attacked zombie is the current zombie
                for currentTimer in range(len(self.zombiesDamagedTimer)): # run a for loop for all zombie attack timers
                    timer = self.zombiesDamagedTimer[currentTimer] # unpack tuple
                    if timer[0] == zombie: # if the timer is assigned for this current zombie
                        break # end the timer for loop
                    timer = None # if it's reached this point than the zombie has no timer
                if timer != None: # if a timer was found
                    if timer[1].getAlert() == True: # if it's attack timer has finished
                        return True # say it can attack
                    else:
                        timer[1].update(secondsElapsed) # update the timer
                        return False # say it can't attack
        return True # if it's gotten to this point, it has no timer therefore can attack

    # Update the blade
    def update(self, zombies, player, game_Clock):
        if self.existenceTimer.getAlert() == True: # if the blade is finished
            self.setActive(False) # Set the blade inactive
        else: # UPDATE BLADE:
            self.existenceTimer.update(game_Clock.getElapsedSeconds()) # update existence timer
            # Rotate blade:
            self.rotation += self.rotationVel
            self.texture = pygame.transform.rotate(self.image, self.rotation)
            self.setRect(self.texture.get_rect(center=(self.getRect().center))) # Center blade
            
            # Check collision with zombies:
            for zombie in range(len(zombies)): # run a for loop for all zombies
                if zombies[zombie].getLifeStatus() == True: # check if the zombie is active
                    if self.Rect.colliderect(zombies[zombie].getRect()) == True and self.canAttack(zombie, game_Clock.getElapsedSeconds()) == True: # if the blade collides with the zombie and can attack it
                        zombies[zombie].changeHealth(-2)
                        player.addScore(2) # give the player 2 score
                        self.zombiesDamaged.append(zombie)
                        self.zombiesDamagedTimer.append((zombie, Timer(1, game_Clock.getElapsedSeconds()))) # set a timer for when the blade can attack the zombie again

    # Draw the blade
    def draw(self, canvas):
        canvas.blit(self.texture, self.getRect()) # draw blade with rotation
예제 #57
0
class Items(Sprite):
    def __init__(self,screen, animation):
        super().__init__()
        self.screen=screen
        self.animation = animation
        self.animation_timer = Timer(frames=self.animation)
        self.image = self.animation_timer.imagerect()
        self.rect = self.image.get_rect()
        self.floor=False
        self.obstacleR=False
        self.obstacleL=False
        self.speedx = 2
        self.speedy = 4
        self.bound = False

    def blitme(self):
        self.screen.blit(self.image, self.rect)
        
    def check_collisions(self, level):
        for blocks in level.environment:
            if (pygame.sprite.collide_rect(self, blocks)):
                # floor==========================================================================================
                if str(type(blocks)) == "<class 'Brick.Floor'>" and self.rect.bottom >= blocks.rect.top:
                    self.floor = True
                    self.rect.y = blocks.rect.y - 32

                # sides===========================================================================================
                # PIPE-------------------------------------------------------------------------------------------
                if str(type(blocks)) == "<class 'Brick.Pipe'>" \
                        and (self.rect.left <= blocks.rect.right or self.rect.right >= blocks.rect.left) \
                        and self.rect.bottom > blocks.rect.top \
                        and self.rect.top > blocks.rect.top - 16:
                    if self.rect.right >= blocks.rect.left \
                            and not self.obstacleL \
                            and self.rect.left < blocks.rect.left:
                        self.rect.right = blocks.rect.left - 1
                        self.obstacleR = True
                        self.speedx = self.speedx * (-1)
                    else:
                        self.obstacleR = False
                    if self.rect.left <= blocks.rect.right \
                            and not self.obstacleR \
                            and self.rect.right > blocks.rect.right:
                        self.rect.left = blocks.rect.right + 1
                        self.obstacleL = True
                        self.speedx = self.speedx * (-1)
                    else:
                        self.obstacleL = False
                # BASIC--------------------------------------------------------------------------------------------
                elif str(type(blocks)) == "<class 'Brick.Basic'>" \
                        and (self.rect.left <= blocks.rect.right or self.rect.right >= blocks.rect.left) \
                        and self.rect.bottom > blocks.rect.top \
                        and self.rect.top > blocks.rect.top - 16 \
                        and self.rect.bottom <= blocks.rect.bottom:
                    if self.rect.right >= blocks.rect.left \
                            and not self.obstacleL \
                            and self.rect.left < blocks.rect.left:
                        self.rect.right = blocks.rect.left - 1
                        self.obstacleR = True
                        self.speedx = self.speedx * (-1)
                    else:
                        self.obstacleR = False
                    if self.rect.left <= blocks.rect.right \
                            and not self.obstacleR \
                            and self.rect.right > blocks.rect.right:
                        self.rect.left = blocks.rect.right + 1
                        self.obstacleL = True
                        self.speedx = self.speedx * (-1)
                    else:
                        self.obstacleL = False

                # QUESTION------------------------------------------------------------------------------------------
                elif str(type(blocks)) == "<class 'Brick.Question'>" \
                        and (self.rect.left <= blocks.rect.right or self.rect.right >= blocks.rect.left) \
                        and self.rect.bottom > blocks.rect.top \
                        and self.rect.top > blocks.rect.top - 16 \
                        and self.rect.bottom <= blocks.rect.bottom:
                    if self.rect.right >= blocks.rect.left \
                            and not self.obstacleL \
                            and self.rect.left < blocks.rect.left:
                        self.rect.right = blocks.rect.left - 1
                        self.obstacleR = True
                        self.speedx = self.speedx * (-1)
                    else:
                        self.obstacleR = False
                    if self.rect.left <= blocks.rect.right \
                            and not self.obstacleR \
                            and self.rect.right > blocks.rect.right:
                        self.rect.left = blocks.rect.right + 1
                        self.obstacleL = True
                        self.speedx = self.speedx * (-1)
                    else:
                        self.obstacleL = False
                # INTERACTABLEV------------------------------------------------------------------------------------------
                elif str(type(blocks)) == "<class 'Brick.InteractableV'>" \
                        and (self.rect.left <= blocks.rect.right or self.rect.right >= blocks.rect.left) \
                        and self.rect.bottom > blocks.rect.top \
                        and self.rect.top > blocks.rect.top - 16 \
                        and self.rect.bottom <= blocks.rect.bottom:
                    if self.rect.right >= blocks.rect.left \
                            and not self.obstacleL \
                            and self.rect.left < blocks.rect.left:
                        self.rect.right = blocks.rect.left - 1
                        self.obstacleR = True
                        self.speedx = self.speedx * (-1)
                    else:
                        self.obstacleR = False
                    if self.rect.left <= blocks.rect.right \
                            and not self.obstacleR \
                            and self.rect.right > blocks.rect.right:
                        self.rect.left = blocks.rect.right + 1
                        self.obstacleL = True
                        self.speedx = self.speedx * (-1)
                    else:
                        self.obstacleL = False
                # RESET-----------------------------------------------------------------------------------------------
                else:
                    self.obstacleR = False
                    self.obstacleL = False

                # top==================================================================================================
                # PIPE-----------------------------------------------------------------------------------------------
                if str(type(blocks)) == "<class 'Brick.Pipe'>" \
                        and (self.rect.left < blocks.rect.right - 5 and self.rect.right > blocks.rect.left + 5) \
                        and self.rect.bottom > blocks.rect.top - 32 \
                        and not self.obstacleL and not self.obstacleR:
                    self.floor = True
                    self.rect.y = blocks.rect.y - 32
                # BASIC-----------------------------------------------------------------------------------------------
                if str(type(blocks)) == "<class 'Brick.Basic'>" \
                        and (self.rect.left < blocks.rect.right - 5 and self.rect.right > blocks.rect.left + 5) \
                        and self.rect.bottom > blocks.rect.top - 32 \
                        and self.rect.top < blocks.rect.top \
                        and not self.obstacleL and not self.obstacleR:
                    self.floor = True
                    self.rect.y = blocks.rect.y - 32
                # QUESTION-----------------------------------------------------------------------------------------------
                if str(type(blocks)) == "<class 'Brick.Question'>" \
                        and (self.rect.left < blocks.rect.right - 5 and self.rect.right > blocks.rect.left + 5) \
                        and self.rect.bottom > blocks.rect.top - 32 \
                        and self.rect.top < blocks.rect.top \
                        and not self.obstacleL and not self.obstacleR:
                    self.floor = True
                    self.rect.y = blocks.rect.y - 32
                # INTERACTABLEV-----------------------------------------------------------------------------------------------
                if str(type(blocks)) == "<class 'Brick.InteractableV'>" \
                        and (self.rect.left < blocks.rect.right - 5 and self.rect.right > blocks.rect.left + 5) \
                        and self.rect.bottom > blocks.rect.top - 32 \
                        and self.rect.top < blocks.rect.top \
                        and not self.obstacleL and not self.obstacleR:
                    self.floor = True
                    self.rect.y = blocks.rect.y - 32
                # INTERACTABLEH-----------------------------------------------------------------------------------------------
                if str(type(blocks)) == "<class 'Brick.InteractableH'>" \
                        and (self.rect.left < blocks.rect.right - 5 and self.rect.right > blocks.rect.left + 5) \
                        and self.rect.bottom > blocks.rect.top - 32 \
                        and self.rect.top < blocks.rect.top \
                        and not self.obstacleL and not self.obstacleR:
                    self.floor = True
                    self.rect.y = blocks.rect.y - 32

            # bounds====================================================================================
            if self.rect.left < -32:
                self.obstacleL = True
                self.rect.left = -32
                self.bounds=True
예제 #58
0
파일: Main.py 프로젝트: komaflash/twdump
if __name__ == '__main__':

    #
    # CONFIG BEGIN
    #

    # account_file = 'DataSources/company_names.txt'
    account_file = 'DataSources/notweets_test.txt'
    output_directory = "DataSets"
    begin = dt.date(2010, 1, 1)
    end = dt.date(2017, 12, 31)

    #
    # CONFIG END
    #

    with open(account_file, 'r') as f:
        handles = f.readlines()

    handles = [h.rstrip('\n') for h in handles]

    timer = Timer()
    for handle in handles:
        timer.start()
        # query = TweetQuery(handle, output_directory, "weekly")
        query = TweetQuery(handle, output_directory, "monthly")
        cnt = query.download_tweets_to_csv(begin, end)
        timer.stop()
        logging.info("SUCCESS (company=%s tweets=%s duration=%s)", handle, cnt,
                     timer.elapsed())
예제 #59
0
    def run(self):
        '''
            Initiates full WPA hanshake capture attack.
        '''

        # Check if user only wants to run PixieDust attack
        if Configuration.pixie_only and self.target.wps:
            Color.pl('{!} {O}--pixie{R} set, ignoring WPA-handshake attack')
            self.success = False
            return self.success

        # First, start Airodump process
        with Airodump(channel=self.target.channel,
                      target_bssid=self.target.bssid,
                      skip_wash=True,
                      output_file_prefix='wpa') as airodump:

            Color.clear_entire_line()
            Color.pattack("WPA", self.target, "Handshake capture",
                          "Waiting for target to appear...")
            airodump_target = self.wait_for_target(airodump)

            self.clients = []

            handshake = None

            timeout_timer = Timer(Configuration.wpa_attack_timeout)
            deauth_timer = Timer(Configuration.wpa_deauth_timeout)

            while handshake is None and not timeout_timer.ended():
                step_timer = Timer(1)
                Color.clear_entire_line()
                Color.pattack(
                    "WPA", airodump_target, "Handshake capture",
                    "Listening. (clients:{G}%d{W}, deauth:{O}%s{W}, timeout:{R}%s{W})"
                    % (len(self.clients), deauth_timer, timeout_timer))

                # Find .cap file
                cap_files = airodump.find_files(endswith='.cap')
                if len(cap_files) == 0:
                    # No cap files yet
                    time.sleep(step_timer.remaining())
                    continue
                cap_file = cap_files[0]

                # Copy .cap file to temp for consistency
                temp_file = Configuration.temp('handshake.cap.bak')
                copy(cap_file, temp_file)

                # Check cap file in temp for Handshake
                bssid = airodump_target.bssid
                essid = airodump_target.essid if airodump_target.essid_known else None
                handshake = Handshake(temp_file, bssid=bssid, essid=essid)
                if handshake.has_handshake():
                    # We got a handshake
                    Color.pl('\n\n{+} {G}successfully captured handshake{W}')
                    break

                # There is no handshake
                handshake = None
                # Delete copied .cap file in temp to save space
                os.remove(temp_file)

                # Look for new clients
                airodump_target = self.wait_for_target(airodump)
                for client in airodump_target.clients:
                    if client.station not in self.clients:
                        Color.clear_entire_line()
                        Color.pattack(
                            "WPA", airodump_target, "Handshake capture",
                            "Discovered new client: {G}%s{W}" % client.station)
                        Color.pl("")
                        self.clients.append(client.station)

                # Send deauth to a client or broadcast
                if deauth_timer.ended():
                    self.deauth(airodump_target)
                    # Restart timer
                    deauth_timer = Timer(Configuration.wpa_deauth_timeout)

                # Sleep for at-most 1 second
                time.sleep(step_timer.remaining())
                continue  # Handshake listen+deauth loop

            if not handshake:
                # No handshake, attack failed.
                Color.pl(
                    "\n{!} {O}WPA handshake capture {R}FAILED:{O} Timed out after %d seconds"
                    % (Configuration.wpa_attack_timeout))
                self.success = False
                return self.success

            # Save copy of handshake to ./hs/
            self.save_handshake(handshake)

            # Print analysis of handshake file
            Color.pl('\n{+} analysis of captured handshake file:')
            handshake.analyze()

            # Try to crack handshake
            key = self.crack_handshake(handshake, Configuration.wordlist)
            if key is None:
                self.success = False
            else:
                self.crack_result = CrackResultWPA(bssid, essid,
                                                   handshake.capfile, key)
                self.crack_result.dump()
                self.success = True
            return self.success
예제 #60
0
    ask = raw_input  # python2
except AttributeError:
    ask = input  #python3

duty_counter_h = 0  # C
duty_counter_v = 0  # D
duty_cycle_h = 45  # H horizontal velocity (rightwards direction)
duty_cycle_v = 125  # V vertical velocity (75 in upwards direction)
v_adjust = -1  # amount to adjust V by each time round loop
duty_master = 125  # M
x = 0  # x position of projectile
y = 0  # y position of projectile

LOOP_RATE = None  # run the loop as fast as possible

timer = Timer(LOOP_RATE)
screen = None


def output(x, y):
    global screen
    if screen is None:
        from screen import Screen
        screen = Screen()
        screen.start()

    screen.plot(x, screen.height - y)


while y >= 0:  # stop when projectile hits ground
    timer.wait()