Python

Moderators: None (Apply to moderate this forum)
Number of threads: 473
Number of posts: 1172

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Can this code be shortened? Posted by falcon3166 on 24 Aug 2006 at 7:48 PM
Can the following snippets be reduced? The total lines of each snippet is 136!


while n <= x:
     if char == 'A':
          newstr = newstr + 'D'
     elif char == 'B':
          newstr = newstr + 'E'
     elif char == 'C':
          newstr = newstr + 'F'
...
    elif char == '!':
          newstr = newstr + 'A'
    elif char == '-':
          newstr = newstr + 'B'
    elif char == ' ':
          newstr = newstr + 'C'
    str = newstr

while n <= x:
      if char == 'A':
           newstr = newstr + '!'
      elif char == 'B':
           newstr = newstr + '-'
     elif char == 'C':
          newstr = ' '
...
     elif char == '!':
          newstr = newstr + ','
     elif char == '-':
          newstr = newstr + '.'
     elif char == ' ':
          newstr = newstr + '?'
     str = newstr


Any ideas?

Thanks in advance,
Nathan
Swat spam before it fills your inbox!
Report
Re: Can this code be shortened? Posted by Gregry2 on 25 Aug 2006 at 3:25 AM
: Can the following snippets be reduced? The total lines of each snippet is 136!
:
:
: 
: while n <= x:
:      if char == 'A':
:           newstr = newstr + 'D'
:      elif char == 'B':
:           newstr = newstr + 'E'
:      elif char == 'C':
:           newstr = newstr + 'F'
: ...
:     elif char == '!':
:           newstr = newstr + 'A'
:     elif char == '-':
:           newstr = newstr + 'B'
:     elif char == ' ':
:           newstr = newstr + 'C'
:     str = newstr
: 

:
: while n <= x:
:       if char == 'A':
:            newstr = newstr + '!'
:       elif char == 'B':
:            newstr = newstr + '-'
:      elif char == 'C':
:           newstr = ' '
: ...
:      elif char == '!':
:           newstr = newstr + ','
:      elif char == '-':
:           newstr = newstr + '.'
:      elif char == ' ':
:           newstr = newstr + '?'
:      str = newstr
: 

:
: Any ideas?
:
: Thanks in advance,
: Nathan
: Swat spam before it fills your inbox!
:

What are you trying to do? May be its possible to generalize things... post the whole code, if you can...or you yourself look for patterns you can exploit

like for the first part
      if char == 'A':
           newstr = newstr + 'D'
      elif char == 'B':
           newstr = newstr + 'E'
      elif char == 'C':
           newstr = newstr + 'F'

you can go (pseduocode)

      if char is in range 'A' to 'D'
            newstr = newstr + str(int(char)+3)

...thats how I would do it in C, or something like that, it runs atop the fact that characters are numbers, but I'm not sure you can do such stuff in python efficiently...

if that didn't explain much (I doubt it did), just post mroe of your code, and we can help :)

{2}rIng
Report
How can I fix this code? (was Re: Can this code be shortened?) Posted by falcon3166 on 25 Aug 2006 at 10:39 AM
: : Can the following snippets be reduced? The total lines of each snippet is 136!
: :
: :
: : 
: : while n <= x:
: :      if char == 'A':
: :           newstr = newstr + 'D'
: :      elif char == 'B':
: :           newstr = newstr + 'E'
: :      elif char == 'C':
: :           newstr = newstr + 'F'
: : ...
: :     elif char == '!':
: :           newstr = newstr + 'A'
: :     elif char == '-':
: :           newstr = newstr + 'B'
: :     elif char == ' ':
: :           newstr = newstr + 'C'
: :     str = newstr
: : 

: :
: : while n <= x:
: :       if char == 'A':
: :            newstr = newstr + '!'
: :       elif char == 'B':
: :            newstr = newstr + '-'
: :      elif char == 'C':
: :           newstr = ' '
: : ...
: :      elif char == '!':
: :           newstr = newstr + ','
: :      elif char == '-':
: :           newstr = newstr + '.'
: :      elif char == ' ':
: :           newstr = newstr + '?'
: :      str = newstr
: : 

: :
: : Any ideas?
: :
: : Thanks in advance,
: : Nathan
: : Swat spam before it fills your inbox!
: :
:
: What are you trying to do? May be its possible to generalize things... post the whole code, if you can...or you yourself look for patterns you can exploit
:
: like for the first part
:
:       if char == 'A':
:            newstr = newstr + 'D'
:       elif char == 'B':
:            newstr = newstr + 'E'
:       elif char == 'C':
:            newstr = newstr + 'F'
: 

: you can go (pseduocode)
:
:
:       if char is in range 'A' to 'D'
:             newstr = newstr + str(int(char)+3)
: 

: ...thats how I would do it in C, or something like that, it runs atop the fact that characters are numbers, but I'm not sure you can do such stuff in python efficiently...
:
: if that didn't explain much (I doubt it did), just post mroe of your code, and we can help :)
:
: {2}rIng
:

I'm trying to encrypt/decrypt messages. I switched to PyCrypto and ezPyCrypto and got the following error:

>>> from ezPyCrypto import key
>>> str = '4 big dogs.'
>>> key.encString(str)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in ?
key.encString(str)
TypeError: unbound method encString() must be called with key instance as first argument (got str instance instead)
>>>

How can I fix the error
Swat spam before it fills your inbox!

Report
Re: How can I fix this code? (was Re: Can this code be shortened?) Posted by infidel on 25 Aug 2006 at 10:48 AM
: I'm trying to encrypt/decrypt messages. I switched to PyCrypto and ezPyCrypto and got the following error:
:
: >>> from ezPyCrypto import key
: >>> str = '4 big dogs.'
: >>> key.encString(str)
: Traceback (most recent call last):
: File "<pyshell#6>", line 1, in ?
: key.encString(str)
: TypeError: unbound method encString() must be called with key instance as first argument (got str instance instead)
: >>>
:
: How can I fix the error

The problem is you're passing a string and it wants a key instead. The fact that it's calling it an "unbound" method tells me you're probably calling a class method without instantiating an object of that class. I've never used these libraries so I can only guess at the solution. Try something like this to see if it helps:

from exPyCrypto import key
k = key()
e = k.encString('4 big dogs.')


infidel

$ select * from users where clue > 0
no rows returned


Report
Re: How can I fix this code? (was Re: Can this code be shortened?) Posted by falcon3166 on 25 Aug 2006 at 11:28 PM
Report
Re: How can I fix this code? (was Re: Can this code be shortened?) Posted by falcon3166 on 25 Aug 2006 at 11:28 PM
Report
Re: Can this code be shortened? Posted by infidel on 25 Aug 2006 at 7:53 AM
: Can the following snippets be reduced? The total lines of each snippet is 136!
:
:
: 
: while n <= x:
:      if char == 'A':
:           newstr = newstr + 'D'
:      elif char == 'B':
:           newstr = newstr + 'E'
:      elif char == 'C':
:           newstr = newstr + 'F'
: ...
:     elif char == '!':
:           newstr = newstr + 'A'
:     elif char == '-':
:           newstr = newstr + 'B'
:     elif char == ' ':
:           newstr = newstr + 'C'
:     str = newstr
: 

:
: while n <= x:
:       if char == 'A':
:            newstr = newstr + '!'
:       elif char == 'B':
:            newstr = newstr + '-'
:      elif char == 'C':
:           newstr = ' '
: ...
:      elif char == '!':
:           newstr = newstr + ','
:      elif char == '-':
:           newstr = newstr + '.'
:      elif char == ' ':
:           newstr = newstr + '?'
:      str = newstr
: 


It looks to me like you're just doing a simple cipher-like translation. Python's string module can do this for you very easily:

import string
table = string.maketrans('ABC!- ', 'DEFABC')
print string.translate('!- ABC', table)



infidel

$ select * from users where clue > 0
no rows returned





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.