473,563 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with wrapping GNU Units

Hi,
As part of a larger project, I am trying to use the GNU Units program
to provide unit conversions between quantities.

My first iteration, which worked OK, was to simply use units as a
command-line app, and capture its output. The larger program however,
calls the conversion routine many times, and the overhead of starting
units for every call made things too slow.

I then re-formulated my use of units to open it using popen2, and do
conversions with it through stdin and stdout (Windows XP). This too
works OK, at least up to the point I want to stop the program, which is
where I am having problems.

If I simply try to exit my app, python.exe hangs, and units.exe keeps
running, according to the Task Manager. If I try sending Control-D, as
the on-line help suggests, the program hangs too.

Does anyone have any ideas on how to control this app?

Here is some of my code:

# Functions for driving GNU Units (if installed)
GNU_Units_stdin = None
GNU_Units_stdou t = None

def SetupGNU_Units( ):
import time, os, sys
# Initialization routine to set up pipe to running GNU Units
process
# Start GNU Units as a child and feed and read through stdin and
stdout
global GNU_Units_stdin , GNU_Units_stdou t
CurrentDir = os.getcwd()
# Hack to find correct directory
if not 'units.exe' in os.listdir(os.g etcwd()):
UnitsDir = os.path.join(Cu rrentDir, 'GNU_Units')
else:
UnitsDir = CurrentDir
print "CurrentDir ", CurrentDir
os.chdir(UnitsD ir)
(GNU_Units_stdi n, GNU_Units_stdou t) = os.popen2('unit s.exe -f
..\units.dat')
time.sleep(0.5)
print 'First initialization read: ', GNU_Units_stdou t.readline()
time.sleep(0.5)
print 'Second initialization read: ', GNU_Units_stdou t.readline()
time.sleep(0.5)
print 'Units initialization complete.'

def ShutdownGNU_Uni ts():
global GNU_Units_stdin , GNU_Units_stdou t
# From the GNU Units help, to quit the program from the interactive
prompt
# (which is how we are using it), one needs to feed it Control-D
GNU_Units_stdin .write(chr(4))
# ResponseLine1 = GNU_Units_stdou t.readline()
GNU_Units_stdin = None
GNU_Units_stdou t = None

def ConvertUnits(Va lue, FromUnitStr, ToUnitStr):
import os, sys
global GNU_Units_stdin , GNU_Units_stdou t
# Sanity check on the numerical portion of the input
try:
dummy=float(Val ue)
except ValueError:
raise ConvertUnitsErr or('ConvertUnit s: non-numeric input')
return 0
if not GNU_Units_stdin :
# Initialize pipes to GNU Units
SetupGNU_Units( )
FromStr = str(Value)
FromStr += ' ' + FromUnitStr + '\r'
ToStr = ToUnitStr + '\r'
GNU_Units_stdin .write(FromStr)
GNU_Units_stdin .write(ToStr)
ResponseLine1 = GNU_Units_stdou t.readline()
ResponseLine2 = GNU_Units_stdou t.readline()
ReadBackLine = GNU_Units_stdou t.readline()
ReadBackLine2 = GNU_Units_stdou t.readline()
if 'conformability error' == ReadBackLine.st rip():
raise ConvertUnitsErr or('ConvertUnit s: conformability error')
AnswerElement = ReadBackLine.st rip()
return float(AnswerEle ment.split()[1])

Thanks in advance,
Duane

Apr 3 '06 #1
2 1558
TheSeeker wrote:
Hi,
As part of a larger project, I am trying to use the GNU Units program
to provide unit conversions between quantities.


Maybe Unum would be a useful alternate:
http://home.tiscali.be/be052320/Unum.html

Kent
Apr 3 '06 #2
Hi,

Perhaps in another revision, as utilizing Unum, while cool, would be
quite a change to my code.

Could it be that the readline library used by GNU Units can detect the
difference between a 'real' tty and a pipe? If so, how can I trick it?

Thanks,
Duane

Apr 4 '06 #3

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

Similar topics

2
2053
by: plopez | last post by:
Hi I'm a bit of a noob to xslt. I have a data.xml document like this.. input.xml <energy> ..lots and lots of other data..... <units>(Watts)<\units> <value>1000.0<\value> </energy>
10
2497
by: Lorenzo Gordon | last post by:
Hi all, I've trawled the archives in vain for a similar problem: In building a client's site, I've got a menu section where each menu heading sits in a table cell. It's _paramount_ that the text of the menu heading literally sits at the bottom of the cell, so that when you add the table border, the text looks underlined (this is not the...
3
1453
by: Doo-Dah Man | last post by:
I hope I can make this clear: I have an Access 2000 database that drives an ASP web site to track sales leads. There is a combo box , "units", that lists the inventory of models we sell. Here's a code snippet Fills the combo box:
2
3274
by: Christian Christmann | last post by:
Hi, I'm using "flex" which is creating a file called lex.yy.c and this C file is added to a library. In another directory I have to use flex for another file and also add the second lex.yy.c to another library. Hence, I need to include both libraries and while linking I get an e.g. "multiple definition of `yyleng'" since yyleng is created...
8
25046
by: John Baker | last post by:
Hi: Access 2000 W98! I have a table with numerous records in it, and am attempting to delete certain records that have been selected from it. These are selected based on the ID number in a different table. While I am using the tools in Access for query setup, its easier to show it on here using the SQL for the query, which is as follows(...
33
2326
by: Xah Lee | last post by:
The Harm of hard-wrapping Lines 20050222 Computing Folks of the industry: please spread the debunking of the truncating line business of the fucking unix-loving fuckheads, as outlines here: http://xahlee.org/UnixResource_dir/writ/truncate_line.html
0
972
by: Nathan Carroll | last post by:
In the small test class below I have roughed out a wrapping of the string using drawstring and measurestring. Note that without the to Math.Ceiling uses that the drawstring still produces ellipsis. Is there anything I'm missing here? I attempted to use a class from Duncan Mackenzie which seemed to be doing the same thing as below but...
11
1671
by: Gagan | last post by:
//Problem Statement // //You work for a company that sells mechanical bit counting devices. These devices //wear out when they do a lot of counting. You are going to see how much wear has //been put on one particular 15-bit binary counter. One unit of wear occurs for //every bit that is flipped in the counter. //
1
2313
kiemxai
by: kiemxai | last post by:
you see that,i did correct or not? PROBLEM ANALYSIS For an introduction to program transaction journal, see the background section of assignment . Not only transaction journal, but also inventory sheet is needed to print at the end of the day. It is the list of items, inventory cost, inventory units, and inventory amount When items of the same...
0
7664
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...
0
7583
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...
0
7885
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. ...
0
8106
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...
0
7948
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...
0
5213
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...
0
3642
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...
1
2082
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
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.