Creating a Simple User Interface for a Function

C

CTSB01

I have the following code that runs perfectly:

def psi_j(x, j):
rtn = []
for n2 in range(0, len(x) * j - 2):
n = n2 / j
r = n2 - n * j
rtn.append(j * x[n] + r * (x[n + 1] - x[n]))
print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn
return rtn

This code takes a string x = [0,1,1,1,2] for example (it must always begin with 0) and a parameter j, say 2, and outputs a string (x = [0, 1, 2, 2, 2, 2, 2, 3] in this example).

It does this in two steps: First it decomposes some number m into a multiple of j and a remainder. Then it runs this decomposition through a functionon the rtn.append line.

Notice that this has cj - 1 terms where c is the number of terms in the input string and j is the parameter. Normally, we would like it to be able tocalculate cj terms. This is an issue with the function that I am more than happy to put aside for the moment.

My key interest is to be able to make this program usable for someone who has no knowledge of programming. In particular, I need some kind of user interface that prompts the user to input a string (ideally just by putting innumbers in the form 011123334 for example) and a parameter, and then displays the output sequence. This is essentially what the program already doesbut the idea is to make it usable for even the most technologically disinclined. Ideally it would do this without needing to run Python at all. If anyone is able to make this happen in Python I would be eternally grateful.
 
D

Dave Angel

I have the following code that runs perfectly:

def psi_j(x, j):
rtn = []
for n2 in range(0, len(x) * j - 2):
n = n2 / j
r = n2 - n * j
rtn.append(j * x[n] + r * (x[n + 1] - x[n]))
print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn
return rtn

No it doesn't run perfectly. It'll get a syntax error on the print
function call. That's assuming you're still using Python 3.3. You
really need to start by specifying your environment, without making us
look back through previous threads from you.

This code takes a string x = [0,1,1,1,2] for example

That's not a string. A string would be like
xx = psi_j("0abcd1234")

Perhaps you mean list? And is it a list of integers, or of arbitrary
numbers? Are there any constraints on the sizes or signs of those numbers?
(it must always begin with 0) and a parameter j, say 2, and outputs a string (x = [0, 1, 2, 2, 2, 2, 2, 3] in this example).

It does this in two steps: First it decomposes some number m into a multiple of j and a remainder.

Only if you replace the / with //. Or just use the function divmod():
n, r = divmod(n2, m)

Then it runs this decomposition through a function on the rtn.append line.

Notice that this has cj - 1 terms where c is the number of terms in the input string and j is the parameter. Normally, we would like it to be able to calculate cj terms.
This is an issue with the function that I am more than happy to put aside for the moment.

My key interest is to be able to make this program

So far you have a function, not a program. If you put it in a text file
and run it from python, it'll do nothing but display a syntax error
message. And when you fix that, it'll just run without doing anything.

usable for someone who has no knowledge of programming. In
particular, I need some kind of user interface that prompts
the user to input a string (ideally just by putting in numbers in the form 011123334 for example) and a parameter,
and then displays the output sequence. This is essentially what the program already does but the idea is to make it usable
for even the most technologically disinclined. Ideally it would do this without needing to run Python at all.

Then why are you asking on the Python forum? Or perhaps you mean
without him knowing he's running Python? In that case, use a shebang
line at the beginning, which will tell Linux to automatically invoke the
specified program (or programming language in this case).
If anyone is able to make this happen in Python I would be eternally grateful.

If we assume you're running Python 3.3 on Linux, and the user is willing
to us the terminal, then how about parsing the string from the command
line he types? You can access it as011123334 a string from sys.argv,
and convert it to separate numbers. Of course as it stands now, you
cannot tell whether the user wanted
0,1,1,1,2,3,3,3,4
or
0, 111, 23, 3, 3, 4

or something else.
 
C

CTSB01

I have the following code that runs perfectly:

def psi_j(x, j):
for n2 in range(0, len(x) * j - 2):
n = n2 / j
r = n2 - n * j
rtn.append(j * x[n] + r * (x[n + 1] - x[n]))
print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn
return rtn

No it doesn't run perfectly. It'll get a syntax error on the print

function call. That's assuming you're still using Python 3.3. You

really need to start by specifying your environment, without making us

look back through previous threads from you.
This code takes a string x = [0,1,1,1,2] for example

That's not a string. A string would be like

xx = psi_j("0abcd1234")

Perhaps you mean list? And is it a list of integers, or of arbitrary

numbers? Are there any constraints on the sizes or signs of those numbers?
(it must always begin with 0) and a parameter j, say 2, and outputs a string (x = [0, 1, 2, 2, 2, 2, 2, 3] in this example).
It does this in two steps: First it decomposes some number m into a multiple of j and a remainder.

Only if you replace the / with //. Or just use the function divmod():

n, r = divmod(n2, m)
Then it runs this decomposition through a function on the rtn.append line.
Notice that this has cj - 1 terms where c is the number of terms in theinput string and j is the parameter. Normally, we would like it to be able to calculate cj terms.
This is an issue with the function that I am more than happy to put aside for the moment.
My key interest is to be able to make this program

So far you have a function, not a program. If you put it in a text file

and run it from python, it'll do nothing but display a syntax error

message. And when you fix that, it'll just run without doing anything.

usable for someone who has no knowledge of programming. In

particular, I need some kind of user interface that prompts
the user to input a string (ideally just by putting in numbers in the form 011123334 for example) and a parameter,
and then displays the output sequence. This is essentially what the program already does but the idea is to make it usable
for even the most technologically disinclined. Ideally it would do this without needing to run Python at all.

Then why are you asking on the Python forum? Or perhaps you mean

without him knowing he's running Python? In that case, use a shebang

line at the beginning, which will tell Linux to automatically invoke the

specified program (or programming language in this case).
If anyone is able to make this happen in Python I would be eternally grateful.

If we assume you're running Python 3.3 on Linux, and the user is willing

to us the terminal, then how about parsing the string from the command

line he types? You can access it as011123334 a string from sys.argv,

and convert it to separate numbers. Of course as it stands now, you

cannot tell whether the user wanted

0,1,1,1,2,3,3,3,4

or

0, 111, 23, 3, 3, 4

or something else.

DaveA

Sorry Dave, to answer each part of your response:

1) I decided to use Python 2.7, and I will be sure to specify this in all future threads.
2) It is a list of positive integers. In fact, it is always going to be a list of positive increasing integers.
3) You're right. What I meant was that if after running that bit of code Ienter
I will get output that matches my requirements.
4) Yes, sorry that's what I meant (if I understood correctly). I was told elsewhere that I might want to try using tkinter. Essentially I'm trying to create a user interface that allows the user to just type in a string 01112345 for example, and choose a parameter (say j=2) and then click a button to run the function. I'd like to be able to run send a .exe file that the user can just open up and use with no further setup.

So on top of the user interface I would also it looks like need to determine how to make Python change a string 01112345 into a list so that it does that automatically when the user clicks 'run'.

Would a shebang still be the right way to go?

Thanks again Dave, apologies for the ambiguity.
 
T

Terry Reedy

1) I decided to use Python 2.7, and I will be sure to specify this in
all future threads.

Given that you are not using any libraries, let alone one that does not
run on Python 3, I strongly recommend using the latest version (3.3).
2) It is a list of positive integers. In fact, it is always going to
be a list of positive increasing integers.

Your example below starts with 0, which is not positive.
Perhaps you mean that all integers after a single leading 0 have to be
positive and increasing.

If you run digits together, then the max int is 9. Do you intend this?
4) Yes, sorry that's what I meant (if I understood correctly). I was
told elsewhere that I might want to try using tkinter.

If users start the program at a command line, the core of an input
function would be
input = (raw)input('Enter digits: ') # Include "raw" on 2.x
You would need a more elaborate prompt printed first, and input checking
with the request repeated if the input does not pass the check.

It would be pretty simple to do the equivalent with a tkinter dialog box.
I'd like to be
able to run send a .exe file that the user can just open up and use
with no further setup.

There are programs that will package your code with an interpreter. But
do give people the option to get just the program without installing a
duplicate interpreter.
So on top of the user interface I would also it looks like need to
determine how to make Python change a string 01112345 into a list so
that it does that automatically when the user clicks 'run'.
list('01112345') ['0', '1', '1', '1', '2', '3', '4', '5']
'0,1,1,1,2,3,4,5'.split(',')
['0', '1', '1', '1', '2', '3', '4', '5']
Would a shebang still be the right way to go?

On Linux, definitely, whether you have user enter on the command line or
in response to a prompt. On windows, it only helps with 3.3+.
 
D

Dave Angel

On 07/25/2013 04:58 PM, CTSB01 wrote:

Sorry Dave, to answer each part of your response:

1) I decided to use Python 2.7, and I will be sure to specify this in all future threads.
2) It is a list of positive integers. In fact, it is always going to be a list of positive increasing integers.
3) You're right. What I meant was that if after running that bit of code I enter
x = [0,1,2,3,4,5]
psi_j(x,2)
I will get output that matches my requirements.
4) Yes, sorry that's what I meant (if I understood correctly). I was told elsewhere that I might want to try using tkinter. Essentially I'm trying to create a user interface that allows the user to just type in a string 01112345 for example, and choose a parameter (say j=2) and then click a button to run the function. I'd like to be able to run send a .exe file

But Linux doesn't understand exe files.

Once again, you need to tell us your environment. I'm now supposing you
mean your user will be using Windows. I have no experience with making
a single runnable .exe file for Windows. But many others here do, and
can presumably help you.

that the user can just open up and use with no further setup.

So on top of the user interface I would also it looks like need to determine how to make Python change a string 01112345 into a list so that it does that automatically when the user clicks 'run'.

Since you give no upper limits to the numbers the user might be
specifying (other than saying their strictly positively monatonic), the
string "01112345" could be interpreted only as:


0, 1, 11, 23, 45
0, 11, 12, 345
0, 11, 12345
0, 111, 2345

(If the individual numbers were limited to the range 0-9, then we'd come
up with the sequence 0,1,1,1,2,3,4,5, but that's not increasing, so it
wouldn't be legal.)

Is there a reason you don't want the user to have to separate the
numbers, either with spaces or commas? If they typed "0 1 11 23 45"
you could trivially parse that with something like:
[int(x) for x in data.split()]

Would a shebang still be the right way to go?

A shebang is a Linux/Unix concept. Although with Python 3.3 there's a
similar Windows concept that lets a user switch between Python versions
using the shebang, it's not really the same thing, and we shouldn't
discuss it till we know whether you're talking Windows or Linux.
 
T

Terry Reedy

Some additional comments.

Given that you are not using any libraries, let alone one that does not
run on Python 3, I strongly recommend using the latest version (3.3).

It would be pretty easy to make your simple code run on both 3.x and
2.6/7. Start your file (after any docstring or initial comment) with
from __future__ import division, print_function

Use "except XyxError as e:" instead of "except XyzError, e:".
If users start the program at a command line, the core of an input
function would be
numbers = input('Enter digits: ') # see below
You would need a more elaborate prompt printed first, and input checking
with the request repeated if the input does not pass the check.

# To run on both 2.x and 3.x, put this after the __future__ import:
try:
input = raw_input
except NameError:
pass
There are programs that will package your code with an interpreter.

A Python pre-built binary is overkill for such a small function. The
reason for doing so, packaging all dependencies together, does not
apply. Any binary is limited to what machines it will run on.
do give people the option to get just the program without installing a
duplicate interpreter.

A Python file, especially if designed to run on 2.6, will run on most
any recent installation.
 
P

Prasad, Ramit

CTSB01 said:
I have the following code that runs perfectly:

def psi_j(x, j):
for n2 in range(0, len(x) * j - 2):
n = n2 / j
r = n2 - n * j
rtn.append(j * x[n] + r * (x[n + 1] - x[n]))
print 'n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn
return rtn

No it doesn't run perfectly. It'll get a syntax error on the print

function call. That's assuming you're still using Python 3.3. You

really need to start by specifying your environment, without making us

look back through previous threads from you.
This code takes a string x = [0,1,1,1,2] for example

That's not a string. A string would be like

xx = psi_j("0abcd1234")

Perhaps you mean list? And is it a list of integers, or of arbitrary

numbers? Are there any constraints on the sizes or signs of those numbers?
(it must always begin with 0) and a parameter j, say 2, and outputs astring (x = [0, 1, 2, 2, 2,
2, 2, 3] in this example).
It does this in two steps: First it decomposes some number m into a multiple of j and a remainder.

Only if you replace the / with //. Or just use the function divmod():

n, r = divmod(n2, m)
Then it runs this decomposition through a function on the rtn.appendline.
Notice that this has cj - 1 terms where c is the number of terms in the input string and j is the
parameter. Normally, we would like it to be able to calculate cj terms.
So far you have a function, not a program. If you put it in a text file

and run it from python, it'll do nothing but display a syntax error

message. And when you fix that, it'll just run without doing anything.

usable for someone who has no knowledge of programming. In

particular, I need some kind of user interface that prompts
and a parameter,
idea is to make it usable
Python at all.

Then why are you asking on the Python forum? Or perhaps you mean

without him knowing he's running Python? In that case, use a shebang

line at the beginning, which will tell Linux to automatically invoke the

specified program (or programming language in this case).


If we assume you're running Python 3.3 on Linux, and the user is willing

to us the terminal, then how about parsing the string from the command

line he types? You can access it as011123334 a string from sys.argv,

and convert it to separate numbers. Of course as it stands now, you

cannot tell whether the user wanted

0,1,1,1,2,3,3,3,4

or

0, 111, 23, 3, 3, 4

or something else.

DaveA

Sorry Dave, to answer each part of your response:

1) I decided to use Python 2.7, and I will be sure to specify this in allfuture threads.
2) It is a list of positive integers. In fact, it is always going to be alist of positive increasing
integers.
3) You're right. What I meant was that if after running that bit of codeI enter
x = [0,1,2,3,4,5]
psi_j(x,2)
I will get output that matches my requirements.
4) Yes, sorry that's what I meant (if I understood correctly). I was toldelsewhere that I might want
to try using tkinter. Essentially I'm trying to create a user interface that allows the user to just
type in a string 01112345 for example, and choose a parameter (say j=2)and then click a button to run
the function. I'd like to be able to run send a .exe file that the user can just open up and use with
no further setup.

Any UI will work whether graphical or command line. TK is a good choice if you assume that the user
has Python installed. If you are planning to create an exe (Windows) then you can probably
bundle any GUI library (wx/gtk/qt) but you might be limited by the support of the exe creating
tool. I have never created an executable like this, so I am not sure.
So on top of the user interface I would also it looks like need to determine how to make Python change
a string 01112345 into a list so that it does that automatically when theuser clicks 'run'.

This really does not make sense to me. Does that mean 0, 1, 11, 23, 45 or 0,111, 2345 or 0,11,
2345 or something else entirely? If you are doing this on the shell I wouldhave the user pass
in a string of delimited values. "0,1,11,23,45" and then do a .split(',') onthe string. If
you are doing this via GUI then you can create separate fields for each number or still ask
for a delimited string.

However, if the parameter (j=2) determines parsing of numbers then you canignore my concerns.
Would a shebang still be the right way to go?

The shebang is useful when executing a script from cmd/shell directly.

$ ./script.py
# rather than
$ python script.py

If you are making an executable (.exe in Windows) then I do not
think it will matter either way for 2.7.
Thanks again Dave, apologies for the ambiguity.


~Ramit



This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy andcompleteness of information, viruses, confidentiality, legal privilege, andlegal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top