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
a question on for loops in python
588,052
<p>I want to calaculate pythagorean triplets(code below) and I want to calculate infinitly how do I do it without using the three for loops? Could I use a for loop in some way? thanks.</p> <pre><code>import math def main(): for x in range (10000, 1000): for y in range (10000, 1000): for z in range(1000...
3
2009-02-25T21:47:41Z
588,915
<p>At least three loops are needed for infinity. To make something flexible takes a ton of loops. This example is a solution to <a href="http://projecteuler.net/index.php?section=problems&amp;id=9" rel="nofollow">Project Euler Problem 9</a> and more.</p> <pre><code>#!/usr/bin/env python def fcount(start=1): n =...
0
2009-02-26T02:52:46Z
[ "python", "for-loop" ]
a question on for loops in python
588,052
<p>I want to calaculate pythagorean triplets(code below) and I want to calculate infinitly how do I do it without using the three for loops? Could I use a for loop in some way? thanks.</p> <pre><code>import math def main(): for x in range (10000, 1000): for y in range (10000, 1000): for z in range(1000...
3
2009-02-25T21:47:41Z
6,937,631
<p>How about use itertools.product instead?</p> <pre><code># range is [10000, 1000) for x, y, z in itertools.product(range(10000, 1000, -1), repeat=3): if x * x == y * y + z * z: print(y, z, x) </code></pre> <p>with a litter bit optimize:</p> <pre><code>for x, y, z in itertools.product(range(10000, 1000...
0
2011-08-04T07:22:53Z
[ "python", "for-loop" ]
Is there a Django apps pattern equivalent in Google App Engine?
588,342
<p>Django has a very handy pattern known as "apps". Essentially, a self-contained plug-in that requires a minimal amount of wiring, configuring, and glue code to integrate into an existing project. Examples are tagging, comments, contact-form, etc. They let you build up large projects by gathering together a collect...
3
2009-02-25T23:09:54Z
591,169
<p>The Django implementation of <em>apps</em> is closely tied to Django operation as a framework - I mean plugging application using Django url mapping features (for mapping urls to view functions) and Django application component discovery (for discovering models and admin configuration). There is no such mechanisms i...
3
2009-02-26T15:59:04Z
[ "python", "django", "design-patterns", "google-app-engine", "django-apps" ]
Is there a Django apps pattern equivalent in Google App Engine?
588,342
<p>Django has a very handy pattern known as "apps". Essentially, a self-contained plug-in that requires a minimal amount of wiring, configuring, and glue code to integrate into an existing project. Examples are tagging, comments, contact-form, etc. They let you build up large projects by gathering together a collect...
3
2009-02-25T23:09:54Z
622,150
<p>I'd like to add that you can run Django apps inside App Engine. I've been doing this successfully for the last few months. Basically, you can make use of the <a href="http://code.google.com/appengine/articles/appengine%5Fhelper%5Ffor%5Fdjango.html" rel="nofollow">App Engine Helper</a> project or <a href="http://code...
2
2009-03-07T17:08:56Z
[ "python", "django", "design-patterns", "google-app-engine", "django-apps" ]
How to produce a colored GUI in a console application?
588,622
<p>For the following questions, answers may be for C/C++, C#, or Python. I would like the answers to be cross platform if possible but I realize I will probably need <code>conio</code> or <code>ncurses</code></p> <ol> <li>How do I output colored text?</li> <li>How would I do a GUI like <code>top</code> or <code>nethac...
4
2009-02-26T00:47:02Z
588,637
<p>Most terminal windows understand the ANSI escape sequences, which allow coloring, cursor movement etc. You can find a list of them <a href="http://en.wikipedia.org/wiki/ANSI%5Fescape%5Fcode" rel="nofollow">here</a>.</p> <p>Use of these sequences can seem a bit "old school", but you can use them in cases where curse...
1
2009-02-26T00:53:24Z
[ "c#", "c++", "python", "c", "console-application" ]
How to produce a colored GUI in a console application?
588,622
<p>For the following questions, answers may be for C/C++, C#, or Python. I would like the answers to be cross platform if possible but I realize I will probably need <code>conio</code> or <code>ncurses</code></p> <ol> <li>How do I output colored text?</li> <li>How would I do a GUI like <code>top</code> or <code>nethac...
4
2009-02-26T00:47:02Z
588,642
<p>Yes, these are VT100 escape codes. The simplest thing is to use some flavor of Curses. Once, you choose a curses flavor it is pretty simple to do both 1 and 2.</p> <p>Here's a HowTo on ncurses.</p> <p><a href="http://web.cs.mun.ca/~rod/ncurses/ncurses.html" rel="nofollow">http://web.cs.mun.ca/~rod/ncurses/ncurses....
4
2009-02-26T00:54:26Z
[ "c#", "c++", "python", "c", "console-application" ]
How to produce a colored GUI in a console application?
588,622
<p>For the following questions, answers may be for C/C++, C#, or Python. I would like the answers to be cross platform if possible but I realize I will probably need <code>conio</code> or <code>ncurses</code></p> <ol> <li>How do I output colored text?</li> <li>How would I do a GUI like <code>top</code> or <code>nethac...
4
2009-02-26T00:47:02Z
588,645
<p>Not cross platform but for Windows / C# colour, see</p> <p><a href="http://www.daniweb.com/code/snippet134.html" rel="nofollow">Color your Console text (C#)</a></p> <p><a href="http://www.daniweb.com/code/snippet83.html" rel="nofollow">c++</a></p>
0
2009-02-26T00:55:32Z
[ "c#", "c++", "python", "c", "console-application" ]
How to produce a colored GUI in a console application?
588,622
<p>For the following questions, answers may be for C/C++, C#, or Python. I would like the answers to be cross platform if possible but I realize I will probably need <code>conio</code> or <code>ncurses</code></p> <ol> <li>How do I output colored text?</li> <li>How would I do a GUI like <code>top</code> or <code>nethac...
4
2009-02-26T00:47:02Z
588,648
<p>From this point of view, the console is in many ways just an emulation of a classic terminal device. Curses was created originally to support a way of doing common operations on different terminal types, where the actual terminal in use could be selected by the user as part of the login sequence. That heritage survi...
1
2009-02-26T00:56:19Z
[ "c#", "c++", "python", "c", "console-application" ]
How to produce a colored GUI in a console application?
588,622
<p>For the following questions, answers may be for C/C++, C#, or Python. I would like the answers to be cross platform if possible but I realize I will probably need <code>conio</code> or <code>ncurses</code></p> <ol> <li>How do I output colored text?</li> <li>How would I do a GUI like <code>top</code> or <code>nethac...
4
2009-02-26T00:47:02Z
588,652
<p>In C#, you can set the text color and the background color via the Console.ForegroundColor and Console.BackgroundColor properties, respectively. For a list of valid colors, see this <a href="http://msdn.microsoft.com/en-us/library/system.consolecolor.aspx" rel="nofollow">MSDN doc</a>.</p>
0
2009-02-26T00:57:00Z
[ "c#", "c++", "python", "c", "console-application" ]
Python Daemon Packaging Best Practices
588,749
<p>I have a tool which I have written in python and generally should be run as a daemon. What are the best practices for packaging this tool for distribution, particularly how should settings files and the daemon executable/script be handled?</p> <p>Relatedly are there any common tools for setting up the daemon for r...
23
2009-02-26T01:32:39Z
588,780
<p>The best tool I found for helping with init.d scripts is "start-stop-daemon". It will run any application, monitor run/pid files, create them when necessary, provide ways to stop the daemon, set process user/group ids, and can even background your process.</p> <p>For example, this is a script which can start/stop a...
14
2009-02-26T01:46:15Z
[ "python", "packaging", "setuptools", "distutils" ]
Python Daemon Packaging Best Practices
588,749
<p>I have a tool which I have written in python and generally should be run as a daemon. What are the best practices for packaging this tool for distribution, particularly how should settings files and the daemon executable/script be handled?</p> <p>Relatedly are there any common tools for setting up the daemon for r...
23
2009-02-26T01:32:39Z
588,800
<p>"generally should be run as a daemon?"</p> <p>Doesn't -- on surface -- make a lot of sense. "Generally" isn't sensible. It's either a a daemon or not. You might want to update your question.</p> <p>For examples of daemons, read up on daemons like Apache's httpd or any database server (they're daemons) or the SM...
-10
2009-02-26T01:52:47Z
[ "python", "packaging", "setuptools", "distutils" ]
Python Daemon Packaging Best Practices
588,749
<p>I have a tool which I have written in python and generally should be run as a daemon. What are the best practices for packaging this tool for distribution, particularly how should settings files and the daemon executable/script be handled?</p> <p>Relatedly are there any common tools for setting up the daemon for r...
23
2009-02-26T01:32:39Z
588,835
<p>On Linux systems, the system's package manager (Portage for Gentoo, Aptitude for Ubuntu/Debian, yum for Fedora, etc.) usually takes care of installing the program including placing init scripts in the right places. If you want to distribute your program for Linux, you might want to look into bundling it up into the ...
0
2009-02-26T02:09:29Z
[ "python", "packaging", "setuptools", "distutils" ]
Python Daemon Packaging Best Practices
588,749
<p>I have a tool which I have written in python and generally should be run as a daemon. What are the best practices for packaging this tool for distribution, particularly how should settings files and the daemon executable/script be handled?</p> <p>Relatedly are there any common tools for setting up the daemon for r...
23
2009-02-26T01:32:39Z
588,891
<p>There are many snippets on the internet offering to write a daemon in pure python (no bash scripts)</p> <p><a href="http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/">http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/</a> looks clean...</p> <p>If you want to write...
8
2009-02-26T02:44:33Z
[ "python", "packaging", "setuptools", "distutils" ]
Python Daemon Packaging Best Practices
588,749
<p>I have a tool which I have written in python and generally should be run as a daemon. What are the best practices for packaging this tool for distribution, particularly how should settings files and the daemon executable/script be handled?</p> <p>Relatedly are there any common tools for setting up the daemon for r...
23
2009-02-26T01:32:39Z
588,904
<p>To answer one part of your question, there are no tools I know of that will do daemon setup portably even across Linux systems let alone Windows or Mac OS X.</p> <p>Most Linux distributions seem to be using <code>start-stop-daemon</code> within init scripts now, but you're still going to have minor difference in fi...
11
2009-02-26T02:48:06Z
[ "python", "packaging", "setuptools", "distutils" ]
Python Daemon Packaging Best Practices
588,749
<p>I have a tool which I have written in python and generally should be run as a daemon. What are the best practices for packaging this tool for distribution, particularly how should settings files and the daemon executable/script be handled?</p> <p>Relatedly are there any common tools for setting up the daemon for r...
23
2009-02-26T01:32:39Z
591,832
<p>I can't remember where I downloaded it... but this is the best daemonizing script that I've found. It works beautifully (on Mac and Linux.) (save it as daemonize.py)</p> <pre><code>import sys, os def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): # Perform first fork. try: ...
2
2009-02-26T18:29:22Z
[ "python", "packaging", "setuptools", "distutils" ]
Python Daemon Packaging Best Practices
588,749
<p>I have a tool which I have written in python and generally should be run as a daemon. What are the best practices for packaging this tool for distribution, particularly how should settings files and the daemon executable/script be handled?</p> <p>Relatedly are there any common tools for setting up the daemon for r...
23
2009-02-26T01:32:39Z
1,962,593
<p>Not a silver bullet for what you're asking, but check out <a href="http://supervisord.org/" rel="nofollow" title="supervisord">supervisord</a>. It handles all the fun bits of managing processes. I use it heavily in a large production environment. Also, it's written in Python!</p>
3
2009-12-26T04:03:37Z
[ "python", "packaging", "setuptools", "distutils" ]
Python Daemon Packaging Best Practices
588,749
<p>I have a tool which I have written in python and generally should be run as a daemon. What are the best practices for packaging this tool for distribution, particularly how should settings files and the daemon executable/script be handled?</p> <p>Relatedly are there any common tools for setting up the daemon for r...
23
2009-02-26T01:32:39Z
2,847,591
<p>Check the Ben Finney's daemon module. He has started to write a PEP targeting python 3.X:</p> <p><a href="http://www.python.org/dev/peps/pep-3143/">http://www.python.org/dev/peps/pep-3143/</a></p> <p>But an implementation is already available here :</p> <p><a href="http://pypi.python.org/pypi/python-daemon/">http...
5
2010-05-17T08:31:26Z
[ "python", "packaging", "setuptools", "distutils" ]
Python Daemon Packaging Best Practices
588,749
<p>I have a tool which I have written in python and generally should be run as a daemon. What are the best practices for packaging this tool for distribution, particularly how should settings files and the daemon executable/script be handled?</p> <p>Relatedly are there any common tools for setting up the daemon for r...
23
2009-02-26T01:32:39Z
8,817,383
<p>This <a href="http://jimmyg.org/blog/2010/python-daemon-init-script.html" rel="nofollow">blog entry</a> made it clear for me that there are actually two common ways to have your Python program run as a deamon (I hadn't figured that out so clearly from the existing answers):</p> <blockquote> <p>There are two appr...
0
2012-01-11T10:02:48Z
[ "python", "packaging", "setuptools", "distutils" ]
Python persistent Popen
589,093
<p>Is there a way to do multiple calls in the same "session" in Popen? For instance, can I make a call through it and then another one after it without having to concatenate the commands into one long string?</p>
3
2009-02-26T04:19:43Z
589,104
<blockquote> <p>For instance, can I make a call through it and then another one after it without having to concatenate the commands into one long string?</p> </blockquote> <p>Sounds like you're using shell=True. Don't, unless you need to. Instead use shell=False (the default) and pass in a command/arg list.</p> <bl...
0
2009-02-26T04:24:14Z
[ "python", "subprocess", "popen" ]
Python persistent Popen
589,093
<p>Is there a way to do multiple calls in the same "session" in Popen? For instance, can I make a call through it and then another one after it without having to concatenate the commands into one long string?</p>
3
2009-02-26T04:19:43Z
589,282
<p>You're not "making a call" when you use popen, you're running an executable and talking to it over stdin, stdout, and stderr. If the executable has some way of doing a "session" of work (for instance, by reading lines from stdin) then, yes, you can do it. Otherwise, you'll need to exec multiple times.</p> <p>subpro...
3
2009-02-26T05:59:09Z
[ "python", "subprocess", "popen" ]
Python persistent Popen
589,093
<p>Is there a way to do multiple calls in the same "session" in Popen? For instance, can I make a call through it and then another one after it without having to concatenate the commands into one long string?</p>
3
2009-02-26T04:19:43Z
589,312
<p>Assuming you want to be able to run a shell and send it multiple commands (and read their output), it appears you can do something like this:</p> <pre><code>from subprocess import * p = Popen(['/bin/sh'], shell=False, stdin=PIPE, stdout=PIPE, stderr=PIPE) </code></pre> <p>After which, e.g.,:</p> <pre><code>&gt;&g...
0
2009-02-26T06:09:06Z
[ "python", "subprocess", "popen" ]
Miminal Linux For a Pylons Web App?
589,115
<p>I am going to be building a Pylons-based web application. For this purpose, I'd like to build a minimal Linux platform, upon which I would then install the necessary packages such as Python and Pylons, and other necessary dependencies. The other reason to keep it minimal is because this machine will be virtual, prob...
2
2009-02-26T04:33:49Z
589,127
<p>I really like <a href="http://www.ubuntu.com/products/whatisubuntu/serveredition/jeos" rel="nofollow">JeOS</a> "Just enough OS" which is a minimal distribution of the Ubuntu Server Edition.</p>
8
2009-02-26T04:37:24Z
[ "python", "linux", "pylons" ]
Miminal Linux For a Pylons Web App?
589,115
<p>I am going to be building a Pylons-based web application. For this purpose, I'd like to build a minimal Linux platform, upon which I would then install the necessary packages such as Python and Pylons, and other necessary dependencies. The other reason to keep it minimal is because this machine will be virtual, prob...
2
2009-02-26T04:33:49Z
589,638
<p>debootstrap is your friend.</p>
0
2009-02-26T08:25:41Z
[ "python", "linux", "pylons" ]
Miminal Linux For a Pylons Web App?
589,115
<p>I am going to be building a Pylons-based web application. For this purpose, I'd like to build a minimal Linux platform, upon which I would then install the necessary packages such as Python and Pylons, and other necessary dependencies. The other reason to keep it minimal is because this machine will be virtual, prob...
2
2009-02-26T04:33:49Z
589,645
<p>Damn Small Linux? Slax?</p>
0
2009-02-26T08:28:15Z
[ "python", "linux", "pylons" ]
Miminal Linux For a Pylons Web App?
589,115
<p>I am going to be building a Pylons-based web application. For this purpose, I'd like to build a minimal Linux platform, upon which I would then install the necessary packages such as Python and Pylons, and other necessary dependencies. The other reason to keep it minimal is because this machine will be virtual, prob...
2
2009-02-26T04:33:49Z
589,674
<p>If you want to go serious about the virtual appliance idea, take a look at the newly released <strong><a href="http://www.vmware.com/appliances/learn/vmware%5Fstudio.html" rel="nofollow">VMware Studio</a></strong>. It was built exactly for trimming down a system (only Linux for now afaik) so it provides only enough ...
0
2009-02-26T08:43:21Z
[ "python", "linux", "pylons" ]
Miminal Linux For a Pylons Web App?
589,115
<p>I am going to be building a Pylons-based web application. For this purpose, I'd like to build a minimal Linux platform, upon which I would then install the necessary packages such as Python and Pylons, and other necessary dependencies. The other reason to keep it minimal is because this machine will be virtual, prob...
2
2009-02-26T04:33:49Z
589,678
<p><a href="http://www.debian-administration.org/articles/426" rel="nofollow">Debootstrap</a>, or use <a href="http://www.redhat.com/docs/manuals/linux/RHL-7.3-Manual/custom-guide/ch-kickstart2.html" rel="nofollow">kickstart</a> to strap your FC domains. However, other methods of strapping an RPM based distro exist, su...
0
2009-02-26T08:44:15Z
[ "python", "linux", "pylons" ]
Miminal Linux For a Pylons Web App?
589,115
<p>I am going to be building a Pylons-based web application. For this purpose, I'd like to build a minimal Linux platform, upon which I would then install the necessary packages such as Python and Pylons, and other necessary dependencies. The other reason to keep it minimal is because this machine will be virtual, prob...
2
2009-02-26T04:33:49Z
590,001
<p>If you want to be able to remove all the cruft but still be using a ‘mainstream’ distro rather than one cut down to aim at tiny devices, look at Slackware. You can happily remove stuff as low-level as sysvinit, cron and so on, without collapsing into dependency hell. And nothing in it relies on Perl or Python, s...
1
2009-02-26T10:38:51Z
[ "python", "linux", "pylons" ]
Miminal Linux For a Pylons Web App?
589,115
<p>I am going to be building a Pylons-based web application. For this purpose, I'd like to build a minimal Linux platform, upon which I would then install the necessary packages such as Python and Pylons, and other necessary dependencies. The other reason to keep it minimal is because this machine will be virtual, prob...
2
2009-02-26T04:33:49Z
895,583
<blockquote> <p>For this purpose, I'd like to build a minimal Linux platform...</p> </blockquote> <p>So Why not try to use ArchLinux www.archlinux.org?</p> <p>Also you can use virtualenv with Pylons in it.</p>
1
2009-05-21T22:30:24Z
[ "python", "linux", "pylons" ]
imploding a list for use in a python MySQLDB IN clause
589,284
<p>I know how to map a list to a string:</p> <pre><code>foostring = ",".join( map(str, list_of_ids) ) </code></pre> <p>And I know that I can use the following to get that string into an IN clause:</p> <pre><code>cursor.execute("DELETE FROM foo.bar WHERE baz IN ('%s')" % (foostring)) </code></pre> <p>What I need is ...
35
2009-02-26T05:59:25Z
589,416
<p>Use the <code>list_of_ids</code> directly:</p> <pre><code>format_strings = ','.join(['%s'] * len(list_of_ids)) cursor.execute("DELETE FROM foo.bar WHERE baz IN (%s)" % format_strings, tuple(list_of_ids)) </code></pre> <p>That way you avoid having to quote yourself, and avoid all kinds of sql inject...
68
2009-02-26T06:46:38Z
[ "python", "mysql" ]
imploding a list for use in a python MySQLDB IN clause
589,284
<p>I know how to map a list to a string:</p> <pre><code>foostring = ",".join( map(str, list_of_ids) ) </code></pre> <p>And I know that I can use the following to get that string into an IN clause:</p> <pre><code>cursor.execute("DELETE FROM foo.bar WHERE baz IN ('%s')" % (foostring)) </code></pre> <p>What I need is ...
35
2009-02-26T05:59:25Z
13,542,147
<p>Pain-less MySQLdb <code>execute('...WHERE name1 = %s AND name2 IN (%s)', value1, values2)</code></p> <pre><code>def execute(sql, *values): assert sql.count('%s') == len(values), (sql, values) placeholders = [] new_values = [] for value in values: if isinstance(value, (list, tuple)): ...
0
2012-11-24T14:28:18Z
[ "python", "mysql" ]
imploding a list for use in a python MySQLDB IN clause
589,284
<p>I know how to map a list to a string:</p> <pre><code>foostring = ",".join( map(str, list_of_ids) ) </code></pre> <p>And I know that I can use the following to get that string into an IN clause:</p> <pre><code>cursor.execute("DELETE FROM foo.bar WHERE baz IN ('%s')" % (foostring)) </code></pre> <p>What I need is ...
35
2009-02-26T05:59:25Z
15,662,693
<p>As this person suggested (<a href="http://stackoverflow.com/questions/4574609/executing-select-where-in-using-mysqldb#comment18905458_4574647">Executing &quot;SELECT ... WHERE ... IN ...&quot; using MySQLdb</a>), it is faster to use itertools.repeat() to create the list of '%s's than to multiply a ['%s'] list (and m...
-2
2013-03-27T15:26:58Z
[ "python", "mysql" ]
imploding a list for use in a python MySQLDB IN clause
589,284
<p>I know how to map a list to a string:</p> <pre><code>foostring = ",".join( map(str, list_of_ids) ) </code></pre> <p>And I know that I can use the following to get that string into an IN clause:</p> <pre><code>cursor.execute("DELETE FROM foo.bar WHERE baz IN ('%s')" % (foostring)) </code></pre> <p>What I need is ...
35
2009-02-26T05:59:25Z
25,677,198
<pre><code>list_of_ids = [ 1, 2, 3] query = "select * from table where x in %s" % str(tuple(list_of_ids)) print query </code></pre> <p>This could work for some use-cases if you don't wish to be concerned with the method in which you have to pass arguments to complete the query string and would like to invoke just <cod...
0
2014-09-05T01:01:05Z
[ "python", "mysql" ]
Python - How to check if a file is used by another application?
589,407
<p>I want to open a file which is periodically written to by another application. This application cannot be modified. I'd therefore like to only open the file when I know it is not been written to by an other application.</p> <p>Is there a pythonic way to do this? Otherwise, how do I achieve this in Unix and Windows?...
8
2009-02-26T06:44:48Z
589,440
<p>Will your python script desire to open the file for writing or for reading? Is the legacy application opening and closing the file between writes, or does it keep it open?</p> <p>It is extremely important that we understand what the legacy application is doing, and what your python script is attempting to achieve....
7
2009-02-26T06:55:28Z
[ "python", "windows", "unix", "logging", "file-io" ]
Python - How to check if a file is used by another application?
589,407
<p>I want to open a file which is periodically written to by another application. This application cannot be modified. I'd therefore like to only open the file when I know it is not been written to by an other application.</p> <p>Is there a pythonic way to do this? Otherwise, how do I achieve this in Unix and Windows?...
8
2009-02-26T06:44:48Z
589,445
<p>Unix does not have file locking as a default. The best suggestion I have for a Unix environment would be to look at the sources for the lsof command. It has deep knowledge about which process have which files open. You could use that as the basis of your solution. Here are the <a href="https://launchpad.net/ubuntu/%...
0
2009-02-26T06:56:59Z
[ "python", "windows", "unix", "logging", "file-io" ]
Python - How to check if a file is used by another application?
589,407
<p>I want to open a file which is periodically written to by another application. This application cannot be modified. I'd therefore like to only open the file when I know it is not been written to by an other application.</p> <p>Is there a pythonic way to do this? Otherwise, how do I achieve this in Unix and Windows?...
8
2009-02-26T06:44:48Z
592,121
<p>One thing I've done is have python very temporarily rename the file. If we're able to rename it, then no other process is using it. I only tested this on Windows.</p>
0
2009-02-26T19:50:29Z
[ "python", "windows", "unix", "logging", "file-io" ]
How to find a relative URL and translate it to an absolute URL in Python
589,833
<p>I extract some code from a web page (<a href="http://www.opensolaris.org/os/community/on/flag-days/all/" rel="nofollow">http://www.opensolaris.org/os/community/on/flag-days/all/</a>) like follows,</p> <pre><code>&lt;tr class="build"&gt; &lt;th colspan="0"&gt;Build 110&lt;/th&gt; &lt;/tr&gt; &lt;tr class="arccase ...
2
2009-02-26T09:37:39Z
589,845
<p>I'm not sure about what you're trying to achieve but using the <a href="http://www.w3schools.com/TAGS/tag%5Fbase.asp">BASE tag in HTML</a> may do this trick for you without having to resort to regular expressions when doing the processing.</p>
6
2009-02-26T09:42:12Z
[ "python", "regex" ]
How to find a relative URL and translate it to an absolute URL in Python
589,833
<p>I extract some code from a web page (<a href="http://www.opensolaris.org/os/community/on/flag-days/all/" rel="nofollow">http://www.opensolaris.org/os/community/on/flag-days/all/</a>) like follows,</p> <pre><code>&lt;tr class="build"&gt; &lt;th colspan="0"&gt;Build 110&lt;/th&gt; &lt;/tr&gt; &lt;tr class="arccase ...
2
2009-02-26T09:37:39Z
589,864
<p>Something like this should do it:</p> <pre><code>"(?:[^/:"]+|/(?!/))(?:/[^/"]+)*" </code></pre>
1
2009-02-26T09:48:14Z
[ "python", "regex" ]
How to find a relative URL and translate it to an absolute URL in Python
589,833
<p>I extract some code from a web page (<a href="http://www.opensolaris.org/os/community/on/flag-days/all/" rel="nofollow">http://www.opensolaris.org/os/community/on/flag-days/all/</a>) like follows,</p> <pre><code>&lt;tr class="build"&gt; &lt;th colspan="0"&gt;Build 110&lt;/th&gt; &lt;/tr&gt; &lt;tr class="arccase ...
2
2009-02-26T09:37:39Z
589,890
<p>Don't use regular expressions to parse HTML. Use a real parser for that. For example <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a>.</p>
2
2009-02-26T10:00:31Z
[ "python", "regex" ]
How to find a relative URL and translate it to an absolute URL in Python
589,833
<p>I extract some code from a web page (<a href="http://www.opensolaris.org/os/community/on/flag-days/all/" rel="nofollow">http://www.opensolaris.org/os/community/on/flag-days/all/</a>) like follows,</p> <pre><code>&lt;tr class="build"&gt; &lt;th colspan="0"&gt;Build 110&lt;/th&gt; &lt;/tr&gt; &lt;tr class="arccase ...
2
2009-02-26T09:37:39Z
589,939
<p>First, I'd recommend using a HTML parser, such as <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a>. HTML is not a regular language, and thus can't be parsed fully by regular expressions alone. Parts of HTML can be parsed though.</p> <p>If you don't want to use a full HTML par...
3
2009-02-26T10:16:50Z
[ "python", "regex" ]
How to find a relative URL and translate it to an absolute URL in Python
589,833
<p>I extract some code from a web page (<a href="http://www.opensolaris.org/os/community/on/flag-days/all/" rel="nofollow">http://www.opensolaris.org/os/community/on/flag-days/all/</a>) like follows,</p> <pre><code>&lt;tr class="build"&gt; &lt;th colspan="0"&gt;Build 110&lt;/th&gt; &lt;/tr&gt; &lt;tr class="arccase ...
2
2009-02-26T09:37:39Z
4,232,118
<p>this isn't elegant, but does the job:</p> <pre><code>import re from urlparse import urljoin relative_urls_re = re.compile('(&lt;\s*a[^&gt;]+href\s*=\s*["\']?)(?!http)([^"\'&gt;]+)', re.IGNORECASE) relative_urls_re.sub(lambda m: m.group(1) + urljoin(base_url, m.group(2)), html) </code></pre>
1
2010-11-20T09:50:00Z
[ "python", "regex" ]
Python or IronPython
590,007
<p>How does IronPython stack up to the default Windows implementation of Python from python.org? If I am learning Python, will I be learning a subtley different language with IronPython, and what libraries would I be doing without?</p> <p>Are there, alternatively, any pros to IronPython (not including .NET IL compiled...
26
2009-02-26T10:41:16Z
590,024
<p>Well, it's generally faster.</p> <p>Can't use modules, and only has a subset of the library.</p> <p><a href="http://www.codeplex.com/IronPython/Wiki/View.aspx?title=Differences">Here's a list of differences.</a></p>
6
2009-02-26T10:47:34Z
[ "python", "ironpython", "cpython" ]
Python or IronPython
590,007
<p>How does IronPython stack up to the default Windows implementation of Python from python.org? If I am learning Python, will I be learning a subtley different language with IronPython, and what libraries would I be doing without?</p> <p>Are there, alternatively, any pros to IronPython (not including .NET IL compiled...
26
2009-02-26T10:41:16Z
590,026
<p>There are some subtle differences in how you write your code, but the biggest difference is in the libraries you have available.</p> <p>With IronPython, you have all the .Net libraries available, but at the expense of some of the "normal" python libraries that haven't been ported to the .Net VM I think.</p> <p>Bas...
13
2009-02-26T10:47:57Z
[ "python", "ironpython", "cpython" ]
Python or IronPython
590,007
<p>How does IronPython stack up to the default Windows implementation of Python from python.org? If I am learning Python, will I be learning a subtley different language with IronPython, and what libraries would I be doing without?</p> <p>Are there, alternatively, any pros to IronPython (not including .NET IL compiled...
26
2009-02-26T10:41:16Z
590,029
<p>It also depends on whether you want your code to work on Linux. Dunno if IronPython will work on anything beside windows platforms.</p>
0
2009-02-26T10:50:26Z
[ "python", "ironpython", "cpython" ]
Python or IronPython
590,007
<p>How does IronPython stack up to the default Windows implementation of Python from python.org? If I am learning Python, will I be learning a subtley different language with IronPython, and what libraries would I be doing without?</p> <p>Are there, alternatively, any pros to IronPython (not including .NET IL compiled...
26
2009-02-26T10:41:16Z
590,052
<p>Python is Python, the only difference is that IronPython was designed to run on the CLR (.NET Framework), and as such, can inter-operate and consume .NET assemblies written in other .NET languages. So if your platform is Windows and you also use .NET or your company does then should consider IronPython.</p>
1
2009-02-26T10:55:51Z
[ "python", "ironpython", "cpython" ]
Python or IronPython
590,007
<p>How does IronPython stack up to the default Windows implementation of Python from python.org? If I am learning Python, will I be learning a subtley different language with IronPython, and what libraries would I be doing without?</p> <p>Are there, alternatively, any pros to IronPython (not including .NET IL compiled...
26
2009-02-26T10:41:16Z
590,216
<p>One of the pros of IronPython is that, unlike CPython, IronPython doesn't use the Global Interpreter Lock, thus making threading more effective. </p> <p>In the standard Python implementation, threads grab the GIL on each object access. This limits parallel execution, which matters especially if you expect to fully ...
2
2009-02-26T11:50:53Z
[ "python", "ironpython", "cpython" ]
Python or IronPython
590,007
<p>How does IronPython stack up to the default Windows implementation of Python from python.org? If I am learning Python, will I be learning a subtley different language with IronPython, and what libraries would I be doing without?</p> <p>Are there, alternatively, any pros to IronPython (not including .NET IL compiled...
26
2009-02-26T10:41:16Z
590,782
<p>There are a number of important differences:</p> <ol> <li>Interoperability with other .NET languages. You can use other .NET libraries from an IronPython application, or use IronPython from a C# application, for example. This interoperability is increasing, with a movement toward greater support for dynamic types...
27
2009-02-26T14:30:48Z
[ "python", "ironpython", "cpython" ]
Python or IronPython
590,007
<p>How does IronPython stack up to the default Windows implementation of Python from python.org? If I am learning Python, will I be learning a subtley different language with IronPython, and what libraries would I be doing without?</p> <p>Are there, alternatively, any pros to IronPython (not including .NET IL compiled...
26
2009-02-26T10:41:16Z
590,811
<p>See the blog post <a href="http://www.johndcook.com/blog/2009/02/26/ironpython-is-a-one-way-gate/" rel="nofollow">IronPython is a one-way gate</a>. It summarizes some things I've learned about IronPython from asking questions on StackOverflow.</p>
2
2009-02-26T14:36:37Z
[ "python", "ironpython", "cpython" ]
Python or IronPython
590,007
<p>How does IronPython stack up to the default Windows implementation of Python from python.org? If I am learning Python, will I be learning a subtley different language with IronPython, and what libraries would I be doing without?</p> <p>Are there, alternatively, any pros to IronPython (not including .NET IL compiled...
26
2009-02-26T10:41:16Z
590,824
<p>Pro: You can run <a href="http://www.codeplex.com/sdlsdk" rel="nofollow">IronPython in a browser</a> if SilverLight is installed.</p>
2
2009-02-26T14:39:43Z
[ "python", "ironpython", "cpython" ]
Video and Voice chat operability in Python
590,053
<p>I'm trying to find resources on video and voice chat operability in Python... Does anybody know of some good resources or sample projects?</p> <p>Any help would really be appreciated!</p>
3
2009-02-26T10:56:32Z
590,202
<p>If you want something higher level you can try <a href="http://live.gnome.org/Empathy" rel="nofollow">Empathy</a>. It has python bindings so you can use chat GUI elements on your application or create your own. If you want something lower level, you might want to try to use <a href="http://telepathy.freedesktop.org/...
1
2009-02-26T11:46:50Z
[ "python", "audio", "chat", "voice" ]
Video and Voice chat operability in Python
590,053
<p>I'm trying to find resources on video and voice chat operability in Python... Does anybody know of some good resources or sample projects?</p> <p>Any help would really be appreciated!</p>
3
2009-02-26T10:56:32Z
6,936,316
<p>Basically a farsight lib which has xmpp-jingle that can accomodate video and voice chat is commonly used,for python itz farsight.py: try it.....!</p>
0
2011-08-04T04:47:54Z
[ "python", "audio", "chat", "voice" ]
How to change a Python module name?
590,250
<p>Is it only possible if I rename the file? Or is there a <code>__module__</code> variable to the file to define what's its name?</p>
3
2009-02-26T12:01:22Z
590,262
<p>Yes, you should rename the file. Best would be after you have done that to remove the <code>oldname.pyc</code> and <code>oldname.pyo</code> compiled files (if present) from your system, otherwise the module will be importable under the old name too.</p>
8
2009-02-26T12:05:14Z
[ "python", "module" ]
How to change a Python module name?
590,250
<p>Is it only possible if I rename the file? Or is there a <code>__module__</code> variable to the file to define what's its name?</p>
3
2009-02-26T12:01:22Z
590,271
<p>Every class has an <code>__module__</code> property, although I believe changing this will not change the namespace of the Class.</p> <p>If it is possible, it would probably involve using setattr to insert the methods or class into the desired module, although you run the risk of making your code very confusing to ...
0
2009-02-26T12:06:27Z
[ "python", "module" ]
How to change a Python module name?
590,250
<p>Is it only possible if I rename the file? Or is there a <code>__module__</code> variable to the file to define what's its name?</p>
3
2009-02-26T12:01:22Z
590,279
<p>Where would you like to have this <code>__module__</code> variable, so your original script knows what to import? Modules are recognized by file names and looked in paths defined in <code>sys.path</code> variable. </p> <p>So, you have to rename the file, then remove the <code>oldname.pyc</code>, just to make sure e...
0
2009-02-26T12:07:30Z
[ "python", "module" ]
How to change a Python module name?
590,250
<p>Is it only possible if I rename the file? Or is there a <code>__module__</code> variable to the file to define what's its name?</p>
3
2009-02-26T12:01:22Z
590,312
<p>If you really want to import the file 'oldname.py' with the statement 'import newname', there is a trick that makes it possible: Import the module <em>somewhere</em> with the old name, then inject it into <code>sys.modules</code> with the new name. Subsequent import statements will also find it under the new name....
15
2009-02-26T12:16:09Z
[ "python", "module" ]
How to change a Python module name?
590,250
<p>Is it only possible if I rename the file? Or is there a <code>__module__</code> variable to the file to define what's its name?</p>
3
2009-02-26T12:01:22Z
590,353
<p>You can change the name used for a module when importing by using as:</p> <pre><code>import foo as bar print bar.baz </code></pre>
9
2009-02-26T12:26:13Z
[ "python", "module" ]
How to change a Python module name?
590,250
<p>Is it only possible if I rename the file? Or is there a <code>__module__</code> variable to the file to define what's its name?</p>
3
2009-02-26T12:01:22Z
590,358
<p>When you do <code>import module_name</code> the Python interpreter looks for a file <code>module_name</code><em><code>.extension</code></em> in PYTHONPATH. So there's no chaging that name without changing name of the file. But of course you can do:</p> <pre><code>import module_name as new_module_name </code></pre>...
1
2009-02-26T12:27:07Z
[ "python", "module" ]
How to change a Python module name?
590,250
<p>Is it only possible if I rename the file? Or is there a <code>__module__</code> variable to the file to define what's its name?</p>
3
2009-02-26T12:01:22Z
20,102,459
<p>I had an issue like this with bsddb. I was forced to install the bsddb3 module but hundreds of scripts imported bsddb. Instead of changing the import in all of them, I extracted the bsddb3 egg, and created a soft link in the site-packages directory so that both "bsddb" and "bsddb3" were one in the same to python.</p...
0
2013-11-20T17:13:31Z
[ "python", "module" ]
How can I query a model by if there is a subclass instance?
590,653
<p>I have these two simple models, A and B:</p> <pre><code>from django.db import models class A(models.Model): name = models.CharField(max_length=10) class B(A): age = models.IntegerField() </code></pre> <p>Now, how can I query for all instances of A which do not have an instance of B?</p> <p>The only way I fo...
2
2009-02-26T13:57:53Z
590,785
<p>I don't work with django, but it looks like you want the isinstance(obj, type) built-in python method.</p> <p><strong>Edit:</strong><br> Would A.objects.exclude(id__exact=B__id) work?</p>
0
2009-02-26T14:31:35Z
[ "python", "django", "model-inheritance" ]
How can I query a model by if there is a subclass instance?
590,653
<p>I have these two simple models, A and B:</p> <pre><code>from django.db import models class A(models.Model): name = models.CharField(max_length=10) class B(A): age = models.IntegerField() </code></pre> <p>Now, how can I query for all instances of A which do not have an instance of B?</p> <p>The only way I fo...
2
2009-02-26T13:57:53Z
591,157
<p>I'm not sure it's possible to do this purely in the DB with Django's ORM, in a single query. Here's the best I've been able to do:</p> <pre><code>A.objects.exclude(id__in=[r[0] for r in B.objects.values_list("a_ptr_id")]) </code></pre> <p>This is 2 DB queries, and works best with a simplistic inheritance graph - e...
1
2009-02-26T15:57:12Z
[ "python", "django", "model-inheritance" ]
How can I query a model by if there is a subclass instance?
590,653
<p>I have these two simple models, A and B:</p> <pre><code>from django.db import models class A(models.Model): name = models.CharField(max_length=10) class B(A): age = models.IntegerField() </code></pre> <p>Now, how can I query for all instances of A which do not have an instance of B?</p> <p>The only way I fo...
2
2009-02-26T13:57:53Z
10,824,061
<p>Since some version of django or python this works as well:</p> <pre><code>A.Objects.all().filter(b__isnull=True) </code></pre> <p>because if a is an A object a.b gives the subclass B of a when it exists</p> <p>I Know this is an old question, but my answer might help new searchers on this subject.</p> <p>see also...
2
2012-05-30T20:50:32Z
[ "python", "django", "model-inheritance" ]
Django - designing models with virtual fields?
590,921
<p>I'd like to ask about the most elegant approach when it comes to designing models with virtual fields such as below in Django... </p> <p>Let's say we're building an online store and all the products in the system are defined by the model "<em>Product</em>".</p> <pre><code>class Product(models.Model): # common ...
2
2009-02-26T15:08:17Z
590,928
<p>Ruby on Rails has a "serialized" field which allows you to pack a dictionary into a text field. Perhaps DJango offers something similar?</p> <p><a href="http://www.davidcramer.net/code/181/custom-fields-in-django.html" rel="nofollow">This article</a> has an implementation of a SerializedDataField.</p>
3
2009-02-26T15:09:48Z
[ "python", "django", "django-models" ]
Django - designing models with virtual fields?
590,921
<p>I'd like to ask about the most elegant approach when it comes to designing models with virtual fields such as below in Django... </p> <p>Let's say we're building an online store and all the products in the system are defined by the model "<em>Product</em>".</p> <pre><code>class Product(models.Model): # common ...
2
2009-02-26T15:08:17Z
590,968
<p>Products have Features.</p> <pre><code>class Feature( models.Model ): feature_name = models.CharField( max_length=128 ) feature_value = models.TextField() part_of = models.ForeignKey( Product ) </code></pre> <p>Like that.</p> <p>Just a list of features. </p> <pre><code>p= Product( "iPhone", "Apple",...
13
2009-02-26T15:17:49Z
[ "python", "django", "django-models" ]
Django - designing models with virtual fields?
590,921
<p>I'd like to ask about the most elegant approach when it comes to designing models with virtual fields such as below in Django... </p> <p>Let's say we're building an online store and all the products in the system are defined by the model "<em>Product</em>".</p> <pre><code>class Product(models.Model): # common ...
2
2009-02-26T15:08:17Z
591,015
<p>Personally, I'd go with S. Lott's answer. However, you might want to create a custom JSON Field:</p> <p><a href="http://svn.navi.cx/misc/trunk/djblets/djblets/util/fields.py" rel="nofollow">http://svn.navi.cx/misc/trunk/djblets/djblets/util/fields.py</a></p> <p><a href="http://www.djangosnippets.org/snippets/377/"...
2
2009-02-26T15:28:37Z
[ "python", "django", "django-models" ]
Django - designing models with virtual fields?
590,921
<p>I'd like to ask about the most elegant approach when it comes to designing models with virtual fields such as below in Django... </p> <p>Let's say we're building an online store and all the products in the system are defined by the model "<em>Product</em>".</p> <pre><code>class Product(models.Model): # common ...
2
2009-02-26T15:08:17Z
596,609
<p>Go with the inheritance. Create Produce subclasses with their own, additional fields. </p>
0
2009-02-27T20:43:55Z
[ "python", "django", "django-models" ]
Can I use a decorator to mutate the local scope of a function in Python?
591,200
<p>Is there any way of writing a decorator such that the following would work?</p> <pre><code>assert 'z' not in globals() @my_decorator def func(x, y): print z </code></pre> <p><hr /></p> <p>EDIT: moved from anwser</p> <p>In answer to hop's "why?": syntax sugar / DRY.</p> <p>It's not about caching, it's about ...
8
2009-02-26T16:03:08Z
591,295
<p>I don't know about the local scope, but you could provide an alternative global name space temporarily. Something like:</p> <pre> <code> import types def my_decorator(fn): def decorated(*args,**kw): my_globals={} my_globals.update(globals()) my_globals['z']='value of z' call_f...
8
2009-02-26T16:23:43Z
[ "python", "decorator" ]
Can I use a decorator to mutate the local scope of a function in Python?
591,200
<p>Is there any way of writing a decorator such that the following would work?</p> <pre><code>assert 'z' not in globals() @my_decorator def func(x, y): print z </code></pre> <p><hr /></p> <p>EDIT: moved from anwser</p> <p>In answer to hop's "why?": syntax sugar / DRY.</p> <p>It's not about caching, it's about ...
8
2009-02-26T16:03:08Z
591,309
<p>a) don't do it.</p> <p>b) seriously, why would you do that?</p> <p>c) you could declare z as global within your decorator, so z will not be in globals() until after the decorator has been called for the first time, so the assert won't bark.</p> <p>d) why???</p>
6
2009-02-26T16:26:19Z
[ "python", "decorator" ]
Can I use a decorator to mutate the local scope of a function in Python?
591,200
<p>Is there any way of writing a decorator such that the following would work?</p> <pre><code>assert 'z' not in globals() @my_decorator def func(x, y): print z </code></pre> <p><hr /></p> <p>EDIT: moved from anwser</p> <p>In answer to hop's "why?": syntax sugar / DRY.</p> <p>It's not about caching, it's about ...
8
2009-02-26T16:03:08Z
591,948
<p><em>I could probably get a similar effect from using the class infrastructure as well, but I wanted to see if it was doable with raw functions.</em></p> <p>Well, Python is an object-oriented language. You should do this in a class, in my opinion. Making a nice class interface would surely simplify your problem. Th...
1
2009-02-26T19:02:39Z
[ "python", "decorator" ]
Can I use a decorator to mutate the local scope of a function in Python?
591,200
<p>Is there any way of writing a decorator such that the following would work?</p> <pre><code>assert 'z' not in globals() @my_decorator def func(x, y): print z </code></pre> <p><hr /></p> <p>EDIT: moved from anwser</p> <p>In answer to hop's "why?": syntax sugar / DRY.</p> <p>It's not about caching, it's about ...
8
2009-02-26T16:03:08Z
591,982
<p>I'll first echo the "please don't", but that's your choice. Here's a solution for you:</p> <pre><code>assert 'z' not in globals () class my_dec: def __init__ (self, f): self.f = f def __call__ (self,x,y): z = x+y self.f(x,y,z) @my_dec def func (x,y,z): print z func (1,3) </code></pre>...
2
2009-02-26T19:09:07Z
[ "python", "decorator" ]
Can I use a decorator to mutate the local scope of a function in Python?
591,200
<p>Is there any way of writing a decorator such that the following would work?</p> <pre><code>assert 'z' not in globals() @my_decorator def func(x, y): print z </code></pre> <p><hr /></p> <p>EDIT: moved from anwser</p> <p>In answer to hop's "why?": syntax sugar / DRY.</p> <p>It's not about caching, it's about ...
8
2009-02-26T16:03:08Z
592,019
<p>Echoing Hop's answer</p> <ol> <li>Don't do it.</li> <li>Seriously, don't do this. Lisp and Ruby are more appropriate languages for writing your own custom syntax. Use one of those. Or find a cleaner way to do this</li> <li>If you must, you want dynamic scoped variables, not lexically scoped.</li> </ol> <p>Pytho...
10
2009-02-26T19:20:17Z
[ "python", "decorator" ]
Can I use a decorator to mutate the local scope of a function in Python?
591,200
<p>Is there any way of writing a decorator such that the following would work?</p> <pre><code>assert 'z' not in globals() @my_decorator def func(x, y): print z </code></pre> <p><hr /></p> <p>EDIT: moved from anwser</p> <p>In answer to hop's "why?": syntax sugar / DRY.</p> <p>It's not about caching, it's about ...
8
2009-02-26T16:03:08Z
593,077
<p>Explicit is better than implicit.</p> <p>Is this good enough?</p> <pre><code>def provide_value(f): f.foo = "Bar" return f @provide_value def g(x): print g.foo </code></pre> <p>(If you really want evil, assigning to f.func_globals seems fun.)</p>
0
2009-02-27T00:19:45Z
[ "python", "decorator" ]
Can I use a decorator to mutate the local scope of a function in Python?
591,200
<p>Is there any way of writing a decorator such that the following would work?</p> <pre><code>assert 'z' not in globals() @my_decorator def func(x, y): print z </code></pre> <p><hr /></p> <p>EDIT: moved from anwser</p> <p>In answer to hop's "why?": syntax sugar / DRY.</p> <p>It's not about caching, it's about ...
8
2009-02-26T16:03:08Z
593,144
<p>Others have given a few ways of making a working decorator, many have advised against doing so because it's so stylistically different from normal python behavior that it'll really confuse anyone trying to understand the code.</p> <p>If you're needing to recalculate things a lot, would it make sense to group them t...
0
2009-02-27T00:50:36Z
[ "python", "decorator" ]
When using random, which form returns an equal 50% chance?
591,253
<p>I'm guessing that most built in random generators return something like this:</p> <pre><code>[0.0, 1.0) </code></pre> <p>so if I would like a 50% chance would I use something like this:</p> <pre><code>if random() &lt; .5 </code></pre> <p>or something like:</p> <pre><code>if random() &lt;= .5 </code></pre> <p>T...
6
2009-02-26T16:15:37Z
591,272
<p>It doesn't matter. Both formulas yield the desired result.</p>
-2
2009-02-26T16:19:19Z
[ "python", "random" ]
When using random, which form returns an equal 50% chance?
591,253
<p>I'm guessing that most built in random generators return something like this:</p> <pre><code>[0.0, 1.0) </code></pre> <p>so if I would like a 50% chance would I use something like this:</p> <pre><code>if random() &lt; .5 </code></pre> <p>or something like:</p> <pre><code>if random() &lt;= .5 </code></pre> <p>T...
6
2009-02-26T16:15:37Z
591,276
<p>To a first approximation, either works.</p> <p>The best way is to choose a random generator that specifically emits booleans or integers with range. Then you can nail the range exactly.</p> <p>Operations like "equality" with floating point is iffy anyway.</p>
4
2009-02-26T16:20:05Z
[ "python", "random" ]
When using random, which form returns an equal 50% chance?
591,253
<p>I'm guessing that most built in random generators return something like this:</p> <pre><code>[0.0, 1.0) </code></pre> <p>so if I would like a 50% chance would I use something like this:</p> <pre><code>if random() &lt; .5 </code></pre> <p>or something like:</p> <pre><code>if random() &lt;= .5 </code></pre> <p>T...
6
2009-02-26T16:15:37Z
591,282
<pre><code> if random() &lt; .5 </code></pre> <p>In binary, .5 is .10000000000.... So, basically, the question reduces to, "What is the first binary digit after the radix point... 0 (for &lt;.5) or 1 (for .5 or more) ?"</p>
3
2009-02-26T16:21:16Z
[ "python", "random" ]
When using random, which form returns an equal 50% chance?
591,253
<p>I'm guessing that most built in random generators return something like this:</p> <pre><code>[0.0, 1.0) </code></pre> <p>so if I would like a 50% chance would I use something like this:</p> <pre><code>if random() &lt; .5 </code></pre> <p>or something like:</p> <pre><code>if random() &lt;= .5 </code></pre> <p>T...
6
2009-02-26T16:15:37Z
591,283
<pre><code>&lt; .5 </code></pre> <p>but that assumes that your floating point support is good enough to satisfy your precision demands (since it's never <em>exactly</em>).</p>
0
2009-02-26T16:21:19Z
[ "python", "random" ]
When using random, which form returns an equal 50% chance?
591,253
<p>I'm guessing that most built in random generators return something like this:</p> <pre><code>[0.0, 1.0) </code></pre> <p>so if I would like a 50% chance would I use something like this:</p> <pre><code>if random() &lt; .5 </code></pre> <p>or something like:</p> <pre><code>if random() &lt;= .5 </code></pre> <p>T...
6
2009-02-26T16:15:37Z
591,286
<p>If I want 50% chance, I'd just check for the LSB (Least Significant Bit).</p>
0
2009-02-26T16:21:40Z
[ "python", "random" ]
When using random, which form returns an equal 50% chance?
591,253
<p>I'm guessing that most built in random generators return something like this:</p> <pre><code>[0.0, 1.0) </code></pre> <p>so if I would like a 50% chance would I use something like this:</p> <pre><code>if random() &lt; .5 </code></pre> <p>or something like:</p> <pre><code>if random() &lt;= .5 </code></pre> <p>T...
6
2009-02-26T16:15:37Z
591,294
<p>Ah, the old ".5" problem. Here's the answer:</p> <p>If you're going to divide 10 things into two <strong>equal</strong> parts, you need 5 things in each part. 0 thru 4 in the first part, 5-9 in the second part. So... <code>&lt; .5</code> is correct.</p>
12
2009-02-26T16:23:32Z
[ "python", "random" ]
When using random, which form returns an equal 50% chance?
591,253
<p>I'm guessing that most built in random generators return something like this:</p> <pre><code>[0.0, 1.0) </code></pre> <p>so if I would like a 50% chance would I use something like this:</p> <pre><code>if random() &lt; .5 </code></pre> <p>or something like:</p> <pre><code>if random() &lt;= .5 </code></pre> <p>T...
6
2009-02-26T16:15:37Z
591,353
<p>For a particular system you should test it, if you really want to find out. Could take a while though :-)</p> <p>I agree with the other posters that to the first order, it doesn't matter. If it does matter, you'll need a better RNG anyway.</p> <p><strong>EDIT:</strong> I just tried the default RNG in C# with 1,...
1
2009-02-26T16:39:14Z
[ "python", "random" ]
When using random, which form returns an equal 50% chance?
591,253
<p>I'm guessing that most built in random generators return something like this:</p> <pre><code>[0.0, 1.0) </code></pre> <p>so if I would like a 50% chance would I use something like this:</p> <pre><code>if random() &lt; .5 </code></pre> <p>or something like:</p> <pre><code>if random() &lt;= .5 </code></pre> <p>T...
6
2009-02-26T16:15:37Z
591,378
<p>If you need truly random numbers, you can try <a href="http://random.org/" rel="nofollow">random.org</a>. They offer a web service connected to a device that detects noise coming from the universe. Still not random technically speaking, but close enough I guess. </p> <p>They have all sort of way to make the query s...
0
2009-02-26T16:44:19Z
[ "python", "random" ]
When using random, which form returns an equal 50% chance?
591,253
<p>I'm guessing that most built in random generators return something like this:</p> <pre><code>[0.0, 1.0) </code></pre> <p>so if I would like a 50% chance would I use something like this:</p> <pre><code>if random() &lt; .5 </code></pre> <p>or something like:</p> <pre><code>if random() &lt;= .5 </code></pre> <p>T...
6
2009-02-26T16:15:37Z
591,389
<p>If you're going to be that fussy about your random numbers, don't rely on anything that comes built-in. Get a well-documented RNG from a place you trust (I trust Boost, for what that's worth), and read the documentation. It used to be that standard RNGs were notoriously bad, and I still wouldn't trust them.</p> <...
0
2009-02-26T16:46:01Z
[ "python", "random" ]
When using random, which form returns an equal 50% chance?
591,253
<p>I'm guessing that most built in random generators return something like this:</p> <pre><code>[0.0, 1.0) </code></pre> <p>so if I would like a 50% chance would I use something like this:</p> <pre><code>if random() &lt; .5 </code></pre> <p>or something like:</p> <pre><code>if random() &lt;= .5 </code></pre> <p>T...
6
2009-02-26T16:15:37Z
993,264
<p>Why do it yourself? Python has random.choice :)</p> <p>random.choice([0, 1]) will give you 0/1 with equal chances -- and it is a standard part of Python, coded up by the same people who wrote random.random (and thus know more about its semantics than anyone else)</p>
2
2009-06-14T17:28:14Z
[ "python", "random" ]
When using random, which form returns an equal 50% chance?
591,253
<p>I'm guessing that most built in random generators return something like this:</p> <pre><code>[0.0, 1.0) </code></pre> <p>so if I would like a 50% chance would I use something like this:</p> <pre><code>if random() &lt; .5 </code></pre> <p>or something like:</p> <pre><code>if random() &lt;= .5 </code></pre> <p>T...
6
2009-02-26T16:15:37Z
1,020,029
<pre><code>import random def fifty_fifty(): "Return 0 or 1 with 50% chance for each" return random.randrange(2) </code></pre>
1
2009-06-19T21:08:18Z
[ "python", "random" ]
How can I modify password expiration in Windows using Python?
591,300
<p>How can I modify the password expiration to "never" on Windows XP for a local user with Python? I have the PyWIN and WMI modules on board but have no solution. I managed to query the current settings via WMI(based on Win32_UserAccount class), but how can modify it?</p>
4
2009-02-26T16:24:28Z
591,375
<p>That change would require administrator permissions, which may (or may not) cause issues inside of PyWin32. I don't see any straight-forward way of making this change from a Python script, but I'm sure this can be automated using a different method.</p> <p>This MSFN thread seems to have info that will help you, or...
0
2009-02-26T16:43:51Z
[ "python", "windows", "passwords" ]
How can I modify password expiration in Windows using Python?
591,300
<p>How can I modify the password expiration to "never" on Windows XP for a local user with Python? I have the PyWIN and WMI modules on board but have no solution. I managed to query the current settings via WMI(based on Win32_UserAccount class), but how can modify it?</p>
4
2009-02-26T16:24:28Z
600,635
<p>You might need admin priviliges to do that, so look into elevating the current process or launch a new process with more priviliges. (I.e. something like vista's UAC but on XP.)</p> <p>Can't help with details though. :-/</p>
0
2009-03-01T22:05:10Z
[ "python", "windows", "passwords" ]
How can I modify password expiration in Windows using Python?
591,300
<p>How can I modify the password expiration to "never" on Windows XP for a local user with Python? I have the PyWIN and WMI modules on board but have no solution. I managed to query the current settings via WMI(based on Win32_UserAccount class), but how can modify it?</p>
4
2009-02-26T16:24:28Z
603,428
<p>If you are running your python script with ActvePython against Active Directory, then you can use something like this:</p> <pre><code>import win32com.client ads = win32com.client.Dispatch('ADsNameSpaces') user = ads.getObject("", "WinNT://DOMAIN/username,user") user.Getinfo() user.Put('userAccountControl', 65536 | ...
1
2009-03-02T18:29:00Z
[ "python", "windows", "passwords" ]
python variables not accepting names
591,421
<p>I'm trying to declare a few simple variables as part of a function in a very basic collision detection programme. For some reason it's rejecting my variables (although only some of them even though they're near identical). Here's the code for the function;</p> <pre><code>def TimeCheck(): timechecknumber = int(t...
0
2009-02-26T16:53:16Z
591,431
<p>You have mismatched parentheses on the line beginning with <code>backgroundr</code>. I think maybe you want this:</p> <pre><code>backgroundr = int(random.random() * 255) + 1 </code></pre> <p>Note that each of the next two lines also have mismatched parentheses, so you'll have to fix those, too.</p>
8
2009-02-26T16:55:47Z
[ "python", "variables", "syntax-error" ]
python variables not accepting names
591,421
<p>I'm trying to declare a few simple variables as part of a function in a very basic collision detection programme. For some reason it's rejecting my variables (although only some of them even though they're near identical). Here's the code for the function;</p> <pre><code>def TimeCheck(): timechecknumber = int(t...
0
2009-02-26T16:53:16Z
591,444
<p>mipadi's answer will always yield a 1. You need to multiply by 255 before you cast to int. Try this.</p> <pre><code>backgroundr = int(random.random() * 255) + 1 </code></pre>
2
2009-02-26T17:00:12Z
[ "python", "variables", "syntax-error" ]
Python Win32 - DriveInfo On Mapped Drive
591,443
<p>Does anyone know how I can determine the server and share name of a mapped network drive?</p> <p>For example:</p> <pre><code>import win32file, win32api for logDrive in win32api.GetLogicalDriveStrings().split("\x00"): if win32file.GetDriveType(logDrive) != win32file.DRIVE_REMOTE: continue # get server and share...
2
2009-02-26T17:00:07Z
591,452
<p>You'll have to call the win32 API: <a href="http://msdn.microsoft.com/en-us/library/aa385474%28VS.85%29.aspx" rel="nofollow">WNetGetUniversalName</a></p>
2
2009-02-26T17:01:34Z
[ "python", "winapi" ]
How can you programmatically tell the CPython interpreter to enter interactive mode when done?
591,520
<p>If you invoke the cpython interpreter with the -i option, it will enter the interactive mode upon completing any commands or scripts it has been given to run. Is there a way, within a program to get the interpreter to do this even when it has not been given -i? The obvious use case is in debugging by interactively ...
8
2009-02-26T17:21:57Z
591,531
<p>You want the <a href="http://docs.python.org/library/code.html">code module</a>.</p> <pre><code>#!/usr/bin/env python import code code.interact("Enter Here") </code></pre>
13
2009-02-26T17:25:18Z
[ "cpython", "python" ]
How can you programmatically tell the CPython interpreter to enter interactive mode when done?
591,520
<p>If you invoke the cpython interpreter with the -i option, it will enter the interactive mode upon completing any commands or scripts it has been given to run. Is there a way, within a program to get the interpreter to do this even when it has not been given -i? The obvious use case is in debugging by interactively ...
8
2009-02-26T17:21:57Z
591,971
<p>Set the PYTHONINSPECT environment variable. This can also be done in the script itself:</p> <pre><code>import os os.environ["PYTHONINSPECT"] = "1" </code></pre> <p>For debugging unexpected exceptions, you could also use this nice recipe <a href="http://code.activestate.com/recipes/65287/">http://code.activestate....
5
2009-02-26T19:06:18Z
[ "cpython", "python" ]
How can you programmatically tell the CPython interpreter to enter interactive mode when done?
591,520
<p>If you invoke the cpython interpreter with the -i option, it will enter the interactive mode upon completing any commands or scripts it has been given to run. Is there a way, within a program to get the interpreter to do this even when it has not been given -i? The obvious use case is in debugging by interactively ...
8
2009-02-26T17:21:57Z
593,013
<p>The <a href="http://code.activestate.com/recipes/65287/" rel="nofollow">recipe</a> metioned in the other answer using <code>sys.excepthook</code>, sounds like what you want. Otherwise, you could run <code>code.interact</code> on program exit:</p> <pre><code>import code import sys sys.exitfunc = code.interact </code...
3
2009-02-26T23:49:05Z
[ "cpython", "python" ]
How can you programmatically tell the CPython interpreter to enter interactive mode when done?
591,520
<p>If you invoke the cpython interpreter with the -i option, it will enter the interactive mode upon completing any commands or scripts it has been given to run. Is there a way, within a program to get the interpreter to do this even when it has not been given -i? The obvious use case is in debugging by interactively ...
8
2009-02-26T17:21:57Z
17,989,239
<p>The best way to do this that I know of is:</p> <pre><code>from IPython import embed embed() </code></pre> <p>which allows access to variables in the current scope and brings you the full power of IPython.</p>
1
2013-08-01T08:48:04Z
[ "cpython", "python" ]
pygame function appears to be being ignored
591,776
<p>I'm building a relatively simple programme to test collision detection, it's all working fine at the moment except one thing, I'm trying to make the background colour change randomly, the only issue is that it appears to be completely skipping the function to do this;</p> <pre><code>import pygame from pygame.locals...
2
2009-02-26T18:15:48Z
591,821
<p>I believe backgroundr, backgroundg, and backgroundb are local variables to your ColourCheck() function.</p> <p>If you're determined to use global variables, try this at the top of your file:</p> <pre><code>global backgroundr; global backgroundg; global backgroundb; backgroundr = int(random.random()*255)+1 backgrou...
6
2009-02-26T18:26:27Z
[ "python", "function", "pygame" ]
pygame function appears to be being ignored
591,776
<p>I'm building a relatively simple programme to test collision detection, it's all working fine at the moment except one thing, I'm trying to make the background colour change randomly, the only issue is that it appears to be completely skipping the function to do this;</p> <pre><code>import pygame from pygame.locals...
2
2009-02-26T18:15:48Z
591,824
<p><code>Move()</code>, <code>CollisionDetect()</code>, and <code>Draw()</code> all refer to <code>Circles</code>, but don't declare it global. Try adding a <code>global Circles</code> line to the beginning of each function. Also, I'd recommend changing your variables to lower-case; not only does an initial cap typic...
0
2009-02-26T18:27:08Z
[ "python", "function", "pygame" ]
Is there a python equivalent of the prefuse visualization toolkit?
591,839
<p>The <a href="http://prefuse.org/" rel="nofollow">prefuse visualization toolkit</a> is pretty nice, but for Java. I was wondering if there was something similar for python. My primary interest is being able to navigate dynamic graphs.</p>
10
2009-02-26T18:30:45Z
591,847
<p><a href="http://mayavi.sourceforge.net/" rel="nofollow">MayaVi</a></p>
-1
2009-02-26T18:33:07Z
[ "python", "visualization", "prefuse" ]
Is there a python equivalent of the prefuse visualization toolkit?
591,839
<p>The <a href="http://prefuse.org/" rel="nofollow">prefuse visualization toolkit</a> is pretty nice, but for Java. I was wondering if there was something similar for python. My primary interest is being able to navigate dynamic graphs.</p>
10
2009-02-26T18:30:45Z
591,851
<p>Note that prefuse now has the <a href="http://flare.prefuse.org/" rel="nofollow">flare</a> package which uses flash. </p> <p>Connect that to a Python backend via <a href="http://mdp.cti.depaul.edu/" rel="nofollow">web2py</a> and you've got a great web app (just an idea).</p>
0
2009-02-26T18:35:03Z
[ "python", "visualization", "prefuse" ]
Is there a python equivalent of the prefuse visualization toolkit?
591,839
<p>The <a href="http://prefuse.org/" rel="nofollow">prefuse visualization toolkit</a> is pretty nice, but for Java. I was wondering if there was something similar for python. My primary interest is being able to navigate dynamic graphs.</p>
10
2009-02-26T18:30:45Z
592,004
<p>You could try using prefuse with <a href="http://jpype.sourceforge.net/" rel="nofollow">JPype</a>, if you can't find a suitable replacement.</p>
0
2009-02-26T19:16:35Z
[ "python", "visualization", "prefuse" ]
Is there a python equivalent of the prefuse visualization toolkit?
591,839
<p>The <a href="http://prefuse.org/" rel="nofollow">prefuse visualization toolkit</a> is pretty nice, but for Java. I was wondering if there was something similar for python. My primary interest is being able to navigate dynamic graphs.</p>
10
2009-02-26T18:30:45Z
592,336
<p>If you're using a Mac, check out <a href="http://nodebox.net/code/index.php/Home" rel="nofollow">NodeBox</a>. One extension it offers is a <a href="http://nodebox.net/code/index.php/Graphing" rel="nofollow">graph library</a> that looks pretty good. Poke around in the <a href="http://nodebox.net/code/index.php/Galler...
2
2009-02-26T20:38:22Z
[ "python", "visualization", "prefuse" ]
Is there a python equivalent of the prefuse visualization toolkit?
591,839
<p>The <a href="http://prefuse.org/" rel="nofollow">prefuse visualization toolkit</a> is pretty nice, but for Java. I was wondering if there was something similar for python. My primary interest is being able to navigate dynamic graphs.</p>
10
2009-02-26T18:30:45Z
849,870
<p>I know this is not exactly python, but you could use prefuse in python <em>through</em> <a href="http://jython.org" rel="nofollow">jython</a></p> <p>Something along the lines of:</p> <p>Add prefuse to your path:</p> <p><code>export JYTHONPATH=$JYTHONPATH:prefuse.jar</code></p> <p>and</p> <p><code>&gt;&gt;&gt; i...
6
2009-05-11T20:10:57Z
[ "python", "visualization", "prefuse" ]
Is there a python equivalent of the prefuse visualization toolkit?
591,839
<p>The <a href="http://prefuse.org/" rel="nofollow">prefuse visualization toolkit</a> is pretty nice, but for Java. I was wondering if there was something similar for python. My primary interest is being able to navigate dynamic graphs.</p>
10
2009-02-26T18:30:45Z
988,053
<p>You might want to check out <a href="http://people.csail.mit.edu/rasmus/summon/index.shtml" rel="nofollow">SUMMON</a>, a visualization system that uses python but handles fairly large data sets. There's an impressive video of visualizing and navigating a massive tree. (Can't post the link because I'm a first time ...
3
2009-06-12T17:48:49Z
[ "python", "visualization", "prefuse" ]