Citizen.to_json() bugfixed and optimised
This commit is contained in:
parent
7cf6cf0e12
commit
66f0e648df
@ -5,7 +5,7 @@
|
|||||||
__author__ = """Eriks Karls"""
|
__author__ = """Eriks Karls"""
|
||||||
__email__ = 'eriks@72.lv'
|
__email__ = 'eriks@72.lv'
|
||||||
__version__ = '0.20.1'
|
__version__ = '0.20.1'
|
||||||
__commit_id__ = "a825917"
|
__commit_id__ = "7cf6cf0"
|
||||||
|
|
||||||
from erepublik import classes, utils
|
from erepublik import classes, utils
|
||||||
from erepublik.citizen import Citizen
|
from erepublik.citizen import Citizen
|
||||||
|
@ -427,7 +427,7 @@ class BaseCitizen(CitizenAPI):
|
|||||||
def __dict__(self):
|
def __dict__(self):
|
||||||
ret = super().__dict__.copy()
|
ret = super().__dict__.copy()
|
||||||
ret.pop('stop_threads', None)
|
ret.pop('stop_threads', None)
|
||||||
ret.pop('_Citizen__last_war_update_data', None)
|
ret.pop('_CitizenMilitary__last_war_update_data', None)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import hashlib
|
import hashlib
|
||||||
import threading
|
import threading
|
||||||
from collections import defaultdict, deque
|
from collections import defaultdict
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from typing import Any, Dict, List, NamedTuple, Tuple, Union, Optional
|
from typing import Any, Dict, List, NamedTuple, Tuple, Union, Optional
|
||||||
|
|
||||||
@ -65,11 +65,20 @@ class Holding:
|
|||||||
return dict(frm=frm, wrm=wrm)
|
return dict(frm=frm, wrm=wrm)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"Holding (#{self.id}) with {len(self.companies)} compan{'y' if len(self.companies) % 10 == 1 else 'ies'}"
|
name = f"Holding (#{self.id}) with {len(self.companies)} "
|
||||||
|
if len(self.companies) % 10 == 1:
|
||||||
|
name += "company"
|
||||||
|
else:
|
||||||
|
name += "companies"
|
||||||
|
return name
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return str(self)
|
return str(self)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def __dict__(self):
|
||||||
|
return dict(name=str(self), id=self.id, region=self.region, companies=self.companies, wam_count=self.wam_count)
|
||||||
|
|
||||||
|
|
||||||
class Company:
|
class Company:
|
||||||
holding: Holding
|
holding: Holding
|
||||||
@ -172,6 +181,13 @@ class Company:
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return str(self)
|
return str(self)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def __dict__(self):
|
||||||
|
return dict(name=str(self), holding=self.holding.id, id=self.id, quality=self.quality, is_raw=self.is_raw,
|
||||||
|
raw_usage=self.raw_usage, products_made=self.products_made, wam_enabled=self.wam_enabled,
|
||||||
|
can_wam=self.can_wam, cannot_wam_reason=self.cannot_wam_reason, industry=self.industry,
|
||||||
|
already_worked=self.already_worked, preset_works=self.preset_works)
|
||||||
|
|
||||||
|
|
||||||
class MyCompanies:
|
class MyCompanies:
|
||||||
work_units: int = 0
|
work_units: int = 0
|
||||||
@ -278,6 +294,11 @@ class MyCompanies:
|
|||||||
holding.companies.clear()
|
holding.companies.clear()
|
||||||
self.companies.clear()
|
self.companies.clear()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def __dict__(self):
|
||||||
|
return dict(name=str(self), work_units=self.work_units, next_ot_time=self.next_ot_time, ff_lockdown=self.ff_lockdown, holdings=self.holdings,
|
||||||
|
company_count=len(self.companies))
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
email = ""
|
email = ""
|
||||||
@ -576,7 +597,7 @@ class MyJSONEncoder(json.JSONEncoder):
|
|||||||
return float(f"{o:.02f}")
|
return float(f"{o:.02f}")
|
||||||
elif isinstance(o, datetime.datetime):
|
elif isinstance(o, datetime.datetime):
|
||||||
return dict(__type__='datetime', date=o.strftime("%Y-%m-%d"), time=o.strftime("%H:%M:%S"),
|
return dict(__type__='datetime', date=o.strftime("%Y-%m-%d"), time=o.strftime("%H:%M:%S"),
|
||||||
tzinfo=o.tzinfo.tzname if o.tzinfo else None)
|
tzinfo=str(o.tzinfo) if o.tzinfo else None)
|
||||||
elif isinstance(o, datetime.date):
|
elif isinstance(o, datetime.date):
|
||||||
return dict(__type__='date', date=o.strftime("%Y-%m-%d"))
|
return dict(__type__='date', date=o.strftime("%Y-%m-%d"))
|
||||||
elif isinstance(o, datetime.timedelta):
|
elif isinstance(o, datetime.timedelta):
|
||||||
@ -586,18 +607,14 @@ class MyJSONEncoder(json.JSONEncoder):
|
|||||||
return dict(headers=o.headers.__dict__, url=o.url, text=o.text)
|
return dict(headers=o.headers.__dict__, url=o.url, text=o.text)
|
||||||
elif hasattr(o, '__dict__'):
|
elif hasattr(o, '__dict__'):
|
||||||
return o.__dict__
|
return o.__dict__
|
||||||
elif isinstance(o, (deque, set)):
|
elif isinstance(o, set):
|
||||||
return list(o)
|
return list(o)
|
||||||
elif isinstance(o, Citizen):
|
elif isinstance(o, Citizen):
|
||||||
return o.to_json()
|
return o.to_json()
|
||||||
try:
|
try:
|
||||||
return super().default(o)
|
return super().default(o)
|
||||||
except TypeError as e:
|
except Exception as e: # noqa
|
||||||
name = None
|
return 'Object is not JSON serializable'
|
||||||
for ___, ____ in globals().copy().items():
|
|
||||||
if id(o) == id(____):
|
|
||||||
name = ___
|
|
||||||
return dict(__error__=str(e), __type__=str(type(o)), __name__=name)
|
|
||||||
|
|
||||||
|
|
||||||
class BattleSide:
|
class BattleSide:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user