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 |
|---|---|---|---|---|---|---|---|---|---|
How do I parse a listing of files to get just the filenames in python? | 237,699 | <p>So lets say I'm using Python's <a href="http://www.python.org/doc/2.5.2/lib/module-ftplib.html" rel="nofollow">ftplib</a> to retrieve a list of log files from an FTP server. How would I parse that list of files to get just the file names (the last column) inside a list? See the link above for example output.</p>
| 2 | 2008-10-26T07:42:17Z | 237,709 | <p>Since every filename in the output starts at the same column, all you have to do is get the position of the dot on the first line:</p>
<blockquote>
<p><code>drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 .</code></p>
</blockquote>
<p>Then slice the filename out of the other lines using the position of tha... | 1 | 2008-10-26T08:00:33Z | [
"python",
"parsing",
"scripting"
] |
How do I parse a listing of files to get just the filenames in python? | 237,699 | <p>So lets say I'm using Python's <a href="http://www.python.org/doc/2.5.2/lib/module-ftplib.html" rel="nofollow">ftplib</a> to retrieve a list of log files from an FTP server. How would I parse that list of files to get just the file names (the last column) inside a list? See the link above for example output.</p>
| 2 | 2008-10-26T07:42:17Z | 237,721 | <p>Is there any reason why <strong>ftplib.FTP.nlst()</strong> won't work for you? I just checked and it returns only names of the files in a given directory.</p>
| 1 | 2008-10-26T08:15:24Z | [
"python",
"parsing",
"scripting"
] |
How do I parse a listing of files to get just the filenames in python? | 237,699 | <p>So lets say I'm using Python's <a href="http://www.python.org/doc/2.5.2/lib/module-ftplib.html" rel="nofollow">ftplib</a> to retrieve a list of log files from an FTP server. How would I parse that list of files to get just the file names (the last column) inside a list? See the link above for example output.</p>
| 2 | 2008-10-26T07:42:17Z | 237,769 | <h2>This best answer</h2>
<p>You may want to use ftp.nlst() instead of ftp.retrlines(). It will give you exactly what you want.</p>
<p>If you cant, read the following :</p>
<h2>Generators for sysadmin processes</h2>
<p>In his now famous review, <a href="http://www.dabeaz.com/generators/Generators.pdf" rel="nofollow... | 3 | 2008-10-26T09:09:11Z | [
"python",
"parsing",
"scripting"
] |
How do I parse a listing of files to get just the filenames in python? | 237,699 | <p>So lets say I'm using Python's <a href="http://www.python.org/doc/2.5.2/lib/module-ftplib.html" rel="nofollow">ftplib</a> to retrieve a list of log files from an FTP server. How would I parse that list of files to get just the file names (the last column) inside a list? See the link above for example output.</p>
| 2 | 2008-10-26T07:42:17Z | 3,114,591 | <p>If the FTP server supports the <code>MLSD</code> command, then please see section âsingle directory caseâ from <a href="http://stackoverflow.com/questions/2867217/how-to-delete-files-with-a-python-script-from-a-ftp-server-which-are-older-than-7/3114477#3114477">that</a> answer.</p>
<p>Use an instance (say <code... | 1 | 2010-06-24T23:17:14Z | [
"python",
"parsing",
"scripting"
] |
Daylight savings time change affecting the outcome of saving and loading an icalendar file? | 237,731 | <p>I have some unit tests that started failing today after a switch in daylight savings time.</p>
<p>We're using the <a href="http://codespeak.net/icalendar/" rel="nofollow">iCalendar python module</a> to load and save ics files.</p>
<p>The following script is a simplified version of our test. The script works fine ... | 1 | 2008-10-26T08:26:31Z | 237,785 | <p>Without looking at your code (and the quoted test-run-script my brain fails to understand right now)
I notice that you try to get a time that is in a different timezone than the one you are at.
(Think of DST as a another TIMEZONE instead of +-1 hour from current timezone).
This could (depending on how you do it) le... | 1 | 2008-10-26T09:31:08Z | [
"python",
"unit-testing",
"icalendar"
] |
Formatting dict.items() for wxPython | 237,859 | <p>I have a text box in wxPython that takes the output of dictionary.items() and displays it to the user as items are added to the dictionary. However, the raw data is very ugly, looking like </p>
<pre><code>[(u'BC',45)
(u'CHM',25)
(u'CPM',30)]
</code></pre>
<p>I know dictionary.items() is a list of tuples, but I can... | 2 | 2008-10-26T10:50:34Z | 237,872 | <p>Maybe the <a href="http://www.python.org/doc/2.5.2/lib/module-pprint.html" rel="nofollow">pretty print</a> module will help:</p>
<pre><code>>>> import pprint
>>> pprint.pformat({ "my key": "my value"})
"{'my key': 'my value'}"
>>>
</code></pre>
| 0 | 2008-10-26T11:02:06Z | [
"python",
"dictionary",
"wxpython"
] |
Formatting dict.items() for wxPython | 237,859 | <p>I have a text box in wxPython that takes the output of dictionary.items() and displays it to the user as items are added to the dictionary. However, the raw data is very ugly, looking like </p>
<pre><code>[(u'BC',45)
(u'CHM',25)
(u'CPM',30)]
</code></pre>
<p>I know dictionary.items() is a list of tuples, but I can... | 2 | 2008-10-26T10:50:34Z | 237,937 | <pre><code>text_for_display = '\n'.join(item + u' ' + unicode(value) for item, value in my_dictionary.items())
</code></pre>
| 0 | 2008-10-26T12:10:39Z | [
"python",
"dictionary",
"wxpython"
] |
Formatting dict.items() for wxPython | 237,859 | <p>I have a text box in wxPython that takes the output of dictionary.items() and displays it to the user as items are added to the dictionary. However, the raw data is very ugly, looking like </p>
<pre><code>[(u'BC',45)
(u'CHM',25)
(u'CPM',30)]
</code></pre>
<p>I know dictionary.items() is a list of tuples, but I can... | 2 | 2008-10-26T10:50:34Z | 237,941 | <p>use % formatting (known in C as sprintf), e.g:</p>
<pre><code>"%10s - %d" % dict.items()[0]
</code></pre>
<p>Number of <em>% conversion specifications</em> in the format string should match tuple length, in the dict.items() case, 2. The result of the string formatting operator is a string, so that using it as an a... | 0 | 2008-10-26T12:17:42Z | [
"python",
"dictionary",
"wxpython"
] |
Formatting dict.items() for wxPython | 237,859 | <p>I have a text box in wxPython that takes the output of dictionary.items() and displays it to the user as items are added to the dictionary. However, the raw data is very ugly, looking like </p>
<pre><code>[(u'BC',45)
(u'CHM',25)
(u'CPM',30)]
</code></pre>
<p>I know dictionary.items() is a list of tuples, but I can... | 2 | 2008-10-26T10:50:34Z | 237,998 | <p>That data seems much better displayed as a Table/Grid.</p>
| 0 | 2008-10-26T13:16:30Z | [
"python",
"dictionary",
"wxpython"
] |
Formatting dict.items() for wxPython | 237,859 | <p>I have a text box in wxPython that takes the output of dictionary.items() and displays it to the user as items are added to the dictionary. However, the raw data is very ugly, looking like </p>
<pre><code>[(u'BC',45)
(u'CHM',25)
(u'CPM',30)]
</code></pre>
<p>I know dictionary.items() is a list of tuples, but I can... | 2 | 2008-10-26T10:50:34Z | 238,453 | <p>There is no built-in dictionary method that would return your desired result.</p>
<p>You can, however, achieve your goal by creating a helper function that will format the dictionary, e.g.:</p>
<pre><code>def getNiceDictRepr(aDict):
return '\n'.join('%s %s' % t for t in aDict.iteritems())
</code></pre>
<p>Thi... | 4 | 2008-10-26T18:59:14Z | [
"python",
"dictionary",
"wxpython"
] |
Formatting dict.items() for wxPython | 237,859 | <p>I have a text box in wxPython that takes the output of dictionary.items() and displays it to the user as items are added to the dictionary. However, the raw data is very ugly, looking like </p>
<pre><code>[(u'BC',45)
(u'CHM',25)
(u'CPM',30)]
</code></pre>
<p>I know dictionary.items() is a list of tuples, but I can... | 2 | 2008-10-26T10:50:34Z | 297,811 | <p>I figured out a "better" way of formatting the output. As usual, I was trying to nuke it out when a more elegant method will do.</p>
<pre><code>for key, value in sorted(self.dict.items()):
self.current_list.WriteText(key + " " + str(self.dict[key]) + "\n")
</code></pre>
<p>This way also sorts the dictionary al... | 0 | 2008-11-18T03:57:39Z | [
"python",
"dictionary",
"wxpython"
] |
Refactoring "to hit" values for a game | 237,876 | <p>I'm making a game and one of the methods calculates a character's base hit numbers based on skill values. The method currently calculates each value individually, since each skill can be used at short, medium, and long range.</p>
<p>I originally thought I could combine the skills into a tuple and iterate over it, d... | 1 | 2008-10-26T11:08:32Z | 237,901 | <p>@Vinko: perhaps make calcBaseHitNumbers, do the "if not self.calculatedBase:" check internally, and just no-op if it's been done before. That said, I can't see the pressing need for precalculating this information. But I'm no Python performance expert.</p>
| 0 | 2008-10-26T11:35:07Z | [
"python",
"refactoring"
] |
Refactoring "to hit" values for a game | 237,876 | <p>I'm making a game and one of the methods calculates a character's base hit numbers based on skill values. The method currently calculates each value individually, since each skill can be used at short, medium, and long range.</p>
<p>I originally thought I could combine the skills into a tuple and iterate over it, d... | 1 | 2008-10-26T11:08:32Z | 237,905 | <p>Lets see if I understand you scenario: each weapon has its own distinct hit point so a rifle may have 1, a heavy weapon may have 2 etc. Then each character has a short, medium and long value to be multiplied by the hit point of the weapon.</p>
<p>You should consider using a Strategy design. That is create a weapon... | 1 | 2008-10-26T11:39:29Z | [
"python",
"refactoring"
] |
Refactoring "to hit" values for a game | 237,876 | <p>I'm making a game and one of the methods calculates a character's base hit numbers based on skill values. The method currently calculates each value individually, since each skill can be used at short, medium, and long range.</p>
<p>I originally thought I could combine the skills into a tuple and iterate over it, d... | 1 | 2008-10-26T11:08:32Z | 237,906 | <p>What sense of dynamic do you mean? What is likely to vary - the number of skills, or the weighting factors, the number of ranges (short, med, long) or all of these?</p>
<p>What happens to the (e.g.) bhPST_* values afterwards - do they get combined into one number?</p>
<p>One thing that leaps out is that the list o... | 0 | 2008-10-26T11:39:38Z | [
"python",
"refactoring"
] |
Refactoring "to hit" values for a game | 237,876 | <p>I'm making a game and one of the methods calculates a character's base hit numbers based on skill values. The method currently calculates each value individually, since each skill can be used at short, medium, and long range.</p>
<p>I originally thought I could combine the skills into a tuple and iterate over it, d... | 1 | 2008-10-26T11:08:32Z | 237,907 | <p>It feels like what you really want is a class representing the weapon, with attributes to handle the base values and calculate hit values with various modifiers. Here's a simple example:</p>
<pre><code>SHORT_RANGE = 'S'
MEDIUM_RANGE = 'M'
LONG_RANGE = 'L'
SHORT_RANGE_MODIFIER = 0.6
MEDIUM_RANGE_MODIFIER = 0.3
LONG_... | 6 | 2008-10-26T11:42:26Z | [
"python",
"refactoring"
] |
Refactoring "to hit" values for a game | 237,876 | <p>I'm making a game and one of the methods calculates a character's base hit numbers based on skill values. The method currently calculates each value individually, since each skill can be used at short, medium, and long range.</p>
<p>I originally thought I could combine the skills into a tuple and iterate over it, d... | 1 | 2008-10-26T11:08:32Z | 239,131 | <p>I would have a class for the character's attributes (so you don't have heaps of things in the character class) and a class for a weapon's attributes:</p>
<pre><code>class WeaponAttribute(object):
short_mod = 0.6
med_mod = 0.3
long_mod = 0.1
def __init__(self, base):
self.base = base
@... | 0 | 2008-10-27T05:14:52Z | [
"python",
"refactoring"
] |
How do you log server errors on django sites | 238,081 | <p>So, when playing with the development I can just set <code>settings.DEBUG</code> to <code>True</code> and if an error occures I can see it nicely formatted, with good stack trace and request information.</p>
<p>But on kind of production site I'd rather use <code>DEBUG=False</code> and show visitors some standard er... | 139 | 2008-10-26T14:37:42Z | 238,111 | <p>Well, when <code>DEBUG = False</code>, Django will automatically mail a full traceback of any error to each person listed in the <code>ADMINS</code> setting, which gets you notifications pretty much for free. If you'd like more fine-grained control, you can write and add to your settings a middleware class which def... | 74 | 2008-10-26T14:53:31Z | [
"python",
"django",
"error-logging"
] |
How do you log server errors on django sites | 238,081 | <p>So, when playing with the development I can just set <code>settings.DEBUG</code> to <code>True</code> and if an error occures I can see it nicely formatted, with good stack trace and request information.</p>
<p>But on kind of production site I'd rather use <code>DEBUG=False</code> and show visitors some standard er... | 139 | 2008-10-26T14:37:42Z | 239,882 | <p>Obviously James is correct, but if you wanted to log exceptions in a datastore there are a few opensource solutions already available:</p>
<p>1) CrashLog is a good choice: <a href="http://code.google.com/p/django-crashlog/">http://code.google.com/p/django-crashlog/</a></p>
<p>2) Db-Log is a good choice as well: <a... | 27 | 2008-10-27T13:33:44Z | [
"python",
"django",
"error-logging"
] |
How do you log server errors on django sites | 238,081 | <p>So, when playing with the development I can just set <code>settings.DEBUG</code> to <code>True</code> and if an error occures I can see it nicely formatted, with good stack trace and request information.</p>
<p>But on kind of production site I'd rather use <code>DEBUG=False</code> and show visitors some standard er... | 139 | 2008-10-26T14:37:42Z | 4,198,664 | <p>django-db-log, mentioned in another answer, has been replaced with:</p>
<p><a href="https://github.com/dcramer/django-sentry">https://github.com/dcramer/django-sentry</a></p>
| 39 | 2010-11-16T20:34:56Z | [
"python",
"django",
"error-logging"
] |
How do you log server errors on django sites | 238,081 | <p>So, when playing with the development I can just set <code>settings.DEBUG</code> to <code>True</code> and if an error occures I can see it nicely formatted, with good stack trace and request information.</p>
<p>But on kind of production site I'd rather use <code>DEBUG=False</code> and show visitors some standard er... | 139 | 2008-10-26T14:37:42Z | 6,395,837 | <p>Django Sentry is a good way to go, as already mentioned, but there is a bit of work involved in setting it up properly (as a separate website). If you just want to log everything to a simple text file here's the logging configuration to put in your <code>settings.py</code></p>
<pre><code>LOGGING = {
'version': ... | 64 | 2011-06-18T11:58:01Z | [
"python",
"django",
"error-logging"
] |
How do you log server errors on django sites | 238,081 | <p>So, when playing with the development I can just set <code>settings.DEBUG</code> to <code>True</code> and if an error occures I can see it nicely formatted, with good stack trace and request information.</p>
<p>But on kind of production site I'd rather use <code>DEBUG=False</code> and show visitors some standard er... | 139 | 2008-10-26T14:37:42Z | 19,267,228 | <p>Some time has passed since EMP's most helpful code submission. I just now implemented it, and while thrashing around with some manage.py option, to try to chase down a bug, I got a deprecation warning to the effect that with my current version of Django (1.5.?) a require_debug_false filter is now needed for the mail... | 10 | 2013-10-09T08:51:51Z | [
"python",
"django",
"error-logging"
] |
How do you log server errors on django sites | 238,081 | <p>So, when playing with the development I can just set <code>settings.DEBUG</code> to <code>True</code> and if an error occures I can see it nicely formatted, with good stack trace and request information.</p>
<p>But on kind of production site I'd rather use <code>DEBUG=False</code> and show visitors some standard er... | 139 | 2008-10-26T14:37:42Z | 39,106,487 | <p>I just had an annoying problem with my <code>fcgi</code> script. It occurred before django even started. The lack of logging is sooo painful. Anyway, redirecting stderr to a file as the very first thing helped a lot:</p>
<pre><code>#!/home/user/env/bin/python
sys.stderr = open('/home/user/fcgi_errors', 'a')
</code>... | 1 | 2016-08-23T16:33:50Z | [
"python",
"django",
"error-logging"
] |
Pattern matching of lists in Python | 238,102 | <p>I want to do some pattern matching on lists in Python. For example, in Haskell, I can do something like the following:</p>
<pre><code>fun (head : rest) = ...
</code></pre>
<p>So when I pass in a list, <code>head</code> will be the first element, and <code>rest</code> will be the trailing elements.</p>
<p>Likewise... | 31 | 2008-10-26T14:50:40Z | 238,123 | <p>That's a very much a 'pure functional' approach and as such is a sensible idiom in Haskell but it's probably not so appropriate to Python. Python only has a very limited concept of <a href="http://www.haskell.org/tutorial/patterns.html" rel="nofollow">patterns</a> in this way - and I suspect you might need a somewh... | 4 | 2008-10-26T15:00:29Z | [
"python",
"functional-programming",
"pattern-matching"
] |
Pattern matching of lists in Python | 238,102 | <p>I want to do some pattern matching on lists in Python. For example, in Haskell, I can do something like the following:</p>
<pre><code>fun (head : rest) = ...
</code></pre>
<p>So when I pass in a list, <code>head</code> will be the first element, and <code>rest</code> will be the trailing elements.</p>
<p>Likewise... | 31 | 2008-10-26T14:50:40Z | 238,124 | <p>So far as I know there's no way to make it a one-liner in current Python without introducing another function, e.g.:</p>
<pre><code>split_list = lambda lst: (lst[0], lst[1:])
head, rest = split_list(my_func())
</code></pre>
<p>However, in Python 3.0 the specialized syntax used for variadic argument signatures and ... | 48 | 2008-10-26T15:01:12Z | [
"python",
"functional-programming",
"pattern-matching"
] |
Pattern matching of lists in Python | 238,102 | <p>I want to do some pattern matching on lists in Python. For example, in Haskell, I can do something like the following:</p>
<pre><code>fun (head : rest) = ...
</code></pre>
<p>So when I pass in a list, <code>head</code> will be the first element, and <code>rest</code> will be the trailing elements.</p>
<p>Likewise... | 31 | 2008-10-26T14:50:40Z | 238,126 | <p>Well, why you want it in 1-line in the first place? </p>
<p>If you really want to, you can always do a trick like this:</p>
<pre><code>def x(func):
y = func()
return y[0], y[1:]
# then, instead of calling my_func() call x(my_func)
(head, rest) = x(my_func) # that's one line :)
</code></pre>
| 2 | 2008-10-26T15:03:08Z | [
"python",
"functional-programming",
"pattern-matching"
] |
Pattern matching of lists in Python | 238,102 | <p>I want to do some pattern matching on lists in Python. For example, in Haskell, I can do something like the following:</p>
<pre><code>fun (head : rest) = ...
</code></pre>
<p>So when I pass in a list, <code>head</code> will be the first element, and <code>rest</code> will be the trailing elements.</p>
<p>Likewise... | 31 | 2008-10-26T14:50:40Z | 238,153 | <p>extended unpacking was introduced in 3.0
<a href="http://www.python.org/dev/peps/pep-3132/" rel="nofollow">http://www.python.org/dev/peps/pep-3132/</a></p>
| 2 | 2008-10-26T15:40:59Z | [
"python",
"functional-programming",
"pattern-matching"
] |
Pattern matching of lists in Python | 238,102 | <p>I want to do some pattern matching on lists in Python. For example, in Haskell, I can do something like the following:</p>
<pre><code>fun (head : rest) = ...
</code></pre>
<p>So when I pass in a list, <code>head</code> will be the first element, and <code>rest</code> will be the trailing elements.</p>
<p>Likewise... | 31 | 2008-10-26T14:50:40Z | 238,170 | <p>First of all, please note that the "pattern matching" of functional languages and the assignment to tuples you mention are not really that similar. In functional languages the patterns are used to give partial definitions of a function. So <code>f (x : s) = e</code> does not mean take the head and tail of the argume... | 28 | 2008-10-26T16:05:12Z | [
"python",
"functional-programming",
"pattern-matching"
] |
Pattern matching of lists in Python | 238,102 | <p>I want to do some pattern matching on lists in Python. For example, in Haskell, I can do something like the following:</p>
<pre><code>fun (head : rest) = ...
</code></pre>
<p>So when I pass in a list, <code>head</code> will be the first element, and <code>rest</code> will be the trailing elements.</p>
<p>Likewise... | 31 | 2008-10-26T14:50:40Z | 238,185 | <p>there was a reciepe in the python cookbook to do this. i cant seem to find it now but here is the code (i modified it slightly)</p>
<pre><code>
def peel(iterable,result=tuple):
'''Removes the requested items from the iterable and stores the remaining in a tuple
>>> x,y,z=peel('test')
>>> print repr(x),... | 1 | 2008-10-26T16:15:17Z | [
"python",
"functional-programming",
"pattern-matching"
] |
Pattern matching of lists in Python | 238,102 | <p>I want to do some pattern matching on lists in Python. For example, in Haskell, I can do something like the following:</p>
<pre><code>fun (head : rest) = ...
</code></pre>
<p>So when I pass in a list, <code>head</code> will be the first element, and <code>rest</code> will be the trailing elements.</p>
<p>Likewise... | 31 | 2008-10-26T14:50:40Z | 239,676 | <p>Further to the other answers, note that the equivalent head / tail operation in Python, including python3's extension of the * syntax is generally going to be less efficient than Haskell's pattern matching.</p>
<p>Python lists are implemented as vectors, so obtaining the tail will need to take a copy of the list. ... | 2 | 2008-10-27T12:08:14Z | [
"python",
"functional-programming",
"pattern-matching"
] |
Pattern matching of lists in Python | 238,102 | <p>I want to do some pattern matching on lists in Python. For example, in Haskell, I can do something like the following:</p>
<pre><code>fun (head : rest) = ...
</code></pre>
<p>So when I pass in a list, <code>head</code> will be the first element, and <code>rest</code> will be the trailing elements.</p>
<p>Likewise... | 31 | 2008-10-26T14:50:40Z | 240,018 | <p>Unlike Haskell or ML, Python doesn't have built-in pattern-matching of structures. The most Pythonic way of doing pattern-matching is with a try-except block:</p>
<pre><code>def recursive_sum(x):
try:
head, tail = x[0], x[1:]
return head + recursive-sum(tail)
except IndexError: # empty lis... | 1 | 2008-10-27T14:15:34Z | [
"python",
"functional-programming",
"pattern-matching"
] |
Pattern matching of lists in Python | 238,102 | <p>I want to do some pattern matching on lists in Python. For example, in Haskell, I can do something like the following:</p>
<pre><code>fun (head : rest) = ...
</code></pre>
<p>So when I pass in a list, <code>head</code> will be the first element, and <code>rest</code> will be the trailing elements.</p>
<p>Likewise... | 31 | 2008-10-26T14:50:40Z | 11,588,095 | <p>I'm working on <a href="https://github.com/martinblech/pyfpm" rel="nofollow">pyfpm</a>, a library for pattern matching in Python with a Scala-like syntax. You can use it to unpack objects like this:</p>
<pre><code>from pyfpm import Unpacker
unpacker = Unpacker()
unpacker('head :: tail') << (1, 2, 3)
unpack... | 2 | 2012-07-20T23:18:17Z | [
"python",
"functional-programming",
"pattern-matching"
] |
Is there a windows implementation to python libsvn? | 238,151 | <p>Because windows is case-insensitive and because SVN is case-sensitive and because VS2005 tends to rename files giving them the lower-case form which messes my repositories' history, I've tried to add the pre-commit hook script from <a href="http://svn.collab.net/repos/svn/trunk/contrib/hook-scripts/case-insensitive.... | 1 | 2008-10-26T15:39:36Z | 238,263 | <p>There are two alternative Python bindings for libsvn:</p>
<ul>
<li><a href="http://pysvn.tigris.org/" rel="nofollow">pysvn</a>.</li>
<li><a href="https://launchpad.net/subvertpy" rel="nofollow">subvertpy</a>. </li>
</ul>
<p>Subvertpy is quite new and is written by the author of <a href="http://bazaar-vcs.org/BzrFo... | 4 | 2008-10-26T17:02:43Z | [
"python",
"svn",
"hook",
"pre-commit"
] |
Is there a windows implementation to python libsvn? | 238,151 | <p>Because windows is case-insensitive and because SVN is case-sensitive and because VS2005 tends to rename files giving them the lower-case form which messes my repositories' history, I've tried to add the pre-commit hook script from <a href="http://svn.collab.net/repos/svn/trunk/contrib/hook-scripts/case-insensitive.... | 1 | 2008-10-26T15:39:36Z | 238,479 | <p>The Tigris.org's pre-complied python bindings for libsvn are a separate download. The latest as of Oct 27 could be found <a href="http://subversion.tigris.org/files/documents/15/44104/svn-win32-1.5.4_py25.zip" rel="nofollow">here</a>.</p>
<p>There are other binary SVN distributions listed <a href="http://subversion... | 3 | 2008-10-26T19:20:27Z | [
"python",
"svn",
"hook",
"pre-commit"
] |
Match unicode in ply's regexes | 238,223 | <p>I'm matching identifiers, but now I have a problem: my identifiers are allowed to contain unicode characters. Therefore the old way to do things is not enough:</p>
<pre><code>t_IDENTIFIER = r"[A-Za-z](\\.|[A-Za-z_0-9])*"
</code></pre>
<p>In <a href="http://freehg.org/u/cheery/aml/" rel="nofollow">my markup languag... | 4 | 2008-10-26T16:35:07Z | 238,227 | <p>Probably <a href="http://www.regular-expressions.info/posixbrackets.html" rel="nofollow">POSIX character classes</a> are right for you?</p>
| 0 | 2008-10-26T16:37:58Z | [
"python",
"regex",
"unicode",
"ply",
"character-properties"
] |
Match unicode in ply's regexes | 238,223 | <p>I'm matching identifiers, but now I have a problem: my identifiers are allowed to contain unicode characters. Therefore the old way to do things is not enough:</p>
<pre><code>t_IDENTIFIER = r"[A-Za-z](\\.|[A-Za-z_0-9])*"
</code></pre>
<p>In <a href="http://freehg.org/u/cheery/aml/" rel="nofollow">my markup languag... | 4 | 2008-10-26T16:35:07Z | 238,257 | <p>Check the answers to this question</p>
<p><a href="http://stackoverflow.com/questions/92438/stripping-non-printable-characters-from-a-string-in-python">http://stackoverflow.com/questions/92438/stripping-non-printable-characters-from-a-string-in-python</a></p>
<p>you'd just need to use the other unicode character c... | 1 | 2008-10-26T16:58:56Z | [
"python",
"regex",
"unicode",
"ply",
"character-properties"
] |
Match unicode in ply's regexes | 238,223 | <p>I'm matching identifiers, but now I have a problem: my identifiers are allowed to contain unicode characters. Therefore the old way to do things is not enough:</p>
<pre><code>t_IDENTIFIER = r"[A-Za-z](\\.|[A-Za-z_0-9])*"
</code></pre>
<p>In <a href="http://freehg.org/u/cheery/aml/" rel="nofollow">my markup languag... | 4 | 2008-10-26T16:35:07Z | 238,293 | <p>Solved it with the help of Vinko.</p>
<p>I realised that getting unicode range is plain dumb. So I'll do this:</p>
<pre><code>symbols = re.escape(''.join([chr(i) for i in xrange(33, 127) if not chr(i).isalnum()]))
symnums = re.escape(''.join([chr(i) for i in xrange(33, 127) if not chr(i).isalnum()]))
t_IDENTIFIER... | 1 | 2008-10-26T17:19:46Z | [
"python",
"regex",
"unicode",
"ply",
"character-properties"
] |
Match unicode in ply's regexes | 238,223 | <p>I'm matching identifiers, but now I have a problem: my identifiers are allowed to contain unicode characters. Therefore the old way to do things is not enough:</p>
<pre><code>t_IDENTIFIER = r"[A-Za-z](\\.|[A-Za-z_0-9])*"
</code></pre>
<p>In <a href="http://freehg.org/u/cheery/aml/" rel="nofollow">my markup languag... | 4 | 2008-10-26T16:35:07Z | 238,646 | <p>the <a href="http://docs.python.org/library/re#regular-expression-syntax">re</a> module supports the \w syntax which:</p>
<blockquote>
<p>If UNICODE is set, this will match the
characters [0-9_] plus whatever is
classified as alphanumeric in the
Unicode character properties database.</p>
</blockquote>
<p>t... | 5 | 2008-10-26T21:18:53Z | [
"python",
"regex",
"unicode",
"ply",
"character-properties"
] |
Match unicode in ply's regexes | 238,223 | <p>I'm matching identifiers, but now I have a problem: my identifiers are allowed to contain unicode characters. Therefore the old way to do things is not enough:</p>
<pre><code>t_IDENTIFIER = r"[A-Za-z](\\.|[A-Za-z_0-9])*"
</code></pre>
<p>In <a href="http://freehg.org/u/cheery/aml/" rel="nofollow">my markup languag... | 4 | 2008-10-26T16:35:07Z | 8,502,908 | <p>You need pass pass parameter reflags in lex.lex:</p>
<pre><code>lex.lex(reflags=re.UNICODE)
</code></pre>
| 3 | 2011-12-14T10:26:38Z | [
"python",
"regex",
"unicode",
"ply",
"character-properties"
] |
How can I support wildcards in user-defined search strings in Python? | 238,600 | <p>Is there a simple way to support wildcards ("*") when searching strings - without using RegEx?</p>
<p>Users are supposed to enter search terms using wildcards, but should not have to deal with the complexity of RegEx:</p>
<pre><code>"foo*" => str.startswith("foo")
"*foo" => str.endswith("foo")
"*foo*" ... | 8 | 2008-10-26T20:52:35Z | 238,602 | <p>You could try the <a href="http://www.python.org/doc/2.5.2/lib/module-fnmatch.html"><code>fnmatch</code></a> module, it's got a shell-like wildcard syntax.</p>
| 14 | 2008-10-26T20:54:30Z | [
"python",
"search",
"parsing",
"string",
"wildcard"
] |
XPath search with ElementTree | 238,697 | <p>New to xml. Looking for XPath to search a xml file with python ElementTree format</p>
<pre><code><root>
<child>One</child>
<child>Two</child>
<child>Three</child>
</root>
</code></pre>
<p>to do search for child with "Two" and return true/false</p>
<p>if it was star... | 4 | 2008-10-26T22:00:16Z | 239,422 | <p>I've been playing with ElementTree lately, lets see..</p>
<pre><code>>>> from xml.etree import ElementTree
>>> help(ElementTree.ElementPath)
>>> root = ElementTree.fromstring("""
<root><child>One</child><child>Two</child><child>Three</child></r... | 1 | 2008-10-27T09:32:36Z | [
"python",
"xml",
"xpath"
] |
XPath search with ElementTree | 238,697 | <p>New to xml. Looking for XPath to search a xml file with python ElementTree format</p>
<pre><code><root>
<child>One</child>
<child>Two</child>
<child>Three</child>
</root>
</code></pre>
<p>to do search for child with "Two" and return true/false</p>
<p>if it was star... | 4 | 2008-10-26T22:00:16Z | 285,719 | <p>When the following XPath expression is evaluated:</p>
<p> <code>boolean(/*/*[.='Two'])</code></p>
<p>the result is <strong>true</strong>, if such an element (a child of the top element such that its string value is equal to "Two") exists,</p>
<p>and <strong>false</strong> otherwise.</p>
<p... | 1 | 2008-11-12T23:00:26Z | [
"python",
"xml",
"xpath"
] |
How do I install plpython on MacOs X 10.5? | 238,882 | <p>I have just installed PostgreSQL 8.3.4 on Mac OS X 10.5 (using ports), but I cannot figure out how to enable PL/Python. When I run the <code>CREATE LANGUAGE plpythonu</code> I get the following errors:</p>
<pre><code>ERROR: could not access file "$libdir/plpython": No such file or directory
STATEMENT: CREATE LANG... | 1 | 2008-10-27T01:03:40Z | 238,906 | <p>Silly me:</p>
<pre><code>[lib/postgresql83] > variants postgresql83
postgresql83 has the variants:
universal
python: add support for python
krb5: add support for Kerberos 5 authentication
perl: add Perl support
</code></pre>
<p>(I'd had <code>universal</code>.)</p>
<p>This means that you have ... | 3 | 2008-10-27T01:20:29Z | [
"python",
"osx",
"postgresql"
] |
getting pywin32 to work inside open office 2.4 built in python 2.3 interpreter | 239,009 | <p>I need to update data to a mssql 2005 database so I have decided to use adodbapi, which is supposed to come built into the standard installation of python 2.1.1 and greater.</p>
<p>It needs pywin32 to work correctly and the open office python 2.3 installation does not have pywin32 built into it. It also seems like ... | 0 | 2008-10-27T03:32:24Z | 239,014 | <p><a href="http://www.time-travellers.org/shane/howtos/MS-SQL-Express-Python-HOWTO.html" rel="nofollow">http://www.time-travellers.org/shane/howtos/MS-SQL-Express-Python-HOWTO.html</a></p>
<p>use an alternative?</p>
| 0 | 2008-10-27T03:34:37Z | [
"python",
"openoffice.org",
"pywin32",
"adodbapi"
] |
getting pywin32 to work inside open office 2.4 built in python 2.3 interpreter | 239,009 | <p>I need to update data to a mssql 2005 database so I have decided to use adodbapi, which is supposed to come built into the standard installation of python 2.1.1 and greater.</p>
<p>It needs pywin32 to work correctly and the open office python 2.3 installation does not have pywin32 built into it. It also seems like ... | 0 | 2008-10-27T03:32:24Z | 239,179 | <p>I don't know about open office python.
I suggest trying the standard <a href="http://www.python.org/download/" rel="nofollow">windows python installation</a> followed by <a href="http://sourceforge.net/projects/pywin32/" rel="nofollow">Pywin32</a>. Alternatively, there is a single installer containing both at <a hre... | 0 | 2008-10-27T05:56:42Z | [
"python",
"openoffice.org",
"pywin32",
"adodbapi"
] |
getting pywin32 to work inside open office 2.4 built in python 2.3 interpreter | 239,009 | <p>I need to update data to a mssql 2005 database so I have decided to use adodbapi, which is supposed to come built into the standard installation of python 2.1.1 and greater.</p>
<p>It needs pywin32 to work correctly and the open office python 2.3 installation does not have pywin32 built into it. It also seems like ... | 0 | 2008-10-27T03:32:24Z | 239,487 | <p>maybe the best way to install pywin32 is to place it in </p>
<p>(openofficedir)\program\python-core-2.3.4\lib\site-packages</p>
<p>it is easy if you have a python 2.3 installation (with pywin installed) under </p>
<p>C:\python2.3 </p>
<p>move the C:\python2.3\Lib\site-packages\ to your</p>
<p>(openofficedir)\p... | 1 | 2008-10-27T10:13:39Z | [
"python",
"openoffice.org",
"pywin32",
"adodbapi"
] |
How can I call a DLL from a scripting language? | 239,020 | <p>I have a third-party product, a terminal emulator, which provides a DLL that can be linked to a C program to basically automate the driving of this product (send keystrokes, detect what's on the screen and so forth).</p>
<p>I want to drive it from a scripting language (I'm comfortable with Python and slightly less ... | 9 | 2008-10-27T03:42:07Z | 239,041 | <p>One way to call C libraries from Python is to use <a href="https://docs.python.org/library/ctypes.html" rel="nofollow">ctypes</a>:</p>
<pre><code>>>> from ctypes import *
>>> windll.user32.MessageBoxA(None, "Hello world", "ctypes", 0);
</code></pre>
| 15 | 2008-10-27T03:57:11Z | [
"python",
"perl",
"dll"
] |
How can I call a DLL from a scripting language? | 239,020 | <p>I have a third-party product, a terminal emulator, which provides a DLL that can be linked to a C program to basically automate the driving of this product (send keystrokes, detect what's on the screen and so forth).</p>
<p>I want to drive it from a scripting language (I'm comfortable with Python and slightly less ... | 9 | 2008-10-27T03:42:07Z | 239,043 | <p>In Perl, <a href="http://search.cpan.org/perldoc?Win32::API" rel="nofollow">Win32::API</a> is an easy way to some interfacing to DLLs. There is also <a href="http://search.cpan.org/perldoc?Inline::C" rel="nofollow">Inline::C</a>, if you have access to a compiler and the windows headers.</p>
<p>Perl <a href="http://... | 12 | 2008-10-27T03:58:05Z | [
"python",
"perl",
"dll"
] |
How can I call a DLL from a scripting language? | 239,020 | <p>I have a third-party product, a terminal emulator, which provides a DLL that can be linked to a C program to basically automate the driving of this product (send keystrokes, detect what's on the screen and so forth).</p>
<p>I want to drive it from a scripting language (I'm comfortable with Python and slightly less ... | 9 | 2008-10-27T03:42:07Z | 239,064 | <p>In Perl, <a href="http://search.cpan.org/perldoc/P5NCI" rel="nofollow">P5NCI</a> will also do that, at least in some cases. But it seems to me that anything you use that directly manages interfacing with the dll is going to be user-unfriendly, and if you are going to have a user (scriptor?) friendly wrapper, it mig... | 5 | 2008-10-27T04:13:34Z | [
"python",
"perl",
"dll"
] |
How can I call a DLL from a scripting language? | 239,020 | <p>I have a third-party product, a terminal emulator, which provides a DLL that can be linked to a C program to basically automate the driving of this product (send keystrokes, detect what's on the screen and so forth).</p>
<p>I want to drive it from a scripting language (I'm comfortable with Python and slightly less ... | 9 | 2008-10-27T03:42:07Z | 239,098 | <p>For Python, you could compile an extension which links to the DLL, so that in Python you could just import it like a normal module. You could do this by hand, by using a library like Boost.Python, or by using a tool such as SWIG (which also supports Perl and other scripting languages) to generate a wrapper automatic... | 4 | 2008-10-27T04:46:03Z | [
"python",
"perl",
"dll"
] |
How can I call a DLL from a scripting language? | 239,020 | <p>I have a third-party product, a terminal emulator, which provides a DLL that can be linked to a C program to basically automate the driving of this product (send keystrokes, detect what's on the screen and so forth).</p>
<p>I want to drive it from a scripting language (I'm comfortable with Python and slightly less ... | 9 | 2008-10-27T03:42:07Z | 241,652 | <p>The Python <strong>Py_InitModule</strong> API function allows you to create a module from c/c++ functions which can then be call from Python. </p>
<p>It takes about a dozen or so lines of c/c++ code to achieve but it is pretty easy code to write:</p>
<p><a href="https://python.readthedocs.org/en/v2.7.2/extending/e... | 3 | 2008-10-27T22:56:18Z | [
"python",
"perl",
"dll"
] |
Python: DISTINCT on GQuery result set (GQL, GAE) | 239,258 | <p>Imagine you got an entity in the Google App Engine datastore, storing links for anonymous users.
You would like to perform the following SQL query, which is not supported:</p>
<pre><code>SELECT DISTINCT user_hash FROM links
</code></pre>
<p>Instead you could use:</p>
<pre><code>user = db.GqlQuery("SELECT user_ha... | 7 | 2008-10-27T07:13:11Z | 239,305 | <p>One option would be to put the results into a set object:</p>
<p><a href="http://www.python.org/doc/2.6/library/sets.html#sets.Set" rel="nofollow">http://www.python.org/doc/2.6/library/sets.html#sets.Set</a></p>
<p>The resulting set will consist only of the distinct values passed into it.</p>
<p>Failing that, bui... | 1 | 2008-10-27T08:01:47Z | [
"python",
"sql",
"google-app-engine",
"distinct",
"gql"
] |
Python: DISTINCT on GQuery result set (GQL, GAE) | 239,258 | <p>Imagine you got an entity in the Google App Engine datastore, storing links for anonymous users.
You would like to perform the following SQL query, which is not supported:</p>
<pre><code>SELECT DISTINCT user_hash FROM links
</code></pre>
<p>Instead you could use:</p>
<pre><code>user = db.GqlQuery("SELECT user_ha... | 7 | 2008-10-27T07:13:11Z | 239,326 | <p>A set is good way to deal with that:</p>
<pre><code>>>> a = ['google.com', 'livejournal.com', 'livejournal.com', 'google.com', 'stackoverflow.com']
>>> b = set(a)
>>> b
set(['livejournal.com', 'google.com', 'stackoverflow.com'])
>>>
</code></pre>
<p>One suggestion w/r/t the firs... | 3 | 2008-10-27T08:25:57Z | [
"python",
"sql",
"google-app-engine",
"distinct",
"gql"
] |
Python: DISTINCT on GQuery result set (GQL, GAE) | 239,258 | <p>Imagine you got an entity in the Google App Engine datastore, storing links for anonymous users.
You would like to perform the following SQL query, which is not supported:</p>
<pre><code>SELECT DISTINCT user_hash FROM links
</code></pre>
<p>Instead you could use:</p>
<pre><code>user = db.GqlQuery("SELECT user_ha... | 7 | 2008-10-27T07:13:11Z | 5,340,901 | <p>Sorry to dig this question up but in GAE I cannot compare objects like that, I must use .key() for comparison like that:</p>
<p>Beware, this is very inefficient :</p>
<pre><code>def unique_result(array):
urk={} #unique results with key
for c in array:
if c.key() not in urwk:
urk[str(c.k... | 0 | 2011-03-17T15:04:24Z | [
"python",
"sql",
"google-app-engine",
"distinct",
"gql"
] |
Python: DISTINCT on GQuery result set (GQL, GAE) | 239,258 | <p>Imagine you got an entity in the Google App Engine datastore, storing links for anonymous users.
You would like to perform the following SQL query, which is not supported:</p>
<pre><code>SELECT DISTINCT user_hash FROM links
</code></pre>
<p>Instead you could use:</p>
<pre><code>user = db.GqlQuery("SELECT user_ha... | 7 | 2008-10-27T07:13:11Z | 14,169,747 | <p>Reviving this question for completion:</p>
<p>The DISTINCT keyword has been introduced in <a href="http://googleappengine.blogspot.com/2012/12/app-engine-174-released.html" rel="nofollow">release 1.7.4</a>.</p>
<p>You can find the updated GQL reference (for example for Python) <a href="https://developers.google.co... | 4 | 2013-01-05T07:38:24Z | [
"python",
"sql",
"google-app-engine",
"distinct",
"gql"
] |
WindowsError: priveledged instruction when saving a FreeImagePy Image in script, works in IDLE | 240,031 | <p>I'm working on a program to do some image wrangling in Python for work. I'm using FreeImagePy because PIL doesn't support multi-page TIFFs. Whenever I try to save a file with it from my program I get this error message (or something similar depending on which way I try to save):</p>
<pre><code>Error returned. TI... | 1 | 2008-10-27T14:18:54Z | 242,366 | <p>Looks like a permission issues, make sure you don't have the file open in another application, and that you have write permissions to the file location your trying to write to.</p>
| 1 | 2008-10-28T06:04:56Z | [
"python",
"exception",
"windowserror"
] |
WindowsError: priveledged instruction when saving a FreeImagePy Image in script, works in IDLE | 240,031 | <p>I'm working on a program to do some image wrangling in Python for work. I'm using FreeImagePy because PIL doesn't support multi-page TIFFs. Whenever I try to save a file with it from my program I get this error message (or something similar depending on which way I try to save):</p>
<pre><code>Error returned. TI... | 1 | 2008-10-27T14:18:54Z | 244,526 | <p>That's what I thought too, but I figured it out a couple hours ago. Apparently if the directory/file I'm trying to write to doesn't exist, FreeImagePy isn't smart enough to create it (most of the time. Creating a new multipage image seems to work fine) but i guess running it within IDLE, IDLE figures it out and ta... | 0 | 2008-10-28T19:29:43Z | [
"python",
"exception",
"windowserror"
] |
Python list of lists, changes reflected across sublists unexpectedly | 240,178 | <p>I needed to create a list of lists in Python, so I typed the following: </p>
<pre><code>myList = [[1] * 4] * 3
</code></pre>
<p>The list looked like this: </p>
<pre><code>[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code></pre>
<p>Then I changed one of the innermost values: </p>
<pre><code>myList[0][0] = 5... | 227 | 2008-10-27T14:57:22Z | 240,202 | <pre><code> [[1] * 4] * 3
</code></pre>
<p>or even</p>
<pre><code> [[1, 1, 1, 1]] * 3
</code></pre>
<p>Creates a list that references the internal <code>[1,1,1,1]</code> 3 times - not three copies of the inner list, so any time you modify the list (in any position), you'll see the change three times.</p>
<p>It's th... | 19 | 2008-10-27T15:02:54Z | [
"python",
"list",
"nested-lists",
"mutable"
] |
Python list of lists, changes reflected across sublists unexpectedly | 240,178 | <p>I needed to create a list of lists in Python, so I typed the following: </p>
<pre><code>myList = [[1] * 4] * 3
</code></pre>
<p>The list looked like this: </p>
<pre><code>[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code></pre>
<p>Then I changed one of the innermost values: </p>
<pre><code>myList[0][0] = 5... | 227 | 2008-10-27T14:57:22Z | 240,205 | <p>When you write <code>[x]*3</code> you get, essentially, the list <code>[x, x, x]</code>. That is, a list with 3 references to the same <code>x</code>. When you then modify this single <code>x</code> it is visible via all three references to it.</p>
<p>To fix it, you need to make sure that you create a new list at e... | 186 | 2008-10-27T15:03:30Z | [
"python",
"list",
"nested-lists",
"mutable"
] |
Python list of lists, changes reflected across sublists unexpectedly | 240,178 | <p>I needed to create a list of lists in Python, so I typed the following: </p>
<pre><code>myList = [[1] * 4] * 3
</code></pre>
<p>The list looked like this: </p>
<pre><code>[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code></pre>
<p>Then I changed one of the innermost values: </p>
<pre><code>myList[0][0] = 5... | 227 | 2008-10-27T14:57:22Z | 240,215 | <p>Actually, this is exactly what you would expect. Let's decompose what is happening here:</p>
<p>You write</p>
<pre><code>lst = [[1] * 4] * 3
</code></pre>
<p>This is equivalent to:</p>
<pre><code>lst1 = [1]*4
lst = [lst1]*3
</code></pre>
<p>This means <code>lst</code> is a list with 3 elements all pointing to <... | 26 | 2008-10-27T15:07:02Z | [
"python",
"list",
"nested-lists",
"mutable"
] |
Python list of lists, changes reflected across sublists unexpectedly | 240,178 | <p>I needed to create a list of lists in Python, so I typed the following: </p>
<pre><code>myList = [[1] * 4] * 3
</code></pre>
<p>The list looked like this: </p>
<pre><code>[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code></pre>
<p>Then I changed one of the innermost values: </p>
<pre><code>myList[0][0] = 5... | 227 | 2008-10-27T14:57:22Z | 18,454,568 | <pre><code>size = 3
matrix_surprise = [[0] * size] * size
matrix = [[0]*size for i in range(size)]
</code></pre>
<p><img src="https://i.stack.imgur.com/uHuck.png" alt="Frames and Objects"></p>
<p><a href="http://pythontutor.com/visualize.html#code=size+%3D+3%0Amatrix_surprise+%3D+%5B%5B0%5D+*+size%5D+*+size%0Amatrix+... | 56 | 2013-08-26T23:17:52Z | [
"python",
"list",
"nested-lists",
"mutable"
] |
Python list of lists, changes reflected across sublists unexpectedly | 240,178 | <p>I needed to create a list of lists in Python, so I typed the following: </p>
<pre><code>myList = [[1] * 4] * 3
</code></pre>
<p>The list looked like this: </p>
<pre><code>[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code></pre>
<p>Then I changed one of the innermost values: </p>
<pre><code>myList[0][0] = 5... | 227 | 2008-10-27T14:57:22Z | 28,146,989 | <p>Actually, think it in another case. Assume that if your list is this;</p>
<pre><code>[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code></pre>
<p>and if you write <code>myList[0][0] = 5</code> output will be;</p>
<pre><code>>>>
[[5, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
>>>
</code></pre>
<p>As... | -1 | 2015-01-26T08:50:32Z | [
"python",
"list",
"nested-lists",
"mutable"
] |
Python list of lists, changes reflected across sublists unexpectedly | 240,178 | <p>I needed to create a list of lists in Python, so I typed the following: </p>
<pre><code>myList = [[1] * 4] * 3
</code></pre>
<p>The list looked like this: </p>
<pre><code>[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code></pre>
<p>Then I changed one of the innermost values: </p>
<pre><code>myList[0][0] = 5... | 227 | 2008-10-27T14:57:22Z | 30,759,580 | <p>Let us rewrite your code in the following way:</p>
<pre><code>x = 1
y = [x]
z = y * 4
myList = [z] * 3
</code></pre>
<p>Then having this, run the following code to make everything more clear. What the code does is basically print the <a href="https://docs.python.org/2/library/functions.html#id" rel="nofollow"><co... | 0 | 2015-06-10T14:38:24Z | [
"python",
"list",
"nested-lists",
"mutable"
] |
Python list of lists, changes reflected across sublists unexpectedly | 240,178 | <p>I needed to create a list of lists in Python, so I typed the following: </p>
<pre><code>myList = [[1] * 4] * 3
</code></pre>
<p>The list looked like this: </p>
<pre><code>[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code></pre>
<p>Then I changed one of the innermost values: </p>
<pre><code>myList[0][0] = 5... | 227 | 2008-10-27T14:57:22Z | 30,898,048 | <p>Alongside the accepted answer that explained the problem correctly instead of creating a list with duplicated elements using following code :</p>
<pre><code>[[1]*4 for n in range(3)]
</code></pre>
<p>That use the throwaway variable <code>n</code> and the <code>range()</code> function, if You are in python 2 use <c... | 3 | 2015-06-17T17:08:52Z | [
"python",
"list",
"nested-lists",
"mutable"
] |
Python list of lists, changes reflected across sublists unexpectedly | 240,178 | <p>I needed to create a list of lists in Python, so I typed the following: </p>
<pre><code>myList = [[1] * 4] * 3
</code></pre>
<p>The list looked like this: </p>
<pre><code>[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code></pre>
<p>Then I changed one of the innermost values: </p>
<pre><code>myList[0][0] = 5... | 227 | 2008-10-27T14:57:22Z | 36,452,923 | <p>Python containers contain references to other objects. See this example:</p>
<pre><code>>>> a = []
>>> b = [a]
>>> b
[[]]
>>> a.append(1)
>>> b
[[1]]
</code></pre>
<p>In this <code>b</code> is a list that contains one item that is a reference to list <code>a</code>. The... | 0 | 2016-04-06T13:40:43Z | [
"python",
"list",
"nested-lists",
"mutable"
] |
Python list of lists, changes reflected across sublists unexpectedly | 240,178 | <p>I needed to create a list of lists in Python, so I typed the following: </p>
<pre><code>myList = [[1] * 4] * 3
</code></pre>
<p>The list looked like this: </p>
<pre><code>[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code></pre>
<p>Then I changed one of the innermost values: </p>
<pre><code>myList[0][0] = 5... | 227 | 2008-10-27T14:57:22Z | 36,823,796 | <p>I guess everybody explain what is happening.
I suggest one way to solve it:</p>
<p><code>myList = [[1 for i in range(4)] for j in range(3)]</code></p>
<pre><code>myList[0][0] = 5
</code></pre>
<p><code>print myList</code></p>
<p>And then you have:</p>
<pre><code>[[5, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code... | 0 | 2016-04-24T13:31:04Z | [
"python",
"list",
"nested-lists",
"mutable"
] |
Python list of lists, changes reflected across sublists unexpectedly | 240,178 | <p>I needed to create a list of lists in Python, so I typed the following: </p>
<pre><code>myList = [[1] * 4] * 3
</code></pre>
<p>The list looked like this: </p>
<pre><code>[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code></pre>
<p>Then I changed one of the innermost values: </p>
<pre><code>myList[0][0] = 5... | 227 | 2008-10-27T14:57:22Z | 37,804,636 | <p>In simple words this is happening because in python everything works <strong>by reference</strong>, so when you create a list of list that way you basically end up with such problems.</p>
<p>To solve your issue you can do either one of them:
1. Use numpy array <a href="http://docs.scipy.org/doc/numpy-1.10.0/referen... | 1 | 2016-06-14T06:36:52Z | [
"python",
"list",
"nested-lists",
"mutable"
] |
Python list of lists, changes reflected across sublists unexpectedly | 240,178 | <p>I needed to create a list of lists in Python, so I typed the following: </p>
<pre><code>myList = [[1] * 4] * 3
</code></pre>
<p>The list looked like this: </p>
<pre><code>[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code></pre>
<p>Then I changed one of the innermost values: </p>
<pre><code>myList[0][0] = 5... | 227 | 2008-10-27T14:57:22Z | 38,397,772 | <p>By using the inbuilt list function you can do like this</p>
<pre><code>a
out:[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
#Displaying the list
a.remove(a[0])
out:[[1, 1, 1, 1], [1, 1, 1, 1]]
# Removed the first element of the list in which you want altered number
a.append([5,1,1,1])
out:[[1, 1, 1, 1], [1, 1, 1, 1],... | 0 | 2016-07-15T13:48:36Z | [
"python",
"list",
"nested-lists",
"mutable"
] |
Python list of lists, changes reflected across sublists unexpectedly | 240,178 | <p>I needed to create a list of lists in Python, so I typed the following: </p>
<pre><code>myList = [[1] * 4] * 3
</code></pre>
<p>The list looked like this: </p>
<pre><code>[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
</code></pre>
<p>Then I changed one of the innermost values: </p>
<pre><code>myList[0][0] = 5... | 227 | 2008-10-27T14:57:22Z | 38,866,487 | <p>Trying to explain it more descriptively,</p>
<p>Operation 1:</p>
<pre><code>x = [[0, 0], [0, 0]]
print(type(x)) # <class 'list'>
print(x) # [[0, 0], [0, 0]]
x[0][0] = 1
print(x) # [[1, 0], [0, 0]]
</code></pre>
<p>Operation 2:</p>
<pre><code>y = [[0] * 2] * 2
print(type(y)) # <class 'list'>
print(y)... | 0 | 2016-08-10T07:09:51Z | [
"python",
"list",
"nested-lists",
"mutable"
] |
post_save signal on m2m field | 240,659 | <p>I have a pretty generic Article model, with m2m relation to Tag model. I want to keep count of each tag usage, i think the best way would be to denormalise count field on Tag model and update it each time Article being saved. How can i accomplish this, or maybe there's a better way?</p>
| 5 | 2008-10-27T17:09:41Z | 241,430 | <p>You can do this by creating an <a href="http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships" rel="nofollow">intermediate model</a> for the M2M relationship and use it as your hook for the <code>post_save</code> and <code>post_delete</code> signals to update the denormali... | 2 | 2008-10-27T21:17:58Z | [
"python",
"django",
"django-signals"
] |
post_save signal on m2m field | 240,659 | <p>I have a pretty generic Article model, with m2m relation to Tag model. I want to keep count of each tag usage, i think the best way would be to denormalise count field on Tag model and update it each time Article being saved. How can i accomplish this, or maybe there's a better way?</p>
| 5 | 2008-10-27T17:09:41Z | 2,732,790 | <p>This is a new feature in Django 1.2:
<a href="http://docs.djangoproject.com/en/dev/ref/signals/#m2m-changed" rel="nofollow">http://docs.djangoproject.com/en/dev/ref/signals/#m2m-changed</a></p>
| 3 | 2010-04-28T20:10:44Z | [
"python",
"django",
"django-signals"
] |
Possible to integrate Google AppEngine and Google Code for continuous integration? | 241,007 | <p>Anyone have any thoughts on how/if it is possible to integrate Google Code commits to cause a Google AppEngine deployment of the most recent code?</p>
<p>I have a simple Google AppEngine project's source hosted on Google Code and would love if everytime I committed to Subversion, that AppEngine would reflect the la... | 18 | 2008-10-27T18:43:41Z | 241,126 | <p>Very interesting, but not yet possible, AFAIK. I have been looking for that option in Google Code with no success.</p>
<p>The only solution I can figure out is to install something in your machine that checks for changes in your SVN repository.</p>
<p>I'll be happy to hear about other approaches.</p>
| 1 | 2008-10-27T19:26:59Z | [
"python",
"svn",
"google-app-engine",
"continuous-integration",
"google-code"
] |
Possible to integrate Google AppEngine and Google Code for continuous integration? | 241,007 | <p>Anyone have any thoughts on how/if it is possible to integrate Google Code commits to cause a Google AppEngine deployment of the most recent code?</p>
<p>I have a simple Google AppEngine project's source hosted on Google Code and would love if everytime I committed to Subversion, that AppEngine would reflect the la... | 18 | 2008-10-27T18:43:41Z | 241,672 | <p>For those of us who are using Github, this feature from the GAE team would make us all seriously consider switching to Google Code...</p>
| 1 | 2008-10-27T23:02:16Z | [
"python",
"svn",
"google-app-engine",
"continuous-integration",
"google-code"
] |
Possible to integrate Google AppEngine and Google Code for continuous integration? | 241,007 | <p>Anyone have any thoughts on how/if it is possible to integrate Google Code commits to cause a Google AppEngine deployment of the most recent code?</p>
<p>I have a simple Google AppEngine project's source hosted on Google Code and would love if everytime I committed to Subversion, that AppEngine would reflect the la... | 18 | 2008-10-27T18:43:41Z | 242,262 | <p>You'd probably have to have some glue on another computer which monitored SVN commits and deployed a new version for you. Google Code has yet to develop and release an API (which they need to do soon if they're serious about this whole development thing), but GAE can be deployed to with relative automated ease, so I... | 2 | 2008-10-28T04:31:13Z | [
"python",
"svn",
"google-app-engine",
"continuous-integration",
"google-code"
] |
Possible to integrate Google AppEngine and Google Code for continuous integration? | 241,007 | <p>Anyone have any thoughts on how/if it is possible to integrate Google Code commits to cause a Google AppEngine deployment of the most recent code?</p>
<p>I have a simple Google AppEngine project's source hosted on Google Code and would love if everytime I committed to Subversion, that AppEngine would reflect the la... | 18 | 2008-10-27T18:43:41Z | 342,168 | <p><a href="http://www.madebysofa.com" rel="nofollow">Made By Sofa</a> had a <a href="http://www.madebysofa.com/#blog/appengine_hosting" rel="nofollow">blog post</a> about their workflow with Google App Engine. In the second last paragraph they have <a href="http://www.madebysofa.com/media/downloads/appengine_deploy.sh... | 5 | 2008-12-04T21:37:02Z | [
"python",
"svn",
"google-app-engine",
"continuous-integration",
"google-code"
] |
Possible to integrate Google AppEngine and Google Code for continuous integration? | 241,007 | <p>Anyone have any thoughts on how/if it is possible to integrate Google Code commits to cause a Google AppEngine deployment of the most recent code?</p>
<p>I have a simple Google AppEngine project's source hosted on Google Code and would love if everytime I committed to Subversion, that AppEngine would reflect the la... | 18 | 2008-10-27T18:43:41Z | 466,252 | <p>Google Code Project Hosting now supports <a href="http://code.google.com/p/support/wiki/PostCommitWebHooks" rel="nofollow">Post-Commit Web Hooks</a>, which ping a project-owner-specified URL after every commit. This would eliminate the need to regularly poll your Google Code repository.</p>
| 5 | 2009-01-21T17:34:57Z | [
"python",
"svn",
"google-app-engine",
"continuous-integration",
"google-code"
] |
Python lazy list | 241,141 | <p>I would like create my own collection that has all the attributes of python list and also knows how to save/load itself into/from a database. Also I want to make the load implicit and lazy, as in it doesn't happen at the point of creation of the list, but waits until its first used.</p>
<p>Is there a single<code>_... | 14 | 2008-10-27T19:32:52Z | 241,169 | <p><a href="http://www.python.org/doc/2.5.2/ref/new-style-attribute-access.html" rel="nofollow"><code>__getattribute__</code></a></p>
| 3 | 2008-10-27T19:42:08Z | [
"python",
"lazylist"
] |
Python lazy list | 241,141 | <p>I would like create my own collection that has all the attributes of python list and also knows how to save/load itself into/from a database. Also I want to make the load implicit and lazy, as in it doesn't happen at the point of creation of the list, but waits until its first used.</p>
<p>Is there a single<code>_... | 14 | 2008-10-27T19:32:52Z | 241,519 | <p>No, there isn't.</p>
| 1 | 2008-10-27T21:56:35Z | [
"python",
"lazylist"
] |
Python lazy list | 241,141 | <p>I would like create my own collection that has all the attributes of python list and also knows how to save/load itself into/from a database. Also I want to make the load implicit and lazy, as in it doesn't happen at the point of creation of the list, but waits until its first used.</p>
<p>Is there a single<code>_... | 14 | 2008-10-27T19:32:52Z | 242,111 | <p>Not exactly. For emulating things <em>other</em> than lists, there's <code>__getattribute__</code>, but unfortunately Python doesn't consider operators like <code>x[y]</code> or <code>x(y)</code> to be <em>exactly</em> the same as <code>x.__getitem__(y)</code> or <code>x.__call__(y)</code>. Operators like that are... | 7 | 2008-10-28T02:59:18Z | [
"python",
"lazylist"
] |
Python lazy list | 241,141 | <p>I would like create my own collection that has all the attributes of python list and also knows how to save/load itself into/from a database. Also I want to make the load implicit and lazy, as in it doesn't happen at the point of creation of the list, but waits until its first used.</p>
<p>Is there a single<code>_... | 14 | 2008-10-27T19:32:52Z | 5,104,787 | <p>Not a <em>single</em>, but 5 is enough:</p>
<pre><code>from collections import MutableSequence
class Monitored(MutableSequence):
  def __init__(self):
    super(Monitored, self).__init__()
    self._list = []
  def __len__(self):
    r = len(self._list)
    print "len: {0:d... | 7 | 2011-02-24T12:39:42Z | [
"python",
"lazylist"
] |
Python lazy list | 241,141 | <p>I would like create my own collection that has all the attributes of python list and also knows how to save/load itself into/from a database. Also I want to make the load implicit and lazy, as in it doesn't happen at the point of creation of the list, but waits until its first used.</p>
<p>Is there a single<code>_... | 14 | 2008-10-27T19:32:52Z | 18,638,351 | <p>There isn't a single method. You have to redefine quite a lot of them. MutableSequence seems to be the modern way of doing it. Here is a version that works with Python 2.4+::</p>
<pre><code>class LazyList(list):
"""List populated on first use."""
def __new__(cls, fill_iter):
class LazyList(list):
... | 1 | 2013-09-05T13:57:11Z | [
"python",
"lazylist"
] |
Single Table Inheritance in Django | 241,250 | <p>Is there explicit support for Single Table Inheritance in Django? Last I heard, the feature was still under development and debate. </p>
<p>Are there libraries/hacks I can use in the meantime to capture the basic behavior? I have a hierarchy that mixes different objects. The canonical example of a corporation struc... | 15 | 2008-10-27T20:18:08Z | 243,543 | <p>There are currently two forms of inheritance in Django - MTI (model table inheritance) and ABC (abstract base classes).</p>
<p>I wrote a <a href="http://web.archive.org/web/20090227074910/http://thisweekindjango.com/articles/2008/jun/17/abstract-base-classes-vs-model-tab/" rel="nofollow">tutorial</a> on what's goin... | 15 | 2008-10-28T14:29:20Z | [
"python",
"django",
"django-models",
"single-table-inheritance"
] |
Single Table Inheritance in Django | 241,250 | <p>Is there explicit support for Single Table Inheritance in Django? Last I heard, the feature was still under development and debate. </p>
<p>Are there libraries/hacks I can use in the meantime to capture the basic behavior? I have a hierarchy that mixes different objects. The canonical example of a corporation struc... | 15 | 2008-10-27T20:18:08Z | 1,720,733 | <p>I think the OP is asking about Single-Table Inheritance as <a href="http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html" rel="nofollow">defined here</a>:</p>
<blockquote>
<p>Relational databases don't support inheritance, so when mapping from objects to databases we have to consider how to represen... | 14 | 2009-11-12T08:25:21Z | [
"python",
"django",
"django-models",
"single-table-inheritance"
] |
Single Table Inheritance in Django | 241,250 | <p>Is there explicit support for Single Table Inheritance in Django? Last I heard, the feature was still under development and debate. </p>
<p>Are there libraries/hacks I can use in the meantime to capture the basic behavior? I have a hierarchy that mixes different objects. The canonical example of a corporation struc... | 15 | 2008-10-27T20:18:08Z | 2,383,744 | <p>I think you can do something akin to this. </p>
<p>I have to implement a solution for this problem myself, and here was how I solved it:</p>
<pre><code>class Citrus(models.Model)
how_acidic = models.PositiveIntegerField(max_value=100)
skin_color = models.CharField()
type = models.CharField()
class Tan... | 1 | 2010-03-05T00:28:58Z | [
"python",
"django",
"django-models",
"single-table-inheritance"
] |
Single Table Inheritance in Django | 241,250 | <p>Is there explicit support for Single Table Inheritance in Django? Last I heard, the feature was still under development and debate. </p>
<p>Are there libraries/hacks I can use in the meantime to capture the basic behavior? I have a hierarchy that mixes different objects. The canonical example of a corporation struc... | 15 | 2008-10-27T20:18:08Z | 5,615,845 | <p>See my attempt:</p>
<p><a href="http://djangosnippets.org/snippets/2408/" rel="nofollow">http://djangosnippets.org/snippets/2408/</a></p>
<blockquote>
<p>An emulation of "table per hierarchy" a.k.a. "single table inheritance" in Django. The base class must hold all the fields. It's subclasses are not allowed to ... | 3 | 2011-04-11T01:00:01Z | [
"python",
"django",
"django-models",
"single-table-inheritance"
] |
Single Table Inheritance in Django | 241,250 | <p>Is there explicit support for Single Table Inheritance in Django? Last I heard, the feature was still under development and debate. </p>
<p>Are there libraries/hacks I can use in the meantime to capture the basic behavior? I have a hierarchy that mixes different objects. The canonical example of a corporation struc... | 15 | 2008-10-27T20:18:08Z | 14,661,921 | <p>this might be of use: <a href="https://github.com/craigds/django-typed-models" rel="nofollow">https://github.com/craigds/django-typed-models</a>
It looks to be somewhat of an implementation of Single Table Inheritance but it has the limitation that subclasses can't have any extra fields.</p>
<p>there is also a fork... | 1 | 2013-02-02T12:31:11Z | [
"python",
"django",
"django-models",
"single-table-inheritance"
] |
Python snippet to remove C and C++ comments | 241,327 | <p>I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)</p>
<p>I realize that I could .match() substrings with a Regex, but that doesn't solve nesting <code>/*</code>, or having a <code>//</code> inside a <code>/* */</code>.</p>
<p>Ideally, I... | 32 | 2008-10-27T20:47:24Z | 241,329 | <p>C (and C++) comments cannot be nested. Regular expressions work well:</p>
<pre><code>//.*?\n|/\*.*?\*/
</code></pre>
<p>This requires the âSingle lineâ flag (<code>Re.S</code>) because a C comment can span multiple lines.</p>
<pre><code>def stripcomments(text):
return re.sub('//.*?\n|/\*.*?\*/', '', text,... | 19 | 2008-10-27T20:48:57Z | [
"c++",
"python",
"c",
"regex",
"comments"
] |
Python snippet to remove C and C++ comments | 241,327 | <p>I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)</p>
<p>I realize that I could .match() substrings with a Regex, but that doesn't solve nesting <code>/*</code>, or having a <code>//</code> inside a <code>/* */</code>.</p>
<p>Ideally, I... | 32 | 2008-10-27T20:47:24Z | 241,333 | <p>you may be able to leverage <a href="http://www.language-binding.net/pyplusplus/pyplusplus.html" rel="nofollow">py++</a> to parse the C++ source with GCC.</p>
<blockquote>
<p>Py++ does not reinvent the wheel. It
uses GCC C++ compiler to parse C++
source files. To be more precise, the
tool chain looks like t... | 3 | 2008-10-27T20:50:45Z | [
"c++",
"python",
"c",
"regex",
"comments"
] |
Python snippet to remove C and C++ comments | 241,327 | <p>I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)</p>
<p>I realize that I could .match() substrings with a Regex, but that doesn't solve nesting <code>/*</code>, or having a <code>//</code> inside a <code>/* */</code>.</p>
<p>Ideally, I... | 32 | 2008-10-27T20:47:24Z | 241,506 | <p>This handles C++-style comments, C-style comments, strings and simple nesting thereof.</p>
<pre><code>def comment_remover(text):
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return " " # note: a space and not an empty string
else:
return s
pat... | 54 | 2008-10-27T21:48:07Z | [
"c++",
"python",
"c",
"regex",
"comments"
] |
Python snippet to remove C and C++ comments | 241,327 | <p>I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)</p>
<p>I realize that I could .match() substrings with a Regex, but that doesn't solve nesting <code>/*</code>, or having a <code>//</code> inside a <code>/* */</code>.</p>
<p>Ideally, I... | 32 | 2008-10-27T20:47:24Z | 242,107 | <p>Don't forget that in C, backslash-newline is eliminated before comments are processed, and trigraphs are processed before that (because ??/ is the trigraph for backslash). I have a C program called SCC (strip C/C++ comments), and here is part of the test code...</p>
<pre><code>" */ /* SCC has been trained to know ... | 6 | 2008-10-28T02:57:06Z | [
"c++",
"python",
"c",
"regex",
"comments"
] |
Python snippet to remove C and C++ comments | 241,327 | <p>I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)</p>
<p>I realize that I could .match() substrings with a Regex, but that doesn't solve nesting <code>/*</code>, or having a <code>//</code> inside a <code>/* */</code>.</p>
<p>Ideally, I... | 32 | 2008-10-27T20:47:24Z | 242,110 | <p>The regular expression cases will fall down in some situations, like where a string literal contains a subsequence which matches the comment syntax. You really need a parse tree to deal with this.</p>
| 4 | 2008-10-28T02:58:24Z | [
"c++",
"python",
"c",
"regex",
"comments"
] |
Python snippet to remove C and C++ comments | 241,327 | <p>I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)</p>
<p>I realize that I could .match() substrings with a Regex, but that doesn't solve nesting <code>/*</code>, or having a <code>//</code> inside a <code>/* */</code>.</p>
<p>Ideally, I... | 32 | 2008-10-27T20:47:24Z | 242,226 | <p>I don't know if you're familiar with <code>sed</code>, the UNIX-based (but Windows-available) text parsing program, but I've found a sed script <a href="http://sed.sourceforge.net/grabbag/scripts/remccoms3.sed">here</a> which will remove C/C++ comments from a file. It's very smart; for example, it will ignore '//' a... | 9 | 2008-10-28T04:03:20Z | [
"c++",
"python",
"c",
"regex",
"comments"
] |
Python snippet to remove C and C++ comments | 241,327 | <p>I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)</p>
<p>I realize that I could .match() substrings with a Regex, but that doesn't solve nesting <code>/*</code>, or having a <code>//</code> inside a <code>/* */</code>.</p>
<p>Ideally, I... | 32 | 2008-10-27T20:47:24Z | 1,078,406 | <p>You don't really need a parse tree to do this perfectly, but you do in effect need the token stream equivalent to what is produced by the compiler's front end. Such a token stream must necessarilyy take care of all the weirdness such as line-continued comment start, comment start in string, trigraph normalization, ... | 0 | 2009-07-03T08:38:46Z | [
"c++",
"python",
"c",
"regex",
"comments"
] |
Python snippet to remove C and C++ comments | 241,327 | <p>I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)</p>
<p>I realize that I could .match() substrings with a Regex, but that doesn't solve nesting <code>/*</code>, or having a <code>//</code> inside a <code>/* */</code>.</p>
<p>Ideally, I... | 32 | 2008-10-27T20:47:24Z | 1,078,484 | <p>I'm sorry this not a Python solution, but you could also use a tool that understands how to remove comments, like your C/C++ preprocessor. Here's how GNU CPP <a href="http://gcc.gnu.org/onlinedocs/cpp/Invocation.html#index-fpreprocessed-171" rel="nofollow">does it</a>.</p>
<pre><code>cpp -fpreprocessed foo.c
</code... | 0 | 2009-07-03T09:08:24Z | [
"c++",
"python",
"c",
"regex",
"comments"
] |
Python snippet to remove C and C++ comments | 241,327 | <p>I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)</p>
<p>I realize that I could .match() substrings with a Regex, but that doesn't solve nesting <code>/*</code>, or having a <code>//</code> inside a <code>/* */</code>.</p>
<p>Ideally, I... | 32 | 2008-10-27T20:47:24Z | 1,294,188 | <p>There is also a non-python answer: use the program <a href="http://www.bdc.cx/software/stripcmt/" rel="nofollow">stripcmt</a>:</p>
<blockquote>
<p>StripCmt is a simple utility written
in C to remove comments from C, C++,
and Java source files. In the grand
tradition of Unix text processing
programs, it ca... | 1 | 2009-08-18T14:18:07Z | [
"c++",
"python",
"c",
"regex",
"comments"
] |
Python snippet to remove C and C++ comments | 241,327 | <p>I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)</p>
<p>I realize that I could .match() substrings with a Regex, but that doesn't solve nesting <code>/*</code>, or having a <code>//</code> inside a <code>/* */</code>.</p>
<p>Ideally, I... | 32 | 2008-10-27T20:47:24Z | 5,221,953 | <p>I ran across this problem recently when I took a class where the professor required us to strip javadoc from our source code before submitting it to him for a code review. We had to do this several times, but we couldn't just remove the javadoc permanently because we were required to generate javadoc html files as ... | -1 | 2011-03-07T16:10:54Z | [
"c++",
"python",
"c",
"regex",
"comments"
] |
Python snippet to remove C and C++ comments | 241,327 | <p>I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)</p>
<p>I realize that I could .match() substrings with a Regex, but that doesn't solve nesting <code>/*</code>, or having a <code>//</code> inside a <code>/* */</code>.</p>
<p>Ideally, I... | 32 | 2008-10-27T20:47:24Z | 18,234,680 | <p>This posting provides a coded-out version of the improvement to Markus Jarderot's code that was described by atikat, in a comment to Markus Jarderot's posting. (Thanks to both for providing the original code, which saved me a lot of work.)</p>
<p>To describe the improvement somewhat more fully: The improvement kee... | 5 | 2013-08-14T14:36:25Z | [
"c++",
"python",
"c",
"regex",
"comments"
] |
Python snippet to remove C and C++ comments | 241,327 | <p>I'm looking for Python code that removes C and C++ comments from a string. (Assume the string contains an entire C source file.)</p>
<p>I realize that I could .match() substrings with a Regex, but that doesn't solve nesting <code>/*</code>, or having a <code>//</code> inside a <code>/* */</code>.</p>
<p>Ideally, I... | 32 | 2008-10-27T20:47:24Z | 18,996,903 | <p>The following worked for me: </p>
<pre><code>from subprocess import check_output
class Util:
def strip_comments(self,source_code):
process = check_output(['cpp', '-fpreprocessed', source_code],shell=False)
return process
if __name__ == "__main__":
util = Util()
print util.strip_comments("somefile.e... | 0 | 2013-09-25T05:27:45Z | [
"c++",
"python",
"c",
"regex",
"comments"
] |
Reading collections of extended elements in an RSS feed with Universal Feed Parser | 241,503 | <p>Is there any way to read a collection of extension elements with <a href="http://www.feedparser.org/" rel="nofollow">Universal Feed Parser</a>?</p>
<p>This is just a short snippet from Kuler RSS feed:</p>
<pre><code><channel>
<item>
<!-- snip: regular RSS elements -->
<kuler:themeIte... | 4 | 2008-10-27T21:46:19Z | 242,254 | <p>Universal Feed Parser is really nice for most feeds, but for extended feeds, you might wanna try something called <a href="http://crummy.com/software/BeautifulSoup" rel="nofollow">BeautifulSoup</a>. It's an XML/HTML/XHTML parsing library which is originally designed for screenscraping; turns out it's also brilliant ... | 3 | 2008-10-28T04:23:48Z | [
"python",
"rss",
"adobe",
"feeds"
] |
I want a program that writes every possible combination to a different line of a text file | 241,533 | <p>I want to write a program that would print every combination of a set of variables to a text file, creating a word list. Each answer should be written on a separate line and write all of the results for 1 digit, 2 digits, and 3 digits to a single text file.</p>
<p>Is there a simple way I can write a python program ... | 4 | 2008-10-27T22:03:06Z | 241,542 | <p>It shouldn't be too hard in most languages. Does the following pseudo-code help?</p>
<pre><code>for(int i=0; i < 2^digits; i++)
{
WriteLine(ToBinaryString(i));
}
</code></pre>
| 2 | 2008-10-27T22:06:54Z | [
"python",
"recursion"
] |
I want a program that writes every possible combination to a different line of a text file | 241,533 | <p>I want to write a program that would print every combination of a set of variables to a text file, creating a word list. Each answer should be written on a separate line and write all of the results for 1 digit, 2 digits, and 3 digits to a single text file.</p>
<p>Is there a simple way I can write a python program ... | 4 | 2008-10-27T22:03:06Z | 241,557 | <pre><code># Given two lists of strings, return a list of all ways to concatenate
# one from each.
def combos(xs, ys):
return [x + y for x in xs for y in ys]
digits = ['0', '1']
for c in combos(digits, combos(digits, digits)):
print c
#. 000
#. 001
#. 010
#. 011
#. 100
#. 101
#. 110
#. 111
</code></pre>
| 3 | 2008-10-27T22:12:51Z | [
"python",
"recursion"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.