From: Joern E. <Joe...@mi...> - 2001-07-24 14:40:11
|
> [Robert] > > Hello Joern, > > I only had two seconds free, but appended some ideas in case they > help. Sorry for any gaps due to my rush. >>>> [Joern] >>>> >>>> While working in a DOS-Box import works really fine. But writing >>>> a script which imports the same pakages throws an exception. >>> [finn] >>> >>> It sounds like a embedding problem. Try to check the value of sys.path >>> at the beginning of your script. >>> >>> If you are doing the embedding take a look at this FAQ answer for how >>> to set up a usefull python.path. >>> >>> http://www.jython.org/cgi-bin/faqw.py?req=show&file=faq06.002.htp >>> >>> Otherwise you can always add entries to sys.path from within your >>> script. >> [Joern] >> >> Hi again, >> >> thanks, finn, for the hint, but ... it doesn't work yet. I've read the site >> you've quoted above and the according site at jython.com. I've tried to >> find the registry file, but wasn't able to do so. Actually it isn't >> neccessary at all, right? I'm working on WinNT and a name .jython for a >> file isn't allowed (Message: You must type a filename). > [Robert] > > Not to worry- Jython doesn't mind (on win98 at least): > > import java, os > filename = os.path.join(java.lang.System.getProperty("user.home"), > ".jython") > open(filename, "w") [Joern] May it was mistakable. WinNT does not allow a filename .jython, because the file*name* is missing. You can create filenames without an extension but filenames without file*names* aren't possible, therefore ".jython" isn't a valid filename for WinNT. WinNT separates name and extension with a dot. But whom I'm telling this? :-) (Or may I *totally* misunderstood you.) >> [Joern] >> >> So I suggest it's >> the file called registry at c:\program files\jython as stated in the docs >> of jython (It took half a day to find the hint for that. It's placed >> _very_ unfavourable). > [Robert] > > Yes, it is the "registry" file in Jython's installation directory or > .jython in your home directory. I added a FAQ entry in case that makes > this information easier to locate. If you get an opportunity to check its > accuracy, and more importantly, its helpfulness, your feedback would be > appreciated. >> [Joern] >> >> Even after adding an own line specifying the path >> matching my system it doesn't work. This is what I've added to >> c:\program files\jython\registry: >> >> # this is how to specify the path at my computer. Joern >> python.path=c:\\program files\\jython;c:\\program files\\jython\\lib >> >> >> After that and adding the code to set the python.path within the script, >> too, I've started the AdventNet SimulationToolkit again. This message was >> written to the log-file of the SimulationToolkit: >> >> Traceback (innermost last): >> (no code object) at line 0 >> File "D:\AdventNet\simulator\mibs\_behavior_scripts_\script.py", line 1 >> Properties props = new Properties(); >> ^ >> SyntaxError: invalid syntax >> Exception while executing the Python Script >> >> >> But ... erm ... the registry-file is updated, so why can't I remove the >> code specifying the path in the script? Well, after removing the code the >> path could not be found, therefore string could not be imported. >> >> >> Environmet is set: >> PYTHONPATH=c:\program files\jython;c:\program files\jython\lib >> >> .. Path within script is set: >> Properties props = new Properties(); >> props.setProperty("python.path", "c:\Program Files\Jython;c:\Program >> Files\Jython\lib"); >> PythonInterpreter.initialize(System.getProperties(), props, new String[] >> {""}); >> >> .. and line to file registry has been added: >> # this is how to specify the path at my computer. Joern >> python.path=c:\\program files\\jython;c:\\program files\\jython\\lib >> >> >> So, what to do next, IOW: What am I doing wrong? It seems to me that the >> Properties could not be found, i.e. jython could not resolve the line >> Properties props = new Properties() - but how? > [Robert] > > I'm unsure of the problem, but thought it was worth checking the > python.home property. The reason this comes up is because your path > includes the "Lib" directory, but if python.home is set correctly this > "Lib" directory should automatically be in the path. It doesn't hurt > anything, it just made me wonder if python.home is set. > > 1. is python.home set so that it actually finds and reads the registry? [Joern] I don't know. How do I find out? The whole Jython-related code is "hidden" within a jar-file. No chance to dip into it. > [Robert] > > 2. should the property value be "c:\\Program Files\\Jython;c:\\Program > Files\\jython\\lib" # My frustration of escape char=path sep means > I've never actually tried single back-slashes to know. > > 3. does this application that jython is embedded in have special class > loaders? If so, do you need to add packages/classes with > sys.add_package(packagename) or sys.add_classdir(directory) or > sys.add_extdir(directoryOfJarsAndZips)? [Joern] As said under 1. - don't know. > 4. If nothing helps, can we see the code? Your snippets and > descriptions are clear, but I always waste too much time guessing about > what I'm not seeing. Seeing the offending class, plus the command-line you > use to run this class, would be easier. [Joern] Of course. See the end of this e-mil - code as far as you can look. > [Robert] > > Keep us posted on the progress. If it's a lack-of-progress, don't worry- > just post the class, and more ideas will come. [Joern] The problem is much to straightforward, so I don't work with classes. It's only a very small and simple jython-script - nothing complex or difficult. > Cheers, > Robert [Joern] Hi all. Thanks a lot, Robert. The import doesn't work at all yet, but my script (to be more exact: one of my scripts) does what it should do. This is the idea: I'm simulating agents using the AdventNet AgentSimulator v1. The AgentSimulator now supports agents' behavior. You can read and write a nodes' value by using jython-scripts. Here my example for a counter: # ---------- start of working script # Get the value of the scalar node (nodes' OID shortened) value = scriptinterface.getNodeValue(".1.3.6.1...") # Convert value to INT by "misusing" eval() valueAsInt=eval(value) # Add 1 to value valueAsInt=valueAsInt+1 # Set limit for value to 256. If reached reset value to 1. if valueAsInt > 256: valueAsInt=1 # Convert valueAsInt back to string value=str(valueAsInt) # Write (increased) value to node (nodes' OID shortened) scriptinterface.updateValue(".1.3.6.1...",value,"CONST") # ---------- end of working script My first idea was to use the function string.atoi() to convert the value from string to int. After browsing through the reference and tutorial I've found eval() and thought "Hey, why do I have to use eval for calculation?". So I've tried to "eval" the string '1' and ... fortunately (and surprisingly) it works. This is the history of the (working) script as shown above. But I've the foreboding that I have to use import, too, to implement more difficult behavior and therefore I'm trying to figure out how it works *with* imports. Here we are: # ---------- start of noneworking script from org.python.util import PythonInterpreter from java.util import Properties from java.lang import System props = Properties() props.setProperty("python.path", "c:\Program Files\Jython;c:\Program Files\Jython\lib") PythonInterpreter.initialize(System.getProperties(), props, "") # Import support for strings import string # Get the value of the scalar node value = scriptinterface.getNodeValue(".1.3.6.1.4.1.781.5.8.1.1.1") # Convert value to INT valueAsInt=string.atoi(value) # Add 1 to value valueAsInt=valueAsInt+1 # Set limit for value to 256. If reached reset value to 1. if valueAsInt > 256: valueAsInt=1 # Convert valueAsInt back to string value=str(valueAsInt) # Write back (increased) value to node scriptinterface.updateValue(".1.3.6.1.4.1.781.5.8.1.1.1",value,"CONST") # ---------- end of noneworking script The scripts were executed by the AgentSimulator at runtime whereat the condition the script will be started can be chosen freely (requestBased, timerBased, thresholdBased). How, why and were the script will be started can't be seen at all - the whole jython-stuff is "hidden" in a jar. The AgentSimulators' logfile contains this after executing the (noneworking) script: Traceback (innermost last): File "D:\AdventNet\simulator\mibs\_behavior_scripts_\counter.py", line 7, in ? AttributeError: class 'org.python.util.PythonInterpreter' has no attribute 'initialize' Exception while executing the Python Script I'm wondering how this can happen, because using System.getProperties() is absolutely similar to using PythonInterpreter.initialize() or am I wrong somehow? And 'initialize' isn't an attribute but a method. From my point of view this should work fine, but as you can see it doesn't. It looks like the path to the libs can't be found as before. I've set the correct path within the file 'registry'. Using a DOS-Box behaves the same way: (Inside the DOS-Box, again.) C:\>jython Jython 2.0 on java1.3.0 (JIT: null) Type "copyrigth", ... etc. >>> from org.python.util import PythonInterpreter >>> PythonInterpreter.initialize("old", "new", "") Traceback (innermost last): File "<console>", line 1, in ? AttributeError: class 'org.python.util.PythonInterpreter' has no attribute 'initialize' >>> dir(PythonInterpreter) ['err', 'eval', 'exec', 'execfile', 'get', 'getLocale', 'inizialize', 'locals', 'out', 'set', 'setErr', 'setLocals', 'setOut'] >>> dir(PythonInterpreter.initialize.__doc__) [] >>> {CTRL}+{C} C:\>_ May jython has to be started with the -D option? ... *truncateMUCH* ... Ok, gimme a minute to figure out ... (Starting DOS-Box.) C:\>jython Jython 2.0 on java1.3.0 (JIT: null) Type "copyrigth", ... etc. >>> from org.python.util import PythonInterpreter >>> inter = PythonInterpreter >>> inter.initialize("old", "new", "") Traceback (innermost last): File "<console>", line 1, in ? TypeError: initialize(): 1st arg can't be coerced to java.util.Properties >>> _ ... this! Erm, bingo?! Let me have a looong and close look to that and await my report on this topic soon. And last but not least a hopingly simple question. Why are so much people using two single quotes instead of a quotation mark (like ''this'') or different quotes (like `this') instead of two identical ones to distinguish some kind of text (e.g. strings)? I'm wondering every time I see it. TIA "Terrifying newbie using python/jython" Joern. :-) |
From: dman <ds...@ri...> - 2001-07-24 15:55:51
|
On Tue, Jul 24, 2001 at 04:37:56PM +0200, Joern Eckhoff wrote: | May it was mistakable. WinNT does not allow a filename .jython, because the | file*name* is missing. You can create filenames without an extension but | filenames without file*names* aren't possible, therefore ".jython" isn't a | valid | filename for WinNT. WinNT separates name and extension with a dot. But whom | I'm telling this? :-) | (Or may I *totally* misunderstood you.) Actually, you can create "dotfiles" (files where the first character of the name is a dot) in WinNT and Win2k. I have lots of them on my machine at work. The problem must be your method of creating it. I use cygwin (g)vim heavily. They have no problems creating, editing, reading dotfiles at all. Maybe you could try using a little python to create the .jython file then use your favorite editor to edit it? FYI, -D |
From: Robert W. B. <rb...@di...> - 2001-07-24 17:32:32
|
Hello Joern, On Tue, 24 Jul 2001, Joern Eckhoff wrote: > > [Robert] > > Hello Joern, > > > > I only had two seconds free, but appended some ideas in case they > > help. Sorry for any gaps due to my rush. > > >>>> [Joern] > >>>> > >>>> While working in a DOS-Box import works really fine. But writing > >>>> a script which imports the same pakages throws an exception. > > >>> [finn] > >>> > >>> It sounds like a embedding problem. Try to check the value of sys.path > >>> at the beginning of your script. > >>> > >>> If you are doing the embedding take a look at this FAQ answer for how > >>> to set up a usefull python.path. > >>> > >>> http://www.jython.org/cgi-bin/faqw.py?req=show&file=faq06.002.htp > >>> > >>> Otherwise you can always add entries to sys.path from within your > >>> script. > > [Robert] > > 1. is python.home set so that it actually finds and reads the registry? > > [Joern] > > I don't know. How do I find out? The whole Jython-related code is "hidden" > within a jar-file. No chance to dip into it. >>> import java >>> java.lang.System.getProperty("python.home") Also add... >>> import sys >>> print sys.path, sys.prefix Knowing these three values is the best first step Is fpformat is the import error you originally saw? if so, it may be that you need to copy that module from the CPython distribution into your Jython Lib directory. <snip> > # ---------- start of noneworking script > from org.python.util import PythonInterpreter > from java.util import Properties > from java.lang import System re-initializing is probably the wrong way to go here. If you are running something in Jython and need to import modules from a specific directory, it is better to use: >>> import sys >>> sys.path.append("\\the\\required\\directory") instead of: > props = Properties() > props.setProperty("python.path", "c:\Program Files\Jython;c:\Program > Files\Jython\lib") > PythonInterpreter.initialize(System.getProperties(), props, "") Also make sure that the module does in fact live in one of the directories in sys.path. Side note: The 3rd arg to initialize should be [""] - it needs to convert to a String[] rather than just a String. However, the error message indicates other problems... > # Import support for strings > import string > > # Get the value of the scalar node > value = scriptinterface.getNodeValue(".1.3.6.1.4.1.781.5.8.1.1.1") > > # Convert value to INT > valueAsInt=string.atoi(value) > > # Add 1 to value > valueAsInt=valueAsInt+1 > > # Set limit for value to 256. If reached reset value to 1. > if valueAsInt > 256: > valueAsInt=1 > > # Convert valueAsInt back to string > value=str(valueAsInt) > > # Write back (increased) value to node > scriptinterface.updateValue(".1.3.6.1.4.1.781.5.8.1.1.1",value,"CONST") > # ---------- end of noneworking script This script could be written as: value = int( scriptinterface.getNodeValue(".1.3.6.1.4.1.781.5.8.1.1.1") ) value = value > 255 and "1" or str(value + 1) scriptinterface.updateValue(".1.3.6.1.4.1.781.5.8.1.1.1",value,"CONST") read the second line as: if value + 1 > 256, return "1" for the expression, else return value + 1 as a string. > The scripts were executed by the AgentSimulator at runtime whereat the > condition the script will be started can be chosen freely (requestBased, > timerBased, thresholdBased). How, why and were the script will be started > can't be seen at all - the whole jython-stuff is "hidden" in a jar. The > AgentSimulators' logfile contains this after executing the (noneworking) > script: > > Traceback (innermost last): > File "D:\AdventNet\simulator\mibs\_behavior_scripts_\counter.py", line 7, > in ? > AttributeError: class 'org.python.util.PythonInterpreter' has no attribute > 'initialize' > Exception while executing the Python Script My guess is that the error message above indicates initialize has been modified/renamed. No matter, I don't think it is what you are after anyway. > I'm wondering how this can happen, because using System.getProperties() is > absolutely similar to using PythonInterpreter.initialize() or am I wrong > somehow? Initialize is all about establishing system state information from system properties, registry properties and the new properties specified in the 2nd arg. So it is not really like getProperties. If jython is already running, however, intialize is trying to re-initialize things, which isn't really effective. > And 'initialize' isn't an attribute but a method. In the Python world, anything in a class is an attribute. The error message you see is a Jython error- not a Java error, so it is the correct error message to report only if "initialize" truly does not exist in your PythonInterpreter class. Check dir(PythonInterpreter) a few times to see if "initialize" is really there, or if it has been modified. Note that this is only to explain the error, I still don't think you want initialize for your situation :) > From my point of > view this should work fine, but as you can see it doesn't. It looks like > the path to the libs can't be found as before. I've set the correct path > within the file 'registry'. Using a DOS-Box behaves the same way: > > (Inside the DOS-Box, again.) > C:\>jython > Jython 2.0 on java1.3.0 (JIT: null) > Type "copyrigth", ... etc. > >>> from org.python.util import PythonInterpreter > >>> PythonInterpreter.initialize("old", "new", "") > Traceback (innermost last): > File "<console>", line 1, in ? > AttributeError: class 'org.python.util.PythonInterpreter' has no attribute > 'initialize' > >>> dir(PythonInterpreter) > ['err', 'eval', 'exec', 'execfile', 'get', 'getLocale', 'inizialize', > 'locals', 'out', 'set', 'setErr', 'setLocals', 'setOut'] Is 'inizialize' a typo in the mail, or does the dir really and truly report 'inizialize'. If it really and truly says 'inizialize', your results make more sense. <snip> > May jython has to be started with the -D option? "has to"? - no. You may use the -D option to start java applications that embed Jython. This allows the setting of properties from the command-line. Look at the jython.bat file for example. If your embedded Jython application that is giving you trouble implements initialize like org.python.util.jython does, you may be able to force certain properties with -D. For example: java -cp \path\to\jython.jar -Dpython.hom="c:\my\Jython\home\dir" \ -Dpython.path="c:\some\dir" org.python.util.jython This starts Jython with a specific python.home and python.path. Other Java apps that embed Jython and use the PythonInterpreter.initialize(System.getProperties(), props, new String[0]) should also make use of the -D options (unless they specifically override these options in the props values internally). My thought was that the application you are working with could merely be started with a -Dpython.path="\\some\\path" to add the path you desired to sys.path. But I misunderstood the situation. > .. *truncateMUCH* ... Ok, gimme a minute to figure out ... > > (Starting DOS-Box.) > C:\>jython > Jython 2.0 on java1.3.0 (JIT: null) > Type "copyrigth", ... etc. > >>> from org.python.util import PythonInterpreter > >>> inter = PythonInterpreter > >>> inter.initialize("old", "new", "") > Traceback (innermost last): > File "<console>", line 1, in ? > TypeError: initialize(): 1st arg can't be coerced to java.util.Properties > >>> _ > > .. this! Erm, bingo?! Let me have a looong and close look to that and > await my report on this topic soon. There's nothing wrong with using an interpreter object from within Jython, but the initialize is suspect, and again, probably not what you really wanted. > And last but not least a hopingly simple question. Why are so much people > using two single quotes instead of a quotation mark (like ''this'') or > different quotes (like `this') instead of two identical ones to distinguish > some kind of text (e.g. strings)? I'm wondering every time I see it. I'm not sure why others do. I use ` and ' because they look nice like smart quotes, and use ''this'' because of habitual avoidance of embedded double-quotes in Java strings. Cheers, Robert |