在Python中创建具有多个属性的用户配置文件的最佳方法?
我正在为要运行的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用字典将其作为普通列表保留
,并且应该使用 loop的
检查每个配置文件
edit:
最终您可以使用
成员
创建pandas.dataframe
并在csv
中写入 - 稍后您可以从csv
读取而不是使用成员
和
编辑:
如果要使用类,则首先可以使用
所有实例为单个配置文件和下一个列表
创建类,以后您可以将其与 -loop一起使用
,但可以使用< code> profile.dob
,profile.name
,profile.ip
而不是profile ['dob']
,配置文件['name']
,配置文件['ip']
You could keep it as normal list with dictionares
and you should use
for
-loop to check every profileEDIT:
Eventually you could use
members
to createpandas.DataFrame
and write incsv
- so later you could read fromcsv
instead of usemembers
and
EDIT:
If you want to use class then first you can create class for single profile
and next list with all instances
and later you can use it with
for
-loop but you can useprofile.dob
,profile.name
,profile.ip
instead ofprofile['dob']
,profile['name']
,profile['ip']