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

发布于 2025-01-23 21:52:27 字数 1432 浏览 2 评论 0原文

我正在为要运行的Python脚本编写一个用户配置文件,其中包括用户名,出生日期及其IP地址。会有多个用户,我在正确的轨道上吗?还是我使用列表/数组来替换它?

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))

I am writing a user profile for a python script to run, which includes the user name, date of birth and their ip address. There will be multiple users, am I on the right track? Or do I use a list/array to replace it?

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"

The code will be imported to the udp send script to send a message when the date match.

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))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说好的呢 2025-01-30 21:52:27

您可以使用字典将其作为普通列表保留

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",
    },
]

,并且应该使用 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 ...

edit:

最终您可以使用成员创建pandas.dataframe并在csv中写入 - 稍后您可以从csv读取而不是使用成员

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 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
]

创建类,以后您可以将其与 -loop一起使用,但可以使用< code> profile.dobprofile.nameprofile.ip而不是profile ['dob']配置文件['name']配置文件['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 ...

You could keep it as normal list with dictionares

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",
    },
]

and you should use for-loop to check every profile

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 ...

EDIT:

Eventually you could use members to create pandas.DataFrame and write in csv - so later you could read from csv instead of use members

import pandas as pd

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

print(df)

and

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 ...

EDIT:

If you want to use class then first you can create class for single profile

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

and next list with all instances

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
]

and later you can use it with for-loop but you can use profile.dob, profile.name, profile.ip instead of 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 ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文