jice 53 Posting Whiz in Training

If you're interested in generators (and you should be), here is the link that made me use generators and list comprehension:
http://www.dabeaz.com/generators/Generators.pdf

TrustyTony commented: Great reference on generators! +3
jice 53 Posting Whiz in Training

By exploring the mailbox module code, i finally found what i was looking for.
It is the "get_from" function, which is also mentionned in the module documentation...
So, it works allright now.

I don't want to convert my mbox files because what i want to do is :
- automatically reorganize my mails (processed with thunderbird) by creating one folder per year
- automatically detach attached files and replace them with a html file to keep the link to reduce the mailbox size.

So, at the end, i want to have my mailbox with exactly the same format as before.

Thanks

Gribouillis commented: good! post your mbox parsing code when it's finished! +4
jice 53 Posting Whiz in Training

Fast method, using sets :

lines=open(myfile,'r').readlines()
uniquelines=set(lines)
open(outfile,'w').writelines(uniquelines)

which can be done in only one line :

open(outfile,'w').writelines(set(open(myfile,'r').readlines()))
jice 53 Posting Whiz in Training

Sorry but i don't understand what you want...
Can you post your code ? part of your sql files ?
How do you execute the content of your sql files ?
What do you want to write in your logfile ?
Your question in far too open for me to answer.

jice 53 Posting Whiz in Training

Sorry, i've been too fast

import pprint # don't worry abour this it is only to print the result quickly at the end

rows=[[0 for i in range(9)] for i in range(9)]
cols=[[0 for i in range(9)] for i in range(9)]
# these are called list comprehensions and are the same as
rows=[]
for i in range(9):
    rows.append([])
    for j in range(9):
        rows[-1].append(0)
# so now rows=[[0, 0, 0, 0... ], [0,0,0,0...  ] ... ]
cols=[]
for i in range(9):
    cols.append([])
    for j in range(9):
        cols[-1].append(0)
# this is to create 2 lists skeltons
# now, let's fill them
for i in range(9):
    for j in range(9):
        # here, instead of 0 in each 'cell', I put a dictionary with 1 key : 'value'
        # note that you can put others datas in this dictionnary (like hidden, shown or whatever)
        rows[i][j]={"value":(i+j) % 9 + 1} # I put whatever value. Call another function... It's here you choose the values
        cols[j][i]=rows[i][j] # cols[j][i] and rows[i][j] are the SAME object
                              # altering one will alter the other

# Here is the print section.
# I use the pprint module and list comprehension to be quick on this part.
# This doesn't need to be understood ;-)
pprint.pprint([[elt["value"] for elt in row] for row in rows])
pprint.pprint([[elt["value"] for elt in col] for col in cols])

print 
# WHAT IS IMPORTANT TO SEE IS HERE
rows[3][4]["value"]=1515
# I change a rows value and in the result, you'll see that cols have changed too …
jice 53 Posting Whiz in Training

Something to think abour is also to use objects as cells (dict for example)
This way, you can use several lists to order the same cells different way.

For example

import pprint # don't worry abour this it is only to print the result quickly at the end

rows=[[0 for i in range(9)] for i in range(9)]
cols=[[0 for i in range(9)] for i in range(9)]
for i in range(9):
    for j in range(9):
        rows[i][j]={"value":(i+j) % 9 + 1}
        cols[j][i]=rows[i][j] # cols[j][i] and rows[i][j] are the SAME object
                              # altering one will alter the other

# Here is the print section.
# I use the pprint module and list comprehension to be quick on this part.
# This doesn't need to be understood ;-)
pprint.pprint([[elt["value"] for elt in row] for row in rows])
pprint.pprint([[elt["value"] for elt in col] for col in cols])

print 
rows[3][4]["value"]=1515

pprint.pprint([[elt["value"] for elt in row] for row in rows])
pprint.pprint([[elt["value"] for elt in col] for col in cols])

I didn't build the lists for the squares as it is a little more complicated and of no use for this example.
When you've done this, it is very quick and to verify all your groups...

jice 53 Posting Whiz in Training

So I explored a little my solution (which may not be the simplest).
In your case, you have to use an encoding parameter which is using the same length to encode all the polish characters. It seems to be iso-8859-13 as mentionned here
standard-encodings
So, my example becomes :

# -*- coding: utf-8 -*-
import string
a='ąóęśłżźćńĄÓĘŚŁŻŹĆŃ' ## utf8 has variable number of bytes
b='aoeslzzcnAOESLZZCN'

t = string.maketrans(a.decode("utf8").encode('iso8859_13'),b.decode("utf8").encode('iso8859_13'))

print 'ąóęśłżźćńĄÓĘŚŁŻŹĆŃ'.decode("utf8").encode('iso8859_13').translate(t).encode("utf8")
# .decode("utf8").encode('iso8859_13') : decodes the preceeding string using the utf-8 encoding and encodes it in a fixed length encoding defining the letters you need
# .translate(t) translates the string using the rule you mentionned
# .encode("utf8") reencodes (if needed) the result (here encoded in iso8859_13) in utf8

When you're not sure of which encoding you should use, you can try this code :

# These are all the standards encodings
encs=['ascii', 'big5', 'big5hkscs', 'cp037', 'cp424', 'cp437', 'cp500', 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856', 'cp857', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865', 'cp866', 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'cp1006', 'cp1026', 'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255', 'cp1256', 'cp1257', 'cp1258', 'euc_jp', 'euc_jis_2004', 'euc_jisx0213', 'euc_kr', 'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022_jp', 'iso2022_jp_1', 'iso2022_jp_2', 'iso2022_jp_2004', 'iso2022_jp_3', 'iso2022_jp_ext', 'iso2022_kr', 'latin_1', 'iso8859_2', 'iso8859_3', 'iso8859_4', 'iso8859_5', 'iso8859_6', 'iso8859_7', 'iso8859_8', 'iso8859_9', 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15', 'iso8859_16', 'johab', 'koi8_r', 'koi8_u', 'mac_cyrillic', 'mac_greek', 'mac_iceland', 'mac_latin2', 'mac_roman', 'mac_turkish', 'ptcp154', 'shift_jis', 'shift_jis_2004', 'shift_jisx0213', 'utf_32', 'utf_32_be', 'utf_32_le', 'utf_16', 'utf_16_be', 'utf_16_le', 'utf_7', 'utf_8']
for e in encs:
    try:
        t = string.maketrans(a.decode("utf8").encode(e),b.decode("utf8").encode(e))
        print "%s : %s" % (e, 'ąóęśłżźćńĄÓĘŚŁŻŹĆŃ'.decode("utf8").encode(e).translate(t).encode("utf8")) …
Gribouillis commented: Nice. I didn't think it was possible with translate. +3
jice 53 Posting Whiz in Training

Problem 1 (done with python 2.4)

# -*- coding: latin-1 -*-
# The first line is important and must be consistant with your file encoding (maybe utf8)
import string
t=string.maketrans('àéèëêôöîïù', 'aeeeeooiiu') 
print "lévitèrent à l'ïstrùmen".translate(t)

for python 3.1

# -*- coding: latin-1 -*-
t=str.maketrans('àéèëêôöîïù', 'aeeeeooiiu')
print ("lévitèrent à l'ïstrùmen".translate(t))

problem 2

mylist=[]
for i in range(10):
    mylist.append([])
    for j in range(1,9,2):
        mylist[i].append(j)
print mylist

# same result with list comprehension
print [[i for i in range(1,9,2)] for i in range(10)]
jice 53 Posting Whiz in Training

You don't need to split and join...

datas = """US 9 15 13 37
GE 10 13 7 30
CA 14 7 5 26""".split("\n")
for l in datas:
    print l.split(None,1)[1] #  the 1 split argument will tell to split only once

# Faster
# you can also use lists comprehensions :
print [l.split(None,1)[1] for l in datas]
# or
print "\n".join([l.split(None,1)[1] for l in datas])
jice 53 Posting Whiz in Training

I can see 2 solutions :

list1 = ["20100122 http google.com 200", "20100124 http hushmail.com 200", "20100123 http microsoft.com 404" ]
list2 = ["google.com", "yahoo.com", "msn.com", "hotmail.com", "gmail.com"]
# 1
for i, e1 in enumerate(list1):
    for e2 in list2:
        if e2 in e1:
            print ("line %d : %s" % (i,e1))

# 2
for i,e in enumerate(list1):
    d=e.split()[2]
    if d in list2:
        print ("line %d : %s" % (i,e))

Note that I don't understand where your microsoft output line comes from.

jice 53 Posting Whiz in Training

This should do the trick

keywords=[word1,word2,wor3,word4]
file1="/path/to/file1.txt"
file2="/path/to/file2.txt"
f2=open(file2).readlines() # I load the 2d file in a list to be able to change a line by another easily
for l in open(file1): # no need to open the file and read it before. This will read the file line by line
    for k in keywords:
        if k in l:
            for i, l2 in enumerate(f2): # enumerate will give you the number of the line and the line the same time
                if k in l2:
                    f2[i]=l # I change the line
of=open(file2,"w")
of.writelines(f2) # I rewrite the wole list
of.close()
deonis commented: very nice post! Well explained ! +1
jice 53 Posting Whiz in Training

You should use modules like pickle or marshal to serialize and load your datas

import pickle
c=['Hondros', {'Equiped': [['none', [], 0], ['Rags', [0, 1, 0, 0], 1, 1], ['Mocassins', [0, 0, 1, 0], 1, 1], ['Gauntlets', [0, 0, 0, 1], 6, 5]], 'Equipment': [0, 1, 1, 1], 'Stats': {'AC': 14, 'Strength': 15, 'Constitution': 14, 'Level': 1, 'HP': 12, 'Experience': 0, 'Dexterity': 12, 'Intelligence': 12, 'Base AC': 8, 'Wisdom': 11, 'HP max': 12}, 'Inventory': [], 'Gold': 0}]
f=open('test.pkl','wb')
pickle.dump(c, f)
f.close()
f=open('test.pkl','rb')
d=pickle.load(f)
f.close()
print d[1]['Stats']

Eventually, you can use exec but this is not to be recomended

a = "['Hondros', {'Equiped': [['none', [], 0], ['Rags', [0, 1, 0, 0], 1, 1], ['Mocassins', [0, 0, 1, 0], 1, 1], ['Gauntlets', [0, 0, 0, 1], 6, 5]], 'Equipment': [0, 1, 1, 1], 'Stats': {'AC': 14, 'Strength': 15, 'Constitution': 14, 'Level': 1, 'HP': 12, 'Experience': 0, 'Dexterity': 12, 'Intelligence': 12, 'Base AC': 8, 'Wisdom': 11, 'HP max': 12}, 'Inventory': [], 'Gold': 0}]"
exec("b=" + a)
print b[1]['Stats']
jice 53 Posting Whiz in Training

You can have a look at this to have an idea of web programming in python
http://webpython.codepoint.net/
and this
http://webpy.org/
I've never done web programming in python because norm in my job is php so i had to learn php. And i find it much less pleasant to write.
If I had choice, i'd certainly give a chance to python because it's very pleasant and fast to write.

jice 53 Posting Whiz in Training

you can use list comprehensions

g = "ThisIsATestt"
print [g[i:i+2] for i in range(0,len(g),2)]
jice 53 Posting Whiz in Training

I think this one is better...

import re
testStrings=[ "POST at the start without the other word", "GET at the start without the other word", "GET first\nthen POST and more", "POST first\nthen GET and more"]
splitter = re.compile('(POST|GET)')
for s in testStrings:
    splittedString=splitter.split(s)[ 1:]
    print [ splittedString[i]+splittedString[ i+1] for i in range(0,len(splittedString),2)]

But you can even do

import re
teststrings=["POST at the start without the other word", "GET at the start without the other word", "GET first\nthen POST and more", "POST first\nthen GET and more"]
print [(lambda splittedstring: [splittedstring[i]+splittedstring[i+1] for i in range(0,len(splittedstring),2)])(re.compile('(POST|GET)').split(s)[1:]) for s in teststrings]

which is less readable.
You have a one liner if you don't use the intermediate variable teststrings ;-)

import re
print [(lambda splittedstring: [splittedstring[i]+splittedstring[i+1] for i in range(0,len(splittedstring),2)])(re.compile('(POST|GET)').split(s)[1:]) for s in ["POST at the start without the other word", "GET at the start without the other word", "GET first\nthen POST and more", "POST first\nthen GET and more"]]
amadain commented: that is great. Thanks +1
jice 53 Posting Whiz in Training

How does this determine the row # of FCITC?

Here :

if "FCITC" in line:
        noline+=1

The code seems to set No line to 1. Is this saying that the FCITC is located at line 1 and that no line +=1 would be the following line where the values are located? Is this the same as noline==2?

The idea is to say :
when i see a line with FCITC in it, i increase the number (i named it noline but it may not be the best name... You can choose noLineRelativelyToTheHeaderLine ;-) )
If this number is 1, i load fcitc and limit constraint in variables and i increase the number.
If this number is 2, i load the Contingency description in another variable (i take the whole line and strip it as there is nothing else on this line) and set the number to 0 so that every other line is ignored until FCITC is found again.

d5e5 commented: Good start. It brought me closer to feeling I understood what the OP wants to do. +0
jice 53 Posting Whiz in Training

and you should use float instead of int

print float(miles_driven) / float(gallons_of_gas_used)
vegaseat commented: a good deduction +16
jice 53 Posting Whiz in Training

Vegaseat forgot a little point

# This script will determine the size if the gwarchive directories 
# in everyone's home folder

import os

folder_size = 0

for username in open("L:\\dirl.txt"):
    folder = "L:\\" + username.strip('\n') + "\\gwarchiv" # <== remove the ending "\n"
    print folder
    for (path, dirs, files) in os.walk(folder):
        for name in files:
            filename = os.path.join(path, name)
            folder_size += os.path.getsize(filename)
        print "Archive = %0.1f MB" % (folder_size/(1024*1024.0))
vegaseat commented: thanks, I didn't test my code +15
jice 53 Posting Whiz in Training

Hi
Thanks folks for your replies.
Testing the general format shown below returns a character and not the complete line

lpa=1
text_file = open("English101.txt", "r")
print text_file.readline()[lpa]
text_file.close()

What I am trying to achieve is that a complete line of text is returned, from a text file that has 600 lines or so. Maybe I am looking at the wrong instruction

readline() reads the next line. So, readline()[nb] takes the nbth character of the next line.
You can't read the nth line of a file that way. You have to read line by line TILL the nth line or read the whole file in a list (like you did in linelist) and then

lpa=1
text_file = open("English101.txt", "r")
linelist=text_file.readlines() # I load the whole file in a list
text_file.close()
print linelist[lpa]
chico2009 commented: Thanks a very helpful comment +1
jice 53 Posting Whiz in Training

Input Temperature in Celcius in Main function in 1st.py file passed on as variable C.

so you call, in your Main function

def Main():
    C=int(input("°C ?"))
    F=twond.CelciusToFarenheit(C) # Here, the returned value is stored in F
        # But this F has nothing to do with any F you'll see in the second function (same name but different variables)
        # You'll see later...

Second function in file twond.py will convert that to Fahrenheit and store in variable F.

This is where you have to concentrate ;-)
The Main function doesn't care about where the second function (let's say CelciusToFarenheit) stores its intermediates values.
It only cares about what is returned

def CelciusToFarenheit(C):
    F=1,8C+32 # Main() won't see this F whatever you do
    return F # This will return the value to the calling function (here Main). It returns a Value.
        # It is called F here but it could be called anyway the result would be exactly the same

Now this should be passed back of to main function in 1st.py file.
So when i command print(F) in the main file, it prints the output in Fahrenheit. So now I can use this Fahrenheit and play with this variable.

Let's complete the Main function

def Main():
    C=int(input("°C ?"))
    F=twond.CelciusToFarenheit(C)
    print (F)
    # play with F as much as you want. As soon as the returned value is stored in a variable, it can be used.
    A=F*1.05
    print (A)
romes87 commented: Very helpful +1
jice 53 Posting Whiz in Training

You should not remove elements from a list you are looping on

list=[1,2,3,4,5,6,7,8,9]
for i in list:
    if i in [4,5,6]:
        list.remove(i)
print list

> [1, 2, 3, 5, 7, 8, 9]

When you remove an element, the followings ranks are modified (minus 1) so some elements are never used (those that takes the ranks of removed elements).
You have to loop on a copy of your list :

list=[1,2,3,4,5,6,7,8,9]
for i in list[:]: # list[:] is a copy of list...
    if i in [4,5,6]:
        list.remove(i)
print list

> [1, 2, 3, 7, 8, 9]
nunos commented: Excellent post! Very simple and clarifying. +1
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

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