Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
import datetime

def getData(phase,room,month):
def getDataObject(phase,room,month):

regexRowUsage = '<TR><TD>(.*)</TD><TD>(.*)</TD><TD ALIGN="RIGHT">(.*)</TD><TD ALIGN="RIGHT">(.*)</TD></TR>'
regexUsage = '<TR><TD COLSPAN="3"><B>Total combin&eacute;:</B></TD><TD ALIGN="RIGHT">(.*)</TD></TR>'
Expand Down Expand Up @@ -56,5 +56,9 @@ def getData(phase,room,month):
data['left']=left
data['maximum']=maximum

dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None
return json.dumps(data,default=dthandler)
return data

def getData(phase,room,month):
data = getDataObject(phase,room,month)
dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None
return json.dumps(data,default=dthandler)
16 changes: 9 additions & 7 deletions netusage.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ def getUsage(type,phase,room):
room must be an existing room in the block
"""

return api.getData(phase,room,datetime.now().month)

data = api.getDataObject(phase,room,datetime.now().month)
pct = (data["maximum"]-data["left"])*100/data["maximum"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'était juste pas là ça ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

En fait, j'ai séparé la méthode qui va chercher la data et la méthode qui transforme la data en json dans l'api. Celle qui retourne le JSON a garder le même nom. Celle qui retourne le dictionnaire Python s'appelle getDataObject. J'utilise ce dictionnaire pour les print, parce que quand j'utilisais le script original j'avais ben des erreurs de variable non défini.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d'après moi tu pourrais faire data = api.getData(...) et garder la transformation de json là quand même... et faire pct = pct = (data["maximum"]-data["left"])*100/data["maximum"]) ici quand même, non? Tu peux accéder au data du json, pas besoin de traîner le dict

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai essayé et il me donne une erreur. Puisque que json est considérer comme une string. Donc, Si je ne me trompe pas il faudrait quand même parser le json en object quelconque ce qui demande un certain nombre de calcul.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine, mon python est rusty. :)

if type == "percent":
return "{:0.2f}%".format(pct)
if type == "left":
return "{:0.2f}GB".format(left)
if type =="usage":
return "{:0.2f}GB".format(usage/1024)
if type =="all":
return "Used :\t\t{:0.2f}GB ({:0.2f}%)\nLeft :\t\t{:0.2f}GB ({:0.2f}%)\nTotal :\t\t{:0.2f}GB".format(usage/1024,pct,left,100-pct,max/1024)
return "{:0.2f}GB".format(data["left"])
if type == "usage":
return "{:0.2f}GB".format(data["usage"])
if type == "json":
return api.getData(phase,room,datetime.now().month)
if type =="all":
return "Used :\t\t{:0.2f}GB ({:0.2f}%)\nLeft :\t\t{:0.2f}GB ({:0.2f}%)\nTotal :\t\t{:0.2f}GB".format(data["usage"],pct,data["left"],100-pct,data["maximum"])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cooptel les donne en GB maintenant?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'imagine parce que j'ai pas changer l'interprétation des données dans le fichier api.py.

raise Exception('Must choose between "percent" and "left" ')


Expand Down