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
Detect script start up from command prompt or "double click" on Windows
558,776
<p>Is is possible to detect if a Python script was started from the command prompt or by a user "double clicking" a .py file in the file explorer on Windows?</p>
4
2009-02-17T21:20:24Z
38,834,845
<p>I put this little function (pybyebye()) just before the return statement in some of my programs. I have tested it under Windows 10 on my desktop and laptop and it does what I want, i.e. it pauses awaiting user input only when the program has been started by double-clicking the program in File Explorer. This prevents...
0
2016-08-08T17:03:50Z
[ "python", "windows" ]
Issue with PyAMF, Django, and Python's "property" feature
558,926
<p>So far, I've had great success using PyAMF to communicate between my Flex front-end and my Django back-end. However, I believe I've encountered a bug. The following example (emphasis on the word "example") demonstrates the (potential) bug:</p> <p>My Flex app contains the following VO:</p> <pre><code>package mypr...
2
2009-02-17T22:05:10Z
560,919
<p>Not at all.</p> <p>Do you think the error is from Django or from Flex? You could first of all trace the AMF Object in Flex. If the value there is allready 0 then have a good look what PyAMF does.</p>
-1
2009-02-18T12:47:55Z
[ "python", "django", "flex", "flex3", "pyamf" ]
Issue with PyAMF, Django, and Python's "property" feature
558,926
<p>So far, I've had great success using PyAMF to communicate between my Flex front-end and my Django back-end. However, I believe I've encountered a bug. The following example (emphasis on the word "example") demonstrates the (potential) bug:</p> <p>My Flex app contains the following VO:</p> <pre><code>package mypr...
2
2009-02-17T22:05:10Z
561,072
<p>I just received the following response from PyAMF's lead developer. It's definitely a bug:</p> <blockquote> <p>This is a bug in the way the Django adapter handles non models.fields.* properties.</p> <p>If I do:</p> </blockquote> <pre><code>import pyamf class Book(object): def _get_number_of_odd_pa...
1
2009-02-18T13:35:27Z
[ "python", "django", "flex", "flex3", "pyamf" ]
Eclipse PyDev: setting breakpoints in site-packages source
558,999
<p>I am debugging a problem in Django with <strong>Pydev</strong>.<br /> I <strong>can set breakpoint</strong> in my django <strong>project code</strong> with out a problem.<br /> However I <strong>can't set breakpoints</strong> in the <strong>Django library source code</strong> (in site-packages). </p> <p>The PyDev ...
4
2009-02-17T22:25:46Z
559,411
<p>You might try instead the Python debugger pdb in this instance.</p> <p>This is a helpful link describing it: <a href="http://www.ferg.org/papers/debugging_in_python.html" rel="nofollow">http://www.ferg.org/papers/debugging_in_python.html</a></p>
0
2009-02-18T00:54:32Z
[ "python", "django", "eclipse", "pydev" ]
Eclipse PyDev: setting breakpoints in site-packages source
558,999
<p>I am debugging a problem in Django with <strong>Pydev</strong>.<br /> I <strong>can set breakpoint</strong> in my django <strong>project code</strong> with out a problem.<br /> However I <strong>can't set breakpoints</strong> in the <strong>Django library source code</strong> (in site-packages). </p> <p>The PyDev ...
4
2009-02-17T22:25:46Z
559,588
<p>Hey, this is timely! Eric Moritz just announced the release of an interesting new way to debug views using pdb called <a href="http://eric.themoritzfamily.com/2009/02/17/announcing-django-viewtools/" rel="nofollow">django-viewtools</a>.</p>
1
2009-02-18T02:06:51Z
[ "python", "django", "eclipse", "pydev" ]
Eclipse PyDev: setting breakpoints in site-packages source
558,999
<p>I am debugging a problem in Django with <strong>Pydev</strong>.<br /> I <strong>can set breakpoint</strong> in my django <strong>project code</strong> with out a problem.<br /> However I <strong>can't set breakpoints</strong> in the <strong>Django library source code</strong> (in site-packages). </p> <p>The PyDev ...
4
2009-02-17T22:25:46Z
561,957
<p>Have you imported the Django source as a project? To do that you just create a new PyDev project and set it's location to the Django source folder.</p>
4
2009-02-18T17:03:21Z
[ "python", "django", "eclipse", "pydev" ]
Eclipse PyDev: setting breakpoints in site-packages source
558,999
<p>I am debugging a problem in Django with <strong>Pydev</strong>.<br /> I <strong>can set breakpoint</strong> in my django <strong>project code</strong> with out a problem.<br /> However I <strong>can't set breakpoints</strong> in the <strong>Django library source code</strong> (in site-packages). </p> <p>The PyDev ...
4
2009-02-17T22:25:46Z
2,516,750
<p>PyDev 1.5.5 seems to have an issue with Eclipse. Uninstall 1.5.5 and install the 1.5.4 version</p>
0
2010-03-25T15:17:27Z
[ "python", "django", "eclipse", "pydev" ]
Check whether a PDF-File is valid (Python)
559,096
<p><strong>I get a File via a HTTP-Upload and need to be sure its a pdf-file.</strong> Programing Language is Python, but this should not matter.</p> <p>I thought of the following solutions:</p> <ol> <li><p>Check if the first bytes of the string are "%PDF". <em>This is not a good check but prevents the use from uploa...
10
2009-02-17T22:53:27Z
559,116
<p>If you're on a Linux or OS X box, you could use <a href="http://en.wikipedia.org/wiki/Pdftotext" rel="nofollow">Pdftotext</a> (part of Xpdf, found <a href="http://www.foolabs.com/xpdf/download.html" rel="nofollow">here</a>). If you pass a non-PDF to pdftotext, it will certainly bark at you, and you can use commands...
2
2009-02-17T23:00:05Z
[ "python", "file", "pdf" ]
Check whether a PDF-File is valid (Python)
559,096
<p><strong>I get a File via a HTTP-Upload and need to be sure its a pdf-file.</strong> Programing Language is Python, but this should not matter.</p> <p>I thought of the following solutions:</p> <ol> <li><p>Check if the first bytes of the string are "%PDF". <em>This is not a good check but prevents the use from uploa...
10
2009-02-17T22:53:27Z
559,176
<p>In a project if mine I need to check for the mime type of some uploaded file. I simply use the file command like this:</p> <pre><code>from subprocess import Popen, PIPE filetype = Popen("/usr/bin/file -b --mime -", shell=True, stdout=PIPE, stdin=PIPE).communicate(file.read(1024))[0].strip() </code></pre> <p>You of...
7
2009-02-17T23:19:52Z
[ "python", "file", "pdf" ]
Check whether a PDF-File is valid (Python)
559,096
<p><strong>I get a File via a HTTP-Upload and need to be sure its a pdf-file.</strong> Programing Language is Python, but this should not matter.</p> <p>I thought of the following solutions:</p> <ol> <li><p>Check if the first bytes of the string are "%PDF". <em>This is not a good check but prevents the use from uploa...
10
2009-02-17T22:53:27Z
559,442
<p>The two most commonly used PDF libraries for Python are:</p> <ul> <li><a href="http://pybrary.net/pyPdf/">pyPdf</a></li> <li><a href="http://www.reportlab.org/downloads.html">ReportLab</a></li> </ul> <p>Both are pure python so should be easy to install as well be cross-platform.</p> <p>With pyPdf it would probabl...
9
2009-02-18T01:10:35Z
[ "python", "file", "pdf" ]
Check whether a PDF-File is valid (Python)
559,096
<p><strong>I get a File via a HTTP-Upload and need to be sure its a pdf-file.</strong> Programing Language is Python, but this should not matter.</p> <p>I thought of the following solutions:</p> <ol> <li><p>Check if the first bytes of the string are "%PDF". <em>This is not a good check but prevents the use from uploa...
10
2009-02-17T22:53:27Z
584,225
<p>By valid do you mean that it can be displayed by a PDF viewer, or that the text can be extracted? They are two very different things.</p> <p>If you just want to check that it really is a PDF file that has been uploaded then the pyPDF solution, or something similar, will work. </p> <p>If, however, you want to check...
0
2009-02-25T00:10:24Z
[ "python", "file", "pdf" ]
Check whether a PDF-File is valid (Python)
559,096
<p><strong>I get a File via a HTTP-Upload and need to be sure its a pdf-file.</strong> Programing Language is Python, but this should not matter.</p> <p>I thought of the following solutions:</p> <ol> <li><p>Check if the first bytes of the string are "%PDF". <em>This is not a good check but prevents the use from uploa...
10
2009-02-17T22:53:27Z
32,654,661
<p>Since apparently neither <code>PyPdf</code> nor <code>ReportLab</code> is available anymore, the current solution I found (as of 2015) is to use <a href="https://github.com/mstamy2/PyPDF2" rel="nofollow"><code>PyPDF2</code></a> and catch exceptions (and possibly analyze <a href="https://pythonhosted.org/PyPDF2/PdfFi...
1
2015-09-18T14:36:41Z
[ "python", "file", "pdf" ]
How to display data using openlayers with OpenStreetMap in geodjango?
559,431
<p>I've got geodjango running using <a href="http://openlayers.org/">openlayers</a> and <a href="http://www.openstreetmap.org/">OpenStreetMaps</a> with the admin app.</p> <p>Now I want to write some views to display the data. Basically, I just want to add a list of points (seen in the admin) to the map.</p> <p>Geodja...
12
2009-02-18T01:03:40Z
569,890
<p>I think your solution is workable and probably the easiest approach. Just templatize the javascript and use Django to inject your data points as the template is rendered. </p> <p>If you wanted to get fancier, you could have a Django view that served up the data points as JSON (application/json) and then use AJAX ...
2
2009-02-20T15:09:57Z
[ "python", "mapping", "openlayers", "geodjango" ]
How to display data using openlayers with OpenStreetMap in geodjango?
559,431
<p>I've got geodjango running using <a href="http://openlayers.org/">openlayers</a> and <a href="http://www.openstreetmap.org/">OpenStreetMaps</a> with the admin app.</p> <p>Now I want to write some views to display the data. Basically, I just want to add a list of points (seen in the admin) to the map.</p> <p>Geodja...
12
2009-02-18T01:03:40Z
1,976,231
<p>Another solution is to create a form that utilizes the GeoDjango Admin widget. </p> <p>To do this, I:</p> <p>Setup a GeneratePolygonAdminClass:</p> <pre><code>class GeneratePolygonAdmin(admin.GeoModelAdmin): list_filter=('polygon',) list_display=('object', 'polygon') </code></pre> <p>Where the form is b...
4
2009-12-29T18:51:11Z
[ "python", "mapping", "openlayers", "geodjango" ]
How to display data using openlayers with OpenStreetMap in geodjango?
559,431
<p>I've got geodjango running using <a href="http://openlayers.org/">openlayers</a> and <a href="http://www.openstreetmap.org/">OpenStreetMaps</a> with the admin app.</p> <p>Now I want to write some views to display the data. Basically, I just want to add a list of points (seen in the admin) to the map.</p> <p>Geodja...
12
2009-02-18T01:03:40Z
7,641,372
<p>You could consider using <a href="http://django-floppyforms.readthedocs.org/en/latest/geodjango.html" rel="nofollow">FloppyForms</a>. In the end, I usually end up customizing the solution to my own needs, but it's a nice way to get started.</p>
1
2011-10-03T22:26:06Z
[ "python", "mapping", "openlayers", "geodjango" ]
How to display data using openlayers with OpenStreetMap in geodjango?
559,431
<p>I've got geodjango running using <a href="http://openlayers.org/">openlayers</a> and <a href="http://www.openstreetmap.org/">OpenStreetMaps</a> with the admin app.</p> <p>Now I want to write some views to display the data. Basically, I just want to add a list of points (seen in the admin) to the map.</p> <p>Geodja...
12
2009-02-18T01:03:40Z
8,839,431
<p>Checkout this tutorial from the geodjango-basic-apps project:<br> <a href="http://code.google.com/p/geodjango-basic-apps/wiki/FOSS4GWorkshop" rel="nofollow">http://code.google.com/p/geodjango-basic-apps/wiki/FOSS4GWorkshop</a></p> <p>maybe you don't have to hack up your own javascript just yet</p>
0
2012-01-12T17:25:58Z
[ "python", "mapping", "openlayers", "geodjango" ]
How to display data using openlayers with OpenStreetMap in geodjango?
559,431
<p>I've got geodjango running using <a href="http://openlayers.org/">openlayers</a> and <a href="http://www.openstreetmap.org/">OpenStreetMaps</a> with the admin app.</p> <p>Now I want to write some views to display the data. Basically, I just want to add a list of points (seen in the admin) to the map.</p> <p>Geodja...
12
2009-02-18T01:03:40Z
13,852,322
<p>This is quite old, and I wouldn't go around creating a template hack as I originally was thinking. Now I would use <a href="http://leafletjs.com/examples/geojson.html" rel="nofollow">leaflet.js</a> with an ajax request to a django view that returns geojson to a leaflet geojson layer.</p> <p>This makes the django s...
2
2012-12-13T03:11:07Z
[ "python", "mapping", "openlayers", "geodjango" ]
How do I validate the MX record for a domain in python?
559,436
<p>I have a large number of email addresses to validate. Initially I parse them with a regexp to throw out the completely crazy ones. I'm left with the ones that look sensible but still might contain errors. </p> <p>I want to find which addresses have valid domains, so given me@abcxyz.com I want to know if it's even p...
13
2009-02-18T01:06:20Z
559,479
<p>There is no DNS interface in the standard library so you will either have to roll your own or use a third party library.</p> <p>This is not a fast-changing concept though, so the external libraries are stable and well tested.</p> <p>The one I've used successful for the same task as your question is <a href="http:/...
16
2009-02-18T01:25:06Z
[ "python", "email", "dns" ]
How do I validate the MX record for a domain in python?
559,436
<p>I have a large number of email addresses to validate. Initially I parse them with a regexp to throw out the completely crazy ones. I'm left with the ones that look sensible but still might contain errors. </p> <p>I want to find which addresses have valid domains, so given me@abcxyz.com I want to know if it's even p...
13
2009-02-18T01:06:20Z
37,038,815
<p>The easy way to do this NOT in the standard library is to use the <a href="https://pypi.python.org/pypi/validate_email" rel="nofollow" title="validate_email package">validate_email package</a>: </p> <pre><code>from validate_email import validate_email is_valid = validate_email('example@example.com', check_mx=True) ...
0
2016-05-04T21:59:14Z
[ "python", "email", "dns" ]
Need a better way to execute console commands from python and log the results
559,578
<p>I have a python script which needs to execute several command line utilities. The stdout output is sometimes used for further processing. In all cases, I want to log the results and raise an exception if an error is detected. I use the following function to achieve this:</p> <pre><code>def execute(cmd, logsink): ...
4
2009-02-18T02:02:08Z
559,676
<p>You can redirect to a file if you just want the output in a file for later evaluation.</p> <p>Your already defining the stdout/stderr of the processes your executuing by the stdout=/stderr= methods.</p> <p>In your example code your just redirecting to the scripts current out/err assigments.</p> <pre><code>subproc...
5
2009-02-18T02:58:26Z
[ "python", "logging", "command-line" ]
Need a better way to execute console commands from python and log the results
559,578
<p>I have a python script which needs to execute several command line utilities. The stdout output is sometimes used for further processing. In all cases, I want to log the results and raise an exception if an error is detected. I use the following function to achieve this:</p> <pre><code>def execute(cmd, logsink): ...
4
2009-02-18T02:02:08Z
560,619
<p>You can look into pexpect (<a href="http://www.noah.org/wiki/Pexpect" rel="nofollow">http://www.noah.org/wiki/Pexpect</a>)</p> <p>It solves 1) and 2) out of the box, prefixing the output might be a little trickier.</p>
2
2009-02-18T11:06:56Z
[ "python", "logging", "command-line" ]
Need a better way to execute console commands from python and log the results
559,578
<p>I have a python script which needs to execute several command line utilities. The stdout output is sometimes used for further processing. In all cases, I want to log the results and raise an exception if an error is detected. I use the following function to achieve this:</p> <pre><code>def execute(cmd, logsink): ...
4
2009-02-18T02:02:08Z
2,448,906
<p>One other option:</p> <pre><code>def run_test(test_cmd): with tempfile.TemporaryFile() as cmd_out: proc = subprocess.Popen(test_cmd, stdout=cmd_out, stderr=cmd_out) proc.wait() cmd_out.seek(0) output = "".join(cmd_out.readlines()) return (proc.returncode, output) </code></pre...
1
2010-03-15T16:55:12Z
[ "python", "logging", "command-line" ]
Need a better way to execute console commands from python and log the results
559,578
<p>I have a python script which needs to execute several command line utilities. The stdout output is sometimes used for further processing. In all cases, I want to log the results and raise an exception if an error is detected. I use the following function to achieve this:</p> <pre><code>def execute(cmd, logsink): ...
4
2009-02-18T02:02:08Z
3,097,473
<p>This is by no means a complete or exhaustive answer, but perhaps you should look into the Fabric module. </p> <p><a href="http://docs.fabfile.org/0.9.1/" rel="nofollow">http://docs.fabfile.org/0.9.1/</a></p> <p>Makes parallel execution of shell commands and error handling rather easy. </p>
1
2010-06-22T22:00:37Z
[ "python", "logging", "command-line" ]
Shared folder sessions in Python
559,662
<p>I'm trying to get a list of currently-open sessions in Python via WMI.</p> <p>What I'm after is the exact information displayed in the Computer Management thingy, when you go to <strong>System Tools</strong> -> <strong>Shared Folders</strong> -> <strong>Sessions</strong> (ie username, computer name, connected time,...
1
2009-02-18T02:48:10Z
559,896
<p>Never mind -- I found it:</p> <pre><code>&gt;&gt;&gt; import wmi &gt;&gt;&gt; c = wmi.WMI() &gt;&gt;&gt; for x in c.Win32_ConnectionShare(): print "%s: %s" % (x.Dependent.Username, x.Dependent.ComputerName) </code></pre>
1
2009-02-18T04:59:36Z
[ "python", "winapi", "wmi", "shared-folders" ]
Django - queries made repeat/inefficient
559,701
<p>Alright, I have a Django view, like this:</p> <pre><code>@render_to('home/main.html') def login(request): # also tried Client.objects.select_related().all() clients = Client.objects.all() return {'clients':clients} </code></pre> <p>And I have a template, <code>main.html</code>, like this:</p> <pre><co...
2
2009-02-18T03:12:29Z
559,708
<p>Django uses a cache. The RDBMS uses a cache. Don't prematurely optimize the queries. </p> <p>You can play with bulk queries in your view function instead of one-at-a-time queries in your template. </p> <pre><code>@render_to('home/main.html') def login(request): # Query all clients clients = Client.obj...
7
2009-02-18T03:18:27Z
[ "python", "django" ]
Django - queries made repeat/inefficient
559,701
<p>Alright, I have a Django view, like this:</p> <pre><code>@render_to('home/main.html') def login(request): # also tried Client.objects.select_related().all() clients = Client.objects.all() return {'clients':clients} </code></pre> <p>And I have a template, <code>main.html</code>, like this:</p> <pre><co...
2
2009-02-18T03:12:29Z
559,712
<p>try using Client.objects.all().select_related()</p> <p>This will automagically also cache related models in a single database query.</p>
4
2009-02-18T03:20:39Z
[ "python", "django" ]
Django - queries made repeat/inefficient
559,701
<p>Alright, I have a Django view, like this:</p> <pre><code>@render_to('home/main.html') def login(request): # also tried Client.objects.select_related().all() clients = Client.objects.all() return {'clients':clients} </code></pre> <p>And I have a template, <code>main.html</code>, like this:</p> <pre><co...
2
2009-02-18T03:12:29Z
560,552
<p>Does Client 1 have 2 Pets and Client 2 have 1 Pet?</p> <p>If so, that would indicate to me that <code>Pet.full_name</code> or something else you're doing in the Pet display loop is trying to access its related Client's details. Django's ORM doesn't use an <a href="http://martinfowler.com/eaaCatalog/identityMap.html...
3
2009-02-18T10:40:17Z
[ "python", "django" ]
Conditional compilation in Python
560,040
<p>How to do conditional compilation in Python ?</p> <p>Is it using DEF ?</p>
10
2009-02-18T06:26:54Z
560,044
<p>Python isn't compiled in the same sense as C or C++ or even Java, python files are compiled "on the fly", you can think of it as being similar to a interpreted language like Basic or Perl.<sub>1</sub></p> <p>You can do something equivalent to conditional compile by just using an if statement. For example:</p> <pre...
21
2009-02-18T06:30:22Z
[ "python", "conditional-compilation" ]
Conditional compilation in Python
560,040
<p>How to do conditional compilation in Python ?</p> <p>Is it using DEF ?</p>
10
2009-02-18T06:26:54Z
560,045
<p>Doesn't make much sense in a dynamic environment. If you are looking for conditional definition of functions, you can use <code>if</code>:</p> <pre><code>if happy: def makemehappy(): return "I'm good" </code></pre>
3
2009-02-18T06:31:23Z
[ "python", "conditional-compilation" ]
Conditional compilation in Python
560,040
<p>How to do conditional compilation in Python ?</p> <p>Is it using DEF ?</p>
10
2009-02-18T06:26:54Z
561,338
<p>There is actually a way to get conditional compilation, but it's very limited.</p> <pre><code>if __debug__: doSomething() </code></pre> <p>The <code>__debug__</code> flag is a special case. When calling python with the <code>-O</code> or <code>-OO</code> options, <code>__debug__</code> will be false, and the ...
15
2009-02-18T14:47:12Z
[ "python", "conditional-compilation" ]
Conditional compilation in Python
560,040
<p>How to do conditional compilation in Python ?</p> <p>Is it using DEF ?</p>
10
2009-02-18T06:26:54Z
563,187
<p>Python compiles a module automatically when you import it, so the only way to avoid compiling it is to not import it. You can write something like:</p> <pre><code>if some_condition: import some_module </code></pre> <p>But that would only work for complete modules. In C and C++ you typically use a preprocessor fo...
4
2009-02-18T22:43:46Z
[ "python", "conditional-compilation" ]
Conditional compilation in Python
560,040
<p>How to do conditional compilation in Python ?</p> <p>Is it using DEF ?</p>
10
2009-02-18T06:26:54Z
5,320,530
<p>Use <a href="http://github.com/evanplaice/pypreprocessor/" rel="nofollow">pypreprocessor</a></p> <p>Which can also be found on <a href="http://pypi.python.org/pypi/pypreprocessor" rel="nofollow">PYPI (Python Package Index)</a> and can be fetched using pip.</p> <p>The basic example of usage is:</p> <pre><code>from...
6
2011-03-16T02:53:03Z
[ "python", "conditional-compilation" ]
Conditional compilation in Python
560,040
<p>How to do conditional compilation in Python ?</p> <p>Is it using DEF ?</p>
10
2009-02-18T06:26:54Z
6,420,277
<p>You could use the method discussed here: <a href="http://stackoverflow.com/questions/1592565/determine-if-variable-is-defined-in-python">Determine if variable is defined in Python</a> as a substitute for <code>#ifdef</code> </p>
0
2011-06-21T04:16:17Z
[ "python", "conditional-compilation" ]
How do you construct an array suitable for numpy sorting?
560,283
<p>I need to sort two arrays simultaneously, or rather I need to sort one of the arrays and bring the corresponding element of its associated array with it as I sort. That is if the array is [(5, 33), (4, 44), (3, 55)] and I sort by the first axis (labeled below dtype='alpha') then I want: [(3.0, 55.0) (4.0, 44.0) (5....
1
2009-02-18T08:45:47Z
560,503
<p>I think you just need to specify the axis that you are sorting on when you have made your final ndarray. Alternatively argsort one of the original arrays and you'll have an index array that you can use to look up in both x and y, which might mean you don't need values at all.</p> <p>(scipy.org seems to be unreacha...
2
2009-02-18T10:18:49Z
[ "python", "algorithm", "arrays", "numpy" ]
How do you construct an array suitable for numpy sorting?
560,283
<p>I need to sort two arrays simultaneously, or rather I need to sort one of the arrays and bring the corresponding element of its associated array with it as I sort. That is if the array is [(5, 33), (4, 44), (3, 55)] and I sort by the first axis (labeled below dtype='alpha') then I want: [(3.0, 55.0) (4.0, 44.0) (5....
1
2009-02-18T08:45:47Z
560,505
<p>I think what you want is the zip function. If you have </p> <pre><code>x = [1,2,3] y = [4,5,6] </code></pre> <p>then <code>zip(x,y) == [(1,4),(2,5),(3,6)]</code></p> <p>So your array could be constructed using</p> <pre><code>a = numpy.array(zip(x,y), dtype=dtype) </code></pre>
6
2009-02-18T10:19:56Z
[ "python", "algorithm", "arrays", "numpy" ]
How do you construct an array suitable for numpy sorting?
560,283
<p>I need to sort two arrays simultaneously, or rather I need to sort one of the arrays and bring the corresponding element of its associated array with it as I sort. That is if the array is [(5, 33), (4, 44), (3, 55)] and I sort by the first axis (labeled below dtype='alpha') then I want: [(3.0, 55.0) (4.0, 44.0) (5....
1
2009-02-18T08:45:47Z
560,521
<p>I couldn't get a working solution using Numpy's <code>sort</code> function, but here's something else that works:</p> <pre><code>import numpy x = [5,4,3] y = [33,44,55] r = numpy.asarray([(x[i],y[i]) for i in numpy.lexsort([x])]) </code></pre> <p><code>lexsort</code> returns the permutation of the array indices wh...
1
2009-02-18T10:27:04Z
[ "python", "algorithm", "arrays", "numpy" ]
How do you construct an array suitable for numpy sorting?
560,283
<p>I need to sort two arrays simultaneously, or rather I need to sort one of the arrays and bring the corresponding element of its associated array with it as I sort. That is if the array is [(5, 33), (4, 44), (3, 55)] and I sort by the first axis (labeled below dtype='alpha') then I want: [(3.0, 55.0) (4.0, 44.0) (5....
1
2009-02-18T08:45:47Z
562,621
<p>for your bonus question -- zip actually unzips too:</p> <pre><code>In [1]: a = range(10) In [2]: b = range(10, 20) In [3]: c = zip(a, b) In [4]: c Out[4]: [(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), (7, 17), (8, 18), (9, 19)] In [5]: d, e = zip(*c) In [6]: d, e Out[6]: ((0, 1, 2, 3, 4,...
3
2009-02-18T20:11:46Z
[ "python", "algorithm", "arrays", "numpy" ]
How do you construct an array suitable for numpy sorting?
560,283
<p>I need to sort two arrays simultaneously, or rather I need to sort one of the arrays and bring the corresponding element of its associated array with it as I sort. That is if the array is [(5, 33), (4, 44), (3, 55)] and I sort by the first axis (labeled below dtype='alpha') then I want: [(3.0, 55.0) (4.0, 44.0) (5....
1
2009-02-18T08:45:47Z
1,084,546
<p>Simon suggested <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html" rel="nofollow">argsort</a> as an alternative approach; I'd recommend it as the way to go. No messy merging, zipping, or unzipping: just access by index.</p> <pre><code>idx = numpy.argsort(x) ans = [ (x[idx[i]],y[idx[i]]...
3
2009-07-05T18:09:08Z
[ "python", "algorithm", "arrays", "numpy" ]
How do you construct an array suitable for numpy sorting?
560,283
<p>I need to sort two arrays simultaneously, or rather I need to sort one of the arrays and bring the corresponding element of its associated array with it as I sort. That is if the array is [(5, 33), (4, 44), (3, 55)] and I sort by the first axis (labeled below dtype='alpha') then I want: [(3.0, 55.0) (4.0, 44.0) (5....
1
2009-02-18T08:45:47Z
2,612,647
<p><code>zip()</code> <em>might</em> be inefficient for large arrays. <a href="http://www.scipy.org/Numpy_Example_List#dstack" rel="nofollow"><code>numpy.dstack()</code></a> could be used instead of <code>zip</code>:</p> <pre><code>ndx = numpy.argsort(x) values = numpy.dstack((x[ndx], y[ndx])) </code></pre>
3
2010-04-10T07:44:08Z
[ "python", "algorithm", "arrays", "numpy" ]
How do i extract my required data from HTML file?
560,936
<p>This is the HTML I have:</p> <pre><code>p_tags = '''&lt;p class="foo-body"&gt; &lt;font class="test-proof"&gt;Full name&lt;/font&gt; Foobar&lt;br /&gt; &lt;font class="test-proof"&gt;Born&lt;/font&gt; July 7, 1923, foo, bar&lt;br /&gt; &lt;font class="test-proof"&gt;Current age&lt;/font&gt; 27 years 226 days&...
2
2009-02-18T12:51:28Z
560,973
<p>You want to find the strings preceded by > and followed by &lt;, ignoring trailing or leading whitespace. You can do this quite easily with a loop looking at each character in the string, or regular expressions could help. Something like >[ \t]*[^&lt;]+[ \t]*&lt;.</p> <p>You could also use <a href="http://www.amk.c...
0
2009-02-18T13:03:57Z
[ "python", "screen-scraping", "beautifulsoup" ]
How do i extract my required data from HTML file?
560,936
<p>This is the HTML I have:</p> <pre><code>p_tags = '''&lt;p class="foo-body"&gt; &lt;font class="test-proof"&gt;Full name&lt;/font&gt; Foobar&lt;br /&gt; &lt;font class="test-proof"&gt;Born&lt;/font&gt; July 7, 1923, foo, bar&lt;br /&gt; &lt;font class="test-proof"&gt;Current age&lt;/font&gt; 27 years 226 days&...
2
2009-02-18T12:51:28Z
561,052
<p>The issue is that your HTML is not very well thought out -- you have a "mixed content model" where your labels and your data are interleaved. Your labels are wrapped in <code>&lt;font&gt;</code> Tags, but your data is in NavigableString nodes.</p> <p>You need to iterate over the contents of <code>p_tag</code>. Th...
4
2009-02-18T13:30:15Z
[ "python", "screen-scraping", "beautifulsoup" ]
How do i extract my required data from HTML file?
560,936
<p>This is the HTML I have:</p> <pre><code>p_tags = '''&lt;p class="foo-body"&gt; &lt;font class="test-proof"&gt;Full name&lt;/font&gt; Foobar&lt;br /&gt; &lt;font class="test-proof"&gt;Born&lt;/font&gt; July 7, 1923, foo, bar&lt;br /&gt; &lt;font class="test-proof"&gt;Current age&lt;/font&gt; 27 years 226 days&...
2
2009-02-18T12:51:28Z
561,085
<p>Sorry for the unnecessarily complex code, I badly need a big dose of caffeine ;)</p> <pre><code>import re str = """&lt;p class="foo-body"&gt; &lt;font class="test-proof"&gt;Full name&lt;/font&gt; Foobar&lt;br /&gt; &lt;font class="test-proof"&gt;Born&lt;/font&gt; July 7, 1923, foo, bar&lt;br /&gt; &lt;font c...
2
2009-02-18T13:41:10Z
[ "python", "screen-scraping", "beautifulsoup" ]
How do i extract my required data from HTML file?
560,936
<p>This is the HTML I have:</p> <pre><code>p_tags = '''&lt;p class="foo-body"&gt; &lt;font class="test-proof"&gt;Full name&lt;/font&gt; Foobar&lt;br /&gt; &lt;font class="test-proof"&gt;Born&lt;/font&gt; July 7, 1923, foo, bar&lt;br /&gt; &lt;font class="test-proof"&gt;Current age&lt;/font&gt; 27 years 226 days&...
2
2009-02-18T12:51:28Z
561,106
<p>I started answering this before I realised you were using 'beautiful soup' but here's a parser that I think works with your example string written using the HTMLParser library</p> <pre><code>from HTMLParser import HTMLParser results = {} class myParse(HTMLParser): def __init__(self): self.state = "" ...
4
2009-02-18T13:50:26Z
[ "python", "screen-scraping", "beautifulsoup" ]
Implementing chat in an application?
561,301
<p>I'm making a game and I am using Python for the server side. </p> <p>It would be fairly trivial to implement chat myself using Python - that's not my question.<br> <strong>My question is</strong><br> I was just wondering if there were any pre-made chat servers or some kind of service that I would be able to implem...
3
2009-02-18T14:38:01Z
561,313
<p>I recommend using XMPP/Jabber. There are a lot of libraries for clients and servers in different languages. It's free/open source.</p> <p><a href="http://en.wikipedia.org/wiki/XMPP" rel="nofollow">http://en.wikipedia.org/wiki/XMPP</a></p>
10
2009-02-18T14:40:42Z
[ "python", "chat" ]
Implementing chat in an application?
561,301
<p>I'm making a game and I am using Python for the server side. </p> <p>It would be fairly trivial to implement chat myself using Python - that's not my question.<br> <strong>My question is</strong><br> I was just wondering if there were any pre-made chat servers or some kind of service that I would be able to implem...
3
2009-02-18T14:38:01Z
561,331
<p>Maybe you could use IRC as a chat service, I know of <a href="http://python-irclib.sourceforge.net/" rel="nofollow">irclib</a> for python, its more of a client but in theory you could use it to proxy an other IRC server from the game server.</p> <p>Its a little hackish, just thought I'd mention it</p>
1
2009-02-18T14:45:15Z
[ "python", "chat" ]
Implementing chat in an application?
561,301
<p>I'm making a game and I am using Python for the server side. </p> <p>It would be fairly trivial to implement chat myself using Python - that's not my question.<br> <strong>My question is</strong><br> I was just wondering if there were any pre-made chat servers or some kind of service that I would be able to implem...
3
2009-02-18T14:38:01Z
564,419
<p>Honestly, I think it'd be best for you to roll your own and get it tightly integrated with your program. I know there's no sense in reinventing the wheel, but there are several advantages to doing so in your case: integration, learning, security, and simplicity.</p>
1
2009-02-19T08:42:01Z
[ "python", "chat" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
561,493
<p>I'd go the 'encode integer as binary string, then base64 encode that' method you suggest, and I'd do it using struct:</p> <pre><code>&gt;&gt;&gt; import struct, base64 &gt;&gt;&gt; base64.b64encode(struct.pack('l', 47)) 'LwAAAA==' &gt;&gt;&gt; struct.unpack('l', base64.b64decode(_)) (47,) </code></pre> <p>Edit aga...
1
2009-02-18T15:27:18Z
[ "python", "url", "base64" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
561,534
<p>Base64 takes 4 bytes/characters to encode 3 bytes and can only encode multiples of 3 bytes (and adds padding otherwise).</p> <p>So representing 4 bytes (your average int) in Base64 would take 8 bytes. Encoding the same 4 bytes in hex would also take 8 bytes. So you wouldn't gain anything for a single int.</p>
4
2009-02-18T15:33:18Z
[ "python", "url", "base64" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
561,547
<p>If you are looking for a way to <em>shorten</em> the integer representation using base64, I think you need to look elsewhere. When you encode something with base64 it doesn't get shorter, in fact it gets longer. </p> <p>E.g. 11234 encoded with base64 would yield MTEyMzQ=</p> <p>When using base64 you have overlooke...
2
2009-02-18T15:35:18Z
[ "python", "url", "base64" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
561,630
<p>You don't want base64 encoding, you want to represent a base 10 numeral in numeral base X.</p> <p>If you want your base 10 numeral represented in the 26 letters available you could use: <a href="http://en.wikipedia.org/wiki/Hexavigesimal">http://en.wikipedia.org/wiki/Hexavigesimal</a>. (You can extend that example...
7
2009-02-18T15:48:31Z
[ "python", "url", "base64" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
561,631
<p>The easy bit is converting the byte string to web-safe base64:</p> <pre><code>import base64 output = base64.urlsafe_b64encode(s) </code></pre> <p>The tricky bit is the first step - convert the integer to a byte string.</p> <p>If your integers are small you're better off hex encoding them - see <a href="http://sta...
8
2009-02-18T15:48:31Z
[ "python", "url", "base64" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
561,704
<p>This answer is similar in spirit to Douglas Leeder's, with the following changes:</p> <ul> <li>It doesn't use actual Base64, so there's no padding characters</li> <li><p>Instead of converting the number first to a byte-string (base 256), it converts it directly to base 64, which has the advantage of letting you rep...
54
2009-02-18T16:00:18Z
[ "python", "url", "base64" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
561,799
<p>a little hacky, but it works:</p> <pre><code>def b64num(num_to_encode): h = hex(num_to_encode)[2:] # hex(n) returns 0xhh, strip off the 0x h = len(h) &amp; 1 and '0'+h or h # if odd number of digits, prepend '0' which hex codec requires return h.decode('hex').encode('base64') </code></pre> <p>you could ...
3
2009-02-18T16:19:56Z
[ "python", "url", "base64" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
561,809
<p>To encode <code>n</code>:</p> <pre><code>data = '' while n &gt; 0: data = chr(n &amp; 255) + data n = n &gt;&gt; 8 encoded = base64.urlsafe_b64encode(data).rstrip('=') </code></pre> <p>To decode <code>s</code>:</p> <pre><code>data = base64.urlsafe_b64decode(s + '===') decoded = 0 while len(data) &gt; 0: ...
9
2009-02-18T16:22:44Z
[ "python", "url", "base64" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
561,875
<p>You probably do not want real base64 encoding for this - it will add padding etc, potentially even resulting in larger strings than hex would for small numbers. If there's no need to interoperate with anything else, just use your own encoding. Eg. here's a function that will encode to any base (note the digits are...
13
2009-02-18T16:40:37Z
[ "python", "url", "base64" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
6,602,286
<p>I maintain a little library named zbase62: <a href="http://pypi.python.org/pypi/zbase62" rel="nofollow">http://pypi.python.org/pypi/zbase62</a></p> <p>With it you can convert from a Python 2 str object to a base-62 encoded string and vice versa:</p> <pre><code>Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GC...
3
2011-07-06T20:08:32Z
[ "python", "url", "base64" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
7,011,482
<p>I needed a signed integer, so I ended up going with:</p> <pre><code>import struct, base64 def b64encode_integer(i): return base64.urlsafe_b64encode(struct.pack('i', i)).rstrip('=\n') </code></pre> <p>Example:</p> <pre><code>&gt;&gt;&gt; b64encode_integer(1) 'AQAAAA' &gt;&gt;&gt; b64encode_integer(-1) '_____w'...
2
2011-08-10T13:23:09Z
[ "python", "url", "base64" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
18,001,426
<p>All the answers given regarding Base64 are very reasonable solutions. But they're technically incorrect. To convert an integer to the <em>shortest URL safe string</em> possible, what you want is base 66 (there are <a href="http://tools.ietf.org/html/rfc3986#section-2.3">66 URL safe characters</a>).</p> <p>That code...
18
2013-08-01T18:08:21Z
[ "python", "url", "base64" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
30,480,048
<p>I'm working on making a pip package for this.</p> <p>I recommend you use my bases.py <a href="https://github.com/kamijoutouma/bases.py" rel="nofollow">https://github.com/kamijoutouma/bases.py</a> which was inspired by bases.js</p> <pre><code>from bases import Bases bases = Bases() bases.toBase16(200) ...
2
2015-05-27T10:37:39Z
[ "python", "url", "base64" ]
How to convert an integer to the shortest url-safe string in Python?
561,486
<p>I want the shortest possible way of representing an integer in a URL. For example, 11234 can be shortened to '2be2' using hexadecimal. Since base64 uses is a 64 character encoding, it should be possible to represent an integer in base64 using even less characters than hexadecimal. The problem is I can't figure out t...
60
2009-02-18T15:25:25Z
31,845,739
<p><strong>Pure python, no dependancies, no encoding of byte strings or other nonesense, just turning a base 10 int into base 64 int with the correct RFC 4648 characters:</strong></p> <pre><code>def tetrasexagesimal(number): out="" while number&gt;=0: if number == 0: out = 'A' + out; break digi...
1
2015-08-06T02:14:30Z
[ "python", "url", "base64" ]
Import python functions into a .NET language?
561,626
<p>I am a C# .NET programmer and am learning Python. I have downloaded IronPython, and know that it can call into .NET libraries. </p> <p>I'm wondering whether there is a way to do the reverse, that is to call into some existing "classic" Python libraries in my C# code, maybe using .NET Interop.</p> <p>I'd like to ...
5
2009-02-18T15:47:53Z
562,490
<p>Ironpython 2.0 is CPython 2.5 compatible, so pure Python that uses &lt;=2.5 APIs should work fine under Ironpython. I believe Ironpython code can then be compiled into a DLL.</p> <p>For C-extensions like Pygame, you might want to take a look at <a href="http://code.google.com/p/ironclad/">Ironclad</a>. It's a proje...
5
2009-02-18T19:32:18Z
[ "c#", "python", "ironpython" ]
Import python functions into a .NET language?
561,626
<p>I am a C# .NET programmer and am learning Python. I have downloaded IronPython, and know that it can call into .NET libraries. </p> <p>I'm wondering whether there is a way to do the reverse, that is to call into some existing "classic" Python libraries in my C# code, maybe using .NET Interop.</p> <p>I'd like to ...
5
2009-02-18T15:47:53Z
564,023
<p>You can use <a href="http://pythonnet.sourceforge.net/" rel="nofollow">Python for .Net</a>, which allows you to 'use CLR services and continue to use existing Python code and C-based extensions while maintaining native execution speeds for Python code.' </p> <p>Further, 'A key goal for this project has been that P...
3
2009-02-19T05:12:58Z
[ "c#", "python", "ironpython" ]
python MySQL module class file name
561,791
<p>I am confused how directory name, file name and class name all work together.</p> <p>This is what I have at the moment</p> <pre class="lang-none prettyprint-override"><code> app.py database/ client.py staff.py order.py </code></pre> <p>Inside <code>client.py</code> I have a single class called <...
2
2009-02-18T16:16:36Z
561,860
<p>Python has two basic ways of importing content. Modules and Packages.</p> <p><hr /></p> <p>A module is simply a python file on the include path: <strong>order.py</strong></p> <p>If order.py defines a class named <strong>foo</strong>, then access to that class could be had by:</p> <pre><code>import order o = ord...
4
2009-02-18T16:37:41Z
[ "python", "file", "class", "directory", "module" ]
Detect file handle leaks in python?
561,988
<p>My program appears to be leaking file handles. How can I find out where?</p> <p>My program uses file handles in a few different places—output from child processes, call <code>ctypes</code> API (ImageMagick) opens files, and they are copied.</p> <p>It crashes in <code>shutil.copyfile</code>, but I'm pretty sure t...
7
2009-02-18T17:09:13Z
562,005
<p>Look at output from <code>ls -l /proc/$pid/fd/</code> (substituting the PID of your process, of course) to see which files are open [or, on win32, use <A HREF="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx" rel="nofollow">Process Explorer</A> to list open files]; then figure out where in your code yo...
3
2009-02-18T17:12:26Z
[ "python", "file", "handle" ]
Detect file handle leaks in python?
561,988
<p>My program appears to be leaking file handles. How can I find out where?</p> <p>My program uses file handles in a few different places—output from child processes, call <code>ctypes</code> API (ImageMagick) opens files, and they are copied.</p> <p>It crashes in <code>shutil.copyfile</code>, but I'm pretty sure t...
7
2009-02-18T17:09:13Z
562,072
<p>Use <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx" rel="nofollow">Process Explorer</a>, select your process, View->Lower Pane View->Handles - then look for what seems out of place - usually lots of the same or similar files open points to the problem.</p>
3
2009-02-18T17:28:47Z
[ "python", "file", "handle" ]
Detect file handle leaks in python?
561,988
<p>My program appears to be leaking file handles. How can I find out where?</p> <p>My program uses file handles in a few different places—output from child processes, call <code>ctypes</code> API (ImageMagick) opens files, and they are copied.</p> <p>It crashes in <code>shutil.copyfile</code>, but I'm pretty sure t...
7
2009-02-18T17:09:13Z
4,485,624
<p><code>lsof -p &lt;process_id&gt;</code> works well on several UNIX-like systems including FreeBSD.</p>
3
2010-12-19T23:01:21Z
[ "python", "file", "handle" ]
Detect file handle leaks in python?
561,988
<p>My program appears to be leaking file handles. How can I find out where?</p> <p>My program uses file handles in a few different places—output from child processes, call <code>ctypes</code> API (ImageMagick) opens files, and they are copied.</p> <p>It crashes in <code>shutil.copyfile</code>, but I'm pretty sure t...
7
2009-02-18T17:09:13Z
23,762,447
<p>I had similar problems, running out of file descriptors during subprocess.Popen() calls. I used the following script to debug on what is happening:</p> <pre><code>import os import stat _fd_types = ( ('REG', stat.S_ISREG), ('FIFO', stat.S_ISFIFO), ('DIR', stat.S_ISDIR), ('CHR', stat.S_ISCHR), ('...
3
2014-05-20T14:22:46Z
[ "python", "file", "handle" ]
wxPython - Redrawing Error when replacing wxFrame's Panel
561,991
<p>I'm creating a small wxPython utility for the first time, and I'm stuck on a problem. </p> <p>I would like to add components to an already created frame. To do this, I am destroying the frame's old panel, and creating a new panel with all new components. </p> <p>1: Is there a better way of dynamically adding conte...
0
2009-02-18T17:09:37Z
562,057
<p>1) I beleive the Sizer will let you insert elements into the existing ordering of them. That would probably be a bit faster.</p> <p>2) I don't see the behavior you're describing on OSX, but at a guess, try calling self.Layout() before self.Show() in layoutElements?</p>
1
2009-02-18T17:25:31Z
[ "python", "wxpython", "wxwidgets" ]
wxPython - Redrawing Error when replacing wxFrame's Panel
561,991
<p>I'm creating a small wxPython utility for the first time, and I'm stuck on a problem. </p> <p>I would like to add components to an already created frame. To do this, I am destroying the frame's old panel, and creating a new panel with all new components. </p> <p>1: Is there a better way of dynamically adding conte...
0
2009-02-18T17:09:37Z
828,593
<p>I had a similar problem where the panel would be squished into the upper-right corner. I solved it by calling <code>panel.Fit()</code>.</p> <p>In your example, you should call <code>self.panel.Fit()</code> after <code>self.panel.SetSizer(vbox)</code></p>
0
2009-05-06T08:29:12Z
[ "python", "wxpython", "wxwidgets" ]
python upload - where are tmp/FILES?
562,278
<p>I'm running python 2.4 from cgi and I'm trying to upload to a cloud service using a python api. In php, the $_FILE array contains a "tmp" element which is where the file lives until you place it where you want it. What's the equivalent in python? </p> <p>if I do this </p> <pre><code>fileitem = form['file'] </co...
0
2009-02-18T18:27:21Z
562,322
<p>Here's a code snippet taken from my site:</p> <pre><code>h = open("user_uploaded_file", "wb") while 1: data = form["file"].file.read(4096) if not data: break h.write(data) h.close() </code></pre> <p>Hope this helps.</p>
1
2009-02-18T18:36:34Z
[ "python", "upload", "cgi", "mosso" ]
python upload - where are tmp/FILES?
562,278
<p>I'm running python 2.4 from cgi and I'm trying to upload to a cloud service using a python api. In php, the $_FILE array contains a "tmp" element which is where the file lives until you place it where you want it. What's the equivalent in python? </p> <p>if I do this </p> <pre><code>fileitem = form['file'] </co...
0
2009-02-18T18:27:21Z
562,535
<p>The file is a real file, but the <code>cgi.FieldStorage</code> unlinked it as soon as it was created so that it would exist only as long as you keep it open, and no longer has a real path on the file system.</p> <p>You can, however, change this...</p> <p>You can extend the <code>cgi.FieldStorage</code> and replace...
2
2009-02-18T19:46:11Z
[ "python", "upload", "cgi", "mosso" ]
I want to load all of the unit-tests in a tree, can it be done?
562,349
<p>I have a heirarchical folder full of Python unit-tests. They are all importable ".py" files which define TestCase objects. This folder contains thousands of files in many nested subdirectories and was written by somebody else. I do not have permission to change it, I just have to run it.</p> <p>I want to generate a...
0
2009-02-18T18:44:56Z
562,403
<p>The <strong>nose</strong> application may be useful for you, either directly, or to show how to implement this.</p> <p><a href="http://code.google.com/p/python-nose/" rel="nofollow">http://code.google.com/p/python-nose/</a> seems to be the home page.</p> <p>Basically, what you want to do is walk the source tree (<...
4
2009-02-18T19:06:15Z
[ "python", "unit-testing" ]
I want to load all of the unit-tests in a tree, can it be done?
562,349
<p>I have a heirarchical folder full of Python unit-tests. They are all importable ".py" files which define TestCase objects. This folder contains thousands of files in many nested subdirectories and was written by somebody else. I do not have permission to change it, I just have to run it.</p> <p>I want to generate a...
0
2009-02-18T18:44:56Z
562,489
<p>Look at the unittest.TestLoader (<a href="https://docs.python.org/library/unittest.html#loading-and-running-tests" rel="nofollow">https://docs.python.org/library/unittest.html#loading-and-running-tests</a>)</p> <p>And the os.walk (<a href="https://docs.python.org/library/os.html#files-and-directories" rel="nofollow...
2
2009-02-18T19:32:06Z
[ "python", "unit-testing" ]
I want to load all of the unit-tests in a tree, can it be done?
562,349
<p>I have a heirarchical folder full of Python unit-tests. They are all importable ".py" files which define TestCase objects. This folder contains thousands of files in many nested subdirectories and was written by somebody else. I do not have permission to change it, I just have to run it.</p> <p>I want to generate a...
0
2009-02-18T18:44:56Z
562,529
<p>The test directory of the <a href="http://svn.python.org/view/python/trunk/Lib/test/" rel="nofollow">Python Library source</a> shows the way. The <a href="http://svn.python.org/view/python/trunk/Lib/test/README?view=markup" rel="nofollow">README</a> file describes how to write Python Regression Tests for library mod...
1
2009-02-18T19:43:18Z
[ "python", "unit-testing" ]
Python Imaging Library save function syntax
562,519
<p>Simple one I think but essentially I need to know what the syntax is for the save function on the PIL. The help is really vague and I can't find anything online. Any help'd be great, thanks :).</p>
11
2009-02-18T19:38:57Z
562,530
<p>From the <a href="http://effbot.org/imagingbook/image.htm#tag-Image.Image.save">PIL Handbook</a>:</p> <pre><code>im.save(outfile, options...) im.save(outfile, format, options...) </code></pre> <p>Simplest case:</p> <pre><code>im.save('my_image.png') </code></pre> <p>or whatever. In this case, the type of the im...
15
2009-02-18T19:43:19Z
[ "python", "python-imaging-library" ]
Python Imaging Library save function syntax
562,519
<p>Simple one I think but essentially I need to know what the syntax is for the save function on the PIL. The help is really vague and I can't find anything online. Any help'd be great, thanks :).</p>
11
2009-02-18T19:38:57Z
562,533
<p><code>Image.save(filename[, format[, options]])</code>. You can usually just use <code>Image.save(filename)</code> since it automatically figures out the file type for you from the extension.</p>
1
2009-02-18T19:45:19Z
[ "python", "python-imaging-library" ]
What's Python good practice for importing and offering optional features?
563,022
<p>I'm writing a piece of software over on github. It's basically a tray icon with some extra features. I want to provide a working piece of code without actually having to make the user install what are essentially dependencies for optional features and I don't actually want to import things I'm not going to use so I ...
12
2009-02-18T21:58:01Z
563,060
<p>The <code>try:</code> method does not need to be global — it can be used in any scope and so modules can be "lazy-loaded" at runtime. For example:</p> <pre><code>def foo(): try: import external_module except ImportError: pass if external_module: external_module.some_whizzy_fe...
25
2009-02-18T22:05:59Z
[ "python", "python-import" ]
What's Python good practice for importing and offering optional features?
563,022
<p>I'm writing a piece of software over on github. It's basically a tray icon with some extra features. I want to provide a working piece of code without actually having to make the user install what are essentially dependencies for optional features and I don't actually want to import things I'm not going to use so I ...
12
2009-02-18T21:58:01Z
563,075
<p>You might want to have a look at the <a href="http://docs.python.org/library/imp.html">imp module</a>, which basically does what you do manually above. So you can first look for a module with find_module() and then load it via load_module() or by simply importing it (after checking the config).</p> <p>And btw, if u...
7
2009-02-18T22:09:38Z
[ "python", "python-import" ]
What's Python good practice for importing and offering optional features?
563,022
<p>I'm writing a piece of software over on github. It's basically a tray icon with some extra features. I want to provide a working piece of code without actually having to make the user install what are essentially dependencies for optional features and I don't actually want to import things I'm not going to use so I ...
12
2009-02-18T21:58:01Z
565,179
<p>One way to handle the problem of different dependencies for different features is to implement the optional features as plugins. That way the user has control over which features are activated in the app but isn't responsible for tracking down the dependencies herself. That task then gets handled at the time of each...
0
2009-02-19T12:49:09Z
[ "python", "python-import" ]
What's Python good practice for importing and offering optional features?
563,022
<p>I'm writing a piece of software over on github. It's basically a tray icon with some extra features. I want to provide a working piece of code without actually having to make the user install what are essentially dependencies for optional features and I don't actually want to import things I'm not going to use so I ...
12
2009-02-18T21:58:01Z
38,891,837
<p>If <code>optional_module</code> is used in multiple occasions, it might become awkward to add an <code>if optional_module_available ...</code> each time. Alternatively, you might replace the module object with a dummy which raises an informative error on any attribute access. And use <code>optional_module</code> as ...
0
2016-08-11T09:02:18Z
[ "python", "python-import" ]
Getting return values from a MySQL stored procedure in Python, using MySQLdb
563,182
<p>I've got a stored procedure in a MySQL database that simply updates a date column and returns the previous date. If I call this stored procedure from the MySQL client, it works fine, but when I try to call the stored procedure from Python using MySQLdb I can't seem to get it to give me the return value.</p> <p>Her...
6
2009-02-18T22:42:25Z
563,258
<p>You still have to fetch the results.</p> <pre><code>results = cursor.fetchone() </code></pre> <p>or</p> <pre><code>results = cursor.fetchall() </code></pre> <p>etc..</p>
5
2009-02-18T23:05:57Z
[ "python", "mysql" ]
Getting return values from a MySQL stored procedure in Python, using MySQLdb
563,182
<p>I've got a stored procedure in a MySQL database that simply updates a date column and returns the previous date. If I call this stored procedure from the MySQL client, it works fine, but when I try to call the stored procedure from Python using MySQLdb I can't seem to get it to give me the return value.</p> <p>Her...
6
2009-02-18T22:42:25Z
566,260
<p>What I had to do is modify the Python code to use execute() instead of callproc(), and then use the fetchone() to get the results. I'm answering it myself since mluebke's answer wasn't entirely complete (even though it was helpful!).</p> <pre><code>mysql_cursor.execute( "call get_lastpoll();" ) results=mysql_curso...
9
2009-02-19T17:11:44Z
[ "python", "mysql" ]
Getting return values from a MySQL stored procedure in Python, using MySQLdb
563,182
<p>I've got a stored procedure in a MySQL database that simply updates a date column and returns the previous date. If I call this stored procedure from the MySQL client, it works fine, but when I try to call the stored procedure from Python using MySQLdb I can't seem to get it to give me the return value.</p> <p>Her...
6
2009-02-18T22:42:25Z
1,019,866
<p><code>callproc</code> also works fine, you don't need to use <code>execute</code>:</p> <pre><code>mysql_cursor.callproc( "get_lastpoll", () ) result = mysql_cursor.fetchone() </code></pre>
5
2009-06-19T20:19:55Z
[ "python", "mysql" ]
Getting return values from a MySQL stored procedure in Python, using MySQLdb
563,182
<p>I've got a stored procedure in a MySQL database that simply updates a date column and returns the previous date. If I call this stored procedure from the MySQL client, it works fine, but when I try to call the stored procedure from Python using MySQLdb I can't seem to get it to give me the return value.</p> <p>Her...
6
2009-02-18T22:42:25Z
19,341,993
<p>From the API documentation for the <code>MySQLdb</code> library. You will need to call <code>cursor_obj.nextset()</code> before you will see the result set returned by the stored procedure. This is because the call to the stored procedure creates a result set. The result sets returned by the stored procedure foll...
0
2013-10-13T04:53:55Z
[ "python", "mysql" ]
What's the fastest way to test the validity of a large number of well-formed URLs
563,384
<p>My project requires me to validate a large number of web URLs. These URLs have been captured by a very unreliable process which I do not control. All of the URLs have already been regexp validated and are known to be well-formed. I also know that they all have valid TLDs</p> <p>I want to be able to filter these URL...
2
2009-02-18T23:46:48Z
563,398
<p>Just send HTTP HEAD requests as shown in the accepted answer to <a href="http://stackoverflow.com/questions/107405/how-do-you-send-a-head-http-request-in-python">this question</a>.</p>
3
2009-02-18T23:54:51Z
[ "python", "http" ]
What's the fastest way to test the validity of a large number of well-formed URLs
563,384
<p>My project requires me to validate a large number of web URLs. These URLs have been captured by a very unreliable process which I do not control. All of the URLs have already been regexp validated and are known to be well-formed. I also know that they all have valid TLDs</p> <p>I want to be able to filter these URL...
2
2009-02-18T23:46:48Z
563,402
<p>I'm assuming you want to do it in Python based on your tags. In that case, I'd use httplib. Optionally, somehow group the URLs by host so you can make multiple requests in one connection for those URLs that have the same host. Use the HEAD request.</p> <pre><code>conn = httplib.HTTPConnection("example.com") conn...
6
2009-02-18T23:56:34Z
[ "python", "http" ]
What's the fastest way to test the validity of a large number of well-formed URLs
563,384
<p>My project requires me to validate a large number of web URLs. These URLs have been captured by a very unreliable process which I do not control. All of the URLs have already been regexp validated and are known to be well-formed. I also know that they all have valid TLDs</p> <p>I want to be able to filter these URL...
2
2009-02-18T23:46:48Z
563,412
<p>To really make this fast you might also use <a href="http://pypi.python.org/pypi/eventlet" rel="nofollow">eventlet</a> which uses non-blocking IO to speed things up.</p> <p>You can use a head request like this:</p> <pre><code>from eventlet import httpc try: res = httpc.head(url) except httpc.NotFound: # ha...
7
2009-02-18T23:59:04Z
[ "python", "http" ]
What's the fastest way to test the validity of a large number of well-formed URLs
563,384
<p>My project requires me to validate a large number of web URLs. These URLs have been captured by a very unreliable process which I do not control. All of the URLs have already been regexp validated and are known to be well-formed. I also know that they all have valid TLDs</p> <p>I want to be able to filter these URL...
2
2009-02-18T23:46:48Z
563,415
<p>Instead of sending an HTTP GET request for each URL you can try sending an HTTP HEAD request. They are described in <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4" rel="nofollow">this document</a>.</p>
1
2009-02-18T23:59:55Z
[ "python", "http" ]
What's the fastest way to test the validity of a large number of well-formed URLs
563,384
<p>My project requires me to validate a large number of web URLs. These URLs have been captured by a very unreliable process which I do not control. All of the URLs have already been regexp validated and are known to be well-formed. I also know that they all have valid TLDs</p> <p>I want to be able to filter these URL...
2
2009-02-18T23:46:48Z
563,416
<p>Using <a href="http://docs.python.org/library/httplib.html" rel="nofollow">httplib</a> and <a href="http://docs.python.org/library/urlparse.html" rel="nofollow">urlparse</a>:</p> <pre><code>def checkURL(url): import httplib import urlparse protocol, host, path, query, fragment = urlparse.urlsplit(url) ...
4
2009-02-19T00:00:00Z
[ "python", "http" ]
What's the fastest way to test the validity of a large number of well-formed URLs
563,384
<p>My project requires me to validate a large number of web URLs. These URLs have been captured by a very unreliable process which I do not control. All of the URLs have already been regexp validated and are known to be well-formed. I also know that they all have valid TLDs</p> <p>I want to be able to filter these URL...
2
2009-02-18T23:46:48Z
563,591
<p>This is a trivial case for <a href="http://twistedmatrix.com/" rel="nofollow">twisted</a>. There are a couple of concurrency tools you can use to slow it down, otherwise, it'll pretty much do it all at once.</p> <p>Twisted is definitely my favorite thing about python. :)</p>
0
2009-02-19T01:30:44Z
[ "python", "http" ]
What's the fastest way to test the validity of a large number of well-formed URLs
563,384
<p>My project requires me to validate a large number of web URLs. These URLs have been captured by a very unreliable process which I do not control. All of the URLs have already been regexp validated and are known to be well-formed. I also know that they all have valid TLDs</p> <p>I want to be able to filter these URL...
2
2009-02-18T23:46:48Z
583,223
<p>This might help you to start. The file sitelist.txt contains a list of URIs. You might have to install httplib2, highly recommended. I put a sleep between each request so if you have many URIs on the same site, your client will not be blacklisted for abusing resources.</p> <pre><code> import httplib2 import ti...
0
2009-02-24T19:34:30Z
[ "python", "http" ]
What's the fastest way to test the validity of a large number of well-formed URLs
563,384
<p>My project requires me to validate a large number of web URLs. These URLs have been captured by a very unreliable process which I do not control. All of the URLs have already been regexp validated and are known to be well-formed. I also know that they all have valid TLDs</p> <p>I want to be able to filter these URL...
2
2009-02-18T23:46:48Z
590,517
<p>A Python program which does a similar work (for a list of URL stored at <a href="http://del.icio.us/" rel="nofollow">del.icio.us</a>) is <a href="http://www.bortzmeyer.org/disastrous.html" rel="nofollow">disastrous</a>. </p> <p>And, yes, it uses HEAD and not GET but do note some (not HTTP standard) servers send dif...
0
2009-02-26T13:21:16Z
[ "python", "http" ]
How to stop Tkinter Frame from shrinking to fit its contents?
563,827
<p>This is the code that's giving me trouble. </p> <pre><code>f = Frame(root, width=1000, bg="blue") f.pack(fill=X, expand=True) l = Label(f, text="hi", width=10, bg="red", fg="white") l.pack() </code></pre> <p>If I comment out the lines with the Label, the Frame displays with the right width. However, adding the L...
27
2009-02-19T03:21:07Z
566,840
<p>By default, tk frames <em>shrink or grow to fit their contents</em>, which is what you want 99% of the time. The term that describes this feature is "geometry propagation". There is a <a href="http://effbot.org/tkinterbook/pack.htm#Tkinter.Pack.pack_propagate-method">command</a> to turn geometry propagation on or of...
37
2009-02-19T19:42:43Z
[ "python", "label", "tkinter", "frame" ]
How can I check the memory usage of objects in iPython?
563,840
<p>I am using iPython to run my code. I wonder if there is any module or command which would allow me to check the memory usage of an object. For instance:</p> <pre><code>In [1]: a = range(10000) In [2]: %memusage a Out[2]: 1MB </code></pre> <p>Something like <code>%memusage &lt;object&gt;</code> and return the memor...
15
2009-02-19T03:27:34Z
563,921
<p>UPDATE: Here is <a href="http://code.activestate.com/recipes/544288/">another</a>, maybe more thorough recipe for estimating the size of a python object. </p> <p>Here is a <a href="http://mail.python.org/pipermail/python-list/2008-January/472683.html">thread</a> addressing a similar question </p> <p>The solution...
13
2009-02-19T04:07:23Z
[ "python", "memory", "ipython" ]
How can I check the memory usage of objects in iPython?
563,840
<p>I am using iPython to run my code. I wonder if there is any module or command which would allow me to check the memory usage of an object. For instance:</p> <pre><code>In [1]: a = range(10000) In [2]: %memusage a Out[2]: 1MB </code></pre> <p>Something like <code>%memusage &lt;object&gt;</code> and return the memor...
15
2009-02-19T03:27:34Z
565,382
<p>Unfortunately this is not possible, but there are a number of ways of approximating the answer:</p> <ol> <li><p>for very simple objects (e.g. ints, strings, floats, doubles) which are represented more or less as simple C-language types you can simply calculate the number of bytes as with <a href="http://stackoverfl...
22
2009-02-19T13:50:09Z
[ "python", "memory", "ipython" ]
How can I check the memory usage of objects in iPython?
563,840
<p>I am using iPython to run my code. I wonder if there is any module or command which would allow me to check the memory usage of an object. For instance:</p> <pre><code>In [1]: a = range(10000) In [2]: %memusage a Out[2]: 1MB </code></pre> <p>Something like <code>%memusage &lt;object&gt;</code> and return the memor...
15
2009-02-19T03:27:34Z
15,591,157
<p>If you are using a <a href="http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html">numpy array</a>, then you can use the attribute <code>ndarray.nbytes</code> to evaluate its size in memory:</p> <pre><code>from pylab import * d = array([2,3,4,5]) d.nbytes #Output: 32 </code></pre>
11
2013-03-23T19:30:46Z
[ "python", "memory", "ipython" ]
Anybody tried mosso CloudFiles with Google AppEngine?
564,460
<p>I'm wondering if anybody tried to integrate mosso CloudFiles with an application running on Google AppEngine (mosso does not provide testing sandbox so I cann't check for myself without registering)? Looking at the code it seems that this will not work due to httplib and urllib limitations in AppEngine environment, ...
1
2009-02-19T09:01:04Z
564,966
<p>It appears to implement a simple RESTful API, so there's no reason you couldn't use it from App Engine. Previously, you'd have had to write your own library to do so, using App Engine's urlfetch API, but with the release of SDK 1.1.9, you can now use urllib and httplib instead.</p>
1
2009-02-19T11:56:20Z
[ "python", "google-app-engine", "storage", "cloud", "mosso" ]
Python - ConfigParser throwing comments
564,662
<p>Based on ConfigParser module how can I filter out and throw every comments from an ini file?</p> <pre><code>import ConfigParser config = ConfigParser.ConfigParser() config.read("sample.cfg") for section in config.sections(): print section for option in config.options(section): print option, "=", co...
1
2009-02-19T10:27:21Z
564,690
<p>according to <a href="http://docs.python.org/library/configparser.html" rel="nofollow">docs</a> lines starting with <code>;</code> or <code>#</code> will be ignored. it doesn't seem like your format satisfies that requirement. can you by any chance change format of your input file?</p> <p><strong>edit</strong>: sin...
5
2009-02-19T10:36:28Z
[ "python", "configparser" ]
Python - ConfigParser throwing comments
564,662
<p>Based on ConfigParser module how can I filter out and throw every comments from an ini file?</p> <pre><code>import ConfigParser config = ConfigParser.ConfigParser() config.read("sample.cfg") for section in config.sections(): print section for option in config.options(section): print option, "=", co...
1
2009-02-19T10:27:21Z
564,897
<p>It seems your comments are not on lines that <em>start</em> with the comment leader. It should work if the comment leader is the first character on the line.</p>
2
2009-02-19T11:40:31Z
[ "python", "configparser" ]
Python - ConfigParser throwing comments
564,662
<p>Based on ConfigParser module how can I filter out and throw every comments from an ini file?</p> <pre><code>import ConfigParser config = ConfigParser.ConfigParser() config.read("sample.cfg") for section in config.sections(): print section for option in config.options(section): print option, "=", co...
1
2009-02-19T10:27:21Z
565,332
<p>Best way is to write a commentless <code>file</code> subclass:</p> <pre><code>class CommentlessFile(file): def readline(self): line = super(CommentlessFile, self).readline() if line: line = line.split(';', 1)[0].strip() return line + '\n' else: return ...
3
2009-02-19T13:38:19Z
[ "python", "configparser" ]