Hi Experts!!
I am trying to get the following little snippet to push my data to the
function func(). What I would expect to happen is it to print out the
contents of a and loglevel. But it's not working. Can someone please
help me out.
---------<snip>--------------
#!/usr/bin/env python
import random
def func(*args, **kwargs):
print kwargs.get('a', "NOPE")
print kwargs.get('loglevel', "NO WAY")
def main():
b = []
for x in range(5):
b.append({'a':random.random(), "loglevel":10})
for y in b:
apply(func,y)
# First attempt - didn't work
# for y in b:
# func(y)
if __name__ == '__main__':
main() 6 1331
rh0dium schrieb:
Hi Experts!!
I am trying to get the following little snippet to push my data to the
function func(). What I would expect to happen is it to print out the
contents of a and loglevel. But it's not working. Can someone please
help me out.
---------<snip>--------------
#!/usr/bin/env python
import random
def func(*args, **kwargs):
print kwargs.get('a', "NOPE")
print kwargs.get('loglevel', "NO WAY")
def main():
b = []
for x in range(5):
b.append({'a':random.random(), "loglevel":10})
for y in b:
apply(func,y)
# First attempt - didn't work
# for y in b:
# func(y)
if __name__ == '__main__':
main()
``apply()`` is deprecated -- use the asterisk-syntax_ instead.
>>>dic = {'a': 5, 'loglevel': 10} def foo(*args, **kwargs):
.... print kwargs
....
>>foo(**dic)
{'a': 5, 'loglevel': 10}
So, your first attempt was close -- just two signs missing. :-)
HTH,
Stargaming
... _asterisk-sytax: http://docs.python.org/tut/node6.htm...00000000000000
On May 1, 10:38 am, rh0dium <steven.kl...@gmail.comwrote:
Hi Experts!!
I am trying to get the following little snippet to push my data to the
function func(). What I would expect to happen is it to print out the
contents of a and loglevel. But it's not working. Can someone please
help me out.
---------<snip>--------------
#!/usr/bin/env python
import random
def func(*args, **kwargs):
print kwargs.get('a', "NOPE")
print kwargs.get('loglevel', "NO WAY")
def main():
b = []
for x in range(5):
b.append({'a':random.random(), "loglevel":10})
for y in b:
apply(func,y)
# First attempt - didn't work
# for y in b:
# func(y)
if __name__ == '__main__':
main()
1) apply() is deprecated
2) You need to unpack the dictionary using ** before sending it to
func(), whereupon it will be repacked into a dictionary.
import random
def func(*args, **kwargs):
print kwargs.get('a', "NOPE")
print kwargs.get('loglevel', "NO WAY")
def main():
b = []
for x in range(5):
b.append({'a':random.random(), "loglevel":10})
for y in b:
func(**y)
if __name__ == '__main__':
main()
You might consider redefining func() so that you don't have to do the
unpack--repack:
On May 1, 11:06 am, 7stud <bbxx789_0...@yahoo.comwrote:
>
You might consider redefining func() so that you don't have to do the
unpack--repack...
When you define a function like this:
def func(**keywordargs):
print keywordargs
the function expects to receive arguments in the form:
func(a="hello", b="world")
not:
func(adict)
Thanks to all who helped!!
Let me expand a bit more - I am working on a threading class and I
want to be able to push on the Queue a list of args. If you run the
following program - I am failing to understand how to push items onto
the queue in a manner so that func2 recognizes them as kwargs not as
args. Can anyone help me with this.
test1 works
test2 fails again I can't figure out how to push onto the queue a
dictionary and get it back off.
I know this is a lot longer than I tend to post but I really want to
solve this bugger.
Any help is greatly appreciated.
------------<snip>----------------
#!/usr/bin/env python
# encoding: utf-8
"""
myThreading.py
"""
import sys
import os
import traceback
import logging
import threading
import Queue
LOGLEVEL=logging.DEBUG
# Basic logger
logging.basicConfig(level=LOGLEVEL, format="%(asctime)s %(name)s %
(levelname)-8s %(message)s",
datefmt='%d %b %Y %H:%M:%S', stream=sys.stderr)
# Identifies (hopefully) the current module name
try:
module= os.path.basename(traceback.extract_stack(limit=2)[1]
[0]).split(".py")[0]+"."
except:
module = os.path.basename(traceback.extract_stack(limit=2)[0]
[0]).split(".py")[0]+"."
class myThread(threading.Thread):
def __init__(self, *args, **kwargs):
"""description"""
self.id = kwargs.get('id', 0)
self.requestQ = kwargs.get('requestQ', None)
self.responseQ = kwargs.get('responseQ', None)
self.function = kwargs.get('function', None)
# Setup Logging
self.log = logging.getLogger(module+self.__class__.__name__
+"."+str(self.id))
self.loglevel = kwargs.get('loglevel', logging.WARN)
self.setLoglevel(self.loglevel)
self.log.debug("Starting Thread %d" % self.id)
threading.Thread.__init__(self, name=module
+self.__class__.__name__+"."+str(self.id))
def setLoglevel(self,loglevel):
"""setLog log level"""
if loglevel is not False:
self.log.setLevel(loglevel)
self.log.debug("Setting Logging level to %s" % loglevel)
else:
self.log.setLevel(logging.WARN)
if self.loglevel == logging.DEBUG: self.debug = True
else: self.debug=False
def run(self):
while 1:
input = self.requestQ.get()
if input is None:
self.log.debug("Ending the thread - Recieved None")
if self.responseQ is not None:
self.responseQ.put(None)
break
self.log.info("Applying %s to function %s" %
(str(input),str(self.function.__name__)))
result = self.function(input)
if self.responseQ is not None:
self.log.debug("Response recieved = %s" % result)
self.responseQ.put(result)
def func(input):
import time
time.sleep(input)
return 2*input
def test1(loglevel=False):
log = logging.getLogger(module+sys._getframe().f_code.co _name )
if loglevel is not False: log.setLevel(loglevel)
else:log.setLevel(logging.WARN)
maxThreads=5
# Set up two queues one for sending request data (req) one for
getting response to the request (res)
reqQ = Queue.Queue()
resQ = Queue.Queue()
# Push some data onto the reqestQ end it with None
import random
for x in range(200): reqQ.put(random.random())
for n in range(maxThreads): reqQ.put(None)
# Start Up some threads to do some work
for n in range(maxThreads):
t = myThread(id=n,loglevel=logging.INFO, function=func,
requestQ=reqQ, responseQ=resQ).start()
# Collect the results
results = 0
while 1:
try:
data = resQ.get()
if data is None:
break
else:
results += data
except:
break
print results
def func2( input, loglevel=False ):
import time
#print "args", args
#print "kwargs", kwargs
log = logging.getLogger(module+sys._getframe().f_code.co _name )
#loglevel = kwargs.get('loglevel', logging.WARN)
log.setLevel(loglevel)
# input = kwargs.get('input', 0.0)
log.debug("Using input %s" % str(input))
time.sleep(input)
return 3*input
def test2(loglevel=False):
log = logging.getLogger(module+sys._getframe().f_code.co _name )
if loglevel is not False: log.setLevel(loglevel)
else:log.setLevel(logging.WARN)
maxThreads=5
# Set up two queues one for sending request data (req) one for
getting response to the request (res)
reqQ = Queue.Queue()
resQ = Queue.Queue()
# Push some data onto the reqestQ end it with None
import random
for x in range(5): reqQ.put({'input':random.random(),
'loglevel':loglevel})
for n in range(maxThreads): reqQ.put(None)
# Start Up some threads to do some work
for n in range(maxThreads):
t = myThread(id=n,loglevel=logging.INFO, function=func2,
requestQ=reqQ, responseQ=resQ).start()
# Collect the results
results = 0
while 1:
try:
data = resQ.get()
if data is None:
break
else:
results += data
except:
break
print results
def main(loglevel=False):
""" """
# Setup Logging
log = logging.getLogger(module+sys._getframe().f_code.co _name )
if loglevel is not False: log.setLevel(loglevel)
else:log.setLevel(logging.WARN)
# test1(loglevel)
test2(loglevel)
if __name__ == '__main__':
sys.exit(main(loglevel=LOGLEVEL))
kwargs is not a built in name--it's a made up name used in the
docs. Would you expect this function to work:
def somefunc(x=10, y=20):
print a
The best way to figure out a feature of a programming language that
you don't understand is not in the middle of some complex program.
Instead, you should begin a new program, or if you are smart you will
already have several blank programs already created waiting in the
wings for testing purposes. In the new program, you can play around
with functions, default values and catch all parameters like *a and
**b to figure out how they work.
En Tue, 01 May 2007 18:42:18 -0300, rh0dium <st**********@gmail.com>
escribió:
Let me expand a bit more - I am working on a threading class and I
want to be able to push on the Queue a list of args. If you run the
following program - I am failing to understand how to push items onto
the queue in a manner so that func2 recognizes them as kwargs not as
args. Can anyone help me with this.
You can put a tuple in the queue: the first item being the positional
arguments, the second item being a dictionary used as keyword arguments.
pyfrom Queue import Queue
pyq = Queue()
pyq.put(((1,2,3),{"a":100, "b":200}))
pyq.put(((),dict(boca=2,racing=2)))
pydef f(*args, **kw):
.... print "args", args
.... for k in kw:
.... print k, kw[k]
....
pyitem = q.get()
pyf(*item[0], **item[1])
args (1, 2, 3)
a 100
b 200
pyargs, kw = q.get()
pyf(*args, **kw)
args ()
racing 2
boca 2
--
Gabriel Genellina This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Arturo DiDonna |
last post by:
Hello everyone. I am trying to compile someone else code and I am
stuck with compilation problems using
the g++ 3.3 compiler. Basically, when compiling the following code, I
get this error...
|
by: clintonG |
last post by:
After the last six days trying to download VS2005 Professional it seems
there may be a problem with my instance of the File Transfer Manager (FTM)
or somewhere in the network in between. I can't...
|
by: python |
last post by:
hello and thanks for reading this,
i have been a dos/windows user using some form of the basic language for 30 years now.
i own and run a small programming company and there is one feature that...
|
by: don |
last post by:
I have two existing interfaces having methods with same names. Now I have to
implement both intrfaces in one class. Is there any way I could implement
methods with same names in both interfaces...
|
by: manstey |
last post by:
Hi,
I have a text file called a.txt:
# comments
I read it using this:
|
by: tshad |
last post by:
I am trying to build my own DB Class and am trying to set up my parameter
list in an array of SqlParameter objects.
In my regular code I would do something like:...
|
by: darren |
last post by:
Hi everybody
I am a bit confused about functions that take pointers / references as
parameters.
Are these two functions the same?
void myFunc(int& anInt);
void myFunct(int* anInt(0;
Would...
|
by: nidaar |
last post by:
From a security point of view, is accepting wildcards like "%" in
input parameters of stored procedures against any best practices?
As an example, if a user defined function uses "Productname...
|
by: ehud37new |
last post by:
this script work fine in IE but not in FireFox
where is the problem?
here is the script
/*------------------------------------------------------------------
File: menu.js
...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |