added
This commit is contained in:
75
Mittwoch/User_modify.py
Normal file
75
Mittwoch/User_modify.py
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
#! /usr/bin/env python3
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
|
from argv import argv_value
|
||||||
|
|
||||||
|
|
||||||
|
def read_file(path: str) -> "List of lines":
|
||||||
|
#f = open(path, 'r')
|
||||||
|
with open(path, 'r') as f:
|
||||||
|
# f.read(buflen) - binary read
|
||||||
|
# f.readline() - 1 Textzeile lesen -> str
|
||||||
|
# f.readlines - alle Textzeilen lesen ->str
|
||||||
|
# f ist gleichzeitig Iterator
|
||||||
|
# 1 möglichkeit
|
||||||
|
#lines = list(map(lambda s : s.rstrip(), f))
|
||||||
|
# 2 möflichkeit
|
||||||
|
lines = list(map(str.rstrip, f))
|
||||||
|
# 3 möglichkeit
|
||||||
|
#lines = []
|
||||||
|
#for line in f.readlines():
|
||||||
|
# lines.append(line.rstrip())
|
||||||
|
#f.close()
|
||||||
|
return lines
|
||||||
|
|
||||||
|
def parse_passwd_lines(line: str) -> "Dict of passwd details":
|
||||||
|
parts = line.split(':')
|
||||||
|
userdict = {
|
||||||
|
"username" : parts[0],
|
||||||
|
"uid" : int(parts[2]),
|
||||||
|
"gid" : int(parts[3]),
|
||||||
|
"realname" : parts[4].split(',')[0],
|
||||||
|
"gecos" : parts[4],
|
||||||
|
"home" : parts[5],
|
||||||
|
"shell": parts[6]
|
||||||
|
}
|
||||||
|
return userdict
|
||||||
|
|
||||||
|
def build_userlist(lines) -> "List of user dicts":
|
||||||
|
result = []
|
||||||
|
for line in lines:
|
||||||
|
result.append(parse_passwd_lines(line))
|
||||||
|
return result
|
||||||
|
|
||||||
|
def print_userlist_sorted_by_username(userlist):
|
||||||
|
#ruft für jedes Element key(elem) also e im Lambda-Ausdruck ist ein Element der Liste user dict
|
||||||
|
for user in sorted(userlist, key=lambda e: e['username'].lower()):
|
||||||
|
#print("{username:{width}} {realname}".format(width=32 **user))
|
||||||
|
print("{:5} {:32} {}".format(
|
||||||
|
user['uid'],
|
||||||
|
user['username'],
|
||||||
|
user['realname'],
|
||||||
|
user['home'],
|
||||||
|
user['shell']
|
||||||
|
))
|
||||||
|
|
||||||
|
def print_userlist_sorted_by_uid(userlist):
|
||||||
|
userlen = max(map(lambda u: len(u['username']), userlist))
|
||||||
|
for user in sorted(userlist, key=lambda e: e['uid']):
|
||||||
|
print("{uid:5} {username:{width}} {realname}".format(width=userlen, **user))
|
||||||
|
|
||||||
|
def main():
|
||||||
|
output_functions = {
|
||||||
|
'pprint': pprint,
|
||||||
|
'username': print_userlist_sorted_by_username,
|
||||||
|
'uid': print_userlist_sorted_by_uid,
|
||||||
|
|
||||||
|
}
|
||||||
|
#outfunc = output_functions['username']
|
||||||
|
outfunc = output_functions[argv_value('-o', default='uid')]
|
||||||
|
lines = read_file("/etc/passwd")
|
||||||
|
userlist = build_userlist(lines)
|
||||||
|
outfunc(userlist)
|
||||||
|
|
||||||
|
|
||||||
|
main()
|
||||||
BIN
Mittwoch/__pycache__/argv.cpython-39.pyc
Normal file
BIN
Mittwoch/__pycache__/argv.cpython-39.pyc
Normal file
Binary file not shown.
12
Mittwoch/argv.py
Normal file
12
Mittwoch/argv.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#! /usr/bin/env python3
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def argv_value(param, default=None):
|
||||||
|
idx = 1
|
||||||
|
while idx < len(sys.argv):
|
||||||
|
if sys.argv[idx] == param:
|
||||||
|
if idx+1 < len(sys.argv) and not sys.argv[idx+1].startswith('-'):
|
||||||
|
return sys.argv[idx+1]
|
||||||
|
idx += 1
|
||||||
|
|
||||||
|
return default
|
||||||
18
Mittwoch/command.py
Normal file
18
Mittwoch/command.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#! /usr/bin/env python3
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def argv_value(param, default=None):
|
||||||
|
idx = 1
|
||||||
|
while idx < len(sys.argv):
|
||||||
|
if sys.argv[idx] == param:
|
||||||
|
if idx+1 < len(sys.argv) and not sys.argv[idx+1].startswith('-'):
|
||||||
|
return sys.argv[idx+1]
|
||||||
|
idx += 1
|
||||||
|
|
||||||
|
return default
|
||||||
|
|
||||||
|
print("Aktuelles Program : ", sys.argv[0:])
|
||||||
|
print("Kommandozeile : ", sys.argv[1:])
|
||||||
|
print("Kommandozeile : ", sys.argv[1:])
|
||||||
|
|
||||||
|
print("Kommandozeile : ", argv_value('-o'))
|
||||||
8
Mittwoch/import.py
Normal file
8
Mittwoch/import.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#! /usr/bin/env python3
|
||||||
|
from pprint import pprint
|
||||||
|
import sys as pysys
|
||||||
|
|
||||||
|
data = dict(userlist = ["nutzer01", "nutzer02", "nutzer03", "nutzer04"],
|
||||||
|
loccation = ["Linuxhotel"]
|
||||||
|
)
|
||||||
|
pprint(data)
|
||||||
17
Readme.md
17
Readme.md
@@ -17,4 +17,19 @@ rstrip schneidet \n ab
|
|||||||
|
|
||||||
schneidet hinten was ab bei einem String
|
schneidet hinten was ab bei einem String
|
||||||
|
|
||||||
list
|
Lambda meistens mit List verwenden wenn ich eine Liste übergebe.
|
||||||
|
|
||||||
|
dir und help geben dir Hilfestellung
|
||||||
|
|
||||||
|
dir("abc")
|
||||||
|
help("abc".isupper)
|
||||||
|
|
||||||
|
Stringformat :
|
||||||
|
|
||||||
|
{}, {1}, {name} -> nächste Index
|
||||||
|
{1:13} -> 13 Zeichen breit und 1 Hoch
|
||||||
|
{1 :< 13 }, {1:>13}, {1:^13} verschiebt die Ausgabe
|
||||||
|
{0:>..} -> s=string, d=dict, x=Hexa, X=Oktal, b=binar, f=float
|
||||||
|
{0:->13} 0x{1:0>4b}".format("abc", 23) -> Beispiel oder "{0:->{width}} 0x{1:0>4b}".format("abc", 23, width=13)
|
||||||
|
|
||||||
|
pydoc3 zeigt die Helps an Beispiel pydoc3 sys
|
||||||
Reference in New Issue
Block a user