Cool article, thanks. Haven't tried typing in Python. Could this be changed so that besides escaping, it also enforces that the string must be parsed as an integer? Maybe even within a specific size range?
I think to keep the same spirit, I would abstract it in another layer. I would define a function resize that takes in an int. As for specific range, NewType could also be used the same way we did for strings:
Size=NewType("Size",int)defparse_size(unsafe_size:str)->Size:size=int(size_str)returnvalid_size(size)defvalid_size(unsafe_size:int)->Size:ifsize>2000:raiseValueError("Too big!")returnsize# int as str are always safe for shell!
defshell_int(x:int)->ShellQuotedString:returnstr(x)defresize(size:Size)->None:command=shell_format("convert INPUT -resize {} OUTPUT",shell_int(size))system(command)resize(parse_size("123"))# ok
resize(valid_size(123))# ok
resize(123)# error!
OK, that makes sense thanks. As long as you're strict about only accepting the appropriate typed versions at the command input, enforcement works. I think what is missing is the equivalent of Haskell's enforcing of types everywhere; this is a useful tool, but you have to pay attention to make sure it's used consistently.
Having it enforced everywhere would be more of a challenge I suppose, because there isn't really the equivalent of that compile-time checking in a more dynamic environment.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Cool article, thanks. Haven't tried typing in Python. Could this be changed so that besides escaping, it also enforces that the string must be parsed as an integer? Maybe even within a specific size range?
I think to keep the same spirit, I would abstract it in another layer. I would define a function
resize
that takes in anint
. As for specific range,NewType
could also be used the same way we did for strings:OK, that makes sense thanks. As long as you're strict about only accepting the appropriate typed versions at the command input, enforcement works. I think what is missing is the equivalent of Haskell's enforcing of types everywhere; this is a useful tool, but you have to pay attention to make sure it's used consistently.
Having it enforced everywhere would be more of a challenge I suppose, because there isn't really the equivalent of that compile-time checking in a more dynamic environment.