Creating a Simple User Interface for a Function

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Jul 26 10:08:14 EDT 2013


CTSB01 wrote:
> On Thursday, July 25, 2013 3:19:27 PM UTC-4, Dave Angel wrote:
> > On 07/25/2013 12:03 PM, CTSB01 wrote:
> >
> > > 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.
> >
> > 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 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 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 the user 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 would have the user pass 
in a string of delimited values. "0,1,11,23,45" and then do a .split(',') on the 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 can ignore 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 and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list