session3.DictSession module
A session object that also acts like a dictionary.
""" A session object that also acts like a dictionary. """ #from UserDict import UserDict from collections import UserDict from session3.Session import Session class DictSession(UserDict, Session): """ A session object that also acts like a dictionary. Unlike some object/dict hybrids, keys and attributes are distinct and not interchangeable. Beware of assigning attributes that override dict methods. """ def __init__(self, id): Session.__init__(self, id) UserDict.__init__(self)
Classes
class DictSession
A session object that also acts like a dictionary.
Unlike some object/dict hybrids, keys and attributes are distinct and not interchangeable. Beware of assigning attributes that override dict methods.
class DictSession(UserDict, Session): """ A session object that also acts like a dictionary. Unlike some object/dict hybrids, keys and attributes are distinct and not interchangeable. Beware of assigning attributes that override dict methods. """ def __init__(self, id): Session.__init__(self, id) UserDict.__init__(self)
Ancestors (in MRO)
- DictSession
- collections.UserDict
- collections.abc.MutableMapping
- collections.abc.Mapping
- collections.abc.Sized
- collections.abc.Iterable
- collections.abc.Container
- session3.Session.Session
- builtins.object
Class variables
var MAX_FORM_TOKENS
Static methods
def __init__(
self, id)
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, id): Session.__init__(self, id) UserDict.__init__(self)
def clear(
self)
D.clear() -> None. Remove all items from D.
def clear(self): 'D.clear() -> None. Remove all items from D.' try: while True: self.popitem() except KeyError: pass
def copy(
self)
def copy(self): if self.__class__ is UserDict: return UserDict(self.data.copy()) import copy data = self.data try: self.data = {} c = copy.copy(self) finally: self.data = data c.update(self) return c
def create_form_token(
self)
() -> string
Create a new form token and add it to a queue of outstanding form tokens for this session. A maximum of MAX_FORM_TOKENS are saved. The new token is returned.
def create_form_token(self): """() -> string Create a new form token and add it to a queue of outstanding form tokens for this session. A maximum of MAX_FORM_TOKENS are saved. The new token is returned. """ token = randbytes(16) self._form_tokens.append(token) extra = len(self._form_tokens) - self.MAX_FORM_TOKENS if extra > 0: del self._form_tokens[:extra] return token
def dump(
self, file=None, header=True, deep=True)
def dump(self, file=None, header=True, deep=True): time_fmt = "%Y-%m-%d %H:%M:%S" ctime = strftime(time_fmt, localtime(self._creation_time)) atime = strftime(time_fmt, localtime(self._access_time)) if header: file.write('session %s:' % self.id) file.write(' user %s' % self.user) file.write(' _remote_address: %s' % self._remote_address) file.write(' created %s, last accessed %s' % (ctime, atime)) file.write(' _form_tokens: %s\n' % self._form_tokens)
def get(
self, key, default=None)
D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
def get(self, key, default=None): 'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.' try: return self[key] except KeyError: return default
def get_access_age(
self, _now=None)
Return the number of seconds since session was last accessed.
def get_access_age(self, _now=None): """Return the number of seconds since session was last accessed.""" # _now arg is for SessionManager's use return (_now or time()) - self._access_time
def get_access_time(
self)
Return the time that this session was last accessed (seconds since epoch).
def get_access_time(self): """Return the time that this session was last accessed (seconds since epoch). """ return self._access_time
def get_creation_age(
self, _now=None)
Return the number of seconds since session was created.
def get_creation_age(self, _now=None): """Return the number of seconds since session was created.""" # _now arg is not strictly necessary, but there for consistency # with get_access_age() return (_now or time()) - self._creation_time
def get_creation_time(
self)
Return the time that this session was created (seconds since epoch).
def get_creation_time(self): """Return the time that this session was created (seconds since epoch). """ return self._creation_time
def get_remote_address(
self)
Return the IP address (dotted-quad string) that made the initial request in this session.
def get_remote_address(self): """Return the IP address (dotted-quad string) that made the initial request in this session. """ return self._remote_address
def get_user(
self)
def get_user(self): return self.user
def has_form_token(
self, token)
(token : string) -> boolean
Return true if 'token' is in the queue of outstanding tokens.
def has_form_token(self, token): """(token : string) -> boolean Return true if 'token' is in the queue of outstanding tokens. """ return token in self._form_tokens
def has_info(
self)
() -> boolean
Return true if this session contains any information that must be saved.
def has_info(self): """() -> boolean Return true if this session contains any information that must be saved. """ return self.user
def items(
self)
D.items() -> a set-like object providing a view on D's items
def items(self): "D.items() -> a set-like object providing a view on D's items" return ItemsView(self)
def keys(
self)
D.keys() -> a set-like object providing a view on D's keys
def keys(self): "D.keys() -> a set-like object providing a view on D's keys" return KeysView(self)
def pop(
self, key, default=<object object at 0x7fb1c851f050>)
D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised.
def pop(self, key, default=__marker): '''D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised. ''' try: value = self[key] except KeyError: if default is self.__marker: raise return default else: del self[key] return value
def popitem(
self)
D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.
def popitem(self): '''D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty. ''' try: key = next(iter(self)) except StopIteration: raise KeyError value = self[key] del self[key] return key, value
def remove_form_token(
self, token)
(token : string)
Remove 'token' from the queue of outstanding tokens.
def remove_form_token(self, token): """(token : string) Remove 'token' from the queue of outstanding tokens. """ self._form_tokens.remove(token)
def set_user(
self, user)
def set_user(self, user): self.user = user
def setdefault(
self, key, default=None)
D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
def setdefault(self, key, default=None): 'D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D' try: return self[key] except KeyError: self[key] = default return default
def start_request(
self)
Called near the beginning of each request: after the HTTPRequest object has been built, but before we traverse the URL or call the callable object found by URL traversal.
def start_request(self): """ Called near the beginning of each request: after the HTTPRequest object has been built, but before we traverse the URL or call the callable object found by URL traversal. """ pass
def update(
*args, **kwds)
D.update([E, ]**F) -> None. Update D from mapping/iterable E and F. If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
def update(*args, **kwds): ''' D.update([E, ]**F) -> None. Update D from mapping/iterable E and F. If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v ''' if not args: raise TypeError("descriptor 'update' of 'MutableMapping' object " "needs an argument") self, *args = args if len(args) > 1: raise TypeError('update expected at most 1 arguments, got %d' % len(args)) if args: other = args[0] if isinstance(other, Mapping): for key in other: self[key] = other[key] elif hasattr(other, "keys"): for key in other.keys(): self[key] = other[key] else: for key, value in other: self[key] = value for key, value in kwds.items(): self[key] = value
def values(
self)
D.values() -> an object providing a view on D's values
def values(self): "D.values() -> an object providing a view on D's values" return ValuesView(self)
Methods
def fromkeys(
cls, iterable, value=None)
@classmethod def fromkeys(cls, iterable, value=None): d = cls() for key in iterable: d[key] = value return d