Beat_Slayer 17 Posting Pro in Training

Texts K-Nearest Neighbor (KNN) using the Euclidean algorithm

Cheers and Happy coding.

import cPickle
import re
from math import sqrt

class Words_Works():

    def __init__(self):
        self.all_texts = {}
        self.categories = {}
        self.knn_results = {}
        self.leaf_words = ['s', 'es', 'ed', 'er', 'ly', 'ing']
        self.stop_words = ['a', 'i', 'it', 'am', 'at', 'on', 'in', 'of', 'to', 'is', 'so', 'too', 'my', 'the', 'and', 'but', 'are', 'very', 'here', 'even', 'from', 'them', 'then', 'than', 'this', 'that', 'though']

    def load_categories(self):
        try:
            cat_db = open('categories.pkl', 'rb')
            self.categories = cPickle.load(cat_db)
            cat_db.close()
        except:
            print 'Load of categories file failed'

    def add_category(self, f, cat_name):
        f_in = open(f)
        self.text = f_in.read().lower()
        f_in.close()
        self.wordify()
        self.unstopify()
        self.unleafify()
        self.categories[cat_name] = {}        
        for item in self.unleaf:
            if self.categories[cat_name].has_key(item):
                self.categories[cat_name][item] += 1
            else:
                self.categories[cat_name][item] = 1

    def save_categories(self):
        cat_db = open('categories.pkl', 'wb')
        cPickle.dump(self.categories, cat_db, -1)
        cat_db.close()

    def add_text(self, f):
        f_in = open(f)
        self.text = f_in.read().lower()
        f_in.close()
        self.wordify()
        self.unstopify()
        self.unleafify()
        self.indexify()
        self.all_texts[f] = {}        
        for item in self.unleaf:
            if self.all_texts[f].has_key(item):
                self.all_texts[f][item] += 1
            else:
                self.all_texts[f][item] = 1

    def wordify(self):
        words_pat = re.compile('\\w+')
        self.words = words_pat.findall(self.text)

    def unstopify(self):
        self.unstop = [item for item in self.words if item not in self.stop_words]

    def unleafify(self):
        self.unleaf = self.unstop[:]
        for leaf in self.leaf_words:
            leaf_len = len(leaf)
            leaf_pat = re.compile('%s$' % leaf)
            for i in range(len(self.unleaf)):
                if leaf_pat.findall(self.unleaf[i]):
                    self.unleaf[i] = self.unleaf[i][:-leaf_len]

    def knn_calc(self):
        for text in self.all_texts.keys():
            self.knn_results[text] = {}
            for category in self.categories.keys():
                self.knn_results[text][category] = {}
                iterations = 0
                distance = 0
                for word in self.all_texts[text].keys():
                    if word in self.categories[category].keys():
                        distance += (self.all_texts[text][word] - self.categories[category][word]) ** 2
                        iterations += 1 …
Beat_Slayer 17 Posting Pro in Training

Aproach to the implementation of K-Nearest Neighbor (KNN) using the Euclidean algorithm.

Sample Usage:

mywork = Words_Works()

lit = 'literature.txt'
mywork.add_category(lit, 'Literature')         # adding files as category
comp = 'computers.txt'
mywork.add_category(comp, 'Computers')
phy = 'physics.txt'
mywork.add_category(phy, 'Physics')
                                               # saving categories dictionary to file
mywork.save_categories()                       # can be loaded calling load_categories()

print mywork.categories                        # prints categories dictionary
print

txts = ('sample1.txt', 'sample2.txt')          # creating list of files to add

for text in txts:
    mywork.add_text(text)                      # adding files

print mywork.all_texts                         # prints files word ocurrence count
print

mywork.knn_calc()                              # perform calculation 

print mywork.knn_results                       # print overall results
print

mywork.knn()                                   # print files results

Cheers and Happy coding!

Beat_Slayer 17 Posting Pro in Training

I don't know if it is this function but this should work.

from scipy.special import kv

Happy coding

Beat_Slayer 17 Posting Pro in Training

I use cx_freeze on win32 without minimum problem.

Have you tried this?

py2app - Create standalone Mac OS X applications with Python

Happy coding

Beat_Slayer 17 Posting Pro in Training

It works with your file again. :)

f_in = open('blocks.txt').read()
f_out = open('output.csv', 'w')

f_out.write('AC\tID\tFA\tOS\tSF\tBS\tGE\n')

blocks = [x for x in f_in.split('//') if x]

for item in blocks:
    infos = [x for x in item.split('\n') if x and x != 'XX']
    AC = ''
    ID = ''
    FA = ''
    OS = ''
    SF = ''
    BS = ''
    GE = ''
    for field in infos:
        if field.startswith('AC'):
            AC += ' ' + field[3:]
        elif field.startswith('ID'):
            ID += ' ' + field[3:]
        elif field.startswith('FA'):
            FA += ' ' + field[3:]
        elif field.startswith('OS'):
            OS += ' ' + field[3:]
        elif field.startswith('SF'):
            SF += ' ' + field[3:]
        elif field.startswith('BS'):
            BS += ' ' + field[3:]
        elif field.startswith('GE'):
            GE += ' ' + field[3:]

    f_out.write('%s\t%s\t%s\t%s\t%s\t%s\t%s\n' % (AC, ID, FA, OS, SF, BS, GE))
    
f_out.close()

Cheers and Happy coding

Beat_Slayer 17 Posting Pro in Training

Your welcome.

You can mark as solved,

Cheers and Happy coding

Beat_Slayer 17 Posting Pro in Training

You must provide full path to the dtsx file I believe.

import os

PACKAGENAME = 'something'
os.system(r'C:\\dtexec /f "c:\\folder\\%s.dtsx"' % PACKAGENAME)
Beat_Slayer 17 Posting Pro in Training

The 'r' stands for raw, so the string is interpreted as it is, otherwise it must be:

import os

os.system(r'C:\\dtexec /f "PACKAGENAME.dtsx"')

because of the backslash interpreted as a escape.

You can also do:

import os

PACKAGENAME = 'something'
os.system(r'C:\\dtexec /f "%s.dtsx"' % PACKAGENAME)
Beat_Slayer 17 Posting Pro in Training

With a simple hex editor you can see that the GIMP file headers are completely different.

Cheers and happy coding

Beat_Slayer 17 Posting Pro in Training
import os

os.system(r'C:\dtexec /f "PACKAGENAME.dtsx"')

Cheers and Happy codding

Beat_Slayer 17 Posting Pro in Training

If the submitted form data contains more than one field with the same name, the object retrieved by form[key] is not a FieldStorage or MiniFieldStorage instance but a list of such instances. Similarly, in this situation, form.getvalue(key) would return a list of strings. If you expect this possibility (when your HTML form contains multiple fields with the same name), use the getlist() function, which always returns a list of values (so that you do not need to special-case the single item case).

Source

I guess you have more than one field.

Beat_Slayer 17 Posting Pro in Training

I don't really understand the context but...

def high_Priority():
    ##
    
    print''
    import shelve

    file_sizeh = file_size * week_selec
    db = shelve.open("netback.dat")
    db['hremspace'] = h_back
    if h_back > 0:
        print'There are ',db['hremspace'],' Mb\'s left in High priority storage'
        print'after your backup of ',file_sizeh,' Mb\'s'
        db['hremspace'] = h_back
        db['netback'] = back_name
        db['Highprofile'] = file_sizeh
    else:
        print'There is not enough space to save your backup.'
        print'Please delete a backup and try again.'
    db.close()

Cheers and Happy coding

Beat_Slayer 17 Posting Pro in Training

I believe the problem relys on input file.

It works here with the sample file you provided.

Can you provide more data and info.

Cheers

Beat_Slayer 17 Posting Pro in Training

I convert no xml.

The '.itl' file is binnary, I convert it to hex so it's more easy to search and read the '.itl' key.

When replacing the key on '.itl' file, I read it, convert to hex, replace the key, and output it converted as bin again to file.

I don't see a need for performance in such a small task.

Cheers and happy coding.

Beat_Slayer 17 Posting Pro in Training

I got it. LOL Pretty easy also. :)

Because you should be doing a 'cur.prepare' and not an 'cur.execute'.

Execute for actions on the table, prepare for visualization.

Beat_Slayer 17 Posting Pro in Training

The implementation is on the page of the snippet.

You copy the class to your script, then do:

fxml = 'iTunes Music Library.xml'       # path to xml file
fitl = 'iTunes Library.itl'             # path to itl file

itk = ITunesLibKeys(fxml, fitl)

if not itk.valid_key:                      # if keys don't match
    itk.xml_key_replace(itk.itl_key)       # replace xml key with the itl key

Any question, just fell free to ask.

Beat_Slayer 17 Posting Pro in Training

Like this?

totalRows = int(raw_input ("Please enter a number: ")) 

for currentRow in range(1, totalRows + 1):
    print '* ' * currentRow

print

for currentRow in reversed(range(1, totalRows + 1)): 
    print '* ' * currentRow
Beat_Slayer 17 Posting Pro in Training

Maybe like this?

import string


def RemovePunc(text_input):
    line = []
    i = 0
    total_text_input = ""
    #This part removes the punctuation and converts input text to lowercase
    new_char_string = "" 
    for char in text_input:
        if char in string.punctuation:
            char = " "
                    
        new_char_string = new_char_string + char
                
    line = line + [new_char_string.lower()]
    #This is a list with all of the text that was entered in
    total_text_input = (total_text_input + new_char_string).lower()
    return line

def RemoveStopWords(line):
    line_stop_words = []
    stop_words = "a","i","it","am","at","on","in","of","to","is","so","too","my","the","and","but","are","very","here","even","from","them","then","than","this","that","though"
    #this part removes the stop words for the list of inputs
    line_stop_words = []
    sent = ""
    word = ""
    test = []
    for sent in line:
        word_list = string.split(sent)
        new_string = ""
        for word in word_list:
            if word  not in stop_words:
                new_string = new_string + word + " "
        new_string = string.split(new_string)
        line_stop_words = line_stop_words + [new_string]
    return(line_stop_words)


def StemWords(line_stop_words):
    leaf_words = "s","es","ed","er","ly","ing"
    i=0
    while i < 6:    
        count = 0
        length = len(leaf_words[i])
        while count < len(line_stop_words):
            line = line_stop_words[count]
            count2 = 0
            while count2 < len(line):
                #line is the particular list(or line) that we are dealing with, count if the specific word
                if leaf_words[i] == line[count2][-length:]:
                    line[count2] = line[count2][:-length]
                count2 = count2 + 1
            line_stop_words[count] = line
            count2 = 0
            count = count + 1
        count = 0
        i = i + 1
    return(line_stop_words)

def indexDupe(lineCount,occur):
    if str(lineCount) in occur:
        return True
    else:
        return False

def Indexing(line_stop_words):
    line_limit = len(line_stop_words)
    index = []
    line_count = 0

    while line_count < line_limit:
        for x in line_stop_words[line_count]:
            count = …
Beat_Slayer 17 Posting Pro in Training

Like this?

#!/usr/bin/env python

import urllib

def save_page(site="http://www.tvrage.com/Warehouse_13/episode_list"):
    mypath = site
    f = open('temp2.txt', 'w')
    for item in urllib.urlopen(mypath).readlines():    
        f.write(item)
    f.close()

def find_title(temp="temp2.txt"):
    f = open(temp)
    site = f.readlines()
    f.close()
    for item in site:
        if item.find('<title>') != -1:
            before_html, tag_before, rest_html = str(item).partition('<title>')
            title, tag_after, after_html = rest_html.partition('</title>')
    print 'Title:', title

def find_episodes(temp="temp2.txt"):
    f = open(temp)
    site = f.readlines()
    f.close()
    for item in site:
        if item.find('''onmouseover="showToolTip2(event,'View Trailer');return false;" onmouseout="hideToolTip2();" ></a> <a href='/Warehouse_13/episodes/''') != -1:
            before_html, tag_before, rest_html = str(item).partition('''onmouseover="showToolTip2(event,'View Trailer');return false;" onmouseout="hideToolTip2();" ></a> <a href='/Warehouse_13/episodes/''')
            title, tag_after, after_html = rest_html.partition('</a> </td>')
            print 'Episodes:', title[12:]

save_page()
find_title()
find_episodes()

Happy coding.

stam0283 commented: this is very nice. it helps you avoid regexes, which is awesome. +0
Beat_Slayer 17 Posting Pro in Training

I don't get it.

Something weird. :D

Can you post the db.

Beat_Slayer 17 Posting Pro in Training

ITunes Library Persistent ID Editor

I had nothing to do. ;)

lol

Happy coding

Beat_Slayer 17 Posting Pro in Training

ITunes Library Persistent ID Editor.

Sample usage:

fxml = 'iTunes Music Library.xml'    # path to xml file
fitl = 'iTunes Library.itl'          # path to itl file

itk = ITunesLibKeys(fxml, fitl)

print itk.file_xml                   # prints path to xml file
print itk.file_itl                   # prints path to itl file
print itk.xml_key                    # prints key on xml
print itk.itl_key                    # prints key on itl
print itk.valid_key                  # prints True if keys match

itk.new_user_key()                   # ask for user input key

itk.xml_key_replace(itk.itl_key)     # replace key on xml file with the itl key

itk.itl_key_replace(itk.new_key)     # replace key on itl with user input key
fxml = 'iTunes Music Library.xml'       # path to xml file
fitl = 'iTunes Library.itl'             # path to itl file

itk = ITunesLibKeys(fxml, fitl)

if not itk.valid_key:                      # if keys don't match
    itk.xml_key_replace(itk.itl_key)       # replace xml key with the itl key
Beat_Slayer 17 Posting Pro in Training

You can do the rest. ;)

import binascii


def xml_key_find(xml_file):
    f_x = open(xml_file)
    f_xml = f_x.readlines()
    f_x.close()    
    for item in f_xml:
        if item.find('<key>Library Persistent ID</key>') != -1:
            before_xml, tag_before, rest_xml = str(item).partition('<key>Library Persistent ID</key><string>')
            key_xml, tag_after, after_xml = rest_xml.partition('</string>')
            print "Key on 'iTunes Music Library.xml':", key_xml
    return key_xml

def xml_key_replace(xml_file, old_key, new_key):
    f_x = open(xml_file)
    f_xml = f_x.read()
    f_x.close()    
    f_xml = f_xml.replace(old_key, new_key)
    f_x = open(xml_file, 'w')
    f_x.write(f_xml)
    f_x.close()

def itl_key_compare(itl_file, old_key):
    f_i = open(itl_file, 'rb')
    f_itl_bin = f_i.read()
    f_i.close()
    f_itl_hex = str(binascii.hexlify(f_itl_bin)).upper()
    results = f_itl_hex.find(old_key)
    if results != -1:
        return True
    else:
        return False

def itl_key_replace(itl_file, old_key, new_key):
    f_i = open(itl_file, 'rb')
    f_itl_bin = f_i.read()
    f_i.close()
    f_itl_hex = str(binascii.hexlify(f_itl_bin)).upper()
    f_itl_hex = f_itl_hex.replace(old_key, new_key.upper())
    f_i = open(itl_file, 'wb')
    f_itl_bin = binascii.unhexlify(f_itl_hex)
    f_i.write(f_itl_bin)
    f_i.close()

file_xml = 'iTunes Music Library.xml'

xml_key = xml_key_find(file_xml)

print xml_key

user_key = 'E5F4BA35355AC51A'

xml_key_replace(file_xml, xml_key, user_key)

file_itl = 'iTunes Library.itl'

valid = itl_key_compare(file_itl, xml_key)

print valid

itl_key_replace(file_itl, xml_key, user_key)

Just tell if you need help, hope the coding is clear enought.

Cheers and Happy coding.

EDIT: the code it's Pyhton 2.x, but it's easilly adjustable.

Beat_Slayer 17 Posting Pro in Training

Can you post as file or PM it to me, it's a litle messy to copy.

Cheers and Happy coding

Beat_Slayer 17 Posting Pro in Training

Can you provide a sample file.

Your sample doesn't even have the GE.

I changed the second BS tag to GE.

f_in = open('blocks.txt').read()
f_out = open('output.csv', 'w')
f_out.write('AC\tID\tFA\tOS\tSF\tBS\tGE\n')

blocks = [x for x in f_in.split('//') if x]
for item in blocks:
    infos = [x for x in item.split('\n') if x and x != 'XX']
    for field in infos:
        if field.startswith('AC'):
            f_out.write('%s\t' % field[3:])
        elif field.startswith('ID'):
            f_out.write('%s\t' % field[3:])
        elif field.startswith('FA'):
            f_out.write('%s\t' % field[3:])
        elif field.startswith('OS'):
            f_out.write('%s\t' % field[3:])
        elif field.startswith('SF'):
            f_out.write('%s\t' % field[3:])
        elif field.startswith('BS'):
            f_out.write('%s\t' % field[3:])
        elif field.startswith('GE'):
            f_out.write('%s\t\n' % field[3:])

f_out.close()
Beat_Slayer 17 Posting Pro in Training

Can you provide a sample xml and itl, it's a pretty easy task to achieve.

Cheers and Happy coding...

Beat_Slayer 17 Posting Pro in Training

Yes, snippsat told you how to do it correctly some posts before.

Can you try like that, eihter of the ways, they do the task pretty nicelly.

Happy coding

Beat_Slayer 17 Posting Pro in Training

Like this?

def periodic():
    global t 
    print "Tic-Tac"
    t = threading.Timer(2, periodic)
    t.start()

t = threading.Timer(2, periodic)
t.start()

Happy coding

Beat_Slayer 17 Posting Pro in Training
import os
import signal

processname = 'sysproc'

for line in os.popen('ps xa'):
    if processname in line.split()[4]:
        os.kill(int(line.split()[0]), signal.SIGHUP)
Beat_Slayer 17 Posting Pro in Training

How do you get those times?

Beat_Slayer 17 Posting Pro in Training

Well you need Pyserial or USPP if you connect through the COM port.

And from there on, more info is needed.

Happy coding

Beat_Slayer 17 Posting Pro in Training

One question.

When the player looses, how are you saving the highscore?

In your example you show a player that played and then you call the highscore from the menu.

Beat_Slayer 17 Posting Pro in Training

You must return the value from the recursive function.

def function(depth):
    if depth > 0:
        return function(depth - 1)
    else:
        return 10

function(5)

EDIT: Sorry, was testing it while it was answered.

Beat_Slayer 17 Posting Pro in Training

There are no aditional modules there.

You can even use the cElementTree module also included on python.

Beat_Slayer 17 Posting Pro in Training

lol

How about some search?

I've written something easilly adjustable for some user something like one week ago.


Finding string instance in file - replace bytes after it

Beat_Slayer 17 Posting Pro in Training

Thats not complicated. If there is a fixed code lenght you can call them like:

codeA, codeB = CAcodes.split(' ')

and eliminate the for's.

EDIT: I'll read your code when I return.

Beat_Slayer 17 Posting Pro in Training

What I mean by taking out the breaks, was to design the code in other way without that logical path.

Beat_Slayer 17 Posting Pro in Training

I'm sorry but I'm going to redirect you. lol

I believe this is the right point to redirect you.

Beat_Slayer 17 Posting Pro in Training

This is what I've been trying to work with, but it just keeps printing out whatever is on the file.

def highscore():
    newScore = sum(points)
    print
    openHigh = open("highScore.txt", 'r')
    highScore = openHigh.read()
    print highScore
    print newScore

    if newScore > highScore:
        openHigh.write(newScore)
        openHigh.close()
        print highScore

You have the file open as reading mode. Thats why.

openHigh = open("highScore.txt", [B]'r'[/B])
Beat_Slayer 17 Posting Pro in Training

The breaks are your problem. Try to take them out.

Happy coding...

Beat_Slayer 17 Posting Pro in Training

Here.

score.txt content before:

Jane-60
f_score = open ('score.txt')
old_highscore = f_score.readlines()[0]
f_score.close()
old_name, old_score = old_highscore.split('-')
name = 'Joe'
score = 80
print old_name, old_score
if score > int(old_score):
    print name, score
    f_score = open ('score.txt', 'w')
    f_score.write('%s-%s' % (name, score))
    f_score.close()

score.txt content after:

Joe-80
Beat_Slayer 17 Posting Pro in Training

See my example.

Write the name and the socre with some separator.

Then do some spliting on reading, and compare only the last field.

Beat_Slayer 17 Posting Pro in Training

And the code?

I can't see it. lol

:)

f_score = open ('score.txt')
old_score = int(f_score.readlines()[0])
f_score.close()
score = 80
print old_score
if score > old_score:
    f_score = open ('score.txt', 'w')
    f_score.write('%s' % score)
    f_score.close()
Beat_Slayer 17 Posting Pro in Training

Here it is.

import warnings

def maxgen(fname, worksheet=1,encoding='cp1251'):
    warnings.filterwarnings('ignore')
    from pyExcelerator import *
    warnings.filterwarnings('always')

Happy coding...

Beat_Slayer 17 Posting Pro in Training

Maybe a sample file and some info would help mate.

I'm just trying to help, but I have litle insight on this.

And this?

Beat_Slayer 17 Posting Pro in Training

I believe I have something for you.

Parsing Simulink mdl files with Pyparsing

Maybe you can adapt or reuse some of the code, or at least get a base to start with.

Beat_Slayer 17 Posting Pro in Training

Gribouillis said it all.

There is no reason for repeating code when it can be reused.

Beat_Slayer 17 Posting Pro in Training

I saw your scripts just now.

So what do you want?

You searching for a way of importing MDL0? Right?

Beat_Slayer 17 Posting Pro in Training

Wich windows it is?

And how about the trying to run it as admin, or in compatibility mode?

I never had problems.

Beat_Slayer 17 Posting Pro in Training

You need to start here, so you can load those collada files and work with them.