Skip to content

Commit ede57d6

Browse files
authored
Merge pull request #10 from gjbex/development
Development
2 parents 53b5e09 + b2bcbbd commit ede57d6

File tree

8 files changed

+97
-18
lines changed

8 files changed

+97
-18
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Python for systems rogramming
1+
# Python for systems programming
22

33
GitHub repository for participants of the "Python for systems programming" training.
44
For information on the training, see the website

Diff for: source-code/command-line-arguments/Fire/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ applications with a command line interface.
55

66
## What is it?
77

8-
1. `calculator.py`: Calculator application that implements addition
9-
and multiplication.
8+
1. `calculator.py`/`calculations.py`: Calculator application that
9+
implements addition, substraction, multiplication and division.
1010
1. `sayer.py`: Illustration of grouped commands.
11+
1. `cl_logger.py`/`log_management.py`/`log_operations.py`: logging
12+
from the command line example.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def add(x, y):
2+
return x + y
3+
4+
def sub(x, y):
5+
return x - y
6+
7+
def mult(x, y):
8+
return x*y
9+
10+
def div(x, y):
11+
return x/y
12+
13+
def mod(x, y):
14+
return x % y
+2-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
#!/usr/bin/env python
22
import fire
3-
4-
5-
def add(x, y):
6-
return x + y
7-
8-
def mult(x, y):
9-
return x*y
3+
import calculations
104

115

126
if __name__ == '__main__':
13-
fire.Fire()
7+
fire.Fire(calculations)

Diff for: source-code/command-line-arguments/Fire/cl_logger.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python
2+
3+
import fire
4+
import log_operations
5+
6+
7+
if __name__ == '__main__':
8+
fire.Fire(log_operations)
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import re
2+
3+
4+
def create(file_name):
5+
with open('.cl_logger.txt', 'w') as file:
6+
print(f'file = {file_name}', file=file)
7+
8+
def _get_log_file():
9+
with open('.cl_logger.txt', 'r') as file:
10+
for line in file:
11+
if match := re.match('file = (.+)', line):
12+
return match[1]
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from datetime import datetime as _datetime
2+
from log_management import _get_log_file, create
3+
4+
5+
def _log(level, msg):
6+
time = _datetime.strftime(_datetime.now(), '%Y-%d-%m %H:%M:%S')
7+
with open(_get_log_file(), 'a') as file:
8+
print(f'{time} [{level}]: {msg}', file=file)
9+
10+
def error(msg):
11+
_log('error', msg)
12+
13+
def warning(msg):
14+
_log('warning', msg)
15+
16+
def info(msg):
17+
_log('info', msg)

Diff for: source-code/command-line-arguments/Fire/sayer.py

+39-7
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,62 @@
44

55

66
class Hello:
7+
'''Hello group of commands, there is a to and a everyone
8+
command
9+
'''
710

811
def __init__(self, name):
912
self.name = name
1013

11-
def say(self):
12-
return f'Hello {self.name}'
14+
def to(self, name=None):
15+
if name is None:
16+
if self.name is None:
17+
return 'No one to say hello to'
18+
else:
19+
return f'Hello to {self.name}'
20+
else:
21+
return f'Hello {name}'
22+
23+
def everyone(self):
24+
return 'hello to everyone'
25+
1326

1427
class Bye:
28+
'''Bye group of commands, there is a to and a no_one
29+
command
30+
'''
1531

1632
def __init__(self, name):
1733
self.name = name
1834

19-
def say(self):
20-
return f'Bye {self.name}'
35+
def to(self, name=None):
36+
if name is None:
37+
if self.name is None:
38+
return 'No one to say bye to'
39+
else:
40+
return f'Bye to {self.name}'
41+
else:
42+
return f'Bye {name}'
43+
44+
def no_one(self):
45+
return f'Bye to no one'
2146

2247

2348
class Sayer:
49+
'''Class to make the hello and bye groups available, it
50+
also has its own to command, as well as the info command
51+
'''
2452

25-
def __init__(self, hello_name, bye_name):
53+
def __init__(self, hello_name=None, bye_name=None):
2654
self.hello = Hello(hello_name)
2755
self.bye = Bye(bye_name)
2856

29-
def say(self):
30-
return f'Bla, bla'
57+
def to(self):
58+
return 'Do you want to say hello or bye'
59+
60+
def info(self):
61+
return 'This is version 0.1beta'
62+
3163

3264
if __name__ == '__main__':
3365
fire.Fire(Sayer)

0 commit comments

Comments
 (0)