How to recognise "generator functions" ?
Hi all. Is there a way to know if a function object is actually a "generator function" or not ? e.g.: def f(): pass def g(): yield None f.__class__ is the same as g.__class__ , i.e. "function" type. But i "know" that the second, when invoked, returns a generator object, because there is a "yield" statement in its body. Is there a (eventually hackish) method to get such information ? Thanks in advance, Diego. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to recognise "generator functions" ?
Georg Brandl ha scritto: f.func_code.co_flags > 67 g.func_code.co_flags > 99 > > => 32 (CO_GENERATOR in compiler.consts) is the flag that indicates a > generator code object. > > Georg What a fast reply! Thank You very much! :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: package search
boris ha scritto:
> I have two directories, lib1 and lib2, that both contain the package
> foo, one with the submodule mod1
> and the other with the submodule mod2:
> [...]
> Now this script:
>
> import sys
> sys.path.append("lib1")
> sys.path.append("lib2")
> import foo.mod1
>
> will find the module foo.mod1, while the same script with the two
> append-lines interchanged will not:
>
> import sys
> sys.path.append("lib2")
> sys.path.append("lib1")
> import foo.mod1
>
> The error is:
>
> import foo.mod1
> ImportError: No module named mod1
> [...]
You just have to put in "__init__.py" in "lib2" (the package directory
you are "extending"), the following lines:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
"__path__", in each __init__ module, is a list initialized with the
module's path, but you can extend it by appending paths where you want
the interpreter to look for further modules.
pkgutil.extend_path automatically appends to __path__ all subdirectories
of directories on sys.path named after the package.
HTH :-)
Diego.
--
http://mail.python.org/mailman/listinfo/python-list
Re: package search
boris ha scritto: > > COOL! You just saved me an awful lot of work. > > Thanks, Diego! > > Boris > ;-) Bye -- http://mail.python.org/mailman/listinfo/python-list
Re: mapping None values to ''
[EMAIL PROTECTED] ha scritto:
> hi
> i wish to map None or "None" values to "".
> eg
> a = None
> b = None
> c = "None"
>
> map( , [i for i in [a,b,c] if i in ("None",None) ])
>
> I can't seem to find a way to put all values to "". Can anyone help?
> thanks
>
You already filtered [a,b,c] in the comprehension list, so you just have
to map all its values to "":
map(lambda x:"" , [i for i in [a,b,c] if i in ("None",None) ])
--
http://mail.python.org/mailman/listinfo/python-list
Re: mapping None values to ''
Roberto Bonvallet ha scritto:
> imho <[EMAIL PROTECTED]>:
>> map(lambda x:"" , [i for i in [a,b,c] if i in ("None",None) ])
>
> You don't need map when using list comprehensions:
>
>["" for i in [a, b, c] if i in ("None", None)]
>
I know that... I tried to match the idiom used by the o.p. :-)
--
http://mail.python.org/mailman/listinfo/python-list
Re: Accessing function in a module by name
Tobiah ha scritto: > Is there a way to call function in an imported > module having only the name? > > > func_name = 'doit' > > real_func = foo.some_magic(func_name) > > #Now call it > real_func(args) > > > I'm trying to set up a function dispatcher for a > SOAP server. > > Thanks, > > Tobiah > What's wrong with real_func = getattr(foo, func_name) ? Maybe I misunderstood your question ? -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Split a String
Siah ha scritto:
> Hi,
>
> I need to convert the string: '(a, b, "c", d, "e")' into the following
> list ['a', 'b', 'c', 'd', 'e']. Much like a csv reader does. I usually
> use the split function, but this mini-monster wouldn't properly get
> split up due to those random quotations postgresql returns to me.
>
> Please help me with this,
> Thanks,
> Sia
One solution:
>>> s = '(a, b, "c", d, "e")'
>>> print [x.strip('" ') for x in s.strip('()').split(',')]
['a', 'b', 'c', 'd', 'e']
--
http://mail.python.org/mailman/listinfo/python-list
Re: How to Split a String
Grant Edwards ha scritto:
>> One solution:
>>
> s = '(a, b, "c", d, "e")'
> print [x.strip('" ') for x in s.strip('()').split(',')]
>> ['a', 'b', 'c', 'd', 'e']
>
> That fails when a quoted string contains commas:
>
s = '(a, b, "c", d, "e,f,g")'
print [x.strip('" ') for x in s.strip('()').split(',')]
> ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>
> I presume the correct result would be
>
> ['a', 'b', 'c', 'd', 'e,f,g']
>
Uhm, agree. I supposed quoted strings were presumed to be 'trivial'.
Definitely csv module is the solution :-)
--
http://mail.python.org/mailman/listinfo/python-list
Re: a trick with lists ?
Steve Holden ha scritto: >>> What I do not fully understand is the line "self.tasks[:] = tasks". >>> Why does the guy who coded this did not write it as "self.tasks = >>> tasks"? What is the use of the "[:]" trick ? >> >> It changes the list in-place. If it has been given to other objects, >> it might require that. > > Nowadays it's stylistically better to write > > self.tasks = list(tasks) > > as it does just the same and makes it a little clearer what's going on > (though of course if tasks *isn't* a list it won't do *exactly* the same. > > regards > Steve No: self.tasks = list(tasks) is the same of self.tasks = tasks[:], not a replacement for self.tasks[:] = tasks , the latter performing a different operation, i.e. resetting the list self.tasks 'in place' without assigning it a different list. -- http://mail.python.org/mailman/listinfo/python-list
Re: Better way to do this?
PRC ha scritto: > Hi folks, > > I have a tuple of tuples, in the form--> ((code1, 'string1'),(code2, > 'string2'),(code3, 'string3'),) > > Codes are unique. A dict would probably be the best approach but this > is beyond my control. > > Here is an example: pets = ((0,'cat'),(1,'dog'),(2,'mouse')) > > If I am given a value for the code I need to retrieve the string > representation. The value is guaranteed to be valid. > > This is what I came up with... > value=1 [ pet for code, pet in pets if value==code ][0] > 'dog' > > It does the job, I was just curious if there was a better way to do > it. Can't You first convert the tuple of tuples in a dict, and then retrieving the value given a code value ? >>> dct = dict(pets) >>> dct[1] 'dog' -- http://mail.python.org/mailman/listinfo/python-list
