<commit_before># encoding: utf-8
"""
ip/__init__.py

Created by <NAME> on 2010-01-15.
Copyright (c) 2009-2015 Exa Networks. All rights reserved.
"""

import socket

from exabgp.protocol.family import AFI
from exabgp.protocol.family import SAFI

# =========================================================================== IP
#


class IP (object):
	afi = None  # here for the API
	_known = dict()

	_UNICAST = SAFI(SAFI.unicast)
	_MULTICAST = SAFI(SAFI.multicast)

	_multicast_range = set(range(224,240))  # 239

	__slots__ = ['ip','packed']

	def __init__ (self):
		raise Exception("You should use IP.create() to use IP")

	def init (self, ip, packed):
		self.ip = ip
		self.packed = packed
		return self

	@staticmethod
	def pton (ip):
		return socket.inet_pton(IP.toaf(ip),ip)

	@staticmethod
	def ntop (data):
		return socket.inet_ntop(socket.AF_INET if len(data) == 4 else socket.AF_INET6,data)

	@staticmethod
	def toaf (ip):
		# the orders matters as fc00:e968:6179::de52:7100:<ipv4> is an IPv6 address
		if ':' in ip:
			return socket.AF_INET6
		if '.' in ip:
			return socket.AF_INET
		raise Exception('unrecognised ip address %s' % ip)

	@staticmethod
	def toafi (ip):
		# the orders matters as fc00:e968:6179::de52:7100:<ipv4> is an IPv6 address
		if ':' in ip:
			return AFI.ipv6
		if '.' in ip:
			return AFI.ipv4
		raise Exception('unrecognised ip address %s' % ip)

	@staticmethod
	def tosafi (ip):
		if ':' in ip:
			# XXX: FIXME: I assume that fc00:e968:6179::de52:7100:<ip> must be treated unicast
			# if int(ip.split(':')[-1].split('.')[0]) in IP._multicast_range:
			return SAFI.unicast
		elif '.' in ip:
			if int(ip.split('.')[0]) in IP._multicast_range:
				return SAFI.multicast
			return SAFI.unicast
		raise Exception('unrecognised ip address %s' % ip)

	def ipv4 (self):
		return True if len(self.packed) == 4 else False

	def ipv6 (self):
		return False if len(self.packed) == 4 else True

	@staticmethod
	def length (afi):
		return 4 if afi == AFI.ipv4 else 16

	def pack (self):
		return self.packed

	def __str__ (self):
		return self.ip

	def __repr__ (self):
		return str(self)

	def __cmp__ (self, other):
		if not isinstance(other, self.__class__):
			return -1
		if self.packed == other.packed:
			return 0
		if self.packed < other.packed:
			return -1
		return 1

	def __hash__ (self):
		return hash(str(self.__class__.__name__) + self.packed)

	@classmethod
	def klass (cls, ip):
		# the orders matters as fc00:e968:6179::de52:7100:<ipv4> is an IPv6 address
		if ':' in ip:
			afi = IPv6.afi
		elif '.' in ip:
			afi = IPv4.afi
		else:
			raise Exception('can not decode this ip address : %s' % ip)
		if afi in cls._known:
			return cls._known[afi]<commit_msg>IP: keep only __repr__ , __str__ not needed if __repr__ exists and does the same<commit_after># encoding: utf-8
"""
ip/__init__.py

Created by <NAME> on 2010-01-15.
Copyright (c) 2009-2015 Exa Networks. All rights reserved.
"""

import socket

from exabgp.protocol.family import AFI
from exabgp.protocol.family import SAFI

# =========================================================================== IP
#


class IP (object):
	afi = None  # here for the API
	_known = dict()

	_UNICAST = SAFI(SAFI.unicast)
	_MULTICAST = SAFI(SAFI.multicast)

	_multicast_range = set(range(224,240))  # 239

	__slots__ = ['ip','packed']

	def __init__ (self):
		raise Exception("You should use IP.create() to use IP")

	def init (self, ip, packed):
		self.ip = ip
		self.packed = packed
		return self

	@staticmethod
	def pton (ip):
		return socket.inet_pton(IP.toaf(ip),ip)

	@staticmethod
	def ntop (data):
		return socket.inet_ntop(socket.AF_INET if len(data) == 4 else socket.AF_INET6,data)

	@staticmethod
	def toaf (ip):
		# the orders matters as fc00:e968:6179::de52:7100:<ipv4> is an IPv6 address
		if ':' in ip:
			return socket.AF_INET6
		if '.' in ip:
			return socket.AF_INET
		raise Exception('unrecognised ip address %s' % ip)

	@staticmethod
	def toafi (ip):
		# the orders matters as fc00:e968:6179::de52:7100:<ipv4> is an IPv6 address
		if ':' in ip:
			return AFI.ipv6
		if '.' in ip:
			return AFI.ipv4
		raise Exception('unrecognised ip address %s' % ip)

	@staticmethod
	def tosafi (ip):
		if ':' in ip:
			# XXX: FIXME: I assume that fc00:e968:6179::de52:7100:<ip> must be treated unicast
			# if int(ip.split(':')[-1].split('.')[0]) in IP._multicast_range:
			return SAFI.unicast
		elif '.' in ip:
			if int(ip.split('.')[0]) in IP._multicast_range:
				return SAFI.multicast
			return SAFI.unicast
		raise Exception('unrecognised ip address %s' % ip)

	def ipv4 (self):
		return True if len(self.packed) == 4 else False

	def ipv6 (self):
		return False if len(self.packed) == 4 else True

	@staticmethod
	def length (afi):
		return 4 if afi == AFI.ipv4 else 16

	def pack (self):
		return self.packed

	def __repr__ (self):
		return self.ip

	def __cmp__ (self, other):
		if not isinstance(other, self.__class__):
			return -1
		if self.packed == other.packed:
			return 0
		if self.packed < other.packed:
			return -1
		return 1

	def __hash__ (self):
		return hash(str(self.__class__.__name__) + self.packed)

	@classmethod
	def klass (cls, ip):
		# the orders matters as fc00:e968:6179::de52:7100:<ipv4> is an IPv6 address
		if ':' in ip:
			afi = IPv6.afi
		elif '.' in ip:
			afi = IPv4.afi
		else:
			raise Exception('can not decode this ip address : %s' % ip)
		if afi in cls._known:
			return cls._known[afi]
{'fraction_non_alphanumeric': 0.15057132325838554, 'fraction_numerical': 0.04201990416513085, 'mean_word_length': 3.694636678200692, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '650472', 'n_tokens_mistral': 2309, 'n_tokens_neox': 2042, 'n_words': 605}