jice 53 Posting Whiz in Training

You can use the csv module :
http://docs.python.org/library/csv.html

or, as your case is simple, do it yourself :

f=open('myfile.txt','r') # Opens the file read only
d={} # Dict to store result
for line in f:  # For each line of the file
    sline=line.rstrip('\n').split(" | ")  # split the line according to separators used in your file (here " | ")
    if len(sline)>1: # we don't want to process empty lines
        sline.sort() # to have the same order for all records (avoid double testine)
        key=tuple(sline) # Dictionary keys can't be lists
        try:
            d[key]+=1 # Add one
        except KeyError: # If the key doesn't exists yet, count is 1
            d[key]=1
print d
f.close()
jice 53 Posting Whiz in Training

You can use regexp (re module) instead of string module for that.

jice 53 Posting Whiz in Training

you can use the pprint module to format lists, tuples or dictionaries into readable strings...

jice 53 Posting Whiz in Training
jice 53 Posting Whiz in Training

Can you rewite the code and put here so that I will understand what you are saying! By the way thanks for response
Steve

# SMD Inc since 2003.py

import wx
ID_ABOUT = 100
ID_PRINT = 101
ID_EXIT  =102

class MainWindow(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(500, 350))
        self.CreateStatusBar()
        self.SetStatusText("Displays program Information")
        
        menubar = wx.MenuBar()
        # File menu
        filemenu = wx.Menu()
        filemenu.Append(ID_PRINT, "&Print", "Print the current Window")
        filemenu.Append(ID_EXIT, "E&xit", "Exit the program")
        menubar.Append(filemenu, "&File")
        # Help Menu
        helpmenu = wx.Menu()
        helpmenu.Append(ID_ABOUT, "&File", "About this program")
        menubar.Append(helpmenu, "He&lp")

        # Attach and set Menubar on Frame
        self.SetMenuBar(menubar)

        # Binding Events
        self.Bind(wx.EVT_MENU, self.OnPrint, ID_PRINT)      
        
       

        self.Centre()
        self.Show(True)

    # Setting methods
    def OnPrint(self, event):
        self.close()
        dia = wx.MessageDialog(self, "Thank you!", "About the program", wx.OK)
        dia.ShowModal()
        dia.Destroy()      
        
app = wx.App()
MainWindow(None, -1, "Elijah's SMD Inc. since 2003")
app.MainLoop()
jice 53 Posting Whiz in Training

Please, use (code=python)(/code) tags around your code to make it readable...

jice 53 Posting Whiz in Training

I don't know beautifulsoup as i don't parse xml nor html. So, i won't be able to help you but if you google "beautifulsoup tutorial", you'll have plenty of examples.

There's another thread about a similar problem here
http://www.daniweb.com/forums/thread90256.html

jice 53 Posting Whiz in Training

This code goes recursively in all the sudirs of your rootdir. Open all the files, read the lines, calls a function to process your line (here, it does nothing) and overwrite your files with your new lines

import os

rootdir='c:\Your\Path'

def doWhatYouWant(line):
    return line

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        f=open(file, 'r')
        lines=f.readlines()
        f.close()
        f=open(file, 'w')
        for line in lines:
            newline=doWhatYouWant(line)
            f.write(newline)
        f.close()
jice 53 Posting Whiz in Training

In your code :
1. when a and b are outside ]1, 53[, you do nothing except looping while true (infinite loop) - look at indentation.
2. If 1<a<b, you don't control if b is allright. You immediately break the loop

def lasin ():
    a=int( input ("Put in a 1<digit<53\n"))
    b=int( input ("put in a 1<digit<53\n"))  
    return a,b

def kontroll(a,b):
    while True:
        try:
             if a>1 and a<53 and b>1 and b<53:   
                break
        except NameError:
            print "Try again"
        (a,b)=lasin()
jice 53 Posting Whiz in Training

In this case, this is not a problem because dictionary is used only for a control purpose. The order of the sql statements is the same as the in file's one...

jice 53 Posting Whiz in Training

Sorry if this is explained too fast.

About dictionaries.
A dictionary is a type which variables are built like this
dict={"key1":"value1", "key2":"value2,...}
Keys and values can be whatever you want.
each key is unique and you can recall a value like this
value=dict["key1"]

So, to be sure you haven't any duplicate, you can create a dictionary that has your lines as keys :
dict[line]=None (or whatever you want, we only use it for the keys).

Before creating this new record, you can test if the key already exists by using dict.has_key(line)
If this returns True, the key exists so you have already processed a line like this.

for line in listLines:
    if not dict.has_key(line): # We test if the line is processed
        generateSQLStatement()
        dict[line]=''   # We say that this line is processed.

That's all. But anyway, if your solution is OK...

jice 53 Posting Whiz in Training

You don't need the insert statement, only the line...

@woooee : The key in list may not be the fastest but (just as you do for your sorting) if you cut the list in a dictionnary of 1000 lists
something like (very simple sample) :

import String

dict={}
for i in range(1000):
    dict[String.zfill(i,3)]=[]  # to fill with 0 (001)
for line in file(filename):
    if not (line in dict[line[x:x+3]):   # x points somewhere in the line where you are waiting 3 following numbers
        generateSQLStatement()
        dict[line[x:x+3]].append(line)

then, you only have to search ONE list.
When you sort, you have to sort all the lists...
I'm not sure this is better than sorting, i didn't make tests, but this is certainly simpler to write if you have to write your own sorting algorithm ;)
This may not be the best solution, I agree. In fact, i don't have very often to deal with this kind of problems...

Anyway, about the dictionary (probably faster anyway), you have have at least 2 or 3 ways to do it.
The simplest (i don't know if it is the most efficient) may be :

dict={}
for line in file(filename):
    if not dict.has_key(line):
        generateSQLStatement()
        dict[line]=None # we don't care about the value

Of course here again you'd better create numbers of sub-dictionaries (for example as i showed just before for the lists)

jice 53 Posting Whiz in Training

I'm not sure it is required to sort the lists.
You may proceed just as woooee says about soring keys in lists and then simply test "if not key in list".
Something like (i just do 1 list here to be simple)

alreadyStored=[]
for line in file(filename):
    if not (line in alreadyStored): # You may use part of the line, depending on the key you want to use
        generateSqlInsert() # You know how to do this
        alreadyStored.append(line)

Of course, it is better to define more than one list but I think than testing presence in the list is faster than sorting (I didn't test so maybe am I wrong)

By the way, to increase readability, instead of writing

'insert into database.table values (scn_telco.npanxx_id_seq.nextval,0,\
\'%s\',\'%s\',%s,%s,\'%s\',\'%s\',\'%s\',\'X\',\'%s\',%s,\'\');\n'

You can use "

"insert into database.table values (scn_telco.npanxx_id_seq.nextval,0,\
'%s','%s',%s,%s,'%s','%s','%s','X','%s',%s,'');\n"

Looks better no ?

jice 53 Posting Whiz in Training

could you please wrap your code between [ code=python] [ /code] tags.
Indent is very important in python and not doing this makes your code unreadable.
thx

jice 53 Posting Whiz in Training


I know, its ugly :) I'm no programmer. It's complaining about line 26, which is:
if fnmatch.fnmatch(filename, '*.vob'):

And what does he say (Which exception is thrown) ?

jice 53 Posting Whiz in Training

but how can i prove for example that the result of sqrt(n) is an integer?

You can do this (maybe is there more elegant way)

if math.sqrt(n) == float(int(math.sqrt(n))):
    print "sqrt(n) is an integer"
vegaseat commented: nice work +7
jice 53 Posting Whiz in Training

You should look at the functions os.walk or os.path.walk (modules os and os.walk) which explore a tree and let you do what you want with the files.

Maybe os.walk is easier to use...

see
http://docs.python.org/lib/os-file-dir.html
http://docs.python.org/lib/module-os.path.html

jice 53 Posting Whiz in Training

You should look at Tkinter (available in the normal python distrib) - It would be much simpler than reinvent the wheel.
http://www.ferg.org/thinking_in_tkinter/index.html

jice 53 Posting Whiz in Training

If you find something better, don't hesitate to post it here... I'd be glad to know it.

jice 53 Posting Whiz in Training

This one is not very clean but it may work till you've got a better solution...

test = '\u2022'
exec 'print u"%s".encode("utf-8")' % (test)

jice 53 Posting Whiz in Training

test = u"\u2022"
print test.encode("utf-8")
ÔÇó

Have you tested this one ?

jice 53 Posting Whiz in Training

Well, maybe... I don't think this may be a problem. it's just like when you have to be careful about {{{ and }}}.
The positive point of that is that you HAVE to be careful of your code readability.
Python is compact and clear (and that's why I use it instead of perl for instance)

jice 53 Posting Whiz in Training

Hi,
In python, blocks structure rely on indentation

while E:
    Bt
else:
    Bf
back to the while level
jice 53 Posting Whiz in Training

Sorry to answer that late.
I don't understant how you can't reach an exception...
You certainly have, in the telnetlib module (maybe something like telnetlib.Telnet.EOFError

I don't use telnet in python but i use a little ftp. I imagine it is the same idea.
In my previous post, the exception handling was not to display message but to reconnect (self.conn = telnetlib.Telnet(so.telnetHost, so.telnetPort)). Reconnection is exactly the same as connection.

The idea is to loop until your commands are all executed.
When one fails because of a net failure (maybe recognised in the exception type), You can try to reconnect (self.conn = telnetlib.Telnet(so.telnetHost, so.telnetPort)) in a loop process with a timer (every 10 s for example).

You may be interested by this post :
http://www.velocityreviews.com/forums/t348486-how-to-know-if-connection-is-active-when-using-telnetlib.html

jice 53 Posting Whiz in Training

You can do something like

for myCommand in myCommandList:
    try:
        self.conn.telnet(myCommand) # I don't know the exact syntax 
                             # but you'll know that easily
    except theTelnetExceptionRaised:
        self.conn = telnetlib.Telnet(so.telnetHost, so.telnetPort)

This should work ok