[python exercise, 6.14] (class exercise)

Keywords: Attribute Qt Python

1. Check the continuous repetition of words in a word document. In the word document, there are often repeated words in the document due to careless keyboard operation, such as "user's information" or "user input required". Use the extension library Python docx to check the word document (QT learning road 2.doc) and prompt similar repeated Chinese characters

import re
from docx import Document
doc=Document('QT Learning road 2.docx')
text=''.join((p.text for p in doc.paragraphs))
result=re.findall(r'(([\u4e00-\u9fa5,!: ;,]).?\2)',text)
for word in result:
    print(word[0])


2. Class, complete each of the following single exercises step by step. Each part should have corresponding code.
2.1 create a class named User with the property first_name and last_name, along with several other attributes that User profiles usually store. Define a class named describe in the User class_ Method of User (), which prints a summary of User information; another one is defined as greet_ Method of User (), which sends a personalized greeting to the User.
Create multiple instances representing different users, and call the above two methods for each instance.

 class User:
    def __init__(self,first_name,last_name,age):
        self.first_name=first_name
        self.last_name=last_name
        self.age=age
    def describe_user(self):
        print("User first_name: "+self.first_name)
        print("User last_name: "+self.last_name)
        print("User age: "+str(self.age))
    def greet_user(self):
        print("Hello,I'm "+self.first_name+" "+self.last_name)
a=User("One","Two",20)
b=User("Third","Fourth",21)
a.describe_user()
a.greet_user()
b.describe_user()
b.greet_user()


2.2 in the User class above, add a login_ Properties of attempts. Write an increment_ login_ Method of attempts(), which will attribute login_ The value of attempts is increased by 1. Write another one called reset_ login_ Method of attempts(), which will attribute login_ The value of attempts is reset to 0.
Create an instance according to the User class, and then call the increment method_ login_attempts() multiple times. Print attribute login_ Value of attempts, verify that it is incremented correctly; then, call the method reset_login_attempts() and print the attribute login again_ Value of attempts, verify that it is reset to 0.

 class User:
    def __init__(self,first_name,last_name,age):
        self.first_name=first_name
        self.last_name=last_name
        self.age=age
        self.login_attempts=0
    def describe_user(self):
        print("User first_name: "+self.first_name)
        print("User last_name: "+self.last_name)
        print("User age: "+str(self.age))
    def greet_user(self):
        print("Hello,I'm "+self.first_name+" "+self.last_name)
    def increment_login_attempts(self):
        self.login_attempts+=1
    def reset_login_attempts(self):
        self.login_attempts=0
a=User("One","Two",20)
print(a.login_attempts)
for i in range(3):
    a.increment_login_attempts()
    print(a.login_attempts)
a.reset_login_attempts()
print(a.login_attempts)


2.3 the administrator is a special User. Write a class called Admin that inherits the User class you wrote for exercise 2.1 or exercise 2.2. Add an attribute called privileges to store a list of strings such as "can add post", "can delete post", "can ban user", etc. Write a show_ Method of privileges (), which shows the administrator's privileges. Create an instance of Admin and call this method.

class User:
    def __init__(self,first_name,last_name,age):
        self.first_name=first_name
        self.last_name=last_name
        self.age=age
        self.login_attempts=0
    def describe_user(self):
        print("User first_name: "+self.first_name)
        print("User last_name: "+self.last_name)
        print("User age: "+str(self.age))
    def greet_user(self):
        print("Hello,I'm "+self.first_name+" "+self.last_name)
    def increment_login_attempts(self):
        self.login_attempts+=1
    def reset_login_attempts(self):
        self.login_attempts=0
class Admin(User):
    __cando=["can add post","can delete post","can ban user"]
    def __init__(self,first_name,last_name,age):
        super().__init__(first_name,last_name,age)
    def show_privileges(self):
        for i in self.__cando:
            print(self.first_name+" "+self.last_name+" "+i)
a=Admin("One","Two",20)
a.show_privileges()


2.4 write a class called privileges with only one property - privileges, which stores the list of strings as described in exercise 2.3. Show method_ Privileges () is moved to this class. In the Admin class, use a privileges instance as its property. Create an instance of Admin and use the
show_privileges() to display their permissions.

class User():
	"""docstring for User"""
	def __init__(self, first_name,last_name):
		self.first_name = first_name
		self.last_name = last_name
		self.login_attempts = 0
 
	def describe_name(self):
		print("fitst_name :",self.first_name," last_name: ",self.last_name)
 
	def greet_user(self):
		print("hello, ",self.first_name," ",self.last_name)
 
	def  increment_login_attempts(self):
		self.login_attempts += 1
	def reset_login_attempts(self):
		self.login_attempts = 0
class Privileges():
	"""docstring for Privileges"""
	def __init__(self):
		self.privileges = ['can add post','can ban user','can delete post']
	def show_privileges(self):
		print(self.privileges)
class Admin(User):
	"""docstring for Admin"""
	def __init__(self, first_name,last_name):
		super().__init__(first_name,last_name)
		self.privileges = Privileges()
 
	def show_privileges(self):
		self.privileges.show_privileges()
 
admin = Admin("Alice",'Bob')
admin.show_privileges()


2.5 based on the work done in 2.1-2.4 above, users, Privileges and
The Admin class is stored in a module, and then a file is created, in which an instance of Admin is created and the method show is called_ Privileges () to make sure everything works correctly.
user.py

class User():
	"""docstring for User"""
	def __init__(self, first_name,last_name):
		self.first_name = first_name
		self.last_name = last_name
		self.login_attempts = 0
 
	def describe_name(self):
		print("fitst_name :",self.first_name," last_name: ",self.last_name)
 
	def greet_user(self):
		print("hello, ",self.first_name," ",self.last_name)
 
	def  increment_login_attempts(self):
		self.login_attempts += 1
	def reset_login_attempts(self):
		self.login_attempts = 0
class Privileges():
	"""docstring for Privileges"""
	def __init__(self):
		self.privileges = ['can add post','can ban user','can delete post']
	def show_privileges(self):
		print(self.privileges)
class Admin(User):
	"""docstring for Admin"""
	def __init__(self, first_name,last_name):
		super().__init__(first_name,last_name)
		self.privileges = Privileges()
 
	def show_privileges(self):
		self.privileges.show_privileges()

main.py

from user import Admin
admin = Admin("Alice",'Bob')
admin.show_privileges()


2.6 store the User class in one module, and the privileges and Admin classes in another module. Create another file, create an instance of Admin, and call show_privileges() to make sure everything still works correctly.
user.py

class User():
	"""docstring for User"""
	def __init__(self, first_name,last_name):
		self.first_name = first_name
		self.last_name = last_name
		self.login_attempts = 0
 
	def describe_name(self):
		print("fitst_name :",self.first_name," last_name: ",self.last_name)
 
	def greet_user(self):
		print("hello, ",self.first_name," ",self.last_name)
 
	def  increment_login_attempts(self):
		self.login_attempts += 1
	def reset_login_attempts(self):
		self.login_attempts = 0

privileges.py

class Privileges():
	"""docstring for Privileges"""
	def __init__(self):
		self.privileges = ['can add post','can ban user','can delete post']
	def show_privileges(self):
		print(self.privileges)

admin.py

from user import User
from privileges import Privileges
class Admin(User):
	"""docstring for Admin"""
	def __init__(self, first_name,last_name):
		super().__init__(first_name,last_name)
		self.privileges = Privileges()
 
	def show_privileges(self):
		self.privileges.show_privileges()
		

main.py

from user import User
from admin import Admin
from privileges import Privileges
admin = Admin("Alice",'Bob')
admin.show_privileges()

Posted by kazuki on Sat, 13 Jun 2020 22:26:46 -0700