@@ -1318,18 +1318,27 @@ def Create_External_Statistics(self):
1318
1318
if module_title not in external_statistics ["List" ]:
1319
1319
external_statistics ["List" ].append (module_title )
1320
1320
1321
+ # Add the module title to the dictionary of external statistics if it is not already present
1322
+ if module_title not in external_statistics ["Dictionary" ]:
1323
+ external_statistics ["Dictionary" ][module_title ] = {}
1324
+
1321
1325
# Get the statistic key from the module dictionary
1322
1326
statistic_key = module ["Statistic key" ]
1323
1327
1324
1328
# Add the statistic key to the list of keys if it is not already present
1325
1329
if statistic_key not in external_statistics ["Keys" ]:
1326
1330
external_statistics ["Keys" ].append (statistic_key )
1327
1331
1328
- # If the module title is inside the external statistics dictionary
1332
+ # Define the "check empty dictionary" switch
1333
+ check_empty_dictionary = True
1334
+
1335
+ # If the "check empty dictionary" switch is True
1329
1336
# And the "Years" dictionary of the external statistic is empty
1337
+ # or the "check empty dictionary" switch is False
1330
1338
if (
1331
- module ["Title" ] in external_statistics ["Dictionary" ] and
1332
- external_statistics ["Dictionary" ][module ["Title" ]]["Years" ] == {}
1339
+ check_empty_dictionary == True and
1340
+ external_statistics ["Dictionary" ][module ["Title" ]]["Years" ] == {} or
1341
+ check_empty_dictionary == False
1333
1342
):
1334
1343
# Import the module
1335
1344
module ["Module" ] = importlib .import_module ("." + module ["Title" ], module ["Title" ])
@@ -1372,7 +1381,8 @@ def Create_External_Statistics(self):
1372
1381
# Add the local statistic dictionary to the root statistics dictionary template
1373
1382
self .statistics_template [statistic_key ] = {
1374
1383
"Module" : module ["Title" ],
1375
- "Total" : 0
1384
+ "Total" : 0 ,
1385
+ "Dictionary" : {}
1376
1386
}
1377
1387
1378
1388
# Update the module dictionary inside the modules dictionary
@@ -1563,6 +1573,13 @@ def Update_Statistics(self):
1563
1573
# Get the list of keys
1564
1574
keys = list (years .keys ())
1565
1575
1576
+ # Iterate through the list of keys
1577
+ for key in keys .copy ():
1578
+ # If the year key is not inside the dictionary of years
1579
+ if key not in self .years ["Dictionary" ]:
1580
+ # Remove the key
1581
+ keys .pop (key )
1582
+
1566
1583
# Iterate through the list of keys
1567
1584
for key in keys :
1568
1585
# Get the year dictionary of that key
@@ -1576,9 +1593,11 @@ def Update_Statistics(self):
1576
1593
1577
1594
# Add the "Total" key and the number dictionaries inside the "Numbers" to the root year statistics dictionary
1578
1595
root_year ["Statistics" ][statistic_key ] = {
1579
- ** root_statistics ,
1596
+ "Module" : root_statistics [ "Module" ] ,
1580
1597
"Total" : year ["Total" ],
1581
- ** year ["Numbers" ]
1598
+ "Dictionary" : {
1599
+ ** year ["Numbers" ]
1600
+ }
1582
1601
}
1583
1602
1584
1603
# Iterate through the months inside the year dictionary
@@ -1594,9 +1613,11 @@ def Update_Statistics(self):
1594
1613
1595
1614
# Add the "Total" key and the number dictionaries inside the "Numbers" to the root month statistics dictionary
1596
1615
root_year ["Months" ][month_key ]["Statistics" ][statistic_key ] = {
1597
- ** root_statistics ,
1616
+ "Module" : root_statistics [ "Module" ] ,
1598
1617
"Total" : month ["Total" ],
1599
- ** month ["Numbers" ]
1618
+ "Dictionary" : {
1619
+ ** month ["Numbers" ]
1620
+ }
1600
1621
}
1601
1622
1602
1623
# Add the year dictionary to the local dictionary of years
@@ -1660,13 +1681,168 @@ def Update_Statistics(self):
1660
1681
# Update the "External statistics" JSON file with the "External statistics" dictionary
1661
1682
self .JSON .Edit (self .diary_slim ["Folders" ]["Data" ]["External statistics" ]["Statistics" ], self .statistics ["External statistics" ])
1662
1683
1663
- def Update_Current_Year_Statistics (self , year_statistics , month_statistics ):
1684
+ def Update_Current_Year_Statistics (self , year_statistics = None , month_statistics = None ):
1664
1685
# Write the year statistics dictionary into the year statistics file
1665
1686
self .JSON .Edit (self .diary_slim ["Current year" ]["Folders" ]["Statistics" ], year_statistics )
1666
1687
1667
1688
# Write the month statistics dictionary into the month statistics file
1668
1689
self .JSON .Edit (self .diary_slim ["Current year" ]["Month" ]["Folders" ]["Statistics" ], month_statistics )
1669
1690
1691
+ def Update_External_Statistics (self , statistic_key , statistics ):
1692
+ # Define the list of parameters (statistic dictionaries to update)
1693
+ parameters = [
1694
+ self .diary_slim ["Current year" ]["Statistics" ],
1695
+ self .diary_slim ["Current year" ]["Month" ]["Statistics" ]
1696
+ ]
1697
+
1698
+ # Define the root statistics text
1699
+ text = ""
1700
+
1701
+ # ---------- #
1702
+
1703
+ # Get the current year statistics
1704
+ year_statistics = statistics ["Year" ]
1705
+
1706
+ # Update the statistic key inside the root year statistics dictionary
1707
+ parameters [0 ][statistic_key ] = year_statistics
1708
+
1709
+ # Get the year statistics text
1710
+ text += self .Show_Statistics ("Year" , year_statistics , statistics ["Dictionary" ], return_text = True ) + "\n "
1711
+
1712
+ # ---------- #
1713
+
1714
+ # Get the current month statistics
1715
+ month_statistics = statistics ["Month" ]
1716
+
1717
+ # Update the statistic key inside the root month statistics dictionary
1718
+ parameters [1 ][statistic_key ] = month_statistics
1719
+
1720
+ # Get the year statistics text
1721
+ text += self .Show_Statistics ("Month" , month_statistics , statistics ["Dictionary" ], return_text = True )
1722
+
1723
+ # ---------- #
1724
+
1725
+ # Update the statistics of the current year, with the two parameters
1726
+ self .Update_Current_Year_Statistics (* parameters )
1727
+
1728
+ # Return the statistics text
1729
+ return text
1730
+
1731
+ def Show_Statistics (self , date_type , statistics , dictionary = None , return_text = False ):
1732
+ # Define the text key
1733
+ text_key = date_type .lower ()
1734
+
1735
+ # If the "Number" key is inside the statistics dictionary
1736
+ if "Number" in statistics :
1737
+ # Get the number of updated statistics for the month/year
1738
+ new_number = statistics ["Number" ]
1739
+
1740
+ # If the "dictionary" parameter is not None
1741
+ if dictionary != None :
1742
+ # Get the old and new numbers from the numbers dictionary (inside the date type dictionary)
1743
+ old_number = dictionary ["Numbers" ][date_type ]["Old" ]
1744
+ new_number = 1
1745
+
1746
+ # Define the statistic text to show as singular or plural depending on the number
1747
+ singular = self .Language .language_texts ["updated_" + text_key + "_statistic" ]
1748
+ plural = self .Language .language_texts ["updated_" + text_key + "_statistics" ]
1749
+
1750
+ show_text = self .Text .By_Number (new_number , singular , plural )
1751
+
1752
+ # If the "dictionary" parameter is not None
1753
+ if dictionary != None :
1754
+ # Get the new number from the numbers dictionary (inside the date type dictionary)
1755
+ new_number = dictionary ["Numbers" ][date_type ]["New" ]
1756
+
1757
+ # Define the root statistics text
1758
+ statistics_text = "\n " + \
1759
+ show_text + ":" + "\n "
1760
+
1761
+ # If the "return text" parameter is False
1762
+ if return_text == False :
1763
+ # Show the statistics text
1764
+ print (statistics_text )
1765
+
1766
+ # Define the default "in text" as "in [year]"
1767
+ in_text = self .Language .language_texts ["in" ] + " " + str (self .diary_slim ["Current year" ]["Number" ])
1768
+
1769
+ # If the date type is "Month"
1770
+ if date_type == "Month" :
1771
+ # Re-define the in text to be "in [month name]"
1772
+ in_text = self .Language .language_texts ["in" ] + " " + str (self .diary_slim ["Current year" ]["Month" ]["Name text" ])
1773
+
1774
+ # If the "dictionary" parameter is not None
1775
+ if dictionary != None :
1776
+ # Get the first key of the statistics dictionary
1777
+ first_key = list (statistics ["Dictionary" ].keys ())[0 ]
1778
+
1779
+ # Make a copy of the statistics dictionary
1780
+ statistics = deepcopy (statistics )
1781
+
1782
+ # Make the dictionary only have the first key
1783
+ statistics ["Dictionary" ] = {
1784
+ first_key : statistics ["Dictionary" ][first_key ]
1785
+ }
1786
+
1787
+ # Iterate through the dictionary of statistics
1788
+ for statistic in statistics ["Dictionary" ].values ():
1789
+ # If the statistic is a dictionary
1790
+ if isinstance (statistic , dict ):
1791
+ # If the "Old number" key is inside the statistic dictionary
1792
+ if "Old number" in statistic :
1793
+ # Get the old number from it
1794
+ old_number = statistic ["Old number" ]
1795
+
1796
+ # If the "Number" key is inside the statistic dictionary
1797
+ if "Number" in statistic :
1798
+ # Get the new number from it
1799
+ new_number = statistic ["Number" ]
1800
+
1801
+ # If the "Text" key is inside the statistic dictionary
1802
+ if "Text" in statistic :
1803
+ text = statistic ["Text" ]
1804
+
1805
+ # If the "dictionary" parameter is not None
1806
+ # And there is a "Text" key inside it
1807
+ if (
1808
+ dictionary != None and
1809
+ "Text" in dictionary
1810
+ ):
1811
+ # Get the text from it
1812
+ text = dictionary ["Text" ]
1813
+
1814
+ # Add the in text text and a colon
1815
+ text = "\t " + text + " " + in_text + ": "
1816
+
1817
+ # Define the number
1818
+ number = str (new_number )
1819
+
1820
+ # If the statistic is a dictionary
1821
+ # And the "Money" key is inside the statistic dictionary
1822
+ if (
1823
+ isinstance (statistic , dict ) and
1824
+ "Money" in statistic
1825
+ ):
1826
+ # Define the money text
1827
+ number = self .Define_Money_Text (number )
1828
+
1829
+ # Add the current number of the statistic
1830
+ text += number
1831
+
1832
+ # Add the old number with the "before" text
1833
+ text += " (" + self .Language .language_texts ["before, title()" ].lower () + ": " + str (old_number ) + ")"
1834
+
1835
+ # Add the text to the root statistics text
1836
+ statistics_text += text
1837
+
1838
+ # If the "return text" parameter is False
1839
+ if return_text == False :
1840
+ # Show the text and number with a tab
1841
+ print (text )
1842
+
1843
+ # Return the statistics text
1844
+ return statistics_text
1845
+
1670
1846
def Next_State (self , dictionary ):
1671
1847
# Define the states variable for easier typing
1672
1848
states = dictionary ["States" ]
0 commit comments