Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 26th, 2004, 05:41 PM
luismg luismg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18 luismg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old May 26th, 2004, 07:29 PM
rebbit rebbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 84 rebbit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #3  
Old May 26th, 2004, 07:46 PM
luismg luismg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18 luismg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
  #4  
Old May 26th, 2004, 10:13 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #5  
Old May 27th, 2004, 07:14 AM
luismg luismg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18 luismg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 :)

Reply With Quote
  #6  
Old May 27th, 2004, 07:48 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #7  
Old May 27th, 2004, 07:50 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #8  
Old May 27th, 2004, 08:02 AM
luismg luismg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18 luismg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
  #9  
Old May 27th, 2004, 08:06 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #10  
Old May 27th, 2004, 09:08 AM
luismg luismg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18 luismg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
It works! Thank you!!

Last edited by luismg : May 27th, 2004 at 09:14 AM.

Reply With Quote
  #11  
Old May 27th, 2004, 09:16 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Not a problem at all Luis. Glad you got it working for you .

Reply With Quote
  #12  
Old May 27th, 2004, 12:04 PM
luismg luismg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18 luismg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 (?)

Reply With Quote
  #13  
Old May 28th, 2004, 01:57 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > psyco problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap