The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
psyco problem
Discuss psyco problem in the Python Programming forum on Dev Shed. psyco problem Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 26th, 2004, 05:41 PM
|
|
Registered User
|
|
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
psyco problem
I tried using psyco many times with simple scripts (I'm just learning python), but I always get error messages like this one:
"warning: input() cannot see the locals in functions bound by Psyco; consider using eval() in its two- or three-arguments form"
I searched the documentation, the manual, google, everywhere, looking for an example of how to use the three-arguments form of eval(), but I have no idea.
Can anyone give me a hint?
|

May 26th, 2004, 07:29 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 84
Time spent in forums: 8 h 7 m
Reputation Power: 10
|
|
Quote:
>>> help(eval)
Help on built-in function eval:
eval(...)
eval(source[, globals[, locals]]) -> value
Evaluate the source in the context of globals and locals.
The source may be a string representing a Python expression
or a code object as returned by compile().
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it. |
that should get you started.
|

May 26th, 2004, 07:46 PM
|
|
Registered User
|
|
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Thanks for your reply, I already read this help on the ide, but this is chinese for me...
I don't know where should I use eval in my code, what are the globals, the locals, etc...
Maybe if I send you the code you could find the problem. It is a simple banking simulation script that I translated to python from a c# book...
|

May 26th, 2004, 10:13 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
locals and globals are Python's scopes, like in a lot of languages. Though both are implimended as Python dictionaries; the global namespace holds names that are vistable from all over your your program. Locals on the other hand are visable only from within the function (first member class) or class. If you know C# these should be pretty familier consepts  .
Quote: >>> globals()
{...dictionary of global names...}
>>> locals()
{...dictionary of local names...}
>>> |
Code would be good though, maybe then we can see what your to doing  . Maybe also the C# code would be helpful.
Mark.
__________________
programming language development: www.netytan.com – Hula
|

May 27th, 2004, 07:14 AM
|
|
Registered User
|
|
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Hereinbelow is the python code (simplified version).
It works just fine without psyco...
(sorry, I don't know how to keep the indentation when pasting the text...)
By the way, I don't know c# exactly. I'm just trying to learn all at once in my scarce spear time (python, c#, php)...
Code:
import psyco
psyco.full()
class Account:
def __init__(self):
self.balance = 0
def Withdraw(self, amount):
self.balance-=amount
def Deposit(self, amount):
self.balance+= amount
def GetBalance(self):
return self.balance
class Bank:
def __init__(self):
self.accounts = {}
print 'Congratulations! You have created a new bank'
accountsQty=input('Please enter number of accounts in bank: ')
for i in range(accountsQty):
self.accounts[i]=Account()
def Deposit(self):
print 'DEPOSIT'
amount= input('Please enter amount to deposit: ')
accountNr= input('Enter account number: ')
self.accounts[accountNr - 1].Deposit(amount)
print 'New balance of account', accountNr,':', self.accounts[accountNr -1].GetBalance()
def Withdraw(self):
print 'WITHDRAW'
amount= input('Please enter amount to withdraw: ')
accountNr= input('Enter account number: ')
self.accounts[accountNr - 1].Withdraw(amount)
print 'New balance of account Nr. %s: %s' %(accountNr, self.accounts[accountNr -1].GetBalance())
def Trans(self):
print 'TRANSFER'
account1= input('Enter account of origin: ')
account2= input('Enter destination account: ')
amount= input('Enter amount to transfer: ')
self.accounts[account1-1].Withdraw(amount)
self.accounts[account2-1].Deposit(amount)
print '%s transfered from account Nr. %s to account Nr. %s' %(amount, account1, account2)
def PrintAllBalances(self):
print 'Account balances for all accounts'
for i in self.accounts.keys():
print 'Account Nr. %s :\t%s' %((i+1), self.accounts[i].GetBalance())
def PrintMenu():
print ''
print 'What would you like to do?'
print 'D)eposit'
print 'W)ithdraw'
print 'T)ransfer'
print 'P)rint all balances'
print 'E)nd session'
print ''
bigBucksBank= Bank()
command=1
while (command != 'E'):
PrintMenu()
command= raw_input()
command= command.upper()
if command=='D':
bigBucksBank.Deposit()
elif command=='W':
bigBucksBank.Withdraw()
elif command=='T':
bigBucksBank.Trans()
elif command=='P':
bigBucksBank.PrintAllBalances()
elif command=='E':
exit()
else:
print 'Invalid choice'
Last edited by netytan : May 27th, 2004 at 07:32 AM.
Reason: Please use [ Code ] tags in future :)
|

May 27th, 2004, 07:48 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Ok, from what I can see (and not being able to test the script right now) psyco is just giving you a helpful little hint about using input() in your methods - which is infact a shortcut for eval(raw_input()).
Try changing input() to int(raw_input()) and you shouldn't see the warning anymore. You may need to wrap these in a try-except block to catch any ValueError resulting in a user entering a string or expresion instead of a number.
Good luck with the learning, i'm sure you'll do fine though you might want to take a little time to master one before you move onto the other or you'll end up comfused  .
Hope this helps,
Mark.
|

May 27th, 2004, 07:50 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Its probably worth mentioning again that this isn't an error it's actually a warning. You should look though the Python docs about warnings, usually a good read  . There is even a way to turn them off! There should be a few examples on the forum if you have a quick search.
Later,
Mark.
|

May 27th, 2004, 08:02 AM
|
|
Registered User
|
|
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
I had tried replacing input by eval(raw_input()) before, as described in psyco docs, but this time I got a slightly different warning:
"warning: eval()/execfile() cannot see the locals in functions bound by Psyco; consider using eval() in its two- or three-arguments form"
So it seems that it doesn't solve the problem.
However, it's good to know it's just a warning, not an error...
|

May 27th, 2004, 08:06 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
eval() and int() are too different functions. The first evaluates a Python expresion and the second one preforms a type-cast. Give it a go.
Mark.
|

May 27th, 2004, 09:08 AM
|
|
Registered User
|
|
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
It works! Thank you!!
Last edited by luismg : May 27th, 2004 at 09:14 AM.
|

May 27th, 2004, 09:16 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Not a problem at all Luis. Glad you got it working for you  .
|

May 27th, 2004, 12:04 PM
|
|
Registered User
|
|
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
I just have one more question:
If I save the file as a compiled one (.pyc), it doesn't run. It crashes before starting.
On the other hand, if I save it just as a .py it works perfect (?)
|

May 28th, 2004, 01:57 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
The prblem your getting is related to the fact that .py files contain human readable Phon programs where .pyc files are only readable by the Python interpreter - Python bytecode.
You shouldn't need to save any Pthon program with .pyc as the interpreter automatically creates and uses these files where needed.
Check out the command line options -O and -OO for generating optomized bytecode files (.pyo).
Hope this helps,
Mark.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|