Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sem3-ivt19-task1-1-mozgovoy
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Мозговой Никита Александрович
sem3-ivt19-task1-1-mozgovoy
Commits
1ace3537
Commit
1ace3537
authored
Dec 10, 2020
by
Мозговой Никита Александрович
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
final
parent
08daf9e7
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
128 additions
and
41 deletions
+128
-41
calcprint.cpython-38.pyc
__pycache__/calcprint.cpython-38.pyc
+0
-0
calc-history.log.txt
calc-history.log.txt
+30
-7
calcprint.py
calcprint.py
+2
-2
calculate.py
calculate.py
+96
-32
No files found.
__pycache__/calcprint.cpython-38.pyc
View file @
1ace3537
No preview for this file type
calc-history.log.txt
View file @
1ace3537
2020-11-26 13:59:15.377841 : +: (1, 2, 3, 4, 5, 6, 7, 8, 9) = 45.0
2020-11-26 13:59:15.421833 : +: (1.000001, 2.000002, 3.00005) = 6.000053
2020-11-26 13:59:15.425833 : +: (1, 2, 3, 4, 5, 6, 7, 8, 9) = 45.0
2020-11-26 13:59:15.429832 : -: (1, 2, 3, 4, 5, 6, 7, 8, 9) = -45.0
2020-11-26 13:59:15.433831 : /: (1, 2, 3, 4) = 0.04167
2020-11-26 13:59:15.433831 : *: (1.0001, 2.2345) = 2.23472
2020-11-26 13:59:15.433831 : +: (1, 2, 3, 4, 5, 6, 7, 8, 9) = 45.0
2020-12-06 22:01:41.637499 : + : (4.0, 4.0) = 8.0
2020-12-06 22:01:41.657444 : - : (2, 10.34) = -8.34
2020-12-06 22:01:41.658442 : - : (8, 2.3, 0, 4.55) = 1.15
2020-12-06 22:01:41.658442 : / : (25, 5.0, 2.5) = 2.0
2020-12-06 22:01:41.660436 : * : (3.2, 5.6, 1) = 17.92
2020-12-06 22:01:41.660436 : + : (3.2, 5.4, 1.0, 6) = 15.6
2020-12-06 22:02:14.841294 : + : (4.0, 4.0) = 8.0
2020-12-06 22:02:14.859614 : - : (14.2, 4, 5.4) = 4.8
2020-12-06 22:02:14.860605 : - : (5.2, 6.7, 8) = -9.5
2020-12-06 22:02:14.865592 : / : (36, 6.0, 3) = 2.0
2020-12-06 22:02:14.868583 : * : (7, 2.3, 4.9) = 78.89
2020-12-06 22:02:14.868583 : + : (1.7, 4.4, 3.0, 8) = 17.1
2020-12-06 22:03:42.085424 : + : (4.0, 4.0) = 8.0
2020-12-06 22:03:42.104374 : - : (14.2, 4, 5.4) = 4.8
2020-12-06 22:03:42.104374 : - : (5.2, 6.7, 8) = -9.5
2020-12-06 22:03:42.105372 : / : (36, 6.0, 3) = 2.0
2020-12-06 22:03:42.107367 : * : (7, 2.3, 4.9) = 78.89
2020-12-06 22:03:42.108399 : + : (1.7, 4.4, 3.0, 8) = 17.1
2020-12-06 22:04:58.979922 : + : (4.0, 4.0) = 8.0
2020-12-06 22:04:58.997868 : - : (2, 10.34) = -8.34
2020-12-06 22:04:58.998839 : - : (8, 2.3, 0, 4.55) = 1.15
2020-12-06 22:04:58.998839 : / : (25, 5.0, 2.5) = 2.0
2020-12-06 22:04:59.000833 : * : (3.2, 5.6, 1) = 17.92
2020-12-06 22:04:59.000833 : + : (3.2, 5.4, 1.0, 6) = 15.6
2020-12-06 22:05:40.580340 : + : (4.0, 4.0) = 8.0
2020-12-06 22:05:40.597848 : - : (14.2, 4, 5.4) = 4.8
2020-12-06 22:05:40.597848 : - : (5.2, 6.7, 8) = -9.5
2020-12-06 22:05:40.598847 : / : (36, 6.0, 3) = 2.0
2020-12-06 22:05:40.605827 : * : (7, 2.3, 4.9) = 78.89
2020-12-06 22:05:40.606926 : + : (1.7, 4.4, 3.0, 8) = 17.1
calcprint.py
View file @
1ace3537
...
...
@@ -20,7 +20,7 @@ def print_results(*args, action=None, result=None):
return
lst
def
actionPrint
(
operands
,
action
):
lst
=
[]
for
i
in
range
(
operands
[
0
],
operands
[
-
1
]):
for
i
in
list
(
operands
[
0
:
-
1
]):
lst
.
append
(
f
"{i} {action}"
)
lst
.
append
(
f
"{operands[-1]}"
)
return
lst
...
...
@@ -34,5 +34,5 @@ def print_results(*args, action=None, result=None):
def
write_log
(
*
args
,
action
=
None
,
result
=
None
,
file
=
'calc-history.log.txt'
):
f
=
open
(
file
,
mode
=
'a'
,
errors
=
'ignore'
)
f
.
write
(
f
"{datetime.now()}
: {action}
: {args} = {result}
\n
"
)
f
.
write
(
f
"{datetime.now()}
: {action}
: {args} = {result}
\n
"
)
f
.
close
()
\ No newline at end of file
calculate.py
View file @
1ace3537
from
calcprint
import
print_results
,
write_log
import
unittest
PARAMS
=
{
'precision'
:
None
,
'output_type'
:
None
,
'possible_types'
:
None
,
'dest'
:
None
}
def
load_params
(
file
=
"params.ini"
):
''' Функция загружает параметры вычислений из внешнего файла,
которым по умолчанию является params.ini '''
global
PARAMS
f
=
open
(
file
,
mode
=
'r'
,
errors
=
'ignore'
)
with
open
(
file
,
'r'
,
errors
=
'ignore'
)
as
f
:
lines
=
f
.
readlines
()
for
l
in
lines
:
param
=
l
.
split
(
'='
)
...
...
@@ -19,6 +24,12 @@ def load_params(file="params.ini"):
def
convert_precision
(
prec
):
''''
Преобразует точность данную в виде числа с плавающей точкой в целое число, возможное для использования с функцией round.
>>> convert_precision(0.00001)
5
>>> convert_precision(0.0000000001)
10
>>> convert_precision('s')
None
'''
prec
=
str
(
prec
)
for
i
in
prec
:
...
...
@@ -29,7 +40,47 @@ def convert_precision(prec):
x
=
prec
.
split
(
'.'
)
return
int
(
len
(
x
[
1
]))
def
user_input
():
args
=
[]
while
True
:
val
=
input
(
"Enter value: "
)
try
:
if
val
==
""
:
break
val
=
float
(
val
)
except
ValueError
:
print
(
"Введите число в правильном формате (разделитель дробной части '.') "
)
else
:
args
.
append
(
val
)
print
(
args
)
if
len
(
args
)
<=
1
:
return
action
=
input
(
"action: "
)
try
:
res
=
calculate
(
*
args
,
action
=
action
,
**
PARAMS
)
except
Exception
:
print
(
"Ошибка вычисления. Результат не определен"
)
else
:
print_results
(
*
args
,
action
=
action
,
result
=
res
)
def
calculate
(
*
args
,
action
=
None
,
**
kwargs
):
''' Главная функция приложения калькулятора, в которой и производятся все вычисления,
а также запись в историю вычислений. Результат приводится к заданному типу данных и округляется до заданного количества знаков
>>> calculate(*list(range(1, 10)), action = "+", **PARAMS)
45.0
>>> calculate(*list(range(1, 10)), action = "-", **PARAMS)
-45
>>> calculate(*list(range(1, 5)), action = "/", **PARAMS)
0.04167
>>> calculate(*[1.0001, 2.2345], action = "*", **PARAMS)
2.23472
'''
load_params
()
global
result
precision
=
convert_precision
(
kwargs
[
'precision'
])
...
...
@@ -37,44 +88,57 @@ def calculate(*args, action=None, **kwargs):
if
action
==
'+'
:
result
=
sum
(
args
)
if
type
(
result
)
is
not
output_type
:
result
=
output_type
(
result
)
if
action
==
'-'
:
result
=
0
for
n
in
args
:
result
=
args
[
0
]
for
n
in
args
[
1
:(
len
(
list
(
args
)))]
:
result
-=
n
if
type
(
result
)
is
not
output_type
:
result
=
output_type
(
result
)
if
action
==
'*'
:
result
=
1
for
n
in
args
:
result
*=
n
if
type
(
result
)
is
not
output_type
:
result
=
output_type
(
result
)
result
=
round
(
result
,
precision
)
if
action
==
'/'
:
result
=
args
[
0
]
for
n
in
args
:
if
0
in
args
[
1
:
len
(
args
)]:
return
'Деление невозможно'
else
:
for
n
in
args
[
1
:(
len
(
list
(
args
)))]:
result
/=
n
if
type
(
result
)
is
not
output_type
:
result
=
output_type
(
result
)
result
=
round
(
result
,
precision
)
write_log
(
*
args
,
action
=
action
,
result
=
result
)
return
result
class
TestCalculator
(
unittest
.
TestCase
):
if
__name__
==
"__main__"
:
def
test_summ
(
self
):
self
.
assertEqual
((
calculate
(
*
(
3.2
,
5.4
,
1.0
,
6
),
action
=
"+"
,
**
PARAMS
)),
15.6
)
load_params
()
print
(
calculate
(
*
list
(
range
(
1
,
10
)),
action
=
"+"
,
**
PARAMS
))
def
test_diff_pos
(
self
):
self
.
assertEqual
((
calculate
(
*
(
8
,
2.3
,
0
,
4.55
),
action
=
"-"
,
**
PARAMS
)),
1.15
)
def
test_diff_neg
(
self
):
self
.
assertEqual
((
calculate
(
*
(
2
,
10.34
),
action
=
"-"
,
**
PARAMS
)),
-
8.34
)
def
test_mult
(
self
):
self
.
assertEqual
((
calculate
(
*
(
3.2
,
5.6
,
1
),
action
=
"*"
,
**
PARAMS
)),
17.92
)
def
test_div
(
self
):
self
.
assertEqual
((
calculate
(
*
(
25
,
5.0
,
2.5
),
action
=
"/"
,
**
PARAMS
)),
2.0
)
def
test_error
(
self
):
with
self
.
assertRaises
(
Exception
):
calculate
(
*
(
5
,
0
),
action
=
"/"
,
**
PARAMS
)
calculate
(
*
(
'yes'
,
5
),
action
=
"+"
,
**
PARAMS
)
def
test_file
(
self
):
assert
PARAMS
.
get
(
'dest'
)
==
'output.txt'
,
"Имя файла для записи истории вызовов функции calculate должно быть output.txt"
write_log
(
*
(
1.000001
,
2.000002
,
3.00005
),
action
=
'+'
,
result
=
'6.000053'
)
assert
calculate
(
*
list
(
range
(
1
,
10
)),
action
=
"+"
,
**
PARAMS
)
==
45.0
# 1 + 2 + 3 + .. + 9
assert
calculate
(
*
list
(
range
(
1
,
10
)),
action
=
"-"
,
**
PARAMS
)
==
-
45
assert
calculate
(
*
list
(
range
(
1
,
5
)),
action
=
"/"
,
**
PARAMS
)
==
0.04167
assert
calculate
(
*
[
1.0001
,
2.2345
],
action
=
"*"
,
**
PARAMS
)
==
2.23472
print_results
(
*
list
(
range
(
1
,
10
)),
action
=
"+"
,
result
=
calculate
(
*
list
(
range
(
1
,
10
)),
action
=
"+"
,
**
PARAMS
))
\ No newline at end of file
if
__name__
==
"__main__"
:
load_params
()
user_input
()
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment