commit 90d9816ea275696e242d030a92a72d1b6f63f4ec Author: Elex Date: Sat Feb 25 01:48:44 2023 +0900 2023-02-25 01:48 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..457f44d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.analysis.typeCheckingMode": "basic" +} \ No newline at end of file diff --git a/datatypes.py b/datatypes.py new file mode 100755 index 0000000..e197a6c --- /dev/null +++ b/datatypes.py @@ -0,0 +1,5 @@ +#!/usr/bin/python +table = str.maketrans('aeiou', 'AEIOU') +a = 'i like an apple'.translate(table) + +print(a) diff --git a/file_io.py b/file_io.py new file mode 100755 index 0000000..4d1bf9b --- /dev/null +++ b/file_io.py @@ -0,0 +1,22 @@ +#!/usr/bin/python + +def read_file(file_name): + file = open(file_name, "r") + while True: + line = file.readline() + if not line: + break + print(line) + file.close() + + +def write_file(file_name): + file = open(file_name, "w", encoding="UTF-8") + file.write("Hello,\n") + file.write("There") + file.close() + + +if __name__ == "__main__": + read_file("./file_io.py") + write_file("./test.txt") diff --git a/filewalk.py b/filewalk.py new file mode 100755 index 0000000..db4a230 --- /dev/null +++ b/filewalk.py @@ -0,0 +1,18 @@ +#!/usr/bin/python +import os +from posixpath import dirname + + +def main(rootDir): + for (root, dirs, files) in os.walk(rootDir): + print("-> " + root) + if len(dirs) > 0: + for dirName in dirs: + print(dirName) + if len(files) > 0: + for fileName in files: + print(fileName) + + +if __name__ == "__main__": + main("/home/elex/Workspace/ELEX") diff --git a/flow.py b/flow.py new file mode 100755 index 0000000..c116dfa --- /dev/null +++ b/flow.py @@ -0,0 +1,7 @@ +#!/usr/bin/python +a = 10 +if (a > 0): + print(a) + +for a in reversed(range(5)): + print(a) diff --git a/hello.py b/hello.py new file mode 100755 index 0000000..7d47f22 --- /dev/null +++ b/hello.py @@ -0,0 +1,3 @@ +#!/usr/bin/python +s = 'Hello, World!' +print(s) diff --git a/regex.py b/regex.py new file mode 100755 index 0000000..87842f4 --- /dev/null +++ b/regex.py @@ -0,0 +1,59 @@ +#!/usr/bin/python + +import re + + +def fn_match(): + # 문자열의 전체가 정규식과 일치하는지 판별합니다. + # 일치할 때에는 Match 객체를, 일치하지 않으면 None을 돌려줍니다. + str = "bat" + + pattern = re.compile("[a-z]at") + match = pattern.match(str) + if match: + print("Matches: ", match.group()) + else: + print("Not matches.") + + +def fn_search(): + # 문자열 중에서 정규식과 일치하는 부분을 찾습니다. + # 일치할 때에는 Match 객체를, 일치하지 않으면 None을 돌려줍니다. + str = "I'm the bat man. Where is the rat?" + + pattern = re.compile("[a-z]at") + match = pattern.search(str) + if match: + print("Matches: ", match.group()) + else: + print("Not matches.") + + +def fn_findall(): + # 정규식과 일치되는 모든 문자열을 리스트로 반환합니다. + str = "I'm the bat man. Where is the rat?" + + pattern = re.compile("[a-z]at") + list = pattern.findall(str) + print(list) + + +def main(): + fn_match() + fn_search() + fn_findall() + + +def test_1(): + line = '\tid("com.github.ben-manes.versions") version "0.42.0"' + pattern = re.compile( + 'id\("com.github.ben-manes.versions"\) version "([0-9.]+)"') + match = pattern.search(line) + if match: + line = line.replace(match.group(1), "0.46.0") + print(line) + + +if __name__ == "__main__": + # main() + test_1() diff --git a/switch.py b/switch.py new file mode 100755 index 0000000..28da48d --- /dev/null +++ b/switch.py @@ -0,0 +1,17 @@ +#!/usr/bin/python + +def function_1(): + return 1 + +def function_2(): + return 2 + +def function_3(): + return 3 + +switch = {1:function_1, 2:function_2, 3:function_3} + +val = 2 +function = switch[val] +result = function() +print(result) \ No newline at end of file diff --git a/sys_call.py b/sys_call.py new file mode 100755 index 0000000..35f9c3c --- /dev/null +++ b/sys_call.py @@ -0,0 +1,17 @@ +#!/usr/bin/python +import os +import subprocess + + +def fn_system(): + os.system("ls -al") + + +def fn_call(): + result = subprocess.call(["ls", "-al"]) + print(result) + + +if __name__ == "__main__": + # fn_system() + fn_call()