Skip to content

Commit

Permalink
new dict methods in session file&memory
Browse files Browse the repository at this point in the history
  • Loading branch information
manatlan committed Sep 26, 2023
1 parent 3a4b2e2 commit dd2b3ca
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 43 deletions.
15 changes: 15 additions & 0 deletions htagweb/sessions/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def __init__(self,uid:str,persistent:bool):
else:
self._d={}

def __len__(self):
return len(self._d.keys())

def __contains__(self,key):
return key in self._d.keys()

def items(self):
return self._d.items()

Expand All @@ -35,13 +41,22 @@ def get(self,k:str,default=None):
def __getitem__(self,k:str):
return self._d[k]

def __delitem__(self,k:str):
""" save session """
del self._d[k]

with open(self._file,"wb+") as fid:
pickle.dump(self._d,fid, protocol=4)

def __setitem__(self,k:str,v):
""" save session """
self._d[k]=v

with open(self._file,"wb+") as fid:
pickle.dump(self._d,fid, protocol=4)

def clear(self):
""" save session """
self._d.clear()
if os.path.isfile(self._file):
os.unlink(self._file)
Expand Down
12 changes: 12 additions & 0 deletions htagweb/sessions/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def __init__(self,uid,dico:dict):
self._uid=uid
self._dico=dico

def __len__(self):
return len(self._dico.keys())

def __contains__(self,key):
return key in self._dico.keys()

def items(self):
return self._dico.items()

Expand All @@ -27,6 +33,10 @@ def __setitem__(self,k:str,v): # could be inplemented in SessionMem
self._dico[k]=v
PX.clientsync.set( self._uid, self._dico)

def __delitem__(self,k:str):
del self._dico[k]
PX.clientsync.set( self._uid, self._dico)

def clear(self):
self._dico.clear()
PX.clientsync.set( self._uid, {})
Expand All @@ -45,6 +55,8 @@ def get(self,uid:str) -> ServerMemDict:
def set(self,uid:str,dico):
self.SESSIONS[uid] = dico



PX=Usot( SessionMemory, port=19999 )

def startServer():
Expand Down
74 changes: 37 additions & 37 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ classifiers = [
[tool.poetry.dependencies]
# https://python-poetry.org/docs/dependency-specification/
python = "^3.7"
htag = ">= 0.9.44"
htag = ">= 0.41.0"
starlette = ">= 0.21.0"
pycryptodomex = ">= 3.0.0"
uvicorn = {version = ">=0.22,<1.0", extras = ["standard"]}
Expand Down
10 changes: 5 additions & 5 deletions test_hr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class App(Tag.body):
def init(self):
self.b=Tag.Button("say hello",_onclick=self.bind.doit() )
self+=self.b
self+=Tag.cpt(self.state.get('cpt',0))
self.state['created']=True
self+=Tag.cpt(self.session.get('cpt',0))
self.session['created']=True
def doit(self):
self.state['cpt']=int(self.state.get('cpt',0))+1
self.state['interacted']=True
self+="hello" +Tag.cpt2(self.state['cpt'])
self.session['cpt']=int(self.session.get('cpt',0))+1
self.session['interacted']=True
self+="hello" +Tag.cpt2(self.session['cpt'])

@pytest.mark.asyncio
async def test_hr():
Expand Down

0 comments on commit dd2b3ca

Please sign in to comment.