[Tutor] Syntax error with if statement

2011-08-24 Thread Ray Parrish

Hello,

I haven't been programming for a while and today I 
am working on something and have encountered an 
error I cannot figure out.


Here is my code:

  thisFile = column[6]
  if thisFile == "/Images/My%20Face.JPG"
   CallCount += 1
   print CallCount

And here is the error message:

ray@RaysComputer:~$ python 
/home/ray/EmailCount/CountEmails.py

  File "/home/ray/EmailCount/CountEmails.py", line 41
if thisFile == "/Images/My%20Face.JPG"
  ^
SyntaxError: invalid syntax

Any help you can be will be much appreciated.

Later, Ray Parrish

--
The Unknown Lead Player, Ray Parrish
http://www.facebook.com/pages/The-Unknown-Lead-Player/123388551041130?sk=wall
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


Re: [Tutor] Boa Constructor list control

2010-04-29 Thread Ray Parrish

Ray Parrish wrote:

Hello,

I am just learning how to use Boa Constructor to make GUI apps, and am 
having some problem with understanding the list control.


The list control has a property called columns, but I cannot figure 
out how to add columns, and specify names for them.


When I click the column property a small window opens up with it's 
title set to col1 something with the word "Name" in a label on it. 
Subsequent clicks on the column properry of the list control just 
brings this little window up again.


I cannot change the text in the label of that small window to the name 
I want for a column, and I cannot figure out how to add more columns 
to the list control.


When the small column window is selected no properties show for it in 
the Inspector window.


Can anyone tell me how to use this control? I tried using the Boa 
Constructor help menu, and it led me to a very brief description of 
the list control, with no details on what it's properties were even.


Thanks, Ray Parrish

OK, I've discovered the collection editor, and now know how to use it, 
but when I click the new button in the collection editor for a list 
control, it pops up an error message stating that the list needs to be 
created with the wx.LC_REPORT flag, and I have no idea how to accomplish 
that.


Could someone please enlighten me?

Thanks, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


[Tutor] Boa Constructor list control

2010-04-29 Thread Ray Parrish

Hello,

I am just learning how to use Boa Constructor to make GUI apps, and am 
having some problem with understanding the list control.


The list control has a property called columns, but I cannot figure out 
how to add columns, and specify names for them.


When I click the column property a small window opens up with it's title 
set to col1 something with the word "Name" in a label on it. Subsequent 
clicks on the column properry of the list control just brings this 
little window up again.


I cannot change the text in the label of that small window to the name I 
want for a column, and I cannot figure out how to add more columns to 
the list control.


When the small column window is selected no properties show for it in 
the Inspector window.


Can anyone tell me how to use this control? I tried using the Boa 
Constructor help menu, and it led me to a very brief description of the 
list control, with no details on what it's properties were even.


Thanks, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


Re: [Tutor] Passing headers with httplib

2010-04-16 Thread Ray Parrish

Ray Parrish wrote:

Hello,

I am trying to figure out how to send cookie data with an httplib 
request command. Here is what the doc says -


request(  method, url[, body[, headers]])
   This will send a request to the server using the HTTP request 
method method and the selector url. If the body argument is present, 
it should be a string of data to send after the headers are finished. 
The header Content-Length is automatically set to the correct value. 
The headers argument should be a mapping of extra HTTP headers to send 
with the request.


My question is how do I formulate a "mapping" of a header for a few 
cookies? Here are the cookies I would like to send with the request -


TVGID=029690D849714243A9DDEA34652CA804; Provider=Broadcast; 
__qca=P0-547310088-1271379357584; 
LastGrid=fmt=0&srvid=20442&gridmins=120&gridyr=2010&gridmo=4&griddy=15&gridhr=20&chanrow=1&genre=0&favchan=false&magic=1269|9898&magictype=0&zip=&music=1&ppv=1&24hr=1&HDTVOnly=false; 
s_cc=true; s_sq=%5B%5BB%5D%5D; wtpgcnt=1; 
s_vi=[CS]v1|25E3DAD0050118C7-610E60004530[CE]; 
base_domain_4631b4ff1e53d450c3f9726dd45be7de=tvguide.com; 
rsi_ct=2010_4_15:6;


Thanks for any help you can be, Ray Parrish
OK,  I got it figured, but still no luck, passing the cookies didn't 
help me get at the search results I was trying to get. Here is the code 
for passing cookies -


connection = httplib.HTTPConnection("www.tvguide.com", port)
connection.putrequest("GET", "/listings/default.aspx?keyword=Star+Trek")
connection.putheader("Cookie", "TVGID=029690D849714243A9DDEA34652CA804; 
Provider=Broadcast; __qca=P0-547310088-1271379357584; 
LastGrid=fmt=0&srvid=20442&gridmins=120&gridyr=2010&gridmo=4&griddy=15&gridhr=20&chanrow=1&genre=0&favchan=false&magic=1269|9898&magictype=0&zip=&music=1&ppv=1&24hr=1&HDTVOnly=false; 
s_cc=true; s_sq=%5B%5BB%5D%5D; wtpgcnt=1; 
s_vi=[CS]v1|25E3DAD0050118C7-610E60004530[CE]; 
base_domain_4631b4ff1e53d450c3f9726dd45be7de=tvguide.com; 
rsi_ct=2010_4_15:8; 
rsi_segs=D04451_10006|D04451_10077|D04451_10089|D04451_10116; 
__utma=208178903.2088698592.1271379370.1271389872.1271398039.3; 
__utmc=208178903; 
__utmz=208178903.1271398041.3.3.utmccn=(referral)|utmcsr=tvguide.com|utmcct=/search/index.aspx|utmcmd=referral; 
zip=97424; ServiceID=20442; srvid=20442; ev1=star%20trek;")

connection.endheaders()
response = connection.getresponse()
headers = response.getheaders()
print headers
fileContents = response.read()

Later, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


[Tutor] Passing headers with httplib

2010-04-15 Thread Ray Parrish

Hello,

I am trying to figure out how to send cookie data with an httplib 
request command. Here is what the doc says -


request(  method, url[, body[, headers]])
   This will send a request to the server using the HTTP request method 
method and the selector url. If the body argument is present, it should 
be a string of data to send after the headers are finished. The header 
Content-Length is automatically set to the correct value. The headers 
argument should be a mapping of extra HTTP headers to send with the 
request.


My question is how do I formulate a "mapping" of a header for a few 
cookies? Here are the cookies I would like to send with the request -


TVGID=029690D849714243A9DDEA34652CA804; Provider=Broadcast; 
__qca=P0-547310088-1271379357584; 
LastGrid=fmt=0&srvid=20442&gridmins=120&gridyr=2010&gridmo=4&griddy=15&gridhr=20&chanrow=1&genre=0&favchan=false&magic=1269|9898&magictype=0&zip=&music=1&ppv=1&24hr=1&HDTVOnly=false; 
s_cc=true; s_sq=%5B%5BB%5D%5D; wtpgcnt=1; 
s_vi=[CS]v1|25E3DAD0050118C7-610E60004530[CE]; 
base_domain_4631b4ff1e53d450c3f9726dd45be7de=tvguide.com; 
rsi_ct=2010_4_15:6;


Thanks for any help you can be, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


[Tutor] Declaring methods in modules.

2010-04-11 Thread Ray Parrish

Hello,

I am working on some stuff, and I would like to be able to write a 
module which can be imported, and after it's been imported I would like 
to be able to access it's functions as methods.


In other words, if I do the import of module ISPdetector, I want to then 
be able to make calls like the following -


   ipAddress = "123.123.123.123"
   emails = ipAddress.GetEmailAddresses()

where GetEmailAddresses() is defined in module ISPdetector. Do I just 
wite that function in ISPdetector.py as a normally deffed function, or 
does it have to be part of a class within the module?


Thanks for any help you can be.

Later, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


[Tutor] Speech recognition, and synthesis

2010-04-11 Thread Ray Parrish

Hello,

Are there any Python libraries that deal with speech recognition, and 
speech synthesis?


If so, where are they available, and are there any open source versions?

Thanks for any help you can be.

Later, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


[Tutor] KeyError: '61.135.168.82' with dictionary.

2010-03-25 Thread Ray Parrish

Hello,

The following code works interactively, but when ran from a script, I 
get the errors, after the second code block, which is the code from the 
script.


>>> lineList = []
>>> thisIPAddress = '61.135.168.82'
>>> date = "2010 3 12"
>>> dates = {date:{thisIPAddress:lineList}}
>>> dates[date][thisIPAddress]
[]
>>> dates[date][thisIPAddress].append(16)
>>> dates[date][thisIPAddress]
[16]
>>> dates[date][thisIPAddress].append(17)
>>> dates[date][thisIPAddress]
[16, 17]
>>>

Script code begins here -

 thisIPAddress = columns[10]
 lineList = []
 if date == "":
  date = columns[0]
  dates = {date:{thisIPAddress:lineList}}
  # For each date in the input file collect an array of log 
lines

  # for each unique ip address.
 date = columns[0]
 dates[date][thisIPAddress].append(eachLine)

Error messages follow -

Traceback (most recent call last):
 File "/home/ray/LogAnalyzer/iis-log-analyze.py", line 2519, in 
   ReadInDaysLog(tempLogName, countryCodes)
 File "/home/ray/LogAnalyzer/iis-log-analyze.py", line 2241, in 
ReadInDaysLog

   dates[date][thisIPAddress].append(eachLine)
KeyError: '61.135.168.82'


As you can see, it is tossing a key error on the same ip address that 
was used in the interactive code successfully,


I can not figure out what to change to make the script work. Does 
someone know why this happens?


Thanks, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


[Tutor] Declaring a compound dictionary structure.

2010-03-13 Thread Ray Parrish

Hello,

I am stuck on the following -

   # Define the Dates{} 
dictionary structure as a dictionary
   # containing two dictionaries, 
each of which contains a list.
   Dates = 
{Today:{ThisIPAddress:[]}, 
Tomorrow:{ThisIPAddress:[]}}


How do I pass this declaration empty values for 
Today, Tomorrow, and ThisIPAddress to initially 
clare it? I have managed to pass it [] as an empty 
list, but passing # "" as an empty dictionary 
label does not work correctly.


 Dates = {"":"":[]}, "":{"":[]}}

The above is what I tried, and that creates a 
dictionary with index values of "" for all of the 
key values from the previous declaration. Is there 
a way to create this comound dictionary structure 
with no key values defined at all to begin with? 
The reason I'd like to know is that with the 
second declaration the "" indexed keys  have to be 
removed after assigning at least one key value 
with a non-blank name.


The idea behind the structure is to sort through a 
server log that contains entries for two dates, 
collecting a pair of dictionaries of ip address 
indexed server log lines which can then be 
iterated over to
extract daily visit counts, and other extractable 
data sorted by date, and visitor.


The need would not arise if the log files did not 
contain two dates each, but my service provider 
rotates their server logs around 2 am, so I always 
get a few entries for the next day in every log file.


Thanks, Ray Parrish


--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, 
and articles by Ray.

http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to 
be a schizo, and other

things, including my poetry.
http://www.writingsoftheschizophrenic.com



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


Re: [Tutor] Escaping a single quote mark in a triple quoted string.

2010-03-13 Thread Ray Parrish

Steven D'Aprano wrote:

On Sun, 14 Mar 2010 04:33:57 am Ray Parrish wrote:
  

Hello,

I am getting the following -

 >>> String = """http://www.rayslinks.com";>Ray's Links"""
 >>> String
'http://www.rayslinks.com";>Ray\'s Links'

Note the magically appearing back slash in my result string.



You are confusing the printed representation of the string with the 
contents of the string. The backslash is not part of the string, any 
more than the leading and trailing quotes are part of the string. They 
are part of the display of the string.


Consider:

  

s = "ABC"  # Three characters A B C.
s  # Looks like five?


'ABC'
  

len(s)  # No, really only three.


3

The quotes are not part of the string, but part of the printable 
representation. This is supposed to represent what you would type to 
get the string ABC. You have to type (quote A B C quote).


Now consider:

  

s = """A"'"B"""  # Five chars A double-quote single-quote d-quote B
s  # Looks like eight?


'A"\'"B'
  

len(s)  # But actually only five.


5

When printing the representation of the string, Python always wraps it 
in quotation marks. If the contents include quotation marks as well, 
Python will escape the inner quotation marks if needed, but remember 
this is only for the display representation. If you want to see what 
the string looks like without the external quotes and escapes:


  

print s


A"'"B


  

 >>> NewString = """'"""
 >>> NewString

"'"
Hmmm, no back slash this time...



In this case, the string itself only contains a single-quote, no 
double-quote, so when showing the representation, Python wraps the 
contents with double-quotes and there is no need to escape the 
single-quote.


Python's rules for showing the representation of the string includes:

* wrap the string contents in single quotes '
* unless the string contains single quotes, in which case wrap it 
  in double quotes " and display the single quotes unescaped
* unless the string contains double quotes as well, in which case 
  wrap it in single quotes and escape the inner single quotes.


But remember: this is only the display of the string, not the contents.
  
Thank you, that was a very concise description, and has aided my 
comprehension greatly. Now if I can just keep it separate from the 
syntax in JavaScript, I'll be doing good. I keep a good record of these 
forum posts, so I can re-read this if necessary.


Later, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


Re: [Tutor] First program

2010-03-13 Thread Ray Parrish

Ken G. wrote:

I am using Ubuntu 9.04 and I have versions 2.6.2 and 3.0.1+ installed.

Look for IDLE in Add/Remove Applications.
Perhaps, you may have a different version of Ubuntu.

Ken

Ray Parrish wrote:
 
Yes, I'm using 2.45.2 as that is the highest version available in the 
Ubuntu repositories, and I'd like to keep it simple for users of 
programs i write. If I install a higher version from somewhere other 
than the repositories it will force users of my programs to do the 
same, and the repositories are the trusted source of software for 
Ubuntu, and other Linux users.


Thanks for being so observant.

Later, Ray Parrish
That should have been version 2.5.2 without the 4 I typoed. I'm using 
Hardy Heron, 8.04, and plan to stick with it until version 10 comes out 
as it is the next LTS version.


I'm eyeballing Idle in Synaptic, and it appears to be an IDE for version 
2.5 of Python for my version of Ubuntu. Idle is available for version 
2.4 there as well. I'm pretty used to writing all of my code with only 
the aid of syntax highlighting, but have been hankering for an editor 
that will allow tabbed, or collapsible functions, and collapsible block 
elements.


Does Idle do that? I guess I'll just go ahead and install it, and give 
it a try. Some IDEs I don't like with their project centric viewpoint, 
and my inability to quickly absorb their project file structures details 
to the point I can use them.


If that doesn't happen pretty much within ten or fifteen minutes I go 
back to gedit. 8-)


Later, Ray Parrish


--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


Re: [Tutor] Escaping a single quote mark in a triple quoted string.

2010-03-13 Thread Ray Parrish

Sander Sweers wrote:

On 13 March 2010 18:33, Ray Parrish  wrote:
  

Hello,

I am getting the following -



String = """http://www.rayslinks.com";>Ray's Links"""
String
  

'http://www.rayslinks.com";>Ray\'s Links'

Note the magically appearing back slash in my result string.



It is not really there. When you have a single quote in a single quote
string it need to be escaped with a backslash. Simulary a double quote
in a double quote string. The triple quote string does this for you.

When you write the string to a file the backslash will "magically" disappear.

  

Now I tiry to escape the single quote.



String = """http://www.rayslinks.com";>Ray\'s Links"""
String
  

'http://www.rayslinks.com";>Ray\'s Links'

Once again the unwanted back slash appears.



See above single quotes in single quote string need to be escaped.

  

NewString = """'"""
NewString
  

"'"
Hmmm, no back slash this time...



Correct, here you have a single quote in a double quote string.

  

String = """http://www.rayslinks.com";>Ray""" + """'""" + """s
Links"""
String
  

'http://www.rayslinks.com";>Ray\'s Links'

Why did quoting the single quote work in NewString when I triple quoted just
the single quote, but not in the other examples where the result shows a
back slash before my singe quote in String?

Is there a proper way to do this?



You are trying to solve something that is not really a problem. What
is however is your usage of a single quote in html text. You need to
replace it with ' or ‘.

Greets
Sander
  
Thanks for your quick answer. I'll accept that there will not be a back 
slash in my file when I write it then.


I was not aware that single quotes were bad news in HTML. I don't think 
I've ever experienced a problem using them myself, but will adjust, and 
start using the & code for it instead.


Later, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


Re: [Tutor] First program

2010-03-13 Thread Ray Parrish

Andre Engels wrote:

On Sat, Mar 13, 2010 at 3:11 AM, Ray Parrish  wrote:
  

Andre Engels wrote:


On 3/12/10, yd  wrote:
  

 else:
   raise Exception('{0}, is not a valid choice'.format(choice))



This will cause the program to stop-with-error if something wrong is
entered. I think that's quite rude. I would change this to:
 else:
   print('{0}, is not a valid choice'.format(choice))

  

Here's what I get from that, could you please explain why?



You're probably using Python 2.4 or 2.5; the .format method has been
introduced in Python 2.6, and is considered the 'standard' way of
working in Python 3. For older Python versions, this should read

print('%s, is not a valid choice'%(choice))
  
Yes, I'm using 2.45.2 as that is the highest version available in the 
Ubuntu repositories, and I'd like to keep it simple for users of 
programs i write. If I install a higher version from somewhere other 
than the repositories it will force users of my programs to do the same, 
and the repositories are the trusted source of software for Ubuntu, and 
other Linux users.


Thanks for being so observant.

Later, Ray Parrish
--

Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


Re: [Tutor] First program

2010-03-13 Thread Ray Parrish

Luke Paireepinart wrote:

Ray, please reply on-list in the future in case someone else has input.

On Fri, Mar 12, 2010 at 8:01 PM, Ray Parrish <mailto:c...@cmc.net>> wrote:


Luke Paireepinart wrote:

   print "A %s with dimensions %sx%s has an area of %s." %
(choice, height, width, width*height)


Isn't it a little more understandable to use a construct like the
following?

>>> print "The area of a " + Choice + "is " str(Width) + " x " +
str(Height) + " equals " + str(Width * Height) + " square feet"

The area of a rectangle is 12 x 10 equals 120 square feet.

I find that putting the variables on the end like that, when
you're not actually applying any special formatting to them makes
it less readable when I'm debugging my stuff, or when someone else
is reading my code, and trying to understand it.


Your version creates at least 10 intermediate strings before outputting.
Remember strings are immutable in Python. 
So you're constructing strings

The area of a
The area of a rectangle
The area of a rectangle is
12
The area of a rectangle is 12
The area of a rectangle is 12 x
10
The area of a rectangle is 12 x 10
The area of a rectangle is 12 x 10 equals
120
The area of a rectangle is 12 x 10 equals 120
The area of a rectangle is 12 x 10 equals 120 square feet

With string formatting you avoid all of these intermediate strings, so 
it's arguably more efficient.
Other than just viewing from a performance standpoint though, I find 
it much easier to read my version, because any computation required 
takes place at the end of the line.
For example, your inline str(width*height) requires you to read the 
whole line to see it.


It's really a personal thing, it's easier for me to read the 
formatting version than the string concatenation version, in most cases.
Now if you had used the comma convention I would have seen your 
point.  This is, I think, the easiest to read of all 3

area = width * height
print "The area of a", choice, "is", width, "x", height, ", which 
equals", area, "square feet."


Also, why are you capitalizing variable names?  That's a pretty 
unusual convention.


-Luke
Thanks for letting me know how inefficient my method is. I'll remember 
that, and apply your suggestions to my code from now on. So, you're 
saying that the commas method also does not suffer from the overhead of 
creating a bunch of individual strings?


As far as the capitalizations, it's just a habit I've held over from my 
Visual Basic days, and earlier programming. It's a little easier for me 
to pick out the individual words in a variable like ThisPerson as 
opposed to thisperson. I have been made aware that Python standard 
coding practice requires lower case variable names, and as soon as I can 
force myself to do it that way, or can remember to post that way I will 
be adopting the in place standards and conventions.


Is it actually supposed to be this_person? I read part of the standards 
document, but can not remember right off the top of my head if 
underlines between words is required.


Thanks, Ray Parrish



--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


Re: [Tutor] First program

2010-03-13 Thread Ray Parrish

Luke Paireepinart wrote:



On Sat, Mar 13, 2010 at 3:03 AM, Alan Gauld <mailto:alan.ga...@btinternet.com>> wrote:


"Ray Parrish" mailto:c...@cmc.net>> wrote

   print "A %s with dimensions %sx%s has an area of %s." %
(choice, height, width, width*height)

Isn't it a little more understandable to use a construct like
the following?

print "The area of a " + Choice + "is " str(Width)
+ " x " +

str(Height) + " equals " + str(Width * Height) + " square feet"


It depends on where you come from.
Those of us brought up on C or COBOL are used to separating the
presentation from the data. Those brought up with PASCAL and BASIC
are used to iterleaving data with presentation.

One thing - you don't need all the str() calls in your example,
print already calls str() for you. Also comma separators are
better than + signs since the plus operation on strings is quite
expensive - you create a new string for each addition.


print actually doesn't call str if you use concatenation.  So the 
str() calls are necessary if you do not use "," but use "+" instead. 
So there are at least 2 reasons why + is worse than comma.

Another thing to be aware of is that if you use commas,
print inserts a space in the string, which may be either an advantage 
or a disadvantage depending on what you're trying to do.


-Luke
Ahhh, thank you for the clarifications. for the cases where spaces are 
needed it the commas seem like a pretty good way to do it, but when I 
need to add together stuff with no spaces I'll try the formatted method.


Thanks again, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


Re: [Tutor] First program

2010-03-13 Thread Ray Parrish

Alan Gauld wrote:

"Ray Parrish"  wrote
print "A %s with dimensions %sx%s has an area of %s." % (choice, 
height, width, width*height)


Isn't it a little more understandable to use a construct like the 
following?


print "The area of a " + Choice + "is " str(Width) + " x " + 

str(Height) + " equals " + str(Width * Height) + " square feet"


It depends on where you come from.
Those of us brought up on C or COBOL are used to separating the 
presentation from the data. Those brought up with PASCAL and BASIC are 
used to iterleaving data with presentation.


One thing - you don't need all the str() calls in your example, print 
already calls str() for you. Also comma separators are better than + 
signs since the plus operation on strings is quite expensive - you 
create a new string for each addition.


HTH,
Thanks for the tips. If I understand you correctly I can do the call 
this way? -


print "The area of ", Choice, " is ", Width, " x ", Height, " 
equals ", (Width * Height), " square feet"


That eliminates the redundancy, and is quite readable to me. I like it. 8-)

Later, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


Re: [Tutor] First program

2010-03-13 Thread Ray Parrish

Steven D'Aprano wrote:

On Sat, 13 Mar 2010 01:04:42 pm Ray Parrish wrote:
  

print "A %s with dimensions %sx%s has an area of %s." %
(choice, height, width, width*height)
  

Hello,

Isn't it a little more understandable to use a
construct like the following?



print "The area of a " + Choice + "is " str(Width) + " x " +
  

str(Height) + " equals " + str(Width * Height) + "
square feet"

The area of a rectangle is 12 x 10 equals 120
square feet.

I find that putting the variables on the end like
that, when you're not actually applying any special formatting to them
makes it less readable
when I'm debugging my stuff, or when someone else
is reading my code,
and trying to understand it.




Of course you are welcome to use whatever coding standards you like, but 
I think you will find that among experienced coders, you are in a 
vanishingly small minority. As a beginner, I found string interpolation 
confusing at first, but it soon became second-nature. And of course, 
there are legions of C coders who are used to it.


I find an expression like:

"The area of a " + Choice + "is " str(Width) + " x " + str(Height) 
+ "equals " + str(Width * Height) + "square feet"


difficult to follow: too many quotes, too many sub-expressions being 
added, too many repeated calls to str(), it isn't clear what is the 
template and what is being inserted into the template. It is too easy 
to miss a quote and get a SyntaxError, or to forget to add spaces where 
needed. To me, this is MUCH easier:


template = "The area of a %s is %s x %s equals %s square feet"
print template % (Width, Height Width*Height)

One pair of quotes instead of five, no problems with remembering to add 
spaces around pieces, and no need to explicitly call str().
  
OK, that does seem a bit easier now to me. I'm going to have to read up 
on the %s, and any other formatting % codes there are however, since I'm 
dead green in Python yet. 8-)


So, would I read about those in the string module portion of the 
documentation?


Thanks, Ray Parrish


--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


Re: [Tutor] Opening a dos exe

2010-03-13 Thread Ray Parrish

Alan Gauld wrote:


"Ray Parrish"  wrote

but I cannot find a way of getting this information to the dos program
from python. Any ideas?

You could use os.system("startprogram.bat"), and create startprogram.bat
to run the dos program, and feed it the files, either all at once, or 
one at a time via the command line if it accepts command line input.


I don't think you can do the second option with a bat file. There is 
no way to interactively respond to the program once it starts. Thats 
why WSH is better for that kind of interactive input.


Or just use subprocess.Popen...

OK, it was not clear to me that he needed to be interactive with the dos 
program. He just said he needed to feed those files to the dos program, 
and I assumed he meant on the command line within a dos box, which can 
indeed be solved by running a batch file, if the dos program accepts 
command line parameters..


A good tool for writing interactive scripts used to be WinBatch, but I 
haven't used, or seen it anywhere for years. It would have to be 
installed on the machine you wanted to get interactive with a dos 
program however.


I remember WinBatch from back in the Windows 3.1 days, and am not sure 
if they are still keeping it up to date.


Later, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


[Tutor] Escaping a single quote mark in a triple quoted string.

2010-03-13 Thread Ray Parrish

Hello,

I am getting the following -

>>> String = """http://www.rayslinks.com";>Ray's Links"""
>>> String
'http://www.rayslinks.com";>Ray\'s Links'

Note the magically appearing back slash in my result string. Now I tiry 
to escape the single quote.


>>> String = """http://www.rayslinks.com";>Ray\'s Links"""
>>> String
'http://www.rayslinks.com";>Ray\'s Links'

Once again the unwanted back slash appears.

>>> NewString = """'"""
>>> NewString
"'"
Hmmm, no back slash this time...

>>> String = """http://www.rayslinks.com";>Ray""" + """'""" + 
"""s Links"""

>>> String
'http://www.rayslinks.com";>Ray\'s Links'

Why did quoting the single quote work in NewString when I triple quoted 
just the single quote, but not in the other examples where the result 
shows a back slash before my singe quote in String?


Is there a proper way to do this?

Thanks, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com


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


Re: [Tutor] Opening a dos exe

2010-03-13 Thread Ray Parrish

Emile van Sebille wrote:

On 3/10/2010 11:33 AM Armstrong, Richard J. said...

The problem comes in that the dos program requires three
inputs (input1.txt, input2.txt and input3.txt - see attached picture)
but I cannot find a way of getting this information to the dos program
from python. Any ideas?
You could use os.system("startprogram.bat"), and 
create startprogram.bat
to run the dos program, and feed it the files, 
either all at once, or
one at a time via the command line if it accepts 
command line input.


You could write out startprogram.bat 
programatically, just before you
call it, then remove it after running it to reduce 
clutter.


Hope this is what you're looking for.

Later, Ray Parrish

--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, 
and articles by Ray.

http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to 
be a schizo, and other

things, including my poetry.
http://www.writingsoftheschizophrenic.com



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


Re: [Tutor] First program

2010-03-12 Thread Ray Parrish

Luke Paireepinart wrote:



On Fri, Mar 12, 2010 at 4:03 AM, yd <mailto:ydmt...@gmail.com>> wrote:


Hi,
I am new to programming, altough i have read a few books about OOP
and O'Reily's Learning Python.
I would like some critique on my first program, is it normal for
it to be this long to do something simple?
I know i could have turned some of these things into classes and
functions but i don't know how to do that yet.
Some critique of the algorithm and writing style or anything in
general would help and any pointers would be appreciated.
Thanks.
 

One thing you should do is use more indentation.  Your program 
structure is VERY hard to read with single space indentations.  
Consider using 4-spaces.

This is not overtly long, I would say it's reasonable for a first program.
There are some things that you could probably group together, though.  
Remember that code reuse is one of the core tenets of computer 
programming!


One example: area of square, parallelogram and rectangle are all the 
same.  You could do something like this (assuming user enters strings 
as choice (rather than ints)):

if choice in ['rectangle', 'square', 'parallelogram']:
height = int(raw_input("Height: "))
if choice != 'square':
width = int(raw_input("Width: "))
else:
width = height
print "A %s with dimensions %sx%s has an area of %s." % (choice, 
height, width, width*height)





Hello,

Isn't it a little more understandable to use a 
construct like the

following?

print "The area of a " + Choice + "is " str(Width) + " x " + 
str(Height) + " equals " + str(Width * Height) + " 
square feet"


The area of a rectangle is 12 x 10 equals 120 
square feet.


I find that putting the variables on the end like 
that, when you're not
actually applying any special formatting to them 
makes it less readable
when I'm debugging my stuff, or when someone else 
is reading my code,

and trying to understand it.

Later, Ray Parrish



--
Linux dpkg Software Report script set..
http://www.rayslinks.com/LinuxdpkgSoftwareReport.html
Ray's Links, a variety of links to usefull things, 
and articles by Ray.

http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to 
be a schizo, and other

things, including my poetry.
http://www.writingsoftheschizophrenic.com



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


Re: [Tutor] First program

2010-03-12 Thread Ray Parrish

Andre Engels wrote:

On 3/12/10, yd  wrote:
  

Hi,
I am new to programming, altough i have read a few books about OOP and
O'Reily's Learning Python.
I would like some critique on my first program, is it normal for it to be
this long to do something simple?



Well, many of your lines are user interface. Writing two lines of text
to the user will in general cost you (at least) two lines of code.

  

I know i could have turned some of these things into classes and functions
but i don't know how to do that yet.



I definitely see use for functions (long lists of if...elif.. are
usually better modeled using functions and a dictionary); using
classes feels like overkill for something this simple.

  

Some critique of the algorithm and writing style or anything in general
would help and any pointers would be appreciated.



General remark: The usual number of spaces indented per level is 4,
rather than the 2 that you use. This makes it easier to see the
indentation level at first glance.

  

#title Area calculator
#author Yudhishthir Singh


#welcome screen
msg = 'Welcome to the area calculator program '
print(msg)
print('-'*len(msg))
loop = 'y'
print()
while loop == 'y':
  #Choices menu
  print('Please select a shape\n')
  print('1. Rectangle')
  print('2. Square')
  print('3. Parallelogram ')
  print('4. Trapezoid ')
  print('5. Circle ')
  print('6. Ellipse')
  print('7. Traingle\n')
  print('-'*len(msg))
  choice = input('\nPlease enter your choice: ')
  if choice.isdigit() ==True:
choice = int(choice)



1. The if can be shortened to

if choice.isdigit():

2. This thing can be removed completely if you chance the if-statements below to

if choice == "1"

etcetera.

  

  if choice ==1:
#Rect
height = input('please enter the height: ')
width = input('please enter the width: ')
height = int(height)
width = int(width)
areaRectangle = height*width
print('\nThe area of a rectangle with {0} height and {1} width is
'.format(height,width),areaRectangle,'\n')



I think it's ugly to mix styles here - either use format or use commas, not both

  

  elif choice ==2:
#Square
side = input('enter the height or width: ')
side = int(side)
areaSquare = side**2
print('\nThe area of a square with a height or width of {0} is
'.format(side), areaSquare,'\n')
  elif choice ==3:
#Parallelogram
height = input('enter the height: ')
base = input('enter the width aka base: ')
height = int(height)
base = int(base)
areaParallelogram = height*base
print('\nThe area of a parrallelogram with height {0} and width {1} is
'.format(height,base), areaParallelogram,'\n')
  elif choice ==4:
#Trapezoid
height = input('enter the height: ')
base1 = input('enter the width of shorter side: ')
base2 = input('enter the width of longer side: ')
height = int(height)
base1 = int(base1)
base2 = int(base2)
areaTrapezoid = (height/2)*(base1+base2)
print('\nThe area of a trapezoid with height {0} ,base {1} and {2} is
'.format(height,base1,base2), areaTrapezoid, '\n')
  elif choice ==5:
#Circle
radius = input('radius: ')
radius = int(radius)
areaCircle = 3.14*(radius**2)
print('\nThe area of a circle with radius {0} is '.format(radius),
areaCircle, '\n')
  elif choice ==6:
#Ellipse
radius1 = input('enter length of radius 1: ')
radius2 = input('enter length of radius 2: ')
radius1 = int(radius1)
radius2 = int(radius2)
areaEllipse = 3.14*radius1*radius2
print('\nThe area of an ellipse with radii of length {0} and {1} is
'.format(radius1,radius2), areaEllipse, '\n')
  elif choice ==7:
#Triangle
base = input('enter base: ')
height = input('enter height: ')
base = int(base)
height = int(height)
areaTriangle = (1/2 *base)*height
print('\nThe area of a triange with height {0} and base {1} is
'.format(height,base), areaTriangle, '\n')
  else:
raise Exception('{0}, is not a valid choice'.format(choice))



This will cause the program to stop-with-error if something wrong is
entered. I think that's quite rude. I would change this to:
  else:
print('{0}, is not a valid choice'.format(choice))
  


Here's what I get from that, could you please explain why?

>>> print('{0}, is not a valid choice'.format(choice))
Traceback (most recent call last):
 File "", line 1, in 
AttributeError: 'str' object has no attribute 'format'
>>&