Terry Reedy | 1 Feb 01:12
Picon
Favicon

Re: except clause syntax question

On 1/31/2012 8:57 AM, Charles Yeomans wrote:

> In any case, though I appreciate your attempt at a post hoc justification,
 > I was hoping for a positive explanation.

I think the best you are going to get is that Python somewhat 
consistently*, for both practical and historical reasons#, uses tuples 
when the syntax allows an object or collection of objects.

* except, isinstance, isubclass, ''%x, perhaps other places.

In the last case, that creates a problem when one wants to interpolate a 
tuple as an object rather than having it viewed as a container of 
several objects to be interpolated. That was on

# Python once treated tuples as different from lists in ways that is not 
true now. (Read the 1.5 docs if really interested.)

--

-- 
Terry Jan Reedy

Para | 1 Feb 01:00
Favicon

the script can not go through all the list, anyone could help, would be highly appreciated, thanks a lot !

the script can not go through all the list, anyone could help, would be highly appreciated, thanks a lot !

output:
initial 7 lines:
[9379, 'G', '0.1830']
[9378, 'G', '0.1752']
[9377, 'G', '0.1929']
[9375, 'G', '0.1950']
[9370, 'G', '0.1872']
[937, 'G', '0.1931']
[93, 'G', '0.1974']
processing:
code_K = 93 rate_K = -1 len_K = 1 len_G = 7
code_K = 93 rate_K = 0.1830 code_G = 9379 rate_G = 0.1830
code_K = 93 rate_K = 0.1929 code_G = 9377 rate_G = 0.1929
code_K = 93 rate_K = 0.1929 code_G = 9370 rate_G = 0.1872
code_K = 93 rate_K = 0.1974 code_G = 93 rate_G = 0.1974
final 3 lines:
[9378, 'G', '0.1752']
[9375, 'G', '0.1950']
[937, 'G', '0.1931']


code:
......  # read data from .csv
print "initial", len(list_G), "lines:"
for row_G in list_G:
  print row_G

print "processing:"
for row_K in list_K:
  code_K = str(row_K[0])
  rate_K = -1
  len_K = len(list_K)
  len_G = len(list_G)
  print "code_K =", code_K, "rate_K =", rate_K, "len_K =", len_K, "len_G =", len_G
  for row_G in list_G:
    code_G = str(row_G[0])
    rate_G = str(row_G[2])
    if re.match(code_K, code_G):
      if rate_K < rate_G:        rate_K = rate_G
      print "code_K =", code_K, "rate_K =", rate_K, "code_G =", code_G, "rate_G =", rate_G
      list_G.remove(row_G)

print "final", len(list_G), "lines:"
for row_G in list_G:
  print row_G


Terry Reedy | 1 Feb 01:21
Picon
Favicon

Re: configobj

On 1/31/2012 11:06 AM, Andrea Crotti wrote:
> I have a couple of questions about configobj, which I'm happily trying
> to use for this project.

When asking about 3rd party modules, please include a url, so we can be 
sure of what you mean and even take a look. Is
www.voidspace.org.uk/python/configobj.html
what you mean?

> PS. and why by the way ConfigObj is not in the standard lib, instead of
> ConfigParser,

Perhaps history. Informed suggestions for change are welcome.

> which looked less complete and more cumbersome (to me at least)

Does ConfigParser have the same problems you found with ConfigObj.

--

-- 
Terry Jan Reedy

Charles Yeomans | 1 Feb 01:27
Favicon

Re: except clause syntax question


On Jan 31, 2012, at 7:12 PM, Terry Reedy wrote:

> On 1/31/2012 8:57 AM, Charles Yeomans wrote:
> 
>> In any case, though I appreciate your attempt at a post hoc justification,
> > I was hoping for a positive explanation.
> 
> I think the best you are going to get is that Python somewhat consistently*, for both practical and
historical reasons#, uses tuples when the syntax allows an object or collection of objects.
> 
> * except, isinstance, isubclass, ''%x, perhaps other places.
> 
> In the last case, that creates a problem when one wants to interpolate a tuple as an object rather than
having it viewed as a container of several objects to be interpolated. That was on
> 
> # Python once treated tuples as different from lists in ways that is not true now. (Read the 1.5 docs if
really interested.)
> 

I'll do that.  Thanks.

Charles Yeomans

Andres Soto | 1 Feb 01:44
Picon
Favicon

How can I verify if the content of a variable is a list or a string?

Hi,
I'm writing a function which receive a list which elements are strings or new lists (sublists) containing strings.
How can I verify if sone element of the list (which is contained in a variable) is a list or a string?
I found the method isinstance(object,class) but I don't know which class should I use for.
Thank you, regards
 
Prof. Dr. Andrés Soto
DES DACI
UNACAR
Chris Angelico | 1 Feb 01:53
Picon

Re: except clause syntax question

On Wed, Feb 1, 2012 at 9:03 AM, Duncan Booth
<duncan.booth <at> invalid.invalid> wrote:
> Abitrarily nested tuples of exceptions cannot contain loops so the code
> simply needs to walk through the tuples until it finds a match.

Is this absolutely guaranteed? The C API for CPython provides:
(Py2) http://docs.python.org/c-api/tuple.html#PyTuple_SetItem
(Py3) http://docs.python.org/dev/c-api/tuple.html#PyTuple_SetItem

which doesn't have massive warnings on it saying "USE THIS ONLY TO
INITIALIZE A TUPLE" (compare, for instance, _PyTuple_Resize which does
carry a similar warning). Is the assumption then that we're all
adults, and that mutating a tuple is like passing a null pointer to an
API function (aka "loaded gun in proximity to foot")?

ChrisA
Terry Reedy | 1 Feb 01:56
Picon
Favicon

Re: How can I verify if the content of a variable is a list or a string?

On 1/31/2012 7:44 PM, Andres Soto wrote:
> Hi,
> I'm writing a function which receive a list which elements are strings
> or new lists (sublists) containing strings.
> How can I verify if sone element of the list (which is contained in a
> variable) is a list or a string?
> I found the method isinstance(object,class) but I don't know which class
> should I use for.

For 3.x, 'list' or 'str' (where 'str' means unicode). For 2.x, I believe 
'basestring' includes unicode strings as well as byte strings.

 >>> isinstance([], list)
True
 >>> isinstance([], str)
False

--

-- 
Terry Jan Reedy

Noah Hall | 1 Feb 01:58
Picon

Re: How can I verify if the content of a variable is a list or a string?

On Wed, Feb 1, 2012 at 12:44 AM, Andres Soto <soto_andres <at> yahoo.com> wrote:
> Hi,
> I'm writing a function which receive a list which elements are strings or
> new lists (sublists) containing strings.
> How can I verify if sone element of the list (which is contained in a
> variable) is a list or a string?
> I found the method isinstance(object,class) but I don't know which class
> should I use for.
> Thank you, regards
>
> Prof. Dr. Andrés Soto
> DES DACI
> UNACAR

"list" and "str"

>>> my_list = [1, 2, 3]
>>> isinstance(my_list, list)
True
>>> my_string = "foobar"
>>> isinstance(my_string, str)
True
--

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

Emmanuel Mayssat | 1 Feb 01:54
Picon

'class' named tuple

I have the following program.
I am trying to have index the attributes of an object using __getitem__.
Reading them this way works great, but assigning them a value doesn't
Is there a way to do such a thing?
(Almost like a named tuple, but with custom methods)

class LIter(object):
    def __init__(self,parent=None):
        super(LIter, self).__init__()
        self.toto = 3
        self.tata = 'terto'

    def __getitem__(self,index):
        if index == 0 : return self.toto
        if index == 1 : return self.tata

   # [other methods]

if __name__ == "__main__":
    i = LIter()
    print i[0]
    print i[1]
    i.toto = 2
    i.tata = 'bye'
    print i[0]
    print i[1]
    i[0] = -1
    i[1] = 'salut'
    print i[0]
    print i[1]

$ python iter.py
3
terto
2
bye
Traceback (most recent call last):
  File "iter.py", line 25, in <module>
    i[0] = -1
TypeError: 'LIter' object does not support item assignment
MRAB | 1 Feb 02:06

Re: the script can not go through all the list, anyone could help, would be highly appreciated, thanks a lot !

On 01/02/2012 00:00, Para wrote:
> the script can not go through all the list, anyone could help, would be
> highly appreciated, thanks a lot !
>
> output:
> initial 7 lines:
> [9379, 'G', '0.1830']
> [9378, 'G', '0.1752']
> [9377, 'G', '0.1929']
> [9375, 'G', '0.1950']
> [9370, 'G', '0.1872']
> [937, 'G', '0.1931']
> [93, 'G', '0.1974']
> processing:
> code_K = 93 rate_K = -1 len_K = 1 len_G = 7
> code_K = 93 rate_K = 0.1830 code_G = 9379 rate_G = 0.1830
> code_K = 93 rate_K = 0.1929 code_G = 9377 rate_G = 0.1929
> code_K = 93 rate_K = 0.1929 code_G = 9370 rate_G = 0.1872
> code_K = 93 rate_K = 0.1974 code_G = 93 rate_G = 0.1974
> final 3 lines:
> [9378, 'G', '0.1752']
> [9375, 'G', '0.1950']
> [937, 'G', '0.1931']
>
>
> code:
> ...... # read data from .csv
> print "initial", len(list_G), "lines:"
> for row_G in list_G:
>     print row_G
>
> print "processing:"
> for row_K in list_K:
>     code_K = str(row_K[0])
>     rate_K = -1
>     len_K = len(list_K)
>     len_G = len(list_G)
>     print "code_K =", code_K, "rate_K =", rate_K, "len_K =", len_K, "len_G =", len_G
>     for row_G in list_G:
>         code_G = str(row_G[0])
>         rate_G = str(row_G[2])
>         if re.match(code_K, code_G):
>             if rate_K < rate_G:
>                 rate_K = rate_G
>             print "code_K =", code_K, "rate_K =", rate_K, "code_G =", code_G,
>             "rate_G =", rate_G
>             list_G.remove(row_G)
>
> print "final", len(list_G), "lines:"
> for row_G in list_G:
> print row_G

rate_K is initially a number (rate_K = -1), but rate_G is a string. If
you want to compare numeric values, make sure that they are both
numbers. (In Python 3 it will complain if you say x < y when one of
them is a string and the other is a number; in Python 2 it will return
a consistent but arbitrary result.)

The re.match(code_K, code_G) checks whether code_G start with code_K.
It is better to use code_G.startswith(code_K) instead.

Also, you have:

     for row_G in list_G:
         ...
         list_G.remove(row_G)

Don't add or remove items in a list over which you are iterating.

Gmane