예제 #1
0
파일: parser.py 프로젝트: rymurr/q
def get_data(bstream, endianness):
    val_type = bstream.read(8).int
    if val_type in int_types:
        data = int_types[val_type](bstream, endianness, val_type)
    elif -20 < val_type < -10:
        data = get_hour(bstream.read(format(val_type, endianness)))
    elif val_type < 0:
        data = bstream.read(format(val_type, endianness))
    elif 20 > val_type > 10:
        attributes = bstream.read(8).int
        length = bstream.read(format(INT, endianness))
        data = [get_hour(x) for x in bstream.readlist(format_list(val_type, endianness, length))]
    elif 10 > val_type > 0:
        attributes = bstream.read(8).int
        length = bstream.read(format(INT, endianness))
        nptype, bstype = format_raw_list(val_type, length)
        data = np.fromstring(bstream.read(bstype).bytes, dtype=nptype)
        #data = bstream.readlist(format_list(val_type, endianness, length))
    elif val_type > 90:
        data = []
    else:
        attributes = bstream.read(8).int
        length = bstream.read(format(INT, endianness))
        data = [get_data(bstream, endianness) for _ in range(length)]
    return data        
예제 #2
0
파일: kb.py 프로젝트: sjoerdapp/airdialogue
    def __init__(self, fact_obj, airport_list, flight_number,
                 ref_departure_date, ref_return_date):
        # 1. origin and destination
        self.origin = random.randint(0, len(airport_list) - 1)
        self.dest = random.randint(0, len(airport_list) - 1)
        if self.dest == self.origin:
            self.dest = (self.dest + 1) % len(airport_list)
        self.dest = airport_list[self.dest]
        self.origin = airport_list[self.origin]
        # 2. date and time
        d1 = np.random.normal(ref_departure_date, fact_obj.time_deviation,
                              1)[0]
        d2 = np.random.normal(ref_return_date, fact_obj.time_deviation, 1)[0]

        # makes ure that departure date comes first
        if d1 < d2:
            self.departure_date = d1
            self.return_date = d2
        else:
            self.departure_date = d2
            self.return_date = d1

        # assert self.departure_date <= self.return_date
        # assert self.return_date - self.departure_date <= 3600*24*365/2

        # 3. class
        self.flight_class = utils.choice(fact_obj.class_member,
                                         1,
                                         p=fact_obj.class_prior)[0]

        # 4. connections
        self.connection = utils.choice(fact_obj.connection_member,
                                       1,
                                       p=fact_obj.connection_prior)[0]

        # 5. price
        mean_base_price = fact_obj.flight_price_mean[self.flight_class]
        base = mean_base_price * fact_obj.low_cost_mean_fraction[
            self.flight_class]
        self.price = int(
            np.random.normal(
                base, base * fact_obj.flight_price_norm_std[self.connection]))
        self.price = utils.discrete_price(self.price)

        # 6. flight number
        # self.flight_number = random.randint(1000, 9999)
        self.flight_number = flight_number

        # 7. airline
        airline_ind = random.randint(0, len(fact_obj.airline_list) - 1)
        self.airline = fact_obj.airline_list.keys()[airline_ind]

        # post process
        self.departure_month, self.departure_day = utils.get_month_and_day(
            fact_obj, self.departure_date)
        self.return_month, self.return_day = utils.get_month_and_day(
            fact_obj, self.return_date)
        self.departure_time_num = utils.get_hour(self.departure_date)
        self.return_time_num = utils.get_hour(self.return_date)
예제 #3
0
def json_msg_reserve_day_schedule(request, pk):
    day_schedule = get_object_or_404(DaySchedule, pk=pk)

    reserve_obj_dict = {}
    reserve_obj_dict['reserveFor'] = 'cours'
    reserve_obj_dict['day'] = day_schedule.day_name.name
    reserve_obj_dict['parentPath'] = '#/courses/' + str(
        day_schedule.schedule.course.id)
    reserve_obj_dict['hourStart'] = utils.get_hour(day_schedule.hour_start)
    reserve_obj_dict['hourEnd'] = utils.get_hour(day_schedule.hour_end)
    reserve_obj_dict['peoples'] = unicode(day_schedule.schedule.course.teacher)
    reserve_obj_dict['title'] = unicode(day_schedule.schedule.course.course_name) +\
     ', ' + unicode(day_schedule.schedule.name)

    reservation_header = "Demande de r&eacute;servation pour %s"\
                                    %(reserve_obj_dict['reserveFor'])
    reservation_header += " <strong>%s</strong>" % (reserve_obj_dict['title'])
    reservation_header += " avec %s" % (reserve_obj_dict['peoples'])
    reservation_header += " le %s" % (reserve_obj_dict['day'])
    reservation_header += " de %s" % (reserve_obj_dict['hourStart'])
    reservation_header += " &agrave; %s." % (reserve_obj_dict['hourEnd'])

    # Create the client Object
    reserve_obj_dict['client'] = {}
    reserve_obj_dict['client']['selections'] = []
    reserve_obj_dict['client']['reservationHeader'] = reservation_header

    if request.method == 'GET':

        reserve_obj_dict['dayStart'] = utils.get_day(day_schedule.day_start)
        reserve_obj_dict['dayEnd'] = utils.get_day(day_schedule.day_end)
        reserve_obj_dict['dayRange'] = utils.get_day_range_dict(
            day_schedule.day_start, day_schedule.day_end,
            day_schedule.day_name.name)

        q_testing_days = TestingDay.objects.filter(day_schedule=day_schedule)
        reserve_obj_dict['testingDays'] = utils.get_testing_days(
            q_testing_days)

    elif request.method == 'POST':
        errors = utils.send_reserve_day_mail(request.body, reservation_header)

        if errors:
            errors_obj = {"errors": errors}
            return HttpResponse(json.dumps(errors_obj),\
                    content_type="application/json")
            #return HttpResponseBadRequest(json.dumps(errors),\
            #content_type="application/json")

        reserve_obj_dict = {}
    return HttpResponse(json.dumps(reserve_obj_dict),\
                content_type="application/json")
예제 #4
0
def suggest_pairs(events, times, locations):
    """
  Suggests possible couplings of times and locations.
  Only suggest a location for a given time if an event at that location
  has taken place around that time.
  """
    pairs = []
    for time in times:
        for event in events:
            if math.fabs(time - utils.get_hour(event.start_time)
                         ) <= 2 and event.venue_id in locations:
                pairs.append({"time": time, "venue_id": event.venue_id})
    to_add = []
    for pair in pairs:
        to_add.append(True)
    unique_pairs = []
    for i in xrange(0, len(pairs)):
        for j in xrange(i, len(pairs)):
            if i != j and pairs[i]["time"] == pairs[j]["time"] and pairs[i][
                    "venue_id"] == pairs[j]["venue_id"]:
                to_add[j] = False
    for i in xrange(0, len(to_add)):
        if to_add[i]:
            unique_pairs.append(pairs[i])
    return unique_pairs
예제 #5
0
 async def fTime(self, message):
     time = self.data.timePartRegex.search(message.content.lower())
     if time is not None:
         period = time.group(0)
         hour = utils.get_hour()
         if period == 'morning':
             if hour is None or (hour > self.data.timeBound['morning'][0]
                                 and
                                 hour < self.data.timeBound['morning'][1]):
                 mess = self.data.responses['time'].format(
                     'morning', rand.choice(self.data.cute))
                 await message.channel.send(mess)
             else:
                 mess = self.data.responses['nottime'].format(
                     'morning', rand.choice(self.data.sad))
                 await message.channel.send(mess)
         elif period == 'night':
             if hour is None or (hour > self.data.timeBound['night'][0]
                                 or hour < self.data.timeBound['night'][1]):
                 mess = self.data.responses['time'].format(
                     'night', rand.choice(self.data.cute))
                 await message.channel.send(mess)
             else:
                 mess = self.data.responses['nottime'].format(
                     'night', rand.choice(self.data.sad))
                 await message.channel.send(mess)
예제 #6
0
 def __init__(self,data):
     self._type          = "attack.daily.stats"
     self._identifier    = data["identifier"]
     self._date          = utils.get_date(data['timestamp'])
     self._counts        = 1
     self._hours         = utils.get_hour(data["timestamp"])
     self._hourly        = {self._hours: 1}
    def day_conferences_fct(self, obj):

        day_conference_lst = []

        day_conferences = DayConference.objects.filter(conference=obj)
        for day_conference in day_conferences:
            day_conference_dict = {}
            day_conference_dict['id'] = day_conference.id
            day_conference_dict['day'] = cal_utils.get_day(day_conference.day)
            day_conference_dict['hour_start'] = cal_utils.get_hour(
                day_conference.hour_start)
            day_conference_dict['hour_end'] = cal_utils.get_hour(
                day_conference.hour_end)
            day_conference_dict['duration'] = cal_utils.get_duration(
                day_conference.hour_start, day_conference.hour_end)
            day_conference_dict['is_full'] = day_conference.is_full
            day_conference_lst.append(day_conference_dict)

        return day_conference_lst
예제 #8
0
def _get_day_conference_html_content(day_conference):
    from django.template.loader import get_template
    from django.template import Context

    from calendar_activities.serializers import ConferenceNestedChildsSerializer

    ser = ConferenceNestedChildsSerializer()
    speakers_str = ser.get_speakers_str(day_conference.conference)

    template_data = {}
    template_data['reserveFor'] = 'attelier/conférence'
    template_data['peoples'] = speakers_str
    template_data['day'] = utils.get_day(day_conference.day)
    template_data['hourStart'] = utils.get_hour(day_conference.hour_start)
    template_data['hourEnd'] = utils.get_hour(day_conference.hour_end)
    template_data['title'] = day_conference.conference.title

    htmly = get_template('calendar_activities/reserve_day_html_msg.html')
    d = Context(template_data)
    html_content = htmly.render(d)

    return unicode(html_content)
예제 #9
0
파일: main.py 프로젝트: ardikabs/fipro-core
def daily_counter(data):
    type = "attack.daily.stats"
    date = utils.get_date(data['timestamp'])
    hour = utils.get_hour(data["timestamp"])
    identifier = data['identifier']

    # Variable untuk mengetahui jumlah data pada collection ADS sesuai dengan tanggal
    dt = coll_daily.find({
        "type": type,
        "date": date,
        "identifier": identifier
    }).count()
    if dt == 0:  # Ketika jumlah data 0 maka membuat dokumen baru
        newdata = AttackDailyStats(data)
        coll_daily.insert(newdata.to_mongo())

    else:
        # Digunakan untuk cek pada dokumen dan mengetahui telah terdapat tanggal sesuai dengan data baru atau tidak
        mongodata = coll_daily.find(
            {
                "type": type,
                "date": date,
                "identifier": identifier
            }, {
                "hourly." + hour: {
                    '$exists': True
                }
            }).count()

        existdata = AttackDailyStats(data)
        if mongodata == 0:
            coll_daily.update({
                "type": type,
                "date": date
            }, existdata.to_set(), True, False)
        else:
            coll_daily.update({"type": type, "date": date}, existdata.to_inc())
예제 #10
0
def create_tweet_df(tweets):

    #Initialize an empty dataframe.
    df = pd.DataFrame([])

    # Add columns of twitter data to df.
    df['id'] = [tweet.id for tweet in tweets]
    df['text'] = [tweet.text for tweet in tweets
                  ]  # Some entries may be missing (see line ).
    df['retweet_count'] = [tweet.retweet_count for tweet in tweets]
    df['time'] = [tweet.created_at for tweet in tweets]
    df['location'] = [tweet.user.location for tweet in tweets]

    # Set the index of df as the tweet ids, and convert the text in the 'text' column to all
    # lowercase.
    df = df.set_index('id')
    df['text'] = df['text'].str.lower()

    # Create a new column called 'pst_time' that converts the datetime values in 'time' to follow
    # the Pacific Standard Time (PST).
    df['pst_time'] = (
        df['time'].dt.tz_localize("UTC")  # Set initial timezone to UTC.
        .dt.tz_convert(
            "America/Los_Angeles")  # Convert to Pacific Standard Time.
    )

    # Create a new column called hours that contains the hour of the day as floating point number
    # computed by: hour + minute/60 + second/60^2.
    df['hour'] = [utils.get_hour(val.time()) for val in df['pst_time']]

    # Remove all punctuations from 'text' and save it as a new column, 'no_punc'.
    punct_re = r'' "[^\w\d\s]"
    df['no_punc'] = df['text'].str.replace(punct_re, ' ')

    # Convert the tweets in 'no_punc' into a tidy format to make sentiments easier to calculate.
    # The index of the table should be the IDs of the tweets, repeated once for every word in the tweet.
    # tidy_format has 2 columns:
    # 1) num: the location of the word in the tweet.
    # 2) word: the individual words of each tweet.
    tempDF = df['no_punc'].str.split(expand=True)

    tempDF2 = pd.DataFrame(tempDF.stack(), columns=['word'])
    tempDF2.reset_index(inplace=True)

    tidy_format = tempDF2.rename(columns={
        'id': 'id',
        'level_1': 'num',
        'word': 'word'
    })
    tidy_format.set_index('id', inplace=True)

    # Add a polarity column to df. The polarity column contains the sum of the sentiment polarity of
    # each word in the text of the tweet.
    tf2 = tidy_format.reset_index()
    words = sent.reset_index().rename(columns={'token': 'word'})

    tempPol = pd.merge(tf2, words, how='left', on='word')

    pol = tempPol.groupby('id').sum()
    pol.drop('num', axis=1, inplace=True)

    df['polarity'] = pol['polarity']

    # Filter out the retweets.
    # df = df[df['text'].str[0:2] != 'rt']

    return df