osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

python 2 to 3 converter


Chris Angelico wrote:
...
>
> Here's an example piece of code.
>
> sock = socket.socket(...)
> name = input("Enter your username: ")
> code = input("Enter the base64 code: ")
> code = base64.b64decode(code)
> sock.write("""GET /foo HTTP/1.0
> Authentication: Demo %s/%s
>
> """ % (name, code))
> match = re.search(r"#[A-Za-z0-9]+#", sock.read())
> if match: print("Response: " + match.group(0))
>
> Your challenge: Figure out which of those strings should be a byte
> string and which should be text. Or alternatively, prove that this is
> a hard problem. There are only a finite number of types - two, to be
> precise - so by your argument, this should be straightforward, right?

  this isn't a process of looking at isolated code.  this
is a process of looking at the code, but also the test cases
or working examples.  so the inputs are known and the code 
itself gives clues about what it is expecting.

  regular expressions can be matched in finite time as well
as a fixed length text of any type can be scanned as a match
or rejected.

  if you examined a thousand uses of match and found the
pattern used above and then examined what those programs did
with that match what would you select as the first type, the
one used the most first, if that doesn't work go with the 2nd,
etc.


  songbird