Re: Broken IF statement
On Tuesday, January 13, 2015 at 4:57:50 AM UTC-5, Maxime S wrote: > 2015-01-12 22:19 GMT+01:00 : > > > > https://bpaste.net/show/93be9e15634b <--- Line 19 through 22 > > > > At all times, my program is assigning the object priority of 0, even if one > > already exists in the database with a priority of 0 (it's supposed to be > > assigning it a priority of 1 in those cases). > > > > I'm a non developer trying to fix a freelancer's code. Would anybody be > > able to suggest changes to the IF logic that might be able to fix it, > > assuming the statements in the code provided look flawed? > > > > Thanks... > > -- > > https://mail.python.org/mailman/listinfo/python-list > > > This line: > > obj, created = SocialAccount.objects.get_or_create(...) > > suggest you are using Django. If it is the case you have to add obj.save() > after changing the priority to send the new value to the DB. > > Best, > > Maxime Thanks Maxime. I'll try that as well. -- https://mail.python.org/mailman/listinfo/python-list
Re: Broken IF statement
On Monday, January 12, 2015 at 4:55:43 PM UTC-5, jobop...@gmail.com wrote: > Thanks Chris. This definitely helps. I will test it and see what happens. In > terms of the previous code, what it was intended to do wasn't actually > happening. Thanks Chris. Your change worked. -- https://mail.python.org/mailman/listinfo/python-list
Re: Broken IF statement
2015-01-12 22:19 GMT+01:00 : > > https://bpaste.net/show/93be9e15634b <--- Line 19 through 22 > > At all times, my program is assigning the object priority of 0, even if one already exists in the database with a priority of 0 (it's supposed to be assigning it a priority of 1 in those cases). > > I'm a non developer trying to fix a freelancer's code. Would anybody be able to suggest changes to the IF logic that might be able to fix it, assuming the statements in the code provided look flawed? > > Thanks... > -- > https://mail.python.org/mailman/listinfo/python-list This line: obj, created = SocialAccount.objects.get_or_create(...) suggest you are using Django. If it is the case you have to add obj.save() after changing the priority to send the new value to the DB. Best, Maxime -- https://mail.python.org/mailman/listinfo/python-list
Re: Broken IF statement
Thanks Chris. This definitely helps. I will test it and see what happens. In terms of the previous code, what it was intended to do wasn't actually happening. -- https://mail.python.org/mailman/listinfo/python-list
Re: Broken IF statement
On Tue, Jan 13, 2015 at 8:19 AM, wrote: > https://bpaste.net/show/93be9e15634b <--- Line 19 through 22 > > At all times, my program is assigning the object priority of 0, even if one > already exists in the database with a priority of 0 (it's supposed to be > assigning it a priority of 1 in those cases). > > I'm a non developer trying to fix a freelancer's code. Would anybody be able > to suggest changes to the IF logic that might be able to fix it, assuming the > statements in the code provided look flawed? Normally, I would suggest talking to the freelancer who wrote the code, unless you're no longer working with him/her. Changing someone's code out from under them is a great way to annoy and confuse. Including the text in-line as it's short enough for that: def create_socialaccount(profile, urls, twitters, facebooks, statuses=None): """ Add or update all the social accounts linked to the 'profile' """ results = [] l = [(urls, SocialAccount.HOMEPAGE, HOMEPAGE_COL_START), (twitters, SocialAccount.TWITTER, TWITTER_COL_START), (facebooks, SocialAccount.FACEBOOK, FACEBOOK_COL_START)] for objs, service, offset in l: for i, value in enumerate(objs): if value: obj, created = SocialAccount.objects.get_or_create( social_profile=profile, service=service, value=value, ) # The first object added/updated gets a priority of 0, all # others get a 1 if i == 0: obj.priority = 0 else: obj.priority = 1 What kind of object is this 'obj'? After you make a change to it, do you need to tell it to write to a database or something? What you could try is changing the priority assignments to, say, 2 and 3. That would tell you that it's making the change. But if the intention is to have the first successful one at priority 0 and all others at priority 1 (which is what the comment implies), then I'd write it like this: def create_socialaccount(profile, urls, twitters, facebooks, statuses=None): """ Add or update all the social accounts linked to the 'profile' """ l = [(urls, SocialAccount.HOMEPAGE, HOMEPAGE_COL_START), (twitters, SocialAccount.TWITTER, TWITTER_COL_START), (facebooks, SocialAccount.FACEBOOK, FACEBOOK_COL_START)] for objs, service, offset in l: prio = 0 for value in objs: if value: obj, created = SocialAccount.objects.get_or_create( social_profile=profile, service=service, value=value, ) # The first object added/updated gets a priority of 0, all # others get a 1 obj.priority = prio prio = 1 It's not clear whether "first" means "first of each type" or "first overall". For instance, if someone has three twitters and two facebooks, should one twitter and one facebook be given priority 0, or should one twitter get prio 0 and everything else prio 1? I've coded it for the former, but you could easily make it the latter by simply shifting the "prio = 0" statement one line further up (and unindenting it), thus putting it before the entire loop. Does that help, at all? ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Broken IF statement
https://bpaste.net/show/93be9e15634b <--- Line 19 through 22 At all times, my program is assigning the object priority of 0, even if one already exists in the database with a priority of 0 (it's supposed to be assigning it a priority of 1 in those cases). I'm a non developer trying to fix a freelancer's code. Would anybody be able to suggest changes to the IF logic that might be able to fix it, assuming the statements in the code provided look flawed? Thanks... -- https://mail.python.org/mailman/listinfo/python-list