[python-uk] Python, VB math simple problem

Mr X durango at mail2world.com
Sat Apr 1 06:41:06 CEST 2006



Hi looking for help with what should be a fairly simple Python problem,
relating to VB inter-operability.
Got a great response from a fellow named Matt at help at python.org,
pointed me in some good directions - some areas, concerns still foggy
on, the below thread is included.... any feedback on this simple dilemma
would be very appreciated.

Thanks,

D

thread follows below;

------------------------------------

To: help at python.org 
Subject: Problem with Python math functions and VB 
Date: 3/30/2006 9:39:28 PM 
Download Message Display Headers Printer Friendly 
Previous | Next 

Wondering if you might either know how to solve the following.

I've a background in Visual Basic, and am using an old version, 4.0, it
compiles to a smaller executable which I prefer. I find myself in an odd
situation, needing a very simple yet powerful capability of Python for a
VB app
Im working on.

Simply, a large 300 digit number is divided by a smaller number ranging
from 1 to 3 digits. I.e;

This large 300 digit number is generated as a string from the VB app,
and I want to input this somehow
from the VB app directly to Python for simple math operations.

Where; x = 300 digit number, y = divisor ( say '37')


x / 37

I want to divide x by y but I want the remainder of this division to at
least 3 or 4 decimal places, so my Python script at the command line;

x %y /y. = z

So now I want to take the resultant, the full number plus its remainder,
and I want to round this number up
to the next highest number and divide it by the same constant;

z rounded up to next highest number (never the lowest)

so

z /y = z Long

Remove the 'L' at the end, round up the last digit of z = Z

Then;

Z %y /y. = a

Then I want the last five digits of z (not Z) and a truncated at the
end, so the last digit before
the decimal point and the four digits past the decimal point printed to
a text file.

I want to be able to open the text file with the VB app and use this
data as inputs.
==========

Ok, so here is my dilemma, I know VERY litle about Python and a fair bit
about VB.

Ideally, I'd love to be able to simply have some extremely small
executable that just accepts inputs
does the calculations above and then spits out the outputs. If it were
possible to write some
simple lines of math code in Python and then compile these scripts in
Python to a Windows
compatible executable,that would be fantastic.

If I could simply have my VB app, 'call' the name of the tiny Python
executable, and then the Python executable
just automatically looked for a named text file (created by the VB app)
and extracted the 300 digit number from this, then performed the calcs,
then spit this data out as a new text file name it created, which I
could then use the VB app to open and read from, THAT would be ideal.

However, I don't know if Python can compile scripts to an exe? If it can
how could I find out how to do this?

If it doesn't, how could I get VB to directly pass commands to the
Python command line and then automatically
extract the outputs? Shelling out from VB to Python would be tough to
the command line I think, since the Python command line uses the 'Edit /
Mark, Paste' approach to inserting, copy inputs, outputs and this would
be virtually untenable, as far as I can tell to automate in a VB shell
out routine.

So basically, how the heck can I access Pythons ability to perform
simple calculations on very large numbers, easily, from within VB 4.0 ?
There must be a way, it seems like such a simple think to do, especially
since the actual math operations are so simple, straight forward, and
would never change.....

Any ideas?



------
Matthew, thanks for your response. 

<-----Original Message-----> 
>From: Matthew Dixon Cowles
>Sent: 3/31/2006 9:41:18 AM
>To: durango at mail2world.com
>Cc: help at python.org
>Subject: Re: [Python-Help] Problem with Python math functions and VB

>I'm sure that there's a way to do that, but I'm not familiar with 
>Visual Basic and I don't know what inter-process communication 
>facilities it offers. 

Is there a person or group you might direct me to that has worked with
this 
inter-process communication between VB and Python?

>I don't think that Python is going to be able to do that for you out 
>of the box. Hundreds of digits of floating-point precision is a lot. 

Could you explain that a bit more, sorry Im not sure what you mean
by 'out of the box' ? If I run the Python command line screen in windows
and manually type out a very large number, say 180 digits; where 'X' =
very large number;

X %37 /37.

returns what Im after, value wise..... but of course I don't want to do
this manually each time
for every dividend.

>You might find that one of the Python modules that let you use an 
>extended-precision library would do what you want. GMPY is one: 

>http://gmpy.sourceforge.net/ 

Hey, thats interesting, wonder if these modules can be operated on with
VB.....

>> So now I want to take the resultant, the full number plus its 
>> remainder, and I want to round this number up 
>> to the next highest number and divide it by the same constant; 
>
>That's easy enough. 
>
>> I want to be able to open the text file with the VB app and use this 
>> data as inputs. 
>
>Python can write to a file without any trouble, so it that form of 
>inter-process communication suits you, you shouldn't have much 
>trouble. I assume that Visual Basic has an easy way to start a 
>program and supply it with arguments, so you could have your Python 
>program get its inputs from sys.argv. 

What is sys.argv ? Thats really good news. In fact, all I really need
for the moment is
a python executable that;

================
PYTHON ALGORITHM ABSTRACT

a) opens a text file
b) reads a string from the file, which is a very large number
c) performs simple arithmetic operations;

X %37 /37. = y (four digit remainder after decimal point)
X /37 = w (quotient as long, the resulting output is stored as a
variable, the 'L' suffix tagged on the end of this resultant then gets
removed.
then the last digit in the quotient resultant string is increased in
value by one, rounded upwards = 'Z'

then 

Z %37 /37. = a

then, y and a are printed to a text file with hard returns between them.
Thats it, thats all I need to do.
===================
>Alas, it's not going to be extremely small. There isn't a compiler 
>from Python to machine code. Py2exe will bundle a Python program, 
>with everything that it needs to run, into a single executable 
>archive. But the archive isn't small. Py2exe is at: 
>
>http://www.py2exe.org/ 

the most important thing is the functionality, I'll sacrifice size if I
have to. My guess is this should work based on your comments, because
perhaps all I really have to do is have VB dump out the value of the
Very large number, `180 to 300 digits or so', to a text file, which then
becomes the input data for the Python executable, and then if I simply
call the name of the Python executable from VB as an instance,
then the Python App runs, spits out the data as a new text file, then
the VB app goes and opens the new text file and reads in the values,
and voila! There it is. I'm pretty sure I can call the Python app from
VB....... the alternative to all this would be to try and feed Python
scripts
directly to Python from VB, which I have NO idea how to do or where to
begin.... and am guessing would be much more messy...

I haven't programmed in Python, how would the "PYTHON ALGORITHM
ABSTRACT" I describe above look like, code wise? 
Is this fairly easy for you to describe?

>It may be that Python isn't the best solution for you here. Are there 
>extended-precision libraries for Visual Basic? 

Alas, none that I know of that are reliable and not incredibly
expensive, been looking for years, plus Im hooped because I have to work
with VB 4.0 instead of 6 +, guh.... 

>Regards, 
>Matt 

Matt..... good name, why do I always seem to get along with Matts, you
people keep popping up in my life and its always a blast!

Best regards,

D 


<span id=m2wTl><p><font face="Arial, Helvetica, sans-serif" size="2" style="font-size:13.5px">_______________________________________________________________<BR>Get the FREE email that has everyone talking at <a href=http://www.mail2world.com target=new>http://www.mail2world.com</a><br>  <font color=#999999>Unlimited Email Storage &#150; POP3 &#150; Calendar &#150; SMS &#150; Translator &#150; Much More!</font></font></span>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-uk/attachments/20060331/9d6b513f/attachment.html 


More information about the python-uk mailing list