Josif_tepe

Untitled

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