473,323 Members | 1,560 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,323 software developers and data experts.

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_stdout = 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_stdout
CurrentDir = os.getcwd()
# Hack to find correct directory
if not 'units.exe' in os.listdir(os.getcwd()):
UnitsDir = os.path.join(CurrentDir, 'GNU_Units')
else:
UnitsDir = CurrentDir
print "CurrentDir ", CurrentDir
os.chdir(UnitsDir)
(GNU_Units_stdin, GNU_Units_stdout) = os.popen2('units.exe -f
..\units.dat')
time.sleep(0.5)
print 'First initialization read: ', GNU_Units_stdout.readline()
time.sleep(0.5)
print 'Second initialization read: ', GNU_Units_stdout.readline()
time.sleep(0.5)
print 'Units initialization complete.'

def ShutdownGNU_Units():
global GNU_Units_stdin, GNU_Units_stdout
# 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_stdout.readline()
GNU_Units_stdin = None
GNU_Units_stdout = None

def ConvertUnits(Value, FromUnitStr, ToUnitStr):
import os, sys
global GNU_Units_stdin, GNU_Units_stdout
# Sanity check on the numerical portion of the input
try:
dummy=float(Value)
except ValueError:
raise ConvertUnitsError('ConvertUnits: 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_stdout.readline()
ResponseLine2 = GNU_Units_stdout.readline()
ReadBackLine = GNU_Units_stdout.readline()
ReadBackLine2 = GNU_Units_stdout.readline()
if 'conformability error' == ReadBackLine.strip():
raise ConvertUnitsError('ConvertUnits: conformability error')
AnswerElement = ReadBackLine.strip()
return float(AnswerElement.split()[1])

Thanks in advance,
Duane

Apr 3 '06 #1
2 1551
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
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
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...
3
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. ...
2
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...
8
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...
33
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:...
0
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...
11
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...
1
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.