Re: [sqlalchemy] Tea Rota

2012-01-13 Thread Ian Kelly
On Thu, Jan 12, 2012 at 3:14 PM, Calum MacLeod pan...@sky.com wrote:
     if date not in duties:
     duty = Duty(date=date)
     duties[date] = duty

     if name not in volunteers:
     volunteer = Volunteer(fore=fore, surn=surn, name=name)
     volunteers[name] = volunteer
     volunteer.duties.append(duty)

If the date or the volunteer are already in the respective dicts, you
never assign the 'duty' or 'volunteer' variable.  Thus it would retain
its value from the last iteration.  This is probably why you seem to
be missing volunteers -- some of your dict entries are getting
clobbered with the wrong volunteers.

Cheers,
Ian

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



Re: [sqlalchemy] Tea Rota

2012-01-13 Thread Calum MacLeod

Thanks, Ian,

I appreciate your advice and have removed the first conditional (if name 
not in duties).


Have now changed that to:

duty = Duty(date=date)
duties[date] = duty

if name not in volunteers:
volunteer = Volunteer(fore=fore, surn=surn, name=name)
volunteers[name] = volunteer
volunteer.duties.append(duty)

which gives me all the volunteers and their correct last date of duty.
How can I include all the previous dates for each volunteer?

Basically, I do not know what to do if the volunteer is already in the 
dictionary.
I presume that I put an else: after the last line of the code snippet 
but what do I do then?
It seems that currently I am overwriting the data as it comes in, so 
that only the last is shown.


I feel that I am so near and yet so far.

Thanks,

Calum

On 13/01/12 08:15, Ian Kelly wrote:

On Thu, Jan 12, 2012 at 3:14 PM, Calum MacLeodpan...@sky.com  wrote:

 if date not in duties:
 duty = Duty(date=date)
 duties[date] = duty

 if name not in volunteers:
 volunteer = Volunteer(fore=fore, surn=surn, name=name)
 volunteers[name] = volunteer
 volunteer.duties.append(duty)

If the date or the volunteer are already in the respective dicts, you
never assign the 'duty' or 'volunteer' variable.  Thus it would retain
its value from the last iteration.  This is probably why you seem to
be missing volunteers -- some of your dict entries are getting
clobbered with the wrong volunteers.

Cheers,
Ian



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



Re: [sqlalchemy] Tea Rota

2012-01-13 Thread werner

Hi Calum,

On 01/13/2012 01:31 PM, Calum MacLeod wrote:

Thanks, Ian,

I appreciate your advice and have removed the first conditional (if 
name not in duties).


Have now changed that to:

duty = Duty(date=date)
duties[date] = duty

if name not in volunteers:
volunteer = Volunteer(fore=fore, surn=surn, name=name)
volunteers[name] = volunteer
volunteer.duties.append(duty)
Another hobbiest here (so take the following with a grain of salt), I 
think you would want this:


else:
volunteers[name].duties.append(duty)

Or

if name not in volunteers:
volunteer = Volunteer(fore=fore, surn=surn, name=name)
volunteers[name] = volunteer

volunteers[name].duties.append(duty)


Would also change your Volunteer definition from:

class Volunteer(Base):
__tablename__ = 'volunteers'
id = Column(Integer, primary_key=True)
fore = Column(String)
surn = Column(String)
name = Column(String)
dates = association_proxy('duties', 'date')

to:

class Volunteer(Base):
__tablename__ = 'volunteers'
id = Column(Integer, primary_key=True)
fore = Column(String)
surn = Column(String)
dates = association_proxy('duties', 'date')

  # read only columns
@hybrid_property
def name(self):
return self.fore + ', ' + self.surn

I.e. don't store the same information in two different columns - why not 
is better explained on e.g. the wiki - 
http://en.wikipedia.org/wiki/Database_normalization


hybrid_property is described here  
http://www.sqlalchemy.org/docs/orm/extensions/hybrid.html?highlight=hybrid_property#sqlalchemy.ext.hybrid.hybrid_property


If you do this you would need to change this:

volunteer = Volunteer(fore=fore, surn=surn, name=name)

to:
volunteer = Volunteer(fore=fore, surn=surn)

Hope this helps
Werner

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



Re: [sqlalchemy] Tea Rota

2012-01-13 Thread Calum MacLeod

Dear Werner,

Thanks for completing the puzzle for me!
I followed your code precisely and it worked as you would have expected.
So now I have the @hybrid property in my class definition too.

(The reason that I ended up previously with fore,surn and name was that 
I was not sure of how the line

volunteers[name] = volunteer
might be affected if I had fore,surn in there instead.)

Basically, I am just taking baby steps and getting the entire code to 
work gives me the confidence to progress.

Having code that works is great.

Thanks for your help.

Calum

On 13/01/12 16:31, werner wrote:

Hi Calum,

On 01/13/2012 01:31 PM, Calum MacLeod wrote:

Thanks, Ian,

I appreciate your advice and have removed the first conditional (if 
name not in duties).


Have now changed that to:

duty = Duty(date=date)
duties[date] = duty

if name not in volunteers:
volunteer = Volunteer(fore=fore, surn=surn, name=name)
volunteers[name] = volunteer
volunteer.duties.append(duty)
Another hobbiest here (so take the following with a grain of salt), I 
think you would want this:


else:
volunteers[name].duties.append(duty)

Or

if name not in volunteers:
volunteer = Volunteer(fore=fore, surn=surn, name=name)
volunteers[name] = volunteer

volunteers[name].duties.append(duty)


Would also change your Volunteer definition from:

class Volunteer(Base):
__tablename__ = 'volunteers'
id = Column(Integer, primary_key=True)
fore = Column(String)
surn = Column(String)
name = Column(String)
dates = association_proxy('duties', 'date')

to:

class Volunteer(Base):
__tablename__ = 'volunteers'
id = Column(Integer, primary_key=True)
fore = Column(String)
surn = Column(String)
dates = association_proxy('duties', 'date')

  # read only columns
@hybrid_property
def name(self):
return self.fore + ', ' + self.surn

I.e. don't store the same information in two different columns - why 
not is better explained on e.g. the wiki - 
http://en.wikipedia.org/wiki/Database_normalization


hybrid_property is described here 
http://www.sqlalchemy.org/docs/orm/extensions/hybrid.html?highlight=hybrid_property#sqlalchemy.ext.hybrid.hybrid_property


If you do this you would need to change this:

volunteer = Volunteer(fore=fore, surn=surn, name=name)

to:
volunteer = Volunteer(fore=fore, surn=surn)

Hope this helps
Werner
--
You received this message because you are subscribed to the Google 
Groups sqlalchemy group.

To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.


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