473,598 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inherit from file and create stdout instance?

5 New Member
How can I inherit from file but stil create an instance that writes to stdout?
-----------
I'm writing a file-like object that has verbosity options (among some other things). I know I could just set self.file to a file object rather than inheriting from file. I started with something like this (simplified for clarity):
Expand|Select|Wrap|Line Numbers
  1. class Output(object):
  2.     def __init__(self, file=sys.stdout, verbosity=1):
  3.         self.verbosity = verbosity
  4.         self.file = file
  5.  
  6.     def write(self, string, messageVerbosity=1):
  7.         if messageVerbosity <= self.verbosity:
  8.             self.file.write(string)
  9. ...
  10.  
  11. but it is frustrating me that if I try to inherit from file it works fine for regular files but I can't figure out a clean way to instantiate an object that writes to stdout using sys.stdout. something like the following:
  12.  
  13. class Output(file):
  14.     def __init__(self, name, mode='w', buffering=None, verbosity=1):
  15.         super(Output, self).__init__(name, mode, buffering)
  16.         self.verbosity = 1
  17.  
  18.     def write(self, string, messageVerbosity=1):
  19.         if messageVerbosity <= self.verbosity
  20.             super(Output, self).write(string)
...

I hope that's clear. Is it just a bad idea to inherit from file to create a class to write to stdout or am I missing something? Any insight is appreciated.

Thanks,
Pete
May 11 '07 #1
0 1703

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2590
by: Jody Burgess | last post by:
Hi; I am writing my first python program and would like to know how to change stdout to refer to my default printer or any other printer on my network. The other question is, Is there an API set of classes that allow me to interact with network devices. In other words, if I have a path and filename inside of a string variable in python, how do I send the file to the printer?? Thanks in advance
7
3969
by: Mike | last post by:
I would like my 'print' statements to send its output to the user's screen and a log file. This is my initial attempt: class StdoutLog(file): def __init__(self, stdout, name='/tmp/stdout.log', mode='w',bufsize=-1): super(StdoutLog, self).__init__(name,mode,bufsize) self.stdout = stdout
4
2088
by: Nader Emami | last post by:
L.S., I have used the profile module to measure some thing as the next command: profile.run('command', 'file') But this make a binary file! How can I write the result of 'profile' in a ascii file? Others how can I read (or convert) the binary file to am ascii file?
6
3488
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being written. /* Book name : File name : E:\programs\cpp\iti01\ch10\ex09_5p1.cpp Program discription: Adding name,Address to customer_record SETUP PROGRAM
13
11124
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it writes a specific string to the log file. My original program (MKS Toolkit shell program) which keeps running "grep" checking the "exit string" on the "log files". There are no file sharing problem.
5
8635
by: Cameron Laird | last post by:
Question: import subprocess, StringIO input = StringIO.StringIO("abcdefgh\nabc\n") # I don't know of a compact, evocative, and # cross-platform way to exhibit this behavior. # For now, depend on cat(1). p = subprocess.Popen(, stdout = subprocess.PIPE, stdin = response)
10
7620
by: SamG | last post by:
How could i make, from inside the program, to have the stdout and stderr to be printed both to a file as well the terminal(as usual).
7
384
by: MisterPete | last post by:
How can I inherit from file but stil create an instance that writes to stdout? ----------- I'm writing a file-like object that has verbosity options (among some other things). I know I could just set self.file to a file object rather than inheriting from file. I started with something like this (simplified for clarity): class Output(object): def __init__(self, file=sys.stdout, verbosity=1):
5
2603
by: =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post by:
I wanna print the log to both the screen and file, so I simulatered a 'tee' class Tee(file): def __init__(self, name, mode): file.__init__(self, name, mode) self.stdout = sys.stdout sys.stdout = self
7
1412
by: bukzor | last post by:
I was trying to change the behaviour of print (tee all output to a temp file) by inheriting from file and overwriting sys.stdout, but it looks like print uses C-level stuff to do its writes which bypasses the python object/inhertiance system. It looks like I need to use composition instead of inheritance, but thought this was strange enough to note. $python -V Python 2.5
0
8284
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...
0
8392
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8262
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
6711
agi2029
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...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
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();...
0
3894
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
3938
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1245
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.