[Tutor] Embedding resources

2014-08-20 Thread Danny Yoo
On Aug 19, 2014 11:53 PM, "diliup gabadamudalige"  wrote:
>
> Hi all!
>
> Is there any way I can bundle all my graphics only using Python and
Pygame so that the user may not be able to read them out of the program?

(Changing subject line to more descriptive title)

Would something like http://www.pygame.org/pcr/image2py/index.php apply to
your problem?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] How to end this program

2014-08-20 Thread abid saied

# OK, I'm stuck on setting up an instruction that ends the program once you
# say either 'yes' or 'no'. And if you don't say 'yes' or 'no', and you 
reach
# the end, the program comes to an end. Please remember I'm a newbie and 
would
# appreciate any advise in plain simple English, like I was 5 years old!



import time

def main():
print()
print("YES/NO Game ")
time.sleep(2)
print()
print()

print("In this game, I will ask you a series of questions, if you answer 
YES or NO, you loose!")
print()
print("Remember to answer in Capitals!")
print()
time.sleep(2)
print("Starting the game ...")
print()
time.sleep(2)
name = input("What is your name? ")
print()
time.sleep(2) 
print("ok " + (name) + (", time for the questions..."))
print()
time.sleep(2)


answer = input("How old are you?  ")
if answer == "YES":
print("...You said yes, you loose!")
elif answer == "NO":
print ("...You said NO, you loose!")


answer = input("Do you like apples? ")
if answer == "YES":
print("...You said yes, you loose!")
elif answer == "NO":
print ("...You said NO, you loose!")


answer = input("Do you enjoy music?  ")
if answer == "YES":
print("...You said yes, you loose!")
elif answer == "NO":
print ("...You said NO, you loose!")

answer = input("Have you ever been on a train?  ")
if answer == "YES":
print("...You said yes, you loose!")
elif answer == "NO":
print ("...You said NO, you loose!")

answer = input("Do you watch 'Home and Away'?  ")
if answer == "YES":
print("...You said yes, you loose!")
elif answer == "NO":
print ("...You said NO, you loose!")

answer = input("What is 50/5?  ")
if answer == "YES":
print("...You said yes, you loose!")
elif answer == "NO":
print ("...You said NO, you loose!")

answer = input("Can a duck fly?  ")
if answer == "YES":
print("...You said yes, you loose!")
elif answer == "NO":
print ("...You said NO, you loose!")

answer = input("Can you drive a car?  ")
if answer == "YES":
print("...You said yes, you loose!")
elif answer == "NO":
print ("...You said NO, you loose!")

answer = input("Do you still watch cartoons?  ")
if answer == "YES":
print("...You said yes, you loose!")
elif answer == "NO":
print ("...You said NO, you loose!")

answer = input("Can a duck fly?  ")
if answer == "YES":
print("...You said yes, you loose!")
elif answer == "NO":
print ("...You said NO, you loose!")


print("OK, you win!You didn't say 'YES' or 'NO', well done!")
time.sleep(2)




Abid Saied
abidsa...@gmail.com



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Embedding resources

2014-08-20 Thread diliup gabadamudalige
Hi Danny!

The routine works fine and so does this one.

http://www.pygame.org/pcr/zipshow/index.php

The only problem with all these are that that the time taken to read these
files and to either make a list or send to screen is WAY TOO LONG.
The only one that works without much delay is in the file I have attached.
It is a bit of code that I wrote with verbose print statements to follow
the program in action while testing. As this uses base64 only there is no
secure encryption. Please have a look at it and try to improve it.

Am I doing this correctly? Are these messages getting to the community?




On Wed, Aug 20, 2014 at 1:18 PM, diliup gabadamudalige 
wrote:

> Hi Danny!
>
> You are always there to lend a helping hand I've noticed! Thanks for the
> quick response! Let me try the code and get back to you.
>
>
>
> On Wed, Aug 20, 2014 at 1:02 PM, Danny Yoo  wrote:
>
>>
>> On Aug 19, 2014 11:53 PM, "diliup gabadamudalige" 
>> wrote:
>> >
>> > Hi all!
>> >
>> > Is there any way I can bundle all my graphics only using Python and
>> Pygame so that the user may not be able to read them out of the program?
>>
>> (Changing subject line to more descriptive title)
>>
>> Would something like http://www.pygame.org/pcr/image2py/index.php apply
>> to your problem?
>>
>
>
>
> --
> Diliup Gabadamudalige
>
> http://www.diliupg.com
> http://soft.diliupg.com/
>
>
> **
> This e-mail is confidential. It may also be legally privileged. If you are
> not the intended recipient or have received it in error, please delete it
> and all copies from your system and notify the sender immediately by return
> e-mail. Any unauthorized reading, reproducing, printing or further
> dissemination of this e-mail or its contents is strictly prohibited and may
> be unlawful. Internet communications cannot be guaranteed to be timely,
> secure, error or virus-free. The sender does not accept liability for any
> errors or omissions.
>
> **
>
>


-- 
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**
"""
Author: Diliup Gabadamudalige. dili...@gmail.com>

Copyright: If you wish to use this code please keep this multiline comment,
   along with function source. Thank you.

read the files in the current directory and get all files except ones in the 
exempt list or directories..
Get the file extension.
and convert them by using maketrans without file extensions.
When converting from original files with file extensions, remove the file 
extension(with the .), reverse it and add it to the begining of the filename.
encode the file.
add new file ext "@lm".
eg: file name -> picture.tiff -> ffit.picture ->AFKDiO$1#&mL@lm.
encoded files added to a directory "coded" which is created if not exist.
decoded files added to a directory "decoded" which is created if not exist.

path = your path where the files are
run = True -> encode
run = False -> decode
"""

import os
import string
import base64
from master_vars import alpha, coder

def main(path, code = None, ex = [".wpr", ".wpu", ".py", ".pyc", ".@lm"]):

getfiles = []
path = path
code = code  #
exclude = ex
items = os.listdir(path)

if code:  # if code = True then encode
mode = "encode"
if not os.path.exists("coded"):
os.makedirs("coded")

else:  # else decode
mode = "decode"
## if decode mode and coded folder does not exist exit program
if not os.path.exists("coded"):
print "nothing to decode."

elif not os.path.exists("decoded") and os.path.exists("coded"):
os.makedirs("decoded")
elif os.path.exists("decoded"):
items = os.listdir(path + "\\" + "coded")

## get the names of the files in the path which are not in the exclude list
## encode or decode either way this works correct
for n in items:
if code:
fpath = path + "\\" + n
if  os.path.isfile(fpath):  # check if file is not a directory
if n[n.index("."):] not in exclude:
getfiles.append(n)
else:
fpath = pa

[Tutor] Parsing txt file

2014-08-20 Thread Dima Kulik
 Hi to all. I have a problem with parsing file.
I have txt file exported from AD and it has such structure: 

DistinguishedName : CN=*** ,OU=*** ,OU=*** ,DC=*** ,DC=***,DC=***
GroupCategory : Distribution
GroupScope    : Universal
Name  : 
ObjectClass   : group
ObjectGUID    : 0b74b4e2-aad1-4342-a8f4-2fa7763e1d49
SamAccountName    : 
SID   : S-1-5-21-1801674531-492894223-839522115-16421


i need to export to file all stings which start from "Name"

I've tried to make little parser:

keywords = ['Name', 'Name:']
input_file=open("Mail_Groups.txt","r").readlines()
output_file=open("Out.txt","w")
for line in input_file:
    for word in line.split():
    if word in keywords:
    output_file.write(line)

but the output is clear.

What the problem is? Can some one help me?
-- 
Dima Kulik___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to end this program

2014-08-20 Thread Joel Goldstick
On Wed, Aug 20, 2014 at 7:00 AM, abid saied  wrote:
>
> # OK, I'm stuck on setting up an instruction that ends the program once 
> you
> # say either 'yes' or 'no'. And if you don't say 'yes' or 'no', and you 
> reach
> # the end, the program comes to an end. Please remember I'm a newbie and 
> would
> # appreciate any advise in plain simple English, like I was 5 years old!
>
>
>
> import time
>
> def main():
> print()
> print("YES/NO Game ")
> time.sleep(2)
> print()
> print()
>
> print("In this game, I will ask you a series of questions, if you answer 
> YES or NO, you loose!")
> print()
> print("Remember to answer in Capitals!")
> print()
> time.sleep(2)
> print("Starting the game ...")
> print()
> time.sleep(2)
> name = input("What is your name? ")
> print()
> time.sleep(2)
> print("ok " + (name) + (", time for the questions..."))
> print()
> time.sleep(2)
>
>
> answer = input("How old are you?  ")
> if answer == "YES":
> print("...You said yes, you loose!")
> elif answer == "NO":
> print ("...You said NO, you loose!")
>
>
> answer = input("Do you like apples? ")
> if answer == "YES":
> print("...You said yes, you loose!")
> elif answer == "NO":
> print ("...You said NO, you loose!")
>
You repeat this pattern throughout your program, so it should be made
a function like this:

def ask_question(question):
answer = input(question)
answer = answer.upper()
if answer == "YES" or answer == "NO":
print "...You said %s, you loose!", answer
exit(0)# this will end the program
return   # this will get you out of your function


To ask all of your questions, create a list of questions like this:

list_of_questions = ["first question", "second question", etc ... ]

Now do this:
for question in list_of_questions:
ask_question


then finish with what you have below.


>
> print("OK, you win!You didn't say 'YES' or 'NO', well done!")
> time.sleep(2)
>
>
>
>
> Abid Saied
> abidsa...@gmail.com
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



-- 
Joel Goldstick
http://joelgoldstick.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to end this program

2014-08-20 Thread Joel Goldstick
On Wed, Aug 20, 2014 at 2:34 PM, Joel Goldstick
 wrote:
> On Wed, Aug 20, 2014 at 7:00 AM, abid saied  wrote:
>>
>> # OK, I'm stuck on setting up an instruction that ends the program once 
>> you
>> # say either 'yes' or 'no'. And if you don't say 'yes' or 'no', and you 
>> reach
>> # the end, the program comes to an end. Please remember I'm a newbie and 
>> would
>> # appreciate any advise in plain simple English, like I was 5 years old!
>>
>>
>>
>> import time
>>
>> def main():
>> print()
>> print("YES/NO Game ")
>> time.sleep(2)
>> print()
>> print()
>>
>> print("In this game, I will ask you a series of questions, if you answer 
>> YES or NO, you loose!")
>> print()
>> print("Remember to answer in Capitals!")
>> print()
>> time.sleep(2)
>> print("Starting the game ...")
>> print()
>> time.sleep(2)
>> name = input("What is your name? ")
>> print()
>> time.sleep(2)
>> print("ok " + (name) + (", time for the questions..."))
>> print()
>> time.sleep(2)
>>
>>
>> answer = input("How old are you?  ")
>> if answer == "YES":
>> print("...You said yes, you loose!")
>> elif answer == "NO":
>> print ("...You said NO, you loose!")
>>
>>
>> answer = input("Do you like apples? ")
>> if answer == "YES":
>> print("...You said yes, you loose!")
>> elif answer == "NO":
>> print ("...You said NO, you loose!")
>>
> You repeat this pattern throughout your program, so it should be made
> a function like this:
>
> def ask_question(question):
> answer = input(question)
> answer = answer.upper()
> if answer == "YES" or answer == "NO":
> print "...You said %s, you loose!", answer
> exit(0)# this will end the program
> return   # this will get you out of your function
>
>
> To ask all of your questions, create a list of questions like this:
>
> list_of_questions = ["first question", "second question", etc ... ]
>
> Now do this:
> for question in list_of_questions:
> ask_question()
>
>
> then finish with what you have below.
>
>
>>
>> print("OK, you win!You didn't say 'YES' or 'NO', well done!")
>> time.sleep(2)
>>
>>
>>
>>
>> Abid Saied
>> abidsa...@gmail.com
>>
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>
>
>
> --
> Joel Goldstick
> http://joelgoldstick.com



-- 
Joel Goldstick
http://joelgoldstick.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to end this program

2014-08-20 Thread Joel Goldstick
On Wed, Aug 20, 2014 at 2:40 PM, Joel Goldstick
 wrote:
> On Wed, Aug 20, 2014 at 2:34 PM, Joel Goldstick
>  wrote:
>> On Wed, Aug 20, 2014 at 7:00 AM, abid saied  wrote:
>>>
>>> # OK, I'm stuck on setting up an instruction that ends the program once 
>>> you
>>> # say either 'yes' or 'no'. And if you don't say 'yes' or 'no', and you 
>>> reach
>>> # the end, the program comes to an end. Please remember I'm a newbie 
>>> and would
>>> # appreciate any advise in plain simple English, like I was 5 years old!
>>>
>>>
>>>
>>> import time
>>>
>>> def main():
>>> print()
>>> print("YES/NO Game ")
>>> time.sleep(2)
>>> print()
>>> print()
>>>
>>> print("In this game, I will ask you a series of questions, if you 
>>> answer YES or NO, you loose!")
>>> print()
>>> print("Remember to answer in Capitals!")
>>> print()
>>> time.sleep(2)
>>> print("Starting the game ...")
>>> print()
>>> time.sleep(2)
>>> name = input("What is your name? ")
>>> print()
>>> time.sleep(2)
>>> print("ok " + (name) + (", time for the questions..."))
>>> print()
>>> time.sleep(2)
>>>
>>>
>>> answer = input("How old are you?  ")
>>> if answer == "YES":
>>> print("...You said yes, you loose!")
>>> elif answer == "NO":
>>> print ("...You said NO, you loose!")
>>>
>>>
>>> answer = input("Do you like apples? ")
>>> if answer == "YES":
>>> print("...You said yes, you loose!")
>>> elif answer == "NO":
>>> print ("...You said NO, you loose!")
>>>
>> You repeat this pattern throughout your program, so it should be made
>> a function like this:
>>
>> def ask_question(question):
>> answer = input(question)
>> answer = answer.upper()
>> if answer == "YES" or answer == "NO":
>> print "...You said %s, you loose!", answer
>> exit(0)# this will end the program
>> return   # this will get you out of your function
>>
>>
>> To ask all of your questions, create a list of questions like this:
>>
>> list_of_questions = ["first question", "second question", etc ... ]
>>
>> Now do this:
>> for question in list_of_questions:
>> ask_question(question)

sorry I messed up the last line a couple of times
>>
>>
>> then finish with what you have below.
>>
>>
>>>
>>> print("OK, you win!You didn't say 'YES' or 'NO', well done!")
>>> time.sleep(2)
>>>
>>>
>>>
>>>
>>> Abid Saied
>>> abidsa...@gmail.com
>>>
>>>
>>>
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> https://mail.python.org/mailman/listinfo/tutor
>>
>>
>>
>> --
>> Joel Goldstick
>> http://joelgoldstick.com
>
>
>
> --
> Joel Goldstick
> http://joelgoldstick.com



-- 
Joel Goldstick
http://joelgoldstick.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-20 Thread Terry--gmail

Alan Gauld

Hi!
We are not quite out of the woods on this last example you gave me. It 
now seems to be complaining
that it doesn't want to append an integer to the list or that this isn't 
the place to use '.append'  -- I am probably interpreting it's complaint 
wrong:


Python 3.3

If I run this last piece of code that we just added 'enumerate(row)' to:

lens = [0] * len(catalog2[0])
for row in catalog2:
for col, item in enumerate(row):
print(col, item, len(item))
lens[col].append(len(item))
lens = [max(col) for col in lens]

My result is:

0 Drives 6 << my print statement result

Traceback (most recent call last):
  File "/home/justme/1a_Computer_Related/Python/scripts/scratch.py", 
line 43, in 

lens[col].append(len(item))
AttributeError: 'int' object has no attribute 'append'


While messing with the above problem, I found myself thinking (almost 
always a mistake on my part) that the '.append' extension doesn't need 
the list pre-formatted any more than a simple lens= [], since .append 
has the ability to expand the list anyway, which would let the number of 
columns be more easily expanded later in life by the programmer simply 
adding a column of data to the table, so I tried-


lens = []
for row in catalog2:
for col, item in enumerate(row):
print(col, item, len(item))
lens[col].append(len(item))
lens = [max(col) for col in lens]

But my result says otherwise:

0 Drives 6

Traceback (most recent call last):
  File "/home/justme/1a_Computer_Related/Python/scripts/scratch.py", 
line 45, in 

lens[col].append(len(item))
IndexError: list index out of range


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Embedding resources

2014-08-20 Thread Danny Yoo
On Wed, Aug 20, 2014 at 6:45 AM, diliup gabadamudalige
 wrote:
> Hi Danny!
>
> The routine works fine and so does this one.
>
> http://www.pygame.org/pcr/zipshow/index.php
>
> The only problem with all these are that that the time taken to read these
> files and to either make a list or send to screen is WAY TOO LONG.
> The only one that works without much delay is in the file I have attached.

Hi Dilliup,

You'll probably want to contact the PyGame community to get more
specialized help on this one then. I suspect that this is a common
problem that the PyGame folks there have encountered (and hopefully
solved!).

The problem of including resources in packaged programs, in a way
that's resistant to outside peeking, is specialized enough that Tutor
is probably not the most effective place to ask.

You can talk to them from the links here:

http://pygame.org/wiki/info

Good luck!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-20 Thread Marc Tompkins
On Aug 20, 2014 12:07 PM, "Terry--gmail"  wrote:
>
> Alan Gauld
>
> Hi!
> We are not quite out of the woods on this last example you gave me. It
now seems to be complaining
> that it doesn't want to append an integer to the list or that this isn't
the place to use '.append'  -- I am probably interpreting it's complaint
wrong:
>
> Python 3.3
>
> If I run this last piece of code that we just added 'enumerate(row)' to:
>
> lens = [0] * len(catalog2[0])
> for row in catalog2:
>
> for col, item in enumerate(row):
> print(col, item, len(item))
>
> lens[col].append(len(item))
> lens = [max(col) for col in lens]
>
> My result is:
>
> 0 Drives 6 << my print statement result
>
> Traceback (most recent call last):
>   File "/home/justme/1a_Computer_Related/Python/scripts/scratch.py", line
43, in 
> lens[col].append(len(item))
> AttributeError: 'int' object has no attribute 'append'
>
Once again you're confusing the list with its contents.  lens is a list;
lens[col] is an integer.  lens.append(whatever) should do the trick.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-20 Thread Terry--gmail

Marc, my understanding is, is that:

lens[col].append(len(item))

-should be building a mirror image of my list of lists called catalog2, 
which currently has 9 columns by x number of rows, and that we are 
plugging into these positions, the sizes of all the elements in that 
block of data.


If that is true, then lens[col] is creating lists which contain the 
sizes of the elements in that particular column.


So, I completely don't understand why we would eliminate the positioning 
of which list we are referencing in lens by saying:


lens.append(len(item))

It seems to me that, that statement would put the entire block of 
element sizes into one list, and the next MAX statement would then yield 
only a single number, which would be the largest size element it 
encounted in the whole of catalog2!


Or am I really missing the boat here? :)

Thanks for your thoughts!
--Terry



On 08/20/2014 02:26 PM, Marc Tompkins wrote:


On Aug 20, 2014 12:07 PM, "Terry--gmail" > wrote:

>
> Alan Gauld
>
> Hi!
> We are not quite out of the woods on this last example you gave me. 
It now seems to be complaining
> that it doesn't want to append an integer to the list or that this 
isn't the place to use '.append'  -- I am probably interpreting it's 
complaint wrong:

>
> Python 3.3
>
> If I run this last piece of code that we just added 'enumerate(row)' to:
>
> lens = [0] * len(catalog2[0])
> for row in catalog2:
>
> for col, item in enumerate(row):
> print(col, item, len(item))
>
> lens[col].append(len(item))
> lens = [max(col) for col in lens]
>
> My result is:
>
> 0 Drives 6 << my print statement result
>
> Traceback (most recent call last):
>   File "/home/justme/1a_Computer_Related/Python/scripts/scratch.py", 
line 43, in 

> lens[col].append(len(item))
> AttributeError: 'int' object has no attribute 'append'
>
Once again you're confusing the list with its contents.  lens is a 
list; lens[col] is an integer.  lens.append(whatever) should do the trick.




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-20 Thread Marc Tompkins
On Wed, Aug 20, 2014 at 1:38 PM, Terry--gmail  wrote:
> Marc, my understanding is, is that:
>
> lens[col].append(len(item))
>
> -should be building a mirror image of my list of lists called catalog2,
> which currently has 9 columns by x number of rows, and that we are plugging
> into these positions, the sizes of all the elements in that block of data.

What's important is how you defined lens:
>  lens = [0] * len(catalog2[0])

That's a list of integers, as far as I can tell without running it
(I'm away from an interpreter at the moment.)  So no, you cannot do
lens[col].append(whatever), because - as the error message said -
'int' object has no attribute 'append'.

There might be some misunderstanding about what list.append(whatever)
does - it adds "whatever" to the end of list.  It does NOT assign
values to elements that already exist; to do that, you need to do
assignment by index.  So maybe this is what you're looking for?:
> lens[col] = len(item)



> So, I completely don't understand why we would eliminate the positioning of
> which list we are referencing in lens by saying:
>
> lens.append(len(item))
>
> It seems to me that, that statement would put the entire block of element
> sizes into one list, and the next MAX statement would then yield only a
> single number, which would be the largest size element it encounted in the
> whole of catalog2!
>
> Or am I really missing the boat here? :)

> lens.append(len(item))
will append a single integer to lens.  I'm afraid I don't quite follow
the bit about "the next MAX statement", as I've lost the thread of
what your larger project is trying to accomplish...  In any case,
max() _should_ only return a single number, no?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-20 Thread Peter Otten
Terry--gmail wrote:

> Marc, my understanding is, is that:
> 
>  lens[col].append(len(item))
> 
> -should be building a mirror image of my list of lists called catalog2,
> which currently has 9 columns by x number of rows, and that we are
> plugging into these positions, the sizes of all the elements in that
> block of data.
> 
> If that is true, then lens[col] is creating lists which contain the
> sizes of the elements in that particular column.
> 
> So, I completely don't understand why we would eliminate the positioning
> of which list we are referencing in lens by saying:
> 
>  lens.append(len(item))
> 
> It seems to me that, that statement would put the entire block of
> element sizes into one list, and the next MAX statement would then yield
> only a single number, which would be the largest size element it
> encounted in the whole of catalog2!
> 
> Or am I really missing the boat here? :)

[Alan Gauld]

> lens = [] * len(catalog2[0])
> for row in catalog2[1:]:   # miss the first row
>  for col,item in row:
> lens[col].append(len(item))
> 
> lens = [max(col) for col in lens]
 
In the above snipped Alan introduced introduced another bug that wasn't 
fixed in any of the posts I saw: lens should be a list of lists. 

With "lens" referring to

 [[], [], [], ...]

lens[n] refers to the n-th list and thus

lens[col].append(len(item))

appends the items's length to the respective column/list. 
The complete fixed code is then:

# make a list with one empty list per column
lens = []
for item in catalog2[0]:
lens.append([])

for row in catalog2:
for col, item in enumerate(row):
lens[col].append(len(item))

lens = [max(col) for col in lens]


The first for-loop can also be written as a list comprehension:

lens = [[] for item in catalog2[0]]

You might be tempted to simplify further

# WRONG:
lens = [[]] * len(catalog2[0])

but that repeats the same inner list len(catalog2[0]) times where you 
actually need len(catalog2[0]) distinct lists.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Parsing txt file

2014-08-20 Thread Dave Angel
Dima Kulik  Wrote in message:

Please post in text mode. The html mode you used can cause
 multiple problems. 

Please specify your Python version and os version in any new
 thread.It sometimes makes a big difference in the
 solution.

Your primary problem is that you don't close the files.

But you should start by writing a specification. I'm not sure what
 you mean by all stings. I'd assume you mean all lines. But that's
 not what the code does. The code is looking at every word of
 every line. Perhaps you mean the first word of the input
 file.

You say the output is clear,  by which I'm guessing you mean the
 output file is empty. Since there could 
be many causes for that,
 I'd suggest adding appropriate print statements to see what
 happened.

I'd also suggest removing the call to readlines. It doesn't change
 the result,  and causes several problems.  One is that input _
 file is not a File, but a list.  It becomes impossible to close
 it, and if the file is large it wastes memory. 

-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-20 Thread Terry--gmail


Hi Marc Tompkins!

You are absolutely right that

lens = [0] * len(catalog2[0])

Just creates a list of integers!

Here is what happened, my original method of finding the maximum element 
sizes in the 9 x ? block of data held in catalog2, only needed a final 
list of integers to contain it. However, as everyone helped me make my 
routine more pythonic, things evolved, but we always used the same 
beginning formatting statement until Alan Gauld gave me, as a last 
example, a different way to do the same thing, but he left out, I think, 
a matching formatting statement, and I ASSUMED WE WERE STILL USING THE 
OLD ONE.


You drew my eyes to it. So, I replaced the above statement with this line:

lens = [[], [], [], [], [], [], [],[], []]

So, trying that:

lens = [[], [], [], [], [], [], [],[], []]
for row in catalog2:
for col, item in enumerate(row):
lens[col].append(len(item))
lens = [max(col) for col in lens]
print(lens)

My result is:

[6, 17, 5, 9, 12, 7, 0, 0, 0]  <

Re: [Tutor] Parsing txt file

2014-08-20 Thread Danny Yoo
> i need to export to file all stings which start from "Name"
>
> I've tried to make little parser:
>
> keywords = ['Name', 'Name:']
> input_file=open("Mail_Groups.txt","r").readlines()
> output_file=open("Out.txt","w")
> for line in input_file:
> for word in line.split():
> if word in keywords:
> output_file.write(line)
>
> but the output is clear.


Questions:

1.  How are you determining that the output is clear?  I don't know
what you mean by "output is clear".


2.  Does your program close() the file that it is writing to?  See:

https://docs.python.org/2/library/stdtypes.html#file.write
https://docs.python.org/2/library/stdtypes.html#file.close

as to why this may be important.


3.  Your problem description says:

i need to export to file all stings which start from "Name"

but the code here:

for word in line.split():
if word in keywords:
...

is doing much more than what is required.  It is checking whether
"Name" shows up anywhere on the line, not just as part of the name of
the left-hand-side key.  Is that ok?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-20 Thread Danny Yoo
>
> lens = [0] * len(catalog2[0])


Ah.  Read the following closely:


https://docs.python.org/2/faq/programming.html#how-do-i-create-a-multidimensional-list


If you have any questions, please feel free to ask further.  See if
that clears up the mistake that's in your code.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-20 Thread Alan Gauld

On 20/08/14 20:06, Terry--gmail wrote:


We are not quite out of the woods on this last example



lens = [0] * len(catalog2[0])


This is what happens when you don't test code.
My apologies, the initialisation is all wrong.

Try this instead:

lens = [ [] for n in catalog2[0] ]

That adds an empty list for each element in catalog2's header.


for row in catalog2:
 for col, item in enumerate(row):
 print(col, item, len(item))
 lens[col].append(len(item))
lens = [max(col) for col in lens]


This should now work... although I still haven't tested it!



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-20 Thread Japhy Bartlett
this forms a list of integers

>>> [0]*5
[0, 0, 0, 0, 0]

what I think you want is something like:

>>> [[0] for i in range(5)]
[[0], [0], [0], [0], [0]]

(a list of lists)

>>> foo = [[0] for i in range(5)]
>>> foo[3].append('bar')
>>> foo
[[0], [0], [0], [0, 'bar'], [0]]



On Wed, Aug 20, 2014 at 3:56 PM, Marc Tompkins 
wrote:

> On Wed, Aug 20, 2014 at 1:38 PM, Terry--gmail 
> wrote:
> > Marc, my understanding is, is that:
> >
> > lens[col].append(len(item))
> >
> > -should be building a mirror image of my list of lists called catalog2,
> > which currently has 9 columns by x number of rows, and that we are
> plugging
> > into these positions, the sizes of all the elements in that block of
> data.
>
> What's important is how you defined lens:
> >  lens = [0] * len(catalog2[0])
>
> That's a list of integers, as far as I can tell without running it
> (I'm away from an interpreter at the moment.)  So no, you cannot do
> lens[col].append(whatever), because - as the error message said -
> 'int' object has no attribute 'append'.
>
> There might be some misunderstanding about what list.append(whatever)
> does - it adds "whatever" to the end of list.  It does NOT assign
> values to elements that already exist; to do that, you need to do
> assignment by index.  So maybe this is what you're looking for?:
> > lens[col] = len(item)
>
>
>
> > So, I completely don't understand why we would eliminate the positioning
> of
> > which list we are referencing in lens by saying:
> >
> > lens.append(len(item))
> >
> > It seems to me that, that statement would put the entire block of element
> > sizes into one list, and the next MAX statement would then yield only a
> > single number, which would be the largest size element it encounted in
> the
> > whole of catalog2!
> >
> > Or am I really missing the boat here? :)
>
> > lens.append(len(item))
> will append a single integer to lens.  I'm afraid I don't quite follow
> the bit about "the next MAX statement", as I've lost the thread of
> what your larger project is trying to accomplish...  In any case,
> max() _should_ only return a single number, no?
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Parsing txt file

2014-08-20 Thread Cameron Simpson

On 20Aug2014 16:35, Dima Kulik  wrote:

Hi to all. I have a problem with parsing file.
I have txt file exported from AD and it has such structure:

DistinguishedName : CN=*** ,OU=*** ,OU=*** ,DC=*** ,DC=***,DC=***
GroupCategory : Distribution
GroupScope    : Universal
Name  : 
ObjectClass   : group
ObjectGUID    : 0b74b4e2-aad1-4342-a8f4-2fa7763e1d49
SamAccountName    : 
SID   : S-1-5-21-1801674531-492894223-839522115-16421

[...]

I've tried to make little parser:

keywords = ['Name', 'Name:']
input_file=open("Mail_Groups.txt","r").readlines()
output_file=open("Out.txt","w")
for line in input_file:
    for word in line.split():


Aside from the remarks from others, I would change the way you're parsing each 
line. Based entirely on what you show above, I'd make the main out loops look 
like this:


  for line in input_file:
  left, right = line.split(':', 1)
  label = left.strip()
  value = right.strip()

and then made decisions using "label" and "value".

Your approach breaks the line into "words" on whitespace, which has several 
difficulties, including that the example input data look like a report. Often 
things with trailing colons will abut the colon if the label is long, eg:


HereIsALongNameLabel: info...

Your split() will be presuming the colon is spaced out.

Just splitting once on the first colon and the trimming the whitespace from the 
two piece is simpler and gets you a more reliable parse.


Cheers,
Cameron Simpson 

Trust the computer industry to shorten Year 2000 to Y2K. It was this
thinking that caused the problem in the first place.
- Mark Ovens 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-20 Thread Terry--gmail

Thanks Japhy Bartlett!

[[0] for i in range(5)]

Works! I converted to fit into my routine as:


lens = [[] for i in range(len(catalog2[0]))] << the new statement
for row in catalog2:
for col, item in enumerate(row):
lens[col].append(len(item))
lens = [max(col) for col in lens]
print(lens)

My result is:

[6, 17, 5, 9, 12, 7, 0, 0, 0]  <