Exemple #1
0
def run(sentset, labelset, postagset, all_feats, info, weights, testdata, ad):
    tsents, tgoldtagseqs, tpostagseqs, tinfo = testdata

    weights_avg = init_weights(all_feats)
    order = [i for i in range(len(sentset))]
    shuffle(order)
    k = 0
    for j in order:
	sys.stderr.write(str(k)+"\r")
	
	sent = sentset[j]
	labelseq = labelset[j]
	postagseq = postagset[j]
        
	predseq = execute(sent, all_labels, postagseq, weights, labelseq, info)
	if labelseq != predseq:
	    update(weights, predseq, labelseq, sent, postagseq, info, ad)
	    add_weights(weights_avg, weights)

        k += 1
        if k % 10000 == 0: 
            framework.write_weights(weights, k)
            decode(tsents, tgoldtagseqs, tpostagseqs, tinfo, weights)

    decode(tsents, tgoldtagseqs, tpostagseqs, tinfo, weights)
    return weights_avg, weights
Exemple #2
0
def learn_and_decode(trainfile, featlistfile, gazfile, brownfile, num_iter, testfile):
    sentset, labelset, postagset, all_feats, info = framework.get_all(trainfile, gazfile, featlistfile, brownfile)
    sys.stderr.write("\n" + str(len(all_feats)) + " features in all\n")

    sys.stderr.write("\nreading test data \n")
    tsents, tgoldtagseqs, tpostagseqs = framework.read_data(testfile)
    tinfo = framework.get_maps(tsents, tpostagseqs, gazfile, brownfile)
    
    testdata = (tsents, tgoldtagseqs, tpostagseqs, tinfo)
    weights = init_weights(all_feats)
    tot_weights = init_weights(all_feats)
 
    #ADAGRAD
    ad = init_weights(all_feats)

    for ite in range(num_iter):
        sys.stderr.write("Iteration " + str(ite) + "\n---------------------------\ntotal train sentences = "+ str(len(sentset)) + "\n")
        weights_a, weights = run(sentset, labelset, postagset, all_feats, info, weights, testdata, ad) #ADAGRAD
        framework.write_weights(weights, ite)
        add_weights(tot_weights, weights_a)

    for key in tot_weights.iterkeys():
        tot_weights[key] /= num_iter*len(sentset)

    sys.stderr.write("\n\nfinal performance on test\n")
    decode(tsents, tgoldtagseqs, tpostagseqs, tinfo, tot_weights)
Exemple #3
0
def listen_for_input(r, mic):
    # listen to input
    print("Now listening...")
    beep()

    with mic as source:
        # this ambient noise adjustment is causing more problems than it's solving
        # r.adjust_for_ambient_noise(source)
        audio = r.listen(source)

    # try to recognize audio

    try:
        recognized_speech = r.recognize_google(audio)
        print(recognized_speech)
        decoder.decode(recognized_speech.lower())

        listen_for_input(r, mic)
    except sr.RequestError:
        # API was unreachable or unresponsive
        print("There was an error: API unavailable")
    except sr.UnknownValueError:
        # speech was unintelligible
        print("There was an error: Unable to recognize speech")
        speaker.speak("Sorry, I didn't get that.")
        listen_for_input(r, mic)
Exemple #4
0
def test_decoding_with_ec():
    # Decoding correct encoded binary with error correction
    encoded_correct = ['000', '000', '000', '010', '010',
                       '000', '001', '010', '001', '011',
                       '000', '000', '000', '011', '011',
                       '000', '000', '000', '011', '011',
                       '000', '001', '000', '001', '110',
                       '000', '000', '010', '010', '100',
                       '000', '001', '000', '010', '011',
                       '000', '001', '000', '001', '110',
                       '000', '000', '000', '100', '010',
                       '000', '000', '000', '011', '011',
                       '000', '000', '001', '000', '010']
    # Decoding wrong encoded binary + missing value with error correction
                     #'100',
    encoded_error =  [       '000', '000', '010', '010',
                      '100', '001', '010', '001', '011',
                      '000', '000', '000', '011', '011',
                      '000', '000', '000', '011', '011',
                      '000', '001', '000', '001', '110',
                      '000', '000', '010', '010', '100',
                      '000', '001', '000', '010', '011',
                      '000', '001', '000', '001', '110',
                      '000', '000', '000', '100', '010',
                      '000', '000', '000', '011', '011',
                      '000', '000', '001', '000', '010']
    # Expected output string
    expected = 'Hello World'
    # Tests
    assert expected == decode(encoded_correct), "Char decoding \'{0}\' is incorrect to expected output \'{1}\'.".format(decode(encoded_correct), expected)
    print("Decoded correct secret:", decode(encoded_correct))
    assert expected == decode(encoded_error), "Char decoding \'{0}\' is incorrect to expected output \'{1}\'.".format(decode(encoded_error), expected)
    print("Decoded secret with error(s):", decode(encoded_error))
def image_visualizer_windows(num_d, num_n):
    # Decodifica la imagen
    decode(num_d, num_n)
    # Quita la ventana anterior
    window.withdraw()
    # Cambia la ventana
    visual_window = Toplevel()
    visual_window.title("Decodificador de imagen")
    visual_window.geometry('880x360')

    labelOwo = Label(visual_window, text="Imagen codificada", font = "Helvetica 24 bold italic")
    labelOwo.place(x=75, y=0)
    img1 = Image.open("owo.png")
    renderOwo = ImageTk.PhotoImage(img1.resize((320, 214), Image.ANTIALIAS))
    imgOwo = Label(visual_window, image=renderOwo)
    imgOwo.image = renderOwo
    imgOwo.place(x=60, y=80)

    labelUwu = Label(visual_window, text="Imagen decodificada", font="Helvetica 24 bold italic")
    labelUwu.place(x=500, y=0)
    img2 = Image.open("uwu.png")
    renderUwu = ImageTk.PhotoImage(img2.resize((160, 214), Image.ANTIALIAS))
    imgUwu = Label(visual_window, image=renderUwu)
    imgUwu.image = renderUwu
    imgUwu.place(x=580, y=80)

    visual_window.mainloop()
Exemple #6
0
def main(argv):
    plaintext = "The quick brown fox jumps over the lazy dog"
    ciphertext = "1442779157967667200000|262059538099831245374033664000000000|491524481646305217710445277411617404468653198242187500|54736736297607421875000000|26575780025450776966354193208522490921501091250000000000|5232777644455317290286534758400000|21499084800000|24555699822948576064581298828125000000000000|17936133750000"

    # this would be less ugly if python had the ternary operator
    hexadecimal = True if "--hex" in argv else False

    if "--encode" in argv:
        print encoder.encode(plaintext, hexadecimal)

    if "--decode" in argv:
        print decoder.decode(ciphertext)
Exemple #7
0
def detokenize(data, options):
    logging.debug("detokenize")
    if options.verbose:
        sys.stderr.write("Detokenizing...")
    
    detokenized = StringIO.StringIO()
    decode(data, detokenized)
    if options.verbose:
        sys.stderr.write("done\n")
    
    detokenHandle = StringIO.StringIO(detokenized.getvalue())
    return detokenHandle
def exec_next_inst ():
    comp["ir"] = comp["instmem"][comp["pc"]]
    
    comp["log"].append ( "ir             ↢ instmem[pc (${})] ({})".format ( int(comp["pc"]) + 1 , comp["ir"] ) )

    if comp["pc"] < 999:
        comp["pc"] += 1 
    else:
        comp["pc"]=0

    comp["log"].append ( "pc             ↢ {}".format ( int(comp["pc"]) + 1 ) )

    if comp["ir"]:
        decode (comp)
Exemple #9
0
def network(cont_plce_hlder, que_plce_hlder, context_mask_placeholder,
            question_mask_placeholder, ans_plce_hlder, dropout_placeholder):

    G = encode(cont_plce_hlder, context_mask_placeholder, que_plce_hlder,
               question_mask_placeholder, drop_out, hidden_states,
               max_para_lgth)
    pred_ans = decode(G, context_mask_placeholder, drop_out, hidden_states)

    print(pred_ans)

    loss = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(labels=ans_plce_hlder,
                                                       logits=pred_ans))

    params = tf.compat.v1.trainable_variables()
    gradients = tf.gradients(loss, params)
    gradient_norm = tf.linalg.global_norm(gradients)
    clipped_gradients, _ = tf.clip_by_global_norm(gradients, max_gradient_norm)
    param_norm = tf.linalg.global_norm(params)

    global_step = tf.Variable(0, name="global_step", trainable=False)
    opt = tf.compat.v1.train.AdamOptimizer(learning_rate=learning_rate)
    updates = opt.apply_gradients(zip(clipped_gradients, params),
                                  global_step=global_step)

    return updates, loss, global_step, param_norm, gradient_norm, pred_ans
def run_button():
    initializereg()

    exec(open(r"parent.py").read()
         )  # This puts all the data and code into the memory.
    PC.PC = 0x0
    global instructions_executed, IR, decoded_result, r_value, r_index, m_type, m_value, m_index, opc
    # print(memory.code_stop)

    while PC.PC < memory.code_stop and configme.breakflag == 0:
        IR = decoder.fetch(PC.PC)  # FETCH
        decoded_result = decoder.decode(
            IR)  # DECODE   # decoded_result = (op,rd,rs1,rs2,imm)
        opc, r_index, r_value, m_index, m_value, m_type = EXEC_instruct.execute_instruction(
            decoded_result)  # EXECUTE
        EXEC_instruct.memory_access(m_index, m_value, m_type)  # MemAcc
        EXEC_instruct.write_back(r_index, r_value)  # WrBack
        instructions_executed += 1
        QApplication.processEvents()

    memory.print_mem_into_apache()
    memory.print_code_into_apache()
    fO = open('console.txt', 'w+')
    fO.write(f'Assume CPI = 5\n')
    fO.write(f'Total clock cycles for program = {5*instructions_executed}\n')
    fO.close()
Exemple #11
0
    def run_decoding(self):
        '''
        '''
        translations = {}

        language_model = self.language_model
        source_language_model = self.source_language_model
        translation_model =  self.translation_model
        beam_size = self.beam_size
        max_phrase_length = self.max_phrase_length
        stack_limit = self.max_phrase_length
        stupid_backoff = self.stupid_backoff
        n_size = self.n_size
        feature_weights = self.feature_weights
        nbest = self.nbest
        empty_default = self.empty_default

        point = self.max_lines / 100 if self.max_lines > 100 else 1

        for sentence, idx in iter(self.queue.get, 'STOP'):
            idx = int(idx)
            translations[idx] = \
                    decoder.decode(sentence, language_model,
                                   source_language_model,
                                   translation_model,
                                   beam_size, max_phrase_length, stack_limit,
                                   stupid_backoff, n_size, feature_weights,
                                   nbest, empty_default)
            if idx % point == 0:
                phr.show_progress(idx, self.max_lines, 40, "DECODING")

            self.queue.task_done()
        self.queue.task_done()

        self.pipe.send(translations)
Exemple #12
0
def mk_js_test(lib, lispson_test, i):
    lispson = lispson_test[1]
    code, defs, natives = decoder.decode(lispson, lib)
    def_codes = sorted(defs.values())
    defs_code = '\n'.join(def_codes)
    def_fun = lib['lang']['target']['def_fun_with_code']
    return def_fun('', '', defs_code, code)
Exemple #13
0
def test(arr_adr):
    for i in arr_adr:
        out = com.com_session(i)  # 0.06 seconds
        data = dec.decode(out)
        print(data)
        #firebase.post('/ik', data, params='2')
        data = 0
def uart(epoch, decode):
    uart = UART(2, 115200)
    uart.init(115200, bits=8, parity=0, stop=1, tx=4, rx=5)
    while True:
        pkt = uart.readline()
        if (pkt != None):
            pkt = str(pkt)
            pkt = pkt.upper()
            pkt = pkt[2:-4]
            pkt = pkt.split(",")

            try:
                if len(pkt[0]) == 12 and len(pkt[1]) > 6 and len(
                        pkt[2]) > -3 and int(pkt[2]) <= 0:
                    if epoch:
                        flT = time.time() + 946684800
                    else:
                        flT = time_stamp(time.localtime())
                    m = {
                        'ts': flT,
                        'mac': pkt[0],
                        'data': pkt[1],
                        'rssi': int(pkt[2])
                    }
                    if decode:
                        d = decoder.decode(pkt[1])
                        m.update(d)
                    msgJson = ujson.dumps(m)
                    clientB.publish(topic=TOPIC + "ble" + pkt[0], msg=msgJson)
                else:
                    # Malformed UART data
                    pass
            except:
                pass
Exemple #15
0
def test(ctx, enc, dec, gdc, test_data):
    global test_idx
    ae_metric = mx.metric.MSE()
    gd_metric = mx.metric.Accuracy()
    samples = []
    for images, labels in test_data:
        gauss_yes = nd.ones((labels.shape[0], 1), ctx=ctx)

        features = encode(enc, images, labels)
        images_out = decode(dec, features, labels)
        ae_metric.update([images], [images_out])

        gauss_fit = gdc(features)
        gd_metric.update([gauss_yes], [gauss_fit])

        idx = np.random.randint(images.shape[0])
        samples.append(
            mx.nd.concat(images[idx], images_out[idx], dim=2)[0].asnumpy())

    name, mse = ae_metric.get()
    print('  AutoEncoder: {}={:.4f}'.format(name, mse))
    name, mse = gd_metric.get()
    print('  GaussDiscriminator: feature space satisfaction {}={:.4f}'.format(
        name, mse))

    try:
        imgdir = '/tmp/mnist'
        save_images(samples[::2], imgdir, test_idx * 1000)
        test_idx += 1
        print("  test images written to", imgdir)
    except Exception as e:
        print("  writing images failed:", e)
Exemple #16
0
def encrypt():
    if request.method == 'POST':
        # check if the post request has the file part

        if 'up_pic' not in request.files:
            return "No file selected"
            return redirect(request.url)
        file = request.files['up_pic']
        degree = int(request.form.get('degree'))
        pwd = request.form.get('pwd')

        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            return "No file selected"
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        links = [str(os.path.join(app.config['UPLOAD_FOLDER'], filename))]
        if request.form['submit']=="enc":
            (im,arr,path)=enc.encode(links[0],degree,pwd)
        else:
            (im,arr,path)=dec.decode(links[0],degree,pwd)
        links.append(path)
        return render_template("display.html",link=links)
Exemple #17
0
def pick_url(candidates_byte, encoding='utf-8'):
    pick = random.randint(0, len(candidates_byte) - 1)

    chosen_byte = candidates_byte[pick]
    chosen_byte = urllib.unquote(chosen_byte)
    chosen_u = decoder.decode(chosen_byte, encoding)

    return chosen_u
Exemple #18
0
def step_button(C=0):
    global check_on_step
    if check_on_step == 0:
        exec(open(r"parent.py").read())
        check_on_step = 1
    global instructions_executed
    if PC.PC < memory.code_stop:
        decoder.decode(PC.PC)
        instructions_executed += 1

    if C == 0:
        memory.print_mem_into_apache()
        memory.print_code_into_apache()
    fO = open('console.txt', 'w+')
    fO.write(f'Assume CPI = 5\n')
    fO.write(f'Total clock cycles for program = {5 * instructions_executed}\n')
    fO.close()
Exemple #19
0
def pick_url(candidates_byte, encoding='utf-8'):
    pick = random.randint(0, len(candidates_byte) - 1)

    chosen_byte = candidates_byte[pick]
    chosen_byte = urllib.unquote(chosen_byte)
    chosen_u = decoder.decode(chosen_byte, encoding)

    return chosen_u
Exemple #20
0
    def on_decode_clicked(self, button):
        buffer = self.message_to_decode.get_buffer()
        (start, stop) = buffer.get_bounds()
        message_to_decode = buffer.get_text(start, stop, 0)
        decoded_message = decode(message_to_decode)

        # display the decoded message
        self.decoded_label.set_text(decoded_message)
def main():
    """ Simple example entry point"""
    message = 'Better Faster Forever'
    cipher = make_cipher('QWERTY')
    secret = encode(message, cipher)
    print('secret: {}'.format(secret))
    # secret: vTnnTl zQmnTl zilTpTl
    print(decode(secret, cipher))
Exemple #22
0
    def create_players(self,pm, league):
        divisions_href = {}

        players = self.player_data.tsv_players.keys()
        for player_name in players:
            tsv_record = self.player_data.tsv_players[player_name]
            tier_player = TierPlayer()
            division_name = decode(tsv_record['division_name'])

            if not divisions_href.has_key(division_name):
                divisions_href[division_name] = pm.get_division(division_name, league)

            tier_player.division = divisions_href[division_name]
            tier_player.tier = tier_player.division.tier
            tier_player.name = player_name
            tier_player.person_name = decode(tsv_record['person_name'])
            pm.db_connector.get_session().add(tier_player)
        pm.db_connector.get_session().commit()
Exemple #23
0
def process():
    if request.method == "GET":
        query = request.args.to_dict()
        print("Received GET Query:", query)
        return Response(encode(query), mimetype='proton')
    else:
        decoded = decode(request.data)
        print("Received POST ProtoN Message:", decoded)
        return Response(encode(decoded), mimetype='proton')
Exemple #24
0
def deckListBuilder(deckcode):
    #turns deckcode into list of singles and doubles
    deck = decoder.decode(deckcode)

    singles = deck[6:6+deck[5]]
    doubles = deck[7+deck[5]:7+deck[5]+deck[6+deck[5]]]
    cardList = [singles,doubles]

    return cardList
def main(argv):
    if len(argv) <= 1:
        print "Usage: %s bit0 bit1 bit2 ... bitn # (n is odd)" % (argv[0], )
        print
        print "See http://github.com/wnyc/directional_parity for more details"
    value = float(decode(map(int, argv[1:])))
    f = (value * 9.0 - 80.0) / 20.0
    c = (value / 4.0) - 20.0
    print "%0.2fC %0.2fF" % (c, f)
    return 0
Exemple #26
0
def run_button():
    initializereg()

    exec(open(r"parent.py").read()
         )  # This puts all the data and code into the memory.
    PC.PC = 0x0
    global instructions_executed
    # print(memory.code_stop)
    while PC.PC < memory.code_stop and configme.breakflag == 0:
        decoder.decode(PC.PC)
        instructions_executed += 1
        QApplication.processEvents()

    memory.print_mem_into_apache()
    memory.print_code_into_apache()
    fO = open('console.txt', 'w+')
    fO.write(f'Assume CPI = 5\n')
    fO.write(f'Total clock cycles for program = {5*instructions_executed}\n')
    fO.close()
def main(argv):
    if len(argv) <= 1:
        print "Usage: %s bit0 bit1 bit2 ... bitn # (n is odd)" % (argv[0],)
        print
        print "See http://github.com/wnyc/directional_parity for more details"
    value = float(decode(map(int, argv[1:])))
    f = (value * 9.0 - 80.0) / 20.0
    c = (value / 4.0) - 20.0
    print "%0.2fC %0.2fF" % (c, f)
    return 0
Exemple #28
0
def store_champs():

    store_champs = simple_cache.get('store-champ-data')
    if store_champs is None:
        pm = PersistenceManager(myapp.db_connector)
        tourneys = pm.get_tourneys()
        store_champs = []
        for tourney in tourneys:
            for rank in tourney.rankings:
                if tourney.is_store_championship():
                    rec = { 'tourney' : decode(tourney.tourney_name),
                            'num_participants': tourney.participant_count,
                            'player' : decode(rank.player.player_name),
                            'swiss_standing': rank.rank,
                            'championship_standing' : rank.elim_rank,
                            'pretty_print' : rank.pretty_print() }
                    store_champs.append(rec)

        simple_cache.set( 'store-champ-data', store_champs, timeout=5*60)

    return render_template( 'store_champ_lists.html', championship_lists=store_champs)
Exemple #29
0
 def _load_url(self, url_u, encoding=None):
     # word hit list obsolete
     self.wordhitview.clear_words()
     # set text in textview
     ret = fetcher.fetch(url_u)
     if not encoding:
         encoding = decoder.detect_encoding(ret.txt_byte)
     txt_u = decoder.decode(ret.txt_byte, encoding)
     txt_u = unmarkup.unwiki(txt_u) or unmarkup.unhtml(txt_u)
     self.text = word.Text()
     self.text.set_from_txt_u(txt_u)
     self.textview.set_text(self.text, encoding, url_u)
Exemple #30
0
 def _load_url(self, url_u, encoding=None):
     # word hit list obsolete
     self.wordhitview.clear_words()
     # set text in textview
     ret = fetcher.fetch(url_u)
     if not encoding:
         encoding = decoder.detect_encoding(ret.txt_byte)
     txt_u = decoder.decode(ret.txt_byte, encoding)
     txt_u = unmarkup.unwiki(txt_u) or unmarkup.unhtml(txt_u)
     self.text = word.Text()
     self.text.set_from_txt_u(txt_u)
     self.textview.set_text(self.text, encoding, url_u)
Exemple #31
0
def addToResult(morseChar):
    """Decodes a given morse character and adds the decoded result
    to the global resulting string."""
    global resultingText
    if (morseChar == DIT or morseChar == DAH
            or morseChar == NEW_MORSE_CHARACTER):
        #if DIT, DAH or NEW_MORSE_CHARACTER add to morse result
        resultingMorse.append(morseChar)
    elif (morseChar == NEW_LETTER):
        #if new letter, add to morse result and compute completed
        #letter and add to result string
        resultingMorse.append(morseChar)
        previousChar = getPreviousCharFromMorse(resultingMorse)
        if VERBOSE:
            print(previousChar)
            print("=")
            print(decode(previousChar))
        resultingText += decode(previousChar)
    elif (morseChar == NEW_WORD):
        #if new word, add to morse result, compute completed letter
        #and add space to result string
        resultingMorse.append(NEW_LETTER)
        previousChar = getPreviousCharFromMorse(resultingMorse)
        if VERBOSE:
            print(previousChar)
            print("=")
            print(decode(previousChar))
        resultingText += decode(previousChar)
        #add space
        resultingMorse.append(morseChar)
        resultingText += " "
    else:
        #if none of the above, add a question mark to the results
        resultingMorse.append("?")
        resultingText += "?"
    if VERBOSE:
        print("####")
    print(resultingText)
    if VERBOSE:
        print("####")
Exemple #32
0
async def send_packet(pkt):
    global globall
    global p_id
    p_id += 1
    if not globall:
        await sio.emit('toggleCapturer', False)
        e.set()
    pkth = bytes(pkt).hex().upper()
    ret = decoder.decode(pkth)
    if not ret["skip"]:
        ret["id"] = p_id
        packets.insert(0, ret)
        dofilter(ret)
def addToResult(morseChar):
    """Decodes a given morse character and adds the decoded result
    to the global resulting string."""
    global resultingText
    if(morseChar == DIT or morseChar == DAH or morseChar == NEW_MORSE_CHARACTER):
        #if DIT, DAH or NEW_MORSE_CHARACTER add to morse result
        resultingMorse.append(morseChar)
    elif(morseChar == NEW_LETTER):
        #if new letter, add to morse result and compute completed
        #letter and add to result string
        resultingMorse.append(morseChar)
        previousChar = getPreviousCharFromMorse(resultingMorse)
        if VERBOSE:
            print(previousChar)
            print("=")
            print(decode(previousChar))
        resultingText += decode(previousChar)
    elif(morseChar == NEW_WORD):
        #if new word, add to morse result, compute completed letter
        #and add space to result string
        resultingMorse.append(NEW_LETTER)
        previousChar = getPreviousCharFromMorse(resultingMorse)
        if VERBOSE:
            print(previousChar)
            print("=")
            print(decode(previousChar))
        resultingText += decode(previousChar)
        #add space
        resultingMorse.append(morseChar)
        resultingText += " "
    else:
        #if none of the above, add a question mark to the results
        resultingMorse.append("?")
        resultingText += "?"
    if VERBOSE:
        print("####")
    print(resultingText)
    if VERBOSE:
        print("####")
Exemple #34
0
 def venue_string(self):
     ret = ""
     if self.venue is not None:
         if self.venue.venue is not None:
             ret = self.venue.venue
         if self.venue.city is not None:
             ret = ret + "/" + self.venue.city
         if self.venue.state is not None:
             ret = ret + "/" + self.venue.state
         if self.venue.country is not None:
             ret = ret + "/" + self.venue.country
     else:
         ret = "Unknown"
     return decode(ret)
Exemple #35
0
def execution(befehlscode, highlight, updateAll):
    global index
    global skipnext

    index += 1

    # print(hex(befehlscode))
    if not skipnext:
        decoder.decode(befehlscode)
        print("Info: Executed")
    else:
        skipnext = False

    commands.doTimer()
    commands.doInterrupt()

    time.sleep((4 / quartz_frequency) * timescale)

    data.data_memory[0x02] = index & 0b11111111

    highlight(simulationParser.queue[index][0])

    updateAll()
Exemple #36
0
def train(sess, train_data, test_data, epochs=40):
    ctx = mx.cpu()
    sess.init_all(ctx)

    enc = sess.get_block('enc')
    dec = sess.get_block('dec')

    enc_trainer = sess.get_trainer('enc')
    dec_trainer = sess.get_trainer('dec')

    loss = gluon.loss.SigmoidBCELoss(from_sigmoid=True)
    metric = mx.metric.MSE()

    for epoch in range(epochs):
        metric.reset()
        for i, (images, labels) in enumerate(train_data):
            images = images.as_in_context(ctx)
            labels = labels.as_in_context(ctx)

            batch_size = images.shape[0]

            # record computation graph for differentiating with backward()
            with autograd.record():
                features = encode(enc, images, labels)
                images_out = decode(dec, features, labels)
                L = loss(images_out, images)
                L.backward()

            enc_trainer.step(batch_size)
            dec_trainer.step(batch_size)

            metric.update([images], [images_out])

            if (i+1) % 100 == 0:
                name, mse = metric.get()
                print('[Epoch {} Batch {}] Training: {}={:.4f}'.format(epoch+1, i+1, name, mse))

        name, mse = metric.get()
        print('[Epoch {}] Training: {}={:.4f}'.format(epoch+1, name, mse))

        name, test_mse = test(ctx, enc, dec, test_data)
        print('[Epoch {}] Validation: {}={:.4f}'.format(epoch+1, name, test_mse))

        sess.save_all()
        print('Model parameters and trainer state saved.')
Exemple #37
0
def update_tourney_details():
    tourney_id            = request.form['tourney_id']
    name                  = decode( request.form['name'] )
    type                  = request.form['tourney_type']
    print request.form['datepicker']
    mmddyyyy              = request.form['datepicker'].split('/')
    date                  = datetime.date( int(mmddyyyy[2]),int(mmddyyyy[0]), int(mmddyyyy[1])) #YYYY, MM, DD
    round_length  = request.form['round_length_userdef']
    tourney_format_def    = request.form['tourney_format_dropdown']
    tourney_format_custom = request.form['tourney_format_custom']
    participant_count     = int(request.form['participant_count'])
    country               = decode(request.form['country'])
    state                 = decode(request.form['state'])
    city                  = decode(request.form['city'])
    venue                 = decode(request.form['venue'])


    tourney_format = None
    if tourney_format_def is None or len(tourney_format_def) == 0:
        if tourney_format_custom is None or len(tourney_format_custom) == 0:
            tourney_format = xwingmetadata.format_default
        else:
            tourney_format = decode(tourney_format_custom)
    else:
        tourney_format = str(tourney_format_def)


    pm                = PersistenceManager(myapp.db_connector)
    tourney           = pm.get_tourney_by_id(tourney_id)

    tourney.tourney_name = name
    tourney.tourney_type  = type
    tourney.tourney_date = date
    tourney.round_length = round_length
    tourney.format = tourney_format
    tourney.participant_count = participant_count
    tourney.venue.country = country
    tourney.venue.state = state
    tourney.venue.city = city
    tourney.venue.venue = venue

    event = Event(remote_address=myapp.remote_address(request),
                  event_date=func.now(),
                  event="edit tourney information",
                  event_details="edited " + name )

    pm.db_connector.get_session().add(event)
    pm.db_connector.get_session().commit()

    return redirect( url_for( 'get_tourney_details', tourney_id=tourney_id) )
def create_players(c, pm, ch, league):
    divisions_href = {}
    cin = ""

    for tier in league.tiers:
        players = ch.participant_index(tier.get_challonge_name())
        for player in players:
            lookup_name = None
            player = player['participant']
            challonge_username_ = player['challonge_username']
            checked_in = player['checked_in']
            if challonge_username_ is None or checked_in is False:
                lookup_name = player['display_name']
                print "player %s has not checked in " % (lookup_name)
            else:
                lookup_name = challonge_username_
            if c.tsv_players.has_key(lookup_name):
                # we're good to go
                tsv_record = c.tsv_players[lookup_name]
                if checked_in is False:
                    cin = cin + decode(tsv_record['email_address']) + ","

                # create the player record
                tier_player = TierPlayer()
                division_name = decode(tsv_record['division_name'])
                print "looking up division %s for player %s" % (division_name,
                                                                lookup_name)

                if not divisions_href.has_key(division_name):
                    divisions_href[division_name] = pm.get_division(
                        division_name, league)
                tier_player.division = divisions_href[division_name]
                tier_player.tier = tier_player.division.tier
                tier_player.challengeboards_handle = decode(
                    tsv_record['challengeboards_name'])
                tier_player.challonge_id = player['id']
                print player['group_player_ids'][0]
                tier_player.group_id = player['group_player_ids'][0]
                tier_player.name = lookup_name
                tier_player.email_address = decode(tsv_record['email_address'])
                tier_player.person_name = decode(tsv_record['person_name'])
                tier_player.reddit_handle = decode(tsv_record['reddit_handle'])
                tier_player.timezone = decode(tsv_record['time_zone'])
                pm.db_connector.get_session().add(tier_player)
    pm.db_connector.get_session().commit()
Exemple #39
0
def main():
    if (len(sys.argv) > 1):
        filePath = os.path.abspath(sys.argv[1])
        fileName = os.path.basename(filePath)

        file = open(filePath, 'r').read().lower()

        code = splitSegment(file, '.code', '.endcode')
        code = removeEmpity(code.split('\n'))
        code = removeComment(code)
        code = splitInsts(code)

        binaryCode = bytearray(decode(code))

        binaryFileName = fileName.split('.')[0] + '.hex'
        binaryFile = open(binaryFileName, 'wb')

        binaryFile.write(binaryCode)

        binaryFile.close()
Exemple #40
0
def edit_ranking_row():

    #see https://editor.datatables.net/manual/server
    tourney_id        = request.args['tourney_id']
    pm                = PersistenceManager(myapp.db_connector)
    tourney           = pm.get_tourney_by_id(tourney_id)

    de = RankingEditor(pm, tourney)

    event = Event(remote_address=myapp.remote_address(request),
                  event_date=func.now(),
                  event="edit ranking row")

    player_name = request.values['data[player_name]']
    player_name = decode(player_name)
    ret = de.set_and_get_json(request, player_name, event)
    event.event_details = event.event_details + " in tourney " + tourney.tourney_name
    pm.db_connector.get_session().add(event)
    pm.db_connector.get_session().commit()
    return ret
Exemple #41
0
def main():

    # Reading file name
    filename = my_io.getFilename()

    # Reading input file
    content = my_io.readBinaryFile(filename)

    # Calculating probabilities for each symbol
    probabilities = utils.calculateProbability(content)

    # Saving probabilities for future use
    savedProbability = probabilities.copy()

    # Calculatin entropy
    entropy = utils.calculateEntropy(probabilities)

    # Encoding the file
    huffCode = encoder.encode(probabilities)

    # Calculating average length
    averageLength = utils.calculateAverageLength(huffCode, savedProbability)

    # Getting the name of the compressed file and the overhead tree
    compressedName, overhead = my_io.compressFile(filename, huffCode)

    # Getting the name of the decompressed file after it is decoded
    decompressedFilename = decoder.decode(filename)

    # Calculating files' sizes (original, compressed, decompressed)
    originalSize = utils.calculateSize(filename)
    compressedSize = utils.calculateSize(compressedName)
    decompressedSize = utils.calculateSize(decompressedFilename)

    # Showing information on scree, such as sizes, entropy, average length and comparisons on screen
    my_io.showEntropy(entropy)
    my_io.showAverageLength(averageLength)
    my_io.showFileSize(1, originalSize, 0)
    my_io.showFileSize(2, compressedSize, overhead)
    my_io.showCompression(originalSize, compressedSize)
    my_io.showFileSize(3, decompressedSize, 0)
Exemple #42
0
def api(request):

    response = {}
    if request.method == 'GET':

        # Return encoded message (soundprint)
        if request.GET.get('message'):
            response['soundprint'] = encoder.encode(request.GET['message'])

        # Return decoded message
        elif request.GET.get('soundprint'):
            response['message'] = decoder.decode(request.GET['soundprint'])

        else:
            response['error'] = 'missing 1 parameter (message or soundprint)'

    else:
        response['error'] = 'must send GET request'

    return HttpResponse(json.dumps(response, indent=4), \
                        content_type='application/json')
def create_players(c, pm, ch, league):
    divisions_href = {}
    cin = ""

    for tier in league.tiers:
        players = ch.participant_index(tier.get_challonge_name())
        for player in players:
            lookup_name = None
            player = player['participant']
            challonge_username_ = player['challonge_username']
            checked_in = player['checked_in']
            if challonge_username_ is None or checked_in is False:
                lookup_name = player['display_name']
                # print "player %s has not checked in " % ( player['display-name'])
            else:
                lookup_name = challonge_username_
            if c.tsv_players.has_key(lookup_name):
                # we're good to go
                tsv_record = c.tsv_players[lookup_name]
                if checked_in is False:
                    cin = cin + decode(tsv_record['email_address']) + ","

                # create the player record
                tier_player = TierPlayer()
                division_name = decode(tsv_record['division_name'])
                print "looking up division %s for player %s" % (division_name, lookup_name)

                if not divisions_href.has_key(division_name):
                    divisions_href[division_name] = pm.get_division(division_name)
                tier_player.division = divisions_href[division_name]

                tier_player.tier = tier_player.division.tier
                tier_player.challengeboards_handle = decode(tsv_record['challengeboards_name'])
                tier_player.challonge_id = player['id']
                print player['group_player_ids'][0]
                tier_player.group_id = player['group_player_ids'][0]
                tier_player.name = lookup_name
                tier_player.email_address = decode(tsv_record['email_address'])
                tier_player.person_name = decode(tsv_record['person_name'])
                tier_player.reddit_handle = decode(tsv_record['reddit_handle'])
                tier_player.timezone = decode(tsv_record['time_zone'])
                pm.db_connector.get_session().add(tier_player)
    pm.db_connector.get_session().commit()
Exemple #44
0
def api(request):

    response = {}
    if request.method == 'GET':

        # Return encoded message (soundprint)
        if request.GET.get('message'):
            response['soundprint'] = encoder.encode(request.GET['message'])

        # Return decoded message
        elif request.GET.get('soundprint'):
            response['message'] = decoder.decode(request.GET['soundprint'])

        else:
            response['error'] = 'missing 1 parameter (message or soundprint)'

    else:
        response['error'] = 'must send GET request'

    return HttpResponse(json.dumps(response, indent=4), \
                        content_type='application/json')
Exemple #45
0
def test(ctx, enc, dec, test_data):
    global test_idx
    metric = mx.metric.MSE()
    samples = []
    for images, labels in test_data:
        features = encode(enc, images, labels)
        images_out = decode(dec, features, labels)
        metric.update([images], [images_out])

        idx = np.random.randint(images.shape[0])
        samples.append(mx.nd.concat(images[idx], images_out[idx], dim=2)[0].asnumpy())

    try:
        imgdir = '/tmp/mnist'
        save_images(samples[::2], imgdir, test_idx*1000)
        test_idx += 1
        print("test images written to", imgdir)
    except Exception as e:
        print("writing images failed:", e)

    return metric.get()
Exemple #46
0
 def get_tourney_name(self):
     return decode(self.tourney_name)
def perform_operations(message_body,message_length_size,message_type_size,conn) :
		#print "Just Echoing the message back to the client after verification of signature"
		payload_length = long(message_body[0:message_length_size])
		signed_digest_length = long(message_body[message_length_size:2*message_length_size])
		message_type = message_body[2*message_length_size:2*message_length_size + message_type_size]
		payload = message_body[2*message_length_size + message_type_size :2*message_length_size+ message_type_size + payload_length]
		signed_digest = long(message_body[2*message_length_size + payload_length + message_type_size:])

		h = SHA256.new()
		h.update(payload)
		computed_message_digest = h.hexdigest()
		input_tuple = (signed_digest,None)
		global total_number_of_adds
		global max_no_of_adds
		global flush_in_progress

		while flush_in_progress == 1 :
			pass
		
		if rsapubkey.verify(computed_message_digest,input_tuple) == True :
			print "Server : Signature Verification of the received message suceeded"

			if message_type == '01' : # ADD command
				print "Switched to ADD mode. Waiting for key@file_data ... "
				data = payload
				split_list = data.split('@')
				key = split_list[0]
				file_data_string = split_list[1]
				base_filename_to_store = key + ".txt"
				insert_command = "INSERT INTO " + TABLE_NAME + " SET " + "Timestamp=" + '"' + key + '"' + "," + " filename=" + '"' + key + ".txt" + '"' + ";"
				db = MySQLdb.connect(host="127.0.0.1", user= user_name, passwd= passwd, db=DATABASE_NAME) 
				# you must create a Cursor object. It will let
				#  you execute all the queries you need
				cur = db.cursor() 
				#print "Insert command = ", insert_command
				cur.execute(insert_command)			
				db.commit()
				db.close()
				encode(k=k,m=m,data_to_encode=file_data_string,base_filename_to_store = base_filename_to_store,ec_type = ec_type)

				#total_number_of_adds = total_number_of_adds + 1
				#if total_number_of_adds >= max_no_of_adds :
				#	total_number_of_adds = 0
				#	flush_all()
				conn.sendall("OK")
				
			elif message_type == '02' : #RETRIEVE Command
				#print "Switched to retrieve mode. Waiting for retrieve key ... "
				key = payload
				#print "Received key = ", key
				retrieve_command = "SELECT * FROM " + TABLE_NAME + " WHERE Timestamp=" + '"' + key + '";'
				#print "retrieve_command = ", retrieve_command
				db = MySQLdb.connect(host="127.0.0.1", user= user_name, passwd= passwd, db=DATABASE_NAME) 
				# you must create a Cursor object. It will let
				#  you execute all the queries you need
				cur = db.cursor() 
				cur.execute(retrieve_command)
				db.commit()
				db.close()
				# print all the first cell of all the rows
				decoded_data_list = []
				#print "Result of command = "
				for row in cur.fetchall() :
					#print "Row = ", row		
					# row should be file name to decode and fetch
					decoded_data = decode(k,m,row[1],ec_type)
		
					decoded_data_list.append(decoded_data)
				#print "Done"
				#print "Decoded data for transmission = "
				for entry in decoded_data_list :
					if entry == -1 :
						conn.sendall("-1")
					else :
						print entry
						#conn.sendall(entry)
						send_message(conn,entry,"OK")
				if len(decoded_data_list) == 0 :
					conn.sendall("-2")
				



		return
Exemple #48
0
    def convert(self, t, provide_points=False):
        tournament = {}
        ret = {}
        ret[TOURNAMENT] = tournament
        tournament[ID] = t.id
        tournament[NAME] = t.get_tourney_name()
        tournament[DATE] = str(t.tourney_date)
        tournament[TYPE] = t.tourney_type
        tournament[ROUND_LENGTH] = t.round_length
        if t.format is not None:
            tournament[FORMAT] = t.format
        if t.video_url is not None:
            tournament[VIDEO_URL] = t.get_video_url()

        if t.venue is not None:
            v = t.venue
            lon = v.longitude
            lat = v.latitude
            if lon is not None:
                lon = str(lon)
            if lat is not None:
                lat = str(lat)
            tournament[VENUE] = {
                VENUE_NAME: decode(v.venue),
                STATE: decode(v.state),
                CITY: decode(v.city),
                LATITUDE: lat,
                LONGITUDE: lon,
                COUNTRY: decode(v.country)
            }

        # build the tournament to ranking map
        # naive assumption: assume the rankings are there
        players = []
        tournament[PLAYERS] = players
        for ranking in t.rankings:
            player = {}
            player[PLAYER_NAME] = ranking.player.get_player_name()
            player[SCORE] = ranking.score
            player[MOV] = ranking.mov
            if ranking.sos is None:
                ranking.sos = 0
            sos = "%.2f" % ranking.sos
            player[SOS] = sos
            rank = {}
            player[RANK] = rank
            rank[SWISS] = ranking.rank
            if ranking.elim_rank is not None:
                rank[ELIMINATION] = ranking.elim_rank
            players.append(player)

            # if the player has a list, xws it
            tlist = ranking.player.get_first_tourney_list()
            if tlist and tlist.archtype_list:
                converter = XWSListConverter(tlist.archtype_list,provide_points=provide_points)
                player[LIST] = converter.data

        # and now the rounds
        rounds = []
        tournament[ROUNDS] = rounds
        for round in t.rounds:
            rhref = {}
            rounds.append(rhref)
            rhref[ROUND_TYPE] = round.round_type_str()
            rhref[ROUND_NUMBER] = round.round_num
            matches = []
            rhref[MATCHES] = matches
            for result in round.results:
                resref = {}
                matches.append(resref)
                resref[PLAYER1] = result.player1_name()
                resref[PLAYER2] = result.player2_name()
                resref[PLAYER1_POINTS] = result.list1_score
                resref[PLAYER2_POINTS] = result.list2_score
                resref[RESULT] = result.get_result_for_json()
                if result.final_salvo is not None:
                    resref[FINAL_SALVO] = result.final_salvo

        return ret
Exemple #49
0
    def post(self):
        json_data = None
        pm = PersistenceManager(myapp.db_connector)

        helper = TournamentApiHelper(pm)
        self.helper = helper

        try:
            json_data = request.get_json(force=True)
        except Exception:
            return helper.bail("bad json received!", 403)
        if json_data is not None:
            if json_data.has_key(TOURNAMENT):
                t = json_data[TOURNAMENT]

                # it should have all the required fields
                missing_field = helper.missing_required_field(t)
                if missing_field is not None:
                    return helper.bail(
                        "invalid tourney submission, must contain required fields, missing %s " % (missing_field),
                        403)

                tourney_name = t[NAME]
                tourney_date = t[DATE]
                tourney_type = t[TYPE]
                round_length = t[ROUND_LENGTH]
                participant_count = t[PARTICIPANT_COUNT]

                # validate the tourney date
                parsed_date = None
                try:
                    parsed_date = dateutil.parser.parse(tourney_date)
                except Exception:
                    return helper.bail("invalid tourney date %s" % (parsed_date), 403)

                # validate the tourney type
                if not tourney_type in TournamentApiHelper.tourney_types:
                    return helper.bail("invalid tourney type %s" % (tourney_type), 403)

                # good to go!
                tourney = Tourney(tourney_name=tourney_name, tourney_date=tourney_date,
                                  tourney_type=tourney_type, round_length=round_length, entry_date=parsed_date,
                                  participant_count=participant_count, locked=False)
                pm.db_connector.get_session().add(tourney)

                helper.extract_email(t, tourney)
                helper.extract_video_url(t,tourney)
                helper.extract_tourney_format(t, tourney)
                helper.extract_venue(t, tourney)
                # now see if the players are there.  if so, add 'em
                bail, tlists_by_id, tlists_by_name = helper.extract_players(t,
                                                                            tourney)  # round by round results, if it exists

                if bail:
                    return bail

                # set gook
                bailout = helper.extract_set(t, tourney, pm)
                if bailout:
                    return bailout

                bailout = helper.extract_rounds(t, tourney, tlists_by_id, tlists_by_name, pm)
                if bailout:
                    return bailout

                # looking good.
                # grab a uuid to finish the job
                tourney.api_token = str(uuid.uuid4())

                # and log it
                event = Event(remote_address=myapp.remote_address(request),
                              event_date=func.now(),
                              event="API",
                              event_details="tournament API: tourney creation via POST")

                pm.db_connector.get_session().add(event)
                pm.db_connector.get_session().commit()

                players = []
                for player in tourney.tourney_players:
                    players.append({NAME: player.get_player_name(), ID: player.id})

                response = jsonify({TOURNAMENT: {NAME: decode(tourney.tourney_name),
                                                 ID: tourney.id,
                                                 API_TOKEN: tourney.api_token,
                                                 PLAYERS: players}})
                response.status_code = 201
                return response

            else:
                return helper.bail(
                    "invalid tourney submission, must contain required fields, missing %s " % (TOURNAMENTS), 403)
        else:
            return helper.bail("invalid tourney submission, must contain a json payload", 403)
def flush_all(restarted):
	global flush_in_progress

	flush_in_progress = 0
	print "Flushing database ..."
	
	
		
	#Delete_command = "DELETE * FROM " + TABLE_NAME + ";"
	Delete_command = "TRUNCATE TABLE " + TABLE_NAME + ";"
	Select_all_command = "SELECT * FROM " + TABLE_NAME + ";"


	if restarted == 0 :
		# SELECT ALL FROM DATA BASE FOR TRANSMISSION TO CLOUD
		db = MySQLdb.connect(host="127.0.0.1", user= user_name, passwd= passwd, db=DATABASE_NAME) 
				# you must create a Cursor object. It will let
				#  you execute all the queries you need
		cur = db.cursor() 

		cur.execute(Select_all_command)
		fetched_data = cur.fetchall()
	
		db.commit()
		db.close()


	

	if restarted == 0 :
		#TRANSMIT DATA TO CLOUD HERE
		print "Flushing to Mongo DB"
		try :
			client = MongoClient(MONGO_HOST, MONGO_PORT)
			db = client[MONGO_DB_NAME]
			collection = db[MONGO_COLLECTION_NAME]
			posts = collection.posts
			count = 0
			min_timestamp = 10000000000
			max_timestamp = 0
			Timestamp_dict = {}
			
			for row in fetched_data:
				#print "Row = ", row		
				# row should be file name to decode and fetch
				decoded_data = decode(k,m,row[1],ec_type)
				transmit_dict = {}
				key_list = row[1].split('.')
				key = key_list[0]
				node_id_timestamp_list = key.split(':')
				node_id = node_id_timestamp_list[0]
				timestamp = node_id_timestamp_list[0]
				if node_id in Timestamp_dict.keys() :
					if int(timestamp) < Timestamp_dict[node_id]['min'] :
						Timestamp_dict[node_id]['min'] = int(timestamp)
					if Timestamp_dict[node_id]['max'] < int(timestamp) :
						Timestamp_dict[node_id]['max'] = int(timestamp)
					
				else :
					Timestamp_dict[node_id] = {}
					Timestamp_dict[node_id]['min'] = int(timestamp)
					Timestamp_dict[node_id]['max'] = int(timestamp)
				
				transmit_dict["_id"] = key
				transmit_dict["value"] = decoded_data
				post_obj = posts.insert_one(transmit_dict)

			client.close()
		except :
			print "Error during flush to MONGODB. Probably due to already existing record "

		# SEND a notification message to Central Query server with min and max timestamps flushed.
		sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		host = CENTRAL_QUERY_SERVER_HOST
		port = CENTRAL_QUERY_SERVER_PORT
		print "Flushed to Mongo DB"
		try :
			print "Connected to Central Query server"
			sock.connect((host,port))
			
			msg_to_transmit = str(my_IP_address)
			for node_id in Timestamp_dict.keys() :
				msg_to_strnamit += "@" + node_id + ":" + str(Timestamp_dict[node_id]['min']) + ":" + str(Timestamp_dict[node_id]['max'])
			sock.sendall(msg_to_transmit)
			sock.close()
		except socket.error , msg:
			print 'Connect failed to Central Query server. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
Exemple #51
0
 def set_from_txt_byte(self, txt_byte, encoding, untag=False):
     txt_u = decoder.decode(txt_byte, encoding)
     if untag:
         txt_u = unmarkup.unhtml(txt_u)
     self.txt_u = txt_u
__author__ = 'anton'

import image
import encoder
import decoder
import encoder_gpu


if __name__ == '__main__':
    img = image.load_image('nature.bmp')
    print img.width, img.height
    transformations = encoder_gpu.encode(img)
    print len(transformations), len(transformations[0])
    img_data = decoder.decode(10, img.width, img.height, transformations)
    print len(transformations)
    img = image.ImageData(img.width, img.height, img_data, img.mode)
    image.save_image('anture_test.bmp', img)
Exemple #53
0
def add_tourney():

    #TODO: better edge testing against user input
    name                  = decode( request.form['name'] )
    email                 = decode( request.form['email'] )
    type                  = request.form['tourney_type']
    mmddyyyy              = request.form['datepicker'].split('/')
    date                  = datetime.date( int(mmddyyyy[2]),int(mmddyyyy[0]), int(mmddyyyy[1])) #YYYY, MM, DD
    round_length_dropdown = request.form['round_length_dropdown']
    round_length_userdef  = request.form['round_length_userdef']
    tourney_format_def    = request.form['tourney_format_dropdown']
    tourney_format_custom = request.form['tourney_format_custom']
    participant_count     = int(request.form['participant_count'])
    sets_used             = request.form.getlist('sets[]')
    country               = decode(request.form['country'])
    state                 = decode(request.form['state'])
    city                  = decode(request.form['city'])
    venue                 = decode(request.form['venue'])

    round_length = None
    if round_length_dropdown is None or len(round_length_dropdown) == 0:
        round_length = int(round_length_userdef)
    else:
        round_length = int(round_length_dropdown)

    tourney_format = None
    if tourney_format_def is None or len(tourney_format_def) == 0:
        if tourney_format_custom is None or len(tourney_format_custom) == 0:
            tourney_format = xwingmetadata.format_default
        else:
            tourney_format = decode(tourney_format_custom)
    else:
        tourney_format = str(tourney_format_def)

    tourney_report  = request.files['tourney_report']

    mail_message("New tourney creation attempt", "Request to create tourney name %s from user %s of type %s on day %s with %d participants" % \
          ( name, email, type, date, participant_count) )

    if tourney_report:
        filename        = tourney_report.filename
        data            = None
        if tourney_report and allowed_file(filename):
            try:
                data = tourney_report.read()
                data = decode(data)
                cryodex = Cryodex(data, filename)
                t = create_tourney(cryodex, name, date, type, round_length,
                                   sets_used, country, state, city, venue, email, participant_count, tourney_format )
                sfilename = secure_filename(filename) + "." + str(t.id)
                save_cryodex_file( failed=False, filename=sfilename, data=data)

                event = Event(remote_address=myapp.remote_address(request),
                              event_date=func.now(),
                              event="create tourney",
                              event_details="created tourney %s from croydex input" % ( t.tourney_name ))
                pm = PersistenceManager(myapp.db_connector)
                pm.db_connector.get_session().add(event)
                pm.db_connector.get_session().commit()
                mail_message("New cryodex tourney created", "A new tourney named '%s' with id %d was created from file %s!" % ( t.tourney_name, t.id, filename ))
                print "getting tourney details"
                return redirect( url_for('get_tourney_details', tourney_id=t.id))
            except (Exception,UnicodeEncodeError ) as err:
                filename=str(uuid.uuid4()) + ".html"
                save_cryodex_file( failed=True, filename=filename, data=data)
                mail_error(errortext=str(err) + "<br><br>Filename =" + filename )
                return render_template( 'tourney_entry_error.html', errortext=str(err))


    else: #user didnt provide a cryodex file ... have to do it manually
        try:
            pm = PersistenceManager(myapp.db_connector)
            t = Tourney(tourney_name=name, tourney_date=date, tourney_type=type, locked=False,
                        round_length=round_length, email=email, entry_date=datetime.datetime.now(),
                        participant_count=participant_count,format=tourney_format)
            pm.db_connector.get_session().add(t)
            add_sets_and_venue_to_tourney(city, country, pm, sets_used, state, t, venue )
            pm.db_connector.get_session().commit()
            event = Event(remote_address=myapp.remote_address(request),
                          event_date=func.now(),
                          event="create tourney",
                          event_details="created tourney %s from manual input" % ( t.tourney_name ))
            pm = PersistenceManager(myapp.db_connector)
            pm.db_connector.get_session().add(event)
            pm.db_connector.get_session().commit()
            mail_message("New manual tourney created", "A new tourney named '%s' with id %d was created!" % ( t.tourney_name, t.id ))
            return redirect(url_for('get_tourney_details', tourney_id=t.id))
        except Exception as err:
            mail_error(errortext=str(err))
            return render_template( 'tourney_entry_error.html', errortext=str(err))
Exemple #54
0
 def get_event_details(self):
     if self.event_details is None or len(self.event_details) == 0:
         return ""
     else:
         return decode(self.event_details)
Exemple #55
0
def download(conns, download_dir, files, inBackground = False, guiInfo = None):
    # some stats
    leech_start = time.time()
    leech_raw_bytes = 0
    leech_files = 0
    leech_speeds = []
    totalFiles  = len(files)

    # download + decode files
    fileNumber = 0
    for file in files:
        fileNumber += 1
        # TODO: skip file if already downloaded
        #if file["downloaded"] == True:
        #    continue

        if not inBackground:
            print "  %s" % file["subject"]
            print "  + downloading"

        # prepare connections
        active = []
        ready = []
        for conn in conns:
            conn.setblocking(0)
            ready.append(conn.fd())
    
        # stats
        file_start = time.time()
        file_speeds = []
        file_raw_bytes = 0

        # vars
        skipfile = False
        segments = file["segments"]
        decodeList = segments[:]
        lastSegment = ''

        if not inBackground:
            # update gui stats
            guiInfo["subject"]        = file["subject"]
            guiInfo["tsegments"]      = len(file["segments"])
            guiInfo["segments"]       = file["segments"]
            guiInfo["tfiles"]         = totalFiles
            guiInfo["fnum"]           = fileNumber
            guiInfo["file_raw_bytes"] = 0
            guiInfo["file_start"]     = file_start
            guiInfo["file_size"]      = file["size"]
            guiInfo["transfering"]    = True
            guiInfo["hide"]           = False

        while 1:
            if ready:
                # theres stuff left to do
                if segments and not skipfile:
                    segment = segments.pop()
                    fd = ready.pop()
                    active.append(fd)
                    nwrap = utils.fdToConn(fd, conns)
                    nwrap.body("<%s>" % segment)
                    lastSegment = segment
                # lets get out of here
                elif not active:
                    break
            # lets dance
            read = select.select(active, [], [], 0)[0]
            if read:
                for fd in read:
                    nwrap = utils.fdToConn(fd, conns)
                    # update stats
                    read_bytes, done = nwrap.recv_chunk()
                    file_raw_bytes += read_bytes
                    leech_raw_bytes += read_bytes
                    if not inBackground:
                        # update gui
                        guiInfo["file_raw_bytes"] = file_raw_bytes
                    if done:
                        speed = file_raw_bytes / 1024.0 / (time.time() - file_start)
                        file_speeds.append(speed)
                        # write to disk
                        fn = nwrap.resp.split(" ")[2][1:-1]
                        f = open("cache/%s" % fn,"wb")
                        for line in nwrap.lines:
                            f.write(line + "\r\n")
                        f.close()
                        nwrap.reset()
                        active.remove(fd)
                        ready.append(fd)
                        break
                    else:
                        # check for error
                        if len(nwrap.lines) == 1:
                            if nwrap.lines[0][:3] in ('423', '430'):
                                if not inBackground:
                                    print "      * error: file segment missing, file is corrupt"
                                nwrap.reset()
                                active.remove(fd)
                                ready.append(fd)
                                break
                            elif not nwrap.lines[0].startswith('2'):
                                print "      * error: %s" % nwrap.lines[0]
                                nwrap.reset()
                                active.remove(fd)
                                ready.append(fd)
                                skipfile = True
                                break
            # this lessens the cpu load
            time.sleep(0.01)
            # are we skipping this file?
            if skipfile:
                break

        if not inBackground:
            # clear gui info
            guiInfo["transfering"] = False

        # skip!
        if skipfile:
            continue

        # spit out some nice info
        dur = time.time() - file_start
        # TODO: reconnect to servers if lag in speed
        #print file_speeds
        speed = 0
        try:
            speed = int(sum(file_speeds)/len(file_speeds))
        except:
            pass
        size = utils.nsize(file_raw_bytes)
        if not inBackground:
            # no files transfered?
            if speed == 0:
                print "      * error: percentage of missing segments is 100%, skipping file"
            print "      * transferred %s in %s at %dKB/s" % (size, utils.ntime(dur), speed)

        # no files transfered?
        if speed == 0:
            continue
        # global stats
        leech_files += 1
        leech_speeds.append(speed)
    
        # clean up
        for conn in conns:
            conn.setblocking(1)

        # decode
        if not inBackground:
            print "  + decoding"
        dec_start = time.time()
        decoder.decode(decodeList, download_dir)
        dur = time.time() - dec_start
        if not inBackground:
            print "      * decoded %s in %s" % (size, utils.ntime(dur))

        # TODO: after we decode, pickle out to unfinished.tmp the remaining files
        file["downloaded"] = True
        file["decoded"] = True

    if not inBackground:
        # update gui info
        guiInfo["hide"] = True

    # summary of session
    dur = time.time() - leech_start
    speed = 0
    try:
        speed = sum(leech_speeds)/len(leech_speeds)
    except:
        pass
    size = utils.nsize(leech_raw_bytes)
    if not inBackground:
        if leech_files == 1:
            print "\n  received %s file (%s) in %s at %dKB/s" % (leech_files, size, utils.ntime(dur), int(speed))
        else:
            print "\n  received %s files (a total of %s) in %s at %dKB/s" % (leech_files, size, utils.ntime(dur), int(speed))

    # remove leftover junk
    segfiles = os.listdir("./cache/")
    for segfile in segfiles:
        try:
            os.remove("./cache/%s" % segfile)
        except:
            pass
        
    return { "files" : leech_files, "size" : size, "time" : utils.ntime(dur), "speed" : int(speed) }
Exemple #56
0
batchCount = 0

for route in routes:
	if startPush == False and ( route['startID'] == lastFrom and route['endID'] == lastTo ):
		startPush = True
	elif startPush == False:
		continue

	if batchCount == 0:
		postRoutes = []

	start = str(route['startID'])
	end = str(route['endID'])
	print 'from ' + start + ' to ' + end
	overview_polyline = route['overview_polyline']
	points = decoder.decode(overview_polyline)
	# pointsStr = '|'.join(','.join(map(str, p)) for p in points)

	one = {}
	one['start'] = route['startID']
	one['end'] = route['endID']
	one['duration'] = route['duration']
	one['distance'] = route['distance']
	one['points'] = points

	postObj = {}
	postObj['method'] = 'POST'
	postObj['path'] = '/1/classes/route'
	postObj['body'] = one
	postRoutes.append(postObj)
Exemple #57
0
 def get_player_name(self):
     return decode(self.player_name)
 def test_decode(self):
     value = decoder.decode("0010011")
     self.assertTrue(value == "xxyxz")
Exemple #59
0
 def player2_name(self):
     if self.list2 is None:
         return ""
     return decode(self.list2.player.player_name)
Exemple #60
0
 def player1_name(self):
     return decode(self.list1.player.player_name)