View this page on GitHub

Setting Up Python Environment


  • Use a Python 3.11 environment - later code samples will not support 3.13
  # navigate to your project folder
cd your-folder
# set up a Python 3.11 virtual environment in the `venvs` folder in your home folder: `~/venvs/py311env`
/opt/homebrew/opt/[email protected]/bin/python3.11 -m venv ~/venvs/py311env

# activate the virtual environment for this session - so python modules will be installed here (for the project)
source ~/venvs/py311env/bin/activate

# install Python packages
pip install -r requirements.txt
  

From the repo readme. Start the web application

  uvicorn main:app --reload --port 8000
  

Some Helpful Python Syntax

Including a Line Number in a Print Statement

  print(f"ERROR: [LINE>{inspect.currentframe().f_lineno}] an error occurred; see: {str(e)}")
  

Open files for reading and writing

  with open("pg84-images.html", encoding="utf-8") as f:
    file_text = f.read()

with open("output.txt", "w", encoding="utf-8") as f:
    f.write(my_book_json)
  

Table for Week 1 FastAPI App

  CREATE TABLE resumes (
  resume_id BIGSERIAL PRIMARY KEY,
  resume JSONB NOT NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  
  -- Handy generated columns (optional but great for filtering)
  name TEXT GENERATED ALWAYS AS (resume->>'name') STORED,
  location TEXT GENERATED ALWAYS AS (resume->'contact_information'->>'location') STORED,
  
  -- Minimal structural checks (tune to your tolerance)
  CONSTRAINT resume_has_name CHECK (
    resume ? 'name' AND jsonb_typeof(resume->'name') = 'string'
  ),
  CONSTRAINT resume_has_prof_summary CHECK (
    resume ? 'professional_summary' AND jsonb_typeof(resume->'professional_summary') = 'string'
  ),
  CONSTRAINT resume_arrays_types_ok CHECK (
    (NOT (resume ? 'work_experience') OR jsonb_typeof(resume->'work_experience') = 'array') AND
    (NOT (resume ? 'education') OR jsonb_typeof(resume->'education') = 'array') AND
    (NOT (resume ? 'skills') OR jsonb_typeof(resume->'skills') = 'array') AND
    (NOT (resume ? 'certifications') OR jsonb_typeof(resume->'certifications') = 'array') AND
    (NOT (resume ? 'projects') OR jsonb_typeof(resume->'projects') = 'array')
  )
);
  

View this page on GitHub