Re: [Tutor] Generator next()

2013-12-29 Thread Steven D'Aprano
On Sun, Dec 29, 2013 at 01:57:31AM -0500, Keith Winston wrote:

 I don't really get the inner thing, I tried to look it up, but I don't
 think I found the right thing, just references to nested functions. I'd
 like to understand what I'm looking at better, but can't figure out what
 question to ask...

Let's start with a trivial example: a function inside another function.


def f(x):
print(Inside the outer function x =, x)
def g(y):  # a function nested inside another function
print(Inside the inner function x =, x)
print(Inside the inner function y =, y)
return x + y
return f(23)


And in use, in Python 3.3:

py f(1000)
Inside the outer function x = 1000
Inside the inner function x = 1000
Inside the inner function y = 23
1023


The main benefit of writing the function this way is that it hides 
function g() from everything else. If I try to call it from outside of 
f(), I get an error:

py g(1)
Traceback (most recent call last):
  File stdin, line 1, in module
NameError: name 'g' is not defined


Note a few things:

- x is a local variable to function f(). f() is free to
  modify x without restriction.

- x is a non-local variable to the nested function g(). That 
  means it is neither local, nor global. By default, g()
  cannot modify x.

- y is a local variable to g(). This means that y is invisible 
  to f(), since it doesn't exist in the outer scope. In the 
  same way that code outside of f() cannot peer inside the 
  function and see its local variable x, so code outside of g()
  cannot peer inside of it to see its local variable y. That 
  includes f().


Now, let's crank it up a bit. In Python, functions are first-class 
values just like strings, numbers, lists and so forth. That means that 
you can return a function as the result. The usual name for this is a 
factory function, or just factory, a function which creates a new 
function and returns it.

def adder_factory(n):
def plus(arg):
return arg + n
return plus  # returns the function itself


If you call adder_factory(), it returns a function:

py adder_factory(10)
function adder_factory.locals.plus at 0xb7af6f5c

What good is this? Watch carefully:


py add_two = adder_factory(2)
py add_three = adder_factory(3)
py add_two(100)
102
py add_three(100)
103


The factory lets you programmatically create functions on the fly. 
add_two() is a function which adds two to whatever argument it gets. 
add_three() is a function which adds three to whatever argument it gets. 
We can create an add_whatever function without knowing in advance what 
whatever is going to be:

py from random import randint
py add_whatever = adder_factory(randint(1, 10))
py add_whatever(200)
205
py add_whatever(300)
305


So now you see the main reason for nested functions in Python: they let 
use create a function where you don't quite know exactly what it will do 
until runtime. You know the general form of what it will do, but the 
precise details aren't specified until runtime.

There's another term you will come across from time to time: closure. 
The functions add_two, add_three and add_whatever above are all 
closures. That's a term that comes from computer science, and its exact 
definition isn't important, but the main thing is that in practice a 
closure is a function which gains at least one variable from the 
enclosing outer function that creates it. In the case of the various 
add_* functions, the inner function plus() requires a value for variable 
n, which it gets from enclosing adder_factory() scope.

Closures are advanced but *incredibly* useful in Python. In languages 
like Java where functions are not first-class values, you have to do 
something like this instead:

class Adder:
def __init__(self, n):
self.n = n
def __call__(self, arg):
return arg + self.n

add_two = Adder(2)
add_three = Adder(3)


[more to follow]


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generator next()

2013-12-29 Thread Steven D'Aprano
On Sun, Dec 29, 2013 at 01:57:31AM -0500, Keith Winston wrote:

 Also: in the timer function, it has a series of attribute assignments to
 0/None after the inner function definition... from the behaviour, I assume
 those are applied once, the first time the timer function is called
 wrapping a new method/function, and then not again, but that doesn't really
 make sense. Again, I don't really have a well-formed question, maybe that's
 clear enough?

Here's the code in question, stripped to the absolutely bare minimum, 
and with a change of name:

def decorator(function):
def inner(*args, **kwargs):
result = function(*args, **kwargs)
inner.count += 1
return result
inner.count = 0
return inner


Let's see what happens when you call decorator with some function as 
argument:

def spam(n):
return spam*n

f = decorator(spam)

What does this function f() do? Let's go through the process step by 
step.

When we call decorator(spam), we enter the decorator() function with the 
local variable function set to the argument spam. The next thing that 
happens is that decorator() creates a new function called inner. 
Inside inner, stuff happens, but we don't care about that yet. (We'll 
come back to that in a moment.) The inner function is created, and 
then decorator() adds a new attribute to it:

inner.count = 0

Functions, being objects, can have attributes. We can add our own. We 
use this attribute to keep track of how many times the function gets 
called.

Then decorator() returns this newly created function object, complete 
with its attribute count=0. This new function object is assigned to the 
variable f.

What happens when we call f()?

Here's the code for f a.k.a. inner, again, stripped of the outer 
layer:

def inner(*args, **kwargs):
result = function(*args, **kwargs)
inner.count += 1
return result

Notice that this is a *closure* (remember them from my previous email?). 
There are two non-local variables inside this inner function, one called 
function and one called inner. function is the original spam 
function, the argument passed to the outer decorator function. inner 
is also a closure: it is the inner function itself. So inside the 
inner function, we have five variables:

- local variable args, which collects positional arguments;

- local variable kwargs, which collects keyword arguments;

- non-local variable function, set to spam (the argument to the
  outer decorator function);

- local variable result, set to the result of calling function
  with the collected arguments;

- and finally non-local variable inner, which is set to the 
  inner function itself.


So calling f() collects all the arguments (if any) into parameters args 
and kwargs. Then spam gets called with those arguments, and the result 
saved. Then the count attribute is incremented, and finally the result 
returned.

All this can be a bit tricky to keep track of, because the one function 
can be known by different names depending on where in the code you are 
looking. Outside of the decorator function, we have a function called 
spam. Inside the decorator, it is known as function. Inside the 
decorator, there is a nested function called inner. Outside of the 
decorator, it is known as f.

We say that f (a.k.a. inner) is a wrapper function around spam. 
Alternatively, we say that f decorates spam. The special decorator 
syntax:

@decorator
def spam(n):
return spam*n


is a short-cut for this:

def spam(n):
return spam*n

spam = decorator(spam)


This may be a lot to digest in one sitting. Factory functions, 
decorators, closures, they're all fantastically useful and powerful 
features of Python, but they can be a bit tricky for people to wrap 
their brains around.

[more to come]



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generator next()

2013-12-29 Thread Steven D'Aprano
Last one!

On Sun, Dec 29, 2013 at 01:57:31AM -0500, Keith Winston wrote:

 In the previous timer function that I was using, it defined a timer class,
 and then I had to instantiate it before I could use it, and then it saved a
 list of timing results. I think in yours, it adds attributes to each
 instance of a function/method, in which the relevant timings are stored. 

Correct. Rather than dump those attributes on the original function, it 
creates a new function and adds the attributes to that. The new function 
wraps the older one, that is, it calls the older one:

def f(x):
return g(x)

We say that f wraps g. Now, obviously wrapping a function just to 
return its result unchanged is pretty pointless, normally the wrapper 
function will do something to the arguments, or the result, or both. 
Otherwise why bother?

 I guess that's just an observation, I've played with it a bit and it's
 interesting. You said you didn't like the prior implementation, I'd be
 interested in anything further you have to say about why: 

I find it too complicated for something that shouldn't be that 
complicated.


 specifically, you
 called it a coroutine, and a decorator factory (tell me you made that
 up). Any more on that?

After my previous two emails, you should have an idea of what a factory 
is by now: it's a function that returns a new function. So a decorator 
factory is a function that returns a decorator. What's a decorator? In 
this context, it's a function that takes a function as argument and 
wraps it in another function.

So here we have a function A which creates a function B which takes as 
argument a function C and returns another function D which wraps 
(decorates) C.

That's actually not the complicated part. The complicated part is that 
it uses coroutines instead of normal function calls. What's a coroutine, 
I hear you ask? Don't ask.

Actually, I can give a quick and dirty explanation of coroutines. In a 
normal function, you have one entry point, calling the function, and 
at least one exit point (a return statement, or falling off the end of 
the function when there's nothing more to do). We enter the function at 
the top, and run the code inside it until we get to the end, then we 
finish:

def test():
do_this()  # runs first
do_that()  # runs second
do_more()  # runs third
# nothing more to do, the function exits


In a coroutine, you don't have a single entry point, you have multiple 
entry points, and you use the send method to feed values into the 
coroutine, and get values back. This makes them very powerful. For 
something as simple as timing instrumentation, too powerful -- it's 
using a nuclear-powered bulldozer to nudge your mouse an inch to the 
left.



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generator next()

2013-12-29 Thread Steven D'Aprano
On Sun, Dec 29, 2013 at 10:33:15PM +1100, Steven D'Aprano wrote:

 def f(x):
 print(Inside the outer function x =, x)
 def g(y):  # a function nested inside another function
 print(Inside the inner function x =, x)
 print(Inside the inner function y =, y)
 return x + y
 return f(23)

Oops, sorry a typo crept into this. That last line ought to be 
return g(23). Sorry for any confusion.



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] can't install

2013-12-29 Thread Lolo Lolo
On Wed, Dec 25, 2013 at 2:16 AM, Tobias M. t...@tobix.eu wrote:

 ./configure --prefix=prefix

 where prefix is a custom path (e.g. in your home directory) where you want
 to install python. You can configure without the --prefix option, but if you
 just want to use this python installation for virtuelenvs I would install it
 separate from the rest of the system.

 Then you can build and install Python:


Hi Tobias can i just ask. As i already have Python 3.3, when i install this 
separate version of 3.3, will there be a conflict on the command line when i 
type python3.3? This install i want just for virtualenvs but i wonder if it 
would replace my other 3.3 as the default on the command line?



On Wednesday, December 25, 2013 1:46 PM, Lolo Lolo losermelo...@yahoo.com 
wrote:
 
 On Wed, Dec 25, 2013 at 2:16 AM, Tobias M. t...@tobix.eu wrote:
 Is this the error you get when executing distribute_setup.py?
 urllib.error.URLError: urlopen error unknown url type: https

Thank you so much Tobias. And yes, that was the exact error message 


 On Tue, Dec 24, 2013 at 11:36 PM, Steven D'Aprano st...@pearwood.info 
wrote:
 But the good news is that all this should become history from Python 3.4
 onwards, there are plans to include pip in 3.4.

also thanks Steven. I'll definately upgrade to Python 3.4___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] unicode: alpha, whitespaces and digits

2013-12-29 Thread Ulrich Goebel

Hallo,

I have a unicode string s, for example uabc, uäöü, u123 or 
something else, and I have to find out wether


1. s is not empty and contains only digits (as in u123, but not in 
u3.1415)


or

2. s is empty or contains only whitespaces

For all other cases I would assume a normal unicode string in s, 
whatever that may be.


For the first case it could be s.isdigit(), s.isnumeric() or 
s.isdecimal() - but which one is the best?


For the second case it should be s.isspace(), but it works only on 
strings, not on unicode strings?


Many thanks for any help!

Ulrich


--
Ulrich Goebel
Paracelsusstr. 120, 53177 Bonn
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode: alpha, whitespaces and digits

2013-12-29 Thread Mark Lawrence

On 29/12/2013 13:36, Ulrich Goebel wrote:

Hallo,

I have a unicode string s, for example uabc, uäöü, u123 or
something else, and I have to find out wether

1. s is not empty and contains only digits (as in u123, but not in
u3.1415)

or

2. s is empty or contains only whitespaces

For all other cases I would assume a normal unicode string in s,
whatever that may be.

For the first case it could be s.isdigit(), s.isnumeric() or
s.isdecimal() - but which one is the best?

For the second case it should be s.isspace(), but it works only on
strings, not on unicode strings?

Many thanks for any help!

Ulrich



This depends on whether you are using python 2 or 3.  In the latter all 
strings are unicode.  Please see 
http://docs.python.org/X/library/stdtypes.html#string-methods where X is 
2 or 3.  You might also want to look at 
http://docs.python.org/3.3/howto/unicode.html


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode: alpha, whitespaces and digits

2013-12-29 Thread Dave Angel
On Sun, 29 Dec 2013 19:20:04 +, Mark Lawrence 
breamore...@yahoo.co.uk wrote:

 2. s is empty or contains only whitespaces


Call strip() on it. If it's now empty, it was whitespace.

--
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] can't install

2013-12-29 Thread Tobias M.


Quoting Lolo Lolo losermelo...@yahoo.com:


Hi Tobias can i just ask. As i already have Python 3.3, when i  
install this separate version of 3.3, will there be a conflict on  
the command line when i type python3.3? This install i want just for  
virtualenvs but i wonder if it would replace my other 3.3 as the  
default on the command line?




As long as you install the new 3.3 build in a custom path (via  
--prefix) it will not overwrite your current installation and by  
default python3.3 will invoke the standard installation (probably in  
your /usr directory).
To use your custom build in virtualenvs make sure you create the  
virtualenv with the python interpreter of that build (the one in the  
prefix/bin directory). Your virtualenv will link to this  
installation and when your virtualenv is active, python3.3 will invoke  
the interpreter from your own build.


I hope this explaination is understandable (and correct), if not just  
ask. When using virtualenvs the first time I found it very confusing,  
too.






___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] can't install

2013-12-29 Thread Tobias M.


Quoting Tobias M. t...@tobix.eu:

Quoting Lolo Lolo losermelo...@yahoo.com:


Hi Tobias can i just ask. As i already have Python 3.3, when i  
install this separate version of 3.3, will there be a conflict on  
the command line when i type python3.3? This install i want just  
for virtualenvs but i wonder if it would replace my other 3.3 as  
the default on the command line?




As long as you install the new 3.3 build in a custom path (via  
--prefix) it will not overwrite your current installation and by  
default python3.3 will invoke the standard installation (probably in  
your /usr directory).
To use your custom build in virtualenvs make sure you create the  
virtualenv with the python interpreter of that build (the one in the  
prefix/bin directory). Your virtualenv will link to this  
installation and when your virtualenv is active, python3.3 will  
invoke the interpreter from your own build.


I hope this explaination is understandable (and correct), if not  
just ask. When using virtualenvs the first time I found it very  
confusing, too.




Another hint, if you are not sure which interpreter is started when  
using python3.3 at the command line:
In interactive mode the date and time of the build are displayed at  
the beginning and you can get the full path of the current python  
interpreter with:


import sys
sys.executable


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode: alpha, whitespaces and digits

2013-12-29 Thread Steven D'Aprano
On Sun, Dec 29, 2013 at 02:36:32PM +0100, Ulrich Goebel wrote:
 Hallo,
 
 I have a unicode string s, for example uabc, uäöü, u123 or 
 something else, and I have to find out wether
 
 1. s is not empty and contains only digits (as in u123, but not in 
 u3.1415)

 or
 
 2. s is empty or contains only whitespaces

 For all other cases I would assume a normal unicode string in s, 
 whatever that may be.
 
 For the first case it could be s.isdigit(), s.isnumeric() or 
 s.isdecimal() - but which one is the best?

Depends what you are trying to do. Only you can decide which is best. 
The three methods do slightly different things:

- isdigit() tests for the digit characters 0...9, or their
  equivalent in whatever native language your computer is 
  using. 

- isdecimal() tests for decimal characters. That includes the
  so-called Arabic numerals 0...9 (although the Arabs don't
  use them!) as well as other decimal digits like ٠١٢...

  (The above three are ARABIC-INDIC DIGIT ZERO through TWO.)

- isnumeric() tests for characters which have the Unicode 
  numeric value property. That includes decimal digits, as well 
  as non-digit numbers such as ½ and ¾.


If you want to test for something that a human reader will recognise as 
a whole number, s.isdigit() is probably the best one to use.


 For the second case it should be s.isspace(), but it works only on 
 strings, not on unicode strings?

What gives you that impression? isspace works on Unicode strings too.

py '   x'.isspace()
False
py ''.isspace()
True

For the second case, you also need to check for empty strings, so you 
should use:

not s or s.isspace()

which will return True is s is empty or all whitespace, otherwise False.


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode: alpha, whitespaces and digits

2013-12-29 Thread Steven D'Aprano
On Mon, Dec 30, 2013 at 09:58:10AM +1100, Steven D'Aprano wrote:

 What gives you that impression? isspace works on Unicode strings too.
 
 py '   x'.isspace()
 False
 py ''.isspace()
 True

Oops, the above was copied and pasted from Python 3, which is why there 
are no u' prefixes. But it still holds in Python 2:

py u''.isspace()
True
py u'   x'.isspace()
False


Sorry for any confusion.


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unicode: alpha, whitespaces and digits

2013-12-29 Thread eryksun
On Sun, Dec 29, 2013 at 5:58 PM, Steven D'Aprano st...@pearwood.info wrote:
 If you want to test for something that a human reader will recognise as
 a whole number, s.isdigit() is probably the best one to use.

isdigit() includes decimal digits plus other characters that have a digit value:

 print u'\N{superscript two}'
²
 unicodedata.digit(u'\N{superscript two}')
2

However, int(u'\N{superscript two}') raises a ValueError. int() only
accepts the subset of digits that are isdecimal().
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Using Regular Expression to extracting string in brackets on a list

2013-12-29 Thread Jing Ai
Hello,

I am trying to rewrite some contents on a long list that contains words
within brackets and outside brackets and I'm having trouble extracting the
words within brackets, especially since I have to add the append function
for list as well.  Does anyone have any suggestions? Thank you!

*An example of list*:

['hypothetical protein BRAFLDRAFT_208408 [Branchiostoma floridae]\n',
'hypoxia-inducible factor 1-alpha [Mus musculus]\n', 'hypoxia-inducible
factor 1-alpha [Gallus gallus]\n' ]

*What I'm trying to extract out of this*:

['Branchiostoma floridae', 'Mus musculus', 'Gallus gallus']
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using Regular Expression to extracting string in brackets on a list

2013-12-29 Thread Joel Goldstick
On Sun, Dec 29, 2013 at 4:02 PM, Jing Ai jai...@g.rwu.edu wrote:

 Hello,

 I am trying to rewrite some contents on a long list that contains words
 within brackets and outside brackets and I'm having trouble extracting the
 words within brackets, especially since I have to add the append function
 for list as well.  Does anyone have any suggestions? Thank you!

 *An example of list*:

 ['hypothetical protein BRAFLDRAFT_208408 [Branchiostoma floridae]\n',
 'hypoxia-inducible factor 1-alpha [Mus musculus]\n', 'hypoxia-inducible
 factor 1-alpha [Gallus gallus]\n' ]


Is the above line a python  list, or is it what you get when you read a
line of a data file.  The reason I ask, is if it is a list you can split
the list by looping of each list item.  Then just maybe try some of these
ideas:

http://stackoverflow.com/questions/10017147/python-replace-characters-in-string

 *What I'm trying to extract out of this*:

 ['Branchiostoma floridae', 'Mus musculus', 'Gallus gallus']





 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 https://mail.python.org/mailman/listinfo/tutor




-- 
Joel Goldstick
http://joelgoldstick.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using Regular Expression to extracting string in brackets on a list

2013-12-29 Thread Steven D'Aprano
On Sun, Dec 29, 2013 at 04:02:01PM -0500, Jing Ai wrote:
 Hello,
 
 I am trying to rewrite some contents on a long list that contains words
 within brackets and outside brackets and I'm having trouble extracting the
 words within brackets, especially since I have to add the append function
 for list as well.  Does anyone have any suggestions? Thank you!
 
 *An example of list*:
 
 ['hypothetical protein BRAFLDRAFT_208408 [Branchiostoma floridae]\n',
 'hypoxia-inducible factor 1-alpha [Mus musculus]\n', 'hypoxia-inducible
 factor 1-alpha [Gallus gallus]\n' ]
 
 *What I'm trying to extract out of this*:
 
 ['Branchiostoma floridae', 'Mus musculus', 'Gallus gallus']

You have a list of strings. Each string has exactly one pair of square 
brackets []. You want the content of the square brackets.

Start with a function that extracts the content of the square brackets 
from a single string.

def extract(s):
start = s.find('[')
if start == -1:
# No opening bracket found. Should this be an error?
return ''
start += 1  # skip the bracket, move to the next character
end = s.find(']', start)
if end == -1:
# No closing bracket found after the opening bracket.
# Should this be an error instead?
return s[start:]
else:
return s[start:end]


Let's test it and see if it works:

py s = 'hypothetical protein BRAFLDRAFT_208408 [Branchiostoma floridae]\n'
py extract(s)
'Branchiostoma floridae'

So far so good. Now let's write a loop:

names = []
for line in list_of_strings:
names.append(extract(line))


where list_of_strings is your big list like the example above.

We can simplify the loop by using a list comprehension:

names = [extract(line) for line in list_of_strings]


If you prefer to use a regular expression, that's simple enough. Here's 
a new version of the extract function:

import re
def extract(s):
mo = re.search(r'\[(.*)\]', s)
if mo:
return mo.group(1)
return ''


The list comprehension remains the same.


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using Regular Expression to extracting string in brackets on a list

2013-12-29 Thread Joel Goldstick
On Sun, Dec 29, 2013 at 9:00 PM, Jing Ai jai...@g.rwu.edu wrote:

 Thanks, but I don't think I can get out the string in the brackets by only
 replacing other items...(there's too many things to replace and may
 interfere with the items within the string).




I am not sure what you mean by your previous sentence.  Check out Steven's
excellent answer.  Also, remember to reply to the list, or no one will see
your question.

Good luck




 On Sun, Dec 29, 2013 at 7:08 PM, Joel Goldstick 
 joel.goldst...@gmail.comwrote:




 On Sun, Dec 29, 2013 at 4:02 PM, Jing Ai jai...@g.rwu.edu wrote:

 Hello,

 I am trying to rewrite some contents on a long list that contains words
 within brackets and outside brackets and I'm having trouble extracting the
 words within brackets, especially since I have to add the append function
 for list as well.  Does anyone have any suggestions? Thank you!

 *An example of list*:

 ['hypothetical protein BRAFLDRAFT_208408 [Branchiostoma floridae]\n',
 'hypoxia-inducible factor 1-alpha [Mus musculus]\n', 'hypoxia-inducible
 factor 1-alpha [Gallus gallus]\n' ]


 Is the above line a python  list, or is it what you get when you read a
 line of a data file.  The reason I ask, is if it is a list you can split
 the list by looping of each list item.  Then just maybe try some of these
 ideas:


 http://stackoverflow.com/questions/10017147/python-replace-characters-in-string

  *What I'm trying to extract out of this*:

 ['Branchiostoma floridae', 'Mus musculus', 'Gallus gallus']





 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 https://mail.python.org/mailman/listinfo/tutor




 --
 Joel Goldstick
 http://joelgoldstick.com





-- 
Joel Goldstick
http://joelgoldstick.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generator next()

2013-12-29 Thread Keith Winston
Wow Steven, this is great.I'll be brief I'm on a phone

 def f(x):
 print(Inside the outer function x =, x)
 def g(y):  # a function nested inside another function
 print(Inside the inner function x =, x)
 print(Inside the inner function y =, y)
 return x + y
 return f(23)

Actually as I was forming my question I sorted out what's going on. Your
examples and descriptions are very clear and exceedingly helpful, you could
write a useful book. Thanks for all the help. Now on to your next two posts
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generator next()

2013-12-29 Thread Keith Winston
This is all really quite awesome, though I'm sure it'll be a while before
these are really available tools for me. I think I get(a bit more than) the
basic concept. Thanks!

 is a short-cut for this:

 def spam(n):
 return spam*n

 spam = decorator(spam)


 This may be a lot to digest in one sitting. Factory functions,
 decorators, closures, they're all fantastically useful and powerful
 features of Python, but they can be a bit tricky for people to wrap
 their brains around.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generator next()

2013-12-29 Thread Keith Winston
Hah,I must understand,I read it that way!

 Oops, sorry a typo crept into this. That last line ought to be
 return g(23). Sorry for any confusion.



 --
 Steven
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using Regular Expression to extracting string in brackets on a list

2013-12-29 Thread Jing Ai
Thank you all for the suggestions! I decided to use Steven's re loop in the
end.

Joel, what i meant earlier was that the link you sent seems to suggest me
to replace some characters in the list and I'm not sure how it would work...





On Sun, Dec 29, 2013 at 9:24 PM, Joel Goldstick joel.goldst...@gmail.comwrote:




 On Sun, Dec 29, 2013 at 9:00 PM, Jing Ai jai...@g.rwu.edu wrote:

 Thanks, but I don't think I can get out the string in the brackets by
 only replacing other items...(there's too many things to replace and may
 interfere with the items within the string).




 I am not sure what you mean by your previous sentence.  Check out Steven's
 excellent answer.  Also, remember to reply to the list, or no one will see
 your question.

 Good luck




 On Sun, Dec 29, 2013 at 7:08 PM, Joel Goldstick joel.goldst...@gmail.com
  wrote:




 On Sun, Dec 29, 2013 at 4:02 PM, Jing Ai jai...@g.rwu.edu wrote:

 Hello,

 I am trying to rewrite some contents on a long list that contains words
 within brackets and outside brackets and I'm having trouble extracting the
 words within brackets, especially since I have to add the append function
 for list as well.  Does anyone have any suggestions? Thank you!

 *An example of list*:

 ['hypothetical protein BRAFLDRAFT_208408 [Branchiostoma floridae]\n',
 'hypoxia-inducible factor 1-alpha [Mus musculus]\n', 'hypoxia-inducible
 factor 1-alpha [Gallus gallus]\n' ]


 Is the above line a python  list, or is it what you get when you read a
 line of a data file.  The reason I ask, is if it is a list you can split
 the list by looping of each list item.  Then just maybe try some of these
 ideas:


 http://stackoverflow.com/questions/10017147/python-replace-characters-in-string

  *What I'm trying to extract out of this*:

 ['Branchiostoma floridae', 'Mus musculus', 'Gallus gallus']





 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 https://mail.python.org/mailman/listinfo/tutor




 --
 Joel Goldstick
 http://joelgoldstick.com





 --
 Joel Goldstick
 http://joelgoldstick.com

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using Regular Expression to extracting string in brackets on a list

2013-12-29 Thread Bod Soutar
Steven's answer is probably a lot more robust, but I would use a simple
split.

mylist = ['hypothetical protein BRAFLDRAFT_208408 [Branchiostoma
floridae]\n', 'hypoxia-inducible factor 1-alpha [Mus musculus]\n',
'hypoxia-inducible factor 1-alpha [Gallus gallus]\n' ]
for item in mylist:
item.split([)[1].split(])[0]

-- Bodsda


On 30 December 2013 03:28, Jing Ai jai...@g.rwu.edu wrote:

 Thank you all for the suggestions! I decided to use Steven's re loop in
 the end.

 Joel, what i meant earlier was that the link you sent seems to suggest me
 to replace some characters in the list and I'm not sure how it would work...





 On Sun, Dec 29, 2013 at 9:24 PM, Joel Goldstick 
 joel.goldst...@gmail.comwrote:




 On Sun, Dec 29, 2013 at 9:00 PM, Jing Ai jai...@g.rwu.edu wrote:

 Thanks, but I don't think I can get out the string in the brackets by
 only replacing other items...(there's too many things to replace and may
 interfere with the items within the string).




 I am not sure what you mean by your previous sentence.  Check out
 Steven's excellent answer.  Also, remember to reply to the list, or no one
 will see your question.

 Good luck




 On Sun, Dec 29, 2013 at 7:08 PM, Joel Goldstick 
 joel.goldst...@gmail.com wrote:




 On Sun, Dec 29, 2013 at 4:02 PM, Jing Ai jai...@g.rwu.edu wrote:

 Hello,

 I am trying to rewrite some contents on a long list that contains
 words within brackets and outside brackets and I'm having trouble
 extracting the words within brackets, especially since I have to add the
 append function for list as well.  Does anyone have any suggestions? Thank
 you!

 *An example of list*:

 ['hypothetical protein BRAFLDRAFT_208408 [Branchiostoma floridae]\n',
 'hypoxia-inducible factor 1-alpha [Mus musculus]\n', 'hypoxia-inducible
 factor 1-alpha [Gallus gallus]\n' ]


 Is the above line a python  list, or is it what you get when you read a
 line of a data file.  The reason I ask, is if it is a list you can split
 the list by looping of each list item.  Then just maybe try some of these
 ideas:


 http://stackoverflow.com/questions/10017147/python-replace-characters-in-string

  *What I'm trying to extract out of this*:

 ['Branchiostoma floridae', 'Mus musculus', 'Gallus gallus']





 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 https://mail.python.org/mailman/listinfo/tutor




 --
 Joel Goldstick
 http://joelgoldstick.com





 --
 Joel Goldstick
 http://joelgoldstick.com



 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 https://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor