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
Parsing unicode input using python json.loads
2,234,228
<p>What is the best way to load JSON Strings in Python?</p> <p>I want to use json.loads to process unicode like this:</p> <pre><code>import json json.loads(unicode_string_to_load) </code></pre> <p>I also tried supplying 'encoding' parameter with value 'utf-16', but the error did not go away.</p> <p>Full SSCCE with ...
10
2010-02-10T03:32:17Z
15,805,933
<p>With django you can use SimpleJSON and use loads instead of just load.</p> <pre><code>from django.utils import simplejson simplejson.loads(str_to_load, "utf-8") </code></pre>
1
2013-04-04T08:18:29Z
[ "python", "django", "json", "unicode" ]
Parsing unicode input using python json.loads
2,234,228
<p>What is the best way to load JSON Strings in Python?</p> <p>I want to use json.loads to process unicode like this:</p> <pre><code>import json json.loads(unicode_string_to_load) </code></pre> <p>I also tried supplying 'encoding' parameter with value 'utf-16', but the error did not go away.</p> <p>Full SSCCE with ...
10
2010-02-10T03:32:17Z
30,107,752
<p>The simplest way I have found is</p> <pre><code>import simplejson as json </code></pre> <p>that way your code remains the same</p> <pre><code>json.loads(str_to_load) </code></pre> <p>reference: <a href="https://simplejson.readthedocs.org/en/latest/" rel="nofollow">https://simplejson.readthedocs.org/en/latest/</a...
0
2015-05-07T17:17:33Z
[ "python", "django", "json", "unicode" ]
Emulate floating point string conversion behaviour of Linux on Windows
2,234,303
<p>I've encountered an annoying problem in outputting a floating point number. When I format 11.545 with a precision of 2 decimal points on Windows it outputs "11.55", as I would expect. However, when I do the same on Linux the output is "11.54"!</p> <p>I originally encountered the problem in Python, but further inves...
6
2010-02-10T03:55:45Z
2,234,326
<p>Consider comparing floating point numbers with some tolerance/epsilon instead. This is much more robust than trying to match exactly.</p> <p>What I mean is, except saying that two floats are equal when:</p> <pre><code>f1 == f2 </code></pre> <p>Say they're equal when:</p> <pre><code>fabs(f1 - f2) &lt; eps </code>...
0
2010-02-10T04:02:52Z
[ "python", "c", "floating-point", "printf", "precision" ]
Emulate floating point string conversion behaviour of Linux on Windows
2,234,303
<p>I've encountered an annoying problem in outputting a floating point number. When I format 11.545 with a precision of 2 decimal points on Windows it outputs "11.55", as I would expect. However, when I do the same on Linux the output is "11.54"!</p> <p>I originally encountered the problem in Python, but further inves...
6
2010-02-10T03:55:45Z
2,234,372
<p>You could try subtracting (or adding for a negative number) a small delta that will have no effect on the rounding for numbers far enough away from the precision.</p> <p>For example, if you're rounding with <code>%.2f</code>, try this version on Windows:</p> <pre><code>printf("%.2f", 11.545 - 0.001); </code></pre>...
0
2010-02-10T04:16:45Z
[ "python", "c", "floating-point", "printf", "precision" ]
Emulate floating point string conversion behaviour of Linux on Windows
2,234,303
<p>I've encountered an annoying problem in outputting a floating point number. When I format 11.545 with a precision of 2 decimal points on Windows it outputs "11.55", as I would expect. However, when I do the same on Linux the output is "11.54"!</p> <p>I originally encountered the problem in Python, but further inves...
6
2010-02-10T03:55:45Z
2,234,378
<p>The decimal module gives you access to several rounding modes:</p> <pre><code>import decimal fs = ['11.544','11.545','11.546'] def convert(f,nd): # we want 'nd' beyond the dec point nd = f.find('.') + nd c1 = decimal.getcontext().copy() c1.rounding = decimal.ROUND_HALF_UP c1.prec = nd d1 =...
1
2010-02-10T04:17:48Z
[ "python", "c", "floating-point", "printf", "precision" ]
Emulate floating point string conversion behaviour of Linux on Windows
2,234,303
<p>I've encountered an annoying problem in outputting a floating point number. When I format 11.545 with a precision of 2 decimal points on Windows it outputs "11.55", as I would expect. However, when I do the same on Linux the output is "11.54"!</p> <p>I originally encountered the problem in Python, but further inves...
6
2010-02-10T03:55:45Z
2,234,454
<p>First of all it sounds like Windows has it <strike>wrong</strike> right in this case (not that this really matters). The C Standard requires that the value output by <code>%.2f</code> is <em>rounded to the appropriate number of digits</em>. The best known algorithm for this is <a href="http://netlib.sandia.gov/fp/...
2
2010-02-10T04:40:27Z
[ "python", "c", "floating-point", "printf", "precision" ]
Emulate floating point string conversion behaviour of Linux on Windows
2,234,303
<p>I've encountered an annoying problem in outputting a floating point number. When I format 11.545 with a precision of 2 decimal points on Windows it outputs "11.55", as I would expect. However, when I do the same on Linux the output is "11.54"!</p> <p>I originally encountered the problem in Python, but further inves...
6
2010-02-10T03:55:45Z
2,234,513
<p>You may be able to do subtract a tiny amount from the value to force the rounding down</p> <pre><code>print "%.2f"%(11.545-1e-12) </code></pre>
0
2010-02-10T04:59:22Z
[ "python", "c", "floating-point", "printf", "precision" ]
Emulate floating point string conversion behaviour of Linux on Windows
2,234,303
<p>I've encountered an annoying problem in outputting a floating point number. When I format 11.545 with a precision of 2 decimal points on Windows it outputs "11.55", as I would expect. However, when I do the same on Linux the output is "11.54"!</p> <p>I originally encountered the problem in Python, but further inves...
6
2010-02-10T03:55:45Z
2,236,298
<p>I don't think Windows is doing anything especially clever (like trying to reinterpret the float in base 10) here: I'd guess that it's simply computing the first 17 significant digits accurately (which would give '11.545000000000000') and then tacking extra zeros on the end to make up the requested number of places ...
2
2010-02-10T11:13:29Z
[ "python", "c", "floating-point", "printf", "precision" ]
Python design question
2,234,531
<p>I'm a C programmer and I'm getting quite good with Python. But I still have some problems getting my mind around the OO awesomeness of Python.</p> <p>Here is my current design problem:</p> <p>The end "product" is a JSON data structure created in Python (and passed to Javascript code) containing different types of...
1
2010-02-10T05:03:35Z
2,234,565
<p>In my mind, it basically goes like this: you should try to keep things the same where they <em>are</em> the same, and separate them where they're different.</p> <p>If you're performing the exact same operations on and with the data, and it can all be represented in a common format, then there's no reason to have se...
5
2010-02-10T05:14:31Z
[ "python", "json", "oop" ]
Python design question
2,234,531
<p>I'm a C programmer and I'm getting quite good with Python. But I still have some problems getting my mind around the OO awesomeness of Python.</p> <p>Here is my current design problem:</p> <p>The end "product" is a JSON data structure created in Python (and passed to Javascript code) containing different types of...
1
2010-02-10T05:03:35Z
2,234,568
<p>Do the different response sources represent fundamentally different categories or <i>class</i>es of objects? They don't appear to, the way you've described it.</p> <p>Thus, various encode/decode functions and passing around only one type seems the best solution for you.</p> <p>That type can be a dict or your own ...
3
2010-02-10T05:15:15Z
[ "python", "json", "oop" ]
Python design question
2,234,531
<p>I'm a C programmer and I'm getting quite good with Python. But I still have some problems getting my mind around the OO awesomeness of Python.</p> <p>Here is my current design problem:</p> <p>The end "product" is a JSON data structure created in Python (and passed to Javascript code) containing different types of...
1
2010-02-10T05:03:35Z
2,234,575
<p>I've had success with the OOP approach. Consider a base class with a "ToJson" method and have each subclass implement it appropriately. Then your higher level code doesn't need to know any detail about how the data was obtained...it just knows it has to call "ToJson" on every object in the list you mentioned.</p> <...
1
2010-02-10T05:16:29Z
[ "python", "json", "oop" ]
Python design question
2,234,531
<p>I'm a C programmer and I'm getting quite good with Python. But I still have some problems getting my mind around the OO awesomeness of Python.</p> <p>Here is my current design problem:</p> <p>The end "product" is a JSON data structure created in Python (and passed to Javascript code) containing different types of...
1
2010-02-10T05:03:35Z
2,234,577
<p>Personally, I opt for the latter (passing around a list of data) wherever and whenever possible. I think OO is often misused/abused for certain things. I specifically avoid things like wrapping data in an object just for the sake of wrapping it in an object. So this, <code>{'type':'url', 'data':{some_other_dict}}...
1
2010-02-10T05:16:55Z
[ "python", "json", "oop" ]
Python design question
2,234,531
<p>I'm a C programmer and I'm getting quite good with Python. But I still have some problems getting my mind around the OO awesomeness of Python.</p> <p>Here is my current design problem:</p> <p>The end "product" is a JSON data structure created in Python (and passed to Javascript code) containing different types of...
1
2010-02-10T05:03:35Z
2,234,728
<p>Your receiver objects (which can perfectly well be instances of different classes, perhaps generated by a Factory pattern depending on the source of incoming data) should all have a common method that returns the appropriate dict (or other directly-JSON'able structure, such as a list that will turn into a JSON array...
2
2010-02-10T05:49:54Z
[ "python", "json", "oop" ]
PyEval_CallObject failing in loop occasionally
2,234,539
<p>I am struggling a bit with the Python C API. I am calling a python method to do some game AI at about 60hz. It works <em>most</em> of the time but every second or so the call to PyEval_CallObject results in a NULL return value. If I correctly detect the error and continue looping, all is well for the next second o...
2
2010-02-10T05:07:11Z
2,234,589
<p>You can call <code>PyImport_Import()</code> as often as you like, but you'll just keep getting the same module object back. Python caches imports. Also, instead of creating a new Python string and leaking the reference (and thus the object), you should just use <code>PyImport_ImportModule()</code>, which takes a <co...
3
2010-02-10T05:19:45Z
[ "python", "c", "python-c-api" ]
Manipulate table values with PyObjC and Core Data
2,234,540
<p><br/> I recently started programming with xCode and PyObjC and I'm trying to create a logistics application. I have a table with the store's items and a second table with the expenses. Until now everything worked ok by using just the interface builder to link values to core data information and I didn't have to writ...
2
2010-02-10T05:07:50Z
5,110,905
<p>You'll need to write some code. </p> <p>The usually way of doing this is to put the logic into a transitional attribute in your entity. Then code a custom getter for the transitional attribute such that the value of the attribute can change as other data changes. </p> <p>In this case, <code>cost</code> would be yo...
0
2011-02-24T21:49:32Z
[ "python", "xcode", "core-data", "pyobjc" ]
draw text to center of the image using PIL
2,234,874
<p>hi I have a few stings I would like to draw to imagea. the thing is that I want each string to be displayed in the middle of each image. how do I do it? or how can I know the length in pixels of a string (giving ofcourse the font)? thanks</p>
1
2010-02-10T06:29:40Z
2,235,081
<p>PIL font's do have a getsize method which you can use to get width and height of font, did you try that? e.g.</p> <pre><code>from PIL import Image, ImageDraw, ImageFont fontFile = "/usr/share/fonts/truetype/freefont/FreeSansBold.ttf" font = ImageFont.truetype(fontFile, 10) text="anurag" print font.getsize(text) as...
1
2010-02-10T07:22:54Z
[ "python", "text", "python-imaging-library" ]
draw text to center of the image using PIL
2,234,874
<p>hi I have a few stings I would like to draw to imagea. the thing is that I want each string to be displayed in the middle of each image. how do I do it? or how can I know the length in pixels of a string (giving ofcourse the font)? thanks</p>
1
2010-02-10T06:29:40Z
18,807,470
<p>You could find an implementation of this at <a href="http://tools.jedutils.com/tools/center-text-image" rel="nofollow">http://tools.jedutils.com/tools/center-text-image</a></p> <p>You could use that page for creating the image right there instead of implementing the routine yourself but the code is included on the ...
1
2013-09-14T23:41:05Z
[ "python", "text", "python-imaging-library" ]
Why both, import logging and import logging.config are needed?
2,234,982
<p>Should not it be handled by a single import? i.e. import logging. If I do not include import logging.config in my script, it gives :</p> <p>AttributeError: 'module' object has no attribute 'config'</p>
6
2010-02-10T06:56:10Z
2,235,012
<p><code>logging</code> is a package. Modules in packages aren't imported until you (or something in your program) imports them. You don't need both <code>import logging</code> and <code>import logging.config</code> though: just <code>import logging.config</code> will make the name <code>logging</code> available alread...
15
2010-02-10T07:03:53Z
[ "python", "logging", "import" ]
Probem with links Embedding the app engine's shell app within my own app, for help coding
2,235,080
<p>In my own App Engine App, at the browser, I wanted to mess around with new code by simulating having access to the python interpretor's >> within a browser window by embdedding this app --> <code>http://shell.appspot.com/</code> .</p> <p>One can get <code>shell_20091112.tar.gz</code> at <code>http://code.google.co...
1
2010-02-10T07:22:45Z
2,260,048
<p>The answer to my own question is to install the following shell console, very easy, and looks very helpful:</p> <p><a href="http://con.appspot.com/console/help/about" rel="nofollow">http://con.appspot.com/console/help/about</a></p>
1
2010-02-14T03:26:53Z
[ "python", "google-app-engine", "httphandler" ]
Easiest way to display a pdf, ps or dvi file from python in linux
2,235,090
<p>I have created an application with python and wxpython.</p> <p>I would like to display a help file in pdf, ps or dvi format in GNU/Linux.</p> <p>Could use the distribution pdf viewer, but not so easy when you don't know which they have.</p> <p>Any ideas on how to solve this?</p>
1
2010-02-10T07:24:05Z
2,235,115
<p>Invoke <a href="http://portland.freedesktop.org/xdg-utils-1.0/xdg-open.html" rel="nofollow"><code>xdg-open</code></a> against the file.</p>
4
2010-02-10T07:28:30Z
[ "python", "linux", "pdf", "wxpython" ]
Easiest way to display a pdf, ps or dvi file from python in linux
2,235,090
<p>I have created an application with python and wxpython.</p> <p>I would like to display a help file in pdf, ps or dvi format in GNU/Linux.</p> <p>Could use the distribution pdf viewer, but not so easy when you don't know which they have.</p> <p>Any ideas on how to solve this?</p>
1
2010-02-10T07:24:05Z
2,237,345
<p>Try also <a href="http://projects.gnome.org/evince/" rel="nofollow">evince</a>.</p>
1
2010-02-10T14:11:31Z
[ "python", "linux", "pdf", "wxpython" ]
Automate file download from http using python
2,235,458
<p>I want to automatically save a file from a website. I don't know how to bypass the Download File prompt in python and save it directly to my c: drive.</p> <p>Any help is appreciated, Elliott</p>
0
2010-02-10T08:52:03Z
2,235,466
<p>Modules like <a href="http://docs.python.org/library/urllib2.html" rel="nofollow"><code>urllib2</code></a> and <a href="http://urlgrabber.baseurl.org/" rel="nofollow"><code>urlgrabber</code></a> don't <strong>have</strong> a "Download File" prompt.</p>
3
2010-02-10T08:54:27Z
[ "python" ]
Automate file download from http using python
2,235,458
<p>I want to automatically save a file from a website. I don't know how to bypass the Download File prompt in python and save it directly to my c: drive.</p> <p>Any help is appreciated, Elliott</p>
0
2010-02-10T08:52:03Z
2,235,640
<p>One idea is to use a module like <a href="http://pypi.python.org/pypi/mechanize/0.1.7b" rel="nofollow">mechanize</a> to automate the query and the download. <a href="http://stockrt.github.com/p/emulating-a-browser-in-python-with-mechanize/" rel="nofollow">Here</a> you can find some documentation.</p> <ul> <li>Usual...
0
2010-02-10T09:22:53Z
[ "python" ]
Subclass builtin List
2,235,556
<p>I want to subclass the <code>list</code> type and have slicing return an object of the descendant type, however it is returning a <code>list</code>. What is the minimum code way to do this? </p> <p>If there isn't a neat way to do it, I'll just include a list internally which is slightly more messy, but not unreason...
11
2010-02-10T09:08:43Z
2,235,580
<p>I guess you should override the <code>__getslice__</code> method to return an object of your type...</p> <p>Maybe something like the following?</p> <pre><code>class MyList(list): #your stuff here def __getslice__(self, i, j): return MyList(list.__getslice__(self, i, j)) </code></pre>
9
2010-02-10T09:12:33Z
[ "python", "list", "subclass", "builtin" ]
Slow XML-RPC in Windows 7 with XML-RPC.NET
2,235,643
<p>I'm considering to use XML-RPC.NET to communicate with a Linux XML-RPC server written in Python. I have tried a sample application (MathApp) from Cook Computing's XML-RPC.NET but it took 30 seconds for the app to add two numbers within the same LAN with server. </p> <p>I have also tried to run a simple client writt...
1
2010-02-10T09:23:18Z
2,235,703
<p>Run a packet capture on the client machine, check the network traffic timings versus the time the function is called.</p> <p>This may help you determine where the latency is in your slow process, e.g. application start-up time, name resolution, etc.</p> <p>How are you addressing the server from the client? By IP? ...
0
2010-02-10T09:35:24Z
[ "c#", ".net", "python", "windows-7", "xml-rpc" ]
Slow XML-RPC in Windows 7 with XML-RPC.NET
2,235,643
<p>I'm considering to use XML-RPC.NET to communicate with a Linux XML-RPC server written in Python. I have tried a sample application (MathApp) from Cook Computing's XML-RPC.NET but it took 30 seconds for the app to add two numbers within the same LAN with server. </p> <p>I have also tried to run a simple client writt...
1
2010-02-10T09:23:18Z
2,678,350
<p>There is <a href="http://bugs.python.org/issue6085" rel="nofollow">a bug</a> that affects <code>BaseHTTPServer</code> and its subclasses (including <code>SimpleXMLRPCServer</code>). Basically, your server is likely to call <code>socket.getfqdn</code> function for every IP address it is trying to log. This <a href="h...
1
2010-04-20T20:21:37Z
[ "c#", ".net", "python", "windows-7", "xml-rpc" ]
Are there any visual tools for Python unit tests?
2,235,701
<p>I'm writing quite a few unit tests and using <a href="https://nose.readthedocs.org/en/latest/" rel="nofollow">nosetests</a> to run them. Nose certainly makes it nice and easy to run tests, but the output can be pretty cluttered at the best of times, and a downright mess at others, depending on warnings and errors.<...
8
2010-02-10T09:35:00Z
2,235,726
<p>There's a very similar question <a href="http://stackoverflow.com/questions/638127/is-there-a-gui-for-nosetests">here</a>.</p> <p>It doesn't look like there's a standardized GUI for nosetests (as in, standardized for just the python interface). There seems to be a few GUI plugins for nosetests depending on which ID...
4
2010-02-10T09:39:29Z
[ "python", "unit-testing", "user-interface", "nose", "nosetests" ]
Are there any visual tools for Python unit tests?
2,235,701
<p>I'm writing quite a few unit tests and using <a href="https://nose.readthedocs.org/en/latest/" rel="nofollow">nosetests</a> to run them. Nose certainly makes it nice and easy to run tests, but the output can be pretty cluttered at the best of times, and a downright mess at others, depending on warnings and errors.<...
8
2010-02-10T09:35:00Z
2,235,744
<p>Have you taken a look at the <a href="http://nose-plugins.jottit.com/" rel="nofollow">Nose plugins page</a>? A quick glance revealed at least a couple that stated they could provide a GUI interface.</p>
0
2010-02-10T09:42:01Z
[ "python", "unit-testing", "user-interface", "nose", "nosetests" ]
Are there any visual tools for Python unit tests?
2,235,701
<p>I'm writing quite a few unit tests and using <a href="https://nose.readthedocs.org/en/latest/" rel="nofollow">nosetests</a> to run them. Nose certainly makes it nice and easy to run tests, but the output can be pretty cluttered at the best of times, and a downright mess at others, depending on warnings and errors.<...
8
2010-02-10T09:35:00Z
12,208,024
<p>Well, what you can do is to use the <a href="http://en.wikipedia.org/wiki/Jenkins_%28software%29" rel="nofollow">jenkins</a> integration server, we use it with py.test and phpunit (it is really simple to use it with any other testing framework), it provides us some fancy (and useful!) graphs containing </p> <ul> <l...
3
2012-08-31T02:06:18Z
[ "python", "unit-testing", "user-interface", "nose", "nosetests" ]
Interchange of Position of Two Keyword Arguments Throws Error
2,235,895
<p>I have an odd problem. I know that in Python, kwargs follow args, so I checked for that and it's not the problem. What is the problem is this:</p> <p>Fine: </p> <p><code>def __init__(self, sample_rate, label=u"", data=[] ):</code></p> <p>TypeError: <code>__init__()</code> got multiple values for keyword argument ...
0
2010-02-10T10:04:22Z
2,235,944
<p>You are calling the code with</p> <pre><code>Channel(self.sample_rate, self.label, data=list.__getslice__(self,start,stop)) </code></pre> <p>Note that the second parameter has no keyword, so the interpreter assumes this is the <code>data</code> parameter (because that's the order they are defined in the function)....
4
2010-02-10T10:14:46Z
[ "python", "kwargs" ]
Interchange of Position of Two Keyword Arguments Throws Error
2,235,895
<p>I have an odd problem. I know that in Python, kwargs follow args, so I checked for that and it's not the problem. What is the problem is this:</p> <p>Fine: </p> <p><code>def __init__(self, sample_rate, label=u"", data=[] ):</code></p> <p>TypeError: <code>__init__()</code> got multiple values for keyword argument ...
0
2010-02-10T10:04:22Z
2,235,947
<p>In the second version (<code>def __init__(self, sample_rate, data=[], label=u""):</code>), the second positional argument (when called, that means not counting <code>self</code>) is <code>data</code>, but in <code>__getslice__</code>, the second argument that you pass is <code>label</code>. So you should either keep...
4
2010-02-10T10:15:17Z
[ "python", "kwargs" ]
Interchange of Position of Two Keyword Arguments Throws Error
2,235,895
<p>I have an odd problem. I know that in Python, kwargs follow args, so I checked for that and it's not the problem. What is the problem is this:</p> <p>Fine: </p> <p><code>def __init__(self, sample_rate, label=u"", data=[] ):</code></p> <p>TypeError: <code>__init__()</code> got multiple values for keyword argument ...
0
2010-02-10T10:04:22Z
2,235,955
<p>The problem is that in your calling code, you have two positional arguments:</p> <pre><code>return Channel(self.sample_rate, self.label, data=list.__getslice__(self,start,stop)) # sample_rate (pos) data (pos) data (kw) </code></pre> <p>In Python 2.x, there is no distinction between positional and key...
2
2010-02-10T10:16:28Z
[ "python", "kwargs" ]
How to get enumerated results from a query in Django?
2,236,010
<p>I have a query that orders a table according to a particular field. The table is related to one competition, so knowing the position is important.</p> <p>As the table can grow to have a lot of rows, I have paginate it (25 rows per page) using generic views, but the problem I've got is to present the numbered positi...
2
2010-02-10T10:25:17Z
2,236,043
<p>If it's not about identifying rows in the frontend, why not use the primary key of current table?</p>
0
2010-02-10T10:31:34Z
[ "python", "django" ]
How to get enumerated results from a query in Django?
2,236,010
<p>I have a query that orders a table according to a particular field. The table is related to one competition, so knowing the position is important.</p> <p>As the table can grow to have a lot of rows, I have paginate it (25 rows per page) using generic views, but the problem I've got is to present the numbered positi...
2
2010-02-10T10:25:17Z
2,236,050
<p>If you're in your template, you can use something like:</p> <pre><code>{{ forloop.counter0|add:page.start_index }} </code></pre> <p>Where <code>start_index</code> on pagination's page object gives you the 1-based starting index for that page. Of course, the other way around is probably more readable...</p> <pre><...
3
2010-02-10T10:32:19Z
[ "python", "django" ]
Google App Engine Internationalization Help needed (Python)
2,236,153
<p>Has anyone any suggestions on how to use internationalization in app engine / webapp / python. I have seen some posts re - django - translation support but i cant seem to find enough info on how to make it work.</p> <p>What i need is a solution where</p> <ol> <li>browser can detect language</li> <li>user can overr...
7
2010-02-10T10:47:42Z
3,489,359
<p>Here I provide some informations about Internationalization and Localization under the Google App Engine framework. <a href="http://eflorent.blogspot.com/2010/08/internationalization-under-google-app.html" rel="nofollow">http://eflorent.blogspot.com/2010/08/internationalization-under-google-app.html</a></p>
1
2010-08-15T21:29:08Z
[ "python", "google-app-engine", "internationalization" ]
Google App Engine Internationalization Help needed (Python)
2,236,153
<p>Has anyone any suggestions on how to use internationalization in app engine / webapp / python. I have seen some posts re - django - translation support but i cant seem to find enough info on how to make it work.</p> <p>What i need is a solution where</p> <ol> <li>browser can detect language</li> <li>user can overr...
7
2010-02-10T10:47:42Z
3,963,175
<p>There are several options to consider. </p> <ul> <li>Standard gettext(). See <a href="http://code.google.com/p/appengine-i18n/source/browse/#svn/trunk%3Fstate%3Dclosed" rel="nofollow">this</a> code example. The code is outdated: there is a standard way to manage cookies and sessions, so it should be rewritten for t...
2
2010-10-18T20:37:43Z
[ "python", "google-app-engine", "internationalization" ]
Compiling gVim with Python 3 support
2,236,171
<p>I hope this is the right place to ask this question:</p> <p>I am trying to compile gVim with python 3 support using cygwin under windows:</p> <p>I changed the Make_cyg.mak files Python section to the following:</p> <pre><code>############################## # DYNAMIC_PYTHON=yes works. # DYNAMIC_PYTHON=no does not ...
2
2010-02-10T10:48:49Z
2,237,572
<p>There is a lot of things going on at once here. First of all, why do you want Python 3.0? If you really want Python 3, then you should use Python 3.1. </p> <p>Second of all, what does "Python 3 support" mean in the case of gVim? Is it to make extensions with Python? Then you don't want Python 3 support, as any exte...
1
2010-02-10T14:45:28Z
[ "python", "vim", "compilation", "python-3.x" ]
Compiling gVim with Python 3 support
2,236,171
<p>I hope this is the right place to ask this question:</p> <p>I am trying to compile gVim with python 3 support using cygwin under windows:</p> <p>I changed the Make_cyg.mak files Python section to the following:</p> <pre><code>############################## # DYNAMIC_PYTHON=yes works. # DYNAMIC_PYTHON=no does not ...
2
2010-02-10T10:48:49Z
2,674,495
<p>I have compiled vim with python3 support. Here is the <a href="http://groups.google.com/group/vim_dev/attach/24f6ef58245030c7/vim-7.2.411_python3.patch?part=4" rel="nofollow">Patch</a> updated for vim 7.2.411.</p> <p>For compilation instruction check out my 2009 September 22 mail on </p> <blockquote> <p>groups.g...
4
2010-04-20T11:05:55Z
[ "python", "vim", "compilation", "python-3.x" ]
Position charts on Django
2,236,181
<p>I'm trying to do something similar to this, in Django. This is part of the page of Anna:</p> <pre><code>Pos NickName Points --- --------- ------ 1 The best 1000 ... 35 Roger 550 36 Anna 545 37 Paul 540 </code></pre> <p>It's a chart showing the scoring system, and it int...
1
2010-02-10T10:53:19Z
2,236,194
<p>Get the <code>COUNT()</code> of records that have a higher <code>points</code>.</p>
2
2010-02-10T10:55:11Z
[ "python", "django" ]
Will Python use all processors in thread mode?
2,236,321
<p>While developing a Django app deployed on Apache mod_wsgi I found that in case of multithreading (Python threads; mod_wsgi processes=1 threads=8) Python won't use all available processors. With the multiprocessing approach (mod_wsgi processes=8 threads=1) all is fine and I can load my machine at full.</p> <p>So the...
5
2010-02-10T11:17:56Z
2,236,368
<p>Will Python use all processors in thread mode? No.</p> <p>Python won't use all available processors; is this Python behavior normal? Yes, it's normal because of the GIL.</p> <p>For a discussion see <a href="http://mail.python.org/pipermail/python-3000/2007-May/007414.html">http://mail.python.org/pipermail/python-...
10
2010-02-10T11:24:47Z
[ "python", "django", "multithreading", "performance", "multiprocessing" ]
Will Python use all processors in thread mode?
2,236,321
<p>While developing a Django app deployed on Apache mod_wsgi I found that in case of multithreading (Python threads; mod_wsgi processes=1 threads=8) Python won't use all available processors. With the multiprocessing approach (mod_wsgi processes=8 threads=1) all is fine and I can load my machine at full.</p> <p>So the...
5
2010-02-10T11:17:56Z
2,236,370
<p>I don't know if it is still the case, but there is a global lock in the Python interpreter, which prevents the use of all processor resources from a single interpreter, even when using multi threading. <a href="http://en.wiktionary.org/wiki/IIRC" rel="nofollow">IIRC</a>, the global lock has to do with I/O.</p> <p>I...
1
2010-02-10T11:25:09Z
[ "python", "django", "multithreading", "performance", "multiprocessing" ]
Will Python use all processors in thread mode?
2,236,321
<p>While developing a Django app deployed on Apache mod_wsgi I found that in case of multithreading (Python threads; mod_wsgi processes=1 threads=8) Python won't use all available processors. With the multiprocessing approach (mod_wsgi processes=8 threads=1) all is fine and I can load my machine at full.</p> <p>So the...
5
2010-02-10T11:17:56Z
2,236,374
<ol> <li><p>Will python use all processors in thread mode? No.</p></li> <li><p>It this normal? Yes, this is normal. Python makes no effort to locate all your cores.</p></li> <li><p>"1 process with few threads is default mod_wsgi approach". But that's not optimal or even desirable. That's just a default. Don't read...
4
2010-02-10T11:25:35Z
[ "python", "django", "multithreading", "performance", "multiprocessing" ]
Will Python use all processors in thread mode?
2,236,321
<p>While developing a Django app deployed on Apache mod_wsgi I found that in case of multithreading (Python threads; mod_wsgi processes=1 threads=8) Python won't use all available processors. With the multiprocessing approach (mod_wsgi processes=8 threads=1) all is fine and I can load my machine at full.</p> <p>So the...
5
2010-02-10T11:17:56Z
2,236,386
<p>Yes. Python is not really multi-threaded. Instead, there is a global lock and each thread gets to execute a few operations in turn. This makes it much more simple to write MT applications in Python since there can't be any problems with stale caches, etc.</p> <p>So one Python process can only ever occupy a single C...
1
2010-02-10T11:26:49Z
[ "python", "django", "multithreading", "performance", "multiprocessing" ]
Will Python use all processors in thread mode?
2,236,321
<p>While developing a Django app deployed on Apache mod_wsgi I found that in case of multithreading (Python threads; mod_wsgi processes=1 threads=8) Python won't use all available processors. With the multiprocessing approach (mod_wsgi processes=8 threads=1) all is fine and I can load my machine at full.</p> <p>So the...
5
2010-02-10T11:17:56Z
2,236,855
<p>There is still hope. The GIL is only an implementation artifact of the C Python implementation that you download from python.org. Jython and IronPython are two other implementations of Python, and they have no GIL, so you may have better threading results with one of them.</p>
3
2010-02-10T12:53:38Z
[ "python", "django", "multithreading", "performance", "multiprocessing" ]
Tell urllib2 to use custom DNS
2,236,498
<p>I'd like to tell <code>urllib2.urlopen</code> (or a <strong>custom opener</strong>) to use <code>127.0.0.1</code> (or <code>::1</code>) to resolve addresses. I wouldn't change my <code>/etc/resolv.conf</code>, however.</p> <p>One possible solution is to use a tool like <code>dnspython</code> to query addresses and ...
11
2010-02-10T11:46:05Z
2,237,040
<p>Looks like name resolution is ultimately handled by <code>socket.create_connection</code>.</p> <pre><code>-&gt; urllib2.urlopen -&gt; httplib.HTTPConnection -&gt; socket.create_connection </code></pre> <p>Though once the "Host:" header has been set, you can resolve the host and pass on the IP address through down ...
19
2010-02-10T13:24:10Z
[ "python", "dns", "urllib2", "dnspython", "urlopen" ]
Tell urllib2 to use custom DNS
2,236,498
<p>I'd like to tell <code>urllib2.urlopen</code> (or a <strong>custom opener</strong>) to use <code>127.0.0.1</code> (or <code>::1</code>) to resolve addresses. I wouldn't change my <code>/etc/resolv.conf</code>, however.</p> <p>One possible solution is to use a tool like <code>dnspython</code> to query addresses and ...
11
2010-02-10T11:46:05Z
2,237,322
<p>You will need to implement your own dns lookup client (or using dnspython as you said). The name lookup procedure in glibc is pretty complex to ensure compatibility with other non-dns name systems. There's for example no way to specify a particular DNS server in the glibc library at all.</p>
0
2010-02-10T14:07:44Z
[ "python", "dns", "urllib2", "dnspython", "urlopen" ]
Tell urllib2 to use custom DNS
2,236,498
<p>I'd like to tell <code>urllib2.urlopen</code> (or a <strong>custom opener</strong>) to use <code>127.0.0.1</code> (or <code>::1</code>) to resolve addresses. I wouldn't change my <code>/etc/resolv.conf</code>, however.</p> <p>One possible solution is to use a tool like <code>dnspython</code> to query addresses and ...
11
2010-02-10T11:46:05Z
15,065,711
<p>Another (dirty) way is monkey-patching <code>socket.getaddrinfo</code>.</p> <p>For example this code adds a (unlimited) cache for dns lookups.</p> <pre><code>import socket prv_getaddrinfo = socket.getaddrinfo dns_cache = {} # or a weakref.WeakValueDictionary() def new_getaddrinfo(*args): try: return d...
11
2013-02-25T11:15:27Z
[ "python", "dns", "urllib2", "dnspython", "urlopen" ]
PHP / cURL problem opening remote file
2,236,864
<p>We have a script which pulls some XML from a remote server. If this script is running on any server other than production, it works.</p> <p>Upload it to production however, and it fails. It is using cURL for the request but it doesn't matter how we do it - fopen, file_get_contents, sockets - it just times out. This...
0
2010-02-10T12:54:40Z
2,236,930
<p>Run Wireshark and see how far the request goes. Could be a firewall issue, a DNS resolution problem, among other things.</p> <p>Also, try bumping your curl timeout to something much higher, like 300s, and see how it goes.</p>
1
2010-02-10T13:05:34Z
[ "php", "python", "xml", "apache", "curl" ]
Passing arguments to the generic views Django queryset
2,236,886
<p>I would like to make a queryset on a <strong>generic view</strong> in this way:</p> <pre><code> category_info = { 'queryset' : ModelObject.objects.filter(category=category_id) } </code></pre> <p>where the <code>category_id</code> will be stated on the URL</p> <pre><code> (r'^category/(?P&lt;category_id&gt;...
1
2010-02-10T12:58:08Z
2,236,941
<p>You'll have to define your own view and return the generic view from within:</p> <p>urls.py:</p> <pre><code>(r'^category/(?P&lt;category_id&gt;\d+)$', 'myapp.views.category_list') </code></pre> <p>myapp/views.py</p> <pre><code>from django.views.generic.list_detail import object_list def category_list(request, ca...
3
2010-02-10T13:06:59Z
[ "python", "django" ]
First Python list index greater than x?
2,236,906
<p>What would be the most pythonesque way to find the first index in a list that is greater than x?</p> <p>For example, with</p> <pre><code>list = [0.5, 0.3, 0.9, 0.8] </code></pre> <p>The function</p> <pre><code>f(list, 0.7) </code></pre> <p>would return</p> <pre><code>2. </code></pre>
25
2010-02-10T13:01:40Z
2,236,922
<pre><code>&gt;&gt;&gt; f=lambda seq, m: [ii for ii in xrange(0, len(seq)) if seq[ii] &gt; m][0] &gt;&gt;&gt; f([.5, .3, .9, .8], 0.7) 2 </code></pre>
0
2010-02-10T13:03:56Z
[ "python", "list" ]
First Python list index greater than x?
2,236,906
<p>What would be the most pythonesque way to find the first index in a list that is greater than x?</p> <p>For example, with</p> <pre><code>list = [0.5, 0.3, 0.9, 0.8] </code></pre> <p>The function</p> <pre><code>f(list, 0.7) </code></pre> <p>would return</p> <pre><code>2. </code></pre>
25
2010-02-10T13:01:40Z
2,236,935
<pre><code>next(x[0] for x in enumerate(L) if x[1] &gt; 0.7) </code></pre>
40
2010-02-10T13:06:16Z
[ "python", "list" ]
First Python list index greater than x?
2,236,906
<p>What would be the most pythonesque way to find the first index in a list that is greater than x?</p> <p>For example, with</p> <pre><code>list = [0.5, 0.3, 0.9, 0.8] </code></pre> <p>The function</p> <pre><code>f(list, 0.7) </code></pre> <p>would return</p> <pre><code>2. </code></pre>
25
2010-02-10T13:01:40Z
2,236,955
<pre><code>for index, elem in enumerate(elements): if elem &gt; reference: return index raise ValueError("Nothing Found") </code></pre>
3
2010-02-10T13:09:28Z
[ "python", "list" ]
First Python list index greater than x?
2,236,906
<p>What would be the most pythonesque way to find the first index in a list that is greater than x?</p> <p>For example, with</p> <pre><code>list = [0.5, 0.3, 0.9, 0.8] </code></pre> <p>The function</p> <pre><code>f(list, 0.7) </code></pre> <p>would return</p> <pre><code>2. </code></pre>
25
2010-02-10T13:01:40Z
2,236,956
<pre><code>&gt;&gt;&gt; alist= [0.5, 0.3, 0.9, 0.8] &gt;&gt;&gt; [ n for n,i in enumerate(alist) if i&gt;0.7 ][0] 2 </code></pre>
9
2010-02-10T13:09:31Z
[ "python", "list" ]
First Python list index greater than x?
2,236,906
<p>What would be the most pythonesque way to find the first index in a list that is greater than x?</p> <p>For example, with</p> <pre><code>list = [0.5, 0.3, 0.9, 0.8] </code></pre> <p>The function</p> <pre><code>f(list, 0.7) </code></pre> <p>would return</p> <pre><code>2. </code></pre>
25
2010-02-10T13:01:40Z
2,236,969
<p><strong>Another one:</strong></p> <pre><code>map(lambda x: x&gt;.7, seq).index(True) </code></pre>
2
2010-02-10T13:11:22Z
[ "python", "list" ]
First Python list index greater than x?
2,236,906
<p>What would be the most pythonesque way to find the first index in a list that is greater than x?</p> <p>For example, with</p> <pre><code>list = [0.5, 0.3, 0.9, 0.8] </code></pre> <p>The function</p> <pre><code>f(list, 0.7) </code></pre> <p>would return</p> <pre><code>2. </code></pre>
25
2010-02-10T13:01:40Z
2,236,993
<pre><code>filter(lambda x: x&gt;.7, seq)[0] </code></pre>
7
2010-02-10T13:14:35Z
[ "python", "list" ]
First Python list index greater than x?
2,236,906
<p>What would be the most pythonesque way to find the first index in a list that is greater than x?</p> <p>For example, with</p> <pre><code>list = [0.5, 0.3, 0.9, 0.8] </code></pre> <p>The function</p> <pre><code>f(list, 0.7) </code></pre> <p>would return</p> <pre><code>2. </code></pre>
25
2010-02-10T13:01:40Z
2,240,016
<p>if list is sorted then <code>bisect_left(alist, value)</code> is faster for a large list than <code>next(i for i, x in enumerate(alist) if x &gt;= value)</code>.</p>
5
2010-02-10T20:13:18Z
[ "python", "list" ]
First Python list index greater than x?
2,236,906
<p>What would be the most pythonesque way to find the first index in a list that is greater than x?</p> <p>For example, with</p> <pre><code>list = [0.5, 0.3, 0.9, 0.8] </code></pre> <p>The function</p> <pre><code>f(list, 0.7) </code></pre> <p>would return</p> <pre><code>2. </code></pre>
25
2010-02-10T13:01:40Z
19,693,684
<p>I had similar problem when my list was very long. comprehension or filter -based solutions would go thru whole list. itertools.takewhile will break the loop once condition become false first time:</p> <pre><code>from itertools import takewhile def f(l, b): return len([x for x in takewhile(lambda x: x[1] &lt;= b, e...
0
2013-10-30T20:58:52Z
[ "python", "list" ]
passing arguments to a dynamic form in django
2,237,064
<p>I have a Dynamic Form in forms. How can I pass an argument from my view when I instantiate my form?</p> <p>Something like: </p> <pre><code>form = DynamicForm("some string argument I'm passing to my form") </code></pre> <p>This is the form I have:</p> <pre><code>class DynamicForm(Form): def __init__(self, *args, ...
18
2010-02-10T13:28:06Z
2,237,118
<p>Add it as keyword argument, say it's called my_arg.</p> <pre><code>class DynamicForm(Form): def __init__(self, *args, **kwargs): my_arg = kwargs.pop('my_arg') super(DynamicForm, self).__init__(*args, **kwargs) for item in range(5): self.fields['test_field_%d' % item] = CharField(max_length=255...
41
2010-02-10T13:36:49Z
[ "python", "django", "django-forms" ]
passing arguments to a dynamic form in django
2,237,064
<p>I have a Dynamic Form in forms. How can I pass an argument from my view when I instantiate my form?</p> <p>Something like: </p> <pre><code>form = DynamicForm("some string argument I'm passing to my form") </code></pre> <p>This is the form I have:</p> <pre><code>class DynamicForm(Form): def __init__(self, *args, ...
18
2010-02-10T13:28:06Z
38,540,776
<p>Whenever we inherit a class and override some of the methods of inherited class, we should use '*args' and '**kwargs' and pass the received positional and keyword arguments to the superclass method. it recieves a tuple containing the positional arguments beyond the formal parameter list. So, "args" is a tuple. Don't...
0
2016-07-23T10:32:54Z
[ "python", "django", "django-forms" ]
How can I write to the textfile with "while"?
2,237,154
<pre><code>while 1: text_file = open("write_it.txt", "w") word = input("Please add to a text file: ") </code></pre> <p>What else do I need to add to make my code run properly?</p>
-6
2010-02-10T13:41:59Z
2,237,169
<p>Not sure, but that open statement inside of the while couldn't be affecting its behaviour? Have you tried just moving it out of while loop?</p>
0
2010-02-10T13:44:22Z
[ "python", "file-io" ]
How can I write to the textfile with "while"?
2,237,154
<pre><code>while 1: text_file = open("write_it.txt", "w") word = input("Please add to a text file: ") </code></pre> <p>What else do I need to add to make my code run properly?</p>
-6
2010-02-10T13:41:59Z
2,237,268
<p>This should work:</p> <pre><code>text_file = open("write_it.txt", "w") while 1: word = input("Please add to a text file: ") if not word: break text_file.write(word) text_file.close() </code></pre>
2
2010-02-10T13:58:36Z
[ "python", "file-io" ]
Using sub filters/queries in Google App Engine
2,237,224
<p>I'm trying to use figure out how to sub query a query that uses a filter. From what I've figured out so far while using .filter() it changes the original query, that leads to a second .filter() would also have to match the first filter.</p> <p>I would like to make something like this:</p> <pre><code>modules = data...
1
2010-02-10T13:50:51Z
2,237,790
<p>It appears that what you're trying to do is an OR query, which isn't supported in App Engine. You can use an IN query, which simulates this by doing multiple queries for you.</p> <p>The reason the first thing you tried doesn't work is that you're trying to filter your query so that your results match both "Test" a...
3
2010-02-10T15:09:09Z
[ "python", "google-app-engine" ]
Use Python logging to determine where a method was called from
2,237,355
<p>I'm trying to debug a Python Django app.</p> <p>I've got additional fields being added to a model. </p> <p>I've tracked this down to one method:<br> django.db.models.options.add_field()</p> <p>The only place this method is called is:<br> django.db.models.fields.<strong>init</strong>.contribute_to_class() </p> <...
0
2010-02-10T14:13:48Z
2,237,448
<p>Use <a href="http://docs.python.org/library/traceback.html" rel="nofollow">traceback</a> module.</p>
1
2010-02-10T14:26:58Z
[ "python", "django", "logging" ]
Serving static media during Django development: Why not MEDIA_ROOT?
2,237,418
<p>I read <a href="http://docs.djangoproject.com/en/1.1/howto/static-files/">this guide</a> about serving static media with Django during development.</p> <p>I noticed that <code>MEDIA_URL</code> and <code>MEDIA_ROOT</code> were not used in this. Why? What's the difference?</p> <p>I tried doing it with <code>MEDIA_UR...
17
2010-02-10T14:21:59Z
2,237,514
<p>In a production situation you will want your media to be served from your front end web server (Apache, Nginx or the like) to avoid extra load on the Django/Python process. The MEDIA_URL and MEDIA_ROOT are usually used for this.</p> <p>Running the built in Development server you will need to set the correct url in ...
24
2010-02-10T14:37:21Z
[ "python", "django", "static-media" ]
Serving static media during Django development: Why not MEDIA_ROOT?
2,237,418
<p>I read <a href="http://docs.djangoproject.com/en/1.1/howto/static-files/">this guide</a> about serving static media with Django during development.</p> <p>I noticed that <code>MEDIA_URL</code> and <code>MEDIA_ROOT</code> were not used in this. Why? What's the difference?</p> <p>I tried doing it with <code>MEDIA_UR...
17
2010-02-10T14:21:59Z
2,237,542
<p>Straight from the comments in settings.py...</p> <h3>MEDIA_ROOT</h3> <p>The <code>MEDIA_ROOT</code> is the absolute path to the directory that holds media such as <code>/home/media/media.lawrence.com/</code>. </p> <h3>MEDIA_URL</h3> <p>The <code>MEDIA_URL</code> is the URL that handles the media served from <co...
20
2010-02-10T14:40:57Z
[ "python", "django", "static-media" ]
Serving static media during Django development: Why not MEDIA_ROOT?
2,237,418
<p>I read <a href="http://docs.djangoproject.com/en/1.1/howto/static-files/">this guide</a> about serving static media with Django during development.</p> <p>I noticed that <code>MEDIA_URL</code> and <code>MEDIA_ROOT</code> were not used in this. Why? What's the difference?</p> <p>I tried doing it with <code>MEDIA_UR...
17
2010-02-10T14:21:59Z
38,446,584
<p>The Django docs recommend the following approach I've modified for my use case:</p> <pre><code>urlpatterns = [ # url patterns ] from django.conf import settings if settings.DEBUG: from django.conf.urls.static import static urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) </...
1
2016-07-18T21:53:02Z
[ "python", "django", "static-media" ]
How to send file to serial port using kermit protocol in python
2,237,483
<p>I have device connected through serial port to PC. Using c-kermit I can send commands to device and read output. I can also send files using kermit protocol.</p> <p>In python we have pretty nice library - pySerial. I can use it to send/receive data from device. But is there some nice solution to send files using ke...
1
2010-02-10T14:31:17Z
3,143,184
<p>You should be able to do it via the subprocess module. The following assumes that you can send commands to your remote machine and parse out the results already. :-)</p> <p>I don't have anything to test this on at the moment, so I'm going to be pretty general. Roughly: 1.) use pyserial to connect to the remote s...
1
2010-06-29T17:27:07Z
[ "python", "serial-port", "pyserial", "kermit" ]
Applying python decorators to methods in a class
2,237,624
<p>I have decorator <code>@login_testuser</code> applied against method <strong>test_1</strong>:</p> <pre><code>class TestCase(object): @login_testuser def test_1(self): print "test_1()" </code></pre> <p>Is there a way I can apply <code>@login_testuser</code> on every method of the class prefixed with...
12
2010-02-10T14:51:10Z
2,237,670
<p>Sure. Iterate all attributes of the class. Check each one for being a method and if the name starts with "test_". Then replace it with the function returned from your decorator</p> <p>Something like:</p> <pre><code>from inspect import ismethod, getmembers for name, obj in getmembers(TestCase, ismethod): if name...
3
2010-02-10T14:56:49Z
[ "python", "decorator" ]
Applying python decorators to methods in a class
2,237,624
<p>I have decorator <code>@login_testuser</code> applied against method <strong>test_1</strong>:</p> <pre><code>class TestCase(object): @login_testuser def test_1(self): print "test_1()" </code></pre> <p>Is there a way I can apply <code>@login_testuser</code> on every method of the class prefixed with...
12
2010-02-10T14:51:10Z
2,237,695
<p>Yes, you can loop over the class's <code>dir</code>/<code>__dict__</code> or have a metaclass that does so, identifying if the attributes start with "test". However, this will create less straightforward, explicit code than writing the decorator explicitly.</p>
0
2010-02-10T14:59:28Z
[ "python", "decorator" ]
Applying python decorators to methods in a class
2,237,624
<p>I have decorator <code>@login_testuser</code> applied against method <strong>test_1</strong>:</p> <pre><code>class TestCase(object): @login_testuser def test_1(self): print "test_1()" </code></pre> <p>Is there a way I can apply <code>@login_testuser</code> on every method of the class prefixed with...
12
2010-02-10T14:51:10Z
2,238,000
<p>Are you sure you wouldn't be better off by putting login_testuser's code into setUp instead? That's what setUp is for: it's run before every test method.</p>
3
2010-02-10T15:32:36Z
[ "python", "decorator" ]
Applying python decorators to methods in a class
2,237,624
<p>I have decorator <code>@login_testuser</code> applied against method <strong>test_1</strong>:</p> <pre><code>class TestCase(object): @login_testuser def test_1(self): print "test_1()" </code></pre> <p>Is there a way I can apply <code>@login_testuser</code> on every method of the class prefixed with...
12
2010-02-10T14:51:10Z
2,238,076
<p>In Python 2.6, a <a href="http://docs.python.org/whatsnew/2.6.html#pep-3129-class-decorators" rel="nofollow">class decorator</a> is definitely the way to go. E.g., here's a pretty general one for these kind of tasks:</p> <pre><code>import inspect def decallmethods(decorator, prefix='test_'): def dectheclass(cls...
17
2010-02-10T15:40:32Z
[ "python", "decorator" ]
AttributeError in tkinter gui programming
2,237,700
<p>I want to display my calculated output in a Gui window in python. I am trying with Tkinter. But I'm having problems displaying the output on Tkinter level widget. I am putting input data as address information in text field of Tkinter window and want latitude, longitude of that inputed address to the text label. Can...
1
2010-02-10T15:00:07Z
2,238,261
<p>On line 94 in <code>F:\JavaWorkspace\Test\src\gui_geo_location.py</code>, you're using <code>self.entryVariable</code> but that object does not have an <code>entryVariable</code> attribute.</p> <p>Based on your <code>__init__</code>, it seems you haven't defined <code>entryVariable</code> anywhere. Try adding:</p> ...
3
2010-02-10T16:02:48Z
[ "python", "tkinter" ]
How to do drag & drop with wxWidgets module of Python?
2,237,725
<p>I'm using Python and I want to do a drag &amp; drop interface. For example, with a large picture whose size is bigger then the screen, I want to click on it and drag it to see other parts. Something like "google maps"!</p> <p>In google maps if we click two times we do "zoom" but if we click one time and while press...
1
2010-02-10T15:03:03Z
2,250,788
<p>I found the solution over here: <a href="http://www.java2s.com/Code/Python/Event/Mouseactiondrag.htm" rel="nofollow">http://www.java2s.com/Code/Python/Event/Mouseactiondrag.htm</a></p> <p>With this code I can do what I want!</p> <p>And the name was only Mouse Drag and not Mouse Drag and Drop (soz)</p>
0
2010-02-12T09:34:18Z
[ "python", "drag-and-drop", "wxpython", "wxwidgets" ]
Delete Old directories in Python
2,237,909
<p>I have several directories and I want directories over 7 days old to be deleted. I have code already implemented but It doesn't seem to be working. Can anyone see where I am going wrong?</p> <pre><code>def delete_sandbox(): for directories in os.listdir(os.getcwd()): if not os.path.isdir(directories)...
1
2010-02-10T15:23:28Z
2,238,003
<p><code>os.listdir</code> returns a list of strings, which are relative paths. When you chdir to rootdir, dependent on what rootDir is, these paths might not be valid anymore. </p>
1
2010-02-10T15:32:58Z
[ "python", "time", "directory", "timestamp" ]
Delete Old directories in Python
2,237,909
<p>I have several directories and I want directories over 7 days old to be deleted. I have code already implemented but It doesn't seem to be working. Can anyone see where I am going wrong?</p> <pre><code>def delete_sandbox(): for directories in os.listdir(os.getcwd()): if not os.path.isdir(directories)...
1
2010-02-10T15:23:28Z
2,238,097
<ul> <li>What does <code>drop_sandbox()</code> do? (The function you gave us is <code>delete_sandbox()</code>) Maybe you meant this to be a recursive function and used the wrong function name</li> <li>Is rootDir a global variable? Perhaps you meant <code>os.chdir("..")</code></li> <li>What does <code>rootDir</code> con...
1
2010-02-10T15:43:02Z
[ "python", "time", "directory", "timestamp" ]
Delete Old directories in Python
2,237,909
<p>I have several directories and I want directories over 7 days old to be deleted. I have code already implemented but It doesn't seem to be working. Can anyone see where I am going wrong?</p> <pre><code>def delete_sandbox(): for directories in os.listdir(os.getcwd()): if not os.path.isdir(directories)...
1
2010-02-10T15:23:28Z
2,238,162
<pre><code>import os import time import shutil numdays = 86400*7 now = time.time() directory=os.path.join("/home","path") for r,d,f in os.walk(directory): for dir in d: timestamp = os.path.getmtime(os.path.join(r,dir)) if now-numdays &gt; timestamp: try: print "remov...
9
2010-02-10T15:51:06Z
[ "python", "time", "directory", "timestamp" ]
Delete Old directories in Python
2,237,909
<p>I have several directories and I want directories over 7 days old to be deleted. I have code already implemented but It doesn't seem to be working. Can anyone see where I am going wrong?</p> <pre><code>def delete_sandbox(): for directories in os.listdir(os.getcwd()): if not os.path.isdir(directories)...
1
2010-02-10T15:23:28Z
24,329,965
<p>While the general idea of the accepted answer is sound the implementation has a few points that are not.</p> <ol> <li>Variable names aren't charged by the character.</li> <li>Please do not overwrite python methods like dir()</li> </ol> <p>Since I needed a bit more functionality I took your idea and built this:</p>...
0
2014-06-20T14:45:45Z
[ "python", "time", "directory", "timestamp" ]
Subtract two dates to give a timedelta
2,238,008
<p>I'm trying to get a value from one of my database values, which will be given by subtracting the purchase date from today's date. I've written my code this way:</p> <pre><code>delta = datetime.now() - item.purchase_date </code></pre> <p>But this gives me this error:</p> <pre><code>unsupported operand type(s) for...
0
2010-02-10T15:33:24Z
2,238,053
<p>you need to use <a href="http://docs.python.org/library/datetime.html#datetime.date.today" rel="nofollow"><code>date.today</code></a> or <code>datetime.now().date()</code> instead of <code>datetime.now</code>:</p> <pre><code>&gt;&gt;&gt; import datetime &gt;&gt;&gt; datetime.date.today() datetime.date(2010, 2, 10) ...
5
2010-02-10T15:37:42Z
[ "python", "datetime" ]
How to verify ECDSA/SHA2 S-MIME signature with python?
2,238,128
<p>We need to choose between two signature schemes:</p> <ul> <li>RSA/SHA2 S-MIME signatures</li> <li>ECDSA/SHA2 S-MIME signatures</li> </ul> <p>For that our python software needs to support one of this scheme. Currently for some political reasons the ECDSA solution is prefered.</p> <p>Is the ECDSA solution supported...
1
2010-02-10T15:46:25Z
2,239,180
<p>Ecliptic Curve Cryptography (ECDSA) as well as the more common RSA is supported by the OpenSSL library. I recommend using the <a href="http://sourceforge.net/projects/pyopenssl/" rel="nofollow">pyOpenSSL</a> bridge. </p>
0
2010-02-10T18:00:29Z
[ "python", "security", "cryptography", "m2crypto" ]
How to verify ECDSA/SHA2 S-MIME signature with python?
2,238,128
<p>We need to choose between two signature schemes:</p> <ul> <li>RSA/SHA2 S-MIME signatures</li> <li>ECDSA/SHA2 S-MIME signatures</li> </ul> <p>For that our python software needs to support one of this scheme. Currently for some political reasons the ECDSA solution is prefered.</p> <p>Is the ECDSA solution supported...
1
2010-02-10T15:46:25Z
2,248,113
<p><a href="http://www.heikkitoivonen.net/m2crypto/api/M2Crypto.EC-module.html" rel="nofollow">ECDSA is supported</a> in <a href="http://chandlerproject.org/Projects/MeTooCrypto" rel="nofollow">M2Crypto</a>, but it can be optionally disabled. For example Fedora-based systems ship with ECDSA disabled in OpenSSL and M2Cr...
1
2010-02-11T22:07:21Z
[ "python", "security", "cryptography", "m2crypto" ]
What is the pythonic way to unpack tuples?
2,238,355
<p>This is ugly. What's a more Pythonic way to do it?</p> <pre><code>import datetime t= (2010, 10, 2, 11, 4, 0, 2, 41, 0) dt = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], t[6]) </code></pre>
55
2010-02-10T16:14:49Z
2,238,361
<p>Generally you can use the <code>func(*tuple)</code> syntax. You can even pass a part of the tuple, which seems like what you're trying to do here:</p> <pre><code>t = (2010, 10, 2, 11, 4, 0, 2, 41, 0) dt = datetime.datetime(*t[0:7]) </code></pre> <p>This is called <em>unpacking</em> a tuple, and can be used for oth...
88
2010-02-10T16:15:52Z
[ "python", "tuples" ]
What is the pythonic way to unpack tuples?
2,238,355
<p>This is ugly. What's a more Pythonic way to do it?</p> <pre><code>import datetime t= (2010, 10, 2, 11, 4, 0, 2, 41, 0) dt = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], t[6]) </code></pre>
55
2010-02-10T16:14:49Z
2,238,362
<p>Refer <a href="https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists" rel="nofollow">https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists</a></p> <pre><code> dt = datetime.datetime(*t[:7]) </code></pre>
8
2010-02-10T16:15:59Z
[ "python", "tuples" ]
How to distuingish between Django's automatically created ManyToMany through-models and manually defined ones?
2,238,504
<p>Say we have models:</p> <pre><code>from django.db import models class AutomaticModel(models.Model): others = models.ManyToManyField('OtherModel') class ManualModel(models.Model): others = models.ManyToManyField('OtherModel', through='ThroughModel') class OtherModel(models.Model): pass class ThroughM...
2
2010-02-10T16:33:16Z
2,243,590
<p>Well, South developers seemed to know it: model is autogenerated if</p> <pre><code># Django 1.0/1.1 (not field.rel.through) or # Django 1.2+ getattr(getattr(field.rel.through, "_meta", None), "auto_created", False) </code></pre> <p>Woohoo!</p>
2
2010-02-11T09:55:18Z
[ "python", "django", "django-models", "many-to-many" ]
simple auth system in django failing
2,238,594
<p>I'm writing a simple auth system to login (and logout) users. The username is an email address, which looks up an email field.</p> <p>I'm using:</p> <pre><code>user = User.objects.get(email__exact=email) # if user obj exists if user: # if authenticate if authenticate(user, email, password): # creat...
0
2010-02-10T16:45:30Z
2,238,628
<p>Use <code>filter()</code> instead of <code>get()</code>.</p>
2
2010-02-10T16:49:38Z
[ "python", "django", "authentication" ]
simple auth system in django failing
2,238,594
<p>I'm writing a simple auth system to login (and logout) users. The username is an email address, which looks up an email field.</p> <p>I'm using:</p> <pre><code>user = User.objects.get(email__exact=email) # if user obj exists if user: # if authenticate if authenticate(user, email, password): # creat...
0
2010-02-10T16:45:30Z
2,240,144
<p>I think this is probably more what you want:</p> <pre><code>try: user = User.objects.get(email__exact=email) if authenticate(user, email, password): request.session['user'] = user return HttpResponseRedirect('/home/') else: return HttpResponseRedirect('/home/login/') except User....
1
2010-02-10T20:32:53Z
[ "python", "django", "authentication" ]
Python's JSON module doesn't use __get__?
2,238,847
<p>When I serialize a list of objects with a custom <code>__get__</code> method, <code>__get__</code> is not called and the raw (unprocessed by custom <code>__get__</code>) value from <code>__set__</code> is used. How does Python's <code>json</code> module iterate over an item? </p> <p>Note: if I iterate over the list...
1
2010-02-10T17:18:49Z
2,239,056
<p>It checks whether the object is certain values, or isinstances of list, tuple, or dicts...</p> <p>It provides a method for what to do if all this fails, and documents how to do this:</p> <pre><code> import simplejson class IterEncoder(simplejson.JSONEncoder): def default(self, o): try: iterable = iter(...
1
2010-02-10T17:41:18Z
[ "python", "json" ]
Python's JSON module doesn't use __get__?
2,238,847
<p>When I serialize a list of objects with a custom <code>__get__</code> method, <code>__get__</code> is not called and the raw (unprocessed by custom <code>__get__</code>) value from <code>__set__</code> is used. How does Python's <code>json</code> module iterate over an item? </p> <p>Note: if I iterate over the list...
1
2010-02-10T17:18:49Z
2,752,432
<p>From the Python 3.1.2 documentation (re-formatted for viewing here but otherwise unedited):</p> <pre><code>json.dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, **kw) </code></pre> <blockquote> <p><em>d...
0
2010-05-02T04:18:08Z
[ "python", "json" ]
How do I properly work with unicode characters in python to keep from getting errors?
2,239,017
<p>I'm working on a python plugin for Google Quick Search Box, and it's doing some odd things with non-ascii characters. It seems like the code works fine up until I try constructing a string containing the non-ascii characters (ü has been my test character). I am using the following code snippet for the construction,...
1
2010-02-10T17:36:50Z
2,239,105
<p>This is a bit beyond my expertise, but I think <code># -*- coding: iso-8859-15 -*-</code> at the top declares the text encoding that your Python source file is saved in.</p> <p>Is it really saved in iso-8859-15?</p>
0
2010-02-10T17:49:18Z
[ "python", "unicode", "encoding", "ascii" ]
How do I properly work with unicode characters in python to keep from getting errors?
2,239,017
<p>I'm working on a python plugin for Google Quick Search Box, and it's doing some odd things with non-ascii characters. It seems like the code works fine up until I try constructing a string containing the non-ascii characters (ü has been my test character). I am using the following code snippet for the construction,...
1
2010-02-10T17:36:50Z
2,239,137
<p>I guess you're using Python 2.x.</p> <p>The file encoding declaration specifies how string <em>literals</em> are read by the interpreter.</p> <p>You should handle all strings as <code>unicode</code> values, not <code>str</code> ones. If you read a <code>str</code> from the outside world, you should decode it to <c...
1
2010-02-10T17:53:32Z
[ "python", "unicode", "encoding", "ascii" ]
How do I properly work with unicode characters in python to keep from getting errors?
2,239,017
<p>I'm working on a python plugin for Google Quick Search Box, and it's doing some odd things with non-ascii characters. It seems like the code works fine up until I try constructing a string containing the non-ascii characters (ü has been my test character). I am using the following code snippet for the construction,...
1
2010-02-10T17:36:50Z
2,239,162
<p>There are a few things you should do to fix this.</p> <ol> <li><p>Convert all string literal that contain non-ASCII characters to Unicode literals. Example: <code>u'über'</code>.</p></li> <li><p>Do intermediate processing on Unicode. In other words, if you receive an encoded string (no matter the encoding), decode...
4
2010-02-10T17:57:18Z
[ "python", "unicode", "encoding", "ascii" ]
Dynamically update ModelForm’s Meta class model field
2,239,096
<pre><code>def SiteAdminForm(model_cls, *args, **kwargs): class MerchantAdminForm(forms.ModelForm): class Meta: exclude = ('external_links', 'published', 'logo','image_zip_file',) model = model_cls def __init__(self, *args, **kwargs): super(MerchantAdminForm, sel...
2
2010-02-10T17:47:51Z
2,239,473
<p>That's because your <code>merchant.__class__</code> is also <code>django.db.models.base.ModelBase</code>. If you just try to print merchant, then you will get proper class path (I get <code>app.models.TestModel</code>), but <strong>class</strong> is BaseModel.</p> <p>I believe that's because of metaclasses use. Pro...
0
2010-02-10T18:51:21Z
[ "python", "django", "dynamic", "model", "modelform" ]
Dynamically update ModelForm’s Meta class model field
2,239,096
<pre><code>def SiteAdminForm(model_cls, *args, **kwargs): class MerchantAdminForm(forms.ModelForm): class Meta: exclude = ('external_links', 'published', 'logo','image_zip_file',) model = model_cls def __init__(self, *args, **kwargs): super(MerchantAdminForm, sel...
2
2010-02-10T17:47:51Z
2,239,661
<p>Try to use like this:</p> <pre><code>merchant_form = SiteAdminForm(merchant.model, instance=merchant) </code></pre> <p>One suggestion is to use the Python shell (in django case, ./manage.py shell) to inspect attributes and methods of an object with the built-in dir() function. </p>
0
2010-02-10T19:22:52Z
[ "python", "django", "dynamic", "model", "modelform" ]
Dynamically update ModelForm’s Meta class model field
2,239,096
<pre><code>def SiteAdminForm(model_cls, *args, **kwargs): class MerchantAdminForm(forms.ModelForm): class Meta: exclude = ('external_links', 'published', 'logo','image_zip_file',) model = model_cls def __init__(self, *args, **kwargs): super(MerchantAdminForm, sel...
2
2010-02-10T17:47:51Z
2,243,226
<p>I'm an idiot, slept on it, looked at it again and figured it out almost immediately!!!!</p> <p><code>return MerchantAdminForm()</code> should have been: <code>return MerchantAdminForm(*args, **kwargs)</code></p>
0
2010-02-11T08:49:01Z
[ "python", "django", "dynamic", "model", "modelform" ]
Dynamically update ModelForm’s Meta class model field
2,239,096
<pre><code>def SiteAdminForm(model_cls, *args, **kwargs): class MerchantAdminForm(forms.ModelForm): class Meta: exclude = ('external_links', 'published', 'logo','image_zip_file',) model = model_cls def __init__(self, *args, **kwargs): super(MerchantAdminForm, sel...
2
2010-02-10T17:47:51Z
20,703,656
<pre><code>def get_form(model_class): class DynamoForm(forms.ModelForm): class Meta: model = model_class return DynamoForm form_set = get_form(ActualModel)(request.POST) </code></pre> <p>is that what you want ?</p>
0
2013-12-20T12:28:06Z
[ "python", "django", "dynamic", "model", "modelform" ]
Python win32com: Internet Explorer COM object ? (used to work?)
2,239,199
<p>I have this very simple program:</p> <pre><code>from win32com import client ie=client.Dispatch("InternetExplorer.Application") </code></pre> <p>This used to work (I think I broke something when I re-used 'makepy.py' to try and add in constants for IE).</p> <p>It still works on another machine where I haven't been...
1
2010-02-10T18:03:14Z
2,239,642
<p>Went into here:</p> <pre><code>Python26\Lib\site-packages\win32com\gen_py </code></pre> <p>Renamed the .py and .pyc file to .py_ and .pyc_ files :</p> <pre><code>85CC894D-5673-4868-9A22-9E15B7E694D3x0x1x1.pyc </code></pre> <p>Restarted Python: now get the Internet Explorer. phew...</p>
1
2010-02-10T19:19:58Z
[ "python", "com", "winapi", "automation", "internet-explorer" ]
Python-based document metadata parser?
2,239,459
<p>Does anyone know a good parser for document metadata in python for unix like systems. In Java, <a href="http://lucene.apache.org/tika/" rel="nofollow">apache tika</a> is great.</p> <p>No com ... please :)</p> <p>Thanks</p>
3
2010-02-10T18:47:45Z
2,239,484
<p>If you like tika, you could always use <a href="http://www.jython.org/" rel="nofollow">Jython</a> so you can reference tika directly.</p>
1
2010-02-10T18:53:30Z
[ "python", "parsing" ]
Python-based document metadata parser?
2,239,459
<p>Does anyone know a good parser for document metadata in python for unix like systems. In Java, <a href="http://lucene.apache.org/tika/" rel="nofollow">apache tika</a> is great.</p> <p>No com ... please :)</p> <p>Thanks</p>
3
2010-02-10T18:47:45Z
2,303,565
<p>hachoir_metadata works great with excel documents <a href="http://bitbucket.org/haypo/hachoir/wiki/Home" rel="nofollow">http://bitbucket.org/haypo/hachoir/wiki/Home</a></p>
0
2010-02-20T20:17:06Z
[ "python", "parsing" ]
Python-based document metadata parser?
2,239,459
<p>Does anyone know a good parser for document metadata in python for unix like systems. In Java, <a href="http://lucene.apache.org/tika/" rel="nofollow">apache tika</a> is great.</p> <p>No com ... please :)</p> <p>Thanks</p>
3
2010-02-10T18:47:45Z
3,431,939
<p>You don't have to use Jython to use Tika. You can call Java from Python using <a href="http://pypi.python.org/pypi/JCC/2.6" rel="nofollow">JCC</a>. You can find decent instructions for this <a href="http://redmine.djity.net/projects/pythontika/wiki/Wiki" rel="nofollow">here</a>. </p> <p>When installing JCC you'l...
3
2010-08-07T20:31:13Z
[ "python", "parsing" ]
Python-based document metadata parser?
2,239,459
<p>Does anyone know a good parser for document metadata in python for unix like systems. In Java, <a href="http://lucene.apache.org/tika/" rel="nofollow">apache tika</a> is great.</p> <p>No com ... please :)</p> <p>Thanks</p>
3
2010-02-10T18:47:45Z
9,097,086
<p>Tika seems like a great option. It's the only tool I've found (apart from OpenOffice in server mode) which supports old-style XLS files. I've done some work on making it easier to integrate Tika into a Python project, which you can find in <a href="http://blog.aptivate.org/2012/02/01/content-indexing-in-django-using...
1
2012-02-01T14:05:16Z
[ "python", "parsing" ]
Python: simple CLI GUI
2,239,476
<p>A simple question on a python module. Let's say I have the following code:</p> <pre><code>for i in range(1000): print i </code></pre> <p>It'll output something along the lines of:</p> <blockquote> <p>1</p> <p>2</p> <p>'Snip'</p> <p>999</p> </blockquote> <p>Is it possible to have the program ou...
0
2010-02-10T18:51:38Z
2,239,504
<p>If you want to draw a GUI inside a terminal, you'll have to use the <a href="http://docs.python.org/library/curses.html" rel="nofollow">curses module</a>.</p>
3
2010-02-10T18:58:01Z
[ "python" ]