Skip to content

Instantly share code, notes, and snippets.

@kajuberdut
Created June 15, 2023 01:11
Show Gist options
  • Save kajuberdut/56f39dfef18ea1d560bd9d3b312a15ec to your computer and use it in GitHub Desktop.
Save kajuberdut/56f39dfef18ea1d560bd9d3b312a15ec to your computer and use it in GitHub Desktop.
Using WizardCoder-15B-1.0-GPTQ to make a simple note app

Below is an instruction that describes a task. Write a response that appropriately completes the request

Instruction:

Please write a detailed list of files, and the functions those files should contain, for a python application. The application is a simple note taking app. The application should use a sqlite3 back-end and must use the falcon web framework.

Response:Great! Here's a basic outline of what the application should do:

  1. The user should be able to create a new note by providing a title and a body.
  2. The user should be able to view all their notes.
  3. The user should be able to view a specific note by providing its ID.
  4. The user should be able to update a note by providing its ID and the new title and/or body.
  5. The user should be able to delete a note by providing its ID.

Here's a basic outline of how the application should be structured:

  1. Create a new falcon project using the falcon-quickstart command.
  2. Create a models.py file to define the Note model and a database.py file to handle database connections and queries.
  3. Define the Note model in models.py with fields for title, body, and creation date.
  4. Define a NoteResource class in resources.py that handles the API endpoints for creating, viewing, updating, and deleting notes.
  5. Implement the create_note, get_notes, get_note, update_note, and delete_note methods in NoteResource to handle the API requests.
  6. Implement the create_note method to insert a new note into the database and return the new note's ID.
  7. Implement the get_notes method to retrieve all notes from the database and return them as a list of Note objects.
  8. Implement the get_note method to retrieve a specific note by ID from the database and return it as a Note object.
  9. Implement the update_note method to update an existing note in the database and return the updated note.
  10. Implement the delete_note method to delete an existing note from the database.
  11. Create a config.py file to define the database connection string and any other configuration settings.
  12. Update the main.py file to create the database connection and register the NoteResource with the falcon app.

Here's some sample code to get you started:

# models.py
from datetime import datetime

class Note:
    def __init__(self, title, body, created_at=None):
        self.title = title
        self.body = body
        self.created_at = created_at or datetime.now()

# database.py
import sqlite3

class Database:
    def __init__(self, connection_string):
        self.connection_string = connection_string
        self.connection = None

    def connect(self):
        self.connection = sqlite3.connect(self.connection_string)

    def close(self):
        self.connection.close()

    def create_note(self, title, body):
        cursor = self.connection.cursor()
        cursor.execute("INSERT INTO notes (title, body, created_at) VALUES (?,?,?)", (title, body, datetime.now()))
        self.connection.commit()
        return cursor.lastrowid

    def get_notes(self):
        cursor = self.connection.cursor()
        cursor.execute("SELECT id, title, body, created_at FROM notes")
        rows = cursor.fetchall()
        return [Note(row[1], row[2], row[3]) for row in rows]

    def get_note(self, note_id):
        cursor = self.connection.cursor()
        cursor.execute("SELECT id, title, body, created_at FROM notes WHERE id =?", (note_id,))
        row = cursor.fetchone()
        if row:
            return Note(row[1], row[2], row[3])

    def update_note(self, note_id, title=None, body=None):
        cursor = self.connection.cursor()
        if title:
            cursor.execute("UPDATE notes SET title =? WHERE id =?", (title, note_id))
        if body:
            cursor.execute("UPDATE notes SET body =? WHERE id =?", (body, note_id))
        self.connection.commit()
        return self.get_note(note_id)

    def delete_note(self, note_id):
        cursor = self.connection.cursor()
        cursor.execute("DELETE FROM notes WHERE id =?", (note_id,))
        self.connection.commit()

# resources.py
from falcon import Request, Response
from models import Note
from database import Database

class NoteResource:
    def __init__(self, database):
        self.database = database

    def on_get(self, req: Request, resp: Response):
        notes = self.database.get_notes()
        resp.media = [note.to_dict() for note in notes]

    def on_post(self, req: Request, resp: Response):
        note = Note.from_dict(req.media)
        note_id = self.database.create_note(note.title, note.body)
        resp.media = {"id": note_id}

    def on_get_note(self, req: Request, resp: Response, note_id: int):
        note = self.database.get_note(note_id)
        if note:
            resp.media = note.to_dict()
        else:
            resp.status = falcon.HTTP_404

    def on_put_note(self, req: Request, resp: Response, note_id: int):
        note = Note.from_dict(req.media)
        updated_note = self.database.update_note(note_id, note.title, note.body)
        if updated_note:
            resp.media = updated_note.to_dict()
        else:
            resp.status = falcon.HTTP_404

    def on_delete_note(self, req: Request, resp: Response, note_id: int):
        self.database.delete_note(note_id)
        resp.status = falcon.HTTP_204

# config.py
DATABASE_CONNECTION_STRING = "sqlite:///notes.db"

# main.py
import falcon
from resources import NoteResource
from config import DATABASE_CONNECTION_STRING

app = falcon.API()
db = Database(DATABASE_CONNECTION_STRING)
db.connect()
note_resource = NoteResource(db)
app.add_route("/notes", note_resource)
app.add_route("/notes/{note_id}", note_resource, suffix="note")

Note that this is just a basic outline and there are many ways to structure a falcon application. Depending on your specific requirements, you may need to add additional functionality or modify the structure of the application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment