Thread: psyco problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Buenos Aires, Argentina
    Posts
    18
    Rep 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?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    >>> 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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Buenos Aires, Argentina
    Posts
    18
    Rep 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...
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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 .

    >>> 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

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Buenos Aires, Argentina
    Posts
    18
    Rep 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 :)
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Buenos Aires, Argentina
    Posts
    18
    Rep 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...
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Buenos Aires, Argentina
    Posts
    18
    Rep Power
    0
    It works! Thank you!!
    Last edited by luismg; May 27th, 2004 at 09:14 AM.
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Not a problem at all Luis. Glad you got it working for you .
    programming language development: www.netytan.com Hula

  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Buenos Aires, Argentina
    Posts
    18
    Rep 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 (?)
  24. #13
  25. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo