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
c8b70977
Commit
c8b70977
authored
Dec 24, 2020
by
Мозговой Никита Александрович
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
final
parent
7d3efded
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
83 additions
and
52 deletions
+83
-52
calcprint.cpython-38.pyc
__pycache__/calcprint.cpython-38.pyc
+0
-0
calculate.cpython-38.pyc
__pycache__/calculate.cpython-38.pyc
+0
-0
calc-history.log.txt
calc-history.log.txt
+16
-0
calcprint.py
calcprint.py
+2
-1
calculate.py
calculate.py
+30
-49
params.ini
params.ini
+1
-2
tests.py
tests.py
+34
-0
No files found.
__pycache__/calcprint.cpython-38.pyc
View file @
c8b70977
No preview for this file type
__pycache__/calculate.cpython-38.pyc
0 → 100644
View file @
c8b70977
File added
calc-history.log.txt
View file @
c8b70977
...
@@ -33,3 +33,19 @@
...
@@ -33,3 +33,19 @@
2020-12-10 11:49:47.014899 : / : (25, 5.0, 2.5) = 2.0
2020-12-10 11:49:47.014899 : / : (25, 5.0, 2.5) = 2.0
2020-12-10 11:49:47.016894 : * : (3.2, 5.6, 1) = 17.92
2020-12-10 11:49:47.016894 : * : (3.2, 5.6, 1) = 17.92
2020-12-10 11:49:47.017892 : + : (3.2, 5.4, 1.0, 6) = 15.6
2020-12-10 11:49:47.017892 : + : (3.2, 5.4, 1.0, 6) = 15.6
2020-12-10 14:53:14.404546 : * : (4.0, 3.0, 2.0, 62.0) = 1488.0
2020-12-10 14:53:14.484896 : - : (2, 10.34) = -8.34
2020-12-10 14:53:14.485893 : - : (8, 2.3, 0, 4.55) = 1.15
2020-12-10 14:53:14.486890 : / : (25, 5.0, 2.5) = 2.0
2020-12-10 14:53:14.488885 : * : (3.2, 5.6, 1) = 17.92
2020-12-10 14:53:14.488885 : + : (3.2, 5.4, 1.0, 6) = 15.6
2020-12-24 12:34:30.506825 : - : (8, 2.3, 0, 4.55) = 1.15
2020-12-24 12:34:30.534744 : / : (25, 5.0, 2.5) = 2.0
2020-12-24 12:34:30.537736 : * : (3.2, 5.6, 1) = 17.92
2020-12-24 12:34:30.538733 : + : (3.2, 5.4, 1.0, 6) = 15.6
2020-12-24 12:35:56.150849 : - : (2, 10.34) = -8.34
2020-12-24 12:35:56.151815 : - : (8, 2.3, 0, 4.55) = 1.15
2020-12-24 12:35:56.152843 : / : (25, 5.0, 2.5) = 2.0
2020-12-24 12:35:56.157801 : * : (3.2, 5.6, 1) = 17.92
2020-12-24 12:35:56.158796 : + : (3.2, 5.4, 1.0, 6) = 15.6
2020-12-24 13:08:11.592307 : + : (3.0, 4.0) = 7.0
calcprint.py
View file @
c8b70977
from
datetime
import
datetime
def
print_results
(
*
args
,
action
=
None
,
result
=
None
):
def
print_results
(
*
args
,
action
=
None
,
result
=
None
):
"""
"""
Вывод в табличном виде результатов вычислений
Вывод в табличном виде результатов вычислений
...
@@ -18,6 +17,7 @@ def print_results(*args, action=None, result=None):
...
@@ -18,6 +17,7 @@ def print_results(*args, action=None, result=None):
for
i
in
operands
:
for
i
in
operands
:
lst
.
append
(
f
"| {i} |"
)
lst
.
append
(
f
"| {i} |"
)
return
lst
return
lst
def
actionPrint
(
operands
,
action
):
def
actionPrint
(
operands
,
action
):
lst
=
[]
lst
=
[]
for
i
in
list
(
operands
[
0
:
-
1
]):
for
i
in
list
(
operands
[
0
:
-
1
]):
...
@@ -33,6 +33,7 @@ def print_results(*args, action=None, result=None):
...
@@ -33,6 +33,7 @@ def print_results(*args, action=None, result=None):
print
(
'-'
*
len
(
st
))
print
(
'-'
*
len
(
st
))
def
write_log
(
*
args
,
action
=
None
,
result
=
None
,
file
=
'calc-history.log.txt'
):
def
write_log
(
*
args
,
action
=
None
,
result
=
None
,
file
=
'calc-history.log.txt'
):
from
datetime
import
datetime
f
=
open
(
file
,
mode
=
'a'
,
errors
=
'ignore'
)
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
()
f
.
close
()
\ No newline at end of file
calculate.py
View file @
c8b70977
from
calcprint
import
print_results
,
write_log
from
calcprint
import
print_results
,
write_log
import
unittest
import
unittest
PARAMS
=
{
'precision'
:
None
,
PARAMS
=
{
'precision'
:
None
,
'output_type'
:
None
,
'output_type'
:
None
,
'possible_types'
:
None
,
'possible_types'
:
None
'dest'
:
None
}
}
def
load_params
(
file
=
"params.ini"
):
def
load_params
(
file
=
"params.ini"
):
''' Функция загружает параметры вычислений из внешнего файла,
''' Функция загружает параметры вычислений из внешнего файла,
...
@@ -12,7 +14,8 @@ def load_params(file="params.ini"):
...
@@ -12,7 +14,8 @@ def load_params(file="params.ini"):
global
PARAMS
global
PARAMS
try
:
try
:
open
(
file
,
'r'
,
errors
=
'ignore'
)
f
=
open
(
file
,
'r'
,
errors
=
'ignore'
)
f
.
close
()
except
FileNotFoundError
:
except
FileNotFoundError
:
print
(
"Такого файла с параметрами не существует"
)
print
(
"Такого файла с параметрами не существует"
)
else
:
else
:
...
@@ -26,6 +29,7 @@ def load_params(file="params.ini"):
...
@@ -26,6 +29,7 @@ def load_params(file="params.ini"):
param
[
1
]
=
eval
(
param
[
1
])
param
[
1
]
=
eval
(
param
[
1
])
PARAMS
[
param
[
0
]]
=
param
[
1
]
PARAMS
[
param
[
0
]]
=
param
[
1
]
def
convert_precision
(
prec
):
def
convert_precision
(
prec
):
''''
''''
Преобразует точность данную в виде числа с плавающей точкой в целое число, возможное для использования с функцией round.
Преобразует точность данную в виде числа с плавающей точкой в целое число, возможное для использования с функцией round.
...
@@ -36,15 +40,16 @@ def convert_precision(prec):
...
@@ -36,15 +40,16 @@ def convert_precision(prec):
>>> convert_precision('s')
>>> convert_precision('s')
None
None
'''
'''
prec
=
str
(
prec
)
prec
=
str
(
prec
)
for
i
in
prec
:
for
i
in
prec
:
if
i
==
'e'
:
if
i
==
'e'
:
x
=
prec
.
split
(
'-'
)
x
=
prec
.
split
(
'-'
)
return
int
(
x
[
1
])
return
int
(
x
[
1
])
if
i
==
"."
:
if
i
==
"."
:
x
=
prec
.
split
(
'.'
)
x
=
prec
.
split
(
'.'
)
return
int
(
len
(
x
[
1
]))
return
int
(
len
(
x
[
1
]))
def
user_input
():
def
user_input
():
args
=
[]
args
=
[]
...
@@ -55,7 +60,9 @@ def user_input():
...
@@ -55,7 +60,9 @@ def user_input():
break
break
val
=
float
(
val
)
val
=
float
(
val
)
except
ValueError
:
except
ValueError
:
print
(
"Введите число в правильном формате (разделитель дробной части '.') "
)
print
(
"Введите число в правильном формате (разделитель дробной части '.') "
)
else
:
else
:
args
.
append
(
val
)
args
.
append
(
val
)
...
@@ -63,6 +70,7 @@ def user_input():
...
@@ -63,6 +70,7 @@ def user_input():
if
len
(
args
)
<=
1
:
if
len
(
args
)
<=
1
:
return
return
print
(
"Введите один из знаков действия: +, -, *, / "
)
action
=
input
(
"action: "
)
action
=
input
(
"action: "
)
try
:
try
:
...
@@ -72,6 +80,7 @@ def user_input():
...
@@ -72,6 +80,7 @@ def user_input():
else
:
else
:
print_results
(
*
args
,
action
=
action
,
result
=
res
)
print_results
(
*
args
,
action
=
action
,
result
=
res
)
def
calculate
(
*
args
,
action
=
None
,
**
kwargs
):
def
calculate
(
*
args
,
action
=
None
,
**
kwargs
):
''' Главная функция приложения калькулятора, в которой и производятся все вычисления,
''' Главная функция приложения калькулятора, в которой и производятся все вычисления,
а также запись в историю вычислений. Результат приводится к заданному типу данных и округляется до заданного количества знаков
а также запись в историю вычислений. Результат приводится к заданному типу данных и округляется до заданного количества знаков
...
@@ -114,40 +123,12 @@ def calculate(*args, action=None, **kwargs):
...
@@ -114,40 +123,12 @@ def calculate(*args, action=None, **kwargs):
if
type
(
result
)
is
not
output_type
:
if
type
(
result
)
is
not
output_type
:
result
=
output_type
(
result
)
result
=
output_type
(
result
)
result
=
round
(
result
,
precision
)
result
=
round
(
result
,
precision
)
write_log
(
*
args
,
action
=
action
,
result
=
result
)
write_log
(
*
args
,
action
=
action
,
result
=
result
)
return
result
return
result
class
TestCalculator
(
unittest
.
TestCase
):
def
test_summ
(
self
):
self
.
assertEqual
((
calculate
(
*
(
3.2
,
5.4
,
1.0
,
6
),
action
=
"+"
,
**
PARAMS
)),
15.6
)
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_precision
(
self
):
self
.
assertEqual
(
convert_precision
(
0.0000001
),
7
)
self
.
assertEqual
(
convert_precision
(
0.001
),
3
)
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"
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
load_params
()
load_params
()
user_input
()
user_input
()
unittest
.
main
()
params.ini
View file @
c8b70977
precision
=
0.00001
precision
=
0.00001
output_type
=
float
output_type
=
float
possible_types
=
(int, float)
possible_types
=
(int, float)
dest
=
output.txt
\ No newline at end of file
tests.py
0 → 100644
View file @
c8b70977
from
calculate
import
calculate
,
load_params
,
convert_precision
,
PARAMS
import
unittest
if
__name__
==
"__main__"
:
load_params
()
class
TestCalculator
(
unittest
.
TestCase
):
def
test_summ
(
self
):
self
.
assertEqual
(
(
calculate
(
*
(
3.2
,
5.4
,
1.0
,
6
),
action
=
"+"
,
**
PARAMS
)),
15.6
)
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_precision
(
self
):
self
.
assertEqual
(
convert_precision
(
0.0000001
),
7
)
self
.
assertEqual
(
convert_precision
(
0.001
),
3
)
def
test_error
(
self
):
with
self
.
assertRaises
(
Exception
):
calculate
(
*
(
5
,
0
),
action
=
"/"
,
**
PARAMS
)
calculate
(
*
(
'yes'
,
5
),
action
=
"+"
,
**
PARAMS
)
unittest
.
main
()
\ No newline at end of file
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