Re: [Tutor] Opinion - help could use more examples

2013-06-20 Thread Alan Gauld

On 20/06/13 03:38, Steven D'Aprano wrote:

On 20/06/13 10:21, Devin Jeanpierre wrote:



they're doing -- it omits things like advice to do with security,
including neglecting to declare that functions are not safe and can
execute arbitrary Python code,



I think it is perfectly acceptable for the Python documentation to
assume that anyone reading it will understand that calling a function
executes code.


I took it that Devin was referring to specific functions such as
v2.x input() that execute or evaluate the input parameters
as arbitrary code. I don't think he meant the fact that functions in 
general execute code.


eg. Help on input() says:

Help on built-in function input in module __builtin__:

input(...)
input([prompt]) -> value

Equivalent to eval(raw_input(prompt)).
(END)

There is no explicit mention that it is insecure or that it will execute 
it's input argument as code other than the reference to eval() which a 
beginner might not understand.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Best Code testing practice?

2013-06-20 Thread Matt D
Hey guys!
Is there a fast way test some piece of code?  I would like to be able to
look at the GUI I am making with out changing the file in dir 'baz' and
running the actual program (which is quite extensive).  Like if I could
just have a .py file with only the smallest amount of code possible to
make the GUI run, and I could run the .py file from the interpreter to
make the GUI run, this would be beautiful.  This allow me to
easily/quickly experiment with code and test my code.  Do we need an IDE
for this sort of thing or can we just use the interpreter?  OMG i was up
till 3am trying to find some way to do this.
Thanks!
-- 
Matt D

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best Code testing practice?

2013-06-20 Thread Alan Gauld

On 20/06/13 12:43, Matt D wrote:


Is there a fast way test some piece of code?


There are several testing frameworks for testing Python code.
nose is one example.
But...


look at the GUI I am making with out changing the file in dir 'baz'


In general your GUI should not be tied into one particular file or 
folder structure. You should be able to specify that either in a config 
file that you read when the GUI starts or as command line arguments.


That way you can separate your test data from your real production
data.


running the actual program (which is quite extensive).


Hopefully you have separated the presentation logic (the GUI
components) from the processing logic? If so testing the processing 
logic is fairly easy using one of the aforementioned frameworks. Testing 
the GUI itself is more tricky and usually requires

using some kind of robot to mimic what a user does by sending
dummy event messages into the GUI toolkit.


just have a .py file with only the smallest amount of code possible to
make the GUI run, and I could run the .py file from the interpreter to
make the GUI run, this would be beautiful.


That depends on how you built the app. If the GUI is encapsulated
as an an object then you can usually import the GUI code and instantiate 
the object. But if you have hard coded links from the GUI event handlers 
to your processing logic and from there to the real data/files then its 
going to be difficult.



Do we need an IDE for this sort of thing


Almost certainly not, although it may help.
You never *need* an IDE for python, they are only there to help.

The bottom line is that if your application architecture
has been well designed to separate the GUI and processing
and you have decoupled the data from the logic then testing
should be straightforward. If its all one big organic mashup
then you have a tough time ahead. It may be easier to
re-architect the application and then test it than to
struggle to bolt on tests retrospectively.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best Code testing practice?

2013-06-20 Thread Matt D
On 06/20/2013 08:52 AM, Alan Gauld wrote:
> On 20/06/13 12:43, Matt D wrote:
> 
>> Is there a fast way test some piece of code?
> 
> There are several testing frameworks for testing Python code.
> nose is one example.
> But...
> 
>> look at the GUI I am making with out changing the file in dir 'baz'
> 
> In general your GUI should not be tied into one particular file or
> folder structure. You should be able to specify that either in a config
> file that you read when the GUI starts or as command line arguments.
> 
> That way you can separate your test data from your real production
> data.
> 
>> running the actual program (which is quite extensive).
> 
> Hopefully you have separated the presentation logic (the GUI
> components) from the processing logic? If so testing the processing
> logic is fairly easy using one of the aforementioned frameworks. Testing
> the GUI itself is more tricky and usually requires
> using some kind of robot to mimic what a user does by sending
> dummy event messages into the GUI toolkit.
> 
>> just have a .py file with only the smallest amount of code possible to
>> make the GUI run, and I could run the .py file from the interpreter to
>> make the GUI run, this would be beautiful.
> 
> That depends on how you built the app. If the GUI is encapsulated
> as an an object then you can usually import the GUI code and instantiate
> the object. But if you have hard coded links from the GUI event handlers
> to your processing logic and from there to the real data/files then its
> going to be difficult.
> 
>> Do we need an IDE for this sort of thing
> 
> Almost certainly not, although it may help.
> You never *need* an IDE for python, they are only there to help.
> 
> The bottom line is that if your application architecture
> has been well designed to separate the GUI and processing
> and you have decoupled the data from the logic then testing
> should be straightforward. If its all one big organic mashup
> then you have a tough time ahead. It may be easier to
> re-architect the application and then test it than to
> struggle to bolt on tests retrospectively.
> 
all i really want to do is test the the GUI code.   i am working on a
'tab' in a notebook of 7 tabs, which is itself part of a large python
program which gets all of its computations done in C++.  The code for
'tab', or wxPanel really, i am using is in one file.
But i dont need to get into all that at this point.  all i want to be
able to do is make a small sample app that mimics the one tab i am
working on and be able to run it to see if the lay out and dialogs are
correct.
Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best Code testing practice?

2013-06-20 Thread Oscar Benjamin
On 20 June 2013 15:32, Matt D  wrote:
> all i really want to do is test the the GUI code.   i am working on a
> 'tab' in a notebook of 7 tabs, which is itself part of a large python
> program which gets all of its computations done in C++.  The code for
> 'tab', or wxPanel really, i am using is in one file.
> But i dont need to get into all that at this point.  all i want to be
> able to do is make a small sample app that mimics the one tab i am
> working on and be able to run it to see if the lay out and dialogs are
> correct.

Then make a small app that has just one tab (or has six dummy tabs if
necessary). When this app runs it should use the same function from
your main application to lay out the widgets but but it shouldn't bind
the (same) event handlers.


Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best Code testing practice?

2013-06-20 Thread Matt D
On 06/20/2013 10:49 AM, Oscar Benjamin wrote:
> On 20 June 2013 15:32, Matt D  wrote:
>> all i really want to do is test the the GUI code.   i am working on a
>> 'tab' in a notebook of 7 tabs, which is itself part of a large python
>> program which gets all of its computations done in C++.  The code for
>> 'tab', or wxPanel really, i am using is in one file.
>> But i dont need to get into all that at this point.  all i want to be
>> able to do is make a small sample app that mimics the one tab i am
>> working on and be able to run it to see if the lay out and dialogs are
>> correct.
> 
> Then make a small app that has just one tab (or has six dummy tabs if
> necessary). When this app runs it should use the same function from
> your main application to lay out the widgets but but it shouldn't bind
> the (same) event handlers.
> 
right make a small sample app.  exactly.  im sorry if im dense or
whatever and obviously i am super new to this process. but i can write
the scripts in gedit and then what?  how do i run that file and make the
gui start? should i be doing something like this?:

>>> execfile('filename.py')



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best Code testing practice?

2013-06-20 Thread Alan Gauld

On 20/06/13 16:11, Matt D wrote:


right make a small sample app.  exactly.  im sorry if im dense or
whatever and obviously i am super new to this process. but i can write
the scripts in gedit and then what?  how do i run that file and make the
gui start? should i be doing something like this?:


execfile('filename.py')


Probably not. You could just run the GUI directly:

python guifile.py

Then just play with it. If you define the eventhandlers
to print messages you can see which method is getting
called by each widget etc. But you won;t be using
the >>> prompt.

If you really want to drive it from the interpreter - maybe
to examine status fields etc - then the best thing is to make the
GUI a class and then import your file and create an instance of
it. You can then send messages to the object to exercise its
behaviour.

# file testGUI.py
class MyGUI
def __init__(self):
self.buildGUI()

def eventHandler_1(self):
   # do something in response to event1
   # maybe print a message or log the GUI data in a file
def eventHandler_2(self):
   # same for event 2 etc...

def buildGUI(self):
# build your GUI here including your tab.
# bind any widgets to use the event handlers defined above


Now, in the interpreter:

>>> import testGUI
>>> app = testGUI.MyGUI()  # GUI should appear for you to play with
>>> app.eventHandler_1()   # test the event handlers independent of GUI
>>> print app.myVariable   # check a GUI variable value

Does that help?


PS.
Not sure how this will come through. Thunderbird has changed font colour 
half way through and I can't figure out how to change it back - which 
makes me suspicious!


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best Code testing practice?

2013-06-20 Thread Mark Lawrence

On 20/06/2013 12:43, Matt D wrote:

Hey guys!
Is there a fast way test some piece of code?  I would like to be able to
look at the GUI I am making with out changing the file in dir 'baz' and
running the actual program (which is quite extensive).  Like if I could
just have a .py file with only the smallest amount of code possible to
make the GUI run, and I could run the .py file from the interpreter to
make the GUI run, this would be beautiful.  This allow me to
easily/quickly experiment with code and test my code.  Do we need an IDE
for this sort of thing or can we just use the interpreter?  OMG i was up
till 3am trying to find some way to do this.
Thanks!



I suspect that you'd get better answers on a GUI specific mailing list, 
like one for wxPython, but I note that you've already asked pretty much 
the same question there.


--
"Steve is going for the pink ball - and for those of you who are 
watching in black and white, the pink is next to the green." Snooker 
commentator 'Whispering' Ted Lowe.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Playing with XML

2013-06-20 Thread Danilo Chilene
Hello,

Below is my code:

#!/bin/env python
# -*- coding: utf-8 -*-
import requests
from lxml import etree

url = 'http://192.168.0.1/webservice.svc?wsdl'
headers = {'Content-Type': 'text/xml;charset=UTF-8', 'SOAPAction': '
http://tempuri.org/ITService/SignIn'}
xml = '''http://schemas.xmlsoap.org/soap/envelope/"; xmlns:tem="http://tempuri.org/";>




123
123
123
omg



'''

response = requests.post(url, data=xml, headers=headers).text
print response

doc = etree.parse(response)


The content of variable response is a big XML with some values that I want.

Part of variable response:
-
http://schemas.xmlsoap.org/soap/envelope/";>http://tempuri.org/";>http://schemas.datacontract.org/2004/07/Core.DTO.Envelopes.Authentication";
xmlns:i="http://www.w3.org/2001/XMLSchema-instance";>http://schemas.datacontract.org/2004/07/Framework.BaseEnvelopes"; xmlns:b="
http://schemas.datacontract.org/2004/07/Framework"/>http://schemas.datacontract.org/2004/07/Framework.BaseEnvelopes"; xmlns:b="
http://schemas.datacontract.org/2004/07/Framework"/>http://schemas.datacontract.org/2004/07/Framework.BaseEnvelopes";>truehttp://schemas.datacontract.org/2004/07/Framework.BaseEnvelopes";
xmlns:b="http://schemas.datacontract.org/2004/07/Framework"/>http://schemas.microsoft.com/2003/10/Serialization/Arrays
">removeDuplicatedFlightstrueuseWeakPassword
-

Below the return of doc = etree.parse(response)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 3630:
ordinal not in range(128)
The type of response is unicode.


The whole idea is sign in on this webservice and get a Security token and
then run another XML on the same script.

Any ideas to transform this unicode on XML and parse it?

Best Regards,

Danilo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Writing logfile data to a user opened file

2013-06-20 Thread Matt D
Hey guys!
So now my UI panel works well with this:

# Open file button click event binded to openfile
btn = wx.Button(self, -1, "Click me")
sizer.Add(btn, pos=(7,2))
btn.Bind(wx.EVT_BUTTON, self.openFile)  

#set the panel layout
self.SetSizer(sizer)
#makes the gui system fit all the controls onto the panel7
self.Layout()
self.Fit()

EVT_DATA_EVENT(self, self.display_data)
#self.watcher = traffic_watcher_thread(self.msgq, self)

# openfile defined to start FileDialog
def openFile(self, evt):
with wx.FileDialog(self, "Choose a file", os.getcwd(), "",
"*.*", wx.OPEN) as dlg:
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
mypath = os.path.basename(path)


So as you guys taught me to do I was opening 'logfile' this way:

self.logfile = open('logfile.txt', 'a')

And the logger code:

#logger code---
#  first new line
self.logfile.write('\n')
#  date and time
self.logfile.write('%s,'%(str(strftime("%Y-%m-%d %H:%M:%S", localtime()
#  loop through each of the TextCtrl objects
for k,v in self.fields.items():
#  get the value of the current TextCtrl field
f = field_values.get(k, None)
if f:
#  output the value with trailing comma
self.logfile.write('%s,'%(str(f)))  
#end logger code 

And that is working well.
Now I am trying to think of a way to get what is in the logfile.txt into
the file that is opened by the user via the UI.  Getting stuck trying to
come up with an idea!?  maybe something in python like 'user_opened_file
= logfile' or 'write logfile to user_opened_file'?  I am not able to
find standard way to do this.
Cheers!

-- 
Matt D

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to redirect console output to a TextEdit box on a QT Python Gui ?

2013-06-20 Thread SM
Thanks, much, Ramit.


On Wed, Jun 19, 2013 at 4:44 PM, Prasad, Ramit wrote:

> SM wrote:
> > Hello Chris, Thanks for your response. I have a follow-up question, if
> you don't mind, to understand
> > your answer better.
> > I am running a python3 script. So  first part of your answer applies
> here:
> >
> > "If the application you run is a Python script, import it, execute the
> > functions and have the data returned (as opposed to printing it), and
> > then you can do self.textEdit.setText(output_
> > goes_here)"
> > My python script prints a lot of text at various stages. There are
> "print" statements spread across
> > multiple python files.  So when you say "have the data returned" and
> "output_goes_here" as the
> > argument of setText, I am a bit confused as to how I can redirect
> multiple print statements to the
> > setText call. Can you please clarify?
> >
> > Thanks!
> > Sm
>
> Usually your logic functions should not print (except for debugging). It
> should return
> data which you can then wrap in a UI which prints or displays on GUI. He
> does not mean to
> actually redirect output but instead do each "stage" and then
> print/display the appropriate
> text *after* the task is done.
>
> Instead of trying to redirect output you can try logging to file and
> having the GUI read that file
> and print what is appropriate.
>
> You can also try to replace stdout with an in-memory buffer which you
> aggregate and then display after
> the stage (or at any point) like this (untested).
>
> self.oldstdout = sys.stdout
> sys.stdout = cStringIO.StringIO()
> # Do processing stages
> # And later
> self.textEdit.setText( sys.stdout.getvalue() )
> # Replace stdout if needed
> sys.stdout = self.oldstdout
>
>
> ~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.
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Data persistence problem

2013-06-20 Thread Arijit Ukil
I have following random number generation function

def rand_int ():
rand_num = int(math.ceil (random.random()*1000))
return rand_num

I like to make the value of rand_num (return of rand_int) static/ 
unchanged after first call even if it is called multiple times. If x= 
rand_int () returns 45 at the first call, x should retain 45 even in 
multiple calls.
Pls help.

Regards,
Arijit Ukil
Tata Consultancy Services
Mailto: arijit.u...@tcs.com
Website: http://www.tcs.com

Experience certainty.   IT Services
Business Solutions
Consulting

=-=-=
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor