Rectangle 27 133

I recommend that you use the standard packages email and smtplib together to send email. Please look at the following example (reproduced from the Python documentation). Notice that if you follow this approach, the "simple" task is indeed simple, and the more complex tasks (like attaching binary objects or sending plain/HTML multipart messages) are accomplished very rapidly.

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.mime.text import MIMEText

# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.
with open(textfile, 'rb') as fp:
    # Create a text/plain message
    msg = MIMEText(fp.read())

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()

For sending email to multiple destinations, you can also follow the example in the Python documentation:

# Import smtplib for the actual sending function
import smtplib

# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = me
msg['To'] = ', '.join(family)
msg.preamble = 'Our family reunion'

# Assume we know that the image files are all in PNG format
for file in pngfiles:
    # Open the files in binary mode.  Let the MIMEImage class automatically
    # guess the specific image type.
    with open(file, 'rb') as fp:
        img = MIMEImage(fp.read())
    msg.attach(img)

# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.sendmail(me, family, msg.as_string())
s.quit()

As you can see, the header To in the MIMEText object must be a string consisting of email addresses separated by commas. On the other hand, the second argument to the sendmail function must be a list of strings (each string is an email address).

So, if you have three email addresses: person1@example.com, person2@example.com, and person3@example.com, you can do as follows (obvious sections omitted):

to = ["person1@example.com", "person2@example.com", "person3@example.com"]
msg['To'] = ",".join(to)
s.sendmail(me, to, msg.as_string())

the "","".join(to) part makes a single string out of the list, separated by commas.

From your questions I gather that you have not gone through the Python tutorial - it is a MUST if you want to get anywhere in Python - the documentation is mostly excellent for the standard library.

Thank you this works very nicely from within a function, however how can I send to multiple recipients? Since msg[to] looks like it's a dictionary key, I tried seperating the msg[to] with a semicolon but that does not seem to work.

@cloud311 exactly as you have it in your code. It wants a string delimited with commas: ", ".join(["a@example.com", "b@example.net"])

Thanks again, this should do the trick. I appreciate the help, I am relatively new to Python/Programming in general.

Note also that the To: header has different semantics than the envelope recipient. For example, you can use '"Tony Meyer" <tony.meyer@gmail.com>' as an address in the To: header, but the envelope recipient must be only "tony.meyer@gmail.com". To build a 'nice' To: address, use email.utils.formataddr, like email.utils.formataddr("Tony Meyer", "tony.meyer@gmail.com").

Small improvement: the file should be opened using with: with open(textfile, 'rb') as fp:. The explicit close can be dropped, as the with block will close the file even if an error occurs inside it.

How to send an email with Python? - Stack Overflow

python email function smtplib
Rectangle 27 133

I recommend that you use the standard packages email and smtplib together to send email. Please look at the following example (reproduced from the Python documentation). Notice that if you follow this approach, the "simple" task is indeed simple, and the more complex tasks (like attaching binary objects or sending plain/HTML multipart messages) are accomplished very rapidly.

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.mime.text import MIMEText

# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.
with open(textfile, 'rb') as fp:
    # Create a text/plain message
    msg = MIMEText(fp.read())

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()

For sending email to multiple destinations, you can also follow the example in the Python documentation:

# Import smtplib for the actual sending function
import smtplib

# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = me
msg['To'] = ', '.join(family)
msg.preamble = 'Our family reunion'

# Assume we know that the image files are all in PNG format
for file in pngfiles:
    # Open the files in binary mode.  Let the MIMEImage class automatically
    # guess the specific image type.
    with open(file, 'rb') as fp:
        img = MIMEImage(fp.read())
    msg.attach(img)

# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.sendmail(me, family, msg.as_string())
s.quit()

As you can see, the header To in the MIMEText object must be a string consisting of email addresses separated by commas. On the other hand, the second argument to the sendmail function must be a list of strings (each string is an email address).

So, if you have three email addresses: person1@example.com, person2@example.com, and person3@example.com, you can do as follows (obvious sections omitted):

to = ["person1@example.com", "person2@example.com", "person3@example.com"]
msg['To'] = ",".join(to)
s.sendmail(me, to, msg.as_string())

the "","".join(to) part makes a single string out of the list, separated by commas.

From your questions I gather that you have not gone through the Python tutorial - it is a MUST if you want to get anywhere in Python - the documentation is mostly excellent for the standard library.

Thank you this works very nicely from within a function, however how can I send to multiple recipients? Since msg[to] looks like it's a dictionary key, I tried seperating the msg[to] with a semicolon but that does not seem to work.

@cloud311 exactly as you have it in your code. It wants a string delimited with commas: ", ".join(["a@example.com", "b@example.net"])

Thanks again, this should do the trick. I appreciate the help, I am relatively new to Python/Programming in general.

Note also that the To: header has different semantics than the envelope recipient. For example, you can use '"Tony Meyer" <tony.meyer@gmail.com>' as an address in the To: header, but the envelope recipient must be only "tony.meyer@gmail.com". To build a 'nice' To: address, use email.utils.formataddr, like email.utils.formataddr("Tony Meyer", "tony.meyer@gmail.com").

Small improvement: the file should be opened using with: with open(textfile, 'rb') as fp:. The explicit close can be dropped, as the with block will close the file even if an error occurs inside it.

How to send an email with Python? - Stack Overflow

python email function smtplib
Rectangle 27 5

The syntax is relatively similar to Matlab (not exactly, but fairly easy to get used to). You also have the matplotlib python library, which gives some of Matlab's functionality while trying to provide the same API (i.e. function names etc.). This could be a starting point for you to start using Python, while staying in a familiar ground.

To learn Python, I'd recommend the Official Python Tutorial - which IMO is quite good for beginners.

Suggestions to learn any programming language after Matlab - Stack Ove...

matlab programming-languages
Rectangle 27 20

To add files to the Compile Sources build phase using a script, you will need to manipulate your project's project.pbxproj file programmatically.

Generally speaking, you would accomplish this by parsing the project.pbxproj file into an in-memory data structure, manipulating that data structure through a programmatic interface, and then writing the data structure out to a new project.pbxproj file.

There are several projects out there that could potentially help you do this, I haven't tried any of them:

And here is a series of blog posts with great general info on the contents and format of XCode project.pbxproj files.

Finally, it may be worth noting that for very simple manipulations, particularly if you aren't concerned about the cosmetics of your project.pbxproj file getting messed up, you can follow the suggestion over at this Stack Overflow answer to parse the project.pbxproj file on the command line like so:

plutil -convert xml1 -o - myproj.xcodeproj/project.pbxproj

While more than possible to accomplish this way, it would be a crime to be paid by the hour to do it.

Don't rely on plutil to do the job, I just had cases where it produced corrupt data without even showing a warning message.

python - Tutorial or Guide for Scripting XCode Build Phases - Stack Ov...

python ruby xcode build-automation
Rectangle 27 20

To add files to the Compile Sources build phase using a script, you will need to manipulate your project's project.pbxproj file programmatically.

Generally speaking, you would accomplish this by parsing the project.pbxproj file into an in-memory data structure, manipulating that data structure through a programmatic interface, and then writing the data structure out to a new project.pbxproj file.

There are several projects out there that could potentially help you do this, I haven't tried any of them:

And here is a series of blog posts with great general info on the contents and format of XCode project.pbxproj files.

Finally, it may be worth noting that for very simple manipulations, particularly if you aren't concerned about the cosmetics of your project.pbxproj file getting messed up, you can follow the suggestion over at this Stack Overflow answer to parse the project.pbxproj file on the command line like so:

plutil -convert xml1 -o - myproj.xcodeproj/project.pbxproj

While more than possible to accomplish this way, it would be a crime to be paid by the hour to do it.

Don't rely on plutil to do the job, I just had cases where it produced corrupt data without even showing a warning message.

python - Tutorial or Guide for Scripting XCode Build Phases - Stack Ov...

python ruby xcode build-automation
Rectangle 27 225

+1 I agree they were good for a beginner like me. I ran through both, now I at least know what it is and the basics of getting/using it. (The second for some reason explained activate but neglected deactivate o_O). I'm still hoping for more elaboration on when to use it (and when not to), and deeper examples.

I believe the answer is to always use it, in the same way you should always use version control.

on windows use /Scripts/activate (and deactivate) instead of /bin/activate.

Be aware that --no-site-packages is deprecated, and now it's the default behaviour.

This gave me a good idea about virtualenv. Recommended to use virtualenvwrapper which was mentioned in later part of the doc.

It's funny how basically not a single virtualenv tutorial on the internet, nor virtualenv's user guide, actually show you how to use the environment on your Python script. You aren't setting up an environment just for the sake of setting up an environment. I found one tutorial that at least shows you the alternative.

python - Comprehensive beginner's virtualenv tutorial? - Stack Overflo...

python virtualenv
Rectangle 27 225

+1 I agree they were good for a beginner like me. I ran through both, now I at least know what it is and the basics of getting/using it. (The second for some reason explained activate but neglected deactivate o_O). I'm still hoping for more elaboration on when to use it (and when not to), and deeper examples.

I believe the answer is to always use it, in the same way you should always use version control.

Be aware that --no-site-packages is deprecated, and now it's the default behaviour.

This gave me a good idea about virtualenv. Recommended to use virtualenvwrapper which was mentioned in later part of the doc.

It's funny how basically not a single virtualenv tutorial on the internet, nor virtualenv's user guide, actually show you how to use the environment on your Python script. You aren't setting up an environment just for the sake of setting up an environment. I found one tutorial that at least shows you the alternative.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

python - Comprehensive beginner's virtualenv tutorial? - Stack Overflo...

python virtualenv
Rectangle 27 225

+1 I agree they were good for a beginner like me. I ran through both, now I at least know what it is and the basics of getting/using it. (The second for some reason explained activate but neglected deactivate o_O). I'm still hoping for more elaboration on when to use it (and when not to), and deeper examples.

I believe the answer is to always use it, in the same way you should always use version control.

Be aware that --no-site-packages is deprecated, and now it's the default behaviour.

This gave me a good idea about virtualenv. Recommended to use virtualenvwrapper which was mentioned in later part of the doc.

It's funny how basically not a single virtualenv tutorial on the internet, nor virtualenv's user guide, actually show you how to use the environment on your Python script. You aren't setting up an environment just for the sake of setting up an environment. I found one tutorial that at least shows you the alternative.

python - Comprehensive beginner's virtualenv tutorial? - Stack Overflo...

python virtualenv
Rectangle 27 62

Most performant - Python 2.7 and 3, dict comprehension:

keys = ('name', 'age', 'food')
values = ('Monty', 42, 'spam')

What is the simplest way to produce the following dictionary ?

dict = {'name' : 'Monty', 'age' : 42, 'food' : 'spam'}

A possible improvement on using the dict constructor is to use the native syntax of a dict comprehension (not a list comprehension, as others have mistakenly put it):

In Python 2, zip returns a list, to avoid creating an unnecessary list, use izip instead (aliased to zip can reduce code changes when you move to Python 3).

from itertools import izip as zip
new_dict = {k: v for k, v in zip(keys, values)}

izip from itertools becomes zip in Python 3. izip is better than zip for Python 2 (because it avoids the unnecessary list creation), and ideal for 2.6 or below:

from itertools import izip
new_dict = dict(izip(keys, values))

In Python 3, zip becomes the same function that was in the itertools module, so that is simply:

new_dict = dict(zip(keys, values))

A dict comprehension would be more performant though (see performance review at the end of this answer).

>>> new_dict
{'age': 42, 'name': 'Monty', 'food': 'spam'}

If we look at the help on dict we see that it takes a variety of forms of arguments:

>>> help(dict)

class dict(object)
 |  dict() -> new empty dictionary
 |  dict(mapping) -> new dictionary initialized from a mapping object's
 |      (key, value) pairs
 |  dict(iterable) -> new dictionary initialized as if via:
 |      d = {}
 |      for k, v in iterable:
 |          d[k] = v
 |  dict(**kwargs) -> new dictionary initialized with the name=value pairs
 |      in the keyword argument list.  For example:  dict(one=1, two=2)
>>> zip(keys, values)
[('name', 'Monty'), ('age', 42), ('food', 'spam')]

In Python 3, the equivalent would be:

>>> list(zip(keys, values))
[('name', 'Monty'), ('age', 42), ('food', 'spam')]

and Python 3's zip merely creates an iterable object:

>>> zip(keys, values)
<zip object at 0x7f0e2ad029c8>

Since we want to avoid creating unnecessary data structures, we usually want to avoid Python 2's zip (since it creates an unnecessary list).

This is a generator expression being passed to the dict constructor:

generator_expression = ((k, v) for k, v in zip(keys, values))
dict(generator_expression)
dict((k, v) for k, v in zip(keys, values))

And this is a list comprehension being passed to the dict constructor:

dict([(k, v) for k, v in zip(keys, values)])

In the first two cases, an extra layer of non-operative (thus unnecessary) computation is placed over the zip iterable, and in the case of the list comprehension, an extra list is unnecessarily created. I would expect all of them to be less performant, and certainly not more-so.

In 64 bit Python 3.4.3, on Ubuntu 14.04, ordered from fastest to slowest:

>>> min(timeit.repeat(lambda: {k: v for k, v in zip(keys, values)}))
0.7836067057214677
>>> min(timeit.repeat(lambda: dict(zip(keys, values))))
1.0321204089559615
>>> min(timeit.repeat(lambda: {keys[i]: values[i] for i in range(len(keys))}))
1.0714934510178864
>>> min(timeit.repeat(lambda: dict([(k, v) for k, v in zip(keys, values)])))
1.6110592018812895
>>> min(timeit.repeat(lambda: dict((k, v) for k, v in zip(keys, values))))
1.7361853648908436

Map two lists into a dictionary in Python - Stack Overflow

python dictionary
Rectangle 27 186

Install pip in python 2.7.9 and python 3.4

For me, the following mostly works:

have pip installed, e.g.:

$ easy_install pip

Check, how is your installed package named from pip point of view:

$ pip freeze

This shall list names of all packages, you have installed (and which were detected by pip). The name can be sometime long, then use just the name of the package being shown at the and after #egg=. You can also in most cases ignore the version part (whatever follows == or -).

$ pip uninstall package.name.you.have.found

If it asks for confirmation about removing the package, then you are lucky guy and it will be removed.

pip shall detect all packages, which were installed by pip. It shall also detect most of the packages installed via easy_install or setup.py, but this may in some rare cases fail.

Here is real sample from my local test with package named ttr.rdstmc on MS Windows.

$ pip freeze |grep ttr
ttr.aws.s3==0.1.1dev
ttr.aws.utils.s3==0.3.0
ttr.utcutils==0.1.1dev

$ python setup.py develop
.....
.....
Finished processing dependencies for ttr.rdstmc==0.0.1dev

$ pip freeze |grep ttr
ttr.aws.s3==0.1.1dev
ttr.aws.utils.s3==0.3.0
-e hg+https://vlcinsky@bitbucket.org/vlcinsky/ttr.rdstmc@d61a9922920c508862602f7f39e496f7b99315f0#egg=ttr.rdstmc-dev
ttr.utcutils==0.1.1dev

$ pip uninstall ttr.rdstmc
Uninstalling ttr.rdstmc:
  c:\python27\lib\site-packages\ttr.rdstmc.egg-link
Proceed (y/n)? y
  Successfully uninstalled ttr.rdstmc

$ pip freeze |grep ttr
ttr.aws.s3==0.1.1dev
ttr.aws.utils.s3==0.3.0
ttr.utcutils==0.1.1dev

All what is written above still applies, anyway, there are small modifications available now.

Recent python versions come with a package ensurepip allowing to install pip even when being offline:

On some systems (like Debian Jessie) this is not available (to prevent breaking system python installation).

Examples above assume, you have grep installed. I had (at the time I had MS Windows on my machine) installed set of linux utilities (incl. grep). Alternatively, use native MS Windows find or simply ignore that filtering and find the name in a bit longer list of detected python packages.

which packaging tool/version are you using for this to work? (distutils, distribute, etc...)

I use setuptools. You may check bitbucket.org/vlcinsky/ttr.aws.utils.s3 (which is not perfect). Anyway, I am quite unhappy with use of namespace packages, sometime import are broken without visible reason. But it is another story.

correcting myself. Even though I import setuptools, in fact I use distribute. Another lesson: when installing my own package from pypi and planning to develop another one locally sharing the same namespace, do not use $ pip install <the package>, use $ easy_install <the package>, otherwise you run into conflicts when importing your develop package.

easy_install --user --prefix=$HOME pip
ln -s /home/tflutre/.local/bin/pip2 ~/bin

This should be accepted answer, as is much easier and pip usually comes with Python anyway. It is simple to use in command prompt/terminal - pip uninstall module or python -m pip uninstall module should work. Also should be cross-platform, as is part of Python.

freeze
develop

python setup.py uninstall - Stack Overflow

python setup.py pypi
Rectangle 27 186

Install pip in python 2.7.9 and python 3.4

For me, the following mostly works:

have pip installed, e.g.:

$ easy_install pip

Check, how is your installed package named from pip point of view:

$ pip freeze

This shall list names of all packages, you have installed (and which were detected by pip). The name can be sometime long, then use just the name of the package being shown at the and after #egg=. You can also in most cases ignore the version part (whatever follows == or -).

$ pip uninstall package.name.you.have.found

If it asks for confirmation about removing the package, then you are lucky guy and it will be removed.

pip shall detect all packages, which were installed by pip. It shall also detect most of the packages installed via easy_install or setup.py, but this may in some rare cases fail.

Here is real sample from my local test with package named ttr.rdstmc on MS Windows.

$ pip freeze |grep ttr
ttr.aws.s3==0.1.1dev
ttr.aws.utils.s3==0.3.0
ttr.utcutils==0.1.1dev

$ python setup.py develop
.....
.....
Finished processing dependencies for ttr.rdstmc==0.0.1dev

$ pip freeze |grep ttr
ttr.aws.s3==0.1.1dev
ttr.aws.utils.s3==0.3.0
-e hg+https://vlcinsky@bitbucket.org/vlcinsky/ttr.rdstmc@d61a9922920c508862602f7f39e496f7b99315f0#egg=ttr.rdstmc-dev
ttr.utcutils==0.1.1dev

$ pip uninstall ttr.rdstmc
Uninstalling ttr.rdstmc:
  c:\python27\lib\site-packages\ttr.rdstmc.egg-link
Proceed (y/n)? y
  Successfully uninstalled ttr.rdstmc

$ pip freeze |grep ttr
ttr.aws.s3==0.1.1dev
ttr.aws.utils.s3==0.3.0
ttr.utcutils==0.1.1dev

All what is written above still applies, anyway, there are small modifications available now.

Recent python versions come with a package ensurepip allowing to install pip even when being offline:

On some systems (like Debian Jessie) this is not available (to prevent breaking system python installation).

Examples above assume, you have grep installed. I had (at the time I had MS Windows on my machine) installed set of linux utilities (incl. grep). Alternatively, use native MS Windows find or simply ignore that filtering and find the name in a bit longer list of detected python packages.

which packaging tool/version are you using for this to work? (distutils, distribute, etc...)

I use setuptools. You may check bitbucket.org/vlcinsky/ttr.aws.utils.s3 (which is not perfect). Anyway, I am quite unhappy with use of namespace packages, sometime import are broken without visible reason. But it is another story.

correcting myself. Even though I import setuptools, in fact I use distribute. Another lesson: when installing my own package from pypi and planning to develop another one locally sharing the same namespace, do not use $ pip install <the package>, use $ easy_install <the package>, otherwise you run into conflicts when importing your develop package.

easy_install --user --prefix=$HOME pip
ln -s /home/tflutre/.local/bin/pip2 ~/bin

This should be accepted answer, as is much easier and pip usually comes with Python anyway. It is simple to use in command prompt/terminal - pip uninstall module or python -m pip uninstall module should work. Also should be cross-platform, as is part of Python.

python setup.py uninstall - Stack Overflow

python setup.py pypi
Rectangle 27 87

Optimizing tail-recursion in Python

Edit (2015-07-12): I finally published a module performing tail-call optimization (handling both tail-recursion and continuation-passing style): https://github.com/baruchel/tco

It has often been claimed that tail-recursion doesn't suit the pythonic way of coding and that one shouldn't care about how to embed it in a loop. I don't want to argue with this point of view; sometimes however I like trying or implementing new ideas as tail-recursive functions rather than with loops for various reasons (focusing on the idea rather than on the process, having twenty short functions on my screen in the same time rather than only three "pythonic" functions, working in an interactive session rather than editing my code, etc.).

Optimizing tail-recursion in Python is in fact quite easy. While it is said to be impossible or very tricky, I think it can be achieved with elegant, short and general solutions; I even think that most of these solutions don't use Python features otherwise than they should. Clean lambda expressions working along with very standard loops lead to quick, efficient and fully usable tools for implementing tail-recursion optimization.

As a personal convenience, I wrote a small module implementing such an optimization by two different ways. I would like to discuss here about my two main functions.

The Y combinator is well known; it allows to use lambda functions in a recursive manner, but it doesn't allow by itself to embed recursive calls in a loop. Lambda calculus alone can't do such a thing. A slight change in the Y combinator however can protect the recursive call to be actually evaluated. Evaluation can thus be delayed.

Here is the famous expression for the Y combinator:

With a very slight change, I could get:

lambda f: (lambda x: x(x))(lambda y: f(lambda *args: lambda: y(y)(*args)))

Instead of calling itself, the function f now returns a function performing the very same call, but since it returns it, the evaluation can be done later from outside.

def bet(func):
    b = (lambda f: (lambda x: x(x))(lambda y:
          f(lambda *args: lambda: y(y)(*args))))(func)
    def wrapper(*args):
        out = b(*args)
        while callable(out):
            out = out()
        return out
    return wrapper

The function can be used in the following way; here are two examples with tail-recursive versions of factorial and Fibonacci:

>>> from recursion import *
>>> fac = bet( lambda f: lambda n, a: a if not n else f(n-1,a*n) )
>>> fac(5,1)
120
>>> fibo = bet( lambda f: lambda n,p,q: p if not n else f(n-1,q,p+q) )
>>> fibo(10,0,1)
55
>>> bet( lambda f: lambda n: 42 if not n else f(n-1) )(50000)
42

This is of course the single real purpose of the function.

Only one thing can't be done with this optimization: it can't be used with a tail-recursive function evaluating to another function (this comes from the fact that callable returned objects are all handled as further recursive calls with no distinction). Since I usually don't need such a feature, I am very happy with the code above. However, in order to provide a more general module, I thought a little more in order to find some workaround for this issue (see next section).

Concerning the speed of this process (which isn't the real issue however), it happens to be quite good; tail-recursive functions are even evaluated much quicker than with the following code using simpler expressions:

def bet1(func):
    def wrapper(*args):
        out = func(lambda *x: lambda: x)(*args)
        while callable(out):
            out = func(lambda *x: lambda: x)(*out())
        return out
    return wrapper

I think that evaluating one expression, even complicated, is much quicker than evaluating several simple expressions, which is the case in this second version. I didn't keep this new function in my module, and I see no circumstances where it could be used rather than the "official" one.

Here is a more general function; it is able to handle all tail-recursive functions, including those returning other functions. Recursive calls are recognized from other return values by the use of exceptions. This solutions is slower than the previous one; a quicker code could probably be written by using some special values as "flags" being detected in the main loop, but I don't like the idea of using special values or internal keywords. There is some funny interpretation of using exceptions: if Python doesn't like tail-recursive calls, an exception should be raised when a tail-recursive call does occur, and the pythonic way will be to catch the exception in order to find some clean solution, which is actually what happens here...

class _RecursiveCall(Exception):
  def __init__(self, *args):
    self.args = args
def _recursiveCallback(*args):
  raise _RecursiveCall(*args)
def bet0(func):
    def wrapper(*args):
        while True:
          try:
            return func(_recursiveCallback)(*args)
          except _RecursiveCall as e:
            args = e.args
    return wrapper

Now all functions can be used. In the following example, f(n) is evaluated to the identity function for any positive value of n:

>>> f = bet0( lambda f: lambda n: (lambda x: x) if not n else f(n-1) )
>>> f(5)(42)
42

Of course it could be argued that exceptions are not intended to be used for intentionnaly redirecting the interpreter (as a kind of goto statement or probably rather a kind of continuation passing style), which I have to admit. But, again, I find funny the idea of using try with a single line being a return statement: we try to return something (normal behaviour) but we can't do it because of a recursive call occuring (exception).

It can embed a lambda function written with a tail recursion style in another function which will evaluate it as a loop.

The most interesting feature in this small function, in my humble opinion, is that the function doesn't rely on some dirty programming hack but on mere lambda calculus: the behaviour of the function is changed to another one when inserted in another lambda function which looks very like the Y-combinator.

This answer is quite interesting. Why down voted? He could show some code as a proof though. But if no code was the reason for the down vote it was no different from other highly voted answers just quoting Guido said no hence no.

Does Python optimize tail recursion? - Stack Overflow

python recursion stack stack-overflow tail-recursion
Rectangle 27 26

Let's say you're working in 2 different projects, A and B. Project A is a web project and the team is using the following packages:

The project B is also a web project but your team is using:

The machine that you're working doesn't have any version of django, what should you do? Install django 1.4? django 1.6? If you install django 1.4 globally would be easy to point to django 1.6 to work in project A?

Virtualenv is your solution! You can create 2 different virtualenv's, one for project A and another for project B. Now, when you need to work in project A, just activate the virtualenv for project A, and vice-versa.

A better tip when using virtualenv is to install virtualenvwrapper to manage all the virtualenv's that you have, easily. It's a wrapper for creating, working, removing virtualenv's.

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.

python - Comprehensive beginner's virtualenv tutorial? - Stack Overflo...

python virtualenv
Rectangle 27 26

Let's say you're working in 2 different projects, A and B. Project A is a web project and the team is using the following packages:

The project B is also a web project but your team is using:

The machine that you're working doesn't have any version of django, what should you do? Install django 1.4? django 1.6? If you install django 1.4 globally would be easy to point to django 1.6 to work in project A?

Virtualenv is your solution! You can create 2 different virtualenv's, one for project A and another for project B. Now, when you need to work in project A, just activate the virtualenv for project A, and vice-versa.

A better tip when using virtualenv is to install virtualenvwrapper to manage all the virtualenv's that you have, easily. It's a wrapper for creating, working, removing virtualenv's.

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.

python - Comprehensive beginner's virtualenv tutorial? - Stack Overflo...

python virtualenv
Rectangle 27 1474

Python 2 2.7.8 and Python 3 3.3

Good news! Python 3.4 (released March 2014) and Python 2.7.9 (released December 2014) ship with Pip. This is the best feature of any Python release. It makes the community's wealth of libraries accessible to everyone. Newbies are no longer excluded from using community libraries by the prohibitive difficulty of setup. In shipping with a package manager, Python joins Ruby, Node.js, Haskell, Perl, Go--almost every other contemporary language with a majority open-source community. Thank you Python.

And, alas for everyone using Python 2.7.8 or earlier (a sizable portion of the community). There's no plan to ship Pip to you. Manual instructions follow.

Flying in the face of its 'batteries included' motto, Python ships without a package manager. To make matters worse, Pip was--until recently--ironically difficult to install.

Download get-pip.py, being careful to save it as a .py file rather than .txt. Then, run it from the command prompt:

python get-pip.py

You possibly need an administrator command prompt to do this. Follow Start a Command Prompt as an Administrator (Microsoft TechNet).

This installs the pip package, which (in Windows) contains ...\Scripts\pip.exe that path must be in PATH environment variable to use pip from the command line (see second part of 'Alternative Instructions' for adding it to your PATH,

The official documentation tells users to install Pip and each of its dependencies from source. That's tedious for the experienced, and prohibitively difficult for newbies.

For our sake, Christoph Gohlke prepares Windows installers (.msi) for popular Python packages. He builds installers for all Python versions, both 32 and 64 bit. You need to:

For me, this installed Pip at C:\Python27\Scripts\pip.exe. Find pip.exe on your computer, then add its folder (for example, C:\Python27\Scripts) to your path (Start / Edit environment variables). Now you should be able to run pip from the command line. Try installing a package:

pip install httpie

There you go (hopefully)! Solutions for common problems are given below:

If you work in an office, you might be behind a HTTP proxy. If so, set the environment variables http_proxy and https_proxy. Most Python applications (and other free software) respect these. Example syntax:

http://proxy_url:port
http://username:password@proxy_url:port

If you're really unlucky, your proxy might be a Microsoft NTLM proxy. Free software can't cope. The only solution is to install a free software friendly proxy that forwards to the nasty proxy. http://cntlm.sourceforge.net/

Python modules can be part written in C or C++. Pip tries to compile from source. If you don't have a C/C++ compiler installed and configured, you'll see this cryptic error message.

You can fix that by installing a C++ compiler such as MinGW or Visual C++. Microsoft actually ship one specifically for use with Python. Or try Microsoft Visual C++ Compiler for Python 2.7.

Is that "can be fixed and will not be fixed" or "cannot be fixed and will not be fixed"?

@MikeMcMahon that happened to me too. Put Python before Perl in your path, so typing pip gets you the Python package manager.

I didn't have to do any of this, I just followed the instructions on pip-installer.org/en/latest/installing.html (basically you just write "python ez_setup.py" and then "python get-pip.py")

This is probably a dumb question. I entered python get-pip.py on a windows prompt but get "python is not recognized as an internal or external command, operable program or batch file." Am I doing something wrong? I have Python 2.7.8 installed. Do I need to change the directory? Thanks.

To start the "build in" pip you have to use python -m pip on windows!

python - How do I install pip on Windows? - Stack Overflow

python windows installation pip easy-install
Rectangle 27 1461

Python 2 2.7.8 and Python 3 3.3

Good news! Python 3.4 (released March 2014) and Python 2.7.9 (released December 2014) ship with Pip. This is the best feature of any Python release. It makes the community's wealth of libraries accessible to everyone. Newbies are no longer excluded from using community libraries by the prohibitive difficulty of setup. In shipping with a package manager, Python joins Ruby, Node.js, Haskell, Perl, Go--almost every other contemporary language with a majority open-source community. Thank you Python.

And, alas for everyone using Python 2.7.8 or earlier (a sizable portion of the community). There's no plan to ship Pip to you. Manual instructions follow.

Flying in the face of its 'batteries included' motto, Python ships without a package manager. To make matters worse, Pip was--until recently--ironically difficult to install.

Download get-pip.py, being careful to save it as a .py file rather than .txt. Then, run it from the command prompt:

python get-pip.py

You possibly need an administrator command prompt to do this. Follow Start a Command Prompt as an Administrator (Microsoft TechNet).

This installs the pip package, which (in Windows) contains ...\Scripts\pip.exe that path must be in PATH environment variable to use pip from the command line (see second part of 'Alternative Instructions' for adding it to your PATH,

The official documentation tells users to install Pip and each of its dependencies from source. That's tedious for the experienced, and prohibitively difficult for newbies.

For our sake, Christoph Gohlke prepares Windows installers (.msi) for popular Python packages. He builds installers for all Python versions, both 32 and 64 bit. You need to:

For me, this installed Pip at C:\Python27\Scripts\pip.exe. Find pip.exe on your computer, then add its folder (for example, C:\Python27\Scripts) to your path (Start / Edit environment variables). Now you should be able to run pip from the command line. Try installing a package:

pip install httpie

There you go (hopefully)! Solutions for common problems are given below:

If you work in an office, you might be behind a HTTP proxy. If so, set the environment variables http_proxy and https_proxy. Most Python applications (and other free software) respect these. Example syntax:

http://proxy_url:port
http://username:password@proxy_url:port

If you're really unlucky, your proxy might be a Microsoft NTLM proxy. Free software can't cope. The only solution is to install a free software friendly proxy that forwards to the nasty proxy. http://cntlm.sourceforge.net/

Python modules can be part written in C or C++. Pip tries to compile from source. If you don't have a C/C++ compiler installed and configured, you'll see this cryptic error message.

You can fix that by installing a C++ compiler such as MinGW or Visual C++. Microsoft actually ship one specifically for use with Python. Or try Microsoft Visual C++ Compiler for Python 2.7.

Is that "can be fixed and will not be fixed" or "cannot be fixed and will not be fixed"?

@MikeMcMahon that happened to me too. Put Python before Perl in your path, so typing pip gets you the Python package manager.

I didn't have to do any of this, I just followed the instructions on pip-installer.org/en/latest/installing.html (basically you just write "python ez_setup.py" and then "python get-pip.py")

This is probably a dumb question. I entered python get-pip.py on a windows prompt but get "python is not recognized as an internal or external command, operable program or batch file." Am I doing something wrong? I have Python 2.7.8 installed. Do I need to change the directory? Thanks.

To start the "build in" pip you have to use python -m pip on windows!

python - How do I install pip on Windows? - Stack Overflow

python windows installation pip easy-install
Rectangle 27 1458

Python 2 2.7.8 and Python 3 3.3

Good news! Python 3.4 (released March 2014) and Python 2.7.9 (released December 2014) ship with Pip. This is the best feature of any Python release. It makes the community's wealth of libraries accessible to everyone. Newbies are no longer excluded from using community libraries by the prohibitive difficulty of setup. In shipping with a package manager, Python joins Ruby, Node.js, Haskell, Perl, Go--almost every other contemporary language with a majority open-source community. Thank you Python.

And, alas for everyone using Python 2.7.8 or earlier (a sizable portion of the community). There's no plan to ship Pip to you. Manual instructions follow.

Flying in the face of its 'batteries included' motto, Python ships without a package manager. To make matters worse, Pip was--until recently--ironically difficult to install.

Download get-pip.py, being careful to save it as a .py file rather than .txt. Then, run it from the command prompt:

python get-pip.py

You possibly need an administrator command prompt to do this. Follow Start a Command Prompt as an Administrator (Microsoft TechNet).

This installs the pip package, which (in Windows) contains ...\Scripts\pip.exe that path must be in PATH environment variable to use pip from the command line (see second part of 'Alternative Instructions' for adding it to your PATH,

The official documentation tells users to install Pip and each of its dependencies from source. That's tedious for the experienced, and prohibitively difficult for newbies.

For our sake, Christoph Gohlke prepares Windows installers (.msi) for popular Python packages. He builds installers for all Python versions, both 32 and 64 bit. You need to:

For me, this installed Pip at C:\Python27\Scripts\pip.exe. Find pip.exe on your computer, then add its folder (for example, C:\Python27\Scripts) to your path (Start / Edit environment variables). Now you should be able to run pip from the command line. Try installing a package:

pip install httpie

There you go (hopefully)! Solutions for common problems are given below:

If you work in an office, you might be behind a HTTP proxy. If so, set the environment variables http_proxy and https_proxy. Most Python applications (and other free software) respect these. Example syntax:

http://proxy_url:port
http://username:password@proxy_url:port

If you're really unlucky, your proxy might be a Microsoft NTLM proxy. Free software can't cope. The only solution is to install a free software friendly proxy that forwards to the nasty proxy. http://cntlm.sourceforge.net/

Python modules can be part written in C or C++. Pip tries to compile from source. If you don't have a C/C++ compiler installed and configured, you'll see this cryptic error message.

You can fix that by installing a C++ compiler such as MinGW or Visual C++. Microsoft actually ship one specifically for use with Python. Or try Microsoft Visual C++ Compiler for Python 2.7.

Is that "can be fixed and will not be fixed" or "cannot be fixed and will not be fixed"?

@MikeMcMahon that happened to me too. Put Python before Perl in your path, so typing pip gets you the Python package manager.

I didn't have to do any of this, I just followed the instructions on pip-installer.org/en/latest/installing.html (basically you just write "python ez_setup.py" and then "python get-pip.py")

This is probably a dumb question. I entered python get-pip.py on a windows prompt but get "python is not recognized as an internal or external command, operable program or batch file." Am I doing something wrong? I have Python 2.7.8 installed. Do I need to change the directory? Thanks.

To start the "build in" pip you have to use python -m pip on windows!

python - How do I install pip on Windows? - Stack Overflow

python windows installation pip easy-install
Rectangle 27 1044

Connecting to MYSQL with Python in 3 steps

You must install a MySQL driver before doing anything. Unlike PHP, only the SQLite driver is installed by default with Python. The most used package to do so is MySQLdb but it's hard to install it using easy_install.

For Windows user, you can get an exe of MySQLdb.

For Linux, this is a casual package (python-mysqldb). (You can use sudo apt-get install python-mysqldb (for debian based distros), yum install MySQL-python (for rpm-based), or dnf install python-mysql (for modern fedora distro) in command line to download.)

After installing, reboot. This is not mandatory, but will prevent me from answering 3 or 4 other questions in this post if something goes wrong. So please reboot.

Then it is just like using another package :

#!/usr/bin/python
import MySQLdb

db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                     user="john",         # your username
                     passwd="megajonhy",  # your password
                     db="jonhydb")        # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

# Use all the SQL you like
cur.execute("SELECT * FROM YOUR_TABLE_NAME")

# print all the first cell of all the rows
for row in cur.fetchall():
    print row[0]

db.close()

Of course, there are thousand of possibilities and options; this is a very basic example. You will have to look at the documentation. A good starting point.

Once you know how it works, you may want to use an ORM to avoid writting SQL manually and manipulate your tables as they were Python objects. The most famous ORM in the Python community is SQLAlchemy.

I recently discovered another jewel in the Python world: peewee. It's a very lite ORM, really easy and fast to setup then use. It makes my day for small projects or stand alone apps, where using big tools like SQLAlchemy or Django is overkill :

import peewee
from peewee import *

db = MySQLDatabase('jonhydb', user='john', passwd='megajonhy')

class Book(peewee.Model):
    author = peewee.CharField()
    title = peewee.TextField()

    class Meta:
        database = db

Book.create_table()
book = Book(author="me", title='Peewee is cool')
book.save()
for book in Book.filter(author="me"):
    print book.title

This example works out of the box. Nothing other than having peewee (pip install peewee) is required.

Glad you like peewee!! I've added support for MySQL as well as some documentation on integrating with it. Happy hacking!

Note, as of writing this, MySQLdb does not support Python 3. The sourceforge page says "Python 3 support coming soon" but it has not been updated since 2012-10-08. For Python 3 there's PyMySQL and oursql.

pip install MySQL-python

Peewee has change the default behavior and does not use autocommit by default anymore. Even the DB driver situation has changed now. The question needs updating, but since the abuse of edits on some of my answers, I don't edit them anymore.

How do I connect to a MySQL Database in Python? - Stack Overflow

python mysql
Rectangle 27 1034

Connecting to MYSQL with Python in 3 steps

You must install a MySQL driver before doing anything. Unlike PHP, only the SQLite driver is installed by default with Python. The most used package to do so is MySQLdb but it's hard to install it using easy_install.

For Windows user, you can get an exe of MySQLdb.

For Linux, this is a casual package (python-mysqldb). (You can use sudo apt-get install python-mysqldb (for debian based distros), yum install MySQL-python (for rpm-based), or dnf install python-mysql (for modern fedora distro) in command line to download.)

After installing, reboot. This is not mandatory, but will prevent me from answering 3 or 4 other questions in this post if something goes wrong. So please reboot.

Then it is just like using another package :

#!/usr/bin/python
import MySQLdb

db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                     user="john",         # your username
                     passwd="megajonhy",  # your password
                     db="jonhydb")        # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

# Use all the SQL you like
cur.execute("SELECT * FROM YOUR_TABLE_NAME")

# print all the first cell of all the rows
for row in cur.fetchall():
    print row[0]

db.close()

Of course, there are thousand of possibilities and options; this is a very basic example. You will have to look at the documentation. A good starting point.

Once you know how it works, you may want to use an ORM to avoid writting SQL manually and manipulate your tables as they were Python objects. The most famous ORM in the Python community is SQLAlchemy.

I recently discovered another jewel in the Python world: peewee. It's a very lite ORM, really easy and fast to setup then use. It makes my day for small projects or stand alone apps, where using big tools like SQLAlchemy or Django is overkill :

import peewee
from peewee import *

db = MySQLDatabase('jonhydb', user='john', passwd='megajonhy')

class Book(peewee.Model):
    author = peewee.CharField()
    title = peewee.TextField()

    class Meta:
        database = db

Book.create_table()
book = Book(author="me", title='Peewee is cool')
book.save()
for book in Book.filter(author="me"):
    print book.title

This example works out of the box. Nothing other than having peewee (pip install peewee) is required.

Glad you like peewee!! I've added support for MySQL as well as some documentation on integrating with it. Happy hacking!

Note, as of writing this, MySQLdb does not support Python 3. The sourceforge page says "Python 3 support coming soon" but it has not been updated since 2012-10-08. For Python 3 there's PyMySQL and oursql.

pip install MySQL-python

Peewee has change the default behavior and does not use autocommit by default anymore. Even the DB driver situation has changed now. The question needs updating, but since the abuse of edits on some of my answers, I don't edit them anymore.

How do I connect to a MySQL Database in Python? - Stack Overflow

python mysql
Rectangle 27 1034

Connecting to MYSQL with Python in 3 steps

You must install a MySQL driver before doing anything. Unlike PHP, only the SQLite driver is installed by default with Python. The most used package to do so is MySQLdb but it's hard to install it using easy_install.

For Windows user, you can get an exe of MySQLdb.

For Linux, this is a casual package (python-mysqldb). (You can use sudo apt-get install python-mysqldb (for debian based distros), yum install MySQL-python (for rpm-based), or dnf install python-mysql (for modern fedora distro) in command line to download.)

After installing, reboot. This is not mandatory, but will prevent me from answering 3 or 4 other questions in this post if something goes wrong. So please reboot.

Then it is just like using another package :

#!/usr/bin/python
import MySQLdb

db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                     user="john",         # your username
                     passwd="megajonhy",  # your password
                     db="jonhydb")        # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

# Use all the SQL you like
cur.execute("SELECT * FROM YOUR_TABLE_NAME")

# print all the first cell of all the rows
for row in cur.fetchall():
    print row[0]

db.close()

Of course, there are thousand of possibilities and options; this is a very basic example. You will have to look at the documentation. A good starting point.

Once you know how it works, you may want to use an ORM to avoid writting SQL manually and manipulate your tables as they were Python objects. The most famous ORM in the Python community is SQLAlchemy.

I recently discovered another jewel in the Python world: peewee. It's a very lite ORM, really easy and fast to setup then use. It makes my day for small projects or stand alone apps, where using big tools like SQLAlchemy or Django is overkill :

import peewee
from peewee import *

db = MySQLDatabase('jonhydb', user='john', passwd='megajonhy')

class Book(peewee.Model):
    author = peewee.CharField()
    title = peewee.TextField()

    class Meta:
        database = db

Book.create_table()
book = Book(author="me", title='Peewee is cool')
book.save()
for book in Book.filter(author="me"):
    print book.title

This example works out of the box. Nothing other than having peewee (pip install peewee) is required.

Glad you like peewee!! I've added support for MySQL as well as some documentation on integrating with it. Happy hacking!

Note, as of writing this, MySQLdb does not support Python 3. The sourceforge page says "Python 3 support coming soon" but it has not been updated since 2012-10-08. For Python 3 there's PyMySQL and oursql.

pip install MySQL-python

I especially like peewee's included pwiz.py script which generates the python models for you!

Peewee has change the default behavior and does not use autocommit by default anymore. Even the DB driver situation has changed now. The question needs updating, but since the abuse of edits on some of my answers, I don't edit them anymore.

How do I connect to a MySQL Database in Python? - Stack Overflow

python mysql