Example #1
1
    def process_message(self, peer, mailfrom, rcpttos, data):
        if savemsg:
            filename = "savedmsg/%s-%d.eml" % (datetime.now().strftime("%Y%m%d%H%M%S"), self.no)
            f = open(filename, "w")
            f.write(data)
            f.close

        if "X-Arf: yes" in data:
            mail = email.message_from_string(data)

            for part in mail.walk():
                ctypes = part.get_params(None, "Content-Type")
                if ctypes and len(ctypes) > 2:
                    if ctypes[0] == ("text/plain", "") and ctypes[2] == ("name", "report.txt"):
                        payload = part.get_payload()
                        xarf = load(payload, Loader=Loader)
                        handle_xarf(xarf)

                dtypes = part.get_params(None, "Content-Disposition")
                if dtypes and dtypes[1] == ("filename", "report.txt"):
                    payload = b64decode(part.get_payload())
                    xarf = load(payload, Loader=Loader)
                    handle_xarf(xarf)

        print "Email received at %s" % datetime.now().strftime("%H:%M:%S")
        self.no += 1
Example #2
1
def db_register_disk(conn, device):
    """Permanently stores a disk to the disks database.
    :param device: The device to add
    """
    # First we must populate basic information for the disk.
    try:
        inserted = (
            r.db("wanwipe")
            .table("disks")
            .insert(
                {
                    "serial_no": get_disk_serial(device),
                    "device_name": device,
                    "device_node": "/dev/{}".format(device),
                    "host_ip": get_global_ip(),
                    "boot_id": get_boot_id(),
                    "machine_id": get_dbus_machine_id(),
                }
            )
            .run(conn)
        )
        print(
            "{}: RegisterDisk: disk created: {}".format(dt.isoformat(dt.now()), inserted["generated_keys"][0]),
            file=sys.stderr,
        )
        return inserted["generated_keys"][0]
    except RqlRuntimeError as kaboom:
        print(
            "{}: RegisterDisk: disk creation failed somehow: {}".format(dt.isoformat(dt.now()), kaboom), file=sys.stderr
        )
Example #3
1
    def flush_next(self):

        if len(self._trs_to_flush) > 0:

            td = self._last_flush + self._THROTTLING_DELAY - datetime.now()
            # Python 2.7 has this built in, python < 2.7 don't...
            if hasattr(td, "total_seconds"):
                delay = td.total_seconds()
            else:
                delay = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / 10.0 ** 6

            if delay <= 0:
                tr = self._trs_to_flush.pop()
                self._last_flush = datetime.now()
                logging.debug("Flushing transaction %d" % tr.get_id())
                try:
                    tr.flush()
                except Exception, e:
                    logging.exception(e)
                    self.tr_error(tr)
                    self.flush_next()
            else:
                # Wait a little bit more
                if tornado.ioloop.IOLoop.instance().running():
                    tornado.ioloop.IOLoop.instance().add_timeout(time.time() + delay, lambda: self.flush_next())
                elif self._flush_without_ioloop:
                    # Tornado is no started (ie, unittests), do it manually: BLOCKING
                    time.sleep(delay)
                    self.flush_next()
    def workerFunction(jobObj, tid, args):
        a = datetime.now()
        print "worker #%02d start read file %s" % (tid, jobObj)
        f = open(os.path.join(inputPath, jobObj), "r")
        content = json.load(f)
        f.close()
        diff = datetime.now() - a
        print "worker #%02d read file %s, use [%d.%d s]" % (tid, jobObj, diff.seconds, diff.microseconds)
        results = {}
        count = 0
        for subFilename in content:
            ngs = content[subFilename]
            count += 1
            result = {}
            for model in models:
                mn = models[model]
                # result[model] = 0
                for ng in ngs:
                    if ng in mn:
                        if not model in result:
                            result[model] = 0
                        result[model] += 1

            if count % 100 == 0:
                print "worker #%02d scan %d files" % (tid, count)

            results[subFilename] = result

        a = datetime.now()
        print "worker #%02d start write file %s" % (tid, jobObj)
        f = open(os.path.join(outputPath, jobObj), "w")
        json.dump(results, f)
        f.close()
        diff = datetime.now() - a
        print "worker #%02d  write file %s, use [%d.%d s]" % (tid, jobObj, diff.seconds, diff.microseconds)
def sentiment_emails():
    """
    Go through each file in archive/ and compute the various sentiment values.

    The file lkml.2012.12.23.75.email.json is a notorious rant by Linus that is
    a good example of confrontational communication.
    """
    sentiment_by_words = SentimentByWords()
    archive_dir = "archive/"
    # filenames = ["lkml.2010.10.27.102.email.json","lkml.2012.12.23.75.email.json"]
    # for filenum, filename in enumerate(filenames):
    for filenum, filename in enumerate(os.listdir(archive_dir)):
        if filenum % 10 == 0:
            print datetime.now(), filenum
        if filename.endswith(".email.json"):
            full_filename = os.path.join(archive_dir, filename)
            fh = open(full_filename, "r")
            email_data = load(fh)
            fh.close()

            modified = False

            if "naive-word-sentiment" not in email_data and "clean_body" in email_data:
                email_data["naive-word-sentiment"] = sentiment_by_words.score(email_data["clean_body"])
                modified = True

            if False and "tp.com-sentiment" not in email_data and "clean_body" in email_data:
                email_data["tp.com-sentiment"] = sentimentize_text(email_data["clean_body"])
                modified = True

            if modified:
                fh = open(full_filename, "w")
                fh.write(dumps(email_data))
                fh.close()
Example #6
1
    def save(self):
        """Saves the stat fields to the database """

        try:
            collection_time = datetime.now()
            num_campaigns = self.stat_fields["Campaign"]
            num_deployments = (self.stat_fields["Deployment"],)
            num_images = (self.stat_fields["Image"],)
            num_auv_deployments = (self.stat_fields["AUVDeployment"],)
            num_bruv_deployments = (self.stat_fields["BRUVDeployment"],)
            num_dov_deployments = (self.stat_fields["DOVDeployment"],)
            num_tv_deployments = (self.stat_fields["TVDeployment"],)
            num_ti_deployments = (self.stat_fields["TIDeployment"],)
            db_size_on_disk = self.query_database_size()

            data = DataLogger.objects.create(
                collection_time=datetime.now(),
                num_campaigns=self.stat_fields["Campaign"],
                num_deployments=self.stat_fields["Deployment"],
                num_images=self.stat_fields["Image"],
                num_auv_deployments=self.stat_fields["AUVDeployment"],
                num_bruv_deployments=self.stat_fields["BRUVDeployment"],
                num_dov_deployments=self.stat_fields["DOVDeployment"],
                num_tv_deployments=self.stat_fields["TVDeployment"],
                num_ti_deployments=self.stat_fields["TIDeployment"],
                db_size_on_disk=self.query_database_size(),
            )
        except:
            traceback.print_exc(file=sys.stdout)
            raise
        data.save()
Example #7
1
def conn_tests(solver, save_format):
    print ">>>", datetime.now(), "Begin conn tests"
    solver.net.set_phase_test()
    solver.test_nets[0].share_with(solver.net)
    net = solver.test_nets[0]
    save_dir = save_format.format(solver.iter)
    os.mkdir(save_dir)
    hist = np.zeros((2, 2))
    for fname in fnames_val:
        net.forward()
        im = Image.fromarray(net.blobs["left-conn"].data[0].argmax(0).astype(np.uint8), mode="P")
        im.save(os.path.join(save_dir, fname + "-L.png"))
        im = Image.fromarray(net.blobs["top-conn"].data[0].argmax(0).astype(np.uint8), mode="P")
        im.save(os.path.join(save_dir, fname + "-T.png"))
        h, _, _ = np.histogram2d(
            net.blobs["left-gt"].data[0, 0].flatten(),
            net.blobs["left-conn"].data[0].argmax(0).flatten(),
            bins=2,
            range=[[0, 2], [0, 2]],
        )
        hist += h
        h, _, _ = np.histogram2d(
            net.blobs["top-gt"].data[0, 0].flatten(),
            net.blobs["top-conn"].data[0].argmax(0).flatten(),
            bins=2,
            range=[[0, 2], [0, 2]],
        )
        hist += h
    print ">>>", datetime.now(), "Iteration", solver.iter, "overall accuracy", np.diag(hist).sum() / hist.sum()
    print ">>>", datetime.now(), "Iteration", solver.iter, "total IU", hist[1, 1] / (
        hist[0, 1] + hist[1, 0] + hist[1, 1]
    )
    solver.net.set_phase_train()
Example #8
0
 def forcast(self, Goal, day=None, month=None, year=None, produce_daily_allowance=0):
     """
     # Get the current Date
     # Predict the future remaining Date
     # Compute the gaol setting amount versus time
     # For time until present
     #   Compute the present daily allownace --> fork it to the panels
     #   Compute the summation of all the new daily allowance I got
     # 
     """
     if day is None:
         day = datetime.now().day
     if month is None:
         month = datetime.now().month
     if year is None:
         year = 2012
     self.preRange = day
     [prediction, prediction_detail] = self.predictOverAll()  # Fix this function --> Include the single predictions
     #    self.PredictionDataInsertion(prediction_detail,month,year,day)
     if produce_daily_allowance:
         if howManyDay == 0:
             howManyDay = Total - 1
         Goal_diff = prediction[howManyDay] - Goal
         print prediction[howManyDay]
         daily_allowance = np.zeros((HowManyDay, 1))
         actual_transactions = self.acquireData(start=datetime(year, month, 1), end=datetime(year, month, day))
         actual_transactions = np.apply_along_axis(
             lambda x, m: np.multiply(x, m), 1, actual_transactions, self.std_X
         )
         actual_transactions = np.apply_along_axis(lambda x, m: np.add(x, m), 1, actual_transactions, self.mean_X)
         self.daily_allowance = np.sum(actual_transactions, 1) - prediction[actual_transactions.shape[0]]
         self.GoalGather = sum(daily_allowance) / Goal_diff
         return None
     else:
         return None
Example #9
0
 def clockout(self, buttonnumber):
     """
     Reads the Contents of the choosen info, IE Button1 = info1
     Change the Student Clockout time from None to time right now
     :param buttonnumber: 1-5 number
     :return:
     """
     self.clockoutinforows[buttonnumber].stringvar.set("-------------------------")  # clears out
     clockouttime = str(datetime.now().time().hour) + ":" + str(datetime.now().time().minute)
     try:
         self.mystudentcollection.listofstudents[buttonnumber].clockouttime = clockouttime
         self.controller.sysprint(
             ">DEBUG: ClockoutMethod trying.."
             + str(self.mystudentcollection.listofstudents[buttonnumber].name)
             + " at "
             + clockouttime
         )
         self.mydatabaseinterface.clockout(self.mystudentcollection.listofstudents[buttonnumber])
         logoutstring = (
             "Logout of " + str(self.mystudentcollection.listofstudents[buttonnumber].name) + " At " + clockouttime
         )
         self.controller.updateflag = True  # dangerous is it not?
         self.changeframe("PrimaryPage")
         mypopup = Popups("Logout Successful", logoutstring, "Ok")
         mypopup.mainloop()
     except:  # meh to broad?
         self.controller.sysprint(">ERROR!: Bad Clockout")
         pass
Example #10
0
 def clockin(self, name, studentid, room):
     """
     Clocks a student into a room, calls checkroom, to check the room, and checks inputs
     for student id and name. All inputs must come back valid to proceed.
     Gathers time and date
     :param name: Name of Student to Clock in
     :param studentid: Id of student that needs to clock in
     :param room: Room of Student to clockin
     :return:
     """
     booleanreturn, stringreturn = self.validinput(name, studentid)
     if booleanreturn is True:  # Room and inputs OK
         if room == 0:
             self.controller.sysprint("ERROR! Room 0!")
             mypopup = Popups("ERROR!", "Bad Input Choose  room!")
             mypopup.mainloop()
         else:
             clockindate = str(datetime.now().date())  # format: 2015-12-31
             clockintime = str(datetime.now().time().hour) + ":" + str(datetime.now().time().minute)
             mystudentlogin = Student(studentid, name, clockindate, room, clockintime)  # no clockin as None
             mydatabaseinterface = DataBaseInterface()  # default file location
             mydatabaseinterface.clockin(mystudentlogin)
             self.clearinputs()
             self.controller.sysprint(">DEBUG: Clockedin Student")
             mypopup = Popups("Clock In", "Clocked in " + str(name) + " at " + str(clockintime))
             mypopup.mainloop()
             self.controller.updateflag = True  # dangerous is it not?
             self.changeframe("PrimaryPage")
     else:
         self.controller.sysprint(">ERROR! Bad Input: " + stringreturn)
         mypopup = Popups("Error!", "Bad Input " + stringreturn)
         mypopup.mainloop()
def door_opening():
    print("You stepped to the door. Then suddenly the door began to speak:")
    print("'I need to know the current date!' - said the door.")
    print("Please add the current date in format: YYYY-MM-DD")

    while True:
        next = input(prompt)
        current_time = date_checker(next)
        yesterday = datetime.now() - timedelta(days=1)
        yesterday = yesterday.date()

        if current_time.date() == datetime.now().date():
            print("'The answer is correct. But I need the password, too.' said the door.")
            password_to_the_door()
        elif "key" in next and key_to_gold_room:
            print("You put your magic key into the keyhole and the door opened.")
            gold_room()
        elif "key" in next and not key_to_gold_room:
            print("You don't have the magic key to this door!")
            print("So, you have to answer the question of the door.")
        elif next == "I ask the bear":
            print("The bear looked at the table where a yesterday's newspaper was.")
            print("'It's", yesterday, "today... ' - answered the bear and continued to eat the honey.")
        else:
            print("'This is not the correct answer.' - said the door")
Example #12
0
 def news_count_last_month(self, obj):
     now = datetime.now().date()
     week_before = datetime.now().date() - timedelta(weeks=4)
     return self._get_text(
         Item.objects.filter(status="active", tags__name=obj.name, created_at__range=(week_before, now)).count(),
         Item.objects.filter(tags__name=obj.name, created_at__range=(week_before, now)).count(),
     )
Example #13
0
 def deal(self, phenny, input):
     nickk = (input.nick).lower()
     if not self.game_on:
         phenny.msg(CHANNEL, STRINGS["NOT_STARTED"])
         return
     if len(self.players) < 2:
         phenny.msg(CHANNEL, STRINGS["NOT_ENOUGH"])
         return
     if nickk != self.game_on:
         phenny.msg(CHANNEL, STRINGS["NEEDS_TO_DEAL"] % self.game_on)
         return
     if len(self.deck):
         phenny.msg(CHANNEL, STRINGS["ALREADY_DEALT"])
         return
     self.startTime = datetime.now()
     self.lastActive = datetime.now()
     self.deck = self.createnewdeck()
     for i in xrange(0, 7):
         for p in self.players:
             self.players[p].append(self.getCard())
     self.topCard = self.getCard()
     while self.topCard in ["R", "S", "D2", "W", "WD4"]:
         self.topCard = self.getCard()
     self.currentPlayer = 1
     self.cardPlayed(phenny, self.topCard)
     self.showOnTurn(phenny)
     self.dealt = True
Example #14
0
def get_system_context(ms_id):
    map_system = get_object_or_404(MapSystem, pk=ms_id)

    # If map_system represents a k-space system get the relevant KSystem object
    if map_system.system.is_kspace():
        system = map_system.system.ksystem
    else:
        system = map_system.system.wsystem

    scan_threshold = datetime.now(pytz.utc) - timedelta(hours=int(get_config("MAP_SCAN_WARNING", None).value))
    interest_offset = int(get_config("MAP_INTEREST_TIME", None).value)
    interest_threshold = datetime.now(pytz.utc) - timedelta(minutes=interest_offset)

    scan_warning = system.lastscanned < scan_threshold
    if interest_offset > 0:
        interest = map_system.interesttime and map_system.interesttime > interest_threshold
    else:
        interest = map_system.interesttime
        # Include any SiteTracker fleets that are active
    st_fleets = map_system.system.stfleets.filter(ended=None).all()
    return {
        "system": system,
        "mapsys": map_system,
        "scanwarning": scan_warning,
        "isinterest": interest,
        "stfleets": st_fleets,
    }
Example #15
0
    def setup(self, input_files, settings, name=None, clr_type="plos", num_bins=10, spline_type=3):

        """ This function sets up clr based on the input parameters above.
       For setting something up with just a config file, use setup_config """
        if name == None:
            self.name = "clr-" + datetime.now().strftime("%Y-%m-%d_%H.%M.%S")
        else:
            self.name = name

        self.exp_data = None
        self.input_files = input_files

        t = datetime.now().strftime("%Y-%m-%d_%H.%M.%S")

        # Read in the default values for clr, we'll override these later
        settings = ReadConfig(settings, "./config/default_values/clr.cfg")
        settings["clr"]["clr_type"] = clr_type
        settings["clr"]["num_bins"] = num_bins
        settings["clr"]["spline_type"] = 3
        settings = ReadConfig(settings, settings["clr"]["config"])

        self.create_directories(settings)

        self.write_data(input_files, settings)

        self.settings = copy.deepcopy(settings)
        self.write_config(settings)

        self.cmd = "matlab -nodesktop -nodisplay -nosplash -r \"addpath(genpath('./'));try;run_clr, catch ,end, exit\""
        self.cwd = self.working_dir
Example #16
0
    def update_next_pixel(self, val):
        col, row = self._col, self._row
        assert col < self.num_hpxl
        assert row < self.num_vpxl

        if isinstance(val, int):
            nbits = sum(self.color_depth)
            val = intbv(val)[nbits:]
            cd = self.color_depth
            rgb = (int(val[nbits : nbits - cd[0]]), int(val[nbits - cd[0] : cd[2]]), int(val[cd[2] : 0]))
        elif isinstance(val, tuple):
            rgb = val
        else:
            raise ValueError

        self._uvmem[row][col] = rgb

        col += 1
        if col == self.num_hpxl:
            col = 0
            row += 1
        if row == self.num_vpxl:
            row = 0

        if col == 0 and row == 0:
            # @todo: remove print add option to create png or not
            self.update_cnt += 1
            td = datetime.now() - self.time_last
            print("{:<10d}: full display update {} ({})".format(now(), self.update_cnt, td))
            self._vvmem = deepcopy(self._uvmem)
            self.create_save_image()
            self.time_last = datetime.now()

        self._col, self._row = col, row
        return
Example #17
0
def create_distro_packages_list(distro_check_dir):
    pkglst_dir = os.path.join(distro_check_dir, "package_lists")
    if not os.path.isdir(pkglst_dir):
        os.makedirs(pkglst_dir)
    # first clear old stuff
    for file in os.listdir(pkglst_dir):
        os.unlink(os.path.join(pkglst_dir, file))

    per_distro_functions = [
        ["Debian", get_latest_released_debian_source_package_list],
        ["Ubuntu", get_latest_released_ubuntu_source_package_list],
        ["Fedora", get_latest_released_fedora_source_package_list],
        ["OpenSuSE", get_latest_released_opensuse_source_package_list],
        ["Mandriva", get_latest_released_mandriva_source_package_list],
        ["Meego", get_latest_released_meego_source_package_list],
    ]

    from datetime import datetime

    begin = datetime.now()
    for distro in per_distro_functions:
        name = distro[0]
        release, package_list = distro[1]()
        bb.note("Distro: %s, Latest Release: %s, # src packages: %d" % (name, release, len(package_list)))
        package_list_file = os.path.join(pkglst_dir, name + "-" + release)
        f = open(package_list_file, "w+b")
        for pkg in package_list:
            f.write(pkg + "\n")
        f.close()
    end = datetime.now()
    delta = end - begin
    bb.note("package_list generatiosn took this much time: %d seconds" % delta.seconds)
Example #18
0
    def search(self, seconds, words):
        """ Looks for all the pangrams for <seconds> number of seconds """
        # grab the alphabet
        alphabet = self.all_chars()
        pangrams = []

        # start the clock
        start_time = datetime.now()
        difference = (datetime.now() - start_time).total_seconds()

        # while we still have time left.
        while difference <= seconds:
            alphabet = self.all_chars()
            # and there are still characters left to check
            pangram = ""
            while len(alphabet) > 0:
                word = words[random.randrange(0, len(words))]
                pangram += " " + str(word)
                for c in word.lower():
                    if c in alphabet:
                        alphabet.remove(c)
                    else:
                        pass

            pangrams.append(pangram)
            difference = (datetime.now() - start_time).total_seconds()
        return pangrams
Example #19
0
    def doSpeedTest(self):
        # run a speed test
        result = os.popen("/usr/local/bin/speedtest-cli --simple").read()
        if "Cannot" in result:
            return {"date": datetime.now(), "uploadResult": 0, "downloadResult": 0, "ping": 0}

        # Result:
        # Ping: 529.084 ms
        # Download: 0.52 Mbit/s
        # Upload: 1.79 Mbit/s

        resultSet = result.split("\n")
        pingResult = resultSet[0]
        downloadResult = resultSet[1]
        uploadResult = resultSet[2]

        pingResult = float(pingResult.replace("Ping: ", "").replace(" ms", ""))
        downloadResult = float(downloadResult.replace("Download: ", "").replace(" Mbit/s", ""))
        uploadResult = float(uploadResult.replace("Upload: ", "").replace(" Mbit/s", ""))

        return {
            "date": datetime.now(),
            "uploadResult": uploadResult,
            "downloadResult": downloadResult,
            "ping": pingResult,
        }
Example #20
0
def bootstrap(request):
    # Don't create dummy playlist tracks if playlist tracks already exist!
    pl_tracks = PlaylistTrack.all().fetch(1)
    if len(pl_tracks) > 0:
        return HttpResponse(status=404)

    playlist = ChirpBroadcast()

    minutes = 0
    tracks = Track.all().fetch(100)
    for track in tracks:
        pl_track = PlaylistTrack(
            playlist=playlist,
            selector=request.user,
            established=datetime.now() - timedelta(minutes=minutes),
            artist=track.album.album_artist,
            album=track.album,
            track=track,
        )
        pl_track.put()
        if minutes > 0 and minutes % 25 == 0:
            pl_break = PlaylistBreak(playlist=playlist, established=datetime.now() - timedelta(minutes=minutes - 1))
            pl_break.put()
        minutes += 5

    return HttpResponseRedirect("/playlists/")
Example #21
0
    def test_voting(self):
        """
            Tests the voting on both characters in a game and retrieves results.
        """
        self.initialize()

        contest_helper = ContestHelper()

        result = contest_helper.vote(self.game1, self.game1.character1, self.u1)
        self.assertEquals(result, 0)
        result = contest_helper.vote(self.game1, self.game1.character2, self.u2)
        self.assertEquals(result, 0)
        result = contest_helper.vote(self.game1, self.game1.character2, self.u3)
        self.assertEquals(result, 0)

        the_game = contest_helper.get_game_for_date(self.contest, datetime.now())
        self.assertEquals(the_game.character1score, 1)
        self.assertEquals(the_game.character2score, 2)

        # and now some error cases
        result = contest_helper.vote(self.game2, self.game2.character1, self.u4)
        self.assertEquals(result, -1)
        result = contest_helper.vote(self.game1, self.character3, self.u4)
        self.assertEquals(result, -2)
        result = contest_helper.vote(self.game1, self.game2.character1, self.u3)
        self.assertEquals(result, -3)
        result = contest_helper.vote(self.game1, self.game2.character2, self.u3)
        self.assertEquals(result, -3)

        # just to confirm it all hasn't changed
        the_game = contest_helper.get_game_for_date(self.contest, datetime.now())
        self.assertEquals(the_game.character1score, 1)
        self.assertEquals(the_game.character2score, 2)
Example #22
0
    def save(self):
        post = super(AdminPostForm, self).save(commit=False)

        if post.pk is None:
            if self.cleaned_data["publish"]:
                post.published = datetime.now()
        else:
            if Post.objects.filter(pk=post.pk, published=None).count():
                if self.cleaned_data["publish"]:
                    post.published = datetime.now()

        post.teaser_html = parse(self.cleaned_data["teaser"], emitter=BiblionHtmlEmitter)
        post.content_html = parse(self.cleaned_data["content"], emitter=BiblionHtmlEmitter)
        post.updated = datetime.now()
        post.save()

        r = Revision()
        r.post = post
        r.title = post.title
        r.teaser = self.cleaned_data["teaser"]
        r.content = self.cleaned_data["content"]
        r.author = post.author
        r.updated = post.updated
        r.published = post.published
        r.save()

        if can_tweet() and self.cleaned_data["tweet"]:
            post.tweet()

        return post
Example #23
0
    def test_serializer(self):
        class SomeModel(models.Model):
            dt = models.DateTimeField()
            t = models.TimeField()
            d = models.DateField()

        def method(self):
            pass

        instance = SomeModel(dt=datetime.now(), d=date.today(), t=datetime.now().time())
        # make sure serializer doesn't fail if a method is attached to
        # the instance
        instance.method = method
        instance.nonfield = "hello"
        data = utils.serialize_instance(instance)
        instance2 = utils.deserialize_instance(SomeModel, data)
        self.assertEqual(getattr(instance, "method", None), method)
        self.assertEqual(getattr(instance2, "method", None), None)
        self.assertEqual(instance.nonfield, instance2.nonfield)
        self.assertEqual(instance.d, instance2.d)
        self.assertEqual(instance.dt.date(), instance2.dt.date())
        for t1, t2 in [(instance.t, instance2.t), (instance.dt.time(), instance2.dt.time())]:
            self.assertEqual(t1.hour, t2.hour)
            self.assertEqual(t1.minute, t2.minute)
            self.assertEqual(t1.second, t2.second)
            # AssertionError: datetime.time(10, 6, 28, 705776)
            #     != datetime.time(10, 6, 28, 705000)
            self.assertEqual(int(t1.microsecond / 1000), int(t2.microsecond / 1000))
def process(tasks):
    (source, foo) = os.path.split(tasks[0])
    if source == "":
        source = inputFile
    else:
        if finished.count(source) == 0:
            printlock.acquire()
            print "trying to split from", source, "which is not finished yet, re-queuing and sleeping 5 seconds"
            printlock.release()
            q.put(tasks)
            time.sleep(5)
            return

        source = outputDir + "/" + source + dataType

    if not simulate and not os.path.exists(source):
        source = inputFile

    printlock.acquire()
    print "splitting", source, "to", tasks
    printlock.release()

    if sys.argv.count("--plan") > 0:
        return

    (fp, configfile) = tempfile.mkstemp()
    os.write(fp, "# auto-generated\n")
    for task in tasks:
        dest = os.path.join(outputDir, task + dataType)
        dirname = os.path.dirname(dest)

        if not os.path.exists(dirname):
            printlock.acquire()
            print "Creating", dirname
            os.mkdir(dirname)
            printlock.release()

        os.write(fp, dest)
        os.write(fp, "\t")
        os.write(fp, clipType)
        os.write(fp, "\t")
        os.write(fp, clipDir + "/" + task + clipExtension)
        os.write(fp, "\n")

    os.close(fp)

    start = datetime.now()
    if simulate:
        time.sleep(random.randint(1, 10))
    else:
        os.spawnl(os.P_WAIT, splitterCommand, splitterCommand, "--softcut", source, configfile)

    printlock.acquire()
    print "finished splitting to", tasks
    print "runtime:", datetime.now() - start
    printlock.release()
    for task in tasks:
        finished.append(task)

    os.unlink(configfile)
Example #25
0
 def insert(self, stockInfo):
     self.c.execute(
         "insert into "
         + self.tableName
         + """ (
                     stock_code,
                     stock_name,
                     industrial_category,
                     total_market_value,
                     general_capital,
                     circulation_stock,
                     insert_time,
                     update_time) values (?, ?, ?, ?, ?, ?, ? ,?)
                     """,
         [
             stockInfo.getStockCode(),
             stockInfo.getStockName(),
             stockInfo.getIndustrialCategory(),
             stockInfo.getTotalMarketValue(),
             stockInfo.getGeneralCapital(),
             stockInfo.getCirculationStock(),
             datetime.now(),
             datetime.now(),
         ],
     )
Example #26
0
def _do_work(qTasks, qResults, qWatch, prefix, run_skipped, timeout, show_cmd):
    while True:
        test = qTasks.get(block=True, timeout=sys.maxint)
        if test is EndMarker:
            qWatch.put(EndMarker)
            qResults.put(EndMarker)
            return

        if not test.enable and not run_skipped:
            qResults.put(NullTestOutput(test))
            continue

        # Spawn the test task.
        cmd = test.get_command(prefix)
        if show_cmd:
            print(escape_cmdline(cmd))
        tStart = datetime.now()
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        # Push the task to the watchdog -- it will kill the task
        # if it goes over the timeout while we keep its stdout
        # buffer clear on the "main" worker thread.
        qWatch.put(proc)
        out, err = proc.communicate()
        qWatch.put(TaskFinishedMarker)

        # Create a result record and forward to result processing.
        dt = datetime.now() - tStart
        result = TestOutput(test, cmd, out, err, proc.returncode, dt.total_seconds(), dt > timedelta(seconds=timeout))
        qResults.put(result)
Example #27
0
    def test_with_week_counter(self):
        self._load_simple()
        LogEntry.make(self.app_access.key, "free_u", None, action="share_photo").put()
        LogEntry.make(
            self.app_access.key, "free_u", None, action="share_photo", when=(datetime.now() - timedelta(hours=25))
        ).put()
        LogEntry.make(
            self.app_access.key, "free_u", None, action="share_photo", when=(datetime.now() - timedelta(days=3))
        ).put()
        LogEntry.make(
            self.app_access.key, "free_u", None, action="share_photo", when=(datetime.now() - timedelta(days=6))
        ).put()
        # outside
        LogEntry.make(
            self.app_access.key, "free_u", None, action="share_photo", when=(datetime.now() - timedelta(days=10))
        ).put()

        profile_state = self.app_access.compile_profile_state(user_id="free_u")
        self.assertEquals(profile_state["profile"], "free")
        self.assertEquals(profile_state["default"], "deny")
        self.assertEquals(len(profile_state["states"]), 3)
        self.assertEquals(profile_state["states"]["share_photo"][0]["left"], 9)
        self.assertEquals(profile_state["states"]["share_photo"][0]["limit_to"], 10)
        self.assertEquals(profile_state["states"]["share_photo"][1]["left"], 16)
        self.assertEquals(profile_state["states"]["share_photo"][1]["limit_to"], 20)
Example #28
0
def main():
    start_time = datetime.now()
    ids = db_select_show()
    count = 1

    for id in ids:
        show_id = id["id"]
        order = id["order"]
        tvdb_id = get_tvdb_id(order, show_id)

        if tvdb_id is not None:
            collection_show.update({"id": show_id}, {"$unset": {"tvdb_fetched": 1, "tvdb_id": 1}}, False, False)
            collection_show.update_one({"id": show_id}, {"$set": {"tvdb_fetched": True, "tvdb_id": tvdb_id}})
        else:
            collection_show.update({"id": show_id}, {"$unset": {"tvdb_fetched": 1, "tvdb_id": 1}}, False, False)
            collection_show.update_one({"id": show_id}, {"$set": {"tvdb_fetched": True, "tvdb_id": "N/A"}})
            print("No TVDB match found.")

        count += 1

    print("Process complete. ", count - 1, "shows processed.")
    end_time = datetime.now()
    duration = end_time - start_time
    print("Start time: ", str(start_time))
    print("End time: ", str(end_time))
    print("Total duration (minutes): ", str(duration.seconds / 60))
Example #29
0
    def new_func():

        starttime = datetime.now()

        func()

        print("exectime: ", datetime.now() - starttime)
Example #30
0
def stabilizer(selfsocket, neighbours, dvtable, updated, neighboursdict, lastadvertisement):
    updatetime = datetime.now()
    linkdown = list()
    olddvtable = copy.deepcopy(dvtable)
    for neighbour in neighbours:
        neighbournodeid = neighbour - 50000
        if not neighbournodeid in updated.keys() or (datetime.now() - updated[neighbournodeid]).seconds > LINK_DOWNTIME:
            # If no reply from a neighbour for a long time, assume link down
            dvtable[neighbournodeid] = (64,)
            linkdown.append(neighbour)
            for key, value in dvtable.items():
                # Check all those destinations for which the just-down link was next hop
                if len(value) == 2 and value[1] == neighbournodeid:
                    if key in neighboursdict.keys() and neighboursdict[key][0] < 64:
                        # If there is a better value in neighboursdict, take it
                        dvtable[key] = neighboursdict[key]
                    else:
                        # Otherwise, even these links are unreachable
                        # as the current node doesn't know any better alternative
                        dvtable[key] = (64,)
    for link in linkdown:
        neighbours.remove(link)
        if neighboursdict.has_key(link - 50000):
            neighboursdict.pop(link - 50000)

    if olddvtable != dvtable:  # If the Dv has been updated because of the neighbour's DV,
        updatetime, lastadvertisement = updateAdvertiser(selfsocket, neighbours, olddvtable, dvtable)
    print "DV at", datetime.now(), ":", dvtable
    return (updatetime, lastadvertisement, neighbours, neighboursdict, dvtable)