multiprocess passing arguments double asterisks

2016-10-23 Thread pic8690
import multiprocessing as mp
 
def bar(**kwargs):
   for a in kwargs:
  print a,kwargs[a]
 
arguments={'name':'Joe','age':20}
p=mp.Pool(processes=4)
p.map(bar,**arguments)
p.close()
p.join()



Errors:

Traceback (most recent call last):
  File "post.py", line 9, in 
p.map(bar,**arguments)
TypeError: map() got an unexpected keyword argument 'age'

How do I pass the dictionary arguments?
-- 
https://mail.python.org/mailman/listinfo/python-list


multiprocess passing arguments double asterisks

2016-10-23 Thread pic8690
import multiprocessing as mp
 
def bar(**kwargs):
   for a in kwargs:
  print a,kwargs[a]
 
arguments={'name':'Joe','age':20}
p=mp.Pool(processes=4)
p.map(bar,**arguments)
p.close()
p.join()



Errors:

Traceback (most recent call last):
  File "post.py", line 9, in 
p.map(bar,**arguments)
TypeError: map() got an unexpected keyword argument 'age'

How do I pass the dictionary arguments?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Internet Data Handling » mailbox

2016-10-23 Thread andy
Sat, 22 Oct 2016 19:41:45 -0400 wrote Adam Jensen:

> On 10/22/2016 05:47 AM, andy wrote:
>> I would type: help(mailbox) after importing it.
> 
> I guess the output of that might be more meaningful once I understand
> the underlying structures and conventions.

yes - you are right. fortunatelly python autors have thought about 
'documntation strings' and 'coding style', the syntax of python itself 
helps reading source code (indentation). this allows using auto-
documentation features like help(...).

when i don't know enough about a module like 'mailbox' , i first try a 
file search for the source code on the local system: i.e. 'locate 
mailbox.py' on a linux system. possibly i have to install the module 
first when there is nothing found (using pip or package manager).

this yields on my system ('sudo updatedb' - for updating the db) to this 
result:

/usr/lib/python2.7/mailbox.py
/usr/lib/python2.7/mailbox.pyc
/usr/lib/python3.5/mailbox.py

i can read the source file with: 'less /usr/lib/python3.5/mailbox.py'.
within the sourcefile i can study the imports and data structures.

Other sources of information: doc.python.org - even with search:
https://docs.python.org/3/search.html?q=mailbox

and finally all these mail-modules should follow the RFCs ;-)
https://tools.ietf.org/html/rfc2822

best regards
andy
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Odd name shadowing in comprehension

2016-10-23 Thread eryk sun
On Sun, Oct 23, 2016 at 6:15 AM, Steve D'Aprano
 wrote:
> On Sun, 23 Oct 2016 01:15 pm, eryk sun wrote:
>
>> I meant the behavior seems to have been copied to align with generator
>> expressions, even though the cited rationale doesn't apply. I'm not
>> saying this is wrong. It's useful that the expression for the outer
>> iterator is evaluated in the defining scope. However, it's only
>> documented for generator expressions, in 6.2.8. The documentation for
>> comprehensions in 6.2.4 makes no mention of it.
>
> 6.2.8? 6.2.4? What are these references to?

They're section numbers in the Python 3 language reference. Chapter 6
covers expressions.

https://docs.python.org/3/reference/expressions
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: function call questions

2016-10-23 Thread chenyong20000
在 2016年10月22日星期六 UTC+8下午9:15:06,Frank Millman写道:
> wrote in message
> news:9c91a4cf-1f3e-43b3-b75c-afc96b0b4...@googlegroups.com...
> 
> > I have read Anssi's post already before I sent the post. To be frankly, I
> can't understand why he got the right answer. I'm sorry for my silly. "So
> when we assign to r again, it's the empty dict inside t (the one accessed
> by key 'a')". I do can't understand why this happens. that is the reason why
> I have asked for this once again and again. There must be some import point
> I missed but I don't what is it.
> 
> Let's try this -
> 
> >>> t = {}
> >>> r = t
> >>> r = r.setdefault('a', {})
> >>> t
> {'a': {}}
> 
> I think you are happy up to this point.
> 
> We now have three objects -
> 
> "t" is a dictionary
> 'a' is a key in the dictionary
> {} is the value associated with the key 'a' in "t"
> 
> I think you are happy up to this point.
> 
> The question is, what is "r"?
> 
> Before the assignment, "r" was a reference to the dictionary referenced by 
> "t".
> 
> After the assignment, "r" no longer refers to "t". It is now a reference to 
> the
> third object listed above, the {} that is the value associated with the key 
> 'a'.
> 
> >>> t
> {'a': {}}
> >>> t['a']
> {}
> >>> r
> {}
> >>> t['a] is r
> True
> 
> Keep looking at this until it sinks in. "r" and "t['a']" are *the same 
> object*. We just have two ways of accessing it.
> 
> Try adding some key/values to the empty dictionary -
> 
> >>> r['x'] = 99
> >>> r
> {'x': 99}
> >>> t['a']
> {'x': 99}
> >>> t
> {'a': {'x': 99}}
> 
> I will pause at this point, and give you a moment to absorb that.
> 
> Hopefully, the penny will drop and everything will become clear.
> 
> If not, let us know which of the above steps you do not understand.
> 
> Good luck - keep plugging away, and you will get there :-)
> 
> Frank
> 
> P.S. I assume you understand that the lines prefixed with '>>>' are to be 
> entered while in the python interpreter. It is really important that you 
> type these lines in yourself and examine the results.

Hi Frank,

I got it this time. Thanks very much for your help. I thought r is an empty 
dictionary without any connection to t before. Now I know that happened. Thanks.


regards
skyworld
-- 
https://mail.python.org/mailman/listinfo/python-list


MySQL connector issue

2016-10-23 Thread Joseph L. Casale
I have some code that I am testing on Windows without c extensions which
runs on a RHEL server with c extensions. In a simplified test case as follows:

connection = mysql.connector.connect(...)
cursor = connection.cursor(cursor_class=MySQLCursorDict)
while True:
cursor.execute('SELECT foo,biz FROM bar WHERE baz IS NULL)
rows = cursor.fetchall()
print(rows)

cursor.execute('UPDATE bar SET baz=42 WHERE baz IS NULL')
connection.commit()

sleep(.5)

This works on Windows, the select query consistently returns new results as they
appear in the database when generated by other applications.

However on the RHEL server, the initial select only produces a result on the 
first
iteration and then as new results are written to the database, the select does 
not
find results?

What might be the issue?

Thanks,
jlc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: MySQL connector issue

2016-10-23 Thread Peter Otten
Joseph L. Casale wrote:

> I have some code that I am testing on Windows without c extensions which
> runs on a RHEL server with c extensions. In a simplified test case as
> follows:
> 
> connection = mysql.connector.connect(...)
> cursor = connection.cursor(cursor_class=MySQLCursorDict)
> while True:
> cursor.execute('SELECT foo,biz FROM bar WHERE baz IS NULL)
> rows = cursor.fetchall()
> print(rows)
> 
> cursor.execute('UPDATE bar SET baz=42 WHERE baz IS NULL')
> connection.commit()
> 
> sleep(.5)
> 
> This works on Windows, the select query consistently returns new results
> as they appear in the database when generated by other applications.
> 
> However on the RHEL server, the initial select only produces a result on
> the first iteration and then as new results are written to the database,
> the select does not find results?
> 
> What might be the issue?

Perhaps you simplified too much, but changes between the select and the 
update could be lost. I think you need at least three states:

1 mark rows where baz is null (by setting baz to some value other than NULL 
or 42, 24, say: set baz = 24 where baz is NULL)
2 show marked rows (select ... where baz = 24)
3 mark rows as seen (set baz = 42 where baz = 24)


-- 
https://mail.python.org/mailman/listinfo/python-list


RE: MySQL connector issue

2016-10-23 Thread Joseph L. Casale
> Perhaps you simplified too much, but changes between the select and the 
> update could be lost. I think you need at least three states:
>
> 1 mark rows where baz is null (by setting baz to some value other than NULL 
> or 42, 24, say: set baz = 24 where baz is NULL)
> 2 show marked rows (select ... where baz = 24)
> 3 mark rows as seen (set baz = 42 where baz = 24)

Hi Peter,
The UPDATE happens only if rows were found, the sleep is much longer as it
is expected that another application adds rows satisfying the SELECT.

It really is that simple which is why I am baffled. Given the throughput is so
low, if I close the cursor and connection at the end of loop and instantiate 
them
both at the start of the loop, it works as expected but that's obviously not 
optimal.

Thanks,
jlc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: MySQL connector issue

2016-10-23 Thread Chris Angelico
On Mon, Oct 24, 2016 at 3:46 AM, Joseph L. Casale
 wrote:
> It really is that simple which is why I am baffled. Given the throughput is so
> low, if I close the cursor and connection at the end of loop and instantiate 
> them
> both at the start of the loop, it works as expected but that's obviously not 
> optimal.

Interesting. Generally, I allocate cursors exactly at the same time as
I open transactions; not sure if this works with the mysql connector,
but with psycopg2 (PostgreSQL), my code looks like this:

with conn, conn.cursor() as cur:
cur.execute(...)
... = cur.fetchall()

The 'with' block guarantees that (a) the cursor will be closed and all
resources freed, and (b) the transaction will be committed or rolled
back (on exception, roll back, otherwise commit), at the unindent.
It's a nice, clean way to operate. Never had problems with it.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Obtain javascript result

2016-10-23 Thread eproser
Ok, I solved to this way:

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://www.betexplorer.com/soccer/russia/youth-\league/matchdetails.php?matchid=rLu2Xsdi')

pg_src = driver.page_source
driver.close()
soup = BeautifulSoup(pg_src, 'html.parser')
# start from here I do something with soup ...

Windows 10 / Python 3.5.2

Thanks


-- 
https://mail.python.org/mailman/listinfo/python-list


RE: MySQL connector issue

2016-10-23 Thread Joseph L. Casale
> Interesting. Generally, I allocate cursors exactly at the same time as I open 
> transactions;
> not sure if this works with the mysql connector, but with psycopg2 
> (PostgreSQL), my code looks like this:
> 
> with conn, conn.cursor() as cur:
> cur.execute(...)
> ... = cur.fetchall()
>
> The 'with' block guarantees that (a) the cursor will be closed and all 
> resources freed, and (b) the
> transaction will be committed or rolled back (on exception, roll back, 
> otherwise commit), at the unindent.
> It's a nice, clean way to operate. Never had problems with it.

Ditto, however the official Oracle python module lacks context managers.

Regardless, using try/finally and creating and closing cursors for both the 
query and the update
still did not help. The connection must be reset in order for the query to see 
new results.
That sounds ridiculous, certainly I overlooking something in a connection 
parameter possibly?

Thanks,
jlc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Internet Data Handling » mailbox

2016-10-23 Thread Jason Friedman
>
> for message in mailbox.mbox(sys.argv[1]):
> if message.has_key("From") and message.has_key("To"):
> addrs = message.get_all("From")
> addrs.extend(message.get_all("To"))
> for addr in addrs:
> addrl = addr.lower()
> if addrl.find(name) > 0:
> print message
> break
> -


I usually see

if addrl.find(name) > 0:

written as

if name in addrl:
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: multiprocess passing arguments double asterisks

2016-10-23 Thread Thomas Nyberg

On 10/23/2016 03:12 AM, pic8...@gmail.com wrote:

import multiprocessing as mp

def bar(**kwargs):
   for a in kwargs:
  print a,kwargs[a]

arguments={'name':'Joe','age':20}
p=mp.Pool(processes=4)
p.map(bar,**arguments)
p.close()
p.join()


What are you trying to do? The map method is similar to the map built-in:


https://docs.python.org/2/library/multiprocessing.html#multiprocessing.pool.multiprocessing.Pool.map
https://docs.python.org/2/library/functions.html#map

map(function, iterable, ...)
Apply function to every item of iterable and return a list of the results...

You can't apply it to keyword arguments like this. There are some 
different SO threads talking about this sort of thing:



http://stackoverflow.com/questions/13499824/using-python-map-function-with-keyword-arguments

http://stackoverflow.com/questions/10212445/python-map-list-item-to-function-with-arguments

http://stackoverflow.com/questions/16874244/python-map-and-arguments-unpacking

Maybe those (especially the last one) are helpful.

Cheers,
Thomas
--
https://mail.python.org/mailman/listinfo/python-list


Re: Quick way to calculate lines of code/comments in a collection of Python scripts?

2016-10-23 Thread Tomasz Rola
On Wed, Oct 05, 2016 at 01:56:59PM -0400, Malcolm Greene wrote:
> Looking for a quick way to calculate lines of code/comments in a
> collection of Python scripts. This isn't a LOC per day per developer
> type analysis - I'm looking for a metric to quickly judge the complexity
> of a set of scripts I'm inheriting.
> 
> Thank you,
> Malcolm

A bit more than what you asked for (and sorry for being late) but I
find sloccount quite good. Or at least interesting (computes sloc and
some stats about project, given project dir or a single file with
code):

http://www.dwheeler.com/sloccount/

-- 
Regards,
Tomasz Rola

--
** A C programmer asked whether computer had Buddha's nature.  **
** As the answer, master did "rm -rif" on the programmer's home**
** directory. And then the C programmer became enlightened...  **
** **
** Tomasz Rola  mailto:tomasz_r...@bigfoot.com **
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Internet Data Handling » mailbox

2016-10-23 Thread Jon Ribbens
On 2016-10-23, Jon Ribbens  wrote:
> On 2016-10-23, Jason Friedman  wrote:
>>>
>>> for message in mailbox.mbox(sys.argv[1]):
>>> if message.has_key("From") and message.has_key("To"):
>>> addrs = message.get_all("From")
>>> addrs.extend(message.get_all("To"))
>>> for addr in addrs:
>>> addrl = addr.lower()
>>> if addrl.find(name) > 0:
>>> print message
>>> break
>>> -
>>
>> I usually see
>>
>> if addrl.find(name) > 0:
>>
>> written as
>>
>> if name in addrl:
>
> I suppose technically it would be:
>
>   iaddrf name in addrl[1:]:

s/iaddrf/if/ obviously!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Internet Data Handling » mailbox

2016-10-23 Thread Jon Ribbens
On 2016-10-23, Jason Friedman  wrote:
>>
>> for message in mailbox.mbox(sys.argv[1]):
>> if message.has_key("From") and message.has_key("To"):
>> addrs = message.get_all("From")
>> addrs.extend(message.get_all("To"))
>> for addr in addrs:
>> addrl = addr.lower()
>> if addrl.find(name) > 0:
>> print message
>> break
>> -
>
> I usually see
>
> if addrl.find(name) > 0:
>
> written as
>
> if name in addrl:

I suppose technically it would be:

  iaddrf name in addrl[1:]:

-- 
https://mail.python.org/mailman/listinfo/python-list


exist loop by pressing esc

2016-10-23 Thread chris alindi
simple while loop range(10) if user press esc exits loop
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Has any one automated the vmware-vra setup using python?

2016-10-23 Thread grant . a . orchard
On Thursday, 6 October 2016 04:36:15 UTC+11, Robert Clove  wrote:
> 

Not yet. There are a few people working towards it though.

Grant
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: exist loop by pressing esc

2016-10-23 Thread Steve D'Aprano
On Mon, 24 Oct 2016 08:34 am, chris alindi wrote:

> simple while loop range(10) if user press esc exits loop


Your post is not a question nor even a grammatical sentence? Would you like
us to guess what you mean? Or perhaps you could ask your question in actual
proper sentences?

If English is not your first language, please say so, and try your best.

It may help if you show some code, and explain what result you want.



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

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: exist loop by pressing esc

2016-10-23 Thread Wildman via Python-list
On Sun, 23 Oct 2016 14:34:29 -0700, chris alindi wrote:

> simple while loop range(10) if user press esc exits loop

If I understand you correctly you want to exit a while loop
with the ESC key.  That can be done but it depends on the
platform.  For Windows use this: (not tested)

import msvcrt

while something:
if msvcrt.kbhit() and msvcrt.getch() == chr(27):
break


In Linux you need to install getch:

https://pypi.python.org/pypi/getch

Download it here:

https://pypi.python.org/pypi/getch#downloads

Extract the tar.gz file.  A directory called getch-1.0
will be created.  Open a terminal in that directory and
enter this...

sudo python setup.py install

Here is a code example: (tested)

import getch

while something:
if getch.getch() == '\x1b':
break

-- 
 GNU/Linux user #557453
May the Source be with you.
-- 
https://mail.python.org/mailman/listinfo/python-list