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
Getting total/free RAM from within Python
1,204,378
<p>From within a Python application, how can I get the total amount of RAM of the system and how much of it is currently free, in a cross-platform way?</p> <p>Ideally, the amount of free RAM should consider only physical memory that can actually be allocated to the Python process.</p>
9
2009-07-30T03:52:42Z
21,589,439
<p>In windows I use this method. It's kinda hacky but it works using standard os library:</p> <pre><code>import os process = os.popen('wmic memorychip get capacity') result = process.read() process.close() totalMem = 0 for m in result.split(" \r\n")[1:-1]: totalMem += int(m) print totalMem / (1024**3) </code></pr...
1
2014-02-05T21:58:24Z
[ "python", "wxpython" ]
removing extensions in subdirectories
1,204,617
<p>I need to remove the extension ".tex":</p> <pre><code>./1-aoeeu/1.tex ./2-thst/2.tex ./3-oeu/3.tex ./4-uoueou/4.tex ./5-aaa/5.tex ./6-oeua/6.tex ./7-oue/7.tex </code></pre> <p>Please, do it with some tools below:</p> <ol> <li><p>Sed and find</p></li> <li><p>Ruby </p></li> <li><p>Python</p></li> </ol> <p><strong>...
1
2009-07-30T05:31:03Z
1,204,638
<p>A Python script to do the same:</p> <pre><code>import os.path, shutil def remove_ext(arg, dirname, fnames): argfiles = (os.path.join(dirname, f) for f in fnames if f.endswith(arg)) for f in argfiles: shutil.move(f, f[:-len(arg)]) os.path.walk('/some/path', remove_ext, '.tex') </code></pre>
4
2009-07-30T05:39:09Z
[ "python", "ruby", "sed", "find" ]
removing extensions in subdirectories
1,204,617
<p>I need to remove the extension ".tex":</p> <pre><code>./1-aoeeu/1.tex ./2-thst/2.tex ./3-oeu/3.tex ./4-uoueou/4.tex ./5-aaa/5.tex ./6-oeua/6.tex ./7-oue/7.tex </code></pre> <p>Please, do it with some tools below:</p> <ol> <li><p>Sed and find</p></li> <li><p>Ruby </p></li> <li><p>Python</p></li> </ol> <p><strong>...
1
2009-07-30T05:31:03Z
1,204,663
<p>One way, not necessarily the fastest (but at least the quickest developed):</p> <p><code><pre> pax> for i in *.c */*.c */*/*.c ; do ...> j=$(echo "$i" | sed 's/\.c$//') ...> echo mv "$i" "$j" ...> done </pre></code></p> <p>It's equivalent since your maxdepth is 2. The script is just echoing the <co...
1
2009-07-30T05:48:12Z
[ "python", "ruby", "sed", "find" ]
removing extensions in subdirectories
1,204,617
<p>I need to remove the extension ".tex":</p> <pre><code>./1-aoeeu/1.tex ./2-thst/2.tex ./3-oeu/3.tex ./4-uoueou/4.tex ./5-aaa/5.tex ./6-oeua/6.tex ./7-oue/7.tex </code></pre> <p>Please, do it with some tools below:</p> <ol> <li><p>Sed and find</p></li> <li><p>Ruby </p></li> <li><p>Python</p></li> </ol> <p><strong>...
1
2009-07-30T05:31:03Z
1,204,694
<p>Using <code>bash</code>, <code>find</code> and <code>mv</code> from your base directory. </p> <pre><code> for i in $(find . -type f -maxdepth 2 -name "*.tex"); do mv $i $(echo "$i" | sed 's|.tex$||'); done </code></pre> <p><hr /></p> <p><strong>Variation 2</strong> based on other answers here.</p> <pre>...
0
2009-07-30T05:57:08Z
[ "python", "ruby", "sed", "find" ]
removing extensions in subdirectories
1,204,617
<p>I need to remove the extension ".tex":</p> <pre><code>./1-aoeeu/1.tex ./2-thst/2.tex ./3-oeu/3.tex ./4-uoueou/4.tex ./5-aaa/5.tex ./6-oeua/6.tex ./7-oue/7.tex </code></pre> <p>Please, do it with some tools below:</p> <ol> <li><p>Sed and find</p></li> <li><p>Ruby </p></li> <li><p>Python</p></li> </ol> <p><strong>...
1
2009-07-30T05:31:03Z
1,204,724
<p>Using "for i in" may cause "too many parameters" errrors</p> <p>A better approach is to pipe find onto the next process.</p> <p>Example:</p> <pre><code>find . -type f -name "*.tex" | while read file do mv $file ${file%%tex}g done </code></pre> <p>(Note: Wont handle files with spaces)</p>
0
2009-07-30T06:07:28Z
[ "python", "ruby", "sed", "find" ]
removing extensions in subdirectories
1,204,617
<p>I need to remove the extension ".tex":</p> <pre><code>./1-aoeeu/1.tex ./2-thst/2.tex ./3-oeu/3.tex ./4-uoueou/4.tex ./5-aaa/5.tex ./6-oeua/6.tex ./7-oue/7.tex </code></pre> <p>Please, do it with some tools below:</p> <ol> <li><p>Sed and find</p></li> <li><p>Ruby </p></li> <li><p>Python</p></li> </ol> <p><strong>...
1
2009-07-30T05:31:03Z
1,207,202
<p>There's an excellent Perl rename script that ships with some distributions, and otherwise you can find it on the web. (I'm not sure where it resides officially, but <a href="http://tips.webdesign10.com/files/rename.pl.txt" rel="nofollow">this is it</a>). Check if your rename was written by Larry Wall (AUTHOR sectio...
0
2009-07-30T15:01:39Z
[ "python", "ruby", "sed", "find" ]
TypeError: can't multiply sequence by non-int of type 'str'
1,204,744
<pre><code>&gt;&gt;&gt; Enter muzzle velocity (m/2): 60 Enter angle (degrees): 45 Traceback (most recent call last): File "F:/Python31/Lib/idlelib/test", line 9, in &lt;module&gt; range() File "F:/Python31/Lib/idlelib/test", line 7, in range Distance = float(decimal((2*(x*x))((decimal(math.zsin(y)))*(decim...
1
2009-07-30T06:15:35Z
1,204,759
<pre><code>&gt;&gt;&gt; '60' * '60' Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; TypeError: can't multiply sequence by non-int of type 'str' </code></pre> <p>You are trying to multiply two strings together. You must convert the string input from the user to a number using <code...
5
2009-07-30T06:20:53Z
[ "python" ]
TypeError: can't multiply sequence by non-int of type 'str'
1,204,744
<pre><code>&gt;&gt;&gt; Enter muzzle velocity (m/2): 60 Enter angle (degrees): 45 Traceback (most recent call last): File "F:/Python31/Lib/idlelib/test", line 9, in &lt;module&gt; range() File "F:/Python31/Lib/idlelib/test", line 7, in range Distance = float(decimal((2*(x*x))((decimal(math.zsin(y)))*(decim...
1
2009-07-30T06:15:35Z
1,204,807
<p>You should convert the data you get from console to integers:</p> <pre><code>x = int(x) y = int(y) Distance = float(decimal((2*(x*x))((decimal(math.zsin(y)))*(decimal(math.acos(y)))))/2) </code></pre>
3
2009-07-30T06:33:59Z
[ "python" ]
TypeError: can't multiply sequence by non-int of type 'str'
1,204,744
<pre><code>&gt;&gt;&gt; Enter muzzle velocity (m/2): 60 Enter angle (degrees): 45 Traceback (most recent call last): File "F:/Python31/Lib/idlelib/test", line 9, in &lt;module&gt; range() File "F:/Python31/Lib/idlelib/test", line 7, in range Distance = float(decimal((2*(x*x))((decimal(math.zsin(y)))*(decim...
1
2009-07-30T06:15:35Z
1,204,961
<p>You are using raw_input() for getting the input. Instead use input(). It will return an Int. Make sure that you input only numbers or input() will raise an error (say in case of a string). Also, it would be nice if you name your variables properly. x and y don't convey much. (velocity and angle would be so much bett...
0
2009-07-30T07:21:39Z
[ "python" ]
Scheduling issues in python
1,205,328
<p>I'm using python to interface a hardware usb sniffer device with the python API provided by the vendor and I'm trying to read (usb packets) from the device in a separate thread in an infinite loop (which works fine). The problem is that my main loop does not seem to ever get scheduled again (my read loop gets all th...
0
2009-07-30T08:55:10Z
1,205,371
<p>I think the vendor is correct. Assuming this is CPython, there is no true parallel threading; only one thread can execute at a time. This is because of the implementation of the <a href="http://docs.python.org/glossary.html#term-global-interpreter-lock" rel="nofollow">global interpreter lock</a>.</p> <p>You may be ...
0
2009-07-30T09:04:40Z
[ "python", "multithreading", "scheduling" ]
Scheduling issues in python
1,205,328
<p>I'm using python to interface a hardware usb sniffer device with the python API provided by the vendor and I'm trying to read (usb packets) from the device in a separate thread in an infinite loop (which works fine). The problem is that my main loop does not seem to ever get scheduled again (my read loop gets all th...
0
2009-07-30T08:55:10Z
1,205,415
<p>I'm assuming you wrote a Python C module that exposes the ReadUSBDevice function, and that it's intended to block until a USB packet is received, then return it.</p> <p>The native ReadUSBDevice implementation needs to release the Python GIL while it's waiting for a USB packet, and then reacquire it when it receives...
2
2009-07-30T09:15:37Z
[ "python", "multithreading", "scheduling" ]
Scheduling issues in python
1,205,328
<p>I'm using python to interface a hardware usb sniffer device with the python API provided by the vendor and I'm trying to read (usb packets) from the device in a separate thread in an infinite loop (which works fine). The problem is that my main loop does not seem to ever get scheduled again (my read loop gets all th...
0
2009-07-30T08:55:10Z
1,205,524
<p>Your vendor would be right if yours was <strong>pure python</strong> code; however, C extensions may release the <a href="http://effbot.org/pyfaq/what-is-the-global-interpreter-lock.htm" rel="nofollow">GIL</a>, and therefore allows for actual multithreading.</p> <p>In particular, time.sleep <strong>does</strong> re...
3
2009-07-30T09:35:48Z
[ "python", "multithreading", "scheduling" ]
Debugging Ruby/Python/Groovy
1,205,343
<p>I'm rephrasing this question because it was either too uninteresting or too incomprehensible. :)</p> <p>The original question came about because I'm making the transation from Java to Groovy, but the example could apply equally when transitioning to any of the higher-level languages (Ruby, Python, Groovy).</p> <p>...
1
2009-07-30T08:57:59Z
1,422,698
<p>If I were to debug your example, the first thing I would do is break it down into multiple steps. I don't care if it's "pythonic" or "the ruby way" or "tclish" or whatever, code like that can be difficult to debug. </p> <p>That's not to say I don't write code like that. Once it's been debugged it is sometimes OK to...
3
2009-09-14T16:48:35Z
[ "python", "ruby", "debugging", "groovy", "dynamic-languages" ]
Debugging Ruby/Python/Groovy
1,205,343
<p>I'm rephrasing this question because it was either too uninteresting or too incomprehensible. :)</p> <p>The original question came about because I'm making the transation from Java to Groovy, but the example could apply equally when transitioning to any of the higher-level languages (Ruby, Python, Groovy).</p> <p>...
1
2009-07-30T08:57:59Z
1,422,710
<p>If I have to debug such a line as the one you posted I find that nothing helps as much as breaking it into stand-alone statements. That way you can see what each method receives as a parameter , and what it returns. </p> <p>Such statements make code hard to maintain.</p>
0
2009-09-14T16:51:33Z
[ "python", "ruby", "debugging", "groovy", "dynamic-languages" ]
Debugging Ruby/Python/Groovy
1,205,343
<p>I'm rephrasing this question because it was either too uninteresting or too incomprehensible. :)</p> <p>The original question came about because I'm making the transation from Java to Groovy, but the example could apply equally when transitioning to any of the higher-level languages (Ruby, Python, Groovy).</p> <p>...
1
2009-07-30T08:57:59Z
1,422,717
<p>Combining multiple actions into a single line is all well and good, when you can still look at the line in question and know that it's going to do exactly what you want it to do. The minute you get to the point where you can't look at the code and go "yeah, ok, it does xyz, there's no way it couldn't" is when you sh...
2
2009-09-14T16:52:51Z
[ "python", "ruby", "debugging", "groovy", "dynamic-languages" ]
Filter by property
1,205,375
<p>Is it possible to filter by property?</p> <p>i have a method in my model:</p> <pre><code>@property def myproperty(self): [..] </code></pre> <p>and now i want to filter by this property like:</p> <pre><code>MyModel.objects.filter(myproperty=[..]) </code></pre> <p>is this somehow possible?</p>
44
2009-07-30T09:06:23Z
1,205,389
<p>Nope. Django filters operate at the database level, generating SQL. To filter based on Python properties, you have to load the object into Python to evaluate the property--and at that point, you've already done all the work to load it.</p>
41
2009-07-30T09:09:45Z
[ "python", "django" ]
Filter by property
1,205,375
<p>Is it possible to filter by property?</p> <p>i have a method in my model:</p> <pre><code>@property def myproperty(self): [..] </code></pre> <p>and now i want to filter by this property like:</p> <pre><code>MyModel.objects.filter(myproperty=[..]) </code></pre> <p>is this somehow possible?</p>
44
2009-07-30T09:06:23Z
1,205,416
<p>I might be misunderstanding your original question, but there is a <a href="http://docs.python.org/library/functions.html#filter">filter</a> builtin in python.</p> <pre><code>filtered = filter(myproperty, MyModel.objects) </code></pre> <p>But it's better to use a <a href="http://docs.python.org/tutorial/datastruct...
19
2009-07-30T09:15:41Z
[ "python", "django" ]
Filter by property
1,205,375
<p>Is it possible to filter by property?</p> <p>i have a method in my model:</p> <pre><code>@property def myproperty(self): [..] </code></pre> <p>and now i want to filter by this property like:</p> <pre><code>MyModel.objects.filter(myproperty=[..]) </code></pre> <p>is this somehow possible?</p>
44
2009-07-30T09:06:23Z
19,334,674
<p>i know it is an old question, but for the sake of those jumping here i think it is useful to read the question below and the relative answer:</p> <p><a href="http://stackoverflow.com/questions/14617834/how-to-customize-admin-filter-in-django-1-4">How to customize admin filter in Django 1.4</a></p>
1
2013-10-12T13:23:40Z
[ "python", "django" ]
Filter by property
1,205,375
<p>Is it possible to filter by property?</p> <p>i have a method in my model:</p> <pre><code>@property def myproperty(self): [..] </code></pre> <p>and now i want to filter by this property like:</p> <pre><code>MyModel.objects.filter(myproperty=[..]) </code></pre> <p>is this somehow possible?</p>
44
2009-07-30T09:06:23Z
27,139,492
<p>PLEASE someone correct me, but I guess I have found a solution, at least for my own case.</p> <p>I want to work on all those elements whose properties are exactly equal to ... whatever. </p> <p>But I have several models, and this routine should work for all models. And it does:</p> <pre><code>def selectByProperti...
2
2014-11-26T00:46:02Z
[ "python", "django" ]
Filter by property
1,205,375
<p>Is it possible to filter by property?</p> <p>i have a method in my model:</p> <pre><code>@property def myproperty(self): [..] </code></pre> <p>and now i want to filter by this property like:</p> <pre><code>MyModel.objects.filter(myproperty=[..]) </code></pre> <p>is this somehow possible?</p>
44
2009-07-30T09:06:23Z
35,282,445
<p>Looks like <a href="https://docs.djangoproject.com/en/1.9/ref/models/expressions/#using-f-with-annotations" rel="nofollow">using F() with annotations</a> will be my solution to this.</p> <p>It's not going to filter by <code>@property</code>, since <code>F</code> talks to the databse before objects are brought into ...
2
2016-02-09T01:28:33Z
[ "python", "django" ]
Filter by property
1,205,375
<p>Is it possible to filter by property?</p> <p>i have a method in my model:</p> <pre><code>@property def myproperty(self): [..] </code></pre> <p>and now i want to filter by this property like:</p> <pre><code>MyModel.objects.filter(myproperty=[..]) </code></pre> <p>is this somehow possible?</p>
44
2009-07-30T09:06:23Z
36,996,962
<p>Riffing off @TheGrimmScientist's suggested workaround, you can make these "sql properties" by defining them on the Manager or the QuerySet, and reuse/chain/compose them:</p> <p>With a Manager:</p> <pre><code>class CompanyManager(models.Manager): def with_chairs_needed(self): return self.annotate(chairs...
0
2016-05-03T06:17:56Z
[ "python", "django" ]
Listing serial (COM) ports on Windows?
1,205,383
<p>I'm looking for a robust way to list the available serial (COM) ports on a Windows machine. There's <a href="http://stackoverflow.com/questions/1081871/how-to-find-available-com-ports">this post about using WMI</a>, but I would like something less .NET specific - I want to get the list of ports in a Python or a C++ ...
23
2009-07-30T09:08:22Z
1,205,515
<p>Several options are available:</p> <ol> <li><p>Call <a href="http://msdn.microsoft.com/en-us/library/aa365461%28VS.85%29.aspx">QueryDosDevice</a> with a NULL lpDeviceName to list all DOS devices. Then use CreateFile and <a href="http://msdn.microsoft.com/en-us/library/aa363256%28VS.85%29.aspx">GetCommConfig</a> wit...
6
2009-07-30T09:33:41Z
[ "python", "c", "windows", "winapi", "serial-port" ]
Listing serial (COM) ports on Windows?
1,205,383
<p>I'm looking for a robust way to list the available serial (COM) ports on a Windows machine. There's <a href="http://stackoverflow.com/questions/1081871/how-to-find-available-com-ports">this post about using WMI</a>, but I would like something less .NET specific - I want to get the list of ports in a Python or a C++ ...
23
2009-07-30T09:08:22Z
3,018,813
<p>I just created the following, based on reading through the C++ source to <a href="http://www.naughter.com/enumser.html" rel="nofollow">EnumSerialPorts</a> and seeing the function <code>GetDefaultCommConfig()</code>. It looked like the simplest method using simple ANSI C and a single API call for each possible COM p...
3
2010-06-10T21:49:32Z
[ "python", "c", "windows", "winapi", "serial-port" ]
Listing serial (COM) ports on Windows?
1,205,383
<p>I'm looking for a robust way to list the available serial (COM) ports on a Windows machine. There's <a href="http://stackoverflow.com/questions/1081871/how-to-find-available-com-ports">this post about using WMI</a>, but I would like something less .NET specific - I want to get the list of ports in a Python or a C++ ...
23
2009-07-30T09:08:22Z
3,018,890
<p>The PySerial project provides a <a href="http://web.archive.org/web/20150906231735/http://pyserial.sourceforge.net/examples.html" rel="nofollow">couple of solutions</a>.</p>
4
2010-06-10T22:05:30Z
[ "python", "c", "windows", "winapi", "serial-port" ]
Listing serial (COM) ports on Windows?
1,205,383
<p>I'm looking for a robust way to list the available serial (COM) ports on a Windows machine. There's <a href="http://stackoverflow.com/questions/1081871/how-to-find-available-com-ports">this post about using WMI</a>, but I would like something less .NET specific - I want to get the list of ports in a Python or a C++ ...
23
2009-07-30T09:08:22Z
8,844,967
<p>There's an example in the pyserial distro now that does this called scanwin32.py</p> <p><a href="http://pyserial.sourcearchive.com/documentation/2.5/scanwin32_8py_source.html" rel="nofollow">http://pyserial.sourcearchive.com/documentation/2.5/scanwin32_8py_source.html</a></p>
1
2012-01-13T01:55:34Z
[ "python", "c", "windows", "winapi", "serial-port" ]
Listing serial (COM) ports on Windows?
1,205,383
<p>I'm looking for a robust way to list the available serial (COM) ports on a Windows machine. There's <a href="http://stackoverflow.com/questions/1081871/how-to-find-available-com-ports">this post about using WMI</a>, but I would like something less .NET specific - I want to get the list of ports in a Python or a C++ ...
23
2009-07-30T09:08:22Z
16,944,571
<p>This is definitely fairly late, but it proved helpful to me!</p> <p><a href="http://eli.thegreenplace.net/2009/07/31/listing-all-serial-ports-on-windows-with-python/" rel="nofollow">http://eli.thegreenplace.net/2009/07/31/listing-all-serial-ports-on-windows-with-python/</a></p> <p>Particularly this example:</p> <...
3
2013-06-05T15:58:29Z
[ "python", "c", "windows", "winapi", "serial-port" ]
Listing serial (COM) ports on Windows?
1,205,383
<p>I'm looking for a robust way to list the available serial (COM) ports on a Windows machine. There's <a href="http://stackoverflow.com/questions/1081871/how-to-find-available-com-ports">this post about using WMI</a>, but I would like something less .NET specific - I want to get the list of ports in a Python or a C++ ...
23
2009-07-30T09:08:22Z
25,539,425
<p>Using pySerial with Python:</p> <pre><code>import serial.tools.list_ports ports = list(serial.tools.list_ports.comports()) for p in ports: print p </code></pre>
20
2014-08-28T01:37:32Z
[ "python", "c", "windows", "winapi", "serial-port" ]
Listing serial (COM) ports on Windows?
1,205,383
<p>I'm looking for a robust way to list the available serial (COM) ports on a Windows machine. There's <a href="http://stackoverflow.com/questions/1081871/how-to-find-available-com-ports">this post about using WMI</a>, but I would like something less .NET specific - I want to get the list of ports in a Python or a C++ ...
23
2009-07-30T09:08:22Z
29,284,793
<p>I think WMI is the way to go since it's pretty easy to get going and it has minimal code. It saves you from having to dig inside the registry and gives you some guarantee that it will work for more general situations in the future.</p> <p>You can install everything needed with <code>pip install pypiwin32 WMI</code>...
1
2015-03-26T17:13:45Z
[ "python", "c", "windows", "winapi", "serial-port" ]
Listing serial (COM) ports on Windows?
1,205,383
<p>I'm looking for a robust way to list the available serial (COM) ports on a Windows machine. There's <a href="http://stackoverflow.com/questions/1081871/how-to-find-available-com-ports">this post about using WMI</a>, but I would like something less .NET specific - I want to get the list of ports in a Python or a C++ ...
23
2009-07-30T09:08:22Z
31,887,738
<p>Nowadays there's a Powershell one-liner for that.</p> <pre><code>[System.IO.Ports.SerialPort]::GetPortNames() </code></pre>
2
2015-08-07T22:53:24Z
[ "python", "c", "windows", "winapi", "serial-port" ]
Neural net input/output
1,205,449
<p>Can anyone explain to me how to do more complex data sets like team stats, weather, dice, complex number types </p> <p>i understand all the math and how everything works i just dont know how to input more complex data, and then how to read the data it spits out </p> <p>if someone could provide examples in python t...
3
2009-07-30T09:22:31Z
1,205,509
<p>You have to encode your input and your output to something that can be represented by the neural network units. ( for example 1 for "x has a certain property p" -1 for "x doesn't have the property p" if your units' range is in [-1, 1])</p> <p>The way you encode your input and the way you decode your output depends ...
3
2009-07-30T09:32:33Z
[ "python", "neural-network" ]
Neural net input/output
1,205,449
<p>Can anyone explain to me how to do more complex data sets like team stats, weather, dice, complex number types </p> <p>i understand all the math and how everything works i just dont know how to input more complex data, and then how to read the data it spits out </p> <p>if someone could provide examples in python t...
3
2009-07-30T09:22:31Z
1,206,597
<p>More complex data usually means adding more neurons in the input and output layers.</p> <p>You can feed each "field" of your register, properly encoded as a real value (normalized, etc.) to each input neuron, or maybe you can even decompose even further into bit fields, assigning saturated inputs of 1 or 0 to the n...
0
2009-07-30T13:26:16Z
[ "python", "neural-network" ]
Neural net input/output
1,205,449
<p>Can anyone explain to me how to do more complex data sets like team stats, weather, dice, complex number types </p> <p>i understand all the math and how everything works i just dont know how to input more complex data, and then how to read the data it spits out </p> <p>if someone could provide examples in python t...
3
2009-07-30T09:22:31Z
1,207,505
<p>Your features must be decomposed into parts that can be represented as real numbers. The magic of a Neural Net is it's a black box, the correct associations will be made (with internal weights) during the training</p> <p><hr /></p> <p><strong>Inputs</strong></p> <p>Choose as few features as are needed to accurate...
2
2009-07-30T15:46:57Z
[ "python", "neural-network" ]
Neural net input/output
1,205,449
<p>Can anyone explain to me how to do more complex data sets like team stats, weather, dice, complex number types </p> <p>i understand all the math and how everything works i just dont know how to input more complex data, and then how to read the data it spits out </p> <p>if someone could provide examples in python t...
3
2009-07-30T09:22:31Z
20,683,280
<p>You have to add the number of units for input and output you need for the problem. If the unknown function to approximate depends on <em>n</em> parameter, you will have n input units. The number of output units depends on the nature of the funcion. For real functions with n real parameters you will have one output u...
0
2013-12-19T13:41:09Z
[ "python", "neural-network" ]
Django - Repeating a form field n times in one form
1,205,626
<p>I have a Django form with several fields in it one of which needs to be repeated n times (where n is not known at design time) how would I go about coding this (if it is possible at all)?</p> <p>e.g. instead of :-</p> <pre><code>Class PaymentsForm(forms.form): invoice = forms.CharField(widget=ValueHiddenInput(...
4
2009-07-30T10:00:57Z
1,205,637
<p>Use <a href="http://docs.djangoproject.com/en/dev/topics/forms/formsets/" rel="nofollow">formsets</a>.</p>
3
2009-07-30T10:02:50Z
[ "python", "django", "django-forms" ]
Django - Repeating a form field n times in one form
1,205,626
<p>I have a Django form with several fields in it one of which needs to be repeated n times (where n is not known at design time) how would I go about coding this (if it is possible at all)?</p> <p>e.g. instead of :-</p> <pre><code>Class PaymentsForm(forms.form): invoice = forms.CharField(widget=ValueHiddenInput(...
4
2009-07-30T10:00:57Z
1,205,767
<p>You can create the repeated fields in the <code>__init__</code> method of your form:</p> <pre><code>class PaymentsForm(forms.Form): invoice = forms.CharField(widget=forms.HiddenInput()) total = forms.CharField(widget=forms.HiddenInput()) def __init__(self, *args, **kwargs): super(PaymentsForm, ...
8
2009-07-30T10:34:46Z
[ "python", "django", "django-forms" ]
How do I get monotonic time durations in python?
1,205,722
<p>I want to log how long something takes in real walltime. Currently I'm doing this:</p> <pre><code>startTime = time.time() someSQLOrSomething() print "That took %.3f seconds" % (time.time() - startTime) </code></pre> <p>But that will fail (produce incorrect results) if the time is adjusted while the SQL query (or w...
38
2009-07-30T10:23:55Z
1,205,762
<p>That function is simple enough that you can use ctypes to access it:</p> <pre><code>#!/usr/bin/env python __all__ = ["monotonic_time"] import ctypes, os CLOCK_MONOTONIC_RAW = 4 # see &lt;linux/time.h&gt; class timespec(ctypes.Structure): _fields_ = [ ('tv_sec', ctypes.c_long), ('tv_nsec', ct...
59
2009-07-30T10:33:40Z
[ "python", "linux", "benchmarking", "clock" ]
How do I get monotonic time durations in python?
1,205,722
<p>I want to log how long something takes in real walltime. Currently I'm doing this:</p> <pre><code>startTime = time.time() someSQLOrSomething() print "That took %.3f seconds" % (time.time() - startTime) </code></pre> <p>But that will fail (produce incorrect results) if the time is adjusted while the SQL query (or w...
38
2009-07-30T10:23:55Z
4,486,117
<p>As pointed out in <a href="http://stackoverflow.com/questions/3657289/linux-clock-gettimeclock-monotonic-strange-non-monotonic-behavior">this question</a>, avoiding NTP readjustments on Linux requires CLOCK_MONOTONIC_RAW. That's defined as 4 on Linux (since 2.6.28).</p> <p>Portably getting the correct constant #def...
8
2010-12-20T01:03:00Z
[ "python", "linux", "benchmarking", "clock" ]
How do I get monotonic time durations in python?
1,205,722
<p>I want to log how long something takes in real walltime. Currently I'm doing this:</p> <pre><code>startTime = time.time() someSQLOrSomething() print "That took %.3f seconds" % (time.time() - startTime) </code></pre> <p>But that will fail (produce incorrect results) if the time is adjusted while the SQL query (or w...
38
2009-07-30T10:23:55Z
14,416,514
<p>Now, in Python 3.3 you would use <a href="http://www.python.org/dev/peps/pep-0418/#time-monotonic">time.monotonic</a>.</p>
19
2013-01-19T16:49:11Z
[ "python", "linux", "benchmarking", "clock" ]
How can I get non-blocking socket connect()'s?
1,205,863
<p>I have a quite simple problem here. I need to communicate with a lot of hosts simultaneously, but I do not really need any synchronization because each request is pretty self sufficient.</p> <p>Because of that, I chose to work with asynchronous sockets, rather than spamming threads. Now I do have a little problem:<...
9
2009-07-30T10:58:16Z
1,205,944
<p>Did you look at the <a href="http://docs.python.org/library/asyncore.html#module-asyncore" rel="nofollow">asyncore</a> module? Might be just what you need.</p>
0
2009-07-30T11:15:18Z
[ "python", "sockets", "asynchronous", "nonblocking" ]
How can I get non-blocking socket connect()'s?
1,205,863
<p>I have a quite simple problem here. I need to communicate with a lot of hosts simultaneously, but I do not really need any synchronization because each request is pretty self sufficient.</p> <p>Because of that, I chose to work with asynchronous sockets, rather than spamming threads. Now I do have a little problem:<...
9
2009-07-30T10:58:16Z
1,205,978
<p>Use the <a href="http://docs.python.org/library/select.html"><code>select</code></a> module. This allows you to wait for I/O completion on multiple non-blocking sockets. Here's <a href="http://www.amk.ca/python/howto/sockets/sockets.html#SECTION000600000000000000000">some more information</a> on select. From the lin...
8
2009-07-30T11:21:52Z
[ "python", "sockets", "asynchronous", "nonblocking" ]
How can I get non-blocking socket connect()'s?
1,205,863
<p>I have a quite simple problem here. I need to communicate with a lot of hosts simultaneously, but I do not really need any synchronization because each request is pretty self sufficient.</p> <p>Because of that, I chose to work with asynchronous sockets, rather than spamming threads. Now I do have a little problem:<...
9
2009-07-30T10:58:16Z
1,206,726
<p>You need to parallelize the connects as well, since the sockets block when you set a timeout. Alternatively, you could not set a timeout, and use the select module.</p> <p>You can do this with the dispatcher class in the <a href="http://docs.python.org/library/asyncore.html#module-asyncore">asyncore</a> module. Tak...
4
2009-07-30T13:47:23Z
[ "python", "sockets", "asynchronous", "nonblocking" ]
How can I get non-blocking socket connect()'s?
1,205,863
<p>I have a quite simple problem here. I need to communicate with a lot of hosts simultaneously, but I do not really need any synchronization because each request is pretty self sufficient.</p> <p>Because of that, I chose to work with asynchronous sockets, rather than spamming threads. Now I do have a little problem:<...
9
2009-07-30T10:58:16Z
1,206,741
<p>Use <a href="http://twistedmatrix.com" rel="nofollow">twisted</a>.</p> <p>It is an asynchronous networking engine written in Python, supporting numerous protocols, and you can add your own. It can be used to develop clients and servers. It doesn't block on connect.</p>
4
2009-07-30T13:49:15Z
[ "python", "sockets", "asynchronous", "nonblocking" ]
How can I get non-blocking socket connect()'s?
1,205,863
<p>I have a quite simple problem here. I need to communicate with a lot of hosts simultaneously, but I do not really need any synchronization because each request is pretty self sufficient.</p> <p>Because of that, I chose to work with asynchronous sockets, rather than spamming threads. Now I do have a little problem:<...
9
2009-07-30T10:58:16Z
1,211,859
<p>Unfortunately there are is no example code which shows the bug, so it's a bit hard to see where this block comes from.</p> <p>He does something like:</p> <pre><code>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setblocking(0) s.connect(("www.nonexistingname.org", 80)) </code></pre> <p>The socket module ...
6
2009-07-31T10:40:47Z
[ "python", "sockets", "asynchronous", "nonblocking" ]
Creating a new input event dispatcher in Pyglet (infra red input)
1,206,628
<p>I recently asked this question in the pyglet-users group, but got response, so I'm trying here instead.</p> <p>I would like to extend Pyglet to be able to use an infra red input device supported by lirc. I've used pyLirc before ( <a href="http://pylirc.mccabe.nu/" rel="nofollow">http://pylirc.mccabe.nu/</a> ) with ...
1
2009-07-30T13:30:53Z
1,242,726
<p>The correct way is whatever works. You can always change it later if you find a better way.</p>
1
2009-08-07T03:23:08Z
[ "python", "pyglet" ]
Creating a new input event dispatcher in Pyglet (infra red input)
1,206,628
<p>I recently asked this question in the pyglet-users group, but got response, so I'm trying here instead.</p> <p>I would like to extend Pyglet to be able to use an infra red input device supported by lirc. I've used pyLirc before ( <a href="http://pylirc.mccabe.nu/" rel="nofollow">http://pylirc.mccabe.nu/</a> ) with ...
1
2009-07-30T13:30:53Z
1,262,678
<p>It's probably too late for the OP, but I'll reply anyway in case it's helpful to anyone else.</p> <p>Creating the event dispatcher and using pyglet.clock.schedule_interval to call poll() at regular intervals is a good way to do it.</p> <p>To attach the event dispatcher to your window, you need to create an instanc...
1
2009-08-11T20:18:55Z
[ "python", "pyglet" ]
Django ORM for desktop application
1,206,793
<p>Recently, I have become increasingly familiar with Django. I have a new project that I am working on that will be using Python for a desktop application. Is it possible to use the Django ORM in a desktop application? Or should I just go with something like <a href="http://www.sqlalchemy.org/">SQLAlchemy</a>?</p>
11
2009-07-30T13:58:56Z
1,206,816
<p>Yes it is. The Commonsense Computing Project at the MIT media lab does that for ConceptNet, a semantic network. You can get the source here: <a href="http://pypi.python.org/pypi/ConceptNet/4.0b3" rel="nofollow">http://pypi.python.org/pypi/ConceptNet/4.0b3</a></p>
3
2009-07-30T14:04:29Z
[ "python", "django", "orm" ]
Django ORM for desktop application
1,206,793
<p>Recently, I have become increasingly familiar with Django. I have a new project that I am working on that will be using Python for a desktop application. Is it possible to use the Django ORM in a desktop application? Or should I just go with something like <a href="http://www.sqlalchemy.org/">SQLAlchemy</a>?</p>
11
2009-07-30T13:58:56Z
1,206,820
<p>The Django people are sensible people with a philosophy of decoupling things. So yes, in theory you should be perfectly able to use Django's ORM in a standalone application. </p> <p>Here's one guide I found: <a href="http://jystewart.net/process/2008/02/using-the-django-orm-as-a-standalone-component/">Django ORM as...
11
2009-07-30T14:05:39Z
[ "python", "django", "orm" ]
Django ORM for desktop application
1,206,793
<p>Recently, I have become increasingly familiar with Django. I have a new project that I am working on that will be using Python for a desktop application. Is it possible to use the Django ORM in a desktop application? Or should I just go with something like <a href="http://www.sqlalchemy.org/">SQLAlchemy</a>?</p>
11
2009-07-30T13:58:56Z
1,206,837
<p>I would suggest using SQLAlchemy and a declarative layer on top of it such as <a href="http://elixir.ematia.de/trac/wiki" rel="nofollow" title="Elixir">Elixir</a> if you prefer a Django-like syntax.</p>
4
2009-07-30T14:07:59Z
[ "python", "django", "orm" ]
Django ORM for desktop application
1,206,793
<p>Recently, I have become increasingly familiar with Django. I have a new project that I am working on that will be using Python for a desktop application. Is it possible to use the Django ORM in a desktop application? Or should I just go with something like <a href="http://www.sqlalchemy.org/">SQLAlchemy</a>?</p>
11
2009-07-30T13:58:56Z
1,211,122
<p>I would suggest another ORM for a desktop application maybe SQLAlchemy or SQLObject. It i possible to use the django ORM but I think other ORM are a better ones if you are going to use them standalone.</p>
0
2009-07-31T07:19:32Z
[ "python", "django", "orm" ]
Django ORM for desktop application
1,206,793
<p>Recently, I have become increasingly familiar with Django. I have a new project that I am working on that will be using Python for a desktop application. Is it possible to use the Django ORM in a desktop application? Or should I just go with something like <a href="http://www.sqlalchemy.org/">SQLAlchemy</a>?</p>
11
2009-07-30T13:58:56Z
4,714,471
<p><a href="http://www.python-camelot.com/" rel="nofollow">Camelot</a> seems promising if you want to do Python desktop apps using a DB. It uses SQLAlchemy though. Haven't tried it yet.</p>
0
2011-01-17T14:53:23Z
[ "python", "django", "orm" ]
Django ORM for desktop application
1,206,793
<p>Recently, I have become increasingly familiar with Django. I have a new project that I am working on that will be using Python for a desktop application. Is it possible to use the Django ORM in a desktop application? Or should I just go with something like <a href="http://www.sqlalchemy.org/">SQLAlchemy</a>?</p>
11
2009-07-30T13:58:56Z
7,949,881
<p>The peewee ORM has a declarative syntax that should be familiar to django users, and can be used as a standalone. Here are the project <a href="http://charlesleifer.com/docs/peewee/" rel="nofollow">docs</a></p>
1
2011-10-31T03:57:53Z
[ "python", "django", "orm" ]
Importing the entire Python standard library
1,206,832
<p>I need a way to import the entire Python standard library into my program.</p> <p>While this may seems like a bad idea, I want to do this is so py2exe will package the entire standard library with my program, so my users could import from it in the shell that I give them.</p> <p>Is there an easy way to do this?</p...
4
2009-07-30T14:06:50Z
1,206,924
<p>Hey, I just thought of something: I only need a list of all the modules in stdlib, and then I'll automatically generate a Python script that imports each of them "manually", like this:</p> <pre><code>import re import math import time # ... </code></pre> <p>And then include that with my program.</p> <p>So all I ne...
2
2009-07-30T14:23:01Z
[ "python", "import", "packaging", "py2exe" ]
Importing the entire Python standard library
1,206,832
<p>I need a way to import the entire Python standard library into my program.</p> <p>While this may seems like a bad idea, I want to do this is so py2exe will package the entire standard library with my program, so my users could import from it in the shell that I give them.</p> <p>Is there an easy way to do this?</p...
4
2009-07-30T14:06:50Z
1,206,973
<p>I created a zip file from all the Python standard library and then added it to <code>sys.path</code> when the program started.</p> <p>You can have a look at the sources <a href="http://svn.berlios.de/svnroot/repos/sconsexe/trunk/" rel="nofollow">here</a> (abandoned project)</p>
1
2009-07-30T14:29:34Z
[ "python", "import", "packaging", "py2exe" ]
How to change a GtkTreeView style in Python?
1,207,250
<p>I have an app written in python that presents some of its data in a tree view. By default, the tree view is a floaty white affair with little floaty triangles to expand the nodes.</p> <p>Is it possible to change this style to be more like a Windows explorer tree view? Specifically, I'd like to have vertical lines i...
2
2009-07-30T15:09:07Z
1,207,847
<p>you need to create a custom <code>CellRenderer</code>s for this. the below links might help.</p> <p><a href="http://www.pygtk.org/pygtk2tutorial/ch-TreeViewWidget.html" rel="nofollow">http://www.pygtk.org/pygtk2tutorial/ch-TreeViewWidget.html</a></p> <p><a href="http://www.pygtk.org/pygtk2tutorial/sec-CellRenderer...
1
2009-07-30T16:46:20Z
[ "python", "gtk", "pygtk", "gtktreeview" ]
How to change a GtkTreeView style in Python?
1,207,250
<p>I have an app written in python that presents some of its data in a tree view. By default, the tree view is a floaty white affair with little floaty triangles to expand the nodes.</p> <p>Is it possible to change this style to be more like a Windows explorer tree view? Specifically, I'd like to have vertical lines i...
2
2009-07-30T15:09:07Z
1,208,692
<p>For lines linking the arrows there is a method in gtk.TreeView for that, see <a href="http://library.gnome.org/devel/pygtk/stable/class-gtktreeview.html#method-gtktreeview--set-enable-tree-lines" rel="nofollow">http://library.gnome.org/devel/pygtk/stable/class-gtktreeview.html#method-gtktreeview--set-enable-tree-lin...
3
2009-07-30T19:10:08Z
[ "python", "gtk", "pygtk", "gtktreeview" ]
Is there a Python shortcut for variable checking and assignment?
1,207,333
<p>I'm finding myself typing the following a lot (developing for Django, if that's relevant):</p> <pre><code>if testVariable then: myVariable = testVariable else: # something else </code></pre> <p>Alternatively, and more commonly (i.e. building up a parameters list)</p> <pre><code>if 'query' in request.POST.ke...
6
2009-07-30T15:23:27Z
1,207,353
<p>The first instance is stated oddly... Why set a boolean to another boolean? </p> <p>What you may mean is to set myVariable to testVariable when testVariable is not a zero length string or not None or not something that happens to evaluate to False.</p> <p>If so, I prefer the more explicit formulations</p> <pre...
7
2009-07-30T15:27:20Z
[ "python", "django", "idioms" ]
Is there a Python shortcut for variable checking and assignment?
1,207,333
<p>I'm finding myself typing the following a lot (developing for Django, if that's relevant):</p> <pre><code>if testVariable then: myVariable = testVariable else: # something else </code></pre> <p>Alternatively, and more commonly (i.e. building up a parameters list)</p> <pre><code>if 'query' in request.POST.ke...
6
2009-07-30T15:23:27Z
1,207,359
<p>Assuming you want to leave myVariable untouched to its previous value in the "not exist" case,</p> <pre><code>myVariable = testVariable or myVariable </code></pre> <p>deals with the first case, and</p> <pre><code>myVariable = request.POST.get('query', myVariable) </code></pre> <p>deals with the second one. Neith...
24
2009-07-30T15:28:59Z
[ "python", "django", "idioms" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
1,207,427
<p>You need to take a copy of the list and iterate over it first, or the iteration will fail with what may be unexpected results.</p> <p>For example (depends on what type of list):</p> <pre><code>for tup in somelist[:]: etc.... </code></pre> <p>An example:</p> <pre><code>&gt;&gt;&gt; somelist = range(10) &gt;&g...
117
2009-07-30T15:38:32Z
[ "python", "iteration" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
1,207,460
<p>Your best approach for such an example would be a <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions">list comprehension</a></p> <pre><code>somelist = [tup for tup in somelist if determine(tup)] </code></pre> <p>In cases where you're doing something more complex than calling a <code>d...
39
2009-07-30T15:41:30Z
[ "python", "iteration" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
1,207,461
<p>You can use a list comprehension to create a new list containing only the elements you don't want to remove:</p> <pre><code>somelist = [x for x in somelist if not determine(x)] </code></pre> <p>Or, by assigning to the slice <code>somelist[:]</code>, you can mutate the existing list to contain only the items you wa...
359
2009-07-30T15:41:33Z
[ "python", "iteration" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
1,207,485
<pre><code>for i in xrange(len(somelist) - 1, -1, -1): if some_condition(somelist, i): del somelist[i] </code></pre> <p>You need to go backwards otherwise it's a bit like sawing off the tree-branch that you are sitting on :-)</p>
58
2009-07-30T15:44:59Z
[ "python", "iteration" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
1,207,500
<p>For those that like functional programming:</p> <pre><code>somelist[:] = filter(lambda tup: not determine(tup), somelist) </code></pre> <p>or</p> <pre><code>from itertools import ifilterfalse somelist[:] = list(ifilterfalse(determine, somelist)) </code></pre>
29
2009-07-30T15:46:46Z
[ "python", "iteration" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
1,208,792
<p>The answers suggesting list comprehensions are ALMOST correct -- except that they build a completely new list and then give it the same name the old list as, they do NOT modify the old list in place. That's different from what you'd be doing by selective removal, as in @Lennart's suggestion -- it's faster, but if yo...
376
2009-07-30T19:28:27Z
[ "python", "iteration" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
4,639,072
<p>You might want to use filter() available as the built-in.</p> <p><a href="http://docs.python.org/library/functions.html#filter" rel="nofollow">http://docs.python.org/library/functions.html#filter</a></p>
2
2011-01-09T12:20:34Z
[ "python", "iteration" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
25,363,359
<p>If you want to do anything else during the iteration, it may be nice to get both the index (which guarantees you being able to reference it, for example if you have a list of dicts) and the actual list item contents.</p> <pre><code>inlist = [{'field1':10, 'field2':20}, {'field1':30, 'field2':15}] for idx, i in ...
2
2014-08-18T12:30:16Z
[ "python", "iteration" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
31,350,162
<p>You can try for-looping in reverse so for some_list you'll do something like:</p> <pre><code>list_len = len(some_list) for i in range(list_len): reverse_i = list_len - 1 - i cur = some_list[reverse_i] # some logic with cur element if some_condition: some_list.pop(reverse_i) </code></pre> ...
0
2015-07-10T20:58:49Z
[ "python", "iteration" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
34,238,688
<p><strong>The <a href="https://docs.python.org/2/tutorial/controlflow.html#for-statements" rel="nofollow">official Python 2 tutorial 4.2. "for Statements"</a> says</strong>:</p> <blockquote> <p>If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items),...
7
2015-12-12T10:18:22Z
[ "python", "iteration" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
34,310,158
<p>I needed to do something similar and in my case the problem was memory - I needed to merge multiple dataset objects within a list, after doing some stuff with them, as a new object, and needed to get rid of each entry I was merging to avoid duplicating all of them and blowing up memory. In my case having the objects...
1
2015-12-16T10:56:37Z
[ "python", "iteration" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
36,096,883
<p>It might be smart to also just create a new list if the current list item meets the desired criteria. </p> <p>so:</p> <pre><code>for item in originalList: if (item != badValue): newList.append(item) </code></pre> <p>and to avoid having to re-code the entire project with the new lists name:</p> <pre><c...
1
2016-03-19T01:41:18Z
[ "python", "iteration" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
37,277,264
<p>TLDR:</p> <p>I wrote a library that allows you to do this:</p> <pre><code>from fluidIter import FluidIterable fSomeList = FluidIterable(someList) for tup in fSomeList: if determine(tup): # remove 'tup' without "breaking" the iteration fSomeList.remove(tup) # tup has also been removed ...
0
2016-05-17T13:15:10Z
[ "python", "iteration" ]
Remove items from a list while iterating in Python
1,207,406
<p>I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria. </p> <pre><code>for tup in somelist: if determine(tup): code_to_remove_tup </code></pre> <p>What should I use in place of <code>code_to_remove_tup</code>? I can't figure out how to remove the...
447
2009-07-30T15:36:42Z
39,293,411
<p>One possible solution, useful if you don't want to use comprehensions:</p> <pre><code>alist = ['good', 'bad', 'good', 'bad', 'good'] i = 0 for x in alist[:]: if x == 'bad': alist.pop(i) i -= 1 else: # do something cool with x or just print x print x i += 1 </code></pre>
0
2016-09-02T13:16:37Z
[ "python", "iteration" ]
Convert a Unicode string to a string in Python (containing extra symbols)
1,207,457
<p>How do you convert a Unicode string (containing extra characters like £ $, etc.) into a Python string?</p>
258
2009-07-30T15:41:11Z
1,207,479
<pre><code>title = u"Klüft skräms inför på fédéral électoral große" import unicodedata unicodedata.normalize('NFKD', title).encode('ascii','ignore') 'Kluft skrams infor pa federal electoral groe' </code></pre>
369
2009-07-30T15:44:32Z
[ "python", "string", "unicode", "type-conversion" ]
Convert a Unicode string to a string in Python (containing extra symbols)
1,207,457
<p>How do you convert a Unicode string (containing extra characters like £ $, etc.) into a Python string?</p>
258
2009-07-30T15:41:11Z
1,207,496
<p>Here is an example:</p> <pre><code>&gt;&gt;&gt; u = u'€€€' &gt;&gt;&gt; s = u.encode('utf8') &gt;&gt;&gt; s '\xe2\x82\xac\xe2\x82\xac\xe2\x82\xac' </code></pre>
42
2009-07-30T15:46:26Z
[ "python", "string", "unicode", "type-conversion" ]
Convert a Unicode string to a string in Python (containing extra symbols)
1,207,457
<p>How do you convert a Unicode string (containing extra characters like £ $, etc.) into a Python string?</p>
258
2009-07-30T15:41:11Z
1,207,634
<p>Well, if you're willing/ready to switch to Python 3 (which you may not be due to the backwards incompatibility with some Python 2 code), you don't have to do any converting; all text in Python 3 is represented with Unicode strings, which also means that there's no more usage of the <code>u'&lt;text&gt;'</code> synta...
2
2009-07-30T16:09:31Z
[ "python", "string", "unicode", "type-conversion" ]
Convert a Unicode string to a string in Python (containing extra symbols)
1,207,457
<p>How do you convert a Unicode string (containing extra characters like £ $, etc.) into a Python string?</p>
258
2009-07-30T15:41:11Z
1,207,836
<p>If you have a Unicode string, and you want to write this to a file, or other serialised form, you must first <em>encode</em> it into a particular representation that can be stored. There are several common Unicode encodings, such as UTF-16 (uses two bytes for most Unicode characters) or UTF-8 (1-4 bytes / codepoint...
75
2009-07-30T16:44:54Z
[ "python", "string", "unicode", "type-conversion" ]
Convert a Unicode string to a string in Python (containing extra symbols)
1,207,457
<p>How do you convert a Unicode string (containing extra characters like £ $, etc.) into a Python string?</p>
258
2009-07-30T15:41:11Z
1,211,102
<p>You can use encode to ASCII if you don't need to translate the non-ASCII characters:</p> <pre><code>&gt;&gt;&gt; a=u"aaaàçççñññ" &gt;&gt;&gt; type(a) &lt;type 'unicode'&gt; &gt;&gt;&gt; a.encode('ascii','ignore') 'aaa' &gt;&gt;&gt; a.encode('ascii','replace') 'aaa???????' &gt;&gt;&gt; </code></pre>
178
2009-07-31T07:13:09Z
[ "python", "string", "unicode", "type-conversion" ]
Convert a Unicode string to a string in Python (containing extra symbols)
1,207,457
<p>How do you convert a Unicode string (containing extra characters like £ $, etc.) into a Python string?</p>
258
2009-07-30T15:41:11Z
13,073,070
<pre><code>&gt;&gt;&gt; text=u'abcd' &gt;&gt;&gt; str(text) 'abcd' </code></pre> <p>If a simple conversion.</p>
60
2012-10-25T16:27:20Z
[ "python", "string", "unicode", "type-conversion" ]
How to workaround lack of multiple ao.lock?
1,207,497
<p>I'm programming a simple pyS60 app, not really done anything with python or using multiple threads before so this is all a bit new to me. In order to keep the app open, I set an e32.Ao_lock to wait() after the body of the application is initialised, and then signal the lock on the exit_key_handler.</p> <p>One of th...
1
2009-07-30T15:46:27Z
3,085,134
<p>I solved this problem by defining a separate second lock, and making sure only one was waiting at a time. It seems to work without any problem. Current code can be found <a href="http://code.google.com/p/scanscrobbler/" rel="nofollow">hosted on google code</a></p>
0
2010-06-21T13:41:24Z
[ "python", "multithreading", "locking", "nokia", "pys60" ]
Check absolute paths in Python
1,207,954
<p>How can I check whether two file paths point to the same file in Python?</p>
9
2009-07-30T17:04:34Z
1,207,974
<p>You want to use <a href="http://docs.python.org/library/os.path.html"><code>os.path.abspath(path)</code></a> to normalize each path for comparison.</p> <pre><code>os.path.abspath(foo) == os.path.abspath(bar) </code></pre>
6
2009-07-30T17:07:36Z
[ "python", "path" ]
Check absolute paths in Python
1,207,954
<p>How can I check whether two file paths point to the same file in Python?</p>
9
2009-07-30T17:04:34Z
1,207,984
<p>A simple string compare should work:</p> <pre><code>import os print os.path.abspath(first) == os.path.abspath(second) </code></pre> <p>Credit to Andrew, he corrected my initial post which included a call to <code>os.path.normpath</code>: this is unneeded because the implementation of <code>os.path.abspath</code> d...
3
2009-07-30T17:09:26Z
[ "python", "path" ]
Check absolute paths in Python
1,207,954
<p>How can I check whether two file paths point to the same file in Python?</p>
9
2009-07-30T17:04:34Z
1,207,987
<pre><code>$ touch foo $ ln -s foo bar $ python Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; import os &gt;&gt;&gt; help(os.path.samefile) Help on function samefile in module posixpath:...
23
2009-07-30T17:10:04Z
[ "python", "path" ]
Check absolute paths in Python
1,207,954
<p>How can I check whether two file paths point to the same file in Python?</p>
9
2009-07-30T17:04:34Z
3,178,889
<p>On Windows systems, there is no <code>samefile</code> function and you also have to worry about case. The <code>normcase</code> function from <code>os.path</code> can be combined with <code>abspath</code> to handle this case.</p> <pre><code>from os.path import abspath, normcase def are_paths_equivalent(path1, path...
0
2010-07-05T10:51:50Z
[ "python", "path" ]
Check absolute paths in Python
1,207,954
<p>How can I check whether two file paths point to the same file in Python?</p>
9
2009-07-30T17:04:34Z
5,176,531
<p>May be one can use os.path.relpath(path1, path2) as workaround for os.path.samefile(path1, path2) on Windows?</p> <p>If os.path.relpath(path1, path2) returns '.' than path1 and path2 point to the same place</p>
0
2011-03-03T04:06:39Z
[ "python", "path" ]
Where's my JSON data in my incoming Django request?
1,208,067
<p>I'm trying to process incoming JSON/Ajax requests with Django/Python.</p> <p><a href="https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpRequest.is_ajax"><code>request.is_ajax()</code></a> is <code>True</code> on the request, but I have no idea where the payload is with the JSON data.</p...
94
2009-07-30T17:22:53Z
1,208,112
<p>request.POST is just a dictionary-like object, so just index into it with dict syntax.</p> <p>Assuming your form field is fred, you could do something like this:</p> <pre><code>if 'fred' in request.POST: mydata = request.POST['fred'] </code></pre> <p>Alternately, use a form object to deal with the POST data.<...
-3
2009-07-30T17:31:31Z
[ "python", "ajax", "json", "django", "content-type" ]
Where's my JSON data in my incoming Django request?
1,208,067
<p>I'm trying to process incoming JSON/Ajax requests with Django/Python.</p> <p><a href="https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpRequest.is_ajax"><code>request.is_ajax()</code></a> is <code>True</code> on the request, but I have no idea where the payload is with the JSON data.</p...
94
2009-07-30T17:22:53Z
1,208,285
<p>The HTTP POST payload is just a flat bunch of bytes. Django (like most frameworks) decodes it into a dictionary from either URL encoded parameters, or MIME-multipart encoding. If you just dump the JSON data in the POST content, Django won't decode it. Either do the JSON decoding from the full POST content (not the d...
5
2009-07-30T18:03:45Z
[ "python", "ajax", "json", "django", "content-type" ]
Where's my JSON data in my incoming Django request?
1,208,067
<p>I'm trying to process incoming JSON/Ajax requests with Django/Python.</p> <p><a href="https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpRequest.is_ajax"><code>request.is_ajax()</code></a> is <code>True</code> on the request, but I have no idea where the payload is with the JSON data.</p...
94
2009-07-30T17:22:53Z
3,020,756
<p>If you are posting JSON to Django, I think you want <code>request.body</code> (<code>request.raw_post_data</code> on Django &lt; 1.4). This will give you the raw JSON data sent via the post. From there you can process it further.</p> <p>Here is an example using JavaScript, <a href="http://en.wikipedia.org/wiki/JQue...
147
2010-06-11T06:56:22Z
[ "python", "ajax", "json", "django", "content-type" ]
Where's my JSON data in my incoming Django request?
1,208,067
<p>I'm trying to process incoming JSON/Ajax requests with Django/Python.</p> <p><a href="https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpRequest.is_ajax"><code>request.is_ajax()</code></a> is <code>True</code> on the request, but I have no idea where the payload is with the JSON data.</p...
94
2009-07-30T17:22:53Z
3,244,765
<p>I had the same problem. I had been posting a complex JSON response, and I couldn't read my data using the request.POST dictionary.</p> <p>My JSON POST data was:</p> <pre><code>//JavaScript code: //Requires json2.js and jQuery. var response = {data:[{"a":1, "b":2},{"a":2, "b":2}]} json_response = JSON.stringify(res...
35
2010-07-14T09:13:29Z
[ "python", "ajax", "json", "django", "content-type" ]
Where's my JSON data in my incoming Django request?
1,208,067
<p>I'm trying to process incoming JSON/Ajax requests with Django/Python.</p> <p><a href="https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpRequest.is_ajax"><code>request.is_ajax()</code></a> is <code>True</code> on the request, but I have no idea where the payload is with the JSON data.</p...
94
2009-07-30T17:22:53Z
10,990,800
<p><code>request.raw_response</code> is now deprecated. Use <code>request.body</code> instead to process non-conventional form data such as XML payloads, binary images, etc.</p> <p><a href="https://docs.djangoproject.com/en/dev/ref/request-response/">Django documentation on the issue</a>.</p>
22
2012-06-12T05:06:01Z
[ "python", "ajax", "json", "django", "content-type" ]
Where's my JSON data in my incoming Django request?
1,208,067
<p>I'm trying to process incoming JSON/Ajax requests with Django/Python.</p> <p><a href="https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpRequest.is_ajax"><code>request.is_ajax()</code></a> is <code>True</code> on the request, but I have no idea where the payload is with the JSON data.</p...
94
2009-07-30T17:22:53Z
20,122,297
<p><code>request.raw_post_data</code> has been deprecated. Use <code>request.body</code> instead</p>
5
2013-11-21T13:28:39Z
[ "python", "ajax", "json", "django", "content-type" ]
Where's my JSON data in my incoming Django request?
1,208,067
<p>I'm trying to process incoming JSON/Ajax requests with Django/Python.</p> <p><a href="https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpRequest.is_ajax"><code>request.is_ajax()</code></a> is <code>True</code> on the request, but I have no idea where the payload is with the JSON data.</p...
94
2009-07-30T17:22:53Z
23,008,197
<p><strong>Method 1</strong></p> <p>Client : Send as <code>JSON</code></p> <pre><code>$.ajax({ url: 'example.com/ajax/', type: 'POST', contentType: 'application/json; charset=utf-8', processData: false, data: JSON.stringify({'name':'John', 'age': 42}), ... }); //Sent as a JSON object {'name':...
21
2014-04-11T09:07:42Z
[ "python", "ajax", "json", "django", "content-type" ]
Where's my JSON data in my incoming Django request?
1,208,067
<p>I'm trying to process incoming JSON/Ajax requests with Django/Python.</p> <p><a href="https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpRequest.is_ajax"><code>request.is_ajax()</code></a> is <code>True</code> on the request, but I have no idea where the payload is with the JSON data.</p...
94
2009-07-30T17:22:53Z
24,939,871
<p>on django 1.6 python 3.3</p> <p>client</p> <pre><code>$.ajax({ url: '/urll/', type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify(json_object), dataType: 'json', success: function(result) { alert(result.Result); } }); </code></pre> <p>server</p> ...
5
2014-07-24T17:06:46Z
[ "python", "ajax", "json", "django", "content-type" ]
Where's my JSON data in my incoming Django request?
1,208,067
<p>I'm trying to process incoming JSON/Ajax requests with Django/Python.</p> <p><a href="https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpRequest.is_ajax"><code>request.is_ajax()</code></a> is <code>True</code> on the request, but I have no idea where the payload is with the JSON data.</p...
94
2009-07-30T17:22:53Z
27,599,561
<pre><code>html code file name : view.html &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script&gt; $(document).ready(function(){ $("#mySelect").change(function(){ ...
2
2014-12-22T09:07:32Z
[ "python", "ajax", "json", "django", "content-type" ]
Where's my JSON data in my incoming Django request?
1,208,067
<p>I'm trying to process incoming JSON/Ajax requests with Django/Python.</p> <p><a href="https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpRequest.is_ajax"><code>request.is_ajax()</code></a> is <code>True</code> on the request, but I have no idea where the payload is with the JSON data.</p...
94
2009-07-30T17:22:53Z
29,273,926
<p>Using Angular you should add header to request or add it to module config headers: <code>{'Content-Type': 'application/x-www-form-urlencoded'}</code></p> <pre><code>$http({ url: url, method: method, timeout: timeout, data: data, headers: {'Content-Type': 'application/x-www-form-urlencoded'} }) <...
-1
2015-03-26T08:29:18Z
[ "python", "ajax", "json", "django", "content-type" ]
Where's my JSON data in my incoming Django request?
1,208,067
<p>I'm trying to process incoming JSON/Ajax requests with Django/Python.</p> <p><a href="https://docs.djangoproject.com/en/stable/ref/request-response/#django.http.HttpRequest.is_ajax"><code>request.is_ajax()</code></a> is <code>True</code> on the request, but I have no idea where the payload is with the JSON data.</p...
94
2009-07-30T17:22:53Z
34,495,127
<p>Its important to remember Python 3 has a different way to represent strings - they are byte arrays.</p> <p>Using Django 1.9 and Python 2.7 and sending the JSON data in the main body (not a header) you would use something like:</p> <pre><code>mydata = json.loads(request.body) </code></pre> <p>But for Django 1.9 an...
2
2015-12-28T14:11:44Z
[ "python", "ajax", "json", "django", "content-type" ]
Using numpy to build an array of all combinations of two arrays
1,208,118
<p>I'm trying to run over the parameters space of a 6 parameter function to study it's numerical behavior before trying to do anything complex with it so I'm searching for a efficient way to do this.</p> <p>My function takes float values given a 6-dim numpy array as input. What I tried to do initially was this:</p> <...
73
2009-07-30T17:32:27Z
1,208,739
<p><a href="http://docs.python.org/library/itertools.html#itertools.combinations">itertools.combinations</a> is in general the fastest way to get combinations from a Python container (if you do in fact want combinations, i.e., arrangements WITHOUT repetitions and independent of order; that's not what your code appears ...
26
2009-07-30T19:19:11Z
[ "python", "arrays", "multidimensional-array", "numpy" ]
Using numpy to build an array of all combinations of two arrays
1,208,118
<p>I'm trying to run over the parameters space of a 6 parameter function to study it's numerical behavior before trying to do anything complex with it so I'm searching for a efficient way to do this.</p> <p>My function takes float values given a 6-dim numpy array as input. What I tried to do initially was this:</p> <...
73
2009-07-30T17:32:27Z
1,235,363
<p>Here's a pure-numpy implementation. It's ca. 5× faster than using itertools.</p> <pre><code> import numpy as np def cartesian(arrays, out=None): """ Generate a cartesian product of input arrays. Parameters ---------- arrays : list of array-like 1-D arrays to form the cartesian product...
114
2009-08-05T19:48:42Z
[ "python", "arrays", "multidimensional-array", "numpy" ]
Using numpy to build an array of all combinations of two arrays
1,208,118
<p>I'm trying to run over the parameters space of a 6 parameter function to study it's numerical behavior before trying to do anything complex with it so I'm searching for a efficient way to do this.</p> <p>My function takes float values given a 6-dim numpy array as input. What I tried to do initially was this:</p> <...
73
2009-07-30T17:32:27Z
7,481,643
<p>It looks like you want a grid to evaluate your function, in which case you can use <code>numpy.ogrid</code> (open) or <code>numpy.mgrid</code> (fleshed out):</p> <pre><code>import numpy my_grid = numpy.mgrid[[slice(0,1,0.1)]*6] </code></pre>
6
2011-09-20T07:37:36Z
[ "python", "arrays", "multidimensional-array", "numpy" ]
Using numpy to build an array of all combinations of two arrays
1,208,118
<p>I'm trying to run over the parameters space of a 6 parameter function to study it's numerical behavior before trying to do anything complex with it so I'm searching for a efficient way to do this.</p> <p>My function takes float values given a 6-dim numpy array as input. What I tried to do initially was this:</p> <...
73
2009-07-30T17:32:27Z
25,463,061
<p>You can do something like this</p> <pre><code>import numpy as np def cartesian_coord(*arrays): grid = np.meshgrid(*arrays) coord_list = [entry.ravel() for entry in grid] points = np.vstack(coord_list).T return points a = np.arange(4) # fake data print(cartesian_coord(*6*[a]) </code></pre>...
5
2014-08-23T14:38:44Z
[ "python", "arrays", "multidimensional-array", "numpy" ]
Using numpy to build an array of all combinations of two arrays
1,208,118
<p>I'm trying to run over the parameters space of a 6 parameter function to study it's numerical behavior before trying to do anything complex with it so I'm searching for a efficient way to do this.</p> <p>My function takes float values given a 6-dim numpy array as input. What I tried to do initially was this:</p> <...
73
2009-07-30T17:32:27Z
25,655,090
<p>The following numpy implementation should be approx. 2x the speed of the given answer:</p> <pre><code>def cartesian2(arrays): arrays = [np.asarray(a) for a in arrays] shape = (len(x) for x in arrays) ix = np.indices(shape, dtype=int) ix = ix.reshape(len(arrays), -1).T for n, arr in enumerate(a...
8
2014-09-03T23:22:41Z
[ "python", "arrays", "multidimensional-array", "numpy" ]
Using numpy to build an array of all combinations of two arrays
1,208,118
<p>I'm trying to run over the parameters space of a 6 parameter function to study it's numerical behavior before trying to do anything complex with it so I'm searching for a efficient way to do this.</p> <p>My function takes float values given a 6-dim numpy array as input. What I tried to do initially was this:</p> <...
73
2009-07-30T17:32:27Z
34,931,627
<p>Here's yet another way, using pure NumPy, no recursion, no list comprehension, and no explicit for loops. It's about 20% slower than the original answer, and it's based on np.meshgrid.</p> <pre><code>def cartesian(*arrays): mesh = np.meshgrid(*arrays) # standard numpy meshgrid dim = len(mesh) # number of ...
1
2016-01-21T18:33:29Z
[ "python", "arrays", "multidimensional-array", "numpy" ]