Pickling dictionaries containing dictionaries: failing, recursion-style!

2007-12-01 Thread lysdexia
I'm having great fun playing with Markov chains. I am making a
dictionary of all the words in a given  string, getting a count of how
many appearances word1 makes in the string, getting a list of all the
word2s that follow each appearance of  word1 and a count of how many
times word2 appears in the string as well. (I know I should probably
be only counting how many times word2 actually follows word1, but as I
said, I'm having great fun playing ...)


printed output of the dictionary looks like so:

{'and': [1, {'to': 1}], 'down': [1, {'upon': 1}], 'them': [1, {'down':
1}], 'no': [1, {'others': 1}], 'this': [1, {'it': 1}], 'is': [2, {'a':
2}], 'upon': [1, {'a': 1}], 'it': [2, {'is': 2}], 'think': [2, {'and':
1, 'words': 1}], 'write': [1, {'this': 1}], 'to': [3, {'write': 1,
'put': 1, 'think': 1}], 'words': [1, {'no': 1}], 'others': [1,
{'think': 1}], 'put': [1, {'them': 1}], 'sin': [2, {'to': 2}]}

Here's the actual function.

def assembleVocab(self):
  self.wordDB = {}
  for word in self.words:
try:
  if not word in self.wordDB.keys():
wordsWeights = {}
afterwords =  [self.words[i + 1] for i, e in
enumerate(self.words) if e == word]
for aw in afterwords:
  if not aw in wordsWeights.keys():
wordsWeights[aw] = afterwords.count(aw)
self.wordDB[word] = [self.words.count(word), wordsWeights]
except:
  pass
  out = open("mchain.pkl",'wb')
  pickle.dump(self.wordDB, out, -1)
  out.close()

My problem is, I can't seem to get it to unpickle. When I attempt to
load the
saved data, I get:

AttributeError: 'tuple' object has no attribute 'readline'

with pickle, and

TypeError: argument must have 'read' and 'readline' attributes

Looking at the pickle pages on docs.python.org, I see that I am
indeed
supposed to be able to pickle ``tuples, lists, sets, and dictionaries
containing only picklable objects''.

I'm sure I'm missing something obvious.  Clues?
-- 
http://mail.python.org/mailman/listinfo/python-list


Terminal Emulation Modules?

2006-04-11 Thread lysdexia
 I am attempting to screen scrape SuperDOS, an extremely closed system
that uses wyse 60 terminals to communicate with a dos machine. I have
not been able to communicate properly with superdos until trying the
handy miniterm.py example from the pyserial package in conjunction with
Markus Gutschke's wy60 emulator which translates input through an xterm
into wyse 60 commands. These programs together allow me to interact
with superdos pretty well ... with the exception of the arrow keys: for
those I must fall back to the old cntl-letter combos to get the cursor
to behave (actually cntl-letter x 2.. for some reason it likes an extra
prod). This is fine, as now I have a way to debug my eventual script.

My big problem is,  I am completely unable to get SuperDos to respond
to my carriage returns from within the script!

I can watch the script work through miniterm.py. I have sent the return
and newline characters in various combinations starting with "\n,\r",
"\x0a\x0d", but they respond weirdly, putting the cursor *above* the
existing command line, changing the cursor to an outline and generally
letting me know that I am on the wrong track.

Has some clever human already created a handy module to handle wyse60
and other terminal emulation from within a python program?

I have looked over the curses module, but it seems to be aimed at
drawing proper screens for the user, not translation.

PySerial's ability to suck up the output via readlines() is awesome,
and I can see how I *think* it should all be done, but the charset
weirdness has got me stymied!

I am going to look at Mr. Gutscheke's source to see how he does it, but
I am barely conversant in python and fear that exposure to that much C
code may cause dizziness!

Thanks!

-- 
http://mail.python.org/mailman/listinfo/python-list