Posts: 81
Threads: 27
Joined: Jan 2020
Jan-14-2020, 10:06 PM
(This post was last modified: Jan-14-2020, 10:06 PM by t4keheart.)
Hi everybody,
I'm working on a program that involves querying a database with pyodbc and working with the results. below you will see my program, and the function I'm working with here. The end goal is to be able to work with the returned variables to be able to import/pass them along to another python program.
As you can see, the function works to connect to a database, run a query, and stores the query results in a dictionary. After that, I assign the specific dictionary keys to variables and "return" them at the end of the function. As noted above, I need to be able to import these variables (msg, destPhone, caseNum, timeStamp, sender) from another program... however when I try to print one of them (to test), I don't get any result.
Forgive me for the noobish question, but I'm new to python.. and new around here.
Thank you for your time!
def pullData():
cnxn = pyodbc.connect('UID='+dbUser+';PWD='+dbPassword+';DSN='+dbHost)
cursor = cnxn.cursor()
outbound = cursor.execute("SELECT * FROM WKM_SMS_outbound ORDER BY id DESC")
results = cursor.fetchone()
collection = {}
for index, outbound in enumerate(results):
key_name = "col{0}".format(index)
collection[key_name] = outbound
msg = collection['col1']
destPhone = collection['col2']
caseNum = collection['col4']
timeStamp = collection['col8']
sender = collection['col10']
cnxn.close()
return (msg, destPhone, caseNum, timeStamp, sender)
pullData()
print(msg)
Posts: 165
Threads: 7
Joined: Nov 2018
Jan-14-2020, 11:13 PM
(This post was last modified: Jan-14-2020, 11:14 PM by joe_momma.)
try this:
msg, destPhone, caseNum, timeStamp, sender= pullData()
print(msg)
Posts: 1,838
Threads: 2
Joined: Apr 2017
Think about what you're doing: on line 22, you call the function and don't do anything with its return value, so it's just thrown away. Either assign it to a variable and then use that variable (which you already know how to do, e.g. lines 2 or 3) or use the value directly in an expression as shown above (though you already seem to know how to do that too: it's exactly what you're doing with the call to enumerate on line 8).
On line 23, you try to print the value of msg , but the variable doesn't exist outside of the scope of the pullData function. Perhaps you need to revise the concept of scope?
Posts: 2,128
Threads: 11
Joined: May 2017
Jan-15-2020, 09:22 AM
(This post was last modified: Jan-15-2020, 09:29 AM by DeaD_EyE.)
Very easy trick, but in the most cases it's True .
def foo():
name = 'Foo'
return name Everything, which is indented, is not accessible from outside.
This is True for functions, which don't use global variables.
Returning something from a function, requires that the caller assign the result.
But the caller can't see from outside, whats happening inside the function.
It's a kind of black box for the caller. You call it with something and then you get something back.
# I'm the caller
my_result = foo() First the interpreter searches for the name foo (it could be everything).
Then the interpreter calls the object, which is referenced by foo . The function foo is executed and return the result.
On the caller side, the return value is assigned to the name result .
A = sign means always assignment to a name. (I don't like to use the term variable for it).
Edit:
If you see something like this:
a, b, c, d = some_function() Then there is a step before assignment.
In this case the programmer expect 4 return values (objects).
If there is one name to less or too much, it won't work.
This technique is called Iterable Unpacking .
In addition, there is a Extended Iterable Unpacking.
I can't find the old pep.
Posts: 1,950
Threads: 8
Joined: Jun 2018
(Jan-15-2020, 09:22 AM)DeaD_EyE Wrote: A = sign means always assignment to a name. (I don't like to use the term variable for it).
I have the same sentiment.
Python documentation Assignment statement calls it "name is bound to the object":
Output: Assignment statements are used to (re)bind names to values and to modify attributes or items of mutable objects
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 81
Threads: 27
Joined: Jan 2020
thank you everybody for your input and thoughts.
Would somebody be kind enough to explain why this works?
msg, destPhone, caseNum, timeStamp, sender = pullData()
print(msg)
print(destPhone)
print(caseNum)
print(timeStamp)
print(sender) I don't understand what's going on here... what's happening when declaring the returned values back to the funtion itself?
I'm a sys admin rather than a software guy by trade, I've mostly just fallen into a programming role to meet the needs that my employer decides they want... so a lot of these things don't inherently make sense to me... still learning.
Thanks again!
Posts: 8,169
Threads: 160
Joined: Sep 2016
Jan-15-2020, 02:27 PM
(This post was last modified: Jan-15-2020, 02:27 PM by buran.)
(Jan-15-2020, 02:14 PM)t4keheart Wrote: when declaring the returned values back to the funtion itself not sure what you mean by this
on the RHS is the function call and the function returns tuple with 5 values
on the LHS are 5 names and you unpack the tuple into these (i.e. assign each element from tuple returned by the function to individual name)
probably confusion comes because the names used within the function scope (i.e. the local scope) are same as the names in global scope. However these are names with different scopes
Posts: 1,950
Threads: 8
Joined: Jun 2018
(Jan-15-2020, 02:14 PM)t4keheart Wrote: Would somebody be kind enough to explain why this works?
I don't understand what's going on here...
This is called sequence unpacking (look for explanation at the end of chapter Tuples and Sequences
>>> first, second, third = ('spam', 'ham', 'eggs')
>>> first
'spam'
>>> second
'ham'
>>> third
'eggs'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 81
Threads: 27
Joined: Jan 2020
I appreciate the input and clarification.
Believe it or not, I'm working on a program to integrate our case management system (law firm) and web sms messaging platform api, and allow for lawyers to text clients via posting messages in the CMS... and thanks to you guys I have just about half of that working (the sending side).
Never in a million years did I think I would be able to pull this one off lol.
Thanks again.
Posts: 165
Threads: 7
Joined: Nov 2018
In python you can declare objects on the fly, I used the same assignment names as you did to make a point, but it could be anything that's clear to you or someone else reading your code example:
a,b,c,d,e= pullData()
print(a)#printing your message or
message, destPhoneNumber, caseNumber, time_Stamp, client= pullData()
print(message) the others do a great job of showing you examples re read and run their code in an interactive prompt, play with it.
clear as mud?
|