Re: [Tutor] Need help printing a pickled data

2013-06-25 Thread eryksun
On Mon, Jun 24, 2013 at 6:57 PM, Steven D'Aprano  wrote:
>
> You certainly shouldn't be writing pickle data to a log file! Firstly,
> log files are usually opened in text mode, not binary mode, so it
> probably won't work, and secondly even if it did work, you will be
> dumping a load of pickled binary data into the middle of what should be
> a text file. That's a bad idea. And even if it succeeded, what are you
> going to learn from seeing a line like this:

I don't know which version Matt is using, but 2.x defaults to pickle
protocol 0, which is ASCII. In 3.x you can manually specify protocol
0:

>>> pickle.dumps([1,2,3], protocol=0)
b'(lp0\nL1L\naL2L\naL3L\na.'

With the ASCII protocol you can open the file in either text or binary
mode, so long as you're consistent. But use binary mode if it needs to
be cross-platform.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mistaken about splitting expressions over lines

2013-06-25 Thread eryksun
On Mon, Jun 24, 2013 at 9:58 PM, Dave Angel  wrote:
>
> Alternatively, you can also use the statement continuation mechanism,
> whereby the last character of the line is a backslash.  Using that approach
> you can break almost anywhere, except within a token or inside a string
> literal.

Also, the next character after a line continuation has to be a
newline, e.g. no spaces, tabs, or comments. Your editor should have a
feature to remove trailing white space on each line.

>>> a = 'this' \
  File "", line 1
a = 'this' \
^
SyntaxError: unexpected character after line continuation character


>>> a = ('this'  # this way
...  ' string' ' is long') # is more flexible
>>> a
'this string is long'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help printing a pickled data

2013-06-25 Thread Matt D
On 06/25/2013 07:28 AM, eryksun wrote:
> On Mon, Jun 24, 2013 at 6:57 PM, Steven D'Aprano  wrote:
>>
>> You certainly shouldn't be writing pickle data to a log file! Firstly,
>> log files are usually opened in text mode, not binary mode, so it
>> probably won't work, and secondly even if it did work, you will be
>> dumping a load of pickled binary data into the middle of what should be
>> a text file. That's a bad idea. And even if it succeeded, what are you
>> going to learn from seeing a line like this:
> 
> I don't know which version Matt is using, but 2.x defaults to pickle
> protocol 0, which is ASCII. In 3.x you can manually specify protocol
> 0:
> 
> >>> pickle.dumps([1,2,3], protocol=0)
> b'(lp0\nL1L\naL2L\naL3L\na.'
> 
> With the ASCII protocol you can open the file in either text or binary
> mode, so long as you're consistent. But use binary mode if it needs to
> be cross-platform.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
I'm using 2.7 for this program.  I the docs say what you said about the
default being 0 so I didn't know I needed to convert to string.
Thanks.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mistaken about splitting expressions over lines

2013-06-25 Thread Albert-Jan Roskam
___
>From: eryksun 
>To: Jim Mooney  
>Cc: tutor@python.org 
>Sent: Tuesday, June 25, 2013 2:14 PM
>Subject: Re: [Tutor] mistaken about splitting expressions over lines



>
>    >>> a = ('this'  # this way
>    ...      ' string' ' is long') # is more flexible
>    >>> a
>    'this string is long'

 
I did something similar after having read 
http://docs.python.org/2/howto/doanddont.html, under "Using Backslash to 
Continue Statements".
 
But I always use + signs. I didn't know that omitting them also works. Is 
str.__add__ called then, too?
Isn't this a violation of the 'Explicit is better than implicit(ly concatenate 
strings)' principle?
>>> a = ('this' +
  ' string' +
  ' is long')
>>> a
'this string is long'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mistaken about splitting expressions over lines

2013-06-25 Thread Peter Otten
Albert-Jan Roskam wrote:

> ___
>>From: eryksun 
>>To: Jim Mooney 
>>Cc: tutor@python.org
>>Sent: Tuesday, June 25, 2013 2:14 PM
>>Subject: Re: [Tutor] mistaken about splitting expressions over lines
> 
> 
> 
>>
> a = ('this'  # this way
>>...  ' string' ' is long') # is more flexible
> a
>>'this string is long'
> 
> 
> I did something similar after having read
> http://docs.python.org/2/howto/doanddont.html, under "Using Backslash to
> Continue Statements".
> 
> But I always use + signs. I didn't know that omitting them also works. Is
> str.__add__ called then, too? Isn't this a violation of the 'Explicit is
> better than implicit(ly concatenate strings)' principle?
 a = ('this' +
> ' string' +
> ' is long')
 a
> 'this string is long'

In older Pythons for ("alpha" "beta") the compiler would merge the two 
strings into one whereas ("alpha" + "beta") would trigger a str.__add__() 
call at runtime. Nowadays the peephole optimiser recognizes ("alpha" + 
"beta") and replaces it with a single string:

>>> import dis
>>> def f():
... return ("alpha" +
... "beta")
... 
>>> dis.dis(f)
  3   0 LOAD_CONST   3 ('alphabeta')
  3 RETURN_VALUE


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


Re: [Tutor] mistaken about splitting expressions over lines

2013-06-25 Thread eryksun
On Tue, Jun 25, 2013 at 10:11 AM, Peter Otten <__pete...@web.de> wrote:
>
> In older Pythons for ("alpha" "beta") the compiler would merge the two
> strings into one whereas ("alpha" + "beta") would trigger a str.__add__()
> call at runtime. Nowadays the peephole optimiser recognizes ("alpha" +
> "beta") and replaces it with a single string:
>
 import dis
 def f():
> ... return ("alpha" +
> ... "beta")
> ...
 dis.dis(f)
>   3   0 LOAD_CONST   3 ('alphabeta')
>   3 RETURN_VALUE


Constant folding for binary operations has a length limit of 20 for sequences:

>>> dis.dis(lambda: '0123456789' + '0123456789' + '0')
  1   0 LOAD_CONST   3 ('0123456789
 0123456789')
  3 LOAD_CONST   2 ('0')
  6 BINARY_ADD
  7 RETURN_VALUE


>>> dis.dis(lambda: (0,1,2,3,4,5,6,7,8,9) +
... (0,1,2,3,4,5,6,7,8,9) + (0,))
  2   0 LOAD_CONST  13 ((0, 1, 2, 3, 4,
 5, 6, 7, 8, 9,
 0, 1, 2, 3, 4,
 5, 6, 7, 8, 9))
  3 LOAD_CONST  14 ((0,))
  6 BINARY_ADD
  7 RETURN_VALUE
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mistaken about splitting expressions over lines

2013-06-25 Thread Peter Otten
eryksun wrote:

> Constant folding for binary operations has a length limit of 20 for
> sequences:
> 
> >>> dis.dis(lambda: '0123456789' + '0123456789' + '0')
>   1   0 LOAD_CONST   3 ('0123456789
>  0123456789')
>   3 LOAD_CONST   2 ('0')
>   6 BINARY_ADD
>   7 RETURN_VALUE

Interesting. Do you know why the limit is so low (especially for strings)?

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


Re: [Tutor] mistaken about splitting expressions over lines

2013-06-25 Thread eryksun
On Tue, Jun 25, 2013 at 11:35 AM, Peter Otten <__pete...@web.de> wrote:
> eryksun wrote:
>
>> Constant folding for binary operations has a length limit of 20 for
>> sequences:
>>
>> >>> dis.dis(lambda: '0123456789' + '0123456789' + '0')
>>   1   0 LOAD_CONST   3 ('0123456789
>>  0123456789')
>>   3 LOAD_CONST   2 ('0')
>>   6 BINARY_ADD
>>   7 RETURN_VALUE
>
> Interesting. Do you know why the limit is so low (especially for strings)?

It isn't special-cased for strings. It just checks for a sequence
length in general. The idea is to limit the size of .pyc files. Quote:

If the new constant is a sequence, only folds when the size
is below a threshold value.  That keeps pyc files from
becoming large in the presence of code like:  (None,)*1000.

The threshold of 20 isn't a tunable parameter. It's hard-coded in the source:

size = PyObject_Size(newconst);
if (size == -1)
PyErr_Clear();
else if (size > 20) {
Py_DECREF(newconst);
return 0;
}
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help printing a pickled data

2013-06-25 Thread Prasad, Ramit
Peter Otten wrote:
> Matt D wrote:
> 
> > On 06/24/2013 07:17 PM, Alan Gauld wrote:
> >> On 24/06/13 23:05, Matt D wrote:
> >>> I have been unable to find a way to write pickled data to text file.
> >>
> >> Probably because pickled data is not plain text.
> >> You need to use binary mode. However...
> >>
> >>
> >>>  def __init__(self, data):
> >>>  wx.PyEvent.__init__(self)
> >>>  self.SetEventType (wxDATA_EVENT)
> >>>  # and this is the actual data
> >>>  self.data = data
> >>>  with open('mypicklelog.txt','a') as log:
> >>>  log.write(self.data)
> >>
> >> Since you are not using pickle here, all you are really doing
> >> is trying to write whatever data is to a text file that
> >> happens to have 'pickle' in its name.
> >>
> >> When writing to a text file you need to write strings.
> >> You have no guarantee that 'data' is a string. You should
> >> probably convert it before writing it. Thats one of the
> >> advantages of using real pickles - they take care of
> >> that complication for you.
> >>
> >>> I cant figure out why these last two line dont write to the .txt file
> >>> after the program has received the pickled Python dictionary?
> >>
> >> Pickle data has to be unpickled before you can use it.
> >> Before you can write it back again you need to repickle it.
> >> The code you posted does not show you producing and pickled
> >> data nor indeed you reading any pickled data...
> >>
> >> If 'data' is indeed in pickle format you cannot simply write
> >> it to a text file since Pickle is not in a text format.
> >>
> >>
> > im sorry; some more code will clarify i think ;
> 
> No, you aren't listening to Alan. The suggestive filename notwithstanding
> 
> > with open('mypicklelog.txt','a') as log:
> > log.write(self.data)
> 
> will fail unless self.data is a string. Disregarding all other problems for
> the moment, you need
> 
> with open('mypicklelog.txt','ab') as log: # open in binary mode
> pickle.dump(self.data, log) # serialize data and write to file
> 
> where pickle.dump(obj, file) converts `obj` to a sequence of bytes before it
> is written to `file`.

Well I think self.data is some kind of container with a pickled string, 
given the code to unpickle it is:

   #message is equal to the "data" parameter in the "DataEvent" class
message = event.data
# unpickle the string
pickled_dict = message.to_string()
#separate the string into values for each text control (attrs is
a pickle object)
attrs = pickle.loads(pickled_dict)


Try

 with open('mypicklelog.txt','ab') as log: # open in binary mode
 pickle.dump(pickle.loads(self.data.to_string()), log) # serialize data and 
write to file


~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] Need help printing a pickled data

2013-06-25 Thread Matt D

> 
> with open('mypicklelog.txt','ab') as log: # open in binary mode
> pickle.dump(self.data, log) # serialize data and write to file
> 
> where pickle.dump(obj, file) converts `obj` to a sequence of bytes before it 
> is written to `file`.
> 

I put this like this:

 class DataEvent(wx.PyEvent):
# the values of the text fields get passed into the constructor
inside of data
def __init__(self, data):
wx.PyEvent.__init__(self)
# this line *binds* this class to a certain type of event,
wxDATA_EVENT
self.SetEventType (wxDATA_EVENT)
# and this is the actual data
self.data = data
with open('mypicklelog.txt','ab') as log: # open in binary mode
pickle.dump(self.data, log) # serialize data and write to file


And I still get nothing.  This has me super confused, I have trying at
this for a long time and I have been reading the docs like

http://docs.python.org/2/library/pickle.html

And I still don't get it to work. So the C++ makes the pickle, this is
from the comments in the C++ :

/**
 * snapshot_du_handler. Writes traffic snapshots to a msg_queue based
 * on the HDU frame contents. The format used is that of a pickled
 * python dictionary allowing the other end of the queue to pick only
 * those fields of interest and ignore the rest.
 */

Then the python program (attached) program unpickles and repickles it?
I attached the python program, if you have a minute or two please take a
look, its not too long.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  op25_traffic_panel.py
#  
#  Copyright 2013 Balint Seeber 
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  
#  MyVersion 1.2
#  the import statements; very similar to #include in c++ 
from time import localtime, strftime
import os
import wx
import cPickle as pickle
import gnuradio.gr.gr_threading as _threading

#  wx is the gui class. it implements a version of "events" -- objects that sit and wait for some data to change, and call a specified function when the change happens
wxDATA_EVENT = wx.NewEventType()

# this is a function that *sets* what happens when the event is triggered. it takes in an event (win) and a function (func)
def EVT_DATA_EVENT(win, func):
win.Connect(-1, -1, wxDATA_EVENT, func)

# the dataevent class -- stores the data that gets transmitted when the event occurs. 
#it is the data in text fields, stored in self.data as a dictionary, which is basically a c++ map
#One of these classes gets created whenever an event occurs. 
class DataEvent(wx.PyEvent):
# the values of the text fields get passed into the constructor inside of data
def __init__(self, data):
wx.PyEvent.__init__(self)
# this line *binds* this class to a certain type of event, wxDATA_EVENT
self.SetEventType (wxDATA_EVENT)
# and this is the actual data
self.data = data 
with open('mypicklelog.txt','ab') as log: # open in binary mode
pickle.dump(self.data, log) # serialize data and write to file

# clone is a python function to make a "deep" copy of an object
def Clone (self):
self.__class__ (self.GetId())

# thread that waits for new data to be received 
# when event is triggered new data is passed along 
# this class inherits from the standard Python class _threading.Thread
class traffic_watcher_thread(_threading.Thread):
def __init__(self, rcvd_pktq, event_receiver):
## variables are standard values required to set up a thread
_threading.Thread.__init__(self)
self.setDaemon(1)
self.rcvd_pktq = rcvd_pktq
self.event_receiver = event_receiver
self.keep_running = True
self.start()

def stop(self):
self.keep_running = False

def run(self):
while self.keep_running:
msg = self.rcvd_pktq.delete_head()
## once data is received, an event is "Posted" with PostEvent. 
## This is where the DataEvent object gets created
de = DataEvent (msg)
wx.PostEvent (self.event_receiver, de)
del de

# A snapshot of important fields in current traffic
# this inherits from the gui class Panel (required for all gui programs)
class TrafficPane(wx.Panel):
# Initializer; class

Re: [Tutor] Need help printing a pickled data

2013-06-25 Thread Matt D

> 
> Well I think self.data is some kind of container with a pickled string, 
> given the code to unpickle it is:
> 
Exactly!  This is what the C++ file 'pickle.h' creates to send to the
Python GUI:


/**
 * A pickled Python dictionary. Used to pass stuff to the UI.
 */
class pickle
{

public:

   /**
* pickle constructor.
*
* \param frame_body A const_bit_queue representing the frame body.
*/
   pickle();

   /**
* pickle virtual destructor.
*/
   ~pickle();

   /**
* Add a key/value pair to the pickled dictionary
*/
   void add(std::string key, std::string value);

   /**
* Returns a string describing the Data Unit ID (DUID).
*/
   std::string to_string() const;

private:

   typedef std::map stringmap;

   stringmap map_;

};

#endif /* INCLUDED_PICKLE_H */

I am pretty sure that it sends a C++ map of strings in a form that
Python understands as a pickle.  I put the code you gave me into the
file so ill have to wait and see.
meanwhile can you please take a look at this update() and tell me if you
see something wrong because ever sense I tried using the array for
logging the values from the TextCtrls the program is not updating
meaning when the program receives the c++ map, or the pickle, I get
nothing in the UI and I get nothing in the log.  There got to be
something wrong with the loop that I can not see.  I attached the file
so its not too much code here.  I am pretty sure the problem is in the
update(0) starts at line 210.


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  op25_traffic_panel.py
#  
#  Copyright 2013 Balint Seeber 
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  
#  MyVersion 1.2
#  the import statements; very similar to #include in c++ 
from time import localtime, strftime
import os
import wx
import cPickle as pickle
import gnuradio.gr.gr_threading as _threading

#  wx is the gui class. it implements a version of "events" -- objects that sit and wait for some data to change, and call a specified function when the change happens
wxDATA_EVENT = wx.NewEventType()

# this is a function that *sets* what happens when the event is triggered. it takes in an event (win) and a function (func)
def EVT_DATA_EVENT(win, func):
win.Connect(-1, -1, wxDATA_EVENT, func)

# the dataevent class -- stores the data that gets transmitted when the event occurs. 
#it is the data in text fields, stored in self.data as a dictionary, which is basically a c++ map
#One of these classes gets created whenever an event occurs. 
class DataEvent(wx.PyEvent):
# the values of the text fields get passed into the constructor inside of data
def __init__(self, data):
wx.PyEvent.__init__(self)
# this line *binds* this class to a certain type of event, wxDATA_EVENT
self.SetEventType (wxDATA_EVENT)
# and this is the actual data
self.data = data 
with open('mypicklelog.txt','ab') as log: # open in binary mode
pickle.dump(pickle.loads(self.data.to_string()), log) # serialize data and write to file


# clone is a python function to make a "deep" copy of an object
def Clone (self):
self.__class__ (self.GetId())

# thread that waits for new data to be received 
# when event is triggered new data is passed along 
# this class inherits from the standard Python class _threading.Thread
class traffic_watcher_thread(_threading.Thread):
def __init__(self, rcvd_pktq, event_receiver):
## variables are standard values required to set up a thread
_threading.Thread.__init__(self)
self.setDaemon(1)
self.rcvd_pktq = rcvd_pktq
self.event_receiver = event_receiver
self.keep_running = True
self.start()

def stop(self):
self.keep_running = False

def run(self):
while self.keep_running:
msg = self.rcvd_pktq.delete_head()
## once data is received, an event is "Posted" with PostEvent. 
## This is where the DataEvent object gets created
de = DataEvent (msg)
wx.PostEvent (self.event_receiver, de)
del de

# A snapshot of important fields in current traffic
# this inherits from the gui class Panel (required for all gui programs)
class TrafficPane(wx.Panel

Re: [Tutor] Need help printing a pickled data

2013-06-25 Thread Prasad, Ramit
Please leave attributions in so we know who is saying what.

Matt D wrote:
> [Ramit Prasad wrote]
> > [Peter Otten wrote]
> >
> > with open('mypicklelog.txt','ab') as log: # open in binary mode
> > pickle.dump(self.data, log) # serialize data and write to file
> >
> > where pickle.dump(obj, file) converts `obj` to a sequence of bytes before it
> > is written to `file`.
> >
> 
> I put this like this:
> 
>  class DataEvent(wx.PyEvent):
> # the values of the text fields get passed into the constructor
> inside of data
> def __init__(self, data):
> wx.PyEvent.__init__(self)
> # this line *binds* this class to a certain type of event,
> wxDATA_EVENT
> self.SetEventType (wxDATA_EVENT)
> # and this is the actual data
> self.data = data
> with open('mypicklelog.txt','ab') as log: # open in binary mode
> pickle.dump(self.data, log) # serialize data and write to file
> 
> 
> And I still get nothing.  This has me super confused, I have trying at
> this for a long time and I have been reading the docs like
> 
> http://docs.python.org/2/library/pickle.html
> 
> And I still don't get it to work. So the C++ makes the pickle, this is
> from the comments in the C++ :
> 
> /**
>  * snapshot_du_handler. Writes traffic snapshots to a msg_queue based
>  * on the HDU frame contents. The format used is that of a pickled
>  * python dictionary allowing the other end of the queue to pick only
>  * those fields of interest and ignore the rest.
>  */
> 
> Then the python program (attached) program unpickles and repickles it?
> I attached the python program, if you have a minute or two please take a
> look, its not too long.

The problem is that what gets sent across the queue is actually a container 
of some kind that has a pickled string. The comments in the code are just 
wrong. The key is following display_data which is correct.

# unpickle the string 
pickled_dict = message.to_string()

This is actually retrieving a string (or bytes more accurately) OF pickled data 
from some message or container object. These bytes then needs to be unpickled.

#separate the string into values for each text control (attrs is a pickle 
object)
attrs = pickle.loads(pickled_dict)

Here the pickled data (as bytes/string) are unpickled and attrs
is a Python dictionary (not a "pickle object").

*I use bytes/string interchangeably because in Python 2 they are the same. 
In Python 3 and semantically, they are different so the differentiation is 
important.

The real question is why do you want this pickle in a file?  I am not sure 
it will be easy to pull out and reuse anyway. Given your experience level, 
I think this is a lot of work for something that you are unlikely to be able 
to easily use. I think it would be more useful to `log.write(repr(attrs))`.

Once you have the bytes of pickled data, just write that to file.

with open('mypicklelog.txt','ab') as log: 
# All pickles will run together because there is no spacing.
log.write(self.data.to_string()) 


Again, I think you would be better off with the repr()
with open('mypicklelog.txt','ab') as log: 
log.write(repr(pickle.loads(self.data.to_string(


~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] Need help printing a pickled data

2013-06-25 Thread Alan Gauld

On 25/06/13 17:32, Matt D wrote:


 self.data = data
 with open('mypicklelog.txt','ab') as log: # open in binary mode
 pickle.dump(self.data, log) # serialize data and write to file


And I still get nothing.


Define 'nothing'.

Does the file exist?
Does it have anything in it?
Does self.data exist - what does it look like if you print it?
Are there any error messages?

--
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] Need help printing a pickled data

2013-06-25 Thread Prasad, Ramit
Again, please leave in attributions.

Matt D wrote:
> [Ramit Prasad wrote]
> >
> > Well I think self.data is some kind of container with a pickled string,
> > given the code to unpickle it is:
> >
> Exactly!  This is what the C++ file 'pickle.h' creates to send to the
> Python GUI:

Not really.

> 
> 
> /**
>  * A pickled Python dictionary. Used to pass stuff to the UI.
>  */
> class pickle
> {
> 
[snip C++ code]
> 
> };

You can call it a pickle, but it is not actually a pickle. It is
a wrapper around pickled data. That is *not* the same thing. 

> 
> #endif /* INCLUDED_PICKLE_H */
> 
> I am pretty sure that it sends a C++ map of strings in a form that
> Python understands as a pickle.  I put the code you gave me into the
> file so ill have to wait and see.
> meanwhile can you please take a look at this update() and tell me if you
> see something wrong because ever sense I tried using the array for
> logging the values from the TextCtrls the program is not updating
> meaning when the program receives the c++ map, or the pickle, I get
> nothing in the UI and I get nothing in the log.  There got to be
> something wrong with the loop that I can not see.  I attached the file
> so its not too much code here.  I am pretty sure the problem is in the
> update(0) starts at line 210.
> 



I have 2 concerns.
1. Based on your results, I doubt if you can call to_string more than once
2. I don't think it's a good idea to log the pickle from the 
DataEvent.__init__. 
You should do all logging from update or display_data. 

Look at my most recent email it gives better code. If you really want
to store the pickle data then write pickled_dict to file and remove
the logging code from the DataEvent.__init__. I suspect that will give you
the results you want again.


~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] looking for volunteers with testing simple python program

2013-06-25 Thread Walter Prins
Hi Alexander


On 23 June 2013 22:46, Alexander  wrote:

> I guess this is for testing, but I have a question. If somebody sends you
> their .pub file (email or otherwise over internet), and a villainous third
> party intercepts that .pub file, will they be able to decrypt the data sent
> over this program?


While I've not looked at the actual program, it appears to use standard
public key encryption techniques.  The way public key encryption works is
essentially that entities always have a public and a private key.  The
public keys are always published and freely available, and are used to
*encrypt* messages for given individuals.  Keys are essentially one-way,
which means you cannot de-crypt a message encrypted with the same key it
was encrypted with.  Instead, only the received with the corresponding
private key can decrypt the encrypted message.

Hence, to answer you question:  If a villainous third party intercepts the
pub key, that doesn't help them in decrypting messages encrypted with that
key.  At best, they can also send you encrypted messages. If they wanted to
decrypt messages meant for you they'd have to somehow gain access to your
private key.

Regards

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


Re: [Tutor] Need help printing a pickled data

2013-06-25 Thread Matt D
On 06/25/2013 01:54 PM, Alan Gauld wrote:
> On 25/06/13 17:32, Matt D wrote:
> 
>>  self.data = data
>>  with open('mypicklelog.txt','ab') as log: # open in binary mode
>>  pickle.dump(self.data, log) # serialize data and write to
>> file
>>
>>
>> And I still get nothing.
> 
> Define 'nothing'.
> 
> Does the file exist?
> Does it have anything in it?
> Does self.data exist - what does it look like if you print it?
> Are there any error messages?
> 
Yeh nothing as in an empty file.  The file is there but there is nothing
written in it.  self.data exists.  that the problem if have is printing
what is in it so i dont know what it looks like. No error messages
currently in the terminal.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help printing a pickled data

2013-06-25 Thread Alan Gauld

On 25/06/13 19:30, Matt D wrote:


Does the file exist?
Does it have anything in it?
Does self.data exist - what does it look like if you print it?
Are there any error messages?


Yeh nothing as in an empty file.  The file is there but there is nothing
written in it.


OK, Try deleting the file and rerunning the program.
Check that the file is recreated and is still empty.


self.data exists.  that the problem if have is printing
what is in it so i dont know what it looks like.


Try

print repr(self.data)

See if that helps.

--
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] Need help printing a pickled data

2013-06-25 Thread Matt D

> 
> The real question is why do you want this pickle in a file?  I am not sure 
> it will be easy to pull out and reuse anyway. Given your experience level, 
> I think this is a lot of work for something that you are unlikely to be able 
> to easily use. I think it would be more useful to `log.write(repr(attrs))`.
> 
> Once you have the bytes of pickled data, just write that to file.
> 
> with open('mypicklelog.txt','ab') as log: 
> # All pickles will run together because there is no spacing.
> log.write(self.data.to_string()) 
> 
> 
> Again, I think you would be better off with the repr()
> with open('mypicklelog.txt','ab') as log: 
> log.write(repr(pickle.loads(self.data.to_string(
> 
> 

OK thanks.  what i did is went back to the old logger where everything
was working.  and then i put

self.update(attrs)
with open('mypicklelog.txt','ab') as log:
log.write(repr(attrs))

to try to lay eyes on the pickle.  What i really need is a way to test
this GUI without waiting so long.  I was thinking if i could get a hold
of one of the c++ maps i could make a small program that would enable me
to send it to the python gui at will; to test the gui.

I am not sure if there is a way around this testing difficulty because
two of the TextCtrl fields are empty.  This is because currently the c++
is only sending in the container the contents of the Header Data Unit
(HDU).  In digital radio the transmission consists of frames, the HDU is
one of them, as the name suggest it is the first.  But the Logical Data
Unit 1 (LDU1) contains the source and destination id data that
ultimately i need to get in the UI.  So I am going to have to get into
the c++ map stuff and get familiar with how this pickle process works
anyway. This is pass/fail thing, it doesn't matter if my code is 'ugly'
or whatever and I have time so it shouldn't be impossible.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] looking for volunteers with testing simple python program

2013-06-25 Thread Alexander
On Tue, Jun 25, 2013 at 2:04 PM, Walter Prins  wrote:

> Hi Alexander
>
>
> On 23 June 2013 22:46, Alexander  wrote:
>
>> I guess this is for testing, but I have a question. If somebody sends you
>> their .pub file (email or otherwise over internet), and a villainous third
>> party intercepts that .pub file, will they be able to decrypt the data sent
>> over this program?
>
>
> While I've not looked at the actual program, it appears to use standard
> public key encryption techniques.  The way public key encryption works is
> essentially that entities always have a public and a private key.  The
> public keys are always published and freely available, and are used to
> *encrypt* messages for given individuals.  Keys are essentially one-way,
> which means you cannot de-crypt a message encrypted with the same key it
> was encrypted with.  Instead, only the received with the corresponding
> private key can decrypt the encrypted message.
>
> Hence, to answer you question:  If a villainous third party intercepts the
> pub key, that doesn't help them in decrypting messages encrypted with that
> key.  At best, they can also send you encrypted messages. If they wanted to
> decrypt messages meant for you they'd have to somehow gain access to your
> private key.
>
> Regards
>
> Walter
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
> Thanks for your response, Walter.

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


Re: [Tutor] Need help printing a pickled data

2013-06-25 Thread Steven D'Aprano

On 26/06/13 04:30, Matt D wrote:

On 06/25/2013 01:54 PM, Alan Gauld wrote:

On 25/06/13 17:32, Matt D wrote:


  self.data = data
  with open('mypicklelog.txt','ab') as log: # open in binary mode
  pickle.dump(self.data, log) # serialize data and write to
file


And I still get nothing.


Define 'nothing'.

Does the file exist?
Does it have anything in it?
Does self.data exist - what does it look like if you print it?
Are there any error messages?


Yeh nothing as in an empty file.  The file is there but there is nothing
written in it.  self.data exists.  that the problem if have is printing
what is in it so i dont know what it looks like. No error messages
currently in the terminal.


The obvious test is to confirm that you can see other output written to the log 
file.

self.data = data
with open('mypicklelog.txt','ab') as log: # open in binary mode
log.write('before\n')
pickle.dump(self.data, log)
log.write('after\n')


I still think it is silly to write pickled data to a log. Logs are for 
human-readable information, not arbitrary data. Even with text-mode pickle, 
it's still junk:

py> import pickle
py> data = {'a': None, 'b': 42}
py> pickle.dumps(data, 0)
"(dp0\nS'a'\np1\nNsS'b'\np2\nI42\ns."


Why do you want to see rubbish like that inside your log file? Surely something 
like this is better?

log.write("data = %r" % data)

which will give you a line like this:

data = {'a': None, 'b': 42}


in your log, which is a million times more understandable than a pickle.


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


Re: [Tutor] Need help printing a pickled data

2013-06-25 Thread Marc Tompkins
On Tue, Jun 25, 2013 at 6:12 PM, Steven D'Aprano wrote:


> Why do you want to see rubbish like that inside your log file? Surely
> something like this is better?
>
> log.write("data = %r" % data)
>
> which will give you a line like this:
>
> data = {'a': None, 'b': 42}
>
>
> in your log, which is a million times more understandable than a pickle.
>

This conversation is starting to remind me of Arlo Guthrie's Motorcycle
Song...
http://www.youtube.com/watch?v=BvLtNBm1yyA

"I don't want a pickle
I just wanna ride on my motorcycle..."
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help printing a pickled data

2013-06-25 Thread Steven D'Aprano

On 26/06/13 03:03, Matt D wrote:


meanwhile can you please take a look at this update() and tell me if you
see something wrong because ever sense I tried using the array for
logging the values from the TextCtrls the program is not updating
meaning when the program receives the c++ map, or the pickle, I get
nothing in the UI and I get nothing in the log.  There got to be
something wrong with the loop that I can not see.  I attached the file
so its not too much code here.


Please don't dump big blobs of code on us, attachment or inline doesn't matter.

Please follow the principles here: http://sscce.org/

1) it will make you a better programmer, and

2) it will make it easier for us to help you.


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