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
Escape SQL "LIKE" value for Postgres with psycopg2
2,106,207
<p>Does psycopg2 have a function for escaping the value of a <strong>LIKE</strong> operand for Postgres?</p> <p>For example I may want to match strings that start with the string "20% of all", so I want to write something like this:</p> <pre><code>sql = '... WHERE ... LIKE %(myvalue)s' cursor.fetchall(sql, { 'myvalue...
22
2010-01-21T00:58:54Z
2,106,294
<p>Having failed to find a built-in function so far, the one I wrote is pretty simple:</p> <pre><code>def escape_sql_like(s): return s.replace('\\', '\\\\').replace('%', '\\%').replace('_', '\\_') </code></pre>
0
2010-01-21T01:22:11Z
[ "python", "postgresql", "psycopg2", "python-db-api" ]
Escape SQL "LIKE" value for Postgres with psycopg2
2,106,207
<p>Does psycopg2 have a function for escaping the value of a <strong>LIKE</strong> operand for Postgres?</p> <p>For example I may want to match strings that start with the string "20% of all", so I want to write something like this:</p> <pre><code>sql = '... WHERE ... LIKE %(myvalue)s' cursor.fetchall(sql, { 'myvalue...
22
2010-01-21T00:58:54Z
2,106,443
<p>Yeah, this is a real mess. Both MySQL and PostgreSQL use backslash-escapes for this by default. This is a terrible pain if you're also escaping the string again with backslashes instead of using parameterisation, and it's also incorrect according to ANSI SQL:1992, which says there are by default no extra escape char...
19
2010-01-21T02:04:04Z
[ "python", "postgresql", "psycopg2", "python-db-api" ]
Escape SQL "LIKE" value for Postgres with psycopg2
2,106,207
<p>Does psycopg2 have a function for escaping the value of a <strong>LIKE</strong> operand for Postgres?</p> <p>For example I may want to match strings that start with the string "20% of all", so I want to write something like this:</p> <pre><code>sql = '... WHERE ... LIKE %(myvalue)s' cursor.fetchall(sql, { 'myvalue...
22
2010-01-21T00:58:54Z
2,272,221
<p>You can create a <code>Like</code> class subclassing <code>str</code> and <a href="http://initd.org/psycopg/docs/advanced.html#adapting-new-types" rel="nofollow">register an adapter for it</a> to have it converted in the right like syntax (e.g. using the <code>escape_sql_like()</code> you wrote).</p>
0
2010-02-16T10:45:18Z
[ "python", "postgresql", "psycopg2", "python-db-api" ]
Escape SQL "LIKE" value for Postgres with psycopg2
2,106,207
<p>Does psycopg2 have a function for escaping the value of a <strong>LIKE</strong> operand for Postgres?</p> <p>For example I may want to match strings that start with the string "20% of all", so I want to write something like this:</p> <pre><code>sql = '... WHERE ... LIKE %(myvalue)s' cursor.fetchall(sql, { 'myvalue...
22
2010-01-21T00:58:54Z
2,955,348
<p>I wonder if all of the above is really needed. I am using psycopg2 and was simply able to use:</p> <pre><code>data_dict['like'] = psycopg2.Binary('%'+ match_string +'%') cursor.execute("SELECT * FROM some_table WHERE description ILIKE %(like)s;", data_dict) </code></pre>
1
2010-06-02T06:03:40Z
[ "python", "postgresql", "psycopg2", "python-db-api" ]
Escape SQL "LIKE" value for Postgres with psycopg2
2,106,207
<p>Does psycopg2 have a function for escaping the value of a <strong>LIKE</strong> operand for Postgres?</p> <p>For example I may want to match strings that start with the string "20% of all", so I want to write something like this:</p> <pre><code>sql = '... WHERE ... LIKE %(myvalue)s' cursor.fetchall(sql, { 'myvalue...
22
2010-01-21T00:58:54Z
6,936,321
<p>I made some modifications to the code above to do the following:</p> <pre><code>def escape_sql_like(SQL): return SQL.replace("'%", 'PERCENTLEFT').replace("%'", 'PERCENTRIGHT') def reescape_sql_like(SQL): return SQL.replace('PERCENTLEFT', "'%").replace('PERCENTRIGHT', "%'") SQL = "SELECT blah LIKE '%OUCH%'...
0
2011-08-04T04:48:28Z
[ "python", "postgresql", "psycopg2", "python-db-api" ]
Escape SQL "LIKE" value for Postgres with psycopg2
2,106,207
<p>Does psycopg2 have a function for escaping the value of a <strong>LIKE</strong> operand for Postgres?</p> <p>For example I may want to match strings that start with the string "20% of all", so I want to write something like this:</p> <pre><code>sql = '... WHERE ... LIKE %(myvalue)s' cursor.fetchall(sql, { 'myvalue...
22
2010-01-21T00:58:54Z
14,850,840
<p>You can also look at this problem from a different angle. What do you want? You want a query that for <em>any</em> string argument executes a LIKE by appending a '%' to the argument. A nice way to express that, without resorting to functions and psycopg2 extensions could be:</p> <pre><code>sql = "... WHERE ... LIKE...
1
2013-02-13T10:01:11Z
[ "python", "postgresql", "psycopg2", "python-db-api" ]
Escape SQL "LIKE" value for Postgres with psycopg2
2,106,207
<p>Does psycopg2 have a function for escaping the value of a <strong>LIKE</strong> operand for Postgres?</p> <p>For example I may want to match strings that start with the string "20% of all", so I want to write something like this:</p> <pre><code>sql = '... WHERE ... LIKE %(myvalue)s' cursor.fetchall(sql, { 'myvalue...
22
2010-01-21T00:58:54Z
21,610,562
<p>Instead of escaping the percent character, you could instead make use of PostgreSQL's regex implementation.</p> <p>For example, the following query against the system catalogs will provide a list of active queries which are not from the autovacuuming sub-system:</p> <pre><code>SELECT procpid, current_query FROM pg...
2
2014-02-06T18:02:26Z
[ "python", "postgresql", "psycopg2", "python-db-api" ]
Can you separate python projects logically into separate files/classes like in C#/Java?
2,106,271
<p>I'm looking to develop a project in python and all of the python I have done is minor scripting with no regard to classes or structure. I haven't seen much about this, so is this how larger python projects are done? </p> <p>Also, do things like "namespaces" and "projects" exist in this realm? As well as object o...
2
2010-01-21T01:16:22Z
2,106,288
<p>You can do that, but usually they are arranged a bit different.</p> <p>You can take a look to the source code of one python application. </p> <p>Here's one: "<a href="http://code.google.com/p/jaikuengine/source/browse/" rel="nofollow">JaikuEngine</a>" which powers the website <a href="http://www.jaiku.com/" rel="...
0
2010-01-21T01:21:01Z
[ "python" ]
Can you separate python projects logically into separate files/classes like in C#/Java?
2,106,271
<p>I'm looking to develop a project in python and all of the python I have done is minor scripting with no regard to classes or structure. I haven't seen much about this, so is this how larger python projects are done? </p> <p>Also, do things like "namespaces" and "projects" exist in this realm? As well as object o...
2
2010-01-21T01:16:22Z
2,106,290
<p>Yes, you can, and you should! :)</p> <p>Here is a nice introduction to <a href="http://docs.python.org/tutorial/modules.html" rel="nofollow">Python Modules</a> (including packages).</p> <hr> <p>Correction: you probably should not put each single class into a separate file (like Java mandates and many C++ places d...
2
2010-01-21T01:21:21Z
[ "python" ]
Can you separate python projects logically into separate files/classes like in C#/Java?
2,106,271
<p>I'm looking to develop a project in python and all of the python I have done is minor scripting with no regard to classes or structure. I haven't seen much about this, so is this how larger python projects are done? </p> <p>Also, do things like "namespaces" and "projects" exist in this realm? As well as object o...
2
2010-01-21T01:16:22Z
2,106,293
<p>You can put code (classes, function defs, etc) into python modules (individual source files), which are then imported with <code>import</code>. Typically like functionality (like in the Python standard library) is contained within a single module.</p>
1
2010-01-21T01:22:09Z
[ "python" ]
Can you separate python projects logically into separate files/classes like in C#/Java?
2,106,271
<p>I'm looking to develop a project in python and all of the python I have done is minor scripting with no regard to classes or structure. I haven't seen much about this, so is this how larger python projects are done? </p> <p>Also, do things like "namespaces" and "projects" exist in this realm? As well as object o...
2
2010-01-21T01:16:22Z
2,106,299
<p>Yes. </p> <p>You can put <a href="http://docs.python.org/tutorial/classes.html" rel="nofollow">python classes</a> into separate files, use <a href="http://docs.python.org/tutorial/classes.html#python-scopes-and-namespaces" rel="nofollow">namespaces for scoping</a>, then place this into <a href="http://docs.python....
0
2010-01-21T01:23:11Z
[ "python" ]
Pythonic way to create a numpy array from a list of numpy arrays
2,106,287
<p>I generate a list of one dimensional numpy arrays in a loop and later convert this list to a 2d numpy array. I would've preallocated a 2d numpy array if i knew the number of items ahead of time, but I don't, therefore I put everything in a list.</p> <p>The mock up is below:</p> <pre><code>&gt;&gt;&gt; list_of_arra...
17
2010-01-21T01:21:05Z
2,106,355
<p>What you are doing is the standard way. A property of numpy arrays is that they need contiguous memory. The only possibility of "holes" that I can think of is possible with the <code>strides</code> member of <code>PyArrayObject</code>, but that doesn't affect the discussion here. Since numpy arrays have contiguou...
2
2010-01-21T01:38:00Z
[ "python", "performance", "arrays", "numpy", "scipy" ]
Pythonic way to create a numpy array from a list of numpy arrays
2,106,287
<p>I generate a list of one dimensional numpy arrays in a loop and later convert this list to a 2d numpy array. I would've preallocated a 2d numpy array if i knew the number of items ahead of time, but I don't, therefore I put everything in a list.</p> <p>The mock up is below:</p> <pre><code>&gt;&gt;&gt; list_of_arra...
17
2010-01-21T01:21:05Z
2,106,651
<p>Suppose you know that the final array <code>arr</code> will never be larger than 5000x10. Then you could pre-allocate an array of maximum size, populate it with data as you go through the loop, and then use <code>arr.resize</code> to cut it down to the discovered size after exiting the loop.</p> <p>The tests below ...
12
2010-01-21T03:09:39Z
[ "python", "performance", "arrays", "numpy", "scipy" ]
Pythonic way to create a numpy array from a list of numpy arrays
2,106,287
<p>I generate a list of one dimensional numpy arrays in a loop and later convert this list to a 2d numpy array. I would've preallocated a 2d numpy array if i knew the number of items ahead of time, but I don't, therefore I put everything in a list.</p> <p>The mock up is below:</p> <pre><code>&gt;&gt;&gt; list_of_arra...
17
2010-01-21T01:21:05Z
2,107,433
<p>I'll add my own version of ~unutbu's answer. Similar to numpy_all_the way, but you dynamically resize if you have an index error. I thought it would have been a little faster for small data sets, but it's a little slower - the bounds checking slows things down too much.</p> <pre><code>initial_guess = 1000 def my_n...
2
2010-01-21T06:58:28Z
[ "python", "performance", "arrays", "numpy", "scipy" ]
Pythonic way to create a numpy array from a list of numpy arrays
2,106,287
<p>I generate a list of one dimensional numpy arrays in a loop and later convert this list to a 2d numpy array. I would've preallocated a 2d numpy array if i knew the number of items ahead of time, but I don't, therefore I put everything in a list.</p> <p>The mock up is below:</p> <pre><code>&gt;&gt;&gt; list_of_arra...
17
2010-01-21T01:21:05Z
34,830,765
<p>Convenient way, using <a href="http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.concatenate.html" rel="nofollow"><code>numpy.concatenate</code></a>. I believe it's also faster, than @unutbu's answer:</p> <pre><code>In [32]: import numpy as np In [33]: list_of_arrays = list(map(lambda x: x * np.one...
0
2016-01-16T18:49:43Z
[ "python", "performance", "arrays", "numpy", "scipy" ]
Passing C function pointers between two python modules
2,106,324
<p>I'm writing an application working with plugins. There are two types of plugins: <code>Engine</code> and <code>Model</code>. <code>Engine</code> objects have an <code>update()</code> method that call the <code>Model.velocity()</code> method.</p> <p>For performance reasons these methods are <em>allowed</em> to be wr...
0
2010-01-21T01:29:53Z
2,106,391
<p>The CObject (PyCOBject) data type exists for this purpose. It holds a void*, but you can store any data you wish. You do have to be careful not to pass the wrong CObject to the wrong functions, as some other library's CObjects will look just like your own.</p> <p>If you want more type security, you could easily rol...
1
2010-01-21T01:50:11Z
[ "python", "c", "function-pointers" ]
pydev issue with gobject
2,106,349
<p>It seems that Pydev (1.5.4) on Eclipse (3.5.1) with Python 2.6 isn't able to correctly cross-reference the package <code>gobject</code>. Putting <code>import gobject</code> works OK but any more than that (e.g. <code>class X(gobject.GObject)</code> causes Pydev to report "unresolved reference" errors.</p> <p>What ...
1
2010-01-21T01:37:12Z
2,106,460
<p>The issue is related to <a href="http://pydev.org/faq.html#i_have_a_library_installed_and_pydev_does_not_find" rel="nofollow">this limitation of PyDev</a>:</p> <blockquote> <p>I have a library installed and Pydev does not find it</p> <p>Well, problems have been reported on Mac and Linux, and the main rea...
1
2010-01-21T02:09:15Z
[ "python", "linux", "pydev", "gobject" ]
Python win32 service
2,106,366
<p>I am fairly new to python, and have no experience with writing services for windows. I have tried to hack together a windows service based on afew tutorials i have found out there.</p> <p>I need this service to constantly monitor a directory for changes and when it sees a change it runs a script. Here is what i hav...
1
2010-01-21T01:42:48Z
2,106,687
<p>I'm not familiar with pywin32's <code>ServiceFramework</code>, but based on your code and the error message, I bet you have a problem with <code>path_to_watch</code>.</p> <p>I assume you sanitized the folder name for your example, and you're not actually looking for <code>folder</code>.</p> <p>How about:</p> <ul>...
1
2010-01-21T03:21:43Z
[ "python", "winapi", "service", "pywin32" ]
Python win32 service
2,106,366
<p>I am fairly new to python, and have no experience with writing services for windows. I have tried to hack together a windows service based on afew tutorials i have found out there.</p> <p>I need this service to constantly monitor a directory for changes and when it sees a change it runs a script. Here is what i hav...
1
2010-01-21T01:42:48Z
2,106,731
<p>One thing to keep in mind with Windows services is that by default, they run under the LOCAL_SYSTEM account. This means that permissions to local volumes apply (LOCAL_SYSTEM can certainly be denied access to any given folder), and that LOCAL_SYSTEM does not have any access to any network volumes.</p>
2
2010-01-21T03:29:35Z
[ "python", "winapi", "service", "pywin32" ]
CherryPy changes my response code
2,106,377
<p>In my python application using mod_wsgi and cherrypy ontop of Apache my response code get changed to a 500 from a 403. I am explicitly setting this to 403. </p> <p>i.e. cherrypy.response.status = 403</p> <p>I do not understand where and why the response code that the client receives is 500. Does anyone have any...
3
2010-01-21T01:46:06Z
2,106,456
<p>The HTTP 500 error is used for internal server errors. Something in the server or your application is likely throwing an exception, so no matter what you set the response code to be before this, CherryPy will send a 500 back.</p> <p>You can look into whatever tools CherryPy includes for debugging or logging (I'm n...
1
2010-01-21T02:07:31Z
[ "python", "apache", "mod-wsgi", "cherrypy" ]
Handling a Python exception that occurs within an except clause
2,106,475
<p>I have some code in a Python <code>except</code> clause that is intended to do some logging, but the logging code might itself cause an exception. In my case, I'd like to just ignore any second exception that might occur, and raise the original exception. Here is a very simplified example:</p> <pre><code>try: a...
9
2010-01-21T02:14:32Z
2,106,490
<p>With abstraction:</p> <pre><code>def log_it(): try: 1 / 0 except: pass try: this = that except: log_it() raise </code></pre> <p>Does what you want in Python 2.5</p> <p>Another way to do it is to store the exception in a variable, then re-raise it explicitly:</p> <pre><code>tr...
10
2010-01-21T02:18:43Z
[ "python", "exception" ]
Handling a Python exception that occurs within an except clause
2,106,475
<p>I have some code in a Python <code>except</code> clause that is intended to do some logging, but the logging code might itself cause an exception. In my case, I'd like to just ignore any second exception that might occur, and raise the original exception. Here is a very simplified example:</p> <pre><code>try: a...
9
2010-01-21T02:14:32Z
2,106,518
<p>I believe what you're seeing is the result of <a href="http://www.python.org/dev/peps/pep-3134/">exception chaining</a>, which is a <a href="http://docs.python.org/3.1/whatsnew/3.0.html#changes-to-exceptions">change in Python 3</a>.</p> <p>From the <em>Motivation</em> section of the PEP:</p> <blockquote> <p>Duri...
13
2010-01-21T02:29:38Z
[ "python", "exception" ]
Handling a Python exception that occurs within an except clause
2,106,475
<p>I have some code in a Python <code>except</code> clause that is intended to do some logging, but the logging code might itself cause an exception. In my case, I'd like to just ignore any second exception that might occur, and raise the original exception. Here is a very simplified example:</p> <pre><code>try: a...
9
2010-01-21T02:14:32Z
12,279,238
<p>In my <a href="http://code.activestate.com/recipes/578252-python-exception-chains-or-trees/?in=user-4182236" rel="nofollow">CausedException class</a> I take care of that for Python 2.x (and for Python 3 as well, in case you want to pass cause trees instead of simple cause chains). Maybe it can help you.</p>
0
2012-09-05T10:19:11Z
[ "python", "exception" ]
Error when adding a ManyToManyField in Django
2,106,491
<p>Ok, so I posted a question recently regarding an error when adding a <strong>ManyToManyField</strong></p> <p>The Models are the ones below</p> <pre><code>class MagicType(models.Model): name = models.CharField(max_length=155) parent = models.ForeignKey('self', null=True, blank=True) class Spell(models.M...
3
2010-01-21T02:18:36Z
2,106,540
<p>This is more or less the way I would have done it. In fact, I was puzzled about why this didn't work, and tried it myself in a clean Django 1.1 environment -- it works swimmingly.</p> <p>Have you tried putting this model in a clean environment yourself and seeing what you get?</p>
1
2010-01-21T02:36:16Z
[ "python", "django", "django-models", "django-orm", "django-evolution" ]
Error when adding a ManyToManyField in Django
2,106,491
<p>Ok, so I posted a question recently regarding an error when adding a <strong>ManyToManyField</strong></p> <p>The Models are the ones below</p> <pre><code>class MagicType(models.Model): name = models.CharField(max_length=155) parent = models.ForeignKey('self', null=True, blank=True) class Spell(models.M...
3
2010-01-21T02:18:36Z
2,107,461
<p>Same thing, i answer your another question <a href="http://stackoverflow.com/questions/2098696">http://stackoverflow.com/questions/2098696</a> basicly this error is because your code in models (ORM) change but your database isn't, and django-evolution doesn't fix many problems with changes in the database, i suggest...
3
2010-01-21T07:05:45Z
[ "python", "django", "django-models", "django-orm", "django-evolution" ]
Dynamically create image thumbnails (using django)
2,106,507
<p>I would like to dynamically create thumbnails based on parameters in the URL. For example, <code>http://mysite.com/images/1234/120x45.jpg</code> would create a <code>120x45</code> thumbnail for image id <code>1234</code>.</p> <p>The obvious solution to this is to have a django view which does the following:</p> <o...
7
2010-01-21T02:26:14Z
2,106,556
<p>Check out django imagekit. <a href="http://bitbucket.org/jdriscoll/django-imagekit/wiki/Home" rel="nofollow">http://bitbucket.org/jdriscoll/django-imagekit/wiki/Home</a></p>
2
2010-01-21T02:39:40Z
[ "python", "django", "thumbnails" ]
Dynamically create image thumbnails (using django)
2,106,507
<p>I would like to dynamically create thumbnails based on parameters in the URL. For example, <code>http://mysite.com/images/1234/120x45.jpg</code> would create a <code>120x45</code> thumbnail for image id <code>1234</code>.</p> <p>The obvious solution to this is to have a django view which does the following:</p> <o...
7
2010-01-21T02:26:14Z
2,106,596
<p>You can also try <a href="https://github.com/sorl/sorl-thumbnail/" rel="nofollow">sorl</a>, it is being used by <a href="http://satchmoproject.com/" rel="nofollow">Satchmo</a>.</p>
4
2010-01-21T02:54:54Z
[ "python", "django", "thumbnails" ]
Dynamically create image thumbnails (using django)
2,106,507
<p>I would like to dynamically create thumbnails based on parameters in the URL. For example, <code>http://mysite.com/images/1234/120x45.jpg</code> would create a <code>120x45</code> thumbnail for image id <code>1234</code>.</p> <p>The obvious solution to this is to have a django view which does the following:</p> <o...
7
2010-01-21T02:26:14Z
2,106,612
<p>You don't have to use Django to serve the static content directly. Simply have your server route 404 requests for your images folder to a Django view, where it splits apart the filename and generates the appropriate thumbnail, before redirecting back to the original URL (which hopefully will no longer be a 404).</p...
8
2010-01-21T02:58:22Z
[ "python", "django", "thumbnails" ]
Dynamically create image thumbnails (using django)
2,106,507
<p>I would like to dynamically create thumbnails based on parameters in the URL. For example, <code>http://mysite.com/images/1234/120x45.jpg</code> would create a <code>120x45</code> thumbnail for image id <code>1234</code>.</p> <p>The obvious solution to this is to have a django view which does the following:</p> <o...
7
2010-01-21T02:26:14Z
2,144,676
<p>You can take a peek at <a href="http://sorl-thumbnail.readthedocs.org/en/latest/" rel="nofollow">the sorl-thumbnail documentation</a>. I use it in almost all of my projects together with serving static content with Nginx from the <code>/media/</code> dir :)</p>
2
2010-01-27T04:58:27Z
[ "python", "django", "thumbnails" ]
Dynamically create image thumbnails (using django)
2,106,507
<p>I would like to dynamically create thumbnails based on parameters in the URL. For example, <code>http://mysite.com/images/1234/120x45.jpg</code> would create a <code>120x45</code> thumbnail for image id <code>1234</code>.</p> <p>The obvious solution to this is to have a django view which does the following:</p> <o...
7
2010-01-21T02:26:14Z
9,103,345
<p>I looked up these answers and even though they work they were just too sofisticated for me. If you just want a quick and dirty way to get thumbnails, just using the PIL library might be a good call, this is my code:</p> <pre><code>file, ext = os.path.splitext('image.jpg') im = Image.open('/full/path/to/image.jpg') ...
1
2012-02-01T21:05:13Z
[ "python", "django", "thumbnails" ]
Dynamically create image thumbnails (using django)
2,106,507
<p>I would like to dynamically create thumbnails based on parameters in the URL. For example, <code>http://mysite.com/images/1234/120x45.jpg</code> would create a <code>120x45</code> thumbnail for image id <code>1234</code>.</p> <p>The obvious solution to this is to have a django view which does the following:</p> <o...
7
2010-01-21T02:26:14Z
11,430,282
<p>Check out this discussion page about thumbnail contribution for Django:</p> <p><a href="https://code.djangoproject.com/wiki/ThumbNails" rel="nofollow">https://code.djangoproject.com/wiki/ThumbNails</a></p>
0
2012-07-11T10:08:16Z
[ "python", "django", "thumbnails" ]
Dynamically create image thumbnails (using django)
2,106,507
<p>I would like to dynamically create thumbnails based on parameters in the URL. For example, <code>http://mysite.com/images/1234/120x45.jpg</code> would create a <code>120x45</code> thumbnail for image id <code>1234</code>.</p> <p>The obvious solution to this is to have a django view which does the following:</p> <o...
7
2010-01-21T02:26:14Z
11,804,207
<p>See this application to generate thumbs.</p> <p>Developed in Python, using tornadoweb server.</p> <p><a href="https://github.com/globocom/thumbor" rel="nofollow">https://github.com/globocom/thumbor</a></p> <pre><code>pip install thumbor </code></pre> <p>Great Application</p>
4
2012-08-03T22:38:32Z
[ "python", "django", "thumbnails" ]
Dynamically create image thumbnails (using django)
2,106,507
<p>I would like to dynamically create thumbnails based on parameters in the URL. For example, <code>http://mysite.com/images/1234/120x45.jpg</code> would create a <code>120x45</code> thumbnail for image id <code>1234</code>.</p> <p>The obvious solution to this is to have a django view which does the following:</p> <o...
7
2010-01-21T02:26:14Z
13,129,324
<p>I had the same exact problem: in a high-traffic site, generating dozens of thumbnails at response time is not feasible, as is generating thumbnails for all past content in batch, so I created an app for that. Check it out: <a href="https://github.com/hcarvalhoalves/django-rest-thumbnails" rel="nofollow">https://gith...
0
2012-10-29T20:42:00Z
[ "python", "django", "thumbnails" ]
Dynamically create image thumbnails (using django)
2,106,507
<p>I would like to dynamically create thumbnails based on parameters in the URL. For example, <code>http://mysite.com/images/1234/120x45.jpg</code> would create a <code>120x45</code> thumbnail for image id <code>1234</code>.</p> <p>The obvious solution to this is to have a django view which does the following:</p> <o...
7
2010-01-21T02:26:14Z
16,474,213
<p>Thumbor indeed is a great service, you can use django-thumbor on django.</p> <pre><code>pip install django-thumbor </code></pre> <p>Repo here: <a href="https://github.com/ricobl/django-thumbor" rel="nofollow">https://github.com/ricobl/django-thumbor</a></p>
0
2013-05-10T02:25:42Z
[ "python", "django", "thumbnails" ]
how can i active my account,i send email to me use django-registration
2,106,559
<pre><code>Thank you for registering an account at example.com. To activate your registration, please visit the following page: http://example.com/activate/d5544f645c80f8a6c9af934c03a2ee2d7902dc1f/ This page will expire in 7 days. </code></pre> <p>example.com?????</p> <p>i click the url,but not active my account.</...
-2
2010-01-21T02:40:17Z
2,106,574
<p>Are you running this in your local or remote environment? Of course you can't activate it because the activation URL points to example.com, which is used as an example. You need to point it to your own domain.</p>
0
2010-01-21T02:47:02Z
[ "python", "django", "registration" ]
how can i active my account,i send email to me use django-registration
2,106,559
<pre><code>Thank you for registering an account at example.com. To activate your registration, please visit the following page: http://example.com/activate/d5544f645c80f8a6c9af934c03a2ee2d7902dc1f/ This page will expire in 7 days. </code></pre> <p>example.com?????</p> <p>i click the url,but not active my account.</...
-2
2010-01-21T02:40:17Z
2,106,636
<p>Googling produces this: <a href="http://codespatter.com/2009/01/05/django-settings-site-domain-examplecom/" rel="nofollow">http://codespatter.com/2009/01/05/django-settings-site-domain-examplecom/</a></p> <blockquote> <p>You can change it through the Django admin interface, phpMyAdmin, or however you feel comfort...
2
2010-01-21T03:06:39Z
[ "python", "django", "registration" ]
python, regex to find anchor link html
2,106,597
<p>I need a regex in python to find a links html in a larger set of html. </p> <p>so if I have:</p> <pre><code>&lt;ul class="something"&gt; &lt;li id="li_id"&gt; &lt;a href="#" title="myurl"&gt;URL Text&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre> <p>I would get back:</p> <pre><code>&lt;a href="#" title="myur...
0
2010-01-21T02:55:05Z
2,106,630
<p>you really shouldn't use regexes to parse html.. ever. </p> <p>try beautifulsoup or lxml.</p> <p>but... you asked. so a quick and naive version might look like this:</p> <pre><code>import re html = """ &lt;ul class="something"&gt; &lt;li id="li_id"&gt; &lt;a href="#" title="myurl"&gt;URL Text&lt;/a&gt; &lt;/li&...
3
2010-01-21T03:04:34Z
[ "python", "regex" ]
python, regex to find anchor link html
2,106,597
<p>I need a regex in python to find a links html in a larger set of html. </p> <p>so if I have:</p> <pre><code>&lt;ul class="something"&gt; &lt;li id="li_id"&gt; &lt;a href="#" title="myurl"&gt;URL Text&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre> <p>I would get back:</p> <pre><code>&lt;a href="#" title="myur...
0
2010-01-21T02:55:05Z
2,106,639
<p>you can try this since your requirement is simple. No need BeautifulSoup or regex</p> <pre><code>&gt;&gt;&gt; s=""" ... &lt;ul class="something"&gt; ... &lt;li id="li_id"&gt; ... &lt;a href="#" title="myurl"&gt;URL Text&lt;/a&gt; ... &lt;/li&gt; ... &lt;/ul&gt; ... """ &gt;&gt;&gt; for item in s.split("&lt;/a&gt;")...
1
2010-01-21T03:06:57Z
[ "python", "regex" ]
python, regex to find anchor link html
2,106,597
<p>I need a regex in python to find a links html in a larger set of html. </p> <p>so if I have:</p> <pre><code>&lt;ul class="something"&gt; &lt;li id="li_id"&gt; &lt;a href="#" title="myurl"&gt;URL Text&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre> <p>I would get back:</p> <pre><code>&lt;a href="#" title="myur...
0
2010-01-21T02:55:05Z
2,106,672
<p>Soup is good for you:</p> <pre><code>&gt;&gt;&gt; from BeautifulSoup import BeautifulSoup &gt;&gt;&gt; soup = BeautifulSoup('''&lt;ul class="something"&gt; ... &lt;li id="li_id"&gt; ... &lt;a href="#" title="myurl"&gt;URL Text&lt;/a&gt; ... &lt;/li&gt; ... &lt;/ul&gt;''') </code></pre> <p>There are many arguments ...
4
2010-01-21T03:18:36Z
[ "python", "regex" ]
Re-open files in Python?
2,106,820
<p>Say I have this simple python script:</p> <pre><code>file = open('C:\\some_text.txt') print file.readlines() print file.readlines() </code></pre> <p>When it is run, the first print prints a list containing the text of the file, while the second print prints a blank list. Not completely unexpected I guess. But is t...
24
2010-01-21T03:57:53Z
2,106,825
<p>You can reset the file pointer by calling <a href="http://docs.python.org/library/stdtypes.html#file.seek"><code>seek()</code></a>:</p> <pre><code>file.seek(0) </code></pre> <p>will do it. You need that line after your first <code>readlines()</code>. Note that <code>file</code> has to support random access for t...
57
2010-01-21T03:59:02Z
[ "python", "file" ]
Re-open files in Python?
2,106,820
<p>Say I have this simple python script:</p> <pre><code>file = open('C:\\some_text.txt') print file.readlines() print file.readlines() </code></pre> <p>When it is run, the first print prints a list containing the text of the file, while the second print prints a blank list. Not completely unexpected I guess. But is t...
24
2010-01-21T03:57:53Z
2,106,859
<p>For small files, it's probably much faster to just keep the file's contents in memory</p> <pre><code>file = open('C:\\some_text.txt') fileContents = file.readlines() print fileContents print fileContents # This line will work as well. </code></pre> <p>Of course, if it's a big file, this could put strain on your RA...
3
2010-01-21T04:07:37Z
[ "python", "file" ]
Re-open files in Python?
2,106,820
<p>Say I have this simple python script:</p> <pre><code>file = open('C:\\some_text.txt') print file.readlines() print file.readlines() </code></pre> <p>When it is run, the first print prints a list containing the text of the file, while the second print prints a blank list. Not completely unexpected I guess. But is t...
24
2010-01-21T03:57:53Z
2,107,566
<p>Remember that you can always use the with statement to open and close files:</p> <pre><code>from __future__ import with_statement with open('C:\\some_text.txt') as file: data = file.readlines() #File is now closed for line in data: print line </code></pre>
3
2010-01-21T07:32:26Z
[ "python", "file" ]
Limit a single record in model for django app?
2,106,823
<p>I want use a model to save the system setting for a django app, So I want to limit the model can only have one record, how to do the limit?</p>
3
2010-01-21T03:58:43Z
2,106,836
<p>An easy way is to use the setting's name as the primary key in the settings table. There can't be more of one record with the same primary key, so that will allow both Django and the database to guarantee integrity.</p>
3
2010-01-21T04:00:54Z
[ "python", "django" ]
Limit a single record in model for django app?
2,106,823
<p>I want use a model to save the system setting for a django app, So I want to limit the model can only have one record, how to do the limit?</p>
3
2010-01-21T03:58:43Z
6,524,607
<p>A model with a single allowed row is nothing more than a perverted form of a "persisted object" -- maybe even a "persisted singleton"? Don't do that, that's not how models work.</p> <p>Check out <a href="https://github.com/danielroseman/django-dbsettings" rel="nofollow">https://github.com/danielroseman/django-dbse...
0
2011-06-29T17:10:00Z
[ "python", "django" ]
Limit a single record in model for django app?
2,106,823
<p>I want use a model to save the system setting for a django app, So I want to limit the model can only have one record, how to do the limit?</p>
3
2010-01-21T03:58:43Z
8,094,563
<p>Try this:</p> <pre><code>class MyModel(models.Model): onefield = models.CharField('The field', max_length=100) class MyModelAdmin(admin.ModelAdmin): def has_add_permission(self, request): # if there's already an entry, do not allow adding count = MyModel.objects.all().count() if count == 0: ...
5
2011-11-11T13:20:07Z
[ "python", "django" ]
Limit a single record in model for django app?
2,106,823
<p>I want use a model to save the system setting for a django app, So I want to limit the model can only have one record, how to do the limit?</p>
3
2010-01-21T03:58:43Z
22,297,062
<p>William is right, but I guess this is the best practice</p> <pre><code>def has_add_permission(self, *args, **kwargs): return not MyModel.objects.exists() </code></pre> <p>As reported in the official Django Documentation:</p> <blockquote> <p>Note: Don’t use this if all you want to do is determine if at lea...
2
2014-03-10T10:03:05Z
[ "python", "django" ]
Limit a single record in model for django app?
2,106,823
<p>I want use a model to save the system setting for a django app, So I want to limit the model can only have one record, how to do the limit?</p>
3
2010-01-21T03:58:43Z
26,951,246
<p>Overwriting <code>has_add_permission</code> works, but in the given examples it breaks the permissions system in Django(staff without necessary permissions can add settings). Here's a one that doesn't break it:</p> <pre><code>class SettingAdmin(admin.ModelAdmin): def has_add_permission(self, request): b...
4
2014-11-15T22:03:33Z
[ "python", "django" ]
Pythonic way of summing lists and lists of lists
2,106,996
<p>I'm trying to find a neat way of summing a list and a list of lists in the same function, so far I've got:</p> <pre><code>import operator """ Fails late for item = ['a', 'b'] """ def validate(item): try: return sum(item) == sum(range(1, 10)) except TypeError: return sum(reduce(operator.a...
1
2010-01-21T04:50:00Z
2,107,040
<p>Perhaps you'd find it easier to flatten the list first?</p> <pre><code>def flatten(xs): for x in xs: try: sub = iter(x) except TypeError: yield x else: for y in flatten(sub): yield y </code></pre> <p>With the above, you can do this:</...
5
2010-01-21T05:05:20Z
[ "algorithm", "python" ]
Pythonic way of summing lists and lists of lists
2,106,996
<p>I'm trying to find a neat way of summing a list and a list of lists in the same function, so far I've got:</p> <pre><code>import operator """ Fails late for item = ['a', 'b'] """ def validate(item): try: return sum(item) == sum(range(1, 10)) except TypeError: return sum(reduce(operator.a...
1
2010-01-21T04:50:00Z
2,107,046
<p>Don't forget to describe exactly what you're trying to do. I'm assuming you mean to sum all values to a single value, and not to get eg. [[1,2],[3,4]] -> [3,7]. Here's simple recursion; five lines of code if you skip the tests:</p> <pre><code>def sums(it): """ &gt;&gt;&gt; sums(1) 1 &gt;&gt;&gt; s...
3
2010-01-21T05:06:02Z
[ "algorithm", "python" ]
Pythonic way of summing lists and lists of lists
2,106,996
<p>I'm trying to find a neat way of summing a list and a list of lists in the same function, so far I've got:</p> <pre><code>import operator """ Fails late for item = ['a', 'b'] """ def validate(item): try: return sum(item) == sum(range(1, 10)) except TypeError: return sum(reduce(operator.a...
1
2010-01-21T04:50:00Z
2,124,607
<p>The external <a href="http://numpy.scipy.org/" rel="nofollow">numpy</a> module has many operations (including <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html" rel="nofollow">sum()</a>) which work similarly on scalars, vectors, matrices and even higher-dimensional arrays...</p> <p>Note ho...
0
2010-01-23T20:36:33Z
[ "algorithm", "python" ]
Python EVT_SET_FOCUS
2,107,223
<p>When I run this code and focus on choice it is raise a error. I close this message but it is come back again. I want to see only one time this message. How can i do this? What is error in my code ?</p> <p>Note: I'm sorry for my English. I know it isn't good.</p> <pre><code>#! -*- coding:utf-8 -*- import wx class ...
0
2010-01-21T05:54:35Z
2,113,307
<p>The error dialog gets the focus when it is shown. When you close the error dialog, the focus returns to the choice control, firing the event handler again, which pops up the error dialog again, et cetera.</p> <p>To avoid the event handler from being invoked multiple times, one solution would be to unbind the wx.EVT...
0
2010-01-21T22:05:09Z
[ "python", "wxpython" ]
Python EVT_SET_FOCUS
2,107,223
<p>When I run this code and focus on choice it is raise a error. I close this message but it is come back again. I want to see only one time this message. How can i do this? What is error in my code ?</p> <p>Note: I'm sorry for my English. I know it isn't good.</p> <pre><code>#! -*- coding:utf-8 -*- import wx class ...
0
2010-01-21T05:54:35Z
2,116,427
<p>I solved my problem with following code. Thanks to everyone for help.</p> <pre><code>#! -*- coding:utf-8 -*- import wx import wx.lib.evtmgr as em class MyPanel(wx.Panel): def __init__(self, parent, *args, **kwargs): wx.Panel.__init__(self, parent, *args, **kwargs) sizer = wx.BoxSizer(wx.HORIZ...
0
2010-01-22T10:02:33Z
[ "python", "wxpython" ]
Restart a Python Program
2,107,317
<p>I'm writing a Python program that, if the user changes settings while running it, needs to be restarted to apply the changed settings. Is there a way of doing this? I'm thinking something along the lines of:</p> <pre><code>import sys command_line = ' '.join(sys.argv) # Now do something with command_line # Now call ...
8
2010-01-21T06:28:55Z
2,107,357
<p>I would bypass all the angst you're likely to get from trying to re-run yourself and leave it in the hands of the environment.</p> <p>By that, I mean:</p> <ol> <li><p>Have a controlling program which does nothing more than run your program (with the same parameters it was given) in a loop while your program exits ...
8
2010-01-21T06:39:46Z
[ "python", "restart" ]
Restart a Python Program
2,107,317
<p>I'm writing a Python program that, if the user changes settings while running it, needs to be restarted to apply the changed settings. Is there a way of doing this? I'm thinking something along the lines of:</p> <pre><code>import sys command_line = ' '.join(sys.argv) # Now do something with command_line # Now call ...
8
2010-01-21T06:28:55Z
2,107,361
<p>On unix, you can use <a href="http://docs.python.org/library/os.html?highlight=os.exec#os.execl" rel="nofollow">os.execl</a> family of functions:</p> <blockquote> <p>These functions all execute a new program, replacing the current process; they do not return. On Unix, the new executable is loaded into the current...
4
2010-01-21T06:41:19Z
[ "python", "restart" ]
indentation problem in my python program
2,107,327
<p>I am using the following code and geting some indentation problem</p> <pre><code>from django.db import models class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __unicode__(self): return self.question </code></pre> <p>I ...
1
2010-01-21T06:31:38Z
2,107,332
<p>Most likely you're using a mix of tabs and spaces in your indentation... Use all spaces / all tabs instead. (The most widely adopted style is to use 4 spaces per level of indent.)</p> <p>To fix this particular instance of the problem, check make the <code>def __unicode__(self):</code> line start with the same inden...
3
2010-01-21T06:34:04Z
[ "python", "django" ]
indentation problem in my python program
2,107,327
<p>I am using the following code and geting some indentation problem</p> <pre><code>from django.db import models class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __unicode__(self): return self.question </code></pre> <p>I ...
1
2010-01-21T06:31:38Z
2,107,386
<p>agree with the others, and you can use editor that can visualize the space to check. e.g. vim <code>set list listchars=tab:»·,trail:·</code> (gedit also has such plugins, i think).</p>
1
2010-01-21T06:45:32Z
[ "python", "django" ]
indentation problem in my python program
2,107,327
<p>I am using the following code and geting some indentation problem</p> <pre><code>from django.db import models class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __unicode__(self): return self.question </code></pre> <p>I ...
1
2010-01-21T06:31:38Z
2,107,404
<p>I'm taking a wild guess here, and the fact that there's a filename* in your traceback speaks against my guess, but hey&hellip;</p> <p>Since your tabs/spaces seem more or less okay (assuming a verbatim copy/paste), is it possible that you pasted the code into the interactive interpreter?</p> <p>The newline after th...
0
2010-01-21T06:50:08Z
[ "python", "django" ]
How do I change 'example.com' in my code?
2,107,456
<p>I built a <code>a.py</code> in my mysite file,</p> <p>a.py:</p> <pre><code>from django.core.management import setup_environ from mysite import settings setup_environ(settings) from django.contrib.sites.models import Site domain = Site.objects.get_current().domain print domain </code></pre> <p>It prints:</p> <b...
1
2010-01-21T07:04:12Z
2,107,482
<p>Go to the <code>Site</code> model in the admin interface and change the value there. Or do a query on Site at the Django prompt and modify and save the model.</p>
7
2010-01-21T07:10:00Z
[ "python", "django" ]
How do I change 'example.com' in my code?
2,107,456
<p>I built a <code>a.py</code> in my mysite file,</p> <p>a.py:</p> <pre><code>from django.core.management import setup_environ from mysite import settings setup_environ(settings) from django.contrib.sites.models import Site domain = Site.objects.get_current().domain print domain </code></pre> <p>It prints:</p> <b...
1
2010-01-21T07:04:12Z
2,108,019
<p>Like whrd commented, you can't call <code>update(...)</code> on the result of calling <code>get(...)</code> on a <code>QuerySet</code>. A better way of changing your code is as follows:</p> <pre><code>from django.core.management import setup_environ from register import settings setup_environ(settings) from django...
1
2010-01-21T09:12:29Z
[ "python", "django" ]
How do I change 'example.com' in my code?
2,107,456
<p>I built a <code>a.py</code> in my mysite file,</p> <p>a.py:</p> <pre><code>from django.core.management import setup_environ from mysite import settings setup_environ(settings) from django.contrib.sites.models import Site domain = Site.objects.get_current().domain print domain </code></pre> <p>It prints:</p> <b...
1
2010-01-21T07:04:12Z
4,520,109
<p>Put a file call initial_data.json in your project directory with the following content : <code>[{ "pk": 1, "model": "sites.site", "fields": { "name": "localhost:8080", "domain":"localhost:8080" } } ]</code> then use python manage.py syncdb et voilà the set...
4
2010-12-23T15:31:43Z
[ "python", "django" ]
should not access parent frame untill closes the child?
2,107,489
<p>I have two frames one parent and child. the parent will invoke the child.</p> <p>The program should not allow the user to access the parent until the user closes the child</p> <p>How can i achive it?</p> <p>Need a solution without hiding the parent </p>
1
2010-01-21T07:12:14Z
2,107,570
<p>See the <a href="http://www.wxpython.org/docs/api/wx.Window-class.html#MakeModal" rel="nofollow"><code>wx.Window.MakeModal()</code></a> method (<code>Frame</code> inherits from <code>Window</code>).</p>
2
2010-01-21T07:32:48Z
[ "python", "wxpython" ]
What should I name my global module in Python?
2,107,682
<p>I'm writing an application in Python, and I've got a number of universal variables (such as the reference to the main window, the user settings, and the list of active items in the UI) which have to be accessible from all parts of the program<sup>1</sup>. I only just realized I've named the module <code>globals.py<...
1
2010-01-21T07:58:03Z
2,107,730
<p><code>global</code> is a keyword, not a built-in. 'globals' is not a keyword, but <em>is</em> a built-in function. It can be assigned to, but is bad practice. Code checkers like <em>pylint</em> and <em>pychecker</em> can catch these accidental assignments. How about <code>config</code>?</p>
0
2010-01-21T08:07:44Z
[ "python", "naming-conventions", "global-variables" ]
What should I name my global module in Python?
2,107,682
<p>I'm writing an application in Python, and I've got a number of universal variables (such as the reference to the main window, the user settings, and the list of active items in the UI) which have to be accessible from all parts of the program<sup>1</sup>. I only just realized I've named the module <code>globals.py<...
1
2010-01-21T07:58:03Z
2,107,744
<p><code>top</code>? <code>top_level</code>?</p>
0
2010-01-21T08:10:07Z
[ "python", "naming-conventions", "global-variables" ]
What should I name my global module in Python?
2,107,682
<p>I'm writing an application in Python, and I've got a number of universal variables (such as the reference to the main window, the user settings, and the list of active items in the UI) which have to be accessible from all parts of the program<sup>1</sup>. I only just realized I've named the module <code>globals.py<...
1
2010-01-21T07:58:03Z
2,107,746
<p>I would try to avoid such a global container module altogether, and instead put these variables into their own modules, which can then be imported from all parts of the system.</p> <p>For example, the main window would probably go into a variable in <code>main.py</code>. User settings could go into <code>usersetti...
3
2010-01-21T08:10:21Z
[ "python", "naming-conventions", "global-variables" ]
What should I name my global module in Python?
2,107,682
<p>I'm writing an application in Python, and I've got a number of universal variables (such as the reference to the main window, the user settings, and the list of active items in the UI) which have to be accessible from all parts of the program<sup>1</sup>. I only just realized I've named the module <code>globals.py<...
1
2010-01-21T07:58:03Z
2,107,747
<pre><code>`config` or `settings` </code></pre>
1
2010-01-21T08:10:22Z
[ "python", "naming-conventions", "global-variables" ]
What should I name my global module in Python?
2,107,682
<p>I'm writing an application in Python, and I've got a number of universal variables (such as the reference to the main window, the user settings, and the list of active items in the UI) which have to be accessible from all parts of the program<sup>1</sup>. I only just realized I've named the module <code>globals.py<...
1
2010-01-21T07:58:03Z
2,111,674
<p>I'd call it <code>env</code>. There's little risk that someone will confuse it with <code>os.environ</code> (especially if you organize your code so that you can call it <code>myapp.environ</code>).</p> <p>I'd also make everything exposed by <code>myapp.environ</code> a property of a class, so that I can put break...
1
2010-01-21T18:09:25Z
[ "python", "naming-conventions", "global-variables" ]
What should I name my global module in Python?
2,107,682
<p>I'm writing an application in Python, and I've got a number of universal variables (such as the reference to the main window, the user settings, and the list of active items in the UI) which have to be accessible from all parts of the program<sup>1</sup>. I only just realized I've named the module <code>globals.py<...
1
2010-01-21T07:58:03Z
2,115,432
<pre><code>from globals import Globals </code></pre> <p>This will fix the conflict and also follows PEP 8 recommendations.</p> <p>Also, in other cases like this, Roget's Thesaurus is your friend. I always keep a copy nearby.</p>
0
2010-01-22T06:16:30Z
[ "python", "naming-conventions", "global-variables" ]
Python :How to generate a power law graph
2,107,754
<p>I have installed networkx and matplotlib packages. How can I generate a power law graph based on degree correlation i.e. graphs with high or low degree of homophily</p>
3
2010-01-21T08:12:16Z
2,121,372
<p>Have you looked at the examples on the Networkx site? <a href="http://networkx.lanl.gov/examples/drawing/degree_histogram.html" rel="nofollow">This example</a> might help you get started with this.</p> <p>There are also a number of functions within Networkx which generate random graphs which will probably be helpfu...
1
2010-01-22T23:55:17Z
[ "python", "matplotlib", "graph-theory", "networkx" ]
Python :How to generate a power law graph
2,107,754
<p>I have installed networkx and matplotlib packages. How can I generate a power law graph based on degree correlation i.e. graphs with high or low degree of homophily</p>
3
2010-01-21T08:12:16Z
13,634,519
<p><a href="http://networkx.lanl.gov/reference/generated/networkx.generators.random_graphs.barabasi_albert_graph.html" rel="nofollow"><code>networkx.generators.barabasi_albert_graph</code></a> will generate a graph according to the <a href="https://en.wikipedia.org/wiki/Barab%C3%A1si%E2%80%93Albert_model" rel="nofollow...
0
2012-11-29T20:53:12Z
[ "python", "matplotlib", "graph-theory", "networkx" ]
How do I rotate/flip an image without using photologue/ImageModel?
2,107,888
<p>I need a functionality in <a href="http://en.wikipedia.org/wiki/Django_%28web_framework%29" rel="nofollow">Django</a> to rotate an image posted by users using a form. I need a method I can maybe put in imageutils.py and then use it in my form.</p> <p>How can it be achieved?</p>
2
2010-01-21T08:46:02Z
2,107,965
<p>Use the <a href="http://www.pythonware.com/library/pil/handbook/index.htm" rel="nofollow">Python Imaging Library</a> directly:</p> <pre><code>from PIL import Image im = Image.open("yourfilename.jpg") im = im.rotate(90) im.save("yourrotatedfilename.jpg", "JPEG") </code></pre> <p>This is tested, and works. You'll ne...
3
2010-01-21T09:00:37Z
[ "python", "django" ]
Web frontend for a Python application
2,107,968
<p>I created a nice RSS application in Python. It took a while and most of the code just does heavy work, like formatting XML, downloading feeds, etc. To the application itself requires very little user interaction, just a initial list of RSS feeds and some parameters. </p> <p>What would be really nice, is if I was ...
0
2010-01-21T09:01:39Z
2,107,990
<p><a href="http://webpy.org" rel="nofollow">web.py</a> is a very lightweight 'library' (not framework) that you can put as a front end to your app. Just <code>import</code> your app within the main controller and use it as you would. </p> <p>The Python standard library also includes a builting <code>SimpleHTTPServer<...
2
2010-01-21T09:05:36Z
[ "python" ]
Web frontend for a Python application
2,107,968
<p>I created a nice RSS application in Python. It took a while and most of the code just does heavy work, like formatting XML, downloading feeds, etc. To the application itself requires very little user interaction, just a initial list of RSS feeds and some parameters. </p> <p>What would be really nice, is if I was ...
0
2010-01-21T09:01:39Z
2,108,728
<p>It depends on your needs, free time, etc.</p> <p>I recommend two solutions:</p> <ul> <li><a href="http://en.wikipedia.org/wiki/Django_%28web_framework%29" rel="nofollow">Django</a> - a very rich framework which allows you to create full featured sites using only accessible components (in most cases they are good e...
2
2010-01-21T11:18:49Z
[ "python" ]
Save method manytomany
2,108,039
<p>I've got a model called Card which has a ManyToMany relationship to Tag. When I save a Card, I'd like to create a Product as well, which I want to have the same ManyToMany relationship to tag. </p> <p>How do I access the instance's tags? <code>self.tags.all()</code> gives an empty list, while if I check after sa...
1
2010-01-21T09:17:03Z
2,108,865
<p>You aren't adding any tags to the Card. You can't add ManyToMany relationships until after you save the Card, and there's no time between the <code>save</code> call and the call to <code>self.tags.all()</code> for them to be set.</p>
0
2010-01-21T11:41:28Z
[ "python", "django", "methods", "save", "many-to-many" ]
Save method manytomany
2,108,039
<p>I've got a model called Card which has a ManyToMany relationship to Tag. When I save a Card, I'd like to create a Product as well, which I want to have the same ManyToMany relationship to tag. </p> <p>How do I access the instance's tags? <code>self.tags.all()</code> gives an empty list, while if I check after sa...
1
2010-01-21T09:17:03Z
2,109,177
<p>Are you using the django-admin to save the model and tags? The many-to-many fields don't get saved there until after the post-save signal of the model. What you can do in this case is overide the admin classes save_model method. E.g.:</p> <pre><code>class CardAdmin(admin.ModelAdmin): def save_model(self, reque...
2
2010-01-21T12:39:46Z
[ "python", "django", "methods", "save", "many-to-many" ]
How to add a padding to the data to make it acceptable for AES256 encryption algorithm in pycrypto library
2,108,047
<p>Can someone tell me how to add a padding to the data to make it acceptable for AES256 encryption algorithm in pycrypto library (Python).</p> <p>Thanks a lot in advance.. :)</p>
5
2010-01-21T09:18:18Z
2,108,182
<p>Looking at the <a href="http://www.dlitz.net/software/pycrypto/doc/" rel="nofollow">documentation</a>, it seems that it's up to you, the library user, to pad the data yourself. The documentation states that the block size for AES is always 16 bytes, so you need to pad the data to a multiple of 16 bytes.</p> <p>How ...
3
2010-01-21T09:43:57Z
[ "python", "encryption", "pycrypto" ]
How to add a padding to the data to make it acceptable for AES256 encryption algorithm in pycrypto library
2,108,047
<p>Can someone tell me how to add a padding to the data to make it acceptable for AES256 encryption algorithm in pycrypto library (Python).</p> <p>Thanks a lot in advance.. :)</p>
5
2010-01-21T09:18:18Z
2,109,258
<p>Use a standard padding scheme, such as the scheme outlined in <a href="http://www.rfc-editor.org/rfc/rfc2898.txt" rel="nofollow">PKCS-5</a>, section 6.1.1 step #4 (replace the 8 in that example with 16 if you are using AES).</p>
3
2010-01-21T12:50:39Z
[ "python", "encryption", "pycrypto" ]
PyQt4,How to add a batch of widget (QPushButton) at one time and lets them to execute on SLOT
2,108,049
<p>if i want to add 10 QPushButton at one time:</p> <pre><code>NumCount=20 for i in range(NumCount): btn=QPushButton("%s %s" %("Button" i+1),self) btn.clicked.connect(self.btnclick) def btnclick(self): # here is my question # how to define which button clicked? # how to print btn.text? </code></pre> <p>as...
7
2010-01-21T09:18:36Z
2,146,604
<p>When you are in a slot, you can use sender() method (just call self.sender()) and you will receive a reference to the object, from which signal was emitted. <a href="http://doc.trolltech.com/4.6/qobject.html#sender" rel="nofollow">Here</a> is documentation about it.</p>
3
2010-01-27T11:57:35Z
[ "python", "pyqt", "signals", "sender" ]
PyQt4,How to add a batch of widget (QPushButton) at one time and lets them to execute on SLOT
2,108,049
<p>if i want to add 10 QPushButton at one time:</p> <pre><code>NumCount=20 for i in range(NumCount): btn=QPushButton("%s %s" %("Button" i+1),self) btn.clicked.connect(self.btnclick) def btnclick(self): # here is my question # how to define which button clicked? # how to print btn.text? </code></pre> <p>as...
7
2010-01-21T09:18:36Z
2,292,236
<p>I'd subclass <code>QPushButton</code> and define my own sender and slot. The <code>QObject.sender()</code> method is tempting, but it gives me the heebie-jeebies.</p> <pre><code>class MyPushButton(QPushButton): def __init__(self, text = '', parent = None): QPushButton.__init__(self, text, parent) ...
2
2010-02-18T21:14:03Z
[ "python", "pyqt", "signals", "sender" ]
PyQt4,How to add a batch of widget (QPushButton) at one time and lets them to execute on SLOT
2,108,049
<p>if i want to add 10 QPushButton at one time:</p> <pre><code>NumCount=20 for i in range(NumCount): btn=QPushButton("%s %s" %("Button" i+1),self) btn.clicked.connect(self.btnclick) def btnclick(self): # here is my question # how to define which button clicked? # how to print btn.text? </code></pre> <p>as...
7
2010-01-21T09:18:36Z
2,292,319
<p>As gruszcsy said, there's self.<a href="http://qt.nokia.com/doc/4.6/qobject.html#sender" rel="nofollow">sender()</a> (in QObject) to get that exact info.</p> <p>There's also the <a href="http://qt.nokia.com/doc/4.6/qsignalmapper.html" rel="nofollow">QSignalMapper</a> class that provides a higher-level mapping from ...
1
2010-02-18T21:26:15Z
[ "python", "pyqt", "signals", "sender" ]
PyQt4,How to add a batch of widget (QPushButton) at one time and lets them to execute on SLOT
2,108,049
<p>if i want to add 10 QPushButton at one time:</p> <pre><code>NumCount=20 for i in range(NumCount): btn=QPushButton("%s %s" %("Button" i+1),self) btn.clicked.connect(self.btnclick) def btnclick(self): # here is my question # how to define which button clicked? # how to print btn.text? </code></pre> <p>as...
7
2010-01-21T09:18:36Z
2,298,509
<p>Here is a small app demonstrating one possible solution:</p> <pre><code>from PyQt4.QtGui import QPushButton, QWidget from PyQt4.QtGui import QVBoxLayout, QApplication def smart_connect(btn, btn_click_slot): proxy_slot = lambda checked: btn_click_slot(btn) btn.clicked.connect(proxy_slot) class MyWidget(QWi...
0
2010-02-19T17:58:43Z
[ "python", "pyqt", "signals", "sender" ]
python virtual environment on source control
2,108,105
<p>I have created a python web virtual environment contains all django, pylons related packages. I use the host ubuntu desktop PC at home and I have ubuntu virtual machine running on windows PC laptop.</p> <p>Both the operating systems are linux only. I will be using the same environment for production that will be ub...
1
2010-01-21T09:30:55Z
2,108,129
<p>You <em>can</em> but you don't really need 'version' control for that. You need to setup your environment. It's a one time job to setup your environment. After that you'll just use it. Why version control it? </p>
0
2010-01-21T09:33:29Z
[ "python", "linux", "installation", "virtualenv" ]
python virtual environment on source control
2,108,105
<p>I have created a python web virtual environment contains all django, pylons related packages. I use the host ubuntu desktop PC at home and I have ubuntu virtual machine running on windows PC laptop.</p> <p>Both the operating systems are linux only. I will be using the same environment for production that will be ub...
1
2010-01-21T09:30:55Z
2,108,226
<p>You might want to look into virtualenv. This will allow you to set up your working environment, 'freeze' the list of packages that are needed to replicate it, and store that list of requirements in version control so that others can check it out and rebuild the environment with a single step.</p>
1
2010-01-21T09:54:40Z
[ "python", "linux", "installation", "virtualenv" ]
python virtual environment on source control
2,108,105
<p>I have created a python web virtual environment contains all django, pylons related packages. I use the host ubuntu desktop PC at home and I have ubuntu virtual machine running on windows PC laptop.</p> <p>Both the operating systems are linux only. I will be using the same environment for production that will be ub...
1
2010-01-21T09:30:55Z
2,112,221
<p>If you already have a VM set up, you can export it so that others can copy it and start their own instance with everything installed. VirtualBox and VMWare both support VMDK images, and Xen has its own type of VM images.</p> <p>That is probably not a solution for setting up servers. I like using <a href="http://www...
0
2010-01-21T19:32:14Z
[ "python", "linux", "installation", "virtualenv" ]
How to run two functions simultaneously
2,108,126
<p>I am running test but I want to run 2 functions at the same time. I have a camera and I am telling it to move via suds, I am then logging into the camera via SSH to check the speed the camera is set to. When I check the speed the camera has stopped so no speed is available. Is there a way I can get these functions t...
14
2010-01-21T09:32:58Z
2,108,146
<p>Import the <code>threading</code> module and run <code>SudsMove()</code> like so:</p> <pre><code>threading.Thread(target = SudsMove).start() </code></pre> <p>That will create and start a background thread which does the movement.</p> <p><strong>ANSWER TO EDITED QUESTION:</strong></p> <p>As far as I understand th...
12
2010-01-21T09:37:18Z
[ "python", "testing", "multithreading", "function", "synchronization" ]
How to run two functions simultaneously
2,108,126
<p>I am running test but I want to run 2 functions at the same time. I have a camera and I am telling it to move via suds, I am then logging into the camera via SSH to check the speed the camera is set to. When I check the speed the camera has stopped so no speed is available. Is there a way I can get these functions t...
14
2010-01-21T09:32:58Z
2,131,979
<p>There can only be one thread running at the same time. This has been answered extensively <a href="http://stackoverflow.com/questions/31340/how-do-threads-work-in-python-and-what-are-common-python-threading-specific-pitf">here</a>. One solution will be to use two separate processes. The above answer provides some ti...
2
2010-01-25T11:48:59Z
[ "python", "testing", "multithreading", "function", "synchronization" ]
How to run two functions simultaneously
2,108,126
<p>I am running test but I want to run 2 functions at the same time. I have a camera and I am telling it to move via suds, I am then logging into the camera via SSH to check the speed the camera is set to. When I check the speed the camera has stopped so no speed is available. Is there a way I can get these functions t...
14
2010-01-21T09:32:58Z
2,132,076
<p>If you can get your code to run under Jython or IronPython, then you <em>can</em> run several threads simultaneously; they don't have that goofy "Global Interpreter Lock" thing of CPython.</p>
1
2010-01-25T12:07:41Z
[ "python", "testing", "multithreading", "function", "synchronization" ]
How to run two functions simultaneously
2,108,126
<p>I am running test but I want to run 2 functions at the same time. I have a camera and I am telling it to move via suds, I am then logging into the camera via SSH to check the speed the camera is set to. When I check the speed the camera has stopped so no speed is available. Is there a way I can get these functions t...
14
2010-01-21T09:32:58Z
2,132,460
<p>If you want to use the common Python implementation (CPython), you can certainly use the <a href="http://docs.python.org/library/multiprocessing.html" rel="nofollow">multiprocessing</a> module, which <a href="http://stackoverflow.com/questions/2102216/how-can-multiple-calculations-be-launched-in-parallel-while-stopp...
4
2010-01-25T13:14:24Z
[ "python", "testing", "multithreading", "function", "synchronization" ]
Saving huge bigram dictionary to file using pickle
2,108,293
<p>a friend of mine wrote this little progam. the <code>textFile</code> is 1.2GB in size (7 years worth of newspapers). He successfully manages to create the dictionary but he cannot write it to a file using pickle(program hangs).</p> <pre><code>import sys import string import cPickle as pickle biGramDict = {} textF...
2
2010-01-21T10:08:40Z
2,108,319
<p>Pickle is only meant to write complete (small) objects. Your dictionary is a bit large to even hold in memory, you'd better use a database instead so you can store and retrieve entries one by one instead of all at once.</p> <p>Some good and easily integratable singe-file database formats you can use from Python are...
7
2010-01-21T10:11:50Z
[ "python", "file", "dictionary", "pickle" ]
Saving huge bigram dictionary to file using pickle
2,108,293
<p>a friend of mine wrote this little progam. the <code>textFile</code> is 1.2GB in size (7 years worth of newspapers). He successfully manages to create the dictionary but he cannot write it to a file using pickle(program hangs).</p> <pre><code>import sys import string import cPickle as pickle biGramDict = {} textF...
2
2010-01-21T10:08:40Z
2,108,498
<p>Do you really need the whole data in memory? You could split it in naive ways like one file for each year o each month if you want the dictionary/pickle approach.</p> <p>Also, remember that the dictionaries are not sorted, you can have problems having to sort that ammount of data. In case you want to search or sort...
1
2010-01-21T10:41:50Z
[ "python", "file", "dictionary", "pickle" ]
Saving huge bigram dictionary to file using pickle
2,108,293
<p>a friend of mine wrote this little progam. the <code>textFile</code> is 1.2GB in size (7 years worth of newspapers). He successfully manages to create the dictionary but he cannot write it to a file using pickle(program hangs).</p> <pre><code>import sys import string import cPickle as pickle biGramDict = {} textF...
2
2010-01-21T10:08:40Z
2,109,837
<p>If your <em>really, really</em> want to use a dictionary like semantics, try SQLAlchemy's <code>associationproxy</code>. The following (rather long) piece of code translates your dictionary into <em>Key,Value-Pairs</em> in the <code>entries</code>-Table. I do not know how SQLAlchemy copes with your big dictionary, b...
0
2010-01-21T14:20:12Z
[ "python", "file", "dictionary", "pickle" ]
Saving huge bigram dictionary to file using pickle
2,108,293
<p>a friend of mine wrote this little progam. the <code>textFile</code> is 1.2GB in size (7 years worth of newspapers). He successfully manages to create the dictionary but he cannot write it to a file using pickle(program hangs).</p> <pre><code>import sys import string import cPickle as pickle biGramDict = {} textF...
2
2010-01-21T10:08:40Z
2,110,081
<p>One solution is to use <a href="http://buzhug.sourceforge.net/" rel="nofollow">buzhug</a> instead of pickle. It's a pure Python solution, and retains very Pythonic syntax. I think of it as the next step up from shelve and their ilk. It will handle the data sizes you're talking about. Its size limit is 2 GB per field...
1
2010-01-21T14:52:56Z
[ "python", "file", "dictionary", "pickle" ]
How to update xml file using lxml and python?
2,108,334
<pre><code>&lt;example&gt; &lt;login&gt; &lt;id&gt;1&lt;/id&gt; &lt;username&gt;kites&lt;/username&gt; &lt;password&gt;kites&lt;/password&gt; &lt;/login&gt; &lt;/example&gt; </code></pre> <p>How can i update password using lxml? and now can i add one more record to the same file?</p> <...
4
2010-01-21T10:14:56Z
2,108,374
<pre><code>example = etree.Element("example") login = etree.SubElement(example, "login") password = etree.SubElement(login,"password") password.text = "newPassword" </code></pre> <p>This is a good <a href="http://codespeak.net/lxml/tutorial.html" rel="nofollow">tutorial</a></p>
3
2010-01-21T10:21:31Z
[ "python", "xml", "lxml" ]
Python OpenID Library Usage
2,108,438
<p>Can anyone explain me steps using openid library which is mentioned here. I have import all package of janrain openid in my programe but I cant understand actual flow of code.</p> <p>The process should basically follow this plan:</p> <p>Add an OpenID login field somewhere on your site. When an OpenID is entered in...
3
2010-01-21T10:31:03Z
8,937,482
<p><a href="http://webpy.org" rel="nofollow">web.py</a> includes a <a href="http://webpy.org/docs/0.3/api#web.webopenid" rel="nofollow">webopenid module</a> that implements a complete, if basic, OpenID authentication system using the Janrain. Using the included <code>host</code> class, you can add OpenID-backed authen...
0
2012-01-20T06:16:35Z
[ "python", "openid" ]
pylint giving a number of errors with maximum recursion depth reached
2,108,723
<p>Pylint gives me an error like this very frequently.</p> <pre><code>shabda@jazzy ~/uswaretech_uswaretechsite&gt; pylint -e utpages/forms.py No config file found, using default configuration Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in &lt;type 'exceptions.RuntimeError'&...
1
2010-01-21T11:18:20Z
2,108,790
<p>There's an <a href="http://www.logilab.org/ticket/19641" rel="nofollow">open ticket</a> about this bug. The bug report contains details about the cause.</p>
4
2010-01-21T11:27:59Z
[ "python", "pylint" ]
pylint giving a number of errors with maximum recursion depth reached
2,108,723
<p>Pylint gives me an error like this very frequently.</p> <pre><code>shabda@jazzy ~/uswaretech_uswaretechsite&gt; pylint -e utpages/forms.py No config file found, using default configuration Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in &lt;type 'exceptions.RuntimeError'&...
1
2010-01-21T11:18:20Z
2,336,298
<p>This bug has been killed in the repository of logilab.astng (dependency of Pylint). You may want to update from source. </p> <p>logilab-astng mercurial repository at <a href="http://www.logilab.org/cgi-bin/hgwebdir.cgi/logilab/astng" rel="nofollow">http://www.logilab.org/cgi-bin/hgwebdir.cgi/logilab/astng</a></p> ...
1
2010-02-25T17:50:16Z
[ "python", "pylint" ]
pylint giving a number of errors with maximum recursion depth reached
2,108,723
<p>Pylint gives me an error like this very frequently.</p> <pre><code>shabda@jazzy ~/uswaretech_uswaretechsite&gt; pylint -e utpages/forms.py No config file found, using default configuration Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in &lt;type 'exceptions.RuntimeError'&...
1
2010-01-21T11:18:20Z
8,586,766
<p>This has been fixed in logilab-astng 0.20 which is out for a year and half.</p> <p>Upgrading to an earlier release of pylint and logilab-astng should fix the problem.</p>
0
2011-12-21T08:15:36Z
[ "python", "pylint" ]
MySQL "incorrect string value" error when save unicode string in Django
2,108,824
<p>I got strange error message when tried to save first_name, last_name to Django's auth_user model.</p> <p><strong>Failed examples</strong></p> <pre><code>user = User.object.create_user(username, email, password) user.first_name = u'Rytis' user.last_name = u'Slatkevičius' user.save() &gt;&gt;&gt; Incorrect string v...
104
2010-01-21T11:34:22Z
2,108,886
<p>You aren't trying to save unicode strings, you're trying to save bytestrings in the UTF-8 encoding. Make them actual unicode string literals:</p> <pre><code>user.last_name = u'Slatkevičius' </code></pre> <p>or (when you don't have string literals) decode them using the utf-8 encoding:</p> <pre><code>user.last_na...
1
2010-01-21T11:44:50Z
[ "python", "mysql", "django", "unicode", "utf-8" ]
MySQL "incorrect string value" error when save unicode string in Django
2,108,824
<p>I got strange error message when tried to save first_name, last_name to Django's auth_user model.</p> <p><strong>Failed examples</strong></p> <pre><code>user = User.object.create_user(username, email, password) user.first_name = u'Rytis' user.last_name = u'Slatkevičius' user.save() &gt;&gt;&gt; Incorrect string v...
104
2010-01-21T11:34:22Z
2,110,394
<p>I just figured out one method to avoid above errors.</p> <p><strong>Save to database</strong></p> <pre><code>user.first_name = u'Rytis'.encode('unicode_escape') user.last_name = u'Slatkevičius'.encode('unicode_escape') user.save() &gt;&gt;&gt; SUCCEED print user.last_name &gt;&gt;&gt; Slatkevi\u010dius print use...
6
2010-01-21T15:29:09Z
[ "python", "mysql", "django", "unicode", "utf-8" ]
MySQL "incorrect string value" error when save unicode string in Django
2,108,824
<p>I got strange error message when tried to save first_name, last_name to Django's auth_user model.</p> <p><strong>Failed examples</strong></p> <pre><code>user = User.object.create_user(username, email, password) user.first_name = u'Rytis' user.last_name = u'Slatkevičius' user.save() &gt;&gt;&gt; Incorrect string v...
104
2010-01-21T11:34:22Z
4,793,471
<p>I had the same problem and resolved it by changing the character set of the column. Even though your database has a default character set of <code>utf-8</code> I think it's possible for database columns to have a different character set in MySQL. Here's the SQL QUERY I used:</p> <pre><code> ALTER TABLE database....
79
2011-01-25T12:28:09Z
[ "python", "mysql", "django", "unicode", "utf-8" ]
MySQL "incorrect string value" error when save unicode string in Django
2,108,824
<p>I got strange error message when tried to save first_name, last_name to Django's auth_user model.</p> <p><strong>Failed examples</strong></p> <pre><code>user = User.object.create_user(username, email, password) user.first_name = u'Rytis' user.last_name = u'Slatkevičius' user.save() &gt;&gt;&gt; Incorrect string v...
104
2010-01-21T11:34:22Z
7,983,336
<p>You can change the collation of your text field to UTF8_general_ci and the problem will be solved.</p> <p>Notice, this cannot be done in Django.</p>
6
2011-11-02T15:54:06Z
[ "python", "mysql", "django", "unicode", "utf-8" ]