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


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

Stupid question, just need a quick and dirty fix

Started byJordan Bayless <jmbayless@gmail.com>
First post2016-07-21 21:19 -0700
Last post2016-07-22 15:58 +0100
Articles 14 — 8 participants

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


Contents

  Stupid question, just need a quick and dirty fix Jordan Bayless <jmbayless@gmail.com> - 2016-07-21 21:19 -0700
    Re: Stupid question, just need a quick and dirty fix Chris Angelico <rosuav@gmail.com> - 2016-07-22 14:28 +1000
      Re: Stupid question, just need a quick and dirty fix Jordan Bayless <jmbayless@gmail.com> - 2016-07-21 21:39 -0700
        Re: Stupid question, just need a quick and dirty fix Chris Angelico <rosuav@gmail.com> - 2016-07-22 14:45 +1000
          Re: Stupid question, just need a quick and dirty fix Jordan Bayless <jmbayless@gmail.com> - 2016-07-21 22:26 -0700
            Re: Stupid question, just need a quick and dirty fix Paul Rubin <no.email@nospam.invalid> - 2016-07-21 23:03 -0700
            Re: Stupid question, just need a quick and dirty fix Chris Angelico <rosuav@gmail.com> - 2016-07-22 16:34 +1000
            Re: Stupid question, just need a quick and dirty fix Steven D'Aprano <steve@pearwood.info> - 2016-07-22 16:37 +1000
              Re: Stupid question, just need a quick and dirty fix thilfigr16@gmail.com - 2016-07-22 06:00 -0700
            Re: Stupid question, just need a quick and dirty fix Cousin Stanley <cousinstanley@gmail.com> - 2016-07-22 08:45 -0700
        Re: Stupid question, just need a quick and dirty fix Michael Selik <michael.selik@gmail.com> - 2016-07-22 01:18 -0400
          Re: Stupid question, just need a quick and dirty fix Steven D'Aprano <steve@pearwood.info> - 2016-07-22 16:08 +1000
            Re: Stupid question, just need a quick and dirty fix Michael Selik <michael.selik@gmail.com> - 2016-07-22 18:53 +0000
    Re: Stupid question, just need a quick and dirty fix MRAB <python@mrabarnett.plus.com> - 2016-07-22 15:58 +0100

#111732 — Stupid question, just need a quick and dirty fix

FromJordan Bayless <jmbayless@gmail.com>
Date2016-07-21 21:19 -0700
SubjectStupid question, just need a quick and dirty fix
Message-ID<e1a10af3-cd57-4a56-98bf-7a9ed8b3d999@googlegroups.com>
I'm trying to modify some code to suit my purposes and I'm just trying to filter results as necessary. Basically, the code is returning one of a number from a subset of 150 numbers. I want to only do anything with it if the number is a 'good' one. I'm by no means a Python programmer (C# for me by trade) and I'm not looking for anything that will be distributed..I just want something that works.

Basically, here's what I'm trying to insert:

global blnDesiredInd

blnDesiredInd == False

if IDNum < 10:
    blnDesiredInd = True
if IDNum == 12:
    blnDesiredInd = True
if IDNum == 15:
    blnDesiredInd = True
if IDNum == 24:
    blnDesiredInd = True
if IDNum == 25:
    blnDesiredInd = True
if IDNum == 26:
    blnDesiredInd = True
if IDNum == 27:
    blnDesiredInd = True
if IDNum == 28:
    blnDesiredInd = True
if IDNum == 31:
    blnDesiredInd = True
if IDNum == 34:
    blnDesiredInd = True
if IDNum == 35:
    blnDesiredInd = True
if IDNum == 36:
    blnDesiredInd = True
if IDNum == 37:
    blnDesiredInd = True
if IDNum == 38:
    blnDesiredInd = True
if IDNum == 39:
    blnDesiredInd = True
if IDNum == 40:
    blnDesiredInd = True
if IDNum == 45:
    blnDesiredInd = True
if IDNum == 50:
    blnDesiredInd = True
if IDNum == 51:
    blnDesiredInd = True
if IDNum == 53:
    blnDesiredInd = True
if IDNum == 55:
    blnDesiredInd = True
if IDNum == 56:
    blnDesiredInd = True
if IDNum == 57:
    blnDesiredInd = True
if IDNum == 59:
    blnDesiredInd = True
if IDNum == 62:
    blnDesiredInd = True
if IDNum == 65:
    blnDesiredInd = True
if IDNum == 68:
    blnDesiredInd = True
if IDNum == 71:
    blnDesiredInd = True
if IDNum == 76:
    blnDesiredInd = True
if IDNum == 78:
    blnDesiredInd = True
if IDNum == 80:
    blnDesiredInd = True
if IDNum == 82:
    blnDesiredInd = True
if IDNum == 83:
    blnDesiredInd = True
if IDNum == 87:
    blnDesiredInd = True
if IDNum == 88:
    blnDesiredInd = True
if IDNum == 89:
    blnDesiredInd = True
if IDNum == 91:
    blnDesiredInd = True
if IDNum == 93:
    blnDesiredInd = True
if IDNum == 94:
    blnDesiredInd = True
if IDNum == 96:
    blnDesiredInd = True
if IDNum == 97:
    blnDesiredInd = True
if IDNum == 101:
    blnDesiredInd = True
if IDNum == 103:
    blnDesiredInd = True
if IDNum == 105:
    blnDesiredInd = True
if IDNum == 106:
    blnDesiredInd = True
if IDNum == 107:
    blnDesiredInd = True
if IDNum == 109:
    blnDesiredInd = True
if IDNum == 110:
    blnDesiredInd = True
if IDNum == 112:
    blnDesiredInd = True
if IDNum == 113:
    blnDesiredInd = True
if IDNum == 115:
    blnDesiredInd = True
if IDNum == 122:
    blnDesiredInd = True
if IDNum == 124:
    blnDesiredInd = True
if IDNum == 125:
    blnDesiredInd = True
if IDNum == 126:
    blnDesiredInd = True
if IDNum == 130:
    blnDesiredInd = True
if IDNum == 131:
    blnDesiredInd = True
if IDNum == 132:
    blnDesiredInd = True
if IDNum > 133:
    blnDesiredInd = True

    if blnDesiredInd == True:

I get various errors no matter what I do to this to try and make it work. Variable not defined. Referenced before assignment. etc etc. I'm lost. How do I make it work?

[toc] | [next] | [standalone]


#111733

FromChris Angelico <rosuav@gmail.com>
Date2016-07-22 14:28 +1000
Message-ID<mailman.39.1469161719.22221.python-list@python.org>
In reply to#111732
On Fri, Jul 22, 2016 at 2:19 PM, Jordan Bayless <jmbayless@gmail.com> wrote:
> I get various errors no matter what I do to this to try and make it work. Variable not defined. Referenced before assignment. etc etc. I'm lost. How do I make it work?

It might be easier if you post all your code. To be honest, what I'd
be looking at is something like this:

good_ids = {
    12, 15, 24,
    ...  # fill in all of these
}
desired = id < 10 or id > 133 or id in good_ids

But it's possible your problem has nothing to do with your massive
'if' tree and everything to do with indentation or other problems we
can't see.

ChrisA

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


#111734

FromJordan Bayless <jmbayless@gmail.com>
Date2016-07-21 21:39 -0700
Message-ID<881b184c-11e4-41e7-a01e-6a21d454ce68@googlegroups.com>
In reply to#111733
On Thursday, July 21, 2016 at 11:28:55 PM UTC-5, Chris Angelico wrote:
> On Fri, Jul 22, 2016 at 2:19 PM, Jordan Bayless <jmbayless@gmail.com> wrote:
> > I get various errors no matter what I do to this to try and make it work. Variable not defined. Referenced before assignment. etc etc. I'm lost. How do I make it work?
> 
> It might be easier if you post all your code. To be honest, what I'd
> be looking at is something like this:
> 
> good_ids = {
>     12, 15, 24,
>     ...  # fill in all of these
> }
> desired = id < 10 or id > 133 or id in good_ids
> 
> But it's possible your problem has nothing to do with your massive
> 'if' tree and everything to do with indentation or other problems we
> can't see.
> 
> ChrisA

Posting the entire code snippet is tough because it's thousands of lines of code. Basically, I'm inside of a For loop already. The code worked when I got it, but I'm trying to tap into it and extend it outward to notify me via email in certain circumstances. I got it to send the email, but it seems there's a) no case statement (WTF?) and b) I'm limited to how many elif statements I can use. Again, I don't particularly care how elegant this is because it's ultimately getting disposed of sooner rather than later..I just want it to work.

Let me review my code and see if I can pare it down to the essentials so I can give a better idea of what I'm doing.

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


#111735

FromChris Angelico <rosuav@gmail.com>
Date2016-07-22 14:45 +1000
Message-ID<mailman.40.1469162766.22221.python-list@python.org>
In reply to#111734
On Fri, Jul 22, 2016 at 2:39 PM, Jordan Bayless <jmbayless@gmail.com> wrote:
> it seems there's a) no case statement (WTF?) and b) I'm limited to how many elif statements I can use.

The latter isn't true; and you're not using elif anyway. With no case
statement, you get pushed to other, better ways of doing things, like
the much simpler way I showed in my previous email - it uses set
membership rather than a series of individual equality checks. Faster,
cleaner, much easier to work with.

ChrisA

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


#111737

FromJordan Bayless <jmbayless@gmail.com>
Date2016-07-21 22:26 -0700
Message-ID<2795f3e5-e660-40c6-8a43-97a9613d7577@googlegroups.com>
In reply to#111735
No, I tried using a bunch of elif statements earlier and when I added more than around 3 of them it threw errors. I just assumed that was some kind of limit. We both agree that's piss-poor, lazy coding. I'm just trying to find something that works though. To this point, everything I try fails.

I added your code and now it's telling me my variable isn't defined. 

good_ids = {
    2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 24, 25, 26, 27, 28, 31, 34, 35, 36, 37,
    38, 39, 40, 45, 50, 51, 53, 55, 56, 57, 59, 62, 65, 68, 71, 76, 78, 80,
    82, 83, 87, 88, 89, 91, 93, 94, 96, 97, 101, 103, 105, 106, 107, 109,
    110, 112, 113, 115, 122, 124, 125, 126, 130, 131, 132, 134, 135, 136,
    137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151
}

desired = Id < 10 or Id > 133 or Id in good_ids

When I try to validate whether I passed that check, I'm told there's a Name error and it's not defined (using the last line of the snippet above).

Also, I guess I'm at a loss for what I'm supposed to do with "desired" to check it prior to running my email code. 

if desired == True: doesn't work 

I'm headed to bed. Too tired to focus on making this work and I apparently really don't 'get' what I'm doing. Maybe I'll be better off with fresh eyes on it tomorrow.

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


#111739

FromPaul Rubin <no.email@nospam.invalid>
Date2016-07-21 23:03 -0700
Message-ID<87y44ub46f.fsf@jester.gateway.pace.com>
In reply to#111737
Jordan Bayless <jmbayless@gmail.com> writes:
> desired = Id < 10 or Id > 133 or Id in good_ids
> When I try to validate whether I passed that check, I'm told there's a
> Name error and it's not defined (using the last line of the snippet
> above).

Id was called IDNum in your earlier pst

> Also, I guess I'm at a loss for what I'm supposed to do with "desired"
> to check it prior to running my email code.
>
> if desired == True: doesn't work 

That should work, but it's more concise and idiomatic to say "if desired: ..."

> I'm headed to bed. Too tired to focus on making this work and I
> apparently really don't 'get' what I'm doing. Maybe I'll be better off
> with fresh eyes on it tomorrow.

You're probably better off reading a basic Python tutorial than trying
to mess with a working piece of code by trial and error without knowing
the language.

  https://docs.python.org/2/tutorial/index.html

is a good place to get started.

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


#111741

FromChris Angelico <rosuav@gmail.com>
Date2016-07-22 16:34 +1000
Message-ID<mailman.42.1469169272.22221.python-list@python.org>
In reply to#111737
On Fri, Jul 22, 2016 at 3:26 PM, Jordan Bayless <jmbayless@gmail.com> wrote:
> When I try to validate whether I passed that check, I'm told there's a Name error and it's not defined (using the last line of the snippet above).

You're still not posting (a) your code, or (b) the full traceback, so
it's not easy for us to help you.

ChrisA

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


#111742

FromSteven D'Aprano <steve@pearwood.info>
Date2016-07-22 16:37 +1000
Message-ID<5791bf21$0$1599$c3e8da3$5496439d@news.astraweb.com>
In reply to#111737
On Fri, 22 Jul 2016 03:26 pm, Jordan Bayless wrote:

> No, I tried using a bunch of elif statements earlier and when I added more
> than around 3 of them it threw errors. I just assumed that was some kind
> of limit. 

Right, because a 20+ years old programming language used by millions of
professionals all around the world *obviously* must have a limit of three
elif statements in a clause. What else could it be? *wink*

If only Python would print an exception and an error message so that we
could diagnose the error. Oh wait, it does.

Instead of just complaining about the limitations of a language you clearly
know nothing about, why don't you copy and paste the exceptions so that we
can see them? There's no sin in knowing nothing about a language. We all
started off ignorant. But jumping to conclusions, particularly the
conclusion "well it must be the language's fault", that's just pretty poor.

I know you're frustrated. You need to relax, and approach this
systematically, and not just blindly make changes without understanding
them. More below.


> We both agree that's piss-poor, lazy coding. I'm just trying to 
> find something that works though. To this point, everything I try fails.
> 
> I added your code and now it's telling me my variable isn't defined.

Have you tried defining it?

Which variable? If you read the error message, it will tell you which
variable is not defined. Then you read the code to see where you are trying
to access the variable before defining it. This won't work:

print(x)
x = 1

but this will:

x = 1
print(x)


Can you see why? You need to have given the variable a value *before* using
it.

 
> good_ids = {
>     2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 24, 25, 26, 27, 28, 31, 34, 35, 36,
>     37, 38, 39, 40, 45, 50, 51, 53, 55, 56, 57, 59, 62, 65, 68, 71, 76,
>     78, 80, 82, 83, 87, 88, 89, 91, 93, 94, 96, 97, 101, 103, 105, 106,
>     107, 109, 110, 112, 113, 115, 122, 124, 125, 126, 130, 131, 132, 134,
>     135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148,
>     149, 150, 151
> }
> 
> desired = Id < 10 or Id > 133 or Id in good_ids
> 
> When I try to validate whether I passed that check, I'm told there's a
> Name error and it's not defined (using the last line of the snippet
> above).

Right. And what name does it say is not defined? Is is "Id"? Ask yourself:
is Id defined anywhere? The answer will be No. Where does the name come
from?

You blindly copied it from Chris Angelico's code. He said:

desired = Id < 10 or Id > 133 or Id in good_ids

(actually he used lowercase "id" rather than "Id"). But go back to your
original request, where you wrote:

    Basically, here's what I'm trying to insert:

    global blnDesiredInd
    blnDesiredInd == False
    if IDNum < 10:
        blnDesiredInd = True
    if IDNum == 12:
        blnDesiredInd = True
    [...]


Maybe if you replace the names Chris used with the names your program
already uses?

good_ids = { blah blah blah }
blnDesiredInd = IDNum < 10 or IDNum > 133 or IDNum in good_ids


> Also, I guess I'm at a loss for what I'm supposed to do with "desired" to
> check it prior to running my email code.
> 
> if desired == True: doesn't work


What were you planning on doing with blnDesiredInd?

if blnDesiredInd:  # no need to say "== True"
   # do the thing you want to do when blnDesiredInd is true
   ...
else:
   # do the thing you want to do when blnDesiredInd is not true
   ...


You have to replace the dots ... with actual code, of course, but we can't
help you with that. It's *your* project, not ours, we have no idea what you
want to happen.




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


#111747

Fromthilfigr16@gmail.com
Date2016-07-22 06:00 -0700
Message-ID<b1353e50-006a-4e6b-9e93-24051cbcbb92@googlegroups.com>
In reply to#111742
I'm certainly not going to sugar-coat it and act like I know anything about this language, or wax about how I think it's "inferior" for any reason (because doing so would be pretty foolish). I just figured I'd be able to muddle through and find *something* that would easily filter out results I didn't want. I assumed getting it to port through gmail to notify me would be the hard part and that was actually quite easy. I'm by no means looking to go out and put "Python programmer" on my resume after finishing this task, I was just hoping to dial in the code a bit better for my needs. 

My var names changed on this thread because I was just using them here as references. I'll figure out how to just post the whole PY file when I'm home tonight and I'll point out the lines where I'm having issues. 

I do appreciate the information and I still need to go up and read (comprehend) everything posted to see if I can take it from there. Thanks!

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


#111763

FromCousin Stanley <cousinstanley@gmail.com>
Date2016-07-22 08:45 -0700
Message-ID<nmtf26$gdo$1@dont-email.me>
In reply to#111737
Jordan Bayless wrote:

> ....
> desired = Id < 10 or Id > 133 or Id in good_ids
> 
> When I try to validate whether I passed that check, 
> I'm told there's a Name error and it's not defined 
> .... 

  On the outside chance that failing to define Id 
  produces the Name error, I defined Id in a for loop
  as a test for your copy/pasted code ....


$ cat id_test.py
#!/usr/bin/env python3

good_ids = {
    2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 24, 25, 26, 27, 28, 31, 34, 35, 36, 37,
    38, 39, 40, 45, 50, 51, 53, 55, 56, 57, 59, 62, 65, 68, 71, 76, 78, 80,
    82, 83, 87, 88, 89, 91, 93, 94, 96, 97, 101, 103, 105, 106, 107, 109,
    110, 112, 113, 115, 122, 124, 125, 126, 130, 131, 132, 134, 135, 136,
    137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 
151
}

test_ids = [ 1 , 10 , 128 , 42 , 137 , 444 ]

print( )

for Id in test_ids : 

    desired = Id < 10  or  Id > 133  or  Id in good_ids

    print( '  Id :  %4d .... desired :  %s ' % ( Id , desired ) )


$ ./id_test.py

  Id :     1 .... desired :  True 
  Id :    10 .... desired :  False 
  Id :   128 .... desired :  False 
  Id :    42 .... desired :  False 
  Id :   137 .... desired :  True 
  Id :   444 .... desired :  True 



-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona

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


#111736

FromMichael Selik <michael.selik@gmail.com>
Date2016-07-22 01:18 -0400
Message-ID<mailman.41.1469164707.22221.python-list@python.org>
In reply to#111734
> On Jul 22, 2016, at 12:39 AM, Jordan Bayless <jmbayless@gmail.com> wrote:
> 
> Posting the entire code snippet is tough because it's thousands of lines of code.

You could paste into a GitHub gist (https://gist.github.com/) and share the link.

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


#111740

FromSteven D'Aprano <steve@pearwood.info>
Date2016-07-22 16:08 +1000
Message-ID<5791b872$0$1604$c3e8da3$5496439d@news.astraweb.com>
In reply to#111736
On Fri, 22 Jul 2016 03:18 pm, Michael Selik wrote:

> 
>> On Jul 22, 2016, at 12:39 AM, Jordan Bayless <jmbayless@gmail.com> wrote:
>> 
>> Posting the entire code snippet is tough because it's thousands of lines
>> of code.
> 
> You could paste into a GitHub gist (https://gist.github.com/) and share
> the link.

Are you going to read "thousands of lines" of unfamiliar code trying to work
out what changes need to be made to fix an underspecified problem?

Yeah, sure you are. Have fun.




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


#111771

FromMichael Selik <michael.selik@gmail.com>
Date2016-07-22 18:53 +0000
Message-ID<mailman.63.1469213650.22221.python-list@python.org>
In reply to#111740
On Fri, Jul 22, 2016 at 2:11 AM Steven D'Aprano <steve@pearwood.info> wrote:

> On Fri, 22 Jul 2016 03:18 pm, Michael Selik wrote:
> >> On Jul 22, 2016, at 12:39 AM, Jordan Bayless <jmbayless@gmail.com>
> wrote:
> >>
> >> Posting the entire code snippet is tough because it's thousands of lines
> >> of code.
> >
> > You could paste into a GitHub gist (https://gist.github.com/) and share
> > the link.
>
> Are you going to read "thousands of lines" of unfamiliar code trying to
> work
> out what changes need to be made to fix an underspecified problem?
>
> Yeah, sure you are. Have fun.
>

I suppose not :-)
But then I could follow up with a request for the traceback.

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


#111756

FromMRAB <python@mrabarnett.plus.com>
Date2016-07-22 15:58 +0100
Message-ID<mailman.52.1469199518.22221.python-list@python.org>
In reply to#111732
On 2016-07-22 05:19, Jordan Bayless wrote:
> I'm trying to modify some code to suit my purposes and I'm just trying to filter results as necessary. Basically, the code is returning one of a number from a subset of 150 numbers. I want to only do anything with it if the number is a 'good' one. I'm by no means a Python programmer (C# for me by trade) and I'm not looking for anything that will be distributed..I just want something that works.
>
> Basically, here's what I'm trying to insert:
>
> global blnDesiredInd
>
 > blnDesiredInd == False
>
[snip]

The line:

blnDesiredInd == False

looks wrong. It should probably be:

blnDesiredInd = False

[toc] | [prev] | [standalone]


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


csiph-web