Re: reading files into dicts

2006-01-01 Thread Alex Martelli
rbt <[EMAIL PROTECTED]> wrote:
   ...
> Thanks to everyone for the tips on eval and repr. I went with the 
> cPickle suggestion... this is awesome! It was the easiest and quickest
> solution performance-wise. Just makes me think, "Wow... how the heck 
> does pickle do that?!"

pickle.py implements just the same job, in pure Python, and it's easily
found within your Python's standard library, so you may want to study it
to see how it does perform its task.


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


Re: reading files into dicts

2005-12-30 Thread Tim Williams (gmail)
On 30/12/05, Chris F.A. Johnson <[EMAIL PROTECTED]> wrote:
On 2005-12-30, Tim Williams (gmail) wrote:> Apologies for the top post, it was my first attempt at using gmail's> pda-enabled web interface. There is no option to bottom post.Can you not move the cursor?

Nope,  there is a checkbox option to include the original post, if you include it, it appears under your new text. :-) 

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

Re: reading files into dicts

2005-12-30 Thread Fuzzyman
ConfigObj is good - it (effectively) turns a dictionary into an ini
file, and vice versa.

There is also built in support for type conversion.

See http://www.voidspace.org.uk/python/configobj.html

See the ConfigPersist module which has functions to use ConfigObj for
data persistence. It explains the limitations.

http://www.voidspace.org.uk/python/configpersist.html

Basically you can store and retrieve dictionaries, lists, strings,
integers, floats and booleans. You can nest dictionaries - but you
can't nest dictionaries in lists. All the keys must be strings - but
the module/article suggests a way round that, at the expense of
readability of the resulting text file.

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

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


Re: reading files into dicts

2005-12-29 Thread Chris F.A. Johnson
On 2005-12-30, Tim Williams (gmail) wrote:
> Apologies for the top post, it was my first attempt at using gmail's
> pda-enabled web interface. There is no option to bottom post.

Can you not move the cursor?

-- 
   Chris F.A. Johnson, author   |
   Shell Scripting Recipes: |  My code in this post, if any,
   A Problem-Solution Approach  |  is released under the
   2005, Apress | GNU General Public Licence
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: reading files into dicts

2005-12-29 Thread rbt
Gary Herron wrote:
> rbt wrote:
> 
>> What's a good way to write a dictionary out to a file so that it can 
>> be easily read back into a dict later? I've used realines() to read 
>> text files into lists... how can I do the same thing with dicts? 
>> Here's some sample output that I'd like to write to file and then read 
>> back into a dict:
>>
>> {'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994, 
>> '.\\New Text Document.txt': 1135900552}
>>  
>>
> 
> 
> A better way, than rolling your own marshaling (as this is called), 
> would be to use the cPickle module. It can write almost any Python 
> object to a file, and then read it back in later. It's more efficient, 
> and way more general than any code you're likely to write yourself.
> 
> The contents of the file are quite opaque to anything except the cPickle 
> and pickle modules. If you *do* want to roll you own input and output to 
> the file, the standard lib functions "repr" and "eval" can be used. Repr 
> is meant to write out objects so they can be read back in and recovered 
> with eval. If the contents of your dictionary are well behaved enough 
> (simple Python objects are, and classes you create may be made so), then 
> you may be able to get away with as little as this:
> 
> f = file('file.name', 'wb')
> f.write(repr(myDictionary))
> f.close()
> 
> and
> 
> f = file('file.name', 'rb')
> myDictionary = eval(f.read())
> f.close()
> 
> Simple as that is, I'd still recommend the cPickle module.
> 
> As always, this security warning applys: Evaluating arbitrary text 
> allows anyone, who can change that text, to take over complete control 
> of your program. So be carefully.
> 
> Gary Herron
> 
> 

Thanks to everyone for the tips on eval and repr. I went with the 
cPickle suggestion... this is awesome! It was the easiest and quickest 
solution performance-wise. Just makes me think, "Wow... how the heck 
does pickle do that?!"

Thanks again,
rbt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: reading files into dicts

2005-12-29 Thread Tim Williams (gmail)
Apologies for the top post, it was my first attempt at using gmail's
pda-enabled web interface. There is no option to bottom post.

Apart from the mistake in my previous reply, when I meant to suggest
using import instead of eval() not repr(). I also omitted an example.
So here goes -but I don't know how gmail-CE will format this!!

Save your dict to a file  with a .PY extension using

outdata = 'mydict =' + repr(dict) # or somesuch
 similar

dictfile.py
---
mydict = {'key1':'a', 'key2':'b'}
---

Then:

import dictfile
print dictfile.mydict.keys()
>>>['key1','key2']
reload(dictfile)

HTH :-)

--

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


Re: reading files into dicts

2005-12-29 Thread Tim Williams (gmail)
A further thought, if you write the data to a file in the correct
format, you can use import and reload to access the data later instead
of repr.





On 12/29/05, Tim Williams (gmail) <[EMAIL PROTECTED]> wrote:
> On 30/12/05, Giovanni Bajo <[EMAIL PROTECTED]> wrote:
> >
> >
> > >>> d = {'.\\sync_pics.py': 1135900993, '.\\file_history.txt':
> 1135900994,
> >  '.\\New Text Document.txt': 1135900552}
> > >>> file("foo", "w").write(repr(d))
> > >>> data = file("foo").read()
> > >>> data
> > "{'.sync_pics.py': 1135900993, '.file_history.txt': 1135900994,
> > '.New Text Document.txt': 1135900552}"
> > >>> d = eval(data)
> > >>> d
> > {'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994,
> '.\\New
> > Text
> > Document.txt': 1135900552}
> >
> > eval() is risky if you can't control the contents of the file  :)
>
>


--

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


Re: reading files into dicts

2005-12-29 Thread Gary Herron
rbt wrote:

>What's a good way to write a dictionary out to a file so that it can be 
>easily read back into a dict later? I've used realines() to read text 
>files into lists... how can I do the same thing with dicts? Here's some 
>sample output that I'd like to write to file and then read back into a dict:
>
>{'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994, 
>'.\\New Text Document.txt': 1135900552}
>  
>


A better way, than rolling your own marshaling (as this is called), 
would be to use the cPickle module. It can write almost any Python 
object to a file, and then read it back in later. It's more efficient, 
and way more general than any code you're likely to write yourself.

The contents of the file are quite opaque to anything except the cPickle 
and pickle modules. If you *do* want to roll you own input and output to 
the file, the standard lib functions "repr" and "eval" can be used. Repr 
is meant to write out objects so they can be read back in and recovered 
with eval. If the contents of your dictionary are well behaved enough 
(simple Python objects are, and classes you create may be made so), then 
you may be able to get away with as little as this:

f = file('file.name', 'wb')
f.write(repr(myDictionary))
f.close()

and

f = file('file.name', 'rb')
myDictionary = eval(f.read())
f.close()

Simple as that is, I'd still recommend the cPickle module.

As always, this security warning applys: Evaluating arbitrary text 
allows anyone, who can change that text, to take over complete control 
of your program. So be carefully.

Gary Herron


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


Re: reading files into dicts

2005-12-29 Thread limodou
2005/12/30, rbt <[EMAIL PROTECTED]>:
> What's a good way to write a dictionary out to a file so that it can be
> easily read back into a dict later? I've used realines() to read text
> files into lists... how can I do the same thing with dicts? Here's some
> sample output that I'd like to write to file and then read back into a dict:
>
> {'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994,
> '.\\New Text Document.txt': 1135900552}
> --
> http://mail.python.org/mailman/listinfo/python-list
>

You can try the dict4ini module written by me.

http://wiki.woodpecker.org.cn/moin/Dict4Ini

The dict can be saved as an Ini file, and can be read back from the
Ini file.Just like:

 >>> import dict4ini
 >>> x = {'.\\sync_pics.py': 1135900993, '.\\file_history.txt':
1135900994, '.\\New Text Document.txt': 1135900552}
 >>> d = dict4ini.DictIni(values=x)
 >>> d['.\sync_pics.py']
 1135900993

--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: reading files into dicts

2005-12-29 Thread Tim Williams (gmail)
On 30/12/05, Giovanni Bajo <[EMAIL PROTECTED]> wrote:
>>> d = {'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994, '.\\New Text Document.txt': 1135900552}>>> file("foo", "w").write(repr(d))>>> data = ""
>>> data"{'.sync_pics.py': 1135900993, '.file_history.txt': 1135900994,'.New Text Document.txt': 1135900552}">>> d = eval(data)>>> d{'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994, '.\\New Text
Document.txt': 1135900552}eval() is risky if you can't control the contents of the file  :)

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

Re: reading files into dicts

2005-12-29 Thread Giovanni Bajo
rbt wrote:

> What's a good way to write a dictionary out to a file so that it can
> be easily read back into a dict later? I've used realines() to read
> text
> files into lists... how can I do the same thing with dicts? Here's
> some sample output that I'd like to write to file and then read back
> into a dict:
>
> {'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994,
> '.\\New Text Document.txt': 1135900552}



>>> d = {'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994,
... '.\\New Text Document.txt': 1135900552}
>>> file("foo", "w").write(repr(d))
>>> data = file("foo").read()
>>> data
"{'.sync_pics.py': 1135900993, '.file_history.txt': 1135900994,
'.New Text Document.txt': 1135900552}"
>>> d = eval(data)
>>> d
{'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994, '.\\New Text
Document.txt': 1135900552}

-- 
Giovanni Bajo


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


Re: reading files into dicts

2005-12-29 Thread Tim Williams (gmail)
On 30/12/05, rbt <[EMAIL PROTECTED]> wrote:
What's a good way to write a dictionary out to a file so that it can beeasily read back into a dict later? I've used realines() to read textfiles into lists... how can I do the same thing with dicts? Here's some
sample output that I'd like to write to file and then read back into a dict:
Depending on how often you need to read/write/access the dict, 
either the shelve module,  or the pickle / cpickle modules will do
what you need.
 
-- 
http://mail.python.org/mailman/listinfo/python-list

reading files into dicts

2005-12-29 Thread rbt
What's a good way to write a dictionary out to a file so that it can be 
easily read back into a dict later? I've used realines() to read text 
files into lists... how can I do the same thing with dicts? Here's some 
sample output that I'd like to write to file and then read back into a dict:

{'.\\sync_pics.py': 1135900993, '.\\file_history.txt': 1135900994, 
'.\\New Text Document.txt': 1135900552}
-- 
http://mail.python.org/mailman/listinfo/python-list