from pyramid.view import view_config

from lxml import etree

@view_config(route_name='index', renderer='index.mako')
def my_view(request):
    smsfile = "sms.xml"
    with open(smsfile) as f:
        tree = etree.parse(f)

    root = tree.getroot()

    return {"messages": sorted([e for e in root if e.get("contact_name") == "Lacey Shankle"], key=lambda message: message.get("date"))}
    return {'project': 'smsviewer'}

from pyramid.view import view_config

from lxml import etree

@view_config(route_name='index', renderer='index.mako')
def my_view(request):
    smsfile = "sms.xml"
    with open(smsfile) as f:
        tree = etree.parse(f)

    root = tree.getroot()

    els = root.xpath("*[@contact_name='Lacey Shankle']")

    return {"messages": sorted(els, key=lambda message: message.get("date"))}

Use xpath to find messages instead of a loop
Use xpath to find messages instead of a loop

  from pyramid.view import view_config
  
  from lxml import etree
  
  @view_config(route_name='index', renderer='index.mako')
  def my_view(request):
      smsfile = "sms.xml"
      with open(smsfile) as f:
          tree = etree.parse(f)
  
      root = tree.getroot()
  
+     els = root.xpath("*[@contact_name='Lacey Shankle']")
-     return {"messages": sorted([e for e in root if e.get("contact_name") == "Lacey Shankle"], key=lambda message: message.get("date"))}
-     return {'project': 'smsviewer'}
  
+     return {"messages": sorted(els, key=lambda message: message.get("date"))}
+ 
Use xpath to find messages instead of a loop
## Code Before:
from pyramid.view import view_config

from lxml import etree

@view_config(route_name='index', renderer='index.mako')
def my_view(request):
    smsfile = "sms.xml"
    with open(smsfile) as f:
        tree = etree.parse(f)

    root = tree.getroot()

    return {"messages": sorted([e for e in root if e.get("contact_name") == "Lacey Shankle"], key=lambda message: message.get("date"))}
    return {'project': 'smsviewer'}

## Instruction:
Use xpath to find messages instead of a loop
## Code After:
from pyramid.view import view_config

from lxml import etree

@view_config(route_name='index', renderer='index.mako')
def my_view(request):
    smsfile = "sms.xml"
    with open(smsfile) as f:
        tree = etree.parse(f)

    root = tree.getroot()

    els = root.xpath("*[@contact_name='Lacey Shankle']")

    return {"messages": sorted(els, key=lambda message: message.get("date"))}
