473,698 Members | 2,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with "with-statement-compatible" object

Hello!
I've made some class that can be used with "with statement". It looks this way:

class chdir_to_file( object ):
....
def __enter__(self) :
....

def __exit__(self, type, val, tb):
....
def get_chdir_to_fi le(file_path):
return chdir_to_file(f ile_path)
....

Snippet with object instantiation looks like this:
for s in sys.argv[1:]:
c = chdir_to_file( s )
with c:
print 'Current directory is %s' % os.path.realpat h( os.curdir )

That works fine. I want to enable it to be used in more elegant way:
for s in ... :
with get_chdir_to_fi le( s ) as c:
c.do_something( )

But python complains c is of NoneType and has no "do_something() ". Am
I missing something?
Dec 19 '07 #1
2 1396
Dmitry Teslenko wrote:
Hello!
I've made some class that can be used with "with statement". It looks this way:

class chdir_to_file( object ):
...
def __enter__(self) :
...

def __exit__(self, type, val, tb):
...
def get_chdir_to_fi le(file_path):
return chdir_to_file(f ile_path)
...

Snippet with object instantiation looks like this:
for s in sys.argv[1:]:
c = chdir_to_file( s )
with c:
print 'Current directory is %s' % os.path.realpat h( os.curdir )

That works fine. I want to enable it to be used in more elegant way:
for s in ... :
with get_chdir_to_fi le( s ) as c:
c.do_something( )

But python complains c is of NoneType and has no "do_something() ". Am
I missing something?
Does the chdir_to_file class have a do_something() method? If so,
changing chdir_to_file._ _enter__() to

def __enter__(self) :
# ...
return self

should help.

Peter
Dec 19 '07 #2
On Dec 19, 2007 12:14 PM, Peter Otten <__*******@web. dewrote:
def __enter__(self) :
# ...
return self

should help.
That helps. Thanks!
Dec 19 '07 #3

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

Similar topics

10
3075
by: Berthold Hoellmann | last post by:
Hello, When I use ./configure --with-thread --with-fpectl --with-signal-module \ --with-pymalloc --enable-shared --with-cxx=g++ make test on 2.3.3 I get
29
4393
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member reffers in all objects of this class to the same data Or in other word by using the static keyword all objects of one class can share data. (This is what I want)
0
2115
by: maxim75 | last post by:
Hi! I'm using DBMS_XMLSave package of XSU to insert in a specified column. How can I specify column if destination table is a view with object type? I have used DBMS_XMLSave.setUpdateColumn(insCtx,'nomeCol') but it seems not working if 'nomecol' is an attribute of an object type. ThanX-MaX
0
1067
by: W Hoover | last post by:
Hope someone can help with this. I have a form with about 400 objects on it - most of them fall into two types: radio buttons and text boxes. In the database, I have a table of values that, for each item id (1, 2, 3...) there is an associated list of field names (rbYes, rbNo, mskFirst, mskLast, mskQty). In my form code, I have a LoadHashTable routine: Dim Ctrl As Control
2
3763
by: ulrich schumacher | last post by:
Hi ASP.NET 2.0 Professionals, I've got a question concerning the new ASP.NET 2.0 DataBinding with object oriented business classes: Example: There are two business classes in my domain model: public class Language
13
3253
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else happend. I think the problem may be in my input function or in the main function. If anyone out there can help me it woul be greatly appreciated. Here is the code: #include <iostream>
10
9061
by: lovecreatesbea... | last post by:
Is it correct and safe to compare a string object with "", a pair of quotation marks quoted empty string?If the string object: s = ""; does s contain a single '\'? Is it better to use std::string::size or std::string::empty to deal with this condition? Thank you. string s = ""; if (s == ""); if (s.size == 0); if (s.empty());
4
29384
by: john | last post by:
Hi to All, I am new to html authoring, so sorry if my terminology is not correct or exact. I would like to position a footer div to the bottom of the browser window. As I research in the web shows this could be done a containing element with "min-height: 100%;" and an absolute positioned element (div) inside, aligned to bottom ("position: absolute; bottom:0px")
0
2190
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below). Traditionally I think of a classic structure as simply an object where every member is public. But with...
9
2916
by: Keith G Hicks | last post by:
I'm having a lot of trouble with "file in use" errors in my "folder watcher" project. Starting and stopping the watcher and reading my XML file work fine. Once the watcher is started, I'm reading the text files from the watched folder line by line into variables and then posting them to a SQL table. All of the code for the form is shown below. And it works fine except for 2 issues. First issue: In the Finally of the Try for teh SQL...
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
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,...
0
9170
Oralloy
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...
1
8902
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,...
0
8873
tracyyun
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...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
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...

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.