From 5bc47c8f37f52548b28c81cc527549a8bc138f36 Mon Sep 17 00:00:00 2001 From: iravasu Date: Fri, 22 Nov 2024 23:09:23 +0300 Subject: [PATCH 1/8] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=BE=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D0=BC=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=D0=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 43 ++++++++ __pycache__/calculate.cpython-313.pyc | Bin 0 -> 1508 bytes __pycache__/circle.cpython-313.pyc | Bin 0 -> 484 bytes __pycache__/square.cpython-313.pyc | Bin 0 -> 366 bytes __pycache__/triangle.cpython-313.pyc | Bin 0 -> 501 bytes calculate.py | 50 +++++---- .../test_calculate.cpython-313.pyc | Bin 0 -> 4004 bytes tests/__pycache__/test_circle.cpython-313.pyc | Bin 0 -> 1732 bytes tests/__pycache__/test_square.cpython-313.pyc | Bin 0 -> 1523 bytes .../__pycache__/test_triangle.cpython-313.pyc | Bin 0 -> 1149 bytes tests/test_calculate.py | 99 ++++++++++++++++++ tests/test_circle.py | 47 +++++++++ tests/test_square.py | 46 ++++++++ tests/test_triangle.py | 26 +++++ triangle.py | 4 +- 15 files changed, 290 insertions(+), 25 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 __pycache__/calculate.cpython-313.pyc create mode 100644 __pycache__/circle.cpython-313.pyc create mode 100644 __pycache__/square.cpython-313.pyc create mode 100644 __pycache__/triangle.cpython-313.pyc create mode 100644 tests/__pycache__/test_calculate.cpython-313.pyc create mode 100644 tests/__pycache__/test_circle.cpython-313.pyc create mode 100644 tests/__pycache__/test_square.cpython-313.pyc create mode 100644 tests/__pycache__/test_triangle.cpython-313.pyc create mode 100644 tests/test_calculate.py create mode 100644 tests/test_circle.py create mode 100644 tests/test_square.py create mode 100644 tests/test_triangle.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 000000000..a3531b13f --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,43 @@ +name: Tests Workflow + +on: + push : + branches: + - master + - release + pull_request: + branches: + - master + - release + - develop + +jobs: + test: + runs-on: ubuntu-latest + env: + FULL_NAME: "Сафарян Элен" + GROUP_NUMBER: "M3110" + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Print information + run: | + echo "DO: ${{ env. FULL_NAME }}" + echo "Номер группы: ${{ env.GROUP_NUMBER }}" + echo "Ник на Github: ${{ github.actor }}" + + - name: Set up Python 3.13 + uses: actions/ setup-python@v4 + with: + python-version: 3.13 + + - name: Run unit tests + run: | + python -m unittest discover -s /home/runner/work/geometric_lib/geometric_lib/tests + + - name: Lint code with flake8 + run: | + pip install flake8 + flakes --ignore=F401 . \ No newline at end of file diff --git a/__pycache__/calculate.cpython-313.pyc b/__pycache__/calculate.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..42383443b722fd758a50480b48fad8ead49fd603 GIT binary patch literal 1508 zcmZuw&u`;I6rQmiJE@g4$u3P>3CSzcZoSZyRa?;Z7p#^dD%41==@fSkDT#)%6^Op-V6&3p5`_r7`Q zC6hx4){TpI?IMrRugr*+G*Ei)fpUU0q~Q+{>F=1}(B=p?gEVdf6JFyrVTL1u78`s- zP1K}4?7AfyrzD1}29^(?j zl}o4%sbXF3Uz=u*dU{}7U8u`_w1InI{NqkWN%{fVEAuMEWOfu#R^?>#zTuqZBZv;}n|ZU4BQ1Vm2dQqXXD=ex9`n%N2CNgg40nt2z&N zx%s{WGVLS=D$=Pa2`^k3w4vZQyU)4(%ATOVBrTB}JK_pJY^p&bW#* zuC#>CfgG9@qe|Sk9?v6HE>b*Q&*`pLt5z$-w@OL{-p%mK6?Z|YCsrRxb-W!Tt@fCBxZJ3R{=q&ttti#uk629yQz-%R>p@r`eIzT5e1+4=^GI(wLHou2e zm~#j(K0_);xx$+idmcMb6sq1^*wsCYc)OmlXOLans=)M;viZO%KioA9$E-PqZxyPC zG_hn`uJyP|R%nhn3QJA}c5kI{t)k8 H8^Zq$aujYQ literal 0 HcmV?d00001 diff --git a/__pycache__/circle.cpython-313.pyc b/__pycache__/circle.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7ddedcb00339bed78dc7ab8e3af4a0a27dc0f9d5 GIT binary patch literal 484 zcmaJ-Jxjx25WY*IrpEe(gVe=Agba2qsHnRT(9p?%#P}Km2@RJ>>E;>`>QeA8|0yfxq4g|K+0YY14 zRD3kGscf@6oOMehCso$6%P@f}##%0$olSym9}X58+yfJ>Tq(53P45dO4stAz61jV~ z*!K>dej+Gw68Fv}#}|W0q!f79VQ}Mk0ePY5#-l=WNw_5M(pZx%hDZ1`*2cZ8_k58Z zy=-KsZ(671Jj|nQ<9y=(Xe-uK-bV6CT4Jb4Q;XfA6$=WOs~}qCSeNCR*1NItl#Ykl z@Q)T5Ts2kVKY{9qY9xm$U$4`TqrvS%C{Addo8;#tn+)G7B7T7JvxvA;>I~J)FUNaS AAOHXW literal 0 HcmV?d00001 diff --git a/__pycache__/square.cpython-313.pyc b/__pycache__/square.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..2445ed0d3fdbf29b31ccd6184f0cd96e592157c6 GIT binary patch literal 366 zcmZusJ5Iwu5S_6NaY!PdrJ_JcE})}92+@PwM3E>gMT=Q1OOBGvl7cQJ93Y3tDYyo5 zOGSs+3-HFq9V5-mo6o*C>qnzKf>rM~#Vzuu7M<}Q@CN`J8q*O8jTw=PAPMtsu4@52 zgnOmi&^?{)5S>uM>6W%!vvIn8T%YEb*~D3KnKMsDvck@1_F2k&T9pr(Tds`Q^TkUY zAsgx8jWWAF!kRt?!?%m|okv0cs}C{3r9A2MgY+vxPxLlX)ZQVxfOUUirG&f{8KBml yQ_v{AE>%ln)M*klr!-qFm9|@w_-ZzPTvhg3R9t`Q_7T2AX8wto4}FZ#@caV3??HnA literal 0 HcmV?d00001 diff --git a/__pycache__/triangle.cpython-313.pyc b/__pycache__/triangle.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f6f717390e2f8e9bf3336385b29c89d2a24ece37 GIT binary patch literal 501 zcmY*Vze~eF6n-y>+8>Eku!Bwt4hD%>YX?QD;wThK2y_U@wVt8SUtDx>5V~fINI^Fp z-TYJ9tz=Pfauf3p_%5}g5AJ*49q+sQzKabb15`J!N8TOvce$As{*ZWq$N+3u0Rc7= zqNkW@L!Ay%mt3y6OfY~NTGSmyqK#G-prh7`4p{V>H3Lv9{IZ=XvlS}^unwkL`A|sb z(V}`gdNcO9P#-}TuA6RyX*ZGI`5jk8v>?h;`guf?}| z*OA5qXF_I`sr+q%K8(|uARjIU%OMML{o}E2gw@-8aC%>OD2?>B$Lf= 0 for s in size) + + if fig == 'triangle': + a, b, c = size + assert a + b > c and a + c > b and b + c > a, "Invalid triangle sides" + + result = eval(f'{fig}.{func}(*{size})') + return result + diff --git a/tests/__pycache__/test_calculate.cpython-313.pyc b/tests/__pycache__/test_calculate.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ed71f22c9e4a3b079fbaf9d09688eaccb6581580 GIT binary patch literal 4004 zcmd5<-)kII6u$FwC!3v3vQ5%$)NRr>#bBFQOsK7GX{Z%#ZPbjBVgkc-JK2uR&Zc)} zQ>`FHP#2%_Qs{$93W6*M3i{xmVXQUbR-vFT3qFLTBKYJvGqcm3&2C$)QZMYccjlgZ zX6|>tbIzSjRZS9T$JdS*epd+j6));d3Ok#Zp>vgJMB`2qivRp+?i5e?!Qh@ig)G@c zG+}^f;vAU#iSXtiDR2dDM6TiW)CkAKykY0_ zjZ?VrV)Iq#Tm_Gm(+K4?jtUw-K&XgsMI}uHgeLW#GkKwu|^(O6N$@NZNEE_fxi-vcB@e5W}Vxnu>^Gukx3QU|YJ9)TaT{f9a zO}A`&E-rWw8yi1ycy0zfxpS^DZ_v4dSt^xnD?M9pgu~XOX7)0`G|3JN(7-L16VhYH^a0+(7OI4`h4%ATO6!l$0 zpgDwA%w&9N)LjJJ47LzY%;ItI=I#eTcCw*-SXZ_pw&tdPx#y;`dn-3lTsz?Fq4jc~ z$leb-p-Gw?Mt5S0VhUv)3nsN7#!Tu>uZdu$Oqfqg1aygdm@?xk(lp8qj5^rt}5W2@@uPwMDjn^>zpRi6>Q zNltQ!SYi0TZbXn4<;9X+E}B8CKMRYX&mkFcT7xK$ID>5```s>J5GZ%QKt)!iF&?Nc0AT9 zO>8xr2H&A%V+WU|1C_;!_#n*slKo5_7rsqS@@*s=r?`HWd1MreQ$Z@mU>gwv%_7M) zzf!ehIqc)P%lH8mKLG7`Fox+1FtZBZT3t-hb;l^0y3UfiUM!t2+t^R( zI(|ZgBV9UVEb4l;<(N)M*Xa(JP6rV%t5Pfq^l5}~1U%N$NrdMSUIJi!y6$?02cHp! z2WvQ2hI|N)PNM;ySp6(XF`@f_9T%N_NH)Zzba*41kcKxh326_)v8V_46lvE+uME%) z@CQQR5MAKviiYLr`iR1mvSWFehFM4S?cyhJ+U=VL$rwI;{I_@+@4*+8PfVvWltr7ZXZ#SmhpTBSZD7-^<~LKZHQb+V4k?k4BX zDk_vB*yd3XA&&||SrGb~f1+RvqFf&oeA>6*rmv;v+}WKbF;b9T*l+H+KhB=-<2$$2 z*VoN(EdP0-{I|*2dkQ)u)tpe*GM7&?psz8_rwlNrOfYltx&>C2<(X>^GuJAz zS)H|$DE^LPei~pGyWG^-pBVE77{jw)#OvEJ&&Jzfj+J?t7wjg*GX+Cg9{8S0FZmEu z{K$vW_Itb?w2tBA5he%jGB8{YrfUo{u&6*txfY`BrVzVa8=P`i!BO2m`ZBr{Kq>T_ zM7`8WMmcT#QT?qV0&2mld_LKqS?@U8`Q ziCHw!|Spry+sd-Wxs|JLQoQ6uuzPUzSJm6T7?r!w^f=}g_eBq zUwuv_tHebfNBD)k%M7g8TQ(aw_~7QlBP;f%&C-3ZoRM{BWcB9L==WPsZmngWJ0mZg zZ(4X^r@o|)*7qV#K1W}dj~kQ~nWBmfV+JTJ7_n?Uv20o}Ais4qfk6z^zJ!ms<3mji z?L}2@>APciX9wTKhhHXqv+GWF_2A>oOJ@`g;laIaw17LGMe{$5bR3=Qny%G0Q)&j% z_)ksJ0;ngU2La7QdP5(+@o<`WF%i-0_8=28WnAix9m!F;a zW1=zfK39>yH*cr-YW$4b9<*>h{XV86b4nlS`z{eP~nFOjuPY%)Bz!6 zU{R!LRthhIwSc;K=6cR%*j)SP<>sx0Abea%bdo;+vgmlHH}&rmp9aONBg< z7$fL##{p8*m2TeRt%p@LdLE_lD9!^kr=4?$-75`f?c@Pt7cjFrU=F(-VK5iV>h<* zcrP(=A-_630a0ZOQE|(j5UN**a&@&Gz?AC xen9*uOdgpxwW-bIej>A*$Q%JFjYSR2*v88}Bl+EoeOh?G@Zo900CO}<^e?3LIgbDU literal 0 HcmV?d00001 diff --git a/tests/__pycache__/test_triangle.cpython-313.pyc b/tests/__pycache__/test_triangle.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..438794ba4b9f779554c5280447755e2d25f11577 GIT binary patch literal 1149 zcmcgr&ubGw6n?Y2*=}mm()5REYa!NDNrj?FL5he{ytK4Jw}FCzVH&q#CE2!bH-a}2 z)OagMp~qhP$9O13D&s-HQ*Oc3yYFq2RO{V2eDmJy_h#n%=50Hj?gtvXpB`227yv&w z>5fDvXuqUjA6#(7G9b^!vRE?ESTfPfK^CULH7CHeO0ed1i!bJR^E}2x$#xS98vrT; zK-_@~PB)r{`A(RFil~T!-Q;|=U}#IBr*v}1L%-%l9+tZ`Jc~}!&QY*Wo1<_64OgJ) z8WVsPUl0?ng?1%TuytzP3!`=PRZyvVO^b@k-7R=0R?fF>FAtc^_3 z>?#YaBCK^(QbBJNE!UVQS9y>gD4459W|N87N6_SBnl?efKwi~XR2X^~J=v|R>QO>l zp;z6~LTRz7MOoYEh4pF_a#@atzPGqgDpEyQ3e}dvQpMXQZ_qExs=rx^NMjhIq_tS; z+|DbVywAe<$54c{~4ANm@iq0Vf`jP0p|#~-H}cFTrj`qarDJK2Mq zhu1$l`8Kitmxq|7vzjRa@{QjP(+u2B7vA6qrFtON)aw~N`LE#K$3d>YO>jx|_%#dunO8g`0&pz!)8K{~krS6xqw*9yhLMWs=~P6owOOZ|6X6;Y_>fo+a&N{Gkf<>hx5n7`7@xTH49d%@#4hHez8*TSKh3=ecCd?8jo-K8}|+RL;wH) literal 0 HcmV?d00001 diff --git a/tests/test_calculate.py b/tests/test_calculate.py new file mode 100644 index 000000000..5e19bf404 --- /dev/null +++ b/tests/test_calculate.py @@ -0,0 +1,99 @@ +import unittest +from calculate import calc +import math + +class TestCalculate(unittest.TestCase): + + def test_circle_area(self): + # Arrange (given) + fig = 'circle' + func = 'area' + size = [1] + + # Act (when) + result = calc(fig, func, size) + + # Assert (then) + self.assertAlmostEqual(result, math.pi, places=7) + + def test_square_area(self): + fig = 'square' + func = 'area' + size = [1] + + result = calc(fig, func, size) + + self.assertEqual (result, 1) + + def test_triangle_area(self): + fig = 'triangle' + func = 'area' + size = [3, 4, 5] + + result = calc(fig, func, size) + + self.assertEqual(result, 6) + + def test_circle_perimeter (self): + fig = 'circle' + func = 'perimeter' + size = [1] + + result = calc(fig, func, size) + + self.assertAlmostEqual(result, 2 * math.pi, places=7) + + def test_square_perimeter (self): + fig = 'square' + func = 'perimeter' + size = [1] + + result = calc(fig, func, size) + + self.assertEqual(result, 4) + + def test_triangle_perimeter(self): + fig = 'triangle' + func = 'perimeter' + size = [3, 4, 5] + + result = calc(fig, func, size) + + self.assertEqual(result, 12) + + def test_invalid_figure(self): + # Arrange (given) + fig = 'hexagon' + func = 'area' + size = [1] + + # Act & Assert (when & then) + with self.assertRaises(AssertionError): + calc(fig, func, size) + + def test_invalid_function(self): + fig = 'circle' + func = 'volume' + size = [1] + + with self.assertRaises(AssertionError): + calc(fig, func, size) + + def test_invalid_size_count(self): + fig = 'circle' + func = 'area' + size = [1, 2] + + with self.assertRaises(AssertionError): + calc(fig, func, size) + + def test_invalid_triangle(self): + fig = 'triangle' + func = 'area' + size = [1, 2, 10] + + with self.assertRaises(AssertionError): + calc(fig, func, size) + +if __name__== '__main__': + unittest.main() \ No newline at end of file diff --git a/tests/test_circle.py b/tests/test_circle.py new file mode 100644 index 000000000..1ff5ebb8b --- /dev/null +++ b/tests/test_circle.py @@ -0,0 +1,47 @@ +import unittest +from circle import area, perimeter +import math + +class TestCircle(unittest.TestCase): + def test_area(self): + # Arrange (given) + radius = 1 + + # Act (when) + result = area (radius) + + # Assert (then) + self.assertAlmostEqual(result, math.pi, places = 7) + + def test_area_zero(self): + # Arrange (given) + radius = 0 + + # Act (when) + result = area(radius) + # Assert (then) + + self.assertEqual(result, 0) + + def test_perimeter(self): + # Arrange (given) + radius = 1 + + # Act (when) + result = perimeter (radius) + + # Assert (then) + self.assertAlmostEqual(result, 2 * math.pi, places = 7) + + def test_perimeter_zero(self): + # Arrange (given) + radius = 0 + + # Act (when) + result = perimeter (radius) + + # Assert (then) + self.assertEqual(result, 0) + +if __name__== '__main__': + unittest.main() \ No newline at end of file diff --git a/tests/test_square.py b/tests/test_square.py new file mode 100644 index 000000000..ffd0dd660 --- /dev/null +++ b/tests/test_square.py @@ -0,0 +1,46 @@ +import unittest +from square import area, perimeter + +class TestSquare(unittest. TestCase): + def test_area(self): + # Arrange (given) + side = 1 + + # Act (when) + result = area(side) + + # Assert (then) + self.assertEqual(result, 1) + + def test_area_zero(self): + # Arrange (given) + side = 0 + + # Act (when) + result = area(side) + + # Assert (then) + self.assertEqual(result, 0) + + def test_perimeter (self): + # Arrange (given) + side = 1 + + # Act (when) + result = perimeter(side) + + # Assert (then) + self.assertEqual(result, 4) + + def test_perimeter_zero(self): + # Arrange (given) + side = 0 + + # Act (when) + result = perimeter (side) + + # Assert (then) + self.assertEqual(result, 0) + +if __name__== '__main__': + unittest.main() \ No newline at end of file diff --git a/tests/test_triangle.py b/tests/test_triangle.py new file mode 100644 index 000000000..343001720 --- /dev/null +++ b/tests/test_triangle.py @@ -0,0 +1,26 @@ +import unittest +from triangle import area, perimeter + +class TestTriangle(unittest.TestCase): + def test_area(self): + # Arrange (given) + a, b, c = 3, 4, 5 + + # Act (when) + result = area(a, b, c) + + # Assert (then) + self.assertEqual (result, 6) + + def test_perimeter (self): + # Arrange (given) + a, b, c = 3, 4, 5 + + # Act (when) + result = perimeter(a, b, c) + + # Assert (then) + self.assertEqual(result, 12) + +if __name__== '__main__': + unittest.main() \ No newline at end of file diff --git a/triangle.py b/triangle.py index fb41f60a5..dbd18ba68 100644 --- a/triangle.py +++ b/triangle.py @@ -1,6 +1,6 @@ def area(a, b, c): - return (a + b + c) / 2 - + p = (a + b + c) / 2 + return (p * (p - a) * (p - b) * (p - c)) ** 0.5 def perimeter(a, b, c): return a + b + c From 7c7245a177453666665e3a37fc0c3b40d160e740 Mon Sep 17 00:00:00 2001 From: iravasu Date: Fri, 22 Nov 2024 23:24:02 +0300 Subject: [PATCH 2/8] changes --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a3531b13f..77add9612 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,9 +29,9 @@ jobs: echo "Ник на Github: ${{ github.actor }}" - name: Set up Python 3.13 - uses: actions/ setup-python@v4 + uses: actions/setup-python@v4 with: - python-version: 3.13 + python-version: '3.13' - name: Run unit tests run: | @@ -40,4 +40,4 @@ jobs: - name: Lint code with flake8 run: | pip install flake8 - flakes --ignore=F401 . \ No newline at end of file + flake8 --ignore=F401 . \ No newline at end of file From 7ce09ceeaa788ccf119ba511083f7381cbb08ab8 Mon Sep 17 00:00:00 2001 From: iravasu Date: Fri, 22 Nov 2024 23:28:26 +0300 Subject: [PATCH 3/8] changes1 --- calculate.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/calculate.py b/calculate.py index c9dacd2e5..a937496fe 100644 --- a/calculate.py +++ b/calculate.py @@ -6,32 +6,40 @@ figs = ['circle', 'square', 'triangle'] funcs = ['perimeter', 'area'] sizes = { - 'circle-area' : 1, - 'circle-perimeter' : 1, - 'square-area' : 1, - 'square-perimeter' : 1, - 'triangle-area': 3, - 'triangle-perimeter' : 3 + 'circle-area': 1, + 'circle-perimeter': 1, + 'square-area': 1, + 'square-perimeter': 1, + 'triangle-area': 3, + 'triangle-perimeter': 3 } def calc(fig, func, size): - assert fig in figs - assert func in funcs + assert fig in figs, f"Invalid figure: {fig}" + assert func in funcs, f"Invalid function: {func}" key = f'{fig}-{func}' expected_args = sizes.get(key) assert expected_args is not None, f"Unsupported operation: {key}" assert len(size) == expected_args, f"Expected {expected_args} arguments, got {len(size)}" - assert all(s >= 0 for s in size) + assert all(s >= 0 for s in size), "Sizes must be non-negative" if fig == 'triangle': a, b, c = size assert a + b > c and a + c > b and b + c > a, "Invalid triangle sides" - - result = eval(f'{fig}.{func}(*{size})') - return result - + # Вызываем соответствующую функцию напрямую + if fig == 'circle': + shape = circle + elif fig == 'square': + shape = square + elif fig == 'triangle': + shape = triangle + if func == 'perimeter': + result = shape.perimeter(*size) + elif func == 'area': + result = shape.area(*size) + return result From 25191218c5df0c70c4912142a68c608b31e40bc4 Mon Sep 17 00:00:00 2001 From: iravasu Date: Fri, 22 Nov 2024 23:38:23 +0300 Subject: [PATCH 4/8] changes2 --- calculate.py | 31 +++++++++++++++++-------------- circle.py | 1 - tests/test_calculate.py | 18 ++++++++++-------- tests/test_circle.py | 23 +++++++++++++---------- tests/test_square.py | 15 +++++++++------ tests/test_triangle.py | 11 +++++++---- triangle.py | 1 + 7 files changed, 57 insertions(+), 43 deletions(-) diff --git a/calculate.py b/calculate.py index a937496fe..4ccc69598 100644 --- a/calculate.py +++ b/calculate.py @@ -2,7 +2,6 @@ import square import triangle - figs = ['circle', 'square', 'triangle'] funcs = ['perimeter', 'area'] sizes = { @@ -15,31 +14,35 @@ } def calc(fig, func, size): + # Проверка на допустимость фигуры и функции assert fig in figs, f"Invalid figure: {fig}" assert func in funcs, f"Invalid function: {func}" + # Формируем ключ для поиска в словаре размеров key = f'{fig}-{func}' expected_args = sizes.get(key) assert expected_args is not None, f"Unsupported operation: {key}" + + # Проверка на правильное количество аргументов assert len(size) == expected_args, f"Expected {expected_args} arguments, got {len(size)}" - + + # Проверка на корректность размеров assert all(s >= 0 for s in size), "Sizes must be non-negative" + # Дополнительная проверка для треугольников if fig == 'triangle': a, b, c = size assert a + b > c and a + c > b and b + c > a, "Invalid triangle sides" - # Вызываем соответствующую функцию напрямую - if fig == 'circle': - shape = circle - elif fig == 'square': - shape = square - elif fig == 'triangle': - shape = triangle + # Получаем соответствующий объект фигуры + shape = globals().get(fig) # Получаем объект фигуры (circle, square, triangle) + if shape is None: + raise ValueError(f"Shape {fig} not found") - if func == 'perimeter': - result = shape.perimeter(*size) - elif func == 'area': - result = shape.area(*size) + # Вызываем нужную функцию (perimeter или area) + func_to_call = getattr(shape, func, None) + if func_to_call is None: + raise ValueError(f"Function {func} not found for {fig}") - return result + # Возвращаем результат выполнения функции + return func_to_call(*size) diff --git a/circle.py b/circle.py index c3eb8647c..21e9a1779 100644 --- a/circle.py +++ b/circle.py @@ -7,4 +7,3 @@ def area(r): def perimeter(r): return 2 * math.pi * r - diff --git a/tests/test_calculate.py b/tests/test_calculate.py index 5e19bf404..fadcb8a52 100644 --- a/tests/test_calculate.py +++ b/tests/test_calculate.py @@ -1,9 +1,10 @@ import unittest -from calculate import calc +from calculate import calc import math + class TestCalculate(unittest.TestCase): - + def test_circle_area(self): # Arrange (given) fig = 'circle' @@ -15,7 +16,7 @@ def test_circle_area(self): # Assert (then) self.assertAlmostEqual(result, math.pi, places=7) - + def test_square_area(self): fig = 'square' func = 'area' @@ -23,7 +24,7 @@ def test_square_area(self): result = calc(fig, func, size) - self.assertEqual (result, 1) + self.assertEqual(result, 1) def test_triangle_area(self): fig = 'triangle' @@ -34,7 +35,7 @@ def test_triangle_area(self): self.assertEqual(result, 6) - def test_circle_perimeter (self): + def test_circle_perimeter(self): fig = 'circle' func = 'perimeter' size = [1] @@ -43,7 +44,7 @@ def test_circle_perimeter (self): self.assertAlmostEqual(result, 2 * math.pi, places=7) - def test_square_perimeter (self): + def test_square_perimeter(self): fig = 'square' func = 'perimeter' size = [1] @@ -95,5 +96,6 @@ def test_invalid_triangle(self): with self.assertRaises(AssertionError): calc(fig, func, size) -if __name__== '__main__': - unittest.main() \ No newline at end of file + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_circle.py b/tests/test_circle.py index 1ff5ebb8b..e4efd3379 100644 --- a/tests/test_circle.py +++ b/tests/test_circle.py @@ -1,17 +1,19 @@ import unittest -from circle import area, perimeter +from circle import area, perimeter import math + class TestCircle(unittest.TestCase): + def test_area(self): # Arrange (given) radius = 1 # Act (when) - result = area (radius) + result = area(radius) # Assert (then) - self.assertAlmostEqual(result, math.pi, places = 7) + self.assertAlmostEqual(result, math.pi, places=7) def test_area_zero(self): # Arrange (given) @@ -19,8 +21,8 @@ def test_area_zero(self): # Act (when) result = area(radius) - # Assert (then) + # Assert (then) self.assertEqual(result, 0) def test_perimeter(self): @@ -28,20 +30,21 @@ def test_perimeter(self): radius = 1 # Act (when) - result = perimeter (radius) + result = perimeter(radius) # Assert (then) - self.assertAlmostEqual(result, 2 * math.pi, places = 7) + self.assertAlmostEqual(result, 2 * math.pi, places=7) def test_perimeter_zero(self): # Arrange (given) radius = 0 # Act (when) - result = perimeter (radius) - + result = perimeter(radius) + # Assert (then) self.assertEqual(result, 0) -if __name__== '__main__': - unittest.main() \ No newline at end of file + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_square.py b/tests/test_square.py index ffd0dd660..32eb69f1c 100644 --- a/tests/test_square.py +++ b/tests/test_square.py @@ -1,7 +1,9 @@ import unittest from square import area, perimeter -class TestSquare(unittest. TestCase): + +class TestSquare(unittest.TestCase): + def test_area(self): # Arrange (given) side = 1 @@ -22,7 +24,7 @@ def test_area_zero(self): # Assert (then) self.assertEqual(result, 0) - def test_perimeter (self): + def test_perimeter(self): # Arrange (given) side = 1 @@ -35,12 +37,13 @@ def test_perimeter (self): def test_perimeter_zero(self): # Arrange (given) side = 0 - + # Act (when) - result = perimeter (side) + result = perimeter(side) # Assert (then) self.assertEqual(result, 0) -if __name__== '__main__': - unittest.main() \ No newline at end of file + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_triangle.py b/tests/test_triangle.py index 343001720..b38a2eeda 100644 --- a/tests/test_triangle.py +++ b/tests/test_triangle.py @@ -1,7 +1,9 @@ import unittest from triangle import area, perimeter + class TestTriangle(unittest.TestCase): + def test_area(self): # Arrange (given) a, b, c = 3, 4, 5 @@ -10,9 +12,9 @@ def test_area(self): result = area(a, b, c) # Assert (then) - self.assertEqual (result, 6) + self.assertEqual(result, 6) - def test_perimeter (self): + def test_perimeter(self): # Arrange (given) a, b, c = 3, 4, 5 @@ -22,5 +24,6 @@ def test_perimeter (self): # Assert (then) self.assertEqual(result, 12) -if __name__== '__main__': - unittest.main() \ No newline at end of file + +if __name__ == '__main__': + unittest.main() diff --git a/triangle.py b/triangle.py index dbd18ba68..0d6336828 100644 --- a/triangle.py +++ b/triangle.py @@ -2,5 +2,6 @@ def area(a, b, c): p = (a + b + c) / 2 return (p * (p - a) * (p - b) * (p - c)) ** 0.5 + def perimeter(a, b, c): return a + b + c From df0d2c1fff7e32d321ed31357e1ee2a083efe4a1 Mon Sep 17 00:00:00 2001 From: iravasu Date: Fri, 22 Nov 2024 23:40:59 +0300 Subject: [PATCH 5/8] changes3 --- calculate.py | 1 + 1 file changed, 1 insertion(+) diff --git a/calculate.py b/calculate.py index 4ccc69598..1f1bd6df5 100644 --- a/calculate.py +++ b/calculate.py @@ -2,6 +2,7 @@ import square import triangle +# Список поддерживаемых фигур и функций figs = ['circle', 'square', 'triangle'] funcs = ['perimeter', 'area'] sizes = { From f9e59e15021eb83d316507a692b28a2131be63b3 Mon Sep 17 00:00:00 2001 From: iravasu Date: Fri, 22 Nov 2024 23:42:41 +0300 Subject: [PATCH 6/8] changes4 --- calculate.py | 1 + 1 file changed, 1 insertion(+) diff --git a/calculate.py b/calculate.py index 1f1bd6df5..5fc7176d8 100644 --- a/calculate.py +++ b/calculate.py @@ -14,6 +14,7 @@ 'triangle-perimeter': 3 } + def calc(fig, func, size): # Проверка на допустимость фигуры и функции assert fig in figs, f"Invalid figure: {fig}" From f8c4731e80e3a8bba520f0351320f34ae411946c Mon Sep 17 00:00:00 2001 From: iravasu Date: Fri, 22 Nov 2024 23:49:33 +0300 Subject: [PATCH 7/8] changes5 --- calculate.py | 64 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/calculate.py b/calculate.py index 5fc7176d8..eab9e201d 100644 --- a/calculate.py +++ b/calculate.py @@ -2,7 +2,6 @@ import square import triangle -# Список поддерживаемых фигур и функций figs = ['circle', 'square', 'triangle'] funcs = ['perimeter', 'area'] sizes = { @@ -16,35 +15,50 @@ def calc(fig, func, size): - # Проверка на допустимость фигуры и функции - assert fig in figs, f"Invalid figure: {fig}" - assert func in funcs, f"Invalid function: {func}" + # Проверка наличия передаваемого аргумента fig в figs + assert fig in figs - # Формируем ключ для поиска в словаре размеров + # Проверка наличия передаваемого аргумента func в funcs + assert func in funcs + + # Проверка на количество аргументов key = f'{fig}-{func}' expected_args = sizes.get(key) - assert expected_args is not None, f"Unsupported operation: {key}" - - # Проверка на правильное количество аргументов - assert len(size) == expected_args, f"Expected {expected_args} arguments, got {len(size)}" - - # Проверка на корректность размеров - assert all(s >= 0 for s in size), "Sizes must be non-negative" - - # Дополнительная проверка для треугольников + assert expected_args is not None + assert len(size) == expected_args + + # Проверка на неотрицательность аргументов + assert all(s >= 0 for s in size) + + # Проверка на существование треугольника if fig == 'triangle': a, b, c = size - assert a + b > c and a + c > b and b + c > a, "Invalid triangle sides" + assert a + b > c and a + c > b and b + c > a + + try: + # Вызов функции из соответствующего модуля + result = getattr(globals()[fig], func)(*size) + except ValueError as e: + print(f"Ошибка: {e}") # Обработка исключения ValueError + return None + + return result + + +if __name__ == "main": + func = '' + fig = '' + size = list() + + while fig not in figs: + fig = input(f"Enter figure name, avaliable are {figs}:\n") - # Получаем соответствующий объект фигуры - shape = globals().get(fig) # Получаем объект фигуры (circle, square, triangle) - if shape is None: - raise ValueError(f"Shape {fig} not found") + while func not in funcs: + func = input(f"Enter function name, avaliable are {funcs}:\n") - # Вызываем нужную функцию (perimeter или area) - func_to_call = getattr(shape, func, None) - if func_to_call is None: - raise ValueError(f"Function {func} not found for {fig}") + while len(size) != sizes.get(f"{func}-{fig}", 1): + size = list(map(int, input( + "Input figure sizes separated by space, 1 for circle and square\n" + ).split(' '))) - # Возвращаем результат выполнения функции - return func_to_call(*size) + calc(fig, func, size) \ No newline at end of file From f6845143551abbaa150eda74fa5cbc535d6fa1a6 Mon Sep 17 00:00:00 2001 From: iravasu Date: Fri, 22 Nov 2024 23:50:48 +0300 Subject: [PATCH 8/8] chamges6 --- calculate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/calculate.py b/calculate.py index eab9e201d..b3d494404 100644 --- a/calculate.py +++ b/calculate.py @@ -61,4 +61,4 @@ def calc(fig, func, size): "Input figure sizes separated by space, 1 for circle and square\n" ).split(' '))) - calc(fig, func, size) \ No newline at end of file + calc(fig, func, size)