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 |
|---|---|---|---|---|---|---|---|---|---|
Supplying password to wrapped-up MySQL | 619,804 | <p>Greetings.</p>
<p>I have written a little python script that calls MySQL in a subprocess. [Yes, I know that the right approach is to use MySQLdb, but compiling it under OS X Leopard is a pain, and likely more painful if I wanted to use the script on computers of different architectures.] The subprocess technique ... | 2 | 2009-03-06T17:58:49Z | 619,903 | <p>Try this:</p>
<pre><code>process.stdin.write(mysql_password + "\n")
process.communicate()
(stdout, stderr) = process.communicate( sql_statement )
process.stdin.close()
return stdout
</code></pre>
<p>Call communicate() to force what you just wrote to the buffer to send. Also, it's good to close stdin when you are ... | 0 | 2009-03-06T18:24:09Z | [
"python",
"mysql",
"subprocess"
] |
Supplying password to wrapped-up MySQL | 619,804 | <p>Greetings.</p>
<p>I have written a little python script that calls MySQL in a subprocess. [Yes, I know that the right approach is to use MySQLdb, but compiling it under OS X Leopard is a pain, and likely more painful if I wanted to use the script on computers of different architectures.] The subprocess technique ... | 2 | 2009-03-06T17:58:49Z | 619,944 | <p>The only secure method is to use a MySQL cnf file as one of the other posters mentions. You can also pass a MYSQL_PWD env variable, but that is insecure as well: <a href="http://dev.mysql.com/doc/refman/5.0/en/password-security.html" rel="nofollow">http://dev.mysql.com/doc/refman/5.0/en/password-security.html</a></p... | 3 | 2009-03-06T18:38:01Z | [
"python",
"mysql",
"subprocess"
] |
Progammatically restart windows to make system logs think user logged out | 620,154 | <p>I'm hoping to make a quick script to log-out/restart windows at a set time. For example, start a script to "Restart windows in ten minutes". For this implementation I don't need it to run in the background or pop=up on its own. I just want to set the script and walk away knowing that the computer will log-out/restar... | 4 | 2009-03-06T19:44:12Z | 620,194 | <p>The shutdown command in batch will shutdown the computer</p>
<p>-s to turn it off,
-f to force it,
-t xx to have it shutdown in x seconds,</p>
<p>use the subprocess module in python to call it.</p>
<p>Since you want it to shutdown at a specific time, to automate the job completely you'd need to use something like... | 10 | 2009-03-06T19:53:01Z | [
"python",
"windows",
"automation"
] |
Progammatically restart windows to make system logs think user logged out | 620,154 | <p>I'm hoping to make a quick script to log-out/restart windows at a set time. For example, start a script to "Restart windows in ten minutes". For this implementation I don't need it to run in the background or pop=up on its own. I just want to set the script and walk away knowing that the computer will log-out/restar... | 4 | 2009-03-06T19:44:12Z | 620,195 | <p><a href="http://technet.microsoft.com/en-us/sysinternals/bb897541.aspx" rel="nofollow">PsShutdown</a> is probably what you are looking for.</p>
| 1 | 2009-03-06T19:53:08Z | [
"python",
"windows",
"automation"
] |
Progammatically restart windows to make system logs think user logged out | 620,154 | <p>I'm hoping to make a quick script to log-out/restart windows at a set time. For example, start a script to "Restart windows in ten minutes". For this implementation I don't need it to run in the background or pop=up on its own. I just want to set the script and walk away knowing that the computer will log-out/restar... | 4 | 2009-03-06T19:44:12Z | 620,224 | <p>You can use the <a href="http://technet.microsoft.com/en-us/library/cc732503.aspx" rel="nofollow">shutdown</a> command in a batch script.</p>
| 1 | 2009-03-06T20:04:24Z | [
"python",
"windows",
"automation"
] |
Convert Year/Month/Day to Day of Year in Python | 620,305 | <p>I'm using the Python "datetime" module, i.e.:</p>
<pre><code>>>> import datetime
>>> today = datetime.datetime.now()
>>> print today
2009-03-06 13:24:58.857946
</code></pre>
<p>and I would like to compute the day of year that is sensitive of leap years. e.g. oday (March 6, 2009) is the 6... | 60 | 2009-03-06T20:34:32Z | 620,322 | <p>Couldn't you use <a href="http://docs.python.org/library/datetime.html#strftime-behavior"><code>strftime</code></a>?</p>
<pre><code>>>> import datetime
>>> today = datetime.datetime.now()
>>> print today
2009-03-06 15:37:02.484000
>>> today.strftime('%j')
'065'
</code></pre>
<h3... | 17 | 2009-03-06T20:37:27Z | [
"python",
"datetime"
] |
Convert Year/Month/Day to Day of Year in Python | 620,305 | <p>I'm using the Python "datetime" module, i.e.:</p>
<pre><code>>>> import datetime
>>> today = datetime.datetime.now()
>>> print today
2009-03-06 13:24:58.857946
</code></pre>
<p>and I would like to compute the day of year that is sensitive of leap years. e.g. oday (March 6, 2009) is the 6... | 60 | 2009-03-06T20:34:32Z | 620,345 | <p>Just subtract january 1 from the date:</p>
<pre><code>import datetime
today = datetime.datetime.now()
day_of_year = (today - datetime.datetime(today.year, 1, 1)).days + 1
</code></pre>
| 5 | 2009-03-06T20:43:04Z | [
"python",
"datetime"
] |
Convert Year/Month/Day to Day of Year in Python | 620,305 | <p>I'm using the Python "datetime" module, i.e.:</p>
<pre><code>>>> import datetime
>>> today = datetime.datetime.now()
>>> print today
2009-03-06 13:24:58.857946
</code></pre>
<p>and I would like to compute the day of year that is sensitive of leap years. e.g. oday (March 6, 2009) is the 6... | 60 | 2009-03-06T20:34:32Z | 623,312 | <p>There is a very simple solution:</p>
<pre><code>day_of_year = datetime.now().timetuple().tm_yday
</code></pre>
| 133 | 2009-03-08T09:27:24Z | [
"python",
"datetime"
] |
Convert Year/Month/Day to Day of Year in Python | 620,305 | <p>I'm using the Python "datetime" module, i.e.:</p>
<pre><code>>>> import datetime
>>> today = datetime.datetime.now()
>>> print today
2009-03-06 13:24:58.857946
</code></pre>
<p>and I would like to compute the day of year that is sensitive of leap years. e.g. oday (March 6, 2009) is the 6... | 60 | 2009-03-06T20:34:32Z | 13,032,755 | <p><a href="http://stackoverflow.com/a/623312/40785">DZinX's answer</a> is a great answer for the question. I found this question while looking for the inverse function. I found this to work :</p>
<pre><code>import datetime
datetime.datetime.strptime('1936-077T13:14:15','%Y-%jT%H:%M:%S')
>>>> datetime.d... | 7 | 2012-10-23T14:28:51Z | [
"python",
"datetime"
] |
Convert Year/Month/Day to Day of Year in Python | 620,305 | <p>I'm using the Python "datetime" module, i.e.:</p>
<pre><code>>>> import datetime
>>> today = datetime.datetime.now()
>>> print today
2009-03-06 13:24:58.857946
</code></pre>
<p>and I would like to compute the day of year that is sensitive of leap years. e.g. oday (March 6, 2009) is the 6... | 60 | 2009-03-06T20:34:32Z | 25,852,628 | <p>I want to present performance of different approaches, on Python 3.4, Linux x64. Excerpt from line profiler:</p>
<pre><code> Line # Hits Time Per Hit % Time Line Contents
==============================================================
(...)
823 1508 11334 ... | 3 | 2014-09-15T16:29:18Z | [
"python",
"datetime"
] |
Convert Year/Month/Day to Day of Year in Python | 620,305 | <p>I'm using the Python "datetime" module, i.e.:</p>
<pre><code>>>> import datetime
>>> today = datetime.datetime.now()
>>> print today
2009-03-06 13:24:58.857946
</code></pre>
<p>and I would like to compute the day of year that is sensitive of leap years. e.g. oday (March 6, 2009) is the 6... | 60 | 2009-03-06T20:34:32Z | 30,169,668 | <p>If you have reason to avoid the use of the datetime module, then these functions will work.</p>
<pre><code>def is_leap_year(year):
""" if year is a leap year return True
else return False """
if year % 100 == 0:
return year % 400 == 0
return year % 4 == 0
def doy(Y,M,D):
""" given y... | 1 | 2015-05-11T13:58:49Z | [
"python",
"datetime"
] |
How to import a Django project settings.py Python file from a sub-directory? | 620,342 | <p>I created a sub-directory of my Django project called <code>bin</code> where I want to put all command-line run Python scripts. Some of these scripts need to import my Django project <code>settings.py</code> file that is in a parent directory of <code>bin</code>.</p>
<p>How can I import the <code>settings.py</code... | 16 | 2009-03-06T20:42:11Z | 620,364 | <p>Add the parent directory to your path:</p>
<pre><code>import sys
sys.path.append('../')
import settings
</code></pre>
<p><strong>Update from comments:</strong></p>
<blockquote>
<p>Don't forget the <code>__init__.py</code> file in
the directory that has your
settings.py â S.Lott</p>
</blockquote>
| 2 | 2009-03-06T20:48:27Z | [
"python",
"django"
] |
How to import a Django project settings.py Python file from a sub-directory? | 620,342 | <p>I created a sub-directory of my Django project called <code>bin</code> where I want to put all command-line run Python scripts. Some of these scripts need to import my Django project <code>settings.py</code> file that is in a parent directory of <code>bin</code>.</p>
<p>How can I import the <code>settings.py</code... | 16 | 2009-03-06T20:42:11Z | 620,462 | <p>I think your approach may be over-complicating something that Django 1.x provides for you. As long as your project is in your python path, you can set the environment variable DJANGO_SETTINGS_MODULE at the top of your script like so:</p>
<pre><code>import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settin... | 14 | 2009-03-06T21:21:17Z | [
"python",
"django"
] |
How to import a Django project settings.py Python file from a sub-directory? | 620,342 | <p>I created a sub-directory of my Django project called <code>bin</code> where I want to put all command-line run Python scripts. Some of these scripts need to import my Django project <code>settings.py</code> file that is in a parent directory of <code>bin</code>.</p>
<p>How can I import the <code>settings.py</code... | 16 | 2009-03-06T20:42:11Z | 622,457 | <p>This is going one level up from your question, but probably the best solution here is to implement your scripts as <a href="http://docs.djangoproject.com/en/dev/howto/custom-management-commands/#howto-custom-management-commands">custom manage.py (django-admin.py) commands</a>. This gives you all of Django's functio... | 10 | 2009-03-07T20:38:00Z | [
"python",
"django"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 620,397 | <p><a href="http://docs.python.org/library/linecache.html" rel="nofollow">linecache</a>:</p>
<blockquote>
<p>The <a href="https://docs.python.org/3/library/linecache.html#module-linecache" rel="nofollow"><code>linecache</code></a> module allows one to get any line from a Python source file, while attempting to optim... | 18 | 2009-03-06T20:58:06Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 620,416 | <p>If you know in advance the position in the file (rather the line number), you can use <a href="http://docs.python.org/library/stdtypes.html#file.seek" rel="nofollow">file.seek()</a> to go to that position.</p>
<p><strong>Edit</strong>: you can use the <a href="http://docs.python.org/library/linecache.html#linecache... | 2 | 2009-03-06T21:06:13Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 620,480 | <p>I'm probably spoiled by abundant ram, but 15 M is not huge. Reading into memory with <code>readlines()</code> is what I usually do with files of this size. Accessing a line after that is trivial.</p>
| 12 | 2009-03-06T21:25:53Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 620,492 | <p>You can't jump ahead without reading in the file at least once, since you don't know where the line breaks are. You could do something like:</p>
<pre><code># Read in the file once and build a list of line offsets
line_offset = []
offset = 0
for line in file:
line_offset.append(offset)
offset += len(line)
f... | 67 | 2009-03-06T21:28:23Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 620,494 | <p>You don't really have that many options if the lines are of different length... you sadly need to process the line ending characters to know when you've progressed to the next line.</p>
<p>You can, however, dramatically speed this up AND reduce memory usage by changing the last parameter to "open" to something not ... | 19 | 2009-03-06T21:28:28Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 620,502 | <p>If you don't want to read the entire file in memory .. you may need to come up with some format other than plain text.</p>
<p>of course it all depends on what you're trying to do, and how often you will jump across the file.</p>
<p>For instance, if you're gonna be jumping to lines <em>many times</em> in the same f... | 1 | 2009-03-06T21:31:08Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 620,513 | <p>Since there is no way to determine the lenght of all lines without reading them, you have no choice but to iterate over all lines before your starting line. All you can do is to make it look nice. If the file is really huge then you might want to use a generator based approach:</p>
<pre><code>from itertools import ... | 4 | 2009-03-06T21:33:41Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 620,712 | <p>Do the lines themselves contain any index information? If the content of each line was something like "<code><line index>:Data</code>", then the <code>seek()</code> approach could be used to do a binary search through the file, even if the amount of <code>Data</code> is variable. You'd seek to the midpoint o... | 1 | 2009-03-06T22:33:30Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 621,306 | <p>Here's an example using 'readlines(sizehint)' to read a chunk of lines at a time. DNS pointed out that solution. I wrote this example because the other examples here are single-line oriented.</p>
<pre><code>def getlineno(filename, lineno):
if lineno < 1:
raise TypeError("First line is line 1")
f ... | 0 | 2009-03-07T04:24:33Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 2,727,585 | <p>What generates the file you want to process? If it is something under your control, you could generate an index (which line is at which position.) at the time the file is appended to. The index file can be of fixed line size (space padded or 0 padded numbers) and will definitely be smaller. And thus can be read and ... | 2 | 2010-04-28T07:39:40Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 24,598,751 | <p>I have had the same problem (need to retrieve from huge file specific line).</p>
<p>Surely, I can every time run through all records in file and stop it when counter will be equal to target line, but it does not work effectively in a case when you want to obtain plural number of specific rows. That caused main iss... | 1 | 2014-07-06T18:03:39Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 31,884,215 | <p>You may use mmap to find the offset of the lines. MMap seems to be the fastest way to process a file</p>
<p>example:</p>
<pre><code>with open('input_file', "r+b") as f:
mapped = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
i = 1
for line in iter(mapped.readline, ""):
if i == Line_I_want_to_jum... | 0 | 2015-08-07T18:15:27Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 32,673,701 | <p>Can use this function to return line n:</p>
<pre><code>def skipton(infile, n):
with open(infile,'r') as fi:
for i in range(n-1):
fi.next()
return fi.next()
</code></pre>
| -1 | 2015-09-19T22:05:15Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 36,174,685 | <p>If you're dealing with a <em>text file</em> & based on <em>linux system</em>, you could use the linux commands. <br>
For me, this worked well!</p>
<pre><code>import commands
def read_line(path, line=1):
return commands.getoutput('head -%s %s | tail -1' % (line, path))
line_to_jump = 141978
read_line("path... | 0 | 2016-03-23T09:47:33Z | [
"python",
"text-files"
] |
How to jump to a particular line in a huge text file? | 620,367 | <p>Are there any alternatives to the code below:</p>
<pre><code>startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
</code></pre>
<p... | 74 | 2009-03-06T20:49:52Z | 36,854,340 | <p>I am suprised no one mentioned islice</p>
<pre><code>line = next(itertools.islice(Fhandle,index_of_interest,index_of_interest+1),None) # just the one line
</code></pre>
<p>or if you want the whole rest of the file</p>
<pre><code>rest_of_file = itertools.islice(Fhandle,index_of_interest)
for line in rest_of_file:
... | 1 | 2016-04-26T02:36:41Z | [
"python",
"text-files"
] |
What's the best way to find the closest matching type to an existing type? | 620,530 | <p>I've got a registry of classes and types in Python 2.5, like so:</p>
<pre><code>class ClassA(object):
pass
class ClassB(ClassA):
pass
MY_TYPES = {
basestring : 'A string',
int : 'An integer',
ClassA : 'This is ClassA or a subclass',
}
</code></pre>
<p>I'd like to be able to pass types to a f... | 0 | 2009-03-06T21:37:18Z | 620,540 | <p>To get the superclasses of a class, use <code>__bases__</code>:</p>
<pre><code>class ClassA(object):
pass
class ClassB(ClassA):
pass
print ClassB.__bases__
(<class '__main__.ClassA'>,)
</code></pre>
<p>Beware of things like <code>int</code>, though. The base for all of those will be <code><type ... | 2 | 2009-03-06T21:41:28Z | [
"python",
"types"
] |
What's the best way to find the closest matching type to an existing type? | 620,530 | <p>I've got a registry of classes and types in Python 2.5, like so:</p>
<pre><code>class ClassA(object):
pass
class ClassB(ClassA):
pass
MY_TYPES = {
basestring : 'A string',
int : 'An integer',
ClassA : 'This is ClassA or a subclass',
}
</code></pre>
<p>I'd like to be able to pass types to a f... | 0 | 2009-03-06T21:37:18Z | 620,547 | <pre><code>from inspect import getmro
[st for cls, st in MY_TYPES.items() if cls in getmro(ClassB)]
['This is ClassA or a subclass']
</code></pre>
<p>or if you're only interested in first match(es) generator version:</p>
<pre><code>(st for cls, st in MY_TYPES.iteritems() if cls in getmro(ClassB))
</code></pre>
| 4 | 2009-03-06T21:44:48Z | [
"python",
"types"
] |
What's the best way to find the closest matching type to an existing type? | 620,530 | <p>I've got a registry of classes and types in Python 2.5, like so:</p>
<pre><code>class ClassA(object):
pass
class ClassB(ClassA):
pass
MY_TYPES = {
basestring : 'A string',
int : 'An integer',
ClassA : 'This is ClassA or a subclass',
}
</code></pre>
<p>I'd like to be able to pass types to a f... | 0 | 2009-03-06T21:37:18Z | 620,577 | <p>This is really a job for generic functions. See <a href="http://peak.telecommunity.com/DevCenter/RulesReadme" rel="nofollow">PEAK-Rules</a> and <a href="http://www.python.org/dev/peps/pep-3124/" rel="nofollow">PEP 3124</a>. Generic functions will allow you to dispatch arbitrary functions based on argument types, o... | 0 | 2009-03-06T21:56:20Z | [
"python",
"types"
] |
What's the best way to find the closest matching type to an existing type? | 620,530 | <p>I've got a registry of classes and types in Python 2.5, like so:</p>
<pre><code>class ClassA(object):
pass
class ClassB(ClassA):
pass
MY_TYPES = {
basestring : 'A string',
int : 'An integer',
ClassA : 'This is ClassA or a subclass',
}
</code></pre>
<p>I'd like to be able to pass types to a f... | 0 | 2009-03-06T21:37:18Z | 621,015 | <blockquote>
<p>(st for cls, st in MY_TYPES.iteritems() if cls in getmro(ClassB))</p>
</blockquote>
<p>The simpler way to write that would be:</p>
<pre><code>(st for cls, st in MY_TYPES.iteritems() if issubclass(ClassB, cls))
</code></pre>
<p>However this does not find the ânearestâ match; if âobjectâ were... | 1 | 2009-03-07T00:40:06Z | [
"python",
"types"
] |
SQLAlchemy Obtain Primary Key With Autoincrement Before Commit | 620,610 | <p>When I have created a table with an auto-incrementing primary key, is there a way to obtain what the primary key would be (that is, do something like reserve the primary key) without actually committing?</p>
<p>I would like to place two operations inside a transaction however one of the operations will depend on wh... | 32 | 2009-03-06T22:07:19Z | 620,784 | <p>You can use multiple transactions and manage it within scope.</p>
| 0 | 2009-03-06T22:57:00Z | [
"python",
"sql",
"sqlalchemy"
] |
SQLAlchemy Obtain Primary Key With Autoincrement Before Commit | 620,610 | <p>When I have created a table with an auto-incrementing primary key, is there a way to obtain what the primary key would be (that is, do something like reserve the primary key) without actually committing?</p>
<p>I would like to place two operations inside a transaction however one of the operations will depend on wh... | 32 | 2009-03-06T22:07:19Z | 620,831 | <p>You don't need to <code>commit</code>, you just need to <code>flush</code>. Here's some sample code. After the call to <a href="http://docs.sqlalchemy.org/en/latest/orm/session_api.html#sqlalchemy.orm.session.Session.flush">flush</a> you can access the primary key that was assigned. Note this is with SA 0.4.8.</p... | 66 | 2009-03-06T23:10:07Z | [
"python",
"sql",
"sqlalchemy"
] |
Why do I get unexpected behavior in Python isinstance after pickling? | 620,844 | <p>Putting aside whether the use of <a href="http://www.canonical.org/~kragen/isinstance/" rel="nofollow">isinstance is harmful</a>, I have run into the following conundrum when trying to evaluate isinstance after serializing/deserializing an object via Pickle:</p>
<pre><code>from __future__ import with_statement
impo... | 5 | 2009-03-06T23:17:27Z | 620,857 | <p>Change your code to print the <code>id</code> of <code>x.__class__</code> and <code>x2.__class__</code> and you'll see that they are different:</p>
<pre><code>$ python foo4.py
199876736
200015248
</code></pre>
| 2 | 2009-03-06T23:22:08Z | [
"python",
"pickle"
] |
Why do I get unexpected behavior in Python isinstance after pickling? | 620,844 | <p>Putting aside whether the use of <a href="http://www.canonical.org/~kragen/isinstance/" rel="nofollow">isinstance is harmful</a>, I have run into the following conundrum when trying to evaluate isinstance after serializing/deserializing an object via Pickle:</p>
<pre><code>from __future__ import with_statement
impo... | 5 | 2009-03-06T23:17:27Z | 620,866 | <p>The obvious answer, because its not the same class.</p>
<p>Its a similar class, but not the same.</p>
<pre><code>class myclass(object):
pass
x = myclass()
class myclass(object):
pass
y = myclass()
assert id(x.__class__) == id(y.__class__) # Will fail, not the same object
x.__class__.foo = "bar"
asse... | 4 | 2009-03-06T23:24:32Z | [
"python",
"pickle"
] |
Why do I get unexpected behavior in Python isinstance after pickling? | 620,844 | <p>Putting aside whether the use of <a href="http://www.canonical.org/~kragen/isinstance/" rel="nofollow">isinstance is harmful</a>, I have run into the following conundrum when trying to evaluate isinstance after serializing/deserializing an object via Pickle:</p>
<pre><code>from __future__ import with_statement
impo... | 5 | 2009-03-06T23:17:27Z | 620,878 | <p>This is how the unpickler works (site-packages/pickle.py):</p>
<pre><code>def find_class(self, module, name):
# Subclasses may override this
__import__(module)
mod = sys.modules[module]
klass = getattr(mod, name)
return klass
</code></pre>
<p>To find and instantiate a class.</p>
<p>So of cours... | 3 | 2009-03-06T23:28:57Z | [
"python",
"pickle"
] |
do you know of any python component(s) for syntax highlighting? | 620,954 | <p>Are there any easy to use python components that could be used in a GUI? It would be great to have something like JSyntaxPane for Python. I would like to know of python-only versions ( not interested in jython ) .</p>
| 1 | 2009-03-07T00:10:37Z | 620,975 | <p>Other than pygments? <a href="http://pygments.org/">http://pygments.org/</a></p>
| 9 | 2009-03-07T00:21:01Z | [
"python",
"components",
"syntax-highlighting"
] |
do you know of any python component(s) for syntax highlighting? | 620,954 | <p>Are there any easy to use python components that could be used in a GUI? It would be great to have something like JSyntaxPane for Python. I would like to know of python-only versions ( not interested in jython ) .</p>
| 1 | 2009-03-07T00:10:37Z | 620,983 | <p>If you're using gtk+, there's a binding of gtksourceview for Python in <a href="http://ftp.gnome.org/pub/GNOME/sources/gnome-python-extras/2.25/" rel="nofollow">gnome-python-extras</a>. It seems to work well in my experience. The downside: the documentation is less than perfect.</p>
<p>There's also a binding of ... | 1 | 2009-03-07T00:25:33Z | [
"python",
"components",
"syntax-highlighting"
] |
do you know of any python component(s) for syntax highlighting? | 620,954 | <p>Are there any easy to use python components that could be used in a GUI? It would be great to have something like JSyntaxPane for Python. I would like to know of python-only versions ( not interested in jython ) .</p>
| 1 | 2009-03-07T00:10:37Z | 621,023 | <p>You can use <a href="http://www.wxpython.org/docs/api/wx.stc.StyledTextCtrl-class.html" rel="nofollow">StyledTextCtrl</a> in <a href="http://www.wxpython.org" rel="nofollow">wxPython</a>. Check out the official demo for an example (The <em>demo code</em> tab for any demo).</p>
| 1 | 2009-03-07T00:42:14Z | [
"python",
"components",
"syntax-highlighting"
] |
do you know of any python component(s) for syntax highlighting? | 620,954 | <p>Are there any easy to use python components that could be used in a GUI? It would be great to have something like JSyntaxPane for Python. I would like to know of python-only versions ( not interested in jython ) .</p>
| 1 | 2009-03-07T00:10:37Z | 1,462,099 | <p>You say "in a GUI app" but don't mention the toolkit.</p>
<p>If you are using PyQt, and need a read-only widget, you can use QWebKit which has a whole HTML widget in it based on WebKit, so it supports pretty much anything, from flash to the ACID2 test.</p>
<p>If you want a read-write widget, Qt's QTextEdit support... | 0 | 2009-09-22T19:26:34Z | [
"python",
"components",
"syntax-highlighting"
] |
Python function: Find Change from purchase amount | 621,062 | <p>I'm looking for the most efficient way to figure out a change amount (Quarters, dimes, nickels, and pennies) from a purchase amount. The purchase amount must be less than $1, and the change is from one dollar. I need to know how many quarters, dimes, nickels, and pennies someone would get back. </p>
<p>Would it be ... | -2 | 2009-03-07T00:58:59Z | 621,079 | <p>Your best bet is to probably have a sorted dictionary of coin sizes, and then loop through them checking if your change is greater than the value, add that coin and subtract the value, otherwise move along to the next row in the dictionary. </p>
<p>Eg</p>
<pre><code>Coins = [50, 25, 10, 5, 2, 1]
ChangeDue = 87
Coi... | 0 | 2009-03-07T01:11:34Z | [
"python",
"dictionary",
"coin-change"
] |
Python function: Find Change from purchase amount | 621,062 | <p>I'm looking for the most efficient way to figure out a change amount (Quarters, dimes, nickels, and pennies) from a purchase amount. The purchase amount must be less than $1, and the change is from one dollar. I need to know how many quarters, dimes, nickels, and pennies someone would get back. </p>
<p>Would it be ... | -2 | 2009-03-07T00:58:59Z | 621,118 | <p>Gee, you mean this isn't problem 2b in every programming course any more? Eh, probably not, they don't seem to teach people how to make change any more either. (Or maybe they do: is this a homework assignment?)</p>
<p>If you find someone over about 50 and have them make change for you, it works like this. Say yo... | 7 | 2009-03-07T01:36:04Z | [
"python",
"dictionary",
"coin-change"
] |
Python function: Find Change from purchase amount | 621,062 | <p>I'm looking for the most efficient way to figure out a change amount (Quarters, dimes, nickels, and pennies) from a purchase amount. The purchase amount must be less than $1, and the change is from one dollar. I need to know how many quarters, dimes, nickels, and pennies someone would get back. </p>
<p>Would it be ... | -2 | 2009-03-07T00:58:59Z | 621,133 | <p>This is probably pretty fast - just a few operations per denomination:</p>
<pre><code>def change(amount):
money = ()
for coin in [25,10,5,1]
num = amount/coin
money += (coin,) * num
amount -= coin * num
return money
</code></pre>
| 4 | 2009-03-07T01:49:36Z | [
"python",
"dictionary",
"coin-change"
] |
Python function: Find Change from purchase amount | 621,062 | <p>I'm looking for the most efficient way to figure out a change amount (Quarters, dimes, nickels, and pennies) from a purchase amount. The purchase amount must be less than $1, and the change is from one dollar. I need to know how many quarters, dimes, nickels, and pennies someone would get back. </p>
<p>Would it be ... | -2 | 2009-03-07T00:58:59Z | 631,502 | <p>This problem could be solved pretty easy with integer partitions from number theory. I wrote a recursive function that takes a number and a list of partitions and returns the number of possible combinations that would make up the given number.</p>
<p><a href="http://sandboxrichard.blogspot.com/2009/03/integer-parti... | 0 | 2009-03-10T17:46:29Z | [
"python",
"dictionary",
"coin-change"
] |
Python function: Find Change from purchase amount | 621,062 | <p>I'm looking for the most efficient way to figure out a change amount (Quarters, dimes, nickels, and pennies) from a purchase amount. The purchase amount must be less than $1, and the change is from one dollar. I need to know how many quarters, dimes, nickels, and pennies someone would get back. </p>
<p>Would it be ... | -2 | 2009-03-07T00:58:59Z | 26,681,495 | <p>The above soloution working.</p>
<pre><code>amount=int(input("Please enter amount in pence"))
coins = [50, 25, 10, 5, 2, 1]
coinsReturned = []
for i in coins:
while amount >=i:
coinsReturned.append(i)
amount = amount - i
print(coinsReturned)
</code></pre>
<p>Alternatively a solution can reache... | 0 | 2014-10-31T18:47:45Z | [
"python",
"dictionary",
"coin-change"
] |
Django Forms Newbie Question | 621,121 | <p>Alright, I'm at a loss with the Django Forms, as the documentation just doesn't seem to quite cover what I'm looking for. At least it seems to come to a screeching halt once you get past the most rudimentary of forms. I'm more than willing to take a link to <em>good</em> documentation, or a link to a good book that ... | 4 | 2009-03-07T01:40:11Z | 621,254 | <p>First, you create a ModelForm for a given Model. In this example I'm doing it for Quiz but you can rinse and repeat for your other models. For giggles, I'm making the "label" be a Select box with preset choices:</p>
<pre><code>from django.models import BaseModel
from django import forms
from django.forms import Mod... | 2 | 2009-03-07T03:33:42Z | [
"python",
"django",
"forms"
] |
Django Forms Newbie Question | 621,121 | <p>Alright, I'm at a loss with the Django Forms, as the documentation just doesn't seem to quite cover what I'm looking for. At least it seems to come to a screeching halt once you get past the most rudimentary of forms. I'm more than willing to take a link to <em>good</em> documentation, or a link to a good book that ... | 4 | 2009-03-07T01:40:11Z | 622,328 | <p>Yeah I have to agree the documentation and examples are really lacking here. The is no out of the box solution for the case you are describing because it goes three layers deep: quiz->question->answer.</p>
<p>Django has <a href="http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets">model in... | 6 | 2009-03-07T19:13:19Z | [
"python",
"django",
"forms"
] |
Accessing a MySQL database from python | 621,156 | <p>I have been trying for the past several hours to find a working method of accessing a mysql database in python. The only thing that I've managed to get to compile and install is <code>pyodbc</code> but the necessary driver is not available for ppc leopard. </p>
<p>I already know about <a href="http://stackoverflow... | 0 | 2009-03-07T02:06:35Z | 621,192 | <p>Install fink. It includes the MySQLdb package.</p>
| 1 | 2009-03-07T02:33:31Z | [
"python",
"mysql",
"database"
] |
Accessing a MySQL database from python | 621,156 | <p>I have been trying for the past several hours to find a working method of accessing a mysql database in python. The only thing that I've managed to get to compile and install is <code>pyodbc</code> but the necessary driver is not available for ppc leopard. </p>
<p>I already know about <a href="http://stackoverflow... | 0 | 2009-03-07T02:06:35Z | 621,284 | <p>Try SQL Alchemy.</p>
<p>It is awesome.</p>
| 2 | 2009-03-07T04:06:18Z | [
"python",
"mysql",
"database"
] |
Accessing a MySQL database from python | 621,156 | <p>I have been trying for the past several hours to find a working method of accessing a mysql database in python. The only thing that I've managed to get to compile and install is <code>pyodbc</code> but the necessary driver is not available for ppc leopard. </p>
<p>I already know about <a href="http://stackoverflow... | 0 | 2009-03-07T02:06:35Z | 709,942 | <blockquote>
<p>UPDATE: Now I've gotten sqlalchemy to
install but while it will show up when
called by the command line it won't
import when used in my cgi script.</p>
</blockquote>
<p>Can you verify that the Python being invoked from your CGI script is the same as the one you get when you run Python interacti... | 0 | 2009-04-02T14:30:53Z | [
"python",
"mysql",
"database"
] |
Another Django Forms : Foreign Key in Hidden Field | 621,212 | <p>Another Django Form question.</p>
<p>My form :</p>
<pre><code>class PlanForm(forms.ModelForm):
owner = forms.ModelChoiceField(label="",
queryset=Profile.objects.all(),
widget=forms.HiddenInput())
etc...
class Meta:
model =... | 8 | 2009-03-07T02:59:08Z | 621,651 | <p>There's usually no need to put related object into form field. There's a better way and this is specifying parent id in form URL.</p>
<p>Let's assume you need to render a form for new Plan object and then create one when form is bubmitted. Here's how your urlconf would look like:</p>
<pre><code>(r"/profile/(?P&l... | 1 | 2009-03-07T10:37:24Z | [
"python",
"django",
"forms"
] |
Another Django Forms : Foreign Key in Hidden Field | 621,212 | <p>Another Django Form question.</p>
<p>My form :</p>
<pre><code>class PlanForm(forms.ModelForm):
owner = forms.ModelChoiceField(label="",
queryset=Profile.objects.all(),
widget=forms.HiddenInput())
etc...
class Meta:
model =... | 8 | 2009-03-07T02:59:08Z | 621,804 | <p>When you assign a Profile object to the form, Django stringifies it and uses the output as the value in the form. What you would except though, is for Django to use the ID of the object instead. </p>
<p>Luckily, the workaround is simple: Just give the form primary key values of the Profile objects instead:</p>
<pr... | 6 | 2009-03-07T12:57:27Z | [
"python",
"django",
"forms"
] |
Another Django Forms : Foreign Key in Hidden Field | 621,212 | <p>Another Django Form question.</p>
<p>My form :</p>
<pre><code>class PlanForm(forms.ModelForm):
owner = forms.ModelChoiceField(label="",
queryset=Profile.objects.all(),
widget=forms.HiddenInput())
etc...
class Meta:
model =... | 8 | 2009-03-07T02:59:08Z | 621,917 | <p>I suspect that the <code>__unicode__</code> method for the Profile model instance, or the <code>repr</code> thereof is set to return a value other than <code>self.id</code>. For example, I just set this up:</p>
<pre><code># models.py
class Profile(models.Model):
name = models.CharField('profile name', max_lengt... | 15 | 2009-03-07T14:40:39Z | [
"python",
"django",
"forms"
] |
Another Django Forms : Foreign Key in Hidden Field | 621,212 | <p>Another Django Form question.</p>
<p>My form :</p>
<pre><code>class PlanForm(forms.ModelForm):
owner = forms.ModelChoiceField(label="",
queryset=Profile.objects.all(),
widget=forms.HiddenInput())
etc...
class Meta:
model =... | 8 | 2009-03-07T02:59:08Z | 621,973 | <p>Hmm... </p>
<p>This might actually be a security hole.</p>
<p>Suppose a malicious attacker crafted a POST (say, by using XmlHttpRequest from FireBug) and set the profile term to some wacky value, like, your profile ID. Probably not what you wanted?</p>
<p>If possible, you may want to get the profile from the req... | 7 | 2009-03-07T15:14:33Z | [
"python",
"django",
"forms"
] |
Another Django Forms : Foreign Key in Hidden Field | 621,212 | <p>Another Django Form question.</p>
<p>My form :</p>
<pre><code>class PlanForm(forms.ModelForm):
owner = forms.ModelChoiceField(label="",
queryset=Profile.objects.all(),
widget=forms.HiddenInput())
etc...
class Meta:
model =... | 8 | 2009-03-07T02:59:08Z | 13,479,147 | <p>Since ModelChoiceField inherits from ChoiceFIeld, you should use the <a href="https://docs.djangoproject.com/en/1.4/ref/forms/widgets/#multiplehiddeninput" rel="nofollow">MultipleHiddenInput</a> widget for this:</p>
<pre class="lang-python prettyprint-override"><code>class PlanForm(forms.ModelForm):
owner = f... | 2 | 2012-11-20T17:54:01Z | [
"python",
"django",
"forms"
] |
How to print tuples of unicode strings in original language (not u'foo' form) | 621,321 | <p>I have a list of tuples of unicode objects:</p>
<pre><code>>>> t = [('äº',), ('ç¬',)]
</code></pre>
<p>Printing this out, I get:</p>
<pre><code>>>> print t
[('\xe4\xba\x80',), ('\xe7\x8a\xac',)]
</code></pre>
<p>which I guess is a list of the utf-8 byte-code representation of those strings?</... | 10 | 2009-03-07T04:44:52Z | 621,329 | <p>Try:</p>
<pre><code>import codecs, sys
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
</code></pre>
| 0 | 2009-03-07T04:53:25Z | [
"python",
"unicode"
] |
How to print tuples of unicode strings in original language (not u'foo' form) | 621,321 | <p>I have a list of tuples of unicode objects:</p>
<pre><code>>>> t = [('äº',), ('ç¬',)]
</code></pre>
<p>Printing this out, I get:</p>
<pre><code>>>> print t
[('\xe4\xba\x80',), ('\xe7\x8a\xac',)]
</code></pre>
<p>which I guess is a list of the utf-8 byte-code representation of those strings?</... | 10 | 2009-03-07T04:44:52Z | 621,332 | <p>Python source code files are strictly ASCII, so you must use the <code>\u</code> escape sequences unless you specify an encoding. See <a href="http://www.python.org/dev/peps/pep-0263/" rel="nofollow">PEP 0263</a>.</p>
<pre><code>#!/usr/bin/python
# coding=utf-8
t = [u'äº', u'ç¬']
print t
</code></pre>
<p>When yo... | 2 | 2009-03-07T04:55:28Z | [
"python",
"unicode"
] |
How to print tuples of unicode strings in original language (not u'foo' form) | 621,321 | <p>I have a list of tuples of unicode objects:</p>
<pre><code>>>> t = [('äº',), ('ç¬',)]
</code></pre>
<p>Printing this out, I get:</p>
<pre><code>>>> print t
[('\xe4\xba\x80',), ('\xe7\x8a\xac',)]
</code></pre>
<p>which I guess is a list of the utf-8 byte-code representation of those strings?</... | 10 | 2009-03-07T04:44:52Z | 621,351 | <p>So this appears to do what I want:</p>
<pre><code>print '[%s]' % ', '.join([', '.join('(%s,)' % ', '.join(ti) for ti in t)])
>>> t = [('äº',), ('ç¬',)]
>>> print t
[('\xe4\xba\x80',), ('\xe7\x8a\xac',)]
>>> print '[%s]' % ', '.join([', '.join('(%s,)' % ', '.join(ti) for ti in t)])
[(ä... | 0 | 2009-03-07T05:12:18Z | [
"python",
"unicode"
] |
How to print tuples of unicode strings in original language (not u'foo' form) | 621,321 | <p>I have a list of tuples of unicode objects:</p>
<pre><code>>>> t = [('äº',), ('ç¬',)]
</code></pre>
<p>Printing this out, I get:</p>
<pre><code>>>> print t
[('\xe4\xba\x80',), ('\xe7\x8a\xac',)]
</code></pre>
<p>which I guess is a list of the utf-8 byte-code representation of those strings?</... | 10 | 2009-03-07T04:44:52Z | 621,792 | <p>First, there's a slight misunderstanding in your post. If you define a list like this:</p>
<pre><code>>>> t = [('äº',), ('ç¬',)]
</code></pre>
<p>...those are not <code>unicode</code>s you define, but <code>str</code>s. If you want to have <code>unicode</code> types, you have to add a <code>u</code> bef... | 3 | 2009-03-07T12:45:51Z | [
"python",
"unicode"
] |
How to print tuples of unicode strings in original language (not u'foo' form) | 621,321 | <p>I have a list of tuples of unicode objects:</p>
<pre><code>>>> t = [('äº',), ('ç¬',)]
</code></pre>
<p>Printing this out, I get:</p>
<pre><code>>>> print t
[('\xe4\xba\x80',), ('\xe7\x8a\xac',)]
</code></pre>
<p>which I guess is a list of the utf-8 byte-code representation of those strings?</... | 10 | 2009-03-07T04:44:52Z | 621,798 | <blockquote>
<p>but what I want to see printed out is, surprise:</p>
<p>[('äº',), ('ç¬',)]</p>
</blockquote>
<p>What do you want to see it printed out on? Because if it's the console, it's not at all guaranteed your console can display those characters. This is why Python's ârepr()â representation of obje... | 6 | 2009-03-07T12:49:29Z | [
"python",
"unicode"
] |
How to print tuples of unicode strings in original language (not u'foo' form) | 621,321 | <p>I have a list of tuples of unicode objects:</p>
<pre><code>>>> t = [('äº',), ('ç¬',)]
</code></pre>
<p>Printing this out, I get:</p>
<pre><code>>>> print t
[('\xe4\xba\x80',), ('\xe7\x8a\xac',)]
</code></pre>
<p>which I guess is a list of the utf-8 byte-code representation of those strings?</... | 10 | 2009-03-07T04:44:52Z | 23,022,825 | <p>It seems people are missing what people want here. When I print unicode from a tuple, I just want to get rid of the 'u' '[' '(' and quotes. What we want is a function like below.
After scouring the Net it seems to be the cleanest way to get atomic displayable data.
If the data is not in a tuple or list, I don't thi... | 0 | 2014-04-11T21:23:08Z | [
"python",
"unicode"
] |
How to slice a list from an element n to the end in python? | 621,354 | <p>I'm having some trouble figuring out how to slice python lists, it is illustrated as follows:</p>
<pre><code>>>> test = range(10)
>>> test
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> test[3:-1]
[3, 4, 5, 6, 7, 8]
>>> test[3:0]
[]
>>> test[3:1]
[]
>>> test
[0, 1, 2, 3, 4,... | 14 | 2009-03-07T05:17:34Z | 621,357 | <p>You can leave one end of the slice open by not specifying the value.</p>
<pre><code>test[3:] = [3, 4, 5, 6, 7, 8, 9]
test[:3] = [0, 1, 2]
</code></pre>
| 33 | 2009-03-07T05:19:31Z | [
"python"
] |
How to slice a list from an element n to the end in python? | 621,354 | <p>I'm having some trouble figuring out how to slice python lists, it is illustrated as follows:</p>
<pre><code>>>> test = range(10)
>>> test
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> test[3:-1]
[3, 4, 5, 6, 7, 8]
>>> test[3:0]
[]
>>> test[3:1]
[]
>>> test
[0, 1, 2, 3, 4,... | 14 | 2009-03-07T05:17:34Z | 621,358 | <p>Simply omit the end.</p>
<pre><code>test[n:]
</code></pre>
| 6 | 2009-03-07T05:19:40Z | [
"python"
] |
How to slice a list from an element n to the end in python? | 621,354 | <p>I'm having some trouble figuring out how to slice python lists, it is illustrated as follows:</p>
<pre><code>>>> test = range(10)
>>> test
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> test[3:-1]
[3, 4, 5, 6, 7, 8]
>>> test[3:0]
[]
>>> test[3:1]
[]
>>> test
[0, 1, 2, 3, 4,... | 14 | 2009-03-07T05:17:34Z | 621,521 | <p>If you're using a variable as the range endpoint, you can use <code>None</code>.</p>
<pre><code> start = 4
end = None
test[start:end]
</code></pre>
| 19 | 2009-03-07T08:15:31Z | [
"python"
] |
How to slice a list from an element n to the end in python? | 621,354 | <p>I'm having some trouble figuring out how to slice python lists, it is illustrated as follows:</p>
<pre><code>>>> test = range(10)
>>> test
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> test[3:-1]
[3, 4, 5, 6, 7, 8]
>>> test[3:0]
[]
>>> test[3:1]
[]
>>> test
[0, 1, 2, 3, 4,... | 14 | 2009-03-07T05:17:34Z | 35,066,475 | <p>Leaving out the end still works when you want to skip some:</p>
<pre><code>range(10)[3::2] = [4, 6, 8]
</code></pre>
| 1 | 2016-01-28T16:14:17Z | [
"python"
] |
What does re.compile(r'[[]]') match? | 621,560 | <p>With Python's re module, why do the following act differently:</p>
<pre><code>>>> r = re.compile(r'[][]')
>>> r.findall(r'[]')
['[', ']']
>>> r = re.compile(r'[[]]')
>>> r.findall(r'[]')
['[]']
>>> r.findall(r'][')
[]
</code></pre>
| 4 | 2009-03-07T08:59:27Z | 621,568 | <p>The regular expression "[[]]" matches the substring "[]". The first [ in the expression begins a character class, and the first ] ends it. There is only one character ([) in the class, and then it has to be followed by the second ]. So the expression is "any of the characters in "[", followed by a "]".</p>
| 16 | 2009-03-07T09:11:20Z | [
"python",
"regex"
] |
What does re.compile(r'[[]]') match? | 621,560 | <p>With Python's re module, why do the following act differently:</p>
<pre><code>>>> r = re.compile(r'[][]')
>>> r.findall(r'[]')
['[', ']']
>>> r = re.compile(r'[[]]')
>>> r.findall(r'[]')
['[]']
>>> r.findall(r'][')
[]
</code></pre>
| 4 | 2009-03-07T08:59:27Z | 621,602 | <p>and r'[][]' forms a character class {'[',']'}, and match either '[' or ']'.</p>
| 0 | 2009-03-07T09:47:53Z | [
"python",
"regex"
] |
What does re.compile(r'[[]]') match? | 621,560 | <p>With Python's re module, why do the following act differently:</p>
<pre><code>>>> r = re.compile(r'[][]')
>>> r.findall(r'[]')
['[', ']']
>>> r = re.compile(r'[[]]')
>>> r.findall(r'[]')
['[]']
>>> r.findall(r'][')
[]
</code></pre>
| 4 | 2009-03-07T08:59:27Z | 621,608 | <p>Character classes begin with a <strong><code>[</code></strong> and end with the first <strong><code>]</code></strong>.</p>
<p>So the expression <code>[][]</code> is a character class with the characters <code>]</code> and <code>[</code> as character classes must not be empty: <strong><code>[</code></strong><code>]... | 4 | 2009-03-07T09:56:40Z | [
"python",
"regex"
] |
Python and random keys of 21 char max | 621,649 | <p>I am using an api which takes a name of 21 char max to represent an internal session which has a lifetime of around "two days". I would like the name not to be meaningfull using some kind of hasing ? md5 generates 40 chars, is there something else i could use ?</p>
<p>For now i use 'userid[:10]' + creation time: dd... | 7 | 2009-03-07T10:36:54Z | 621,653 | <p>Why not take first 21 chars from md5 or SHA1 hash?</p>
| 2 | 2009-03-07T10:38:26Z | [
"python",
"encryption",
"key"
] |
Python and random keys of 21 char max | 621,649 | <p>I am using an api which takes a name of 21 char max to represent an internal session which has a lifetime of around "two days". I would like the name not to be meaningfull using some kind of hasing ? md5 generates 40 chars, is there something else i could use ?</p>
<p>For now i use 'userid[:10]' + creation time: dd... | 7 | 2009-03-07T10:36:54Z | 621,658 | <p>The hexadecimal representation of MD5 has very poor randomness: you only get 4 bits of entropy per character.</p>
<p>Use random characters, something like:</p>
<pre><code>import random
import string
"".join([random.choice(string.ascii_letters + string.digits + ".-")
for i in xrange(21)])
</code></pre>
<p>... | 4 | 2009-03-07T10:45:36Z | [
"python",
"encryption",
"key"
] |
Python and random keys of 21 char max | 621,649 | <p>I am using an api which takes a name of 21 char max to represent an internal session which has a lifetime of around "two days". I would like the name not to be meaningfull using some kind of hasing ? md5 generates 40 chars, is there something else i could use ?</p>
<p>For now i use 'userid[:10]' + creation time: dd... | 7 | 2009-03-07T10:36:54Z | 621,668 | <p>Characters, or bytes? If it takes arbitrary strings, you can just use the bytes and not worry about expanding to readable characters (for which base64 would be better than hex anyway).</p>
<p>MD5 generates 16 chars if you don't use the hexadecimal expansion of it. SHA1 generates 20 under the same condition.</p>
<p... | 0 | 2009-03-07T10:54:19Z | [
"python",
"encryption",
"key"
] |
Python and random keys of 21 char max | 621,649 | <p>I am using an api which takes a name of 21 char max to represent an internal session which has a lifetime of around "two days". I would like the name not to be meaningfull using some kind of hasing ? md5 generates 40 chars, is there something else i could use ?</p>
<p>For now i use 'userid[:10]' + creation time: dd... | 7 | 2009-03-07T10:36:54Z | 621,770 | <p>If I read your question correctly, you want to generate some arbitrary identifier token which must be 21 characters max. Does it need to be highly resistant to guessing? The example you gave isn't "crytographically strong" in that it can be guessed by searching well less than 1/2 of the entire possible keyspace. </p... | 23 | 2009-03-07T12:26:47Z | [
"python",
"encryption",
"key"
] |
Python and random keys of 21 char max | 621,649 | <p>I am using an api which takes a name of 21 char max to represent an internal session which has a lifetime of around "two days". I would like the name not to be meaningfull using some kind of hasing ? md5 generates 40 chars, is there something else i could use ?</p>
<p>For now i use 'userid[:10]' + creation time: dd... | 7 | 2009-03-07T10:36:54Z | 1,567,663 | <p>The base64 module can do URL-safe encoding. So, if needed, instead of </p>
<pre><code>u.bytes.encode("base64")
</code></pre>
<p>you could do</p>
<pre><code>import base64
token = base64.urlsafe_b64encode(u.bytes)
</code></pre>
<p>and, conveniently, to convert back</p>
<pre><code>u = uuid.UUID(bytes=base64.urlsa... | 2 | 2009-10-14T17:03:44Z | [
"python",
"encryption",
"key"
] |
Choosing and deploying a comet server | 621,802 | <p>I want to push data to the browser over HTTP without killing my django/python application.</p>
<p>I decided to use a comet server, to proxy requests between my application and the client (though I still haven't really figured it out properly).</p>
<p>I've looked into the following engines:
orbited
cometd
ejabberd
... | 13 | 2009-03-07T12:51:53Z | 622,013 | <p>I need to do something very similar. I found this, but haven't had the time to look at it properly yet:</p>
<blockquote>
<p>django_evserver is simple http server
for Django applications. It's based on
libevent library. The main advantage
of django_evserver is that it provides
methods of preempting django ... | 2 | 2009-03-07T15:44:34Z | [
"python",
"django",
"comet",
"daemon"
] |
Choosing and deploying a comet server | 621,802 | <p>I want to push data to the browser over HTTP without killing my django/python application.</p>
<p>I decided to use a comet server, to proxy requests between my application and the client (though I still haven't really figured it out properly).</p>
<p>I've looked into the following engines:
orbited
cometd
ejabberd
... | 13 | 2009-03-07T12:51:53Z | 622,509 | <p>I would recommend looking into Twisted, their twisted.web server, and the comet work done on top of it at Divmod. They can handle far more concurrent connections than traditional thread or process based servers, which is exactly what you need for something like this. And, yes, I've architected systems using Twisted ... | 5 | 2009-03-07T21:06:49Z | [
"python",
"django",
"comet",
"daemon"
] |
Choosing and deploying a comet server | 621,802 | <p>I want to push data to the browser over HTTP without killing my django/python application.</p>
<p>I decided to use a comet server, to proxy requests between my application and the client (though I still haven't really figured it out properly).</p>
<p>I've looked into the following engines:
orbited
cometd
ejabberd
... | 13 | 2009-03-07T12:51:53Z | 1,159,813 | <p>If you can run Java I would recommend <a href="http://www.stream-hub.com/" rel="nofollow">StreamHub Comet Server</a>. </p>
<p>Firstly, regarding your need not to 'kill' your existing application, the JavaScript include has a really low footprint at less than 10K. I use it for pushing user updates and chat on the s... | 2 | 2009-07-21T15:09:20Z | [
"python",
"django",
"comet",
"daemon"
] |
Choosing and deploying a comet server | 621,802 | <p>I want to push data to the browser over HTTP without killing my django/python application.</p>
<p>I decided to use a comet server, to proxy requests between my application and the client (though I still haven't really figured it out properly).</p>
<p>I've looked into the following engines:
orbited
cometd
ejabberd
... | 13 | 2009-03-07T12:51:53Z | 1,167,542 | <p>One option is Netty, client-server socket framework based on Java NIO from JBoss. For a comparison and discussion <a href="http://amix.dk/blog/viewEntry/19456" rel="nofollow">see here</a>. It reportedly handles 100000 simultaneous open connections on a quad-core server. </p>
| 2 | 2009-07-22T19:14:58Z | [
"python",
"django",
"comet",
"daemon"
] |
Choosing and deploying a comet server | 621,802 | <p>I want to push data to the browser over HTTP without killing my django/python application.</p>
<p>I decided to use a comet server, to proxy requests between my application and the client (though I still haven't really figured it out properly).</p>
<p>I've looked into the following engines:
orbited
cometd
ejabberd
... | 13 | 2009-03-07T12:51:53Z | 1,689,258 | <p>If you're running IIS, you can check out WebSync (http://www.frozenmountain.com/websync), a standards-compliant (bayeux) comet server and client for .NET/IIS. If you don't want the additional load, the On-Demand version is a SaaS option that offloads the heavy lifting.</p>
| 2 | 2009-11-06T17:59:01Z | [
"python",
"django",
"comet",
"daemon"
] |
Choosing and deploying a comet server | 621,802 | <p>I want to push data to the browser over HTTP without killing my django/python application.</p>
<p>I decided to use a comet server, to proxy requests between my application and the client (though I still haven't really figured it out properly).</p>
<p>I've looked into the following engines:
orbited
cometd
ejabberd
... | 13 | 2009-03-07T12:51:53Z | 1,818,372 | <p>If you're looking to combine Django with a Comet server (Orbited), check this project I have going to integrate Django and Orbited in as "clean" and "real-world" as possible here: <a href="http://github.com/clemesha/hotdot" rel="nofollow">http://github.com/clemesha/hotdot</a></p>
<p>The project addresses "real-worl... | 2 | 2009-11-30T07:45:54Z | [
"python",
"django",
"comet",
"daemon"
] |
.cgi problem with web server | 621,874 | <p><a href="http://www.djangobook.com/en/2.0/chapter01/" rel="nofollow">The code</a></p>
<pre><code>#!/usr/bin/env python
import MySQLdb
print "Content-Type: text/html"
print
print "<html><head><title>Books</title></head>"
print "<body>" print "<h1>Books</h1>"
print ... | 1 | 2009-03-07T14:01:33Z | 621,885 | <p>I've tidied up the code a bit by inserting linebreaks where necessary and replacing smart quotes with <code>"</code> and <code>'</code>. Do you have any more luck with the following? Can you run it from a terminal just by typing <code>python test.cgi</code>?</p>
<pre><code>#!/usr/bin/env python
import MySQLdb
p... | 2 | 2009-03-07T14:12:58Z | [
"python",
"cgi"
] |
.cgi problem with web server | 621,874 | <p><a href="http://www.djangobook.com/en/2.0/chapter01/" rel="nofollow">The code</a></p>
<pre><code>#!/usr/bin/env python
import MySQLdb
print "Content-Type: text/html"
print
print "<html><head><title>Books</title></head>"
print "<body>" print "<h1>Books</h1>"
print ... | 1 | 2009-03-07T14:01:33Z | 621,928 | <p>Any number of issues can cause the error you are seeing:</p>
<ol>
<li>Is <code>test.cgi</code> executable (chmod 755) on the server?</li>
<li>Is the directory in which you placed <code>test.cgi</code> designated as a <a href="http://httpd.apache.org/docs/2.2/mod/mod%5Falias.html#scriptalias" rel="nofollow">ScriptAl... | 2 | 2009-03-07T14:49:48Z | [
"python",
"cgi"
] |
.cgi problem with web server | 621,874 | <p><a href="http://www.djangobook.com/en/2.0/chapter01/" rel="nofollow">The code</a></p>
<pre><code>#!/usr/bin/env python
import MySQLdb
print "Content-Type: text/html"
print
print "<html><head><title>Books</title></head>"
print "<body>" print "<h1>Books</h1>"
print ... | 1 | 2009-03-07T14:01:33Z | 622,085 | <p>The error in <a href="http://dpaste.com/8866/" rel="nofollow">http://dpaste.com/8866/</a> is occurring because you are using "curly quotes" instead of standard ASCII quotation marks.</p>
<p>You'll want to replace the â and â with ". Just use find and replace in your text editor.</p>
| 1 | 2009-03-07T16:30:45Z | [
"python",
"cgi"
] |
.cgi problem with web server | 621,874 | <p><a href="http://www.djangobook.com/en/2.0/chapter01/" rel="nofollow">The code</a></p>
<pre><code>#!/usr/bin/env python
import MySQLdb
print "Content-Type: text/html"
print
print "<html><head><title>Books</title></head>"
print "<body>" print "<h1>Books</h1>"
print ... | 1 | 2009-03-07T14:01:33Z | 622,295 | <p>Make sure you're saving the file with correct line endings. i.e. LF only on unix, or CR/LF for Windows. I just recently had the exact same problem...</p>
| 0 | 2009-03-07T18:50:47Z | [
"python",
"cgi"
] |
Unable to import python-mysqldb | 621,968 | <p>I installed python-mysqldb using </p>
<pre><code>sudo apt-get install python-mysqldb
</code></pre>
<p>I have tried to use it unsuccessfully. The following commands do not work in Python prompt:</p>
<pre><code>import python-mysqldb
</code></pre>
<p>Or</p>
<pre><code>import mysqldb
</code></pre>
<p>How can I sol... | 2 | 2009-03-07T15:12:02Z | 621,972 | <p>The package name is "MySQLdb". Case counts.</p>
| 10 | 2009-03-07T15:13:48Z | [
"python",
"import",
"mysql"
] |
Python Reflection and Type Conversion | 622,161 | <p>In Python, functions like str(), int(), float(), etc. are generally used to perform type conversions. However, these require you to know at development time what type you want to convert to. A subproblem of some Python code I'm trying to write is as follows:</p>
<p>Given two variables, <code>foo</code> and <code>... | 7 | 2009-03-07T17:15:41Z | 622,173 | <p>Use <code>foo.__class__</code> to get the type of <code>foo</code>. Then call it to convert bar to the type of foo:</p>
<pre><code>def conv(foo, bar) :
return foo.__class__(bar)
</code></pre>
<p>Of course, this only works if the type of <code>foo</code> has a constructor that knows how to convert bar.</p>
| 11 | 2009-03-07T17:21:42Z | [
"python",
"reflection",
"type-conversion",
"introspection",
"python-datamodel"
] |
Python Reflection and Type Conversion | 622,161 | <p>In Python, functions like str(), int(), float(), etc. are generally used to perform type conversions. However, these require you to know at development time what type you want to convert to. A subproblem of some Python code I'm trying to write is as follows:</p>
<p>Given two variables, <code>foo</code> and <code>... | 7 | 2009-03-07T17:15:41Z | 622,217 | <p>that would be best:</p>
<pre><code>type(foo)(bar)
</code></pre>
| 16 | 2009-03-07T17:57:32Z | [
"python",
"reflection",
"type-conversion",
"introspection",
"python-datamodel"
] |
Python Reflection and Type Conversion | 622,161 | <p>In Python, functions like str(), int(), float(), etc. are generally used to perform type conversions. However, these require you to know at development time what type you want to convert to. A subproblem of some Python code I'm trying to write is as follows:</p>
<p>Given two variables, <code>foo</code> and <code>... | 7 | 2009-03-07T17:15:41Z | 8,390,964 | <pre><code>type(foo)
</code></pre>
<p>is definitely the best, if you want to check a type then "type" is the best method, and I am on <code>python 2.7 - 3.2.2, type()</code> works perfectly.</p>
| 0 | 2011-12-05T19:50:20Z | [
"python",
"reflection",
"type-conversion",
"introspection",
"python-datamodel"
] |
How to change cursor position of wxRichTextCtrl in event handler? | 622,417 | <p>I have a RichTextCtrl in my application, that has a handler for <code>EVT_KEY_DOWN</code>. The code that is executed is the following :</p>
<pre><code>
def move_caret(self):
pdb.set_trace()
self.rich.GetCaret().Move((0,0))
self.Refresh()
def onClick(self,event):
self.move_caret()
event.Skip()
... | 1 | 2009-03-07T20:18:27Z | 623,292 | <p>Apparently, there are two problems with your code:</p>
<ol>
<li><p>You listen on <code>EVT_KEY_DOWN</code>, which is probably handled before <code>EVT_TEXT</code>, whose default handler sets the cursor position.</p></li>
<li><p>You modify the <code>Caret</code> object instead of using <code>SetInsertionPoint</code>... | 3 | 2009-03-08T09:00:18Z | [
"python",
"events",
"wxpython",
"wxwidgets"
] |
Increment Page Hit Count in Django | 622,652 | <p>I have a table with an <code>IntegerField</code> (<code>hit_count</code>), and when a page is visited (for example, <code>http://site/page/3</code>) I want record ID 3's <code>hit_count</code> column in the database to increment by 1.</p>
<p>The query should be like:</p>
<pre class="lang-sql prettyprint-override">... | 16 | 2009-03-07T22:42:05Z | 622,676 | <p>The model conventions won't be atomic; write it by hand:</p>
<pre><code>BEGIN
SELECT * FROM table WHERE id=3 FOR UPDATE
UPDATE table SET hit_count=hit_count+1 WHERE id=3
COMMIT
</code></pre>
| 0 | 2009-03-07T22:59:17Z | [
"python",
"django",
"django-models"
] |
Increment Page Hit Count in Django | 622,652 | <p>I have a table with an <code>IntegerField</code> (<code>hit_count</code>), and when a page is visited (for example, <code>http://site/page/3</code>) I want record ID 3's <code>hit_count</code> column in the database to increment by 1.</p>
<p>The query should be like:</p>
<pre class="lang-sql prettyprint-override">... | 16 | 2009-03-07T22:42:05Z | 622,780 | <p>I would start by looking at the documentation for <a href="http://docs.djangoproject.com/en/dev/topics/http/middleware/#topics-http-middleware" rel="nofollow">middleware</a>. Ignacio's comment about the lack of atomic transactions is true, but this is a problem with the entire Django framework. Unless you're concern... | 0 | 2009-03-08T00:04:07Z | [
"python",
"django",
"django-models"
] |
Increment Page Hit Count in Django | 622,652 | <p>I have a table with an <code>IntegerField</code> (<code>hit_count</code>), and when a page is visited (for example, <code>http://site/page/3</code>) I want record ID 3's <code>hit_count</code> column in the database to increment by 1.</p>
<p>The query should be like:</p>
<pre class="lang-sql prettyprint-override">... | 16 | 2009-03-07T22:42:05Z | 622,859 | <p>As gerdemb says, you should write it into a middleware to make it really reusable. Or (simpler) write a function decorator. In fact, there are adaptors to use a middleware as a decorator and viceversa.</p>
<p>But if you're worried about performance and want to keep the DB queries per page hit low, you can use mem... | 4 | 2009-03-08T01:24:07Z | [
"python",
"django",
"django-models"
] |
Increment Page Hit Count in Django | 622,652 | <p>I have a table with an <code>IntegerField</code> (<code>hit_count</code>), and when a page is visited (for example, <code>http://site/page/3</code>) I want record ID 3's <code>hit_count</code> column in the database to increment by 1.</p>
<p>The query should be like:</p>
<pre class="lang-sql prettyprint-override">... | 16 | 2009-03-07T22:42:05Z | 623,860 | <p>If you use Django 1.1+, just use <a href="http://docs.djangoproject.com/en/dev/topics/db/queries/#filters-can-reference-fields-on-the-model">F expressions</a>:</p>
<pre><code>from django.db.models import F
...
MyModel.objects.filter(id=...).update(hit_count=F('hit_count')+1)
</code></pre>
<p>This will perform a si... | 31 | 2009-03-08T16:46:13Z | [
"python",
"django",
"django-models"
] |
Unable to install Python without sudo access | 622,744 | <p>I extracted, configured and used make for the installation package in my server.</p>
<p>However, I could not use <em>make install</em>. I get the error</p>
<pre><code>[~/wepapps/python/Python-2.6.1]# make install
/usr/bin/install -c python /usr/local/bin/python2.6
/usr/bin/install: cannot create regular file `/usr... | 29 | 2009-03-07T23:42:54Z | 622,753 | <p>You can't; not to <code>/usr</code>, anyway. Only superusers can write to those directories. Try installing Python to a path under your home directory instead.</p>
| 2 | 2009-03-07T23:46:53Z | [
"python",
"installation",
"sudo"
] |
Unable to install Python without sudo access | 622,744 | <p>I extracted, configured and used make for the installation package in my server.</p>
<p>However, I could not use <em>make install</em>. I get the error</p>
<pre><code>[~/wepapps/python/Python-2.6.1]# make install
/usr/bin/install -c python /usr/local/bin/python2.6
/usr/bin/install: cannot create regular file `/usr... | 29 | 2009-03-07T23:42:54Z | 622,810 | <blockquote>
<p>How can I install to a path under my home directory?</p>
</blockquote>
<pre><code>mkdir /home/masi/.local
cd Python-2.6.1
make clean
./configure --prefix=/home/masi/.local
make
make install
</code></pre>
<p>Then run using:</p>
<pre><code>/home/masi/.local/bin/python
</code></pre>
<p>Similarly if ... | 84 | 2009-03-08T00:33:25Z | [
"python",
"installation",
"sudo"
] |
Unable to install Python without sudo access | 622,744 | <p>I extracted, configured and used make for the installation package in my server.</p>
<p>However, I could not use <em>make install</em>. I get the error</p>
<pre><code>[~/wepapps/python/Python-2.6.1]# make install
/usr/bin/install -c python /usr/local/bin/python2.6
/usr/bin/install: cannot create regular file `/usr... | 29 | 2009-03-07T23:42:54Z | 16,740,313 | <p>Extending bobince answer, there is an issue if you don't have the readline development package installed in your system, and you don't have root access. </p>
<p>When Python is compiled without readline, your arrow keys won't work in the interpreter. However, you can install the readline standalone package as follow... | 1 | 2013-05-24T17:13:04Z | [
"python",
"installation",
"sudo"
] |
Python, PIL, crop problem | 622,783 | <p>Can't seem to get crop working correctly, problem is, it crops a region of correct dimensions, but always from top left corner (0, 0), instead of from my passed coordinates.</p>
<pre><code>image = Image.open(input)
region = image.crop((1000,400,2000,600)
region.save(output)
</code></pre>
<p>In image.py from PIL, m... | 0 | 2009-03-08T00:07:03Z | 622,821 | <p>Works For Me: Python 2.6.1, PIL 1.1.6, JPEG of size 2020x1338 pixels.</p>
<p>Are you sure you mean a JPEG of 1600x2390 and not 2390x1600? The (1000,400,2000,600) box dimensions are outside the size of a 1600-wide image; if I try this I get garbage data outside the intersecting area.</p>
| 1 | 2009-03-08T00:42:38Z | [
"python",
"python-imaging-library",
"crop"
] |
Python, PIL, crop problem | 622,783 | <p>Can't seem to get crop working correctly, problem is, it crops a region of correct dimensions, but always from top left corner (0, 0), instead of from my passed coordinates.</p>
<pre><code>image = Image.open(input)
region = image.crop((1000,400,2000,600)
region.save(output)
</code></pre>
<p>In image.py from PIL, m... | 0 | 2009-03-08T00:07:03Z | 2,674,199 | <p>I`m do next:</p>
<pre><code>cover=Image.open(path_to_cover+"/shablon1.jpg")
</code></pre>
<p>....</p>
<pre><code>def generit_nomer_proekt(self,nomer):
size_box=(160,40)
font=ImageFont.truetype('/home/vintello/workspace/mpg_to_dvd/src/cover/ttf/aricyr.ttf',int(30))
im = Image.new ( "RGB", size_box , "w... | 0 | 2010-04-20T10:15:13Z | [
"python",
"python-imaging-library",
"crop"
] |
Django Passing Custom Form Parameters to Formset | 622,982 | <blockquote>
<p>This was fixed in Django 1.9 with <a href="https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-custom-parameters-to-formset-forms">form_kwargs</a>.</p>
</blockquote>
<p>I have a Django Form that looks like this:</p>
<pre><code>class ServiceForm(forms.Form):
option = forms.ModelC... | 113 | 2009-03-08T03:36:08Z | 623,030 | <p>I would build the form class dynamically in a function, so that it has access to the affiliate via closure:</p>
<pre><code>def make_service_form(affiliate):
class ServiceForm(forms.Form):
option = forms.ModelChoiceField(
queryset=ServiceOption.objects.filter(affiliate=affiliate))
... | 43 | 2009-03-08T04:24:10Z | [
"python",
"django",
"forms",
"django-forms"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.