<commit_before>"""
Vishnu: A session library for the python runtime of Google App Engine.
"""
from __future__ import absolute_import

import threading

_thread_local = threading.local() # pylint: disable=C0103
_thread_local.session = None

def get_session():
    """Returns the session for the current request"""
    return _thread_local.session

def delete_expired_sessions(limit=500):
    """Deletes expired sessions
       A session is expired if it expires date is set and has passed or
       if it has not been accessed for a given period of time."""
    from .session import VishnuSession
    from google.appengine.ext import ndb
    from datetime import datetime

    now = datetime.now()

    query = VishnuSession.query(ndb.AND(VishnuSession.expires <= now, VishnuSession.expires!=None))
    results = query.fetch(keys_only=True, limit=limit)

    ndb.delete_multi(results)<commit_msg>finishes session cleanup #10<commit_after>"""
Vishnu: A session library for the python runtime of Google App Engine.
"""
from __future__ import absolute_import

import threading

_thread_local = threading.local() # pylint: disable=C0103
_thread_local.session = None

def get_session():
    """Returns the session for the current request"""
    return _thread_local.session

def delete_expired_sessions(dormant_for=86400, limit=500):
    """Deletes expired sessions
       A session is expired if it expires date is set and has passed or
       if it has not been accessed for a given period of time.
       max_age: seconds since last access to delete sessions, defaults
       to 24 hours.
       limit: amount to delete in one call of the method, the maximum
       and default for this is the NDB fetch limit of 500"""
    from .session import VishnuSession
    from google.appengine.ext import ndb
    from datetime import datetime
    from datetime import timedelta

    now = datetime.now()
    last_accessed = now - timedelta(seconds=dormant_for)

    query = VishnuSession.query(ndb.OR(
        ndb.AND(VishnuSession.expires <= now, VishnuSession.expires!=None),
        VishnuSession.last_accessed <= last_accessed
    ))
    results = query.fetch(keys_only=True, limit=limit)

    ndb.delete_multi(results)
{'fraction_non_alphanumeric': 0.0741745816372682, 'fraction_numerical': 0.011759384893713252, 'mean_word_length': 3.8296943231441047, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '68393', 'n_tokens_mistral': 661, 'n_tokens_neox': 624, 'n_words': 226}