-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
|
||
from contextlib import closing | ||
import re | ||
import socket | ||
|
||
|
||
def scan_free_port(port: int): | ||
while True: | ||
# If we run out of ports, wrap around. | ||
if port > 60000: | ||
port = 10000 | ||
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: | ||
if sock.connect_ex(('localhost', port)): | ||
return port | ||
port += 1 | ||
|
||
|
||
def read_file(p: str) -> str: | ||
with open(p, encoding='utf-8') as f: | ||
return f.read() | ||
|
||
|
||
def atoi(text): | ||
return int(text) if text.isdigit() else text | ||
|
||
|
||
def natural_keys(text): | ||
return [atoi(c) for c in re.split(r'(\d+)', text)] | ||
|
||
|
||
def strip_comment(line: str) -> str: | ||
"""Returns the content of a line without '#' and ' ' characters | ||
remove leading '#', but preserve '#' that is part of a tag | ||
lesson: | ||
>>> '# #hello '.strip('#').strip() | ||
'#hello' | ||
""" | ||
return line.strip('#').strip() |