title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags list |
|---|---|---|---|---|---|---|---|---|---|
javascript locals()? | 39,960 | <p>In python one can get a dictionary of all local and global variables in the current scope with the built-in functions locals() and globals(). Is there some equivalent way of doing this in javascript? For instance, I would like to do something like the following:</p>
<pre><code>var foo = function(){ alert('foo'); ... | 17 | 2008-09-02T16:29:30Z | 40,035 | <p>@e-bartek, I think that window[functionName] won't work if you in some closure, and the function name is local to that closure. For example:</p>
<pre><code>function foo() {
var bar = function () {
alert('hello world');
};
var s = 'bar';
window[s](); // this won't work
}
</code></pre>
<p>In... | 0 | 2008-09-02T17:00:12Z | [
"javascript",
"python"
] |
javascript locals()? | 39,960 | <p>In python one can get a dictionary of all local and global variables in the current scope with the built-in functions locals() and globals(). Is there some equivalent way of doing this in javascript? For instance, I would like to do something like the following:</p>
<pre><code>var foo = function(){ alert('foo'); ... | 17 | 2008-09-02T16:29:30Z | 40,067 | <p>@pkaeding</p>
<p>Yes, you're right. <em>window[functionName]()</em> doesn't work in this case, but <em>eval</em> does. If I needed something like this, I'd create my own object to keep those functions together.</p>
<pre><code>var func = {};
func.bar = ...;
var s = "bar";
func[s]();
</code></pre>
| 0 | 2008-09-02T17:14:25Z | [
"javascript",
"python"
] |
javascript locals()? | 39,960 | <p>In python one can get a dictionary of all local and global variables in the current scope with the built-in functions locals() and globals(). Is there some equivalent way of doing this in javascript? For instance, I would like to do something like the following:</p>
<pre><code>var foo = function(){ alert('foo'); ... | 17 | 2008-09-02T16:29:30Z | 40,173 | <ul>
<li><p>locals() - No. </p></li>
<li><p>globals() - Yes.</p></li>
</ul>
<p><code>window</code> is a reference to the global scope, like <code>globals()</code> in python.</p>
<pre><code>globals()["foo"]
</code></pre>
<p>is the same as:</p>
<pre><code>window["foo"]
</code></pre>
| 13 | 2008-09-02T18:01:29Z | [
"javascript",
"python"
] |
cx_Oracle: how do I get the ORA-xxxxx error number? | 40,586 | <p>In a try/except block, how do I extract the Oracle error number?</p>
| 1 | 2008-09-02T20:40:29Z | 40,593 | <pre><code>try:
cursor.execute("select 1 / 0 from dual")
except cx_Oracle.DatabaseError, exc:
error, = exc
print "Code:", error.code
print "Message:", error.message
</code></pre>
<p>This results in the following output:</p>
<pre><code>Code: 1476
Message: ORA-01476: divisor is equal to zero
</code></pre>
| 9 | 2008-09-02T20:41:33Z | [
"python",
"oracle",
"cx-oracle"
] |
Python deployment and /usr/bin/env portability | 40,705 | <p>At the beginning of all my executable Python scripts I put the <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)">shebang</a> line:</p>
<pre><code>#!/usr/bin/env python
</code></pre>
<p>I'm running these scripts on a system where <code>env python</code> yields a Python 2.2 environment. My scripts quickly fail b... | 12 | 2008-09-02T21:21:14Z | 40,715 | <p>"env" simply executes the first thing it finds in the PATH env var. To switch to different python, prepend the directory for that python's executable to the path before invoking your script.</p>
| 8 | 2008-09-02T21:25:40Z | [
"python",
"executable",
"environment",
"shebang"
] |
Python deployment and /usr/bin/env portability | 40,705 | <p>At the beginning of all my executable Python scripts I put the <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)">shebang</a> line:</p>
<pre><code>#!/usr/bin/env python
</code></pre>
<p>I'm running these scripts on a system where <code>env python</code> yields a Python 2.2 environment. My scripts quickly fail b... | 12 | 2008-09-02T21:21:14Z | 40,721 | <p>If you are running the scripts then you can set your PATH variable to point to a private bin directory first:</p>
<pre><code>$ mkdir ~/bin
$ ln -s `which python2.4` ~/bin/python
$ export PATH=~/bin:$PATH
</code></pre>
<p>Then when you execute your python script it'll use python 2.4. You'll have to change your logi... | 2 | 2008-09-02T21:28:03Z | [
"python",
"executable",
"environment",
"shebang"
] |
Python deployment and /usr/bin/env portability | 40,705 | <p>At the beginning of all my executable Python scripts I put the <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)">shebang</a> line:</p>
<pre><code>#!/usr/bin/env python
</code></pre>
<p>I'm running these scripts on a system where <code>env python</code> yields a Python 2.2 environment. My scripts quickly fail b... | 12 | 2008-09-02T21:21:14Z | 42,516 | <p>Pretty hackish solution - if your check fails, use this function (which probably could be significantly improved) to determine the best interpreter available, determine if it is acceptable, and if so relaunch your script with os.system or something similar and your sys.argv using the new interpreter.</p>
<pre><code... | 4 | 2008-09-03T20:32:40Z | [
"python",
"executable",
"environment",
"shebang"
] |
Python deployment and /usr/bin/env portability | 40,705 | <p>At the beginning of all my executable Python scripts I put the <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)">shebang</a> line:</p>
<pre><code>#!/usr/bin/env python
</code></pre>
<p>I'm running these scripts on a system where <code>env python</code> yields a Python 2.2 environment. My scripts quickly fail b... | 12 | 2008-09-02T21:21:14Z | 42,794 | <p>@morais: That's an interesting idea, but I think maybe we can take it one step farther. Maybe there's a way to use <a href="http://pypi.python.org/pypi/virtualenv" rel="nofollow">Ian Bicking's virtualenv</a> to:</p>
<ul>
<li>See if we're running in an acceptable environment to begin with, and if so, do nothing.</li... | 0 | 2008-09-03T23:07:53Z | [
"python",
"executable",
"environment",
"shebang"
] |
Python deployment and /usr/bin/env portability | 40,705 | <p>At the beginning of all my executable Python scripts I put the <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)">shebang</a> line:</p>
<pre><code>#!/usr/bin/env python
</code></pre>
<p>I'm running these scripts on a system where <code>env python</code> yields a Python 2.2 environment. My scripts quickly fail b... | 12 | 2008-09-02T21:21:14Z | 19,188,285 | <p>Here's a solution if you're (1) absolutely set on using shebangs and (2) able to use Autotools in your build process.</p>
<p>I just found last night that you can use the autoconf macro <code>AM_PATH_PYTHON</code> to find a minimal Python <strong>2</strong> binary. The how-to is <a href="http://stackoverflow.com/a/... | 0 | 2013-10-04T18:27:17Z | [
"python",
"executable",
"environment",
"shebang"
] |
Always including the user in the django template context | 41,547 | <p>I am working on a small intranet site for a small company, where user should be able to post. I have imagined a very simple authentication mechanism where people just enter their email address, and gets sent a unique login url, that sets a cookie that will always identify them for future requests.</p>
<p>In my temp... | 25 | 2008-09-03T12:22:44Z | 41,555 | <p>If you can hook your authentication into the <a href="http://www.djangoproject.com/documentation/authentication/#authentication-in-web-requests" rel="nofollow">Django authentication scheme</a> you'll be able to use <code>request.user</code>.</p>
<p>I think this should just be a case of calling <code>authenticate()<... | 2 | 2008-09-03T12:28:34Z | [
"python",
"django",
"authentication",
"session",
"cookies"
] |
Always including the user in the django template context | 41,547 | <p>I am working on a small intranet site for a small company, where user should be able to post. I have imagined a very simple authentication mechanism where people just enter their email address, and gets sent a unique login url, that sets a cookie that will always identify them for future requests.</p>
<p>In my temp... | 25 | 2008-09-03T12:22:44Z | 41,558 | <p>In a more general sense of not having to explicitly set variables in each view, it sounds like you want to look at writing your own <a href="http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors">context processor</a>.</p>
<p>From the docs:</p>
<blockquote>
<p>A context proc... | 18 | 2008-09-03T12:31:58Z | [
"python",
"django",
"authentication",
"session",
"cookies"
] |
Always including the user in the django template context | 41,547 | <p>I am working on a small intranet site for a small company, where user should be able to post. I have imagined a very simple authentication mechanism where people just enter their email address, and gets sent a unique login url, that sets a cookie that will always identify them for future requests.</p>
<p>In my temp... | 25 | 2008-09-03T12:22:44Z | 41,560 | <p>@Dave
To use {{user.username}} in my templates, I will then have to use
requestcontext rather than just a normal map/hash: <a href="http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext" rel="nofollow">http://www.djangoproject.com/documentation/templates_python/#subclassing... | 2 | 2008-09-03T12:33:31Z | [
"python",
"django",
"authentication",
"session",
"cookies"
] |
Always including the user in the django template context | 41,547 | <p>I am working on a small intranet site for a small company, where user should be able to post. I have imagined a very simple authentication mechanism where people just enter their email address, and gets sent a unique login url, that sets a cookie that will always identify them for future requests.</p>
<p>In my temp... | 25 | 2008-09-03T12:22:44Z | 269,249 | <p>@Ryan: Documentation about preprocessors is a bit small</p>
<p>@Staale: Adding user to the Context every time one is calling the template in view, DRY</p>
<p>Solution is very simple</p>
<p><strong>A</strong>: In your settings add</p>
<pre><code>TEMPLATE_CONTEXT_PROCESSORS = (
'myapp.processor_file_name.user'... | 31 | 2008-11-06T16:05:03Z | [
"python",
"django",
"authentication",
"session",
"cookies"
] |
Always including the user in the django template context | 41,547 | <p>I am working on a small intranet site for a small company, where user should be able to post. I have imagined a very simple authentication mechanism where people just enter their email address, and gets sent a unique login url, that sets a cookie that will always identify them for future requests.</p>
<p>In my temp... | 25 | 2008-09-03T12:22:44Z | 1,064,621 | <p>There is <strong>no need</strong> to write a context processor for the user object if you already have the <a href="https://docs.djangoproject.com/en/dev/topics/auth/default/#authentication-data-in-templates"><code>"django.core.context_processors.auth"</code></a> in <a href="https://docs.djangoproject.com/en/dev/ref... | 43 | 2009-06-30T16:14:29Z | [
"python",
"django",
"authentication",
"session",
"cookies"
] |
Always including the user in the django template context | 41,547 | <p>I am working on a small intranet site for a small company, where user should be able to post. I have imagined a very simple authentication mechanism where people just enter their email address, and gets sent a unique login url, that sets a cookie that will always identify them for future requests.</p>
<p>In my temp... | 25 | 2008-09-03T12:22:44Z | 4,815,619 | <p>its possible by default, by doing the following steps, ensure you have added the context 'django.contrib.auth.context_processors.auth' in your settings. By default its added in settings.py, so its looks like this </p>
<pre><code>TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.contr... | 1 | 2011-01-27T11:15:12Z | [
"python",
"django",
"authentication",
"session",
"cookies"
] |
Always including the user in the django template context | 41,547 | <p>I am working on a small intranet site for a small company, where user should be able to post. I have imagined a very simple authentication mechanism where people just enter their email address, and gets sent a unique login url, that sets a cookie that will always identify them for future requests.</p>
<p>In my temp... | 25 | 2008-09-03T12:22:44Z | 11,878,636 | <p>The hints are in every answer, but once again, from "scratch", for newbies:</p>
<p>authentication data is in templates (almost) by default -- with a small trick:</p>
<p>in <code>views.py</code>:</p>
<pre><code>from django.template import RequestContext
...
def index(request):
return render_to_response('index.... | 3 | 2012-08-09T07:28:58Z | [
"python",
"django",
"authentication",
"session",
"cookies"
] |
How do I do monkeypatching in python? | 41,562 | <p>I've had to do some introspection in python and it wasn't pretty:</p>
<pre><code>name = sys._getframe(1).f_code
name = "%s:%d %s()" %(os.path.split(name.co_filename)[1],name.co_firstlineno,name.co_name)
</code></pre>
<p>To get something like</p>
<pre><code>foo.py:22 bar() blah blah
</code></pre>
<p>In our debugg... | 7 | 2008-09-03T12:35:45Z | 41,574 | <p>A print statement does its IO through "sys.stdout.write" so you can override sys.stdout if you want to manipulate the print stream.</p>
| 3 | 2008-09-03T12:42:43Z | [
"python",
"monkeypatching"
] |
How do I do monkeypatching in python? | 41,562 | <p>I've had to do some introspection in python and it wasn't pretty:</p>
<pre><code>name = sys._getframe(1).f_code
name = "%s:%d %s()" %(os.path.split(name.co_filename)[1],name.co_firstlineno,name.co_name)
</code></pre>
<p>To get something like</p>
<pre><code>foo.py:22 bar() blah blah
</code></pre>
<p>In our debugg... | 7 | 2008-09-03T12:35:45Z | 41,921 | <p>The python <a href="http://www.python.org/doc/current/lib/module-inspect.html" rel="nofollow">inspect</a> module makes this a lot easier and cleaner. </p>
| 1 | 2008-09-03T15:21:20Z | [
"python",
"monkeypatching"
] |
Splitting tuples in Python - best practice? | 41,701 | <p>I have a method in my Python code that returns a tuple - a row from a SQL query. Let's say it has three fields: (jobId, label, username)</p>
<p>For ease of passing it around between functions, I've been passing the entire tuple as a variable called 'job'. Eventually, however, I want to get at the bits, so I've been... | 12 | 2008-09-03T13:48:30Z | 41,707 | <p>I'd say that a dictionary is definitely the best way to do it. It's easily extensible, allows you to give each value a sensible name, and Python has a lot of built-in language features for using and manipulating dictionaries. If you need to add more fields later, all you need to change is the code that converts th... | 13 | 2008-09-03T13:50:24Z | [
"python",
"tuples"
] |
Splitting tuples in Python - best practice? | 41,701 | <p>I have a method in my Python code that returns a tuple - a row from a SQL query. Let's say it has three fields: (jobId, label, username)</p>
<p>For ease of passing it around between functions, I've been passing the entire tuple as a variable called 'job'. Eventually, however, I want to get at the bits, so I've been... | 12 | 2008-09-03T13:48:30Z | 41,709 | <p>With a tuple it will always be a hassle to add or change fields. You're right that a dictionary will be much better. </p>
<p>If you want something with slightly friendlier syntax you might want to take a look at the answers <a href="http://stackoverflow.com/questions/35988/c-like-structures-in-python" rel="nofollow... | 0 | 2008-09-03T13:51:53Z | [
"python",
"tuples"
] |
Splitting tuples in Python - best practice? | 41,701 | <p>I have a method in my Python code that returns a tuple - a row from a SQL query. Let's say it has three fields: (jobId, label, username)</p>
<p>For ease of passing it around between functions, I've been passing the entire tuple as a variable called 'job'. Eventually, however, I want to get at the bits, so I've been... | 12 | 2008-09-03T13:48:30Z | 41,721 | <p>Perhaps this is overkill for your case, but I would be tempted to create a "Job" class that takes the tuple as its constructor argument and has respective properties on it. I'd then pass instances of this class around instead.</p>
| 3 | 2008-09-03T13:57:15Z | [
"python",
"tuples"
] |
Splitting tuples in Python - best practice? | 41,701 | <p>I have a method in my Python code that returns a tuple - a row from a SQL query. Let's say it has three fields: (jobId, label, username)</p>
<p>For ease of passing it around between functions, I've been passing the entire tuple as a variable called 'job'. Eventually, however, I want to get at the bits, so I've been... | 12 | 2008-09-03T13:48:30Z | 41,723 | <p>I would use a dictionary. You can convert the tuple to a dictionary this way:</p>
<pre><code>values = <querycode>
keys = ["jobid", "label", "username"]
job = dict([[keys[i], values [i]] for i in xrange(len(values ))])
</code></pre>
<p>This will first create an array [["jobid", val1], ["label", val2], ["usern... | 2 | 2008-09-03T13:59:28Z | [
"python",
"tuples"
] |
Splitting tuples in Python - best practice? | 41,701 | <p>I have a method in my Python code that returns a tuple - a row from a SQL query. Let's say it has three fields: (jobId, label, username)</p>
<p>For ease of passing it around between functions, I've been passing the entire tuple as a variable called 'job'. Eventually, however, I want to get at the bits, so I've been... | 12 | 2008-09-03T13:48:30Z | 41,730 | <p>How about this:</p>
<pre><code>class TypedTuple:
def __init__(self, fieldlist, items):
self.fieldlist = fieldlist
self.items = items
def __getattr__(self, field):
return self.items[self.fieldlist.index(field)]
</code></pre>
<p>You could then do:</p>
<pre><code>j = TypedTuple(["jobid",... | -2 | 2008-09-03T14:02:37Z | [
"python",
"tuples"
] |
Splitting tuples in Python - best practice? | 41,701 | <p>I have a method in my Python code that returns a tuple - a row from a SQL query. Let's say it has three fields: (jobId, label, username)</p>
<p>For ease of passing it around between functions, I've been passing the entire tuple as a variable called 'job'. Eventually, however, I want to get at the bits, so I've been... | 12 | 2008-09-03T13:48:30Z | 41,846 | <p>@Staale</p>
<p>There is a better way:</p>
<pre><code>job = dict(zip(keys, values))
</code></pre>
| 13 | 2008-09-03T14:51:48Z | [
"python",
"tuples"
] |
Splitting tuples in Python - best practice? | 41,701 | <p>I have a method in my Python code that returns a tuple - a row from a SQL query. Let's say it has three fields: (jobId, label, username)</p>
<p>For ease of passing it around between functions, I've been passing the entire tuple as a variable called 'job'. Eventually, however, I want to get at the bits, so I've been... | 12 | 2008-09-03T13:48:30Z | 387,242 | <p>An old question, but since no one mentioned it I'll add this from the Python Cookbook: </p>
<p><a href="http://code.activestate.com/recipes/81252/" rel="nofollow">Recipe 81252: Using dtuple for Flexible Query Result Access </a></p>
<p>This recipe is specifically designed for dealing with database results, and the ... | 2 | 2008-12-22T20:22:43Z | [
"python",
"tuples"
] |
Splitting tuples in Python - best practice? | 41,701 | <p>I have a method in my Python code that returns a tuple - a row from a SQL query. Let's say it has three fields: (jobId, label, username)</p>
<p>For ease of passing it around between functions, I've been passing the entire tuple as a variable called 'job'. Eventually, however, I want to get at the bits, so I've been... | 12 | 2008-09-03T13:48:30Z | 1,144,246 | <p>This is an old question, but...</p>
<p>I'd suggest using a named tuple in this situation: <a href="http://docs.python.org/3.1/library/collections.html#collections.namedtuple">collections.namedtuple</a></p>
<p>This is the part, in particular, that you'd find useful:</p>
<blockquote>
<p>Subclassing is not useful ... | 5 | 2009-07-17T16:08:40Z | [
"python",
"tuples"
] |
Splitting tuples in Python - best practice? | 41,701 | <p>I have a method in my Python code that returns a tuple - a row from a SQL query. Let's say it has three fields: (jobId, label, username)</p>
<p>For ease of passing it around between functions, I've been passing the entire tuple as a variable called 'job'. Eventually, however, I want to get at the bits, so I've been... | 12 | 2008-09-03T13:48:30Z | 2,609,123 | <p>If you're using the MySQLdb package, you can set up your cursor objects to return dicts instead of tuples.</p>
<pre><code>import MySQLdb, MySQLdb.cursors
conn = MySQLdb.connect(..., cursorclass=MySQLdb.cursors.DictCursor)
cur = conn.cursor() # a DictCursor
cur2 = conn.cursor(cursorclass=MySQLdb.cursors.Cursor) # a ... | 0 | 2010-04-09T16:28:06Z | [
"python",
"tuples"
] |
Standard way to open a folder window in linux? | 41,969 | <p>I want to open a folder window, in the appropriate file manager, from within a cross-platform (windows/mac/linux) Python application.</p>
<p>On OSX, I can open a window in the finder with</p>
<pre><code>os.system('open "%s"' % foldername)
</code></pre>
<p>and on Windows with</p>
<pre><code>os.startfile(foldernam... | 6 | 2008-09-03T15:47:23Z | 41,999 | <p>this would probably have to be done manually, or have as a config item since there are many file managers that users may want to use. Providing a way for command options as well.</p>
<p>There might be an function that launches the defaults for kde or gnome in their respective toolkits but I haven't had reason to lo... | 0 | 2008-09-03T16:02:06Z | [
"python",
"linux",
"cross-platform",
"desktop"
] |
Standard way to open a folder window in linux? | 41,969 | <p>I want to open a folder window, in the appropriate file manager, from within a cross-platform (windows/mac/linux) Python application.</p>
<p>On OSX, I can open a window in the finder with</p>
<pre><code>os.system('open "%s"' % foldername)
</code></pre>
<p>and on Windows with</p>
<pre><code>os.startfile(foldernam... | 6 | 2008-09-03T15:47:23Z | 42,039 | <p>You're going to have to do this based on the running window manager. OSX and Windows have a (defacto) standard way because there is only one choice.</p>
<p>You shouldn't need to specify the exact filemanager application, though, this should be possible to do through the wm. I know Gnome does, and it's important to ... | 0 | 2008-09-03T16:15:43Z | [
"python",
"linux",
"cross-platform",
"desktop"
] |
Standard way to open a folder window in linux? | 41,969 | <p>I want to open a folder window, in the appropriate file manager, from within a cross-platform (windows/mac/linux) Python application.</p>
<p>On OSX, I can open a window in the finder with</p>
<pre><code>os.system('open "%s"' % foldername)
</code></pre>
<p>and on Windows with</p>
<pre><code>os.startfile(foldernam... | 6 | 2008-09-03T15:47:23Z | 42,046 | <pre><code>os.system('xdg-open "%s"' % foldername)
</code></pre>
<p><code>xdg-open</code> can be used for files/urls also</p>
| 7 | 2008-09-03T16:18:25Z | [
"python",
"linux",
"cross-platform",
"desktop"
] |
What is a tuple useful for? | 42,034 | <p>I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type would be useful in practice. Can I have some examples, perhaps in Python, where an immutable set of numbers would be needed? How is this diffe... | 25 | 2008-09-03T16:13:06Z | 42,048 | <ul>
<li>Tuples are used whenever you want to return multiple results from a function.</li>
<li>Since they're immutable, they can be used as keys for a dictionary (lists can't).</li>
</ul>
| 32 | 2008-09-03T16:18:53Z | [
"python",
"tuples"
] |
What is a tuple useful for? | 42,034 | <p>I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type would be useful in practice. Can I have some examples, perhaps in Python, where an immutable set of numbers would be needed? How is this diffe... | 25 | 2008-09-03T16:13:06Z | 42,049 | <p>I find them useful when you always deal with two or more objects as a set.</p>
| 2 | 2008-09-03T16:19:14Z | [
"python",
"tuples"
] |
What is a tuple useful for? | 42,034 | <p>I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type would be useful in practice. Can I have some examples, perhaps in Python, where an immutable set of numbers would be needed? How is this diffe... | 25 | 2008-09-03T16:13:06Z | 42,050 | <p>A list can always replace a tuple, with respect to functionality (except, apparently, as keys in a dict). However, a tuple can make things go faster. The same is true for, for example, immutable strings in Java -- when will you ever need to be unable to alter your strings? Never!</p>
<p>I just read a decent discuss... | 1 | 2008-09-03T16:20:00Z | [
"python",
"tuples"
] |
What is a tuple useful for? | 42,034 | <p>I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type would be useful in practice. Can I have some examples, perhaps in Python, where an immutable set of numbers would be needed? How is this diffe... | 25 | 2008-09-03T16:13:06Z | 42,052 | <p>Tuples make good dictionary keys when you need to combine more than one piece of data into your key and don't feel like making a class for it.</p>
<pre><code>a = {}
a[(1,2,"bob")] = "hello!"
a[("Hello","en-US")] = "Hi There!"
</code></pre>
<p>I've used this feature primarily to create a dictionary with keys that a... | 14 | 2008-09-03T16:20:59Z | [
"python",
"tuples"
] |
What is a tuple useful for? | 42,034 | <p>I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type would be useful in practice. Can I have some examples, perhaps in Python, where an immutable set of numbers would be needed? How is this diffe... | 25 | 2008-09-03T16:13:06Z | 42,055 | <p>A tuple is useful for storing multiple values.. As you note a tuple is just like a list that is immutable - e.g. once created you cannot add/remove/swap elements.</p>
<p>One benefit of being immutable is that because the tuple is fixed size it allows the run-time to perform certain optimizations. This is particular... | 1 | 2008-09-03T16:23:48Z | [
"python",
"tuples"
] |
What is a tuple useful for? | 42,034 | <p>I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type would be useful in practice. Can I have some examples, perhaps in Python, where an immutable set of numbers would be needed? How is this diffe... | 25 | 2008-09-03T16:13:06Z | 42,060 | <p>Tuples and lists have the same uses in general. Immutable data types in general have many benefits, mostly about concurrency issues.</p>
<p>So, when you have lists that are not volatile in nature and you need to guarantee that no consumer is altering it, you may use a tuple.</p>
<p>Typical examples are fixed data ... | 2 | 2008-09-03T16:25:17Z | [
"python",
"tuples"
] |
What is a tuple useful for? | 42,034 | <p>I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type would be useful in practice. Can I have some examples, perhaps in Python, where an immutable set of numbers would be needed? How is this diffe... | 25 | 2008-09-03T16:13:06Z | 48,414 | <p>I like <a href="http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/">this explanation</a>.</p>
<p>Basically, you should use tuples when there's a constant structure (the 1st position always holds one type of value and the second another, and so forth), and lists should be used for lists of... | 7 | 2008-09-07T13:12:28Z | [
"python",
"tuples"
] |
What is a tuple useful for? | 42,034 | <p>I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type would be useful in practice. Can I have some examples, perhaps in Python, where an immutable set of numbers would be needed? How is this diffe... | 25 | 2008-09-03T16:13:06Z | 51,200 | <p>In addition to the places where they're syntactically required like the string % operation and for multiple return values, I use tuples as a form of lightweight classes. For example, suppose you have an object that passes out an opaque cookie to a caller from one method which is then passed into another method. A ... | 0 | 2008-09-09T03:56:09Z | [
"python",
"tuples"
] |
What is a tuple useful for? | 42,034 | <p>I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type would be useful in practice. Can I have some examples, perhaps in Python, where an immutable set of numbers would be needed? How is this diffe... | 25 | 2008-09-03T16:13:06Z | 39,638,334 | <p>A tuple is a sequence of values. The values can be any type, and they are indexed by integer, so tuples are not like lists. The most important difference is that <strong>tuples are immutable.</strong></p>
<p>A tuple is a comma-separated list of values:</p>
<pre><code>t = 'p', 'q', 'r', 's', 't'
</code></pre>
<p>i... | 0 | 2016-09-22T11:54:05Z | [
"python",
"tuples"
] |
Best way to extract text from a Word doc without using COM/automation? | 42,482 | <p>Is there a reasonable way to extract plain text from a Word file that doesn't depend on COM automation? (This is a a feature for a web app deployed on a non-Windows platform - that's non-negotiable in this case.)</p>
<p>Antiword seems like it might be a reasonable option, but it seems like it might be abandoned.</... | 15 | 2008-09-03T20:18:47Z | 42,485 | <p>Open Office has an <a href="http://api.openoffice.org/" rel="nofollow">API</a></p>
| 2 | 2008-09-03T20:20:00Z | [
"python",
"ms-word"
] |
Best way to extract text from a Word doc without using COM/automation? | 42,482 | <p>Is there a reasonable way to extract plain text from a Word file that doesn't depend on COM automation? (This is a a feature for a web app deployed on a non-Windows platform - that's non-negotiable in this case.)</p>
<p>Antiword seems like it might be a reasonable option, but it seems like it might be abandoned.</... | 15 | 2008-09-03T20:18:47Z | 42,492 | <p>If it is Word 2007 docx, you could unzip it and parse the XML files that are contained inside.</p>
| 0 | 2008-09-03T20:22:23Z | [
"python",
"ms-word"
] |
Best way to extract text from a Word doc without using COM/automation? | 42,482 | <p>Is there a reasonable way to extract plain text from a Word file that doesn't depend on COM automation? (This is a a feature for a web app deployed on a non-Windows platform - that's non-negotiable in this case.)</p>
<p>Antiword seems like it might be a reasonable option, but it seems like it might be abandoned.</... | 15 | 2008-09-03T20:18:47Z | 43,301 | <p>Using the OpenOffice API, and Python, and <a href="http://www.pitonyak.org/oo.php" rel="nofollow">Andrew Pitonyak's excellent online macro book</a> I managed to do this. Section 7.16.4 is the place to start.</p>
<p>One other tip to make it work without needing the screen at all is to use the Hidden property:</p>
<... | 2 | 2008-09-04T07:45:26Z | [
"python",
"ms-word"
] |
Best way to extract text from a Word doc without using COM/automation? | 42,482 | <p>Is there a reasonable way to extract plain text from a Word file that doesn't depend on COM automation? (This is a a feature for a web app deployed on a non-Windows platform - that's non-negotiable in this case.)</p>
<p>Antiword seems like it might be a reasonable option, but it seems like it might be abandoned.</... | 15 | 2008-09-03T20:18:47Z | 43,364 | <p>I use catdoc or antiword for this, whatever gives the result that is the easiest to parse. I have embedded this in python functions, so it is easy to use from the parsing system (which is written in python).</p>
<pre><code>import os
def doc_to_text_catdoc(filename):
(fi, fo, fe) = os.popen3('catdoc -w "%s"' % ... | 8 | 2008-09-04T08:52:01Z | [
"python",
"ms-word"
] |
Best way to extract text from a Word doc without using COM/automation? | 42,482 | <p>Is there a reasonable way to extract plain text from a Word file that doesn't depend on COM automation? (This is a a feature for a web app deployed on a non-Windows platform - that's non-negotiable in this case.)</p>
<p>Antiword seems like it might be a reasonable option, but it seems like it might be abandoned.</... | 15 | 2008-09-03T20:18:47Z | 1,387,028 | <p>For docx files, check out the Python script docx2txt available at</p>
<p><a href="http://cobweb.ecn.purdue.edu/~kak/distMisc/docx2txt" rel="nofollow">http://cobweb.ecn.purdue.edu/~kak/distMisc/docx2txt</a></p>
<p>for extracting the plain text from a docx document.</p>
| 1 | 2009-09-06T23:44:00Z | [
"python",
"ms-word"
] |
Best way to extract text from a Word doc without using COM/automation? | 42,482 | <p>Is there a reasonable way to extract plain text from a Word file that doesn't depend on COM automation? (This is a a feature for a web app deployed on a non-Windows platform - that's non-negotiable in this case.)</p>
<p>Antiword seems like it might be a reasonable option, but it seems like it might be abandoned.</... | 15 | 2008-09-03T20:18:47Z | 1,979,931 | <p>(Same answer as <a href="http://stackoverflow.com/questions/125222/extracting-text-from-ms-word-files-in-python">extracting text from MS word files in python</a>)</p>
<p>Use the native Python docx module which I made this week. Here's how to extract all the text from a doc:</p>
<pre><code>document = opendocx('Hell... | 14 | 2009-12-30T12:23:05Z | [
"python",
"ms-word"
] |
Best way to extract text from a Word doc without using COM/automation? | 42,482 | <p>Is there a reasonable way to extract plain text from a Word file that doesn't depend on COM automation? (This is a a feature for a web app deployed on a non-Windows platform - that's non-negotiable in this case.)</p>
<p>Antiword seems like it might be a reasonable option, but it seems like it might be abandoned.</... | 15 | 2008-09-03T20:18:47Z | 20,663,596 | <p>If all you want to do is extracting text from Word files (.docx), it's possible to do it only with Python. Like Guy Starbuck wrote it, you just need to unzip the file and then parse the XML. Inspired by <code>python-docx</code>, I have written a <a href="http://etienned.github.io/posts/extract-text-from-word-docx-si... | 1 | 2013-12-18T16:38:12Z | [
"python",
"ms-word"
] |
Best way to extract text from a Word doc without using COM/automation? | 42,482 | <p>Is there a reasonable way to extract plain text from a Word file that doesn't depend on COM automation? (This is a a feature for a web app deployed on a non-Windows platform - that's non-negotiable in this case.)</p>
<p>Antiword seems like it might be a reasonable option, but it seems like it might be abandoned.</... | 15 | 2008-09-03T20:18:47Z | 30,122,626 | <p><a href="http://stackoverflow.com/questions/16516044/is-it-possible-to-read-word-files-doc-docx-in-python/30122239#30122239">This worked well</a> for .doc and .odt.</p>
<p>It calls openoffice on the command line to convert your file to text, which you can then simply load into python.</p>
<p>(It seems to have othe... | 0 | 2015-05-08T11:23:19Z | [
"python",
"ms-word"
] |
Python re.sub MULTILINE caret match | 42,581 | <p>The Python docs say:</p>
<blockquote>
<p>re.MULTILINE: When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline)... By default, '^' matches only at the beginning of the string...</p>
</blockquote>
<p>So what's going on ... | 31 | 2008-09-03T21:00:33Z | 42,597 | <p>Look at the definition of <a href="http://docs.python.org/library/re.html#re.sub"><code>re.sub</code></a>:</p>
<pre><code>sub(pattern, repl, string[, count])
</code></pre>
<p>The 4th argument is the count, you are using <code>re.MULTILINE</code> (which is 8) as the count, not as a flag.</p>
<p>You have to compile... | 75 | 2008-09-03T21:08:02Z | [
"python",
"regex",
"python-2.x"
] |
Python re.sub MULTILINE caret match | 42,581 | <p>The Python docs say:</p>
<blockquote>
<p>re.MULTILINE: When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline)... By default, '^' matches only at the beginning of the string...</p>
</blockquote>
<p>So what's going on ... | 31 | 2008-09-03T21:00:33Z | 2,517,216 | <pre><code>re.sub('(?m)^//', '', s)
</code></pre>
| 6 | 2010-03-25T16:10:45Z | [
"python",
"regex",
"python-2.x"
] |
Python re.sub MULTILINE caret match | 42,581 | <p>The Python docs say:</p>
<blockquote>
<p>re.MULTILINE: When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline)... By default, '^' matches only at the beginning of the string...</p>
</blockquote>
<p>So what's going on ... | 31 | 2008-09-03T21:00:33Z | 7,248,027 | <p>The full definition of <a href="http://docs.python.org/library/re.html#re.sub" rel="nofollow"><code>re.sub</code></a> is:</p>
<pre><code>re.sub(pattern, repl, string[, count, flags])
</code></pre>
<p>Which means that if you tell Python what the parameters are, then you can pass <code>flags</code> without passing <... | 3 | 2011-08-30T18:33:24Z | [
"python",
"regex",
"python-2.x"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 42,997 | <p>EDIT: See @<a href="#43663">Blair Conrad's answer</a> for a cleaner solution</p>
<p><hr /></p>
<pre><code>>>> import datetime
>>> datetime.date (2000, 2, 1) - datetime.timedelta (days = 1)
datetime.date(2000, 1, 31)
>>>
</code></pre>
| 43 | 2008-09-04T01:26:33Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 43,088 | <p>EDIT: see my other answer. It has a better implementation than this one, which I leave here just in case someone's interested in seeing how one might "roll your own" calculator.</p>
<p>@<a href="#42997">John Millikin</a> gives a good answer, with the added complication of calculating the first day of the next month... | 32 | 2008-09-04T02:25:50Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 43,663 | <p>I didn't notice this earlier when I was looking at the <a href="https://docs.python.org/2/library/calendar.html">documentation for the calendar module</a>, but a method called <a href="http://docs.python.org/library/calendar.html#calendar.monthrange">monthrange</a> provides this information:</p>
<blockquote>
<p><... | 552 | 2008-09-04T12:44:12Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 356,535 | <p>Another solution would be to do something like this: </p>
<pre><code>from datetime import datetime
def last_day_of_month(year, month):
""" Work out the last day of the month """
last_days = [31, 30, 29, 28, 27]
for i in last_days:
try:
end = datetime(year, month, i)
except V... | 10 | 2008-12-10T15:47:57Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 12,064,327 | <p>i have a simple solution:
</p>
<pre><code>import datetime
datetime.date(2012,2, 1).replace(day=1,month=datetime.date(2012,2,1).month+1)-timedelta(days=1)
datetime.date(2012, 2, 29)
</code></pre>
| -6 | 2012-08-21T23:09:57Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 13,386,470 | <p>This does not address the main question, but one nice trick to get the last <em>weekday</em> in a month is to use <code>calendar.monthcalendar</code>, which returns a matrix of dates, organized with Monday as the first column through Sunday as the last.</p>
<pre><code># Some random date.
some_date = datetime.date(2... | 1 | 2012-11-14T20:06:26Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 13,565,185 | <p>If you don't want to import the <code>calendar</code> module, a simple two-step function can also be:</p>
<pre><code>import datetime
def last_day_of_month(any_day):
next_month = any_day.replace(day=28) + datetime.timedelta(days=4) # this will never fail
return next_month - datetime.timedelta(days=next_mon... | 37 | 2012-11-26T12:48:40Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 14,994,380 | <p>This is actually pretty easy with <code>dateutil.relativedelta</code> (package python-datetutil for pip). <code>day=31</code> will always always return the last day of the month.</p>
<p>Example:</p>
<pre><code>from datetime import datetime
from dateutil.relativedelta import relativedelta
date_in_feb = datetime.da... | 14 | 2013-02-21T04:09:09Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 17,135,571 | <pre><code>import datetime
now = datetime.datetime.now()
start_month = datetime.datetime(now.year, now.month, 1)
date_on_next_month = start_month + datetime.timedelta(35)
start_next_month = datetime.datetime(date_on_next_month.year, date_on_next_month.month, 1)
last_day_month = start_next_month - datetime.timedelta(1)... | 4 | 2013-06-16T16:51:42Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 23,383,345 | <p>If you wand to make your own small function, this is a good starting point:</p>
<pre><code>def eomday(year, month):
"""returns the number of days in a given month"""
days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
d = days_per_month[month - 1]
if month == 2 and (year % 4 == 0 and y... | 1 | 2014-04-30T08:33:57Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 23,447,523 | <pre><code>from datetime import timedelta
(any_day.replace(day=1) + timedelta(days=32)).replace(day=1) - timedelta(days=1)
</code></pre>
| 7 | 2014-05-03T17:16:58Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 24,929,705 | <p>For me it's the simplest way:</p>
<pre><code>selected_date = date(some_year, some_month, some_day)
if selected_date.month == 12: # December
last_day_selected_month = date(selected_date.year, selected_date.month, 31)
else:
last_day_selected_month = date(selected_date.year, selected_date.month + 1, 1) - ti... | 3 | 2014-07-24T09:16:25Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 27,667,421 | <p>Using <code>relativedelta</code> you would get last date of month like this:</p>
<pre><code>from dateutil.relativedelta import relativedelta
last_date_of_month = datetime(mydate.year,mydate.month,1)+relativedelta(months=1,days=-1)
</code></pre>
<p>The idea is to get the fist day of month and use <code>relativedelt... | 3 | 2014-12-27T12:54:51Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 28,886,669 | <pre><code>>>> import datetime
>>> import calendar
>>> date = datetime.datetime.now()
>>> print date
2015-03-06 01:25:14.939574
>>> print date.replace(day = 1)
2015-03-01 01:25:14.939574
>>> print date.replace(day = calendar.monthrange(date.year, date.month)[1])... | 3 | 2015-03-05T20:02:18Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 31,618,852 | <p>Use pandas!</p>
<pre><code>def isMonthEnd(date):
return date + pd.offsets.MonthEnd(0) == date
isMonthEnd(datetime(1999, 12, 31))
True
isMonthEnd(pd.Timestamp('1999-12-31'))
True
isMonthEnd(pd.Timestamp(1965, 1, 10))
False
</code></pre>
| 0 | 2015-07-24T19:59:53Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 37,246,666 | <pre><code>import calendar
from time import gmtime, strftime
calendar.monthrange(int(strftime("%Y", gmtime())), int(strftime("%m", gmtime())))[1]
</code></pre>
<p>Output:<br></p>
<pre><code>31
</code></pre>
<p><br><br>
This will print the last day of whatever the current month is. In this example it was 15th May, 20... | 0 | 2016-05-16T04:22:18Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 39,141,916 | <p>I prefer this way</p>
<pre><code>import datetime
import calendar
date=datetime.datetime.now()
month_end_date=datetime.datetime(date.year,date.month,1) + datetime.timedelta(days=calendar.monthrange(date.year,date.month)[1] - 1)
</code></pre>
| 0 | 2016-08-25T09:48:05Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 39,223,365 | <pre><code>from datetime import date, timedelta
import calendar
last_day = date.today().replace(day=calendar.monthrange(date.today().year, date.today().month)[1])
</code></pre>
| 0 | 2016-08-30T09:14:29Z | [
"python",
"date"
] |
Get Last Day of the Month in Python | 42,950 | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| 306 | 2008-09-04T00:54:44Z | 39,288,092 | <p>You can calculate the end date yourself. the simple logic is to subtract a day from the start_date of next month. :) </p>
<p>So write a custom method,</p>
<pre><code>import datetime
def end_date_of_a_month(date):
start_date_of_this_month = date.replace(day=1)
month = start_date_of_this_month.month
... | 1 | 2016-09-02T08:47:11Z | [
"python",
"date"
] |
How can I get a commit message from a bzr post-commit hook? | 43,099 | <p>I'm trying to write a bzr post-commit hook for my private bugtracker, but I'm stuck at the function signature of post_commit(local, master, old_revno, old_revid, new_revno, mew_revid). How can I extract the commit message for the branch from this with bzrlib in Python?</p>
| 3 | 2008-09-04T02:34:41Z | 43,172 | <p>And the answer is like so:</p>
<pre><code>def check_commit_msg(local, master, old_revno, old_revid, new_revno, new_revid):
branch = local or master
revision = branch.repository.get_revision(new_revid)
print revision.message
</code></pre>
<p>local and master are Branch objects, so once you have a revisi... | 5 | 2008-09-04T04:09:00Z | [
"python",
"dvcs",
"bazaar"
] |
How to generate urls in django | 43,290 | <p>In Django's template language, you can use <code>{% url [viewname] [args] %}</code> to generate a URL to a specific view with parameters. How can you programatically do the same in Python code?</p>
<p>What I need is to create a list of menu items where each item has name, URL, and an active flag (whether it's the ... | 22 | 2008-09-04T07:36:39Z | 43,312 | <p>If you need to use something similar to the <code>{% url %}</code> template tag in your code, Django provides the <code>django.core.urlresolvers.reverse()</code>. The <code>reverse</code> function has the following signature:</p>
<pre><code>reverse(viewname, urlconf=None, args=None, kwargs=None)
</code></pre>
<p><... | 32 | 2008-09-04T07:54:57Z | [
"python",
"django",
"url",
"django-urls"
] |
How to generate urls in django | 43,290 | <p>In Django's template language, you can use <code>{% url [viewname] [args] %}</code> to generate a URL to a specific view with parameters. How can you programatically do the same in Python code?</p>
<p>What I need is to create a list of menu items where each item has name, URL, and an active flag (whether it's the ... | 22 | 2008-09-04T07:36:39Z | 55,734 | <p>I'm using two different approaches in my <code>models.py</code>. The first is the <code>permalink</code> decorator:</p>
<pre><code>from django.db.models import permalink
def get_absolute_url(self):
"""Construct the absolute URL for this Item."""
return ('project.app.views.view_name', [str(self.id)])
get_a... | 8 | 2008-09-11T03:05:27Z | [
"python",
"django",
"url",
"django-urls"
] |
How to generate urls in django | 43,290 | <p>In Django's template language, you can use <code>{% url [viewname] [args] %}</code> to generate a URL to a specific view with parameters. How can you programatically do the same in Python code?</p>
<p>What I need is to create a list of menu items where each item has name, URL, and an active flag (whether it's the ... | 22 | 2008-09-04T07:36:39Z | 72,249 | <p>Be aware that using <code>reverse()</code> requires that your urlconf module is 100% error free and can be processed - iow no <code>ViewDoesNotExist</code> errors or so, or you get the dreaded <code>NoReverseMatch</code> exception (errors in templates usually fail silently resulting in <code>None</code>).</p>
| 4 | 2008-09-16T13:36:56Z | [
"python",
"django",
"url",
"django-urls"
] |
Can I write native iPhone apps using Python | 43,315 | <p>Using <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
| 78 | 2008-09-04T07:59:57Z | 43,331 | <p>Not currently, currently the only languages available to access the iPhone SDK are C/C++, Objective C and Swift.</p>
<p>There is no technical reason why this could not change in the future but I wouldn't hold your breath for this happening in the short term.</p>
<p>That said, Objective-C and Swift really are not t... | 32 | 2008-09-04T08:21:31Z | [
"iphone",
"python",
"cocoa-touch"
] |
Can I write native iPhone apps using Python | 43,315 | <p>Using <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
| 78 | 2008-09-04T07:59:57Z | 43,358 | <p>You can use PyObjC on the iPhone as well, due to the excellent work by Jay Freeman (saurik). See <a href="http://www.saurik.com/id/5">iPhone Applications in Python</a>.</p>
<p>Note that this requires a jailbroken iPhone at the moment.</p>
| 51 | 2008-09-04T08:44:11Z | [
"iphone",
"python",
"cocoa-touch"
] |
Can I write native iPhone apps using Python | 43,315 | <p>Using <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
| 78 | 2008-09-04T07:59:57Z | 145,071 | <p>The iPhone SDK agreement is also rather vague about whether you're even allowed to run scripting languages (outside of a WebView's Javascript). My reading is that it is OK - as long as none of the scripts you execute are downloaded from the network (so pre-installed and user-edited scripts seem to be OK).</p>
<p>I... | 5 | 2008-09-28T02:51:14Z | [
"iphone",
"python",
"cocoa-touch"
] |
Can I write native iPhone apps using Python | 43,315 | <p>Using <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
| 78 | 2008-09-04T07:59:57Z | 739,098 | <p>You can do this with PyObjC, with a jailbroken phone of course. But if you want to get it into the App Store, they will not allow it because it "interprets code." However, you may be able to use <a href="http://code.google.com/p/shedskin/" rel="nofollow">Shed Skin</a>, although I'm not aware of anyone doing this. I ... | 1 | 2009-04-10T22:49:09Z | [
"iphone",
"python",
"cocoa-touch"
] |
Can I write native iPhone apps using Python | 43,315 | <p>Using <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
| 78 | 2008-09-04T07:59:57Z | 2,005,197 | <p>The only significant "external" language for iPhone development that I'm aware of with semi-significant support in terms of frameworks and compatibility is <a href="http://monotouch.net/" rel="nofollow">MonoTouch</a>, a C#/.NET environment for developing on the iPhone.</p>
| 0 | 2010-01-05T09:56:51Z | [
"iphone",
"python",
"cocoa-touch"
] |
Can I write native iPhone apps using Python | 43,315 | <p>Using <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
| 78 | 2008-09-04T07:59:57Z | 2,167,033 | <p>Yes you can. You write your code in tinypy (which is restricted Python), then use tinypy to convert it to C++, and finally compile this with XCode into a native iPhone app. Phil Hassey has published a game called Elephants! using this approach. Here are more details,</p>
<p><a href="http://www.philhassey.com/blog/2... | 22 | 2010-01-30T06:03:39Z | [
"iphone",
"python",
"cocoa-touch"
] |
Can I write native iPhone apps using Python | 43,315 | <p>Using <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
| 78 | 2008-09-04T07:59:57Z | 2,637,228 | <p>An update to the iOS Developer Agreement means that you can use whatever you like, as long as you meet the developer guidelines. Section 3.3.1, which restricted what developers could use for iOS development, has been entirely removed.</p>
<p>Source: <a href="http://daringfireball.net/2010/09/app_store_guidelines">h... | 20 | 2010-04-14T12:18:15Z | [
"iphone",
"python",
"cocoa-touch"
] |
Can I write native iPhone apps using Python | 43,315 | <p>Using <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
| 78 | 2008-09-04T07:59:57Z | 2,768,536 | <p>Technically, as long as the interpreted code ISN'T downloaded (excluding JavaScript), the app may be approved. Rhomobiles "Rhodes" framework does just that, bundling mobile Ruby, a lightweight version of Rails, and your app for distribution via the app-store. Because both the interpreter and the interpreted code are... | 2 | 2010-05-04T19:54:20Z | [
"iphone",
"python",
"cocoa-touch"
] |
Can I write native iPhone apps using Python | 43,315 | <p>Using <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
| 78 | 2008-09-04T07:59:57Z | 3,684,714 | <p>It seems this is now something developers are allowed to do: the iOS Developer Agreement was changed yesterday and appears to have been ammended in a such a way as to make embedding a Python interpretter in your application legal:</p>
<p><strong>SECTION 3.3.2 â INTERPRETERS</strong></p>
<p><strong>Old:</strong><... | 21 | 2010-09-10T12:48:14Z | [
"iphone",
"python",
"cocoa-touch"
] |
Can I write native iPhone apps using Python | 43,315 | <p>Using <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
| 78 | 2008-09-04T07:59:57Z | 11,069,342 | <p>I think it was not possible earlier but I recently heard about PyMob, which seems interesting because the apps are written in Python and the final outputs are native source codes in various platforms (Obj-C for iOS, Java for Android etc). This is certainly quite unique. <a href="http://pyzia.com/technology.html" rel... | 1 | 2012-06-17T06:20:58Z | [
"iphone",
"python",
"cocoa-touch"
] |
Can I write native iPhone apps using Python | 43,315 | <p>Using <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
| 78 | 2008-09-04T07:59:57Z | 11,448,458 | <p>Yes, nowadays you can develop apps for iOS in Python. </p>
<p>There are two frameworks that you may want to checkout: <a href="http://kivy.org/">Kivy</a> and <a href="http://pyzia.com/technology.html">PyMob</a>.</p>
<p>Please consider the answers to <a href="http://stackoverflow.com/questions/10664196/is-it-possib... | 15 | 2012-07-12T09:07:42Z | [
"iphone",
"python",
"cocoa-touch"
] |
Can I write native iPhone apps using Python | 43,315 | <p>Using <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
| 78 | 2008-09-04T07:59:57Z | 18,601,032 | <p><a href="http://omz-software.com/pythonista">Pythonista</a> has an Export to Xcode feature that allows you to export your Python scripts as Xcode projects that build standalone iOS apps.</p>
| 6 | 2013-09-03T20:36:35Z | [
"iphone",
"python",
"cocoa-touch"
] |
A python web application framework for tight DB/GUI coupling? | 43,368 | <p>I'm a firm believer of the heretic thought of tight coupling between the backend and frontend: I want existing, implied knowledge about a backend to be automatically made use of when generating user interfaces. E.g., if a VARCHAR column has a maximum with of 20 characters, there GUIs should automatically constrain t... | 11 | 2008-09-04T08:53:58Z | 43,386 | <p>You should have a look at django and especially its <a href="http://www.djangoproject.com/documentation/forms/" rel="nofollow">newforms</a> and <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/#ref-contrib-admin" rel="nofollow">admin</a> modules. The newforms module provides a nice possibility to do s... | 3 | 2008-09-04T09:12:27Z | [
"python",
"sql",
"metadata",
"coupling",
"data-driven"
] |
A python web application framework for tight DB/GUI coupling? | 43,368 | <p>I'm a firm believer of the heretic thought of tight coupling between the backend and frontend: I want existing, implied knowledge about a backend to be automatically made use of when generating user interfaces. E.g., if a VARCHAR column has a maximum with of 20 characters, there GUIs should automatically constrain t... | 11 | 2008-09-04T08:53:58Z | 43,414 | <p>I believe that Django models does not support composite primary keys (see <a href="http://docs.djangoproject.com/en/dev/topics/db/models/#automatic-primary-key-fields" rel="nofollow">documentation</a>). But perhaps you can use SQLAlchemy in Django? A <a href="http://www.google.com/search?q=sqlalchemy+django" rel="no... | 1 | 2008-09-04T09:42:10Z | [
"python",
"sql",
"metadata",
"coupling",
"data-driven"
] |
A python web application framework for tight DB/GUI coupling? | 43,368 | <p>I'm a firm believer of the heretic thought of tight coupling between the backend and frontend: I want existing, implied knowledge about a backend to be automatically made use of when generating user interfaces. E.g., if a VARCHAR column has a maximum with of 20 characters, there GUIs should automatically constrain t... | 11 | 2008-09-04T08:53:58Z | 48,284 | <p><a href="http://www.turbogears.org/" rel="nofollow">TurboGears</a> currently uses <a href="http://www.sqlobject.org/" rel="nofollow">SQLObject</a> by default but you can use it with <a href="http://docs.turbogears.org/1.0/SQLAlchemy" rel="nofollow">SQLAlchemy</a>. They are saying that the next major release of Turbo... | 1 | 2008-09-07T10:03:48Z | [
"python",
"sql",
"metadata",
"coupling",
"data-driven"
] |
A python web application framework for tight DB/GUI coupling? | 43,368 | <p>I'm a firm believer of the heretic thought of tight coupling between the backend and frontend: I want existing, implied knowledge about a backend to be automatically made use of when generating user interfaces. E.g., if a VARCHAR column has a maximum with of 20 characters, there GUIs should automatically constrain t... | 11 | 2008-09-04T08:53:58Z | 48,479 | <p>I know that you specificity ask for a framework but I thought I would let you know about what I get up to here. I have just undergone converting my company's web application from a custom in-house ORM layer into sqlAlchemy so I am far from an expert but something that occurred to me was that sqlAlchemy has types for... | 1 | 2008-09-07T14:39:14Z | [
"python",
"sql",
"metadata",
"coupling",
"data-driven"
] |
A python web application framework for tight DB/GUI coupling? | 43,368 | <p>I'm a firm believer of the heretic thought of tight coupling between the backend and frontend: I want existing, implied knowledge about a backend to be automatically made use of when generating user interfaces. E.g., if a VARCHAR column has a maximum with of 20 characters, there GUIs should automatically constrain t... | 11 | 2008-09-04T08:53:58Z | 198,348 | <p>web2py does most of what you ask:</p>
<p>Based on a field type and its validators it will render the field with the appropriate widget. You can override with</p>
<pre><code>db.table.field.widget=...
</code></pre>
<p>and use a third party widget.</p>
<p>web2py has js to blocks the user from entering a non-integer... | 5 | 2008-10-13T17:31:38Z | [
"python",
"sql",
"metadata",
"coupling",
"data-driven"
] |
How to find the mime type of a file in python? | 43,580 | <p>Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.</p>
<p>Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) h... | 101 | 2008-09-04T12:07:27Z | 43,588 | <p>The <a href="https://docs.python.org/library/mimetypes.html">mimetypes module</a> in the standard library will determine/guess the MIME type from a file extension.</p>
<p>If users are uploading files the HTTP post will contain the MIME type of the file alongside the data. For example, Django makes this data availa... | 53 | 2008-09-04T12:12:20Z | [
"python",
"mime"
] |
How to find the mime type of a file in python? | 43,580 | <p>Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.</p>
<p>Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) h... | 101 | 2008-09-04T12:07:27Z | 43,616 | <p>You didn't state what web server you were using, but Apache has a nice little module called <a href="http://httpd.apache.org/docs/1.3/mod/mod_mime_magic.html" rel="nofollow">Mime Magic</a> which it uses to determine the type of a file when told to do so. It reads some of the file's content and tries to figure out w... | 5 | 2008-09-04T12:22:55Z | [
"python",
"mime"
] |
How to find the mime type of a file in python? | 43,580 | <p>Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.</p>
<p>Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) h... | 101 | 2008-09-04T12:07:27Z | 1,662,074 | <p>in python 2.6:</p>
<pre><code>mime = subprocess.Popen("/usr/bin/file --mime PATH", shell=True, \
stdout=subprocess.PIPE).communicate()[0]
</code></pre>
| 8 | 2009-11-02T15:48:09Z | [
"python",
"mime"
] |
How to find the mime type of a file in python? | 43,580 | <p>Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.</p>
<p>Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) h... | 101 | 2008-09-04T12:07:27Z | 2,133,843 | <p>More reliable way than to use the mimetypes library would be to use the python-magic package.</p>
<pre><code>import magic
m = magic.open(magic.MAGIC_MIME)
m.load()
m.file("/tmp/document.pdf")
</code></pre>
<p>This would be equivalent to using file(1).</p>
<p>On Django one could also make sure that the MIME type m... | 39 | 2010-01-25T16:39:06Z | [
"python",
"mime"
] |
How to find the mime type of a file in python? | 43,580 | <p>Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.</p>
<p>Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) h... | 101 | 2008-09-04T12:07:27Z | 2,753,385 | <p>The python-magic method suggested by toivotuo is outdated. <a href="http://github.com/ahupp/python-magic">Python-magic's</a> current trunk is at Github and based on the readme there, finding the MIME-type, is done like this.</p>
<pre><code># For MIME types
>>> import magic
>>> mime = magic.Magic(m... | 111 | 2010-05-02T12:02:45Z | [
"python",
"mime"
] |
How to find the mime type of a file in python? | 43,580 | <p>Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.</p>
<p>Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) h... | 101 | 2008-09-04T12:07:27Z | 10,713,515 | <p>you can use <strong>imghdr</strong> Python module.</p>
| -2 | 2012-05-23T04:37:49Z | [
"python",
"mime"
] |
How to find the mime type of a file in python? | 43,580 | <p>Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.</p>
<p>Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) h... | 101 | 2008-09-04T12:07:27Z | 11,101,343 | <p>The mimetypes module just recognise an file type based on file extension. If you will try to recover a file type of a file without extension, the mimetypes will not works.</p>
| 3 | 2012-06-19T12:51:55Z | [
"python",
"mime"
] |
How to find the mime type of a file in python? | 43,580 | <p>Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.</p>
<p>Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) h... | 101 | 2008-09-04T12:07:27Z | 12,297,929 | <p>There are 3 different libraries that wraps libmagic.</p>
<p>2 of them are available on pypi (so pip install will work):</p>
<ul>
<li>filemagic</li>
<li>python-magic</li>
</ul>
<p>And another, similar to python-magic is available directly in the latest libmagic sources, and it is the one you probably have in your ... | 9 | 2012-09-06T10:22:50Z | [
"python",
"mime"
] |
How to find the mime type of a file in python? | 43,580 | <p>Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.</p>
<p>Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) h... | 101 | 2008-09-04T12:07:27Z | 21,755,201 | <p>This seems to be very easy </p>
<pre><code>>>> from mimetypes import MimeTypes
>>> import urllib
>>> mime = MimeTypes()
>>> url = urllib.pathname2url('Upload.xml')
>>> mime_type = mime.guess_type(url)
>>> print mime_type
('application/xml', None)
</code></pre>
... | 13 | 2014-02-13T13:09:49Z | [
"python",
"mime"
] |
How to find the mime type of a file in python? | 43,580 | <p>Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.</p>
<p>Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) h... | 101 | 2008-09-04T12:07:27Z | 28,306,825 | <p>@toivotuo 's method worked best and most reliably for me under python3. My goal was to identify gzipped files which do not have a reliable .gz extension. I installed python3-magic.</p>
<pre><code>import magic
filename = "./datasets/test"
def file_mime_type(filename):
m = magic.open(magic.MAGIC_MIME)
m.lo... | 4 | 2015-02-03T19:09:32Z | [
"python",
"mime"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.