diff --git a/hash2b.py b/hash2b.py new file mode 100644 index 00000000..89130b86 --- /dev/null +++ b/hash2b.py @@ -0,0 +1,97 @@ +class Bucket: + def __init__(self, Depth): + self.Local_depth = Depth + self.Bucket_list = [] + + def insert(self,value): + if self.Local_depth == len(self.Bucket_list): + return False + else: + self.Bucket_list.append(value) + return True + + def find(self, value): + for i in self.Bucket_list: + if i[0] == value: + return i[1] + + return False + + def remove(self, value): + for i in self.Bucket_list: + if i[0] == value: + self.Bucket_list.remove(i) + return True + + return False + + def empty(self): + tempList = [] + for i in self.Bucket_list: + tempList.append(i) + self.Bucket_list.remove(i) + + return tempList + + def update_depth(self, newdepth): + self.Local_depth = newdepth + + def show(self): + print(self.Bucket_list) + + class Hash: + def __init__(self, Number_of_elements=1): + + self.Global_depth = 1 + + while (2**self.Global_depth)*self.Global_depth < Number_of_elements: + self.Global_depth += 1 + + self.Buckets = {} + + for i in range(2**self.Global_depth): + self.Buckets[bin(i)] = Bucket(self.Global_depth) + + def insert(self, value, index): + bucket_to_insert = bin(value % (2**self.Global_depth)) + + insertion = self.Buckets[bucket_to_insert].insert([value,index]) + + while not insertion: + self.directory_expansion() + + insertion = self.insert(value,index) + + return True + + def find(self, value): + bucket_to_look_for = bin(value % (2**self.Global_depth)) + return self.Buckets[bucket_to_look_for].find(value) + + def remove(self, value): + + bucket_to_look_for = bin(value % (2**self.Global_depth)) + return self.Buckets[bucket_to_look_for].remove(value) + + def directory_expansion(self): + current_buckets = 2**self.Global_depth + self.Global_depth += 1 + buckets_needed = 2**self.Global_depth + + + for i in range(current_buckets): + self.Buckets[bin(i)].update_depth(self.Global_depth) + + for i in range(current_buckets, buckets_needed): + self.Buckets[bin(i)] = Bucket(self.Global_depth) + + for i in range(current_buckets): + tempList = self.Buckets[bin(i)].empty() + for j in tempList: + self.insert(j[0], j[1]) + + def show(self): + print('\nBuckets: \n') + for i in range(2**self.Global_depth): + print (format(i,'b').zfill(self.Global_depth) , end =" ") + self.Buckets[bin(i)].show() \ No newline at end of file diff --git a/misc1a.py b/misc1a.py new file mode 100644 index 00000000..207b657b --- /dev/null +++ b/misc1a.py @@ -0,0 +1,151 @@ +import operator + +def get_op(op, x, y): + ops = {'>': operator.gt, + '<': operator.lt, + '>=': operator.ge, + '<=': operator.le, + '=': operator.eq, + '!=': operator.ne} + + try: + if ((op == 'between' or op == 'not_between') and type(y) != list ): + raise ValueError(f'Error: \nParameter Value for condition with operator between doesn\'t include range.') + + if (op == 'between'): + return ( x>y[0] and xfloat(y[1]) + return smaller or bigger + return ops[op](x,y) + + except TypeError: + return False + + def split_condition(condition): + ops = {'>=': operator.ge, + '<=': operator.le, + '=': operator.eq, + '>': operator.gt, + '<': operator.lt, + 'between': None} + + condition.strip() + has_NOT_operator = False + if (condition.startswith("not ")): + has_NOT_operator = True + condition = condition[3:] + + for op_key in ops.keys(): + splt=condition.split(op_key) + if len(splt)==2: + left, right = splt[0].strip(), splt[1].strip() + + if right[0] == '"' == right[-1]: + right = right.strip('"') + elif ' ' in right and op_key!='between': + raise ValueError(f'Invalid condition: {condition}\nValue must be enclosed in double quotation marks to include whitespaces.') + if right.find('"') != -1: + raise ValueError(f'Invalid condition: {condition}\nDouble quotation marks are not allowed inside values.') + + if (op_key == 'between'): + + if (right.find('(') == -1 or right.find(')') == -1): + raise ValueError(f'Invalid condition: {condition}\nRange not specified after BETWEEN operator.\nexample: (1,2).') + + right = right.strip('(') + right = right.strip(')') + + if (right.find('(') != -1 or right.find(')') != -1): + raise ValueError(f'Invalid condition: {condition}\nWrong use of Parenthesis marks inside the value.') + Rightsplt = right.split(',') + if len(Rightsplt)==2: + right = [Rightsplt[0].strip(), + Rightsplt[1].strip()] + else: + raise ValueError(f'Invalid condition: {condition}\nWrong input inside the range of BETWEEN operator.') + + if (has_NOT_operator): + reverse_ops = {'>': '<=', + '<': '>=', + '>=': '<', + '<=': '>', + '=': '!=', + 'between': 'not_between'} + op_key = reverse_ops.get(op_key) + + return left, op_key, right + +def reverse_op(op): + ''' + Reverse the operator given + ''' + return { + '>' : '<', + '>=' : '<=', + '<' : '>', + '<=' : '>=', + '=' : '=' + }.get(op) + +def split_complex_conditions(condition): + + split_condition = [] + pointerA = 0 + pointerB = 0 + + while pointerB+5 < len(condition): + if (condition[pointerB:(pointerB+5)] == ' and '): + split_condition.append(condition[pointerA:pointerB]) + split_condition.append('and') + pointerA = pointerB+5 + pointerB = pointerB+4 + elif (condition[pointerB:(pointerB+4)] == ' or '): + split_condition.append(condition[pointerA:pointerB]) + split_condition.append('or') + pointerA = pointerB+4 + pointerB = pointerB+3 + pointerB = pointerB +1 + + split_condition.append(condition[pointerA:].replace('\n','')) + + return split_condition + +def high_priority_conditions(condition): + + high_priority_conditions = [] + + if 'or' not in condition: + for i in condition: + if i != 'and': + high_priority_conditions.append(i) + else: + i = len(condition)-1 + while condition[i-1] != 'or': + if condition[i] != 'and': + high_priority_conditions.append(i) + i = i - 1 + + return high_priority_conditions + + def return_tables_in_join_dict(dict): + left_tables = [] + right_tables = [] + + if isinstance(dict['left'],str): + left_tables.append(dict['left']) + else: + for l in return_tables_in_join_dict(dict['left']): + for j in l: + left_tables.append(j in x) + + if isinstance(dict['right'],str): + right_tables.append(dict['right']) + else: + for x in return_tables_in_join_dict(dict['right']): + for j in i: + right_tables.append(j in l) + + return left_tables,right_tables + \ No newline at end of file diff --git a/misc1b.py b/misc1b.py new file mode 100644 index 00000000..c5d0a84d --- /dev/null +++ b/misc1b.py @@ -0,0 +1,22 @@ +(port == "443" and "https://") or "http://" +#x and y returns y if x is truish, x if x is falsish; a or b, vice versa, returns a if it's truish, otherwise b. +#So if port == "443" is true, this returns the RHS of the and, i.e., "https://". Otherwise, the and is false, so the or gets into play and returns `"http://", its RHS. +#In modern Python, a better way to do translate this old-ish idiom is:" + +"https://" if port == "443" else "http://" + + +#all these are examples, idk if they really work... +def protocol(port): + if port == "443": + if bool("https://"): + return True + elif bool("http://"): + return True + return False + + def protocol(port): + if port == "443": + return True + "https://" + else: + return True + "http://" \ No newline at end of file