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