Re: [Tutor] implementing rot 13 problems

2013-03-11 Thread Jos Kerc
On Tue, Mar 12, 2013 at 6:57 AM, RJ Ewing  wrote:

> I am trying to implement rot 13 and am having troubles. My code is as
> follows:
>
> class Rot_13(webapp2.RequestHandler):
> def write_form(self, user=""):
>  self.response.out.write(rot_form % user)
>  def get(self):
> self.write_form()
>  def post(self):
>  user = self.request.get("text")
> s = self.rot_text(user)
>  print s
> s = "".join(s)
>  self.escape_html(s)
> self.write_form(s)
>

You might want to read up on the translate() method.

>  def rot_text(self, s):
>  ls = [i for i in s]
> for i in ls:
>  if i.isalpha():
> if i.isupper():
>  if i <= 'M':
> x = ls.index(i)
>  ls[ls.index(i)] = chr(ord(i) + 13)
> else:
>  x = ls.index(i)
> ls[ls.index(i)] = chr(ord(i) - 13)
>  elif i.islower():
>  if i <= 'm':
> x = ls.index(i)
>  ls[x] = chr(ord(i) + 13)
> elif i > 'm':
>  x = ls.index(i)
> ls[x] = chr(ord(i) - 13)
>  return ls
>
>  def escape_html(self, s):
> return cgi.escape(s, quote = True)
>
> Now if I enter abc, I get nop. But if I enter abcdefghijklmnop, I
> get abcqrstuvwxyznop. I have included print statements to check if ls[x] is
> changing and it is, but something is going wrong when I return the ls, and
> I am not quite sure what it is.
>
> Me neither, how are you calling /returning it?

> Thanks
>
> ___
> 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] implementing rot 13 problems

2013-03-11 Thread Danny Yoo
The use of index() here to find the target place where the translation
is going to occur is very fragile.

Consider: we conceptually already should know where in the list we
want the target to be, since we're walking across the characters in
the list.  We know that we're looking first at ls[0], transforming it,
then ls[2], then ls[3], then...

The fact that we're using index() here, to try to recompute that
target position, is doing "extra" work: we're searching for something
that we should have already known.  And it turns out that not only is
it superfluous, but it's broken: there may be several places in the
list that qualify, not just the one that you're considering.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help with python keyboard press/navigation commands

2013-03-11 Thread Danny Yoo
On Mon, Mar 11, 2013 at 8:59 PM, akuma ukpo  wrote:
> This is the problem
>
> Implement a function called get_direction which, on a particular character
> , gives the
> direction corresponding to that character.


Do you know how to write a test case for this function?

You had test cases for one of the previous problems you sent the list
earlier.  Can you do the same thing for this problem?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] implementing rot 13 problems

2013-03-11 Thread RJ Ewing
I am trying to implement rot 13 and am having troubles. My code is as
follows:

class Rot_13(webapp2.RequestHandler):
def write_form(self, user=""):
self.response.out.write(rot_form % user)
 def get(self):
self.write_form()
 def post(self):
user = self.request.get("text")
s = self.rot_text(user)
print s
s = "".join(s)
self.escape_html(s)
self.write_form(s)
 def rot_text(self, s):
ls = [i for i in s]
for i in ls:
if i.isalpha():
if i.isupper():
if i <= 'M':
x = ls.index(i)
ls[ls.index(i)] = chr(ord(i) + 13)
else:
x = ls.index(i)
ls[ls.index(i)] = chr(ord(i) - 13)
 elif i.islower():
if i <= 'm':
x = ls.index(i)
ls[x] = chr(ord(i) + 13)
elif i > 'm':
x = ls.index(i)
ls[x] = chr(ord(i) - 13)
return ls

def escape_html(self, s):
return cgi.escape(s, quote = True)

Now if I enter abc, I get nop. But if I enter abcdefghijklmnop, I
get abcqrstuvwxyznop. I have included print statements to check if ls[x] is
changing and it is, but something is going wrong when I return the ls, and
I am not quite sure what it is.

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


[Tutor] Need help with python keyboard press/navigation commands

2013-03-11 Thread akuma ukpo
This is the problem

Implement a function called get_direction which, on a particular character
, gives the
direction corresponding to that character. The correspondences are as
follows:
 The character ’w’ corresponds to the direction ’North’
 The character ’a’ corresponds to the direction ’West’
 The character ’s’ corresponds to the direction ’South’
 The character ’d’ corresponds to the direction ’East’


I am having difficulty coming up with a function and testing it as well.

I have tried

def get_direction(self):
"""
whenever a key is pressed the character moves to
the direction corresponding to the key
"""
'North':w
'South': s
'East' : d
'West' : a

but it didn't work...please help.
-- 
*Akuma*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess module: when to _NOT_ use shell=True

2013-03-11 Thread akleider
Thank you "Eryksun" and "EikeWek" for your responses.  It is this sort of
thing that begins to pull one out of the newbie into the intermediate
category. I'm grateful.
alex

> I've not found anywhere a clear explanation of when not to set shell=True.
> If the command line must be interpreted by the shell then clearly this
> must be set. So the question that comes up is why not set it always?
> In an effort to investigate, I came up with the following script that
> indicates that the shell looks at only the first string in the array if
> the first parameter is an array rather than a string. Switching between
> cmd being a string vs an array and shell being set or not set gives 4
> possibilities.
> Any comments?
>
> #!/usr/bin/env python
>
> # file :  test.py (Python 2.7, NOT Python 3)
> # Running on Linux platform (Ubuntu.)
> print 'Running "tes.py"'
>
> import subprocess
>
> cmd = ["ls", "-l"]
> # cmd = "ls -l"
> p = subprocess.Popen(cmd,
> # shell=True,
> stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
> (s_out, s_err) = p.communicate()
> print "Std_out returns:\n%s\nStd_err returns:\n%s\n"%\
>   (s_out, s_err, )
>
>
> ___
> 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] Print to file

2013-03-11 Thread Hugo Arts
On Mon, Mar 11, 2013 at 10:38 AM, Phil  wrote:

> On 11/03/13 18:35, Hugo Arts wrote:
>
>> On Mon, Mar 11, 2013 at 9:24 AM, Phil > > wrote:
>>
>> The usage of print to file is much like the Arduino print to serial
>> device and it's got me curious to know what the correct syntax is.
>> Neither of the following is correct, but the second one seems closer
>> to the mark.
>>
>>  >>> print("test", file="/home/phil/Python/words"**__)
>>
>> Traceback (most recent call last):
>>File "", line 1, in 
>> AttributeError: 'str' object has no attribute 'write'
>>
>>  >>> f = open("/home/phil/Python/words"**__, 'w')
>>
>>  >>> print("test", file=f
>>
>> I've used the "with" statement to write and read files but print to
>> file could be handy some time.
>>
>>
>> The second one should be correct, if you add the closing parenthesis of
>> print. Closing the file afterwards is not strictly required, I think,
>> though it is good practice.
>>
>>
> Thanks Hugo,
>
> I did forget the closing parenthesis but even so the text file "words" was
> created but it's empty.


Make sure you "reply all" so the rest of the mailing list also gets your
response.

Did you exit the interpreter before checking the file contents? If not, the
string you printed might still be in a buffer, waiting to be printed until
the buffer is flushed. Buffers are usually flushed when they are full, when
flush() is called on the buffered object, or when the object is garbage
collected.

Try calling f.flush() or f.close() after you print, and see if that works.

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


Re: [Tutor] Print to file

2013-03-11 Thread Alan Gauld

On 11/03/13 08:24, Phil wrote:


Neither of the following is correct, but the second one seems closer to
the mark.

 >>> print("test", file="/home/phil/Python/words")
Traceback (most recent call last):
   File "", line 1, in 
AttributeError: 'str' object has no attribute 'write'


So this is obviously wrong, it reports an error.


 >>> f = open("/home/phil/Python/words", 'w')
 >>> print("test", file=f



But apart from the lack of a closing paren what makes you
think this is wrong? What happened?


I've used the "with" statement to write and read files but print to file
could be handy some time.


with is about opening files regardless of what you do
to them so you could use with here too...

with open(("/home/phil/Python/words", 'w') as f:
print( "test", file=f)

I'm not sure what you think the problem is?

--
Alan G
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] Print to file

2013-03-11 Thread Hugo Arts
On Mon, Mar 11, 2013 at 9:24 AM, Phil  wrote:

> The usage of print to file is much like the Arduino print to serial device
> and it's got me curious to know what the correct syntax is. Neither of the
> following is correct, but the second one seems closer to the mark.
>
> >>> print("test", file="/home/phil/Python/words"**)
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: 'str' object has no attribute 'write'
>
> >>> f = open("/home/phil/Python/words"**, 'w')
> >>> print("test", file=f
>
> I've used the "with" statement to write and read files but print to file
> could be handy some time.


The second one should be correct, if you add the closing parenthesis of
print. Closing the file afterwards is not strictly required, I think,
though it is good practice.

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


[Tutor] Print to file

2013-03-11 Thread Phil
The usage of print to file is much like the Arduino print to serial 
device and it's got me curious to know what the correct syntax is. 
Neither of the following is correct, but the second one seems closer to 
the mark.


>>> print("test", file="/home/phil/Python/words")
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'str' object has no attribute 'write'

>>> f = open("/home/phil/Python/words", 'w')
>>> print("test", file=f

I've used the "with" statement to write and read files but print to file 
could be handy some time.


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


Re: [Tutor] Bar Operator ??

2013-03-11 Thread Mark Lybrand
Thanks.  I think I kind of get it.  It probably will take a while for the
nuances of the filter to sink. Some things are filters that don't seem like
they should be, but that seems like my problem.

Mark :)


On Sun, Mar 10, 2013 at 11:33 PM, Danny Yoo  wrote:

> >
> > It's a filter:
> >
> > https://docs.djangoproject.com/en/1.5/ref/templates/builtins/
>
> And to be explicit about Mitya's point: it's technically not Python.
> It's part of Django's template language.  If it were Python, it'd be
> interpreted as the "or" bitwise operator,
>
>
> http://docs.python.org/3/reference/expressions.html#binary-bitwise-operations
>
> but that's definitely not what's happening here.  In your context, as
> part of a Django template document, interpret "|" as notation for the
> filtering operation.
>



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