在 python 中创建具有多个属性的用户配置文件的最佳方法?

Best way to create user profile with multiple attributes in python?

我正在将 python 脚本的用户配置文件写入 运行,其中包括用户名、出生日期和他们的 IP 地址。会有多个用户,我走对了吗?还是用 list/array 代替?

import datetime
from mimetypes import init

class member:
  def __init__(member, name, dob, ip):
    member.name = "John"
    member.dob = datetime.datetime(1989, 10, 20)
    member.ip = "192.168.0.1"
  def __init__(terry, name, dob, ip):
    terry.name = "Terry"
    terry.dob = datetime.datetime(2000, 1, 23)
    terry.ip = "192.168.0.2"
  def __init__(andrew, name, dob, ip):
    andrew.name = "Andrew"
    andrew.dob = datetime.datetime(2001, 8, 3)
    andrew.ip = "192.168.0.3"
  def __init__(adrian, name, dob, ip):
    adrian.name = "Adrian"
    adrian.dob = datetime.datetime(2001, 9, 20)
    adrian.ip = "192.168.0.4"

代码将导入到udp发送脚本中,以便在日期匹配时发送消息。

import os
import socket
from datetime import datetime
from member import member

#TODO
#add time and condition on date match to send to specific ip for each member
if member.dob.month & member.dob.day == datetime.datetime.now():
    MESSAGE = "Happy birthday to you!" + member.name
    print(MESSAGE)
else:
    pass


UDP_IP = member.ip
UDP_PORT = 5005
#MESSAGE = "Happy birthday to you!"

print("UDP target IP: %s" % UDP_IP)
print("UDP target port: %s" % UDP_PORT)
print("message: %s" % MESSAGE)

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

你可以用字典将其保留为普通列表

import datetime

members = [
    {
        'name': "John",
        'dob': datetime.datetime(1989, 10, 20),
        'ip': "192.168.0.1",
    },
    {
        'name': "Terry",
        'dob': datetime.datetime(2000, 1, 23),
        'ip': "192.168.0.2",
    },
    {
        'name': "Andrew",
        'dob': datetime.datetime(2001, 8, 3),
        'ip': "192.168.0.3",
    },
    {
        'name': "Adrian",
        'dob': datetime.datetime(2001, 9, 20),
        'ip': "192.168.0.4",
    },
    {
        'name': "Other",
        'dob': datetime.datetime(2001, 4, 25),  # today date for test
        'ip': "192.168.0.5",
    },
]

并且您应该使用 for-loop 来检查每个配置文件

today = datetime.datetime.now()

for profile in members:
    dob = profile['dob']
    if (dob.month == today.month) and (dob.day == today.day):
        print(profile['name'], profile['ip'])
        # ... send message ...

编辑:

最终您可以使用 members 创建 pandas.DataFrame 并写入 csv - 所以稍后您可以从 csv 读取而不是使用 members

import pandas as pd

df = pd.DataFrame(members)
df.to_csv('memebers.csv', index=False)

print(df)

import pandas as pd

df = pd.read_csv('memebers.csv')
df['dob'] = pd.to_datetime(df['dob'])                 

print(df)

today = datetime.datetime.now()

for index, profile in df.iterrows():
    dob = profile['dob']
    if (dob.month == today.month) and (dob.day == today.day):
        print(profile['name'], profile['ip'])
        # ... send message ...

编辑:

如果您想使用 class,那么您可以先为单个配置文件创建 class

class Profile:   # PEP8: `CamelCaseNames` for classes
    def __init__(self, name, dob, ip):
        self.name = name
        self.dob = dob
        self.ip = ip

和包含所有实例的下一个列表

members = [
    Profile("John", datetime.datetime(1989, 10, 20), "192.168.0.1"),
    Profile("Terry", datetime.datetime(2000, 1, 23), "192.168.0.2"),
    Profile("Andrew", datetime.datetime(2001, 8, 3), "192.168.0.3"),
    Profile("Adrian", datetime.datetime(2001, 9, 20), "192.168.0.4"),
    Profile("Other", datetime.datetime(2001, 4, 25), "192.168.0.5"), # today date for test
]

之后您可以将它与 for 循环一起使用,但您可以使用 profile.dobprofile.nameprofile.ip 而不是 profile['dob']profile['name'], profile['ip']

today = datetime.datetime.now()

for profile in members:
    if (profile.dob.month == today.month) and (profile.dob.day == today.day):
        print(profile.name, profile.ip)
        # ... send message ...