<commit_before># coding: utf-8
# This file is a part of VK4XMPP transport
# © simpleApps, 2014.

"""
Contains useful functions which used across the modules
"""

import threading
import xmpp
import urllib
from socket import error
from writer import *

isNumber = lambda obj: (not execute(int, (obj,), False) is None)


def execute(handler, list=(), log=True):
	"""
	Just executes handler(*list) safely
	Writes a crashlog if errors occurred
	"""
	try:
		result = handler(*list)
	except (SystemExit, xmpp.NodeProcessed):
		result = True
	except Exception:
		result = None
		if log:
			crashLog(handler.func_name)
			logger.error("Exception happened during executing function: %s%s" % (handler.func_name, str(list)))
	return result


def runThread(func, args=(), name=None, att=3, delay=0):
	"""
	Runs a thread with custom args and name
	Needed to reduce code
	Parameters:
		func: function you need to be running in a thread
		args: function arguments
		name: thread name
		att: number of attempts
		delay: if set, then threading.Timer will be started, not threading.Thread

	"""
	if delay:
		logger.debug("threading: starting timer for %s%s, "
			"name:%s, delay:%s" % (func.func_name, str(args), name, delay))
		thr = threading.Timer(delay, execute, (func, args))
	else:
		thr = threading.Thread(target=execute, args=(func, args))
	name = name or func.__name__
	name = str(name) + "-" + str(time.time())
	thr.name = name
	try:
		thr.start()
	except (threading.ThreadError):
		if att:
			return runThread(func, args, name, (att - 1), delay)
		crashLog("runThread.%s" % name)
	return thr


def safe(func):
	"""
	Executes func(*args) safely
	"""
	def wrapper(*args):
		try:
			func(*args)
		except xmpp.NodeProcessed:
			pass
		except Exception:
			crashLog(func.func_name)
	wrapper.__name__ = func.__name__
	return wrapper


def cache(func):
	"""
	Caches user/group ids for future usage
	"""
	def wrapper(self, uid, fields=None):
		fields = fields or []
		fieldsStr = ",".join(fields)
		if uid in self.cache:
			if self.cache[uid]["fields"] == fieldsStr:
				return self.cache[uid]<commit_msg>remove redundant runThread() call retry<commit_after># coding: utf-8
# This file is a part of VK4XMPP transport
# © simpleApps, 2014.

"""
Contains useful functions which used across the modules
"""

import threading
import xmpp
import urllib
from socket import error
from writer import *

isNumber = lambda obj: (not execute(int, (obj,), False) is None)


def execute(handler, list=(), log=True):
	"""
	Just executes handler(*list) safely
	Writes a crashlog if errors occurred
	"""
	try:
		result = handler(*list)
	except (SystemExit, xmpp.NodeProcessed):
		result = True
	except Exception:
		result = None
		if log:
			crashLog(handler.func_name)
			logger.error("Exception happened during executing function: %s%s" % (handler.func_name, str(list)))
	return result


def runThread(func, args=(), name=None, delay=0):
	"""
	Runs a thread with custom args and name
	Needed to reduce code
	Parameters:
		func: function you need to be running in a thread
		args: function arguments
		name: thread name
		att: number of attempts
		delay: if set, then threading.Timer will be started, not threading.Thread

	"""
	if delay:
		logger.debug("threading: starting timer for %s%s, "
			"name:%s, delay:%s" % (func.func_name, str(args), name, delay))
		thr = threading.Timer(delay, execute, (func, args))
	else:
		thr = threading.Thread(target=execute, args=(func, args))
	name = name or func.__name__
	name = str(name) + "-" + str(time.time())
	thr.name = name
	thr.start()
	return thr


def safe(func):
	"""
	Executes func(*args) safely
	"""
	def wrapper(*args):
		try:
			func(*args)
		except xmpp.NodeProcessed:
			pass
		except Exception:
			crashLog(func.func_name)
	wrapper.__name__ = func.__name__
	return wrapper


def cache(func):
	"""
	Caches user/group ids for future usage
	"""
	def wrapper(self, uid, fields=None):
		fields = fields or []
		fieldsStr = ",".join(fields)
		if uid in self.cache:
			if self.cache[uid]["fields"] == fieldsStr:
				return self.cache[uid]
{'fraction_non_alphanumeric': 0.12564671101256467, 'fraction_numerical': 0.003941857600394186, 'mean_word_length': 4.21181001283697, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1019107', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1351, 'n_words': 440}