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 listlengths 1 5 |
|---|---|---|---|---|---|---|---|---|---|
BytesIO with python v2.5 | 1,279,244 | <p><strong>Question:</strong></p>
<p>How do I get a byte stream that works like StringIO for Python 2.5?</p>
<p><strong>Application:</strong></p>
<p>I'm converting a PDF to text, but don't want to save a file to the hard disk.</p>
<p><strong>Other Thoughts:</strong></p>
<p>I figured I could use StringIO, but there... | 1 | 2009-08-14T17:54:01Z | 1,279,265 | <p>If you're working with PDF, then <code>StringIO</code> should be fine as long as you pay heed to the docs:</p>
<blockquote>
<p>The StringIO object can accept either Unicode or 8-bit strings, but mixing the two may take some care. If both are used, 8-bit strings that cannot be interpreted as 7-bit ASCII (that use ... | 2 | 2009-08-14T17:58:23Z | [
"python",
"stringio",
"bytesio"
] |
How do I use extended characters in Python's curses library? | 1,279,341 | <p>I've been reading tutorials about Curses programming in Python, and many refer to an ability to use extended characters, such as line-drawing symbols. They're characters > 255, and the curses library knows how to display them in the current terminal font.</p>
<p>Some of the tutorials say you use it like this:</p>
... | 4 | 2009-08-14T18:15:17Z | 1,279,422 | <p>From <code>curses/__init__.py</code>:</p>
<blockquote>
<p>Some constants, most notably the <code>ACS_*</code>
ones, are only added to the C
<code>_curses</code> module's dictionary after <code>initscr()</code> is called. (Some versions
of SGI's curses don't define values
for those constants until <code>i... | 2 | 2009-08-14T18:30:59Z | [
"python",
"curses"
] |
How do I use extended characters in Python's curses library? | 1,279,341 | <p>I've been reading tutorials about Curses programming in Python, and many refer to an ability to use extended characters, such as line-drawing symbols. They're characters > 255, and the curses library knows how to display them in the current terminal font.</p>
<p>Some of the tutorials say you use it like this:</p>
... | 4 | 2009-08-14T18:15:17Z | 16,564,494 | <p>I believe the below is appropriately related, to be posted under this question. Here I'll be using <a href="http://sdaaubckp.svn.sourceforge.net/viewvc/sdaaubckp/single-scripts/utfinfo.pl" rel="nofollow">utfinfo.pl</a> (see also on <a href="http://superuser.com/questions/581523/program-to-check-look-up-utf-8-unicode... | 3 | 2013-05-15T12:02:49Z | [
"python",
"curses"
] |
How do I use extended characters in Python's curses library? | 1,279,341 | <p>I've been reading tutorials about Curses programming in Python, and many refer to an ability to use extended characters, such as line-drawing symbols. They're characters > 255, and the curses library knows how to display them in the current terminal font.</p>
<p>Some of the tutorials say you use it like this:</p>
... | 4 | 2009-08-14T18:15:17Z | 28,426,471 | <p>you have to set your local to all, then encode your output as utf-8 as follows:</p>
<pre><code>import curses
import locale
locale.setlocale(locale.LC_ALL, '') # set your locale
scr = curses.initscr()
scr.clear()
scr.addstr(0, 0, u'\u3042'.encode('utf-8'))
scr.refresh()
# here implement simple code to wait for ... | 2 | 2015-02-10T07:43:07Z | [
"python",
"curses"
] |
sqlalchemy - grouping items and iterating over the sub-lists | 1,279,356 | <p>Consider a table like this:</p>
<pre><code>| Name | Version | Other |
| ---------------------|-------|
| Foo | 1 | 'a' |
| Foo | 2 | 'b' |
| Bar | 5 | 'c' |
| Baz | 3 | 'd' |
| Baz | 4 | 'e' |
| Baz | 5 | 'f' |
---------... | 2 | 2009-08-14T18:18:30Z | 1,279,433 | <p>Here's the SQL you can call with the <code>engine.execute</code> command:</p>
<pre><code>select
t1.*
from
table t1
inner join
(select
Name,
max(version) as Version
from
table
group by
name) s on
s.name = t1.name
a... | -1 | 2009-08-14T18:32:49Z | [
"python",
"sql",
"sqlalchemy"
] |
sqlalchemy - grouping items and iterating over the sub-lists | 1,279,356 | <p>Consider a table like this:</p>
<pre><code>| Name | Version | Other |
| ---------------------|-------|
| Foo | 1 | 'a' |
| Foo | 2 | 'b' |
| Bar | 5 | 'c' |
| Baz | 3 | 'd' |
| Baz | 4 | 'e' |
| Baz | 5 | 'f' |
---------... | 2 | 2009-08-14T18:18:30Z | 1,279,554 | <p>If you need full objects you'll need to select maximum versions by name in a subquery and join to that:</p>
<pre><code>max_versions = session.query(Cls.name, func.max(Cls.version).label('max_version'))\
.group_by(Cls.name).subquery()
objs = session.query(Cls).join((max_versions,
and... | 4 | 2009-08-14T18:53:40Z | [
"python",
"sql",
"sqlalchemy"
] |
Default route doesn't work | 1,279,403 | <p>I'm using the standard routing module with pylons to try and setup a default route for the home page of my website. </p>
<p>I've followed the instructions in the docs and here <a href="http://routes.groovie.org/recipes.html" rel="nofollow">http://routes.groovie.org/recipes.html</a> but when I try <code>http://127.0... | 4 | 2009-08-14T18:27:52Z | 1,280,099 | <p>You have to delete the static page (myapp/public/index.html). Static
files take priority due to the Cascade configuration at the end of
middleware.py. </p>
| 9 | 2009-08-14T20:40:39Z | [
"python",
"routes",
"pylons"
] |
Django - SQL Query - Timestamp | 1,279,490 | <p>Can anyone turn me to a tutorial, code or some kind of resource that will help me out with the following problem.</p>
<p>I have a table in a mySQL database. It contains an ID, Timestamp, another ID and a value. I'm passing it the 'main' ID which can uniquely identify a piece of data. However, I want to do a time se... | 0 | 2009-08-14T18:41:26Z | 1,279,577 | <p>If the table in question maps to a Django model <code>MyModel</code>, e.g.</p>
<pre><code>class MyModel(models.Model):
...
primaryid = ...
timestamp = ...
secondaryid = ...
valuefield = ...
</code></pre>
<p>then you can use</p>
<pre><code>MyModel.objects.filter(
primaryi... | 3 | 2009-08-14T18:58:46Z | [
"python",
"mysql",
"django"
] |
Django - SQL Query - Timestamp | 1,279,490 | <p>Can anyone turn me to a tutorial, code or some kind of resource that will help me out with the following problem.</p>
<p>I have a table in a mySQL database. It contains an ID, Timestamp, another ID and a value. I'm passing it the 'main' ID which can uniquely identify a piece of data. However, I want to do a time se... | 0 | 2009-08-14T18:41:26Z | 1,284,454 | <p>I don't think Vinay Sajip's answer is correct. The closest correct variant based on his code is:</p>
<pre><code>MyModel.objects.filter(
primaryid=1987
).exclude(
timestamp__lt=min_timestamp
).exclude(
ti... | 2 | 2009-08-16T14:44:12Z | [
"python",
"mysql",
"django"
] |
SQLAlchemy - Trying Eager loading.. Attribute Error | 1,279,583 | <p>I access a a postgres table using SQLAlchemy. I want a query to have eagerloading.</p>
<pre><code>from sqlalchemy.orm import sessionmaker, scoped_session, eagerload
from settings import DATABASE_USER, DATABASE_PASSWORD, DATABASE_HOST, DATABASE_PORT, DATABASE_NAME
from sqlalchemy import create_engine
from sqlalchemy... | 1 | 2009-08-14T18:59:49Z | 1,279,980 | <p>You can only eager load on a relation property. Not on the table itself. Eager loading is meant for loading objects from other tables at the same time as getting a particular object. The way you load all the objects for a query will be simply adding all().</p>
<pre><code>query = session.query(Zipcode).options(eag... | 2 | 2009-08-14T20:19:18Z | [
"python",
"postgresql",
"sqlalchemy"
] |
What is an ORM and where can I learn more about it? | 1,279,613 | <p>Someone suggested I use an ORM for a project I'm designing but I'm having trouble finding information on what it is or how it works. Can anyone give me a brief explanation or a link as to where I can learn more about it?</p>
| 64 | 2009-08-14T19:03:59Z | 1,279,636 | <p>This is a huge topic. Pick up a good hibernate book and it should explain ORM in detail before getting to the nitty gritty hibernate material.</p>
<p><a href="https://www.hibernate.org/" rel="nofollow">https://www.hibernate.org/</a></p>
| 0 | 2009-08-14T19:08:17Z | [
"python",
"orm"
] |
What is an ORM and where can I learn more about it? | 1,279,613 | <p>Someone suggested I use an ORM for a project I'm designing but I'm having trouble finding information on what it is or how it works. Can anyone give me a brief explanation or a link as to where I can learn more about it?</p>
| 64 | 2009-08-14T19:03:59Z | 1,279,638 | <p>An ORM (Object Relational Mapper) is a piece/layer of software that helps map your code Objects to your database.</p>
<p>Some handle more aspects than others...but the purpose is to take some of the weight of the Data Layer off of the developer's shoulders.</p>
<p>Here's a brief clip from Martin Fowler (Data Mappe... | 8 | 2009-08-14T19:08:40Z | [
"python",
"orm"
] |
What is an ORM and where can I learn more about it? | 1,279,613 | <p>Someone suggested I use an ORM for a project I'm designing but I'm having trouble finding information on what it is or how it works. Can anyone give me a brief explanation or a link as to where I can learn more about it?</p>
| 64 | 2009-08-14T19:03:59Z | 1,279,641 | <p>Like all acronyms it's ambiguous, but I assume they mean <a href="http://en.wikipedia.org/wiki/Object-relational%5Fmapping" rel="nofollow">object-relational mapper</a> -- a way to cover your eyes and make believe there's no SQL underneath, but rather it's all objects;-). Not really true, of course, and not without p... | 1 | 2009-08-14T19:09:41Z | [
"python",
"orm"
] |
What is an ORM and where can I learn more about it? | 1,279,613 | <p>Someone suggested I use an ORM for a project I'm designing but I'm having trouble finding information on what it is or how it works. Can anyone give me a brief explanation or a link as to where I can learn more about it?</p>
| 64 | 2009-08-14T19:03:59Z | 1,279,678 | <h2>Introduction</h2>
<p><a href="http://en.wikipedia.org/wiki/Object-relational_mapping">Object-Relational Mapping</a> (ORM) is a technique that lets you query and manipulate data from a database using an object-oriented paradigm. When talking about ORM, most people are referring to a <em>library</em> that implements... | 166 | 2009-08-14T19:17:15Z | [
"python",
"orm"
] |
What is an ORM and where can I learn more about it? | 1,279,613 | <p>Someone suggested I use an ORM for a project I'm designing but I'm having trouble finding information on what it is or how it works. Can anyone give me a brief explanation or a link as to where I can learn more about it?</p>
| 64 | 2009-08-14T19:03:59Z | 1,279,769 | <p>The first chapter of the Hibernate book <a href="http://rads.stackoverflow.com/amzn/click/1932394885" rel="nofollow">Java Persistence with Hibernate (3rd ed.)</a> has an excellent overview of general ORM concepts and discusses motivation and design of ORMs. Highly recommended, even if you don't work with Java.</p>
| 3 | 2009-08-14T19:35:48Z | [
"python",
"orm"
] |
What is an ORM and where can I learn more about it? | 1,279,613 | <p>Someone suggested I use an ORM for a project I'm designing but I'm having trouble finding information on what it is or how it works. Can anyone give me a brief explanation or a link as to where I can learn more about it?</p>
| 64 | 2009-08-14T19:03:59Z | 1,280,345 | <blockquote>
<p><em>Can anyone give me a brief explanation...</em></p>
</blockquote>
<p>Sure.</p>
<p>ORM stands for "Object to Relational Mapping" where </p>
<ul>
<li><p>The <strong>Object</strong> part is the one you use with your programming language ( python in this case ) </p></li>
<li><p>The <strong>Relation... | 17 | 2009-08-14T21:34:45Z | [
"python",
"orm"
] |
What is an ORM and where can I learn more about it? | 1,279,613 | <p>Someone suggested I use an ORM for a project I'm designing but I'm having trouble finding information on what it is or how it works. Can anyone give me a brief explanation or a link as to where I can learn more about it?</p>
| 64 | 2009-08-14T19:03:59Z | 1,687,149 | <p><a href="https://storm.canonical.com/" rel="nofollow">Storm</a> is a great option for those using python.</p>
| 2 | 2009-11-06T11:59:53Z | [
"python",
"orm"
] |
What is an ORM and where can I learn more about it? | 1,279,613 | <p>Someone suggested I use an ORM for a project I'm designing but I'm having trouble finding information on what it is or how it works. Can anyone give me a brief explanation or a link as to where I can learn more about it?</p>
| 64 | 2009-08-14T19:03:59Z | 4,028,003 | <p>DALMP <a href="http://code.google.com/p/dalmp/" rel="nofollow">http://code.google.com/p/dalmp/</a> could be a good one for php/mysql currently supporting many caches backends like redis/memcache/apc</p>
| 0 | 2010-10-26T21:02:32Z | [
"python",
"orm"
] |
OCR Playing Cards | 1,279,768 | <p>I decided to do a project for fun where I want to take as input the image of a playing card and return its rank and suit. I figure that I only need look at the upper-left corner, since that has all the information. It should be robust - if I have a large image of an Ace of Diamonds, I should be able to scale it anyw... | 12 | 2009-08-14T19:35:47Z | 1,280,040 | <p>Check out <a href="http://d-touch.org/" rel="nofollow">http://d-touch.org/</a>, you would have to design your own playing cards though.</p>
<p>You might also be interested in <a href="http://code.google.com/p/ocropus/" rel="nofollow">ocropus</a>.</p>
| 1 | 2009-08-14T20:31:55Z | [
"python",
"artificial-intelligence",
"ocr",
"computer-vision"
] |
OCR Playing Cards | 1,279,768 | <p>I decided to do a project for fun where I want to take as input the image of a playing card and return its rank and suit. I figure that I only need look at the upper-left corner, since that has all the information. It should be robust - if I have a large image of an Ace of Diamonds, I should be able to scale it anyw... | 12 | 2009-08-14T19:35:47Z | 1,280,314 | <p>I don't think there's something already written for what you are trying to accomplish (at least open source and in Python).</p>
<p>As for your second question, it depends on what you are trying to recognize. If the inputs can come from different sources -- e.g., different brands of playing cards with distinctive st... | 2 | 2009-08-14T21:28:24Z | [
"python",
"artificial-intelligence",
"ocr",
"computer-vision"
] |
OCR Playing Cards | 1,279,768 | <p>I decided to do a project for fun where I want to take as input the image of a playing card and return its rank and suit. I figure that I only need look at the upper-left corner, since that has all the information. It should be robust - if I have a large image of an Ace of Diamonds, I should be able to scale it anyw... | 12 | 2009-08-14T19:35:47Z | 1,280,833 | <p>It's not as robust, but you can look at the colours of 3 or 4 locations on the card so that if they are white or if they are a color, you can determine which card and suit it is. Obviously this won't work if you don't always have the same cards.</p>
| 1 | 2009-08-15T00:38:49Z | [
"python",
"artificial-intelligence",
"ocr",
"computer-vision"
] |
OCR Playing Cards | 1,279,768 | <p>I decided to do a project for fun where I want to take as input the image of a playing card and return its rank and suit. I figure that I only need look at the upper-left corner, since that has all the information. It should be robust - if I have a large image of an Ace of Diamonds, I should be able to scale it anyw... | 12 | 2009-08-14T19:35:47Z | 1,280,850 | <p>Personally I would go the machine learning route with this one.</p>
| 1 | 2009-08-15T00:48:00Z | [
"python",
"artificial-intelligence",
"ocr",
"computer-vision"
] |
OCR Playing Cards | 1,279,768 | <p>I decided to do a project for fun where I want to take as input the image of a playing card and return its rank and suit. I figure that I only need look at the upper-left corner, since that has all the information. It should be robust - if I have a large image of an Ace of Diamonds, I should be able to scale it anyw... | 12 | 2009-08-14T19:35:47Z | 1,280,852 | <p>There is a bunch of material online about this in the context of building online poker bots. See, for example: <a href="http://www.codingthewheel.com/archives/ocr-online-poker-optical-character-recognition" rel="nofollow">http://www.codingthewheel.com/archives/ocr-online-poker-optical-character-recognition</a></p>
| 4 | 2009-08-15T00:48:59Z | [
"python",
"artificial-intelligence",
"ocr",
"computer-vision"
] |
OCR Playing Cards | 1,279,768 | <p>I decided to do a project for fun where I want to take as input the image of a playing card and return its rank and suit. I figure that I only need look at the upper-left corner, since that has all the information. It should be robust - if I have a large image of an Ace of Diamonds, I should be able to scale it anyw... | 12 | 2009-08-14T19:35:47Z | 1,288,332 | <p>Given the limited sample size (4 suits, 13 different values) I'd just try to match a reference image of the suit and value with a new input image. First find the bounding box of the incoming suit / value (the smallest box enclosing all non-white pixels), scale your reference pictures to match the size of that boundi... | 1 | 2009-08-17T14:41:17Z | [
"python",
"artificial-intelligence",
"ocr",
"computer-vision"
] |
OCR Playing Cards | 1,279,768 | <p>I decided to do a project for fun where I want to take as input the image of a playing card and return its rank and suit. I figure that I only need look at the upper-left corner, since that has all the information. It should be robust - if I have a large image of an Ace of Diamonds, I should be able to scale it anyw... | 12 | 2009-08-14T19:35:47Z | 1,288,359 | <p>Use <a href="http://sourceforge.net/projects/opencvlibrary/" rel="nofollow">OpenCV</a></p>
| 3 | 2009-08-17T14:45:10Z | [
"python",
"artificial-intelligence",
"ocr",
"computer-vision"
] |
remove duplicates from nested dictionaries in list | 1,279,805 | <p>quick and very basic newbie question.</p>
<p>If i have list of dictionaries looking like this:</p>
<pre><code>L = []
L.append({"value1": value1, "value2": value2, "value3": value3, "value4": value4})
</code></pre>
<p>Let's say there exists multiple entries where value3 and value4 are identical to other nested dic... | 4 | 2009-08-14T19:41:26Z | 1,279,865 | <pre><code>for dic in list:
for anotherdic in list:
if dic != anotherdic:
if dic["value3"] == anotherdic["value3"] or dic["value4"] == anotherdic["value4"]:
list.remove(anotherdic)
</code></pre>
<p>Tested with</p>
<pre><code>list = [{"value1": 'fssd', "value2": 'dsfds', "value3": 'abcd', "value4"... | 1 | 2009-08-14T19:55:18Z | [
"python",
"dictionary"
] |
remove duplicates from nested dictionaries in list | 1,279,805 | <p>quick and very basic newbie question.</p>
<p>If i have list of dictionaries looking like this:</p>
<pre><code>L = []
L.append({"value1": value1, "value2": value2, "value3": value3, "value4": value4})
</code></pre>
<p>Let's say there exists multiple entries where value3 and value4 are identical to other nested dic... | 4 | 2009-08-14T19:41:26Z | 1,279,914 | <p>That's a list of one dictionary and but, assuming there are more dictionaries in the list <code>l</code>:</p>
<pre><code>l = [ldict for ldict in l if ldict.get("value3") != value3 or ldict.get("value4") != value4]
</code></pre>
<p>But is that what you really want to do? Perhaps you need to refine your description... | 1 | 2009-08-14T20:05:27Z | [
"python",
"dictionary"
] |
remove duplicates from nested dictionaries in list | 1,279,805 | <p>quick and very basic newbie question.</p>
<p>If i have list of dictionaries looking like this:</p>
<pre><code>L = []
L.append({"value1": value1, "value2": value2, "value3": value3, "value4": value4})
</code></pre>
<p>Let's say there exists multiple entries where value3 and value4 are identical to other nested dic... | 4 | 2009-08-14T19:41:26Z | 1,279,921 | <p>You can use a temporary array to store an items dict. The previous code was bugged for removing items in the for loop.</p>
<pre><code>(v,r) = ([],[])
for i in l:
if ('value4', i['value4']) not in v and ('value3', i['value3']) not in v:
r.append(i)
v.extend(i.items())
l = r
</code></pre>
<p>Your tes... | 2 | 2009-08-14T20:06:33Z | [
"python",
"dictionary"
] |
remove duplicates from nested dictionaries in list | 1,279,805 | <p>quick and very basic newbie question.</p>
<p>If i have list of dictionaries looking like this:</p>
<pre><code>L = []
L.append({"value1": value1, "value2": value2, "value3": value3, "value4": value4})
</code></pre>
<p>Let's say there exists multiple entries where value3 and value4 are identical to other nested dic... | 4 | 2009-08-14T19:41:26Z | 1,280,217 | <p>Here's one way:</p>
<pre><code>keyfunc = lambda d: (d['value3'], d['value4'])
from itertools import groupby
giter = groupby(sorted(L, key=keyfunc), keyfunc)
L2 = [g[1].next() for g in giter]
print L2
</code></pre>
| 7 | 2009-08-14T21:03:26Z | [
"python",
"dictionary"
] |
remove duplicates from nested dictionaries in list | 1,279,805 | <p>quick and very basic newbie question.</p>
<p>If i have list of dictionaries looking like this:</p>
<pre><code>L = []
L.append({"value1": value1, "value2": value2, "value3": value3, "value4": value4})
</code></pre>
<p>Let's say there exists multiple entries where value3 and value4 are identical to other nested dic... | 4 | 2009-08-14T19:41:26Z | 1,280,364 | <p>If I understand correctly, you want to discard matches that come later in the original list but do not care about the order of the resulting list, so:</p>
<p>(Tested with 2.5.2)</p>
<pre><code>tempDict = {}
for d in L[::-1]:
tempDict[(d["value3"],d["value4"])] = d
L[:] = tempDict.itervalues()
tempDict = None
<... | 0 | 2009-08-14T21:40:29Z | [
"python",
"dictionary"
] |
remove duplicates from nested dictionaries in list | 1,279,805 | <p>quick and very basic newbie question.</p>
<p>If i have list of dictionaries looking like this:</p>
<pre><code>L = []
L.append({"value1": value1, "value2": value2, "value3": value3, "value4": value4})
</code></pre>
<p>Let's say there exists multiple entries where value3 and value4 are identical to other nested dic... | 4 | 2009-08-14T19:41:26Z | 1,280,464 | <p>In Python 2.6 or 3.*:</p>
<pre><code>import itertools
import pprint
L = [{"value1": "fssd", "value2": "dsfds", "value3": "abcd", "value4": "gk"},
{"value1": "asdasd", "value2": "asdas", "value3": "dafdd", "value4": "sdfsdf"},
{"value1": "sdfsf", "value2": "sdfsdf", "value3": "abcd", "value4": "gk"},
{"... | 6 | 2009-08-14T22:15:18Z | [
"python",
"dictionary"
] |
exec() bytecode with arbitrary locals? | 1,280,100 | <p>Suppose I want to execute code, for example</p>
<pre><code> value += 5
</code></pre>
<p>inside a namespace of my own (so the result is essentially <code>mydict['value'] += 5</code>). There's a function <code>exec()</code>, but I have to pass a string there:</p>
<pre><code> exec('value += 5', mydict)
</code>... | 2 | 2009-08-14T20:41:10Z | 1,280,493 | <p>You can pass bytecode instead of a string to <code>exec</code>, you just need to make the right bytecode for the purpose:</p>
<pre><code>>>> bytecode = compile('value += 5', '<string>', 'exec')
>>> mydict = {'value': 23}
>>> exec(bytecode, mydict)
>>> mydict['value']
28
</c... | 6 | 2009-08-14T22:25:28Z | [
"python",
"python-3.x",
"bytecode",
"codeblocks"
] |
exec() bytecode with arbitrary locals? | 1,280,100 | <p>Suppose I want to execute code, for example</p>
<pre><code> value += 5
</code></pre>
<p>inside a namespace of my own (so the result is essentially <code>mydict['value'] += 5</code>). There's a function <code>exec()</code>, but I have to pass a string there:</p>
<pre><code> exec('value += 5', mydict)
</code>... | 2 | 2009-08-14T20:41:10Z | 1,280,876 | <p>Use the <code>global</code> keyword to force dynamic scoping on any variables you want to modify from within the block:</p>
<pre><code>def block():
global value
value += 5
mydict = {"value": 42}
exec(block.__code__, mydict)
print(mydict["value"])
</code></pre>
| 3 | 2009-08-15T00:59:58Z | [
"python",
"python-3.x",
"bytecode",
"codeblocks"
] |
exec() bytecode with arbitrary locals? | 1,280,100 | <p>Suppose I want to execute code, for example</p>
<pre><code> value += 5
</code></pre>
<p>inside a namespace of my own (so the result is essentially <code>mydict['value'] += 5</code>). There's a function <code>exec()</code>, but I have to pass a string there:</p>
<pre><code> exec('value += 5', mydict)
</code>... | 2 | 2009-08-14T20:41:10Z | 1,314,536 | <p>From S.Lott's comment above I think I get the idea for an answer using creation of new class. </p>
<pre><code>class _(__metaclass__ = change(mydict)):
value += 1
...
</code></pre>
<p>where <code>change</code> is a metaclass whose <code>__prepare__</code> reads dictionary and whose <code>__new__</code> upda... | 0 | 2009-08-21T22:54:06Z | [
"python",
"python-3.x",
"bytecode",
"codeblocks"
] |
exec() bytecode with arbitrary locals? | 1,280,100 | <p>Suppose I want to execute code, for example</p>
<pre><code> value += 5
</code></pre>
<p>inside a namespace of my own (so the result is essentially <code>mydict['value'] += 5</code>). There's a function <code>exec()</code>, but I have to pass a string there:</p>
<pre><code> exec('value += 5', mydict)
</code>... | 2 | 2009-08-14T20:41:10Z | 1,844,933 | <p>Here is a crazy decorator to create such a block that uses "custom locals". In reality it is a quick hack to turn all variable access inside the function to global access, and evaluate the result with the custom locals dictionary as environment.</p>
<pre><code>import dis
import functools
import types
import string
... | 3 | 2009-12-04T04:52:37Z | [
"python",
"python-3.x",
"bytecode",
"codeblocks"
] |
How to classify users into different countries, based on the Location field | 1,280,266 | <p>Most web applications have a Location field, in which uses may enter a Location of their choice.</p>
<p>How would you classify users into different countries, based on the location entered.</p>
<p>For eg, I used the Stack Overflow dump of <code>users.xml</code> and extracted users' names, reputation and location:<... | 1 | 2009-08-14T21:15:13Z | 1,280,300 | <p>Force users to specify country, because you'll have to deal with ambiguities. This would be the right way.</p>
<p>If that's not possible, at least make your best-guess in conjunction with their IP address.</p>
<p>For example, ['Grant', '3333', 'Georgia']</p>
<p>Is this Georgia, USA?
Or is this the Republic of Ge... | 1 | 2009-08-14T21:24:03Z | [
"python",
"xml",
"geolocation",
"elementtree"
] |
How to classify users into different countries, based on the Location field | 1,280,266 | <p>Most web applications have a Location field, in which uses may enter a Location of their choice.</p>
<p>How would you classify users into different countries, based on the location entered.</p>
<p>For eg, I used the Stack Overflow dump of <code>users.xml</code> and extracted users' names, reputation and location:<... | 1 | 2009-08-14T21:15:13Z | 1,280,334 | <p>You best bet is to use a Geocoding API like <a href="http://code.google.com/p/geopy/" rel="nofollow">geopy</a> (<a href="http://code.google.com/p/geopy/wiki/GettingStarted" rel="nofollow">some Examples</a>).</p>
<p>The <a href="http://code.google.com/apis/maps/documentation/geocoding/#GeocodingResponses" rel="nofol... | 2 | 2009-08-14T21:33:06Z | [
"python",
"xml",
"geolocation",
"elementtree"
] |
Ruby to python one-liner conversion | 1,280,379 | <p>I have a little one-liner in my Rails app that returns a range of copyright dates with an optional parameter, e.g.:</p>
<pre><code>def copyright_dates(start_year = Date.today().year)
[start_year, Date.today().year].sort.uniq.join(" - ")
end
</code></pre>
<p>I'm moving the app over to Django, and while I love i... | 0 | 2009-08-14T21:46:01Z | 1,280,415 | <pre><code>from datetime import datetime
def copyright_dates(start_year = datetime.now().year):
return " - ".join(str(y) for y in sorted(set([start_year, datetime.now().year])))
</code></pre>
| 5 | 2009-08-14T21:54:45Z | [
"python",
"ruby-on-rails",
"ruby",
"django"
] |
Ruby to python one-liner conversion | 1,280,379 | <p>I have a little one-liner in my Rails app that returns a range of copyright dates with an optional parameter, e.g.:</p>
<pre><code>def copyright_dates(start_year = Date.today().year)
[start_year, Date.today().year].sort.uniq.join(" - ")
end
</code></pre>
<p>I'm moving the app over to Django, and while I love i... | 0 | 2009-08-14T21:46:01Z | 1,280,439 | <p>Lists and sets seem to be overkill to me.</p>
<p>How about this:</p>
<pre><code>def copyright_dates(start=datetime.datetime.today().year):
now = datetime.datetime.today().year
return (start==now and str(now) or "%d - %d" % (min(start, now), max(start, now)))
</code></pre>
| 2 | 2009-08-14T22:03:20Z | [
"python",
"ruby-on-rails",
"ruby",
"django"
] |
Ruby to python one-liner conversion | 1,280,379 | <p>I have a little one-liner in my Rails app that returns a range of copyright dates with an optional parameter, e.g.:</p>
<pre><code>def copyright_dates(start_year = Date.today().year)
[start_year, Date.today().year].sort.uniq.join(" - ")
end
</code></pre>
<p>I'm moving the app over to Django, and while I love i... | 0 | 2009-08-14T21:46:01Z | 1,280,601 | <p>Watch out for the default parameter which is evaluated once. So if your web application runs over 12/31/09 without a restart, you won't get the expected output.</p>
<p>Try:</p>
<pre><code>def copy(start=None):
start, curr = start if start else datetime.today().year, datetime.today().year
return str(start)... | 5 | 2009-08-14T23:02:32Z | [
"python",
"ruby-on-rails",
"ruby",
"django"
] |
wxPython: Items in BoxSizer don't expand horizontally, only vertically | 1,280,600 | <p>I have several buttons in various sizers and they expand in the way that I want them to. However, when I add the parent to a new wx.BoxSizer that is used to add a border around all the elements in the frame, the sizer that has been added functions correctly vertically, but not horizontally.</p>
<p>The following cod... | 9 | 2009-08-14T23:02:25Z | 1,281,041 | <p>First of all, you're passing some flags incorrectly. BoxSizer takes wx.HORIZONTAL or wx.VERTICAL, not wx.EXPAND. sizer.Add does not take wx.HORIZONTAL.</p>
<p>If you have a VERTICAL BoxSizer, wx.EXPAND will make the control fill horizontally, while a proportion of 1 or more (second argument to Add) will make the ... | 23 | 2009-08-15T03:12:03Z | [
"python",
"wxpython",
"wxwidgets"
] |
detecting two simultaneous keys in pyglet (python) | 1,280,616 | <p>I wanted to know how to detect when two keys are simultaneously pressed using pyglet.
I currently have </p>
<pre>
def on_text_motion(self, motion):
(dx,dy) = ARROW_KEY_TO_VERSOR[motion]
self.window.move_dx_dy((dx,dy))
</pre>
<p>But this only gets arrow keys one at a time...
I'd like to distinguish betwee... | 3 | 2009-08-14T23:10:48Z | 1,280,698 | <p>Try <a href="http://www.pyglet.org/doc/api/pyglet.window.key.KeyStateHandler-class.html" rel="nofollow">pyglet.window.key.KeyStateHandler</a>:</p>
<pre><code>import pyglet
key = pyglet.window.key
win = pyglet.window.Window()
keyboard = key.KeyStateHandler()
win.push_handlers(keyboard)
print keyboard[key.UP] and ... | 4 | 2009-08-14T23:42:41Z | [
"python",
"keyboard",
"pyglet"
] |
In python is there an easier way to write 6 nested for loops? | 1,280,667 | <p>This problem has been getting at me for a while now. Is there an easier way to write nested <code>for</code> loops in python? For example if my code went something like this:</p>
<pre><code> for y in range(3):
for x in range(3):
do_something()
for y1 in range(3):
for x1 in range(3):
... | 30 | 2009-08-14T23:31:40Z | 1,280,677 | <p>When faced with that sort of program logic, I would probably break up the sequence of loops into two or more separate functions.</p>
<p>Another technique in Python is to use <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions">list comprehensions</a> where possible, instead of a loop.</... | 7 | 2009-08-14T23:36:03Z | [
"python",
"for-loop",
"nested-loops"
] |
In python is there an easier way to write 6 nested for loops? | 1,280,667 | <p>This problem has been getting at me for a while now. Is there an easier way to write nested <code>for</code> loops in python? For example if my code went something like this:</p>
<pre><code> for y in range(3):
for x in range(3):
do_something()
for y1 in range(3):
for x1 in range(3):
... | 30 | 2009-08-14T23:31:40Z | 1,280,678 | <p>My personal argument would be that you're likely doing something wrong if you have 6 nested loops...</p>
<p>That said, functional decomposition is what you're looking for. Refactor so some of the loops happen in seperate function calls, then call those functions.</p>
| 2 | 2009-08-14T23:36:32Z | [
"python",
"for-loop",
"nested-loops"
] |
In python is there an easier way to write 6 nested for loops? | 1,280,667 | <p>This problem has been getting at me for a while now. Is there an easier way to write nested <code>for</code> loops in python? For example if my code went something like this:</p>
<pre><code> for y in range(3):
for x in range(3):
do_something()
for y1 in range(3):
for x1 in range(3):
... | 30 | 2009-08-14T23:31:40Z | 1,280,679 | <p>Assuming each loop has some sort of independent meaning, break them out into named functions:</p>
<pre><code>def do_tigers():
for x in range(3):
print something
def do_lions():
do_lionesses()
for x in range(3):
do_tigers()
def do_penguins():
for x in range(3):
do_lions()
.... | 4 | 2009-08-14T23:36:59Z | [
"python",
"for-loop",
"nested-loops"
] |
In python is there an easier way to write 6 nested for loops? | 1,280,667 | <p>This problem has been getting at me for a while now. Is there an easier way to write nested <code>for</code> loops in python? For example if my code went something like this:</p>
<pre><code> for y in range(3):
for x in range(3):
do_something()
for y1 in range(3):
for x1 in range(3):
... | 30 | 2009-08-14T23:31:40Z | 1,280,680 | <p>Have you looked into <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions" rel="nofollow">List Comprehensions?</a></p>
<p>Something like:</p>
<pre><code>[do_something() for x in range(3) for y in range(3)]
</code></pre>
| 0 | 2009-08-14T23:37:05Z | [
"python",
"for-loop",
"nested-loops"
] |
In python is there an easier way to write 6 nested for loops? | 1,280,667 | <p>This problem has been getting at me for a while now. Is there an easier way to write nested <code>for</code> loops in python? For example if my code went something like this:</p>
<pre><code> for y in range(3):
for x in range(3):
do_something()
for y1 in range(3):
for x1 in range(3):
... | 30 | 2009-08-14T23:31:40Z | 1,280,685 | <p>That way looks pretty straightforward and easy. Are you are saying you want to generalize to multiple layers of loops.... can you give a real-life example?</p>
<p>Another option I could think of would be to use a function to generate the parameters and then just apply them in a loop</p>
<pre><code>def generate_pa... | 0 | 2009-08-14T23:37:45Z | [
"python",
"for-loop",
"nested-loops"
] |
In python is there an easier way to write 6 nested for loops? | 1,280,667 | <p>This problem has been getting at me for a while now. Is there an easier way to write nested <code>for</code> loops in python? For example if my code went something like this:</p>
<pre><code> for y in range(3):
for x in range(3):
do_something()
for y1 in range(3):
for x1 in range(3):
... | 30 | 2009-08-14T23:31:40Z | 1,280,712 | <p>you can also use the <a href="http://docs.python.org/tutorial/datastructures.html" rel="nofollow">map()</a> <a href="http://en.wikipedia.org/wiki/Map%5F%28higher-order%5Ffunction%29" rel="nofollow">function</a> </p>
| 0 | 2009-08-14T23:47:07Z | [
"python",
"for-loop",
"nested-loops"
] |
In python is there an easier way to write 6 nested for loops? | 1,280,667 | <p>This problem has been getting at me for a while now. Is there an easier way to write nested <code>for</code> loops in python? For example if my code went something like this:</p>
<pre><code> for y in range(3):
for x in range(3):
do_something()
for y1 in range(3):
for x1 in range(3):
... | 30 | 2009-08-14T23:31:40Z | 1,280,724 | <p>If you're frequently iterating over a Cartesian product like in your example, you might want to investigate <a href="http://docs.python.org/library/itertools.html#itertools.product">Python 2.6's itertools.product</a> -- or write your own if you're in an earlier Python.</p>
<pre><code>from itertools import product
f... | 49 | 2009-08-14T23:51:35Z | [
"python",
"for-loop",
"nested-loops"
] |
In python is there an easier way to write 6 nested for loops? | 1,280,667 | <p>This problem has been getting at me for a while now. Is there an easier way to write nested <code>for</code> loops in python? For example if my code went something like this:</p>
<pre><code> for y in range(3):
for x in range(3):
do_something()
for y1 in range(3):
for x1 in range(3):
... | 30 | 2009-08-14T23:31:40Z | 1,280,726 | <p>Technically, you could use <code>itertools.product</code> to get a cartesian product of N sequences, and iterate over that:</p>
<pre><code> for y, x, y1, x1 in itertools.product(range(3), repeat=4):
do_something_else()
</code></pre>
<p>But I don't think that actually wins you anything readability-wise.</p>
| 2 | 2009-08-14T23:51:54Z | [
"python",
"for-loop",
"nested-loops"
] |
In python is there an easier way to write 6 nested for loops? | 1,280,667 | <p>This problem has been getting at me for a while now. Is there an easier way to write nested <code>for</code> loops in python? For example if my code went something like this:</p>
<pre><code> for y in range(3):
for x in range(3):
do_something()
for y1 in range(3):
for x1 in range(3):
... | 30 | 2009-08-14T23:31:40Z | 1,280,730 | <p>This is fairly common when looping over multidimensional spaces. My solution is:</p>
<pre><code>xy_grid = [(x, y) for x in range(3) for y in range(3)]
for x, y in xy_grid:
# do something
for x1, y1 in xy_grid:
# do something else
</code></pre>
| 10 | 2009-08-14T23:53:34Z | [
"python",
"for-loop",
"nested-loops"
] |
In python is there an easier way to write 6 nested for loops? | 1,280,667 | <p>This problem has been getting at me for a while now. Is there an easier way to write nested <code>for</code> loops in python? For example if my code went something like this:</p>
<pre><code> for y in range(3):
for x in range(3):
do_something()
for y1 in range(3):
for x1 in range(3):
... | 30 | 2009-08-14T23:31:40Z | 1,280,745 | <p>From your code it looks like you want to perform an operation with every possible pair of points where x and y are in the range 0..2.</p>
<p>To do that:</p>
<pre><code>for x1,y1,x2,y2 in itertools.product(range(3), repeat=4):
do_something_with_two_points(x1,y1,2,y2)
</code></pre>
<p>The operation <code>do_som... | 1 | 2009-08-14T23:57:37Z | [
"python",
"for-loop",
"nested-loops"
] |
In python is there an easier way to write 6 nested for loops? | 1,280,667 | <p>This problem has been getting at me for a while now. Is there an easier way to write nested <code>for</code> loops in python? For example if my code went something like this:</p>
<pre><code> for y in range(3):
for x in range(3):
do_something()
for y1 in range(3):
for x1 in range(3):
... | 30 | 2009-08-14T23:31:40Z | 1,280,875 | <p>Python iterators, and generators in particular, exist exactly to allow the nice refactoring of otherwise-complicated loops. Of course, it's hard to get an abstraction out from a simple example, but assuming the <code>3</code> needs to be a parameter (maybe the whole <code>range(3)</code> should be?), and the two fu... | 2 | 2009-08-15T00:59:33Z | [
"python",
"for-loop",
"nested-loops"
] |
How can I list the methods in a Python 2.5 module? | 1,280,787 | <p>I'm trying to use a Python library written in C that has no documentation of any kind. I want to use introspection to at least see what methods and classes are in the modules. Does somebody have a function or library I can use to list the functions (with argument lists) and classes (with methods and member variables... | 19 | 2009-08-15T00:13:02Z | 1,280,796 | <p>The dir() functions shows all members a module has.</p>
| 2 | 2009-08-15T00:17:30Z | [
"python",
"introspection",
"python-2.5"
] |
How can I list the methods in a Python 2.5 module? | 1,280,787 | <p>I'm trying to use a Python library written in C that has no documentation of any kind. I want to use introspection to at least see what methods and classes are in the modules. Does somebody have a function or library I can use to list the functions (with argument lists) and classes (with methods and member variables... | 19 | 2009-08-15T00:13:02Z | 1,280,800 | <p>Here are some things you can do at least:</p>
<pre><code>import module
print dir(module) # Find functions of interest.
# For each function of interest:
help(module.interesting_function)
print module.interesting_function.func_defaults
</code></pre>
| 43 | 2009-08-15T00:17:51Z | [
"python",
"introspection",
"python-2.5"
] |
How can I list the methods in a Python 2.5 module? | 1,280,787 | <p>I'm trying to use a Python library written in C that has no documentation of any kind. I want to use introspection to at least see what methods and classes are in the modules. Does somebody have a function or library I can use to list the functions (with argument lists) and classes (with methods and member variables... | 19 | 2009-08-15T00:13:02Z | 1,280,817 | <p>Just this is pretty good too:</p>
<pre><code>import module
help(module)
</code></pre>
<p>It will print the docstring for the module, then list the contents of the module, printing their docstrings too.</p>
| 3 | 2009-08-15T00:25:24Z | [
"python",
"introspection",
"python-2.5"
] |
How can I list the methods in a Python 2.5 module? | 1,280,787 | <p>I'm trying to use a Python library written in C that has no documentation of any kind. I want to use introspection to at least see what methods and classes are in the modules. Does somebody have a function or library I can use to list the functions (with argument lists) and classes (with methods and member variables... | 19 | 2009-08-15T00:13:02Z | 1,280,846 | <p>Mark Pilgrim's <a href="http://diveintopython.net/power_of_introspection/index.html" rel="nofollow">chapter 4</a>, which you mention, does actually apply just fine to Python 2.5 (and any other recent <code>2.*</code> version, thanks to backwards compatibility). Mark doesn't mention <code>help</code>, but I see other... | 12 | 2009-08-15T00:44:24Z | [
"python",
"introspection",
"python-2.5"
] |
Scraping Ajax - Using python | 1,281,075 | <p>I'm trying to scrap a page in youtube with python which has lot of ajax in it</p>
<p>I've to call the java script each time to get the info. But i'm not really sure how to go about it. I'm using the urllib2 module to open URLs. Any help would be appreciated.</p>
| 2 | 2009-08-15T03:34:05Z | 1,281,111 | <p>Youtube (and everything else Google makes) have EXTENSIVE APIs already in place for giving you access to just about any and all data you could possibly want.</p>
<p>Take a look at <a href="http://code.google.com/apis/youtube/overview.html">The Youtube Data API</a> for more information.</p>
<p>I use urllib to make ... | 6 | 2009-08-15T03:59:05Z | [
"python",
"ajax",
"screen-scraping"
] |
Scraping Ajax - Using python | 1,281,075 | <p>I'm trying to scrap a page in youtube with python which has lot of ajax in it</p>
<p>I've to call the java script each time to get the info. But i'm not really sure how to go about it. I'm using the urllib2 module to open URLs. Any help would be appreciated.</p>
| 2 | 2009-08-15T03:34:05Z | 1,281,115 | <p>Main problem is, you're violating the TOS (terms of service) for the youtube site. Youtube engineers and lawyers will do their professional best to track you down and make an example of you if you persist. If you're happy with that prospect, then, on you head be it -- technically, your best bet are <a href="http://p... | 6 | 2009-08-15T04:01:10Z | [
"python",
"ajax",
"screen-scraping"
] |
Scraping Ajax - Using python | 1,281,075 | <p>I'm trying to scrap a page in youtube with python which has lot of ajax in it</p>
<p>I've to call the java script each time to get the info. But i'm not really sure how to go about it. I'm using the urllib2 module to open URLs. Any help would be appreciated.</p>
| 2 | 2009-08-15T03:34:05Z | 1,281,746 | <p>As suggested, you should use the YouTube API to access the data made available legitimately.</p>
<p>Regarding the general question of scraping AJAX, you might want to consider the <a href="http://scrapy.org" rel="nofollow">scrapy framework</a>. It provides extensive support for crawling and scraping web sites and ... | 0 | 2009-08-15T11:57:00Z | [
"python",
"ajax",
"screen-scraping"
] |
Scraping Ajax - Using python | 1,281,075 | <p>I'm trying to scrap a page in youtube with python which has lot of ajax in it</p>
<p>I've to call the java script each time to get the info. But i'm not really sure how to go about it. I'm using the urllib2 module to open URLs. Any help would be appreciated.</p>
| 2 | 2009-08-15T03:34:05Z | 3,134,226 | <p>Here is how I would do it: Install Firebug on Firefox, then turn the NET on in firebug and click on the desired link on YouTube. Now see what happens and what pages are requested. Find the one that are responsible for the AJAX part of page. Now you can use urllib or Mechanize to fetch the link. If you CAN pull the s... | 2 | 2010-06-28T16:28:13Z | [
"python",
"ajax",
"screen-scraping"
] |
Scraping Ajax - Using python | 1,281,075 | <p>I'm trying to scrap a page in youtube with python which has lot of ajax in it</p>
<p>I've to call the java script each time to get the info. But i'm not really sure how to go about it. I'm using the urllib2 module to open URLs. Any help would be appreciated.</p>
| 2 | 2009-08-15T03:34:05Z | 3,162,794 | <p>You could sniff the network traffic with something like <a href="http://www.wireshark.org/" rel="nofollow">Wireshark</a> then replay the HTTP calls via a scraping framework that is robust enough to deal with AJAX, such as <a href="http://scrapy.org/" rel="nofollow">scraPY</a>.</p>
| 0 | 2010-07-02T01:45:05Z | [
"python",
"ajax",
"screen-scraping"
] |
What's the Ruby equivalent of Python's os.walk? | 1,281,090 | <p>Does anyone know if there's an existing module/function inside Ruby to traverse file system directories and files? I'm looking for something similar to Python's <code>os.walk</code>. The closest module I've found is <code>Find</code> but requires some extra work to do the traversal.</p>
<p>The Python code looks li... | 20 | 2009-08-15T03:40:16Z | 1,281,103 | <p>The following will print all files recursively. Then you can use File.directory? to see if the it is a directory or a file.</p>
<pre><code>Dir['**/*'].each { |f| print f }
</code></pre>
| 20 | 2009-08-15T03:51:30Z | [
"python",
"ruby"
] |
What's the Ruby equivalent of Python's os.walk? | 1,281,090 | <p>Does anyone know if there's an existing module/function inside Ruby to traverse file system directories and files? I'm looking for something similar to Python's <code>os.walk</code>. The closest module I've found is <code>Find</code> but requires some extra work to do the traversal.</p>
<p>The Python code looks li... | 20 | 2009-08-15T03:40:16Z | 1,281,222 | <p><a href="http://www.ruby-doc.org/stdlib-2.0/libdoc/find/rdoc/Find.html" rel="nofollow">Find</a> seems pretty simple to me:</p>
<pre><code>require "find"
Find.find('mydir'){|f| puts f}
</code></pre>
| 8 | 2009-08-15T05:17:15Z | [
"python",
"ruby"
] |
What's the Ruby equivalent of Python's os.walk? | 1,281,090 | <p>Does anyone know if there's an existing module/function inside Ruby to traverse file system directories and files? I'm looking for something similar to Python's <code>os.walk</code>. The closest module I've found is <code>Find</code> but requires some extra work to do the traversal.</p>
<p>The Python code looks li... | 20 | 2009-08-15T03:40:16Z | 1,282,690 | <pre><code>require 'pathname'
def os_walk(dir)
root = Pathname(dir)
files, dirs = [], []
Pathname(root).find do |path|
unless path == root
dirs << path if path.directory?
files << path if path.file?
end
end
[root, files, dirs]
end
root, files, dirs = os_walk('.')
</code></pre... | 3 | 2009-08-15T19:50:27Z | [
"python",
"ruby"
] |
Why can't I set a global variable in Python? | 1,281,184 | <p>How do global variables work in Python? I know global variables are evil, I'm just experimenting.</p>
<p>This does not work in python:</p>
<pre><code>G = None
def foo():
if G is None:
G = 1
foo()
</code></pre>
<p>I get an error:</p>
<pre><code>UnboundLocalError: local variable 'G' referenced befor... | 17 | 2009-08-15T04:49:48Z | 1,281,186 | <p>You need the <a href="http://www.python.org/doc/ref/global.html"><code>global</code></a> statement:</p>
<pre><code>def foo():
global G
if G is None:
G = 1
</code></pre>
<p>In Python, variables <em>that you assign to</em> become local variables by default. You need to use <code>global</code> to decl... | 43 | 2009-08-15T04:50:57Z | [
"python",
"global-variables"
] |
Why can't I set a global variable in Python? | 1,281,184 | <p>How do global variables work in Python? I know global variables are evil, I'm just experimenting.</p>
<p>This does not work in python:</p>
<pre><code>G = None
def foo():
if G is None:
G = 1
foo()
</code></pre>
<p>I get an error:</p>
<pre><code>UnboundLocalError: local variable 'G' referenced befor... | 17 | 2009-08-15T04:49:48Z | 1,281,190 | <p>You still have to declare G as global, from within that function:</p>
<pre><code>G = None
def foo():
global G
if G is None:
G = 1
foo()
print G
</code></pre>
<p>which simply outputs</p>
<pre><code>1
</code></pre>
| 4 | 2009-08-15T04:51:33Z | [
"python",
"global-variables"
] |
Why can't I set a global variable in Python? | 1,281,184 | <p>How do global variables work in Python? I know global variables are evil, I'm just experimenting.</p>
<p>This does not work in python:</p>
<pre><code>G = None
def foo():
if G is None:
G = 1
foo()
</code></pre>
<p>I get an error:</p>
<pre><code>UnboundLocalError: local variable 'G' referenced befor... | 17 | 2009-08-15T04:49:48Z | 1,281,193 | <p>Define G as global in the function like this:</p>
<pre><code>#!/usr/bin/python
G = None;
def foo():
global G
if G is None:
G = 1;
print G;
foo();
</code></pre>
<p>The above python prints <code>1</code>. </p>
<p>Using global variables like this is bad practice because: <a href="http://c2.com... | 1 | 2009-08-15T04:54:34Z | [
"python",
"global-variables"
] |
Why can't I set a global variable in Python? | 1,281,184 | <p>How do global variables work in Python? I know global variables are evil, I'm just experimenting.</p>
<p>This does not work in python:</p>
<pre><code>G = None
def foo():
if G is None:
G = 1
foo()
</code></pre>
<p>I get an error:</p>
<pre><code>UnboundLocalError: local variable 'G' referenced befor... | 17 | 2009-08-15T04:49:48Z | 1,281,207 | <p>You need to declare <code>G</code> as <code>global</code>, but as for why: whenever you refer to a variable inside a function, if you <em>set</em> the variable anywhere in that function, Python assumes that it's a local variable. So if a local variable by that name doesn't exist at that point in the code, you'll get... | 6 | 2009-08-15T05:04:52Z | [
"python",
"global-variables"
] |
DOCTEST==argv[0] as a convention? | 1,281,385 | <p>In a bit of Python I'm writing (a command line and filter testing tool: <i><a href="http://bitbucket.org/jimd/claft/" rel="nofollow">claft</a></i>) I wanted a simple way to invoke the built-in test suite (<i>doctest</i>) and I decided on the following:</p>
<pre><code>if 'DOCTEST' in os.environ and os.environ['DOCTE... | 0 | 2009-08-15T07:25:54Z | 1,281,446 | <p>Since you're already doing command-line parsing, why not just add a <code>--selftest</code> option? You won't have to worry about any conflicts that way, and invocation will be easier.</p>
| 3 | 2009-08-15T08:10:32Z | [
"python",
"unit-testing",
"testing",
"doctest"
] |
DOCTEST==argv[0] as a convention? | 1,281,385 | <p>In a bit of Python I'm writing (a command line and filter testing tool: <i><a href="http://bitbucket.org/jimd/claft/" rel="nofollow">claft</a></i>) I wanted a simple way to invoke the built-in test suite (<i>doctest</i>) and I decided on the following:</p>
<pre><code>if 'DOCTEST' in os.environ and os.environ['DOCTE... | 0 | 2009-08-15T07:25:54Z | 1,285,309 | <p>Another <em>hackish</em> way to avoid namespace conflicts with the environment: looks for myprogname_DEBUG or the like. </p>
| 0 | 2009-08-16T20:46:09Z | [
"python",
"unit-testing",
"testing",
"doctest"
] |
How to get the desktop resolution in Mac via Python? | 1,281,397 | <p>Trying to write a python application that downloads images from an RSS feed, and makes a composite background. How do I get the current desktop resolution on Mac OS X (leopard?)</p>
| 5 | 2009-08-15T07:35:13Z | 1,281,568 | <p>As usual, using features that are binded to an OS is a very bad idea. There are hundred of portable libs in Python that give you access to that information. The first that comes to my mind is of course pygame :</p>
<pre><code>import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode... | 1 | 2009-08-15T10:01:06Z | [
"python",
"osx"
] |
How to get the desktop resolution in Mac via Python? | 1,281,397 | <p>Trying to write a python application that downloads images from an RSS feed, and makes a composite background. How do I get the current desktop resolution on Mac OS X (leopard?)</p>
| 5 | 2009-08-15T07:35:13Z | 1,281,659 | <p>With Pyobjc something like this should work. Pyobjc comes with Leopard.</p>
<pre><code>from AppKit import NSScreen
print NSScreen.mainScreen().frame()
</code></pre>
<p>With that, you can also grab the width and height.</p>
<pre><code>NSScreen.mainScreen().frame().width
NSScreen.mainScreen().frame().height
</code... | 10 | 2009-08-15T11:07:52Z | [
"python",
"osx"
] |
How to get the desktop resolution in Mac via Python? | 1,281,397 | <p>Trying to write a python application that downloads images from an RSS feed, and makes a composite background. How do I get the current desktop resolution on Mac OS X (leopard?)</p>
| 5 | 2009-08-15T07:35:13Z | 8,203,217 | <p>If you are doing this from a LaunchAgent script, you may need to drop down to CoreGraphics primitives rather than AppKit-level methods. Working on this today, my LaunchAgent-loaded script gets <code>None</code> back from <code>NSScreen.mainScreen()</code>, but works fine if I load it from a terminal in my session.</... | 3 | 2011-11-20T17:19:54Z | [
"python",
"osx"
] |
Accessing elements with offsets in Python's for .. in loops | 1,281,752 | <p>I've been mucking around a bit with Python, and I've gathered that it's usually better (or 'pythonic') to use</p>
<pre><code>for x in SomeArray:
</code></pre>
<p>rather than the more C-style</p>
<pre><code>for i in range(0, len(SomeArray)):
</code></pre>
<p>I do see the benefits in this, mainly cleaner code, and... | 7 | 2009-08-15T12:00:36Z | 1,281,758 | <p>The way to do this in Python is:</p>
<pre><code>for i, x in enumerate(SomeArray):
print i, x
</code></pre>
<p>The <a href="http://docs.python.org/library/functions.html#enumerate"><code>enumerate</code></a> generator produces a sequence of 2-tuples, each containing the array index and the element.</p>
| 13 | 2009-08-15T12:03:33Z | [
"python",
"loops"
] |
Accessing elements with offsets in Python's for .. in loops | 1,281,752 | <p>I've been mucking around a bit with Python, and I've gathered that it's usually better (or 'pythonic') to use</p>
<pre><code>for x in SomeArray:
</code></pre>
<p>rather than the more C-style</p>
<pre><code>for i in range(0, len(SomeArray)):
</code></pre>
<p>I do see the benefits in this, mainly cleaner code, and... | 7 | 2009-08-15T12:00:36Z | 1,282,034 | <p>List indexing and zip() are your friends.</p>
<p>Here's my answer for your more specific question:</p>
<blockquote>
<p>I might want to add the current element to the element two steps behind it. Is there a way to do this without resorting to explicit indices?</p>
</blockquote>
<pre><code>arr = range(10)
[i+j fo... | 6 | 2009-08-15T14:43:54Z | [
"python",
"loops"
] |
Making sure that psycopg2 database connection alive | 1,281,875 | <p>I have a python application that opens a database connection that can hang online for an hours, but sometimes the database server reboots and while python still have the connection it won't work with <code>OperationalError</code> exception.</p>
<p>So I'm looking for any reliable method to "ping" the database and kn... | 7 | 2009-08-15T13:17:09Z | 1,282,019 | <p><code>pg_connection_status</code> is implemented using PQstatus. psycopg doesn't expose that API, so the check is not available. The only two places psycopg calls PQstatus itself is when a new connection is made, and at the beginning of execute. So yes, you will need to issue a simple SQL statement to find out wheth... | 8 | 2009-08-15T14:36:08Z | [
"python",
"database",
"connection",
"psycopg2"
] |
Making sure that psycopg2 database connection alive | 1,281,875 | <p>I have a python application that opens a database connection that can hang online for an hours, but sometimes the database server reboots and while python still have the connection it won't work with <code>OperationalError</code> exception.</p>
<p>So I'm looking for any reliable method to "ping" the database and kn... | 7 | 2009-08-15T13:17:09Z | 18,708,605 | <p>This question is really old, but still pops up on Google searches so I think it's valuable to know that the <code>psycopg2.connection</code> instance now has a <a href="http://initd.org/psycopg/docs/connection.html#connection.closed"><code>closed</code> attribute</a> that will be <code>0</code> when the connection i... | 13 | 2013-09-09T23:29:24Z | [
"python",
"database",
"connection",
"psycopg2"
] |
Making sure that psycopg2 database connection alive | 1,281,875 | <p>I have a python application that opens a database connection that can hang online for an hours, but sometimes the database server reboots and while python still have the connection it won't work with <code>OperationalError</code> exception.</p>
<p>So I'm looking for any reliable method to "ping" the database and kn... | 7 | 2009-08-15T13:17:09Z | 20,090,982 | <p><code>connection.closed</code> does not reflect a connection closed/severed by the server. It only indicates a connection closed by the client using <code>connection.close()</code></p>
<p>In order to make sure a connection is still valid, read the property <code>connection.isolation_level</code>. This will raise an... | 3 | 2013-11-20T08:39:32Z | [
"python",
"database",
"connection",
"psycopg2"
] |
Copying files to directories as specified in a file list with python | 1,281,944 | <p>I have a bunch of files in a single directory that I would like to organize in sub-directories.</p>
<p>This directory structure (which file would go in which directory) is specified in a file list that looks like this:</p>
<p><code>Directory: Music\</code></p>
<p><code> -> 01-some_song1.mp3</code></p>
<p><co... | 1 | 2009-08-15T13:50:33Z | 1,281,966 | <p>I think that the cause is that you are reusing always the same list. </p>
<p><code>del tmp[:]</code> clears the list and doesn't create a new instance. In your case, you need to create a new list by calling <code>tmp = []</code></p>
<p>Following fix should work (I didn't test it)</p>
<pre>
def get_values(file):
... | 1 | 2009-08-15T14:01:13Z | [
"python",
"file",
"copy",
"directory"
] |
Copying files to directories as specified in a file list with python | 1,281,944 | <p>I have a bunch of files in a single directory that I would like to organize in sub-directories.</p>
<p>This directory structure (which file would go in which directory) is specified in a file list that looks like this:</p>
<p><code>Directory: Music\</code></p>
<p><code> -> 01-some_song1.mp3</code></p>
<p><co... | 1 | 2009-08-15T13:50:33Z | 1,282,027 | <p>no need to use regular expression </p>
<pre><code>d = {}
for line in open("file"):
line=line.strip()
if line.endswith("\\"):
directory = line.split(":")[-1].strip().replace("\\","")
d.setdefault(directory,[])
if line.startswith("->"):
song=line.split(" ")[-1]
d[directo... | 1 | 2009-08-15T14:41:01Z | [
"python",
"file",
"copy",
"directory"
] |
Copying files to directories as specified in a file list with python | 1,281,944 | <p>I have a bunch of files in a single directory that I would like to organize in sub-directories.</p>
<p>This directory structure (which file would go in which directory) is specified in a file list that looks like this:</p>
<p><code>Directory: Music\</code></p>
<p><code> -> 01-some_song1.mp3</code></p>
<p><co... | 1 | 2009-08-15T13:50:33Z | 1,282,076 | <p>If you use collections.defaultdict(list), you get a list that dictionary whose elements are lists. If the key is not found, it is added with a value of empty list, so you can start appending to the list immediately. That's what this line does:</p>
<pre><code>d[dir].append(match.group(1))
</code></pre>
<p>It create... | 0 | 2009-08-15T15:02:56Z | [
"python",
"file",
"copy",
"directory"
] |
Program invoking: at least one command line parameter | 1,282,347 | <p>I'm trying to check that a user of this code runs this program from command line using only one command line parameter (the name of the file). When I run this code like this (I'm on windows)</p>
<pre><code>C:\>python program.py
Usage: program.py <file.txt>
C:\>
</code></pre>
<p>Right. But when I run th... | 2 | 2009-08-15T17:07:29Z | 1,282,354 | <p>Here's your problem:</p>
<pre><code>def main(argv):
if len(argv) < 2:
sys.stderr.write("Usage: %s <file.txt>" % (argv[0],))
return 1
if __name__ == "__main__":
sys.exit(main(sys.argv))
</code></pre>
<p>Your main function returns 1 if <code>len(argv) < 2</code>, otherwise it retu... | 0 | 2009-08-15T17:12:04Z | [
"python"
] |
Program invoking: at least one command line parameter | 1,282,347 | <p>I'm trying to check that a user of this code runs this program from command line using only one command line parameter (the name of the file). When I run this code like this (I'm on windows)</p>
<pre><code>C:\>python program.py
Usage: program.py <file.txt>
C:\>
</code></pre>
<p>Right. But when I run th... | 2 | 2009-08-15T17:07:29Z | 1,282,355 | <p>You need to move the <code>f = open...</code> and beyond into the main function. As it stands, it never gets executed because execution ends at the sys.exit call.</p>
<p>[EDIT] Structuring a module this way is a common Python idiom, BTW. In this way, a file may contain class and function definitions which can be ... | 4 | 2009-08-15T17:12:30Z | [
"python"
] |
Program invoking: at least one command line parameter | 1,282,347 | <p>I'm trying to check that a user of this code runs this program from command line using only one command line parameter (the name of the file). When I run this code like this (I'm on windows)</p>
<pre><code>C:\>python program.py
Usage: program.py <file.txt>
C:\>
</code></pre>
<p>Right. But when I run th... | 2 | 2009-08-15T17:07:29Z | 1,282,358 | <p>I guess you want:</p>
<pre><code>#!/Python26/
# -*- coding: utf-8 -*-
import sys
def main(argv):
if len(argv) < 2:
sys.stderr.write("Usage: %s <file.txt>" % (argv[0],))
return 1
f = open(sys.argv[1])
lines = f.readlines()
f.close()
for line in lines:
line = l... | 3 | 2009-08-15T17:14:56Z | [
"python"
] |
Program invoking: at least one command line parameter | 1,282,347 | <p>I'm trying to check that a user of this code runs this program from command line using only one command line parameter (the name of the file). When I run this code like this (I'm on windows)</p>
<pre><code>C:\>python program.py
Usage: program.py <file.txt>
C:\>
</code></pre>
<p>Right. But when I run th... | 2 | 2009-08-15T17:07:29Z | 1,282,362 | <pre><code>import sys
def main(argv):
if len(argv) < 2:
sys.stderr.write("Usage: %s <file.txt>" % (argv[0],))
return 1
f = open(sys.argv[1])
lines = f.readlines()
f.close()
for line in lines:
line = line.strip()
etc...
return 0
if __name__ == "__main... | 1 | 2009-08-15T17:16:10Z | [
"python"
] |
Program invoking: at least one command line parameter | 1,282,347 | <p>I'm trying to check that a user of this code runs this program from command line using only one command line parameter (the name of the file). When I run this code like this (I'm on windows)</p>
<pre><code>C:\>python program.py
Usage: program.py <file.txt>
C:\>
</code></pre>
<p>Right. But when I run th... | 2 | 2009-08-15T17:07:29Z | 1,282,532 | <p>I notice the peculiar fact that everybody's code is suggesting that your <code>main</code> should ignore its arguments (except for checking it has enough) and reach back right into sys.argv instead. That's truly weird and I can't imagine any case where it would be reasonable -- makes main hard to reuse and surprisin... | 2 | 2009-08-15T18:39:12Z | [
"python"
] |
Program invoking: at least one command line parameter | 1,282,347 | <p>I'm trying to check that a user of this code runs this program from command line using only one command line parameter (the name of the file). When I run this code like this (I'm on windows)</p>
<pre><code>C:\>python program.py
Usage: program.py <file.txt>
C:\>
</code></pre>
<p>Right. But when I run th... | 2 | 2009-08-15T17:07:29Z | 24,611,790 | <p>Please be aware that depending on your Python installation (you said initially you were on Windows), the numbering of command line arguments can be off by one. As it is good practice to write portable code (no matter whether you plan to run it somewhere else, sooner or later this <em>always</em> pays off), I'd recom... | 0 | 2014-07-07T13:31:11Z | [
"python"
] |
Handling both SSL and non-SSL connections when inheriting from httplib.HTTP(s)Connection | 1,282,368 | <p>I have a class that inherits from httplib.HTTPSConnection. </p>
<pre><code>class MyConnection(httplib.HTTPSConnection):
def __init__(self, *args, **kw):
httplib.HTTPSConnection.__init__(self,*args, **kw)
...
</code></pre>
<p>Is it possible to turn off the SSL layer when the class is instantiatied so I ca... | 1 | 2009-08-15T17:18:09Z | 1,282,386 | <p>Per your last paragraph, in Python you can use something like a factory pattern:</p>
<pre><code>class Foo:
def doit(self):
print "I'm a foo"
class Bar:
def doit(self):
print "I'm a bar"
def MakeClass(isSecure):
if isSecure:
base = Foo
else:
base = Bar
class Quux... | 2 | 2009-08-15T17:26:25Z | [
"python",
"http",
"https"
] |
Handling both SSL and non-SSL connections when inheriting from httplib.HTTP(s)Connection | 1,282,368 | <p>I have a class that inherits from httplib.HTTPSConnection. </p>
<pre><code>class MyConnection(httplib.HTTPSConnection):
def __init__(self, *args, **kw):
httplib.HTTPSConnection.__init__(self,*args, **kw)
...
</code></pre>
<p>Is it possible to turn off the SSL layer when the class is instantiatied so I ca... | 1 | 2009-08-15T17:18:09Z | 1,282,390 | <p>Apparently, you want to use MyConnection for multiple subsequent connections to different hosts. If so, you shouldn't be inheriting from HTTP(S)Connection at all - that class isn't really meant to be used for multiple connections. Instead, just make MyConnection <em>have</em> a HTTP(S)Connection.</p>
| 0 | 2009-08-15T17:27:11Z | [
"python",
"http",
"https"
] |
Handling both SSL and non-SSL connections when inheriting from httplib.HTTP(s)Connection | 1,282,368 | <p>I have a class that inherits from httplib.HTTPSConnection. </p>
<pre><code>class MyConnection(httplib.HTTPSConnection):
def __init__(self, *args, **kw):
httplib.HTTPSConnection.__init__(self,*args, **kw)
...
</code></pre>
<p>Is it possible to turn off the SSL layer when the class is instantiatied so I ca... | 1 | 2009-08-15T17:18:09Z | 1,282,550 | <p>As per my comment on @Mark's answer, I like the factory approach he's advocating. However, I wouldn't do it exactly his way, because he makes a new class afresh every time. Rather, this is a nice use case for mixin MI and <code>super</code>, as follows:</p>
<pre><code>class MyConnectionPlugin(object):
def __init_... | 1 | 2009-08-15T18:51:55Z | [
"python",
"http",
"https"
] |
Explain socket buffers please | 1,282,656 | <p>I was trying to find examples about socket programming and came upon this script:
<a href="http://stacklessexamples.googlecode.com/svn/trunk/examples/networking/mud.py" rel="nofollow">http://stacklessexamples.googlecode.com/svn/trunk/examples/networking/mud.py</a></p>
<p>When reading through this script i found thi... | 1 | 2009-08-15T19:38:37Z | 1,282,669 | <p>The following is applicable to sockets in general, but it should help answer your specific question about using sockets from Python.</p>
<p>socket.listen() is used on a <em>server</em> socket to listen for incoming connection requests.</p>
<p>The parameter passed to listen is called the <em>backlog</em> and it mea... | 12 | 2009-08-15T19:42:20Z | [
"python",
"sockets",
"stackless",
"python-stackless"
] |
Explain socket buffers please | 1,282,656 | <p>I was trying to find examples about socket programming and came upon this script:
<a href="http://stacklessexamples.googlecode.com/svn/trunk/examples/networking/mud.py" rel="nofollow">http://stacklessexamples.googlecode.com/svn/trunk/examples/networking/mud.py</a></p>
<p>When reading through this script i found thi... | 1 | 2009-08-15T19:38:37Z | 1,282,673 | <p>This might help you understand the code: <a href="http://www.amk.ca/python/howto/sockets/" rel="nofollow">http://www.amk.ca/python/howto/sockets/</a></p>
| 0 | 2009-08-15T19:43:31Z | [
"python",
"sockets",
"stackless",
"python-stackless"
] |
Explain socket buffers please | 1,282,656 | <p>I was trying to find examples about socket programming and came upon this script:
<a href="http://stacklessexamples.googlecode.com/svn/trunk/examples/networking/mud.py" rel="nofollow">http://stacklessexamples.googlecode.com/svn/trunk/examples/networking/mud.py</a></p>
<p>When reading through this script i found thi... | 1 | 2009-08-15T19:38:37Z | 1,282,677 | <p>The argument <code>5</code> to <code>listenSocket.listen</code> isn't the number of bytes to read or buffer, it's the <code>backlog</code>:</p>
<blockquote>
<p><a href="http://docs.python.org/library/socket.html#socket.socket.listen" rel="nofollow"><code>socket.listen(backlog)</code></a></p>
<p>Listen for co... | 0 | 2009-08-15T19:45:13Z | [
"python",
"sockets",
"stackless",
"python-stackless"
] |
aap - python trouble | 1,282,828 | <p>i'm trying to run aap-application. Version is 1.076 (tried higher). All commands send me an error like: </p>
<pre><code>> Traceback (most recent call last):
> File "/usr/bin/aap", line 10, in
> <module>
> import Main File "/usr/share/aap/Main.py", line 14, in
> <module>
> f... | 1 | 2009-08-15T21:00:29Z | 1,282,839 | <p><code>as</code> is a <a href="http://docs.python.org/reference/lexical%5Fanalysis.html#keywords" rel="nofollow">reserved word</a> in Python.</p>
<p>Seems aap-application was written for Python 2.5 and bellow:</p>
<blockquote>
<p>Changed in version 2.5: Both <strong><code>as</code></strong> and <code>with</code> ... | 3 | 2009-08-15T21:07:05Z | [
"python",
"linux"
] |
aap - python trouble | 1,282,828 | <p>i'm trying to run aap-application. Version is 1.076 (tried higher). All commands send me an error like: </p>
<pre><code>> Traceback (most recent call last):
> File "/usr/bin/aap", line 10, in
> <module>
> import Main File "/usr/share/aap/Main.py", line 14, in
> <module>
> f... | 1 | 2009-08-15T21:00:29Z | 1,282,840 | <p>Well, <code>as</code> is a reserved word in Python. So, that can't be used in FileType.py as a variable name.</p>
<p>Try updating your installation of <code>aap</code> or writing in to the aap authors/forums.</p>
| 6 | 2009-08-15T21:07:16Z | [
"python",
"linux"
] |
Using Python to read the screen and controlling keyboard/mouse on OSX | 1,282,860 | <p>I'm looking for or trying to write a testing suite in Python which will control the mouse/keyboard and watch the screen for changes.</p>
<p>The obvious parts I need are (1) screen watcher, (2) keyboard/mouse control.</p>
<p>The latter is explained <a href="http://stackoverflow.com/questions/281133/controlling-the-... | 0 | 2009-08-15T21:13:45Z | 1,283,063 | <p>I can't think of a smart way to "watch the screen for changes" in any OS nor with any language. On MacOSX, you can take screenshots programmatically at any time, e.g. with code like the one Apple shows at <a href="http://developer.apple.com/samplecode/SonOfGrab/" rel="nofollow">this sample</a> (translating the Objec... | 2 | 2009-08-15T22:50:04Z | [
"python",
"user-interface",
"osx"
] |
How are nested dictionaries handled by DictWriter? | 1,282,920 | <p>Using the CSV module in python, I was experimenting with the DictWriter class to convert dictionaries to rows in a csv. Is there any way to handle nested dictionaries? Specifically, I'm exporting Disqus comments that have a structure like this:</p>
<pre><code>{
u'status': u'approved',
u'forum': {u'id': u'', u'': ... | 2 | 2009-08-15T21:39:49Z | 1,282,968 | <p>I think the main problem your going to have is how to represent a nested data structure in one flat row of csv data.</p>
<p>You could use some form of name mangeling to flatten the keys from the sub dict's into the top level dict.</p>
<p>eg thread': {u'allow_comments': </p>
<p>would become thread_allows_comments.... | 1 | 2009-08-15T21:59:11Z | [
"python",
"csv"
] |
Python integer division yields float | 1,282,945 | <pre><code>Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2/2
1.0
</code></pre>
<p>Is this intended? I strongly remember earlier versions returning <code>int/int=int</code>? What should I do, is ther... | 101 | 2009-08-15T21:48:39Z | 1,282,948 | <p>Oops, immediately found <code>2//2</code>.</p>
| 32 | 2009-08-15T21:50:14Z | [
"python",
"integer",
"python-3.x",
"division"
] |
Python integer division yields float | 1,282,945 | <pre><code>Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2/2
1.0
</code></pre>
<p>Is this intended? I strongly remember earlier versions returning <code>int/int=int</code>? What should I do, is ther... | 101 | 2009-08-15T21:48:39Z | 1,282,954 | <p>Take a look at <a href="http://www.python.org/dev/peps/pep-0238/">PEP-238</a>: Changing the Division Operator</p>
<blockquote>
<p>The // operator will be available to request floor division unambiguously.</p>
</blockquote>
| 142 | 2009-08-15T21:51:44Z | [
"python",
"integer",
"python-3.x",
"division"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.