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
Namespace Specification In Absence of Ambuguity
539,578
<p>Why do some languages, like C++ and Python, require the namespace of an object be specified even when no ambiguity exists? I understand that there are backdoors to this, like <code>using namespace x</code> in C++, or <code>from x import *</code> in Python. However, I can't understand the rationale behind not wanti...
0
2009-02-12T00:56:34Z
540,137
<p>Interesting question. In the case of C++, as I see it, provided the compiler flagged an error as soon as there was a conflict, the only problem this could cause would be:</p> <p><strong>Auto-lookup of all C++ namespaces would remove the ability to hide the names of internal parts of library code.</strong></p> <p>...
0
2009-02-12T05:15:26Z
[ "c++", "python", "namespaces", "language-design" ]
Obtaining all possible states of an object for a NP-Complete(?) problem in Python
539,676
<p>Not sure that the example (nor the actual usecase) qualifies as NP-Complete, but I'm wondering about the most Pythonic way to do the below assuming that this was the algorithm available.</p> <p>Say you have :</p> <pre><code>class Person: def __init__(self): self.status='unknown' def set(self,value): if...
0
2009-02-12T01:45:41Z
539,695
<p>I think this could do it:</p> <pre><code>l = list() for i in xrange(2 ** n): # create the list of n people sublist = [None] * n for j in xrange(n): sublist[j] = Person() sublist[j].set(i &amp; (1 &lt;&lt; j)) l.append(sublist) </code></pre> <p>Note that if you wrote <code>Person</co...
2
2009-02-12T01:53:54Z
[ "iteration", "python", "combinatorics" ]
Obtaining all possible states of an object for a NP-Complete(?) problem in Python
539,676
<p>Not sure that the example (nor the actual usecase) qualifies as NP-Complete, but I'm wondering about the most Pythonic way to do the below assuming that this was the algorithm available.</p> <p>Say you have :</p> <pre><code>class Person: def __init__(self): self.status='unknown' def set(self,value): if...
0
2009-02-12T01:45:41Z
539,831
<p>You can use a cartesian product to get all possible combinations of people and states. Requires Python 2.6+</p> <pre><code>import itertools people = [person_a,person_b,person_c] states = [True,False] all_people_and_states = itertools.product(people,states) </code></pre> <p>The variable *all_people_and_states* cont...
1
2009-02-12T03:06:23Z
[ "iteration", "python", "combinatorics" ]
Obtaining all possible states of an object for a NP-Complete(?) problem in Python
539,676
<p>Not sure that the example (nor the actual usecase) qualifies as NP-Complete, but I'm wondering about the most Pythonic way to do the below assuming that this was the algorithm available.</p> <p>Say you have :</p> <pre><code>class Person: def __init__(self): self.status='unknown' def set(self,value): if...
0
2009-02-12T01:45:41Z
548,358
<p>According to what you've stated in your problem, you're right -- you do need <code>itertools.product</code>, but not exactly the way you've stated.</p> <pre><code>import itertools truth_values = itertools.product((True, False), repeat = 4) people = (person_a, person_b, person_c, person_d) all_people_and_states = [[...
1
2009-02-14T02:23:09Z
[ "iteration", "python", "combinatorics" ]
Custom django widget - decompress() arg not populated
539,899
<p>As an exercise I am trying to create a custom django widget for a 24 hour clock. The widget will is a MultiWidget - a select box for each field.</p> <p>I am trying to follow docs online (kinda sparse) and looking at the Pro Django book, but I can't seem to figure it out. Am I on the right track? I can save my data ...
3
2009-02-12T03:30:40Z
540,678
<p>I can't reproduce the problem:</p> <pre><code>&gt;&gt;&gt; class MyForm(forms.Form): ... t = forms.TimeField(widget=MilitaryTimeWidget()) ... &gt;&gt;&gt; print MyForm(data={'t_0': '13', 't_1': '34'}) ******** 13:34:00 &lt;tr&gt;&lt;th&gt;&lt;label for="id_t_0"&gt;T:&lt;/label&gt;&lt;/th&gt;&lt;td&gt;&lt;select...
0
2009-02-12T10:00:40Z
[ "python", "django", "forms", "widget", "field" ]
Custom django widget - decompress() arg not populated
539,899
<p>As an exercise I am trying to create a custom django widget for a 24 hour clock. The widget will is a MultiWidget - a select box for each field.</p> <p>I am trying to follow docs online (kinda sparse) and looking at the Pro Django book, but I can't seem to figure it out. Am I on the right track? I can save my data ...
3
2009-02-12T03:30:40Z
542,656
<p>Note <a href="http://code.djangoproject.com/browser/django/trunk/django/forms/widgets.py#L582" rel="nofollow">this line in the docstring for MultiWidget</a>:</p> <blockquote> <p>You'll probably want to use this class with MultiValueField.</p> </blockquote> <p>That's the root of your problem. You might be able t...
3
2009-02-12T18:26:20Z
[ "python", "django", "forms", "widget", "field" ]
How to Install Satchmo in Windows?
540,046
<p>I'm working on a Django project that's slated to be using Satchmo for its e-commerce aspects. I'd like to install it on my Windows Vista machine but some of the cPython modules it needs can't be compiled or easy_installed.</p> <p>Has anyone been able to get Satchmo working on Windows, and if so, what additional st...
0
2009-02-12T04:40:04Z
544,322
<p>Which modules are you having trouble with? Pycrypto binaries are here - <a href="http://www.voidspace.org.uk/python/modules.shtml#pycrypto" rel="nofollow">http://www.voidspace.org.uk/python/modules.shtml#pycrypto</a> Python Imaging binaries are here - <a href="http://www.pythonware.com/products/pil/" rel="nofollow"...
3
2009-02-13T01:12:15Z
[ "python", "windows", "django", "windows-vista", "satchmo" ]
Python 3.0 urllib.parse error "Type str doesn't support the buffer API"
540,342
<pre><code> File "/usr/local/lib/python3.0/cgi.py", line 477, in __init__ self.read_urlencoded() File "/usr/local/lib/python3.0/cgi.py", line 577, in read_urlencoded self.strict_parsing): File "/usr/local/lib/python3.0/urllib/parse.py", line 377, in parse_qsl pairs = [s2 for s1 in qs.split('&amp;') for...
21
2009-02-12T07:24:05Z
542,080
<p>urllib is trying to do:</p> <pre><code>b'a,b'.split(',') </code></pre> <p>Which doesn't work. byte strings and unicode strings mix even less smoothly in Py3k than they used to — deliberately, to make encoding problems go wrong sooner rather than later.</p> <p>So the error is rather opaquely telling you ‘y...
25
2009-02-12T16:14:50Z
[ "python", "cgi", "python-3.x", "urllib" ]
Python 3.0 urllib.parse error "Type str doesn't support the buffer API"
540,342
<pre><code> File "/usr/local/lib/python3.0/cgi.py", line 477, in __init__ self.read_urlencoded() File "/usr/local/lib/python3.0/cgi.py", line 577, in read_urlencoded self.strict_parsing): File "/usr/local/lib/python3.0/urllib/parse.py", line 377, in parse_qsl pairs = [s2 for s1 in qs.split('&amp;') for...
21
2009-02-12T07:24:05Z
2,159,263
<p>From the python tutorial ( <a href="http://www.python.org/doc/3.0/tutorial/stdlib.html">http://www.python.org/doc/3.0/tutorial/stdlib.html</a> ) there is an example of using urlopen method. It raises the same error.</p> <pre><code>for line in urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'): if 'EST' in ...
14
2010-01-29T01:13:39Z
[ "python", "cgi", "python-3.x", "urllib" ]
C to Python via SWIG: can't get void** parameters to hold their value
540,427
<p>I have a C interface that looks like this (simplified):</p> <pre><code>extern bool Operation(void ** ppData); extern float GetFieldValue(void* pData); extern void Cleanup(p); </code></pre> <p>which is used as follows:</p> <pre><code>void * p = NULL; float theAnswer = 0.0f; if (Operation(&amp;p)) { theAnswer = ...
7
2009-02-12T08:04:16Z
540,507
<p>Would you be willing to use ctypes? Here is sample code that should work (although it is untested):</p> <pre><code>from ctypes import * test = cdll("mydll") test.Operation.restype = c_bool test.Operation.argtypes = [POINTER(c_void_p)] test.GetFieldValue.restype = c_float test.GetFieldValue.argtypes = [c_void_p]...
4
2009-02-12T08:39:09Z
[ "python", "c", "swig", "wrapping" ]
C to Python via SWIG: can't get void** parameters to hold their value
540,427
<p>I have a C interface that looks like this (simplified):</p> <pre><code>extern bool Operation(void ** ppData); extern float GetFieldValue(void* pData); extern void Cleanup(p); </code></pre> <p>which is used as follows:</p> <pre><code>void * p = NULL; float theAnswer = 0.0f; if (Operation(&amp;p)) { theAnswer = ...
7
2009-02-12T08:04:16Z
541,566
<p>I agree with theller, you should use ctypes instead. It's always easier than thinking about typemaps.</p> <p>But, if you're dead set on using swig, what you need to do is make a typemap for <code>void**</code> that RETURNS the newly allocated <code>void*</code>:</p> <pre><code>%typemap (in,numinputs=0) void** (voi...
7
2009-02-12T14:46:30Z
[ "python", "c", "swig", "wrapping" ]
Pydev and Pylons inside virtual environment, auto completion won’t work
540,538
<p>I have Pydev installed and running without problem with Python 2.6. I installed Pylons 0.9.7 RC 4 into virtual environment, then configured new interpreter to pint into virtual environment and this one is used for pylons project. My problem is that code auto completion does not work for a classes from base library (...
3
2009-02-12T08:53:12Z
565,309
<p>perhaps <a href="http://www.nabble.com/-pydev---Users--Multiple-Interpreters-td17679628.html" rel="nofollow">this</a> or <a href="http://groups.google.com/group/pylons-discuss/browse_thread/thread/916ab8ac6c2ae746?fwc=1&amp;pli=1" rel="nofollow">this</a> would help</p> <p><em>BTW: I guess that this is the correct b...
4
2009-02-19T13:32:44Z
[ "python", "pylons", "pydev", "virtualenv" ]
Specify a sender when sending mail with Python (smtplib)
540,976
<p>I have a very simple piece of code (just for testing):</p> <pre><code>import smtplib import time server = 'smtp.myprovider.com' recipients = ['johndoe@somedomain.com'] sender = 'me@mydomain.com' message = 'Subject: [PGS]: Results\n\nBlaBlaBla' session = smtplib.SMTP(server) session.sendmail(sender,recipients,mes...
4
2009-02-12T11:52:40Z
541,003
<p><code>smtplib</code> doesn't automatically include a <code>From:</code> header, so you have to put one in yourself:</p> <pre><code>message = 'From: me@example.com\nSubject: [PGS]: Results\n\nBlaBlaBla' </code></pre> <p>(In fact, <code>smtplib</code> doesn't include <em>any</em> headers automatically, but just send...
6
2009-02-12T12:08:12Z
[ "python", "email" ]
Specify a sender when sending mail with Python (smtplib)
540,976
<p>I have a very simple piece of code (just for testing):</p> <pre><code>import smtplib import time server = 'smtp.myprovider.com' recipients = ['johndoe@somedomain.com'] sender = 'me@mydomain.com' message = 'Subject: [PGS]: Results\n\nBlaBlaBla' session = smtplib.SMTP(server) session.sendmail(sender,recipients,mes...
4
2009-02-12T11:52:40Z
541,020
<p>You can utilize the <a href="http://docs.python.org/library/email.message.html#email.message.Message">email.message.Message</a> class, and use it to generate mime headers, including <code>from:</code>, <code>to:</code> and <code>subject</code>. Send the <code>as_string()</code> result via SMTP.</p> <pre><code>&gt;&...
11
2009-02-12T12:16:10Z
[ "python", "email" ]
Specify a sender when sending mail with Python (smtplib)
540,976
<p>I have a very simple piece of code (just for testing):</p> <pre><code>import smtplib import time server = 'smtp.myprovider.com' recipients = ['johndoe@somedomain.com'] sender = 'me@mydomain.com' message = 'Subject: [PGS]: Results\n\nBlaBlaBla' session = smtplib.SMTP(server) session.sendmail(sender,recipients,mes...
4
2009-02-12T11:52:40Z
541,030
<p>The "sender" you're specifying in this case is the envelope sender that is passed onto the SMTP server.</p> <p>What your MUA (Mail User Agent - i.e. outlook/Thunderbird etc.) shows you is the "From:" header.</p> <p>Normally, if I'm using smtplib, I'd compile the headers separately:</p> <pre><code>headers = "From:...
7
2009-02-12T12:22:54Z
[ "python", "email" ]
Specify a sender when sending mail with Python (smtplib)
540,976
<p>I have a very simple piece of code (just for testing):</p> <pre><code>import smtplib import time server = 'smtp.myprovider.com' recipients = ['johndoe@somedomain.com'] sender = 'me@mydomain.com' message = 'Subject: [PGS]: Results\n\nBlaBlaBla' session = smtplib.SMTP(server) session.sendmail(sender,recipients,mes...
4
2009-02-12T11:52:40Z
26,717,984
<p><a href="http://stackoverflow.com/a/10553563/486556">See</a> this answer, it's working for me.</p> <p>example code:</p> <pre><code>#send html email from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header from email.utils import formataddr msg = MIMEMult...
5
2014-11-03T15:58:16Z
[ "python", "email" ]
Python Imaging Library and JPEGs on MacOsX
540,991
<p>I've gotten a hold of <a href="http://www.pythonware.com/products/pil/" rel="nofollow">Python Imaging Library (PIL)</a> and installed the PNG support stuff just fine. I am however having issues with the<a href="http://www.ijg.org/" rel="nofollow">JPEG Library</a>.</p> <p>The default setting for it is nothing but th...
2
2009-02-12T12:04:12Z
541,067
<p>For me, the only way to have working Python + PIL on OS X was to install both from <a href="http://www.macports.org/" rel="nofollow">ports</a>. I've never managed to get fully functional PIL under either system Python or installed manually from python.org. Maybe you could try this approach?</p>
5
2009-02-12T12:38:46Z
[ "python", "osx", "jpeg", "python-imaging-library" ]
Finding top-level xml comments using Python's ElementTree
541,100
<p>I'm parsing an xml file using Python's ElementTree, like that: </p> <pre><code>et = ElementTree(file=file("test.xml")) </code></pre> <p>test.xml starts with a few lines of xml comments. </p> <p>Is there a way to get those comments from et?</p>
3
2009-02-12T12:50:15Z
541,117
<p>For ElementTree 1.2.X there is an article on <a href="http://effbot.org/zone/element-pi.htm" rel="nofollow">Reading processing instructions and comments with ElementTree </a> (<a href="http://effbot.org/zone/element-pi.htm" rel="nofollow">http://effbot.org/zone/element-pi.htm</a>).</p> <p><hr /></p> <p><em>EDIT:</...
4
2009-02-12T12:55:05Z
[ "python", "xml", "elementtree" ]
How to lookup custom ip address field stored as integer in Django-admin?
541,115
<p>In my Django model I've created custom MyIPAddressField which is stored as integer in mysql backend. To do that I've implemented to_python, get_db_prep_value, get_iternal_type (returns PositiveIntegerField) and formfield methods (uses stock IPAddressField as form_class).</p> <p>The only problem is field lookup in c...
3
2009-02-12T12:54:42Z
558,461
<p>You could instruct the ORM to add an extra field to your SQL queries, like so:</p> <pre><code>IPAddressModel.objects.extra(select={'ip': "inet_ntoa(ip_address)"}) </code></pre> <p>This adds <code>SELECT inet_ntoa(ip_address) as ip</code> to the query and a field <code>ip</code> to your objects. You can use the new...
2
2009-02-17T19:55:13Z
[ "python", "mysql", "django" ]
Is it possible to programmatically construct a Python stack frame and start execution at an arbitrary point in the code?
541,329
<p>Is it possible to programmatically construct a stack (one or more stack frames) in CPython and start execution at an arbitrary code point? Imagine the following scenario:</p> <ol> <li><p>You have a workflow engine where workflows can be scripted in Python with some constructs (e.g. branching, waiting/joining) that...
20
2009-02-12T13:53:02Z
541,397
<p>The expat python bindings included in the normal Python distribution is constructing stack frames programtically. Be warned though, it relies on undocumented and private APIs.</p> <p><a href="http://svn.python.org/view/python/trunk/Modules/pyexpat.c?rev=64048&amp;view=auto">http://svn.python.org/view/python/trunk/M...
9
2009-02-12T14:13:19Z
[ "python", "serialization", "stack", "continuations", "python-stackless" ]
Is it possible to programmatically construct a Python stack frame and start execution at an arbitrary point in the code?
541,329
<p>Is it possible to programmatically construct a stack (one or more stack frames) in CPython and start execution at an arbitrary code point? Imagine the following scenario:</p> <ol> <li><p>You have a workflow engine where workflows can be scripted in Python with some constructs (e.g. branching, waiting/joining) that...
20
2009-02-12T13:53:02Z
541,529
<p>You could grab the existing stack frame by throwing an exception and stepping back one frame along in the traceback. The problem is there is no way provided to resume execution in the middle (frame.f_lasti) of the code block.</p> <p>“Resumable exceptions” are a really interesting language idea, although it's tr...
2
2009-02-12T14:37:18Z
[ "python", "serialization", "stack", "continuations", "python-stackless" ]
Is it possible to programmatically construct a Python stack frame and start execution at an arbitrary point in the code?
541,329
<p>Is it possible to programmatically construct a stack (one or more stack frames) in CPython and start execution at an arbitrary code point? Imagine the following scenario:</p> <ol> <li><p>You have a workflow engine where workflows can be scripted in Python with some constructs (e.g. branching, waiting/joining) that...
20
2009-02-12T13:53:02Z
542,285
<p>What you generally want are continuations, which I see is already a tag on this question.</p> <p>If you have the ability to work with all of the code in the system, you may want to try doing it this way rather than dealing with the interpreter stack internals. I'm not sure how easily this will be persisted.</p> <p...
5
2009-02-12T16:59:17Z
[ "python", "serialization", "stack", "continuations", "python-stackless" ]
Is it possible to programmatically construct a Python stack frame and start execution at an arbitrary point in the code?
541,329
<p>Is it possible to programmatically construct a stack (one or more stack frames) in CPython and start execution at an arbitrary code point? Imagine the following scenario:</p> <ol> <li><p>You have a workflow engine where workflows can be scripted in Python with some constructs (e.g. branching, waiting/joining) that...
20
2009-02-12T13:53:02Z
1,523,359
<p>I have the same type of problem to solve. I wonder what the original poster decided to do.</p> <p>stackless claims it can pickle tasklets as long as there's no associated 'encumbered' C stack (encumbered is my choice of phrasing).</p> <p>I'll probably use eventlet and figure out some way of pickling 'state', I rea...
1
2009-10-06T02:48:16Z
[ "python", "serialization", "stack", "continuations", "python-stackless" ]
Is it possible to programmatically construct a Python stack frame and start execution at an arbitrary point in the code?
541,329
<p>Is it possible to programmatically construct a stack (one or more stack frames) in CPython and start execution at an arbitrary code point? Imagine the following scenario:</p> <ol> <li><p>You have a workflow engine where workflows can be scripted in Python with some constructs (e.g. branching, waiting/joining) that...
20
2009-02-12T13:53:02Z
7,389,508
<p>With standard CPython this is complicated by the mixture of C and Python data in the stack. Rebuilding the call stack would require the C stack to be reconstructed at the same time. This really puts it in the too hard basket as it could potentially tightly couple the implementation to specific versions of CPython....
2
2011-09-12T14:31:32Z
[ "python", "serialization", "stack", "continuations", "python-stackless" ]
Is it possible to programmatically construct a Python stack frame and start execution at an arbitrary point in the code?
541,329
<p>Is it possible to programmatically construct a stack (one or more stack frames) in CPython and start execution at an arbitrary code point? Imagine the following scenario:</p> <ol> <li><p>You have a workflow engine where workflows can be scripted in Python with some constructs (e.g. branching, waiting/joining) that...
20
2009-02-12T13:53:02Z
17,405,053
<p>How about using <a href="http://pythonhosted.org/joblib/" rel="nofollow">joblib</a>?</p> <p>I'm not quite sure this is what you want but it seems to fit the idea of having a workflow of which stages can be persisted. Joblib's use case seems to be to avoid recomputation, I'm not sure if this is what you are trying t...
1
2013-07-01T12:58:26Z
[ "python", "serialization", "stack", "continuations", "python-stackless" ]
Is it possible to programmatically construct a Python stack frame and start execution at an arbitrary point in the code?
541,329
<p>Is it possible to programmatically construct a stack (one or more stack frames) in CPython and start execution at an arbitrary code point? Imagine the following scenario:</p> <ol> <li><p>You have a workflow engine where workflows can be scripted in Python with some constructs (e.g. branching, waiting/joining) that...
20
2009-02-12T13:53:02Z
21,350,914
<p>Stackless python is probably the best… if you don't mind totally going over to a different python distribution. <code>stackless</code> can serialize <strong>everything</strong> in python, plus their tasklets. If you want to stay in the standard python distribution, then I'd use <a href="https://github.com/uqfound...
2
2014-01-25T13:04:32Z
[ "python", "serialization", "stack", "continuations", "python-stackless" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
541,394
<p>Yes. Use <a href="https://docs.python.org/2/library/os.path.html#os.path.splitext"><code>os.path.splitext</code></a>:</p> <pre><code>&gt;&gt;&gt; import os &gt;&gt;&gt; filename, file_extension = os.path.splitext('/path/to/somefile.ext') &gt;&gt;&gt; filename '/path/to/somefile' &gt;&gt;&gt; file_extension '.ext' <...
970
2009-02-12T14:12:46Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
541,408
<pre><code>import os.path extension = os.path.splitext(filename)[1] </code></pre>
234
2009-02-12T14:15:07Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
7,202,805
<pre><code>import os.path extension = os.path.splitext(filename)[1][1:] </code></pre> <p>To get only text extension </p>
58
2011-08-26T09:37:47Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
7,719,562
<p>Any of the solutions above work, but on linux I have found that there is a newline at the end of the extension string which will prevent matches from succeeding. Add the <code>strip()</code> method to the end. For example: </p> <pre><code>import os.path extension = os.path.splitext(filename)[1][1:].strip() </code...
7
2011-10-10T22:48:43Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
10,078,116
<p>One option may be splitting from dot:</p> <pre><code>&gt;&gt;&gt; filename = "example.jpeg" &gt;&gt;&gt; filename.split(".")[-1] 'jpeg' </code></pre> <p>No error when file doesn't have an extension:</p> <pre><code>&gt;&gt;&gt; "filename".split(".")[-1] 'filename' </code></pre> <p>But you must be careful:</p> <p...
38
2012-04-09T18:48:16Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
14,066,130
<p>worth adding a lower in there so you don't find yourself wondering why the JPG's aren't showing up in your list.</p> <pre><code>os.path.splitext(filename)[1][1:].strip().lower() </code></pre>
21
2012-12-28T07:25:49Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
14,698,139
<p>With splitext there are problems with files with double extension (e.g. <code>file.tar.gz</code>, <code>file.tar.bz2</code>, etc..)</p> <pre><code>&gt;&gt;&gt; fileName, fileExtension = os.path.splitext('/path/to/somefile.tar.gz') &gt;&gt;&gt; fileExtension '.gz' </code></pre> <p>but should be: <code>.tar.gz</cod...
5
2013-02-05T00:19:54Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
20,898,777
<p>Another solution with right split:</p> <pre><code># to get extension only s = 'test.ext' if '.' in s: ext = s.rsplit('.', 1)[1] # or, to get file name and extension def split_filepath(s): """ get filename and extension from filepath filepath -&gt; (filename, extension) """ if not '.' in s: ...
2
2014-01-03T07:32:02Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
21,851,612
<pre><code>filename='ext.tar.gz' extension = filename[filename.rfind('.'):] </code></pre>
6
2014-02-18T10:55:57Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
22,291,843
<p>If you know the exact file extension for example file.txt then you can use</p> <blockquote> <blockquote> <blockquote> <p>print fileName[0:-4]</p> </blockquote> </blockquote> </blockquote>
-7
2014-03-10T03:57:06Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
25,454,134
<pre><code>name_only=file_name[:filename.index(".") </code></pre> <p>That will give you the file name up to the first ".", which would be the most common.</p>
-4
2014-08-22T19:19:24Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
29,396,553
<pre class="lang-py prettyprint-override"><code># try this, it works for anything, any length of extension # e.g www.google.com/downloads/file1.gz.rs -&gt; .gz.rs import os.path class LinkChecker: @staticmethod def get_link_extension(link: str)-&gt;str: if link is None or link == "": retu...
-1
2015-04-01T16:56:27Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
33,575,151
<pre><code>def NewFileName(fichier): cpt = 0 fic , *ext = fichier.split('.') ext = '.'.join(ext) while os.path.isfile(fichier): cpt += 1 fichier = '{0}-({1}).{2}'.format(fic, cpt, ext) return fichier </code></pre>
0
2015-11-06T20:24:11Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
34,376,810
<p>Surprised this wasn't mentioned yet:</p> <pre><code>import os fn = '/some/path/a.tar.gz' basename = os.path.basename(fn) # os independent Out[] a.tar.gz base = basename.split('.')[0] Out[] a ext = '.'.join(basename.split('.')[1:]) # &lt;-- main part # if you want a leading '.', and if no result `None`: ext =...
1
2015-12-20T00:24:08Z
[ "python", "filenames", "file-extension" ]
Extracting extension from filename in Python
541,390
<p>Is there a function to extract the extension from a filename?</p>
599
2009-02-12T14:11:50Z
35,188,296
<p>I'm surprised no one has mentioned <a href="https://docs.python.org/3/library/pathlib.html"><code>pathlib</code></a> yet, <code>pathlib</code> IS awesome!</p> <p><strong>NOTE:</strong> You require to have Python 3.4 at least though!</p> <pre><code>import pathlib print(pathlib.Path('yourPathGoesHere').suffix) </co...
13
2016-02-03T21:41:25Z
[ "python", "filenames", "file-extension" ]
How can one create new scopes in python
541,926
<p>In many languages (and places) there is a nice practice of creating local scopes by creating a block like <a href="http://stackoverflow.com/questions/541705/is-iftrue-a-good-idea-in-c">this</a>.</p> <pre><code>void foo() { ... Do some stuff ... if(TRUE) { char a; int b; ....
6
2009-02-12T15:53:59Z
541,948
<p>Why do you want to create new scopes in python anyway?</p> <p>The normal reason for doing it in other languages is variable scoping, but that doesn't happen in python.</p> <pre><code>if True: a = 10 print a </code></pre>
4
2009-02-12T15:57:19Z
[ "python", "scope" ]
How can one create new scopes in python
541,926
<p>In many languages (and places) there is a nice practice of creating local scopes by creating a block like <a href="http://stackoverflow.com/questions/541705/is-iftrue-a-good-idea-in-c">this</a>.</p> <pre><code>void foo() { ... Do some stuff ... if(TRUE) { char a; int b; ....
6
2009-02-12T15:53:59Z
541,958
<p>variables in list comprehension (Python 3+) and generators are local:</p> <pre><code>&gt;&gt;&gt; i = 0 &gt;&gt;&gt; [i+1 for i in range(10)] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] &gt;&gt;&gt; i 0 </code></pre> <p>but why exactly do you need this?</p>
0
2009-02-12T15:57:57Z
[ "python", "scope" ]
How can one create new scopes in python
541,926
<p>In many languages (and places) there is a nice practice of creating local scopes by creating a block like <a href="http://stackoverflow.com/questions/541705/is-iftrue-a-good-idea-in-c">this</a>.</p> <pre><code>void foo() { ... Do some stuff ... if(TRUE) { char a; int b; ....
6
2009-02-12T15:53:59Z
541,984
<p>I would see this as a clear sign that it's time to create a new function and refactor the code. I can see no reason to create a new scope like that. Any reason in mind?</p>
1
2009-02-12T16:01:19Z
[ "python", "scope" ]
How can one create new scopes in python
541,926
<p>In many languages (and places) there is a nice practice of creating local scopes by creating a block like <a href="http://stackoverflow.com/questions/541705/is-iftrue-a-good-idea-in-c">this</a>.</p> <pre><code>void foo() { ... Do some stuff ... if(TRUE) { char a; int b; ....
6
2009-02-12T15:53:59Z
541,996
<pre><code>def a(): def b(): pass b() </code></pre> <p>If I just want some extra indentation or am debugging, I'll use <code>if True:</code></p>
0
2009-02-12T16:03:19Z
[ "python", "scope" ]
How can one create new scopes in python
541,926
<p>In many languages (and places) there is a nice practice of creating local scopes by creating a block like <a href="http://stackoverflow.com/questions/541705/is-iftrue-a-good-idea-in-c">this</a>.</p> <pre><code>void foo() { ... Do some stuff ... if(TRUE) { char a; int b; ....
6
2009-02-12T15:53:59Z
542,041
<p>In Python, scoping is of three types : global, local and class. You can create specialized 'scope' dictionaries to pass to exec / eval(). In addition you can use nested scopes (defining a function within another). I found these to be sufficient in all my code.</p> <p>As Douglas Leeder said already, the main reason...
5
2009-02-12T16:10:01Z
[ "python", "scope" ]
How can one create new scopes in python
541,926
<p>In many languages (and places) there is a nice practice of creating local scopes by creating a block like <a href="http://stackoverflow.com/questions/541705/is-iftrue-a-good-idea-in-c">this</a>.</p> <pre><code>void foo() { ... Do some stuff ... if(TRUE) { char a; int b; ....
6
2009-02-12T15:53:59Z
542,048
<p>If you just want to create temp variables and let them be garbage collected right after using them, you can use</p> <pre><code>del varname </code></pre> <p>when you don't want them anymore. </p> <p>If its just for aesthetics, you could use comments or extra newlines, no extra indentation, though.</p>
1
2009-02-12T16:11:08Z
[ "python", "scope" ]
How can one create new scopes in python
541,926
<p>In many languages (and places) there is a nice practice of creating local scopes by creating a block like <a href="http://stackoverflow.com/questions/541705/is-iftrue-a-good-idea-in-c">this</a>.</p> <pre><code>void foo() { ... Do some stuff ... if(TRUE) { char a; int b; ....
6
2009-02-12T15:53:59Z
542,114
<blockquote> <p>A <em>scope</em> is a textual region of a Python program where a namespace is directly accessible. “Directly accessible” here means that an unqualified reference to a name attempts to find the name in the namespace...</p> </blockquote> <p>Please, read <a href="http://docs.python.org/t...
1
2009-02-12T16:21:43Z
[ "python", "scope" ]
How can one create new scopes in python
541,926
<p>In many languages (and places) there is a nice practice of creating local scopes by creating a block like <a href="http://stackoverflow.com/questions/541705/is-iftrue-a-good-idea-in-c">this</a>.</p> <pre><code>void foo() { ... Do some stuff ... if(TRUE) { char a; int b; ....
6
2009-02-12T15:53:59Z
542,120
<p>Python has exactly two scopes, local and global. Variables that are used in a function are in local scope no matter what indentation level they were created at. Calling a nested function will have the effect that you're looking for.</p> <pre><code>def foo(): a = 1 def bar(): b = 2 print a, b #will pr...
0
2009-02-12T16:23:30Z
[ "python", "scope" ]
How can one create new scopes in python
541,926
<p>In many languages (and places) there is a nice practice of creating local scopes by creating a block like <a href="http://stackoverflow.com/questions/541705/is-iftrue-a-good-idea-in-c">this</a>.</p> <pre><code>void foo() { ... Do some stuff ... if(TRUE) { char a; int b; ....
6
2009-02-12T15:53:59Z
37,403,179
<p>Depending on your case, you might want <a href="https://docs.python.org/2/reference/compound_stmts.html#the-with-statement" rel="nofollow"><code>with</code> statement</a>:</p> <pre><code>with Cat() as cat, Cake() as cake: cat.eat(cake) </code></pre> <p>is equivalent to</p> <pre><code># scope begin cat = Cat()...
1
2016-05-24T01:52:53Z
[ "python", "scope" ]
How to do variable assignment inside a while(expression) loop in Python?
542,212
<p>I have the variable assignment in order to return the assigned value and compare that to an empty string, directly in the while loop.</p> <p>Here is how I'm doing it in PHP:</p> <pre><code>while((name = raw_input("Name: ")) != ''): names.append(name) </code></pre> <p>What I'm trying to do is identical to this...
12
2009-02-12T16:44:14Z
542,224
<p>No, sorry. It's a FAQ, explained well here:</p> <p>In <a href="http://docs.python.org/faq/design.html#why-can-t-i-use-an-assignment-in-an-expression" rel="nofollow" title="http://docs.python.org/faq/design.html#why-can-t-i-use-an-assignment-in-an-expression">Pydocs</a>, and <a href="http://effbot.org/pyfaq/why-can-...
8
2009-02-12T16:48:34Z
[ "python", "syntax", "while-loop", "expression" ]
How to do variable assignment inside a while(expression) loop in Python?
542,212
<p>I have the variable assignment in order to return the assigned value and compare that to an empty string, directly in the while loop.</p> <p>Here is how I'm doing it in PHP:</p> <pre><code>while((name = raw_input("Name: ")) != ''): names.append(name) </code></pre> <p>What I'm trying to do is identical to this...
12
2009-02-12T16:44:14Z
542,227
<p>You can wrap <code>raw_input()</code> to turn it into a generator:</p> <pre><code>def wrapper(s): while True: result = raw_input(s) if result = '': break yield result names = wrapper('Name:') </code></pre> <p>which means we're back to square one but with more complex code. So if you ne...
12
2009-02-12T16:49:06Z
[ "python", "syntax", "while-loop", "expression" ]
How to do variable assignment inside a while(expression) loop in Python?
542,212
<p>I have the variable assignment in order to return the assigned value and compare that to an empty string, directly in the while loop.</p> <p>Here is how I'm doing it in PHP:</p> <pre><code>while((name = raw_input("Name: ")) != ''): names.append(name) </code></pre> <p>What I'm trying to do is identical to this...
12
2009-02-12T16:44:14Z
544,564
<pre><code>from functools import partial for name in iter(partial(raw_input, 'Name:'), ''): do_something_with(name) </code></pre> <p>or if you want a list:</p> <pre><code>&gt;&gt;&gt; names = list(iter(partial(raw_input, 'Name: '), '')) Name: nosklo Name: Andreas Name: Aaron Name: Phil Name: &gt;&gt;&gt; names ...
14
2009-02-13T02:58:59Z
[ "python", "syntax", "while-loop", "expression" ]
How to do variable assignment inside a while(expression) loop in Python?
542,212
<p>I have the variable assignment in order to return the assigned value and compare that to an empty string, directly in the while loop.</p> <p>Here is how I'm doing it in PHP:</p> <pre><code>while((name = raw_input("Name: ")) != ''): names.append(name) </code></pre> <p>What I'm trying to do is identical to this...
12
2009-02-12T16:44:14Z
16,575,991
<p>I'm only 7 years late, but there's another solution. It's not the best solution I can think of, but it highlights an interesting use of the StopIteration exception. You can do a similar loop for chunk reading files/sockets and handle Timeouts and whatnot nicely.</p> <pre><code>names=[] try: while True: ...
0
2013-05-15T22:23:29Z
[ "python", "syntax", "while-loop", "expression" ]
How to do variable assignment inside a while(expression) loop in Python?
542,212
<p>I have the variable assignment in order to return the assigned value and compare that to an empty string, directly in the while loop.</p> <p>Here is how I'm doing it in PHP:</p> <pre><code>while((name = raw_input("Name: ")) != ''): names.append(name) </code></pre> <p>What I'm trying to do is identical to this...
12
2009-02-12T16:44:14Z
29,604,498
<pre><code>names = [] for name in iter(lambda: raw_input("Name: "), ''): names.append(name) </code></pre>
1
2015-04-13T11:45:03Z
[ "python", "syntax", "while-loop", "expression" ]
Are there any good build frameworks written in Python?
542,289
<p>I switched from NAnt to using Python to write build automation scripts. I am curious if whether any build frameworks worth using that are similar to Make, Ant, and NAnt, but, instead, are Python-based. For example, Ruby has Rake. What about Python?</p>
14
2009-02-12T16:59:28Z
542,309
<p>Try <a href="http://www.scons.org/" rel="nofollow">SCons</a> </p> <p>Or are you looking for something just to build python projects?</p>
20
2009-02-12T17:01:59Z
[ "python", "build-process", "build-automation" ]
Are there any good build frameworks written in Python?
542,289
<p>I switched from NAnt to using Python to write build automation scripts. I am curious if whether any build frameworks worth using that are similar to Make, Ant, and NAnt, but, instead, are Python-based. For example, Ruby has Rake. What about Python?</p>
14
2009-02-12T16:59:28Z
542,394
<p>The following look good, but I haven't used them (yet):</p> <ul> <li><a href="http://www.blueskyonmars.com/projects/paver/">Paver</a></li> <li><a href="http://code.google.com/p/waf/">waf</a></li> <li><a href="http://python-doit.sourceforge.net/">doIt</a></li> </ul> <p>Paver looks especially promising.</p>
5
2009-02-12T17:19:27Z
[ "python", "build-process", "build-automation" ]
Are there any good build frameworks written in Python?
542,289
<p>I switched from NAnt to using Python to write build automation scripts. I am curious if whether any build frameworks worth using that are similar to Make, Ant, and NAnt, but, instead, are Python-based. For example, Ruby has Rake. What about Python?</p>
14
2009-02-12T16:59:28Z
545,191
<p>There is also <a href="http://www.nongnu.org/fab/" rel="nofollow">Fabric</a> but it's specially geared towards deployment rather than generic <em>building</em>.</p>
1
2009-02-13T08:37:06Z
[ "python", "build-process", "build-automation" ]
Are there any good build frameworks written in Python?
542,289
<p>I switched from NAnt to using Python to write build automation scripts. I am curious if whether any build frameworks worth using that are similar to Make, Ant, and NAnt, but, instead, are Python-based. For example, Ruby has Rake. What about Python?</p>
14
2009-02-12T16:59:28Z
3,838,805
<p>My Rapid Throughts: SCons is quite mature and oriented also to other languages (es C++) Waf is very simlar to ant/maven, so you will prefer it if you are used to ant/maven</p> <p>Paver is very pythonic oriented, and seems a good option if you do not know how to start.</p>
2
2010-10-01T11:41:00Z
[ "python", "build-process", "build-automation" ]
Are there any good build frameworks written in Python?
542,289
<p>I switched from NAnt to using Python to write build automation scripts. I am curious if whether any build frameworks worth using that are similar to Make, Ant, and NAnt, but, instead, are Python-based. For example, Ruby has Rake. What about Python?</p>
14
2009-02-12T16:59:28Z
15,874,136
<p><a href="http://wiki.python.org" rel="nofollow">The Python wiki</a> maintains a page <a href="http://wiki.python.org/moin/ConfigurationAndBuildTools" rel="nofollow">on python build and deployment tools</a>.</p>
1
2013-04-08T08:04:06Z
[ "python", "build-process", "build-automation" ]
Should I use Django's contrib applications or build my own?
542,594
<p>The Django apps come with their own features and design. If your requirements don't match 100% with the features of the contib app, you end up customizing and tweaking the app. I feel this involves more effort than just building your own app to fit your requirements.</p> <p>What do you think?</p>
3
2009-02-12T18:10:45Z
542,685
<p>It all depends. We had a need for something that was 98% similar to contrib.flatpages. We could have monkeypatched it, but we decided that the code was so straightforward that we would just copy and fork it. It worked out fine.</p> <p>Doing this with contrib.auth, on the other hand, might be a bad move given its in...
7
2009-02-12T18:34:34Z
[ "python", "django", "django-contrib" ]
Should I use Django's contrib applications or build my own?
542,594
<p>The Django apps come with their own features and design. If your requirements don't match 100% with the features of the contib app, you end up customizing and tweaking the app. I feel this involves more effort than just building your own app to fit your requirements.</p> <p>What do you think?</p>
3
2009-02-12T18:10:45Z
543,335
<p>Most of the apps in django.contrib are written very well and are highly extensible.</p> <p>Don't like quite how comments works? Subclass the models and forms within it, adding your own functionality and you have a working comment system that fits your sites schema, with little effort.</p> <p>I think the best part ...
4
2009-02-12T21:22:15Z
[ "python", "django", "django-contrib" ]
Should I use Django's contrib applications or build my own?
542,594
<p>The Django apps come with their own features and design. If your requirements don't match 100% with the features of the contib app, you end up customizing and tweaking the app. I feel this involves more effort than just building your own app to fit your requirements.</p> <p>What do you think?</p>
3
2009-02-12T18:10:45Z
549,021
<p>I'd also check out third-party re-usable apps before building my own. Many are listed on <a href="http://djangoplugables.com/" rel="nofollow">Django Plug(g)ables</a>, and most are hosted on <a href="http://code.google.com/search/?q=django#q=django" rel="nofollow">Google Code</a>, <a href="http://github.com/search?ty...
6
2009-02-14T12:10:03Z
[ "python", "django", "django-contrib" ]
Benefits of os.path.splitext over regular .split?
542,596
<p>In <a href="http://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python">this other question</a>, the votes clearly show that the <code>os.path.splitext</code> function is preferred over the simple <code>.split('.')[-1]</code> string manipulation. Does anyone have a moment to explain exact...
23
2009-02-12T18:10:51Z
542,610
<p>In the comment to the answer that provided this solution:</p> <blockquote> <p>"If the file has no extension this incorrectly returns the file name instead of an empty string."</p> </blockquote> <p>Not every file has an extension.</p>
0
2009-02-12T18:15:59Z
[ "python" ]
Benefits of os.path.splitext over regular .split?
542,596
<p>In <a href="http://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python">this other question</a>, the votes clearly show that the <code>os.path.splitext</code> function is preferred over the simple <code>.split('.')[-1]</code> string manipulation. Does anyone have a moment to explain exact...
23
2009-02-12T18:10:51Z
542,611
<p>A clearly defined and documented method to get the file extension would always be preferred over splitting a string willy nilly because that method would be more fragile for various reasons.</p> <p>Edit: This is not language specific.</p>
1
2009-02-12T18:16:00Z
[ "python" ]
Benefits of os.path.splitext over regular .split?
542,596
<p>In <a href="http://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python">this other question</a>, the votes clearly show that the <code>os.path.splitext</code> function is preferred over the simple <code>.split('.')[-1]</code> string manipulation. Does anyone have a moment to explain exact...
23
2009-02-12T18:10:51Z
542,614
<p>os.path.splitext will correctly handle the situation where the file has no extension and return an empty string. .split will return the name of the file.</p>
10
2009-02-12T18:16:40Z
[ "python" ]
Benefits of os.path.splitext over regular .split?
542,596
<p>In <a href="http://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python">this other question</a>, the votes clearly show that the <code>os.path.splitext</code> function is preferred over the simple <code>.split('.')[-1]</code> string manipulation. Does anyone have a moment to explain exact...
23
2009-02-12T18:10:51Z
542,615
<p>The first and most obvious difference is that the split call has no logic in it to default when there is no extension.</p> <p>This can also be accomplished by a regex, in order to get it to behave as a 1 liner without extra includes, but still return, empty string if the extension isn't there.</p> <p>Also, the pat...
1
2009-02-12T18:16:55Z
[ "python" ]
Benefits of os.path.splitext over regular .split?
542,596
<p>In <a href="http://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python">this other question</a>, the votes clearly show that the <code>os.path.splitext</code> function is preferred over the simple <code>.split('.')[-1]</code> string manipulation. Does anyone have a moment to explain exact...
23
2009-02-12T18:10:51Z
542,616
<p>There exist operating systems that do not use ‘.’ as an extension separator.</p> <p>(Notably, RISC OS by convention uses ‘/’, since ‘.’ is used there as a path separator.)</p>
6
2009-02-12T18:16:57Z
[ "python" ]
Benefits of os.path.splitext over regular .split?
542,596
<p>In <a href="http://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python">this other question</a>, the votes clearly show that the <code>os.path.splitext</code> function is preferred over the simple <code>.split('.')[-1]</code> string manipulation. Does anyone have a moment to explain exact...
23
2009-02-12T18:10:51Z
542,617
<ol> <li>Right tool for the right job</li> <li>Already thoroughly debugged and tested as part of the Python standard library - no bugs introduced by mistakes in your hand-rolled version (e.g. what if there is no extension, or the file is a hidden file on UNIX like '.bashrc', or there are multiple extensions?)</li> <li>...
2
2009-02-12T18:17:08Z
[ "python" ]
Benefits of os.path.splitext over regular .split?
542,596
<p>In <a href="http://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python">this other question</a>, the votes clearly show that the <code>os.path.splitext</code> function is preferred over the simple <code>.split('.')[-1]</code> string manipulation. Does anyone have a moment to explain exact...
23
2009-02-12T18:10:51Z
542,619
<p>Well, there are separate implementations for separate operating systems. This means that if the logic to extract the extension of a file differs on Mac from that on Linux, this distinction will be handled by those things. I don't know of any such distinction so there might be none.</p> <p><hr /></p> <p><strong>Edi...
28
2009-02-12T18:17:46Z
[ "python" ]
Benefits of os.path.splitext over regular .split?
542,596
<p>In <a href="http://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python">this other question</a>, the votes clearly show that the <code>os.path.splitext</code> function is preferred over the simple <code>.split('.')[-1]</code> string manipulation. Does anyone have a moment to explain exact...
23
2009-02-12T18:10:51Z
542,621
<p><code>splitext()</code> does a reverse search for '.' and returns the extension portion as soon as it finds it. <code>split('.')</code> will do a forward search for all '.' characters and is therefore almost always slower. In other words <code>splitext()</code> is specifically written for returning the extension unl...
8
2009-02-12T18:18:16Z
[ "python" ]
Benefits of os.path.splitext over regular .split?
542,596
<p>In <a href="http://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python">this other question</a>, the votes clearly show that the <code>os.path.splitext</code> function is preferred over the simple <code>.split('.')[-1]</code> string manipulation. Does anyone have a moment to explain exact...
23
2009-02-12T18:10:51Z
542,643
<p>Besides being standard and therefore guaranteed to be available, <code>os.path.splitext</code>:</p> <p><strong>Handles edge cases</strong> - like that of a missing extension.<br /> <strong>Offers guarantees</strong> - Besides correctly returning the extension if one exists, it guarantees that <code>root</code> + <c...
0
2009-02-12T18:23:06Z
[ "python" ]
Benefits of os.path.splitext over regular .split?
542,596
<p>In <a href="http://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python">this other question</a>, the votes clearly show that the <code>os.path.splitext</code> function is preferred over the simple <code>.split('.')[-1]</code> string manipulation. Does anyone have a moment to explain exact...
23
2009-02-12T18:10:51Z
545,368
<p>1) simple split('.')[-1] won't work correctly for the path as C:\foo.bar\Makefile so you need to extract basename first with os.path.basename(), and even in this case it will fail to split file without extension correctly. os.path.splitext do this under the hood.</p> <p>2) Despite the fact os.path.splitext is cross...
0
2009-02-13T09:44:25Z
[ "python" ]
Help needed to convert code from C# to Python
542,908
<p>Can you please convert this code from C# to Python to be run on IronPython?</p> <p>I don’t have any experience with Python.</p> <pre><code>using System; using Baz; namespace ConsoleApplication { class Program { static void Main() { Portal foo = new Portal("Foo"); Agent bar = new Age...
0
2009-02-12T19:42:23Z
542,939
<p>I think it would suit you best if you take a look at the following links:</p> <p><a href="http://www.learningpython.com/2006/10/02/ironpython-hello-world-tutorial/" rel="nofollow">http://www.learningpython.com/2006/10/02/ironpython-hello-world-tutorial/</a> <a href="http://msdn.microsoft.com/en-us/magazine/cc300810...
1
2009-02-12T19:51:16Z
[ "c#", ".net", "python", "ironpython" ]
Help needed to convert code from C# to Python
542,908
<p>Can you please convert this code from C# to Python to be run on IronPython?</p> <p>I don’t have any experience with Python.</p> <pre><code>using System; using Baz; namespace ConsoleApplication { class Program { static void Main() { Portal foo = new Portal("Foo"); Agent bar = new Age...
0
2009-02-12T19:42:23Z
542,942
<p>Instantiation doesn't require a type definition. Methods called the same, assign delegates directly. The previous answer is absolutely right, you'll need a <strong>lot</strong> more context in order to "convert" a C# application to Python; it's more than just syntax.</p> <pre><code>foo = Portal("Foo") bar = Agent(...
5
2009-02-12T19:52:06Z
[ "c#", ".net", "python", "ironpython" ]
Help needed to convert code from C# to Python
542,908
<p>Can you please convert this code from C# to Python to be run on IronPython?</p> <p>I don’t have any experience with Python.</p> <pre><code>using System; using Baz; namespace ConsoleApplication { class Program { static void Main() { Portal foo = new Portal("Foo"); Agent bar = new Age...
0
2009-02-12T19:42:23Z
2,958,339
<p>Or if you're feeling really lazy, there's a <a href="http://www.developerfusion.com/tools/convert/csharp-to-python/" rel="nofollow">C# to Python converter</a> on developer fusion!</p>
2
2010-06-02T13:52:47Z
[ "c#", ".net", "python", "ironpython" ]
Help needed to convert code from C# to Python
542,908
<p>Can you please convert this code from C# to Python to be run on IronPython?</p> <p>I don’t have any experience with Python.</p> <pre><code>using System; using Baz; namespace ConsoleApplication { class Program { static void Main() { Portal foo = new Portal("Foo"); Agent bar = new Age...
0
2009-02-12T19:42:23Z
4,208,936
<p>In case someone else has this question SharpDevelop has a conversion utility to convert between C# and IronPython, VB.NET or Boo <a href="http://community.sharpdevelop.net/blogs/mattward/archive/2009/05/11/ConvertingCSharpVBNetCodeToIronPython.aspx" rel="nofollow">http://community.sharpdevelop.net/blogs/mattward/arc...
0
2010-11-17T20:40:08Z
[ "c#", ".net", "python", "ironpython" ]
Highlighting unmatched brackets in vim
542,929
<p>I'm getting burned repeatedly by unmatched parentheses while writing python code in vim. I like how they're handled for C code - vim highlights in red all of the curly braces following the unmatched paren. I looked at the <code>c.vim</code> syntax file briefly to try to understand it, but the section that handles ...
11
2009-02-12T19:47:23Z
543,521
<p>Stop gap solution:</p> <pre><code>:imap ( ()&lt;C-[&gt;i </code></pre> <p>This will make it so every time you type a left paren it will automatically put in the right and put you in the position of typing in between.</p>
3
2009-02-12T21:52:21Z
[ "python", "vim", "syntax-highlighting" ]
Highlighting unmatched brackets in vim
542,929
<p>I'm getting burned repeatedly by unmatched parentheses while writing python code in vim. I like how they're handled for C code - vim highlights in red all of the curly braces following the unmatched paren. I looked at the <code>c.vim</code> syntax file briefly to try to understand it, but the section that handles ...
11
2009-02-12T19:47:23Z
543,554
<p>You can get vim to do the opposite: do a </p> <blockquote> <p>:set showmatch</p> </blockquote> <p>and it will highlight matching parens. You'll know when you're unbalanced when it doesn't highlight something.</p> <p>I'm also assuming you're familiar with the '%' command, which bounces you to the matching elemen...
8
2009-02-12T21:55:23Z
[ "python", "vim", "syntax-highlighting" ]
Highlighting unmatched brackets in vim
542,929
<p>I'm getting burned repeatedly by unmatched parentheses while writing python code in vim. I like how they're handled for C code - vim highlights in red all of the curly braces following the unmatched paren. I looked at the <code>c.vim</code> syntax file briefly to try to understand it, but the section that handles ...
11
2009-02-12T19:47:23Z
543,881
<p>Not sure if it'll be more or less confusing for you, but you could look at the <code>lisp.vim</code> syntax file (especially the part where <code>g:lisp_rainbow</code> is handled) to see how you can highlight matching parens.</p> <p>If you manage to highlight all the matching parens, you could have the leftover par...
2
2009-02-12T22:54:55Z
[ "python", "vim", "syntax-highlighting" ]
Highlighting unmatched brackets in vim
542,929
<p>I'm getting burned repeatedly by unmatched parentheses while writing python code in vim. I like how they're handled for C code - vim highlights in red all of the curly braces following the unmatched paren. I looked at the <code>c.vim</code> syntax file briefly to try to understand it, but the section that handles ...
11
2009-02-12T19:47:23Z
566,124
<p>As a workaround, I found <a href="http://www.vim.org/scripts/script.php?script_id=974" rel="nofollow">this indent script</a> on the vim website that supposedly does a better job of indenting Python code. When you end a line with unbalanced parens, it indents the next line to line up with the open paren.</p>
0
2009-02-19T16:33:47Z
[ "python", "vim", "syntax-highlighting" ]
Highlighting unmatched brackets in vim
542,929
<p>I'm getting burned repeatedly by unmatched parentheses while writing python code in vim. I like how they're handled for C code - vim highlights in red all of the curly braces following the unmatched paren. I looked at the <code>c.vim</code> syntax file briefly to try to understand it, but the section that handles ...
11
2009-02-12T19:47:23Z
602,453
<p>If I understand correctly and you are trying to look at non-matching parenthesis in C code (that was generated in python), I would recommend you install rainbow.vim from <a href="http://www.drchip.org/astronaut/vim/#RAINBOW" rel="nofollow">Dr Chip's Site</a>. This will highlight braces in different colours dependin...
5
2009-03-02T14:07:48Z
[ "python", "vim", "syntax-highlighting" ]
Highlighting unmatched brackets in vim
542,929
<p>I'm getting burned repeatedly by unmatched parentheses while writing python code in vim. I like how they're handled for C code - vim highlights in red all of the curly braces following the unmatched paren. I looked at the <code>c.vim</code> syntax file briefly to try to understand it, but the section that handles ...
11
2009-02-12T19:47:23Z
602,601
<p>Have you tried using <a href="http://www.vim.org/scripts/script.php?script%5Fid=39" rel="nofollow" title="matchit.vim">matchit.vim</a>? It supports all sorts of matches, and it <em>should</em> work in Python.</p>
1
2009-03-02T14:51:30Z
[ "python", "vim", "syntax-highlighting" ]
Highlighting unmatched brackets in vim
542,929
<p>I'm getting burned repeatedly by unmatched parentheses while writing python code in vim. I like how they're handled for C code - vim highlights in red all of the curly braces following the unmatched paren. I looked at the <code>c.vim</code> syntax file briefly to try to understand it, but the section that handles ...
11
2009-02-12T19:47:23Z
37,886,090
<p>The plugin <a href="https://github.com/arnar/vim-matchopen" rel="nofollow">vim-matchopen</a> does what you are looking for</p> <p><img src="http://i.imgur.com/wmSvzRm.png" alt=""><br> The highlight color changes based on your colorscheme</p>
0
2016-06-17T16:15:19Z
[ "python", "vim", "syntax-highlighting" ]
Invoking built-in operators indirectly in Python
542,987
<p>Let's say you have a small calculator program that takes numbers and an operator to perform on those numbers as input, then prints out the result of applying the specified operation. So if you input "4 + 5" it will print out "9". Simple, right? Well what I want to be able to write is something this:</p> <pre><code>...
2
2009-02-12T20:01:23Z
542,996
<p>If you really wanted to do this, you would need the standard <code>operator</code> module. See also <a href="http://docs.python.org/reference/datamodel.html#object.__add__" rel="nofollow">Emulating numeric types</a>. And, yes, a dictionary full of functions would be a perfectly sound dynamic way to make this happen....
2
2009-02-12T20:03:17Z
[ "python" ]
Invoking built-in operators indirectly in Python
542,987
<p>Let's say you have a small calculator program that takes numbers and an operator to perform on those numbers as input, then prints out the result of applying the specified operation. So if you input "4 + 5" it will print out "9". Simple, right? Well what I want to be able to write is something this:</p> <pre><code>...
2
2009-02-12T20:01:23Z
543,029
<p>Basically no, you will at least need to have a dictionary or function to map operator characters to their implementations. It's actually a little more complicated than that, since not all operators take the form <code>a [op] b</code>, so in general you'd need to do a bit of parsing; see <a href="https://docs.python....
6
2009-02-12T20:10:19Z
[ "python" ]
Invoking built-in operators indirectly in Python
542,987
<p>Let's say you have a small calculator program that takes numbers and an operator to perform on those numbers as input, then prints out the result of applying the specified operation. So if you input "4 + 5" it will print out "9". Simple, right? Well what I want to be able to write is something this:</p> <pre><code>...
2
2009-02-12T20:01:23Z
543,146
<p>Warning: this is not pythonic at all!! (goes against every rule of the <a href="http://www.python.org/dev/peps/pep-0020/" rel="nofollow">Zen of Python</a>!)</p> <p>Here's a <em>magical</em>, one-liner dictionary:</p> <pre><code>ops = eval( '{%s}'%','.join([('\''+op + '\' : lambda a,b: a ' + op + ' b') for op in '+...
2
2009-02-12T20:42:14Z
[ "python" ]
two questions (RFC822, login info) about sending email via python
543,096
<h1>1 -</h1> <p>In my email-sending script, I store spaced-out emails in a string, then I use ", ".join(to.split()). However, it looks like the script only sends to the 1st email - is it something to do with RFC822 format? If so, how can I fix this? </p> <h1>2 -</h1> <p>I feel a bit edgy having my password visable i...
-2
2009-02-12T20:31:56Z
545,517
<p>Use <code>', '.join()</code> for the list in the <code>To:</code> or <code>Cc:</code> header, but the headers are only for show. What determines where the mail actually goes is the RCPT envelope. Assuming you're using smtplib, that's the second argument:</p> <pre><code>connection.sendmail(senderaddress, to.split(),...
3
2009-02-13T10:40:48Z
[ "python", "smtp", "passwords", "rfc822" ]
two questions (RFC822, login info) about sending email via python
543,096
<h1>1 -</h1> <p>In my email-sending script, I store spaced-out emails in a string, then I use ", ".join(to.split()). However, it looks like the script only sends to the 1st email - is it something to do with RFC822 format? If so, how can I fix this? </p> <h1>2 -</h1> <p>I feel a bit edgy having my password visable i...
-2
2009-02-12T20:31:56Z
546,364
<p>For the second part of your question, you could take a look at the netrc module (<a href="http://docs.python.org/library/netrc.html" rel="nofollow">http://docs.python.org/library/netrc.html</a>).</p> <p>This isn't much better than having the password in the script, but it does allow the script to be readable for an...
2
2009-02-13T15:30:45Z
[ "python", "smtp", "passwords", "rfc822" ]
Granularity of Paradigm Mixing
543,140
<p>When using a multi-paradigm language such as Python, C++, D, or Ruby, how much do you mix paradigms within a single application? Within a single module? Do you believe that mixing the functional, procedural and OO paradigms at a fine granularity leads to clearer, more concise code because you're using the right to...
2
2009-02-12T20:41:14Z
543,166
<p>Different paradigms mix in different ways. For example, Using OOP doesn't eliminate the use of subroutines and procedural code from an outside library. It merely moves the procedures around into a different place.</p> <p>It is impossible to purely program with one paradigm. You may think you have a single one in...
2
2009-02-12T20:47:40Z
[ "python", "ruby", "coding-style" ]
Granularity of Paradigm Mixing
543,140
<p>When using a multi-paradigm language such as Python, C++, D, or Ruby, how much do you mix paradigms within a single application? Within a single module? Do you believe that mixing the functional, procedural and OO paradigms at a fine granularity leads to clearer, more concise code because you're using the right to...
2
2009-02-12T20:41:14Z
579,842
<p>I am not sure that I ever think about it like this. </p> <p>Once you start "thinking in Ruby" the multi-paradigms just merge into ... well, Ruby. </p> <p>Ruby is object-oriented, but I find that other things such as the functional aspect tend to mean that some of the "traditional" design patters present in OO lang...
2
2009-02-23T23:23:48Z
[ "python", "ruby", "coding-style" ]
Granularity of Paradigm Mixing
543,140
<p>When using a multi-paradigm language such as Python, C++, D, or Ruby, how much do you mix paradigms within a single application? Within a single module? Do you believe that mixing the functional, procedural and OO paradigms at a fine granularity leads to clearer, more concise code because you're using the right to...
2
2009-02-12T20:41:14Z
583,881
<p>Mixing paradigms has an advantage of letting you express solutions in most natural and esy way. Which is very good thing when it help keeping your program logic smaller. For example, filtering a list by some criteria is several times simpler to express with functional solution compared to traditional loop.</p> <p...
1
2009-02-24T22:21:02Z
[ "python", "ruby", "coding-style" ]
Granularity of Paradigm Mixing
543,140
<p>When using a multi-paradigm language such as Python, C++, D, or Ruby, how much do you mix paradigms within a single application? Within a single module? Do you believe that mixing the functional, procedural and OO paradigms at a fine granularity leads to clearer, more concise code because you're using the right to...
2
2009-02-12T20:41:14Z
622,514
<p>Different problems require different solutions, but it helps if you solve things the same way in the same layer. And varying to wildly will just confuse you and everyone else in the project.</p> <p>For C++, I've found that statically typed OOP (use zope.interface in Python) work well for higher-level parts (connect...
0
2009-03-07T21:10:58Z
[ "python", "ruby", "coding-style" ]
How do I attach a remote debugger to a Python process?
543,196
<p>I'm tired of inserting</p> <pre><code>import pdb; pdb.set_trace() </code></pre> <p>lines into my Python programs and debugging through the console. How do I connect a remote debugger and insert breakpoints from a civilized user interface?</p>
47
2009-02-12T20:54:08Z
543,258
<p>Well, you can get something quite similar to that using a twisted manhole, which works like this:</p> <pre><code>from twisted.internet import reactor from twisted.cred import portal, checkers from twisted.conch import manhole, manhole_ssh def getManholeFactory(namespace): realm = manhole_ssh.TerminalRealm() ...
16
2009-02-12T21:07:19Z
[ "python", "remote-debugging" ]
How do I attach a remote debugger to a Python process?
543,196
<p>I'm tired of inserting</p> <pre><code>import pdb; pdb.set_trace() </code></pre> <p>lines into my Python programs and debugging through the console. How do I connect a remote debugger and insert breakpoints from a civilized user interface?</p>
47
2009-02-12T20:54:08Z
544,838
<p>use <a href="http://winpdb.org/">Winpdb</a>. It is a <strong>platform independent</strong> graphical GPL Python debugger with support for remote debugging over a network, multiple threads, namespace modification, embedded debugging, encrypted communication and is up to 20 times faster than pdb.</p> <p>Features:</p>...
56
2009-02-13T05:32:18Z
[ "python", "remote-debugging" ]
How do I attach a remote debugger to a Python process?
543,196
<p>I'm tired of inserting</p> <pre><code>import pdb; pdb.set_trace() </code></pre> <p>lines into my Python programs and debugging through the console. How do I connect a remote debugger and insert breakpoints from a civilized user interface?</p>
47
2009-02-12T20:54:08Z
28,813,615
<p>Two solutions from modern IDEs:</p> <ol> <li><p>PTVS cross-platform remote debugging</p></li> <li><p>PyCharm/PyDev remote debugging</p></li> </ol>
1
2015-03-02T15:37:29Z
[ "python", "remote-debugging" ]
How do I attach a remote debugger to a Python process?
543,196
<p>I'm tired of inserting</p> <pre><code>import pdb; pdb.set_trace() </code></pre> <p>lines into my Python programs and debugging through the console. How do I connect a remote debugger and insert breakpoints from a civilized user interface?</p>
47
2009-02-12T20:54:08Z
29,451,814
<p>A little bit late, but here is a very lightweight remote debugging solution courtesy of <a href="http://michaeldehaan.net/post/35403909347/tips-on-using-debuggers-with-ansible">http://michaeldehaan.net/post/35403909347/tips-on-using-debuggers-with-ansible</a>:</p> <ol> <li><code>pip install epdb</code> on the remot...
5
2015-04-04T21:55:19Z
[ "python", "remote-debugging" ]
Add Quotes in url string from file
543,199
<p>I need script to add quotes in url string from url.txt</p> <p>from <code>http://www.site.com/info.xx</code> to <code>"http://www.site.com/info.xx"</code></p>
2
2009-02-12T20:54:42Z
543,210
<pre><code>url = '"%s"' % url </code></pre> <p>Example:</p> <pre><code>line = 'http://www.site.com/info.xx \n' url = '"%s"' % line.strip() print url # "http://www.site.com/info.xx" </code></pre> <p>Remember, adding a backslash before a quotation mark will escape it and therefore won't end the string.</p>
7
2009-02-12T20:57:35Z
[ "python", "ruby", "perl" ]
Add Quotes in url string from file
543,199
<p>I need script to add quotes in url string from url.txt</p> <p>from <code>http://www.site.com/info.xx</code> to <code>"http://www.site.com/info.xx"</code></p>
2
2009-02-12T20:54:42Z
543,218
<p>write one...<br /> perl is my favourite scripting language... it appears you may prefer Python.</p> <p>just read in the file and add \" before and after it..<br /> this is pretty easy in perl.</p> <p>this seems more like a request than a question... should this be on stackoverflow?</p>
0
2009-02-12T20:59:12Z
[ "python", "ruby", "perl" ]
Add Quotes in url string from file
543,199
<p>I need script to add quotes in url string from url.txt</p> <p>from <code>http://www.site.com/info.xx</code> to <code>"http://www.site.com/info.xx"</code></p>
2
2009-02-12T20:54:42Z
543,229
<pre><code>url = '"%s"' % url </code></pre> <p>Example:</p> <pre><code>&gt;&gt;&gt; url = "http://www.site.com/info.xx" &gt;&gt;&gt; print url http://www.site.com/info.xx &gt;&gt;&gt; url = '"%s"' % url &gt;&gt;&gt; print url "http://www.site.com/info.xx" &gt;&gt;&gt; </code></pre> <p>Parsing it from a file:</p> <p...
4
2009-02-12T21:00:20Z
[ "python", "ruby", "perl" ]