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
Is there a better library than urlgrabber for fetching remote urls in python?
2,040,628
<p>I'm writing a spider that needs a <code>load_url</code> function that performs the following for me:</p> <ol> <li>Retry the URL if there is a temporary error, without leaking exceptions.</li> <li>Not leak memory or file handles</li> <li>Use HTTP-KeepAlive for speed (optional)</li> </ol> <p><a href="http://linux.du...
2
2010-01-11T08:29:37Z
2,040,669
<p>Also for screen scraping in Python there is <a href="http://scrapy.org/" rel="nofollow">Scrapy</a> though I don't know if this will fulfil your requirements.</p>
0
2010-01-11T08:40:31Z
[ "python", "screen-scraping" ]
Is there a better library than urlgrabber for fetching remote urls in python?
2,040,628
<p>I'm writing a spider that needs a <code>load_url</code> function that performs the following for me:</p> <ol> <li>Retry the URL if there is a temporary error, without leaking exceptions.</li> <li>Not leak memory or file handles</li> <li>Use HTTP-KeepAlive for speed (optional)</li> </ol> <p><a href="http://linux.du...
2
2010-01-11T08:29:37Z
2,040,673
<p>If you are writing a web-crawler / screen-scraper, you may be interested to look at a dedicated framework such as <a href="http://scrapy.org/" rel="nofollow">scrapy</a>. </p> <p>You can write really quite sophisticated web crawlers with very little code: it takes care of all the gory details of scheduling the requ...
4
2010-01-11T08:41:11Z
[ "python", "screen-scraping" ]
Is there a better library than urlgrabber for fetching remote urls in python?
2,040,628
<p>I'm writing a spider that needs a <code>load_url</code> function that performs the following for me:</p> <ol> <li>Retry the URL if there is a temporary error, without leaking exceptions.</li> <li>Not leak memory or file handles</li> <li>Use HTTP-KeepAlive for speed (optional)</li> </ol> <p><a href="http://linux.du...
2
2010-01-11T08:29:37Z
2,043,364
<p>Scrapy sounds great, and I will consider using it in the future. For this project however, I'm really looking for a simple function as described above. I have created one that seems to be getting the job done.</p> <pre><code>import urllib2 class HttpLoadError(RuntimeError): pass class Http404(HttpLoadError): ...
0
2010-01-11T16:57:54Z
[ "python", "screen-scraping" ]
Is there a better library than urlgrabber for fetching remote urls in python?
2,040,628
<p>I'm writing a spider that needs a <code>load_url</code> function that performs the following for me:</p> <ol> <li>Retry the URL if there is a temporary error, without leaking exceptions.</li> <li>Not leak memory or file handles</li> <li>Use HTTP-KeepAlive for speed (optional)</li> </ol> <p><a href="http://linux.du...
2
2010-01-11T08:29:37Z
2,043,408
<p>The methods employed by the <a href="http://code.google.com/p/harvestman-crawler/source/browse/trunk/HarvestMan/harvestman/lib/connector.py" rel="nofollow">Harvestman crawler</a> might be worth studying.</p>
0
2010-01-11T17:04:33Z
[ "python", "screen-scraping" ]
OS/X mimetype handler
2,040,633
<p>I'd like to write a small script that implements <a href="http://greenbytes.de/tech/webdav/rfc4709.html" rel="nofollow">RFC4709</a> for OS/X.</p> <p>I started off by creating an application bundle that registers the application/xml+davmount mimetype and launches a simple python script.</p> <p>It doesn't make a lot...
0
2010-01-11T08:30:53Z
2,040,778
<p>Make it a .app bundle, installed in Applications/Utilities (or really, anywhere). Bundles are the right thing on OS X. But if you really don't want to install it in Applications, how about somewhere under /Library or ~/Library, using a package rather than a drop in app. But I know I'd prefer the .app version... e...
1
2010-01-11T09:08:34Z
[ "python", "osx", "webdav", "mime-types" ]
Best way to start, stop and send parameters to separate Python script from C++ application?
2,040,769
<p>I try to explain the situation:</p> <ul> <li><p>I have a QT application written in C++ and QT.</p></li> <li><p>This QT application starts a separate console C++ application that runs in the background.</p></li> <li><p>These two communicate using perhaps sockets, don't know yet.</p></li> <li><p>Console C++ applicati...
3
2010-01-11T09:05:27Z
2,040,809
<p>Sockets, or you could use <a href="http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html" rel="nofollow">DBUS python</a>, and <a href="http://www.freedesktop.org/wiki/Software/dbus-c%2B%2B" rel="nofollow">DBUS c++</a>, if you want to be all free-desktopy :D</p>
2
2010-01-11T09:14:55Z
[ "c++", "python", "qt", "gnuradio" ]
Best way to start, stop and send parameters to separate Python script from C++ application?
2,040,769
<p>I try to explain the situation:</p> <ul> <li><p>I have a QT application written in C++ and QT.</p></li> <li><p>This QT application starts a separate console C++ application that runs in the background.</p></li> <li><p>These two communicate using perhaps sockets, don't know yet.</p></li> <li><p>Console C++ applicati...
3
2010-01-11T09:05:27Z
2,040,813
<p>Spawn python script as a new process using fork() and execv(). execv() (or any other function of the exec family) lets you pass arguments to the Python script. Use the child process ID to send a kill signal when you are done with the Python script.</p>
2
2010-01-11T09:16:06Z
[ "c++", "python", "qt", "gnuradio" ]
Best way to start, stop and send parameters to separate Python script from C++ application?
2,040,769
<p>I try to explain the situation:</p> <ul> <li><p>I have a QT application written in C++ and QT.</p></li> <li><p>This QT application starts a separate console C++ application that runs in the background.</p></li> <li><p>These two communicate using perhaps sockets, don't know yet.</p></li> <li><p>Console C++ applicati...
3
2010-01-11T09:05:27Z
2,041,387
<p>For your C++ program, you may wanna take a look here :</p> <p><a href="http://www.codeproject.com/KB/cpp/kill_process.aspx" rel="nofollow">http://www.codeproject.com/KB/cpp/kill_process.aspx</a></p> <p>Its gives you the basic code for creating and killing an external process. Remember that launching a python scrip...
0
2010-01-11T11:09:01Z
[ "c++", "python", "qt", "gnuradio" ]
How to change variables in Python
2,040,998
<p>I am trying to change a variable further down the program. I have a global variable declared at the start of the program and I want to change the variable in different functions down the program. I can do this by declaring the variable inside the function again but I would like to know is there a better way of doing...
2
2010-01-11T09:53:12Z
2,041,006
<p>Use the <a href="http://docs.python.org/reference/simple_stmts.html#global" rel="nofollow"><code>global</code></a> statement.</p> <blockquote> <p>The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals.</p> </blockq...
2
2010-01-11T09:55:00Z
[ "python" ]
How to change variables in Python
2,040,998
<p>I am trying to change a variable further down the program. I have a global variable declared at the start of the program and I want to change the variable in different functions down the program. I can do this by declaring the variable inside the function again but I would like to know is there a better way of doing...
2
2010-01-11T09:53:12Z
2,041,034
<p>You can mutate without reassigning:</p> <pre><code>variables = {} def pro(): if variables['ID'] == '': variables['ID'] = 'default' </code></pre>
-1
2010-01-11T10:01:03Z
[ "python" ]
How to change variables in Python
2,040,998
<p>I am trying to change a variable further down the program. I have a global variable declared at the start of the program and I want to change the variable in different functions down the program. I can do this by declaring the variable inside the function again but I would like to know is there a better way of doing...
2
2010-01-11T09:53:12Z
2,041,076
<p>Beware, you're doing it wrong multiple times.</p> <p>Even though you could use the global statement to change a global (it is discouraged since it's better to use function parameters and return values), that would NOT change other already assigned values. E.g. even though you reassign ID, you would NOT reassign pro...
6
2010-01-11T10:10:49Z
[ "python" ]
How to change variables in Python
2,040,998
<p>I am trying to change a variable further down the program. I have a global variable declared at the start of the program and I want to change the variable in different functions down the program. I can do this by declaring the variable inside the function again but I would like to know is there a better way of doing...
2
2010-01-11T09:53:12Z
2,041,087
<p>To update global variables you could use</p> <pre><code>global ID ID="Yes" </code></pre> <p>before assigning variable to ID = "YES"</p> <p>But changing ID will be no effect on project variable, <code>project = ("Yep"+ID)</code>, because project is already a string</p> <p>you need to make a function like</p> <pr...
4
2010-01-11T10:12:03Z
[ "python" ]
How to change variables in Python
2,040,998
<p>I am trying to change a variable further down the program. I have a global variable declared at the start of the program and I want to change the variable in different functions down the program. I can do this by declaring the variable inside the function again but I would like to know is there a better way of doing...
2
2010-01-11T09:53:12Z
2,041,196
<p>In your code you have two problems. The first about changing ID variable, which could be solved by using global. </p> <p>The second that your code calculate project string and after that project don't know about ID.</p> <p>To avoid code duplication you can define function to calc project.</p> <p>So we have:</p> ...
1
2010-01-11T10:34:47Z
[ "python" ]
How to change variables in Python
2,040,998
<p>I am trying to change a variable further down the program. I have a global variable declared at the start of the program and I want to change the variable in different functions down the program. I can do this by declaring the variable inside the function again but I would like to know is there a better way of doing...
2
2010-01-11T09:53:12Z
2,043,750
<p>Why not use a dictionary?</p> <pre><code>&gt;&gt;&gt; attr = {'start':'XXX', 'myid':'No'} &gt;&gt;&gt; &gt;&gt;&gt; def update_and_show(D, value = None): ... if value: D['myid'] = value ... print D['start'] + ' ' + D['myid'] ... &gt;&gt;&gt; update_and_show(attr) XXX No &gt;&gt;&gt; update_and_show(attr,...
0
2010-01-11T18:01:45Z
[ "python" ]
How to make phone calls using Python?
2,041,352
<p>I'm writting a small python program to send voice file to other telephone. The phone is connected to pc over usb. How to make phone calls using Python?</p>
3
2010-01-11T11:02:44Z
2,041,572
<p><a href="http://www.micromedia-int.com/en/jericho.html" rel="nofollow">Micromedia Jericho</a> is a commercial product running on Ms Windows which has this capability. It manages several types of modems and make possible to send wav file over a phone call. It also support "Text to speech" converting a text to a wave ...
1
2010-01-11T11:48:24Z
[ "python", "usb", "voice" ]
How to make phone calls using Python?
2,041,352
<p>I'm writting a small python program to send voice file to other telephone. The phone is connected to pc over usb. How to make phone calls using Python?</p>
3
2010-01-11T11:02:44Z
2,041,616
<p>You could probably use the <a href="http://sourceforge.net/apps/mediawiki/pyusb/index.php?title=Main_Page" rel="nofollow">pyusb</a> library.</p>
0
2010-01-11T11:55:21Z
[ "python", "usb", "voice" ]
How to make phone calls using Python?
2,041,352
<p>I'm writting a small python program to send voice file to other telephone. The phone is connected to pc over usb. How to make phone calls using Python?</p>
3
2010-01-11T11:02:44Z
2,041,675
<p>I think the smart way is to leave it a professional Voice/IP app such as ribbit or Twilio. I would personally recommend twilio which has Python libraries</p>
2
2010-01-11T12:09:58Z
[ "python", "usb", "voice" ]
What is The Pythonic Way for writing matching algorithm
2,041,378
<p>I have this piece of code (should be self-explanatory; if not, just ask):</p> <pre><code>for tr in completed_taskrevs: found = False for nr in completion_noterevs: if tr.description in nr.body: completion_noterevs.remove(nr) found = True break assert found </c...
2
2010-01-11T11:08:04Z
2,041,419
<p>Try this:</p> <pre><code>for tr in compleded_taskrevs: try: nrs = (nr for nr in completion_noterevs if tr.description in nr.body) completion_noterevs.remove(nrs.next()) except StopIteration: raise ValueError('Some error') </code></pre> <p>EDIT: Devin is right. Assertion is not the w...
8
2010-01-11T11:17:11Z
[ "python" ]
What is The Pythonic Way for writing matching algorithm
2,041,378
<p>I have this piece of code (should be self-explanatory; if not, just ask):</p> <pre><code>for tr in completed_taskrevs: found = False for nr in completion_noterevs: if tr.description in nr.body: completion_noterevs.remove(nr) found = True break assert found </c...
2
2010-01-11T11:08:04Z
2,041,420
<p>The list generator below could return you all items nr that are valid <code>tr.description in nr.body</code> however it's hard to simplify your algorithm as it has quite a few branches. I would just say go with the algorithm you have as long as it does what you want.</p> <pre><code>[nr for tr in completed_taskrevs ...
3
2010-01-11T11:17:19Z
[ "python" ]
What is The Pythonic Way for writing matching algorithm
2,041,378
<p>I have this piece of code (should be self-explanatory; if not, just ask):</p> <pre><code>for tr in completed_taskrevs: found = False for nr in completion_noterevs: if tr.description in nr.body: completion_noterevs.remove(nr) found = True break assert found </c...
2
2010-01-11T11:08:04Z
2,041,699
<p>Using <code>assert</code>/<code>AssertionError</code> is probably wrong here, I'd say. It's useful for <a href="http://docs.python.org/reference/simple_stmts.html#the-assert-statement" rel="nofollow">"debugging assertions"</a>, which is to say, to make sure your code is sane. It's not useful as a way to raise an err...
6
2010-01-11T12:14:10Z
[ "python" ]
problem when zooming pictures in wxpython
2,041,418
<p>hello<br> I need to draw over image (to comment over it) in a scrolled panel. I'm having troubles with it since it doesn't behave right when I zoom it in or out. it stops drawing , and then it shows it after a while in a wrong place. right in the upper left corner of the window. and doesn't draw lines correctly . be...
1
2010-01-11T11:17:02Z
2,042,139
<p>You need to scale mouse co-ordinates so that it is in sync with scaling of drawing, so if you are using userScale=2, mouse at x=10 will end up at 20 . so you need to do this</p> <pre><code>sx, sy = x/cur_scale, y/cur_scale </code></pre> <p>You also need to be do drawing in <code>EVT_PAINT</code> event not on onmot...
3
2010-01-11T13:38:47Z
[ "python", "image", "wxpython", "drawing", "zoom" ]
Cancel query execution in pyscopg2
2,041,978
<p>How would one go about cancelling execution of a query statement using pyscopg2 (the python Postgres driver)?</p> <p>As an example, let's say I have the following code:</p> <pre><code>import psycopg2 cnx_string = "something_appropriate" conn = psycopg2.connect(cnx_string) cur = conn.cursor() cur.execute("long_run...
3
2010-01-11T13:09:01Z
2,043,306
<p>psycopg2's async execution support has been <a href="http://lists.initd.org/pipermail/psycopg/2009-October/006642.html" rel="nofollow">removed</a>.</p> <p>If you can use py-postgresql and its <a href="http://python.projects.postgresql.org/docs/0.9/driver.html#transactions" rel="nofollow">transactions</a> (it's py3k...
0
2010-01-11T16:50:15Z
[ "python", "postgresql", "psycopg2", "db-api-2" ]
Cancel query execution in pyscopg2
2,041,978
<p>How would one go about cancelling execution of a query statement using pyscopg2 (the python Postgres driver)?</p> <p>As an example, let's say I have the following code:</p> <pre><code>import psycopg2 cnx_string = "something_appropriate" conn = psycopg2.connect(cnx_string) cur = conn.cursor() cur.execute("long_run...
3
2010-01-11T13:09:01Z
2,275,123
<p>You can cancel a query by calling the <a href="http://www.postgresql.org/docs/8.3/static/functions-admin.html#FUNCTIONS-ADMIN-SIGNAL-TABLE" rel="nofollow"><code>pg_cancel_backend(pid)</code></a> PostgreSQL function in a separate connection.</p> <p>You can know the PID of the backend to cancel from the <a href="http...
4
2010-02-16T18:03:42Z
[ "python", "postgresql", "psycopg2", "db-api-2" ]
Python Sockets - Creating a message format
2,042,133
<p>I have built a Python server to which various clients can connect, and I need to set a predefined series of messages from clients to the server - For example the client passes in a name to the server when it first connects. </p> <p>I was wondering what the best way to approach this is? How should I build a simple p...
3
2010-01-11T13:37:34Z
2,042,151
<p>Depending on the requirements, you might want to consider using <a href="http://json.org/" rel="nofollow">JSON</a>: use "newline" terminated strings with JSON encoding. The transport protocol could be <strong>HTTP</strong>: with this, you could have access to all the "connection related" facilities (e.g. status code...
2
2010-01-11T13:40:51Z
[ "python", "sockets", "protocols" ]
Python Sockets - Creating a message format
2,042,133
<p>I have built a Python server to which various clients can connect, and I need to set a predefined series of messages from clients to the server - For example the client passes in a name to the server when it first connects. </p> <p>I was wondering what the best way to approach this is? How should I build a simple p...
3
2010-01-11T13:37:34Z
2,042,161
<p>First, you need to decide whether you want your protocol to be human readable (much more overhead) or binary. If the first, you probably want to use regular expressions to decode the messages. For this, use the python module <code>re</code>. If the latter, the module <code>struct</code> is your friend.</p> <p>Secon...
1
2010-01-11T13:42:07Z
[ "python", "sockets", "protocols" ]
Python Sockets - Creating a message format
2,042,133
<p>I have built a Python server to which various clients can connect, and I need to set a predefined series of messages from clients to the server - For example the client passes in a name to the server when it first connects. </p> <p>I was wondering what the best way to approach this is? How should I build a simple p...
3
2010-01-11T13:37:34Z
2,042,187
<p>Read some protocols, and try to find one that looks like what you need. Does it need to be message-oriented or stream-oriented? Does it need request order to be preserved, does it need requests to be paired with responses? Do you need message identifiers? Retries, back-off? Is it an RPC protocol, a message queue pro...
1
2010-01-11T13:44:42Z
[ "python", "sockets", "protocols" ]
Python Sockets - Creating a message format
2,042,133
<p>I have built a Python server to which various clients can connect, and I need to set a predefined series of messages from clients to the server - For example the client passes in a name to the server when it first connects. </p> <p>I was wondering what the best way to approach this is? How should I build a simple p...
3
2010-01-11T13:37:34Z
2,042,519
<p>See <a href="http://www.faqs.org/docs/artu/ch05s02.html" rel="nofollow">http://www.faqs.org/docs/artu/ch05s02.html</a> and <a href="http://www.faqs.org/docs/artu/ch05s03.html" rel="nofollow">http://www.faqs.org/docs/artu/ch05s03.html</a> for a good overview and discussion on data file formats and protocols.</p>
0
2010-01-11T14:50:11Z
[ "python", "sockets", "protocols" ]
Django: information leakage problem when using @login_required and setting LOGIN_URL
2,042,201
<p>I found a form of information leakage when using the @login_required decorator and setting the LOGIN_URL variable.</p> <p>I have a site that requires a mandatory login for all content. The problem is that you get redirected to the login page with the next variable set when it's a existing page.</p> <p>So when not ...
10
2010-01-11T13:47:27Z
2,042,329
<p>You can include this line as the last pattern in your <code>urls.py</code> file. It will re-route urls that do not match any other pattern to the login page.</p> <pre><code>urlpatterns = patterns('', ... (r'^(?P&lt;path&gt;.+)$', 'django.views.generic.simple.redirect_to', { 'url': '/login/?next=/%...
5
2010-01-11T14:14:42Z
[ "python", "django", "authentication", "django-urls" ]
How to copy a file from a network share to local disk with variables?
2,042,342
<p>If I use the following line:</p> <pre><code>shutil.copyfile(r"\\mynetworkshare\myfile.txt","C:\TEMP\myfile.txt") </code></pre> <p>everything works fine. However, what I can't seem to figure out is how to use a variable with the network share path, because I need the 'r' (relative?) flag. The end result I would im...
4
2010-01-11T14:16:58Z
2,042,364
<p>The <code>r</code> is for "raw string", not for relative. When you don't prefix your string with <code>r</code>, Python will treat the backslash "<code>\</code>" as an escape character.</p> <p>So when your string contains backslashes, you either have to put an <code>r</code> before it, or put two backslashes for ea...
3
2010-01-11T14:20:06Z
[ "python", "network-programming", "share" ]
How to copy a file from a network share to local disk with variables?
2,042,342
<p>If I use the following line:</p> <pre><code>shutil.copyfile(r"\\mynetworkshare\myfile.txt","C:\TEMP\myfile.txt") </code></pre> <p>everything works fine. However, what I can't seem to figure out is how to use a variable with the network share path, because I need the 'r' (relative?) flag. The end result I would im...
4
2010-01-11T14:16:58Z
2,042,370
<p>This looks like an escaping issue - as balpha says, the <code>r</code> makes the <code>\</code> character a literal, rather than a control sequence. Have you tried:</p> <pre><code>source_path = r"\\mynetworkshare" dest_path = r"C:\TEMP" filename = r"\my_file.txt" shutil.copyfile(source_path + filename, dest_path +...
2
2010-01-11T14:21:46Z
[ "python", "network-programming", "share" ]
How to copy a file from a network share to local disk with variables?
2,042,342
<p>If I use the following line:</p> <pre><code>shutil.copyfile(r"\\mynetworkshare\myfile.txt","C:\TEMP\myfile.txt") </code></pre> <p>everything works fine. However, what I can't seem to figure out is how to use a variable with the network share path, because I need the 'r' (relative?) flag. The end result I would im...
4
2010-01-11T14:16:58Z
2,042,376
<p>The <code>r</code> used in your first code example is making the string a "raw" string. In this example, that means the string will see the backslashes and not try to use them to escape <code>\\</code> to just <code>\</code>.</p> <p>To get your second code sample working, you'd use the <code>r</code> on the strings...
9
2010-01-11T14:22:14Z
[ "python", "network-programming", "share" ]
How to copy a file from a network share to local disk with variables?
2,042,342
<p>If I use the following line:</p> <pre><code>shutil.copyfile(r"\\mynetworkshare\myfile.txt","C:\TEMP\myfile.txt") </code></pre> <p>everything works fine. However, what I can't seem to figure out is how to use a variable with the network share path, because I need the 'r' (relative?) flag. The end result I would im...
4
2010-01-11T14:16:58Z
2,042,408
<p>From your example paths, it's clear that we are discussing the <code>Windows OS</code>. Python implementation on this OS use a common (<code>C</code>) runtime library that accepts forward slashes as equivalent to back-slashes. This way you can avoid escape char issues.</p> <pre><code>source_path = "//mynetworkshare...
0
2010-01-11T14:31:09Z
[ "python", "network-programming", "share" ]
A more pythonic way of iterating a list while excluding an element each iteration
2,042,363
<p>I have the following code:</p> <pre><code>items = ["one", "two", "three"] for i in range(0, len(items)): for index, element in enumerate(items): if index != i: # do something with element </code></pre> <p>Basically I want to exclude every element once and iterate the rest. So for the list ...
10
2010-01-11T14:20:04Z
2,042,394
<p>Although upvoted like crazy, my first solution wasn't what the OP wanted, which is N lists, each missing exactly one of the N original elements:</p> <pre><code>&gt;&gt;&gt; from itertools import combinations &gt;&gt;&gt; L = ["one", "two", "three", "four"] &gt;&gt;&gt; for R in combinations(L, len(L) - 1): ... ...
17
2010-01-11T14:28:04Z
[ "python" ]
A more pythonic way of iterating a list while excluding an element each iteration
2,042,363
<p>I have the following code:</p> <pre><code>items = ["one", "two", "three"] for i in range(0, len(items)): for index, element in enumerate(items): if index != i: # do something with element </code></pre> <p>Basically I want to exclude every element once and iterate the rest. So for the list ...
10
2010-01-11T14:20:04Z
2,042,395
<pre><code>[items[:i]+items[i+1:] for i in range(len(items))] </code></pre> <p>in py2.x use <code>xrange</code>. obviously, slicing all the time on a big sequence is not very efficient, but it's fine for short ones. Better option would be using <a href="http://docs.python.org/library/itertools.html#itertools.combinati...
5
2010-01-11T14:28:09Z
[ "python" ]
A more pythonic way of iterating a list while excluding an element each iteration
2,042,363
<p>I have the following code:</p> <pre><code>items = ["one", "two", "three"] for i in range(0, len(items)): for index, element in enumerate(items): if index != i: # do something with element </code></pre> <p>Basically I want to exclude every element once and iterate the rest. So for the list ...
10
2010-01-11T14:20:04Z
2,042,409
<p>As roger predicted, an answer which doesn't really improve the existing code :)</p> <pre><code>a = ["one", "two", "three"] for i in range(0, len(a)): print [val[1] for val in enumerate(a) if val[0] != i] </code></pre>
1
2010-01-11T14:31:10Z
[ "python" ]
compile python .py file without executing
2,042,426
<p>Is there a way to compile a Python .py file from the command-line <strong>without</strong> executing it?</p> <p>I am working with an application that stores its python extensions in a non-standard path with limited permissions and I'd like to compile the files during installation. I don't need the overhead of Dis...
24
2010-01-11T14:34:44Z
2,042,442
<blockquote> <p>The <a href="http://docs.python.org/library/py_compile.html">py_compile</a> module provides a function to generate a byte-code file from a source file, and another function used when the module source file is invoked as a script.</p> </blockquote> <pre><code>python -m py_compile fileA.py fileB.py fil...
41
2010-01-11T14:39:28Z
[ "python" ]
compile python .py file without executing
2,042,426
<p>Is there a way to compile a Python .py file from the command-line <strong>without</strong> executing it?</p> <p>I am working with an application that stores its python extensions in a non-standard path with limited permissions and I'd like to compile the files during installation. I don't need the overhead of Dis...
24
2010-01-11T14:34:44Z
2,042,452
<p><code>$ python -c "import py_compile; py_compile.compile('yourfile.py')"</code></p> <p>or</p> <p><code>$ python -c "import py_compile; py_compile.compileall('dir')"</code></p>
3
2010-01-11T14:41:03Z
[ "python" ]
compile python .py file without executing
2,042,426
<p>Is there a way to compile a Python .py file from the command-line <strong>without</strong> executing it?</p> <p>I am working with an application that stores its python extensions in a non-standard path with limited permissions and I'd like to compile the files during installation. I don't need the overhead of Dis...
24
2010-01-11T14:34:44Z
2,042,458
<p>Yes, there is module compileall:</p> <p><a href="http://docs.python.org/library/compileall.html#module-compileall">http://docs.python.org/library/compileall.html#module-compileall</a></p> <p>Here's an example that compiles all the <code>.py</code> files in a directory (but not sub-directories)</p> <pre><code>pyth...
8
2010-01-11T14:41:38Z
[ "python" ]
compile python .py file without executing
2,042,426
<p>Is there a way to compile a Python .py file from the command-line <strong>without</strong> executing it?</p> <p>I am working with an application that stores its python extensions in a non-standard path with limited permissions and I'd like to compile the files during installation. I don't need the overhead of Dis...
24
2010-01-11T14:34:44Z
2,042,496
<p>In fact if you're on Linux you may already have a <code>/usr/bin/py_compilefiles</code> command in your PATH. It wraps the the py_compile module mentioned by other people. If you're not on Linux, here's the <a href="http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/trusty/python-central/trusty/view/head:/py_compil...
3
2010-01-11T14:46:24Z
[ "python" ]
Problem with embeding python code in html?
2,042,564
<p>NOT SURE ABOUT THIS: I think we cannot embed python code in html like we do with PHP, JSP</p> <p>I have a piece of code I am trying to tamper with. <code> PAGE = urllib.unquote_plus("%3C%21doctype+html+public+%22-%2F%2FW3C%2FDTD+HTML+4.01%2F%2FEN%22+%22http ........ </code></p> <p>When I view source in the html fi...
2
2010-01-11T14:58:19Z
2,042,611
<p>You can use a template engine like <a href="http://jinja.pocoo.org/2/" rel="nofollow">Jinja2</a>, Django's Template Engine or <a href="http://www.makotemplates.org/" rel="nofollow">Mako Templates</a>.</p>
4
2010-01-11T15:06:37Z
[ "python", "html" ]
DateTimeProperty has error being set to a datetime in Google App Engine
2,042,913
<p>I'm having a weird error with some Google App Engine code I'm writing.</p> <p>My program contains some code like this:</p> <pre><code>import datetime ... class Action(db.Model): visibleDate = db.DateTimeProperty() ... getActionQuery = Action.gql("WHERE user = :user AND __key__ = :key", user = user, key = s...
2
2010-01-11T15:50:16Z
2,044,633
<p>I think there's something you missed in your traceback.</p> <p>I'm seeing:<code>datetime.datetime.strptime(self.request.get("visibleDate"), "%Y/%m/%d"),</code></p> <p>Notice the comma at the end of the line.</p> <p>That comma makes that line return a tuple with your date inside it. I'm assuming you accidentally a...
5
2010-01-11T20:18:23Z
[ "python", "google-app-engine", "datetime", "gae-datastore" ]
Find sequences of digits in long integers efficiently
2,042,916
<p>Is it possible to find a defined sequence in an integer without converting it to a string? That is, is it possible to do some form of pattern matching directly on integers. I have not thought of one but I keeping thinking there should be a mathematical way of doing this. That's not to say it is more efficient.</p> ...
4
2010-01-11T15:50:48Z
2,042,983
<p>Maybe you want to take a look here: <a href="http://en.wikipedia.org/wiki/Cyclic_number" rel="nofollow">Cyclic Numbers</a>; they also have an algorithm to build a cyclic number.</p> <p>This can be useful too: <a href="http://en.wikipedia.org/wiki/Floyd%27s_cycle-finding_algorithm#Tortoise_and_hare" rel="nofollow">C...
0
2010-01-11T16:02:15Z
[ "python", "performance", "integer", "pattern-matching", "sequence" ]
Find sequences of digits in long integers efficiently
2,042,916
<p>Is it possible to find a defined sequence in an integer without converting it to a string? That is, is it possible to do some form of pattern matching directly on integers. I have not thought of one but I keeping thinking there should be a mathematical way of doing this. That's not to say it is more efficient.</p> ...
4
2010-01-11T15:50:48Z
2,043,323
<p>A list of digits is pretty simple.</p> <pre><code># given n, a long integer digits = [] while n != 0: digits.append( n%10 ) n //= 10 digits.reverse() </code></pre> <p>You can then do your pattern matching on this list of digits. Is that what you're looking for?</p>
1
2010-01-11T16:52:38Z
[ "python", "performance", "integer", "pattern-matching", "sequence" ]
Find sequences of digits in long integers efficiently
2,042,916
<p>Is it possible to find a defined sequence in an integer without converting it to a string? That is, is it possible to do some form of pattern matching directly on integers. I have not thought of one but I keeping thinking there should be a mathematical way of doing this. That's not to say it is more efficient.</p> ...
4
2010-01-11T15:50:48Z
2,044,014
<p>You can use this class to have your generator of digits :-)</p> <pre><code>import math class DecimalIndexing: def __init__(self, n): self.n = n def __len__(self): return int(math.floor(math.log10(self.n)+1)) def __getitem__(self, i): if isinstance(i, slice): return [...
3
2010-01-11T18:39:38Z
[ "python", "performance", "integer", "pattern-matching", "sequence" ]
Find sequences of digits in long integers efficiently
2,042,916
<p>Is it possible to find a defined sequence in an integer without converting it to a string? That is, is it possible to do some form of pattern matching directly on integers. I have not thought of one but I keeping thinking there should be a mathematical way of doing this. That's not to say it is more efficient.</p> ...
4
2010-01-11T15:50:48Z
2,044,460
<p>You can make an iterator with the digits ordered from left to right this way</p> <pre><code>&gt;&gt;&gt; import math &gt;&gt;&gt; number = int(123456789012345678901) &gt;&gt;&gt; #Get the maximum power of 10 using a logarithm &gt;&gt;&gt; max_digit = int(math.log10(number)) &gt;&gt;&gt; range_pow = xrange(max_digit...
0
2010-01-11T19:47:19Z
[ "python", "performance", "integer", "pattern-matching", "sequence" ]
Find sequences of digits in long integers efficiently
2,042,916
<p>Is it possible to find a defined sequence in an integer without converting it to a string? That is, is it possible to do some form of pattern matching directly on integers. I have not thought of one but I keeping thinking there should be a mathematical way of doing this. That's not to say it is more efficient.</p> ...
4
2010-01-11T15:50:48Z
2,046,653
<p>@Fortran gives a great solution, it is very versatile.</p> <p>I ask a modified version on mathoverflow.net, They didn't seem to like it but I got a great answer. This does answer a question slightly different from what I ask here but it is very useful to me.</p> <p>so to find the test if the digits 4444 are in 353...
0
2010-01-12T04:05:54Z
[ "python", "performance", "integer", "pattern-matching", "sequence" ]
Traversing an unusual tree in Python
2,042,918
<p>I have an unusual tree array like this: </p> <pre><code>[[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [3, 6], [0, 7], [7, 6], [8, 9], [9, 6]] </code></pre> <p>Each element of the array is a pair, which means second one is a follower of the first, e.g.:</p> <pre><code>[0, 1] - 0 is followed by 1 [1, 2...
3
2010-01-11T15:51:04Z
2,043,025
<p>From what I understand of your question, it looks like you have a set of parent-child relationships as a list of pairs that describes a <a href="http://en.wikipedia.org/wiki/Tree_%28data_structure%29" rel="nofollow">tree</a>. You seem to be running into trouble by thinking that it has a structure like a linked list....
2
2010-01-11T16:09:54Z
[ "python", "tree", "traversal" ]
Traversing an unusual tree in Python
2,042,918
<p>I have an unusual tree array like this: </p> <pre><code>[[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [3, 6], [0, 7], [7, 6], [8, 9], [9, 6]] </code></pre> <p>Each element of the array is a pair, which means second one is a follower of the first, e.g.:</p> <pre><code>[0, 1] - 0 is followed by 1 [1, 2...
3
2010-01-11T15:51:04Z
2,043,045
<p>Looking at the problem, it seems the best approach might be to build the arrays backwards over several iterations. My idea is this, but note we have to assume that this is a tree and so the leaves can only be used once:</p> <ol> <li>Let arrays = list of pairs</li> <li>Until every array in arrays are leaf: <ol> <li...
0
2010-01-11T16:12:40Z
[ "python", "tree", "traversal" ]
Traversing an unusual tree in Python
2,042,918
<p>I have an unusual tree array like this: </p> <pre><code>[[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [3, 6], [0, 7], [7, 6], [8, 9], [9, 6]] </code></pre> <p>Each element of the array is a pair, which means second one is a follower of the first, e.g.:</p> <pre><code>[0, 1] - 0 is followed by 1 [1, 2...
3
2010-01-11T15:51:04Z
2,043,059
<p>You could use the find_all_paths function from the following page: <a href="http://www.python.org/doc/essays/graphs/" rel="nofollow">http://www.python.org/doc/essays/graphs/</a></p> <p>In order to use this you need to make two minor tweeks to your graph. First, loop through your list of edges and create a new repr...
0
2010-01-11T16:15:31Z
[ "python", "tree", "traversal" ]
Traversing an unusual tree in Python
2,042,918
<p>I have an unusual tree array like this: </p> <pre><code>[[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [3, 6], [0, 7], [7, 6], [8, 9], [9, 6]] </code></pre> <p>Each element of the array is a pair, which means second one is a follower of the first, e.g.:</p> <pre><code>[0, 1] - 0 is followed by 1 [1, 2...
3
2010-01-11T15:51:04Z
2,043,077
<p>You could do it using a recursive generator function. I assume that the root node in the tree always comes before all its children in the original list.</p> <pre><code>tree = [[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [3, 6], [0, 7], [7, 6], [8, 9], [9, 6]] paths = {} for t in tree: if t[...
4
2010-01-11T16:17:27Z
[ "python", "tree", "traversal" ]
Traversing an unusual tree in Python
2,042,918
<p>I have an unusual tree array like this: </p> <pre><code>[[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [3, 6], [0, 7], [7, 6], [8, 9], [9, 6]] </code></pre> <p>Each element of the array is a pair, which means second one is a follower of the first, e.g.:</p> <pre><code>[0, 1] - 0 is followed by 1 [1, 2...
3
2010-01-11T15:51:04Z
2,043,081
<p>Produce all longest pathes from all possible starting nodes:</p> <pre><code>tree = [[0, 1], [1, 2], [2, 3], ...] dtree = {} for (k, v) in tree: dtree.setdefault(k, []).append(v) parts = [[root] for root in range(10)] while parts: path = parts.pop(0) if path[-1] in dtree: for n in dtree[path[-1]]: ...
0
2010-01-11T16:18:13Z
[ "python", "tree", "traversal" ]
Traversing an unusual tree in Python
2,042,918
<p>I have an unusual tree array like this: </p> <pre><code>[[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [3, 6], [0, 7], [7, 6], [8, 9], [9, 6]] </code></pre> <p>Each element of the array is a pair, which means second one is a follower of the first, e.g.:</p> <pre><code>[0, 1] - 0 is followed by 1 [1, 2...
3
2010-01-11T15:51:04Z
2,043,109
<p>Here you go. Not the nicest code on earth but it works:</p> <pre><code>inputValues = [[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [3, 6], [0, 7], [7, 6], [8, 9], [9, 6]] tree = {} numberOfChildren = {} for (f, t) in inputValues: if not tree.has_key(f): tree[f] = [] tree[f].append(t) if not nu...
1
2010-01-11T16:21:32Z
[ "python", "tree", "traversal" ]
Traversing an unusual tree in Python
2,042,918
<p>I have an unusual tree array like this: </p> <pre><code>[[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [3, 6], [0, 7], [7, 6], [8, 9], [9, 6]] </code></pre> <p>Each element of the array is a pair, which means second one is a follower of the first, e.g.:</p> <pre><code>[0, 1] - 0 is followed by 1 [1, 2...
3
2010-01-11T15:51:04Z
2,043,160
<p>The easiest way I can think of, would be to construct a dictionary that contains all possible children for a given parent, like so:</p> <pre><code>d = {} for parent, child in tree: try: d[parent].append(child) except KeyError: d[parent] = [child] </code></pre> <p>with tree = [[0, 1], [1, 2...
2
2010-01-11T16:29:09Z
[ "python", "tree", "traversal" ]
Traversing an unusual tree in Python
2,042,918
<p>I have an unusual tree array like this: </p> <pre><code>[[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [3, 6], [0, 7], [7, 6], [8, 9], [9, 6]] </code></pre> <p>Each element of the array is a pair, which means second one is a follower of the first, e.g.:</p> <pre><code>[0, 1] - 0 is followed by 1 [1, 2...
3
2010-01-11T15:51:04Z
2,043,524
<p>The following works - generate the trees starting from root. The roots are considered the nodes that do not have a parent.</p> <pre><code>import operator def genpaths(data): # Initialize dictionary ddata = {} for item in data: ddata.setdefault(item[0], []).append(item[1]) def genpath(root): ...
0
2010-01-11T17:22:01Z
[ "python", "tree", "traversal" ]
Scripting HTTP more effeciently
2,043,058
<p>Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html p...
8
2010-01-11T16:15:08Z
2,043,069
<p>What about using PHP+Curl, or just bash?</p>
0
2010-01-11T16:16:26Z
[ "python", "ruby", "perl", "http", "scripting" ]
Scripting HTTP more effeciently
2,043,058
<p>Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html p...
8
2010-01-11T16:15:08Z
2,043,073
<p>Python <a href="http://docs.python.org/library/urllib.html" rel="nofollow">urllib</a> may be what you're looking for.</p> <p>Alternatively powershell exposes the full .NET http library in a scripting environment.</p>
2
2010-01-11T16:16:43Z
[ "python", "ruby", "perl", "http", "scripting" ]
Scripting HTTP more effeciently
2,043,058
<p>Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html p...
8
2010-01-11T16:15:08Z
2,043,086
<p>Mechanize for Python seems easy to use: <a href="http://wwwsearch.sourceforge.net/mechanize/" rel="nofollow">http://wwwsearch.sourceforge.net/mechanize/</a></p>
6
2010-01-11T16:18:24Z
[ "python", "ruby", "perl", "http", "scripting" ]
Scripting HTTP more effeciently
2,043,058
<p>Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html p...
8
2010-01-11T16:15:08Z
2,043,088
<p>Have a look at <a href="http://seleniumhq.org/" rel="nofollow">Selenium</a>. It generates code for C#, Java, Perl, PHP, Python, and Ruby if you need to customize the script.</p>
6
2010-01-11T16:18:31Z
[ "python", "ruby", "perl", "http", "scripting" ]
Scripting HTTP more effeciently
2,043,058
<p>Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html p...
8
2010-01-11T16:15:08Z
2,043,151
<p>Watir sounds close to what you want although it (like Selenium linked to in another answer) actually opens up a browser to do stuff. You can see some examples <a href="http://watir.com/examples/" rel="nofollow">here</a>. Another browser based record + playback approach system is <a href="http://sahi.co.in" rel="nofo...
6
2010-01-11T16:28:17Z
[ "python", "ruby", "perl", "http", "scripting" ]
Scripting HTTP more effeciently
2,043,058
<p>Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html p...
8
2010-01-11T16:15:08Z
2,043,186
<p>Some <strong>ruby</strong> libraries:</p> <ul> <li><a href="http://railstips.org/blog/archives/2008/07/29/its-an-httparty-and-everyone-is-invited/" rel="nofollow">httparty:</a> really interesting, the philosophy is interesting.</li> <li><a href="http://mechanize.rubyforge.org/" rel="nofollow">mechanize:</a> classic...
0
2010-01-11T16:32:48Z
[ "python", "ruby", "perl", "http", "scripting" ]
Scripting HTTP more effeciently
2,043,058
<p>Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html p...
8
2010-01-11T16:15:08Z
2,043,217
<p>My turn : wget or perl with <a href="http://search.cpan.org/~gaas/libwww-perl-5.834/lib/LWP.pm" rel="nofollow">lwp</a>. You'll find example on the linked page.</p>
6
2010-01-11T16:37:13Z
[ "python", "ruby", "perl", "http", "scripting" ]
Scripting HTTP more effeciently
2,043,058
<p>Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html p...
8
2010-01-11T16:15:08Z
2,043,220
<p>Depending on exactly what you're doing the easiest solution looks to be bash + curl.</p> <p>The man page for the latter is available here:</p> <p><a href="http://curl.haxx.se/docs/manpage.html" rel="nofollow">http://curl.haxx.se/docs/manpage.html</a></p> <p>You can do posts as well as gets, HTTPS, show headers, w...
3
2010-01-11T16:37:18Z
[ "python", "ruby", "perl", "http", "scripting" ]
Scripting HTTP more effeciently
2,043,058
<p>Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html p...
8
2010-01-11T16:15:08Z
2,043,295
<p>I'm testing ReST APIs at the moment and found the <a href="http://code.google.com/p/rest-client/" rel="nofollow">ReST Client</a> very nice. It's a GUI program, but nonetheless you can save and restore queries as XML files (or let them be generated), embed, write test scripts, and so on. And it's Java based (which is...
4
2010-01-11T16:49:02Z
[ "python", "ruby", "perl", "http", "scripting" ]
Scripting HTTP more effeciently
2,043,058
<p>Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html p...
8
2010-01-11T16:15:08Z
2,043,405
<p><a href="http://twill.idyll.org/" rel="nofollow">Twill</a> is pretty good and made for testing. It can be used as script, in an interactive session or within a Python program.</p>
2
2010-01-11T17:03:52Z
[ "python", "ruby", "perl", "http", "scripting" ]
Scripting HTTP more effeciently
2,043,058
<p>Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html p...
8
2010-01-11T16:15:08Z
2,043,541
<p>If you have simple needs (fetch a page and then parse it), it is hard to beat <a href="http://search.cpan.org/perldoc?LWP%3A%3ASimple" rel="nofollow">LWP::Simple</a> and <a href="http://search.cpan.org/perldoc?HTML%3A%3ATreeBuilder" rel="nofollow">HTML::TreeBuilder</a>.</p> <pre><code>use strict; use warnings; use...
6
2010-01-11T17:24:14Z
[ "python", "ruby", "perl", "http", "scripting" ]
Scripting HTTP more effeciently
2,043,058
<p>Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html p...
8
2010-01-11T16:15:08Z
2,045,037
<p>Perl and <a href="http://search.cpan.org/dist/WWW-Mechanize" rel="nofollow">WWW::Mechanize</a> can make web scraping etc simple and easy, including easy handling of forms (let's say you want to go to a login page, fill in a username and password and submit the form, handling cookies / hidden session identifiers just...
2
2010-01-11T21:21:45Z
[ "python", "ruby", "perl", "http", "scripting" ]
Could not get cookie from another (parent) domain in Django
2,043,138
<p>I need to remove a cookie that was previously set for parent domain while browsing host at subdomain of the parent.</p> <p>I.e., a cookie "xyz" was set for example.com, and I am trying to remove it on subdomain.example.com, using Django backend.</p> <p>The request.COOKIES given to the view does not contain any coo...
2
2010-01-11T16:26:34Z
2,043,172
<p>The cookie was probably set with 'domain' parameter. Set the cookie to be accessible from all the subdomains of the domain the cookie is being set in.</p> <p>I'm not the python guy, but my knowledge of http protocol shows that this might be the problem.</p>
1
2010-01-11T16:30:43Z
[ "javascript", "python", "django", "cookies" ]
Could not get cookie from another (parent) domain in Django
2,043,138
<p>I need to remove a cookie that was previously set for parent domain while browsing host at subdomain of the parent.</p> <p>I.e., a cookie "xyz" was set for example.com, and I am trying to remove it on subdomain.example.com, using Django backend.</p> <p>The request.COOKIES given to the view does not contain any coo...
2
2010-01-11T16:26:34Z
2,043,336
<p>You can attempt to call <code>delete_cookie</code> even for a cookie you haven't been able to read. Django will output the relevant <code>Set-Cookie</code> headers to delete the cookie regardless. Naturally the <code>domain</code> and <code>path</code> you pass to <code>delete_cookie</code> must match the cookie you...
0
2010-01-11T16:54:16Z
[ "javascript", "python", "django", "cookies" ]
How can I support * in user-defined search strings in python?
2,043,449
<p>This question is related to this stack overflow question:</p> <p><a href="http://stackoverflow.com/questions/238600/how-can-i-support-wildcards-in-user-defined-search-strings-in-python">http://stackoverflow.com/questions/238600/how-can-i-support-wildcards-in-user-defined-search-strings-in-python</a></p> <p>But I n...
0
2010-01-11T17:10:58Z
2,043,531
<p>You could compile a regexp from your search string using split, re.escape, and '^$'.</p> <pre><code>import re regex = re.compile('^' + '.*'.join(re.escape(foo) for foo in pattern.split('*')) + '$') </code></pre>
3
2010-01-11T17:23:35Z
[ "python", "regex" ]
How can I support * in user-defined search strings in python?
2,043,449
<p>This question is related to this stack overflow question:</p> <p><a href="http://stackoverflow.com/questions/238600/how-can-i-support-wildcards-in-user-defined-search-strings-in-python">http://stackoverflow.com/questions/238600/how-can-i-support-wildcards-in-user-defined-search-strings-in-python</a></p> <p>But I n...
0
2010-01-11T17:10:58Z
2,043,534
<p>If its just one asterisk and you require the search string to be representing the whole matched string, this works:</p> <pre><code>searchstring = "site.*.com/sub/" to_match = "site.hostname.com/sub/" prefix, suffix = searchstring.split("*", 1) if to_match.startswith(prefix) and to_match.endswith(suffix): prin...
1
2010-01-11T17:23:43Z
[ "python", "regex" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
2,043,472
<p>just use return and type it on the next line:</p> <pre><code>user@host:~$ python -c "import sys &gt; for r in range(10): print 'rob'" rob rob ... </code></pre>
10
2010-01-11T17:14:19Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
2,043,499
<p>you could do</p> <pre><code>echo -e "import sys\nfor r in range(10): print 'rob'" | python </code></pre> <p>or w/out pipes:</p> <pre><code>python -c "exec(\"import sys\\nfor r in range(10): print 'rob'\")" </code></pre> <p>or</p> <pre><code>(echo "import sys" ; echo "for r in range(10): print 'rob'") | python <...
72
2010-01-11T17:18:43Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
2,043,503
<p>The problem is not with the <code>import</code> statement. The problem is that the control flow statements don't work inlined in a python command. Replace that <code>import</code> statement with any other statement and you'll see the same problem.</p> <p>Think about it: python can't possibly inline everything. It...
7
2010-01-11T17:19:27Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
2,043,633
<p>The issue is not actually with the import statement, it's with anything being before the for loop. Or more specifically, anything appearing before an inlined block.</p> <p>For example, these all work:</p> <pre><code>python -c "import sys; print 'rob'" python -c "import sys; sys.stdout.write('rob\n')" </code></pre>...
23
2010-01-11T17:39:30Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
2,046,112
<p>If your system is Posix.2 compliant it should supply the <code>printf</code> utility:</p> <pre><code>$ printf "print 'zap'\nfor r in range(3): print 'rob'" | python zap rob rob rob </code></pre>
6
2010-01-12T01:33:45Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
2,356,490
<p>this style can be used in makefiles too (and in fact it is used quite often).</p> <pre><code>python - &lt;&lt;EOF import sys for r in range(3): print 'rob' EOF </code></pre> <p>or</p> <pre><code>python - &lt;&lt;-EOF import sys for r in range(3): print 'rob' EOF </code></pre> <p>in latter case leading <e...
48
2010-03-01T14:38:57Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
4,260,184
<p><sup>(answered Nov 23 '10 at 19:48)</sup> I'm not really a big Pythoner - but I found this syntax once, forgot where from, so I thought I'd document it: </p> <p>if you use <code>sys.stdout.write</code> instead of <code>print</code> (<em>the difference being, <code>sys.stdout.write</code> takes arguments as a functi...
4
2010-11-23T19:48:43Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
8,462,730
<p><code>single/double quotes</code> and <code>backslash</code> everywhere:</p> <pre><code>$ python -c 'exec("import sys\nfor i in range(10): print \"bob\"")' </code></pre> <p>Much better:</p> <pre><code>$ python -c ' &gt; import sys &gt; for i in range(10): &gt; print "bob" &gt; ' </code></pre>
1
2011-12-11T08:17:12Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
8,820,243
<p><code>$ python2.6 -c "import sys; [sys.stdout.write('rob\n') for r in range(10)]"</code></p> <p>Works fine. Use "[ ]" to inline your for loop.</p>
6
2012-01-11T13:40:49Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
9,317,060
<p>This script provides a Perl-like command line interface:</p> <p><a href="http://code.activestate.com/recipes/577075-pyliner-script-to-run-arbitrary-python-code-on-the/?in=user-4119417" rel="nofollow">Pyliner - Script to run arbitrary Python code on the command line (Python recipe)</a></p>
2
2012-02-16T18:37:15Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
14,259,742
<p>If you don't want to touch stdin and simulate as if you had passed "python cmdfile.py", you can do the following from a bash shell:</p> <pre><code>$ python &lt;(printf "word=raw_input('Enter word: ')\nimport sys\nfor i in range(5):\n print(word)") </code></pre> <p>As you can see, it allows you to use stdin for...
0
2013-01-10T14:06:22Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
14,873,472
<p>When I needed to do this, I use</p> <pre><code>python -c "$(echo -e "import sys\nsys.stdout.write('Hello World!\\\n')")" </code></pre> <p>Note the triple backslash for the newline in the sys.stdout.write statement.</p>
0
2013-02-14T11:11:33Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
24,074,109
<p>there is one more option, sys.stdout.write returns None, which keep the list empty </p> <p><pre> cat somefile.log|python -c "import sys;[line for line in sys.stdin if sys.stdout.write(line*2)]" </pre></p>
0
2014-06-06T04:22:39Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
25,245,320
<p>I've written a simple web page for this. You can paste your multiple-line code there and it is converted into a working single-line statement.</p> <p><a href="http://jagt.github.io/python-single-line-convert/" rel="nofollow">Python Single Line Converter</a></p>
0
2014-08-11T14:09:50Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
29,565,580
<p><sup><br> - To make this answer work with Python <em>3.x</em> as well, <code>print</code> is called as a <em>function</em>: in 3.x, <em>only</em> <code>print('foo')</code> works, whereas 2.x also accepts <code>print 'foo'</code>.<br> - For a cross-platform perspective that includes <em>Windows</em>, see <a href="htt...
5
2015-04-10T15:44:56Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
35,651,859
<p>This variant is most portable for putting multi-line scripts on command-line on Windows and *nix, py2/3, without pipes:</p> <pre><code>python -c "exec(\"import sys \nfor r in range(10): print('rob') \")" </code></pre> <p>(None of the other examples seen here so far did so) </p> <p>Neat on Windows is:</p> <pre><c...
0
2016-02-26T12:32:40Z
[ "python", "shell", "command-line", "heredoc" ]
Executing Python multi-line statements in the one-line command-line
2,043,453
<p>I'm using Python with <code>-c</code> to execute a one-liner loop, i.e.:</p> <pre><code>$ python -c "for r in range(10): print 'rob'" </code></pre> <p>This works fine. However, if I import a module before the for loop, I get a syntax error:</p> <pre><code>$ python -c "import sys; for r in range(10): print 'rob'" ...
84
2010-01-11T17:11:27Z
38,235,661
<blockquote> <h1>Any idea how this can be fixed?</h1> </blockquote> <p>Your problem is created by the fact that Python statements, separated by <code>;</code>, are only allowed to be "small statements", which are all one-liners. From the grammar file in the <a href="https://docs.python.org/reference/grammar.html" re...
2
2016-07-06T23:56:34Z
[ "python", "shell", "command-line", "heredoc" ]
There's an example on how to use an API with Python and cURL. Is it feasable to just copy the code if I have IronPython installed and have it work?
2,043,848
<p>Here's the code in question:</p> <pre><code>#!/usr/bin/python import pycurl c = pycurl.Curl() values = [ ("key", "YOUR_API_KEY"), ("image", (c.FORM_FILE, "file.png"))] # OR: ("image", "http://example.com/example.jpg"))] c.setopt(c.URL, "http://imgur.com/api/upload.xml") c.setopt(c.HTTPPOS...
0
2010-01-11T18:16:32Z
2,043,861
<p>The code itself looks straightforward. I don't know if pycurl is available as a .NET module (or whatever it's called) so that it can be used directly from ironpython. What's the problem with trying it once?</p>
0
2010-01-11T18:19:29Z
[ "c#", "python", "curl", "ironpython" ]
There's an example on how to use an API with Python and cURL. Is it feasable to just copy the code if I have IronPython installed and have it work?
2,043,848
<p>Here's the code in question:</p> <pre><code>#!/usr/bin/python import pycurl c = pycurl.Curl() values = [ ("key", "YOUR_API_KEY"), ("image", (c.FORM_FILE, "file.png"))] # OR: ("image", "http://example.com/example.jpg"))] c.setopt(c.URL, "http://imgur.com/api/upload.xml") c.setopt(c.HTTPPOS...
0
2010-01-11T18:16:32Z
2,045,483
<p>In C#, you can use the <a href="http://msdn.microsoft.com/en-us/library/system.net.webrequest%28VS.71%29.aspx" rel="nofollow">WebRequest</a> class to accomplish the same. Take a look at the example toward the bottom of the page "<a href="http://msdn.microsoft.com/en-us/library/debx8sh9.aspx" rel="nofollow">How to: ...
1
2010-01-11T22:41:50Z
[ "c#", "python", "curl", "ironpython" ]
There's an example on how to use an API with Python and cURL. Is it feasable to just copy the code if I have IronPython installed and have it work?
2,043,848
<p>Here's the code in question:</p> <pre><code>#!/usr/bin/python import pycurl c = pycurl.Curl() values = [ ("key", "YOUR_API_KEY"), ("image", (c.FORM_FILE, "file.png"))] # OR: ("image", "http://example.com/example.jpg"))] c.setopt(c.URL, "http://imgur.com/api/upload.xml") c.setopt(c.HTTPPOS...
0
2010-01-11T18:16:32Z
2,045,973
<p>As far as I know, you should be able to do that just fine.</p> <p>One of two things will happen:</p> <ol> <li><p>It will work just fine when you import your code into IronPython</p></li> <li><p>IronPython will throw an error at you saying that it doesn't know where to find cURL. In this case, you will have to add ...
0
2010-01-12T00:47:25Z
[ "c#", "python", "curl", "ironpython" ]
There's an example on how to use an API with Python and cURL. Is it feasable to just copy the code if I have IronPython installed and have it work?
2,043,848
<p>Here's the code in question:</p> <pre><code>#!/usr/bin/python import pycurl c = pycurl.Curl() values = [ ("key", "YOUR_API_KEY"), ("image", (c.FORM_FILE, "file.png"))] # OR: ("image", "http://example.com/example.jpg"))] c.setopt(c.URL, "http://imgur.com/api/upload.xml") c.setopt(c.HTTPPOS...
0
2010-01-11T18:16:32Z
2,048,199
<p>pycurl appears to be a C-Extension for CPython. </p> <p>To use this with IronPython you will need to use IronClad (a 'work in progress' project to allow use of c-extensions in the .Net world).</p>
0
2010-01-12T10:29:43Z
[ "c#", "python", "curl", "ironpython" ]
Tracking system and real time stats analysis in Python
2,043,879
<p>This question is related to an older question: <a href="http://stackoverflow.com/questions/2019096/mysql-tracking-system">http://stackoverflow.com/questions/2019096/mysql-tracking-system</a>. In short: I have to implement a tracking system that will have high loads using Python. For the database part I've settled on...
5
2010-01-11T18:21:56Z
2,044,123
<p>Have you checked out Graphite? It sounds like exactly the kind of thing that you need (looking at your other question) and was designed for application and server monitoring by the Orbitz team. It's extremely robust and easy to use for this sort of thing.</p> <ul> <li>Here's the project site: <a href="http://grap...
3
2010-01-11T18:56:08Z
[ "python", "nginx", "mongodb", "mod-wsgi", "cherrypy" ]
Tracking system and real time stats analysis in Python
2,043,879
<p>This question is related to an older question: <a href="http://stackoverflow.com/questions/2019096/mysql-tracking-system">http://stackoverflow.com/questions/2019096/mysql-tracking-system</a>. In short: I have to implement a tracking system that will have high loads using Python. For the database part I've settled on...
5
2010-01-11T18:21:56Z
2,044,393
<p>Sounds like MongoDB will be a good fit for this - fast updates with advanced operators, and M/R for batch offline processing. I think CherryPy behind Nginx should work well too. If you go the mod_wsgi route just watch out for <a href="http://api.mongodb.org/python/1.3%2B/faq.html#does-pymongo-work-with-mod-wsgi" rel...
1
2010-01-11T19:37:58Z
[ "python", "nginx", "mongodb", "mod-wsgi", "cherrypy" ]
Using win32com and/or active_directory, how can I access an email folder by name?
2,043,980
<p>In python w/ Outlook 2007, using win32com and/or active_directory, how can I get a reference to a sub-folder so that I may move a MailItem to this sub-folder? </p> <p>I have an inbox structure like: <PRE> Inbox | +-- test | `-- todo </PRE></p> <p>I can access the inbox folder like:</p> ...
2
2010-01-11T18:34:38Z
2,044,096
<p>This works for me to move a mail item into a "test" subdirectory (simplified by getting rid of gencache stuff):</p> <pre><code>import win32com.client olFolderInbox = 6 olMailItem = 0 outlook = win32com.client.Dispatch("Outlook.Application") mapi = outlook.GetNamespace('MAPI') inbox = mapi.GetDefaultFolder(olFold...
1
2010-01-11T18:52:01Z
[ "python", "active-directory", "outlook", "win32com" ]
Using win32com and/or active_directory, how can I access an email folder by name?
2,043,980
<p>In python w/ Outlook 2007, using win32com and/or active_directory, how can I get a reference to a sub-folder so that I may move a MailItem to this sub-folder? </p> <p>I have an inbox structure like: <PRE> Inbox | +-- test | `-- todo </PRE></p> <p>I can access the inbox folder like:</p> ...
2
2010-01-11T18:34:38Z
2,044,306
<p>Something that did work for me was iterating over the folder names. ( When I posted this question, I couldn't figure out the folder names ).</p> <pre><code>import win32com.client import active_directory session = win32com.client.gencache.EnsureDispatch("MAPI.session") win32com.client.gencache.EnsureDispatch("Outloo...
1
2010-01-11T19:23:32Z
[ "python", "active-directory", "outlook", "win32com" ]
Using win32com and/or active_directory, how can I access an email folder by name?
2,043,980
<p>In python w/ Outlook 2007, using win32com and/or active_directory, how can I get a reference to a sub-folder so that I may move a MailItem to this sub-folder? </p> <p>I have an inbox structure like: <PRE> Inbox | +-- test | `-- todo </PRE></p> <p>I can access the inbox folder like:</p> ...
2
2010-01-11T18:34:38Z
30,085,820
<p>I realize this is an old question but I've been using the win32com package recently and found the documentation troublesome to say the least...My hope is that someone, someday can be saved the turmoil I experienced trying to wrap my head around MSDN's explanation</p> <p>Here's and example of a python script to trav...
2
2015-05-06T19:26:33Z
[ "python", "active-directory", "outlook", "win32com" ]
NetBeans and Python
2,044,364
<p>When I run some python code in NetBeans, which raises an error, the output in NetBeans just gives an error message and no further information, such as line number. Is there any way to fix that?</p>
0
2010-01-11T19:33:11Z
2,044,412
<p>added solution is debugging if don't have any compiling error </p>
0
2010-01-11T19:40:21Z
[ "python", "netbeans" ]
NetBeans and Python
2,044,364
<p>When I run some python code in NetBeans, which raises an error, the output in NetBeans just gives an error message and no further information, such as line number. Is there any way to fix that?</p>
0
2010-01-11T19:33:11Z
2,044,496
<p>If you can, I would run your script outside of NetBeans either with the built-in editor (IDLE) or just run it from the command line. That should give you a traceback with the error and lineno</p> <p>NetBeans has issues with debugging, as <a href="http://stackoverflow.com/questions/1972049/unable-to-get-netbeans-de...
1
2010-01-11T19:53:08Z
[ "python", "netbeans" ]
Subclassing Decimal in Python
2,044,427
<p>I want to use Decimal class in my Python program for doing financial calculations. Decimals to not work with floats - they need explicit conversion to strings first. So i decided to subclass Decimal to be able to work with floats without explicit conversions.</p> <p>m_Decimal.py:</p> <pre><code># -*- coding: utf-8...
1
2010-01-11T19:42:31Z
2,045,311
<p>Currently, it won't do what you want at all. You can't multiply your m_decimal by anything: it will always return None, due to a missing return statement:</p> <pre><code> def __mul__ ( self, other ) : print (type(other)) return Decimal.__mul__ ( self, other ) </code></pre> <p>Even with the re...
1
2010-01-11T22:07:19Z
[ "python", "decimal", "subclassing" ]
Subclassing Decimal in Python
2,044,427
<p>I want to use Decimal class in my Python program for doing financial calculations. Decimals to not work with floats - they need explicit conversion to strings first. So i decided to subclass Decimal to be able to work with floats without explicit conversions.</p> <p>m_Decimal.py:</p> <pre><code># -*- coding: utf-8...
1
2010-01-11T19:42:31Z
2,045,674
<p>In my opinion you shouldn't use floats at all. Floats are not the right tool for a financial application. Anywhere you are using floats you should be able to use str or Decimal to ensure you are not losing precision.</p> <p>For example.<br> User input, File input - obviously use str and convert to Decimal to do any...
1
2010-01-11T23:22:06Z
[ "python", "decimal", "subclassing" ]