二手产品经理

二手产品经理

THIS IS RENO

replit db - 61 days - Learn Python online for 100 days

Record#

Today's topic is learning to use replit db. Before using replit db, we need to import the db object, which can be done using the from replit import db import method. Unlike before, this syntax imports the db object from replit, rather than importing the entire module. If we only use import, it imports the entire module without specifying the object.

When using replit db, we can store data in the database using the assignment method, for example db[key] = value. To retrieve data from the database, we can use db.keys() to get all the keys. If we want to match all values in the database with a prefix, we can use db.prefix(value). To delete data from the database, we can use del db[key] to remove it.

replit db can store various types of data, such as strings, dictionaries, lists, etc. Today's code exercise is to write a single-user Twitter, which seems like a big project.

CODE#

from replit import db
import datetime, os

print("Tweeter")
again = "y"
while True:
  os.system("clear")
  if again == "y":
    timestamp = datetime.datetime.now()
    menu = input("1: Add tweet\n2: View Tweets\n")
    if menu == "1":
      tweet = input("Input your tweet > ")
      db[timestamp] = tweet
    elif menu == "2":
      tweets = db.keys()
      i = 0
      for key in tweets:
        print(f"{key}:{db[key]}")
        i += 1
        if i == 10:
          show = input("show? > y/n ")
          if show == "y":
            os.system("clear")
            i = 1
            continue
          else:
            break
  elif again == "n":
    break
  again = input("again > ")
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.