Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'say,': 0.05; '"""': 0.07; 'assigning': 0.09; 'assuming': 0.09; 'latter': 0.09; 'logic': 0.09; 'shifting': 0.09; 'statements': 0.09; 'developer': 0.10; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; 'suggest': 0.14; 'wrote': 0.14; 'times,': 0.14; 'changes': 0.15; "(it's": 0.16; 'all?': 0.16; 'assignments': 0.16; 'former,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'it),': 0.16; 'loop.': 0.16; 'normally,': 0.16; 'obj,': 0.16; "someone's": 0.16; 'subject:Broken': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'code,': 0.22; 'putting': 0.22; 'cc:addr:python.org': 0.22; 'exists': 0.24; 'instance,': 0.24; 'non': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'this:': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'thus': 0.29; 'statement': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; '(which': 0.31; 'code': 0.31; '13,': 0.31; 'coded': 0.31; 'freelancer': 0.31; 'service,': 0.32; 'supposed': 0.32; 'text': 0.33; 'comment': 0.34; "i'd": 0.34; 'could': 0.34; 'created': 0.35; 'skip:s 30': 0.35; 'anybody': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'should': 0.36; 'changing': 0.37; 'two': 0.37; 'clear': 0.37; 'easily': 0.37; 'short': 0.38; 'does': 0.39; 'help,': 0.39; 'enough': 0.39; 'even': 0.60; 'tell': 0.60; 'entire': 0.61; 'facebook': 0.61; 'simply': 0.61; "you're": 0.61; 'further': 0.61; 'first': 0.61; 'making': 0.63; 'kind': 0.63; 'accounts': 0.64; 'great': 0.65; 'linked': 0.65; 'talking': 0.65; 'results': 0.69; 'social': 0.69; 'to,': 0.72; '2015': 0.84; 'urls,': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=z+Eja14SIJi1Qcpz3hblllhf72Itnwux1qatG/B8TQw=; b=0QdhSfjxHghQBAm7/1WMP3GBsexP0dQj64oENl7g4kh2A1m+Qs8ix8NByln0/wJDWW D+iFx1ew0TR5hZVNhXXUZBUwe6Brcp+CDw1ML24CSBrz5EGMXGHwX1xyhINb7L4C2EuT AgSM95w9En1wTaapo7qMM8Tejd2MwsBZuQA0ApYcz+JKIV41r5Ncuj0q1SNhTWP1Sxim BT814+FJr1OTNtvRQiOzkci0N+QB7KZRN7Vf6kvKSWCbWgQVNoziBawbJKxoS5iYfhkq nOVDeZyrYItrJP0BJF5D7NaSMZf+CQZDaDfetms+9OJa4bicRtaoy5JwUZnGB7h/5qtR yeKg== MIME-Version: 1.0 X-Received: by 10.107.134.39 with SMTP id i39mr29553893iod.53.1421098849799; Mon, 12 Jan 2015 13:40:49 -0800 (PST) In-Reply-To: References: Date: Tue, 13 Jan 2015 08:40:49 +1100 Subject: Re: Broken IF statement From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 78 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421099337 news.xs4all.nl 2957 [2001:888:2000:d::a6]:51458 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83637 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