def create_market_view(self, market, submission=None, wiki=False, viewtype="submission"): "Create a reddit message to represent the current market. Returns the formatted text." # reply_text = "**ID**: #{} {} \n".format(market.id, " \n**Submission**:{}".format(submission.permalink) if submission else "") reply_text = "**ID**: #{} ".format(market.id) if market.id else "" reply_text += "**Submission**: {} \n".format( submission.permalink) if submission else "" reply_text += "**Wiki**: /r/{}/wiki/{}/{} \n".format( hub_subreddit, market.category.short, market.id) if wiki and market.id else "" reply_text += "{}**{}** \n\n".format( "**This market is CLOSED!** \n" if not market.is_open else "", market.text.lstrip()) reply_text += "Label|Option|Cost|Volume|Cost of 5|Cost of 25| Cost of 100\n" reply_text += " --:|:-- |:-- | --:| --:| --:| --:\n" label = iter(ascii_lowercase.upper()) sorted_stocks = sorted(market.stocks, key=lambda s: -1 * s.cost) for o in sorted_stocks: label = ascii_lowercase.upper()[market.stocks.index(o)] reply_text += "|**{}**|{}|{:,.2f}|{}|${:,.2f}|${:,.2f}|${:,.2f}|\n".format( label, o.text, o.cost, o.num_shares, market._find_total_cost(o, 5), market._find_total_cost(o, 25), market._find_total_cost(o, 100)) reply_text += "\n**b Value**: {} \n**Category**: {} \n".format( market.b, market.category if not hasattr(market.category, "long") else market.category.long) #if not submission and not wiki: # if this comment IS a submission (submission wont link to itself) if viewtype == "submission": # if not submission: reply_text += "To buy seven shares of option A (reply directly to this):\n\n buy A 5\n" reply_text += "To sell three shares of option B:\n\n sell B 3\n" reply_text += "To buy $200 dollars worth of shares of option C:\n\n buy $200 C\n\n" reply_text += "For each correct share, you will get $100 \nFor each wrong share, you get nothing! \n" reply_text += "All redditors get $5000 by default! \n" reply_text += "**Disclaimer**: This is fake money; you can't get free cash that easily!\n" # if this is a comment elif viewtype == "comment": reply_text += "[How to play.](https://i.imgur.com/SatSnjJ.png) \n" #if submission or wiki: # reply_text += self.footer #TODO: implement this, but in submission only. imgur only for comments #reply_text += self.footer return reply_text
def hack(message): letterGoodness = LETTER_GOODNESS.copy() #First, get frequency of letters in message freq = {letter: 0 for letter in alphabet} for letter in message: if letter.isalpha(): freq[letter.lower()] += 1 #Then use that information to construct a key key = [" "] * 26 for x in range(26): maxG = max(letterGoodness.values()) maxF = max(freq.values()) theLetter = getkey(freq, maxF) keyLetter = getkey(letterGoodness, maxG) key[alphabet.index(theLetter)] = keyLetter letterGoodness.pop(keyLetter) freq.pop(theLetter) #Turn the key into a usable format outkey = "" for letter in key: outkey += letter outkey = decode(alphabet.upper(), outkey.upper()) #And try to decode the message with that key Interface.setKey(outkey) return encode(message, outkey)
def plotFreq(ciph): ciph = Format.keepOnly(ciph.lower(), ALPH) fig, ax = plt.subplots(figsize=(10, 5)) barwidth = 0.3 lettcounts = [FreqAnalysis.englishProbabilities.get(x, 0) for x in ALPH] ciphprobs = FreqAnalysis.getFrequencies(ciph) lettplot = [] for x in range(26): lettplot.append(x - (barwidth / 2)) ax.bar(lettplot, lettcounts, width=barwidth, label="English", color="r") try: ciphcounts = [ciphprobs.get(x, 0) / len(ciph) for x in ALPH] ciphplot = [] for x in range(26): ciphplot.append(x + (barwidth / 2)) ax.bar(ciphplot, ciphcounts, width=barwidth, label="Cipher Text", color="b") except ZeroDivisionError: pass ax.get_yaxis().set_visible(False) ax.set_xticks(range(26)) ax.set_xticklabels(ALPH.upper()) ax.legend() output = io.BytesIO() FigureCanvas(fig).print_png(output) return Response(output.getvalue(), mimetype="image/png")
def __init__(self,config,pipe=None,lock=None): super().__init__(config=config,pipe=pipe,lock=lock) self.ID = ("crypto","caesar") self._REGEX = rb"([ -~]+)" # Private attributes self.__LOWER = ascii_lowercase self.__UPPER = ascii_lowercase.upper()
def main(): polymer = open(input_file).readline().strip() # part 1 print len(react_polymer(polymer)), len(reduce(polymer_reduction, polymer)) # part 2 pairs = [set(p) for p in zip(ascii_lowercase, ascii_lowercase.upper())] print min(len(react_polymer(filter_chars(polymer, p))) for p in pairs)
def generate_password(length="12", withCapitalLetters=None, withNumbers=None, withSymbols=None, excludeSimilarCharacters=None): letters = set(ascii_lowercase) capital_letters = set(ascii_lowercase.upper()) if withCapitalLetters is not None else set() numbers = set(map(str, range(10))) if withNumbers is not None else set() symbols = set("!@#$%^&*()") if withSymbols is not None else set() similar_characters = set("iloILO10") if excludeSimilarCharacters is not None else set() allowed_characters = list((letters | capital_letters | numbers | symbols) - similar_characters) return "".join(choice(allowed_characters) for _ in range(int(length)))
def encrypt(offset, text): cipher = '' for i in text: if i not in ascii_lowercase and i not in ascii_lowercase.upper(): cipher += i continue tmp1 = ord(i.lower()) - ord('a') + offset ans = chr(ord('a') + tmp1 % 26) if i.isupper(): cipher += ans.upper() continue cipher += ans return cipher
def password(req): characters = list(alphabet) length = int(req.GET.get('length', 12)) thepassword = '' if req.GET.get('uppercase'): characters.extend(list(alphabet.upper())) if req.GET.get('special'): characters.extend('~!@#$%^&*<>?') if req.GET.get('numbers'): characters.extend('0123456789') for x in range(length): thepassword += random.choice(characters) return render(req, 'generator/password.html', {'password': thepassword})
def get_num(s): new_s = (list(map(lambda x: x.encode('unicode_escape'), s))) result = '' ignore_key = ascii_lowercase + ascii_lowercase.upper() + '0123456789' + '.' for i in new_s: i = i.decode() if i in ignore_key: result += i else: for fontsc_data in [fontscn_3jqwe90k, fontscn_h57yip2q, fontscn_qw2f1m1o, fontscn_yuh4hy4p, fontscn_yx77i032]: if i in fontsc_data: i = fontsc_data[i] break result += i return result
def keyFromWord(word=None): #Still Buggy? if not word: word, ok = QInputDialog.getText(None, "Key from Keyword or Phrase", "Enter a keyword or phrase") if not ok: return word = word.upper() word = removeNonAlpha(word) word = removeDuplicates(word) # now append any unused letters for letter in alphabet.upper(): if not letter in word: word += letter return word
def solution(str1, str2): # str1의 집합 o_list = [] # str2의 집합 t_list = [] # 대소문자 같은것으로 보기때문에 대문자로 통일 STR1 = str1.upper() STR2 = str2.upper() # 소문자로 저장되니까 대문자로 저장 alpha_list = list(ascii_lowercase.upper()) # for 돌면서 알파벳인것만 두개씩 묶어서 저장 for i in range(len(str1)-1): # FRANCE라치면 C까지만 돌면되니까 -1 if STR1[i] in alpha_list and STR1[i+1] in alpha_list: # 두개씩 묶어서 저장 o_list.append((STR1[i]+STR1[i+1])) for j in range(len(str2)-1): if STR2[j] in alpha_list and STR2[j+1] in alpha_list: t_list.append((STR2[j]+STR2[j+1]).upper()) # 교집합을 구하기 위해 결국 dict 투입 o_dict = {} t_dict = {} union = 0 intersection = {} # o_list와 t_list를 딕셔너리로 만들고 for a in o_list: if a in o_dict: o_dict[a] += 1 else: o_dict[a] = 1 for b in t_list: if b in t_dict: t_dict[b] += 1 else: t_dict[b] = 1 # o_dict의 key,val를 불러와서 key값이 t_dict에 있으면 intersection(교집합)의 key 값으로 둘중 작은값을..넣어준다. for key,val in o_dict.items(): if key in t_dict: intersection[key] = min(val,t_dict[key]) # 교집합의 value들을 더해주면 그게 교집합의 크기 inter = sum(intersection.values()) union = len(o_list) + len(t_list) - inter if len(o_list) == 0 and len(t_list) == 0: J = 1 else: J = inter / union return int(J*65536)
def solution(name): answer = 0 alpha = list(ascii_lowercase.upper()) n = len(name) side_cnt = n - 1 for i in name: idx = alpha.index(i) if idx > 13: answer += 26 - idx else: answer += idx for c in range(n): _next = c + 1 while _next < n and name[_next] == 'A': _next += 1 side_cnt = min(side_cnt, c + n - _next + min(c, n-_next)) answer += side_cnt return answer
def f(U, alphaString, FD_Store): # Change format of FDs so they can be passed through dbschema FD_Str = ",".join(["".join([str(k[0]), "->", str(k[1])]) for k in FD_Store]) # Follow path given in dbschema to obtain keys with FDs dbschemaNotation = (alphaString, FD_Str) (attrastxt, abhastxt) = dbschemaNotation (attrs,abhh) = dbschema.ScanAttrAbh(attrastxt, abhastxt) abhh=dbschema.mincoverage(abhh) (primattr, keys) = dbschema.keysTreeAlg(attrs, abhh, 2) KeyList = list(map(dbschema.attr2str, keys)) # Create column_dict Column_Dict = {ascii_lowercase.upper()[i]: U[i] for i in list(range(len(U)))} # Reformat key list KeyList = [str("{" + ", ".join([Column_Dict[char] for char in KeyList[k]]) + "}") for k in range(len(KeyList))] return KeyList;
def build_dataset(folder_name: str = '.\\goethe') -> (list, dict): dst_raw = [] dct = {} for j, c in enumerate(ascii_lowercase + ascii_lowercase.upper() + ''.join([str(n) for n in range(10)])): dct[j] = c for root, dirs, files in os.walk(folder_name): for f_name in files: with open(os.path.join(folder_name, f_name), 'r') as f: cont = ''.join(f.readlines()) cont_chars = list(cont) while len(cont_chars) > 0: if not (cont_chars[0] in dct.values()): dct[list(dct.keys())[-1] + 1] = cont_chars[0] cont_chars = list( filter(lambda x: x != cont_chars[0], cont_chars)) dst_raw.append(cont) dst = np.array([]) for ex in dst_raw: ex_arr = np.zeros((len(ex), len(dct))) for j, c in enumerate(ex): for k, v in dct.items(): if v == c: ex_arr[j, k] = 1 try: np.append(dst, ex_arr, axis=1) except ValueError: dst = ex_arr return dst, dct
def test_encrypt_and_decrypt(self): def encrypt_and_decrypt(inputs): encrypted = [viginere_encrypt(i) for i in inputs] decrypted = [viginere_decrypt(i) for i in encrypted] return decrypted trivialinputs = [ "", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", "Basic text", alphabet, alphabet.upper() ] trivialoutputs = encrypt_and_decrypt(trivialinputs) trivialinputs = [i.replace(' ', '').lower() for i in trivialinputs] for i, j in zip(trivialinputs, trivialoutputs): self.assertEqual(i, j) canyoureplacebasicinput = [ "!@#$%^&*()_+", "0123456789", "<,>.?:\"'\t\n\r" ] output = encrypt_and_decrypt(canyoureplacebasicinput) for i in output: self.assertEqual('', i) def gen_random_string(): random_string = '' for _ in range(10000): random_string += random.choice(alphabet) return random_string bunch_of_randoms = [gen_random_string() for _ in range(10)] output = encrypt_and_decrypt(bunch_of_randoms) for i, j in zip(bunch_of_randoms, output): self.assertEqual(i, j)
@author: robert-karl """ from string import ascii_lowercase as letters Nlett = len(letters) Nshift = 2 #The size of the shift for the cypher request = "What would you like to encrypt?\n" secret = input(request) # This will store the encrypted message revealed = '' '''#First a way from a little googling for char in secret: if char in letters: # append the shifted letter to the revealed message revealed += letters[(letters.index(char)+Nshift)%Nlett] else: revealed += char ''' # After more google, here is a faster way lettersShift = letters[Nshift:] + letters[0:Nshift] revealed = secret.translate(secret.maketrans(letters, lettersShift)) # Now to get the uppercase letters also revealed = revealed.translate( revealed.maketrans(letters.upper(), lettersShift.upper())) # display the message print('{}\nbecomes\n{}'.format(secret, revealed)) # Let the user close when finished. closing = 'Press <ENTER> to close.' input(closing)
from string import ascii_lowercase as az n = input() s = input() k = int(input()) % 26 r = az[k:] + az[:k] trans = str.maketrans(az + az.upper(), r + r.upper()) print(s.translate(trans))
from string import ascii_lowercase, digits ################################## StringName = u'PyJsConstantString%d_' NumberName = u'PyJsConstantNumber%d_' RegExpName = u'PyJsConstantRegExp%d_' ################################## ALPHAS = set(ascii_lowercase + ascii_lowercase.upper()) NUMS = set(digits) IDENTIFIER_START = ALPHAS.union(NUMS) ESCAPE_CHARS = {'n', '0', 'b', 'f', 'r', 't', 'v', '"', "'", '\\'} OCTAL = {'0', '1', '2', '3', '4', '5', '6', '7'} HEX = set('0123456789abcdefABCDEF') from utils import * IDENTIFIER_PART = IDENTIFIER_PART.union({'.'}) def _is_cancelled(source, n): cancelled = False k = 0 while True: k += 1 if source[n - k] != '\\': break cancelled = not cancelled return cancelled def _ensure_regexp(source, n): #<- this function has to be improved '''returns True if regexp starts at n else returns False checks whether it is not a division ''' markers = '(+~"\'=[%:?!*^|&-,;/\\'
def findMissing(self, key): for letter in alphabet.upper(): if letter not in key: return letter
def test_uppercase(i): assert ascii_uppercase[i] == ascii_lowercase.upper()[i]
TRAIN_DATA_SCHEMA = dataset_schema.from_feature_spec({ 'id': tf.FixedLenFeature(shape=[], dtype=tf.float32), 'text': tf.FixedLenFeature(shape=[], dtype=tf.string), 'labels': tf.FixedLenFeature(shape=[MAX_SENTENCE_LEN], dtype=tf.int64), 'label': tf.FixedLenFeature(shape=[], dtype=tf.string), "chars" : tf.FixedLenFeature(shape=[MAX_SENTENCE_LEN,MAX_WORD_LEN], dtype=tf.int64), 'label_length': tf.FixedLenFeature(shape=[], dtype=tf.int64), 'sentence_length': tf.FixedLenFeature(shape=[], dtype=tf.int64), 'chars_in_word': tf.FixedLenFeature(shape=[MAX_SENTENCE_LEN], dtype=tf.int64), }) train_metadata = dataset_metadata.DatasetMetadata(schema=TRAIN_DATA_SCHEMA) OUTPUT_FILE_TRAIN="train_records.tfrecords" MAPPING = {a:index for index,a in enumerate(ascii_lowercase + ascii_lowercase.upper())} UNKONWN = len(MAPPING) PADD_VALUE_CHAR = UNKONWN+1 LABEL_MAPPING = {"O":0,"B-LOC":1,"I-LOC":2,"B-PER":3,"I-PER":4, "B-ORG":5,"I-ORG":6,"B-MISC":7,"I-MISC":8 } # Look here for support and extra guide # https://github.com/guillaumegenthial/tf_ner/blob/master/models/chars_lstm_lstm_crf/main.py def get_cloud_pipeline_options(): """Get apache beam pipeline options to run with Dataflow on the cloud Args: project (str): GCP project to which job will be submitted Returns:
def rotate1(text, key): key %= 26 newchars = al[key:] + al[:key] trans = str.maketrans(al + al.upper(), newchars + newchars.upper()) return text.translate(trans)
def randomKey(message=""): letters = list(alphabet.upper()) random.shuffle(letters) return ''.join(letters)
from string import ascii_lowercase, digits ################################## StringName = u'PyJsConstantString%d_' NumberName = u'PyJsConstantNumber%d_' RegExpName = u'PyJsConstantRegExp%d_' ################################## ALPHAS = set(ascii_lowercase+ ascii_lowercase.upper()) NUMS = set(digits) IDENTIFIER_START = ALPHAS.union(NUMS) ESCAPE_CHARS = {'n', '0', 'b', 'f', 'r', 't', 'v', '"', "'", '\\'} OCTAL = {'0', '1', '2', '3', '4', '5', '6', '7'} HEX = set('0123456789abcdefABCDEF') from utils import * IDENTIFIER_PART = IDENTIFIER_PART.union({'.'}) def _is_cancelled(source, n): cancelled = False k = 0 while True: k+=1 if source[n-k]!='\\': break cancelled = not cancelled return cancelled def _ensure_regexp(source, n): #<- this function has to be improved '''returns True if regexp starts at n else returns False checks whether it is not a division ''' markers = '(+~"\'=[%:?!*^|&-,;/\\' k = 0
number_templates = [] for i in range(10): image = cv.imread(cur_dir + r'\data\templates\\' + str(i) + '.png', cv.IMREAD_GRAYSCALE) _, image = cv.threshold(image, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU) image = image == 0 erosion = cv.erode(image.astype(np.float32), base_template) number_templates.append(erosion) alphabet_templates = [] for s in ascii_lowercase.upper(): image = cv.imread(cur_dir + r'\data\templates\\' + s + '.png', cv.IMREAD_GRAYSCALE) _, image = cv.threshold(image, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU) image = image == 0 erosion = cv.erode(image.astype(np.float32), base_template) alphabet_templates.append(erosion) """ b) """ # Find numbers or letters in the clean image with hit filter
"32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd2061bbde24eb76a19d84aba34d8de287be84d07e7e9a30ee714979c7e1123a8bd9822a33ecaf512472e8e8f8db3f9635c1949e640c621854eba0d79eccf52ff111284b4cc61d11902aebc66f2b2e436434eacc0aba938220b084800c2ca4e693522643573b2c4ce35050b0cf774201f0fe52ac9f26d71b6cf61a711cc229f77ace7aa88a2f19983122b11be87a59c355d25f8e4", "32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c909ba7696cf606ef40c04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9305f4b44f915cb2bd05af51373fd9b4af511039fa2d96f83414aaaf261bda2e97b170fb5cce2a53e675c154c0d9681596934777e2275b381ce2e40582afe67650b13e72287ff2270abcf73bb028932836fbdecfecee0a3b894473c1bbeb6b4913a536ce4f9b13f1efff71ea313c8661dd9a4ce", "315c4eeaa8b5f8bffd11155ea506b56041c6a00c8a08854dd21a4bbde54ce56801d943ba708b8a3574f40c00fff9e00fa1439fd0654327a3bfc860b92f89ee04132ecb9298f5fd2d5e4b45e40ecc3b9d59e9417df7c95bba410e9aa2ca24c5474da2f276baa3ac325918b2daada43d6712150441c2e04f6565517f317da9d3", "271946f9bbb2aeadec111841a81abc300ecaa01bd8069d5cc91005e9fe4aad6e04d513e96d99de2569bc5e50eeeca709b50a8a987f4264edb6896fb537d0a716132ddc938fb0f836480e06ed0fcd6e9759f40462f9cf57f4564186a2c1778f1543efa270bda5e933421cbe88a4a52222190f471e9bd15f652b653b7071aec59a2705081ffe72651d08f822c9ed6d76e48b63ab15d0208573a7eef027", "466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d66f1d559ba520e89a2cb2a83", "32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904", ] from string import ascii_lowercase valid_chars = [c for c in (" ,.'-" + ascii_lowercase + ascii_lowercase.upper() )] def strxor(a, b): # xor two strings of different lengths if len(a) > len(b): return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)]) else: return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])]) def encrypt(key, msg): c = strxor(key, msg) print msg, c print c.encode('hex') return c def spaceDetector(numb):
from string import ascii_lowercase as alphabet_lower alphabet_upper = alphabet_lower.upper() numbers = '0123456789' def chunk(string, size): for i in xrange(0, len(string), size): yield string[i:i+size] ## CHALLENGE 1: Convert hex to base64 ## ''' The string: 49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d Should produce: SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t ''' ### ASCII ## def ascii_to_binary(string): ret = '' for char in string: ret += bin(ord(char))[2:].zfill(8) return ret def binary_to_ascii(string): ret = '' for char in chunk(string, 8): ret += chr(int(char, 2)) return ret ### HEX ##
"""day19.py """ import pathlib from string import ascii_lowercase cwd = pathlib.Path(__file__).parent.absolute() dpath = pathlib.PurePath(cwd, 'data') DIRS = [(1, 0), (-1, 0), (0, 1), (0, -1)] LETTERS = set([l for l in ascii_lowercase.upper()]) ALLOWED = set(['-', '+', '|']) | LETTERS class Route: def __init__(self, fname): self.m = {} self.start = None self.process(fname) def process(self, fname): with open(fname, 'r') as f: data = f.read().splitlines() for y, line in enumerate(data): for x, ch in enumerate(line): self.m[(x, y)] = ch if y == 0 and ch == '|': self.start = (x, y) def follow_path(self): count = 1 found = [] coord = self.start
from string import ascii_lowercase upperAlppha = list(ascii_lowercase.upper()) lowerAlpha = list(ascii_lowercase) upperAlpphaASCI = map(ord, upperAlppha) lowerAlphaASCI = map(ord, lowerAlpha) primes = [67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113] T = int(input()) for i in range(T): N = input() s = raw_input() out = "" for each in s: asci = ord(each) diff = abs(primes[0] - asci) for i in range(1, len(primes)): if abs(primes[i] - asci) < diff: diff = abs(primes[i] - asci) else: out += chr(primes[i - 1]) break else: out += chr(primes[-1]) print(out)
def caesar(pt, shift): al = AL[shift:] + AL[:shift] return pt.translate(str.maketrans(AL + AL.upper(), al + al.upper()))
def main(): # Define filePath filePath = sys.argv[1] # Print reading file print "\n" + "Reading file: \n" + str(filePath) + "\n" sys.stdout.flush() # Define file extension from path fileExtension = ntpath.basename(filePath).split('.')[-1] if not fileExtension == "pkl": # Read in file, determine whether a pkl or txt/csv try: # Detect delimiter sniffer = csv.Sniffer() sniffer.preferred = [',', '|', ';', ':', '~'] csvFile = open(filePath, 'rb') for row in csv.reader(csvFile, delimiter="\t"): row1 = row break csvFile.close() dialect = sniffer.sniff(str(row1)) sepType = dialect.delimiter if sepType not in {",", "|", ";", ":", "~"}: print "Invalid delimiter" sys.stdout.flush() return # Read in pandas data frame from csv file df = pd.read_csv(filePath, sep=sepType) except pd.parser.CParserError: print "Invalid file" sys.stdout.flush() return except IOError: print "File not found" sys.stdout.flush() return else: try: # Read in pandas data fram pkl file df = pd.read_pickle(filePath) except IOError: print "File not found" sys.stdout.flush() return # Define start time start_time = time.time() # Create default name for outFile if one is not chosen on command if len(sys.argv) > 2: file = open(sys.argv[2], 'w+') else: file = open( str(ntpath.basename(filePath)).split('.')[0] + '.FD_Info.txt', 'w+') # Add name of file , row count, columns to info string file.write( str("Table : " + str(ntpath.basename(filePath)).split('.')[0] + "\n" + "Columns : " + str(", ".join(list(df.head(0)))) + "\n\n" + "Functional Dependencies: \n")) # Print line print("Functional Dependencies: ") sys.stdout.flush() # Define header; Initialize k; U = list(df.head(0)) k = 0 try: # Create dictionary to convert column names into alphabetical characters Alpha_Dict = {U[i]: ascii_lowercase.upper()[i] for i in range(len(U))} except IndexError: print "Table exceeds max column count" sys.stdout.flush() return # Initialize lattice with singleton sets at 1-level C = [[[item] for item in U]] + [None for level in range(len(U) - 1)] # Create Generator to find next k-level attribute subsets Subset_Gen = ([ x for x in Apriori_Gen.powerset(U) if len(x) == k ] for k in range(1, len(max(Apriori_Gen.powerset(U), key=len)) + 1)) # Initialize Closure as Python dict Closure = { binaryRepr.toBin(Subset, U): set(Subset) for Subset in next(Subset_Gen) } # Initialize Cardinality as Python dict Cardinality = {element: None for element in Closure} # Create counter for number of Equivalences and FDs; initialize list to store FDs; list to store equivalences; Counter = [0, 0] FD_Store = [] E_Set = [] while True: # Increment k; initialize C_km1 k += 1 C_km1 = C[k - 1] # Initialize Closure at next next k-level; update dict accordinaly Closure_k = { binaryRepr.toBin(Subset, U): set(Subset) for Subset in next(Subset_Gen) } Closure.update(Closure_k) # Update Cardinality dict with next k-level Cardinality.update({element: None for element in Closure_k}) if k > 1: # Dereference Closure and Cardinality at (k-2)-level for Subset in C[k - 2]: del Closure[binaryRepr.toBin(Subset, U)], Cardinality[binaryRepr.toBin( Subset, U)] # Dereference (k-2)-level C[k - 2] = None # Run Apriori_Gen to get k-level Candidate row from (k-1)-level Candidate row C_k = Apriori_Gen.oneUp(C_km1) # Run GetFDs to get closure and set of functional dependencies Closure, F, Cardinality = GetFDs.f(C_km1, df, Closure, U, Cardinality) # Run Obtain Equivalences to get set of attribute equivalences E = ObtainEquivalences.f(C_km1, F, Closure, U) # Run Prune to reduce next k-level iterateion and delete equivalences; initialize C_k C_k, Closure, df = Prune.f(C_k, E, Closure, df, U) C[k] = C_k #Increment counter for the number of Equivalences/FDs added at this level Counter[0] += len(E) Counter[1] += len(F) E_Set += E # Print out FDs for FunctionalDependency in F: # Store well-formatted FDs in empty list FD_Store.append([ "".join( sorted([Alpha_Dict[i] for i in FunctionalDependency[0]])), Alpha_Dict[FunctionalDependency[1]] ]) # Create string for functional dependency String = "{" + ", ".join(FunctionalDependency[0]) + "} -> {" + str( FunctionalDependency[1]) + "}" # Print FD String print(String) sys.stdout.flush() # Write string to TXT file file.write(String + "\n") # Break while loop if cardinality of C_k is 0 if not len(C_k) > 0: break # Break while loop if k-level reaches level set in config if k is not None and MAX_K_LEVEL == k: break # Print equivalences file.write("\n" + "Equivalences: " + "\n") print "\n" + "Equivalences: " sys.stdout.flush() # Iterate through equivalences returned for Equivalence in E_Set: # Create string for functional dependency String = "{" + ", ".join(Equivalence[0]) + "} <-> {" + ", ".join( Equivalence[1]) + "}" # Print equivalence string print(String) sys.stdout.flush() # Write string to TXT file file.write(String + "\n") # Print out keys file.write("\n" + "Keys: " + "\n") print "\n" + "Keys: " sys.stdout.flush() # Get string of column names sorted to alphabetical characters SortedAlphaString = "".join( sorted([Alpha_Dict[item] for item in Alpha_Dict])) # Run required inputs through keyList module to determine keys with keyList = keyRun.f(U, SortedAlphaString, FD_Store) # Iterate through keys returned for key in keyList: # Write keys to file file.write(str(key) + "\n") # Print keys print str(key) sys.stdout.flush() # Create string to give user info of script checkInfoString = str("\n" + "Time (s): " + str(round(time.time() - start_time, 4)) + "\n" + "Row count: " + str(df.count()[0]) + "\n" + "Attribute count: " + str(len(U)) + "\n" + "Number of Equivalences: " + str(Counter[0]) + "\n" + "Number of FDs: " + str(Counter[1]) + "\n" "Number of FDs checked: " + str(GetFDs.CardOfPartition.calls)) # Write info at bottom file.write(checkInfoString) #Print elapsed time print(checkInfoString) sys.stdout.flush() # Close file file.close()
def rotate(text, key): cipher = abc[key:] + abc[:key] table = str.maketrans(abc + abc.upper(), cipher + cipher.upper()) return text.translate(table)