Josif_tepe

Untitled

Aug 7th, 2025
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.36 KB | None | 0 0
  1. import csv
  2. import unittest
  3. import warnings
  4. import os
  5.  
  6.  
  7.  
  8. def preberi_tezo(godina):
  9.     d = {}
  10.     file_path = os.path.join("podatki", f"Banane {godina}.csv")
  11.     is_first_row = True
  12.     with (open(file_path, "r") as f):
  13.         c = csv.reader(f)
  14.  
  15.         for row in c:
  16.             if is_first_row:
  17.                 is_first_row = False
  18.                 continue
  19.  
  20.             drzava = row[0]
  21.             quantity = row[7]
  22.  
  23.             if 'European Union' in drzava:
  24.                 continue
  25.  
  26.             try:
  27.                 d[drzava] = int(quantity)
  28.             except ValueError:
  29.                 pass
  30.     return d
  31.  
  32.  
  33. def preberi_vrednost(godina):
  34.     d = {}
  35.     file_path = os.path.join("podatki", f"Banane {godina}.csv")
  36.     is_first_row = True
  37.     with (open(file_path, "r") as f):
  38.         c = csv.reader(f)
  39.  
  40.         for row in c:
  41.             if is_first_row:
  42.                 is_first_row = False
  43.                 continue
  44.  
  45.             drzava = row[0]
  46.             usd = row[6]
  47.  
  48.             if 'European Union' in drzava:
  49.                 continue
  50.             try:
  51.                 d[drzava] = float(usd) *  1000.0
  52.             except ValueError:
  53.                 pass
  54.     return d
  55. class TestBase(unittest.TestCase):
  56.     def setUp(self):
  57.         warnings.simplefilter("ignore", ResourceWarning)
  58.         self.maxDiff = None
  59.  
  60.     def assertAlmostEqualSeq(self, a, b):
  61.         self.assertEqual(len(a), len(b))
  62.         self.assertIs(type(a), type(b))
  63.         for x, y in zip(a, b):
  64.             self.assertAlmostEqual(x, y)
  65.  
  66. class Test06(TestBase):
  67.     def test_01_preberi(self):
  68.         self.assertEqual({'Algeria': 12170700,
  69.  'Australia': 119374,
  70.  'Belize': 78,
  71.  'Brazil': 55500,
  72.  'Brunei': 845798,
  73.  'Canada': 379614000,
  74.  'Chile': 84684100,
  75.  'China': 20474700,
  76.  'Colombia': 11564000,
  77.  'Croatia': 24997100,
  78.  'Cyprus': 1274570,
  79.  'Denmark': 57819500,
  80.  'Finland': 86144900,
  81.  'Germany': 1378870000,
  82.  'Greece': 42376800,
  83.  'Hungary': 79375000,
  84.  'Iceland': 3265010,
  85.  'Indonesia': 4673,
  86.  'Ireland': 54569500,
  87.  'Japan': 777477000,
  88.  'Kenya': 200398,
  89.  'Korea, Rep.': 171203000,
  90.  'Macao': 1556110,
  91.  'Malaysia': 53326,
  92.  'Mexico': 88531,
  93.  'Netherlands': 201007000,
  94.  'New Zealand': 61170600,
  95.  'Oman': 1211080,
  96.  'Paraguay': 6225860,
  97.  'Peru': 19,
  98.  'Portugal': 97218000,
  99.  'Romania': 18805000,
  100.  'Saudi Arabia': 143044000,
  101.  'Serbia, FR(Serbia/Montenegro)': 22518000,
  102.  'Singapore': 35854100,
  103.  'South Africa': 9074,
  104.  'Spain': 227548,
  105.  'Sri Lanka': 19,
  106.  'St. Lucia': 200,
  107.  'Sweden': 161817000,
  108.  'Switzerland': 78781300,
  109.  'Trinidad and Tobago': 1636350,
  110.  'Tunisia': 6075490,
  111.  'Turkey': 111323000,
  112.  'United Arab Emirates': 1096850,
  113.  'United States': 3693120000}, preberi_tezo(1992))
  114.  
  115.         self.assertEqual({
  116.             'Albania': 20438800,
  117.             'Algeria': 274051000,
  118.             'Andorra': 266709,
  119.             'Angola': 430,
  120.             'Antigua and Barbuda': 1107800,
  121.             'Argentina': 396310000,
  122.             'Armenia': 9875280,
  123.             'Aruba': 3217410,
  124.             'Australia': 307153,
  125.             'Austria': 121018000,
  126.             'Azerbaijan': 8954400,
  127.             'Bahamas, The': 525098,
  128.             'Bahrain': 12623700,
  129.             'Bangladesh': 317,
  130.             'Barbados': 3833010,
  131.             'Belarus': 74475000,
  132.             'Belgium': 763946000,
  133.             'Benin': 826698,
  134.             'Bermuda': 803164,
  135.             'Bolivia': 43682,
  136.             'Bosnia and Herzegovina': 36794300,
  137.             'Botswana': 7766370,
  138.             'Brazil': 4718,
  139.             'Brunei': 3301440,
  140.             'Bulgaria': 38975100,
  141.             'Burkina Faso': 5084580,
  142.             'Burundi': 12588,
  143.             'Canada': 557569000,
  144.             'Cape Verde': 177,
  145.             'China': 514784000,
  146.             'Colombia': 21095300,
  147.             'Comoros': 8900,
  148.             'Congo, Rep.': 88,
  149.             'Costa Rica': 1843360,
  150.             "Cote d'Ivoire": 24,
  151.             'Croatia': 4016130000,
  152.             'Cyprus': 4192850,
  153.             'Czech Republic': 136863000,
  154.             'Denmark': 94491200,
  155.             'Dominican Republic': 161526,
  156.             'Ecuador': 5594210,
  157.             'Egypt, Arab Rep.': 1791330000,
  158.             'El Salvador': 120787000,
  159.             'Estonia': 12231600,
  160.             'Eswatini': 6560520,
  161.             'Fiji': 241,
  162.             'Finland': 77730300,
  163.             'France': 661362000,
  164.             'French Polynesia': 832,
  165.             'Gabon': 2940,
  166.             'Gambia, The': 115011,
  167.             'Georgia': 15231300,
  168.             'Germany': 1365950000,
  169.             'Ghana': 154159,
  170.             'Greece': 140434000,
  171.             'Greenland': 54698,
  172.             'Grenada': 278,
  173.             'Guatemala': 12054800,
  174.             'Guyana': 306,
  175.             'Hong Kong, China': 65067100,
  176.             'Hungary': 56984000,
  177.             'Iceland': 6159340,
  178.             'Indonesia': 336799,
  179.             'Iran, Islamic Rep.': 391635000,
  180.             'Ireland': 87096000,
  181.             'Israel': 8560,
  182.             'Italy': 675030000,
  183.             'Japan': 975394000,
  184.             'Jordan': 42911000,
  185.             'Kazakhstan': 48175900,
  186.             'Kenya': 194185,
  187.             'Kiribati': 1,
  188.             'Korea, Rep.': 313604000,
  189.             'Kuwait': 134063000,
  190.             'Kyrgyz Republic': 13545100,
  191.             'Latvia': 23477800,
  192.             'Lebanon': 251538,
  193.             'Lesotho': 384687,
  194.             'Lithuania': 34179600,
  195.             'Luxembourg': 4963190,
  196.             'Macao': 3575530,
  197.             'Malawi': 60004,
  198.             'Malaysia': 16213400,
  199.             'Maldives': 938743,
  200.             'Malta': 6405490,
  201.             'Mauritania': 3562340,
  202.             'Mexico': 166435,
  203.             'Micronesia, Fed. Sts.': 640,
  204.             'Moldova': 10800300,
  205.             'Mongolia': 2155190,
  206.             'Montenegro': 9339400,
  207.             'Montserrat': 615,
  208.             'Morocco': 22467900,
  209.             'Mozambique': 9178,
  210.             'Myanmar': 11303,
  211.             'Namibia': 4675770,
  212.             'Nepal': 7701090,
  213.             'Netherlands': 297990000,
  214.             'New Caledonia': 1644,
  215.             'New Zealand': 80278000,
  216.             'Nicaragua': 8118410,
  217.             'Niger': 4054410,
  218.             'Nigeria': 2376620,
  219.             'North Macedonia': 18481900,
  220.             'Norway': 81334500,
  221.             'Occ.Pal.Terr': 17475500,
  222.             'Oman': 19060300,
  223.             'Pakistan': 51772,
  224.             'Palau': 81,
  225.             'Paraguay': 3000,
  226.             'Peru': 102,
  227.             'Poland': 299034000,
  228.             'Portugal': 141319000,
  229.             'Qatar': 25752500,
  230.             'Romania': 90116600,
  231.             'Russian Federation': 1339140000,
  232.             'Rwanda': 1419420,
  233.             'Sao Tome and Principe': 15,
  234.             'Saudi Arabia': 306141000,
  235.             'Senegal': 17065500,
  236.             'Serbia, FR(Serbia/Montenegro)': 48299100,
  237.             'Seychelles': 1008,
  238.             'Singapore': 47683500,
  239.             'Slovak Republic': 54619700,
  240.             'Slovenia': 46749400,
  241.             'Solomon Islands': 4,
  242.             'South Africa': 96367200,
  243.             'Spain': 253163000,
  244.             'Sri Lanka': 196,
  245.             'St. Kitts and Nevis': 17451,
  246.             'St. Lucia': 100,
  247.             'Sudan': 27610,
  248.             'Sweden': 181651000,
  249.             'Switzerland': 82901000,
  250.             'Tanzania': 986,
  251.             'Thailand': 15267700,
  252.             'Tonga': 1,
  253.             'Trinidad and Tobago': 9039110,
  254.             'Tunisia': 17553500,
  255.             'Turkey': 534202000,
  256.             'Uganda': 183170,
  257.             'Ukraine': 265648000,
  258.             'United Arab Emirates': 142501000,
  259.             'United Kingdom': 1169560000,
  260.             'United States': 4868130000,
  261.             'Uruguay': 45742600,
  262.             'Vietnam': 152365,
  263.             'Zambia': 828014}, preberi_tezo(2013))
  264.  
  265.         self.assertEqual(
  266.             {'Argentina': 53115280.0,
  267.              'Australia': 31130.0,
  268.              'Bhutan': 4910.0,
  269.              'Brazil': 23270.0,
  270.              'Brunei': 439910.0,
  271.              'Canada': 169924580.0,
  272.              'Chile': 35049860.0,
  273.              'China': 7738950.0,
  274.              'Colombia': 785500.0,
  275.              'Congo, Rep.': 320.0,
  276.              'Croatia': 9469840.0,
  277.              'Czech Republic': 45726850.0,
  278.              'Denmark': 24048990.0,
  279.              'Dominica': 40.0,
  280.              'Finland': 54769630.0,
  281.              'Gabon': 780.0,
  282.              'Germany': 615356990.0,
  283.              'Greece': 33888240.0,
  284.              'Guatemala': 30000.0,
  285.              'Hong Kong, China': 19934540.0,
  286.              'Hungary': 18034000.0,
  287.              'Iceland': 2811190.0,
  288.              'Indonesia': 127360.0,
  289.              'Ireland': 20753690.0,
  290.              'Japan': 475253890.0,
  291.              'Korea, Rep.': 56641070.0,
  292.              'Macao': 586100.0,
  293.              'Malaysia': 9380.0,
  294.              'Mexico': 23000.0,
  295.              'Morocco': 1400.0,
  296.              'Netherlands': 113795170.0,
  297.              'New Zealand': 30820660.0,
  298.              'Nicaragua': 1729430.0,
  299.              'Norway': 36343830.0,
  300.              'Oman': 442640.0,
  301.              'Paraguay': 663460.0,
  302.              'Portugal': 69261980.0,
  303.              'Romania': 10049050.0,
  304.              'Saudi Arabia': 62058530.0,
  305.              'Singapore': 11639890.0,
  306.              'South Africa': 66770.0,
  307.              'Spain': 48490010.0,
  308.              'St. Kitts and Nevis': 33630.0,
  309.              'St. Lucia': 190.0,
  310.              'St. Vincent and the Grenadines': 3540.0,
  311.              'Sweden': 89521210.0,
  312.              'Switzerland': 64723880.0,
  313.              'Thailand': 1410.0,
  314.              'Trinidad and Tobago': 223710.0,
  315.              'Tunisia': 2565840.0,
  316.              'Turkey': 41710700.0,
  317.              'United Arab Emirates': 1470530.0,
  318.              'United Kingdom': 394860960.0,
  319.              'United States': 1305456900.0}, preberi_vrednost(1993))
  320.  
  321.     def test_02_najcenejse_banane(self):
  322.         drzava, cena = najcenejse_banane(2023)
  323.         self.assertEqual("Burkina Faso", drzava)
  324.         self.assertAlmostEqual(0.016667700370582853, cena)
  325.  
  326.         drzava, cena = najcenejse_banane(2001)
  327.         self.assertEqual("Burkina Faso", drzava)
  328.         self.assertAlmostEqual(0.03806840620811682, cena)
  329.  
  330.         drzava, cena = najcenejse_banane(2002)
  331.         self.assertEqual("Costa Rica", drzava)
  332.         self.assertAlmostEqual(0.02951126062984768, cena)
  333.  
  334.     def test_03_najblizje_sloveniji(self):
  335.         self.assertEqual("Kuwait", najblizje_sloveniji(2000))
  336.         self.assertEqual("Kuwait", najblizje_sloveniji(2001))
  337.         self.assertEqual("Belarus", najblizje_sloveniji(2002))
  338.         self.assertIsNone(najblizje_sloveniji(1992))
  339.         self.assertIsNone(najblizje_sloveniji(1993))
  340.         self.assertEqual("Lithuania", najblizje_sloveniji(1994))
  341.  
  342.     def test_04_razpon_cen(self):
  343.         self.assertAlmostEqualSeq((0.016667700370582853, 30.217391304347824), razpon_cen(2023))
  344.         self.assertAlmostEqualSeq((0.02951126062984768, 35.0), razpon_cen(2002))
  345.  
  346.  
  347. class Test07(TestBase):
  348.     def test_01_naj_uvoznice(self):
  349.         self.assertEqual(
  350.             ['United States', 'China', 'Germany', 'Netherlands', 'Japan'],
  351.             naj_uvoznice(2023, 5))
  352.         self.assertEqual(
  353.             ['United States', 'China'],
  354.             naj_uvoznice(2023, 2))
  355.         self.assertEqual(
  356.             ['United States', 'China', 'Germany', 'Netherlands', 'Japan',
  357.              'Italy', 'France', 'Canada', 'Poland', 'Spain'],
  358.             naj_uvoznice(2023, 10))
  359.         self.assertEqual(
  360.             ['United States', 'China', 'Germany', 'Japan', 'Netherlands',
  361.              'France', 'Italy', 'Iran, Islamic Rep.', 'Canada', 'Argentina'],
  362.             naj_uvoznice(2022, 10))
  363.         self.assertEqual(
  364.             ['United States', 'Germany', 'Japan'],
  365.             naj_uvoznice(2000, 3))
  366.  
  367.     def test_02_izpis(self):
  368.         self.assertEqual("""Slovenia           98   0.79
  369. China            1768   0.61
  370. Germany          1367   0.85
  371. Netherlands      1072   0.85
  372. Japan            1033   0.93""",
  373.             izpis(["Slovenia", "China", "Germany", "Netherlands", "Japan"], 2023).strip("\n"))
  374.  
  375.     def test_03_prvih_n(self):
  376.         self.assertEqual("""United States    3693   0.36
  377. Germany          1378   0.57
  378. Japan             777   0.67
  379. Canada            379   0.44
  380. Netherlands       201   0.54
  381. Korea, Rep.       171   0.47
  382. Sweden            161   0.61
  383. Saudi Arabia      143   0.36
  384. Turkey            111   0.25
  385. Portugal           97   0.26""",
  386.                          prvih_n(1992, 10).strip("\n"))
  387.  
  388.         self.assertEqual("""United States    5091   0.62
  389. China            1768   0.61
  390. Germany          1367   0.85
  391. Netherlands      1072   0.85
  392. Japan            1033   0.93
  393. Italy             844   0.74
  394. France            824   1.05
  395. Canada            615   0.82
  396. Poland            494   0.76
  397. Spain             481   0.77""",
  398.                          prvih_n(2023, 10).strip("\n"))
  399.  
  400.  
  401. class Test08(TestBase):
  402.     def test_01_trend(self):
  403.         self.assertEqual(
  404.             [None, None, 25227100, 31298100, 29645500, 30287800, 25391200, 28693400, 26674200,
  405.              26664600, 28098000, 33809900, 41158800, 47687200, 35354800, 42689100, 59178400,
  406.              69266700, 53954900, 67476400, 39476500, 46749400, 58085400, 72154400, 73737800,
  407.              71087300, 78777200, 91765000, 116509000, 114574000, 123048000, 98173100],
  408.             trend("Slovenia", 1992, 2023))
  409.  
  410.         self.assertEqual(
  411.             [1073860000, 770850000, 1039140000, 1544820000, 1939970000, 1764340000],
  412.             trend("China", 2015, 2020))
  413.  
  414.     def test_02_skupni_uvoz(self):
  415.         self.assertEqual(7829944158, skupni_uvoz(1992))
  416.         self.assertEqual(9055484992, skupni_uvoz(1993))
  417.         self.assertEqual(18326286060, skupni_uvoz(2023))
  418.  
  419.     def test_03_rast_porabe(self):
  420.         self.assertEqual([12499315869,
  421.  12150737601,
  422.  12876667524,
  423.  13476760138,
  424.  13311378540,
  425.  13608018907,
  426.  16741421577,
  427.  17330617831,
  428.  16229139850,
  429.  17055847086,
  430.  18133232727],
  431.                          rast_porabe(2000, 2010))
  432.  
  433.     def test_04_inflacija(self):
  434.         self.assertAlmostEqualSeq(
  435.             [0.6738503991609981,
  436.              0.6809512139492303,
  437.              0.6742437705078757,
  438.              0.6855178768269456,
  439.              0.6806523075526903,
  440.              0.6769034579795189],
  441.             inflacija(2015, 2020))
  442.         self.assertAlmostEqualSeq(
  443.             [0.6769034579795189, 0.6892200295239537, 0.7527776871788754, 0.8208387799224389],
  444.             inflacija(2020, 2023))
  445.         self.assertAlmostEqualSeq(
  446.             [0.4551290760303785,
  447.              0.4340479403888785,
  448.              0.4912764163527277,
  449.              0.5294995756358607,
  450.              0.5211428398147174,
  451.              0.49504904182201176,
  452.              0.49917266781740505,
  453.              0.4681445898191336,
  454.              0.46244905085852905,
  455.              0.4786728527098904,
  456.              0.5031545559380399,
  457.              0.5457046660096905,
  458.              0.6130420403475356,
  459.              0.6463971390776964,
  460.              0.5430989804647938,
  461.              0.5967451634355989,
  462.              0.7502147946553064,
  463.              0.6937984323108277,
  464.              0.6359469331041802,
  465.              0.6814821244145716,
  466.              0.6922799408846333,
  467.              0.5519020780710178,
  468.              0.7151778439910942,
  469.              0.6738503991609981,
  470.              0.6809512139492303,
  471.              0.6742437705078757,
  472.              0.6855178768269456,
  473.              0.6806523075526903,
  474.              0.6769034579795189,
  475.              0.6892200295239537,
  476.              0.7527776871788754,
  477.              0.8208387799224389],
  478.             inflacija(1992, 2023))
  479.  
  480.  
  481. class Test09(TestBase):
  482.     def test_01_tabela(self):
  483.         tabela(1992, 2000, ["Slovenia", "Germany", "Japan", "United States"], "my-file1.txt")
  484.         with open("my-file1.txt") as f, open("test1.txt") as g:
  485.             self.assertEqual(g.read().strip("\n"), f.read().rstrip("\n"))
  486.  
  487.         tabela(2000, 2010, ["Croatia", "Mexico"], "my-file2.txt")
  488.         with open("my-file2.txt") as f, open("test2.txt") as g:
  489.             self.assertEqual(g.read().strip("\n"), f.read().rstrip("\n"))
  490.  
  491.         tabela(1992, 2020, ["Croatia", "Mexico", "Slovenia", "Germany", "Japan", "United States", "Peru", "Spain", "Turkey"], "my-file3.txt")
  492.         with open("my-file3.txt") as f, open("test3.txt") as g:
  493.             self.assertEqual(g.read().strip("\n"), f.read().rstrip("\n"))
  494.  
  495.  
  496. class Test10(TestBase):
  497.     def test_01_tabela_drzav(self):
  498.         tabela_drzav(1992, 2000, ["Slovenia", "Germany", "Japan", "United States"], "my-file-d1.txt")
  499.         with (open("my-file-d1.txt", encoding="utf-8") as f,
  500.               open("test-d1.txt", encoding="utf-8") as g):
  501.             self.assertEqual(g.read().strip("\n"), f.read().rstrip("\n"))
  502.  
  503.         tabela_drzav(2000, 2010, ["Croatia", "Mexico"], "my-file-d2.txt")
  504.         with (open("my-file-d2.txt", encoding="utf-8") as f,
  505.               open("test-d2.txt", encoding="utf-8") as g):
  506.             self.assertEqual(g.read().strip("\n"), f.read().rstrip("\n"))
  507.  
  508.         tabela_drzav(1992, 2020, ["Croatia", "Mexico", "Slovenia", "Germany", "Japan", "United States", "Peru", "Spain", "Turkey"], "my-file-d3.txt")
  509.         with (open("my-file-d3.txt", encoding="utf-8") as f,
  510.               open("test-d3.txt", encoding="utf-8") as g):
  511.             self.assertEqual(g.read().strip("\n"), f.read().rstrip("\n"))
  512.  
  513.  
  514. if __name__ == "__main__":
  515.     unittest.main()
  516.  
Advertisement
Add Comment
Please, Sign In to add comment