Tonight I discovered something odd in the __doc__ for tempfile
as shipped with Python 2.4.4 and 2.5: it says:
This module also provides some data items to the user:
TMP_MAX - maximum number of names that will be tried before
giving up.
template - the default prefix for all temporary names.
You may change this to control the default prefix.
... which would lead one to think that the following code would work:
>>import tempfile tempfile.temp late = 'mytest' tf = tempfile.NamedT emporaryFile() tf.name
'/tmp/mytest-XXXXXX'
It doesn't.
In fact I realized, after reading through tempfile.py in /usr/lib/...
that the following also doesn't "work" like I'd expect:
# foo.py
tst = "foo"
def getTst(arg):
return "foo-%s" % arg
# bar.py
import foo
foo.tst = "bar"
print foo.getTst("tes ting")
foo-testing <<<----- NOT "bar-testing"
Now I would feel like a real idiot if I'd come across that in the
foo/bar case here ... because I clearly don't understand quite *why*
I can't "monkey patch" this value. I would ... but I don't.
First, I wouldn't have written code like this foo/bar stuff; except
to test my hypothesis about why changes to tempfile.templa te don't
actually affect the values seen by functions in the tempfile namespace.
Secondly, the author(s) of the tempfile module apparently didn't
understand this either. And no one else even noticed that the __doc__
is wrong (or at least misleading -- since the only way I can see to
change tempfile.templa te is to edit the .py file!
So, I don't feel like an idiot. But I am curious ...
... why can't I change that value in that other namespace? Is it
a closure? (Or like a closure?) Where is this particular aspect
of the import/namespace semantics documented?
--
Jim Dennis,
Starshine: Signed, Sealed, Delivered 6 1387
In <1178693438.689 184@smirk>, James T. Dennis wrote:
Tonight I discovered something odd in the __doc__ for tempfile
as shipped with Python 2.4.4 and 2.5: it says:
This module also provides some data items to the user:
TMP_MAX - maximum number of names that will be tried before
giving up.
template - the default prefix for all temporary names.
You may change this to control the default prefix.
... which would lead one to think that the following code would work:
>>import tempfile
>>tempfile.temp late = 'mytest'
>>tf = tempfile.NamedT emporaryFile()
>>tf.name
'/tmp/mytest-XXXXXX'
It doesn't.
The source says:
__all__ = [
"NamedTemporary File", "TemporaryFile" , # high level safe interfaces
"mkstemp", "mkdtemp", # low level safe interfaces
"mktemp", # deprecated unsafe interface
"TMP_MAX", "gettempprefix" , # constants
"tempdir", "gettempdir "
]
Maybe the doc should be clearer in saying "constants" too.
Secondly, the author(s) of the tempfile module apparently didn't
understand this either. And no one else even noticed that the __doc__
is wrong (or at least misleading -- since the only way I can see to
change tempfile.templa te is to edit the .py file!
You can change it by simply assigning to the name:
In [15]: tempfile.templa te = 'spam'
In [16]: tempfile.templa te
Out[16]: 'spam'
If you want to change the outcome of the functions and objects then simply
give the prefix as argument.
In [21]: tempfile.mktemp (prefix='eggs')
Out[21]: '/tmp/eggsBqiqZD'
In [22]: a = tempfile.NamedT emporaryFile(pr efix='eric')
In [23]: a.name
Out[23]: '/tmp/ericHcns14'
... why can't I change that value in that other namespace? Is it
a closure? (Or like a closure?) Where is this particular aspect
of the import/namespace semantics documented?
You *can* change it, but it is not used by the code in that module.
Ciao,
Marc 'BlackJack' Rintsch
Dennis Lee Bieber <wl*****@ix.net com.comwrote:
On Wed, 09 May 2007 06:50:38 -0000, "James T. Dennis"
<ja******@idiom .comdeclaimed the following in comp.lang.pytho n:
> In fact I realized, after reading through tempfile.py in /usr/lib/... that the following also doesn't "work" like I'd expect:
No idea of the tempfile problem, but...
> # foo.py tst = "foo" def getTst(arg): return "foo-%s" % arg
This return is using a literal "foo-". Change it to
return "%s-%s" % (tst, arg)
Sorry that was a retyping bug in my posting ... not in
my sample code which was on another system.
and try again.
Try it yourself. As I said ... the value of tst in your
name space will be changed, but the value returned by functions
in the imported module will still use the old value!
--
Jim Dennis,
Starshine: Signed, Sealed, Delivered
Marc 'BlackJack' Rintsch <bj****@gmx.net wrote:
In <1178693438.689 184@smirk>, James T. Dennis wrote:
> Tonight I discovered something odd in the __doc__ for tempfile as shipped with Python 2.4.4 and 2.5: it says:
This module also provides some data items to the user:
TMP_MAX - maximum number of names that will be tried before giving up. template - the default prefix for all temporary names. You may change this to control the default prefix.
... which would lead one to think that the following code would work:
> >>import tempfile >>tempfile.temp late = 'mytest' >>tf = tempfile.NamedT emporaryFile() >>tf.name
'/tmp/mytest-XXXXXX'
It doesn't.
The source says:
__all__ = [
"NamedTemporary File", "TemporaryFile" , # high level safe interfaces
"mkstemp", "mkdtemp", # low level safe interfaces
"mktemp", # deprecated unsafe interface
"TMP_MAX", "gettempprefix" , # constants
"tempdir", "gettempdir "
]
Maybe the doc should be clearer in saying "constants" too.
> Secondly, the author(s) of the tempfile module apparently didn't understand this either. And no one else even noticed that the __doc__ is wrong (or at least misleading -- since the only way I can see to change tempfile.templa te is to edit the .py file!
You can change it by simply assigning to the name:
In [15]: tempfile.templa te = 'spam'
In [16]: tempfile.templa te
Out[16]: 'spam'
I know you can change it. But changing it in your namespace
doesn't change the results returned by the functions called
from the module.
If you want to change the outcome of the functions and objects then simply
give the prefix as argument.
I know how to provide the prefix arguments and that was
never the issue.
The issue was twofold:
The docs are wrong (or at least confusing/misleading)
I don't quite understand how this name/variable in
my namespace (__main__) is able to change the value
while the functions in the module still hold the old
value.
--
Jim Dennis,
Starshine: Signed, Sealed, Delivered
In <1178779520.887 569@smirk>, James T. Dennis wrote:
Marc 'BlackJack' Rintsch <bj****@gmx.net wrote:
>In <1178693438.689 184@smirk>, James T. Dennis wrote:
>You can change it by simply assigning to the name:
>In [15]: tempfile.templa te = 'spam'
>In [16]: tempfile.templa te Out[16]: 'spam'
I know you can change it. But changing it in your namespace
doesn't change the results returned by the functions called
from the module.
I'm not changing it in my namespace but in the namespace of the `tempfile`
module.
I don't quite understand how this name/variable in
my namespace (__main__) is able to change the value
while the functions in the module still hold the old
value.
Default arguments are evaluated *once* when the ``def`` is executed and
not at every function call.
Ciao,
Marc 'BlackJack' Rintsch
James T. Dennis <ja******@idiom .comscribis:
In fact I realized, after reading through tempfile.py in /usr/lib/...
that the following also doesn't "work" like I'd expect:
# foo.py
tst = "foo"
def getTst(arg):
If I change this line:
return "foo-%s" % arg
to:
return "%s-%s" % (tst, arg)
# bar.py
import foo
foo.tst = "bar"
print foo.getTst("tes ting")
foo-testing <<<----- NOT "bar-testing"
Then "python bar.py" prints "bar-testing".
0:tolot@jupiter :/tmpcat foo.py
tst = "foo"
def getTst(arg):
return "%s-%s" % (tst,arg)
0:tolot@jupiter :/tmpcat bar.py
import foo
foo.tst = "bar"
print foo.getTst("tes ting")
0:tolot@jupiter :/tmppython bar.py
bar-testing
And regarding the tempfile.templa te problem, this looks like a bug.
Because all functions in tempfile taking a prefix argument use "def
function(... , prefix=template , ...)", only the value of template at
import time matters.
AdiaÅ*, Marc
Marc Christiansen <us****@solar-empire.dewrote:
James T. Dennis <ja******@idiom .comscribis:
>In fact I realized, after reading through tempfile.py in /usr/lib/... that the following also doesn't "work" like I'd expect:
> # foo.py tst = "foo" def getTst(arg):
If I change this line:
> return "foo-%s" % arg
to:
return "%s-%s" % (tst, arg)
> # bar.py import foo foo.tst = "bar" print foo.getTst("tes ting")
> foo-testing <<<----- NOT "bar-testing"
Then "python bar.py" prints "bar-testing".
0:tolot@jupiter :/tmpcat foo.py
tst = "foo"
def getTst(arg):
return "%s-%s" % (tst,arg)
0:tolot@jupiter :/tmpcat bar.py
import foo
foo.tst = "bar"
print foo.getTst("tes ting")
0:tolot@jupiter :/tmppython bar.py
bar-testing
And regarding the tempfile.templa te problem, this looks like a bug.
Because all functions in tempfile taking a prefix argument use "def
function(... , prefix=template , ...)", only the value of template at
import time matters.
Adia?, Marc
I suppose my real sample code was def getTst(arg=tst) :
Oddly I've never come across that (the fact that defaulted arguments are
evaluated during function definition) in my own coding and I guess there
are two reasons for that: I try to avoid global variables and I usually
use defaulted variables of the form:
def (foo=None):
if foo is None:
foo = self.default_fo o
--
Jim Dennis,
Starshine: Signed, Sealed, Delivered This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Pierre Rouleau |
last post by:
Hi all!
I am using Python 2.3.1 on Win32 (NT, 2000).
Whenever a file imports the standard tempfile module, Python 2.3.1
issues the following warning:
C:\Python23\lib\fcntl.py:7: DeprecationWarning: the FCNTL module is
Deprecated; please use fcntl DeprecationWarning).
|
by: James S |
last post by:
Hi,
Basically I've been fighting with this code for a few days now and
can't seem to work around this problem. Included is the output, the
program I use to get this error and the source code for my wrapper.
This is acually part of the project, libxmlconf on sourceforge. The
newest working version isn't there yet, and cvs is lagged by 6 hours
or so. So if you think you want to have a try at this I can tgz the
source for you. My...
|
by: Matt Garman |
last post by:
I've been working on a curses-based application in Python. My
application effectively has a series of screens (or windows). When
one screen closes, the previous screen should be exactly redrawin in
its original state (i.e., before the sub-screen was created).
As far as I can tell, the putwin() and getwin() functions can easily
solve this problem. So I can do something like this:
# draw/write some stuff on stdscr......
|
by: Ron Adam |
last post by:
Does anyone have suggestions on how to improve this further?
Cheers,
Ron_Adam
def getobjs(object, dlist=, lvl=0, maxlevel=1):
""" Retrieve a list of sub objects from an object. """
|
by: Reid Priedhorsky |
last post by:
Dear group,
I'd have a class defined in one module, which descends from another class
defined in a different module. I'd like the superclass to be able to
access objects defined in the first module (given an instance of the first
class) without importing it. Example of what I'm looking for:
<<<file spam.py>>>
class Spam(object):
| |
by: emin.shopper |
last post by:
I had a need recently to check if my subclasses properly implemented
the desired interface and wished that I could use something like an
abstract base class in python. After reading up on metaclass magic, I
wrote the following module. It is mainly useful as a light weight tool
to help programmers catch mistakes at definition time (e.g., forgetting
to implement a method required by the given interface). This is handy
when unit tests or...
|
by: samir.vds |
last post by:
Hello everyone,
I'm trying to test the tempfile module with the following script,
which basically creates a temporary file, fills the file with some
test data and prints it.
import tempfile
t = tempfile.TemporaryFile()
t.write("lalalala")
|
by: byte8bits |
last post by:
Wondering if someone would help me to better understand tempfile. I
attempt to create a tempfile, write to it, read it, but it is not
behaving as I expect. Any tips?
<open file '<fdopen>', mode 'w+b' at 0xab364968>
0
0
0
|
by: Jeff Dyke |
last post by:
I've come across an error that i'm not yet able to create a test case
for but wanted to get see if someone could shed light on this.
I have imported a module at the top of my file with
import mymodulename
this module is used many times in the current file successfully, but
then I attempt to use it one more time and get: UnboundLocalError:
local variable 'mymodulename' referenced before assignment
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |