Re: [Tutor] modify csv textfile

2010-08-07 Thread Sander Sweers
On 7 August 2010 04:35, TGW  wrote:
> I have a pipe delimited text file with 5 columns which looks like this:
> 12345|some text|some more text|example125 oo3 3456|example32423
> 11223|more text|and more|example/73d 77665|example455667
> 12677|text|more|anotherexample 123|anotherexample45
>
> What I want to output is:
> 12345|some text|some more text|example|example32423
> 11223|more text|and more|example|example455667
> ...
> 12677|text|more|anotherexample 123|anotherexample45
>
> So column 4 is where the change occurs, but only if the beginning of the 
> string in column 4  =~ /^example/i  # and it should be case insensitive
>
> #!/usr/bin/env python
> import csv
> import re
>
> filename = raw_input("Enter the filename to edit: ")
>
> reader = csv.reader(open(filename, 'rb'), delimiter='|', 
> quoting=csv.QUOTE_NONE)
> for row in reader:
>    print row
>
> 
> I can print the file, I just need a little help searching and replacing the 
> column 4 data element.

You can test if one item in your list begins with example like:
' example125 oo3 3456'.lstrip()[:7].lower() == 'example'

lstrip() will remove any unwanted white space on the left side of the string.
slice [:7] will give you the first 7 characters from the string.
lower() will make the string lower case so your requirement for case
insensitive is met.

Then your loop would look like (untested):

for row in reader:
if row[3].lstrip()[0:7].lower() == 'example':
 row[3] = row[3].lstrip()[:7] #we replace the fourth item.
print row

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


Re: [Tutor] modify csv textfile

2010-08-07 Thread TGW
> You can test if one item in your list begins with example like:
> ' example125 oo3 3456'.lstrip()[:7].lower() == 'example'

I think I need to use regex here. Perhaps you will agree.

Input file:
1119|Avail|53|Potato Chips
1234|Avail|53|Potato Chips and salse
1399|Avail|53|potato chips
1445|Avail|64|Pretzels
1490|Avail|64|Pretzels and mustard
etc...

#!/usr/bin/env python
import csv
import re

filename = raw_input("Enter the filename to edit: ")

reader = csv.reader(open(filename, 'rb'), delimiter='|', quoting=csv.QUOTE_NONE)
for row in reader:
if re.match('potato chips.*', row[4].lower()):
row[4] = 'Potato Chips'

if re.match('^pretzels.*', row[4].lower()):
row[4] = 'Pretzels'

print row

Row will be variable length, so strip() will not work for me. But neither does 
my latest attempt ^^
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] modify csv textfile

2010-08-07 Thread Alan Gauld


"TGW"  wrote


What I want to output is:
12345|some text|some more text|example|example32423
11223|more text|and more|example|example455667

So column 4 is where the change occurs, but only if the beginning
of the string in column 4  =~ /^example/i  # and it should be case 
insensitive




reader = csv.reader(open(filename, 'rb'), delimiter='|', 
quoting=csv.QUOTE_NONE)

for row in reader:
   print row


I can print the file, I just need a little help searching and 
replacing the column 4 data element.


OK, so I'm not sure which bit is confusing you.
The reader returns a list of fields per row.
You want the fourth column which is element 3 in row - ie. row[3]
You can use startswith() or a regex to test the value
You can replace the string with whatever you like since lists are 
mutable
You can then store/write the modified list to whatever/wherever you 
like.


Now which bit of that is causing you grief?

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] modify csv textfile

2010-08-07 Thread David Hutto
On Sat, Aug 7, 2010 at 7:26 AM, Alan Gauld  wrote:
>
> "TGW"  wrote
>>
>> What I want to output is:
>> 12345|some text|some more text|example|example32423
>> 11223|more text|and more|example|example455667
>>
>> So column 4 is where the change occurs, but only if the beginning
>> of the string in column 4  =~ /^example/i  # and it should be case
>> insensitive
>>
>
>> reader = csv.reader(open(filename, 'rb'), delimiter='|',
>> quoting=csv.QUOTE_NONE)
>> for row in reader:
>>   print row
>>
>> 
>> I can print the file, I just need a little help searching and replacing
>> the column 4 data element.
>
> OK, so I'm not sure which bit is confusing you.
> The reader returns a list of fields per row.
> You want the fourth column which is element 3 in row - ie. row[3]
> You can use startswith() or a regex to test the value
> You can replace the string with whatever you like since lists are mutable
> You can then store/write the modified list to whatever/wherever you like.
>
> Now which bit of that is causing you grief?

Probably the explanation. Mainly because of lack of documentation than
google terms, and appropriate questions
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] modify csv textfile

2010-08-07 Thread David Hutto
On Sat, Aug 7, 2010 at 8:26 AM, David Hutto  wrote:
> On Sat, Aug 7, 2010 at 7:26 AM, Alan Gauld  wrote:
>>
>> "TGW"  wrote
>>>
>>> What I want to output is:
>>> 12345|some text|some more text|example|example32423
>>> 11223|more text|and more|example|example455667
>>>
>>> So column 4 is where the change occurs, but only if the beginning
>>> of the string in column 4  =~ /^example/i  # and it should be case
>>> insensitive
>>>
>>
>>> reader = csv.reader(open(filename, 'rb'), delimiter='|',
>>> quoting=csv.QUOTE_NONE)
>>> for row in reader:
>>>   print row
>>>
>>> 
>>> I can print the file, I just need a little help searching and replacing
>>> the column 4 data element.
>>
>> OK, so I'm not sure which bit is confusing you.
>> The reader returns a list of fields per row.
>> You want the fourth column which is element 3 in row - ie. row[3]
>> You can use startswith() or a regex to test the value
>> You can replace the string with whatever you like since lists are mutable
>> You can then store/write the modified list to whatever/wherever you like.
>>
>> Now which bit of that is causing you grief?
>
> Probably the explanation. Mainly because of lack of documentation than
> google terms,
Not to say that docs are laccking to the proportion of want.

and appropriate questions
>>
>> --
>> Alan Gauld
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>>
>>
>> ___
>> Tutor maillist  -  tu...@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] modify csv textfile

2010-08-07 Thread Sander Sweers
On 7 August 2010 13:45, TGW  wrote:
>> You can test if one item in your list begins with example like:
>> ' example125 oo3 3456'.lstrip()[:7].lower() == 'example'
>
> I think I need to use regex here. Perhaps you will agree.

You could but you don't have to, consider this.

r = '1234|Avail|53|Potato Chips and salse'.split('|')
s = 'potato chips'

if r[3].lstrip().lower().startswith(s):
r[3] = r[3].lstrip()[:len(s)]
print r

> Input file:
> 1119|Avail|53|Potato Chips
> 1234|Avail|53|Potato Chips and salse
> 1399|Avail|53|potato chips
> 1445|Avail|64|Pretzels
> 1490|Avail|64|Pretzels and mustard
> etc...
>
> #!/usr/bin/env python
> import csv
> import re
>
> filename = raw_input("Enter the filename to edit: ")
>
> reader = csv.reader(open(filename, 'rb'), delimiter='|', 
> quoting=csv.QUOTE_NONE)
> for row in reader:
>    if re.match('potato chips.*', row[4].lower()):
>        row[4] = 'Potato Chips'
>
>    if re.match('^pretzels.*', row[4].lower()):
>        row[4] = 'Pretzels'
>
>    print row

Python start numbering indexes at 0 so row[4] is the *5th* item item
in your list.

> Row will be variable length, so strip() will not work for me.

I did not use strip() but lstrip() and only to remove leading
whitespace..? However strip only removes the _leading and trailing_
whitespace or character(s) you pass it.

> But neither does my latest attempt ^^

With the info and example data provided this would surely have given
an IndexError. So if you run into an exception you need to provide
them as they will be giving clues where it is going wrong.

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


Re: [Tutor] Distributing Python Code for Commercial Porpoises?

2010-08-07 Thread Wayne Watson
Thanks, but my partner has enough difficulty with a simple Python 
install, let alone VM or anything beyond Windows.


On 8/6/2010 11:42 AM, Chris Fuller wrote:

It sounds like maybe you could use Enthought Python, which is a bundle of most
of the popular numerical libraries by the scipy sponsors.  Not free, however,
there's a trial version.

http://enthought.com/products/epd.php

The problem of bundling stuff is a real thorny one and has been beaten to death
many times in this list and elsewhere.  It really doesn't solve the problem,
anyway, if you want your friend to be able to play with and rerun the code.

Another idea is to make a virtual machine that you can duplicate or even mail
back and forth with just the stuff required.  Then you'd need an OS license for
it (or use a minimal Linux, like Arch or DSL, but you probably want to stick
to the Windows platform, I'd guess.)

Cheers

On Friday 06 August 2010, Wayne Watson wrote:
   

Yes, porpoises was a (old) pun.

Back in Feb. I raised a question related to Subject. I just wanted to
know if Python code could be compiled in some sense. Robert Berman
pitched in with some help. Although I was making progress, I put it off
for a future date. I really don't want to get into py2exe here, but am
wondering if there are Python vendors who in some way sell their product
in compiled form?

My intent though is really not to produce a commercial product. My
question relates to difficulty my partner and I have to exchanging py
programs w/o him stumbling. I send him a py program written using
Windows Python 2.5. He has the same. I've executed it IDLE and it works
fine. He executes, and it squawks per my post here on finding a version
#, showing his output. We need to make sure we are on the same playing
ground with numpy and scipy. I don't think we are. He barely knows
Python, but did, supposedly, a install of it, numpy and scipy from the
same written direction I use. I think he mistakenly installed a
different version of numpy. So how can we make sure we or anyone are on
the same playing field? Perhaps we should resort to command like
execution. I am not confident that using py2exe will solve this problem.
Is there a Python tool that provides some thorough description of a
Python installation?
 

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

   


--
   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

"An experiment is a question which science poses to
 Nature, and a measurement is the recording of
 Nature’s answer." -- Max Planck


Web Page:

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


Re: [Tutor] Distributing Python Code for Commercial Porpoises?

2010-08-07 Thread Wayne Watson
Yes, that might work, but it gets into other issues that I would rather 
avoid. I hope I mentioned in my original msg that this is a Windows 
envirnoment.


I don't know much about dsutils, but it might work for these situations. 
Just enough to mention it seems like it package an environment.


An easy way out might be to ask him to uninstall Python and any modules 
like numpy. I'm not even sure how to do that. I don't think there's an 
uninstaller for all the Python stuff, so it's probably a matter of 
simply uninstalling Py 2.5 via MS control panel, and then directly 
deleting the modules like numpy and scipy. After that, he should be able 
to follow the instructions written by the sponsor.


On 8/6/2010 2:18 PM, Emile van Sebille wrote:

On 8/6/2010 10:51 AM Wayne Watson said...

Yes, porpoises was a (old) pun.

Back in Feb. I raised a question related to Subject. I just wanted to
know if Python code could be compiled in some sense. Robert Berman
pitched in with some help. Although I was making progress, I put it off
for a future date. I really don't want to get into py2exe here, but am
wondering if there are Python vendors who in some way sell their product
in compiled form?



I think you're making it harder. Go to your partners site, build an 
appropriate base environment, document and leave instructions on where 
to put new *.py modules you send him, run through it, and you're done.


A little education will likely go a lot further than delving deeper 
into heavier technologies in an attempt to 'simplfy'.


HTH,

Emile

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



--
   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

"An experiment is a question which science poses to
 Nature, and a measurement is the recording of
 Nature’s answer." -- Max Planck


Web Page:

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


[Tutor] Word to Sound

2010-08-07 Thread Chris King

 Dear Tutors,
How do you convert a string into a sound object.
Sincerely,
Chris
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Word to Sound

2010-08-07 Thread Steven D'Aprano
On Sun, 8 Aug 2010 01:43:25 am Chris King wrote:
>   Dear Tutors,
>  How do you convert a string into a sound object.

What do you mean by "sound object"?

Can you give an example of what you want to do?


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Distributing Python Code for Commercial Porpoises?

2010-08-07 Thread Wayne Watson




Solved the version question with the other thread two days ago. 

On 8/6/2010 8:38 PM, Che M wrote:

  
> #, showing his output. We need to make sure we are on the same
playing 
> ground with numpy and scipy. I don't think we are. He barely knows
  
> Python, but did, supposedly, a install of it, numpy and scipy from
the 
> same written direction I use. I think he mistakenly installed a 
> different version of numpy. So how can we make sure we or anyone
are on 
> the same playing field? 
  
Both of you do this from IDLE:
  
import numpy
help(numpy)
  
Read the version # at the end.
  

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


-- 
   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet 

"An experiment is a question which science poses to 
 Nature, and a measurement is the recording of 
 Nature’s answer." -- Max Planck
 
   
Web Page: 


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


Re: [Tutor] Distributing Python Code for Commercial Porpoises?

2010-08-07 Thread Wayne Watson




Actually, I arranged to have them on my Yahoo Group. He seemed to
ignore that, so I gave him very specific instructions, including a
snaphot with an arrow pointing to one of the files, to download. It's
the one that seems to be troublesome Numpy. So instead he downloads
another one. There's only so much I can do. I don't plan to dwell  on
this problem very long. I may not offer any other help on this subject.
Although, if the 40 or 50 years of the sponsor's app we all use want to
use my programs, I dread the thought of this non-Python savvy sites
dealing with issues like this. All have at least gotten the sponsors
app working except one. I finally called him and guided him through the
process. He barely understood Window, let alone how to install  the
app. He's  now working with it. 

On 8/6/2010 8:14 PM, j ram wrote:

  
  

My intent though is really not to produce a commercial product. My
question relates to difficulty my partner and I have to exchanging py
programs w/o him stumbling. I send him a py program written using
Windows Python 2.5. He has the same. I've executed it IDLE and it works
fine. He executes, and it squawks per my post here on finding a version
#, showing his output. We need to make sure we are on the same playing
ground with numpy and scipy.

  
  
Why not try bundling your .py modules in a zip file and then importing
the run modules from this zip file? In that way, the package integrity
is ensured. You'd just have to ship your collaborator the zip archive
and also make sure that both of you are running the same versions of
numpy, scipy, python and other packages. 
  
  http://docs.python.org/library/zipimport.html
  
  http://www.doughellmann.com/PyMOTW/zipimport/
  
Regards,
Iyer 
  
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
  


-- 
   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet 

"An experiment is a question which science poses to 
 Nature, and a measurement is the recording of 
 Nature’s answer." -- Max Planck
 
   
Web Page: 


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


Re: [Tutor] LOCATION ISSUES

2010-08-07 Thread Chris King

 On 7/13/2010 2:13 AM, Dipo Elegbede wrote:

Hello All,
Kindly help me with the location for the files created by this codes.
I have already compiled the codes and it has no error.
I copied the code from the following url: 
http://www.pythonware.com/library/pil/handbook/image.htm
This is supposed to create thumbnails of picture in the directory 
where I saved my file.

Please Help.
Thank You.

--
Elegbede Muhammed Oladipupo
OCA
+2348077682428
+2347042171716
www.dudupay.com 
Mobile Banking Solutions | Transaction Processing | Enterprise 
Application Development



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Well I don't have the module on this computer, but, try using the help 
function. It will show the description of any object, made by the 
creator. Its built in so it doesn't need imported. It is always will 
tell you what something is. It is like X-ray vision.

Sincerely,
Me
(P.S. Its funny, techinicly help is a function, its just a callable object.)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Distributing Python Code for Commercial Porpoises?

2010-08-07 Thread Emile van Sebille

On 8/7/2010 8:16 AM Wayne Watson said...


An easy way out might be to ask him to uninstall Python and any modules
like numpy. I'm not even sure how to do that.


More reasons to start out simpler.  Have your partner install one of the 
remote access tools (GoToMyPC, LogMeIn, VNC, etc) and do the work 
yourself.  And then install the changes yourself.  Again, the practice 
on both systems will probably benefit you more at this stage than 
advanced technologies.  Familiarizing yourself with the actions 
automated installation is required to perform will help you understand 
the tools better, and provides clues debugging the results when it 
doesn't quite work right. Don't make it any harder than it needs to be. 
Ultimately, a single byte update to a .py file shouldn't be any more 
involved than replacing the old file with the new revised file.


I'd use the links below -- there doesn't yet seem to be a 2.7 compatible 
released version of scipy yet, so this lists the most current compatible 
set of installable binaries.


As others have mentioned, don't use idle as you're doing.  Give 
pythonwin (included with the activestate distribution) a try.  Or try 
one of the free versions of Komodo or Wing.


HTH,

Emile



http://downloads.activestate.com/ActivePython/releases/2.6.5.14/ActivePython-2.6.5.14-win32-x86.msi

http://sourceforge.net/projects/numpy/files/NumPy/1.5.0b1/numpy-1.5.0b1-win32-superpack-python2.6.exe/download

http://sourceforge.net/projects/scipy/files/scipy/0.8.0/scipy-0.8.0-win32-superpack-python2.6.exe/download

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


Re: [Tutor] Distributing Python Code for Commercial Porpoises?

2010-08-07 Thread Wayne Watson
(I cancelled the post, since the file was 120K, and unacceptable. It's 
now 27K, so everyone should see it, but the print might be a bit tiny.)


Sounds like a plan. I'll give it a go.

For what it's worth, our sponsor has suggested it was the way to go, 
i.e., use IDLE to execute it, his large app. Over the last 24 months 
though I've discovered our sponsor, while able to produce good python 
code, is not real deep in understanding the Python world, environment. 
Worse they decided to improve the app


Actually, I stumbled across the code you mention below to reveal version 
and dependencies. I may have it operational soon.


OK, I just ran it with the double click method, and am attaching a 
snapshot of what I got. I was unable to copy it from the command window. 
I think I can fix that, but it would just take too much time now. In the 
event the attachment doesn't get to you or not posted, the program 
generated warning messages about NumpyTest().test, but opened cleanly 
with a prompt. The code I posted in the other thread results in a 
similar result, but the program dies immediately. The output also shows 
NumpyTest will be removed in the next release (of Numpy?), and that may 
be where my partner went wrong. I'm pretty sure he jumped ahead of my 
versions despite my cautions.


On 8/6/2010 5:18 PM, Alan Gauld wrote:

"Wayne Watson"  wrote


programs w/o him stumbling. I send him a py program written using
Windows Python 2.5. He has the same. I've executed it IDLE and it
works fine. He executes, and it squawks


IDLE is a development environment. Never, ever test final code in
a development environment, test it as it should be run. Double click
the file in explorer. Better still install a separate copy wherever the
file will go on the target system - usually somewhere different to
where you develop it - and run it there.


same written direction I use. I think he mistakenly installed a
different version of numpy. So how can we make sure we or anyone are
on the same playing field? Perhaps we should resort to command like
execution.


You should definitely not run it from IDLE, that's inefficient and
likely to hide errors. Run it from a command prompt or by double
clicking in explorer, or create a shortcut on the desktop.

To check the versions of your packages you could write a short
test program that simply imports all needed modules and prints
out the version info (if available) and file details xxx.__file__

You could even use the __file__ info to check the size of the files
by using the os module functions.


Is there a Python tool that provides some thorough description of a
Python installation?


I'm not aware of such but it should not be hard to check the basics.
One of the best thins about Python is the high level of portability
of programs across versions and OS. Its most likely a location
or PATH setting

HTH,



--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

  (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
   Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

 "An experiment is a question which science poses to
  Nature, and a measurement is the recording of
  Nature’s answer." -- Max Planck


 Web Page:


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


[Tutor] word_probles.py

2010-08-07 Thread Shurui Liu

print \
"""
If a pregnant hippo, weighing 2,000 pounds, gives birth to a 100 pound 
calf,
but then eats 50 pounds of food, how much does she weigh?"""
raw_input("Press the enter key to find out.")
print "2000 - 100 + 50 = ",
print 2000 - 100 + 50
print \
"""
If an adventurer returns from a successful quest and buys each of
6 companions 3 bottles of ale, how many bottles does the adventurer 
buy?"""
raw_input("Press the enter key to find out.")
print "6 * 3 = ",
print 6 * 3
print \
"""
If a kid has 24 pieces of Halloween candy and eats 6 pieces a day,
how many days will the stash last?"""
raw_input("Press the enter key to find out.")
print "24 / 6 = ",
print 24 / 6 
print \
"""
If a group of 4 pirates finds a chest full of 107 gold coins, and
they divide the booty evenly, how many coins will be left over?"""
raw_input("Press the enter key to find out.")
print "107 % 4 = ",
print 107 % 4
print \
"""
If a restaurant check comes to 19 dollars with tip, and you and
your friends split it evenly 4 ways, how much do you each throw in?"""
raw_input("Press the enter key to find out.")
print "19 / 4 = ",
print 19 / 4
print "WRONG!"
raw_input("Press the enter key for the right answer.")
print 19.0 / 4
raw_input("\n\nPress the enter key to exit.")

 
Here is a code named "word_problems.py". I can run it on putty.exe, but I don't 
understand why I cannot run it on IDLE or pyscripter.exe. Both of these two 
platform show that there are syntax errors in the code, errors are on those red 
lines. Thanks! 


##
Never had, never will. 


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


Re: [Tutor] Distributing Python Code for Commercial Porpoises?

2010-08-07 Thread Wayne Werner
I just noticed this thread - if you want a great version of python that has
numpy/scipy so you can  be sure to have the same version, use Python XY:
http://www.pythonxy.com/

It's got a host of scientific packages such as matplotlib, numpy, and scipy.
Then it has all sorts of other bells and whistles, as well.

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


Re: [Tutor] word_probles.py

2010-08-07 Thread Wayne Werner
On Sat, Aug 7, 2010 at 2:17 PM, Shurui Liu  wrote:

> 
> Here is a code named "word_problems.py". I can run it on putty.exe, but I
> don't understand why I cannot run it on IDLE or pyscripter.exe. Both of
> these two platform show that there are syntax errors in the code, errors
> are on those red lines. Thanks!
>

Please post the traceback when asking about errors - they're incredibly
useful!

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


Re: [Tutor] Distributing Python Code for Commercial Porpoises?

2010-08-07 Thread Wayne Watson

Thanks. Looks interesting.

On 8/7/2010 1:25 PM, Wayne Werner wrote:
I just noticed this thread - if you want a great version of python 
that has numpy/scipy so you can be sure to have the same version, use 
Python XY: http://www.pythonxy.com/


It's got a host of scientific packages such as matplotlib, numpy, and 
scipy. Then it has all sorts of other bells and whistles, as well.


HTH,
Wayne


--
   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

"An experiment is a question which science poses to
 Nature, and a measurement is the recording of
 Nature’s answer." -- Max Planck


Web Page:

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


[Tutor] os.urandom()

2010-08-07 Thread Richard D. Moores


using Vista, Python 3.1:

>>> import os
>>> os.urandom(6)
b'\xd1\xfc\xb0\x14\xeaL'

So what is this output? What in ascii? What in hex?  Do those
questions even make sense?

I've tried 2 things to get at it:
>>> import binascii
>>> binascii.b2a_qp(b'\xd1\xfc\xb0\x14\xeaL')
b'=D1=FC=B0=14=EAL'
>>> binascii.b2a_hex(b'\xd1\xfc\xb0\x14\xeaL')
b'd1fcb014ea4c'

Help, please.

Dick Moores





>>> import binascii
>>> binascii.b2a_qp(b'\x05\x8aU\x92\xf3R')
b'=05=8AU=92=F3R'
>>> binascii.b2a_hex(b'\x05\x8aU\x92\xf3R')
b'058a5592f352'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] os.urandom()

2010-08-07 Thread Alan Gauld

"Richard D. Moores"  wrote


import os
os.urandom(6)

b'\xd1\xfc\xb0\x14\xeaL'

So what is this output? What in ascii? What in hex?  Do those
questions even make sense?


The documentation tells you:

os.urandom(n)¶

Return a string of n random bytes suitable for cryptographic use.

So its a string of 5 bytes(*) generated at random by some random
generator function in your OS. What do you not understand?
What did you expect?

Python is telling you its bytes with the b at the front.
The \x tells you they are hex values.

(*)The fact its 5 is odd since you seem to pass 6 as an argument!
When I try it I get 6 bytes back.

Does that help?
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



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


Re: [Tutor] os.urandom()

2010-08-07 Thread Evert Rol
> 
> 
> using Vista, Python 3.1:
> 
 import os
 os.urandom(6)
> b'\xd1\xfc\xb0\x14\xeaL'
> 
> So what is this output? What in ascii? What in hex?  Do those
> questions even make sense?

It returns just what it says in the docs: "a string of [6] random bytes"

You can convert the bytes to characters using eg chr():
>>> [a for a in map(chr, os.urandom(6))]
['Ó', 'ó', 'P', 'Ü', 'Ó', 'C']

Not all converted bytes will be printable characters, so some may appear with 
the \x still prepended (eg, control characters).

Does that help? What actually do you wish to achieve in the first place?


> I've tried 2 things to get at it:
 import binascii
 binascii.b2a_qp(b'\xd1\xfc\xb0\x14\xeaL')
> b'=D1=FC=B0=14=EAL'
 binascii.b2a_hex(b'\xd1\xfc\xb0\x14\xeaL')
> b'd1fcb014ea4c'
> 
> Help, please.
> 
> Dick Moores
> 
> 
> 
> 
> 
 import binascii
 binascii.b2a_qp(b'\x05\x8aU\x92\xf3R')
> b'=05=8AU=92=F3R'
 binascii.b2a_hex(b'\x05\x8aU\x92\xf3R')
> b'058a5592f352'
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] Distributing Python Code for Commercial Porpoises?

2010-08-07 Thread Alan Gauld

"Emile van Sebille"  wrote
As others have mentioned, don't use idle as you're doing.  Give 
pythonwin (included with the activestate distribution) a try.  Or 
try one of the free versions of Komodo or Wing.


In fact I'd strongly recommend not to use ANY development tool to
run your programs. These tools are designed to make life easy while
writing code, they are not intended to be used to execute final code.
They trap errors, they redirect output, they ignore signals and all
manner of things intended to prevent you from having to restart
your development tool while developing. But those traps are
hiding errors that will crop up in the real world.

Always test deployable code in a real, native execution environment
- outside the development tool and outside the development folder
structure too. Include the installation process (whether automated
or manual) in your test.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] os.urandom()

2010-08-07 Thread Richard D. Moores
On Sat, Aug 7, 2010 at 15:26, Alan Gauld  wrote:

> Python is telling you its bytes with the b at the front.
> The \x tells you they are hex values.
>
> (*)The fact its 5 is odd since you seem to pass 6 as an argument!
> When I try it I get 6 bytes back.

Yes, the number of bytes seems to <= 6, or is it?:
>>> os.urandom(6)
b'\xf1\x1c\x15\x83\x14\x0e'
>>> os.urandom(6)
b'l\xbb\xae\xb7\x0ft'
>>> os.urandom(6)
b'\x1f\x00~\xfbz\x98'
>>> os.urandom(6)
b'\x98\xd2\xb2\xaa\x9bv'
>>> os.urandom(6)
b'2\x07\x81v\n-'
>>> os.urandom(6)
b'\x93\x1d\x1a\xa1\x0e\x1d'
>>> os.urandom(6)
b'`G\xa9_\xbab'
>>> os.urandom(6)
b'\r\xb0?p\x85\xc7'
>>> os.urandom(6)
b'\xb5\x1cn!\x93\xf4'
>>> os.urandom(6)
b'M\x0f\x1f34B'

And what's with the l in b'l\xbb\xae\xb7\x0ft' ?
the 2 in b'2\x07\x81v\n-' ?
the `G in b'`G\xa9_\xbab' ?
the \r in b'\r\xb0?p\x85\xc7' ?
the M in b'M\x0f\x1f34B' ?

Alan, are you using Windows, as I am?

I apologize to all for not explaining what I'm trying to accomplish. I
just ran across os.urandom() and am trying to understand it. I have a
script that I use to create passwords for my various website accounts,
, but wondered if
website-permitted, but stronger passwords could be generated with the
use of os.urandom(). At this point, I think not.

(To Steven D'Aprano: yeah I know now, thanks to you, that the
organization and modularization of >
 is woefully deficient :) )

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


Re: [Tutor] os.urandom()

2010-08-07 Thread bob gailer

On 8/7/2010 6:29 PM, Evert Rol wrote:

[a for a in map(chr, os.urandom(6))]
   


Overkill!

map(chr, os.urandom(6))

is sufficient.

Or

[chr(x) for x in os.urandom(6))]

The latter is easier to read.

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] os.urandom()

2010-08-07 Thread Richard D. Moores
On Sat, Aug 7, 2010 at 15:01, Dominik Danter  wrote:

> You could try something like binascii.hexlify(os.urandom(6)) to create hex.

>>> os.urandom(6)
b'f\xc8rnr\xea'
>>> binascii.hexlify(b'f\xc8rnr\xea')
b'66c8726e72ea'
>>> os.urandom(6)
b'D\xe9?\xda\xd80'
>>> binascii.hexlify(b'D\xe9?\xda\xd80')
b'44e93fdad830'

Thanks, but I'm not sure what to do with that..

What I am now sure of, is that I'm multiple steps ahead of myself with
os.urandom() !

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


Re: [Tutor] os.urandom()

2010-08-07 Thread Richard D. Moores
On Sat, Aug 7, 2010 at 16:34, bob gailer  wrote:

> [chr(x) for x in os.urandom(6))]

Correcting this to [chr(x) for x in os.urandom(6)],
most of the time I get an error:

>>> [chr(x) for x in os.urandom(6)]
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python31\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xb9' in
position 12: character maps to 

But once in a while it works:
>>> [chr(x) for x in os.urandom(6)]
['\x9d', '\x9b', '\x0e', 'ê', '^', 'N']

(remember, I'm using Windows)

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


Re: [Tutor] os.urandom()

2010-08-07 Thread Alan Gauld


"Richard D. Moores"  wrote


Yes, the number of bytes seems to <= 6, or is it?:

os.urandom(6)

b'\xf1\x1c\x15\x83\x14\x0e'


ok


os.urandom(6)

b'l\xbb\xae\xb7\x0ft'


still ok - the l and t at the ends are valid characters so Python
prints the letter


os.urandom(6)

b'\x1f\x00~\xfbz\x98'


Same here with the ~ and z characters


os.urandom(6)

b'\x98\xd2\xb2\xaa\x9bv'


and the (second) a and v


os.urandom(6)

b'2\x07\x81v\n-'


and the 2, v and -  Also notice the eol character in there(\n)

Hopefully you get the picture?


Alan, are you using Windows, as I am?


Yes. XP.

website-permitted, but stronger passwords could be generated with 
the

use of os.urandom(). At this point, I think not.


You probably could but it would take a lot of work for very little 
benefit.


HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] os.urandom()

2010-08-07 Thread Alan Gauld


"Richard D. Moores"  wrote


(*)The fact its 5 is odd since you seem to pass 6 as an argument!
When I try it I get 6 bytes back.


For some reason I never spotted the L at the end of the string last 
time.
So it was 6 bytes. I suspect the L was read (by me) as the L at the 
end

of a Long integer... even though you don't get them in v3!

Alan G. 



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


Re: [Tutor] word_probles.py

2010-08-07 Thread Shurui Liu

There is no tracebacks. 
 
I can run this program on putty.exe. That means the code is correct, but I 
cannot run it on IDLE or PyScripter.exe. Both of these two platforms only told 
me "Syntax Error" without tracebacks or something. 

##
Never had, never will. 



 


From: waynejwer...@gmail.com
Date: Sat, 7 Aug 2010 15:28:30 -0500
Subject: Re: [Tutor] word_probles.py
To: shurui@hotmail.com
CC: tutor@python.org


On Sat, Aug 7, 2010 at 2:17 PM, Shurui Liu  wrote:


 
Here is a code named "word_problems.py". I can run it on putty.exe, but I don't 
understand why I cannot run it on IDLE or pyscripter.exe. Both of these two 
platform show that there are syntax errors in the code, errors are on those red 
lines. Thanks! 


Please post the traceback when asking about errors - they're incredibly useful!


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


Re: [Tutor] word_probles.py

2010-08-07 Thread Hugo Arts
On Sat, Aug 7, 2010 at 7:12 PM, Shurui Liu  wrote:
> There is no tracebacks.
>
> I can run this program on putty.exe. That means the code is correct, but I
> cannot run it on IDLE or PyScripter.exe. Both of these two platforms only
> told me "Syntax Error" without tracebacks or something.
>

We'll need to know python versions. The machine you're running it on
through putty might have 2.x while your local machine has 3.x, that
would cause a problem like this.

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


Re: [Tutor] Distributing Python Code for Commercial Porpoises?

2010-08-07 Thread Wayne Watson
I think I'll print this and paste into the inside cover of my Python 
book. :-)


I find it interesting that any Python book I've seen doesn't deal with 
distributing programs in some form or another.


On 8/7/2010 3:33 PM, Alan Gauld wrote:

"Emile van Sebille"  wrote
As others have mentioned, don't use idle as you're doing. Give 
pythonwin (included with the activestate distribution) a try. Or try 
one of the free versions of Komodo or Wing.


In fact I'd strongly recommend not to use ANY development tool to
run your programs. These tools are designed to make life easy while
writing code, they are not intended to be used to execute final code.
They trap errors, they redirect output, they ignore signals and all
manner of things intended to prevent you from having to restart
your development tool while developing. But those traps are
hiding errors that will crop up in the real world.

Always test deployable code in a real, native execution environment
- outside the development tool and outside the development folder
structure too. Include the installation process (whether automated
or manual) in your test.



--
   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

"An experiment is a question which science poses to
 Nature, and a measurement is the recording of
 Nature’s answer." -- Max Planck


Web Page:

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


Re: [Tutor] os.urandom()

2010-08-07 Thread Steven D'Aprano
On Sun, 8 Aug 2010 09:32:02 am Richard D. Moores wrote:
> On Sat, Aug 7, 2010 at 15:26, Alan Gauld  
wrote:
> > Python is telling you its bytes with the b at the front.
> > The \x tells you they are hex values.
> >
> > (*)The fact its 5 is odd since you seem to pass 6 as an argument!
> > When I try it I get 6 bytes back.
>
> Yes, the number of bytes seems to <= 6, or is it?:

No, it's 6.

The underlying urandom generator *can* run out of entropy, but it will 
pause until there is enough entropy to generate sufficient random bytes 
rather than just supply too few bytes. In the worst case, where your 
computer has just started up (and hence has very little entropy), 
hasn't yet given you a GUI (and hence can't gather more entropy from 
mouse and keyboard events), and you've just asked for a wacking great 
pile of random bytes, urandom can lock up for *ages* waiting for more 
entropy so it can meet your request.



> >>> os.urandom(6)
> b'\xf1\x1c\x15\x83\x14\x0e'

Six bytes, each written in hex format \xNN.


> And what's with the l in b'l\xbb\xae\xb7\x0ft' ?

It's just a byte that happens to match the character "l" and therefore 
is printed as "l" instead of \x6c.


> I apologize to all for not explaining what I'm trying to accomplish.
> I just ran across os.urandom() and am trying to understand it. I have
> a script that I use to create passwords for my various website
> accounts, , but wondered if
> website-permitted, but stronger passwords could be generated with the
> use of os.urandom(). At this point, I think not.

Extreme randomness of passwords is not actually a good thing. Generally 
people want to remember their passwords, in which case you want 
passwords which are random enough to be hard for others to guess while 
non-random enough for the owner to remember them.



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] os.urandom()

2010-08-07 Thread Richard D. Moores
On Sat, Aug 7, 2010 at 17:00, Alan Gauld  wrote:
>
> "Richard D. Moores"  wrote
>
>> Yes, the number of bytes seems to <= 6, or is it?:
>
> os.urandom(6)
>>
>> b'\xf1\x1c\x15\x83\x14\x0e'
>
> ok
>
> os.urandom(6)
>>
>> b'l\xbb\xae\xb7\x0ft'
>
> still ok - the l and t at the ends are valid characters so Python
> prints the letter

>>> hex(ord('t'))
'0x74'
>>> hex(ord('l'))
'0x6c'

So if os.urandom() had been written so that it printed only hex,
b'l\xbb\xae\xb7\x0ft' would have been

b'\x6c\xbb\xae\xb7\x0f\x74' , right?

Thanks very much for that, Alan.

How were we supposed to know that all the hexes have 2 digits? How did you?

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