<commit_before>"""
this module provides a mechanism to check if a port or a range of ports
are already in use
"""
import socket


def port_in_use(port):
    """ checks if given port is in use."""
    is_open = False
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    if sock.connect_ex(('127.0.0.1', port)) == 0:
        is_open = True
    sock.close()
    return is_open


def ports_in_use(from_port, to_port):
    """returns a set containing ports in use in a given range"""
    # this function uses the same socket instead of allocating
    # a socket for each port
    in_use = set()
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    for port in xrange(from_port, to_port + 1):
        print port
        if sock.connect_ex(('127.0.0.1', port)) == 0:
            in_use.add(port)
    sock.close()
    return in_use<commit_msg>added a new method: available_in_range (lib/ports.py)<commit_after>"""
this module provides a mechanism to check if a port or a range of ports
are already in use or it's free
"""
import socket


def in_used(port):
    """ checks if given port is in use."""
    is_open = False
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    if sock.connect_ex(('127.0.0.1', port)) == 0:
        is_open = True
    sock.close()
    return is_open


def used_in_range(from_port, to_port):
    """returns a set containing ports in use in a given range"""
    # this function uses the same socket instead of allocating
    # a socket for each port
    in_use = set()
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    for port in xrange(from_port, to_port):
        print port
        if sock.connect_ex(('127.0.0.1', port)) == 0:
            in_use.add(port)
    sock.close()
    return in_use


def available_in_range(from_port, to_port):
    """returns a list of available ports in a given range"""
    are_free = set()
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    for port in xrange(from_port, to_port):
        print port
        if not sock.connect_ex(('127.0.0.1', port)) == 0:
            are_free.add(port)
    return are_free
{'fraction_non_alphanumeric': 0.10823529411764705, 'fraction_numerical': 0.016941176470588234, 'mean_word_length': 2.9297597042513863, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 5, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1618721', 'n_tokens_mistral': 716, 'n_tokens_neox': 687, 'n_words': 227}