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 |
|---|---|---|---|---|---|---|---|---|---|
Django Timezone Confusion; Postgres and Apache | 2,518,240 | <p>Setup: Multiple sites on Django on the same set of servers, being served by the same group of Apache processes. Some sites are Eastern TZ; some are Central. Database is PSQL running on a separate server.</p>
<p>When I started, I didn't put much thought into how the various sites would handle timezones; I guess I sa... | 4 | 2010-03-25T18:15:09Z | 2,549,484 | <p>Here are some details on time zone handling in Django and Postgres, but I <strong>strongly</strong> recommend dealing exclusively with UTC on the backend and only converting to a local time zone in the frontend when presenting a UTC timestamp to a user. In Python, you can get the current time in UTC via <code>dateti... | 2 | 2010-03-30T23:51:49Z | [
"python",
"django",
"timezone"
] |
Thrift client-server multiple roles | 2,518,537 | <p>this is my first question, so sorry if the form is wrong!</p>
<p>I'm trying to make thrift server (python) and client (c++).
However I need to exchange messages in both direction. Client should register (call server's function and wait), and server should listen on same port for N (N-> 100k) incoming connections (c... | 3 | 2010-03-25T19:03:53Z | 2,521,095 | <p>Consider using <a href="http://www.boost.org/doc/libs/1_42_0/doc/html/boost_asio.html" rel="nofollow">boost::asio</a> for your client side, though depending on your level of C++, the code may seem too dense.</p>
<p>If you're looking for a simple example, take a look at:
<a href="http://www.linuxhowtos.org/C_C++/soc... | 1 | 2010-03-26T04:08:13Z | [
"c++",
"python",
"client-server",
"thrift"
] |
Thrift client-server multiple roles | 2,518,537 | <p>this is my first question, so sorry if the form is wrong!</p>
<p>I'm trying to make thrift server (python) and client (c++).
However I need to exchange messages in both direction. Client should register (call server's function and wait), and server should listen on same port for N (N-> 100k) incoming connections (c... | 3 | 2010-03-25T19:03:53Z | 3,130,486 | <p>Since, you say you are having problem with calling functions from client side, here is a sample Thrift code with Java server and C++ client, where the client calls a function in server. <a href="http://fundoonick.blogspot.com/2010/06/sample-thrift-program-for-server-in.html" rel="nofollow">http://fundoonick.blogspot... | 1 | 2010-06-28T06:36:13Z | [
"c++",
"python",
"client-server",
"thrift"
] |
Python | mktime overflow error | 2,518,706 | <p>I have been search all over the net and couldn't find an appropriate solution for this issue </p>
<pre><code>OverflowError: mktime argument out of range
</code></pre>
<p>The code that causes this exception </p>
<pre><code> t = (1956, 3, 2, 0, 0, 0, 0, 0, 0)
ser = time.mktime(t)
</code></pre>
<p>I would like to ... | 14 | 2010-03-25T19:26:53Z | 2,518,828 | <p><code>time.mktime</code> calls the underlying <code>mktime</code> function from the platform's C library. For instance, the above code that you posted works perfectly well for me on Mac OS X, although it returns a negative number as the date is before the Unix epoch. So the reason is that your platform's <code>mktim... | 17 | 2010-03-25T19:42:56Z | [
"python"
] |
Counting removed items in a Set in Python | 2,518,711 | <p>Given two sets</p>
<pre><code>a = [5,3,4,1,2,6,7]
b = [1,2,4,9]
c = set(a) - set(b)
# c -> [5,3,6,7]
</code></pre>
<p>is it possible to count how many items were removed from set 'a' ?</p>
| 1 | 2010-03-25T19:27:14Z | 2,518,735 | <p>How about <code>len(set(a)) - len(c)</code>?</p>
<p>Edit: <code>len(a)</code> could be incorrect if <code>a</code> contains duplicates.</p>
| 6 | 2010-03-25T19:30:11Z | [
"python"
] |
Counting removed items in a Set in Python | 2,518,711 | <p>Given two sets</p>
<pre><code>a = [5,3,4,1,2,6,7]
b = [1,2,4,9]
c = set(a) - set(b)
# c -> [5,3,6,7]
</code></pre>
<p>is it possible to count how many items were removed from set 'a' ?</p>
| 1 | 2010-03-25T19:27:14Z | 2,518,737 | <p>Assuming lack of duplicates:
<code>len(a)-len(c)</code>
otherwise try:
<code>len(set(a)) - len(c)</code></p>
| 3 | 2010-03-25T19:30:23Z | [
"python"
] |
Counting removed items in a Set in Python | 2,518,711 | <p>Given two sets</p>
<pre><code>a = [5,3,4,1,2,6,7]
b = [1,2,4,9]
c = set(a) - set(b)
# c -> [5,3,6,7]
</code></pre>
<p>is it possible to count how many items were removed from set 'a' ?</p>
| 1 | 2010-03-25T19:27:14Z | 2,518,746 | <pre><code>a = [5,3,4,1,2,6,7]
b = [1,2,4,9]
c = set(a) - set(b)
print len(c)
</code></pre>
| -1 | 2010-03-25T19:31:42Z | [
"python"
] |
Counting removed items in a Set in Python | 2,518,711 | <p>Given two sets</p>
<pre><code>a = [5,3,4,1,2,6,7]
b = [1,2,4,9]
c = set(a) - set(b)
# c -> [5,3,6,7]
</code></pre>
<p>is it possible to count how many items were removed from set 'a' ?</p>
| 1 | 2010-03-25T19:27:14Z | 2,518,751 | <p>there might be a more efficient way, but</p>
<pre><code> len(set(a)-set(c))
</code></pre>
<p>will work</p>
| 3 | 2010-03-25T19:32:42Z | [
"python"
] |
A 3-D grid of regularly spaced points | 2,518,730 | <p>I want to create a list containing the 3-D coords of a grid of regularly spaced points, each as a 3-element tuple. I'm looking for advice on the most efficient way to do this. </p>
<p>In C++ for instance, I simply loop over three nested loops, one for each coordinate. In Matlab, I would probably use the meshgrid fu... | 3 | 2010-03-25T19:29:19Z | 2,520,077 | <p>Use <code>ndindex</code>:</p>
<pre><code>import numpy as np
ind=np.ndindex(3,3,2)
for i in ind:
print(i)
# (0, 0, 0)
# (0, 0, 1)
# (0, 1, 0)
# (0, 1, 1)
# (0, 2, 0)
# (0, 2, 1)
# (1, 0, 0)
# (1, 0, 1)
# (1, 1, 0)
# (1, 1, 1)
# (1, 2, 0)
# (1, 2, 1)
# (2, 0, 0)
# (2, 0, 1)
# (2, 1, 0)
# (2, 1, 1)
# (2, 2, 0)
# ... | 3 | 2010-03-25T23:09:24Z | [
"python",
"numpy"
] |
A 3-D grid of regularly spaced points | 2,518,730 | <p>I want to create a list containing the 3-D coords of a grid of regularly spaced points, each as a 3-element tuple. I'm looking for advice on the most efficient way to do this. </p>
<p>In C++ for instance, I simply loop over three nested loops, one for each coordinate. In Matlab, I would probably use the meshgrid fu... | 3 | 2010-03-25T19:29:19Z | 2,520,243 | <p>for multi-d (greater than 2) meshgrids, use <code>numpy.lib.index_tricks.nd_grid</code> like so:</p>
<pre><code>import numpy
grid = numpy.lib.index_tricks.nd_grid()
g1 = grid[:3,:3,:3]
g2 = grid[0:1:0.5, 0:1, 0:2]
g3 = grid[0:1:3j, 0:1:2j, 0:2:2j]
</code></pre>
<p>where g1 has x values of [0,1,2]
and g2 has x valu... | 1 | 2010-03-25T23:41:42Z | [
"python",
"numpy"
] |
A 3-D grid of regularly spaced points | 2,518,730 | <p>I want to create a list containing the 3-D coords of a grid of regularly spaced points, each as a 3-element tuple. I'm looking for advice on the most efficient way to do this. </p>
<p>In C++ for instance, I simply loop over three nested loops, one for each coordinate. In Matlab, I would probably use the meshgrid fu... | 3 | 2010-03-25T19:29:19Z | 2,521,903 | <p>I would say go with <code>meshgrid</code> or <code>mgrid</code>, in particular if you need non-integer coordinates. I'm surprised that Numpy's broadcasting rules would be more efficient, as <code>meshgrid</code> was designed especially for the problem that you want to solve.</p>
| 1 | 2010-03-26T08:31:33Z | [
"python",
"numpy"
] |
A 3-D grid of regularly spaced points | 2,518,730 | <p>I want to create a list containing the 3-D coords of a grid of regularly spaced points, each as a 3-element tuple. I'm looking for advice on the most efficient way to do this. </p>
<p>In C++ for instance, I simply loop over three nested loops, one for each coordinate. In Matlab, I would probably use the meshgrid fu... | 3 | 2010-03-25T19:29:19Z | 11,106,445 | <p>Instead of meshgrid and mgrid, you can use ogrid, which is a "sparse" version of mgrid. That is, only the dimension along which the values change are filled in. The others are simply broadcast. This uses much less memory for large grids than the non-sparse alternatives.</p>
<p>For example:</p>
<pre><code>>>... | 3 | 2012-06-19T17:45:20Z | [
"python",
"numpy"
] |
A 3-D grid of regularly spaced points | 2,518,730 | <p>I want to create a list containing the 3-D coords of a grid of regularly spaced points, each as a 3-element tuple. I'm looking for advice on the most efficient way to do this. </p>
<p>In C++ for instance, I simply loop over three nested loops, one for each coordinate. In Matlab, I would probably use the meshgrid fu... | 3 | 2010-03-25T19:29:19Z | 18,284,230 | <p>Here's an efficient option similar to your C++ solution, which I've used for exactly the same purpose:</p>
<pre><code>import numpy, itertools, collections
def grid(xmin, xmax, xstep, ymin, ymax, ystep, zmin, zmax, zstep):
"return nested tuples of grid-sampled coordinates that include maxima"
return collecti... | 0 | 2013-08-17T01:28:17Z | [
"python",
"numpy"
] |
best way to implement a deck for a card game in python | 2,518,753 | <p>What is the best way to store the cards and suits in python so that I can hold a reference to these values in another variable?</p>
<p>For example, if I have a list called hand (cards in players hand), how could I hold values that could refer to the names of suits and values of specific cards, and how would these n... | 14 | 2010-03-25T19:32:55Z | 2,518,788 | <p>The simplest thing would be to use a list of tuples, where the cards are ints and the suits are strings:</p>
<pre><code>hand = [(1, 'spade'), (10, 'club'), ...]
</code></pre>
<p>But simplest may not be what you want. Maybe you want a class to represent a card:</p>
<pre><code>class Card:
def __init__(self, ra... | 3 | 2010-03-25T19:38:45Z | [
"python"
] |
best way to implement a deck for a card game in python | 2,518,753 | <p>What is the best way to store the cards and suits in python so that I can hold a reference to these values in another variable?</p>
<p>For example, if I have a list called hand (cards in players hand), how could I hold values that could refer to the names of suits and values of specific cards, and how would these n... | 14 | 2010-03-25T19:32:55Z | 2,518,794 | <p>You could simply use a number, and decide on a mapping between number and "card". For example:</p>
<p>number MOD 13 = face value (after a +1)</p>
<p>number DIV 13 = suit</p>
| 1 | 2010-03-25T19:39:25Z | [
"python"
] |
best way to implement a deck for a card game in python | 2,518,753 | <p>What is the best way to store the cards and suits in python so that I can hold a reference to these values in another variable?</p>
<p>For example, if I have a list called hand (cards in players hand), how could I hold values that could refer to the names of suits and values of specific cards, and how would these n... | 14 | 2010-03-25T19:32:55Z | 2,518,806 | <p>Poker servers tend to use a 2-character string to identify each card, which is nice because it's easy to deal with programmatically and just as easy to read for a human.</p>
<pre><code>>>> import random
>>> import itertools
>>> SUITS = 'cdhs'
>>> RANKS = '23456789TJQKA'
>>&... | 19 | 2010-03-25T19:40:49Z | [
"python"
] |
best way to implement a deck for a card game in python | 2,518,753 | <p>What is the best way to store the cards and suits in python so that I can hold a reference to these values in another variable?</p>
<p>For example, if I have a list called hand (cards in players hand), how could I hold values that could refer to the names of suits and values of specific cards, and how would these n... | 14 | 2010-03-25T19:32:55Z | 2,518,898 | <pre><code>import collections
C, H, D, S = "CLUBS", "HEARTS", "DICE", "SPADE"
Card = collections.namedtuple("Card", "suit value")
hand = []
hand.append(Card(C, 3))
hand.append(Card(H, "A"))
hand.append(Card(D, 10))
hand.append(Card(S, "Q"))
for card in hand:
print(card.value, card.suit)
</code></pre>
| 1 | 2010-03-25T19:53:26Z | [
"python"
] |
best way to implement a deck for a card game in python | 2,518,753 | <p>What is the best way to store the cards and suits in python so that I can hold a reference to these values in another variable?</p>
<p>For example, if I have a list called hand (cards in players hand), how could I hold values that could refer to the names of suits and values of specific cards, and how would these n... | 14 | 2010-03-25T19:32:55Z | 2,519,072 | <p>a deck of cards is comprised of the same range of values (1 - 14) in each of <em>four</em> suits, which suggests a <em>Cartesian product</em>. List comprehension is an elegant, dense syntax for doing this:</p>
<pre><code>values = range(1, 11) + "Jack Queen King".split()
suits = "Diamonds Clubs Hearts Spades".split(... | 1 | 2010-03-25T20:23:09Z | [
"python"
] |
Map only certain parts of the class to a database using SQLAlchemy? | 2,519,287 | <p>When mapping an object using SQLAlchemy, is there a way to only map certain elements of a class to a database, or does it have to be a 1:1 mapping?</p>
<p>Example: </p>
<pre><code>class User(object):
def __init__(self, name, username, password, year_of_birth):
self.name = name
self.username = ... | 1 | 2010-03-25T20:54:22Z | 2,525,251 | <p>Your mapper can specify what columns to map, you could even map a single table to multiple objects and multiple tables to a single object.</p>
<p>Here's the documentation for mapping a single object multiple times: <a href="http://www.sqlalchemy.org/docs/05/mappers.html#multiple-mappers-for-one-class" rel="nofollow... | 2 | 2010-03-26T17:18:36Z | [
"python",
"database",
"sqlalchemy"
] |
Importing a function/class from a Python module of the same name | 2,519,511 | <p>I have a Python package <code>mymodule</code> with a sub-package <code>utils</code> (i.e. a subdirectory which contains modules each with a function). The functions have the same name as the file/module in which they live.</p>
<p>I would like to be able to access the functions as follows,</p>
<p><code>from mymodul... | 6 | 2010-03-25T21:24:26Z | 2,520,564 | <p>Do your utils functions need to import other utils functions? (or import other modules that import other utils functions). Suppose for example that a_function.py contains contains "from mymodule.utils import b_function". Here's your utils.py with a bunch of extra comments:</p>
<pre><code># interpreter is executin... | 7 | 2010-03-26T01:12:46Z | [
"python",
"import",
"module"
] |
Example for accessing Tcl functions from Python | 2,519,532 | <p><a href="http://wiki.python.org/moin/elmer">elmer</a> enables one to run Python code from Tcl. What about the other way around? Could anyone give an example in Python?</p>
<p><strong>Update</strong>: by this I mean, being able to access Tcl objects, invoke Tcl functions, etc. instead of <em>simply running</em> some... | 6 | 2010-03-25T21:27:34Z | 2,519,550 | <p>You can take a look at python tkinter standard module, it is a binding to Tk UI, but that executes Tcl code in the background.</p>
| 4 | 2010-03-25T21:29:40Z | [
"python",
"tcl"
] |
Example for accessing Tcl functions from Python | 2,519,532 | <p><a href="http://wiki.python.org/moin/elmer">elmer</a> enables one to run Python code from Tcl. What about the other way around? Could anyone give an example in Python?</p>
<p><strong>Update</strong>: by this I mean, being able to access Tcl objects, invoke Tcl functions, etc. instead of <em>simply running</em> some... | 6 | 2010-03-25T21:27:34Z | 2,519,638 | <p>This?</p>
<pre><code>import subprocess
subprocess.Popen( "tclsh someFile.tcl", shell=True )
</code></pre>
| -3 | 2010-03-25T21:46:15Z | [
"python",
"tcl"
] |
Example for accessing Tcl functions from Python | 2,519,532 | <p><a href="http://wiki.python.org/moin/elmer">elmer</a> enables one to run Python code from Tcl. What about the other way around? Could anyone give an example in Python?</p>
<p><strong>Update</strong>: by this I mean, being able to access Tcl objects, invoke Tcl functions, etc. instead of <em>simply running</em> some... | 6 | 2010-03-25T21:27:34Z | 2,548,927 | <p>While it might be possible in theory by providing some custom Python object types that wrap C level Tcl_Obj*'s transparently, the general case is just exchanging strings back and forth as Tcl's EIAS semantics can shimmer away every other internal representation freely.</p>
<p>So yes, you could do better than string... | 1 | 2010-03-30T21:34:14Z | [
"python",
"tcl"
] |
Example for accessing Tcl functions from Python | 2,519,532 | <p><a href="http://wiki.python.org/moin/elmer">elmer</a> enables one to run Python code from Tcl. What about the other way around? Could anyone give an example in Python?</p>
<p><strong>Update</strong>: by this I mean, being able to access Tcl objects, invoke Tcl functions, etc. instead of <em>simply running</em> some... | 6 | 2010-03-25T21:27:34Z | 6,036,341 | <p>This is a rather old thread, but I recently stumbled on <code>Tkinter.Tcl()</code> which gives you direct access to a Tcl interpreter in python <em>without having to spawn a Tk GUI as <code>Tkinter.Tk()</code> requires</em>.</p>
<p>An example... suppose you have a Tcl file (<code>foo.tcl</code>) with a proc called ... | 5 | 2011-05-17T19:55:19Z | [
"python",
"tcl"
] |
Are strings pooled in Python | 2,519,580 | <p>Does Python have a pool of all strings and are they (strings) singletons there?</p>
<p>More precise, in the following code one or two strings were created in memory:</p>
<pre><code>a = str(num)
b = str(num)
</code></pre>
<p>?</p>
| 7 | 2010-03-25T21:33:32Z | 2,519,585 | <p>Strings are not interned in general. In your example two strings will be created (with the exception of values between 0 and 9). To test this we can use the <code>is</code> operator to see if the two strings are the same object:</p>
<pre><code>>>> str(1056) is str(1056)
False
</code></pre>
| 1 | 2010-03-25T21:35:09Z | [
"python",
"string",
"memory",
"singleton",
"string-interning"
] |
Are strings pooled in Python | 2,519,580 | <p>Does Python have a pool of all strings and are they (strings) singletons there?</p>
<p>More precise, in the following code one or two strings were created in memory:</p>
<pre><code>a = str(num)
b = str(num)
</code></pre>
<p>?</p>
| 7 | 2010-03-25T21:33:32Z | 2,519,616 | <p>Strings are immutable in Python, so the implementation can decide whether to intern (that's a term often associated with C#, meaning that some strings are stored in a pool) strings or not.</p>
<p>In your example, you're dynamically creating strings. CPython does <em>not always</em> look into the pool to detect whet... | 15 | 2010-03-25T21:40:19Z | [
"python",
"string",
"memory",
"singleton",
"string-interning"
] |
Are strings pooled in Python | 2,519,580 | <p>Does Python have a pool of all strings and are they (strings) singletons there?</p>
<p>More precise, in the following code one or two strings were created in memory:</p>
<pre><code>a = str(num)
b = str(num)
</code></pre>
<p>?</p>
| 7 | 2010-03-25T21:33:32Z | 2,519,622 | <p>In general, strings are not interned in Python, but they do sometimes seem to be:</p>
<pre><code>>>> str(5) is str(5)
True
>>> str(50) is str(50)
False
</code></pre>
<p>This isn't uncommon in Python, where common objects might be optimized in ways that unusual ones are not:</p>
<pre><code>>&g... | 5 | 2010-03-25T21:40:55Z | [
"python",
"string",
"memory",
"singleton",
"string-interning"
] |
Writing a telnet client | 2,519,598 | <p>HI,</p>
<p>I have a device that exposes a telnet interface which you can log into using a username and password and then manipulate the working of the device.</p>
<p>I have to write a C program that hides the telnet aspect from the client and instead provides an interface for the user to control the device.</p>
<... | 9 | 2010-03-25T21:37:48Z | 2,519,610 | <p>If Python is an option you could use <a href="http://docs.python.org/library/telnetlib.html">telnetlib</a>.</p>
<p><a href="http://docs.python.org/library/telnetlib.html#telnet-example">Code example</a>:</p>
<pre><code>#!/usr/bin/env python
import getpass
import sys
import telnetlib
HOST = "localhost"
user = raw_... | 10 | 2010-03-25T21:39:31Z | [
"python",
"c",
"telnet"
] |
Writing a telnet client | 2,519,598 | <p>HI,</p>
<p>I have a device that exposes a telnet interface which you can log into using a username and password and then manipulate the working of the device.</p>
<p>I have to write a C program that hides the telnet aspect from the client and instead provides an interface for the user to control the device.</p>
<... | 9 | 2010-03-25T21:37:48Z | 2,519,621 | <p><a href="http://stackoverflow.com/questions/202859/using-telnet-in-a-c-program">This</a> might be useful reading for you.</p>
| 2 | 2010-03-25T21:40:55Z | [
"python",
"c",
"telnet"
] |
Writing a telnet client | 2,519,598 | <p>HI,</p>
<p>I have a device that exposes a telnet interface which you can log into using a username and password and then manipulate the working of the device.</p>
<p>I have to write a C program that hides the telnet aspect from the client and instead provides an interface for the user to control the device.</p>
<... | 9 | 2010-03-25T21:37:48Z | 2,519,626 | <p>Download <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html" rel="nofollow">Putty</a> source code. Examine TELNET.C, the Telnet backend for Putty.</p>
| 2 | 2010-03-25T21:42:34Z | [
"python",
"c",
"telnet"
] |
Writing a telnet client | 2,519,598 | <p>HI,</p>
<p>I have a device that exposes a telnet interface which you can log into using a username and password and then manipulate the working of the device.</p>
<p>I have to write a C program that hides the telnet aspect from the client and instead provides an interface for the user to control the device.</p>
<... | 9 | 2010-03-25T21:37:48Z | 2,519,786 | <p>telnet's protocol is pretty straightforward... you just create a TCP connection, and send and receive ASCII data. That's pretty much it.</p>
<p>So all you really need to do is create a program that connects via TCP, then reads characters from the TCP socket and parses it to update the GUI, and/or writes characters... | 4 | 2010-03-25T22:09:16Z | [
"python",
"c",
"telnet"
] |
Writing a telnet client | 2,519,598 | <p>HI,</p>
<p>I have a device that exposes a telnet interface which you can log into using a username and password and then manipulate the working of the device.</p>
<p>I have to write a C program that hides the telnet aspect from the client and instead provides an interface for the user to control the device.</p>
<... | 9 | 2010-03-25T21:37:48Z | 2,521,795 | <p>I really find <a href="http://beej.us/guide/bgnet/" rel="nofollow">Beej's Guide to Network Programming</a> a good introduction to network programming in C.</p>
| 2 | 2010-03-26T08:03:02Z | [
"python",
"c",
"telnet"
] |
Writing a telnet client | 2,519,598 | <p>HI,</p>
<p>I have a device that exposes a telnet interface which you can log into using a username and password and then manipulate the working of the device.</p>
<p>I have to write a C program that hides the telnet aspect from the client and instead provides an interface for the user to control the device.</p>
<... | 9 | 2010-03-25T21:37:48Z | 2,521,858 | <p>Unless the application is trivial, a better starting point would be to figure out how you're going to create the GUI. This is a bigger question and will have more impact on your project than how exactly you telnet into the device. You mention C at first, but then start talking about Python, which makes me believe yo... | 1 | 2010-03-26T08:18:02Z | [
"python",
"c",
"telnet"
] |
Writing a telnet client | 2,519,598 | <p>HI,</p>
<p>I have a device that exposes a telnet interface which you can log into using a username and password and then manipulate the working of the device.</p>
<p>I have to write a C program that hides the telnet aspect from the client and instead provides an interface for the user to control the device.</p>
<... | 9 | 2010-03-25T21:37:48Z | 2,525,924 | <p>While telnet is almost just a socket tied to a terminal it's not quite. I believe that there can be some control characters that get passed shortly after the connection is made. If your device is sending some unexpected control data then it may be confusing your program.
If you haven't already, go download wiresha... | 3 | 2010-03-26T18:54:35Z | [
"python",
"c",
"telnet"
] |
Writing a telnet client | 2,519,598 | <p>HI,</p>
<p>I have a device that exposes a telnet interface which you can log into using a username and password and then manipulate the working of the device.</p>
<p>I have to write a C program that hides the telnet aspect from the client and instead provides an interface for the user to control the device.</p>
<... | 9 | 2010-03-25T21:37:48Z | 3,066,372 | <p>Check out the source code <a href="http://www.1cplusplusstreet.com/vb/scripts/ShowCode.asp?txtCodeId=1002&lngWId=3" rel="nofollow">here</a>. It has helped me out <em>a lot</em> in understanding Telnet protocol.</p>
| 0 | 2010-06-17T23:41:27Z | [
"python",
"c",
"telnet"
] |
Writing a telnet client | 2,519,598 | <p>HI,</p>
<p>I have a device that exposes a telnet interface which you can log into using a username and password and then manipulate the working of the device.</p>
<p>I have to write a C program that hides the telnet aspect from the client and instead provides an interface for the user to control the device.</p>
<... | 9 | 2010-03-25T21:37:48Z | 22,346,923 | <p>See options in your telnet client: on an arbitrary listening socket and hit <code>escape]</code> to enter the client.</p>
<pre><code>telnet> help
telnet> set ?
</code></pre>
| 0 | 2014-03-12T09:22:05Z | [
"python",
"c",
"telnet"
] |
Python code to use a regular expression to make sure a string is alphanumeric plus . - _ | 2,519,670 | <p>I looked and searched and couldn't find what I needed although I think it should be simple (if you have any Python experience, which I don't).</p>
<p>Given a string, I want to verify, in Python, that it contains ONLY alphanumeric characters: <code>a-zA-Z0-9</code> and <code>.</code> <code>_</code> <code>-</code> </... | 4 | 2010-03-25T21:51:53Z | 2,519,701 | <p><a href="http://docs.python.org/library/re.html#re.match"><code>re.match</code></a> does not return a boolean; it returns a <code>MatchObject</code> on a match, or <code>None</code> on a non-match.</p>
<pre><code>>>> re.match("^[a-zA-Z0-9_.-]+$", "hello")
<_sre.SRE_Match object at 0xb7600250>
>&g... | 13 | 2010-03-25T21:56:22Z | [
"python",
"regex",
"alphanumeric"
] |
Python code to use a regular expression to make sure a string is alphanumeric plus . - _ | 2,519,670 | <p>I looked and searched and couldn't find what I needed although I think it should be simple (if you have any Python experience, which I don't).</p>
<p>Given a string, I want to verify, in Python, that it contains ONLY alphanumeric characters: <code>a-zA-Z0-9</code> and <code>.</code> <code>_</code> <code>-</code> </... | 4 | 2010-03-25T21:51:53Z | 2,519,707 | <p>Never use <code>== True</code> or <code>== False</code> in a comparison. Many types already have a bool equivalent which you should use instead:</p>
<pre><code>if re.match("^[a-zA-Z0-9_.-]+$", username):
</code></pre>
| 3 | 2010-03-25T21:57:13Z | [
"python",
"regex",
"alphanumeric"
] |
Python code to use a regular expression to make sure a string is alphanumeric plus . - _ | 2,519,670 | <p>I looked and searched and couldn't find what I needed although I think it should be simple (if you have any Python experience, which I don't).</p>
<p>Given a string, I want to verify, in Python, that it contains ONLY alphanumeric characters: <code>a-zA-Z0-9</code> and <code>.</code> <code>_</code> <code>-</code> </... | 4 | 2010-03-25T21:51:53Z | 2,521,670 | <p>Could also shorten it slightly to :</p>
<pre><code>if re.match(r'^[\w.-]+$', username):
</code></pre>
| 1 | 2010-03-26T07:28:03Z | [
"python",
"regex",
"alphanumeric"
] |
Python code to use a regular expression to make sure a string is alphanumeric plus . - _ | 2,519,670 | <p>I looked and searched and couldn't find what I needed although I think it should be simple (if you have any Python experience, which I don't).</p>
<p>Given a string, I want to verify, in Python, that it contains ONLY alphanumeric characters: <code>a-zA-Z0-9</code> and <code>.</code> <code>_</code> <code>-</code> </... | 4 | 2010-03-25T21:51:53Z | 2,521,692 | <p>If you are going to use many regular expressions you can compile it for speed (or readability)</p>
<pre><code>import re
ALPHANUM=re.compile('^[a-zA-Z0-9_.-]+$')
for u in users:
if ALPHANUM.match(u) is None:
print "invalid"
</code></pre>
<p>From <a href="http://docs.python.org/2/library/re.html" rel="... | 0 | 2010-03-26T07:37:45Z | [
"python",
"regex",
"alphanumeric"
] |
Python code to use a regular expression to make sure a string is alphanumeric plus . - _ | 2,519,670 | <p>I looked and searched and couldn't find what I needed although I think it should be simple (if you have any Python experience, which I don't).</p>
<p>Given a string, I want to verify, in Python, that it contains ONLY alphanumeric characters: <code>a-zA-Z0-9</code> and <code>.</code> <code>_</code> <code>-</code> </... | 4 | 2010-03-25T21:51:53Z | 15,125,972 | <p>I do my validation this way in my utils class:</p>
<pre><code>def valid_re(self, s, r):
reg = re.compile(r)
return reg.match(s)
</code></pre>
<p>Then I call the utils instance, and check this way:</p>
<pre><code>if not utils.valid_re(username, r'^[a-zA-Z0-9_.-]+$'):
error = "Invalid username!"
</code></... | 0 | 2013-02-28T01:39:12Z | [
"python",
"regex",
"alphanumeric"
] |
Using set in Python inside a loop | 2,519,682 | <p>I have the following list in Python:</p>
<pre><code>[[1, 2], [3, 4], [4, 6], [2, 7], [3, 9]]
</code></pre>
<p>I want to group them into <code>[[1,2,7],[3,4,6,9]]</code></p>
<p>My code to do this looks like this:</p>
<pre><code>l=[[1, 2], [3, 4], [4, 6], [2, 7], [3, 9]]
lf=[]
for li in l:
for lfi in lf:
... | 2 | 2010-03-25T21:53:26Z | 2,519,721 | <p>The problem is here:</p>
<pre><code>lfi=lfi.union(set(li))
</code></pre>
<p>You are not modifying the set. You are creating a new set which is then discarded. The original set is still in the <code>lf</code> array. Use update instead:</p>
<pre><code>lfi.update(li)
</code></pre>
<p>This modifies the original set ... | 5 | 2010-03-25T21:59:05Z | [
"python",
"dataset"
] |
Using set in Python inside a loop | 2,519,682 | <p>I have the following list in Python:</p>
<pre><code>[[1, 2], [3, 4], [4, 6], [2, 7], [3, 9]]
</code></pre>
<p>I want to group them into <code>[[1,2,7],[3,4,6,9]]</code></p>
<p>My code to do this looks like this:</p>
<pre><code>l=[[1, 2], [3, 4], [4, 6], [2, 7], [3, 9]]
lf=[]
for li in l:
for lfi in lf:
... | 2 | 2010-03-25T21:53:26Z | 2,519,789 | <p>Here is another way to write the same thing<br>
For sets,<br>
<code>&</code> means <code>intersection</code><br>
<code>|=</code> means <code>update</code> </p>
<p>I have also used <code>map(set,l)</code> so that you are not recreating the same set over and over</p>
<pre><code>l=[[1, 2], [3, 4], [4, 6], [2,... | 2 | 2010-03-25T22:09:51Z | [
"python",
"dataset"
] |
Using set in Python inside a loop | 2,519,682 | <p>I have the following list in Python:</p>
<pre><code>[[1, 2], [3, 4], [4, 6], [2, 7], [3, 9]]
</code></pre>
<p>I want to group them into <code>[[1,2,7],[3,4,6,9]]</code></p>
<p>My code to do this looks like this:</p>
<pre><code>l=[[1, 2], [3, 4], [4, 6], [2, 7], [3, 9]]
lf=[]
for li in l:
for lfi in lf:
... | 2 | 2010-03-25T21:53:26Z | 2,519,846 | <pre><code>l=[[1, 2], [3, 4], [4, 6], [2, 7], [3, 9]]
lf=[]
for li in l:
for i, lfi in enumerate(lf):
if lfi.intersection(set(li)):
lfi=lfi.union(set(li))
lf[i] = lfi #You forgot to update the list
break
else:
lf.append(set(li))
</code></pre>
| 0 | 2010-03-25T22:21:28Z | [
"python",
"dataset"
] |
Python PyQt Timer Firmata | 2,519,699 | <p>I am pretty new to python and working with firmata I am trying to play around with an arduino . </p>
<p>Here is what I want to happen:</p>
<ul>
<li>Set arduino up with an LED as a
digital out </li>
<li><p>Set potentiometer to analog 0</p></li>
<li><p>Set PyQt timer up to update
potentiometer position in<... | 3 | 2010-03-25T21:55:59Z | 2,519,959 | <p>Self didn't need to be passed. I have no clue why it failed the first time, or why self is included already.</p>
| 0 | 2010-03-25T22:40:12Z | [
"python",
"pyqt",
"arduino",
"firmata"
] |
Python PyQt Timer Firmata | 2,519,699 | <p>I am pretty new to python and working with firmata I am trying to play around with an arduino . </p>
<p>Here is what I want to happen:</p>
<ul>
<li>Set arduino up with an LED as a
digital out </li>
<li><p>Set potentiometer to analog 0</p></li>
<li><p>Set PyQt timer up to update
potentiometer position in<... | 3 | 2010-03-25T21:55:59Z | 2,524,000 | <p>In your code 'a' is the class instance, so all methods, bound to it, already have self pointers passed as first params.
Welcome to python, someday you'd like it :)</p>
<p>In contra, you can call any method as unbound (and I'm sure you do it in every constructor of any derived class). Syntax is:</p>
<pre><code>ins... | 0 | 2010-03-26T14:34:05Z | [
"python",
"pyqt",
"arduino",
"firmata"
] |
Creating Read-only logs with python | 2,519,706 | <p>I am writing a python script that needs to make a log entry whenever it's invoked. The log created by the script must not be changeable by the user (except root) who invoked the script. I tried the syslog module and while this does exactly what I want in terms of file permissions, I need to be able to put the result... | 0 | 2010-03-25T21:56:59Z | 2,519,749 | <p>Run your script with <a href="http://en.wikipedia.org/wiki/Setuid" rel="nofollow">setuid</a> root.</p>
| 0 | 2010-03-25T22:03:01Z | [
"python",
"linux",
"logging",
"permissions"
] |
Creating Read-only logs with python | 2,519,706 | <p>I am writing a python script that needs to make a log entry whenever it's invoked. The log created by the script must not be changeable by the user (except root) who invoked the script. I tried the syslog module and while this does exactly what I want in terms of file permissions, I need to be able to put the result... | 0 | 2010-03-25T21:56:59Z | 2,519,800 | <p>I see you are on linux,</p>
<p>Depending on which filesystem you are using, you may be able to use the <code>chattr</code> command. You can make files that are append only by setting the <code>a</code> attribute</p>
| 1 | 2010-03-25T22:12:54Z | [
"python",
"linux",
"logging",
"permissions"
] |
Setting an class attribute with a given name in python while defining the class | 2,519,807 | <p>I am trying to do something like this:</p>
<pre><code>property = 'name'
value = Thing()
class A:
setattr(A, property, value)
other_thing = 'normal attribute'
def __init__(self, etc)
#etc..........
</code></pre>
<p>But I can't seem to find the reference to the class to get the setattr to work the same as... | 7 | 2010-03-25T22:14:33Z | 2,519,853 | <p>This may be because the class <code>A</code> is not fully initialized when you do your <code>setattr(A, p, v)</code> there. </p>
<p>The first thing to try would be to just move the settattr down to after you close the <code>class</code> block and see if that works, e.g.</p>
<p><pre><code>class A(object):
pass... | 6 | 2010-03-25T22:22:10Z | [
"python",
"class",
"setattr"
] |
Setting an class attribute with a given name in python while defining the class | 2,519,807 | <p>I am trying to do something like this:</p>
<pre><code>property = 'name'
value = Thing()
class A:
setattr(A, property, value)
other_thing = 'normal attribute'
def __init__(self, etc)
#etc..........
</code></pre>
<p>But I can't seem to find the reference to the class to get the setattr to work the same as... | 7 | 2010-03-25T22:14:33Z | 2,519,895 | <p>You'll need to use a metaclass for this:</p>
<pre><code>property = 'foo'
value = 'bar'
class MC(type):
def __init__(cls, name, bases, dict):
setattr(cls, property, value)
super(MC, cls).__init__(name, bases, dict)
class C(object):
__metaclass__ = MC
print C.foo
</code></pre>
| 6 | 2010-03-25T22:28:47Z | [
"python",
"class",
"setattr"
] |
Setting an class attribute with a given name in python while defining the class | 2,519,807 | <p>I am trying to do something like this:</p>
<pre><code>property = 'name'
value = Thing()
class A:
setattr(A, property, value)
other_thing = 'normal attribute'
def __init__(self, etc)
#etc..........
</code></pre>
<p>But I can't seem to find the reference to the class to get the setattr to work the same as... | 7 | 2010-03-25T22:14:33Z | 20,608,050 | <p>You can do it even simpler:</p>
<pre><code>class A():
vars()['key'] = 'value'
</code></pre>
<p>In contrast to the previous answer, this solution plays well with external metaclasses (for ex., Django models).</p>
| 4 | 2013-12-16T10:04:12Z | [
"python",
"class",
"setattr"
] |
Setting an class attribute with a given name in python while defining the class | 2,519,807 | <p>I am trying to do something like this:</p>
<pre><code>property = 'name'
value = Thing()
class A:
setattr(A, property, value)
other_thing = 'normal attribute'
def __init__(self, etc)
#etc..........
</code></pre>
<p>But I can't seem to find the reference to the class to get the setattr to work the same as... | 7 | 2010-03-25T22:14:33Z | 36,721,178 | <p>So I know this is really old and probably beating a dead horse and this may not have been possible at the time but I cam across this trying to solve my own problem.</p>
<p>I realized this can be accomplished without metaclassing.</p>
<p>The setattr takes and object, accessor name, and value. Well the object is not... | 0 | 2016-04-19T14:21:17Z | [
"python",
"class",
"setattr"
] |
Generate unique hashes for django models | 2,519,896 | <p>I want to use unique hashes for each model rather than ids.</p>
<p>I implemented the following function to use it across the board easily.</p>
<pre><code>import random,hashlib
from base64 import urlsafe_b64encode
def set_unique_random_value(model_object,field_name='hash_uuid',length=5,use_sha=True,urlencode=False... | 17 | 2010-03-25T22:29:11Z | 2,519,931 | <p>Use your database engine's UUID support instead of making up your own hash. Almost everything beyond SQLite supports them, so there's little reason to not use them.</p>
| 7 | 2010-03-25T22:34:34Z | [
"python",
"django",
"hash",
"random"
] |
Generate unique hashes for django models | 2,519,896 | <p>I want to use unique hashes for each model rather than ids.</p>
<p>I implemented the following function to use it across the board easily.</p>
<pre><code>import random,hashlib
from base64 import urlsafe_b64encode
def set_unique_random_value(model_object,field_name='hash_uuid',length=5,use_sha=True,urlencode=False... | 17 | 2010-03-25T22:29:11Z | 2,520,136 | <p>I do not like this bit:</p>
<pre><code>uuid = uuid[:5]
</code></pre>
<p>In the best scenario (uuid are uniformly distributed) you will get a collision with probability greater than 0.5 after 1k of elements!</p>
<p>It is because of the <a href="http://en.wikipedia.org/wiki/Birthday_problem">birthday problem</a>. I... | 29 | 2010-03-25T23:20:33Z | [
"python",
"django",
"hash",
"random"
] |
Generate unique hashes for django models | 2,519,896 | <p>I want to use unique hashes for each model rather than ids.</p>
<p>I implemented the following function to use it across the board easily.</p>
<pre><code>import random,hashlib
from base64 import urlsafe_b64encode
def set_unique_random_value(model_object,field_name='hash_uuid',length=5,use_sha=True,urlencode=False... | 17 | 2010-03-25T22:29:11Z | 2,523,320 | <p>The ugly:</p>
<blockquote>
<p>import random</p>
</blockquote>
<p><a href="http://docs.python.org/library/random.html" rel="nofollow">From the documentation:</a></p>
<blockquote>
<p>This module implements <strong>pseudo-random</strong> number generators for various distributions.</p>
</blockquote>
<p>If anyth... | 13 | 2010-03-26T12:53:37Z | [
"python",
"django",
"hash",
"random"
] |
Generate unique hashes for django models | 2,519,896 | <p>I want to use unique hashes for each model rather than ids.</p>
<p>I implemented the following function to use it across the board easily.</p>
<pre><code>import random,hashlib
from base64 import urlsafe_b64encode
def set_unique_random_value(model_object,field_name='hash_uuid',length=5,use_sha=True,urlencode=False... | 17 | 2010-03-25T22:29:11Z | 36,777,750 | <p>Django 1.8+ has a built-in <code>UUIDField</code>. Here's the suggested implementation, using the standard library's <code>uuid</code> module, from <a href="https://docs.djangoproject.com/en/stable/ref/models/fields/#uuidfield" rel="nofollow">the docs</a>:</p>
<p></p>
<pre><code>import uuid
from django.db import m... | 3 | 2016-04-21T18:31:53Z | [
"python",
"django",
"hash",
"random"
] |
What algorithms are suitable for this simple machine learning problem? | 2,520,018 | <p>I have a what I think is a simple machine learning question.</p>
<p>Here is the basic problem: I am repeatedly given a new object and a list of descriptions about the object. For example: new_object: <code>'bob'</code> new_object_descriptions: <code>['tall','old','funny']</code>. I then have to use some kind of mac... | 13 | 2010-03-25T22:54:07Z | 2,520,222 | <p>You could use the vector space model (http://en.wikipedia.org/wiki/Vector_space_model). I think what you are trying to learn is how to weight terms in considering how close two object description vectors are to each other, say for example in terms of a simplified mutual information. This could be very efficient as... | 3 | 2010-03-25T23:38:00Z | [
"python",
"artificial-intelligence",
"machine-learning",
"classification",
"neural-network"
] |
What algorithms are suitable for this simple machine learning problem? | 2,520,018 | <p>I have a what I think is a simple machine learning question.</p>
<p>Here is the basic problem: I am repeatedly given a new object and a list of descriptions about the object. For example: new_object: <code>'bob'</code> new_object_descriptions: <code>['tall','old','funny']</code>. I then have to use some kind of mac... | 13 | 2010-03-25T22:54:07Z | 2,520,366 | <p>SVM is pretty fast. <a href="http://www.csie.ntu.edu.tw/~cjlin/libsvm/" rel="nofollow"><strong>LIBSVM</strong></a> for Python, in particular, provides a very decent implementation of Support Vector Machine for classification.</p>
| 2 | 2010-03-26T00:12:00Z | [
"python",
"artificial-intelligence",
"machine-learning",
"classification",
"neural-network"
] |
What algorithms are suitable for this simple machine learning problem? | 2,520,018 | <p>I have a what I think is a simple machine learning question.</p>
<p>Here is the basic problem: I am repeatedly given a new object and a list of descriptions about the object. For example: new_object: <code>'bob'</code> new_object_descriptions: <code>['tall','old','funny']</code>. I then have to use some kind of mac... | 13 | 2010-03-25T22:54:07Z | 2,520,550 | <p>do you really need a machine learning algorithm for this? What is your metric for similarity? You've mentioned the dimensionality of the number of objects, what about the size of the trait set for each person? Are there a maximum number of trait types? I might try something like this:</p>
<p>1) Have a dictionar... | 2 | 2010-03-26T01:05:19Z | [
"python",
"artificial-intelligence",
"machine-learning",
"classification",
"neural-network"
] |
What algorithms are suitable for this simple machine learning problem? | 2,520,018 | <p>I have a what I think is a simple machine learning question.</p>
<p>Here is the basic problem: I am repeatedly given a new object and a list of descriptions about the object. For example: new_object: <code>'bob'</code> new_object_descriptions: <code>['tall','old','funny']</code>. I then have to use some kind of mac... | 13 | 2010-03-25T22:54:07Z | 2,520,617 | <p>This project departs from typical classification applications in two notable ways:</p>
<ul>
<li>Rather than outputting the class which the new object is thought to belong to (or possibly outputting an array of these classes, each with probability / confidence level), the "classifier" provides a list of "neighbors" ... | 1 | 2010-03-26T01:28:30Z | [
"python",
"artificial-intelligence",
"machine-learning",
"classification",
"neural-network"
] |
What algorithms are suitable for this simple machine learning problem? | 2,520,018 | <p>I have a what I think is a simple machine learning question.</p>
<p>Here is the basic problem: I am repeatedly given a new object and a list of descriptions about the object. For example: new_object: <code>'bob'</code> new_object_descriptions: <code>['tall','old','funny']</code>. I then have to use some kind of mac... | 13 | 2010-03-25T22:54:07Z | 2,520,801 | <p>An algorithm that seems to meet your requirements (and is perhaps similar to what John the Statistician is suggesting) is <a href="http://www.cs.toronto.edu/~rsalakhu/papers/semantic_final.pdf">Semantic Hashing</a>. The basic idea is that it trains a deep belief network (a type of neural network that some have calle... | 8 | 2010-03-26T02:21:55Z | [
"python",
"artificial-intelligence",
"machine-learning",
"classification",
"neural-network"
] |
What algorithms are suitable for this simple machine learning problem? | 2,520,018 | <p>I have a what I think is a simple machine learning question.</p>
<p>Here is the basic problem: I am repeatedly given a new object and a list of descriptions about the object. For example: new_object: <code>'bob'</code> new_object_descriptions: <code>['tall','old','funny']</code>. I then have to use some kind of mac... | 13 | 2010-03-25T22:54:07Z | 2,520,819 | <p>What you describe is somewhat similar to the <code>Locally Weighted Learning</code> algorithm, which given a query instance, it trains a model locally around the neighboring instances weighted by their distances to the query one.</p>
<p>Weka (Java) has an implementation of this in <a href="http://wiki.pentaho.com/d... | 1 | 2010-03-26T02:27:12Z | [
"python",
"artificial-intelligence",
"machine-learning",
"classification",
"neural-network"
] |
What algorithms are suitable for this simple machine learning problem? | 2,520,018 | <p>I have a what I think is a simple machine learning question.</p>
<p>Here is the basic problem: I am repeatedly given a new object and a list of descriptions about the object. For example: new_object: <code>'bob'</code> new_object_descriptions: <code>['tall','old','funny']</code>. I then have to use some kind of mac... | 13 | 2010-03-25T22:54:07Z | 22,704,670 | <p>deep learning can be used. <a href="http://www.deeplearning.net/tutorial/" rel="nofollow">http://www.deeplearning.net/tutorial/</a> just go through this link</p>
| 0 | 2014-03-28T05:10:33Z | [
"python",
"artificial-intelligence",
"machine-learning",
"classification",
"neural-network"
] |
Django OpenID django-openid-auth Login Error | 2,520,144 | <p>I get the following error when attempting to use django-openid-auth <code>OpenID discovery error: No usable OpenID services found for *******@gmail.com</code> </p>
<p>I have followed the instructions that come with it, though it seems there is something I am missing. the installation is on my localhost. </p>
| 0 | 2010-03-25T23:22:37Z | 2,520,867 | <p>Are you supplying an email address or URL? OpenID needs a URL and <code>*******@gmail.com</code> is an email address.</p>
| 1 | 2010-03-26T02:42:06Z | [
"python",
"django",
"openid"
] |
QTreeWidget insertTopLevelItem - index given not accurately displayed in Tree? | 2,520,282 | <p>I am unable to properly insert a QTreeWidgetItem at a specific index, in this case I am removing all QTreeWidgetItems from the tree, doing a custom sort on their Date Objects and then inserting them back into the QTreeWidget. </p>
<p>However, upon inserting (even one at a time) the QTreeWidgetItem is not inserted i... | 0 | 2010-03-25T23:54:41Z | 2,524,316 | <p>You can do your custom sort without a need to refill tree. You just need to overload item's 'less' operator.
Note, that QT figures out, what to draw in the cell, what size it should be, what text should it contain, by looking in item's</p>
<pre><code>virtual QVariant data ( int column, int role ) const
</code></pre... | 2 | 2010-03-26T15:16:17Z | [
"python",
"qt",
"pyqt",
"qtreewidget",
"qtreewidgetitem"
] |
QTreeWidget insertTopLevelItem - index given not accurately displayed in Tree? | 2,520,282 | <p>I am unable to properly insert a QTreeWidgetItem at a specific index, in this case I am removing all QTreeWidgetItems from the tree, doing a custom sort on their Date Objects and then inserting them back into the QTreeWidget. </p>
<p>However, upon inserting (even one at a time) the QTreeWidgetItem is not inserted i... | 0 | 2010-03-25T23:54:41Z | 2,525,664 | <p>The problem must be elsewhere in your code. Maybe you aren't removing the items properly? Because the following works:</p>
<pre><code>import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
app = QApplication(sys.argv)
tree = QTreeWidget()
items = [QTreeWidgetItem(['index %i' % i]) for i in range(5)]
... | 1 | 2010-03-26T18:11:34Z | [
"python",
"qt",
"pyqt",
"qtreewidget",
"qtreewidgetitem"
] |
Python OSError not reporting errors | 2,520,325 | <p>Ive got this snippet that Im using to convert image files to tiff. I want to be informed when a file fails to convert. Imagemagick exits 0 when successfully run, so I figured the following snippet would report the issue. However no errors are being reported at all. </p>
<pre><code>
def image(filePath,dirPath,... | 0 | 2010-03-26T00:03:27Z | 2,520,354 | <p><code>os.system()</code> does not throw an exception if the return value is non-zero. What you should do is capture the return value and check that:</p>
<pre><code>ret = os.system(...)
if ret == ...:
</code></pre>
<p>Of course, what you should <em>also</em> do is replace <code>os.system()</code> with <a href="http... | 5 | 2010-03-26T00:09:39Z | [
"python",
"imagemagick"
] |
Python OSError not reporting errors | 2,520,325 | <p>Ive got this snippet that Im using to convert image files to tiff. I want to be informed when a file fails to convert. Imagemagick exits 0 when successfully run, so I figured the following snippet would report the issue. However no errors are being reported at all. </p>
<pre><code>
def image(filePath,dirPath,... | 0 | 2010-03-26T00:03:27Z | 2,520,378 | <p>A better think will be to use <a href="http://docs.python.org/library/subprocess.html#subprocess.check_call" rel="nofollow">check_call</a> from the subprocess module, it raises CalledProcessError when the subprocess returned a non zero value.</p>
| 3 | 2010-03-26T00:15:00Z | [
"python",
"imagemagick"
] |
Python OSError not reporting errors | 2,520,325 | <p>Ive got this snippet that Im using to convert image files to tiff. I want to be informed when a file fails to convert. Imagemagick exits 0 when successfully run, so I figured the following snippet would report the issue. However no errors are being reported at all. </p>
<pre><code>
def image(filePath,dirPath,... | 0 | 2010-03-26T00:03:27Z | 2,520,622 | <p>You can access <a href="http://www.imagemagick.org/script/index.php" rel="nofollow">ImageMagick</a> directly through Python using PythonMagick (<a href="http://www.imagemagick.org/download/python/" rel="nofollow">download here</a>). A more popular tool for image manipulation is <a href="http://www.pythonware.com/pro... | 1 | 2010-03-26T01:29:08Z | [
"python",
"imagemagick"
] |
Python OSError not reporting errors | 2,520,325 | <p>Ive got this snippet that Im using to convert image files to tiff. I want to be informed when a file fails to convert. Imagemagick exits 0 when successfully run, so I figured the following snippet would report the issue. However no errors are being reported at all. </p>
<pre><code>
def image(filePath,dirPath,... | 0 | 2010-03-26T00:03:27Z | 2,520,653 | <p><code>+</code> is generally a bad way to build strings in Python. </p>
<p>I would tend to replace <code>"convert " + filePath + " +compress " + dirPath + "/" + shortFile + ".tif"</code> with</p>
<pre><code>import os.path
"convert %s +compress %s.tif" % (filePath, os.path.join(dirPath, shortFile))
</code></pre>
<p... | 0 | 2010-03-26T01:35:43Z | [
"python",
"imagemagick"
] |
mongokit and django | 2,520,440 | <p>I'm looking for good tutorial or really simple code to integrate mongokit and django</p>
| 0 | 2010-03-26T00:33:03Z | 2,520,736 | <p><a href="http://www.peterbe.com/plog/how-and-why-to-use-django-mongokit">http://www.peterbe.com/plog/how-and-why-to-use-django-mongokit</a> would seem to cover this. BTW this was the first result on google for search term "mongokit django".</p>
| 5 | 2010-03-26T01:57:40Z | [
"python",
"django",
"mongodb"
] |
Check if Django model field choices exists | 2,520,598 | <p>I'm attempting to check if a value exists in the choices tuple set for a model field. </p>
<p>For example lets say I have a Model like this:</p>
<pre><code>class Vote(models.Model):
VOTE_TYPE = (
(1, "Up"),
(-1, "Down"),
)
value = models.SmallIntegerField(max_length=1, choices=VOTE_TYPES)
</code></pre>
<... | 1 | 2010-03-26T01:22:33Z | 2,520,933 | <p>First off, <code>models.SmallIntegerField</code> cannot have a <code>max_length</code> option. That are used only in <code>CharField</code> and it's derivatives. Secondly, according to the <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#choices" rel="nofollow">documentation</a>, the <code>choices</c... | 1 | 2010-03-26T03:03:04Z | [
"python",
"django",
"django-models"
] |
Check if Django model field choices exists | 2,520,598 | <p>I'm attempting to check if a value exists in the choices tuple set for a model field. </p>
<p>For example lets say I have a Model like this:</p>
<pre><code>class Vote(models.Model):
VOTE_TYPE = (
(1, "Up"),
(-1, "Down"),
)
value = models.SmallIntegerField(max_length=1, choices=VOTE_TYPES)
</code></pre>
<... | 1 | 2010-03-26T01:22:33Z | 2,520,936 | <ol>
<li><p>Are you sure you want to set value to "Up"? that is a display value, you might be interested in setting value to -1 or 1</p></li>
<li><p>You can easily check if a choice is in list by converting choice list to dict e.g.</p>
<p><code>my_choice in dict(VOTE_TYPE)</code></p></li>
<li><p>If possible you can us... | 8 | 2010-03-26T03:04:10Z | [
"python",
"django",
"django-models"
] |
Django templates crashes with no sense | 2,520,618 | <p>Hello I'm trying to use google visualization API along with django templates system. I got an error that don't know how to fix. The error is the following:</p>
<pre><code> invalid_block_tag
raise self.error(token, "Invalid block tag: '%s'" % command)
django.template.TemplateSyntaxError: Invalid block tag: 'endf... | 0 | 2010-03-26T01:28:34Z | 2,520,678 | <p>You goofed your <code>for</code> tag:</p>
<pre><code>{% for d in datos %}
</code></pre>
| 2 | 2010-03-26T01:42:21Z | [
"python",
"django-templates"
] |
How do you throw an HTTP error with mod_python | 2,520,659 | <p>I have a setup where I'm serving simple python pages using the mod_python publisher. At some points I'd like to have the python function raise a standard apache error - for example throwing a 500 error if a required file is missing. How can I throw an apache error from within a mod_python script?</p>
| 1 | 2010-03-26T01:36:45Z | 2,676,325 | <p>I am not a python expert but from <a href="http://www.modpython.org/live/current/doc-html/pyapi-handler.html" rel="nofollow">this documentation</a>, it would appear that you can do:</p>
<pre><code>raise apache.SERVER_RETURN, apache.HTTP_INTERNAL_SERVER_ERROR
</code></pre>
| 1 | 2010-04-20T15:19:46Z | [
"python",
"apache",
"mod-python",
"http-error"
] |
How do you throw an HTTP error with mod_python | 2,520,659 | <p>I have a setup where I'm serving simple python pages using the mod_python publisher. At some points I'd like to have the python function raise a standard apache error - for example throwing a 500 error if a required file is missing. How can I throw an apache error from within a mod_python script?</p>
| 1 | 2010-03-26T01:36:45Z | 2,676,485 | <p>I believe it's:</p>
<pre><code>def my_action(req):
# all the status code constants are defined in the apache module
req.status = apache.HTTP_INTERNAL_SERVER_ERROR
req.content_type = some_mime_type
req.write(content)
raise apache.HTTP_SERVER_RETURN, apache.DONE
</code></pre>
<p>raising apache.DONE tells... | 1 | 2010-04-20T15:39:50Z | [
"python",
"apache",
"mod-python",
"http-error"
] |
How to flush the input stream in python? | 2,520,893 | <p>I'm writing a simple alarm utility in Python.</p>
<pre><code>#!/usr/bin/python
import time
import subprocess
import sys
alarm1 = int(raw_input("How many minutes (alarm1)? "))
while (1):
time.sleep(60*alarm1)
print "Alarm1"
sys.stdout.flush()
doit = raw_input("Continue (Y/N)?[Y]: ")
print "Inp... | 8 | 2010-03-26T02:49:53Z | 2,521,030 | <pre><code>#!/usr/bin/python
import time
import subprocess
import sys
import os, select
alarm1 = int(raw_input("How many minutes (alarm1)? "))
while (1):
time.sleep(3*alarm1)
print "Alarm1"
sys.stdout.flush()
while len(select.select([sys.stdin.fileno()], [], [], 0.0)[0])>0:
os.read(sys.std... | 1 | 2010-03-26T03:46:35Z | [
"python"
] |
How to flush the input stream in python? | 2,520,893 | <p>I'm writing a simple alarm utility in Python.</p>
<pre><code>#!/usr/bin/python
import time
import subprocess
import sys
alarm1 = int(raw_input("How many minutes (alarm1)? "))
while (1):
time.sleep(60*alarm1)
print "Alarm1"
sys.stdout.flush()
doit = raw_input("Continue (Y/N)?[Y]: ")
print "Inp... | 8 | 2010-03-26T02:49:53Z | 2,521,032 | <p>On Unices, you can use <a href="http://docs.python.org/library/termios.html#termios.tcflush" rel="nofollow"><code>termios.tcflush()</code></a>:</p>
<pre><code>import time
import subprocess
import sys
from termios import tcflush, TCIOFLUSH
alarm1 = int(raw_input("How many minutes (alarm1)? "))
while (1):
time.... | 3 | 2010-03-26T03:47:06Z | [
"python"
] |
How to flush the input stream in python? | 2,520,893 | <p>I'm writing a simple alarm utility in Python.</p>
<pre><code>#!/usr/bin/python
import time
import subprocess
import sys
alarm1 = int(raw_input("How many minutes (alarm1)? "))
while (1):
time.sleep(60*alarm1)
print "Alarm1"
sys.stdout.flush()
doit = raw_input("Continue (Y/N)?[Y]: ")
print "Inp... | 8 | 2010-03-26T02:49:53Z | 2,521,054 | <p>It would help to know what operating system you're using, as this is a very operating-system-specific question. For example, Kylar's answer doesn't work on Windows because sys.stdin doesn't have a fileno attribute.</p>
<p>I was curious and threw together a solution using curses, but this won't work on Windows eithe... | 4 | 2010-03-26T03:56:09Z | [
"python"
] |
How to flush the input stream in python? | 2,520,893 | <p>I'm writing a simple alarm utility in Python.</p>
<pre><code>#!/usr/bin/python
import time
import subprocess
import sys
alarm1 = int(raw_input("How many minutes (alarm1)? "))
while (1):
time.sleep(60*alarm1)
print "Alarm1"
sys.stdout.flush()
doit = raw_input("Continue (Y/N)?[Y]: ")
print "Inp... | 8 | 2010-03-26T02:49:53Z | 38,993,222 | <p>From <a href="https://rosettacode.org/wiki/Keyboard_input/Flush_the_keyboard_buffer#Python" rel="nofollow">Rosetta Code</a></p>
<pre><code>def flush_input():
try:
import msvcrt
while msvcrt.kbhit():
msvcrt.getch()
except ImportError:
import sys, termios #for linux/unix
termios.tcflush(sys... | 1 | 2016-08-17T09:48:00Z | [
"python"
] |
Query for model by key | 2,521,012 | <p>What I'm trying to do is query the datastore for a model where the key is <em>not</em> the key of an object I already have. Here's some code:</p>
<pre><code>class User(db.Model):
partner = db.SelfReferenceProperty()
def text_message(self, msg):
user = User.get_or_insert(msg.sender)
if not user.partner... | 4 | 2010-03-26T03:39:29Z | 2,522,395 | <p>There's a really easy way around this: Retrieve two records, and filter out the user's own one, if it's present.</p>
<pre><code>def text_message(self, msg):
user = User.get_or_insert(msg.sender)
if not user.partner:
# user doesn't have a partner, find them one
other = db.Query(User).filter(... | 4 | 2010-03-26T10:13:46Z | [
"python",
"google-app-engine",
"gae-datastore"
] |
Purpose of Zope Interfaces? | 2,521,189 | <p>I have started using Zope interfaces in my code, and as of now, they are really only documentation. I use them to specify what attributes the class should possess, explicitly implement them in the appropriate classes and explicitly check for them where I expect one. This is fine, but I would like them to do more if ... | 32 | 2010-03-26T04:45:26Z | 2,521,316 | <p>I've never used Zope interfaces, but you might consider writing a <a href="http://www.ibm.com/developerworks/linux/library/l-pymeta.html" rel="nofollow">metaclass</a>, which on initialization checks the members of the class against the interface, and raises a runtime exception if a method isn't implemented.</p>
<p>... | 1 | 2010-03-26T05:40:51Z | [
"python",
"interface",
"zope",
"zope.interface"
] |
Purpose of Zope Interfaces? | 2,521,189 | <p>I have started using Zope interfaces in my code, and as of now, they are really only documentation. I use them to specify what attributes the class should possess, explicitly implement them in the appropriate classes and explicitly check for them where I expect one. This is fine, but I would like them to do more if ... | 32 | 2010-03-26T04:45:26Z | 2,521,650 | <p>You can actually test if your object or class implements your interface.
For that you can use <code>verify</code> module (you would normally use it in your tests):</p>
<pre><code>>>> from zope.interface import Interface, Attribute, implements
>>> class IFoo(Interface):
... x = Attribute("The ... | 22 | 2010-03-26T07:20:22Z | [
"python",
"interface",
"zope",
"zope.interface"
] |
Purpose of Zope Interfaces? | 2,521,189 | <p>I have started using Zope interfaces in my code, and as of now, they are really only documentation. I use them to specify what attributes the class should possess, explicitly implement them in the appropriate classes and explicitly check for them where I expect one. This is fine, but I would like them to do more if ... | 32 | 2010-03-26T04:45:26Z | 2,642,063 | <p>Zope interfaces can provide a useful way to decouple two pieces of code that shouldn't depend on each other.</p>
<p>Say we have a component that knows how to print a greeting in module a.py:</p>
<pre><code>>>> class Greeter(object):
... def greet(self):
... print 'Hello'
</code></pre>
<p>And ... | 18 | 2010-04-15T01:02:30Z | [
"python",
"interface",
"zope",
"zope.interface"
] |
Purpose of Zope Interfaces? | 2,521,189 | <p>I have started using Zope interfaces in my code, and as of now, they are really only documentation. I use them to specify what attributes the class should possess, explicitly implement them in the appropriate classes and explicitly check for them where I expect one. This is fine, but I would like them to do more if ... | 32 | 2010-03-26T04:45:26Z | 2,840,774 | <p>Where I work, we use Interfaces so that we can use ZCA, or the <a href="http://www.muthukadan.net/docs/zca.html">Zope Component Architecture</a>, which is a whole framework for making components that are swappable and pluggable using <code>Interface</code>s. We use ZCA so that we can cope with all manner of per-clie... | 44 | 2010-05-15T16:05:57Z | [
"python",
"interface",
"zope",
"zope.interface"
] |
Getting BeautifulSoup to find a specific <p> | 2,521,482 | <p>I'm trying to put together a basic HTML scraper for a variety of scientific journal websites, specifically trying to get the abstract or introductory paragraph. </p>
<p>The current journal I'm working on is Nature, and the article I've been using as my sample can be seen at <a href="http://www.nature.com/nature/jo... | 2 | 2010-03-26T06:32:42Z | 2,521,745 | <p>That html is pretty much malformed, and xml.dom.minidom cannot parse, and BeautiFulSoup parsing not working well.</p>
<p>I removed some <code><!-- ... --></code> parts and parse again with BeautiFulSoup, then its seems better and able to run <code>soup.find('p', attrs={'class' : 'lead'})</code></p>
<p>Here's... | 2 | 2010-03-26T07:51:00Z | [
"python",
"beautifulsoup",
"html-content-extraction"
] |
Getting BeautifulSoup to find a specific <p> | 2,521,482 | <p>I'm trying to put together a basic HTML scraper for a variety of scientific journal websites, specifically trying to get the abstract or introductory paragraph. </p>
<p>The current journal I'm working on is Nature, and the article I've been using as my sample can be seen at <a href="http://www.nature.com/nature/jo... | 2 | 2010-03-26T06:32:42Z | 2,521,794 | <p>here's a non BS way to get the abstract. </p>
<pre><code>address="http://www.nature.com/nature/journal/v463/n7284/full/nature08715.html"
html = urllib2.urlopen(address).read()
for para in html.split("</p>"):
if '<p class="lead">' in para:
abstract=para.split('<p class="lead">')[1:][0]
... | 2 | 2010-03-26T08:03:01Z | [
"python",
"beautifulsoup",
"html-content-extraction"
] |
Avoid IF statement after condition has been met | 2,521,558 | <p>I have a division operation inside a cycle that repeats many times. It so happens that in the first few passes through the loop (more or less first 10 loops) the divisor is zero. Once it gains value, a div by zero error is not longer possible.</p>
<p>I have an <code>if</code> condition to test the divisor value in ... | 4 | 2010-03-26T06:54:05Z | 2,521,564 | <p>Don't worry. An <code>if (a != 0)</code> is cheap.</p>
<p>The alternative (if you really want one) could be to split the loop into two, and exit the first one once the divisor gets its value. But that sounds like it would make the code unnecessarily complex (difficult to read).</p>
| 9 | 2010-03-26T06:56:46Z | [
"python",
"performance",
"if-statement"
] |
Avoid IF statement after condition has been met | 2,521,558 | <p>I have a division operation inside a cycle that repeats many times. It so happens that in the first few passes through the loop (more or less first 10 loops) the divisor is zero. Once it gains value, a div by zero error is not longer possible.</p>
<p>I have an <code>if</code> condition to test the divisor value in ... | 4 | 2010-03-26T06:54:05Z | 2,521,596 | <p>I'm with Thilo: I think it should be pretty cheap.</p>
<p>If you really care, you should time the code and find out whether the slight overhead is unacceptable. I suspect not.</p>
| 1 | 2010-03-26T07:05:38Z | [
"python",
"performance",
"if-statement"
] |
Avoid IF statement after condition has been met | 2,521,558 | <p>I have a division operation inside a cycle that repeats many times. It so happens that in the first few passes through the loop (more or less first 10 loops) the divisor is zero. Once it gains value, a div by zero error is not longer possible.</p>
<p>I have an <code>if</code> condition to test the divisor value in ... | 4 | 2010-03-26T06:54:05Z | 2,521,624 | <p>I would wrap your call in try/except blocks. They are very cheap in python, and cost about as much as a pass statement if an exception isn't thrown. Python is designed so that you should make your calls and parse any errors instead of always asking permission.</p>
<p>Example code:</p>
<pre><code>def print_divide(x... | 5 | 2010-03-26T07:13:21Z | [
"python",
"performance",
"if-statement"
] |
Proper way to set object instance variables | 2,521,753 | <p>I'm writing a class to insert users into a database, and before I get too far in, I just want to make sure that my OO approach is clean:</p>
<pre><code>class User(object):
def setName(self,name):
#Do sanity checks on name
self._name = name
def setPassword(self,password):
#Check p... | 9 | 2010-03-26T07:52:32Z | 2,521,768 | <p>There are no class variables in that code, only instance attributes. And use <a href="http://docs.python.org/library/functions.html#property" rel="nofollow">properties</a> instead of accessors. And do create the instance attributes in the initializer, preferably from values passed in:</p>
<pre><code>class User(obje... | 3 | 2010-03-26T07:58:29Z | [
"python",
"oop",
"pylons"
] |
Proper way to set object instance variables | 2,521,753 | <p>I'm writing a class to insert users into a database, and before I get too far in, I just want to make sure that my OO approach is clean:</p>
<pre><code>class User(object):
def setName(self,name):
#Do sanity checks on name
self._name = name
def setPassword(self,password):
#Check p... | 9 | 2010-03-26T07:52:32Z | 2,521,792 | <p>It's generally correct, AFAIK, but you could clean it up with <a href="http://docs.python.org/library/functions.html#property">properties</a>.</p>
<pre><code>class User(object):
def _setName(self, name=None):
self._name = name
def _getName(self):
return self._name
def _setPassword(sel... | 13 | 2010-03-26T08:02:48Z | [
"python",
"oop",
"pylons"
] |
Proper way to set object instance variables | 2,521,753 | <p>I'm writing a class to insert users into a database, and before I get too far in, I just want to make sure that my OO approach is clean:</p>
<pre><code>class User(object):
def setName(self,name):
#Do sanity checks on name
self._name = name
def setPassword(self,password):
#Check p... | 9 | 2010-03-26T07:52:32Z | 2,521,800 | <p>using a single _ does not make your attributes private: it is a convention telling that it is an internal attribute and should not under normal circumstances be accessed by external code. With your code it also means password and name are read only. </p>
<p>I strongly suggest using an initialiser for your class whi... | 6 | 2010-03-26T08:03:45Z | [
"python",
"oop",
"pylons"
] |
Proper way to set object instance variables | 2,521,753 | <p>I'm writing a class to insert users into a database, and before I get too far in, I just want to make sure that my OO approach is clean:</p>
<pre><code>class User(object):
def setName(self,name):
#Do sanity checks on name
self._name = name
def setPassword(self,password):
#Check p... | 9 | 2010-03-26T07:52:32Z | 2,523,617 | <p>Others already pointed that out: avoid using setters and getters and use simple attribute access if you don't need to perform additional logic when getting/setting your attribute.
If you do need that additional logic then use properties.</p>
<p>If you have many parameters to pass to an instance initializer consider... | 2 | 2010-03-26T13:37:46Z | [
"python",
"oop",
"pylons"
] |
Possible to use Python with Intel's Atom Developer SDK (C/C++)? | 2,521,774 | <p>So I've made a game in Python and PyGame. Now I'm interested in submitting the game to <a href="http://www.ludumdare.com/compo/2010/03/02/intels-march-developer-challenge/" rel="nofollow">Intel's March Developer Challenge</a>. However, the developer challenge requires use of Intel's Atom Developer SDK (http://appdev... | 0 | 2010-03-26T08:00:08Z | 2,521,796 | <p>Don't drop down to bare C if you can help it. Write bindings using <a href="http://www.cython.org/" rel="nofollow">Cython</a>.</p>
| 1 | 2010-03-26T08:03:15Z | [
"python",
"c",
"visual-studio",
"pygame",
"intel-atom"
] |
Automatically Update Field when a Different Field is Changed | 2,521,793 | <p>I have a model with a bunch of different fields like first_name, last_name, etc. I also have fields first_name_ud, last_name_ud, etc. that correspond to the last updated date for the related fields (i.e. when first_name is modified, then first_name_ud is set to the current date).</p>
<p>Is there a way to make this... | 5 | 2010-03-26T08:02:54Z | 2,521,806 | <p>Either <a href="http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#howto-custom-model-fields" rel="nofollow">write <code>Field</code> children</a> that update both fields or use server-side triggers.</p>
| 3 | 2010-03-26T08:05:16Z | [
"python",
"django",
"django-models",
"django-forms"
] |
Automatically Update Field when a Different Field is Changed | 2,521,793 | <p>I have a model with a bunch of different fields like first_name, last_name, etc. I also have fields first_name_ud, last_name_ud, etc. that correspond to the last updated date for the related fields (i.e. when first_name is modified, then first_name_ud is set to the current date).</p>
<p>Is there a way to make this... | 5 | 2010-03-26T08:02:54Z | 2,526,117 | <p>Thanks for your help. I ended up modifying the model's save method, which I think will work:</p>
<pre><code>def save(self):
current_date = date.today()
if self.id:
try:
old = UserProfile.objects.get(pk = self.id)
fields = UserProfile._meta.fields
for field in fie... | 0 | 2010-03-26T19:25:54Z | [
"python",
"django",
"django-models",
"django-forms"
] |
Get a list/tuple/dict of the arguments passed to a function? | 2,521,901 | <p>Given the following function:</p>
<pre><code>def foo(a, b, c):
pass
</code></pre>
<p>How would one obtain a list/tuple/dict/etc of the arguments passed in, <strong>without having to build the structure myself</strong>?</p>
<p>Specifically, I'm looking for Python's version of JavaScript's <code>arguments</code... | 30 | 2010-03-26T08:30:54Z | 2,521,925 | <p>You've specified the parameters in the header?</p>
<p>Why don't you simply use that same info in the body?</p>
<pre><code>def foo(a, b, c):
params = [a, b, c]
</code></pre>
<p>What have I missed?</p>
| 2 | 2010-03-26T08:34:37Z | [
"python",
"function",
"arguments"
] |
Get a list/tuple/dict of the arguments passed to a function? | 2,521,901 | <p>Given the following function:</p>
<pre><code>def foo(a, b, c):
pass
</code></pre>
<p>How would one obtain a list/tuple/dict/etc of the arguments passed in, <strong>without having to build the structure myself</strong>?</p>
<p>Specifically, I'm looking for Python's version of JavaScript's <code>arguments</code... | 30 | 2010-03-26T08:30:54Z | 2,521,928 | <p>You can create a list out of them using:</p>
<pre>
args = [a, b, c]
</pre>
<p>You can easily create a tuple out of them using:</p>
<pre>
args = (a, b, c)
</pre>
| 1 | 2010-03-26T08:35:27Z | [
"python",
"function",
"arguments"
] |
Get a list/tuple/dict of the arguments passed to a function? | 2,521,901 | <p>Given the following function:</p>
<pre><code>def foo(a, b, c):
pass
</code></pre>
<p>How would one obtain a list/tuple/dict/etc of the arguments passed in, <strong>without having to build the structure myself</strong>?</p>
<p>Specifically, I'm looking for Python's version of JavaScript's <code>arguments</code... | 30 | 2010-03-26T08:30:54Z | 2,521,937 | <p>You can use <code>locals()</code> to get a dict of the local variables in your function, like this:</p>
<pre><code>def foo(a, b, c):
print locals()
>>> foo(1, 2, 3)
{'a': 1, 'c': 3, 'b': 2}
</code></pre>
<p>This is a bit hackish, however, as <code>locals()</code> returns all variables in the local sc... | 35 | 2010-03-26T08:38:23Z | [
"python",
"function",
"arguments"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.