Re: [Tutor] Data persistence problem

2013-06-21 Thread Alan Gauld

On 21/06/13 07:21, Arijit Ukil wrote:

I have following random number generation function

def*rand_int* ():
 rand_num = int(math.ceil (random.random()*1000))
returnrand_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.


The simple solution is to store the value in a global variable.

rand_num = None

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

Or if you really don't like globals you could create
a generator function:

def rand_int():
   rand_num = int(math.ceil (random.random()*1000))
   while True:
  yield rand_num

Incidentally, any reason why you don't use the random.randint() function 
rather than the int(ceil(...) stuff?


HTH
--
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] Playing with XML

2013-06-21 Thread Peter Otten
Danilo Chilene wrote:

> 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:
> -
>  xmlns:s="http://schemas.xmlsoap.org/soap/envelope/";> xmlns="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";>true xmlns="http://schemas.datacontract.org/2004/07/Framework.BaseEnvelopes";
> xmlns:b="http://schemas.datacontract.org/2004/07/Framework"/> xmlns:b="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?

Read carefully:

>>> help(lxml.etree.parse)
Help on built-in function parse in module lxml.etree:

parse(...)
parse(source, parser=None, base_url=None)

Return an ElementTree object loaded with source elements.  If no parser
is provided as second argument, the default parser is used.

The ``source`` can be any of the following:

- a file name/path
- a file object
- a file-like object
- a URL using the HTTP or FTP protocol

To parse from a string, use the ``fromstring()`` function instead.

Note that it is generally faster to parse from a file path or URL
than from an open file object or file-like object.  Transparent
decompression from gzip compressed sources is supported (unless
explicitly disabled in libxml2).

The ``base_url`` keyword allows setting a URL for the document
when parsing from a file-like object.  This is needed when looking
up external entities (DTD, XInclude, ...) with relative paths.

A quick test confirms that fromstring() accepts unicode:

>>> lxml.etree.fromstring(u"äöü")

>>> print _.text
äöü

If response is a file-like object the following will work, too:

#untested
response = requests.post(url, data=xml, headers=headers)
doc = etree.parse(response)


___
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-21 Thread Oscar Benjamin
On 20 June 2013 16:11, Matt D  wrote:
>> 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')

You still haven't specified what GUI library you're using or described
your code layout so what I'll say below is very generic. In my mind it
looks very vaguely like this:

# file: myapp.py

def layout(tab):
tab.add_widget()
# blah blah blah

if __name__ == "__main__":
app = App()
frame = Frame(app)
tabs = frame.add_tabs(7)
layout(tabs[0])  # Add widgets to the tab
bindevents(tabs[0])
# blah blah blah
show()

When you run the above script with 'python myapp.py' is runs like
normal. Then you have another script:

# file: test_myapp.py

from myapp import layout

if __name__ == "__main__":
app = App()
frame = Frame(app)
tabs = frame.add_tabs(1)
layout(tabs[0])  # Add widgets to the tab
# don't bind anything here
show()

Now when you run 'python test_myapp.py' it opens up the dummy app. The
same layout() function is used by the real app and the dummy app so
the dummy app gives you a way of checking that it works.


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


Re: [Tutor] Data persistence problem

2013-06-21 Thread Wolfgang Maier
Alan Gauld  btinternet.com> writes:

> 
> On 21/06/13 07:21, Arijit Ukil wrote:
> > I have following random number generation function
> >
> > def*rand_int* ():
> >  rand_num = int(math.ceil (random.random()*1000))
> > returnrand_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.
> 
> The simple solution is to store the value in a global variable.
> 
> rand_num = None
> 
> def rand_int():
> global rand_num
> if not rand_num:
>rand_num = int(math.ceil (random.random()*1000))
> return rand_num
> 
> Or if you really don't like globals you could create
> a generator function:
> 
> def rand_int():
> rand_num = int(math.ceil (random.random()*1000))
> while True:
>yield rand_num
> 
> Incidentally, any reason why you don't use the random.randint() function 
> rather than the int(ceil(...) stuff?
> 
> HTH

a more general solution for random number generation is to use random.seed()
with a fixed argument:

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

then call this with the same argument to obtain the same number. But as Alan
said using random.seed and random.randint is a much simpler choice.
Wolfgang

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


Re: [Tutor] Data persistence problem

2013-06-21 Thread Peter Otten
Alan Gauld wrote:

> rand_num = None
> 
> def rand_int():
> global rand_num
> if not rand_num:

This will not recognize the (unlikely but possible) case that 
random.random() returns 0.0. So you better check for None explicitly

  if rand_num is None:
>rand_num = int(math.ceil (random.random()*1000))
> return rand_num


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


Re: [Tutor] Data persistence problem

2013-06-21 Thread Dave Angel

On 06/21/2013 02:21 AM, Arijit Ukil wrote:

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.



You do realize that this will return the values 1 through 1000 each with 
a probability of just under 0.1%, and return the value 0 extremely 
rarely?  I'd assume you really meant for it to exclude the value of 0 
(or else make it a lot more likely).


def rand_int(precalc_value=random.randint(1,1000)):
return precalc_value

As long as you don't call it with a keyword of precalc_value, it'll 
retain that initial value each time it's called.


And if you wanted the value of zero to also be equally likely (with a 
probability of 1/1001)


def rand_int(precalc_value=random.randint(0,1000)):
return precalc_value


--
DaveA
___
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-21 Thread Chris “Kwpolska” Warrick
On Fri, Jun 21, 2013 at 4:59 AM, SM  wrote:
>> # Replace stdout if needed
>> sys.stdout = self.oldstdout

It’s better to do it no matter what.  Otherwise, hell might break loose.

--
Kwpolska  | GPG KEY: 5EAAEA16
stop html mail| always bottom-post
http://asciiribbon.org| http://caliburn.nl/topposting.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Data persistence problem

2013-06-21 Thread Wolfgang Maier
Arijit Ukil  tcs.com> writes:

> 
> 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.

Sifting through the random module documentation a bit:

def rand_int(update=False):
if update:
dummy = random.random()
oldstate=random.getstate()
rand_num = random.randint(0,1000)
random.setstate(oldstate)

return rand_num

This will return a constant number when called with a False value for update
(the default), but will switch to returning a new number when called
with a True value once.

>>> rand_int()
644
>>> rand_int()
644
>>> rand_int(True)
120
>>> rand_int()
120
>>> rand_int()
120

Best, Wolfgang




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


Re: [Tutor] Writing logfile data to a user opened file

2013-06-21 Thread Prasad, Ramit
Matt D wrote:
> 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')
>

The point of using append mode is that you can close the file
and then re-open it.

> 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 

#  first new line
with open( filename, 'a' ) as f:
#self.logfile.write('\n') # Just add this directly to the end of each line
#  date and time
self.logfile.write('%s,'%( 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)))  
self.logfile.write('\n')
# File is now automatically closed

Although, I would be tempted to just use the csv module (untested
in append mode) and list context.

#import csv (at the top of the module)
with open( filename, 'ab' ) as f: # open as binary to avoid blank rows
writer = csv.writer(f)
data = [ str(field_values.get(key)) for key in sorted(self.fields) 
 if field_values.get(key) ] # keep fields in same order
# insert date to beginning
data.insert(0, strftime("%Y-%m-%d %H:%M:%S", localtime()))
writer.writerow(data)
# now file is automatically closed

> 
> 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
> 

When you open a file the data should be written to that. If you want to
move existing data from logfile.txt into user opened file then you need
to read logfile.txt and then write it to the user opened file. To make
your life simpler, either pass in the file path or open the file save
dialog on __init__.


~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


Re: [Tutor] Data persistence problem

2013-06-21 Thread Jim Mooney
On 21 June 2013 00:06, Alan Gauld  wrote:

> Or if you really don't like globals you could create
> a generator function:

Similar problem, by coincidence. Except I need a generator in a
function to increment a variable each time the function is called,
instead of giving it the same value every time. So far I have a
next(gen) as a parameter into the function but that's exactly not what
I want - since I need to increment three different numbers that will
persist in the function. I tried a few examples I saw but I keep
getting the same number, so I'm doing something wrong

-- 
Jim
A pride of lions, a gaggle of geese, a pack of wolves, a sewer of bankers.
___
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-21 Thread Prasad, Ramit
Matt D wrote:
> Sent: Thursday, June 20, 2013 6:44 AM
> To: tutor@python.org
> Subject: [Tutor] Best Code testing practice?
> 
> 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
> 

Have you heard of the MVC model? The idea is that you separate the 
business logic from the "view" or UI. The "controller" contains
all the business logic (or work actually being done).

Note the below is half pseudo-code just to give you an example.
I have neither tested the code nor expect it to actually run.

=
# File myapp/ui.py
class UI(wx.Frame):
def __init__( self, controller, *args, **kwargs ):
self.controller = controller
super(wx.Frame, self).__init__(*args, **kwargs)
def updateData(self):
data = self.controller.get_data()
self.display_data(data)

# File myapp/test.py
class TestController(object):
def __init__( self, *args, *kwargs ): #accept same options of real 
controller
pass # Don't need real data
def get_date(self):
''' return hard coded test data or generate test data'''
return [ 1,2,34,4,5 ] 

if __name__ == "__main__":
app = wx.App() # or whatever the setup for wx should be.
from myapp.ui import UI
UI(TestController())

# file myapp/main.py
from myapp.ui import UI
from myapp.controller import Controller

if __name__ == "__main__":
# setup wx
f = UI(Controller())
f.Fit() # or Show() or whatever

=

python myapp/test.py
python myapp/main.py


Now you can test your changes to business logic and your user
interface separately. You can even look into automated testing.


~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


Re: [Tutor] Best Code testing practice?

2013-06-21 Thread Matt D

> 
> 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.
> 
Hey guys!
Have decided that it is probably going to be better for my purposes to
simply crack open a terminal, cd into the appropriate directory, and do
the 'python test_code.py' or whatever the file name is from the command
line. I feel it is better for me to learn how to write code in gedit
before i use an IDE.
Thanks guys!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing logfile data to a user opened file

2013-06-21 Thread Matt D

> 
> When you open a file the data should be written to that. If you want to
> move existing data from logfile.txt into user opened file then you need
> to read logfile.txt and then write it to the user opened file. To make
> your life simpler, either pass in the file path or open the file save
> dialog on __init__.
> 
> 
> ~Ramit
> 
I got so frustrated try to figure a way to use the logfile.txt that I
changed how i log. first i name an array:

class TrafficPane(wx.Panel):
# Initializer
# the class constructor
def __init__(self, parent, msgq):
wx.Panel.__init__(self, parent)
self.msgq = msgq
#create the array to put the traffic data in
self.log_array = []

Then this is how the array gets into the file the user chooses:

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

And this is how i get the TextCtrl values into the array:

def update(self, field_values):
next_line = ""
#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()
next_line += (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)))
next_line += (str(f) + ',')
log_array.append(next_line)
#end logger code--

Its running right now.  I haven't had the opportunity to test if it
works so im keeping my fingers crossed.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing logfile data to a user opened file

2013-06-21 Thread Prasad, Ramit
Matt D wrote:
> [Ramit P wrote:]
> > When you open a file the data should be written to that. If you want to
> > move existing data from logfile.txt into user opened file then you need
> > to read logfile.txt and then write it to the user opened file. To make
> > your life simpler, either pass in the file path or open the file save
> > dialog on __init__.
> >
> >
> > ~Ramit
> >
> I got so frustrated try to figure a way to use the logfile.txt that I
> changed how i log. first i name an array:
> 
> class TrafficPane(wx.Panel):
> # Initializer
> # the class constructor
> def __init__(self, parent, msgq):
> wx.Panel.__init__(self, parent)
> self.msgq = msgq
> #create the array to put the traffic data in
> self.log_array = []
> 
> Then this is how the array gets into the file the user chooses:
> 
>   # openfile defined to start FileDialog
> def openFile(self, evt):
> with wx.FileDialog(self, "Choose a file", os.getcwd(), "",
> "*.txt*", wx.OPEN) as dlg:
> if dlg.ShowModal() == wx.ID_OK:
> path = dlg.GetPath()
> #mypath = os.path.basename(path)
> mypath = os.path.abspath(path)
> f = open(mypath, "rw+")

Why are you opening the file in "rw+"? 

> f.writelines(self.log_array)

You should really switch to the "with open() as f:" idiom I keep showing 
you. This will automatically close the file for you.

Also note that your file is only getting written once. You should
probably clear log_array and change the file mode back to append.

> 
> And this is how i get the TextCtrl values into the array:
> 
> def update(self, field_values):
> next_line = ""
> #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()
> next_line += (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)))
> next_line += (str(f) + ',')
> log_array.append(next_line)
> #end logger code--
> 
> Its running right now.  I haven't had the opportunity to test if it
> works so im keeping my fingers crossed.

This is an inefficient string concatenation. It can take large amounts
of memory and time. 

next_line += (str(f) + ',')

You can use str.join or use the csv module (which I recommend as it
will escape the delimeter (eg. commas ) if it shows up in the data ).
I have sent an example of the csv module already. Also note that
your order of the dictionary is not guaranteed so your data
may end up out of order after each run. 

'hi', 1 , 5423
4255, 'hi', 2
# instead of
1, 'hi', 5423
2, 'hi', 4255

# A str.join example 
next_line = []
for key in sorted( self.fields ):
f = field_values.get(k, None)
if f:
next_line.append(str(f))
line = ', '.join(next_line)
log_array.append(line)


~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


Re: [Tutor] Data persistence problem

2013-06-21 Thread ALAN GAULD
> ... I need to increment three different numbers that will
> persist in the function. I tried a few examples I saw but I keep
> getting the same number, so I'm doing something wrong


Give us a clue, show us your code!
 
Alan Gauld
Author of the Learn To Program website
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-21 Thread Alan Gauld

On 21/06/13 19:07, Prasad, Ramit wrote:


Have you heard of the MVC model? The idea is that you separate the
business logic from the "view" or UI. The "controller" contains
all the business logic (or work actually being done).


Being slightly picky but the business logic should sit mainly
in the Models. (ie the rules around how the data is manipulated,
the algorithms etc)

It's the scenario flow (or worklflow) that should sit in the
controller. ie The controller sequences the scenario or
use case by orchestrating the actions of the Models which in turn 
reflect their state changes in their associated views (GUI screens/panels).


In the original (Smalltalk) MVC pattern the Controller manages
user input but in later variants this is often a function of
the Views which then delegate to the associated controller.

--
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] Data persistence problem

2013-06-21 Thread Jim Mooney
On 21 June 2013 14:59, ALAN GAULD  wrote:

>
> Give us a clue, show us your code!

I was hoping you wouldn't say that since it's another of my insane
Lazy Typer programs to avoid typing, which are no doubt considered
frivolous. Although I'm learning a lot doing them ;')

Okay, I have a snippet that posts the below text in automatically. I
can use it to make any number of dicts, lists, or sets, from just
tokens in a single string, and they are printed out as the different
types, named and and numbered so they have different program names.
That way I don't have to type in quotes, commas, colons, and so forth.
Once the interpreter prints them out, I just select all and paste them
into my program, replacing what's below. Kind of a manual
meta-programming ;')

= pasted in snippet 

## Output sequence will be all strings. The first type in the 2nd
parameter list will be the
## default due to 'or" short circuiting, so you can just delete to get
what you want.
## You MUST have an even number of tokens for a dict or you'll get an exception.

from maker import makeseq
makeseq("Replace me, even number of tokens for dict", dict or list or set)

= end snippet 

>From this I can create any number of numbered dicts, lists or sets
(The 'or' short circuit means I get dicts as default, and only have to
delete from the front to get the others - dicts are default since
they're the most annoying to type - now That's lazy.)

For instance if I type this in after the import above:

makeseq("this better be an even number of tokens", dict or list or set)
makeseq("bacon pigs lies politicians foreclosures bankers
cheeseburgers good", dict or list or set)
makeseq("this is a list and it is not a very big list", list or set)
makeseq("Yet another list to show the different types increment
seperately", list or set)
makeseq("and finally some sets", set)
makeseq("sets can be be be be be very very useful to eliminate
duplicates duplicates", set)
makeseq("But this time I'm just counting set incidence up to three", set)

I get this in the interpreter:

D1 = {'Replace': 'me,', 'for': 'dict', 'of': 'tokens', 'even': 'number'}
D2 = {'even': 'number', 'of': 'tokens', 'be': 'an', 'this': 'better'}
D3 = {'cheeseburgers': 'good', 'bacon': 'pigs', 'lies': 'politicians',
'foreclosures': 'bankers'}
L1 = ['this', 'is', 'a', 'list', 'and', 'it', 'is', 'not', 'a',
'very', 'big', 'list']
L2 = ['Yet', 'another', 'list', 'to', 'show', 'the', 'different',
'types', 'increment', 'seperately']
S1 = {'sets', 'some', 'finally', 'and'}
S2 = {'eliminate', 'to', 'very', 'can', 'be', 'sets', 'duplicates', 'useful'}
S3 = {'just', 'to', 'time', 'incidence', 'set', 'this', "I'm", 'But',
'up', 'counting', 'three'}

Come to think of it I should rename makeseq since only the list is a sequence.

Then I just paste all that in to replace the original routine.  Since
I'm learning, I just like to have objects I can practice on without a
lot of typing difficult characters.

Below is the module, maker.py, that I import. As you can see I'm using
globals to increment the numbering for dicts, lists, and sets,
separately. This only creates strings at present, but I realized I can
get around that and "unstring" once I learn regular expressions. Since
I'm going "outside" the program to a higher level, and copying back
into it, I can break some informatic rules. It's the way you could
write a Java program with python, then run it, doing unPythonic
things. That's my tentative theory, anyway. (Although using Python to
write Java is like using a Faberge egg to pound nails, IMHO ;')

== begin maker.py module 

'''Accept a space-separated string of tokens that are each contiguous
characters.
The second parameter will turn them into a dict, list, or set. Dicts
must have an
even number of tokens. More than one of each type will be sequentially numbered
so they can be differentiated, such as D1, D2, L1, L2, L3. S1, S2,
etc. All sequences
will be printed out to be copied from the interpreter for programmatic use.'''

dictnumfarkadoodle = listnumfarkadoodle = setnumfarkadoodle = 0
# Since these are global I'm using words not likely to be duplicated
until I figure a different way and
# replace 'farkadoodle' with '' ;')

def makeseq(instring, typein):
global dictnumfarkadoodle, listnumfarkadoodle, setnumfarkadoodle
if isinstance(dict(), typein):
newdict = {}
dl = instring.split()
if len(dl) % 2 != 0: raise Exception ("list entries must be
even") # so they match
for idx in range(0,len(dl),2):
newdict[dl[idx]] = dl[idx+1]
dictnumfarkadoodle += 1
print('D' + str(dictnumfarkadoodle) + ' =', newdict)
elif isinstance(list(), typein):
newlist = []
dl = instring.split()
for word in dl:
newlist.append(word)
listnumfarkadoodle += 1
print('L' + str(listnumfarkadoodle) + ' =', newlist)
e

Re: [Tutor] Data persistence problem

2013-06-21 Thread ALAN GAULD


 Just a wee thought:

    if isinstance(dict(), typein):
>        newdict = {}
>        dl = instring.split()
>        if len(dl) % 2 != 0: raise Exception ("list entries must be
>even") # so they match
>        for idx in range(0,len(dl),2):
>            newdict[dl[idx]] = dl[idx+1]
>The for loop can be replaced with:

newdict = dict(zip(L[::2],L[1::2]))

Which avoids the explicit arithmetic and indexing and is therefore, arguably, 
cleaner... I'd probably wrap it in a try clause too:

if isinstance(dict(),typein):
   try: newdict = dict(zip(dl[::2],dl[1::2]))
   except TypeError:
    raise ValueError("input lists must be an even length")

And, since you always split the input, move that above all the isinstance() 
tests...

Similarly you don't need the loops for lists or sets, just use:

newlist = dl

and 

newset = set(dl)


But that doesn't answer your question about incrementing the globals! :-)
To me it looks from your sample data  like it is working!

Alan Gauld
Author of the Learn To Program website
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] Writing logfile data to a user opened file

2013-06-21 Thread Matt D
On 06/21/2013 04:44 PM, Prasad, Ramit wrote:
> Matt D wrote:
>> [Ramit P wrote:]
>>> When you open a file the data should be written to that. If you want to
>>> move existing data from logfile.txt into user opened file then you need
>>> to read logfile.txt and then write it to the user opened file. To make
>>> your life simpler, either pass in the file path or open the file save
>>> dialog on __init__.
>>>
>>>
>>> ~Ramit
>>>
>> I got so frustrated try to figure a way to use the logfile.txt that I
>> changed how i log. first i name an array:
>>
>> class TrafficPane(wx.Panel):
>> # Initializer
>> # the class constructor
>> def __init__(self, parent, msgq):
>> wx.Panel.__init__(self, parent)
>> self.msgq = msgq
>> #create the array to put the traffic data in
>> self.log_array = []
>>
>> Then this is how the array gets into the file the user chooses:
>>
>>   # openfile defined to start FileDialog
>> def openFile(self, evt):
>> with wx.FileDialog(self, "Choose a file", os.getcwd(), "",
>> "*.txt*", wx.OPEN) as dlg:
>> if dlg.ShowModal() == wx.ID_OK:
>> path = dlg.GetPath()
>> #mypath = os.path.basename(path)
>> mypath = os.path.abspath(path)
>> f = open(mypath, "rw+")
> 
> Why are you opening the file in "rw+"? 
> 
>> f.writelines(self.log_array)
> 
> You should really switch to the "with open() as f:" idiom I keep showing 
> you. This will automatically close the file for you.
> 
> Also note that your file is only getting written once. You should
> probably clear log_array and change the file mode back to append.
> 
>>
>> And this is how i get the TextCtrl values into the array:
>>
>> def update(self, field_values):
>> next_line = ""
>> #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()
>> next_line += (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)))
>> next_line += (str(f) + ',')
>> log_array.append(next_line)
>> #end logger code--
>>
>> Its running right now.  I haven't had the opportunity to test if it
>> works so im keeping my fingers crossed.
> 
> This is an inefficient string concatenation. It can take large amounts
> of memory and time. 
> 
> next_line += (str(f) + ',')
> 
> You can use str.join or use the csv module (which I recommend as it
> will escape the delimeter (eg. commas ) if it shows up in the data ).
> I have sent an example of the csv module already. Also note that
> your order of the dictionary is not guaranteed so your data
> may end up out of order after each run. 
> 
> 'hi', 1 , 5423
> 4255, 'hi', 2
> # instead of
> 1, 'hi', 5423
> 2, 'hi', 4255
> 
> # A str.join example 
> next_line = []
> for key in sorted( self.fields ):
> f = field_values.get(k, None)
> if f:
> next_line.append(str(f))
> line = ', '.join(next_line)
> log_array.append(line)
> 
> 
> ~Ramit
Thanks!  so i went with:

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

couldn't figure how to use the "with open() as f:"

and then down here:

# Update the field values
# put values in array
def update(self, field_values):
next_line = ""
next_line += strftime("%Y-%m-%d %H:%M:%S")
next_line +=  ','.join( field_values[k] for k in
self.fields.keys() if k in field_values )
log_array.append(next_line)

#if the field 'duid' == 'hdu', then clear all the fields
if field_values['duid'] == 'hdu':
self.clear()
#loop through all TextCtrl fields storing the key/value pairs in
k, v
for k,v in self.fields.items():
# get the pickle value for this TextCtrl
f = field_values.get(k, None)
# if the value is empty then set the new value
if f:
v.SetValue(f)


code definitely looks better but is its not working.  the TextCtrls are
not receiving their values anymore?  i cant tell why?



___
Tutor maillist  -

Re: [Tutor] Data persistence problem

2013-06-21 Thread Jim Mooney
On 21 June 2013 16:56, ALAN GAULD  wrote:

>
> But that doesn't answer your question about incrementing the globals! :-)
> To me it looks from your sample data  like it is working!

Good tips, though. Those sneaky zips are useful ;')  Yes, the globals
works fine. I just wondered if there was a way to get around using
globals; but right now I'm trying something else, anyway - since that
does work. The ultimate in laziness would be to get the program to
append to itself so I,don't have to cut and paste from the
interpreter, but I'm running into some tacky problems. Although some
of them are from the IDE. But others might be from the OS, and there
are different OSes so this wouldn't be portable.

-- 
Jim
A pride of lions, a gaggle of geese, a pack of wolves, a sewer of bankers.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing logfile data to a user opened file

2013-06-21 Thread Matt D

> 
> You should really switch to the "with open() as f:" idiom I keep showing 
> you. This will automatically close the file for you.
> 
it just occured to me to do this:

 def openFile(self, evt):
with wx.FileDialog(self, "Choose a file", os.getcwd(), "",
"*.txt*", wx.OPEN) as dlg:
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
mypath = os.path.basename(path)
with open(mypath, "a") as f:
f.writelines(self.log_array)

so thats how i used what you said, "with open() as f:".  is this the
right way to open the file?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Data persistence problem

2013-06-21 Thread Jim Mooney
On 21 June 2013 16:56, ALAN GAULD  wrote:


> if isinstance(dict(),typein):
>try: newdict = dict(zip(dl[::2],dl[1::2]))
>except TypeError:
> raise ValueError("input lists must be an even length")

Not sure why TypeError and ValueError is used. I would have thought
StopIteration but explain your logic on that as I'm unclear. But the
Exception never tripped, either way. I tried different length
iterables in the zip, but it looks like dict knows tostop before it
trip thems. Only next() does raises the exception. Unless I am
confused ;')

>>> zippy = zip([1,2],[3,4,5,6,7,8,9])
>>> D = dict(zippy)
>>> D
{1: 3, 2: 4} # dict works fine
>>> next(zippy) # exhausting zippy raises StopIteration
Traceback (most recent call last):
  File "", line 301, in runcode
  File "", line 1, in 
StopIteration

-- 
Jim
A pride of lions, a gaggle of geese, a pack of wolves, a sewer of bankers.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor