473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reload after an exception, not possible ?

hello,

I've a graphical application (wxPython),
where the code in the main GUI loop is given below.
1 JAL_Loaded = False
2 while len(App_Running) 0:
3 if JALsPy_globals.State == SS_Run:
4 try:
5 if JAL_Loaded:
6 reload ( JAL_simulation_file )
7 else:
8 JAL_Loaded = True
9 import JAL_simulation_file
10 except JALsPy_globals.Reload_Exception:
11 JALsPy_globals.State = SS_Halt
The first time the while loop is entered,
JAL_Loaded is False (line 1),
and when I press some button,
the code will enter at line 8,
importing a module, that has executable code with an infinite loop.

To exit this infinite loop, an exception is generated (by a button press),
and program comes back in the above while-loop line (10,11,2).

So far so good.

Then I change the code in the imported file,
and when I start the engine again,
the flag JAL_Loaded is True, so I don't import,
but I reload the file.

Now I get the next exception
UnboundLocalError: local variable 'JAL_simulation_file' referenced before assignment

So obviously I can't reload but have to do an import again
(which makes the code much simpler ;-)
but I don't understand why the reload raises an exception ????

please enlighten me,
thanks,
Stef Mientki

Jul 31 '07 #1
3 1535
Stef Mientki wrote:
hello,

I've a graphical application (wxPython),
where the code in the main GUI loop is given below.
1 JAL_Loaded = False
2 while len(App_Running) 0:
3 if JALsPy_globals.State == SS_Run:
4 try:
5 if JAL_Loaded:
6 reload ( JAL_simulation_file )
7 else:
8 JAL_Loaded = True
9 import JAL_simulation_file
10 except JALsPy_globals.Reload_Exception:
11 JALsPy_globals.State = SS_Halt
The first time the while loop is entered,
JAL_Loaded is False (line 1),
and when I press some button,
the code will enter at line 8,
importing a module, that has executable code with an infinite loop.

To exit this infinite loop, an exception is generated (by a button press),
and program comes back in the above while-loop line (10,11,2).

So far so good.

Then I change the code in the imported file,
and when I start the engine again,
the flag JAL_Loaded is True, so I don't import,
but I reload the file.

Now I get the next exception
UnboundLocalError: local variable 'JAL_simulation_file' referenced before assignment

So obviously I can't reload but have to do an import again
(which makes the code much simpler ;-)
but I don't understand why the reload raises an exception ????
You have an assignment to JAL_simulation_file elsewhere inside the same
function, later in the code. This means that Python will assume that it
is local to the function, but the import statement will affect the
module namespace rather than the function namespace.

In other words, your code is roughly equivalent to what follows, with
the added complexity that the illegal reference isn't triggered until
the second iteration of a loop.
>>def f():
.... print os
.... import os
.... os = "Something"
....
>>f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
UnboundLocalError: local variable 'os' referenced before assignment
>>>
The solution? Don't assign to JAL_simulation_file, and the function will
use the local instead.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 1 '07 #2
Steve Holden wrote:
[...]
The solution? Don't assign to JAL_simulation_file, and the function will
use the local instead.
Sorry, that last "local" should have been "global".

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 1 '07 #3
Steve Holden wrote:
Stef Mientki wrote:
>hello,

I've a graphical application (wxPython),
where the code in the main GUI loop is given below.
1 JAL_Loaded = False
2 while len(App_Running) 0:
3 if JALsPy_globals.State == SS_Run:
4 try:
5 if JAL_Loaded:
6 reload ( JAL_simulation_file )
7 else:
8 JAL_Loaded = True
9 import JAL_simulation_file
10 except JALsPy_globals.Reload_Exception:
11 JALsPy_globals.State = SS_Halt
The first time the while loop is entered,
JAL_Loaded is False (line 1),
and when I press some button,
the code will enter at line 8,
importing a module, that has executable code with an infinite loop.

To exit this infinite loop, an exception is generated (by a button
press),
and program comes back in the above while-loop line (10,11,2).

So far so good.

Then I change the code in the imported file,
and when I start the engine again,
the flag JAL_Loaded is True, so I don't import,
but I reload the file.

Now I get the next exception
UnboundLocalError: local variable 'JAL_simulation_file' referenced
before assignment

So obviously I can't reload but have to do an import again
(which makes the code much simpler ;-)
but I don't understand why the reload raises an exception ????
You have an assignment to JAL_simulation_file elsewhere inside the same
function,
No these are the only 2 uses of "JAL_simulation_file" in the whole project.

later in the code. This means that Python will assume that it
is local to the function, but the import statement will affect the
module namespace rather than the function namespace.

In other words, your code is roughly equivalent to what follows, with
the added complexity that the illegal reference isn't triggered until
the second iteration of a loop.
>>def f():
... print os
... import os
... os = "Something"
...
>>f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
UnboundLocalError: local variable 'os' referenced before assignment
>>>

The solution? Don't assign to JAL_simulation_file, and the function will
use the local instead.
The solution is much simpler, but I don't understand why the "import",
which really reloads the changed code in JAL_simulation_file,
works the second time :-(

2 while len(App_Running) 0:
3 if JALsPy_globals.State == SS_Run:
4 try:
9 import JAL_simulation_file
10 except JALsPy_globals.Reload_Exception:
11 JALsPy_globals.State = SS_Halt

cheers,
Stef Mientki
Aug 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
by: Ellinghaus, Lance | last post by:
> > Other surprises: Deprecating reload() >Reload doesn't work the way most people think >it does: if you've got any references to the old module, >they stay around. They aren't replaced. ...
1
by: Emmanuel | last post by:
Hi, I use a 'reload all' feature in my app, that allow to reload every module. I first try this version : import sys def Reload():
1
by: Cooper | last post by:
Hello, is possible to disable the reload (or update, for ie) of the a web page? If yes, what i do? Otherwise if isn't possible, what i do for to set the default value of a form when a user do a...
0
by: fhiemstra0507 | last post by:
Hi I have a listbox that I want to update when the an item is updated/added. Now the listbox is attached to a query. Now I tried to just reload the query by modifying the forms loadDataSet and...
18
by: Alan Z. Scharf | last post by:
1. I have a chain of six asynch callbacks initiated by a button, and want the page to refresh at the end of each callback to display A. Results of a SQLServer query showing cumulative running...
2
by: gen_tricomi | last post by:
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) on win32 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal...
14
by: fdu.xiaojf | last post by:
Hi, I have a program which will continue to run for several days. When it is running, I can't do anything except waiting because it takes over most of the CUP time. Is it possible that the...
1
by: nkoriginal | last post by:
Hello Again: I've a problem with my forms. I woking with an intranet, and the pages in that intranet, has a reload page, every 10 minutos. So my problems star, when any user stay in my forms...
2
by: Max | last post by:
Is it possible to reload a web page on user browser when an event occurs at server side? For example when user A places an order, user B should be notified of that and should see that order on his...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.