Skip to content

Commit 34e0252

Browse files
committed
提交文章对应代码
1 parent ca07ead commit 34e0252

File tree

7 files changed

+59
-0
lines changed

7 files changed

+59
-0
lines changed

xuanyuanyulong/2020-08-23-getopt-parse-command-arguments/__init__.py

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sys
2+
3+
4+
for index, command_arg in enumerate(sys.argv[1:]):
5+
if command_arg.startswith('--'):
6+
try:
7+
value = sys.argv[1:][index+1]
8+
if not value.startswith('-'):
9+
print("%s 为长参数,参数值为 %s" % (command_arg, value))
10+
continue
11+
except IndexError:
12+
pass
13+
14+
print("%s 为长参数,无参数值" % command_arg)
15+
16+
elif command_arg.startswith('-'):
17+
try:
18+
value = sys.argv[1:][index+1]
19+
if not value.startswith('-'):
20+
print("%s 为短参数,参数值为 %s" % (command_arg, value))
21+
continue
22+
except IndexError:
23+
pass
24+
25+
print("%s 为短参数,无参数值" % command_arg)
26+
27+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import sys
2+
import getopt
3+
4+
5+
opts, args = getopt.getopt(sys.argv[1:], 'd:t:', ["author=", "country=", "auto"])
6+
7+
print(opts)
8+
print(args)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import sys
2+
3+
print(sys.argv)

xuanyuanyulong/2020-08-24-python-dis-bytecode/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import dis
2+
3+
with open("test_dis.py", "r", encoding="utf-8") as f:
4+
s = f.read()
5+
6+
compile_obj = compile(s, "test_dis.py","exec")
7+
8+
dis.dis(compile_obj)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import dis
2+
3+
4+
s = """
5+
def add(add_1, add_2):
6+
sum_value = add_1 + add_2
7+
8+
print("Hello World!")
9+
10+
import sys
11+
"""
12+
13+
dis.dis(s)

0 commit comments

Comments
 (0)