Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83635 > unrolled thread

Broken IF statement

Started byjoboppsgpp@gmail.com
First post2015-01-12 13:19 -0800
Last post2015-01-13 06:29 -0800
Articles 6 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Broken IF statement joboppsgpp@gmail.com - 2015-01-12 13:19 -0800
    Re: Broken IF statement Chris Angelico <rosuav@gmail.com> - 2015-01-13 08:40 +1100
      Re: Broken IF statement joboppsgpp@gmail.com - 2015-01-12 13:55 -0800
        Re: Broken IF statement joboppsgpp@gmail.com - 2015-01-13 06:28 -0800
    Re: Broken IF statement Maxime S <maxischmeii@gmail.com> - 2015-01-13 10:31 +0100
      Re: Broken IF statement joboppsgpp@gmail.com - 2015-01-13 06:29 -0800

#83635 — Broken IF statement

Fromjoboppsgpp@gmail.com
Date2015-01-12 13:19 -0800
SubjectBroken IF statement
Message-ID<dd384c6d-17c0-4850-ad5b-6b073c80bdb8@googlegroups.com>
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...

[toc] | [next] | [standalone]


#83637

FromChris Angelico <rosuav@gmail.com>
Date2015-01-13 08:40 +1100
Message-ID<mailman.17641.1421099337.18130.python-list@python.org>
In reply to#83635
On Tue, Jan 13, 2015 at 8:19 AM,  <joboppsgpp@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#83638

Fromjoboppsgpp@gmail.com
Date2015-01-12 13:55 -0800
Message-ID<ae5a458e-2914-4cd3-81f0-a3b93eab1a14@googlegroups.com>
In reply to#83637
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. 

[toc] | [prev] | [next] | [standalone]


#83695

Fromjoboppsgpp@gmail.com
Date2015-01-13 06:28 -0800
Message-ID<8e7a3125-c8dd-4616-aebb-13eb96bc5665@googlegroups.com>
In reply to#83638
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.

[toc] | [prev] | [next] | [standalone]


#83680

FromMaxime S <maxischmeii@gmail.com>
Date2015-01-13 10:31 +0100
Message-ID<mailman.17668.1421143058.18130.python-list@python.org>
In reply to#83635

[Multipart message — attachments visible in raw view] — view raw

2015-01-12 22:19 GMT+01:00 <joboppsgpp@gmail.com>:
>
> 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

[toc] | [prev] | [next] | [standalone]


#83696

Fromjoboppsgpp@gmail.com
Date2015-01-13 06:29 -0800
Message-ID<9a808384-6c59-4266-b5b4-32ce6bed9531@googlegroups.com>
In reply to#83680
On Tuesday, January 13, 2015 at 4:57:50 AM UTC-5, Maxime S wrote:
> 2015-01-12 22:19 GMT+01:00 <jobop...@gmail.com>:
> >
> > 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web