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 |
|---|---|---|---|---|---|---|---|---|---|
SQL returning extra data | 1,672,814 | <p>Hey there, was wondering if anyone could help a newbie on SQL and Python. I thought I had a pretty decent grasp of it, however something odd happened recently.</p>
<p>Here is the the following code snipped from a larger portion:</p>
<pre><code> try:
self.db.query("SELECT * FROM account WHERE email = '{0}' A... | 1 | 2009-11-04T10:11:16Z | 1,672,825 | <p>Is this because the data type in the SQL is 'long'?</p>
<p>As you can see in the tuple, they still are valid numbers, not strings, so this should not pose any problem.</p>
| 1 | 2009-11-04T10:14:30Z | [
"python",
"sql",
"mysql"
] |
SQL returning extra data | 1,672,814 | <p>Hey there, was wondering if anyone could help a newbie on SQL and Python. I thought I had a pretty decent grasp of it, however something odd happened recently.</p>
<p>Here is the the following code snipped from a larger portion:</p>
<pre><code> try:
self.db.query("SELECT * FROM account WHERE email = '{0}' A... | 1 | 2009-11-04T10:11:16Z | 1,672,830 | <p>The L's indicate that the numbers are of the type Long. Your field types in the db are probably set to be of this type.</p>
| 1 | 2009-11-04T10:15:28Z | [
"python",
"sql",
"mysql"
] |
SQL returning extra data | 1,672,814 | <p>Hey there, was wondering if anyone could help a newbie on SQL and Python. I thought I had a pretty decent grasp of it, however something odd happened recently.</p>
<p>Here is the the following code snipped from a larger portion:</p>
<pre><code> try:
self.db.query("SELECT * FROM account WHERE email = '{0}' A... | 1 | 2009-11-04T10:11:16Z | 1,672,847 | <p>2L is the long version of 2: different object but the same value.</p>
<pre><code>>>> print 2L == 2
True
>>> print 2L is 2
False
</code></pre>
<p>Are you running all the same (python, packages, db, OS...)?</p>
| 2 | 2009-11-04T10:18:18Z | [
"python",
"sql",
"mysql"
] |
SQL returning extra data | 1,672,814 | <p>Hey there, was wondering if anyone could help a newbie on SQL and Python. I thought I had a pretty decent grasp of it, however something odd happened recently.</p>
<p>Here is the the following code snipped from a larger portion:</p>
<pre><code> try:
self.db.query("SELECT * FROM account WHERE email = '{0}' A... | 1 | 2009-11-04T10:11:16Z | 1,672,942 | <p>I think, python's dbapi is supposed to always return integer-fields as long. </p>
<p>Anyway, 10L, 5L and so on is the way repr (which is used on every item of a tuple in your case) works for longs.</p>
<p>One more thing. I see, you are using MySQLdb. In that case, I strongly suggest, that you stop using the c-api ... | 7 | 2009-11-04T10:32:47Z | [
"python",
"sql",
"mysql"
] |
Combining 2 lists in python | 1,673,005 | <p>I have 2 lists each of equal size and am interested to combine these two lists and write it into a file.</p>
<pre><code>alist=[1,2,3,5]
blist=[2,3,4,5]
</code></pre>
<p>--the resulting list should be like
[(1,2), (2,3), (3,4), (5,5)]</p>
<p>After that i want that to be written it to a file. How can i accomp... | 3 | 2009-11-04T10:43:54Z | 1,673,012 | <pre><code># combine the lists
zipped = zip(alist, blist)
# write to a file (in append mode)
file = open("filename", 'a')
for item in zipped:
file.write("%d, %d\n" % item)
file.close()
</code></pre>
<p>The resulting output in the file will be:</p>
<pre><code> 1,2
2,3
3,4
5,5
</code></pre>
| 13 | 2009-11-04T10:44:50Z | [
"python",
"list"
] |
Combining 2 lists in python | 1,673,005 | <p>I have 2 lists each of equal size and am interested to combine these two lists and write it into a file.</p>
<pre><code>alist=[1,2,3,5]
blist=[2,3,4,5]
</code></pre>
<p>--the resulting list should be like
[(1,2), (2,3), (3,4), (5,5)]</p>
<p>After that i want that to be written it to a file. How can i accomp... | 3 | 2009-11-04T10:43:54Z | 1,673,091 | <p>For the sake of completeness, I'll add to <a href="http://stackoverflow.com/questions/1673005/combining-2-lists-in-python/1673012#1673012">Ben's solution</a> that <code>itertools.izip</code> is preferable especially for larger lists if the result is used iteratively, as the final result is not an actual list but a g... | 6 | 2009-11-04T11:04:20Z | [
"python",
"list"
] |
What do backticks mean to the python interpreter: `num` | 1,673,071 | <p>I'm playing around with list comprehensions and I came across this little snippet on another site:</p>
<pre><code>return ''.join([`num` for num in xrange(loop_count)])
</code></pre>
<p>I spent a few minutes trying to replicate the function (by typing) before realising the <code>`num`</code> bit was breaking it.</p... | 65 | 2009-11-04T11:00:38Z | 1,673,087 | <p>Backticks are a deprecated alias for <code>repr()</code>. Don't use them any more, the syntax was removed in Python 3.0.</p>
<p>Using backticks seems to be faster than using <code>repr(num)</code> or <code>num.__repr__()</code> in version 2.x. I guess it's because additional dictionary lookup is required in the glo... | 89 | 2009-11-04T11:03:58Z | [
"python",
"list-comprehension"
] |
What do backticks mean to the python interpreter: `num` | 1,673,071 | <p>I'm playing around with list comprehensions and I came across this little snippet on another site:</p>
<pre><code>return ''.join([`num` for num in xrange(loop_count)])
</code></pre>
<p>I spent a few minutes trying to replicate the function (by typing) before realising the <code>`num`</code> bit was breaking it.</p... | 65 | 2009-11-04T11:00:38Z | 1,673,096 | <p>My guess is that <code>num</code> doesn't define the method <code>__str__()</code>, so <code>str()</code> has to do a second lookup for <code>__repr__</code>.</p>
<p>The backticks look directly for <code>__repr__</code>. If that's true, then using <code>repr()</code> instead of the backticks should give you the sam... | 1 | 2009-11-04T11:05:22Z | [
"python",
"list-comprehension"
] |
What do backticks mean to the python interpreter: `num` | 1,673,071 | <p>I'm playing around with list comprehensions and I came across this little snippet on another site:</p>
<pre><code>return ''.join([`num` for num in xrange(loop_count)])
</code></pre>
<p>I spent a few minutes trying to replicate the function (by typing) before realising the <code>`num`</code> bit was breaking it.</p... | 65 | 2009-11-04T11:00:38Z | 1,673,233 | <p>Backtick quoting is generally non-useful and gone in Python 3.</p>
<p>For what it's worth, this:</p>
<pre><code>''.join(map(repr, xrange(10000000)))
</code></pre>
<p>is marginally faster than the backtick version for me. But worrying about this is probably a premature optimisation.</p>
| 9 | 2009-11-04T11:32:57Z | [
"python",
"list-comprehension"
] |
How to store callback methods? | 1,673,483 | <p>i am trying to store some method callbacks but referring to it will keep the bound object alive, so i tried to keep a weakref to method but that doesn't seems to be possible?</p>
<p>so</p>
<ul>
<li><p>Why can't i keep a weak ref. to method (see example below)</p></li>
<li><p>What is the best way to keep method ref... | 1 | 2009-11-04T12:25:47Z | 1,673,519 | <p>Since the method is bound to the object, what would you expect to do with it if the object doesnt exist? What would self contain?</p>
<p>If you dont need the object in the method, make it a classmethod. Then your object will be GC:d even if you have a normal reference to the method.</p>
| 3 | 2009-11-04T12:35:02Z | [
"python",
"methods",
"weak-references"
] |
How to store callback methods? | 1,673,483 | <p>i am trying to store some method callbacks but referring to it will keep the bound object alive, so i tried to keep a weakref to method but that doesn't seems to be possible?</p>
<p>so</p>
<ul>
<li><p>Why can't i keep a weak ref. to method (see example below)</p></li>
<li><p>What is the best way to keep method ref... | 1 | 2009-11-04T12:25:47Z | 1,673,882 | <p>I have asked <a href="http://stackoverflow.com/questions/1364923/how-to-connect-to-a-gobject-signal-in-python-without-it-keeping-a-reference-to-t">the same question here</a>! In my question, I talk about GObject, but recognize it is a general problem in any kind of Python! I got help by lioro there, and what I use i... | 0 | 2009-11-04T13:55:23Z | [
"python",
"methods",
"weak-references"
] |
How to store callback methods? | 1,673,483 | <p>i am trying to store some method callbacks but referring to it will keep the bound object alive, so i tried to keep a weakref to method but that doesn't seems to be possible?</p>
<p>so</p>
<ul>
<li><p>Why can't i keep a weak ref. to method (see example below)</p></li>
<li><p>What is the best way to keep method ref... | 1 | 2009-11-04T12:25:47Z | 1,674,055 | <p>They have a nice solution in:</p>
<p><a href="http://code.activestate.com/recipes/81253/" rel="nofollow">http://code.activestate.com/recipes/81253/</a></p>
<p>Take a look at the last example, posted by Anonymous. </p>
| 0 | 2009-11-04T14:22:59Z | [
"python",
"methods",
"weak-references"
] |
How to store callback methods? | 1,673,483 | <p>i am trying to store some method callbacks but referring to it will keep the bound object alive, so i tried to keep a weakref to method but that doesn't seems to be possible?</p>
<p>so</p>
<ul>
<li><p>Why can't i keep a weak ref. to method (see example below)</p></li>
<li><p>What is the best way to keep method ref... | 1 | 2009-11-04T12:25:47Z | 1,674,701 | <p>Recipe 6.10 in Python Cookbook, "Keeping References to Bound Methods Without Inhibiting Garbage Collection", offers a pretty thorough though concise discussion and solutions. You can read it online (on Google Books) <a href="http://books.google.com/books?id=1Shx%5FVXS6ioC&pg=PA257&lpg=PA257&dq=weak+refe... | 2 | 2009-11-04T15:52:07Z | [
"python",
"methods",
"weak-references"
] |
algorithm for list identification and parsing | 1,673,729 | <p>I have data which in theory is a list, but historically has been input by the user as a free form text field. Now I need to separate each item of the list so that each element can be analysed.</p>
<p>Simplified examples of my data as input by users:</p>
<pre><code>one, two, three, four, five
one. two. three, fou... | 2 | 2009-11-04T13:26:42Z | 1,673,784 | <p>In java, a string tokenizer will do this (i.e. StringTokenizer(inputString, delimiterList))</p>
<pre><code>StringTokenizer st = new StringTokenizer( "A B|C-D", " |-" );
while ( st.hasMoreTokens() ) {
System.out.println( st.nextToken() );
}
</code></pre>
<p>prints </p>
<p>A</p>
<p>B</p>
<p>C</p>
<p>D</p>
| 0 | 2009-11-04T13:36:15Z | [
"java",
"python",
"list",
"parsing"
] |
algorithm for list identification and parsing | 1,673,729 | <p>I have data which in theory is a list, but historically has been input by the user as a free form text field. Now I need to separate each item of the list so that each element can be analysed.</p>
<p>Simplified examples of my data as input by users:</p>
<pre><code>one, two, three, four, five
one. two. three, fou... | 2 | 2009-11-04T13:26:42Z | 1,673,787 | <p>The following will "parse" your input string into sequences of "word" characters separated by non-word characters. </p>
<pre><code>String input = ...
String[] parts = input.split("[^\w]*");
</code></pre>
<p>I don't know how you are going to distinguish a list from gibberish. I think you will need to explain your... | 0 | 2009-11-04T13:36:37Z | [
"java",
"python",
"list",
"parsing"
] |
algorithm for list identification and parsing | 1,673,729 | <p>I have data which in theory is a list, but historically has been input by the user as a free form text field. Now I need to separate each item of the list so that each element can be analysed.</p>
<p>Simplified examples of my data as input by users:</p>
<pre><code>one, two, three, four, five
one. two. three, fou... | 2 | 2009-11-04T13:26:42Z | 1,673,839 | <p>I don't know if I understand your problem. If you want to extract alpha-numeric strings from messed string in python it would be:</p>
<pre><code>>>> import re
>>> re.split('\W+','abaa, asodf ?. poasid - paosfi sec')
['abaa', 'asodf', 'poasid', 'paosfi', 'sec']
</code></pre>
<p>Or if you know the ... | 1 | 2009-11-04T13:45:45Z | [
"java",
"python",
"list",
"parsing"
] |
algorithm for list identification and parsing | 1,673,729 | <p>I have data which in theory is a list, but historically has been input by the user as a free form text field. Now I need to separate each item of the list so that each element can be analysed.</p>
<p>Simplified examples of my data as input by users:</p>
<pre><code>one, two, three, four, five
one. two. three, fou... | 2 | 2009-11-04T13:26:42Z | 1,674,133 | <p>If you can't define your data ("The words can be anything, I there is no way to know beforehand a dictionary of what any individual list can contain. They will not be only numbers... it could be a list of anything") then you have serious problems.</p>
<p>Specifically, if you can't define your data, your problem can... | 2 | 2009-11-04T14:36:15Z | [
"java",
"python",
"list",
"parsing"
] |
algorithm for list identification and parsing | 1,673,729 | <p>I have data which in theory is a list, but historically has been input by the user as a free form text field. Now I need to separate each item of the list so that each element can be analysed.</p>
<p>Simplified examples of my data as input by users:</p>
<pre><code>one, two, three, four, five
one. two. three, fou... | 2 | 2009-11-04T13:26:42Z | 1,674,272 | <p>The first step to solving this problem is to analyze, in detail, how it is that humans solve this problem. I'd break the problem down into two parts.</p>
<ol>
<li><p>How do humans distinguish between lists and non-lists? For example, is it because non-lists are grammatical English sentences? If that's the case, ... | 3 | 2009-11-04T14:52:25Z | [
"java",
"python",
"list",
"parsing"
] |
algorithm for list identification and parsing | 1,673,729 | <p>I have data which in theory is a list, but historically has been input by the user as a free form text field. Now I need to separate each item of the list so that each element can be analysed.</p>
<p>Simplified examples of my data as input by users:</p>
<pre><code>one, two, three, four, five
one. two. three, fou... | 2 | 2009-11-04T13:26:42Z | 1,674,291 | <p>Either you know your dictionary of words or you have a precedence order for list delimiters. Otherwise the problem is too ill defined for a computer to handle. </p>
<p>I suppose your precedence could be commas, dots, hyphens, spaces. So, this means that you split by commas in preference to splitting by dots and so... | 0 | 2009-11-04T14:54:36Z | [
"java",
"python",
"list",
"parsing"
] |
algorithm for list identification and parsing | 1,673,729 | <p>I have data which in theory is a list, but historically has been input by the user as a free form text field. Now I need to separate each item of the list so that each element can be analysed.</p>
<p>Simplified examples of my data as input by users:</p>
<pre><code>one, two, three, four, five
one. two. three, fou... | 2 | 2009-11-04T13:26:42Z | 1,676,051 | <p>I'm not sure what the best answer really is, But if you need to have few false positives, then perhaps what you should do is define a few patterns that are very likely to be lists, and strictly reject every other datum. </p>
<pre><code>patterns = [
re.compile(r'^\s*(\w+)(\s*,\s*(\w+))*\s*$'),
re.compile(r... | 0 | 2009-11-04T19:29:24Z | [
"java",
"python",
"list",
"parsing"
] |
algorithm for list identification and parsing | 1,673,729 | <p>I have data which in theory is a list, but historically has been input by the user as a free form text field. Now I need to separate each item of the list so that each element can be analysed.</p>
<p>Simplified examples of my data as input by users:</p>
<pre><code>one, two, three, four, five
one. two. three, fou... | 2 | 2009-11-04T13:26:42Z | 1,677,274 | <p>Rather than focusing on the code, how about the method. Building a little on what swillden said...</p>
<p>If your lists are consumed by human users, you could ask them to correct you when you make a mistake (this correction is visible either to the person entering the text or to a later user viewing the text). If a... | 1 | 2009-11-04T22:58:57Z | [
"java",
"python",
"list",
"parsing"
] |
algorithm for list identification and parsing | 1,673,729 | <p>I have data which in theory is a list, but historically has been input by the user as a free form text field. Now I need to separate each item of the list so that each element can be analysed.</p>
<p>Simplified examples of my data as input by users:</p>
<pre><code>one, two, three, four, five
one. two. three, fou... | 2 | 2009-11-04T13:26:42Z | 1,677,609 | <p>A pyparsing stab at sift wheat from the chaff...</p>
<pre><code>rawdata = """\
one, two, three, four, five
one. two. three, four. five.
"I start with one, then do two, maybe three and four then five"
one
two
three
four
five.
one, two. three four five
one two three four - five
"not even a list, no list-ele... | 0 | 2009-11-05T00:27:59Z | [
"java",
"python",
"list",
"parsing"
] |
Delineating a Read File | 1,673,740 | <p>Not really too sure how to word this question, therefore if you don't particularly understand it then I can try again.</p>
<p>I have a file called <em>example.txt</em> and I'd like to import this into my Python program. Here I will do some calculations with what it contains and other things that are irrelevant. </p... | 0 | 2009-11-04T13:28:47Z | 1,673,765 | <pre><code>with open("example.txt") as f:
for line in f:
key, value = line.strip().split("=")
do_something(key,value)
</code></pre>
<p>looks like a starting point if I understand you correctly. You need Python 2.6 or 3.x for this.</p>
<p>Another place to look is the <code>csv</code> module that ca... | 4 | 2009-11-04T13:32:11Z | [
"python",
"file-handling"
] |
Delineating a Read File | 1,673,740 | <p>Not really too sure how to word this question, therefore if you don't particularly understand it then I can try again.</p>
<p>I have a file called <em>example.txt</em> and I'd like to import this into my Python program. Here I will do some calculations with what it contains and other things that are irrelevant. </p... | 0 | 2009-11-04T13:28:47Z | 1,673,910 | <pre><code>for line in open("file")
key, value = line.strip().split("=")
key=key.strip()
value=value.strip()
do_something(key,value)
</code></pre>
| 0 | 2009-11-04T13:59:38Z | [
"python",
"file-handling"
] |
Delineating a Read File | 1,673,740 | <p>Not really too sure how to word this question, therefore if you don't particularly understand it then I can try again.</p>
<p>I have a file called <em>example.txt</em> and I'd like to import this into my Python program. Here I will do some calculations with what it contains and other things that are irrelevant. </p... | 0 | 2009-11-04T13:28:47Z | 1,675,005 | <p>There's also another method - you can create a valid python file (let it be a list, dict definition or whatever else), read its content using </p>
<pre><code>f = open('file.txt', r)
content = f.read() #assuming file isn't too long
</code></pre>
<p>And then just parse it:</p>
<pre><code>parsedContent = eval(conten... | 0 | 2009-11-04T16:32:15Z | [
"python",
"file-handling"
] |
How to match alphabetical chars without numeric chars with Python regexp? | 1,673,749 | <p>Using Python module re, how to get the equivalent of the "\w" (which matches alphanumeric chars) WITHOUT matching the numeric characters (those which can be matched by "[0-9]")?</p>
<p>Notice that the basic need is to match any character (including all unicode variation) without numerical chars (which are matched b... | 8 | 2009-11-04T13:30:16Z | 1,673,775 | <pre><code>(?!\d)\w
</code></pre>
<p>A position that is not followed by a digit, and then <code>\w</code>. Effectively cancels out digits but allows the <code>\w</code> range by using a negative look-ahead.</p>
<p>The same could be expressed as a positive look-ahead and <code>\D</code>:</p>
<pre><code>(?=\D)\w
</cod... | 5 | 2009-11-04T13:35:11Z | [
"python",
"regex"
] |
How to match alphabetical chars without numeric chars with Python regexp? | 1,673,749 | <p>Using Python module re, how to get the equivalent of the "\w" (which matches alphanumeric chars) WITHOUT matching the numeric characters (those which can be matched by "[0-9]")?</p>
<p>Notice that the basic need is to match any character (including all unicode variation) without numerical chars (which are matched b... | 8 | 2009-11-04T13:30:16Z | 1,673,804 | <p>You want <code>[^\W\d]</code>: the group of characters that is not (either a digit or not an alphanumeric). Add an underscore in that negated set if you don't want them either.</p>
<p>A bit twisted, if you ask me, but it works. Should be faster than the lookahead alternative.</p>
| 20 | 2009-11-04T13:40:33Z | [
"python",
"regex"
] |
How can I get the full list of running processes on a Mac from a python app | 1,673,874 | <p>I want to get the list of running processes on the Mac, similar to what you get from 'ps -ea'</p>
<p>I have tried <strong>os.popen('ps -ea')</strong> but this only lists a small subset of the processes, presumably those owned by the owning shell.</p>
<p>Other options I have tried are</p>
<pre><code>'sh -c /bin... | 2 | 2009-11-04T13:52:51Z | 1,673,985 | <p><code>os.popen('ps aux')</code> looks like it's listing all processes for me.</p>
| 7 | 2009-11-04T14:11:55Z | [
"python",
"osx",
"process"
] |
Saving work after a SIGINT | 1,674,300 | <p>I have a program which takes a long time to complete. I would like
it to be able to catch <code>SIGINT</code> (ctrl-c) and call the <code>self.save_work()</code> method.</p>
<p>As it stands, my <code>signal_hander()</code> does not work since
<code>self</code> is not defined by the time the program reaches <code>... | 4 | 2009-11-04T14:55:00Z | 1,674,350 | <pre><code>import signal
def signal_handler(signal, frame):
#do some stuff
def main():
#do some more stuff
if __name__=='__main__':
signal.signal(signal.SIGINT, signal_handler)
main()
</code></pre>
| 2 | 2009-11-04T15:01:14Z | [
"python",
"signals",
"sigint"
] |
Saving work after a SIGINT | 1,674,300 | <p>I have a program which takes a long time to complete. I would like
it to be able to catch <code>SIGINT</code> (ctrl-c) and call the <code>self.save_work()</code> method.</p>
<p>As it stands, my <code>signal_hander()</code> does not work since
<code>self</code> is not defined by the time the program reaches <code>... | 4 | 2009-11-04T14:55:00Z | 1,674,376 | <p>If you just want to catch ctr+c then you can catch the <a href="http://docs.python.org/library/exceptions.html#exceptions.KeyboardInterrupt" rel="nofollow">KeyboardInterrupt</a> exception:</p>
<pre><code>class Main(object):
def do_stuff(self):
...
def save_work(self):
...
def __init__(se... | 4 | 2009-11-04T15:05:12Z | [
"python",
"signals",
"sigint"
] |
Saving work after a SIGINT | 1,674,300 | <p>I have a program which takes a long time to complete. I would like
it to be able to catch <code>SIGINT</code> (ctrl-c) and call the <code>self.save_work()</code> method.</p>
<p>As it stands, my <code>signal_hander()</code> does not work since
<code>self</code> is not defined by the time the program reaches <code>... | 4 | 2009-11-04T14:55:00Z | 10,972,782 | <p>Usually, "work" involves some kind of a big loop. To tame your loop, and prevent it from breaking in an unknown step, you can use the following context manager:</p>
<pre><code>import signal
class GracefulInterruptHandler(object):
def __init__(self, sig=signal.SIGINT):
self.sig = sig
def __enter_... | 3 | 2012-06-10T22:20:29Z | [
"python",
"signals",
"sigint"
] |
Django Tests fail with InternalError: no such savepoint. DB: Postgres, passes on mysql | 1,674,530 | <p>Interestingly it also works on the shell.</p>
<pre><code> [MY code which calls Model.objects.get_or_create(...)]
File "/usr/lib/python2.5/site-packages/django/db/models/manager.py", line 123, in get_or_create
return self.get_query_set().get_or_create(**kwargs)
File "/usr/lib/python2.5/site-packages/django... | 5 | 2009-11-04T15:27:01Z | 9,933,322 | <p>If you want to test code that uses transactions, you'll need to subclass <a href="https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.TransactionTestCase" rel="nofollow"><code>TransactionTestCase</code></a> instead of <code>TestCase</code></p>
<p>for example:</p>
<pre><code>from django.t... | 3 | 2012-03-29T20:50:13Z | [
"python",
"mysql",
"django",
"postgresql"
] |
Dynamically decompose list into variables in Python | 1,674,621 | <p>I have 2 dimensional list created at runtime (the number of entries in either dimension is unknown). For example:</p>
<pre><code>long_list = [ [2, 3, 6], [3, 7, 9] ]
</code></pre>
<p>I want to iterate through it by getting the ith entry from each list inside the long_list:</p>
<pre><code>for entry in long_list.it... | 1 | 2009-11-04T15:41:11Z | 1,674,644 | <pre><code>>>> long_list = [ [2, 3, 6], [3, 7, 9] ]
>>> import itertools
>>> for i in itertools.izip_longest(*long_list): # called zip_longest in py3k
print(i)
(2, 3)
(3, 7)
(6, 9)
</code></pre>
<p>Basically, you need to use unpacking feature here. It would work similarly for <cod... | 4 | 2009-11-04T15:44:22Z | [
"python",
"reflection",
"list",
"itertools"
] |
Dictionary Operations... Index / Iterate / Validate | 1,674,683 | <p>I'd like to:</p>
<ul>
<li>Check a key / value at position i</li>
<li>Check to see if key / value contains a string</li>
<li>delete / store in another variable either the key / value</li>
</ul>
<p>The equivelant of this Java code:</p>
<pre><code>//Some list...
ArrayList<String> example;
...
//Index into data... | 0 | 2009-11-04T15:49:43Z | 1,674,703 | <p>Direct translation (for an <code>ArrayList<String></code>, you do not want a dictionary, you want a list):</p>
<pre><code>example = ["foo", "bar", "baz"]
str = example[i]
if "someText" in str:
somestuff()
</code></pre>
<p>Get used to the <code>for</code> keyword, though, it's awesome in Python:</p>
<pre... | 2 | 2009-11-04T15:52:26Z | [
"python",
"dictionary"
] |
Dictionary Operations... Index / Iterate / Validate | 1,674,683 | <p>I'd like to:</p>
<ul>
<li>Check a key / value at position i</li>
<li>Check to see if key / value contains a string</li>
<li>delete / store in another variable either the key / value</li>
</ul>
<p>The equivelant of this Java code:</p>
<pre><code>//Some list...
ArrayList<String> example;
...
//Index into data... | 0 | 2009-11-04T15:49:43Z | 1,674,749 | <p>Python dictionaries are implemented as hash tables, so there is no intrinsic ordering; therefore, "position i" is a totally nonsensical concept for a dict -- it's like asking for the dict entry that's most yellow, or that least resembles a llama... those concepts just don't apply to dict entries, and "position i" is... | 4 | 2009-11-04T15:56:34Z | [
"python",
"dictionary"
] |
Dictionary Operations... Index / Iterate / Validate | 1,674,683 | <p>I'd like to:</p>
<ul>
<li>Check a key / value at position i</li>
<li>Check to see if key / value contains a string</li>
<li>delete / store in another variable either the key / value</li>
</ul>
<p>The equivelant of this Java code:</p>
<pre><code>//Some list...
ArrayList<String> example;
...
//Index into data... | 0 | 2009-11-04T15:49:43Z | 1,674,766 | <p>What's nice about Python is that you can try code interactively.</p>
<p>So we create a <a href="http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange" rel="nofollow"><code>list</code></a> which is like a Java <code>List</code>:</p>
<pre><code>>>> mylist = ["pytho... | 0 | 2009-11-04T15:58:59Z | [
"python",
"dictionary"
] |
Dictionary Operations... Index / Iterate / Validate | 1,674,683 | <p>I'd like to:</p>
<ul>
<li>Check a key / value at position i</li>
<li>Check to see if key / value contains a string</li>
<li>delete / store in another variable either the key / value</li>
</ul>
<p>The equivelant of this Java code:</p>
<pre><code>//Some list...
ArrayList<String> example;
...
//Index into data... | 0 | 2009-11-04T15:49:43Z | 1,674,768 | <p>Yo can do that to reproduce the same behavior that your example in Java.</p>
<pre><code># Some list
example = {} # or example = dict()
...
# Index into data estructure.
example[example.keys(i)]
# Check for some string...
if example[example.keys(i)] == 'someText' :
pass
# Store in some other variable...
exam = e... | 1 | 2009-11-04T15:59:10Z | [
"python",
"dictionary"
] |
how to process long-running requests in python workers? | 1,674,696 | <p>I have a python (well, it's php now but we're rewriting) function that takes some parameters (A and B) and compute some results (finds best path from A to B in a graph, graph is read-only), in typical scenario one call takes 0.1s to 0.9s to complete. This function is accessed by users as a simple REST web-service (G... | 3 | 2009-11-04T15:51:16Z | 1,674,809 | <p>The typical way to handle this sort of arrangement using threads in Python is to use the standard library module <a href="http://docs.python.org/library/queue.html" rel="nofollow">Queue</a>. An example of using the Queue module for managing workers can be found here: <a href="http://effbot.org/librarybook/queue.htm"... | 1 | 2009-11-04T16:04:49Z | [
"python",
"nginx",
"load-balancing",
"wsgi",
"reverse-proxy"
] |
how to process long-running requests in python workers? | 1,674,696 | <p>I have a python (well, it's php now but we're rewriting) function that takes some parameters (A and B) and compute some results (finds best path from A to B in a graph, graph is read-only), in typical scenario one call takes 0.1s to 0.9s to complete. This function is accessed by users as a simple REST web-service (G... | 3 | 2009-11-04T15:51:16Z | 1,674,814 | <p>Looks like you need the "workers" to be separate processes (at least some of them, and therefore might as well make them all separate processes rather than bunches of threads divided into several processes). The <a href="http://docs.python.org/library/multiprocessing.html?highlight=multiprocessing#module-multiproce... | 2 | 2009-11-04T16:05:36Z | [
"python",
"nginx",
"load-balancing",
"wsgi",
"reverse-proxy"
] |
how to process long-running requests in python workers? | 1,674,696 | <p>I have a python (well, it's php now but we're rewriting) function that takes some parameters (A and B) and compute some results (finds best path from A to B in a graph, graph is read-only), in typical scenario one call takes 0.1s to 0.9s to complete. This function is accessed by users as a simple REST web-service (G... | 3 | 2009-11-04T15:51:16Z | 1,675,726 | <p>I think you can configure modwsgi/Apache so it will have several "hot" Python interpreters
in separate processes ready to go at all times and also reuse them for new accesses
(and spawn a new one if they are all busy).
In this case you could load all the preprocessed data as module globals and they would
only get lo... | 0 | 2009-11-04T18:25:37Z | [
"python",
"nginx",
"load-balancing",
"wsgi",
"reverse-proxy"
] |
how to process long-running requests in python workers? | 1,674,696 | <p>I have a python (well, it's php now but we're rewriting) function that takes some parameters (A and B) and compute some results (finds best path from A to B in a graph, graph is read-only), in typical scenario one call takes 0.1s to 0.9s to complete. This function is accessed by users as a simple REST web-service (G... | 3 | 2009-11-04T15:51:16Z | 1,676,102 | <p>You could use nginx load balancer to proxy to PythonPaste paster (which serves WSGI, for example Pylons), that launches each request as separate thread anyway.</p>
| 0 | 2009-11-04T19:39:24Z | [
"python",
"nginx",
"load-balancing",
"wsgi",
"reverse-proxy"
] |
how to process long-running requests in python workers? | 1,674,696 | <p>I have a python (well, it's php now but we're rewriting) function that takes some parameters (A and B) and compute some results (finds best path from A to B in a graph, graph is read-only), in typical scenario one call takes 0.1s to 0.9s to complete. This function is accessed by users as a simple REST web-service (G... | 3 | 2009-11-04T15:51:16Z | 1,681,380 | <p>There are many FastCGI modules with preforked mode and WSGI interface for python around, the most known is <a href="http://trac.saddi.com/flup" rel="nofollow"><code>flup</code></a>. My personal preference for such task is <a href="http://bitbucket.org/barbuza/superfcgi/" rel="nofollow"><code>superfcgi</code></a> wit... | 1 | 2009-11-05T15:43:09Z | [
"python",
"nginx",
"load-balancing",
"wsgi",
"reverse-proxy"
] |
how to process long-running requests in python workers? | 1,674,696 | <p>I have a python (well, it's php now but we're rewriting) function that takes some parameters (A and B) and compute some results (finds best path from A to B in a graph, graph is read-only), in typical scenario one call takes 0.1s to 0.9s to complete. This function is accessed by users as a simple REST web-service (G... | 3 | 2009-11-04T15:51:16Z | 1,682,864 | <p>The most simple solution in this case is to use the webserver to do all the heavy lifting. Why should you handle threads and/or processes when the webserver will do all that for you?</p>
<p>The standard arrangement in deployments of Python is:</p>
<ol>
<li>The webserver start a number of processes each running a c... | 1 | 2009-11-05T19:09:37Z | [
"python",
"nginx",
"load-balancing",
"wsgi",
"reverse-proxy"
] |
how to process long-running requests in python workers? | 1,674,696 | <p>I have a python (well, it's php now but we're rewriting) function that takes some parameters (A and B) and compute some results (finds best path from A to B in a graph, graph is read-only), in typical scenario one call takes 0.1s to 0.9s to complete. This function is accessed by users as a simple REST web-service (G... | 3 | 2009-11-04T15:51:16Z | 1,718,183 | <p>Another option is a queue table in the database.<br>
The worker processes run in a loop or off cron and poll the queue table for new jobs.</p>
| 0 | 2009-11-11T21:23:51Z | [
"python",
"nginx",
"load-balancing",
"wsgi",
"reverse-proxy"
] |
Recommendations for eCommerce Framework / Products for working with Django? | 1,674,730 | <p>We are building out an app in Django and trying to nail down what the right ecommerce framework will be to work with in Django. We've heard of Satchmo. Any other suggestions on ways to approach ecommerce in Django that's clean, simple, cheap and easy to implement? Thanks!</p>
| 5 | 2009-11-04T15:54:42Z | 1,675,543 | <p>Take a look on the <a href="http://www.getlfs.com/">LFS</a> - it is much simpler then Satchmo is</p>
| 5 | 2009-11-04T17:51:17Z | [
"python",
"django",
"e-commerce"
] |
Recommendations for eCommerce Framework / Products for working with Django? | 1,674,730 | <p>We are building out an app in Django and trying to nail down what the right ecommerce framework will be to work with in Django. We've heard of Satchmo. Any other suggestions on ways to approach ecommerce in Django that's clean, simple, cheap and easy to implement? Thanks!</p>
| 5 | 2009-11-04T15:54:42Z | 5,697,927 | <p>I did a comparison of the options <a href="http://readncode.com/blog/the-state-of-ecommerce-in-django/" rel="nofollow">here</a>.</p>
| 2 | 2011-04-18T02:21:10Z | [
"python",
"django",
"e-commerce"
] |
Can I add Runtime Properties to a Python App Engine App? | 1,674,764 | <p>Coming from a java background I'm used to having a bunch of properties files I can swap round at runtime dependent on what server I'm running on e.g. dev/production.</p>
<p>Is there a method in python to do similar, specifically on Google's App Engine framework?</p>
<p>At the minute I have them defined in .py file... | 0 | 2009-11-04T15:58:56Z | 1,674,790 | <p>You can:</p>
<ol>
<li><p>edit records in the datastore through the dashboard ( if you really have to )</p></li>
<li><p>upload new scripts / files ( you can access files in READ-ONLY )</p></li>
<li><p>export a WEB Service API to configuration records in the datastore ( probably not what you had in mind )</p></li>
<... | 1 | 2009-11-04T16:02:28Z | [
"python",
"google-app-engine",
"properties",
"runtime"
] |
Can I add Runtime Properties to a Python App Engine App? | 1,674,764 | <p>Coming from a java background I'm used to having a bunch of properties files I can swap round at runtime dependent on what server I'm running on e.g. dev/production.</p>
<p>Is there a method in python to do similar, specifically on Google's App Engine framework?</p>
<p>At the minute I have them defined in .py file... | 0 | 2009-11-04T15:58:56Z | 1,674,804 | <p>I don't see what is wrong with using python files to configure your application (apart from cultural issues :) ). In fact I have an issue with frameworks which <em>don't</em> allow me to script the configuration parameters.</p>
<p>That said, please have a look <a href="http://aaron.oirt.rutgers.edu/myapp/docs/W110... | 1 | 2009-11-04T16:04:21Z | [
"python",
"google-app-engine",
"properties",
"runtime"
] |
How can I replace a class with another class from another module in a lot of files without a lot of manual editing? | 1,674,791 | <p>Basically, I have a <em>lot</em> of Python classes (representing our database schema) that look something like this:</p>
<pre><code>from foo import xyz, b, c
class bar(object):
x = xyz()
y = b()
z = c()
</code></pre>
<p>...and I want to change it to this:</p>
<pre><code>from foo import b, c
from baz ... | 2 | 2009-11-04T16:02:31Z | 1,674,833 | <p>sed s/a/m would be disasterous since bar would be changed to bmr.</p>
<p>If the variable names are truly short and/or non-unique, non-regex-able,
then perhaps the easiest thing to do would be to insert</p>
<pre><code>from baz import m as a
</code></pre>
<p>Then you don't have to change any other code further down... | 1 | 2009-11-04T16:07:24Z | [
"python",
"automation",
"sed",
"rope"
] |
How can I replace a class with another class from another module in a lot of files without a lot of manual editing? | 1,674,791 | <p>Basically, I have a <em>lot</em> of Python classes (representing our database schema) that look something like this:</p>
<pre><code>from foo import xyz, b, c
class bar(object):
x = xyz()
y = b()
z = c()
</code></pre>
<p>...and I want to change it to this:</p>
<pre><code>from foo import b, c
from baz ... | 2 | 2009-11-04T16:02:31Z | 1,674,843 | <p>I have not used rope but can't you move a to baz then rename baz.a to baz.m You can in other refactoring tools for other languages and the <a href="http://rope.sourceforge.net/" rel="nofollow">rope page</a> suggests it can.</p>
<p>For minimal edits - but probably worse style and maintainability make foo.a call baz.... | 0 | 2009-11-04T16:08:22Z | [
"python",
"automation",
"sed",
"rope"
] |
How can I replace a class with another class from another module in a lot of files without a lot of manual editing? | 1,674,791 | <p>Basically, I have a <em>lot</em> of Python classes (representing our database schema) that look something like this:</p>
<pre><code>from foo import xyz, b, c
class bar(object):
x = xyz()
y = b()
z = c()
</code></pre>
<p>...and I want to change it to this:</p>
<pre><code>from foo import b, c
from baz ... | 2 | 2009-11-04T16:02:31Z | 1,674,851 | <p>Monkey-patching would be the quick and dirty way to do it -- before you do any other import, perform the following preliminary:</p>
<pre><code>import foo
import baz
foo.a = baz.m
</code></pre>
<p>now, every subsequent use of attribute <code>a</code> of module <code>foo</code> will actually be using class <code>m</... | 3 | 2009-11-04T16:09:18Z | [
"python",
"automation",
"sed",
"rope"
] |
How can I replace a class with another class from another module in a lot of files without a lot of manual editing? | 1,674,791 | <p>Basically, I have a <em>lot</em> of Python classes (representing our database schema) that look something like this:</p>
<pre><code>from foo import xyz, b, c
class bar(object):
x = xyz()
y = b()
z = c()
</code></pre>
<p>...and I want to change it to this:</p>
<pre><code>from foo import b, c
from baz ... | 2 | 2009-11-04T16:02:31Z | 1,676,403 | <p>You might try this <code>sed</code> script:</p>
<pre><code>sed -r 's/(^from foo import.*)(xyz, |, xyz)(.*)/\1\3/; T; a\from baz import foobar'
</code></pre>
<p>or, equivalently:</p>
<pre><code>sed 's/\(^from foo import.*\)\(xyz, \|, xyz\)\(.*\)/\1\3/; T; a\from baz import foobar'
</code></pre>
<p>If you try it l... | 1 | 2009-11-04T20:29:17Z | [
"python",
"automation",
"sed",
"rope"
] |
How can I replace a class with another class from another module in a lot of files without a lot of manual editing? | 1,674,791 | <p>Basically, I have a <em>lot</em> of Python classes (representing our database schema) that look something like this:</p>
<pre><code>from foo import xyz, b, c
class bar(object):
x = xyz()
y = b()
z = c()
</code></pre>
<p>...and I want to change it to this:</p>
<pre><code>from foo import b, c
from baz ... | 2 | 2009-11-04T16:02:31Z | 1,681,309 | <p>I wouldn't use monkeypatching, I'd implement my own functions:</p>
<pre><code>import foo
def xyz():
return foo.xyz()
def b():
return foo.b()
def c():
return foo.c()
</code></pre>
<p>Now I can change <code>xyz()</code> to make it do anything that I want, and if I ever want to explicitly call <code>fo... | 0 | 2009-11-05T15:32:35Z | [
"python",
"automation",
"sed",
"rope"
] |
Updating MS Word (or Open Office) bookmarks with Python | 1,674,956 | <p>I'd like to fill MSWord bookmarks from a python script.
I can't find such functions in win32com(MSWord) or in PyUno(OpenOffice).</p>
<p>Does anyone know how to use bookmarks from Python?</p>
| 0 | 2009-11-04T16:24:56Z | 1,675,174 | <p>You don't find the functions in win32com, you find them in the documentation for the COM object that you are using. In this case that would be Word.Application. </p>
<p>You can see <a href="http://mail.python.org/pipermail/tutor/2002-January/011392.html" rel="nofollow">some sample Python code that uses this COM obj... | 1 | 2009-11-04T16:54:43Z | [
"python",
"pywin32",
"win32com",
"pyuno"
] |
Updating MS Word (or Open Office) bookmarks with Python | 1,674,956 | <p>I'd like to fill MSWord bookmarks from a python script.
I can't find such functions in win32com(MSWord) or in PyUno(OpenOffice).</p>
<p>Does anyone know how to use bookmarks from Python?</p>
| 0 | 2009-11-04T16:24:56Z | 25,030,219 | <p>here is some code i made:</p>
<pre><code>def addText(self, bookmark):
self.wordApp.ActiveDocument.Bookmarks(bookmark).Select()
self.wordApp.Selection.TypeText(self.some_text)
# from pandas data frame into word table
def addTable(self, bookmark, df):
self.wordApp.ActiveDocument.Bookmarks(bookmark).Sele... | 0 | 2014-07-30T06:38:07Z | [
"python",
"pywin32",
"win32com",
"pyuno"
] |
emails as email.Message class objects in Python | 1,674,962 | <p>How do I use poplib, and download mails as message instances from email.Message class from email module in Python?</p>
<p>I am writing a program, which analyzes, all emails for specific information, storing parts of the message into a database. I can download the entire mail as text, howver walking through text sea... | 1 | 2009-11-04T16:25:48Z | 1,675,012 | <p>use the <code>FeedParser</code> class in the <code>email.feedparser</code> module to construct an <code>email.Message</code> object from the messages read from the server with <code>poplib</code>.</p>
<p>specifically:</p>
<pre><code>import poplib
import email
pop = poplib.POP3( "server..." )
[establish connection... | 3 | 2009-11-04T16:33:17Z | [
"python",
"email",
"parsing",
"pop3"
] |
emails as email.Message class objects in Python | 1,674,962 | <p>How do I use poplib, and download mails as message instances from email.Message class from email module in Python?</p>
<p>I am writing a program, which analyzes, all emails for specific information, storing parts of the message into a database. I can download the entire mail as text, howver walking through text sea... | 1 | 2009-11-04T16:25:48Z | 5,051,792 | <p>Doesn't deal with character set issues like Adrien Plisson's answer.</p>
<pre><code>import poplib
import email
pop = poplib.POP3( "server..." )
[establish connection, authenticate, ...]
raw = pop.retr( 1 )
pop.close()
message = email.message_from_string('\n'.join(raw[1]))
</code></pre>
| 2 | 2011-02-19T15:58:48Z | [
"python",
"email",
"parsing",
"pop3"
] |
How to enforce unicode arguments for methods? | 1,675,154 | <p>I have a model class with getter and setter methods, and the occasional static methods. I would like to enforce the usage of unicode strings as arguments for specific methods and using decorators was the first idea I had. Now I have something like this:</p>
<pre><code>import types
class require_unicode(object):
... | 0 | 2009-11-04T16:51:45Z | 1,675,228 | <p>I think, your problem is with the @staticmethod decorator, not with your require_unicode decorator. Staticmethods, unlike classmethods don't receive the reference to the class as the first argument, so your argument signature is wrong.</p>
<p>You must either change do_another to be a @classmethod, or remove <code>s... | 1 | 2009-11-04T17:04:11Z | [
"python",
"unicode",
"arguments",
"decorator"
] |
How to enforce unicode arguments for methods? | 1,675,154 | <p>I have a model class with getter and setter methods, and the occasional static methods. I would like to enforce the usage of unicode strings as arguments for specific methods and using decorators was the first idea I had. Now I have something like this:</p>
<pre><code>import types
class require_unicode(object):
... | 0 | 2009-11-04T16:51:45Z | 1,675,353 | <p>I would think that this was unpythonic - you should never check the type of your arguments, but instead check that they have the necessary methods and attributes. The simplest way to do this is by assuming they are there and getting an exception otherwise, but I guess you could do <code>getattr</code> too. Just <e... | 0 | 2009-11-04T17:22:08Z | [
"python",
"unicode",
"arguments",
"decorator"
] |
How to enforce unicode arguments for methods? | 1,675,154 | <p>I have a model class with getter and setter methods, and the occasional static methods. I would like to enforce the usage of unicode strings as arguments for specific methods and using decorators was the first idea I had. Now I have something like this:</p>
<pre><code>import types
class require_unicode(object):
... | 0 | 2009-11-04T16:51:45Z | 1,677,614 | <p>Another option is to use assertions. It depends on whether passing a non-unicode type into your methods should be considered a programming error that should be evident during development.</p>
<pre><code>import types
class Foo:
def set_something(self, string):
assert isinstance(string, types.UnicodeType)... | 0 | 2009-11-05T00:28:28Z | [
"python",
"unicode",
"arguments",
"decorator"
] |
Get str repr with double quotes Python | 1,675,181 | <p>I'm using a small Python script to generate some binary data that will be used in a C header.</p>
<p>This data should be declared as a <code>char[]</code>, and it will be nice if it could be encoded as a string (with the pertinent escape sequences when they are not in the range of ASCII printable chars) to keep the... | 10 | 2009-11-04T16:55:27Z | 1,675,295 | <p>Better not hack the <code>repr()</code> but use the right encoding from the beginning. You can get the repr's encoding directly with the encoding <code>string_escape</code></p>
<pre><code>>>> "naïveté".encode("string_escape")
'na\\xc3\\xafvet\\xc3\\xa9'
>>> print _
na\xc3\xafvet\xc3\xa9
</code><... | 6 | 2009-11-04T17:12:34Z | [
"python",
"c",
"escaping"
] |
Get str repr with double quotes Python | 1,675,181 | <p>I'm using a small Python script to generate some binary data that will be used in a C header.</p>
<p>This data should be declared as a <code>char[]</code>, and it will be nice if it could be encoded as a string (with the pertinent escape sequences when they are not in the range of ASCII printable chars) to keep the... | 10 | 2009-11-04T16:55:27Z | 1,676,170 | <p>If you're asking a python <code>str</code> for its <code>repr</code>, I don't think the type of quote is really configurable. From the <code>PyString_Repr</code> function in the python 2.6.4 source tree:</p>
<pre><code> /* figure out which quote to use; single is preferred */
quote = '\'';
if (smartquotes &&... | 5 | 2009-11-04T19:49:46Z | [
"python",
"c",
"escaping"
] |
Get str repr with double quotes Python | 1,675,181 | <p>I'm using a small Python script to generate some binary data that will be used in a C header.</p>
<p>This data should be declared as a <code>char[]</code>, and it will be nice if it could be encoded as a string (with the pertinent escape sequences when they are not in the range of ASCII printable chars) to keep the... | 10 | 2009-11-04T16:55:27Z | 1,676,407 | <p>repr() isn't what you want. There's a fundamental problem: repr() can use any representation of the string that can be evaluated as Python to produce the string. That means, in theory, that it might decide to use any number of other constructs which wouldn't be valid in C, such as """long strings""".</p>
<p>This ... | 3 | 2009-11-04T20:29:30Z | [
"python",
"c",
"escaping"
] |
Fastest Way To Remove Duplicates In Lists Python | 1,675,321 | <p>I have two very large lists and to loop through it once takes at least a second and I need to do it 200,000 times. What's the fastest way to remove duplicates in two lists to form one?</p>
| 8 | 2009-11-04T17:17:36Z | 1,675,337 | <p>This is the fastest way I can think of:</p>
<pre><code>import itertools
output_list = list(set(itertools.chain(first_list, second_list)))
</code></pre>
<p>Slight update: As <a href="http://stackoverflow.com/questions/1675321/fastest-way-to-remove-duplicates-in-lists-python/1675380#1675380">jcd</a> points out, depe... | 18 | 2009-11-04T17:19:47Z | [
"python",
"list",
"sorting"
] |
Fastest Way To Remove Duplicates In Lists Python | 1,675,321 | <p>I have two very large lists and to loop through it once takes at least a second and I need to do it 200,000 times. What's the fastest way to remove duplicates in two lists to form one?</p>
| 8 | 2009-11-04T17:17:36Z | 1,675,344 | <pre><code>result = list(set(list1).union(set(list2)))
</code></pre>
<p>That's how I'd do it. I am not so sure about performance, though, but it is certainly better, than doing it by hand.</p>
| 3 | 2009-11-04T17:21:21Z | [
"python",
"list",
"sorting"
] |
Fastest Way To Remove Duplicates In Lists Python | 1,675,321 | <p>I have two very large lists and to loop through it once takes at least a second and I need to do it 200,000 times. What's the fastest way to remove duplicates in two lists to form one?</p>
| 8 | 2009-11-04T17:17:36Z | 1,675,357 | <p>As Daniel states, a set cannot contain duplicate entries - so concatenate the lists:</p>
<pre><code>list1 + list2
</code></pre>
<p>Then convert the new list to a set:</p>
<pre><code>set(list1 + list2)
</code></pre>
<p>Then back to a list:</p>
<pre><code>list(set(list1 + list2))
</code></pre>
| 7 | 2009-11-04T17:22:34Z | [
"python",
"list",
"sorting"
] |
Fastest Way To Remove Duplicates In Lists Python | 1,675,321 | <p>I have two very large lists and to loop through it once takes at least a second and I need to do it 200,000 times. What's the fastest way to remove duplicates in two lists to form one?</p>
| 8 | 2009-11-04T17:17:36Z | 1,675,380 | <p>I'd recommend something like this:</p>
<pre><code>def combine_lists(list1, list2):
s = set(list1)
s.update(list2)
return list(s)
</code></pre>
<p>This eliminates the problem of creating a monster list of the concatenation of the first two.</p>
<p>Depending on what you're doing with the output, don't b... | 10 | 2009-11-04T17:25:52Z | [
"python",
"list",
"sorting"
] |
Network analysis and adjacency matrices | 1,675,347 | <p>I want to try and create a network for several hundred shapefiles that consist of polylines. The polylines are snapped to each other and consistent. Then I want to create an adjacency matrix for this network.</p>
<p>What is the best way of doing this? I know how to do it on an individual basis by clicking through o... | 2 | 2009-11-04T17:21:45Z | 1,676,082 | <p>I don't know what exactly you want to achieve but when it comes to network analysis in python take a look at <a href="http://networkx.lanl.gov/" rel="nofollow">networkx</a>. </p>
| 2 | 2009-11-04T19:34:13Z | [
"python",
"vba",
"arcgis",
"esri",
"arcmap"
] |
Error while exiting cherrypy server | 1,675,441 | <p>Guys, I am getting following error while exiting cherrypy server. What is this error about?</p>
<pre><code>2009-11-04 09:32:35,015 WARNING Error in atexit._run_exitfuncs:
2009-11-04 09:32:35,015 WARNING
2009-11-04 09:32:35,015 WARNING Traceback (most recent call last):
2009-11-04 09:32:35,015 WARNING File ... | 0 | 2009-11-04T17:36:36Z | 1,677,316 | <p>You <em>probably</em> log to console and then close it.</p>
| 0 | 2009-11-04T23:08:46Z | [
"python",
"logging",
"cherrypy"
] |
Error while exiting cherrypy server | 1,675,441 | <p>Guys, I am getting following error while exiting cherrypy server. What is this error about?</p>
<pre><code>2009-11-04 09:32:35,015 WARNING Error in atexit._run_exitfuncs:
2009-11-04 09:32:35,015 WARNING
2009-11-04 09:32:35,015 WARNING Traceback (most recent call last):
2009-11-04 09:32:35,015 WARNING File ... | 0 | 2009-11-04T17:36:36Z | 1,680,149 | <p>You closed your log file before exiting. The logging shutdown code wants to flush the log file before exiting. What you see here looks like <a href="http://bugs.python.org/issue3126" rel="nofollow">bug #3126</a> in Python's logging module. It was fixed with:</p>
<blockquote>
<p>r64338 | vinay.sajip | 2008-06-17
... | 0 | 2009-11-05T12:07:13Z | [
"python",
"logging",
"cherrypy"
] |
Using Google AppEngine as a "cache" for personal websites (wordpress blogs, wikis) | 1,675,715 | <p>I read an article of an indie game developer who is using Google AppEngine to cache his main site and blog, to protect provide high-availability during traffic spikes (Digg, Slashdot effect).</p>
<p><a href="http://blog.wolfire.com/2009/03/google-app-engine-for-indie-developers/">Wolfire Blog - Google App Engine fo... | 7 | 2009-11-04T18:22:58Z | 1,675,884 | <p>You could start by taking the code for <a href="http://drydrop.binaryage.com/" rel="nofollow">DryDrop</a>, which mirrors static pages from a repository hosted on <a href="http://github.com/" rel="nofollow">GitHub</a>, and making it a more general reverse proxy. For example, you'd need to ensure that POST requests o... | 9 | 2009-11-04T18:58:22Z | [
"java",
"python",
"google-app-engine",
"caching"
] |
How do I create a namespace package in Python? | 1,675,734 | <p>In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads. For example, with the directories <code>Package-1</code> and <code>Package-2</code> in <code>PYTHONPATH</code>,</p>
<pre><code>Package-1/namespace/... | 89 | 2009-11-04T18:27:10Z | 1,675,780 | <p>You have your Python namespace concepts back to front, it is not possible in python to put packages into modules. Packages contain modules not the other way around.</p>
<p>A Python package is simply a folder containing a <code>__init__.py</code> file. A module is any other file in a package (or directly on the <cod... | -7 | 2009-11-04T18:35:20Z | [
"python",
"namespaces",
"package"
] |
How do I create a namespace package in Python? | 1,675,734 | <p>In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads. For example, with the directories <code>Package-1</code> and <code>Package-2</code> in <code>PYTHONPATH</code>,</p>
<pre><code>Package-1/namespace/... | 89 | 2009-11-04T18:27:10Z | 1,676,069 | <p>There's a standard module, called <a href="http://docs.python.org/library/pkgutil.html">pkgutil</a>, with which you
can 'append' modules to a given namespace.</p>
<p>With the directory structure you've provided:</p>
<pre><code>Package-1/namespace/__init__.py
Package-1/namespace/module1/__init__.py
Package-2/namesp... | 61 | 2009-11-04T19:32:17Z | [
"python",
"namespaces",
"package"
] |
How do I create a namespace package in Python? | 1,675,734 | <p>In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads. For example, with the directories <code>Package-1</code> and <code>Package-2</code> in <code>PYTHONPATH</code>,</p>
<pre><code>Package-1/namespace/... | 89 | 2009-11-04T18:27:10Z | 1,676,076 | <p><a href="http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages" rel="nofollow">This section should be pretty self-explanatory.</a></p>
<p>In short, put the namespace code in <code>__init__.py</code>, update <code>setup.py</code> to declare a namespace, and you are free to go.</p>
| 6 | 2009-11-04T19:33:51Z | [
"python",
"namespaces",
"package"
] |
How do I create a namespace package in Python? | 1,675,734 | <p>In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads. For example, with the directories <code>Package-1</code> and <code>Package-2</code> in <code>PYTHONPATH</code>,</p>
<pre><code>Package-1/namespace/... | 89 | 2009-11-04T18:27:10Z | 14,711,051 | <p>This is an old question, but someone recently commented on my blog that my posting about namespace packages was still relevant, so thought I would link to it here as it provides a practical example of how to make it go:</p>
<p><a href="http://cdent.tumblr.com/post/216241761/python-namespace-packages-for-tiddlyweb" ... | 2 | 2013-02-05T15:30:52Z | [
"python",
"namespaces",
"package"
] |
How do I create a namespace package in Python? | 1,675,734 | <p>In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads. For example, with the directories <code>Package-1</code> and <code>Package-2</code> in <code>PYTHONPATH</code>,</p>
<pre><code>Package-1/namespace/... | 89 | 2009-11-04T18:27:10Z | 27,586,272 | <p>TL;DR:</p>
<p>On Python 3.3 you don't have to do anything, just don't put any <code>__init__.py</code> in your namespace package directories and it will just work. On pre-3.3, choose the <code>pkgutil.extend_path()</code> solution over the <code>pkg_resources.declare_namespace()</code> one, because it's future-proo... | 24 | 2014-12-21T02:44:44Z | [
"python",
"namespaces",
"package"
] |
How to combine Pool.map with Array (shared memory) in Python multiprocessing? | 1,675,766 | <p>I have a very large (read only) array of data that I want to be processed by multiple processes in parallel.</p>
<p>I like the Pool.map function and would like to use it to calculate functions on that data in parallel.</p>
<p>I saw that one can use the Value or Array class to use shared memory data between process... | 28 | 2009-11-04T18:32:20Z | 1,676,328 | <p>If the data is read only just make it a variable in a module <strong>before</strong> the fork from Pool. Then all the child processes should be able to access it, and it won't be copied provided you don't write to it.</p>
<pre><code>import myglobals # anything (empty .py file)
myglobals.data = []
def count_it( key... | 0 | 2009-11-04T20:16:29Z | [
"python",
"multiprocessing",
"shared-memory",
"pool"
] |
How to combine Pool.map with Array (shared memory) in Python multiprocessing? | 1,675,766 | <p>I have a very large (read only) array of data that I want to be processed by multiple processes in parallel.</p>
<p>I like the Pool.map function and would like to use it to calculate functions on that data in parallel.</p>
<p>I saw that one can use the Value or Array class to use shared memory data between process... | 28 | 2009-11-04T18:32:20Z | 1,705,235 | <p>The problem I see is that Pool doesn't support pickling shared data through its argument list. That's what the error message means by "objects should only be shared between processes through inheritance". The shared data needs to be inherited, i.e., global if you want to share it using the Pool class.</p>
<p>If y... | 4 | 2009-11-10T02:08:28Z | [
"python",
"multiprocessing",
"shared-memory",
"pool"
] |
How to combine Pool.map with Array (shared memory) in Python multiprocessing? | 1,675,766 | <p>I have a very large (read only) array of data that I want to be processed by multiple processes in parallel.</p>
<p>I like the Pool.map function and would like to use it to calculate functions on that data in parallel.</p>
<p>I saw that one can use the Value or Array class to use shared memory data between process... | 28 | 2009-11-04T18:32:20Z | 1,721,911 | <p>Trying again as I just saw the bounty ;)</p>
<p>Basically I think the error message means what it said - multiprocessing shared memory Arrays can't be passed as arguments (by pickling). It doesn't make sense to serialise the data - the point is the data is shared memory. So you have to make the shared array global.... | 27 | 2009-11-12T12:39:03Z | [
"python",
"multiprocessing",
"shared-memory",
"pool"
] |
How to combine Pool.map with Array (shared memory) in Python multiprocessing? | 1,675,766 | <p>I have a very large (read only) array of data that I want to be processed by multiple processes in parallel.</p>
<p>I like the Pool.map function and would like to use it to calculate functions on that data in parallel.</p>
<p>I saw that one can use the Value or Array class to use shared memory data between process... | 28 | 2009-11-04T18:32:20Z | 1,723,020 | <blockquote>
<p><a href="http://docs.python.org/library/multiprocessing.html#module-multiprocessing.sharedctypes" rel="nofollow">The <code>multiprocessing.sharedctypes</code> module provides functions for allocating ctypes objects from shared memory which can be inherited by child processes.</a></p>
</blockquote>
<p... | -1 | 2009-11-12T15:26:50Z | [
"python",
"multiprocessing",
"shared-memory",
"pool"
] |
Comparing elements in a list in Python's for -loop | 1,675,860 | <p><strong>What is wrong in the method <code>end</code> in the code?</strong></p>
<p>The method <code>end</code> returns always <em>1</em> although it should return <em>0</em> with the current data.</p>
<pre><code># return 1 if the sum of four consecutive elements equal the sum over other sum of the other three sums
... | 1 | 2009-11-04T18:52:41Z | 1,675,896 | <p>I can't really tell what you're trying to do here, but I can certainly say why <code>end()</code> returns 1 instead of 0. In your last <code>for</code> loop, you reset <code>summat</code> to <code>[]</code> at the start of the loop, so at the end, summat only contains a single value (the one you most recently append... | 2 | 2009-11-04T19:00:14Z | [
"python",
"list"
] |
Comparing elements in a list in Python's for -loop | 1,675,860 | <p><strong>What is wrong in the method <code>end</code> in the code?</strong></p>
<p>The method <code>end</code> returns always <em>1</em> although it should return <em>0</em> with the current data.</p>
<pre><code># return 1 if the sum of four consecutive elements equal the sum over other sum of the other three sums
... | 1 | 2009-11-04T18:52:41Z | 1,675,915 | <p>I think you may have an off-by-one error. Remember that array indexes in Python start at 0, not 1. So where you do this:</p>
<pre><code> for i in summat[2:5]:
print "Comparing: ", summat[1], " and ", i, ".\n"
if summat[1] != i:
equality = 0
</code></pre>
<p>you are not... | 2 | 2009-11-04T19:02:41Z | [
"python",
"list"
] |
Comparing elements in a list in Python's for -loop | 1,675,860 | <p><strong>What is wrong in the method <code>end</code> in the code?</strong></p>
<p>The method <code>end</code> returns always <em>1</em> although it should return <em>0</em> with the current data.</p>
<pre><code># return 1 if the sum of four consecutive elements equal the sum over other sum of the other three sums
... | 1 | 2009-11-04T18:52:41Z | 1,676,376 | <p>First off, <code>end</code> doesn't return 1. It returns <code>None</code>. It <em>prints</em> 1. Kind of deceptive if you're running it from the command line.</p>
<p>Second, when you call <code>end</code>, <code>summat</code> is equal to <code>[34]</code>. So this:</p>
<pre><code>for i in summat[2:5]:
</code>... | 1 | 2009-11-04T20:22:27Z | [
"python",
"list"
] |
Comparing elements in a list in Python's for -loop | 1,675,860 | <p><strong>What is wrong in the method <code>end</code> in the code?</strong></p>
<p>The method <code>end</code> returns always <em>1</em> although it should return <em>0</em> with the current data.</p>
<pre><code># return 1 if the sum of four consecutive elements equal the sum over other sum of the other three sums
... | 1 | 2009-11-04T18:52:41Z | 1,676,791 | <p>You have two problems. Initialising summat to [] inside the loop, also the off by one error Greg mentioned</p>
<pre><code>data = "2|15|14|4|12|6|7|9|8|10|11|5|13|3|2|16"
arra = data.split("|");
def do_row ( arra, n ):
return arra[4*n:4 + 4*n]
def row_summa (row):
return sum(map(int,row))
def end ... | 1 | 2009-11-04T21:36:21Z | [
"python",
"list"
] |
Comparing elements in a list in Python's for -loop | 1,675,860 | <p><strong>What is wrong in the method <code>end</code> in the code?</strong></p>
<p>The method <code>end</code> returns always <em>1</em> although it should return <em>0</em> with the current data.</p>
<pre><code># return 1 if the sum of four consecutive elements equal the sum over other sum of the other three sums
... | 1 | 2009-11-04T18:52:41Z | 1,676,819 | <p>You should also study this piece of code</p>
<pre><code>data = "2|15|14|4|12|6|7|9|8|10|11|5|13|3|2|16"
arra = map(int,data.split("|"))
summat = [sum(arra[i:i+4]) for i in range(0,len(arra),4)]
print summat
print len(set(summat))==1
</code></pre>
| 1 | 2009-11-04T21:40:34Z | [
"python",
"list"
] |
Computing article abstracts | 1,675,943 | <p>I'm looking for a way to automatically produce an abstract, basically the first few sentances/paragraphs of a blog entry, to display in a list of articles (which are written in markdown). Currently, I'm doing something like this:</p>
<pre><code>def abstract(article, paras=3):
return '\n'.join(article.split('\n... | 5 | 2009-11-04T19:09:01Z | 1,675,995 | <p>I'm not exactly sure of what you want.</p>
<p>However, I would suggest cutting the article after X characters and put "...". Then you have more control over the size of your "abstract" (if that's what bothers you in your current implementation).</p>
| 0 | 2009-11-04T19:18:46Z | [
"python",
"markdown"
] |
Computing article abstracts | 1,675,943 | <p>I'm looking for a way to automatically produce an abstract, basically the first few sentances/paragraphs of a blog entry, to display in a list of articles (which are written in markdown). Currently, I'm doing something like this:</p>
<pre><code>def abstract(article, paras=3):
return '\n'.join(article.split('\n... | 5 | 2009-11-04T19:09:01Z | 1,676,000 | <p><strong>EDIT:</strong></p>
<p>You can do something like this:</p>
<pre><code>from textwrap import wrap
def getAbstract(text, lines=5, screenwidth=100):
width = len(' '.join([
line for block in text.splitlines()
for line in wrap(block, width=screenwidth)
][:lines]))
... | 7 | 2009-11-04T19:19:31Z | [
"python",
"markdown"
] |
Feedparser - retrieve old messages from Google Reader | 1,676,223 | <p>I'm using the feedparser library in python to retrieve news from a local newspaper (my intent is to do Natural Language Processing over this corpus) and would like to be able to retrieve many past entries from the RSS feed.</p>
<p>I'm not very acquainted with the technical issues of RSS, but I think this should be ... | 4 | 2009-11-04T19:58:04Z | 1,676,258 | <p>You're only getting a dozen entries or so because that's what the feed contains. If you want historic data you will have to find a feed/database of said data.</p>
<p>Check out this <a href="http://www.readwriteweb.com/archives/where%5Fto%5Ffind%5Fopen%5Fdata%5Fon%5Fthe.php">ReadWriteWeb article</a> for some resourc... | 8 | 2009-11-04T20:02:51Z | [
"python",
"rss",
"google-reader",
"feedparser"
] |
Feedparser - retrieve old messages from Google Reader | 1,676,223 | <p>I'm using the feedparser library in python to retrieve news from a local newspaper (my intent is to do Natural Language Processing over this corpus) and would like to be able to retrieve many past entries from the RSS feed.</p>
<p>I'm not very acquainted with the technical issues of RSS, but I think this should be ... | 4 | 2009-11-04T19:58:04Z | 1,676,311 | <p>To expand on Bartek's answer: You could also start storing all of the entries in the feed that you've already seen, and build up your own historical archive of the feed's content. This would delay your ability to start using it as a corpus (because you'd have to do this for a month to build up a collection of a mo... | 3 | 2009-11-04T20:13:56Z | [
"python",
"rss",
"google-reader",
"feedparser"
] |
Can you only communicate once with a subprocess? | 1,676,340 | <p><a href="http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate" rel="nofollow"><code>communicate</code></a>'s documentation says: </p>
<blockquote>
<p>Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.</p>... | 4 | 2009-11-04T20:17:51Z | 1,676,375 | <p>Then you can't use <code>.communicate()</code>. You can either poll the streams, use <code>select</code> or some other way that allows you to listen to FD changes (both gtk and Qt have tools for that, for example).</p>
| 3 | 2009-11-04T20:22:17Z | [
"python",
"subprocess"
] |
Can you only communicate once with a subprocess? | 1,676,340 | <p><a href="http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate" rel="nofollow"><code>communicate</code></a>'s documentation says: </p>
<blockquote>
<p>Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.</p>... | 4 | 2009-11-04T20:17:51Z | 1,676,397 | <p>Take a look at <a href="http://pymotw.com/2/subprocess/index.html#module-subprocess" rel="nofollow">Doug Hellman's Python Module of the Week writeup about subprocess</a>. Search down until you see "repeater.py".</p>
<p>There you will find an example of how to send and receive input/output to a process.</p>
| 3 | 2009-11-04T20:28:12Z | [
"python",
"subprocess"
] |
Can you only communicate once with a subprocess? | 1,676,340 | <p><a href="http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate" rel="nofollow"><code>communicate</code></a>'s documentation says: </p>
<blockquote>
<p>Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.</p>... | 4 | 2009-11-04T20:17:51Z | 1,676,715 | <p>Don't use <code>subprocess</code> for this, you'll enter a world of pain to do with buffering.</p>
<p>I recommend <a href="http://www.noah.org/wiki/Pexpect" rel="nofollow">pexpect</a> for this purpose - it works very well. Unfortunately it doesn't work under windows at the moment, though I did hear about a port (w... | 2 | 2009-11-04T21:24:18Z | [
"python",
"subprocess"
] |
Can you only communicate once with a subprocess? | 1,676,340 | <p><a href="http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate" rel="nofollow"><code>communicate</code></a>'s documentation says: </p>
<blockquote>
<p>Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.</p>... | 4 | 2009-11-04T20:17:51Z | 1,677,923 | <p>Here's a module I wrote. Make sure to use that -u argument to avoid buffering problems:</p>
<pre><code>import os
import pickle
import subprocess
from subprocess import PIPE
import struct
import builtins
def call_thru_stream(stream,funcname,*args,**kwargs):
"""Used for calling a function through a stream and no ... | 1 | 2009-11-05T02:15:31Z | [
"python",
"subprocess"
] |
How to pass flag to gcc in Python setup.py script? | 1,676,384 | <p>I'm writing a Python extension in C that requires the CoreFoundation framework (among other things). This compiles fine with:</p>
<pre><code>gcc -o foo foo.c -framework CoreFoundation -framework Python
</code></pre>
<p>("-framework" is an Apple-only gcc extension, but that's okay because I'm using their specific f... | 14 | 2009-11-04T20:24:05Z | 1,676,413 | <p>Maybe you need to set <code>extra_link_args</code>, too? <code>extra_compile_args</code> is used when compiling the source code, <code>extra_link_args</code> when linking the result.</p>
| 11 | 2009-11-04T20:30:52Z | [
"python",
"distutils",
"python-c-api"
] |
Pyfacebook from buildout | 1,676,520 | <p>What is the best way to install the latest version of pyfacebook with buildout? The package is hosted on github and is not on pypi. This system doesn't have git installed, so a git-based recipe isn't unfortunately not an option. The github URL is <a href="http://github.com/sciyoshi/pyfacebook" rel="nofollow">http://... | 3 | 2009-11-04T20:51:31Z | 1,676,755 | <p>You can add any python package hosted on git-hub by adding a find-links url pointing to the project tarball URL plus a <code>#egg=packagename</code> postfix. For pyfacebook that is:</p>
<pre><code>http://github.com/sciyoshi/pyfacebook/tarball/master#egg=pyfacebook
</code></pre>
<p>So a simple buildout would be:</p... | 5 | 2009-11-04T21:30:18Z | [
"python",
"facebook",
"github",
"buildout",
"pypi"
] |
Trying to upgrade Python to 3.0 on Mac OS 10.5.8 | 1,676,831 | <p>I'm having some problems upgrading Python on my Mac. For my first attempt, I downloaded and installed the 2.6.4 dmg MacPython installer from <a href="http://python.org/download/mac/" rel="nofollow">http://python.org/download/mac/</a>. This did install 2.6.4, and when I ran 'python' from the terminal it says that ver... | 1 | 2009-11-04T21:41:46Z | 1,676,962 | <p>First, <code>/usr/bin/python</code> should always point to the Apple-supplied python and on 10.5 that means python2.5. Don't change this!</p>
<p>When you installed the python.org python2.6, by default it installs symlinks in <code>/usr/local/bin/</code> so one way to invoke it is <code>/usr/local/bin/python2.6</co... | 4 | 2009-11-04T22:00:50Z | [
"python",
"osx"
] |
Python: How do I get a reference to a module inside the module itself? | 1,676,835 | <p>How can I get a reference to a module from within that module? Also, how can I get a reference to the package containing that module?</p>
| 82 | 2009-11-04T21:42:33Z | 1,676,860 | <pre><code>import sys
current_module = sys.modules[__name__]
</code></pre>
| 123 | 2009-11-04T21:45:59Z | [
"python",
"self-reference"
] |
Python: How do I get a reference to a module inside the module itself? | 1,676,835 | <p>How can I get a reference to a module from within that module? Also, how can I get a reference to the package containing that module?</p>
| 82 | 2009-11-04T21:42:33Z | 1,676,861 | <p>If you have a class in that module, then the <code>__module__</code> property of the class is the module name of the class. Thus you can access the module via <code>sys.modules[klass.__module__]</code>. This is also works for functions.</p>
| 10 | 2009-11-04T21:46:01Z | [
"python",
"self-reference"
] |
Python: How do I get a reference to a module inside the module itself? | 1,676,835 | <p>How can I get a reference to a module from within that module? Also, how can I get a reference to the package containing that module?</p>
| 82 | 2009-11-04T21:42:33Z | 1,676,868 | <p>You can get the name of the current module using <code>__name__</code></p>
<p>The module reference can be found in the <code>sys.modules</code> dictionary.</p>
<p>See the Python <a href="http://docs.python.org/tutorial/modules.html?highlight=%5F%5Fname%5F%5F" rel="nofollow">documentation</a></p>
| 5 | 2009-11-04T21:46:41Z | [
"python",
"self-reference"
] |
how to change default dateformat AAAA-MM-DD to dd/mm/yyyy in Django? | 1,677,035 | <p>My locale dateformat is dd/mm/yyyy and Django is requering AAAA-MM-DD</p>
<p>when the user inputs 04/11/2009 Django raises:</p>
<pre><code>_('Enter a valid date in YYYY-MM-DD format.'))
</code></pre>
<p>Im using simple html input field type="text", Not forms.DateField</p>
<p>Thanks :)</p>
| 0 | 2009-11-04T22:12:40Z | 1,677,064 | <p>I'm assuming you are using a <code>forms.DateField</code>. If so, you need to supply a <code>input_formats</code> argument like so:</p>
<pre><code>class MyForm(forms.Form):
start_date = forms.DateField(input_formats=['%d/%m/%Y'])
</code></pre>
<p>input_formats just takes a list of the formats you want to accep... | 5 | 2009-11-04T22:17:58Z | [
"python",
"django",
"date-format"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.