File tree Expand file tree Collapse file tree 1 file changed +18
-17
lines changed Expand file tree Collapse file tree 1 file changed +18
-17
lines changed Original file line number Diff line number Diff line change 1
1
#!/usr/bin/python3
2
2
3
3
import math
4
+ import sys
4
5
from typing import Generator , Iterable
5
6
6
7
from .. import lib
@@ -107,34 +108,34 @@ def format_exponents(factors: Iterable[int]) -> str:
107
108
108
109
parser .add_option ("-h" , "--exponents" , action = "store_true" )
109
110
111
+
110
112
@lib .command (parser )
111
113
def python_userland_factor (opts , args ):
112
- numbers : list [int ] = []
113
-
114
- for arg in args :
115
- try :
116
- num = int (arg )
117
- if num < 0 :
118
- raise ValueError
119
- except ValueError :
120
- parser .error (f"'{ arg } ' is not a valid positive integer" )
121
-
122
- numbers .append (num )
114
+ failed = False
123
115
124
116
try :
125
- for n in numbers :
126
- if n < 2 :
127
- print (f"{ n } :" )
117
+ for arg in args or lib .readwords_stdin ():
118
+ try :
119
+ num = int (arg )
120
+ if num < 0 :
121
+ raise ValueError
122
+ except ValueError :
123
+ failed = True
124
+ print (f"'{ arg } ' is not a valid positive integer" , file = sys .stderr )
125
+ continue
126
+
127
+ if num < 2 :
128
+ print (f"{ num } :" )
128
129
continue
129
130
130
- factors = sorted (factorize (n ))
131
+ factors = sorted (factorize (num ))
131
132
132
133
print (
133
- f"{ n } : { format_exponents (factors ) if opts .exponents
134
+ f"{ num } : { format_exponents (factors ) if opts .exponents
134
135
else " " .join (map (str , factors ))} "
135
136
)
136
137
except KeyboardInterrupt :
137
138
print ()
138
139
return 130
139
140
140
- return 0
141
+ return int ( failed )
You can’t perform that action at this time.
0 commit comments