Re: Python Style Question

2014-10-29 Thread Rafael Romero Carmona
Hi, first in Python 2.7.6 and Python 3.4.0 list haven't got any add
function but they have append.

I think you could do better with something like

==
import json
l = [1, -1, 0, '1', '-1', '0', json.dumps(-1), json.dumps(1),
json.dumps(0), 'x', 'sqjklsqjk__', (1, 2)]

values = []

for c in l:
try:
c_int = int(c)
except ValueError:
pass
except TypeError:
pass
else:
values.append(c_int)
continue
print(values)
==

The code has been tested in Python 2.7.6 and 3.4 and returns [1, -1,
0, 1, -1, 0, -1, 1, 0]

You don't need to do two try because you can process both exceptions
in the same way. You don't really need to do json.loads because if you
have a json string which is an integer, you could do that directly
with int(c) which can take a string and transform in an integer.

With ValueError it captures the exception when it tries to transform
the characters' strings and with TypeError it captures the exception
when it tries to work with the tuples.

Have a good day and hope it works for you!

2014-10-29 11:42 GMT+01:00 Anton anton.schattenf...@gmail.com:
 Let's say I have an incoming list of values *l*. Every element of *l* can be 
 one of the following options:
 1) an integer value
 2) a string in form of 'int_value', e.g. '7'
 3) a string with a json serialization of an integer value, e.g. '7'
 4) something else that should be ignored

 I need to transform this list into another list with values from options 
 1)-3) coerced to int. The code below should do this.


 Variant 1
 ===

 values = []
 for c in l:
 # Case 1) or 2)
 try:
 c_int = int(c)
 except ValueError:
 pass
 else:
 values.add(c_int)
 continue

 # Case 3)
 try:
 c_int = int(json.loads(c))
 except ValueError:
 pass
 else:
 values.add(c_int)
 continue

 ===

 Is this code ugly?
 Does it follow EAFP?
 Am I missing something in language best practice?

 Or maybe below is more preferable way with a nested try...except clause?

 Variant 2
 ===
 values = []
 for c in l:
 # Case 1) or 2)
 try:
 c_int = int(c)
 except ValueError:

 # Case 3)
 try:
 c_int = int(json.loads(c))
 except ValueError:
 pass
 else:
 values.add(c_int)
 continue

 else:
 values.add(c_int)
 continue
 ===

 Thanks,
 Anton.


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


Re: Python Style Question

2014-10-29 Thread Rafael Romero Carmona
2014-10-29 12:25 GMT+01:00 Martin Kemp martin.k...@ingg.com:
 -BEGIN PGP SIGNED MESSAGE-
 Hash: SHA1

 On 29/10/2014 10:45, Anton wrote:
 Let's say I have an incoming list of values *l*. Every element of
 *l* can be one of the following options: 1) an integer value 2) a
 string in form of 'int_value', e.g. '7' 3) a string with a json
 serialization of an integer value, e.g. '7' 4) something else
 that should be ignored

 I need to transform this list into another list with values from
 options 1)-3) coerced to int. The code below should do this.


 Variant 1 ===

 values = [] for c in l: # Case 1) or 2) try: c_int = int(c) except
 ValueError: pass else: values.add(c_int) continue

 # Case 3) try: c_int = int(json.loads(c)) except ValueError: pass
 else: values.add(c_int) continue

 ===

 Is this code ugly? Does it follow EAFP? Am I missing something in
 language best practice?

 Or maybe below is more preferable way with a nested try...except
 clause?

 Variant 2 === values = [] for c in l: # Case 1) or 2) try: c_int =
 int(c) except ValueError:  # Case 3) try: c_int =
 int(json.loads(c)) except ValueError: pass else: values.add(c_int)
 continue  else: values.add(c_int) continue ===

 Thanks, Anton.



 Your first example is perfectly fine and is EAFP


Actually it doesn't work because there is no add function and it
doesn't catch the TypeError function to ignore other exceptions than
ValueError. Doesn't it? I tested in Python 2.7.6 and 3.4.

 Personally, I prefer to avoid nesting when it's not necessary.

 - --
 Martin Kemp (martin.k...@ingg.com)
 -BEGIN PGP SIGNATURE-
 Version: GnuPG v2

 iQEcBAEBAgAGBQJUUM7zAAoJEJ0Re0UIDzSucugIALn/zY8RdpP8iaMShHoszzqf
 I0zl0mFHyqhNtwgQ0ZF7VGO+H+U0Dk8rhzTYOmEMzPTKNBGwll3fda9mOnrK9Xvp
 9gQjII6DyQIWH7Z3dLcLr2e1j8OMNUSL6UmAYs8urNSIKZLowdV3JI4G/bLyW0KS
 y5Ko8dI6y5nOJ1P9XCmPTmags43UZfR8DrBUaAbzNcS8FGwmUE2KBkEhLQOvmpJi
 jmMc7wMOpq0jL+XbA+7pHUqoVZ7w1tUFjuy9I3h45tgPuTFAFB0gX+FpE+oVgO5o
 spQpVaOPEYN9ceLgHdKSxzdVIhOQLE6H/SYNHlsEW/ZNM6aR9n4yipgkOmtJ0+M=
 =WzHA
 -END PGP SIGNATURE-
 --
 https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list