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
listlengths
1
5
How to get the signed integer value of a long in python?
1,375,897
<p>If lv stores a long value, and the machine is 32 bits, the following code: </p> <pre><code>iv = int(lv &amp; 0xffffffff) </code></pre> <p>results an iv of type long, instead of the machine's int. </p> <p>How can I get the (signed) int value in this case?</p>
12
2009-09-03T20:54:38Z
37,095,855
<p>Essentially, the problem is to sign extend from 32 bits to... an infinite number of bits, because Python has arbitrarily large integers. Normally, sign extension is done automatically by CPU instructions when casting, so it's interesting that this is harder in Python than it would be in, say, C.</p> <p>By playing a...
0
2016-05-08T03:17:02Z
[ "python", "unsigned", "signed" ]
How to get the signed integer value of a long in python?
1,375,897
<p>If lv stores a long value, and the machine is 32 bits, the following code: </p> <pre><code>iv = int(lv &amp; 0xffffffff) </code></pre> <p>results an iv of type long, instead of the machine's int. </p> <p>How can I get the (signed) int value in this case?</p>
12
2009-09-03T20:54:38Z
38,064,259
<p>A quick and dirty solution (x is never greater than 32-bit in my case). </p> <pre><code>if x &gt; 0x7fffffff: x = x - 4294967296 </code></pre>
1
2016-06-27T22:34:28Z
[ "python", "unsigned", "signed" ]
In python, how does one test if a string-like object is mutable?
1,375,936
<p>I have a function that takes a string-like argument.</p> <p>I want to decide if I can safely store the argument and be sure that it won't change. So I'd like to test if it's mutable, e.g the result of a <code>buffer()</code> built from an <code>array.array()</code>, or not.</p> <p>Currently I use:</p> <pre><code>...
5
2009-09-03T21:02:40Z
1,375,970
<p>Just use <a href="http://en.wikipedia.org/wiki/Duck%5Ftyping" rel="nofollow">duck typing</a> -- remember, it's "Easier to Ask for Forgiveness Than Permission". Try to mutate the string-like object, and be prepared to catch an exception if you can't.</p>
3
2009-09-03T21:07:59Z
[ "python" ]
In python, how does one test if a string-like object is mutable?
1,375,936
<p>I have a function that takes a string-like argument.</p> <p>I want to decide if I can safely store the argument and be sure that it won't change. So I'd like to test if it's mutable, e.g the result of a <code>buffer()</code> built from an <code>array.array()</code>, or not.</p> <p>Currently I use:</p> <pre><code>...
5
2009-09-03T21:02:40Z
1,376,014
<p>It would be better to use</p> <pre><code>isinstance(s, basestring) </code></pre> <p>It works for Unicode strings too.</p>
5
2009-09-03T21:16:51Z
[ "python" ]
In python, how does one test if a string-like object is mutable?
1,375,936
<p>I have a function that takes a string-like argument.</p> <p>I want to decide if I can safely store the argument and be sure that it won't change. So I'd like to test if it's mutable, e.g the result of a <code>buffer()</code> built from an <code>array.array()</code>, or not.</p> <p>Currently I use:</p> <pre><code>...
5
2009-09-03T21:02:40Z
1,376,060
<p>I'd just convert it to an immutable string:</p> <pre><code>&gt;&gt;&gt; s1 = "possibly mutable" &gt;&gt;&gt; &gt;&gt;&gt; s2 = str(s1) &gt;&gt;&gt; s1 is s2 True </code></pre> <p>In case <code>s1</code> is immutable the same object is given back, resulting in no memory overhead. If it's mutable a copy is being ma...
3
2009-09-03T21:25:29Z
[ "python" ]
In python, how does one test if a string-like object is mutable?
1,375,936
<p>I have a function that takes a string-like argument.</p> <p>I want to decide if I can safely store the argument and be sure that it won't change. So I'd like to test if it's mutable, e.g the result of a <code>buffer()</code> built from an <code>array.array()</code>, or not.</p> <p>Currently I use:</p> <pre><code>...
5
2009-09-03T21:02:40Z
1,376,326
<p>If it's just a <em>heuristic</em> for your caching, just use whatever works. isinstance(x, str), for example, almost exactly like now. (Given you want to decide whether to cache or not; a False-bearing test just means a cache miss, you don't do anything wrong.)</p> <p><hr /></p> <p>(Remark: It turns out that buffe...
4
2009-09-03T22:35:21Z
[ "python" ]
Python subprocess with heredocs
1,376,016
<p>I was playing around with Python's subprocess module, trying a few examples but I can't seem to get heredoc statements to work.</p> <p>Here is the trivial example I was playing with:</p> <pre><code>import subprocess a = "A String of Text" p = subprocess.Popen(["cat", "&lt;&lt;DATA\n" + a + "\nDATA"]) </code></pre>...
2
2009-09-03T21:17:29Z
1,376,090
<p>The shell "heredoc" support is a shell feature. <code>subprocess.Popen</code> does not run your command through the shell by default, so this syntax certainly won't work.</p> <p>However, since you're using pipes anyway, there isn't any need to use the heredoc support of the shell. Just write your string <code>a</co...
4
2009-09-03T21:33:35Z
[ "python", "subprocess", "heredoc" ]
Python subprocess with heredocs
1,376,016
<p>I was playing around with Python's subprocess module, trying a few examples but I can't seem to get heredoc statements to work.</p> <p>Here is the trivial example I was playing with:</p> <pre><code>import subprocess a = "A String of Text" p = subprocess.Popen(["cat", "&lt;&lt;DATA\n" + a + "\nDATA"]) </code></pre>...
2
2009-09-03T21:17:29Z
1,376,105
<p>You're passing shell syntax as an arguments to <code>cat</code> program. You can try to do it like that:</p> <pre><code>p = subprocess.Popen(["sh", "-c", "cat &lt;&lt;DATA\n" + a + "\nDATA"]) </code></pre> <p>But the concept itself is wrong. You should use Python features instead of calling shell scripts inside yo...
2
2009-09-03T21:37:49Z
[ "python", "subprocess", "heredoc" ]
Python subprocess with heredocs
1,376,016
<p>I was playing around with Python's subprocess module, trying a few examples but I can't seem to get heredoc statements to work.</p> <p>Here is the trivial example I was playing with:</p> <pre><code>import subprocess a = "A String of Text" p = subprocess.Popen(["cat", "&lt;&lt;DATA\n" + a + "\nDATA"]) </code></pre>...
2
2009-09-03T21:17:29Z
1,376,180
<p>As others have pointed out, you need to run it in a shell. Popen makes this easy with a shell=True argument. I get the following output:</p> <pre><code>&gt;&gt;&gt; import subprocess &gt;&gt;&gt; a = "A String of Text" &gt;&gt;&gt; p = subprocess.Popen("cat &lt;&lt;DATA\n" + a + "\nDATA", shell=True) &gt;&gt;&gt; A...
3
2009-09-03T21:58:11Z
[ "python", "subprocess", "heredoc" ]
Locking PC in Python on Ubuntu
1,376,232
<p>i'm doing application that locks the PC using pyGtk, but i have a problem, when i click on the ok button the function of the button should get the time from the textbox, hide the window then sleep for a while, and at last lock the pc using a bash command. but it just don't hide.</p> <p>and here is the <a href="http...
2
2009-09-03T22:08:49Z
1,379,694
<p>Provided you are using Gnome on Ubuntu </p> <pre><code>import os os.system('gnome-screensaver-command –-lock') </code></pre>
3
2009-09-04T14:41:06Z
[ "python", "pygtk" ]
Locking PC in Python on Ubuntu
1,376,232
<p>i'm doing application that locks the PC using pyGtk, but i have a problem, when i click on the ok button the function of the button should get the time from the textbox, hide the window then sleep for a while, and at last lock the pc using a bash command. but it just don't hide.</p> <p>and here is the <a href="http...
2
2009-09-03T22:08:49Z
1,384,252
<p>Is there any reason for the main class to be a thread? I would make it just a normal class, which would be a lot easier to debug. The reason its not working is that all gtk related stuff must happen in the gtk thread, so do all widget method calls like this: <code>gobject.idle_add(widget.method_name)</code>. So to h...
1
2009-09-05T20:59:12Z
[ "python", "pygtk" ]
Web/Screen Scraping with Google App Engine - Code works in python interpreter but not GAE
1,376,377
<p>I want to do some web scraping with GAE. (Infinite Campus Student Information Portal, fyi). This service requires you to login to get in the website. I had some code that worked using mechanize in normal python. When I learned that I couldn't use mechanize in Google App Engine I ended up using urllib2 + ClientForm. ...
3
2009-09-03T22:52:46Z
1,376,630
<p>App Engine does not <em>strip out</em> the Host header: it forces it to be an accurate value based on the URI you are requesting. Assuming that URI's absolute, the server isn't even allowed to consider the Host header anyway, per <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.2" rel="nofollow">...
2
2009-09-04T00:14:50Z
[ "python", "google-app-engine", "screen-scraping" ]
How to make a repeating generator in Python
1,376,438
<p>How do you make a repeating generator, like xrange, in Python? For instance, if I do:</p> <pre><code>&gt;&gt;&gt; m = xrange(5) &gt;&gt;&gt; print list(m) &gt;&gt;&gt; print list(m) </code></pre> <p>I get the same result both times — the numbers 0..4. However, if I try the same with yield:</p> <pre><code>&gt;&g...
12
2009-09-03T23:12:33Z
1,376,497
<p>I think the answer to that is "No". I'm possibly wrong. It may be that with some of the funky new things you can do with generators in 2.6 involving arguments and exception handling that would allow something like what you want. But those features are mostly intended for implementing semi-continuations.</p> <p>W...
0
2009-09-03T23:29:13Z
[ "python" ]
How to make a repeating generator in Python
1,376,438
<p>How do you make a repeating generator, like xrange, in Python? For instance, if I do:</p> <pre><code>&gt;&gt;&gt; m = xrange(5) &gt;&gt;&gt; print list(m) &gt;&gt;&gt; print list(m) </code></pre> <p>I get the same result both times — the numbers 0..4. However, if I try the same with yield:</p> <pre><code>&gt;&g...
12
2009-09-03T23:12:33Z
1,376,531
<p>Not directly. Part of the flexibility that allows generators to be used for implementing co-routines, resource management, etc, is that they are always one-shot. Once run, a generator cannot be re-run. You would have to create a new generator object.</p> <p>However, you can create your own class which overrides <co...
14
2009-09-03T23:40:37Z
[ "python" ]
How to make a repeating generator in Python
1,376,438
<p>How do you make a repeating generator, like xrange, in Python? For instance, if I do:</p> <pre><code>&gt;&gt;&gt; m = xrange(5) &gt;&gt;&gt; print list(m) &gt;&gt;&gt; print list(m) </code></pre> <p>I get the same result both times — the numbers 0..4. However, if I try the same with yield:</p> <pre><code>&gt;&g...
12
2009-09-03T23:12:33Z
1,985,733
<p>If you write a lot of these, John Millikin's answer is the cleanest it gets.</p> <p>But if you don't mind adding 3 lines and some indentation, you can do it without a custom decorator. This composes 2 tricks:</p> <ol> <li><p>[Generally useful:] You can easily make a class iterable without implementing <code>.nex...
1
2009-12-31T14:59:47Z
[ "python" ]
How to make a repeating generator in Python
1,376,438
<p>How do you make a repeating generator, like xrange, in Python? For instance, if I do:</p> <pre><code>&gt;&gt;&gt; m = xrange(5) &gt;&gt;&gt; print list(m) &gt;&gt;&gt; print list(m) </code></pre> <p>I get the same result both times — the numbers 0..4. However, if I try the same with yield:</p> <pre><code>&gt;&g...
12
2009-09-03T23:12:33Z
19,875,981
<p>use this solution:</p> <pre><code>&gt;&gt;&gt; myxrange_ = lambda x: myxrange(x) &gt;&gt;&gt; print list(myxrange_(5)) ... [0, 1, 2, 3, 4] &gt;&gt;&gt; print list(myxrange_(5)) ... [0, 1, 2, 3, 4] &gt;&gt;&gt; for number in myxrange_(5): ... print number ... 0 1 2 3 4 &gt;&gt;&gt; </code><...
-1
2013-11-09T12:32:45Z
[ "python" ]
Python : email sending failing on SSL read
1,376,450
<p>I keep getting this intermittent error when trying to send through 'smtp.gmail.com'. </p> <pre><code>Traceback (most recent call last): File "/var/home/ptarjan/django/mysite/django/core/handlers/base.py", line 92, in get_response response = callback(request, *callback_args, **callback_kwargs) File "/var/h...
1
2009-09-03T23:15:54Z
1,376,862
<p>Looks like gmail may simply be occasionally slow to respond, so your operation times out. Perhaps you can use a try/except to catch such issues and retry a few times (maybe waiting a while between attempts). BTW, this task seems well suited to a dedicated or pooled thread which encapsulates the whole "send mail wit...
0
2009-09-04T01:53:26Z
[ "python", "django", "email", "smtp", "ssl" ]
Python : email sending failing on SSL read
1,376,450
<p>I keep getting this intermittent error when trying to send through 'smtp.gmail.com'. </p> <pre><code>Traceback (most recent call last): File "/var/home/ptarjan/django/mysite/django/core/handlers/base.py", line 92, in get_response response = callback(request, *callback_args, **callback_kwargs) File "/var/h...
1
2009-09-03T23:15:54Z
1,378,565
<p>Altho' I don't know why, I <strong>have been thro' this</strong>, and it works when you have settings variables ordered in a particular order:</p> <ul> <li>EMAIL_HOST</li> <li>EMAIL_PORT</li> <li>EMAIL_HOST_USER</li> <li>EMAIL_HOST_PASSWORD</li> <li>EMAIL_USE_TLS</li> </ul>
3
2009-09-04T11:03:10Z
[ "python", "django", "email", "smtp", "ssl" ]
pyobjc indexed accessor method with range
1,376,463
<p>I'm trying to implement an indexed accessor method for my model class in Python, as per <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/AccessorConventions.html" rel="nofollow">the KVC guide</a>. I want to use the optional ranged method, to load multiple objects...
3
2009-09-03T23:19:51Z
5,336,824
<p>You were close. The correct decorator for this method is:</p> <pre><code>@objc.signature('v@:o^@{_NSRange=QQ}') </code></pre> <p><code>NSRange</code> is not an object, but a struct, and can't be specified simply as <code>@</code>; you need to include the members<sup>1</sup>.</p> <p>Unfortunately, this is not the ...
2
2011-03-17T08:54:04Z
[ "python", "cocoa", "pyobjc", "key-value-coding" ]
How to extract a string between 2 other strings in python?
1,376,640
<p>Like if I have a string like <code>str1 = "IWantToMasterPython"</code></p> <p>If I want to extract <code>"Py"</code> from the above string. I write:</p> <pre><code>extractedString = foo("Master","thon") </code></pre> <p>I want to do all this because i am trying to extract lyrics from an html page. The lyrics are ...
16
2009-09-04T00:17:29Z
1,376,658
<p>The solution is to use a regexp:</p> <pre><code>import re r = re.compile('Master(.*?)thon') m = r.search(str1) if m: lyrics = m.group(1) </code></pre>
27
2009-09-04T00:23:53Z
[ "python", "string" ]
How to extract a string between 2 other strings in python?
1,376,640
<p>Like if I have a string like <code>str1 = "IWantToMasterPython"</code></p> <p>If I want to extract <code>"Py"</code> from the above string. I write:</p> <pre><code>extractedString = foo("Master","thon") </code></pre> <p>I want to do all this because i am trying to extract lyrics from an html page. The lyrics are ...
16
2009-09-04T00:17:29Z
1,376,661
<pre><code>def foo(s, leader, trailer): end_of_leader = s.index(leader) + len(leader) start_of_trailer = s.index(trailer, end_of_leader) return s[end_of_leader:start_of_trailer] </code></pre> <p>this raises ValueError if the leader is not present in string s, or the trailer is not present after that (you have no...
8
2009-09-04T00:24:59Z
[ "python", "string" ]
How to extract a string between 2 other strings in python?
1,376,640
<p>Like if I have a string like <code>str1 = "IWantToMasterPython"</code></p> <p>If I want to extract <code>"Py"</code> from the above string. I write:</p> <pre><code>extractedString = foo("Master","thon") </code></pre> <p>I want to do all this because i am trying to extract lyrics from an html page. The lyrics are ...
16
2009-09-04T00:17:29Z
1,378,517
<p>If you're extracting any data from a html page, I'd strongly suggest using <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a> library. I used it also for extracting data from html and it works great.</p>
2
2009-09-04T10:51:25Z
[ "python", "string" ]
How to extract a string between 2 other strings in python?
1,376,640
<p>Like if I have a string like <code>str1 = "IWantToMasterPython"</code></p> <p>If I want to extract <code>"Py"</code> from the above string. I write:</p> <pre><code>extractedString = foo("Master","thon") </code></pre> <p>I want to do all this because i am trying to extract lyrics from an html page. The lyrics are ...
16
2009-09-04T00:17:29Z
1,380,182
<p>BeautifulSoup is the easiest way to do what you want. It can be installed like:</p> <pre><code>sudo easy_install beautifulsoup </code></pre> <p>The sample code to do what you want is:</p> <pre><code>from BeautifulSoup import BeautifulSoup doc = ['&lt;div class="lyricbox"&gt;Hey You&lt;/div&gt;'] soup = Beautiful...
10
2009-09-04T16:09:02Z
[ "python", "string" ]
How to extract a string between 2 other strings in python?
1,376,640
<p>Like if I have a string like <code>str1 = "IWantToMasterPython"</code></p> <p>If I want to extract <code>"Py"</code> from the above string. I write:</p> <pre><code>extractedString = foo("Master","thon") </code></pre> <p>I want to do all this because i am trying to extract lyrics from an html page. The lyrics are ...
16
2009-09-04T00:17:29Z
14,728,237
<p>You can also try this if your would like all the occurrences output in a list :</p> <pre><code>import re str1 = "IWantToMasterPython" out = re.compile('Master(.*?)thon', re.DOTALL | re.IGNORECASE).findall(str1) if out : print out </code></pre>
0
2013-02-06T11:43:55Z
[ "python", "string" ]
PyParsing simple language expressions
1,376,716
<p>I'm trying to write something that will parse some code. I'm able to successfully parse <code>foo(spam)</code> and <code>spam+eggs</code>, but <code>foo(spam+eggs)</code> (recursive descent? my terminology from compilers is a bit rusty) fails.</p> <p>I have the following code:</p> <pre><code>from pyparsing_py3 imp...
2
2009-09-04T00:46:21Z
1,376,851
<p>Several issues: delimitedList is looking for a comma-delimited list of myVal, i.e. identifiers, as the only acceptable form of argument list, so of course it can't match 'foo+bar' (not a comma-delimited list of myVal!); fixing that reveals another -- myVal and myFunction start the same way so their order in mySubExp...
4
2009-09-04T01:47:33Z
[ "python", "parsing", "pyparsing" ]
PyParsing simple language expressions
1,376,716
<p>I'm trying to write something that will parse some code. I'm able to successfully parse <code>foo(spam)</code> and <code>spam+eggs</code>, but <code>foo(spam+eggs)</code> (recursive descent? my terminology from compilers is a bit rusty) fails.</p> <p>I have the following code:</p> <pre><code>from pyparsing_py3 imp...
2
2009-09-04T00:46:21Z
1,377,708
<p>I've found that a good habit to get into when using the '&lt;&lt;' operator with Forwards is to always enclose the RHS in parentheses. That is:</p> <pre><code>myExpr &lt;&lt; mySubExpr + ZeroOrMore( oneOf('+ - / * =') + mySubExpr ) </code></pre> <p>is better as:</p> <pre><code>myExpr &lt;&lt; ( mySubExpr + ZeroO...
4
2009-09-04T07:29:36Z
[ "python", "parsing", "pyparsing" ]
calculate user inputed time with Python
1,376,835
<p>I need to calculate (using python) how much time a user has inputed, whether they input something like 3:30 or 3.5. I'm not really sure what the best way to go about this is and I thought I'd ask for advice from the experts. </p> <p>=== Edit ==================</p> <p>To specify more clearly, I want the user to i...
0
2009-09-04T01:40:30Z
1,376,843
<p>There are a few possible solutions, but at some point you're gonna run into ambiguous cases that will result in arbitrary conversions.</p> <p>Overall I'd suggest taking any input and parsing the separators (whether : or . or something else) and then converting to seconds based on some schema of units you've defined...
0
2009-09-04T01:45:07Z
[ "python", "regex" ]
calculate user inputed time with Python
1,376,835
<p>I need to calculate (using python) how much time a user has inputed, whether they input something like 3:30 or 3.5. I'm not really sure what the best way to go about this is and I thought I'd ask for advice from the experts. </p> <p>=== Edit ==================</p> <p>To specify more clearly, I want the user to i...
0
2009-09-04T01:40:30Z
1,376,855
<p>Can you precisely define the syntax of the strings that the user is allowed to input? Once you do that, if it's simple enough it can be matched by simple Python string expressions, else you may be better off with pyparsing or the like. Also, a precise syntax will make it easier to identify any ambiguities so you c...
1
2009-09-04T01:50:28Z
[ "python", "regex" ]
calculate user inputed time with Python
1,376,835
<p>I need to calculate (using python) how much time a user has inputed, whether they input something like 3:30 or 3.5. I'm not really sure what the best way to go about this is and I thought I'd ask for advice from the experts. </p> <p>=== Edit ==================</p> <p>To specify more clearly, I want the user to i...
0
2009-09-04T01:40:30Z
1,376,880
<p>First of all, you'll need some conventions. Is 3.55 five minutes to four hours, five milliseconds to four seconds, or 3 and 55/100 of a minute/hour/second? The same applies to 3:55. At least have a distinction between dot and colon, specifying that a dot means a fraction and a colon, a separator of hour/minute/secon...
0
2009-09-04T02:00:46Z
[ "python", "regex" ]
calculate user inputed time with Python
1,376,835
<p>I need to calculate (using python) how much time a user has inputed, whether they input something like 3:30 or 3.5. I'm not really sure what the best way to go about this is and I thought I'd ask for advice from the experts. </p> <p>=== Edit ==================</p> <p>To specify more clearly, I want the user to i...
0
2009-09-04T01:40:30Z
1,377,010
<p>This is the code that we have in one of our internal web applications that we use for <strong>time-tracking</strong> purposes. When the user enters a time, the string value is passed through this function, which returns a structure of time data. </p> <p>It's written in javascript, and the code could be directly po...
0
2009-09-04T03:01:20Z
[ "python", "regex" ]
calculate user inputed time with Python
1,376,835
<p>I need to calculate (using python) how much time a user has inputed, whether they input something like 3:30 or 3.5. I'm not really sure what the best way to go about this is and I thought I'd ask for advice from the experts. </p> <p>=== Edit ==================</p> <p>To specify more clearly, I want the user to i...
0
2009-09-04T01:40:30Z
1,377,036
<p>Can you do this with a <strong>GUI and restrict the user input</strong>? Processing the text seems super error prone otherwise (on the part of the user, not to mention the programmer), and for hours worked... you sort-of want to get that right.</p>
0
2009-09-04T03:15:22Z
[ "python", "regex" ]
Porting from Python to C#
1,376,976
<p>I´m trying to learn C#, coming from a Python/PHP background, and I´m trying to port a script from Python to getting started.</p> <p>The script reads a text file line by line (about 150K lines), apply a list of regex until one is matched, get the named groups results and add the values as properties of a class.</p...
1
2009-09-04T02:41:26Z
1,376,987
<p>Sorry this is not a specific answer, but could you use IronPython to convert your scripts to run under the CLR and then step to C#?</p>
1
2009-09-04T02:47:43Z
[ "c#", "python" ]
Porting from Python to C#
1,376,976
<p>I´m trying to learn C#, coming from a Python/PHP background, and I´m trying to port a script from Python to getting started.</p> <p>The script reads a text file line by line (about 150K lines), apply a list of regex until one is matched, get the named groups results and add the values as properties of a class.</p...
1
2009-09-04T02:41:26Z
1,376,994
<p>1., sure</p> <p>2., see e.g. here</p> <p>3., yep, same basic concept as 2</p> <p>4., nah, C# is flexible enough to allow you to port your architecture over</p> <p>Also consider studying <a href="http://www.manning.com/foord/" rel="nofollow">this book</a> as the best intro to .NET for Python programmers AND vice ...
3
2009-09-04T02:52:14Z
[ "c#", "python" ]
Porting from Python to C#
1,376,976
<p>I´m trying to learn C#, coming from a Python/PHP background, and I´m trying to port a script from Python to getting started.</p> <p>The script reads a text file line by line (about 150K lines), apply a list of regex until one is matched, get the named groups results and add the values as properties of a class.</p...
1
2009-09-04T02:41:26Z
1,376,995
<ol> <li>Yes you can. </li> <li>.Net has support for named groups. So for <code>(?&lt;first&gt;group)(?'second'group)</code>, the returned Match object will support named retrieval like this. You can build youself a dictionary from this object or directly pass the Match object<br /> <code>var match = Regex.Match("subje...
2
2009-09-04T02:53:04Z
[ "c#", "python" ]
Porting from Python to C#
1,376,976
<p>I´m trying to learn C#, coming from a Python/PHP background, and I´m trying to port a script from Python to getting started.</p> <p>The script reads a text file line by line (about 150K lines), apply a list of regex until one is matched, get the named groups results and add the values as properties of a class.</p...
1
2009-09-04T02:41:26Z
1,376,998
<p>If you really want to learn C#, you should demand only references and not full answers, like <a href="http://msdn.microsoft.com/en-us/library/30wbz966%28VS.71%29.aspx" rel="nofollow">this one (RegEx class)</a>, but I'm sure you can find much more information with a quick Google search too.</p>
1
2009-09-04T02:54:09Z
[ "c#", "python" ]
Porting from Python to C#
1,376,976
<p>I´m trying to learn C#, coming from a Python/PHP background, and I´m trying to port a script from Python to getting started.</p> <p>The script reads a text file line by line (about 150K lines), apply a list of regex until one is matched, get the named groups results and add the values as properties of a class.</p...
1
2009-09-04T02:41:26Z
1,377,059
<p>What you're looking for sounds do-able. Of course you'll want to look at <code>System.Text.RegularExpressions</code>, specifically the <code>Regex</code> type there. </p> <p>Additionally, I'm really fond of the iterator pattern for reading lines from a file:</p> <pre><code>public static IEnumerable&lt;string&gt;...
1
2009-09-04T03:23:04Z
[ "c#", "python" ]
Porting from Python to C#
1,376,976
<p>I´m trying to learn C#, coming from a Python/PHP background, and I´m trying to port a script from Python to getting started.</p> <p>The script reads a text file line by line (about 150K lines), apply a list of regex until one is matched, get the named groups results and add the values as properties of a class.</p...
1
2009-09-04T02:41:26Z
1,377,253
<pre><code>dictionary&lt;string,string&gt; dic_test = new dictionary&lt;string,string&gt;(); dic_test.add(key,value); </code></pre>
0
2009-09-04T04:40:36Z
[ "c#", "python" ]
How do you deal with missing data using numpy/scipy?
1,377,130
<p>One of the things I deal with most in data cleaning is missing values. R deals with this well using its "NA" missing data label. In python, it appears that I'll have to deal with masked arrays which seem to be a major pain to set up and don't seem to be well documented. Any suggestions on making this process easier ...
9
2009-09-04T03:44:57Z
1,380,476
<p>I also question the problem with masked arrays. Here are a couple of examples:</p> <pre><code>import numpy as np data = np.ma.masked_array(np.arange(10)) data[5] = np.ma.masked # Mask a specific value data[data&gt;6] = np.ma.masked # Mask any value greater than 6 # Same thing done at initialization time init_dat...
1
2009-09-04T17:08:30Z
[ "python", "numpy", "data-analysis" ]
How do you deal with missing data using numpy/scipy?
1,377,130
<p>One of the things I deal with most in data cleaning is missing values. R deals with this well using its "NA" missing data label. In python, it appears that I'll have to deal with masked arrays which seem to be a major pain to set up and don't seem to be well documented. Any suggestions on making this process easier ...
9
2009-09-04T03:44:57Z
1,380,530
<p>Masked arrays are the anwswer, as DpplerShift describes. For quick and dirty use, you can use fancy indexing with boolean arrays:</p> <pre><code>&gt;&gt;&gt; import numpy as np &gt;&gt;&gt; data = np.arange(10) &gt;&gt;&gt; valid_idx = data % 2 == 0 #pretend that even elements are missing &gt;&gt;&gt; # Get non-mi...
1
2009-09-04T17:22:16Z
[ "python", "numpy", "data-analysis" ]
How do you deal with missing data using numpy/scipy?
1,377,130
<p>One of the things I deal with most in data cleaning is missing values. R deals with this well using its "NA" missing data label. In python, it appears that I'll have to deal with masked arrays which seem to be a major pain to set up and don't seem to be well documented. Any suggestions on making this process easier ...
9
2009-09-04T03:44:57Z
11,086,822
<p>If you are willing to consider a library, pandas (http://pandas.pydata.org/) is a library built on top of numpy which amongst many other things provides:</p> <blockquote> <p>Intelligent data alignment and integrated handling of missing data: gain automatic label-based alignment in computations and easily manipula...
4
2012-06-18T16:12:12Z
[ "python", "numpy", "data-analysis" ]
How to make this code handle big inputs more efficiently?
1,377,335
<p>Hey. I know this is not a 'refactor my code' site but I made this little piece of code which works perfectly fine with moderately sized input but it's problematic with string of size, say, over 2000.</p> <p>What it does - it takes a string of numbers as a parameter, and it returns the number of ways it can be inter...
1
2009-09-04T05:11:36Z
1,377,387
<p>Try using a dynamic programming approach instead:</p> <ol> <li>Create an array (call it 'P') with 1 element per character in the string.</li> <li>Initialize P[0] = 1 (unless the first character is 0, in which case just return 0 for the result).</li> <li>Initialize P[1] = 2 if the first two characters can be interpr...
3
2009-09-04T05:34:12Z
[ "python", "recursion" ]
How to make this code handle big inputs more efficiently?
1,377,335
<p>Hey. I know this is not a 'refactor my code' site but I made this little piece of code which works perfectly fine with moderately sized input but it's problematic with string of size, say, over 2000.</p> <p>What it does - it takes a string of numbers as a parameter, and it returns the number of ways it can be inter...
1
2009-09-04T05:11:36Z
1,377,391
<p>A non-recursive algorithm can be written but I don't think it will be faster. I'm no python expert so I'll just give you an algorithm:</p> <pre><code>Convert the array on numbers to an array of letters using just A thru I and leaving the zeros in place. Create two nested loops where you search and replace all the ...
0
2009-09-04T05:35:33Z
[ "python", "recursion" ]
How to make this code handle big inputs more efficiently?
1,377,335
<p>Hey. I know this is not a 'refactor my code' site but I made this little piece of code which works perfectly fine with moderately sized input but it's problematic with string of size, say, over 2000.</p> <p>What it does - it takes a string of numbers as a parameter, and it returns the number of ways it can be inter...
1
2009-09-04T05:11:36Z
1,377,425
<p>Recursion elimination is always a fun task. Here, I'd focus on ensuring the cache is correctly populated, then just use it, as follows...:</p> <pre><code>import collections def alpha_code(numbers): # populate cache with all needed pieces cache = dict() pending_work = collections.deque([numbers]) wh...
1
2009-09-04T05:46:29Z
[ "python", "recursion" ]
How to make this code handle big inputs more efficiently?
1,377,335
<p>Hey. I know this is not a 'refactor my code' site but I made this little piece of code which works perfectly fine with moderately sized input but it's problematic with string of size, say, over 2000.</p> <p>What it does - it takes a string of numbers as a parameter, and it returns the number of ways it can be inter...
1
2009-09-04T05:11:36Z
1,378,705
<p>You can largely reduce the memory footprint by not copying the string, and passing instead the original string and the index of the first character to study:</p> <pre><code>def alpha_code(numbers, start_from = 0) .... </code></pre> <p>You would then call recursively as:</p> <pre><code>alpha_code(numbers, star...
0
2009-09-04T11:32:27Z
[ "python", "recursion" ]
What is faster in Python, "while" or "for xrange"
1,377,429
<p>We can do numeric iteration like:</p> <pre><code>for i in xrange(10): print i, </code></pre> <p>and in C-style:</p> <pre><code>i = 0 while i &lt; 10: print i, i = i + 1 </code></pre> <p>Yes, I know, the first one is less error-prone, more pythonic but is it fast enough as C-style version?</p> <p>PS....
4
2009-09-04T05:48:23Z
1,377,454
<p>Who cares? Seriously. If you want to know, use timeit package (you can invoke it from command line with -m).</p> <p>But it doesn't matter at all, because the difference is negligible. And in general, Python is not a language that you choose if you want speed.</p>
13
2009-09-04T05:57:32Z
[ "python", "micro-optimization" ]
What is faster in Python, "while" or "for xrange"
1,377,429
<p>We can do numeric iteration like:</p> <pre><code>for i in xrange(10): print i, </code></pre> <p>and in C-style:</p> <pre><code>i = 0 while i &lt; 10: print i, i = i + 1 </code></pre> <p>Yes, I know, the first one is less error-prone, more pythonic but is it fast enough as C-style version?</p> <p>PS....
4
2009-09-04T05:48:23Z
1,377,933
<p>They are <strong>both</strong> to avoid :-)</p> <p>Generally speaking, each time I see an iteration over numbers, I see some non-pythonic code, that could be expressed in a better way using iterations over lists or generators.<br /> Actually, I've said "pythonic", but it is all about <strong>readability</strong>. U...
2
2009-09-04T08:22:59Z
[ "python", "micro-optimization" ]
What is faster in Python, "while" or "for xrange"
1,377,429
<p>We can do numeric iteration like:</p> <pre><code>for i in xrange(10): print i, </code></pre> <p>and in C-style:</p> <pre><code>i = 0 while i &lt; 10: print i, i = i + 1 </code></pre> <p>Yes, I know, the first one is less error-prone, more pythonic but is it fast enough as C-style version?</p> <p>PS....
4
2009-09-04T05:48:23Z
1,378,157
<p>I am sure the <code>while</code> version is slower. Python will have to lookup the add operation for the integer object on each turn of the loop etc, it is not pure C just because it looks like it!</p> <p>And if you want a pythonic version of exactly the above, use:</p> <pre><code>print " ".join(str(i) for i in xr...
13
2009-09-04T09:26:46Z
[ "python", "micro-optimization" ]
What is faster in Python, "while" or "for xrange"
1,377,429
<p>We can do numeric iteration like:</p> <pre><code>for i in xrange(10): print i, </code></pre> <p>and in C-style:</p> <pre><code>i = 0 while i &lt; 10: print i, i = i + 1 </code></pre> <p>Yes, I know, the first one is less error-prone, more pythonic but is it fast enough as C-style version?</p> <p>PS....
4
2009-09-04T05:48:23Z
1,378,502
<p>The first one.</p> <p>You mean, faster to develop, right?</p> <p>PS: It doesn't matter, machines these days are so fast that it is meaningless to ponder on micro optimizations, prior to identifying the bottlenecks using a thorough profiler.</p>
4
2009-09-04T10:48:59Z
[ "python", "micro-optimization" ]
What is faster in Python, "while" or "for xrange"
1,377,429
<p>We can do numeric iteration like:</p> <pre><code>for i in xrange(10): print i, </code></pre> <p>and in C-style:</p> <pre><code>i = 0 while i &lt; 10: print i, i = i + 1 </code></pre> <p>Yes, I know, the first one is less error-prone, more pythonic but is it fast enough as C-style version?</p> <p>PS....
4
2009-09-04T05:48:23Z
1,378,942
<p>Well, if you are after efficiency in numerical code, you ought to use <a href="http://numpy.scipy.org" rel="nofollow" title="numpy">numpy</a> and <a href="http://www.scipy.org" rel="nofollow" title="scipy">scipy</a>. Your integration can be quickly written as <code>numpy.sum( numpy.arange( 10 ) )</code></p>
0
2009-09-04T12:31:53Z
[ "python", "micro-optimization" ]
What is faster in Python, "while" or "for xrange"
1,377,429
<p>We can do numeric iteration like:</p> <pre><code>for i in xrange(10): print i, </code></pre> <p>and in C-style:</p> <pre><code>i = 0 while i &lt; 10: print i, i = i + 1 </code></pre> <p>Yes, I know, the first one is less error-prone, more pythonic but is it fast enough as C-style version?</p> <p>PS....
4
2009-09-04T05:48:23Z
1,380,023
<p>If your program is too slow, try using <a href="http://psyco.sourceforge.net/" rel="nofollow">psyco</a>.</p> <p>Don't worry about the kind of micro-optimisation in your question. Write your program to be maintainable (which includes following standard Python style so other programmers can read it easier).</p>
0
2009-09-04T15:42:02Z
[ "python", "micro-optimization" ]
What is faster in Python, "while" or "for xrange"
1,377,429
<p>We can do numeric iteration like:</p> <pre><code>for i in xrange(10): print i, </code></pre> <p>and in C-style:</p> <pre><code>i = 0 while i &lt; 10: print i, i = i + 1 </code></pre> <p>Yes, I know, the first one is less error-prone, more pythonic but is it fast enough as C-style version?</p> <p>PS....
4
2009-09-04T05:48:23Z
1,380,037
<p>In Python, the shorter and clearer version is always better. If I am not mistaken the range and xrange functions are not native, if you try xrange(sys.maxint+1) you will get an overflow error.</p> <p>Besides, what the hell could this be useful for? If you are just printing 10 numbers, then surely readability counts...
-1
2009-09-04T15:45:16Z
[ "python", "micro-optimization" ]
Render HTML to PDF in Django site
1,377,446
<p>For my django powered site, I am looking for an easy solution to convert dynamic html pages to pdf.</p> <p>Pages include HTML and charts from Google visualization API (which is javascript based, yet including those graphs is a must).</p>
85
2009-09-04T05:54:35Z
1,377,470
<p>Try the <a href="http://torisugari.googlepages.com/commandlineprint2" rel="nofollow">command line print</a> firefox extension.</p>
2
2009-09-04T06:02:11Z
[ "python", "html", "django", "pdf", "pdf-generation" ]
Render HTML to PDF in Django site
1,377,446
<p>For my django powered site, I am looking for an easy solution to convert dynamic html pages to pdf.</p> <p>Pages include HTML and charts from Google visualization API (which is javascript based, yet including those graphs is a must).</p>
85
2009-09-04T05:54:35Z
1,377,652
<p>Try the solution from <a href="http://www.reportlab.org/">Reportlab</a>.</p> <p>Download it and install it as usual with python setup.py install</p> <p>You will also need to install the following modules: xhtml2pdf, html5lib, pypdf with easy_install.</p> <p>Here is an usage example:</p> <p><strong>First define t...
174
2009-09-04T07:12:50Z
[ "python", "html", "django", "pdf", "pdf-generation" ]
Render HTML to PDF in Django site
1,377,446
<p>For my django powered site, I am looking for an easy solution to convert dynamic html pages to pdf.</p> <p>Pages include HTML and charts from Google visualization API (which is javascript based, yet including those graphs is a must).</p>
85
2009-09-04T05:54:35Z
6,353,136
<p>See cool package <a href="https://github.com/NetAngels/django-webodt">https://github.com/NetAngels/django-webodt</a> </p>
5
2011-06-15T04:48:13Z
[ "python", "html", "django", "pdf", "pdf-generation" ]
Render HTML to PDF in Django site
1,377,446
<p>For my django powered site, I am looking for an easy solution to convert dynamic html pages to pdf.</p> <p>Pages include HTML and charts from Google visualization API (which is javascript based, yet including those graphs is a must).</p>
85
2009-09-04T05:54:35Z
13,961,258
<p>I just whipped this up for CBV. Not used in production but generates a PDF for me. Probably needs work for the error reporting side of things but does the trick so far.</p> <pre><code>import StringIO from cgi import escape from xhtml2pdf import pisa from django.http import HttpResponse from django.template.response...
9
2012-12-19T21:11:38Z
[ "python", "html", "django", "pdf", "pdf-generation" ]
Render HTML to PDF in Django site
1,377,446
<p>For my django powered site, I am looking for an easy solution to convert dynamic html pages to pdf.</p> <p>Pages include HTML and charts from Google visualization API (which is javascript based, yet including those graphs is a must).</p>
85
2009-09-04T05:54:35Z
16,890,568
<p>i found this app <a href="https://github.com/directeur/django-pdf" rel="nofollow">https://github.com/directeur/django-pdf</a> . Just have to add "?format=pdf" in url to get the pdf.But css is not working in this too.</p>
1
2013-06-03T05:37:23Z
[ "python", "html", "django", "pdf", "pdf-generation" ]
Render HTML to PDF in Django site
1,377,446
<p>For my django powered site, I am looking for an easy solution to convert dynamic html pages to pdf.</p> <p>Pages include HTML and charts from Google visualization API (which is javascript based, yet including those graphs is a must).</p>
85
2009-09-04T05:54:35Z
18,990,121
<p>You can use iReport editor to define the layout, and publish the report in jasper reports server. After publish you can invoke the rest api to get the results.</p> <p>Here is the test of the functionality:</p> <pre><code>from django.test import TestCase from x_reports_jasper.models import JasperServerClient """ ...
1
2013-09-24T19:15:27Z
[ "python", "html", "django", "pdf", "pdf-generation" ]
Render HTML to PDF in Django site
1,377,446
<p>For my django powered site, I am looking for an easy solution to convert dynamic html pages to pdf.</p> <p>Pages include HTML and charts from Google visualization API (which is javascript based, yet including those graphs is a must).</p>
85
2009-09-04T05:54:35Z
25,800,473
<p><a href="https://github.com/nigma/django-easy-pdf">https://github.com/nigma/django-easy-pdf</a></p> <p>Template:</p> <pre><code>{% extends "easy_pdf/base.html" %} {% block content %} &lt;div id="content"&gt; &lt;h1&gt;Hi there!&lt;/h1&gt; &lt;/div&gt; {% endblock %} </code></pre> <p>View:</p> <p...
6
2014-09-12T03:55:10Z
[ "python", "html", "django", "pdf", "pdf-generation" ]
Render HTML to PDF in Django site
1,377,446
<p>For my django powered site, I am looking for an easy solution to convert dynamic html pages to pdf.</p> <p>Pages include HTML and charts from Google visualization API (which is javascript based, yet including those graphs is a must).</p>
85
2009-09-04T05:54:35Z
27,419,707
<p>Try <a href="http://wkhtmltopdf.org/" rel="nofollow">wkhtmltopdf</a> with either one of the following wrappers</p> <p><a href="https://github.com/incuna/django-wkhtmltopdf" rel="nofollow">django-wkhtmltopdf</a> or <a href="https://github.com/JazzCore/python-pdfkit/" rel="nofollow">python-pdfkit</a> </p> <p>This w...
8
2014-12-11T09:45:27Z
[ "python", "html", "django", "pdf", "pdf-generation" ]
Render HTML to PDF in Django site
1,377,446
<p>For my django powered site, I am looking for an easy solution to convert dynamic html pages to pdf.</p> <p>Pages include HTML and charts from Google visualization API (which is javascript based, yet including those graphs is a must).</p>
85
2009-09-04T05:54:35Z
29,440,704
<p>After trying to get this to work for too many hours, I finally found this: <a href="https://github.com/vierno/django-xhtml2pdf" rel="nofollow">https://github.com/vierno/django-xhtml2pdf</a></p> <p>It's a fork of <a href="https://github.com/chrisglass/django-xhtml2pdf" rel="nofollow">https://github.com/chrisglass/dj...
2
2015-04-03T22:48:41Z
[ "python", "html", "django", "pdf", "pdf-generation" ]
Python multiprocessing with twisted's reactor
1,377,494
<p>Dear everyone I am working on a xmlrpc server which has to perform certain tasks cyclically. I am using twisted as the core of the xmlrpc service but I am running into a little problem:</p> <pre><code>class cemeteryRPC(xmlrpc.XMLRPC): def __init__(self, dic): xmlrpc.XMLRPC.__init__(self) def xmlr...
4
2009-09-04T06:08:57Z
1,378,840
<p>Do you really need to run Twisted in a separate process? That looks pretty unusual to me.</p> <p>Try to think of Twisted's Reactor as your main loop - and hang everything you need off that - rather than trying to run Twisted as a background task.</p> <p>The more normal way of performing this sort of operation woul...
11
2009-09-04T12:06:34Z
[ "python", "twisted", "multiprocessing" ]
Python multiprocessing with twisted's reactor
1,377,494
<p>Dear everyone I am working on a xmlrpc server which has to perform certain tasks cyclically. I am using twisted as the core of the xmlrpc service but I am running into a little problem:</p> <pre><code>class cemeteryRPC(xmlrpc.XMLRPC): def __init__(self, dic): xmlrpc.XMLRPC.__init__(self) def xmlr...
4
2009-09-04T06:08:57Z
1,379,268
<p>Hey asdvawev - .join() in multiprocessing works just like .join() in threading - it's a blocking call the main thread runs to wait for the worker to shut down. If the worker never shuts down, then .join() will never return. For example:</p> <pre><code>class myproc(Process): def run(self): while True: ...
3
2009-09-04T13:29:24Z
[ "python", "twisted", "multiprocessing" ]
What kind of setup (IDE et al) do I need to run IronPython unit tests of C#.NET developed assemblies?
1,377,548
<p>I'm attempting to learn IronPython, to broaden my .NET horizons. I want to be able to use Python to write the unit-tests for my next personal project. So being able to access C#.NET assemblies from my Python code is necessary. I also wanted an IDE with auto-complete and smart indenting. PyScripter seemed like a goo...
0
2009-09-04T06:28:40Z
1,377,575
<p>See Michael Foord's website <a href="http://www.voidspace.org.uk/python/weblog/arch%5FWebsite.shtml" rel="nofollow">for IDE</a> and <a href="http://www.voidspace.org.uk/python/weblog/arch%5Fd7%5F2009%5F06%5F13.shtml" rel="nofollow">unittest</a> also discover. And many IronPython articles and the book IronPython in A...
1
2009-09-04T06:36:16Z
[ ".net", "python", "ironpython" ]
What kind of setup (IDE et al) do I need to run IronPython unit tests of C#.NET developed assemblies?
1,377,548
<p>I'm attempting to learn IronPython, to broaden my .NET horizons. I want to be able to use Python to write the unit-tests for my next personal project. So being able to access C#.NET assemblies from my Python code is necessary. I also wanted an IDE with auto-complete and smart indenting. PyScripter seemed like a goo...
0
2009-09-04T06:28:40Z
1,377,595
<p>Here's a good link to an article about different IDE's and how they work with IronPython: <a href="http://www.voidspace.org.uk/ironpython/tools-and-ides.shtml" rel="nofollow">http://www.voidspace.org.uk/ironpython/tools-and-ides.shtml</a></p>
2
2009-09-04T06:47:30Z
[ ".net", "python", "ironpython" ]
What kind of setup (IDE et al) do I need to run IronPython unit tests of C#.NET developed assemblies?
1,377,548
<p>I'm attempting to learn IronPython, to broaden my .NET horizons. I want to be able to use Python to write the unit-tests for my next personal project. So being able to access C#.NET assemblies from my Python code is necessary. I also wanted an IDE with auto-complete and smart indenting. PyScripter seemed like a goo...
0
2009-09-04T06:28:40Z
1,378,519
<p>FWIW, Frood himself uses Wing IDE.</p> <p>But, if its on Windows, why not VS?</p>
0
2009-09-04T10:51:57Z
[ ".net", "python", "ironpython" ]
What kind of setup (IDE et al) do I need to run IronPython unit tests of C#.NET developed assemblies?
1,377,548
<p>I'm attempting to learn IronPython, to broaden my .NET horizons. I want to be able to use Python to write the unit-tests for my next personal project. So being able to access C#.NET assemblies from my Python code is necessary. I also wanted an IDE with auto-complete and smart indenting. PyScripter seemed like a goo...
0
2009-09-04T06:28:40Z
1,383,258
<p>You can also consider using Eclipse with PyDev which has support for IronPython</p>
0
2009-09-05T12:47:32Z
[ ".net", "python", "ironpython" ]
What kind of setup (IDE et al) do I need to run IronPython unit tests of C#.NET developed assemblies?
1,377,548
<p>I'm attempting to learn IronPython, to broaden my .NET horizons. I want to be able to use Python to write the unit-tests for my next personal project. So being able to access C#.NET assemblies from my Python code is necessary. I also wanted an IDE with auto-complete and smart indenting. PyScripter seemed like a goo...
0
2009-09-04T06:28:40Z
1,389,592
<p>You can use Visual Studio. I use IronPython Studio integrated with VS2008. But I feel that it has very poor intellisense for Python.</p>
0
2009-09-07T14:07:28Z
[ ".net", "python", "ironpython" ]
Python dictionary to store socket objects
1,378,079
<p>Can we store socket objects in a Python dictionary. I want to create a socket, store socket object, do some stuff and then read from the socket(search from dictionary to get socketobject).</p>
3
2009-09-04T09:02:32Z
1,378,084
<p>Yes:</p> <pre><code>&gt;&gt;&gt; import socket &gt;&gt;&gt; s = socket.socket() &gt;&gt;&gt; d = {"key" : s} &gt;&gt;&gt; d {'key': &lt;socket._socketobject object at 0x00CEB5A8&gt;} </code></pre>
10
2009-09-04T09:05:41Z
[ "python" ]
Python dicts in sqlalchemy
1,378,325
<p>I would like to load/save a dict to/from my sqlite DB, but am having some problems figuring out a simple way to do it. I don't really need to be able to filter, etc., based on the contents so a simple conversion to/from string is fine.</p> <p>The next-best thing would be foreign keys. Please don't post links to hug...
15
2009-09-04T10:03:36Z
1,378,366
<p>If you need to map a 1-N <strong>relation</strong> and map it as <code>dict</code> rather then <code>list</code>, then read <a href="http://www.sqlalchemy.org/docs/05/mappers.html#dictionary-based-collections" rel="nofollow">Dictionary Based Collections</a></p> <p>But if you mean a <strong>field</strong>, then what...
2
2009-09-04T10:12:58Z
[ "python", "sql", "sqlite", "sqlalchemy" ]
Python dicts in sqlalchemy
1,378,325
<p>I would like to load/save a dict to/from my sqlite DB, but am having some problems figuring out a simple way to do it. I don't really need to be able to filter, etc., based on the contents so a simple conversion to/from string is fine.</p> <p>The next-best thing would be foreign keys. Please don't post links to hug...
15
2009-09-04T10:03:36Z
1,378,818
<p>The SQLAlchemy <a href="http://docs.sqlalchemy.org/en/latest/core/type_basics.html#sqlalchemy.types.PickleType">PickleType</a> is meant exactly for this.</p> <pre><code>class SomeEntity(Base): __tablename__ = 'some_entity' id = Column(Integer, primary_key=True) attributes = Column(PickleType) # Just se...
42
2009-09-04T12:00:09Z
[ "python", "sql", "sqlite", "sqlalchemy" ]
How to get/set data of(into) visual components (of winodws programs) programmatically?
1,378,803
<p>Let me clarify the question a bit. I am talking about windows GUI programs. Say a program-window has a dialog box (or a confirm button) asking user input. How can I provide input to that program using my program (written in say C#, Java or Python). Or say, a program window is showing some image in one of its panels....
0
2009-09-04T11:56:42Z
1,378,847
<p>To programmatically generate key presses, see <a href="http://stackoverflow.com/questions/136734/key-presses-in-python">http://stackoverflow.com/questions/136734/key-presses-in-python</a>, and search for "generate", "key", "emulate", "fake".</p> <p>To programmatically capture the screen, see <a href="http://stackov...
1
2009-09-04T12:09:23Z
[ "c#", "java", "python", "windows", "impersonation" ]
How to get/set data of(into) visual components (of winodws programs) programmatically?
1,378,803
<p>Let me clarify the question a bit. I am talking about windows GUI programs. Say a program-window has a dialog box (or a confirm button) asking user input. How can I provide input to that program using my program (written in say C#, Java or Python). Or say, a program window is showing some image in one of its panels....
0
2009-09-04T11:56:42Z
1,378,864
<p>spy++ is listening for all win32 messages. It is very useful for debugging an application but I don't think that it is a good idea to use it as an inter-process communication mechanism.</p> <p>You can use win32 apis to send input to your program. As an example, You can modify the content of an edit text by using th...
2
2009-09-04T12:13:03Z
[ "c#", "java", "python", "windows", "impersonation" ]
How to get/set data of(into) visual components (of winodws programs) programmatically?
1,378,803
<p>Let me clarify the question a bit. I am talking about windows GUI programs. Say a program-window has a dialog box (or a confirm button) asking user input. How can I provide input to that program using my program (written in say C#, Java or Python). Or say, a program window is showing some image in one of its panels....
0
2009-09-04T11:56:42Z
1,378,949
<p>If you want to write win32 code to do it, start here:</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms632589%28VS.85%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms632589(VS.85).aspx</a></p> <p>If you just want to hook the gui as easily as possible, investigate:</p> <p><a href="http:/...
1
2009-09-04T12:33:02Z
[ "c#", "java", "python", "windows", "impersonation" ]
Python: how to format traceback objects
1,378,889
<p>I have a traceback object that I want to show in the nice format I get when calling <code>traceback.format_exc()</code>.</p> <p>Is there a builtin function for this? Or a few lines of code?</p>
15
2009-09-04T12:19:51Z
1,378,909
<p>format_exc is really just</p> <pre><code> etype, value, tb = sys.exc_info() return ''.join(format_exception(etype, value, tb, limit)) </code></pre> <p>So if you have the exception type, value, and traceback ready, it should be easy. If you have just the exception, notice that <code>format_exception</code> i...
19
2009-09-04T12:24:44Z
[ "python", "object", "format", "traceback" ]
Python: how to format traceback objects
1,378,889
<p>I have a traceback object that I want to show in the nice format I get when calling <code>traceback.format_exc()</code>.</p> <p>Is there a builtin function for this? Or a few lines of code?</p>
15
2009-09-04T12:19:51Z
1,378,910
<p>Have you tried <a href="http://docs.python.org/library/traceback.html#traceback.print%5Ftb">traceback.print_tb</a> or <a href="http://docs.python.org/library/traceback.html#traceback.format%5Ftb">traceback.format_tb</a>?</p>
7
2009-09-04T12:25:01Z
[ "python", "object", "format", "traceback" ]
Python: how to format traceback objects
1,378,889
<p>I have a traceback object that I want to show in the nice format I get when calling <code>traceback.format_exc()</code>.</p> <p>Is there a builtin function for this? Or a few lines of code?</p>
15
2009-09-04T12:19:51Z
1,378,913
<p><code>traceback</code> docs give <a href="http://docs.python.org/library/traceback.html#traceback-examples" rel="nofollow">few examples</a> and <a href="http://docs.python.org/library/traceback.html" rel="nofollow">whole set of functions</a> for formatting traceback objects.</p>
2
2009-09-04T12:25:50Z
[ "python", "object", "format", "traceback" ]
list of methods for python shell?
1,378,926
<p>You'd have already found out by my usage of terminology that I'm a python n00b.</p> <p>straight forward question:</p> <p>How can i see a list of methods for a particular object in an interactive python shell like i can in ruby (you can do that in ruby irb with a '.methods' after the object)?</p>
6
2009-09-04T12:29:08Z
1,378,929
<p><code>dir( object )</code></p> <p>will give you the list.</p> <p>for instance:</p> <pre><code>a = 2 dir( a ) </code></pre> <p>will list off all the methods you can call for an integer.</p>
7
2009-09-04T12:29:57Z
[ "python", "shell", "interactive" ]
list of methods for python shell?
1,378,926
<p>You'd have already found out by my usage of terminology that I'm a python n00b.</p> <p>straight forward question:</p> <p>How can i see a list of methods for a particular object in an interactive python shell like i can in ruby (you can do that in ruby irb with a '.methods' after the object)?</p>
6
2009-09-04T12:29:08Z
1,378,933
<p>Do this:</p> <pre><code>dir(obj) </code></pre>
1
2009-09-04T12:30:22Z
[ "python", "shell", "interactive" ]
list of methods for python shell?
1,378,926
<p>You'd have already found out by my usage of terminology that I'm a python n00b.</p> <p>straight forward question:</p> <p>How can i see a list of methods for a particular object in an interactive python shell like i can in ruby (you can do that in ruby irb with a '.methods' after the object)?</p>
6
2009-09-04T12:29:08Z
1,378,935
<pre><code>&gt;&gt;&gt; help(my_object) </code></pre>
5
2009-09-04T12:30:51Z
[ "python", "shell", "interactive" ]
list of methods for python shell?
1,378,926
<p>You'd have already found out by my usage of terminology that I'm a python n00b.</p> <p>straight forward question:</p> <p>How can i see a list of methods for a particular object in an interactive python shell like i can in ruby (you can do that in ruby irb with a '.methods' after the object)?</p>
6
2009-09-04T12:29:08Z
1,378,971
<p>Others have mentioned <a href="http://docs.python.org/3.1/library/functions.html#dir" rel="nofollow"><code>dir</code></a>. Let me make an remark of caution: Python objects may have a <a href="http://docs.python.org/3.1/reference/datamodel.html?highlight=%5F%5Fgetattr%5F%5F#object.%5F%5Fgetattr%5F%5F" rel="nofollow">...
2
2009-09-04T12:37:55Z
[ "python", "shell", "interactive" ]
list of methods for python shell?
1,378,926
<p>You'd have already found out by my usage of terminology that I'm a python n00b.</p> <p>straight forward question:</p> <p>How can i see a list of methods for a particular object in an interactive python shell like i can in ruby (you can do that in ruby irb with a '.methods' after the object)?</p>
6
2009-09-04T12:29:08Z
1,379,011
<p>If you want only methods, then</p> <pre><code>def methods(obj): return [attr for attr in dir(obj) if callable(getattr(obj, attr))] </code></pre> <p>But do try out IPython, it has tab completion for object attributes, so typing <code>obj.&lt;tab&gt;</code> shows you a list of available attributes on that object...
0
2009-09-04T12:44:40Z
[ "python", "shell", "interactive" ]
list of methods for python shell?
1,378,926
<p>You'd have already found out by my usage of terminology that I'm a python n00b.</p> <p>straight forward question:</p> <p>How can i see a list of methods for a particular object in an interactive python shell like i can in ruby (you can do that in ruby irb with a '.methods' after the object)?</p>
6
2009-09-04T12:29:08Z
1,379,242
<p>For an enhanced version of <code>dir()</code> check out <code>see()</code>!</p> <pre><code>&gt;&gt;&gt; test = [1,2,3] &gt;&gt;&gt; see(test) [] in + += * *= &lt; &lt;= == != &gt; &gt;= hash() help() iter() len() repr() reversed() str() .append() .co...
3
2009-09-04T13:25:31Z
[ "python", "shell", "interactive" ]
list of methods for python shell?
1,378,926
<p>You'd have already found out by my usage of terminology that I'm a python n00b.</p> <p>straight forward question:</p> <p>How can i see a list of methods for a particular object in an interactive python shell like i can in ruby (you can do that in ruby irb with a '.methods' after the object)?</p>
6
2009-09-04T12:29:08Z
1,379,470
<p>Python supports tab completion as well. I prefer my python prompt clean (so no thanks to IPython), but with tab completion.</p> <p>Setup in .bashrc or similar:</p> <pre><code>PYTHONSTARTUP=$HOME/.pythonrc </code></pre> <p>Put this in .pythonrc:</p> <pre><code>try: import readline except ImportError: prin...
4
2009-09-04T14:01:30Z
[ "python", "shell", "interactive" ]
list of methods for python shell?
1,378,926
<p>You'd have already found out by my usage of terminology that I'm a python n00b.</p> <p>straight forward question:</p> <p>How can i see a list of methods for a particular object in an interactive python shell like i can in ruby (you can do that in ruby irb with a '.methods' after the object)?</p>
6
2009-09-04T12:29:08Z
1,379,700
<p>Existing answers do a good job of showing you how to get the ATTRIBUTES of an object, but do not precisely answer the question you posed -- how to get the METHODS of an object. Python objects have a unified namespace (differently from Ruby, where methods and attributes use different namespaces). Consider for exampl...
14
2009-09-04T14:42:50Z
[ "python", "shell", "interactive" ]
list of methods for python shell?
1,378,926
<p>You'd have already found out by my usage of terminology that I'm a python n00b.</p> <p>straight forward question:</p> <p>How can i see a list of methods for a particular object in an interactive python shell like i can in ruby (you can do that in ruby irb with a '.methods' after the object)?</p>
6
2009-09-04T12:29:08Z
22,969,378
<p>Its simple do this for any object you have created</p> <pre><code>dir(object) </code></pre> <p>it will return a list of all the attributes of the object.</p>
1
2014-04-09T17:09:05Z
[ "python", "shell", "interactive" ]
is there a way to start/stop linux processes with python?
1,378,974
<p>I want to be able to start a process and then be able to kill it afterwards</p>
6
2009-09-04T12:38:06Z
1,378,989
<p>Have a look at the <a href="http://docs.python.org/library/subprocess.html" rel="nofollow"><code>subprocess</code></a> module. You can also use low-level primitives like <code>fork()</code> via the <code>os</code> module.</p>
7
2009-09-04T12:40:44Z
[ "python", "linux" ]
is there a way to start/stop linux processes with python?
1,378,974
<p>I want to be able to start a process and then be able to kill it afterwards</p>
6
2009-09-04T12:38:06Z
1,378,994
<p><a href="http://docs.python.org/library/os.html#process-management" rel="nofollow">http://docs.python.org/library/os.html#process-management</a></p>
3
2009-09-04T12:42:17Z
[ "python", "linux" ]
is there a way to start/stop linux processes with python?
1,378,974
<p>I want to be able to start a process and then be able to kill it afterwards</p>
6
2009-09-04T12:38:06Z
1,379,135
<p>A simple function that uses subprocess module:</p> <pre><code>def CMD(cmd) : p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False) retur...
3
2009-09-04T13:07:31Z
[ "python", "linux" ]
is there a way to start/stop linux processes with python?
1,378,974
<p>I want to be able to start a process and then be able to kill it afterwards</p>
6
2009-09-04T12:38:06Z
1,380,308
<p>see docs for primitive fork() and modules <a href="http://docs.python.org/library/subprocess.html" rel="nofollow">subprocess</a>, <a href="http://docs.python.org/library/multiprocessing.html#module-multiprocessing.managers" rel="nofollow">multiprocessing</a>, <a href="http://docs.python.org/library/threading.html?hi...
0
2009-09-04T16:33:13Z
[ "python", "linux" ]
is there a way to start/stop linux processes with python?
1,378,974
<p>I want to be able to start a process and then be able to kill it afterwards</p>
6
2009-09-04T12:38:06Z
1,380,817
<p>If you need to interact with the sub process at all, I recommend the pexpect module (<a href="http://pypi.python.org/pypi/pexpect/2.4" rel="nofollow">link text</a>). You can send input to the process, receive (or "expect") output in return, and you can close the process (with force=True to send SIGKILL).</p>
0
2009-09-04T18:25:40Z
[ "python", "linux" ]
is there a way to start/stop linux processes with python?
1,378,974
<p>I want to be able to start a process and then be able to kill it afterwards</p>
6
2009-09-04T12:38:06Z
1,385,959
<p>Here's a little python script that starts a process, checks if it is running, waits a while, kills it, waits for it to terminate, then checks again. It uses the 'kill' command. Version 2.6 of python subprocess has a kill function. This was written on 2.5.</p> <pre><code>import subprocess import time proc = subproc...
12
2009-09-06T15:43:41Z
[ "python", "linux" ]
Are there any cpython libraries that work with jsr168 and/or jsr286?
1,379,607
<p>On a Java portal you can have portlets that include data provided by other applications. We want to replace our existing Java portal with a Django application, which means duplicating the Java portal's ability to display portlets. The two Sun specifications in question that we want to duplicate are JSR168 and JSR286...
3
2009-09-04T14:27:28Z
1,382,879
<p>I'm not sure you can do this. From JSR 168:</p> <p><img src="http://imgur.com/8rxJQ.png" alt="JSR 168 Request/response Handling" /></p> <p>If I understand correctly, you want the Django application to take the place of the existing "Java Portal/Portlet Container" in the diagram. Unfortunately, the interface betwee...
3
2009-09-05T09:36:07Z
[ "python", "django", "jsr168", "jsr286" ]
Are there any cpython libraries that work with jsr168 and/or jsr286?
1,379,607
<p>On a Java portal you can have portlets that include data provided by other applications. We want to replace our existing Java portal with a Django application, which means duplicating the Java portal's ability to display portlets. The two Sun specifications in question that we want to duplicate are JSR168 and JSR286...
3
2009-09-04T14:27:28Z
5,405,069
<p>One way to get around this could be using a WSRP (Web Services for Remote Portlets, see Wikipedia) producer, that converts a JSR 168/286 into web services and consume them from django. But it seems that WSRP has not been very popular and I couldn't find any Python platform implementations (although partial works cou...
0
2011-03-23T12:13:58Z
[ "python", "django", "jsr168", "jsr286" ]
pytz localize vs datetime replace
1,379,740
<p>I'm having some weird issues with pytz's .localize() function. Sometimes it wouldn't make adjustments to the localized datetime:</p> <p>.localize behaviour:</p> <pre><code>&gt;&gt;&gt; tz &lt;DstTzInfo 'Africa/Abidjan' LMT-1 day, 23:44:00 STD&gt; &gt;&gt;&gt; d datetime.datetime(2009, 9, 2, 14, 45, 42, 91421) &g...
25
2009-09-04T14:50:11Z
1,379,874
<p><code>localize</code> just assumes that the naive datetime you pass it is "right" (except for not knowing about the timezone!) and so just sets the timezone, no other adjustments.</p> <p>You can (and it's advisable...) internally work in UTC (rather than with naive datetimes) and use <code>replace</code> when you n...
19
2009-09-04T15:12:52Z
[ "python", "datetime", "timezone", "utc", "pytz" ]
pytz localize vs datetime replace
1,379,740
<p>I'm having some weird issues with pytz's .localize() function. Sometimes it wouldn't make adjustments to the localized datetime:</p> <p>.localize behaviour:</p> <pre><code>&gt;&gt;&gt; tz &lt;DstTzInfo 'Africa/Abidjan' LMT-1 day, 23:44:00 STD&gt; &gt;&gt;&gt; d datetime.datetime(2009, 9, 2, 14, 45, 42, 91421) &g...
25
2009-09-04T14:50:11Z
1,592,837
<p>This DstTzInfo class is used for timezones where the offset from UTC changes at certain points in time. For example (as you are probably aware), many locations transition to "daylight savings time" at the beginning of Summer, and then back to "standard time" at the end of Summer. Each DstTzInfo instance only repre...
4
2009-10-20T06:40:10Z
[ "python", "datetime", "timezone", "utc", "pytz" ]
pytz localize vs datetime replace
1,379,740
<p>I'm having some weird issues with pytz's .localize() function. Sometimes it wouldn't make adjustments to the localized datetime:</p> <p>.localize behaviour:</p> <pre><code>&gt;&gt;&gt; tz &lt;DstTzInfo 'Africa/Abidjan' LMT-1 day, 23:44:00 STD&gt; &gt;&gt;&gt; d datetime.datetime(2009, 9, 2, 14, 45, 42, 91421) &g...
25
2009-09-04T14:50:11Z
4,693,938
<p>I realize I'm a little late on this... but here is what I found to work well. Work in UTC as Alex stated: </p> <pre><code>tz = pytz.timezone('Africa/Abidjan') now = datetime.datetime.utcnow() </code></pre> <p>Then to localize:</p> <pre><code>tzoffset = tz.utcoffset(now) mynow = now+tzoffset </code></pre> <p>And...
5
2011-01-14T17:23:16Z
[ "python", "datetime", "timezone", "utc", "pytz" ]
Django and weird legacy database tables
1,379,905
<p>I'm trying to integrate a legacy database in Django.</p> <p>I'm running into problems with some weird tables that result from horribly bad database design, but I'm not free to change it.</p> <p>The problem is that there are tables that dont have a primarykey ID, but a product ID and, here comes the problem, a lot ...
0
2009-09-04T15:18:32Z
1,380,703
<p>Django's ORM will have trouble working with this table unless you add a unique primary key column.</p> <p>If you do add a primary key, then it would be trivial to write a method to query for a given product ID and return a list of the values corresponding to that product ID. Something like:</p> <pre><code>def name...
2
2009-09-04T18:00:32Z
[ "python", "database", "django", "table", "legacy" ]
Splitting up a list into parts of balanced lengths
1,380,162
<p>I need an algorithm which given a list <code>L</code> and a number <code>N</code>, returns a list of <code>N</code> smaller lists where the sublists are "balanced". Examples:</p> <pre><code>algo(range(1, 8), 3) -&gt; [[1,2,3], [4,5], [6,7]] algo(range(1, 6), 4) -&gt; [[1,2], [3], [4], [5]] algo(range(1, 12), 5) ...
6
2009-09-04T16:04:41Z
1,380,225
<p>If I understand your problem... you would only have to add one item for each list under mod(n), where you have algo (range(a,b), n)</p> <p>So you should:</p> <ol> <li>Have b-a > n</li> <li>Calculate b-a = n*x + y (I dont really know if the operator % exists on python, so you should get y)</li> <li>The first y list...
0
2009-09-04T16:16:46Z
[ "python", "algorithm" ]
Splitting up a list into parts of balanced lengths
1,380,162
<p>I need an algorithm which given a list <code>L</code> and a number <code>N</code>, returns a list of <code>N</code> smaller lists where the sublists are "balanced". Examples:</p> <pre><code>algo(range(1, 8), 3) -&gt; [[1,2,3], [4,5], [6,7]] algo(range(1, 6), 4) -&gt; [[1,2], [3], [4], [5]] algo(range(1, 12), 5) ...
6
2009-09-04T16:04:41Z
1,380,256
<p>This is the code I came up with, without the sorting. Just slap on a lst.sort() if the input is not sorted.</p> <p>I think this came out nicely, using iterators and using islice to cut off the next piece.</p> <pre><code>import itertools def partlst(lst, n): """Partition @lst in @n balanced parts, in given ord...
4
2009-09-04T16:21:49Z
[ "python", "algorithm" ]
Splitting up a list into parts of balanced lengths
1,380,162
<p>I need an algorithm which given a list <code>L</code> and a number <code>N</code>, returns a list of <code>N</code> smaller lists where the sublists are "balanced". Examples:</p> <pre><code>algo(range(1, 8), 3) -&gt; [[1,2,3], [4,5], [6,7]] algo(range(1, 6), 4) -&gt; [[1,2], [3], [4], [5]] algo(range(1, 12), 5) ...
6
2009-09-04T16:04:41Z
1,380,306
<p>Assuming you want output to contain lists of equal length when possible, otherwise give preference to lists in the beginning. Difference between lengths of sub-lists no more than one.</p> <pre><code>&gt;&gt;&gt; l = [0, 1, 2, 3, 4, 5, 6] &gt;&gt;&gt; def algo(li, n): a, b = divmod(len(li), n) c = [a...
1
2009-09-04T16:33:02Z
[ "python", "algorithm" ]