Python3 counts every year, every month and every day according to time. Don't be humble and ask for advice. Thank you very much.

def get_firstday_lastday(year=None, month=None):
    """
    :param year: intstr
    :param month: intstr
    :return: firstDay: datetime.date
              lastDay: datetime.date
    """
    if year:
        year = int(year)
    else:
        year = datetime.date.today().year

    if month:
        month = int(month)
    else:
        month = datetime.date.today().month

    -sharp 
    firstDayWeekDay, monthRange = calendar.monthrange(year, month)

    -sharp 
    firstDay = datetime.date(year=year, month=month, day=1)
    lastDay = datetime.date(year=year, month=month, day=monthRange)

    return firstDay, lastDay
-sharp !/usr/bin/python3
-sharp encoding=utf-8


import user_list
import function
import datetime


reg_u = user_list.get_registereduser()
month_list, month1, month2, month3, month4, month5, month6, \
    month7, month8, month9, month10, month11, month12 = \
    [], [], [], [], [], [], [], [], [], [], [], [], []

for i in range(1, 13):
    date = function.get_firstday_lastday(2018, i)
    for d in date:
        day = datetime.datetime.strptime(str(d), "%Y-%m-%d")
        month_list.append(day)

for u in reg_u:
    if month_list[0] <= u["regDate"] < month_list[2]:
        month1.append(u)
    elif month_list[2] <= u["regDate"] < month_list[4]:
        month2.append(u)
    elif month_list[4] <= u["regDate"] < month_list[6]:
        month3.append(u)
    elif month_list[6] <= u["regDate"] < month_list[8]:
        month4.append(u)
    elif month_list[8] <= u["regDate"] < month_list[10]:
        month5.append(u)
    elif month_list[10] <= u["regDate"] < month_list[12]:
        month6.append(u)
    elif month_list[12] <= u["regDate"] < month_list[14]:
        month7.append(u)
    elif month_list[14] <= u["regDate"] < month_list[16]:
        month8.append(u)
    elif month_list[16] <= u["regDate"] < month_list[18]:
        month9.append(u)
    elif month_list[18] <= u["regDate"] < month_list[20]:
        month10.append(u)
    elif month_list[20] <= u["regDate"] < month_list[22]:
        month11.append(u)
    elif month_list[22] <= u["regDate"] < month_list[23]:
        month12.append(u)

print(
    len(month1), len(month2), len(month3), len(month4), len(month5), len(month6),
    len(month7), len(month8), len(month9), len(month10), len(month11), len(month12)
)

early self-study, the above is to do a monthly statistics of the whole year (please give me some advice on optimizing the code)

also want to do some all days in a month and all hours in a day you can make a statistical tree or line chart later. I don"t know how to start

.

Don"t be humble and ask for advice. Thank you very much.

May.22,2021

Why bother? just throw a wide table at the relational database. What dimension you want is directly a matter of sql.


-sharp !/usr/bin/python3
-sharp encoding=utf-8

import user_list
import pandas as pd

month_list = []
day_list = []
hour_list = []

for u in reg_u:
    month_list.append(u['regDate'].month)
    day_list.append(u['regDate'].day)

month = pd.Series(month_list)
month_num = month.value_counts()
day = pd.Series(day_list)
day_num = day.value_counts()


print(month_num)
print(day_num)

understanding of how painful it is

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e476d0-44c26.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e476d0-44c26.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?