Skip to content

Commit 2cc27ef

Browse files
author
shreydan
committed
calculator.py: fixed mod error, new features and code changes
2 parents c444140 + 9d4802e commit 2cc27ef

File tree

2 files changed

+116
-102
lines changed

2 files changed

+116
-102
lines changed

calculator.py

+84-82
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,84 @@
1-
"""
2-
Written by : Shreyas Daniel - github.com/shreydan
3-
Description : Uses Pythons eval() function
4-
as a way to implement calculator.
5-
6-
Functions available:
7-
--------------------------------------------
8-
+ : addition
9-
- : subtraction
10-
* : multiplication
11-
/ : division
12-
% : percentage
13-
e : 2.718281...
14-
pi : 3.141592...
15-
sine : sin(rad)
16-
cosine : cos(rad)
17-
tangent : tan(rad)
18-
square root : sqrt(n)
19-
round to nearest integer : round(n)
20-
convert degrees to radians : rad(deg)
21-
"""
22-
23-
import math
24-
import sys
25-
26-
27-
def calc(k):
28-
29-
k = k.replace(' ', '')
30-
k = k.replace('^', '**')
31-
k = k.replace('=', '')
32-
k = k.replace('?', '')
33-
k = k.replace('%', '/100')
34-
k = k.replace('rad', 'radians')
35-
36-
functions = ['sin', 'cos', 'tan', 'sqrt', 'pi', 'radians', 'e']
37-
38-
for i in functions:
39-
if i in k.lower():
40-
withmath = 'math.' + i
41-
k = k.replace(i, withmath)
42-
43-
try:
44-
k = eval(k)
45-
except ZeroDivisionError:
46-
print("Can't divide by 0")
47-
exit()
48-
except NameError:
49-
print('Invalid input')
50-
exit()
51-
except AttributeError:
52-
print('Check usage method')
53-
exit()
54-
55-
return k
56-
57-
58-
def result(k):
59-
print("\n" + str(calc(k)))
60-
61-
62-
def main():
63-
64-
print("\nScientific Calculator\nEg: sin(rad(90)) + 50% * (sqrt(16)) + round(1.42^2)\nEnter quit to exit")
65-
66-
if sys.version_info.major >= 3:
67-
while True:
68-
k = input("\nWhat is ")
69-
if k == 'quit':
70-
break
71-
result(k)
72-
73-
else:
74-
while True:
75-
k = raw_input("\nWhat is ")
76-
if k == 'quit':
77-
break
78-
result(k)
79-
80-
81-
if __name__ == '__main__':
82-
main()
1+
"""
2+
Written by : Shreyas Daniel - github.com/shreydan
3+
Description : Uses Pythons eval() function
4+
as a way to implement calculator.
5+
6+
Functions available:
7+
--------------------------------------------
8+
+ : addition
9+
- : subtraction
10+
* : multiplication
11+
/ : division
12+
% : percentage
13+
e : 2.718281...
14+
pi : 3.141592...
15+
sine : sin(rad)
16+
cosine : cos(rad)
17+
tangent : tan(rad)
18+
remainder : XmodY
19+
square root : sqrt(n)
20+
round to nearest integer : round(n)
21+
convert degrees to radians : rad(deg)
22+
"""
23+
24+
import math
25+
import sys
26+
27+
28+
def calc(k):
29+
30+
k = k.replace(' ', '')
31+
k = k.replace('^', '**')
32+
k = k.replace('=', '')
33+
k = k.replace('?', '')
34+
k = k.replace('%', '/100')
35+
k = k.replace('rad', 'radians')
36+
k = k.replace('mod', '%')
37+
38+
functions = ['sin', 'cos', 'tan', 'sqrt', 'pi', 'radians', 'e']
39+
40+
for i in functions:
41+
if i in k.lower():
42+
withmath = 'math.' + i
43+
k = k.replace(i, withmath)
44+
45+
try:
46+
k = eval(k)
47+
except ZeroDivisionError:
48+
print("Can't divide by 0")
49+
exit()
50+
except NameError:
51+
print('Invalid input')
52+
exit()
53+
except AttributeError:
54+
print('Check usage method')
55+
exit()
56+
57+
return k
58+
59+
60+
def result(k):
61+
print("\n" + str(calc(k)))
62+
63+
64+
def main():
65+
66+
print("\nScientific Calculator\nEg: sin(rad(90)) + 50% * (sqrt(16)) + round(1.42^2) - 12mod3\nEnter quit to exit")
67+
68+
if sys.version_info.major >= 3:
69+
while True:
70+
k = input("\nWhat is ")
71+
if k == 'quit':
72+
break
73+
result(k)
74+
75+
else:
76+
while True:
77+
k = raw_input("\nWhat is ")
78+
if k == 'quit':
79+
break
80+
result(k)
81+
82+
83+
if __name__ == '__main__':
84+
main()

fileinfo.py

+32-20
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Script Name : fileinfo.py
2-
# Author : Not sure where I got this from
3-
# Created : 28th November 2011
4-
# Last Modified :
5-
# Version : 1.0
6-
# Modifications :
1+
# Script Name : fileinfo.py
2+
# Author : Not sure where I got this from
3+
# Created : 28th November 2011
4+
# Last Modified :
5+
# Version : 1.0
6+
# Modifications :
77

8-
# Description : Show file information for a given file
8+
# Description : Show file information for a given file
99

1010

1111
# get file information using os.stat()
@@ -19,10 +19,18 @@
1919
try_count = 16
2020

2121
while try_count:
22-
file_name = raw_input("Enter a file name: ") # pick a file you have
22+
file_name = input("Enter a file name: ") # pick a file you have
23+
fhand = open(file_name)
24+
count = 0
25+
for lines in fhand:
26+
count = count + 1
27+
fhand = open(file_name)
28+
inp = fhand.read()
29+
t_char = len(inp)
2330
try_count >>= 1
2431
try:
2532
file_stats = os.stat(file_name)
33+
print ("This is os.stat",file_stats)
2634
break
2735
except OSError:
2836
print ("\nNameError : [%s] No such file or directory\n", file_name)
@@ -40,25 +48,29 @@
4048
'f_la' : time.strftime("%d/%m/%Y %I:%M:%S %p",
4149
time.localtime(file_stats[stat.ST_ATIME])),
4250
'f_ct' : time.strftime("%d/%m/%Y %I:%M:%S %p",
43-
time.localtime(file_stats[stat.ST_CTIME]))
51+
time.localtime(file_stats[stat.ST_CTIME])),
52+
'no_of_lines':count,
53+
't_char':t_char
4454
}
4555

46-
print ("\nfile name = %(fname)s", file_info)
47-
print ("file size = %(fsize)s bytes", file_info)
48-
print ("last modified = %(f_lm)s", file_info)
49-
print ("last accessed = %(f_la)s", file_info)
50-
print ("creation time = %(f_ct)s\n", file_info)
56+
print ("\nfile name =", file_info['fname'])
57+
print ("file size =", file_info['fsize'] , "bytes")
58+
print ("last modified =", file_info['f_lm'])
59+
print ("last accessed =", file_info['f_la'])
60+
print ("creation time =", file_info['f_ct'])
61+
print ("Total number of lines are =", file_info['no_of_lines'])
62+
print ("Total number of characters are =", file_info['t_char'])
5163

5264
if stat.S_ISDIR(file_stats[stat.ST_MODE]):
5365
print ("This a directory")
5466
else:
5567
print ("This is not a directory\n")
5668
print ("A closer look at the os.stat(%s) tuple:" % file_name)
5769
print (file_stats)
58-
print ("\nThe above tuple has the following sequence:")
59-
print ("""st_mode (protection bits), st_ino (inode number),
60-
st_dev (device), st_nlink (number of hard links),
61-
st_uid (user ID of owner), st_gid (group ID of owner),
62-
st_size (file size, bytes), st_atime (last access time, seconds since epoch),
63-
st_mtime (last modification time), st_ctime (time of creation, Windows)"""
70+
print ("\nThe above tuple has the following sequence: ")
71+
print ("""st_mode (protection bits), st_ino (inode number),
72+
st_dev (device), st_nlink (number of hard links),
73+
st_uid (user ID of owner), st_gid (group ID of owner),
74+
st_size (file size, bytes), st_atime (last access time, seconds since epoch),
75+
st_mtime (last modification time), st_ctime (time of creation, Windows)"""
6476
)

0 commit comments

Comments
 (0)