Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #66090 > unrolled thread
| Started by | Nir <nirchernia@gmail.com> |
|---|---|
| First post | 2014-02-12 13:18 -0800 |
| Last post | 2014-02-12 14:25 -0800 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.python
Simple Object assignment giving me errors Nir <nirchernia@gmail.com> - 2014-02-12 13:18 -0800
Re: Simple Object assignment giving me errors Chris Angelico <rosuav@gmail.com> - 2014-02-13 08:26 +1100
Re:Simple Object assignment giving me errors Dave Angel <davea@davea.name> - 2014-02-12 16:41 -0500
Re: Simple Object assignment giving me errors Nir <nirchernia@gmail.com> - 2014-02-12 13:42 -0800
Re: Simple Object assignment giving me errors Chris Angelico <rosuav@gmail.com> - 2014-02-13 08:48 +1100
Re: Simple Object assignment giving me errors Jerry Hill <malaclypse2@gmail.com> - 2014-02-12 17:09 -0500
Re: Simple Object assignment giving me errors Nir <nirchernia@gmail.com> - 2014-02-12 14:25 -0800
| From | Nir <nirchernia@gmail.com> |
|---|---|
| Date | 2014-02-12 13:18 -0800 |
| Subject | Simple Object assignment giving me errors |
| Message-ID | <d7d2865e-4184-4c05-b1bd-b4f16547b2f2@googlegroups.com> |
This is from the book 'dive into python'. I am trying to define jeez as being an instance of FileInfo.
class UserDict(object):
def __init__(self, dict = None):
self.data = {}
if dict is not None: self.update(dict)
class FileInfo(UserDict):
def __init__(self, filename=None):
UserDict.__init__(self)
self["name"] = filename
jeez = FileInfo("yo")
I get a TypeError: 'FileInfo' object doesn't support item assignment .
Am I missing something?
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-02-13 08:26 +1100 |
| Message-ID | <mailman.6790.1392240405.18130.python-list@python.org> |
| In reply to | #66090 |
On Thu, Feb 13, 2014 at 8:18 AM, Nir <nirchernia@gmail.com> wrote: > class FileInfo(UserDict): > def __init__(self, filename=None): > UserDict.__init__(self) > self["name"] = filename > > I get a TypeError: 'FileInfo' object doesn't support item assignment . > > Am I missing something? You can't use square-brackets notation like that, unless you've written your class specifically to handle it. More likely, what you want is one of: self.name = filename self.dict["name"] = filename Also, the same problem will occur with the UserDict, which tries to update itself rather than its dict. Actually, a simpler solution might be to have UserDict inherit from dict. I'm not sure what you're trying to achieve here; more detail would help. But if UserDict really is a dict, then you can call self.update, and you can use square-brackets item assignment. I've no idea what you'd gain over just using a dict though. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-02-12 16:41 -0500 |
| Message-ID | <mailman.6794.1392241090.18130.python-list@python.org> |
| In reply to | #66090 |
Nir <nirchernia@gmail.com> Wrote in message:
> This is from the book 'dive into python'. I am trying to define jeez as being an instance of FileInfo.
>
> class UserDict(object):
> def __init__(self, dict = None):
> self.data = {}
> if dict is not None: self.update(dict)
>
> class FileInfo(UserDict):
> def __init__(self, filename=None):
> UserDict.__init__(self)
> self["name"] = filename
>
>
> jeez = FileInfo("yo")
>
>
>
>
> I get a TypeError: 'FileInfo' object doesn't support item assignment .
>
> Am I missing something?
>
Yes, you're missing the rest of the error message. Show the
whole thing, including the stack trace, and I'm sure it'll be
clear that the error happened long before jeez was involved.
I figure that the line in error is
self["name"] = filename
and that what you really need is
self.data ["name"] = filename
You also have a similar problem on the last line of the first class.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Nir <nirchernia@gmail.com> |
|---|---|
| Date | 2014-02-12 13:42 -0800 |
| Message-ID | <5697d584-28ba-44e6-a60a-cb5ff9ae19aa@googlegroups.com> |
| In reply to | #66090 |
Those two classes are from this code here(pasted below). Quite frankly, I don't understand this code.
Also, UserDict is a built in module. I just typed it out so as to give reference or any clue as to why I cant instantiate jeez.
==========================================================================
"""Framework for getting filetype-specific metadata.
Instantiate appropriate class with filename. Returned object acts like a
dictionary, with key-value pairs for each piece of metadata.
import fileinfo
info = fileinfo.MP3FileInfo("/music/ap/mahadeva.mp3")
print "\\n".join(["%s=%s" % (k, v) for k, v in info.items()])
Or use listDirectory function to get info on all files in a directory.
for info in fileinfo.listDirectory("/music/ap/", [".mp3"]):
...
Framework can be extended by adding classes for particular file types, e.g.
HTMLFileInfo, MPGFileInfo, DOCFileInfo. Each class is completely responsible for
parsing its files appropriately; see MP3FileInfo for example.
This program is part of "Dive Into Python", a free Python book for
experienced programmers. Visit http://diveintopython.org/ for the
latest version.
"""
__author__ = "Mark Pilgrim (mark@diveintopython.org)"
__version__ = "$Revision: 1.3 $"
__date__ = "$Date: 2004/05/05 21:57:19 $"
__copyright__ = "Copyright (c) 2001 Mark Pilgrim"
__license__ = "Python"
import os
import sys
from UserDict import UserDict
def stripnulls(data):
"strip whitespace and nulls"
return data.replace("\00", " ").strip()
class FileInfo(UserDict):
"store file metadata"
def __init__(self, filename=None):
UserDict.__init__(self)
self["name"] = filename
class MP3FileInfo(FileInfo):
"store ID3v1.0 MP3 tags"
tagDataMap = {"title" : ( 3, 33, stripnulls),
"artist" : ( 33, 63, stripnulls),
"album" : ( 63, 93, stripnulls),
"year" : ( 93, 97, stripnulls),
"comment" : ( 97, 126, stripnulls),
"genre" : (127, 128, ord)}
def __parse(self, filename):
"parse ID3v1.0 tags from MP3 file"
self.clear()
try:
fsock = open(filename, "rb", 0)
try:
fsock.seek(-128, 2)
tagdata = fsock.read(128)
finally:
fsock.close()
if tagdata[:3] == 'TAG':
for tag, (start, end, parseFunc) in self.tagDataMap.items():
self[tag] = parseFunc(tagdata[start:end])
except IOError:
pass
def __setitem__(self, key, item):
if key == "name" and item:
self.__parse(item)
FileInfo.__setitem__(self, key, item)
def listDirectory(directory, fileExtList):
"get list of file info objects for files of particular extensions"
fileList = [os.path.normcase(f) for f in os.listdir(directory)]
fileList = [os.path.join(directory, f) for f in fileList \
if os.path.splitext(f)[1] in fileExtList]
def getFileInfoClass(filename, module=sys.modules[FileInfo.__module__]):
"get file info class from filename extension"
subclass = "%sFileInfo" % os.path.splitext(filename)[1].upper()[1:]
return hasattr(module, subclass) and getattr(module, subclass) or FileInfo
return [getFileInfoClass(f)(f) for f in fileList]
if __name__ == "__main__":
for info in listDirectory("/music/_singles/", [".mp3"]):
print "\n".join(["%s=%s" % (k, v) for k, v in info.items()])
print
========================================================================
If this makes sense to you, great. I am trying to break it down so that I can make sense of it. As you mentioned self["name"] = filename doesn't work unless I built a class to handle it. I guess my question then, is how is the class handling it in this code? If you can show me by stripping it down to the bare minimum or write an example that would be awesome.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-02-13 08:48 +1100 |
| Message-ID | <mailman.6796.1392241714.18130.python-list@python.org> |
| In reply to | #66099 |
On Thu, Feb 13, 2014 at 8:42 AM, Nir <nirchernia@gmail.com> wrote: > Also, UserDict is a built in module. I just typed it out so as to give reference or any clue as to why I cant instantiate jeez. Recommendation for next time: Don't type it out, copy and paste it. Show the actual code you ran, and the actual error message. As a debugging/exploration technique, btw, copying in the definition of something from the standard library is often useful. You can then cut it down to the barest minimum that does what you want, and post that. But if you're using UserDict exactly as it is in the stdlib, you can simply show your import statement and we'll know what you mean. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Jerry Hill <malaclypse2@gmail.com> |
|---|---|
| Date | 2014-02-12 17:09 -0500 |
| Message-ID | <mailman.6799.1392242945.18130.python-list@python.org> |
| In reply to | #66099 |
On Wed, Feb 12, 2014 at 4:42 PM, Nir <nirchernia@gmail.com> wrote:
> If this makes sense to you, great. I am trying to break it down so that I can make sense of it. As you mentioned self["name"] = filename doesn't work unless I built a class to handle it. I guess my question then, is how is the class handling it in this code? If you can show me by stripping it down to the bare minimum or write an example that would be awesome.
This example works because the UserDict object being used in this code
was built to handle it. The UserDict class in the standard library
does a lot more than the little snippet you had in your original code.
Your original code would work if you did the same thing, like this:
from collections import UserDict
class FileInfo(UserDict):
def __init__(self, filename=None):
UserDict.__init__(self)
self["name"] = filename
jeez = FileInfo("yo")
(If you're using Python 2, then the first line should be "from
UserDict import UserDict" instead).
You can take a look at the source code for the userDict class here:
http://hg.python.org/cpython/file/3.3/Lib/collections/__init__.py#l862
. In that class, the code responsible for handling the bracketed name
lookup (i.e., self["name"]), is in the __getitem__ and __setitem__
methods (and peripherally in __delitem__, __len__ and __contains__)
--
Jerry
[toc] | [prev] | [next] | [standalone]
| From | Nir <nirchernia@gmail.com> |
|---|---|
| Date | 2014-02-12 14:25 -0800 |
| Message-ID | <d980154a-cbe8-46c2-b219-7aa0b4fac043@googlegroups.com> |
| In reply to | #66090 |
Thank you Jerry. And thank you to the rest of you. You all have been tremendously helpful. PS Chris, next time I will do just that.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web