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 |
|---|---|---|---|---|---|---|---|---|---|
Short Integers in Python | 120,250 | <p>Python allocates integers automatically based on the underlying system architecture. Unfortunately I have a huge dataset which needs to be fully loaded into memory. </p>
<p>So, is there a way to force Python to use only 2 bytes for some integers (equivalent of C++ 'short')?</p>
| 10 | 2008-09-23T10:35:09Z | 120,449 | <p>Thanks to Armin for pointing out the 'array' module. I also found the 'struct' module that packs c-style structs in a string:</p>
<p>From the documentation (<a href="https://docs.python.org/library/struct.html" rel="nofollow">https://docs.python.org/library/struct.html</a>):</p>
<pre><code>>>> from struct... | 5 | 2008-09-23T11:34:37Z | [
"python",
"memory-management",
"short"
] |
Short Integers in Python | 120,250 | <p>Python allocates integers automatically based on the underlying system architecture. Unfortunately I have a huge dataset which needs to be fully loaded into memory. </p>
<p>So, is there a way to force Python to use only 2 bytes for some integers (equivalent of C++ 'short')?</p>
| 10 | 2008-09-23T10:35:09Z | 120,454 | <p>Armin's suggestion of the array module is probably best. Two possible alternatives:</p>
<ul>
<li>You can create an extension module yourself that provides the data structure that you're after. If it's really just something like a collection of shorts, then
that's pretty simple to do.</li>
<li>You can
cheat and ma... | 2 | 2008-09-23T11:35:15Z | [
"python",
"memory-management",
"short"
] |
Short Integers in Python | 120,250 | <p>Python allocates integers automatically based on the underlying system architecture. Unfortunately I have a huge dataset which needs to be fully loaded into memory. </p>
<p>So, is there a way to force Python to use only 2 bytes for some integers (equivalent of C++ 'short')?</p>
| 10 | 2008-09-23T10:35:09Z | 120,469 | <p>@<a href="#120256" rel="nofollow">Armin</a>: how come? The Python documentation said the minimum size for that array of short integer is 2 bytes and </p>
<blockquote>
<p>The actual representation of values is
determined by the machine architecture
(strictly speaking, by the C
implementation). The actual siz... | 0 | 2008-09-23T11:38:59Z | [
"python",
"memory-management",
"short"
] |
Short Integers in Python | 120,250 | <p>Python allocates integers automatically based on the underlying system architecture. Unfortunately I have a huge dataset which needs to be fully loaded into memory. </p>
<p>So, is there a way to force Python to use only 2 bytes for some integers (equivalent of C++ 'short')?</p>
| 10 | 2008-09-23T10:35:09Z | 120,924 | <p>If you're doing any sort of manipulation of this huge dataset, you'll probably want to use <a href="http://numpy.scipy.org/" rel="nofollow" title="Numpy Home Page">Numpy</a>, which has support for a wide variety of numeric types, and efficient operations on arrays of them.</p>
| 4 | 2008-09-23T13:20:30Z | [
"python",
"memory-management",
"short"
] |
SVG rendering in a PyGame application | 120,584 | <p>In a <a href="http://www.pygame.org/">pyGame</a> application, I would like to render resolution-free GUI widgets described in SVG.</p>
<p>What tool and/or library can I use to reach this goal ?</p>
<p>(I like the <a href="http://ocemp.sourceforge.net/gui.html">OCEMP GUI</a> toolkit but it seems to be bitmap depend... | 17 | 2008-09-23T12:13:14Z | 120,794 | <p>You can use <a href="http://www.cairographics.org/" rel="nofollow">Cairo</a> (with PyCairo), which has support for rendering SVGs. The PyGame webpage has a <a href="http://www.pygame.org/wiki/CairoPygame" rel="nofollow">HOWTO</a> for rendering into a buffer with a Cairo, and using that buffer directly with PyGame.</... | 3 | 2008-09-23T12:52:32Z | [
"python",
"svg",
"widget",
"pygame"
] |
SVG rendering in a PyGame application | 120,584 | <p>In a <a href="http://www.pygame.org/">pyGame</a> application, I would like to render resolution-free GUI widgets described in SVG.</p>
<p>What tool and/or library can I use to reach this goal ?</p>
<p>(I like the <a href="http://ocemp.sourceforge.net/gui.html">OCEMP GUI</a> toolkit but it seems to be bitmap depend... | 17 | 2008-09-23T12:13:14Z | 121,651 | <p>I realise this doesn't exactly answer your question, but there's a library called <a href="http://www.supereffective.org/?p=14" rel="nofollow">Squirtle</a> that will render SVG files using either Pyglet or PyOpenGL.</p>
| 3 | 2008-09-23T15:16:51Z | [
"python",
"svg",
"widget",
"pygame"
] |
SVG rendering in a PyGame application | 120,584 | <p>In a <a href="http://www.pygame.org/">pyGame</a> application, I would like to render resolution-free GUI widgets described in SVG.</p>
<p>What tool and/or library can I use to reach this goal ?</p>
<p>(I like the <a href="http://ocemp.sourceforge.net/gui.html">OCEMP GUI</a> toolkit but it seems to be bitmap depend... | 17 | 2008-09-23T12:13:14Z | 121,653 | <p>Cairo cannot render SVG out of the box.
It seems we have to use librsvg.</p>
<p>Just found those two pages:</p>
<ul>
<li><a href="http://www.cairographics.org/cookbook/librsvgpython/" rel="nofollow">Rendering SVG with libRSVG,Python and c-types</a> </li>
<li><a href="http://www.cairographics.org/pyrsvg/" rel="nofo... | 2 | 2008-09-23T15:17:06Z | [
"python",
"svg",
"widget",
"pygame"
] |
SVG rendering in a PyGame application | 120,584 | <p>In a <a href="http://www.pygame.org/">pyGame</a> application, I would like to render resolution-free GUI widgets described in SVG.</p>
<p>What tool and/or library can I use to reach this goal ?</p>
<p>(I like the <a href="http://ocemp.sourceforge.net/gui.html">OCEMP GUI</a> toolkit but it seems to be bitmap depend... | 17 | 2008-09-23T12:13:14Z | 152,222 | <p><a href="http://paul.giannaros.org/sandbox_pygamesvg" rel="nofollow">pygamesvg</a> seems to do what you want (though I haven't tried it).</p>
| 3 | 2008-09-30T08:24:24Z | [
"python",
"svg",
"widget",
"pygame"
] |
SVG rendering in a PyGame application | 120,584 | <p>In a <a href="http://www.pygame.org/">pyGame</a> application, I would like to render resolution-free GUI widgets described in SVG.</p>
<p>What tool and/or library can I use to reach this goal ?</p>
<p>(I like the <a href="http://ocemp.sourceforge.net/gui.html">OCEMP GUI</a> toolkit but it seems to be bitmap depend... | 17 | 2008-09-23T12:13:14Z | 341,742 | <p>This is a complete example which combines hints by other people here.
It should render a file called test.svg from the current directory. It was tested on Ubuntu 10.10, python-cairo 1.8.8, python-pygame 1.9.1, python-rsvg 2.30.0.</p>
<pre><code>#!/usr/bin/python
import array
import math
import cairo
import pygam... | 11 | 2008-12-04T19:23:54Z | [
"python",
"svg",
"widget",
"pygame"
] |
SVG rendering in a PyGame application | 120,584 | <p>In a <a href="http://www.pygame.org/">pyGame</a> application, I would like to render resolution-free GUI widgets described in SVG.</p>
<p>What tool and/or library can I use to reach this goal ?</p>
<p>(I like the <a href="http://ocemp.sourceforge.net/gui.html">OCEMP GUI</a> toolkit but it seems to be bitmap depend... | 17 | 2008-09-23T12:13:14Z | 4,950,448 | <p>The last comment crashed when I ran it because svg.render_cairo() is expecting a cairo context and not a cairo surface. I created and tested the following function and it seems to run fine on my system.</p>
<pre><code>import array,cairo, pygame,rsvg
def loadsvg(filename,surface,position):
WIDTH = surface.get_w... | 1 | 2011-02-09T21:06:29Z | [
"python",
"svg",
"widget",
"pygame"
] |
SVG rendering in a PyGame application | 120,584 | <p>In a <a href="http://www.pygame.org/">pyGame</a> application, I would like to render resolution-free GUI widgets described in SVG.</p>
<p>What tool and/or library can I use to reach this goal ?</p>
<p>(I like the <a href="http://ocemp.sourceforge.net/gui.html">OCEMP GUI</a> toolkit but it seems to be bitmap depend... | 17 | 2008-09-23T12:13:14Z | 26,188,526 | <p>Based on other answers, here's a function to read a SVG file into a pygame image - including correcting color channel order and scaling:</p>
<pre><code>def pygame_svg( svg_file, scale=1 ):
svg = rsvg.Handle(file=svg_file)
width, height= map(svg.get_property, ("width", "height"))
width*=scale; height*=sc... | 0 | 2014-10-04T00:43:37Z | [
"python",
"svg",
"widget",
"pygame"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 120,676 | <p>You can use</p>
<pre><code>os.listdir(path)
</code></pre>
<p>For reference and more os functions look here:</p>
<ul>
<li>Python 2 docs: <a href="https://docs.python.org/2/library/os.html#os.listdir">https://docs.python.org/2/library/os.html#os.listdir</a></li>
<li>Python 3 docs: <a href="https://docs.python.org/3... | 370 | 2008-09-23T12:32:00Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 120,692 | <pre><code>import os
for filename in os.listdir("C:\\temp"):
print filename
</code></pre>
| 74 | 2008-09-23T12:34:07Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 120,695 | <p>Try this:</p>
<pre><code>import os
for top, dirs, files in os.walk('./'):
for nm in files:
print os.path.join(top, nm)
</code></pre>
| 9 | 2008-09-23T12:34:34Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 120,701 | <p>This is a way to traverse every file and directory in a directory tree:</p>
<pre><code>import os
for dirname, dirnames, filenames in os.walk('.'):
# print path to all subdirectories first.
for subdirname in dirnames:
print(os.path.join(dirname, subdirname))
# print path to all filenames.
f... | 503 | 2008-09-23T12:35:46Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 120,948 | <p>Here's a helper function I use quite often:</p>
<pre><code>import os
def listdir_fullpath(d):
return [os.path.join(d, f) for f in os.listdir(d)]
</code></pre>
| 63 | 2008-09-23T13:23:29Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 11,753,937 | <p>I wrote a long version, with all the options I might need: <a href="http://sam.nipl.net/code/python/find.py" rel="nofollow">http://sam.nipl.net/code/python/find.py</a></p>
<p>I guess it will fit here too:</p>
<pre><code>#!/usr/bin/env python
import os
import sys
def ls(dir, hidden=False, relative=True):
node... | 2 | 2012-08-01T06:47:52Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 12,572,822 | <p>If you need globbing abilities, there's a module for that as well. For example:</p>
<pre><code>import glob
glob.glob('./[0-9].*')
</code></pre>
<p>will return something like:</p>
<pre><code>['./1.gif', './2.txt']
</code></pre>
<p>See the documentation <a href="http://docs.python.org/library/glob.html">here</a>.<... | 10 | 2012-09-24T20:58:14Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 13,528,334 | <pre><code>#import modules
import os
_CURRENT_DIR = '.'
def rec_tree_traverse(curr_dir, indent):
"recurcive function to traverse the directory"
#print "[traverse_tree]"
try :
dfList = [os.path.join(curr_dir, f_or_d) for f_or_d in os.listdir(curr_dir)]
except:
print "wrong path name/d... | 0 | 2012-11-23T11:38:05Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 25,390,299 | <p>FYI Add a filter of extension or ext file
import os</p>
<pre><code>path = '.'
for dirname, dirnames, filenames in os.walk(path):
# print path to all filenames with extension py.
for filename in filenames:
fname_path = os.path.join(dirname, filename)
fext = os.path.splitext(fname_path)[1]... | 0 | 2014-08-19T18:17:06Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 27,713,560 | <p>A nice one liner to list only the files recursively. I used this in my setup.py package_data directive:</p>
<pre><code>import os
[os.path.join(x[0],y) for x in os.walk('<some_directory>') for y in x[2]]
</code></pre>
<p>I know it's not the answer to the question, but may come in handy</p>
| 1 | 2014-12-30T22:35:24Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 30,471,791 | <p>A recursive implementation</p>
<pre><code>import os
def scan_dir(dir):
for name in os.listdir(dir):
path = os.path.join(dir, name)
if os.path.isfile(path):
print path
else:
scan_dir(path)
</code></pre>
| 3 | 2015-05-27T01:45:10Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 30,878,347 | <pre><code>import os, sys
#open files in directory
path = "My Documents"
dirs = os.listdir( path )
# print the files in given directory
for file in dirs:
print (file)
</code></pre>
| 0 | 2015-06-16T21:15:27Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 35,628,738 | <p>If figured I'd throw this in. Simple and dirty way to do wildcard searches.</p>
<pre><code>import re
import os
[a for a in os.listdir(".") if re.search("^.*\.py$",a)]
</code></pre>
| 0 | 2016-02-25T13:31:56Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 38,609,425 | <p>For files in current working directory without specifying a path:</p>
<pre><code>import os
os.listdir(os.getcwd())
</code></pre>
<p>I still think this is quite clumsy, if anyone knows a shorter equivalent please comment</p>
| 2 | 2016-07-27T09:47:07Z | [
"python",
"filesystems"
] |
Directory listing in Python | 120,656 | <p>How do I get a list of all files (and directories) in a given directory in Python?</p>
| 413 | 2008-09-23T12:28:19Z | 38,941,184 | <h1>For Python 2</h1>
<pre><code>#!/bin/python2
import os
def scan_dir(path):
print map(os.path.abspath, os.listdir(pwd))
</code></pre>
<h1>For Python 3</h1>
<p>For filter and map, you need wrap them with list()</p>
<pre><code>#!/bin/python3
import os
def scan_dir(path):
print(list(map(os.path.abspath, ... | 0 | 2016-08-14T10:11:03Z | [
"python",
"filesystems"
] |
Python subprocess issue with ampersands | 120,657 | <p>I'm currently having a major issue with a python script. The script runs arbitrary commands through a handler to convert incorrect error reporting into correct error reporting.</p>
<p>The issue I'm having is getting the script to work correctly on windows with a command that contains ampersands in it's path. I've a... | 3 | 2008-09-23T12:28:39Z | 120,705 | <p>A proper answer will need more information than that. What are you actually doing? How does it fail? Are you using the subprocess module? Are you passing a list of arguments and shell=False (or no shell argument) or are you actually invoking the shell?</p>
| 1 | 2008-09-23T12:36:18Z | [
"python",
"windows",
"subprocess",
"command-line-arguments"
] |
Python subprocess issue with ampersands | 120,657 | <p>I'm currently having a major issue with a python script. The script runs arbitrary commands through a handler to convert incorrect error reporting into correct error reporting.</p>
<p>The issue I'm having is getting the script to work correctly on windows with a command that contains ampersands in it's path. I've a... | 3 | 2008-09-23T12:28:39Z | 120,706 | <p>Make sure you are using lists and no shell expansion:</p>
<pre><code>subprocess.Popen(['command', 'argument1', 'argument2'], shell=False)
</code></pre>
| 5 | 2008-09-23T12:36:19Z | [
"python",
"windows",
"subprocess",
"command-line-arguments"
] |
Python subprocess issue with ampersands | 120,657 | <p>I'm currently having a major issue with a python script. The script runs arbitrary commands through a handler to convert incorrect error reporting into correct error reporting.</p>
<p>The issue I'm having is getting the script to work correctly on windows with a command that contains ampersands in it's path. I've a... | 3 | 2008-09-23T12:28:39Z | 120,708 | <p>Try quoting the argument that contains the &</p>
<pre><code>wget "http://foo.com/?bar=baz&amp;baz=bar"
</code></pre>
<p>Is usually what has to be done in a Linux shell</p>
| 1 | 2008-09-23T12:36:32Z | [
"python",
"windows",
"subprocess",
"command-line-arguments"
] |
Python subprocess issue with ampersands | 120,657 | <p>I'm currently having a major issue with a python script. The script runs arbitrary commands through a handler to convert incorrect error reporting into correct error reporting.</p>
<p>The issue I'm having is getting the script to work correctly on windows with a command that contains ampersands in it's path. I've a... | 3 | 2008-09-23T12:28:39Z | 120,782 | <p>To answer my own question:</p>
<p>Quoting the actual command when passing the parameters as a list doesn't work correctly (command is first item of list) so to solve the issue I turned the list into a space separated string and passed that into subprocess instead.</p>
<p>Better solutions still welcomed.</p>
| 0 | 2008-09-23T12:49:55Z | [
"python",
"windows",
"subprocess",
"command-line-arguments"
] |
Python subprocess issue with ampersands | 120,657 | <p>I'm currently having a major issue with a python script. The script runs arbitrary commands through a handler to convert incorrect error reporting into correct error reporting.</p>
<p>The issue I'm having is getting the script to work correctly on windows with a command that contains ampersands in it's path. I've a... | 3 | 2008-09-23T12:28:39Z | 120,992 | <p>"escaping the ampersand with ^"</p>
<p>Are you sure <code>^</code> is an escape character to Windows? Shouldn't you use <code>\</code>?</p>
| 1 | 2008-09-23T13:29:31Z | [
"python",
"windows",
"subprocess",
"command-line-arguments"
] |
Python subprocess issue with ampersands | 120,657 | <p>I'm currently having a major issue with a python script. The script runs arbitrary commands through a handler to convert incorrect error reporting into correct error reporting.</p>
<p>The issue I'm having is getting the script to work correctly on windows with a command that contains ampersands in it's path. I've a... | 3 | 2008-09-23T12:28:39Z | 20,922,266 | <p>I try a situation as following:</p>
<pre><code>exe = 'C:/Program Files (x86)/VideoLAN/VLC/VLC.exe'
url = 'http://translate.google.com/translate_tts?tl=en&q=hello+world'
subprocess.Popen([exe, url.replace("&","^&")],shell=True)
</code></pre>
<p>This does work.</p>
| 0 | 2014-01-04T14:36:14Z | [
"python",
"windows",
"subprocess",
"command-line-arguments"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 120,956 | <p>The problem with tabs is that they are invisible, and people can never agree on the width of tabs. When you mix tabs and spaces, and you set tabstops at something other than Python (which uses tabstops every 8 spaces) you will be seeing the code in a different layout than Python sees it. And because the layout deter... | 25 | 2008-09-23T13:24:26Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 120,969 | <p>The universal problem with tabs is that they can be represented differently in different environment.<br />
In a given editor, a tab might be 8 spaces or it might be 2.<br />
In some editors, you can control this, while in others you can't.</p>
<p>Another issue with tabs is how they are represented in printed outpu... | -2 | 2008-09-23T13:25:57Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 120,996 | <p>The most significant advantage I can tell of spaces over tabs is that a lot of programmers and projects use a set number of columns for the source code, and if someone commits a change with their tabstop set to 2 spaces and the project uses 4 spaces as the tabstop the long lines are going to be too long for other pe... | 0 | 2008-09-23T13:30:23Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 121,036 | <p>Since python relies on indentation in order to recognize program structure, a clear way to identify identation is required. This is the reason to pick either spaces or tabs.</p>
<p>However, python also has a strong philosophy of only having one way to do things, therefore there should be an official recommendation ... | 1 | 2008-09-23T13:35:42Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 121,126 | <p>You can have your cake and eat it to. Set your editor to expand tabs into spaces automatically.</p>
<p>(That would be <code>:set expandtab</code> in Vim.)</p>
| 0 | 2008-09-23T13:52:25Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 121,481 | <p>The reason for spaces is that tabs are optional. Spaces are the actual lowest-common denominator in punctuation.</p>
<p>Every decent text editor has a "replace tabs with spaces" and many people use this. But not always.</p>
<p>While some text editors might replace a run of spaces with a tab, this is really rare.... | 28 | 2008-09-23T14:45:39Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 121,671 | <p>The answer was given right there in the PEP [ed: this passage has been edited out in <a href="https://hg.python.org/peps/rev/fb24c80e9afb#l1.75">2013</a>]. I quote:</p>
<blockquote>
<p>The <strong>most popular</strong> way of indenting Python is with spaces only.</p>
</blockquote>
<p>What other underlying reason... | 74 | 2008-09-23T15:19:27Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 143,995 | <p>The answer to the question is: PEP-8 wants to make a recommendation and has decided that since spaces are more popular it will strongly recommend spaces over tabs.</p>
<p><hr /></p>
<p>Notes on PEP-8</p>
<p>PEP-8 says <em>'Use 4 spaces per indentation level.'</em><br />
Its clear that this is the standard recomme... | 7 | 2008-09-27T16:56:59Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 144,096 | <p>The main problems with indentation occur when you mix tabs and spaces. Obviously this doesn't tell you which you should choose, but it is a good reason to to recommend one, even if you pick it by flipping a coin.</p>
<p>However, IMHO there are a few minor reasons to favour spaces over tabs:</p>
<ul>
<li><p>Differ... | 19 | 2008-09-27T17:32:09Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 144,133 | <p><a href="http://www.jwz.org/doc/tabs-vs-spaces.html" rel="nofollow">JWZ says it best</a>:</p>
<blockquote>
<p>When [people are] reading code, and when they're done writing new code, they care about how many screen columns by which the code tends to indent when a new scope (or sexpr, or whatever) opens...</p>
... | 2 | 2008-09-27T17:50:59Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 872,883 | <p>I personally don't agree with spaces over tabs. To me, tabs are a document layout character/mechanism while spaces are for content or delineation between commands in the case of code.</p>
<p>I have to agree with Jim's comments that tabs aren't really the issue, it is people and how they want to mix tabs and spaces... | 23 | 2009-05-16T17:41:35Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 5,048,130 | <p>Well well, seems like everybody is strongly biased towards spaces.
I use tabs exclusively. I know very well why.</p>
<p>Tabs are actually a cool invention, that came <strong>after</strong> spaces. It allows you to indent without pushing space millions of times or using a fake tab (that produces spaces).</p>
<p>I ... | 34 | 2011-02-19T00:56:16Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 6,965,294 | <p>On the discussion between <a href="http://stackoverflow.com/questions/120926/why-does-python-pep-8-strongly-recommend-spaces-over-tabs-for-indentation/120956#120956">Jim and Thomas Wouters</a> in the comments.</p>
<p>The issue was... since the width of tabs and spaces both can vary -- and since programmers can't ag... | -2 | 2011-08-06T06:53:51Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 8,747,788 | <p>I've always used tabs in my code. That said, I've recently found a reason to use spaces: When developing on my Nokia N900 internet tablet, I now had a keyboard without a tab key. This forced me to either copy and paste tabs or re-write my code with spaces.
I've run into the same problem with other phones. Granted, t... | 2 | 2012-01-05T18:31:35Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 15,076,562 | <p>Besides all the other reasons already named (consistency, never mixing spaces and tabs etc) I believe there are a few more reasons for the 4 spaces convention to note. These only apply to Python (and maybe other languages where indentation has meaning). Tabs may be nicer in other languages, depending on individual p... | -1 | 2013-02-25T21:11:22Z | [
"python",
"indentation"
] |
Why does Python pep-8 strongly recommend spaces over tabs for indentation? | 120,926 | <p>I see on Stack Overflow and <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a> that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain.</p>
<p>Is there an underlying reason for spaces to be preferred?... | 90 | 2008-09-23T13:20:34Z | 18,034,554 | <p>Note that the use of tabs confuses another aspect of PEP 8:</p>
<blockquote>
<p>Limit all lines to a maximum of 79 characters.</p>
</blockquote>
<p>Let's say, hypothetically, that you use a tab width of 2 and I use a tab width of 8. You write all your code so your longest lines reach 79 characters, then I start ... | 7 | 2013-08-03T15:49:14Z | [
"python",
"indentation"
] |
How can I normalize a URL in python | 120,951 | <p>I'd like to know do I normalize a URL in python. </p>
<p>For example, If I have a url string like : "http://www.example.com/foo goo/bar.html"</p>
<p>I need a library in python that will transform the extra space (or any other non normalized character) to a proper URL.</p>
| 55 | 2008-09-23T13:23:50Z | 120,959 | <pre><code>import urlparse, urllib
def myquote(url):
parts= urlparse.urlparse(url)
return urlparse.urlunparse(parts[:2] + urllib.quote(parts[2]) + parts[3:])
</code></pre>
<p>This quotes only the path component.</p>
<p>Otherwise, you could do: <code>urllib.quote(url, safe=":/")</code></p>
| 9 | 2008-09-23T13:25:01Z | [
"python",
"url",
"normalization",
"normalize"
] |
How can I normalize a URL in python | 120,951 | <p>I'd like to know do I normalize a URL in python. </p>
<p>For example, If I have a url string like : "http://www.example.com/foo goo/bar.html"</p>
<p>I need a library in python that will transform the extra space (or any other non normalized character) to a proper URL.</p>
| 55 | 2008-09-23T13:23:50Z | 120,971 | <p>use <code>urllib.quote</code> or <code>urllib.quote_plus</code></p>
<p>From the <a href="http://docs.python.org/lib/module-urllib.html">urllib documentation</a>:</p>
<blockquote>
<p><strong>quote(string[, safe])</strong></p>
<p>Replace special characters in string
using the "%xx" escape. Letters,
digits... | 18 | 2008-09-23T13:26:16Z | [
"python",
"url",
"normalization",
"normalize"
] |
How can I normalize a URL in python | 120,951 | <p>I'd like to know do I normalize a URL in python. </p>
<p>For example, If I have a url string like : "http://www.example.com/foo goo/bar.html"</p>
<p>I need a library in python that will transform the extra space (or any other non normalized character) to a proper URL.</p>
| 55 | 2008-09-23T13:23:50Z | 121,017 | <p>Have a look at this module: <a href="https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/urls.py">werkzeug.utils</a>. (now in <code>werkzeug.urls</code>)</p>
<p>The function you are looking for is called "url_fix" and works like this:</p>
<pre><code>>>> url_fix(u'http://de.wikipedia.org/wiki/Elf (B... | 60 | 2008-09-23T13:33:06Z | [
"python",
"url",
"normalization",
"normalize"
] |
How can I normalize a URL in python | 120,951 | <p>I'd like to know do I normalize a URL in python. </p>
<p>For example, If I have a url string like : "http://www.example.com/foo goo/bar.html"</p>
<p>I need a library in python that will transform the extra space (or any other non normalized character) to a proper URL.</p>
| 55 | 2008-09-23T13:23:50Z | 845,595 | <p><a href="http://svn.python.org/view/python/trunk/Lib/urllib.py?r1=71780&r2=71779&pathrev=71780">Real fix in Python 2.7 for that problem</a></p>
<p>Right solution was:</p>
<pre><code> # percent encode url, fixing lame server errors for e.g, like space
# within url paths.
fullurl = quote(fullurl, safe="%/:... | 46 | 2009-05-10T16:15:40Z | [
"python",
"url",
"normalization",
"normalize"
] |
How can I normalize a URL in python | 120,951 | <p>I'd like to know do I normalize a URL in python. </p>
<p>For example, If I have a url string like : "http://www.example.com/foo goo/bar.html"</p>
<p>I need a library in python that will transform the extra space (or any other non normalized character) to a proper URL.</p>
| 55 | 2008-09-23T13:23:50Z | 962,248 | <p>Because this page is a top result for Google searches on the topic, I think it's worth mentioning some work that has been done on URL normalization with Python that goes beyond urlencoding space characters. For example, dealing with default ports, character case, lack of trailing slashes, etc.</p>
<p>When the Atom... | 12 | 2009-06-07T16:35:25Z | [
"python",
"url",
"normalization",
"normalize"
] |
How can I normalize a URL in python | 120,951 | <p>I'd like to know do I normalize a URL in python. </p>
<p>For example, If I have a url string like : "http://www.example.com/foo goo/bar.html"</p>
<p>I need a library in python that will transform the extra space (or any other non normalized character) to a proper URL.</p>
| 55 | 2008-09-23T13:23:50Z | 1,912,115 | <p>Just FYI, urlnorm has moved to github:
<a href="http://gist.github.com/246089" rel="nofollow">http://gist.github.com/246089</a></p>
| 3 | 2009-12-16T03:43:51Z | [
"python",
"url",
"normalization",
"normalize"
] |
How can I normalize a URL in python | 120,951 | <p>I'd like to know do I normalize a URL in python. </p>
<p>For example, If I have a url string like : "http://www.example.com/foo goo/bar.html"</p>
<p>I need a library in python that will transform the extra space (or any other non normalized character) to a proper URL.</p>
| 55 | 2008-09-23T13:23:50Z | 24,203,504 | <p>I encounter such an problem: need to quote the space only.</p>
<p><code>fullurl = quote(fullurl, safe="%/:=&?~#+!$,;'@()*[]")</code> do help, but it's too complicated.</p>
<p>So I used a simple way: <code>url = url.replace(' ', '%20')</code>, it's not perfect, but it's the simplest way and it works for this si... | 1 | 2014-06-13T10:45:33Z | [
"python",
"url",
"normalization",
"normalize"
] |
Accessing object memory address | 121,396 | <p>When you call the <code>object.__repr__()</code> method in python you get something like this back: <code><__main__.Test object at 0x2aba1c0cf890></code>, is there any way to get a hold of the memory address if you overload <code>__repr__()</code>, other then calling <code>super(Class, obj).__repr__()</code> a... | 89 | 2008-09-23T14:35:00Z | 121,422 | <p>Just use </p>
<pre><code>id(object)
</code></pre>
| 32 | 2008-09-23T14:37:24Z | [
"python",
"object",
"tostring",
"memory-address",
"repr"
] |
Accessing object memory address | 121,396 | <p>When you call the <code>object.__repr__()</code> method in python you get something like this back: <code><__main__.Test object at 0x2aba1c0cf890></code>, is there any way to get a hold of the memory address if you overload <code>__repr__()</code>, other then calling <code>super(Class, obj).__repr__()</code> a... | 89 | 2008-09-23T14:35:00Z | 121,426 | <p>You can get something suitable for that purpose with:</p>
<pre><code>id(self)
</code></pre>
| 2 | 2008-09-23T14:38:01Z | [
"python",
"object",
"tostring",
"memory-address",
"repr"
] |
Accessing object memory address | 121,396 | <p>When you call the <code>object.__repr__()</code> method in python you get something like this back: <code><__main__.Test object at 0x2aba1c0cf890></code>, is there any way to get a hold of the memory address if you overload <code>__repr__()</code>, other then calling <code>super(Class, obj).__repr__()</code> a... | 89 | 2008-09-23T14:35:00Z | 121,452 | <p>The <a href="https://docs.python.org/2/library/functions.html#id">Python manual</a> has this to say about id():</p>
<blockquote>
<p>Return the ``identity'' of an object.
This is an integer (or long integer)
which is guaranteed to be unique and
constant for this object during its
lifetime. Two objects with... | 116 | 2008-09-23T14:41:30Z | [
"python",
"object",
"tostring",
"memory-address",
"repr"
] |
Accessing object memory address | 121,396 | <p>When you call the <code>object.__repr__()</code> method in python you get something like this back: <code><__main__.Test object at 0x2aba1c0cf890></code>, is there any way to get a hold of the memory address if you overload <code>__repr__()</code>, other then calling <code>super(Class, obj).__repr__()</code> a... | 89 | 2008-09-23T14:35:00Z | 121,508 | <p>You could reimplement the default repr this way:</p>
<pre><code>def __repr__(self):
return '<%s.%s object at %s>' % (
self.__class__.__module__,
self.__class__.__name__,
hex(id(self))
)
</code></pre>
| 46 | 2008-09-23T14:49:57Z | [
"python",
"object",
"tostring",
"memory-address",
"repr"
] |
Accessing object memory address | 121,396 | <p>When you call the <code>object.__repr__()</code> method in python you get something like this back: <code><__main__.Test object at 0x2aba1c0cf890></code>, is there any way to get a hold of the memory address if you overload <code>__repr__()</code>, other then calling <code>super(Class, obj).__repr__()</code> a... | 89 | 2008-09-23T14:35:00Z | 121,572 | <p>With <a href="http://docs.python.org/lib/module-ctypes.html">ctypes</a>, you can achieve the same thing with</p>
<pre><code>>>> import ctypes
>>> a = (1,2,3)
>>> ctypes.addressof(a)
3077760748L
</code></pre>
<p>Documentation:</p>
<blockquote>
<p><code>addressof(C instance) -> integ... | 6 | 2008-09-23T15:00:58Z | [
"python",
"object",
"tostring",
"memory-address",
"repr"
] |
Accessing object memory address | 121,396 | <p>When you call the <code>object.__repr__()</code> method in python you get something like this back: <code><__main__.Test object at 0x2aba1c0cf890></code>, is there any way to get a hold of the memory address if you overload <code>__repr__()</code>, other then calling <code>super(Class, obj).__repr__()</code> a... | 89 | 2008-09-23T14:35:00Z | 122,032 | <p>While it's true that <code>id(object)</code> gets the object's address in the default CPython implementation, this is generally useless... you can't <i>do</i> anything with the address from pure Python code.</p>
<p>The only time you would actually be able to use the address is from a C extension library... in which... | 0 | 2008-09-23T16:10:37Z | [
"python",
"object",
"tostring",
"memory-address",
"repr"
] |
Accessing object memory address | 121,396 | <p>When you call the <code>object.__repr__()</code> method in python you get something like this back: <code><__main__.Test object at 0x2aba1c0cf890></code>, is there any way to get a hold of the memory address if you overload <code>__repr__()</code>, other then calling <code>super(Class, obj).__repr__()</code> a... | 89 | 2008-09-23T14:35:00Z | 4,628,230 | <p>Just in response to Torsten, I wasn't able to call <code>addressof()</code> on a regular python object. Furthermore, <code>id(a) != addressof(a)</code>. This is in CPython, don't know about anything else.</p>
<pre><code>>>> from ctypes import c_int, addressof
>>> a = 69
>>> addressof(a)
T... | 11 | 2011-01-07T17:14:04Z | [
"python",
"object",
"tostring",
"memory-address",
"repr"
] |
Accessing object memory address | 121,396 | <p>When you call the <code>object.__repr__()</code> method in python you get something like this back: <code><__main__.Test object at 0x2aba1c0cf890></code>, is there any way to get a hold of the memory address if you overload <code>__repr__()</code>, other then calling <code>super(Class, obj).__repr__()</code> a... | 89 | 2008-09-23T14:35:00Z | 26,285,749 | <p>There are a few issues here that aren't covered by any of the other answers.</p>
<p>First, <a href="https://docs.python.org/3/library/functions.html#id"><code>id</code></a> only returns:</p>
<blockquote>
<p>the âidentityâ of an object. This is an integer (or long integer) which is guaranteed to be unique and... | 9 | 2014-10-09T18:41:51Z | [
"python",
"object",
"tostring",
"memory-address",
"repr"
] |
How do I include a stacktrace in my Django 500.html page? | 121,439 | <p>I'm running <code>Django 1.0</code> and I'm close to deploying my app. As such, I'll be changing the DEBUG setting to False.</p>
<p>With that being said, I'd still like to include the stacktrace on my 500.html page when errors occur. By doing so, users can copy-and-paste the errors and easily email them to the de... | 18 | 2008-09-23T14:39:35Z | 121,474 | <p>Automatically log your 500s, that way:</p>
<ul>
<li>You know when they occur.</li>
<li>You don't need to rely on users sending you stacktraces.</li>
</ul>
<p>Joel recommends even going so far as automatically creating tickets in your bug tracker when your application experiences a failure. Personally, I create a (... | 14 | 2008-09-23T14:44:50Z | [
"python",
"django",
"templates",
"stack-trace"
] |
How do I include a stacktrace in my Django 500.html page? | 121,439 | <p>I'm running <code>Django 1.0</code> and I'm close to deploying my app. As such, I'll be changing the DEBUG setting to False.</p>
<p>With that being said, I'd still like to include the stacktrace on my 500.html page when errors occur. By doing so, users can copy-and-paste the errors and easily email them to the de... | 18 | 2008-09-23T14:39:35Z | 121,487 | <p>You could call <code>sys.exc_info()</code> in a custom exception handler. But I don't recommend that. Django can send you emails for exceptions.</p>
| 1 | 2008-09-23T14:46:22Z | [
"python",
"django",
"templates",
"stack-trace"
] |
How do I include a stacktrace in my Django 500.html page? | 121,439 | <p>I'm running <code>Django 1.0</code> and I'm close to deploying my app. As such, I'll be changing the DEBUG setting to False.</p>
<p>With that being said, I'd still like to include the stacktrace on my 500.html page when errors occur. By doing so, users can copy-and-paste the errors and easily email them to the de... | 18 | 2008-09-23T14:39:35Z | 122,482 | <p>As @zacherates says, you really don't want to display a stacktrace to your users. The easiest approach to this problem is what Django does by default if you have yourself and your developers listed in the ADMINS setting with email addresses; it sends an email to everyone in that list with the full stack trace (and ... | 11 | 2008-09-23T17:32:19Z | [
"python",
"django",
"templates",
"stack-trace"
] |
How do I include a stacktrace in my Django 500.html page? | 121,439 | <p>I'm running <code>Django 1.0</code> and I'm close to deploying my app. As such, I'll be changing the DEBUG setting to False.</p>
<p>With that being said, I'd still like to include the stacktrace on my 500.html page when errors occur. By doing so, users can copy-and-paste the errors and easily email them to the de... | 18 | 2008-09-23T14:39:35Z | 17,483,769 | <p>If we want to show exceptions which are generated , on ur template(500.html) then we could write your own 500 view, grabbing the exception and passing it to your 500 template.</p>
<h2>Steps:</h2>
<h2>#.In views.py:</h2>
<pre><code>import sys,traceback
def custom_500(request):
t = loader.get_template('500.html')
... | 0 | 2013-07-05T07:50:53Z | [
"python",
"django",
"templates",
"stack-trace"
] |
How do I include a stacktrace in my Django 500.html page? | 121,439 | <p>I'm running <code>Django 1.0</code> and I'm close to deploying my app. As such, I'll be changing the DEBUG setting to False.</p>
<p>With that being said, I'd still like to include the stacktrace on my 500.html page when errors occur. By doing so, users can copy-and-paste the errors and easily email them to the de... | 18 | 2008-09-23T14:39:35Z | 35,889,521 | <p>I know this is an old question, but these days I would recommend using a service such as <a href="https://getsentry.com/welcome/" rel="nofollow">Sentry</a> to capture your errors.</p>
<p>On Django, the steps to set this up are incredibly simple. From <a href="https://docs.getsentry.com/hosted/clients/python/integra... | 0 | 2016-03-09T10:53:22Z | [
"python",
"django",
"templates",
"stack-trace"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 122,047 | <p>Python with Numeric Python:</p>
<pre><code>from numpy import *
a = random.random_integers(0, 100, 5)
b = unique(a)
</code></pre>
<p>Voilà ! Sure you could do something similar in a functional programming style but... why?</p>
| 0 | 2008-09-23T16:13:51Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 122,062 | <p>I will forgo the simplest solutions using the 'random' module since I take it that's not really what you are after. Here's what I think you are looking for in Python:</p>
<pre><code>>>> import random
>>>
>>> def getUniqueRandomNumbers(num, highest):
... seen = set()
... while len... | 2 | 2008-09-23T16:17:48Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 122,064 | <pre><code>>>> import random
>>> print random.sample(xrange(100), 5)
[61, 54, 91, 72, 85]
</code></pre>
<p>This should yield 5 unique values in the range <code>0 â 99</code>. The <code>xrange</code> object generates values as requested so no memory is used for values that aren't sampled.</p>
| 13 | 2008-09-23T16:18:19Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 122,085 | <p>I can't really read your LINQ, but I think you're trying to get 5 random numbers up to 100 and then remove duplicates.</p>
<p>Here's a solution for that:</p>
<pre><code>def random(max)
(rand * max).to_i
end
# Get 5 random numbers between 0 and 100
a = (1..5).inject([]){|acc,i| acc << random( 100)}
# Rem... | -1 | 2008-09-23T16:23:30Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 122,093 | <p>Hmm... How about (Python):</p>
<pre><code>s = set()
while len(s) <= N: s.update((random.random(),))
</code></pre>
| 3 | 2008-09-23T16:24:18Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 122,116 | <p>In Ruby:</p>
<pre><code>a = (0..100).entries.sort_by {rand}.slice! 0, 5
</code></pre>
<p><strong>Update</strong>: Here is a slightly different way:
a = (0...100).entries.sort_by{rand}[0...5]</p>
<p><strong>EDIT:</strong></p>
<p>and In Ruby 1.9 you can do this:</p>
<pre><code>Array(0..100).sample(5)
</code>... | 5 | 2008-09-23T16:27:31Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 122,121 | <p>Here's another Ruby solution:</p>
<pre><code>a = (1..5).collect { rand(100) }
a & a
</code></pre>
<p>I think, with your LINQ statement, the Distinct will remove duplicates after 5 have already been taken, so you aren't guaranteed to get 5 back. Someone can correct me if I'm wrong, though.</p>
| 2 | 2008-09-23T16:28:02Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 122,146 | <pre><code>import random
def makeRand(n):
rand = random.Random()
while 1:
yield rand.randint(0,n)
yield rand.randint(0,n)
gen = makeRand(100)
terms = [ gen.next() for n in range(5) ]
print "raw list"
print terms
print "de-duped list"
print list(set(terms))
# produces output similar to thi... | 0 | 2008-09-23T16:31:02Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 122,159 | <p>Well, first you rewrite LINQ in Python. Then your solution is a one-liner :)</p>
<pre><code>from random import randrange
def Distinct(items):
set = {}
for i in items:
if not set.has_key(i):
yield i
set[i] = 1
def Take(num, items):
for i in items:
if num > 0:... | 0 | 2008-09-23T16:32:42Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 122,188 | <p>EDIT : Ok, just for fun, a shorter and faster one (and still using iterators).</p>
<pre><code>def getRandomNumbers(max, size) :
pool = set()
return ((lambda x : pool.add(x) or x)(random.randrange(max)) for x in xrange(size) if len(a) < size)
print [x for x in gen(100, 5)]
[0, 10, 19, 51, 18]
</code></p... | 2 | 2008-09-23T16:38:45Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 122,212 | <p>Here's a transliteration from your solution to Python.</p>
<p>First, a generator that creates Random numbers. This isn't very Pythonic, but it's a good match with your sample code. </p>
<pre><code>>>> import random
>>> def getRandomNumbers( max ):
... while True:
... yield random... | 0 | 2008-09-23T16:42:27Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 122,285 | <p>Maybe this will suit your needs and look a bit more linqish:</p>
<pre><code>from numpy import random,unique
def GetRandomNumbers(total=5):
while True:
yield unique(random.random(total*2))[:total]
randomGenerator = GetRandomNumbers()
myRandomNumbers = randomGenerator.next()
</code></pre>
| 0 | 2008-09-23T16:57:09Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 123,258 | <p>Here's another python version, more closely matching the structure of your C# code. There isn't a builtin for giving distinct results, so I've added a function to do this.</p>
<pre><code>import itertools, random
def distinct(seq):
seen=set()
for item in seq:
if item not in seen:
seen.a... | 0 | 2008-09-23T19:33:19Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do I write this in Ruby/Python? Or, can you translate my LINQ to Ruby/Python? | 122,033 | <p>Yesterday, I asked <a href="http://stackoverflow.com/questions/119107/how-do-i-generate-a-list-of-n-unique-random-numbers-in-ruby">this</a> question and never really got an answer I was really happy with. I really would like to know how to generate a list of N unique random numbers using a functional language such a... | 7 | 2008-09-23T16:10:42Z | 3,793,197 | <p>In Ruby 1.9:</p>
<pre><code>Array(0..100).sample(5)
</code></pre>
| 1 | 2010-09-25T09:17:24Z | [
"python",
"ruby",
"linq",
"functional-programming"
] |
How do you translate this regular-expression idiom from Perl into Python? | 122,277 | <p>I switched from Perl to Python about a year ago and haven't looked back. There is only <i>one</i> idiom that I've ever found I can do more easily in Perl than in Python:</p>
<pre><code>if ($var =~ /foo(.+)/) {
# do something with $1
} elsif ($var =~ /bar(.+)/) {
# do something with $1
} elsif ($var =~ /baz(.+)... | 35 | 2008-09-23T16:55:18Z | 122,294 | <p>Using named groups and a dispatch table:</p>
<pre><code>r = re.compile(r'(?P<cmd>foo|bar|baz)(?P<data>.+)')
def do_foo(data):
...
def do_bar(data):
...
def do_baz(data):
...
dispatch = {
'foo': do_foo,
'bar': do_bar,
'baz': do_baz,
}
m = r.match(var)
if m:
dispatch[m.gr... | 16 | 2008-09-23T16:58:53Z | [
"python",
"regex",
"perl"
] |
How do you translate this regular-expression idiom from Perl into Python? | 122,277 | <p>I switched from Perl to Python about a year ago and haven't looked back. There is only <i>one</i> idiom that I've ever found I can do more easily in Perl than in Python:</p>
<pre><code>if ($var =~ /foo(.+)/) {
# do something with $1
} elsif ($var =~ /bar(.+)/) {
# do something with $1
} elsif ($var =~ /baz(.+)... | 35 | 2008-09-23T16:55:18Z | 122,345 | <p>Alternatively, something not using regular expressions at all:</p>
<pre><code>prefix, data = var[:3], var[3:]
if prefix == 'foo':
# do something with data
elif prefix == 'bar':
# do something with data
elif prefix == 'baz':
# do something with data
else:
# do something with var
</code></pre>
<p>Whe... | 4 | 2008-09-23T17:07:16Z | [
"python",
"regex",
"perl"
] |
How do you translate this regular-expression idiom from Perl into Python? | 122,277 | <p>I switched from Perl to Python about a year ago and haven't looked back. There is only <i>one</i> idiom that I've ever found I can do more easily in Perl than in Python:</p>
<pre><code>if ($var =~ /foo(.+)/) {
# do something with $1
} elsif ($var =~ /bar(.+)/) {
# do something with $1
} elsif ($var =~ /baz(.+)... | 35 | 2008-09-23T16:55:18Z | 122,364 | <pre><code>def find_first_match(string, *regexes):
for regex, handler in regexes:
m = re.search(regex, string):
if m:
handler(m)
return
else:
raise ValueError
find_first_match(
foo,
(r'foo(.+)', handle_foo),
(r'bar(.+)', handle_bar),
(r'baz(.+)... | 4 | 2008-09-23T17:11:09Z | [
"python",
"regex",
"perl"
] |
How do you translate this regular-expression idiom from Perl into Python? | 122,277 | <p>I switched from Perl to Python about a year ago and haven't looked back. There is only <i>one</i> idiom that I've ever found I can do more easily in Perl than in Python:</p>
<pre><code>if ($var =~ /foo(.+)/) {
# do something with $1
} elsif ($var =~ /bar(.+)/) {
# do something with $1
} elsif ($var =~ /baz(.+)... | 35 | 2008-09-23T16:55:18Z | 123,083 | <p>Yeah, it's kind of annoying. Perhaps this will work for your case. </p>
<pre><code>
import re
class ReCheck(object):
def __init__(self):
self.result = None
def check(self, pattern, text):
self.result = re.search(pattern, text)
return self.result
var = 'bar stuff'
m = ReCheck()
if... | 10 | 2008-09-23T19:04:53Z | [
"python",
"regex",
"perl"
] |
How do you translate this regular-expression idiom from Perl into Python? | 122,277 | <p>I switched from Perl to Python about a year ago and haven't looked back. There is only <i>one</i> idiom that I've ever found I can do more easily in Perl than in Python:</p>
<pre><code>if ($var =~ /foo(.+)/) {
# do something with $1
} elsif ($var =~ /bar(.+)/) {
# do something with $1
} elsif ($var =~ /baz(.+)... | 35 | 2008-09-23T16:55:18Z | 124,128 | <p>I'd suggest this, as it uses the least regex to accomplish your goal. It is still functional code, but no worse then your old Perl.</p>
<pre><code>import re
var = "barbazfoo"
m = re.search(r'(foo|bar|baz)(.+)', var)
if m.group(1) == 'foo':
print m.group(1)
# do something with m.group(1)
elif m.group(1) ==... | 9 | 2008-09-23T21:50:45Z | [
"python",
"regex",
"perl"
] |
How do you translate this regular-expression idiom from Perl into Python? | 122,277 | <p>I switched from Perl to Python about a year ago and haven't looked back. There is only <i>one</i> idiom that I've ever found I can do more easily in Perl than in Python:</p>
<pre><code>if ($var =~ /foo(.+)/) {
# do something with $1
} elsif ($var =~ /bar(.+)/) {
# do something with $1
} elsif ($var =~ /baz(.+)... | 35 | 2008-09-23T16:55:18Z | 135,720 | <pre><code>r"""
This is an extension of the re module. It stores the last successful
match object and lets you access it's methods and attributes via
this module.
This module exports the following additional functions:
expand Return the string obtained by doing backslash substitution on a
template str... | 9 | 2008-09-25T20:10:55Z | [
"python",
"regex",
"perl"
] |
How do you translate this regular-expression idiom from Perl into Python? | 122,277 | <p>I switched from Perl to Python about a year ago and haven't looked back. There is only <i>one</i> idiom that I've ever found I can do more easily in Perl than in Python:</p>
<pre><code>if ($var =~ /foo(.+)/) {
# do something with $1
} elsif ($var =~ /bar(.+)/) {
# do something with $1
} elsif ($var =~ /baz(.+)... | 35 | 2008-09-23T16:55:18Z | 1,806,345 | <p>With thanks to <a href="http://stackoverflow.com/questions/1663995/python-variable-assignment-and-if-statement">this other SO question</a>:</p>
<pre><code>import re
class DataHolder:
def __init__(self, value=None, attr_name='value'):
self._attr_name = attr_name
self.set(value)
def __call__(... | 6 | 2009-11-27T01:05:41Z | [
"python",
"regex",
"perl"
] |
How do you translate this regular-expression idiom from Perl into Python? | 122,277 | <p>I switched from Perl to Python about a year ago and haven't looked back. There is only <i>one</i> idiom that I've ever found I can do more easily in Perl than in Python:</p>
<pre><code>if ($var =~ /foo(.+)/) {
# do something with $1
} elsif ($var =~ /bar(.+)/) {
# do something with $1
} elsif ($var =~ /baz(.+)... | 35 | 2008-09-23T16:55:18Z | 2,021,009 | <p>Here's the way I solved this issue:</p>
<pre><code>matched = False;
m = re.match("regex1");
if not matched and m:
#do something
matched = True;
m = re.match("regex2");
if not matched and m:
#do something else
matched = True;
m = re.match("regex3");
if not matched and m:
#do yet something else... | 3 | 2010-01-07T14:42:19Z | [
"python",
"regex",
"perl"
] |
How do you translate this regular-expression idiom from Perl into Python? | 122,277 | <p>I switched from Perl to Python about a year ago and haven't looked back. There is only <i>one</i> idiom that I've ever found I can do more easily in Perl than in Python:</p>
<pre><code>if ($var =~ /foo(.+)/) {
# do something with $1
} elsif ($var =~ /bar(.+)/) {
# do something with $1
} elsif ($var =~ /baz(.+)... | 35 | 2008-09-23T16:55:18Z | 4,195,819 | <p>how about using a dictionary?</p>
<pre><code>match_objects = {}
if match_objects.setdefault( 'mo_foo', re_foo.search( text ) ):
# do something with match_objects[ 'mo_foo' ]
elif match_objects.setdefault( 'mo_bar', re_bar.search( text ) ):
# do something with match_objects[ 'mo_bar' ]
elif match_objects.setd... | 1 | 2010-11-16T15:33:00Z | [
"python",
"regex",
"perl"
] |
How do you translate this regular-expression idiom from Perl into Python? | 122,277 | <p>I switched from Perl to Python about a year ago and haven't looked back. There is only <i>one</i> idiom that I've ever found I can do more easily in Perl than in Python:</p>
<pre><code>if ($var =~ /foo(.+)/) {
# do something with $1
} elsif ($var =~ /bar(.+)/) {
# do something with $1
} elsif ($var =~ /baz(.+)... | 35 | 2008-09-23T16:55:18Z | 30,799,800 | <p>My solution would be:</p>
<pre><code>import re
class Found(Exception): pass
try:
for m in re.finditer('bar(.+)', var):
# Do something
raise Found
for m in re.finditer('foo(.+)', var):
# Do something else
raise Found
except Found: pass
</code></pre>
| 0 | 2015-06-12T09:33:08Z | [
"python",
"regex",
"perl"
] |
How do you translate this regular-expression idiom from Perl into Python? | 122,277 | <p>I switched from Perl to Python about a year ago and haven't looked back. There is only <i>one</i> idiom that I've ever found I can do more easily in Perl than in Python:</p>
<pre><code>if ($var =~ /foo(.+)/) {
# do something with $1
} elsif ($var =~ /bar(.+)/) {
# do something with $1
} elsif ($var =~ /baz(.+)... | 35 | 2008-09-23T16:55:18Z | 38,849,153 | <p>Expanding on the solution by Pat Notz a bit, I found it even the more elegant to:<br>
 - name the methods the same as <code>re</code> provides (e.g. <code>search</code> vs. <code>check</code>) and<br>
  - implement the necessary methods like <code>group()</code> on the holder object itself:</p>
<pre><code>class... | 0 | 2016-08-09T11:11:22Z | [
"python",
"regex",
"perl"
] |
How do I find the location of my Python site-packages directory? | 122,327 | <p>How do I find the location of my site-packages directory?</p>
| 396 | 2008-09-23T17:04:43Z | 122,340 | <p>From <a href="http://docs.djangoproject.com/en/dev/topics/install/#remove-any-old-versions-of-django">"How to Install Django" documentation</a> (though this is useful to more than just Django installation) - execute the following from the shell:</p>
<pre><code>python -c "from distutils.sysconfig import get_python_l... | 240 | 2008-09-23T17:06:59Z | [
"python",
"installation"
] |
How do I find the location of my Python site-packages directory? | 122,327 | <p>How do I find the location of my site-packages directory?</p>
| 396 | 2008-09-23T17:04:43Z | 122,360 | <pre><code>from distutils.sysconfig import get_python_lib
print get_python_lib()
</code></pre>
| 5 | 2008-09-23T17:09:54Z | [
"python",
"installation"
] |
How do I find the location of my Python site-packages directory? | 122,327 | <p>How do I find the location of my site-packages directory?</p>
| 396 | 2008-09-23T17:04:43Z | 122,377 | <p>As others have noted, <code>distutils.sysconfig</code> has the relevant settings:</p>
<pre><code>import distutils.sysconfig
print distutils.sysconfig.get_python_lib()
</code></pre>
<p>...though the default <code>site.py</code> does something a bit more crude, paraphrased below:</p>
<pre><code>import sys, os
print... | 19 | 2008-09-23T17:14:02Z | [
"python",
"installation"
] |
How do I find the location of my Python site-packages directory? | 122,327 | <p>How do I find the location of my site-packages directory?</p>
| 396 | 2008-09-23T17:04:43Z | 122,387 | <p>An additional note to the <code>get_python_lib</code> function mentioned already: on some platforms different directories are used for platform specific modules (eg: modules that require compilation). If you pass <code>plat_specific=True</code> to the function you get the site packages for platform specific package... | 7 | 2008-09-23T17:16:22Z | [
"python",
"installation"
] |
How do I find the location of my Python site-packages directory? | 122,327 | <p>How do I find the location of my site-packages directory?</p>
| 396 | 2008-09-23T17:04:43Z | 1,711,808 | <p>A side-note: The proposed solution (distutils.sysconfig.get_python_lib()) does not work when there is more than one site-packages directory (as <a href="http://pythonsimple.noucleus.net/python-install/python-site-packages-what-they-are-and-where-to-put-them">recommended by this article</a>). It will only return the ... | 11 | 2009-11-10T22:49:28Z | [
"python",
"installation"
] |
How do I find the location of my Python site-packages directory? | 122,327 | <p>How do I find the location of my site-packages directory?</p>
| 396 | 2008-09-23T17:04:43Z | 4,611,382 | <p><em>For Ubuntu</em>,</p>
<pre><code>python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
</code></pre>
<p>...is not correct.</p>
<p>It will point you to <code>/usr/lib/pythonX.X/dist-packages</code></p>
<p>This folder only contains packages your operating system has automatically in... | 85 | 2011-01-06T03:08:09Z | [
"python",
"installation"
] |
How do I find the location of my Python site-packages directory? | 122,327 | <p>How do I find the location of my site-packages directory?</p>
| 396 | 2008-09-23T17:04:43Z | 5,095,375 | <p>Let's say you have installed the package 'django'. import it and type in dir(django). It will show you, all the functions and attributes with that module. Type in the python interpreter - </p>
<pre><code>>>> import django
>>> dir(django)
['VERSION', '__builtins__', '__doc__', '__file__', '__name__... | 16 | 2011-02-23T18:32:10Z | [
"python",
"installation"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.