Washing machine - dvirby20/BotIt GitHub Wiki

Washing machine is example for feature that used in mongoDB technology and also used in schedule_actions class.

Let's see some important parts in this feature.

.

For the full code you invited to take a look Hear

Important Examples

Check if exit objects in the collection

This part of code check if exist objects in the collection washing_machine_db

if (len(washing_machine_db.objects) == 0):
    self.details += f"\n\n***No have washing machines to display***"

Explain

  • len(washing_machine_db.objects) == 0- check if have objects in washing_machine_db collection

Get values of field of object

This part of code add to the variable self.details the info about each object of washing_machine_db

for machine in washing_machine_db.objects:
     if (machine.name == None or washing_machine_logic().check_available(machine.name)):
          self.details += f"\n\n***Washing machine name: {machine.name}***\n***Status: *** Available"
     else:
          self.details+= f"\n\n***Washing machine name: {machine.name}***\n***Status: *** Used\n***by: ***{machine.user.name}\n***until:*** {machine.end_time.strftime('%Y-%m-%d, %H:%M')}"

Explain

  • washing_machine_db- the collection
  • washing_machine_db.objects- give all the objects in the collection washing_machine_db
  • machine- is a washing_machine_db object
  • machine.name- give the value of the field name of the object machine

Filter objects by criterions

The following code check if washing machine is in work:

def check_available(self,name:str) -> bool:
    """checking if washing machine in used"""
    washing_machine=washing_machine_db.objects(name=name)
    for machine in washing_machine:
        if (machine.end_time==None or machine.end_time<datetime.now()):
            return True
    return False

Explain

  • washing_machine=washing_machine_db.objects(name=name)- filter and give only the objects that their name equal to name (the first one is the field, and the second value.