[google-appengine] Re: How to append to BlobProperty

2009-01-23 Thread Alexander Kojevnikov

db.Blob is a subclass of str. Try this code:


class Storage(db.Model):
blob = db.BlobProperty()

def append(self, value):
self.blob += value


s = Storage()
s.blob = 'abc'
s.append('def')
s.put()

On Jan 24, 3:20 am, Will  wrote:
> Hi all,
>
> I'd like to append a byte string to a db.BlobProperty, but can't figure out
> how. For example,
>
> class Storage(db.Model)
>     blob = db.BlobProperty()
>
> def append(s, ext)
>     s.blob += ext                # want something like this
>
> I've tried to construct a db.Blob, like this:
>     tmp = db.Blob(s.blob)
>
> but failed because db.Blob's constructor only takes a str object.
>
> Any ideas? Thanks in advance.
>
> Will
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How to append to BlobProperty

2009-01-25 Thread Will
So what you suggest is to define an "append" function as a _member_ of the
Storage, then it should work. I will try that and report back, but why it
should work?

I am new to both Python and GAE, so excuse me for any naive questions.

Thanks,

Will

On Sat, Jan 24, 2009 at 10:09 AM, Alexander Kojevnikov <
alexan...@kojevnikov.com> wrote:

>
> db.Blob is a subclass of str. Try this code:
>
>
> class Storage(db.Model):
>blob = db.BlobProperty()
>
> def append(self, value):
>self.blob += value
>
>
> s = Storage()
> s.blob = 'abc'
> s.append('def')
> s.put()
>
> On Jan 24, 3:20 am, Will  wrote:
> > Hi all,
> >
> > I'd like to append a byte string to a db.BlobProperty, but can't figure
> out
> > how. For example,
> >
> > class Storage(db.Model)
> > blob = db.BlobProperty()
> >
> > def append(s, ext)
> > s.blob += ext# want something like this
> >
> > I've tried to construct a db.Blob, like this:
> > tmp = db.Blob(s.blob)
> >
> > but failed because db.Blob's constructor only takes a str object.
> >
> > Any ideas? Thanks in advance.
> >
> > Will
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How to append to BlobProperty

2009-01-25 Thread Will
No, doesn't work. I got

unsupported operand type(s) for +=: 'BlobProperty' and 'str'

Same as before.

On Mon, Jan 26, 2009 at 10:15 AM, Will  wrote:

> So what you suggest is to define an "append" function as a _member_ of the
> Storage, then it should work. I will try that and report back, but why it
> should work?
>
> I am new to both Python and GAE, so excuse me for any naive questions.
>
> Thanks,
>
> Will
>
>
> On Sat, Jan 24, 2009 at 10:09 AM, Alexander Kojevnikov <
> alexan...@kojevnikov.com> wrote:
>
>>
>> db.Blob is a subclass of str. Try this code:
>>
>>
>> class Storage(db.Model):
>>blob = db.BlobProperty()
>>
>> def append(self, value):
>>self.blob += value
>>
>>
>> s = Storage()
>> s.blob = 'abc'
>> s.append('def')
>> s.put()
>>
>> On Jan 24, 3:20 am, Will  wrote:
>> > Hi all,
>> >
>> > I'd like to append a byte string to a db.BlobProperty, but can't figure
>> out
>> > how. For example,
>> >
>> > class Storage(db.Model)
>> > blob = db.BlobProperty()
>> >
>> > def append(s, ext)
>> > s.blob += ext# want something like this
>> >
>> > I've tried to construct a db.Blob, like this:
>> > tmp = db.Blob(s.blob)
>> >
>> > but failed because db.Blob's constructor only takes a str object.
>> >
>> > Any ideas? Thanks in advance.
>> >
>> > Will
>> >>
>>
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How to append to BlobProperty

2009-01-26 Thread Alexander Kojevnikov

> No, doesn't work. I got
>
> unsupported operand type(s) for +=: 'BlobProperty' and 'str'
>
> Same as before.
>
Could you post here all related code, including the construction of a
Storage entity?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How to append to BlobProperty

2009-01-27 Thread Will
Here you go,

--
class UploadStorage(db.Model):
def __init__(self, field_name, file_name, content_type, content_length,
charset):
self.content = db.BlobProperty(required=True)
self.last_change = db.DateTimeProperty(required=True, auto_now=True)
self.field_name = db.StringProperty(required=True, default=u'')
..

if (field_name):
self.field_name = field_name

def append(self, src):
self.content += src
-
class GAEUploadedFile(UploadedFile):
def __init__(self, field_name, file_name, content_type, content_length,
charset):
self.__storage = UploadStorage(field_name, file_name, content_type,
content_length, charset)
..

 def write(self, content):   # append
self.__storage.append(content)
self.size = len(self.__storage.content)

Thanks,

Will

On Tue, Jan 27, 2009 at 7:47 AM, Alexander Kojevnikov <
alexan...@kojevnikov.com> wrote:

>
> > No, doesn't work. I got
> >
> > unsupported operand type(s) for +=: 'BlobProperty' and 'str'
> >
> > Same as before.
> >
> Could you post here all related code, including the construction of a
> Storage entity?
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How to append to BlobProperty

2009-01-27 Thread Alexander Kojevnikov

Rewrite your UploadStorage class to declare the module properties in
the class body, not in the __init__ method. App Engine forwards access
to property definitions to the actual property values, creation of the
properties in __init__ is probably not compatible with it.

For example, when you access self.content in your append() method, GAE
returns the value of the append property, not the property definition.

Try this code:

class UploadStorage(db.Model):
content = db.BlobProperty(required=True)
last_change = db.DateTimeProperty(required=True, auto_now=True)
field_name = db.StringProperty(required=True, default=u'')
..

def __init__(self, field_name, file_name, content_type,
content_length, charset):
if field_name:
self.field_name = field_name

def append(self, src):
self.content += src


On Jan 27, 9:25 pm, Will  wrote:
> Here you go,
>
> --
> class UploadStorage(db.Model):
>     def __init__(self, field_name, file_name, content_type, content_length,
> charset):
>         self.content = db.BlobProperty(required=True)
>         self.last_change = db.DateTimeProperty(required=True, auto_now=True)
>         self.field_name = db.StringProperty(required=True, default=u'')
>         ..
>
>         if (field_name):
>             self.field_name = field_name
>
>     def append(self, src):
>         self.content += src
> -
> class GAEUploadedFile(UploadedFile):
>     def __init__(self, field_name, file_name, content_type, content_length,
> charset):
>         self.__storage = UploadStorage(field_name, file_name, content_type,
> content_length, charset)
>         ..
>
>      def write(self, content):   # append
>         self.__storage.append(content)
>         self.size = len(self.__storage.content)
>
> Thanks,
>
> Will
>
> On Tue, Jan 27, 2009 at 7:47 AM, Alexander Kojevnikov <
>
> alexan...@kojevnikov.com> wrote:
>
> > > No, doesn't work. I got
>
> > > unsupported operand type(s) for +=: 'BlobProperty' and 'str'
>
> > > Same as before.
>
> > Could you post here all related code, including the construction of a
> > Storage entity?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How to append to BlobProperty

2009-01-27 Thread Will
I rewrote it, now the error message became

unsupported operand type(s) for +=: 'NoneType' and 'str'

pointing at

self.content += src

Seems *content* was not constructed in modified __init__. If I added

self.content = db.BlobProperty(required=True)

into __init__ while keeping the definition in the class body, I got

Property content must be convertible to a Blob instance (Blob() argument
should be str instance, not BlobProperty)

Seems this time* content* was constructed but in __init__() somehow it
wanted to initialize it by a Blob instance, not BlobProperty?!

Will

On Tue, Jan 27, 2009 at 6:43 PM, Alexander Kojevnikov <
alexan...@kojevnikov.com> wrote:

>
> Rewrite your UploadStorage class to declare the module properties in
> the class body, not in the __init__ method. App Engine forwards access
> to property definitions to the actual property values, creation of the
> properties in __init__ is probably not compatible with it.
>
> For example, when you access self.content in your append() method, GAE
> returns the value of the append property, not the property definition.
>
> Try this code:
>
> class UploadStorage(db.Model):
> content = db.BlobProperty(required=True)
> last_change = db.DateTimeProperty(required=True, auto_now=True)
> field_name = db.StringProperty(required=True, default=u'')
>..
>
> def __init__(self, field_name, file_name, content_type,
> content_length, charset):
> if field_name:
>self.field_name = field_name
>
>def append(self, src):
>self.content += src
>
>
> On Jan 27, 9:25 pm, Will  wrote:
> > Here you go,
> >
> > --
> > class UploadStorage(db.Model):
> > def __init__(self, field_name, file_name, content_type,
> content_length,
> > charset):
> > self.content = db.BlobProperty(required=True)
> > self.last_change = db.DateTimeProperty(required=True,
> auto_now=True)
> > self.field_name = db.StringProperty(required=True, default=u'')
> > ..
> >
> > if (field_name):
> > self.field_name = field_name
> >
> > def append(self, src):
> > self.content += src
> > -
> > class GAEUploadedFile(UploadedFile):
> > def __init__(self, field_name, file_name, content_type,
> content_length,
> > charset):
> > self.__storage = UploadStorage(field_name, file_name,
> content_type,
> > content_length, charset)
> > ..
> >
> >  def write(self, content):   # append
> > self.__storage.append(content)
> > self.size = len(self.__storage.content)
> >
> > Thanks,
> >
> > Will
> >
> > On Tue, Jan 27, 2009 at 7:47 AM, Alexander Kojevnikov <
> >
> > alexan...@kojevnikov.com> wrote:
> >
> > > > No, doesn't work. I got
> >
> > > > unsupported operand type(s) for +=: 'BlobProperty' and 'str'
> >
> > > > Same as before.
> >
> > > Could you post here all related code, including the construction of a
> > > Storage entity?
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How to append to BlobProperty

2009-01-27 Thread Alexander Kojevnikov

When creating an UploadStorage entity, you should initialise the
content property:

my_entity = UploadStorage(content='')

or:

my_entity = UploadStorage()
my_entity.content = '' # or whatever your initial value is

I know, it a bit confusing. When you define the property in the class,
UploadStorage.content is an instance of db.BlobProperty.

But when you access it after the entity is created, it returns the
actual value of the property, which is of db.Blob type (subclass of
str).


On Jan 27, 10:13 pm, Will  wrote:
> I rewrote it, now the error message became
>
> unsupported operand type(s) for +=: 'NoneType' and 'str'
>
> pointing at
>
> self.content += src
>
> Seems *content* was not constructed in modified __init__. If I added
>
>     self.content = db.BlobProperty(required=True)
>
> into __init__ while keeping the definition in the class body, I got
>
> Property content must be convertible to a Blob instance (Blob() argument
> should be str instance, not BlobProperty)
>
> Seems this time* content* was constructed but in __init__() somehow it
> wanted to initialize it by a Blob instance, not BlobProperty?!
>
> Will
>
> On Tue, Jan 27, 2009 at 6:43 PM, Alexander Kojevnikov <
>
> alexan...@kojevnikov.com> wrote:
>
> > Rewrite your UploadStorage class to declare the module properties in
> > the class body, not in the __init__ method. App Engine forwards access
> > to property definitions to the actual property values, creation of the
> > properties in __init__ is probably not compatible with it.
>
> > For example, when you access self.content in your append() method, GAE
> > returns the value of the append property, not the property definition.
>
> > Try this code:
>
> > class UploadStorage(db.Model):
> >     content = db.BlobProperty(required=True)
> >     last_change = db.DateTimeProperty(required=True, auto_now=True)
> >     field_name = db.StringProperty(required=True, default=u'')
> >    ..
>
> >     def __init__(self, field_name, file_name, content_type,
> > content_length, charset):
> >         if field_name:
> >            self.field_name = field_name
>
> >    def append(self, src):
> >        self.content += src
>
> > On Jan 27, 9:25 pm, Will  wrote:
> > > Here you go,
>
> > > --
> > > class UploadStorage(db.Model):
> > >     def __init__(self, field_name, file_name, content_type,
> > content_length,
> > > charset):
> > >         self.content = db.BlobProperty(required=True)
> > >         self.last_change = db.DateTimeProperty(required=True,
> > auto_now=True)
> > >         self.field_name = db.StringProperty(required=True, default=u'')
> > >         ..
>
> > >         if (field_name):
> > >             self.field_name = field_name
>
> > >     def append(self, src):
> > >         self.content += src
> > > -
> > > class GAEUploadedFile(UploadedFile):
> > >     def __init__(self, field_name, file_name, content_type,
> > content_length,
> > > charset):
> > >         self.__storage = UploadStorage(field_name, file_name,
> > content_type,
> > > content_length, charset)
> > >         ..
>
> > >      def write(self, content):   # append
> > >         self.__storage.append(content)
> > >         self.size = len(self.__storage.content)
>
> > > Thanks,
>
> > > Will
>
> > > On Tue, Jan 27, 2009 at 7:47 AM, Alexander Kojevnikov <
>
> > > alexan...@kojevnikov.com> wrote:
>
> > > > > No, doesn't work. I got
>
> > > > > unsupported operand type(s) for +=: 'BlobProperty' and 'str'
>
> > > > > Same as before.
>
> > > > Could you post here all related code, including the construction of a
> > > > Storage entity?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How to append to BlobProperty

2009-01-27 Thread TLH

from google.appengine.ext import db

class Foo(db.Model):
  b = db.BlobProperty()
  def butWhy(self, s):
self.b = self.b + s

f = Foo(
  b = "Append to a Blob?"

)

f.butWhy(" You can, but why?")

print f.b  # "Append to a Blob? You can, but why?"

I suspect you should be using a Text property.





On Jan 23, 11:20 am, Will  wrote:
> Hi all,
>
> I'd like to append a byte string to a db.BlobProperty, but can't figure out
> how. For example,

> Any ideas? Thanks in advance.
>
> Will
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How to append to BlobProperty

2009-02-01 Thread Will
Thanks, it finally worked. Following your suggestion, what I did is:

1. in class body, define:
 content = db.BlobProperty(required=False)# notice required
is set to False

2. in __init__(arg1, arg2, ...):
 self.content = ''

Then
   self.content += src

doesn't complain anymore.

When accessing, it returns a db.Blog doesn't sound too foreign to me. For
now, I just think it as a customized type caster in C++.

But I am facing another problem now, I will post it in a new thread
'Different behavior in Debug run'.

Regards,

Will

On Tue, Jan 27, 2009 at 8:31 PM, Alexander Kojevnikov <
alexan...@kojevnikov.com> wrote:

>
> When creating an UploadStorage entity, you should initialise the
> content property:
>
> my_entity = UploadStorage(content='')
>
> or:
>
> my_entity = UploadStorage()
> my_entity.content = '' # or whatever your initial value is
>
> I know, it a bit confusing. When you define the property in the class,
> UploadStorage.content is an instance of db.BlobProperty.
>
> But when you access it after the entity is created, it returns the
> actual value of the property, which is of db.Blob type (subclass of
> str).
>
>
> On Jan 27, 10:13 pm, Will  wrote:
> > I rewrote it, now the error message became
> >
> > unsupported operand type(s) for +=: 'NoneType' and 'str'
> >
> > pointing at
> >
> > self.content += src
> >
> > Seems *content* was not constructed in modified __init__. If I added
> >
> > self.content = db.BlobProperty(required=True)
> >
> > into __init__ while keeping the definition in the class body, I got
> >
> > Property content must be convertible to a Blob instance (Blob() argument
> > should be str instance, not BlobProperty)
> >
> > Seems this time* content* was constructed but in __init__() somehow it
> > wanted to initialize it by a Blob instance, not BlobProperty?!
> >
> > Will
> >
> > On Tue, Jan 27, 2009 at 6:43 PM, Alexander Kojevnikov <
> >
> > alexan...@kojevnikov.com> wrote:
> >
> > > Rewrite your UploadStorage class to declare the module properties in
> > > the class body, not in the __init__ method. App Engine forwards access
> > > to property definitions to the actual property values, creation of the
> > > properties in __init__ is probably not compatible with it.
> >
> > > For example, when you access self.content in your append() method, GAE
> > > returns the value of the append property, not the property definition.
> >
> > > Try this code:
> >
> > > class UploadStorage(db.Model):
> > > content = db.BlobProperty(required=True)
> > > last_change = db.DateTimeProperty(required=True, auto_now=True)
> > > field_name = db.StringProperty(required=True, default=u'')
> > >..
> >
> > > def __init__(self, field_name, file_name, content_type,
> > > content_length, charset):
> > > if field_name:
> > >self.field_name = field_name
> >
> > >def append(self, src):
> > >self.content += src
> >
> > > On Jan 27, 9:25 pm, Will  wrote:
> > > > Here you go,
> >
> > > > --
> > > > class UploadStorage(db.Model):
> > > > def __init__(self, field_name, file_name, content_type,
> > > content_length,
> > > > charset):
> > > > self.content = db.BlobProperty(required=True)
> > > > self.last_change = db.DateTimeProperty(required=True,
> > > auto_now=True)
> > > > self.field_name = db.StringProperty(required=True,
> default=u'')
> > > > ..
> >
> > > > if (field_name):
> > > > self.field_name = field_name
> >
> > > > def append(self, src):
> > > > self.content += src
> > > > -
> > > > class GAEUploadedFile(UploadedFile):
> > > > def __init__(self, field_name, file_name, content_type,
> > > content_length,
> > > > charset):
> > > > self.__storage = UploadStorage(field_name, file_name,
> > > content_type,
> > > > content_length, charset)
> > > > ..
> >
> > > >  def write(self, content):   # append
> > > > self.__storage.append(content)
> > > > self.size = len(self.__storage.content)
> >
> > > > Thanks,
> >
> > > > Will
> >
> > > > On Tue, Jan 27, 2009 at 7:47 AM, Alexander Kojevnikov <
> >
> > > > alexan...@kojevnikov.com> wrote:
> >
> > > > > > No, doesn't work. I got
> >
> > > > > > unsupported operand type(s) for +=: 'BlobProperty' and 'str'
> >
> > > > > > Same as before.
> >
> > > > > Could you post here all related code, including the construction of
> a
> > > > > Storage entity?
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---

[google-appengine] Re: How to append to BlobProperty

2009-02-01 Thread Will
Hi TLH,

I'm writing a FileUploadHandler that saves files into a Blob, hence I'm
using BlobProperty.

Will

On Tue, Jan 27, 2009 at 8:56 PM, TLH  wrote:

>
> from google.appengine.ext import db
>
> class Foo(db.Model):
>  b = db.BlobProperty()
>  def butWhy(self, s):
>self.b = self.b + s
>
> f = Foo(
>  b = "Append to a Blob?"
>
> )
>
> f.butWhy(" You can, but why?")
>
> print f.b  # "Append to a Blob? You can, but why?"
>
> I suspect you should be using a Text property.
>
>
>
>
>
> On Jan 23, 11:20 am, Will  wrote:
> > Hi all,
> >
> > I'd like to append a byte string to a db.BlobProperty, but can't figure
> out
> > how. For example,
>
> > Any ideas? Thanks in advance.
> >
> > Will
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How to append to BlobProperty

2009-02-01 Thread Alexander Kojevnikov

On Feb 2, 6:02 pm, Will  wrote:
> Thanks, it finally worked. Following your suggestion, what I did is:
>
> 1. in class body, define:
>      content = db.BlobProperty(required=False)            # notice required
> is set to False
>
> 2. in __init__(arg1, arg2, ...):
>      self.content = ''
>
> Then
>    self.content += src
>
> doesn't complain anymore.
>
> When accessing, it returns a db.Blog doesn't sound too foreign to me. For
> now, I just think it as a customized type caster in C++.
>
You can also use the 'default' parameter instead of initialising the
'content' value in the constructor:

content = db.BlobProperty(default='')

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---