What is not working with my "map" usage?

2018-09-21 Thread Viet Nguyen via Python-list
Hi,

I want to add up all of the list elements.  But when I use the "map" function, 
it didn't seem to work as I expect.  Could someone point out how "map" can be 
applied here then?

def add_all_elements (*args):  
total = 0
for i in args:
   print(type(i))
   print("i = %s" % i)
   print("BEFORE total = %s" % total)
   total += int(i)
   print("AFTER total = %s\n" % total)
print("FINAL total = %s\n" % total)
return total


alist = ['2', '09', '49']


## this one works Okay

add_all_elements(*alist)

i = 2
BEFORE total = 0
AFTER total = 2


i = 09
BEFORE total = 2
AFTER total = 11


i = 49
BEFORE total = 11
AFTER total = 60

FINAL total = 60


## Why is this NOT Okay when I use map ??  What must I change ?

>>> list(map(add_all_elements,alist))

i = 2
BEFORE total = 0
AFTER total = 2

FINAL total = 2


i = 09
BEFORE total = 0
AFTER total = 9

FINAL total = 9


i = 49
BEFORE total = 0
AFTER total = 49

FINAL total = 49

[2, 9, 49]


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


Re: Why emumerated list is empty on 2nd round of print?

2018-09-06 Thread Viet Nguyen via Python-list
On Thursday, September 6, 2018 at 12:12:20 PM UTC-7, David Raymond wrote:
> The actual "enumerate" object is really just holding a current index and a 
> reference to the original list. So if you alter the original list while 
> you're iterating through it you'll see the changes. If you want a full copy 
> then you can just wrap it with list()
> 
> Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit 
> (AMD64)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> numList = [2, 7, 22, 30, 1, 8]
> >>> aList = enumerate(numList)
> >>> aList.__next__()
> (0, 2)
> >>> numList[1] = 5
> >>> aList.__next__()
> (1, 5)
> >>> aList2 = list(enumerate(numList))
> >>> aList2
> [(0, 2), (1, 5), (2, 22), (3, 30), (4, 1), (5, 8)]
> >>> numList[3] = -12
> >>> aList2
> [(0, 2), (1, 5), (2, 22), (3, 30), (4, 1), (5, 8)]
> >>> aList.__next__()
> (2, 22)
> >>> aList.__next__()
> (3, -12)
> >>> aList.__next__()
> (4, 1)
> >>> aList.__next__()
> (5, 8)
> >>> aList.__next__()
> Traceback (most recent call last):
>   File "", line 1, in 
> StopIteration
> >>>
> 
> 
> -Original Message-
> From: Python-list 
> [mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
> Viet Nguyen via Python-list
> Sent: Thursday, September 06, 2018 2:50 PM
> To: python-list@python.org
> Subject: Re: Why emumerated list is empty on 2nd round of print?
> 
> On Thursday, September 6, 2018 at 10:34:19 AM UTC-7, Chris Angelico wrote:
> > On Fri, Sep 7, 2018 at 3:26 AM, Viet Nguyen via Python-list
> >  wrote:
> > >>>> numList
> > > [2, 7, 22, 30, 1, 8]
> > >
> > >>>> aList = enumerate(numList)
> > >
> > >>>> for i,j in aList:print(i,j)
> > >
> > > 0 2
> > > 1 7
> > > 2 22
> > > 3 30
> > > 4 1
> > > 5 8
> > >
> > >>>> for i,j in aList:print(i,j)
> > >
> > >>>>
> > 
> > Because it's not an enumerated list, it's an enumerated iterator.
> > Generally, you'll just use that directly in the loop:
> > 
> > for i, value in enumerate(numbers):
> > 
> > There's generally no need to hang onto it from one loop to another.
> > 
> > ChrisA
> 
> Thanks ChrisA. If I do this "aList = enumerate(numList)", isn't it stored 
> permanently in aList now?  I see your point to use it directly, but just in 
> case I do need to hang onto it from one loop to another, then how is that 
> done?   Anyway I think I'm ok and I got what I need for now.
> -- 
> https://mail.python.org/mailman/listinfo/python-list

Very clear and good examples!  Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why emumerated list is empty on 2nd round of print?

2018-09-06 Thread Viet Nguyen via Python-list
On Thursday, September 6, 2018 at 10:34:19 AM UTC-7, Chris Angelico wrote:
> On Fri, Sep 7, 2018 at 3:26 AM, Viet Nguyen via Python-list
>  wrote:
> >>>> numList
> > [2, 7, 22, 30, 1, 8]
> >
> >>>> aList = enumerate(numList)
> >
> >>>> for i,j in aList:print(i,j)
> >
> > 0 2
> > 1 7
> > 2 22
> > 3 30
> > 4 1
> > 5 8
> >
> >>>> for i,j in aList:print(i,j)
> >
> >>>>
> 
> Because it's not an enumerated list, it's an enumerated iterator.
> Generally, you'll just use that directly in the loop:
> 
> for i, value in enumerate(numbers):
> 
> There's generally no need to hang onto it from one loop to another.
> 
> ChrisA

Thanks ChrisA. If I do this "aList = enumerate(numList)", isn't it stored 
permanently in aList now?  I see your point to use it directly, but just in 
case I do need to hang onto it from one loop to another, then how is that done? 
  Anyway I think I'm ok and I got what I need for now.
-- 
https://mail.python.org/mailman/listinfo/python-list


Why emumerated list is empty on 2nd round of print?

2018-09-06 Thread Viet Nguyen via Python-list
>>> numList
[2, 7, 22, 30, 1, 8]

>>> aList = enumerate(numList)

>>> for i,j in aList:print(i,j)

0 2
1 7
2 22
3 30
4 1
5 8

>>> for i,j in aList:print(i,j)

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


Re: Is there a way to display source code for Python function?

2014-10-03 Thread Viet Nguyen
On Thursday, October 2, 2014 10:34:15 PM UTC-7, Viet Nguyen wrote:
 Hi,
 
 
 
 When I am debug mode, is there some command which will help display the 
 source code for a Python function of interest?  Much like you'd use info 
 proc to display contents of Tcl proc.
 
 
 
 Thanks,
 
 Viet

I tried this:
 def func(a):
...   a = 'abcd'


 inspect.getsource(func)
Traceback (most recent call last):
  File stdin, line 1, in module
  File /sw/packages/python/current/lib/python2.7/inspect.py, line 701, in 
getsource
lines, lnum = getsourcelines(object)
  File /sw/packages/python/current/lib/python2.7/inspect.py, line 690, in 
getsourcelines
lines, lnum = findsource(object)
  File /sw/packages/python/current/lib/python2.7/inspect.py, line 538, in 
findsource
raise IOError('could not get source code')
IOError: could not get source code

 inspect.getsourcelines(func)
Traceback (most recent call last):
  File stdin, line 1, in module
  File /sw/packages/python/current/lib/python2.7/inspect.py, line 690, in 
getsourcelines
lines, lnum = findsource(object)
  File /sw/packages/python/current/lib/python2.7/inspect.py, line 538, in 
findsource
raise IOError('could not get source code')
IOError: could not get source code

What is wrong?

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


Re: Is there a way to display source code for Python function?

2014-10-03 Thread Viet Nguyen
On Thursday, October 2, 2014 10:47:28 PM UTC-7, Ian wrote:
 On Thu, Oct 2, 2014 at 11:34 PM, Viet Nguyen
 
 vhnguy...@yahoo.com.dmarc.invalid wrote:
 
  Hi,
 
 
 
  When I am debug mode, is there some command which will help display the 
  source code for a Python function of interest?  Much like you'd use info 
  proc to display contents of Tcl proc.
 
 
 
  Thanks,
 
  Viet
 
 
 
 You can use inspect.getsource() to get the source code for a function,
 
 class, or module. The source must be available at whatever location
 
 the module was imported from, and of course it won't work for anything
 
 implemented in C.

Hi,
I tried:

def func(a):
  a = 'abcd'

 inspect.getsource(func)
Traceback (most recent call last):
  File stdin, line 1, in module
  File /sw/packages/python/current/lib/python2.7/inspect.py, line 701, in 
getsource
lines, lnum = getsourcelines(object)
  File /sw/packages/python/current/lib/python2.7/inspect.py, line 690, in 
getsourcelines
lines, lnum = findsource(object)
  File /sw/packages/python/current/lib/python2.7/inspect.py, line 538, in 
findsource
raise IOError('could not get source code')
IOError: could not get source code


What is wrong?

Thanks,
Viet

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


Re: Is there a way to display source code for Python function?

2014-10-03 Thread Viet Nguyen
On Friday, October 3, 2014 12:48:08 AM UTC-7, Peter Otten wrote:
 Viet Nguyen wrote:
 
 
 
  On Thursday, October 2, 2014 10:34:15 PM UTC-7, Viet Nguyen wrote:
 
  Hi,
 
  
 
  
 
  
 
  When I am debug mode, is there some command which will help display the
 
  source code for a Python function of interest?  Much like you'd use info
 
  proc to display contents of Tcl proc.
 
  
 
  
 
  
 
  Thanks,
 
  
 
  Viet
 
  
 
  I tried this:
 
  def func(a):
 
  ...   a = 'abcd'
 
  
 
  
 
  inspect.getsource(func)
 
  Traceback (most recent call last):
 
File stdin, line 1, in module
 
File /sw/packages/python/current/lib/python2.7/inspect.py, line 701,
 
in getsource
 
  lines, lnum = getsourcelines(object)
 
File /sw/packages/python/current/lib/python2.7/inspect.py, line 690,
 
in getsourcelines
 
  lines, lnum = findsource(object)
 
File /sw/packages/python/current/lib/python2.7/inspect.py, line 538,
 
in findsource
 
  raise IOError('could not get source code')
 
  IOError: could not get source code
 
  
 
  inspect.getsourcelines(func)
 
  Traceback (most recent call last):
 
File stdin, line 1, in module
 
File /sw/packages/python/current/lib/python2.7/inspect.py, line 690,
 
in getsourcelines
 
  lines, lnum = findsource(object)
 
File /sw/packages/python/current/lib/python2.7/inspect.py, line 538,
 
in findsource
 
  raise IOError('could not get source code')
 
  IOError: could not get source code
 
  
 
  What is wrong?
 
 
 
 The source of func is compiled and immediately discarded by the interactive 
 
 interpreter. inspect.getsource() only works if the source code is available 
 
 (as a module):
 
 
 
 $ cat ham.py
 
 def spam():
 
 return 42
 
 $ python3
 
 Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
 
 [GCC 4.8.2] on linux
 
 Type help, copyright, credits or license for more information.
 
  import ham, inspect
 
  print(inspect.getsource(ham.spam))
 
 def spam():
 
 return 42
 
 
 
  
 
 
 
 There is an alternative interactive interpreter called ipython that allows 
 
 you to retrieve a function definition:
 
 
 
 In [1]: def foo():
 
...: return bar
 
...: 
 
 
 
 In [2]: %psource foo
 
 def foo():
 
 return bar
 
 
 
 In [3]:

Thanks Peter!
-- 
https://mail.python.org/mailman/listinfo/python-list


Is there a way to display source code for Python function?

2014-10-02 Thread Viet Nguyen
Hi,

When I am debug mode, is there some command which will help display the source 
code for a Python function of interest?  Much like you'd use info proc to 
display contents of Tcl proc.

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


Why captured match is displayed as part of pexpect .after ?

2014-09-13 Thread Viet Nguyen
Hi,

If any is familiar with pexpect, please help to point out why my script seems 
to fail to capture the desired text.

Here, I want to log into a server 172.27.161.19.  Once I see Username: , I 
will type in my userid admin.

The problem here is I have a list of keywords for pexpect to match before 
returning the matched item for me.  The list consists of following list items:

expects = ['yes', '.*Username: ', '.*login: |.*Login: ', 'SY-FAR-1', 
'SY-FAR-1#', 'Password: |password: ', '.*Press RETURN to get started']

Here, it seems my script found Username:  but it got displayed as PART OF 
_pexpect_session.after  (and this is what I don't understand).  The .before 
or .after data should consist of match before and after the desired keyword 
only.  

= Manual Execution ===
telnet 172.27.161.19 
Trying 172.27.161.19...
Connected to 172.27.161.19.
Escape character is '^]'.


User Access Verification

Username: admin
Password: 

SY-FAR-1en
SY-FAR-1#
=== Python Script Snippet ===

expects = ['yes', '.*Username: ', '.*login: |.*Login: ', 'SY-FAR-1', 
'SY-FAR-1#', 'Password: |password: ', '.*Press RETURN to get started']

i = -1   # reset its value
print *step 1*before match, i = %s % i


self._pexpect_session.timeout = 80
cmd = admin
self._log(Executing CLI: '{0}'.format(cmd))
self._pexpect_session.sendline(cmd)


i = self._pexpect_session.expect(expects)
print *step 2* found index i = %s % i 

print *step 3* after match, exec_cmd expects (%s) = % i, expects
print *step 4* exec_cmd match before: '%s' % 
self._pexpect_session.before
print *step 5* exec_cmd match after: '%s' % 
self._pexpect_session.after 

=== Actual Script Output ==

*step1* before match, i = -1
Executing CLI: 'admin'
*step2* found index i = 1
*step3* after match, exec_cmd expects (1) = ['yes', '.*Username: ', '.*login: 
|.*Login: ', 'SY-FAR-1', 'SY-FAR-1#', 'Password: |password: ', '.*Press RETURN 
to get started']

*step4* exec_cmd match before: ''
*step5* exec_cmd match after: '
Username: '

Appreciate any input or explanation.

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


Re: Why command os.popen works in python interactive mode but not in script debugger mode?

2014-09-12 Thread Viet Nguyen
On Thursday, September 11, 2014 10:15:57 PM UTC-7, Viet Nguyen wrote:
 Can anyone give me hint or reason why same command behaves differently in 
 debugger mode from interactive mode:
 
 
 
 From interactive mode:
 
 
 
  import os
 
  p = os.popen('date')
 
  p.read()
 
 'Thu Sep 11 11:18:07 PDT 2014\n'
 
 
 
 But from debugger mode in a script:
 
  import os
 
 (Pdb) p = os.popen('date')
 
 *** SyntaxError: SyntaxError('invalid syntax', ('string', 1, 1, = 
 os.popen('date')))
 
 
 
 
 
 Can anyone help me why there is syntax here?
 
 
 
 Thanks,
 
 Viet

Thank you for your help.  That resolved the issue.
-- 
https://mail.python.org/mailman/listinfo/python-list


Why command os.popen works in python interactive mode but not in script debugger mode?

2014-09-11 Thread Viet Nguyen
Can anyone give me hint or reason why same command behaves differently in 
debugger mode from interactive mode:

From interactive mode:

 import os
 p = os.popen('date')
 p.read()
'Thu Sep 11 11:18:07 PDT 2014\n'

But from debugger mode in a script:
 import os
(Pdb) p = os.popen('date')
*** SyntaxError: SyntaxError('invalid syntax', ('string', 1, 1, = 
os.popen('date')))


Can anyone help me why there is syntax here?

Thanks,
Viet



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