一、功能简介
python实现随机产生用户名、用户ID、用户年龄、用户职业、用户兴趣,随机在用户之间形成followee(关注者)和follower(追随者)(随机性也有一定的几率设置)
根据概率情况为每位用户设置级别(94%机率为普通用户、3%机率为小众明星、2%机率为普通明星、1%机率为超级明星),该级别可以更好的模拟社交平台中的followee(关注者)和follower(追随者)关系
普通用户(normal):只有与该普通用户A年龄相仿(年龄之差在5岁之内)且职业、兴趣爱好均相同的用户B才会成为该普通用户A的follower(追随者);
小众明星(smallstar):与小众明星A年龄相仿(年龄之差在5岁之内)的用户B才会成为该小众明星A的follower(追随者);
普通明星(star):与普通明星A年龄相仿(年龄之差在5岁之内)或职业与兴趣爱好均相同用户B会成为该普通明星A的follower(追随者);
超级明星(superstar):与该超级明星A年龄相仿(年龄之差在5岁之内)或职业或兴趣爱好相同的用户B都会成为该普通用户A的follower(追随者)
- 为了更好模拟社交平台中的用户情况(年龄段用户),在随机设置用户年龄的同时设置概率,控制不同年龄段出现的概率:20%(12岁-18岁)、50%(18岁-35岁)、25%(35岁-50岁)、4.9%(50岁-70岁)、0.1%(70岁-100岁)、无12岁以下用户群体
- 给定任意用户,输出该用户的followee和follower用户
- 给定两个用户,求他们的共同followee和follower用户
亮点:
- 完成所有任务(包括选做题)的同时为用户设置年龄;
- 在随机生成年龄的同时设置概率控制不同年龄段用户出现的概率,同时未设置12岁以下用户(不允许注册),更好的模拟社交平台用户年龄情况;
- 设置概率控制superstar、star、smallstar、normal出现概率,并根据头衔随机分配followee和follower,更符合社交平台社交情况;
- 在设置每位用户的followee和follower时也不是充分随机,用户之间有共同元素(年龄、职业、兴趣)将会更容易建立followee和follower关系,更加符合社交平台用户社交情况;
- 程序具有较强的健壮性,对用户的输入进行检查,遇到错误输入时弹出提示并要求用户根据提示继续输入。
二、功能测试
模拟微博等社交软件的用户机制,生成一组用户集合(以数字或字母ID作为唯一身份标志),并随机在用户之间形成followee(关注者)和follower(粉丝)。
1、设置user类,在类中设置ID,name,interest,occupation,age,follower_ID,followee_ID元素,设置create_user(number,_user)、user_set(_user,user)和menu(_user)函数,分别用于随机创建用户、随机生成follower和followee关系和用户follower和followee的查询;
2、调用1000次create_user(number,_user)函数随机1000个用户进行模拟测试,并将所生成的用户ID、follower和followee输出存入到user_file.txt文件中(方便查询测试);
3、再调用1000次user_set(_user,user)函数随机生成follower和followee关系;
4、调用menu(_user)函数进入程序,用户输入1或2选择服务类型
5、当有错误输入时,即用户输入不是‘1’或‘2’
6、给定任意用户ID,查询该用户的followee和follower用户,然后要求用户根据提示输入决定是否继续查询
7、给定两个用户,查询他们的共同followee和follower用户,然后要求用户根据提示输入决定是否继续查询
三、功能代码
import random
import string
class user():
def __init__(self,ID,name,interest,occupation,age):
self.ID=ID #ID唯一标识身份
self.name=name #用户名
self.interest=interest #用户的兴趣
self.occupation=occupation #用户的职业
self.age=age #用户的年龄
r=random.random() #概率统计时使用
if r<0.94: #94%的概率
self.title='normal' #设置其为普通用户
elif r<0.97: #3%的概率
self.title='smallstar' #设置其为小明星
elif r<0.99: #2%的概率
self.title='star' #设置其为普通明星
else: #1%的概率
self.title='supperstar' #设置其为超级明星
self.follower_ID=[] #follower为关注该用户的用户
self.followee_ID=[] #followee为该用户关注的用户
def create_user(number,_user): #随机创建用户,number为用户数量
user_ID=random.sample(range(1000000,9999999),number) #随机生成不重复的7位数,作为用户的ID
user_name=[''.join(random.sample(string.ascii_letters + string.digits, 5)) for i in range(number)] #随机生成字符串(长度为5)
user_interest=['运动','电影','摄影','游戏','数码','动漫','时尚','无'] #用户兴趣
user_occupation=['学生','医生','教师','程序员','会计','网红','工农','无'] #用户职业
user_information=dict(zip(user_ID,user_name)) #将用户ID和用户名合成为字典对应起来
for key,value in user_information.items():
r=random.random() #概率统计时使用
if r<0.2: #20%的概率年龄在12岁到18岁
random_age=random.randint(12,18) #用户的年龄
elif r<0.7: #50%的概率年龄在18岁到35岁
random_age=random.randint(18,35) #用户的年龄
elif r<0.95: #25%的概率年龄在35岁到50岁
random_age=random.randint(35,50) #用户的年龄
elif r<0.999: #4.9%的概率年龄在50岁到70岁
random_age=random.randint(50,70) #用户的年龄
else: #0.1%的概率年龄在70岁到100岁
random_age=random.randint(70,100) #用户的年龄
if random_age<22:
_user.append(user(key,value,user_interest[random.randint(0,7)],user_occupation[0],random_age))#22岁以下职业默认学生
elif random_age<32:
_user.append(user(key,value,user_interest[random.randint(0,7)],user_occupation[random.randint(0,7)],random_age))
elif random_age<65: #32岁以上不允许职业为学生
_user.append(user(key,value,user_interest[random.randint(0,7)],user_occupation[random.randint(1,7)],random_age))
else: #65岁以上职业默认为无
_user.append(user(key,value,user_interest[random.randint(0,7)],user_occupation[7],random_age))
return user_ID
def user_set(_user,user): #为用户随机分配关注者和被关注者
#如果该用户是超级明星,则所有和该用户相同行业或年龄相仿(差距在5岁之内)或兴趣爱好相同的用户都会成为他的floower
if user.title=='supperstar':
for i in range(1000):
if _user[i].interest==user.interest or _user[i].occupation==user.occupation or abs(_user[i].age-user.age)<=5:
user.follower_ID.append(_user[i].ID)
_user[i].followee_ID.append(user.ID)
#如果该用户是普通明星,则所有和该用户年龄相仿(差距在5岁之内)或兴趣爱好、职业相同的用户都会成为他的floower
elif user.title=='star':
for i in range(1000):
if (_user[i].occupation==user.occupation and _user[i].interest==user.interest) or abs(_user[i].age-user.age)<=5:
user.follower_ID.append(_user[i].ID)
_user[i].followee_ID.append(user.ID)
#如果该用户是小明星,则所有和该用户年龄相仿(差距在5岁之内)的用户都会成为他的floower
elif user.title=='smallstar':
for i in range(1000):
if abs(_user[i].age-user.age)<=5:
user.follower_ID.append(_user[i].ID)
_user[i].followee_ID.append(user.ID)
#如果该用户是普通用户,则所有和该用户相同行业、年龄相仿(差距在5岁之内)且兴趣爱好均相同的用户会成为他的floower
elif user.title=='normal':
for i in range(1000):
if _user[i].interest==user.interest and _user[i].occupation==user.occupation and abs(_user[i].age-user.age)<=5:
user.follower_ID.append(_user[i].ID)
_user[i].followee_ID.append(user.ID)
else:
print("title error!!!!\ntitle error!!!!\ntitle error!!!!")
def menu(_user): #用户查询主页
choice=input("1、给定任意用户ID,查询该用户的followee和follower用户\n2、给定两个用户,查询他们的共同followee和follower用户\n请输入您要执行的操作:")
sign=0 #标记为0表示用户输入的ID未注册,sing=1表示查询成功
if choice=='1':
user_id=input("请输入您要查询的用户ID:")
for user in _user:
if str(user.ID)==user_id:
print(str(user_id)+"的followee:")
print(user.followee_ID)
print(str(user_id)+"的follower:")
print(user.follower_ID)
sign=1
break
if sign==0:
print("ID未注册")
choice2=input("查询完成,继续查询输入'y',退出查询按任意键:")
elif choice=='2':
user1_id=input("请输入您要查询的用户1的ID:")
user2_id=input("请输入您要查询的用户2的ID:")
for user1 in _user:
if str(user1.ID)==user1_id:
for user2 in _user:
if str(user2.ID)==user2_id:
same_followee=[followee for followee in user1.followee_ID if followee in user2.followee_ID]
same_follower=[follower for follower in user1.follower_ID if follower in user2.follower_ID]
print(str(user1_id)+'和'+str(user2_id)+'的共同followee:')
print(same_followee)
print(str(user1_id)+'和'+str(user2_id)+'的共同follower:')
print(same_follower)
sign=1
break
break
if sign==0:
print("ID未注册")
choice2=input("查询完成,继续查询输入'y',退出查询按任意键:")
else:
choice2=input("输入错误,请按要求输入\n继续查询输入'y',退出查询按任意键")
if choice2=='y':
menu(_user)
_user=[] #创建的用户存入列表中
user_ID=create_user(1000,_user) #创建1000个用户,同时把用户的ID返回到user_ID中
with open('user_id.txt','w') as user_file: #将用户ID存入txt文件中
for i in range(1000): #为每位用户随机分配关注者和被关注者
user_set(_user,_user[i])
user_file.write(str(_user[i].ID)+":\nfollowee:"+str(_user[i].followee_ID)+"\nfollower:"+str(_user[i].follower_ID)+"\n")
menu(_user) #调用查询菜单