Example #1
0
    def run(self):
        index_name = (self.source_definition.get('frontend_index')
                      or self.source_definition['index_name'])
        api_args = self.source_definition['frontend_args']

        # TODO: this stuff is hacky but no real idea to fix it in a good way
        try:
            from_date = iso8601.parse_date(api_args['date']['from'])
        except iso8601.ParseError:
            from_date = timestring.Range(api_args['date']['from']).start.date
        try:
            to_date = iso8601.parse_date(api_args['date']['to'])
        except iso8601.ParseError:
            to_date = timestring.Range(api_args['date']['to']).start.date
        api_args['date']['from'] = from_date.isoformat()[0:19]
        api_args['date']['to'] = to_date.isoformat()[0:19]

        api_results = 1
        api_page = 1
        api_offset = 0
        while api_results > 0 and api_page < 250:
            print "Fetching page %s ..." % (api_page, )
            api_args['from'] = api_offset
            results = self.api_request(index_name,
                                       self.source_definition['frontend_type'],
                                       **api_args)  # 100 for now ...
            for result in results[u'hits'][u'hits']:
                yield 'application/json', json.dumps(result)
            api_results = len(results[u'hits'][u'hits'])
            api_offset += api_results
            api_page += 1
Example #2
0
def get_body_data(client, date=None):

    username = str(client.effective_username)
    filename = ""
    start_date = datetime.today().date()
    dates = []
    weights = []
    body_fats = []

    client_weight = OrderedDict()
    client_body_fat = OrderedDict()

    if date is None:
        date_string = timestring.Range(
            '30 days ago').start.date.date().strftime('%m%d%Y')
        filename = username + '_body_data_since_' + date_string + '.csv'
        client_weight = client.get_measurements('Weight')
        client_body_fat = client.get_measurements('Body Fat %')
    else:
        filename = username + 'body_data_since_' + date + '.csv'
        start_date = start_date = timestring.Range(date).start.date.date()
        client_weight = client.get_measurements('Weight', start_date)
        client_body_fat = client.get_measurements('Body Fat %', start_date)

    i = 0
    date_keys = []
    for (d1, w) in sorted(client_weight.iteritems()):
        dates.append(datetime.strftime(d1, "%m/%d/%Y"))
        weights.append(w)
        date_keys.append(d1)

    body_fat_dict = dict(client_body_fat)
    for key in date_keys:
        body_fats.append(body_fat_dict.get(key, 0) or 0)

    f = open(filename, 'wt')
    try:
        writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
        writer.writerow(('Index', 'Date (mm/dd/YYYY)', 'Weight (lbs)',
                         'Weight (kg)', 'Body Fat (%)'))
        for i in range(len(dates)):
            writer.writerow(
                (i + 1, dates[i], weights[i], round(weights[i] * LBS_TO_KG,
                                                    2), body_fats[i]))
    finally:
        f.close()

    print(open(filename, 'rt').read())
Example #3
0
 def test_daterange(self):
     schema = valideer.parse({"daterange": "daterange"})
     for daterange in ('2 weeks', 'this year', 'next thursday'):
         self.assertEqual(
             schema.validate(dict(daterange=daterange))['daterange'],
             timestring.Range(daterange))
     self.assertRaises(error, schema.validate, dict(daterange="never"))
Example #4
0
    def migrate(self):
        db = self.db

        all_users = self.os.get_user()

        services = ['ceilometer', 'nova', 'neutron', 'glance', 'keystone', 'admin']

        for user in all_users:
            if user.name not in services:
                try:
                    found = db.select_by('email', user.email)

                    if found is None:
                        u = User()

                        u.user_id = user.id
                        u.project_id = user.default_project_id
                        u.email = user.email
                        u.enabled = user.enabled
                        u.name = user.name
                        u.expiration = (timestring.Range("next 30d")).end
                        u.history.register()

                        db.insert(u)
                except:
                    continue
Example #5
0
def get_nutrition_data(client, date=None):
    username = str(client.effective_username)
    filename = ""
    start_date = datetime.today().date()
    dates = []
    nutrition_data = []
    client_macros = []

    if date is None:
        date_string = timestring.Range('today').start.date.date().strftime(
            '%m%d%Y')
        filename = username + '_nutrition_data_since_' + date_string + '.csv'
        start_date = timestring.Range('today').start.date.date()
        dates.append(start_date)
        nutrition_data.append(client.get_date(start_date).totals)
    else:
        day = timedelta(days=1)
        date_o = timestring.Range(date).start.date.date()
        date_f = timestring.Range(date).end.date.date()

        filename = username + '_nutrition_data_since_' + date_o.strftime(
            '%m%d%Y') + '.csv'

        while date_o <= date_f:
            dates.append(date_o.strftime('%m/%d/%Y'))
            nutrition_datum = client.get_date(date_o).totals
            nutrition_data.append(client.get_date(date_o).totals)
            date_o += day

    f = open(filename, 'wt')
    try:
        writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
        writer.writerow(
            ('Index', 'Date (mm/dd/YYYY)', 'Calories', 'Fats (g)',
             'Carbohydrates (g)', 'Protein (g)', 'Fiber (g)', 'Sodium (mg)'))
        for i in range(len(nutrition_data)):
            writer.writerow(
                (i + 1, dates[i], nutrition_data[i]['calories'],
                 nutrition_data[i]['fat'], nutrition_data[i]['carbohydrates'],
                 nutrition_data[i]['protein'], nutrition_data[i]['fiber'],
                 nutrition_data[i]['sodium']))
    finally:
        f.close()

    print(open(filename, 'rt').read())
Example #6
0
    def age(self):
        """Return the age in years

        Returns:
            [type] -- [description]
        """

        diff = timestring.Range(self._birthday, timestring.Date('today'))
        return diff.elapse
Example #7
0
 def validate(self, value, adapt=True):
     try:
         if isinstance(value, timestring.Range):
             if value.tz is None:
                 value.tz = "UTC"
             return value
         return timestring.Range(value, tz="UTC")
     except timestring.TimestringInvalid:
         self.error("Invalid range provied")
Example #8
0
def validate_range(ctx, param, custom_range):
    if not custom_range:
        return None
    try:
        r = timestring.Range(custom_range)
        date_range = [r[0].date, r[1].date]
        return date_range
    except timestring.TimestringInvalid:
        raise click.BadParameter(
            "range needs to be a simple phrase like 'last 2 weeks' or a YYYY-MM-DD-YYYY-MM-DD string"
        )
Example #9
0
    def __generate_user_data(self, name, email, enabled, expire):

        user = User()

        user.name = name
        user.email = email
        user.enabled = enabled

        if (is_int(expire)):
            expire = str(expire) + "d"

        user.expiration = (timestring.Range("next " + expire)).end

        project_name = (user.name).title() + "'s project"
        password     = __generate_password()

        user.project_name = project_name
        user.password = password

        return user
Example #10
0
def date_filter(friends, from_date, to_date):
    """ date_filter creates a list of friend objects
    which the friends are added between from_date and to_date

    Arguments:
    friends: a list of friend objects
    from_date: a timestring.Date instance or None
    to_date: a timestring.Date instance or None
    """
    if from_date is None and to_date is None:
        return friends

    if from_date and to_date:  # both are not None
        date_range = timestring.Range(start=from_date, end=to_date)
        # if timestamp in date_range is implemented by the timestring library
        return [f for f in friends if f['timestamp'] in date_range]

    # before to_date
    if from_date is None:
        return [f for f in friends if f['timestamp'] < to_date]
    # after from_date
    if to_date is None:
        return [f for f in friends if f['timestamp'] > from_date]
Example #11
0
def DateConvertorFromInterval(UserInput):
    return timestring.Range(UserInput).start.date, timestring.Range(
        UserInput).end.date
Example #12
0
def sorted_todos_by_project(cfg, todo_cfg=None):
    """
    Takes our todo list, and returns two dictionaries of where the keys equal
    to the project name, and the value is a list of todo items under that
    project.

    - Note that a todo item may be appended to multiple second level HTML lists
        if the item is listed under multiple projects.
    - Note that todo items without a project are discarded.
    - Note that completed items beyond `completion_cutoff` (measured in days)
        are discarded.

    todo_cfg is called to topydo.config directs as the path of the test
        configuration. Setting this to None will use the normal configuration.
    """
    '''
    print(type(cfg))
    print(cfg)
    print(type(cfg['todo']), cfg['todo'])
    print(type(cfg['todo']['completion_cutoff']), cfg['todo']['completion_cutoff'])
    '''
    completion_range = timestring.Range('last {!s} days'.format(cfg['todo']['completion_cutoff']))

    my_sorter = Sorter(p_sortstring=cfg['todo']['sort_string'])

    todofile = TodoFile.TodoFile(topydo_config(todo_cfg).todotxt())
    # print('Loaded todo file from {}'.format(todofile.path))
    todotodos = TodoList.TodoList(todofile.read())
    # todolist = my_sorter.sort(todolist)            # in topydo v0.10
    # json_str = JsonPrinter().print_list(todolist)  # in topydo v0.10
    todolist = my_sorter.sort(todotodos.todos())    # sort before filters
    # filters return a list, so apply them all at once?
    todolist = HiddenTagFilter().filter(todolist)
    todo_json_str = JsonPrinter().print_list(todolist)
    todo_json = json.loads(todo_json_str)

    donefile = TodoFile.TodoFile(topydo_config(todo_cfg).archive())
    # print('Loaded done file from {}'.format(donefile.path))
    donetodos = TodoList.TodoList(donefile.read())
    donelist = my_sorter.sort(donetodos.todos())
    donelist = HiddenTagFilter().filter(donelist)
    done_json_str = JsonPrinter().print_list(donelist)
    done_json = json.loads(done_json_str)

    active_todos = {}
    completed_todos = {}

    for my_json in [todo_json, done_json]:
        for todo in my_json:
            if not todo['completed']:
                for project in todo['projects']:
                    try:
                        active_todos[project].append(todo['source'])
                    except KeyError:
                        active_todos[project] = [todo['source']]
            else:
                completion_date = timestring.Date(todo['completion_date'])
                if completion_date in completion_range:
                    for project in todo['projects']:
                        try:
                            completed_todos[project].append(todo['source'])
                        except KeyError:
                            completed_todos[project] = [todo['source']]

    return active_todos, completed_todos
Example #13
0
def get_full_nutrition_data(client, date=None):
    username = str(client.effective_username)
    filename = ""
    start_date = datetime.today().date()
    dates = []
    nutrition_data = []
    client_macros = []
    meal_keys = client.get_date(start_date).keys(
    )  #['supplements', 'meal 1', 'meal 2', 'meal 3', 'snacks', 'refeed']
    meals = []

    # for a single day

    if date is None:
        date_string = timestring.Range('today').start.date.date().strftime(
            '%m%d%Y')
        filename = username + '_nutrition_data_since_' + date_string + '.csv'
        start_date = timestring.Range('today').start.date.date()
        dates.append(start_date)
        nutrition_data.append(client.get_date(start_date).totals)
    else:
        day = timedelta(days=1)
        date_o = timestring.Range(date).start.date.date()
        date_f = timestring.Range(date).end.date.date()

        filename = username + '_full_nutrition_data_since_' + date_o.strftime(
            '%m%d%Y') + '.csv'

        while date_o <= date_f:
            nutrition_datum = client.get_date(date_o).totals
            nutrition_data.append(client.get_date(date_o))
            dates.append(date_o.strftime('%m/%d/%Y'))
            date_o += day

    f = open(filename, 'wt')
    try:
        writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)

        for i in range(len(nutrition_data)):
            writer.writerow(
                (i + 1, dates[i], 'Meals', 'Food Entry', 'Calories', 'Fat (g)',
                 'Carbohydrates (g)', 'Net Carbohydrate (g)', 'Protein (g)',
                 'Fiber (g)', 'Sodium (mg)'))
            for meal_key in nutrition_data[i].keys():

                writer.writerow(
                    (' ', ' ', meal_key, 'Totals',
                     (nutrition_data[i][meal_key].totals.get('calories', 0)
                      or 0), (nutrition_data[i][meal_key].totals.get('fat', 0)
                              or 0), (nutrition_data[i][meal_key].totals.get(
                                  'carbohydrates', 0) or 0),
                     (nutrition_data[i][meal_key].totals.get(
                         'carbohydrates', 0) or 0) if
                     ((nutrition_data[i][meal_key].totals.get(
                         'carbohydrates', 0) or 0) -
                      (nutrition_data[i][meal_key].totals.get('fiber', 0) or 0)
                      < 0) else
                     ((nutrition_data[i][meal_key].totals.get(
                         'carbohydrates', 0) or 0) -
                      (nutrition_data[i][meal_key].totals.get('fiber', 0) or 0)
                      ), (nutrition_data[i][meal_key].totals.get('protein', 0)
                          or 0),
                     (nutrition_data[i][meal_key].totals.get('fiber', 0) or 0),
                     (nutrition_data[i][meal_key].totals.get('sodium', 0)
                      or 0)))
                if len(nutrition_data[i][meal_key].entries) is 0:
                    continue
                else:
                    for meal_entry in nutrition_data[i][meal_key].entries:
                        writer.writerow(
                            (' ', ' ', ' ', meal_entry.name,
                             meal_entry['calories'], meal_entry['fat'],
                             meal_entry['carbohydrates'],
                             (meal_entry['carbohydrates']) if
                             (meal_entry['carbohydrates'] -
                              meal_entry['fiber'] < 0) else
                             (meal_entry['carbohydrates'] -
                              meal_entry['fiber']), meal_entry['protein'],
                             meal_entry['fiber'], meal_entry['sodium']))
            writer.writerow(
                (' ', ' ', ' ', 'Daily Totals',
                 (nutrition_data[i].totals.get('calories', 0)
                  or 0), (nutrition_data[i].totals.get('fat', 0) or 0),
                 (nutrition_data[i].totals.get('carbohydrates', 0) or 0),
                 (nutrition_data[i].totals.get('carbohydrates', 0) or 0) if
                 ((nutrition_data[i].totals.get('carbohydrates', 0) or 0) -
                  (nutrition_data[i].totals.get('fiber', 0) or 0) < 0) else
                 ((nutrition_data[i].totals.get('carbohydrates', 0) or 0) -
                  (nutrition_data[i].totals.get('fiber', 0) or 0)),
                 (nutrition_data[i].totals.get('protein', 0)
                  or 0), (nutrition_data[i].totals.get('fiber', 0)
                          or 0), (nutrition_data[i].totals.get('sodium', 0)
                                  or 0)))
    finally:
        f.close()

    print(open(filename, 'rt').read())
Example #14
0
def get_food_entries_data(client, filter=None):

    username = str(client.effective_username)
    date_string = timestring.Range('4 days ago').start.date.date().strftime(
        '%m%d%Y')
    dates = []
    nutrition_data = []
    food_entries = {}

    day = timedelta(days=1)
    date_o = timestring.Range('4 days ago').start.date.date()
    date_f = timestring.Range('4 days ago').end.date.date()

    while date_o <= date_f:
        current_diary_entry = client.get_date(date_o)
        for meal in current_diary_entry.meals:
            for entry in meal.entries:
                if food_entries.get(entry.name, 0) == 0:
                    entry_totals = entry.totals
                    entry_totals['frequency'] = 1
                    food_entries[entry.name] = entry_totals
                else:
                    food_entries[entry.name]['frequency'] += 1
        date_o += day

    sorted_food_entries = (
        sorted(food_entries.items(), key=lambda x: x[1].get('calories', 0)),
        sorted(
            food_entries.items(), key=lambda x: (x[1].get('fat', 0) or 1)
        ),  #/((x[1].get('protein',0) or 1) + (x[1].get('carbohydrates', 0) or 1) + (x[1].get('fat', 0) or 1))),
        sorted(
            food_entries.items(),
            key=lambda x: (x[1].get('carbohydrates', 0) or 1)
        ),  #/((x[1].get('protein',0) or 1) + (x[1].get('carbohydrates', 0) or 1) + (x[1].get('fat', 0) or 1))),
        sorted(food_entries.items(),
               key=lambda x: (x[1].get('carbohydrates', 0) or 0)
               if ((x[1].get('carbohydrates', 0) or 0) -
                   (x[1].get('fiber', 0) or 0) < 0) else
               ((x[1].get('carbohydrates', 0) or 0) -
                (x[1].get('fiber', 0) or 0))),
        sorted(
            food_entries.items(), key=lambda x: (x[1].get('protein', 0) or 1)
        ),  #/((x[1].get('protein',0) or 1) + (x[1].get('carbohydrates', 0) or 1) + (x[1].get('fat', 0) or 1))),
        sorted(food_entries.items(), key=lambda x: x[1].get('fiber', 0)),
        sorted(food_entries.items(), key=lambda x: x[1].get('sodium', 0)))

    if filter is None:
        f_calories = username + '_food_entries_sorted_by_calories' + '.csv'
        f_fat = username + '_food_entries_sorted_by_fat' + '.csv'
        f_carbohydrates = username + '_food_entries_sorted_by_carbohydrates' + '.csv'
        f_net_carbohydrates = username + '_food_entries_sorted_by_net_carbohydrates' + '.csv'
        f_protein = username + '_food_entries_sorted_by_protein' + '.csv'
        f_fiber = username + '_food_entries_sorted_by_fiber' + '.csv'
        f_sodium = username + '_food_entries_sorted_by_sodium' + '.csv'

        files = [
            f_calories, f_fat, f_carbohydrates, f_net_carbohydrates, f_protein,
            f_fiber, f_sodium
        ]
        file_number = 0
        for file in files:
            f = open(file, 'wt')
            try:

                writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
                writer.writerow(
                    ('Index', 'Food Entry', 'Calories', 'Fat (g)',
                     'Carbohydrates (g)', 'Net Carbohydrate (g)',
                     'Protein (g)', 'Fiber (g)', 'Sodium (mg)', 'Frequency'))
                i = 0

                for food_entry_name, food_entry_totals in sorted_food_entries[
                        Nutrient().nutrients_array[file_number]]:
                    writer.writerow(
                        (i + 1, food_entry_name, food_entry_totals['calories'],
                         food_entry_totals['fat'],
                         food_entry_totals['carbohydrates'],
                         (food_entry_totals.get('carbohydrates', 0) or 0) if
                         ((food_entry_totals.get('carbohydrates', 0) or 0) -
                          (food_entry_totals.get('fiber', 0) or 0) < 0) else
                         ((food_entry_totals.get('carbohydrates', 0) or 0) -
                          (food_entry_totals.get('fiber', 0) or 0)),
                         food_entry_totals['protein'],
                         food_entry_totals['fiber'],
                         food_entry_totals['sodium'],
                         food_entry_totals['frequency']))
                    i += 1

            finally:

                f.close()

            file_number += 1

    else:
        filename = username + '_food_entries_by_' + filter + '.csv'

        f = open(filename, 'wt')
        try:
            writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)

            writer.writerow(
                ('Index', 'Food Entry', 'Calories', 'Fat (g)',
                 'Carbohydrates (g)', 'Net Carbohydrate (g)', 'Protein (g)',
                 'Fiber (g)', 'Sodium (mg)', 'Frequency'))
            i = 0
            for food_entry_name, food_entry_totals in sorted_food_entries[
                    Nutrient().valueForKey(filter)]:
                writer.writerow(
                    (i + 1, food_entry_name, food_entry_totals['calories'],
                     food_entry_totals['fat'],
                     food_entry_totals['carbohydrates'],
                     (food_entry_totals.get('carbohydrates', 0) or 0) if
                     ((food_entry_totals.get('carbohydrates', 0) or 0) -
                      (food_entry_totals.get('fiber', 0) or 0) < 0) else
                     ((food_entry_totals.get('carbohydrates', 0) or 0) -
                      (food_entry_totals.get('fiber', 0) or 0)),
                     food_entry_totals['protein'], food_entry_totals['fiber'],
                     food_entry_totals['sodium'],
                     food_entry_totals['frequency']))
                i += 1

        finally:
            f.close()

        print(open(filename, 'rt').read())
Example #15
0
 def validate(self, value, adapt=True):
     super(elapse, self).validate(str(value))
     return str(len(timestring.Range(value)))
Example #16
0
#convert soup object to str to get dates.
soupp = str(soup_level1)

all_dates = []
for j in soupp.split('''<span class="date_posted" data-v-5b44ba93="">'''):
    all_dates.append(j.split('<')[0])

print(len(all_dates))
print(all_dates)
all_dates.pop(0)
all_jobs = jobs[2:]

# assert if the length of jobs and dates are same.
assert (len(all_jobs) == len(all_dates))

#change all string of dates(like '3 months ago' ) into datetime object.
datee = []
for u in all_dates:
    datee.append(str(timestring.Range(u)).split(' ')[1])
d = [datetime.strptime(i, '%m/%d/%y') for i in datee]
for i in range(len(all_jobs)):
    all_jobs[i][1] = d[i]

#creating a pandas dataframe
df = pd.DataFrame(all_jobs, columns=["JOB TITLE", "DATE"])
#sorting by ascending order of date.
df = df.sort_values(by='DATE')
#view the final dataframe.
print(df)
#save the dataframe into csv file.
df.to_csv("Techolution.csv")
Example #17
0
 def activity(self):
     r = timestring.Range(self.last_seen(), timestring.Date("now"))
     return r.elapse[:r.elapse.find("hour")+5]
 def _convert_to_seconds(self, s):
     return sum(map(lambda x: len(timestring.Range(x)), s.split(' ')))
Example #19
0
 def validate(self, value, adapt=True):
     try:
         _range = timestring.Range(value)
         return _range if adapt else value
     except timestring.TimestringInvalid:
         self.error("invalid date range provied")
Example #20
0
 def time_left(self):
     if (self.user.enabled is True):
         # e = timestring.Date(self.user.expiration)
         tl = timestring.Range("now", self.user.expiration)
         return tl