Skip to content

Commit

Permalink
fix missing to-dict
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulo Vitor Magacho authored and Paulo Vitor Magacho committed Sep 27, 2024
1 parent 6c04dbf commit b46a190
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
18 changes: 18 additions & 0 deletions xbrl/linkbase.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import abc
import json
import os
import xml.etree.ElementTree as ET
from abc import ABC
Expand Down Expand Up @@ -247,6 +248,23 @@ def __init__(self, label: str, label_type: str, language: str, text: str) -> Non
# the role of the label i.e: http://www.xbrl.org/2003/role/terseLabel
self.label_type: str = label_type

def to_dict(self):
"""
Converts the Label object into a dictionary representation
"""
return {
'label': self.label,
'label_type': self.label_type,
'language': self.language,
'text': self.text
}

def to_json(self):
"""
Converts the Label object into a JSON string
"""
return json.dumps(self.to_dict(), indent=4)

def __str__(self) -> str:
return self.text

Expand Down
24 changes: 24 additions & 0 deletions xbrl/taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import logging
import os
import json
import xml.etree.ElementTree as ET
from functools import lru_cache
from typing import List
Expand Down Expand Up @@ -480,6 +481,29 @@ def __init__(self, xml_id: str, schema_url: str, name: str) -> None:
self.period_type: str or None = None
self.balance: str or None = None
self.labels: [Label] = []

def to_dict(self):
"""
Converts the Concept object into a dictionary representation
"""
return {
'xml_id': self.xml_id,
'schema_url': self.schema_url,
'name': self.name,
'substitution_group': self.substitution_group,
'concept_type': self.concept_type,
'abstract': self.abstract,
'nillable': self.nillable,
'period_type': self.period_type,
'balance': self.balance,
'labels': [label.to_dict() for label in self.labels] if self.labels else [] # Assuming Label class has to_dict()
}

def to_json(self):
"""
Converts the Concept object into a JSON string
"""
return json.dumps(self.to_dict(), indent=4)

def __str__(self) -> str:
return self.name
Expand Down

0 comments on commit b46a190

Please sign in to comment.