Explain how Python's big chat system's shielding swearing function principle _python

Source: Internet
Author: User
Tags datetime file handling local time

It suddenly occurred to me that a video of the pinball screen was very interesting with a full screen of harmony. Then want to use Python to try to write a look, the results are really playing a point of effect, the idea is that first you have to have a bad language storage warehouse good time to detect, then the individual or like to use the list, because the list of flexible use of expansion is very convenient, We're just trying to figure out how to block the dirty words in the core, remember what the player typed in the Barrage data type? The first file is followed by a string if there is no special requirement on it, million change in which is always these data structures, with a string replacement * number of things are much easier, right, today's chat is a complete set of structures, To make it clearer, I'll split the code and then assemble it so that everyone will have a deeper understanding. First look at the core of the features replace the foul language code as follows:

#!/usr/bin/env python
#-*-coding:utf-8-*-

import datetime time
= Datetime.datetime.now ()

speak = ' You dog day, fuckr your mother Yo, you're a fairy board, an old hemp batch '
dirty = [' Fuck ', ' Dog Day ', ' bastard ', ' hemp ', ' Fairy board ', ' R your Mother ', ' You ', ' grass you '] for

i in Dirty:
  Speak = Speak.replace (i, ' * ')
print speak+ "| "+str (Time)

Is it easy to block out the dirty words, this imported the time module DateTime used to get the current local time, later used to write log save, of course, many game dialogs will also show the current time, and then speak is the player to output the foul language ( Now the demo is going to change to input, the real inputs are interacting. Dirty is the definition of the language of the library (of course not write too much because each area is different too much to write), followed by a for loop to retrieve, as long as you say inside the word contains a bad language library, then we will replace with replace, The final print is equal to the caption displayed on the public screen is * ... And, of course, we have to figure out how to keep track of who said what time, this association will use the class, there must be doubt why use class, first class classes are a class (such as table tables, round tables, meetings tables, desks, etc.) description of things, such as a group of players player, They all have their own game names, and then they all have the chat function (which we call the action), which can be categorized and differentiated. The code is as follows:

Class Player (object):

  def __init__ (self,name):
    self.name = name

  def talk (self):
    self.string = ' Whatever fuck no joke '
    self.log ()
    self.string = self.string.replace (' Fuck ', ' ya Minute Fly butterfly ')
    print "Public screen display:%s--%s"% ( Self.name,self.string 

  def log (self):
    print "log as:%s--%s"% (self.name,self.string)

t1 = Player (' White ')
T1.talk ()

Here is a comprehensive description of the use of the class and workflow effects, here first create a class player player class, Init initializes his name, which he has chat function talk, but also has the logging function, then to chat operation Flow, After each player registered login game will have a game name (you can understand to login after QQ), here Self.name=name equals to get your personal name (net name), and then hit the chat window into talk,self.string you enter the chat information, Self.log record log, the focus is to prioritize your original words into the chat log so that you can find your records, Self.string.replace replace you say, then the screen will only show the words after the replacement, such as the printing of this is the fuck programmed minute fly butterfly ... This workflow is not very clear, of course, you do not understand the class method here will be very confused, slowly as long as the idea of learning classes after the very easy. Of course this is not the end, and finally put the basic complete code out of the code as follows:

import datetime

= str (Datetime.datetime.now ()) [: -7]

dirty = [' Fuck ', ' Dog Day ', ' bastard ', ' hemp ', ' Fairy board ', ' R your Mother ', ' Screw you ', ' Grass You ']

class Player (object):

  def __init__ (self,name):
    self.name = name

  def talk (self):
    self.string = Raw_input ("input-write:")
    # self.string = ' You dog day, r your mother Yo, you're a fairy board, an old hemp '
    self.log () for
    I in Dirty:
      self.string = Self.string.replace (i, ' ya Minute Fly butterfly ')
    print ' {} {}-speak:{} '. Format (Time,self.name, Self.string,)

  def log (self): with
    open (' zanghua.txt ', ' a ') as F:
      F.write ("{} {}-speak:{}\n". Format ( time,self.name,self.string)


t1 = player (' white ')
t2 = player (' black ')
t3 = player (' green ')
While True:
  n = raw_input (' Change Player: ')
  if n = = ' 1 ':
    t1.talk ()
  elif n = = ' 2 ':
    t2.talk () 
   elif n = = ' 3 ':
    t3.talk ()
  elif n = = ' Q ':
    print ' Bye ' break
  else:
    print "Are you kidding me?"

The test results are as follows:

You can create a new file named Zanghua.txt empty text on the line, just to demonstrate the behavior of the storage log, the above print said change player is to demonstrate the different user input made a simple switch, the actual system you log in rarely switch users and so on, just the current account chat , the code here adds file handling with open (file name, open mode), and then writes write (remember to add a newline character at the end of the write, or write the first line to make it hard to recognize) about Datetime[:7] Slices It is more concise to ignore the remainder of the time after printing. Basically the process is so, of course, can have a lot of extensions, such as the number of swearing more than the ban on speaking, limit the speeches for a few minutes ah, log can be timed to empty Ah, and so on. This depends on everyone to study, the amount of code is a bit big here will not show.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.