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 |
|---|---|---|---|---|---|---|---|---|---|
Static class variables in Python | 68,645 | <p>Is it possible to have static class variables or methods in python? What syntax is required to do this?</p>
| 1,015 | 2008-09-16T01:46:36Z | 8,201,368 | <p>You could also enforce a class to be static using metaclass.</p>
<pre><code>class StaticClassError(Exception):
pass
class StaticClass:
__metaclass__ = abc.ABCMeta
def __new__(cls, *args, **kw):
raise StaticClassError("%s is a static class and cannot be initiated."
... | 5 | 2011-11-20T12:06:23Z | [
"python",
"class",
"methods",
"static",
"class-variables"
] |
Static class variables in Python | 68,645 | <p>Is it possible to have static class variables or methods in python? What syntax is required to do this?</p>
| 1,015 | 2008-09-16T01:46:36Z | 9,613,563 | <p>One special thing to note about static properties & instance properties, shown in the example below:</p>
<pre><code>class my_cls:
my_prop = 0
#static property
print my_cls.my_prop #--> 0
#assign value to static property
my_cls.my_prop = 1
print my_cls.my_prop #--> 1
#access static property thru' i... | 6 | 2012-03-08T06:06:05Z | [
"python",
"class",
"methods",
"static",
"class-variables"
] |
Static class variables in Python | 68,645 | <p>Is it possible to have static class variables or methods in python? What syntax is required to do this?</p>
| 1,015 | 2008-09-16T01:46:36Z | 15,117,875 | <p>The best way I found is to use another class. You can create an object and then use it on other objects.</p>
<pre><code>class staticFlag:
def __init__(self):
self.__success = False
def isSuccess(self):
return self.__success
def succeed(self):
self.__success = True
class tryIt:
... | 1 | 2013-02-27T17:00:43Z | [
"python",
"class",
"methods",
"static",
"class-variables"
] |
Static class variables in Python | 68,645 | <p>Is it possible to have static class variables or methods in python? What syntax is required to do this?</p>
| 1,015 | 2008-09-16T01:46:36Z | 15,644,143 | <p>When define some member variable outside any member method, the variable can be either static or non-static depending on how the variable is expressed. </p>
<ul>
<li>CLASSNAME.var is static variable</li>
<li>INSTANCENAME.var is not static variable. </li>
<li>self.var inside class is not static variable. </li>
<li>v... | 2 | 2013-03-26T17:56:19Z | [
"python",
"class",
"methods",
"static",
"class-variables"
] |
Static class variables in Python | 68,645 | <p>Is it possible to have static class variables or methods in python? What syntax is required to do this?</p>
| 1,015 | 2008-09-16T01:46:36Z | 24,553,443 | <p>In regards to this <a href="http://stackoverflow.com/a/68672/717357">answer</a>, for a <em>constant</em> static variable, you can use a descriptor. Here's an example:</p>
<pre><code>class ConstantAttribute(object):
'''You can initialize my value but not change it.'''
def __init__(self, value):
self.... | 1 | 2014-07-03T12:14:47Z | [
"python",
"class",
"methods",
"static",
"class-variables"
] |
Static class variables in Python | 68,645 | <p>Is it possible to have static class variables or methods in python? What syntax is required to do this?</p>
| 1,015 | 2008-09-16T01:46:36Z | 27,568,860 | <h2>Static and Class Methods</h2>
<p>As the other answers have noted, static and class methods are easily accomplished using the built-in decorators:</p>
<pre><code>class Test(object):
#regular instance method:
def MyMethod(self):
pass
#class method:
@classmethod
def MyClassMethod(klass):
... | 70 | 2014-12-19T15:16:36Z | [
"python",
"class",
"methods",
"static",
"class-variables"
] |
Static class variables in Python | 68,645 | <p>Is it possible to have static class variables or methods in python? What syntax is required to do this?</p>
| 1,015 | 2008-09-16T01:46:36Z | 36,216,964 | <p>It is possible to have <code>static</code> class variables, but probably not worth the effort.</p>
<p>Here's a proof-of-concept written in Python 3 -- if any of the exact details are wrong the code can be tweaked to match just about whatever you mean by a <code>static variable</code>:</p>
<hr>
<pre><code>class St... | 1 | 2016-03-25T09:02:25Z | [
"python",
"class",
"methods",
"static",
"class-variables"
] |
Best way to open a socket in Python | 68,774 | <p>I want to open a TCP client socket in Python. Do I have to go through all the low-level BSD create-socket-handle / connect-socket stuff or is there a simpler one-line way?</p>
| 30 | 2008-09-16T02:06:11Z | 68,796 | <p>Opening sockets in python is pretty simple. You really just need something like this:</p>
<pre><code>import socket
sock = socket.socket()
sock.connect((address, port))
</code></pre>
<p>and then you can <code>send()</code> and <code>recv()</code> like any other socket</p>
| 55 | 2008-09-16T02:09:46Z | [
"python",
"networking",
"tcp"
] |
Best way to open a socket in Python | 68,774 | <p>I want to open a TCP client socket in Python. Do I have to go through all the low-level BSD create-socket-handle / connect-socket stuff or is there a simpler one-line way?</p>
| 30 | 2008-09-16T02:06:11Z | 68,892 | <p>For developing portable network programs of any sort in Python, <a href="http://twistedmatrix.com" rel="nofollow">Twisted</a> is quite useful. One of its benefits is providing a convenient layer above low-level socket APIs.</p>
| 9 | 2008-09-16T02:28:38Z | [
"python",
"networking",
"tcp"
] |
Best way to open a socket in Python | 68,774 | <p>I want to open a TCP client socket in Python. Do I have to go through all the low-level BSD create-socket-handle / connect-socket stuff or is there a simpler one-line way?</p>
| 30 | 2008-09-16T02:06:11Z | 68,911 | <p>OK, this code worked</p>
<pre><code>s = socket.socket()
s.connect((ip,port))
s.send("my request\r")
print s.recv(256)
s.close()
</code></pre>
<p>It was quite difficult to work that out from the Python socket module documentation. So I'll accept The.Anti.9's answer.</p>
| 13 | 2008-09-16T02:31:24Z | [
"python",
"networking",
"tcp"
] |
Take a screenshot via a python script. [Linux] | 69,645 | <p>I want to take a screenshot via a python script and unobtrusively save it.</p>
<p>I'm only interested in the Linux solution, and should support any X based environment.</p>
| 54 | 2008-09-16T05:44:54Z | 70,051 | <p>A short search turned up <a href="http://www.flagar.com/en/software/gtkshots" rel="nofollow">gtkShots</a> looks like it might help you, as it's a GPLed python screenshot program, so should have what you need in it.</p>
| 3 | 2008-09-16T07:24:13Z | [
"python",
"linux",
"screenshot"
] |
Take a screenshot via a python script. [Linux] | 69,645 | <p>I want to take a screenshot via a python script and unobtrusively save it.</p>
<p>I'm only interested in the Linux solution, and should support any X based environment.</p>
| 54 | 2008-09-16T05:44:54Z | 70,237 | <pre><code>import ImageGrab
img = ImageGrab.grab()
img.save('test.jpg','JPEG')
</code></pre>
<p>this requires Python Imaging Library</p>
| 7 | 2008-09-16T08:01:18Z | [
"python",
"linux",
"screenshot"
] |
Take a screenshot via a python script. [Linux] | 69,645 | <p>I want to take a screenshot via a python script and unobtrusively save it.</p>
<p>I'm only interested in the Linux solution, and should support any X based environment.</p>
| 54 | 2008-09-16T05:44:54Z | 769,221 | <p>This one works on X11, and perhaps on Windows too (someone, please check). Needs <a href="http://www.riverbankcomputing.co.uk/software/pyqt/intro">PyQt4</a>:</p>
<pre><code>import sys
from PyQt4.QtGui import QPixmap, QApplication
app = QApplication(sys.argv)
QPixmap.grabWindow(QApplication.desktop().winId()).save('... | 17 | 2009-04-20T17:12:10Z | [
"python",
"linux",
"screenshot"
] |
Take a screenshot via a python script. [Linux] | 69,645 | <p>I want to take a screenshot via a python script and unobtrusively save it.</p>
<p>I'm only interested in the Linux solution, and should support any X based environment.</p>
| 54 | 2008-09-16T05:44:54Z | 782,768 | <p>This works without having to use scrot or ImageMagick.</p>
<pre><code>import gtk.gdk
w = gtk.gdk.get_default_root_window()
sz = w.get_size()
print "The size of the window is %d x %d" % sz
pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1])
pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1... | 53 | 2009-04-23T17:27:52Z | [
"python",
"linux",
"screenshot"
] |
Take a screenshot via a python script. [Linux] | 69,645 | <p>I want to take a screenshot via a python script and unobtrusively save it.</p>
<p>I'm only interested in the Linux solution, and should support any X based environment.</p>
| 54 | 2008-09-16T05:44:54Z | 6,380,186 | <p>Cross platform solution using <a href="http://wxpython.org">wxPython</a>:</p>
<pre><code>import wx
wx.App() # Need to create an App instance before doing anything
screen = wx.ScreenDC()
size = screen.GetSize()
bmp = wx.EmptyBitmap(size[0], size[1])
mem = wx.MemoryDC(bmp)
mem.Blit(0, 0, size[0], size[1], screen, 0,... | 7 | 2011-06-17T00:33:04Z | [
"python",
"linux",
"screenshot"
] |
Take a screenshot via a python script. [Linux] | 69,645 | <p>I want to take a screenshot via a python script and unobtrusively save it.</p>
<p>I'm only interested in the Linux solution, and should support any X based environment.</p>
| 54 | 2008-09-16T05:44:54Z | 7,711,106 | <p>Compile all answers in one class.
Outputs PIL image.</p>
<pre><code>#!/usr/bin/env python
# encoding: utf-8
"""
screengrab.py
Created by Alex Snet on 2011-10-10.
Copyright (c) 2011 CodeTeam. All rights reserved.
"""
import sys
import os
import Image
class screengrab:
def __init__(self):
try:
... | 35 | 2011-10-10T09:56:22Z | [
"python",
"linux",
"screenshot"
] |
Take a screenshot via a python script. [Linux] | 69,645 | <p>I want to take a screenshot via a python script and unobtrusively save it.</p>
<p>I'm only interested in the Linux solution, and should support any X based environment.</p>
| 54 | 2008-09-16T05:44:54Z | 7,817,506 | <p>I have a wrapper project (<a href="https://github.com/ponty/pyscreenshot">pyscreenshot</a>) for scrot, imagemagick, pyqt, wx and pygtk.
If you have one of them, you can use it.
All solutions are included from this discussion.</p>
<p>Install:</p>
<pre><code>easy_install pyscreenshot
</code></pre>
<p>Example:</p>
... | 11 | 2011-10-19T06:44:20Z | [
"python",
"linux",
"screenshot"
] |
Take a screenshot via a python script. [Linux] | 69,645 | <p>I want to take a screenshot via a python script and unobtrusively save it.</p>
<p>I'm only interested in the Linux solution, and should support any X based environment.</p>
| 54 | 2008-09-16T05:44:54Z | 16,141,058 | <p>Just for completeness:
Xlib - But it's somewhat slow when capturing the whole screen:</p>
<pre><code>from Xlib import display, X
import Image #PIL
W,H = 200,200
dsp = display.Display()
root = dsp.screen().root
raw = root.get_image(0, 0, W,H, X.ZPixmap, 0xffffffff)
image = Image.fromstring("RGB", (W, H), raw.data, ... | 13 | 2013-04-22T06:52:08Z | [
"python",
"linux",
"screenshot"
] |
Take a screenshot via a python script. [Linux] | 69,645 | <p>I want to take a screenshot via a python script and unobtrusively save it.</p>
<p>I'm only interested in the Linux solution, and should support any X based environment.</p>
| 54 | 2008-09-16T05:44:54Z | 20,219,666 | <p>Try it:</p>
<pre><code>#!/usr/bin/python
import gtk.gdk
import time
import random
import socket
import fcntl
import struct
import getpass
import os
import paramiko
while 1:
# generate a random time between 120 and 300 sec
random_time = random.randrange(20,25)
# wait between 120 and 300 seconds (o... | -2 | 2013-11-26T14:23:22Z | [
"python",
"linux",
"screenshot"
] |
Take a screenshot via a python script. [Linux] | 69,645 | <p>I want to take a screenshot via a python script and unobtrusively save it.</p>
<p>I'm only interested in the Linux solution, and should support any X based environment.</p>
| 54 | 2008-09-16T05:44:54Z | 23,609,424 | <p>There is a python package for this <a href="http://www.autopy.org/" rel="nofollow">Autopy</a></p>
<p>The bitmap module can to screen grabbing (bitmap.capture_screen)
It is multiplateform (Windows, Linux, Osx).</p>
| 1 | 2014-05-12T12:44:42Z | [
"python",
"linux",
"screenshot"
] |
Take a screenshot via a python script. [Linux] | 69,645 | <p>I want to take a screenshot via a python script and unobtrusively save it.</p>
<p>I'm only interested in the Linux solution, and should support any X based environment.</p>
| 54 | 2008-09-16T05:44:54Z | 25,805,492 | <p>It's an old question. I would like to answer it using new tools.</p>
<p>Works with python 3 (should work with python 2, but I haven't test it) and PyQt5.</p>
<p>Minimal working example. Copy it to the python shell and get the result.</p>
<pre><code>from PyQt5.QtWidgets import QApplication
app = QApplication([])
s... | 0 | 2014-09-12T09:51:53Z | [
"python",
"linux",
"screenshot"
] |
Take a screenshot via a python script. [Linux] | 69,645 | <p>I want to take a screenshot via a python script and unobtrusively save it.</p>
<p>I'm only interested in the Linux solution, and should support any X based environment.</p>
| 54 | 2008-09-16T05:44:54Z | 29,136,548 | <p>From <a href="http://ubuntuforums.org/showthread.php?t=448160" rel="nofollow">this thread</a>:</p>
<pre><code> import os
os.system("import -window root temp.png")
</code></pre>
| 1 | 2015-03-19T03:05:42Z | [
"python",
"linux",
"screenshot"
] |
Take a screenshot via a python script. [Linux] | 69,645 | <p>I want to take a screenshot via a python script and unobtrusively save it.</p>
<p>I'm only interested in the Linux solution, and should support any X based environment.</p>
| 54 | 2008-09-16T05:44:54Z | 30,503,686 | <p>bit late but nevermind easy one is</p>
<pre><code>import autopy
import time
time.sleep(2)
b = autopy.bitmap.capture_screen()
b.save("C:/Users/mak/Desktop/m.png")
</code></pre>
| 2 | 2015-05-28T10:17:58Z | [
"python",
"linux",
"screenshot"
] |
Can I implement a web user authentication system in python without POST? | 69,979 | <p>My university doesn't support the POST cgi method (I know, it's crazy), and I was hoping to be able to have a system where a user can have a username and password and log in securely. Is this even possible?</p>
<p>If it's not, how would you do it with POST? Just out of curiosity.</p>
<p>Cheers!</p>
| 1 | 2008-09-16T07:07:42Z | 69,989 | <p>You could use HTTP Authentication, if supported.</p>
<p>You'd have to add SSL, as all methods, POST, GET and HTTP Auth (well, except Digest HHTP authentication) send plaintext.</p>
<p>GET is basically just like POST, it just has a limit on the amount of data you can send which is usually a lot smaller than POST an... | 1 | 2008-09-16T07:10:04Z | [
"python",
"authentication",
"cgi"
] |
Can I implement a web user authentication system in python without POST? | 69,979 | <p>My university doesn't support the POST cgi method (I know, it's crazy), and I was hoping to be able to have a system where a user can have a username and password and log in securely. Is this even possible?</p>
<p>If it's not, how would you do it with POST? Just out of curiosity.</p>
<p>Cheers!</p>
| 1 | 2008-09-16T07:07:42Z | 69,995 | <p>You can actually do it all with GET methods. However, you'll want to use a full challenge response protocol for the logins. (You can hash on the client side using javascript. You just need to send out a unique challenge each time.) You'll also want to use SSL to ensure that no one can see the strings as they go ... | 5 | 2008-09-16T07:10:40Z | [
"python",
"authentication",
"cgi"
] |
Can I implement a web user authentication system in python without POST? | 69,979 | <p>My university doesn't support the POST cgi method (I know, it's crazy), and I was hoping to be able to have a system where a user can have a username and password and log in securely. Is this even possible?</p>
<p>If it's not, how would you do it with POST? Just out of curiosity.</p>
<p>Cheers!</p>
| 1 | 2008-09-16T07:07:42Z | 70,003 | <p>With a bit of JavaScript, you could have the client hash the entered password and a server-generated nonce, and use that in an HTTP GET.</p>
| 0 | 2008-09-16T07:12:42Z | [
"python",
"authentication",
"cgi"
] |
Can I implement a web user authentication system in python without POST? | 69,979 | <p>My university doesn't support the POST cgi method (I know, it's crazy), and I was hoping to be able to have a system where a user can have a username and password and log in securely. Is this even possible?</p>
<p>If it's not, how would you do it with POST? Just out of curiosity.</p>
<p>Cheers!</p>
| 1 | 2008-09-16T07:07:42Z | 70,025 | <p>Logging in securely is very subjective. Full 'security' is not easy to achieve (if at all possible...debatable). However, you can come close. </p>
<p>If POST is not an option, maybe you can use a directory security method such as .htaccess or windows authentication depending on what system you're on.</p>
<p>Both... | -1 | 2008-09-16T07:19:07Z | [
"python",
"authentication",
"cgi"
] |
Can I implement a web user authentication system in python without POST? | 69,979 | <p>My university doesn't support the POST cgi method (I know, it's crazy), and I was hoping to be able to have a system where a user can have a username and password and log in securely. Is this even possible?</p>
<p>If it's not, how would you do it with POST? Just out of curiosity.</p>
<p>Cheers!</p>
| 1 | 2008-09-16T07:07:42Z | 70,145 | <p>A good choice: <a href="http://advosys.ca/viewpoints/2006/08/http-digest-authentication/" rel="nofollow">HTTP Digest authentication</a></p>
<p>Harder to pull off well, but an option: <a href="http://blog.paranoidferret.com/index.php/2007/07/22/secure-authentication-without-ssl-using-javascript/" rel="nofollow">Clie... | 0 | 2008-09-16T07:44:03Z | [
"python",
"authentication",
"cgi"
] |
Can I implement a web user authentication system in python without POST? | 69,979 | <p>My university doesn't support the POST cgi method (I know, it's crazy), and I was hoping to be able to have a system where a user can have a username and password and log in securely. Is this even possible?</p>
<p>If it's not, how would you do it with POST? Just out of curiosity.</p>
<p>Cheers!</p>
| 1 | 2008-09-16T07:07:42Z | 204,152 | <p>Javascript is the best option in this case.</p>
<p>Along with the request for the username and password, it sends a unique random string. You can then use a javascript md5 library to generate a hashed password, by combining the random string and the password [pwhash = md5(randomstring+password)]. The javascript th... | 0 | 2008-10-15T09:25:06Z | [
"python",
"authentication",
"cgi"
] |
Why are Python's 'private' methods not actually private? | 70,528 | <p>Python gives us the ability to create 'private' methods and variables within a class by prepending double underscores to the name, like this: <code>__myPrivateMethod()</code>. How, then, can one explain this</p>
<pre><code>>>> class MyClass:
... def myPublicMethod(self):
... print 'public method'
..... | 344 | 2008-09-16T08:59:32Z | 70,555 | <p>From <a href="http://www.faqs.org/docs/diveintopython/fileinfo_private.html">http://www.faqs.org/docs/diveintopython/fileinfo_private.html</a></p>
<blockquote>
<p>Strictly speaking, private methods are
accessible outside their class, just
not easily accessible. Nothing in
Python is truly private; internally... | 116 | 2008-09-16T09:03:45Z | [
"python",
"python-2.7",
"encapsulation",
"information-hiding"
] |
Why are Python's 'private' methods not actually private? | 70,528 | <p>Python gives us the ability to create 'private' methods and variables within a class by prepending double underscores to the name, like this: <code>__myPrivateMethod()</code>. How, then, can one explain this</p>
<pre><code>>>> class MyClass:
... def myPublicMethod(self):
... print 'public method'
..... | 344 | 2008-09-16T08:59:32Z | 70,562 | <p>It's not like you absolutly can't get around privateness of members in any language (pointer arithmetics in C++, Reflections in .NET/Java).</p>
<p>The point is that you get an error if you try to call the private method by accident. But if you want to shoot yourself in the foot, go ahead and do it.</p>
<p>Edit: Yo... | 24 | 2008-09-16T09:04:57Z | [
"python",
"python-2.7",
"encapsulation",
"information-hiding"
] |
Why are Python's 'private' methods not actually private? | 70,528 | <p>Python gives us the ability to create 'private' methods and variables within a class by prepending double underscores to the name, like this: <code>__myPrivateMethod()</code>. How, then, can one explain this</p>
<pre><code>>>> class MyClass:
... def myPublicMethod(self):
... print 'public method'
..... | 344 | 2008-09-16T08:59:32Z | 70,567 | <p>The <code>class.__stuff</code> naming convention lets the programmer know he isn't meant to access <code>__stuff</code> from outside. The name mangling makes it unlikely anyone will do it by accident.</p>
<p>True, you still can work around this, it's even easier than in other languages (which BTW also let you do th... | 9 | 2008-09-16T09:05:52Z | [
"python",
"python-2.7",
"encapsulation",
"information-hiding"
] |
Why are Python's 'private' methods not actually private? | 70,528 | <p>Python gives us the ability to create 'private' methods and variables within a class by prepending double underscores to the name, like this: <code>__myPrivateMethod()</code>. How, then, can one explain this</p>
<pre><code>>>> class MyClass:
... def myPublicMethod(self):
... print 'public method'
..... | 344 | 2008-09-16T08:59:32Z | 70,583 | <p>Its just one of those language design choices. On some level they are justified. They make it so you need to go pretty far out of your way to try and call the method, and if you really need it that badly, you must have a pretty good reason! Debugging hooks and testing come to mind as possible applications, used resp... | 10 | 2008-09-16T09:09:32Z | [
"python",
"python-2.7",
"encapsulation",
"information-hiding"
] |
Why are Python's 'private' methods not actually private? | 70,528 | <p>Python gives us the ability to create 'private' methods and variables within a class by prepending double underscores to the name, like this: <code>__myPrivateMethod()</code>. How, then, can one explain this</p>
<pre><code>>>> class MyClass:
... def myPublicMethod(self):
... print 'public method'
..... | 344 | 2008-09-16T08:59:32Z | 70,736 | <p>The phrase commonly used is "we're all consenting adults here". By prepending a single underscore (don't expose) or double underscore (hide), you're telling the user of your class that you intend the member to be 'private' in some way. However, you're trusting everyone else to behave responsibly and respect that, ... | 66 | 2008-09-16T09:33:18Z | [
"python",
"python-2.7",
"encapsulation",
"information-hiding"
] |
Why are Python's 'private' methods not actually private? | 70,528 | <p>Python gives us the ability to create 'private' methods and variables within a class by prepending double underscores to the name, like this: <code>__myPrivateMethod()</code>. How, then, can one explain this</p>
<pre><code>>>> class MyClass:
... def myPublicMethod(self):
... print 'public method'
..... | 344 | 2008-09-16T08:59:32Z | 70,900 | <p>The name scrambling is used to ensure that subclasses don't accidentally override the private methods and attributes of their superclasses. It's not designed to prevent deliberate access from outside.</p>
<p>For example:</p>
<pre><code>>>> class Foo(object):
... def __init__(self):
... self.__... | 354 | 2008-09-16T10:06:07Z | [
"python",
"python-2.7",
"encapsulation",
"information-hiding"
] |
Why are Python's 'private' methods not actually private? | 70,528 | <p>Python gives us the ability to create 'private' methods and variables within a class by prepending double underscores to the name, like this: <code>__myPrivateMethod()</code>. How, then, can one explain this</p>
<pre><code>>>> class MyClass:
... def myPublicMethod(self):
... print 'public method'
..... | 344 | 2008-09-16T08:59:32Z | 80,000 | <p>Similar behavior exists when module attribute names begin with a single underscore (e.g. _foo).</p>
<p>Module attributes named as such will not be copied into an importing module when using the <code>from*</code> method, e.g.:</p>
<pre><code>from bar import *
</code></pre>
<p>However, this is a convention and not... | 11 | 2008-09-17T04:29:45Z | [
"python",
"python-2.7",
"encapsulation",
"information-hiding"
] |
Why are Python's 'private' methods not actually private? | 70,528 | <p>Python gives us the ability to create 'private' methods and variables within a class by prepending double underscores to the name, like this: <code>__myPrivateMethod()</code>. How, then, can one explain this</p>
<pre><code>>>> class MyClass:
... def myPublicMethod(self):
... print 'public method'
..... | 344 | 2008-09-16T08:59:32Z | 1,949,874 | <p>When I first came from Java to Python i <strong>hated</strong> this. It scared me to death.</p>
<p>Today it might just be the one thing <strong>I love most</strong> about Python.</p>
<p>I love being on a platform, where people trust each other and don't feel like they need to build impenetrable walls around their ... | 100 | 2009-12-22T23:25:42Z | [
"python",
"python-2.7",
"encapsulation",
"information-hiding"
] |
Why are Python's 'private' methods not actually private? | 70,528 | <p>Python gives us the ability to create 'private' methods and variables within a class by prepending double underscores to the name, like this: <code>__myPrivateMethod()</code>. How, then, can one explain this</p>
<pre><code>>>> class MyClass:
... def myPublicMethod(self):
... print 'public method'
..... | 344 | 2008-09-16T08:59:32Z | 3,147,548 | <h3>Example of private function</h3>
<pre><code>import re
import inspect
class MyClass :
def __init__(self) :
pass
def private_function ( self ) :
try :
function_call = inspect.stack()[1][4][0].strip()
# See if the function_call has "self." in the begining
... | 185 | 2010-06-30T08:24:05Z | [
"python",
"python-2.7",
"encapsulation",
"information-hiding"
] |
Why are Python's 'private' methods not actually private? | 70,528 | <p>Python gives us the ability to create 'private' methods and variables within a class by prepending double underscores to the name, like this: <code>__myPrivateMethod()</code>. How, then, can one explain this</p>
<pre><code>>>> class MyClass:
... def myPublicMethod(self):
... print 'public method'
..... | 344 | 2008-09-16T08:59:32Z | 39,123,423 | <p>with python-3.4 this is the behaviour:</p>
<pre><code>>>> class Foo:
def __init__(self):
pass
def __privateMethod(self):
return 3
def invoke(self):
return self.__privateMethod()
>>> help(Foo)
Help on class Foo in module __ma... | 0 | 2016-08-24T12:31:44Z | [
"python",
"python-2.7",
"encapsulation",
"information-hiding"
] |
Python Authentication API | 70,653 | <p>I'm looking for a python library that will help me to create an authentication method for a desktop app I'm writing.
I have found several method in web framework such as django or turbogears.</p>
<p>I just want a kind of username-password association stored into a local file.
I can write it by myself, but I'm reall... | 5 | 2008-09-16T09:17:41Z | 70,712 | <p>I think you should make your own authentication method as you can make it fit your application best but use a library for encryption, such as <a href="http://www.pycrypto.org" rel="nofollow">pycrypto</a> or some other more lightweight library.</p>
<p>btw, if you need windows binaries for pycrypto you can get them <... | 3 | 2008-09-16T09:27:28Z | [
"python",
"authentication",
"desktop"
] |
Python Authentication API | 70,653 | <p>I'm looking for a python library that will help me to create an authentication method for a desktop app I'm writing.
I have found several method in web framework such as django or turbogears.</p>
<p>I just want a kind of username-password association stored into a local file.
I can write it by myself, but I'm reall... | 5 | 2008-09-16T09:17:41Z | 70,832 | <p>If you want simple, then use a dictionary where the keys are the usernames and the values are the passwords (encrypted with something like SHA256). <a href="http://docs.python.org/lib/module-pickle.html" rel="nofollow">Pickle</a> it to/from disk (as this is a desktop application, I'm assuming the overhead of keepin... | 0 | 2008-09-16T09:50:34Z | [
"python",
"authentication",
"desktop"
] |
Python Authentication API | 70,653 | <p>I'm looking for a python library that will help me to create an authentication method for a desktop app I'm writing.
I have found several method in web framework such as django or turbogears.</p>
<p>I just want a kind of username-password association stored into a local file.
I can write it by myself, but I'm reall... | 5 | 2008-09-16T09:17:41Z | 70,915 | <p>Treat the following as pseudo-code..</p>
<pre><code>try:
from hashlib import sha as hasher
except ImportError:
# You could probably exclude the try/except bit,
# but older Python distros dont have hashlib.
try:
import sha as hasher
except ImportError:
import md5 as hasher
def h... | 0 | 2008-09-16T10:08:57Z | [
"python",
"authentication",
"desktop"
] |
Python Authentication API | 70,653 | <p>I'm looking for a python library that will help me to create an authentication method for a desktop app I'm writing.
I have found several method in web framework such as django or turbogears.</p>
<p>I just want a kind of username-password association stored into a local file.
I can write it by myself, but I'm reall... | 5 | 2008-09-16T09:17:41Z | 80,008 | <p>dbr said:</p>
<blockquote>
<pre><code>def hash_password(password):
"""Returns the hashed version of a string
"""
return hasher.new( str(password) ).hexdigest()
</code></pre>
</blockquote>
<p>This is a really insecure way to hash passwords. You <em>don't</em> want to do this. If you want to know why rea... | 9 | 2008-09-17T04:31:02Z | [
"python",
"authentication",
"desktop"
] |
Python Authentication API | 70,653 | <p>I'm looking for a python library that will help me to create an authentication method for a desktop app I'm writing.
I have found several method in web framework such as django or turbogears.</p>
<p>I just want a kind of username-password association stored into a local file.
I can write it by myself, but I'm reall... | 5 | 2008-09-16T09:17:41Z | 1,992,484 | <pre><code>import hashlib
import random
def gen_salt():
salt_seed = str(random.getrandbits(128))
salt = hashlib.sha256(salt_seed).hexdigest()
return salt
def hash_password(password, salt):
h = hashlib.sha256()
h.update(salt)
h.update(password)
return h.hexdigest()
#in datastore
password_s... | 0 | 2010-01-02T19:05:18Z | [
"python",
"authentication",
"desktop"
] |
Python Authentication API | 70,653 | <p>I'm looking for a python library that will help me to create an authentication method for a desktop app I'm writing.
I have found several method in web framework such as django or turbogears.</p>
<p>I just want a kind of username-password association stored into a local file.
I can write it by myself, but I'm reall... | 5 | 2008-09-16T09:17:41Z | 10,495,626 | <p>Use " md5 " it's much better than base64</p>
<pre><code>>>> import md5
>>> hh = md5.new()
>>> hh.update('anoop')
>>> hh.digest
<built-in method digest of _hashlib.HASH object at 0x01FE1E40>
</code></pre>
| 0 | 2012-05-08T09:02:37Z | [
"python",
"authentication",
"desktop"
] |
Python Psycopg error and connection handling (v MySQLdb) | 70,681 | <p>Is there a way to make psycopg and postgres deal with errors without having to reestablish the connection, like MySQLdb? The commented version of the below works with MySQLdb, the comments make it work with Psycopg2:</p>
<pre><code>results = {'felicitas': 3, 'volumes': 8, 'acillevs': 1, 'mosaics': 13, 'perat\xe9':... | 2 | 2008-09-16T09:22:36Z | 70,896 | <p>I think your code looks like this at the moment:</p>
<pre><code>l = "a very long ... text".split()
for e in l:
cursor.execute("INSERT INTO yourtable (yourcol) VALUES ('" + e + "')")
</code></pre>
<p>So try to change it into something like this:</p>
<pre><code>l = "a very long ... text".split()
for e in l:
... | 2 | 2008-09-16T10:04:53Z | [
"python",
"mysql",
"psycopg2"
] |
Python Psycopg error and connection handling (v MySQLdb) | 70,681 | <p>Is there a way to make psycopg and postgres deal with errors without having to reestablish the connection, like MySQLdb? The commented version of the below works with MySQLdb, the comments make it work with Psycopg2:</p>
<pre><code>results = {'felicitas': 3, 'volumes': 8, 'acillevs': 1, 'mosaics': 13, 'perat\xe9':... | 2 | 2008-09-16T09:22:36Z | 463,862 | <p>First of all you should let psycopg do the escaping for you by passing to the execute() method the parameters instead of doing the formatting yourself with '%'. That is:</p>
<pre><code>cur.execute("insert into resultstab values (%s, %s)", (item, results[item]))
</code></pre>
<p>Note how we use "%s" as a marker eve... | 2 | 2009-01-21T01:43:57Z | [
"python",
"mysql",
"psycopg2"
] |
Python: user input and commandline arguments | 70,797 | <p>How do I have a Python script that can accept user input (assuming this is possible) and how do I make it read in arguments if run from the command line?</p>
| 222 | 2008-09-16T09:44:59Z | 70,814 | <pre><code>var = raw_input("Please enter something: ")
print "you entered", var
</code></pre>
| 296 | 2008-09-16T09:48:16Z | [
"python",
"input",
"command-line-arguments"
] |
Python: user input and commandline arguments | 70,797 | <p>How do I have a Python script that can accept user input (assuming this is possible) and how do I make it read in arguments if run from the command line?</p>
| 222 | 2008-09-16T09:44:59Z | 70,818 | <p>The best way to process command line arguments is the <a href="https://docs.python.org/library/argparse.html" rel="nofollow"><code>argparse</code> module.</a></p>
<p>Use <code>raw_input()</code> to get user input. If you import the <a href="https://docs.python.org/library/readline.html" rel="nofollow"><code>readli... | 22 | 2008-09-16T09:48:40Z | [
"python",
"input",
"command-line-arguments"
] |
Python: user input and commandline arguments | 70,797 | <p>How do I have a Python script that can accept user input (assuming this is possible) and how do I make it read in arguments if run from the command line?</p>
| 222 | 2008-09-16T09:44:59Z | 70,833 | <p>To read user input you can try <a href="http://docs.python.org/2/library/cmd.html">the cmd module</a> for easily creating a mini-command line interpreter (with help texts and autocompletion) and <a href="http://docs.python.org/2/library/functions.html#raw_input">raw_input</a> for less fancy stuff (just reading a lin... | 182 | 2008-09-16T09:50:40Z | [
"python",
"input",
"command-line-arguments"
] |
Python: user input and commandline arguments | 70,797 | <p>How do I have a Python script that can accept user input (assuming this is possible) and how do I make it read in arguments if run from the command line?</p>
| 222 | 2008-09-16T09:44:59Z | 70,841 | <p>Use 'raw_input' for input from a console/terminal.</p>
<p>if you just want a command line argument like a file name or something e.g. </p>
<pre><code>$ python my_prog.py file_name.txt
</code></pre>
<p>then you can use sys.argv...</p>
<pre><code>import sys
print sys.argv
</code></pre>
<p>sys.argv is a list where... | 5 | 2008-09-16T09:52:24Z | [
"python",
"input",
"command-line-arguments"
] |
Python: user input and commandline arguments | 70,797 | <p>How do I have a Python script that can accept user input (assuming this is possible) and how do I make it read in arguments if run from the command line?</p>
| 222 | 2008-09-16T09:44:59Z | 70,869 | <p>Careful not to use the <code>input</code> function, unless you know what you're doing. Unlike <code>raw_input</code>, <code>input</code> will accept any python expression, so it's kinda like <code>eval</code></p>
| 10 | 2008-09-16T09:58:33Z | [
"python",
"input",
"command-line-arguments"
] |
Python: user input and commandline arguments | 70,797 | <p>How do I have a Python script that can accept user input (assuming this is possible) and how do I make it read in arguments if run from the command line?</p>
| 222 | 2008-09-16T09:44:59Z | 3,427,325 | <p>As of Python <del>3.2</del> 2.7, there is now <a href="http://docs.python.org/dev/library/argparse.html" rel="nofollow">argparse</a> for processing command line arguments.</p>
| 5 | 2010-08-06T20:00:22Z | [
"python",
"input",
"command-line-arguments"
] |
Python: user input and commandline arguments | 70,797 | <p>How do I have a Python script that can accept user input (assuming this is possible) and how do I make it read in arguments if run from the command line?</p>
| 222 | 2008-09-16T09:44:59Z | 8,334,188 | <p><code>raw_input</code> is no longer available in Python 3.x. But <code>raw_input</code> was renamed <code>input</code>, so the same functionality exists.</p>
<pre><code>input_var = input("Enter something: ")
print ("you entered " + input_var)
</code></pre>
<p><a href="http://docs.python.org/py3k/whatsnew/3.0.htm... | 127 | 2011-11-30T22:53:49Z | [
"python",
"input",
"command-line-arguments"
] |
Python: user input and commandline arguments | 70,797 | <p>How do I have a Python script that can accept user input (assuming this is possible) and how do I make it read in arguments if run from the command line?</p>
| 222 | 2008-09-16T09:44:59Z | 13,089,887 | <p>If you are running Python <2.7, you need <a href="http://docs.python.org/library/optparse.html">optparse</a>, which as the doc explains will create an interface to the command line arguments that are called when your application is run.</p>
<p>However, in Python â¥2.7, optparse has been deprecated, and was repl... | 6 | 2012-10-26T15:29:14Z | [
"python",
"input",
"command-line-arguments"
] |
Python: user input and commandline arguments | 70,797 | <p>How do I have a Python script that can accept user input (assuming this is possible) and how do I make it read in arguments if run from the command line?</p>
| 222 | 2008-09-16T09:44:59Z | 30,341,035 | <p>This simple program helps you in understanding how to feed the user input from command line and to show help on passing invalid argument.</p>
<pre><code>import argparse
import sys
try:
parser = argparse.ArgumentParser()
parser.add_argument("square", help="display a square of a given number",
... | 5 | 2015-05-20T05:33:16Z | [
"python",
"input",
"command-line-arguments"
] |
HTML parser in Python | 71,151 | <p>Using the Python Documentation I found the <a href="http://docs.python.org/lib/module-HTMLParser.html">HTML parser</a> but I have no idea which library to import to use it, how do I find this out (bearing in mind it doesn't say on the page).</p>
| 7 | 2008-09-16T10:49:36Z | 71,161 | <p>Try:</p>
<pre><code>import HTMLParser
</code></pre>
<p>In Python 3.0, the HTMLParser module has been renamed to html.parser
you can check about this <a href="http://docs.python.org/library/htmlparser.html">here</a></p>
<p>Python 3.0</p>
<pre><code>import html.parser
</code></pre>
<p>Python 2.2 and above</p>
<p... | 12 | 2008-09-16T10:51:40Z | [
"python",
"import"
] |
HTML parser in Python | 71,151 | <p>Using the Python Documentation I found the <a href="http://docs.python.org/lib/module-HTMLParser.html">HTML parser</a> but I have no idea which library to import to use it, how do I find this out (bearing in mind it doesn't say on the page).</p>
| 7 | 2008-09-16T10:49:36Z | 71,168 | <p>There's a link to an example on the bottom of (<a href="http://docs.python.org/2/library/htmlparser.html" rel="nofollow">http://docs.python.org/2/library/htmlparser.html</a>) , it just doesn't work with the original python or python3. It has to be python2 as it says on the top.</p>
| 1 | 2008-09-16T10:52:39Z | [
"python",
"import"
] |
HTML parser in Python | 71,151 | <p>Using the Python Documentation I found the <a href="http://docs.python.org/lib/module-HTMLParser.html">HTML parser</a> but I have no idea which library to import to use it, how do I find this out (bearing in mind it doesn't say on the page).</p>
| 7 | 2008-09-16T10:49:36Z | 71,174 | <p>You probably really want <a href="http://stackoverflow.com/questions/55391/python-regular-expression-for-html-parsing-beautifulsoup#55424">BeautifulSoup</a>, check the link for an example. </p>
<p>But in any case</p>
<pre><code>>>> import HTMLParser
>>> h = HTMLParser.HTMLParser()
>>> h.... | 22 | 2008-09-16T10:54:05Z | [
"python",
"import"
] |
HTML parser in Python | 71,151 | <p>Using the Python Documentation I found the <a href="http://docs.python.org/lib/module-HTMLParser.html">HTML parser</a> but I have no idea which library to import to use it, how do I find this out (bearing in mind it doesn't say on the page).</p>
| 7 | 2008-09-16T10:49:36Z | 71,176 | <p>I would recommend using <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">Beautiful Soup</a> module instead and it has <a href="http://www.crummy.com/software/BeautifulSoup/documentation.html" rel="nofollow">good documentation</a>.</p>
| 4 | 2008-09-16T10:54:21Z | [
"python",
"import"
] |
HTML parser in Python | 71,151 | <p>Using the Python Documentation I found the <a href="http://docs.python.org/lib/module-HTMLParser.html">HTML parser</a> but I have no idea which library to import to use it, how do I find this out (bearing in mind it doesn't say on the page).</p>
| 7 | 2008-09-16T10:49:36Z | 71,186 | <p>For real world HTML processing I'd recommend <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a>. It is great and takes away much of the pain. Installation is easy.</p>
| 1 | 2008-09-16T10:55:20Z | [
"python",
"import"
] |
HTML parser in Python | 71,151 | <p>Using the Python Documentation I found the <a href="http://docs.python.org/lib/module-HTMLParser.html">HTML parser</a> but I have no idea which library to import to use it, how do I find this out (bearing in mind it doesn't say on the page).</p>
| 7 | 2008-09-16T10:49:36Z | 71,614 | <p>You should also look at <a href="http://code.google.com/p/html5lib/" rel="nofollow">html5lib</a> for Python as it tries to parse HTML in a way that very much resembles what web browsers do, especially when dealing with invalid HTML (which is more than 90% of today's web).</p>
| 4 | 2008-09-16T12:14:04Z | [
"python",
"import"
] |
HTML parser in Python | 71,151 | <p>Using the Python Documentation I found the <a href="http://docs.python.org/lib/module-HTMLParser.html">HTML parser</a> but I have no idea which library to import to use it, how do I find this out (bearing in mind it doesn't say on the page).</p>
| 7 | 2008-09-16T10:49:36Z | 72,100 | <p>I don't recommend BeautifulSoup if you want speed. lxml is much, much faster, and you can fall back in lxml's BS soupparser if the default parser doesn't work.</p>
| 3 | 2008-09-16T13:21:55Z | [
"python",
"import"
] |
HTML parser in Python | 71,151 | <p>Using the Python Documentation I found the <a href="http://docs.python.org/lib/module-HTMLParser.html">HTML parser</a> but I have no idea which library to import to use it, how do I find this out (bearing in mind it doesn't say on the page).</p>
| 7 | 2008-09-16T10:49:36Z | 82,117 | <p>You may be interested in <a href="http://codespeak.net/lxml/" rel="nofollow">lxml</a>. It is a separate package and has C components, but is the fastest. It has also very nice API, allowing you to easily list links in HTML documents, or list forms, sanitize HTML, and more. It also has capabilities to parse not well-... | 4 | 2008-09-17T11:19:11Z | [
"python",
"import"
] |
Using the docstring from one method to automatically overwrite that of another method | 71,817 | <p>The problem: I have a class which contains a template method <code>execute</code> which calls another method <code>_execute</code>. Subclasses are supposed to overwrite <code>_execute</code> to implement some specific functionality. This functionality should be documented in the docstring of <code>_execute</code>.
... | 2 | 2008-09-16T12:46:28Z | 72,126 | <p>Well the doc-string is stored in <code>__doc__</code> so it wouldn't be too hard to re-assign it based on the doc-string of <code>_execute</code> after the fact.</p>
<p>Basically:</p>
<p><code></p>
<pre>
class MyClass(object):
def execute(self):
'''original doc-string'''
self._execute()
class... | 0 | 2008-09-16T13:25:12Z | [
"python",
"metaclass"
] |
Using the docstring from one method to automatically overwrite that of another method | 71,817 | <p>The problem: I have a class which contains a template method <code>execute</code> which calls another method <code>_execute</code>. Subclasses are supposed to overwrite <code>_execute</code> to implement some specific functionality. This functionality should be documented in the docstring of <code>_execute</code>.
... | 2 | 2008-09-16T12:46:28Z | 72,192 | <p>Is there a reason you can't override the base class's <code>execute</code> function directly?</p>
<pre><code>class Base(object):
def execute(self):
...
class Derived(Base):
def execute(self):
"""Docstring for derived class"""
Base.execute(self)
...stuff specific to Derived..... | 2 | 2008-09-16T13:31:44Z | [
"python",
"metaclass"
] |
Using the docstring from one method to automatically overwrite that of another method | 71,817 | <p>The problem: I have a class which contains a template method <code>execute</code> which calls another method <code>_execute</code>. Subclasses are supposed to overwrite <code>_execute</code> to implement some specific functionality. This functionality should be documented in the docstring of <code>_execute</code>.
... | 2 | 2008-09-16T12:46:28Z | 72,596 | <p>Well, if you don't mind copying the original method in the subclass, you can use the following technique.</p>
<pre><code>import new
def copyfunc(func):
return new.function(func.func_code, func.func_globals, func.func_name,
func.func_defaults, func.func_closure)
class Metaclass(type):
... | 4 | 2008-09-16T14:02:38Z | [
"python",
"metaclass"
] |
Using the docstring from one method to automatically overwrite that of another method | 71,817 | <p>The problem: I have a class which contains a template method <code>execute</code> which calls another method <code>_execute</code>. Subclasses are supposed to overwrite <code>_execute</code> to implement some specific functionality. This functionality should be documented in the docstring of <code>_execute</code>.
... | 2 | 2008-09-16T12:46:28Z | 72,785 | <p>I agree that the simplest, most Pythonic way of approaching this is to simply redefine execute in your subclasses and have it call the execute method of the base class:</p>
<pre><code>class Sub(Base):
def execute(self):
"""New docstring goes here"""
return Base.execute(self)
</code></pre>
<p>Th... | 0 | 2008-09-16T14:17:43Z | [
"python",
"metaclass"
] |
Using the docstring from one method to automatically overwrite that of another method | 71,817 | <p>The problem: I have a class which contains a template method <code>execute</code> which calls another method <code>_execute</code>. Subclasses are supposed to overwrite <code>_execute</code> to implement some specific functionality. This functionality should be documented in the docstring of <code>_execute</code>.
... | 2 | 2008-09-16T12:46:28Z | 73,473 | <p>Look at the functools.wraps() decorator; it does all of this, but I don't know offhand if you can get it to run in the right context</p>
| 1 | 2008-09-16T15:19:05Z | [
"python",
"metaclass"
] |
Python and "re" | 72,393 | <p>A tutorial I have on Regex in python explains how to use the re module in python, I wanted to grab the URL out of an A tag so knowing Regex I wrote the correct expression and tested it in my regex testing app of choice and ensured it worked. When placed into python it failed.</p>
<p>After much head scratching I fou... | 6 | 2008-09-16T13:47:48Z | 72,449 | <p>Are you using the re.match() or re.search() method? My understanding is that re.match() assumes a "^" at the begining of your expression and will only search at the beginning of the text, while re.search() acts more like the Perl regular expressions and will only match the beginning of the text if you include a "^" ... | 1 | 2008-09-16T13:52:02Z | [
"python",
"regex"
] |
Python and "re" | 72,393 | <p>A tutorial I have on Regex in python explains how to use the re module in python, I wanted to grab the URL out of an A tag so knowing Regex I wrote the correct expression and tested it in my regex testing app of choice and ensured it worked. When placed into python it failed.</p>
<p>After much head scratching I fou... | 6 | 2008-09-16T13:47:48Z | 72,470 | <p>In Python, there's a distinction between "match" and "search"; match only looks for the pattern at the start of the string, and search looks for the pattern starting at any location within the string.</p>
<p><a href="http://docs.python.org/lib/module-re.html" rel="nofollow">Python regex docs</a><br />
<a href="http... | 19 | 2008-09-16T13:53:03Z | [
"python",
"regex"
] |
Python and "re" | 72,393 | <p>A tutorial I have on Regex in python explains how to use the re module in python, I wanted to grab the URL out of an A tag so knowing Regex I wrote the correct expression and tested it in my regex testing app of choice and ensured it worked. When placed into python it failed.</p>
<p>After much head scratching I fou... | 6 | 2008-09-16T13:47:48Z | 72,501 | <pre><code>>>> import re
>>> pattern = re.compile("url")
>>> string = " url"
>>> pattern.match(string)
>>> pattern.search(string)
<_sre.SRE_Match object at 0xb7f7a6e8>
</code></pre>
| 3 | 2008-09-16T13:54:56Z | [
"python",
"regex"
] |
Python and "re" | 72,393 | <p>A tutorial I have on Regex in python explains how to use the re module in python, I wanted to grab the URL out of an A tag so knowing Regex I wrote the correct expression and tested it in my regex testing app of choice and ensured it worked. When placed into python it failed.</p>
<p>After much head scratching I fou... | 6 | 2008-09-16T13:47:48Z | 72,521 | <p>You are probably being tripped up by the different methods re.search and re.match.</p>
| 1 | 2008-09-16T13:56:13Z | [
"python",
"regex"
] |
Python and "re" | 72,393 | <p>A tutorial I have on Regex in python explains how to use the re module in python, I wanted to grab the URL out of an A tag so knowing Regex I wrote the correct expression and tested it in my regex testing app of choice and ensured it worked. When placed into python it failed.</p>
<p>After much head scratching I fou... | 6 | 2008-09-16T13:47:48Z | 78,072 | <pre><code>from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(your_html)
for a in soup.findAll('a', href=True):
# do something with `a` w/ href attribute
print a['href']
</code></pre>
| 4 | 2008-09-16T22:44:42Z | [
"python",
"regex"
] |
Python's unittest logic | 72,422 | <p>Can someone explain this result to me. The first test succeeds but the second fails, although the variable tested is changed in the first test.</p>
<pre><code>>>> class MyTest(unittest.TestCase):
def setUp(self):
self.i = 1
def testA(self):
self.i = 3
self.assertEqual(self.i, 3)
... | 3 | 2008-09-16T13:50:25Z | 72,498 | <p>If I recall correctly in that test framework the setUp method is run before each test</p>
| 0 | 2008-09-16T13:54:38Z | [
"python",
"unit-testing"
] |
Python's unittest logic | 72,422 | <p>Can someone explain this result to me. The first test succeeds but the second fails, although the variable tested is changed in the first test.</p>
<pre><code>>>> class MyTest(unittest.TestCase):
def setUp(self):
self.i = 1
def testA(self):
self.i = 3
self.assertEqual(self.i, 3)
... | 3 | 2008-09-16T13:50:25Z | 72,504 | <p>From <a href="http://docs.python.org/lib/minimal-example.html">http://docs.python.org/lib/minimal-example.html</a> :</p>
<blockquote>
<p>When a setUp() method is defined, the
test runner will run that method prior
to each test.</p>
</blockquote>
<p>So setUp() gets run before both testA and testB, setting i t... | 11 | 2008-09-16T13:55:02Z | [
"python",
"unit-testing"
] |
Python's unittest logic | 72,422 | <p>Can someone explain this result to me. The first test succeeds but the second fails, although the variable tested is changed in the first test.</p>
<pre><code>>>> class MyTest(unittest.TestCase):
def setUp(self):
self.i = 1
def testA(self):
self.i = 3
self.assertEqual(self.i, 3)
... | 3 | 2008-09-16T13:50:25Z | 72,702 | <p>The setUp method, as everyone else has said, runs before every test method you write. So, when testB runs, the value of i is 1, not 3.</p>
<p>You can also use a tearDown method which runs after every test method. However if one of your tests crashes, your tearDown method will never run.</p>
| -1 | 2008-09-16T14:12:08Z | [
"python",
"unit-testing"
] |
Python's unittest logic | 72,422 | <p>Can someone explain this result to me. The first test succeeds but the second fails, although the variable tested is changed in the first test.</p>
<pre><code>>>> class MyTest(unittest.TestCase):
def setUp(self):
self.i = 1
def testA(self):
self.i = 3
self.assertEqual(self.i, 3)
... | 3 | 2008-09-16T13:50:25Z | 73,791 | <p>Each test is run using a new instance of the MyTest class. That means if you change self in one test, changes will not carry over to other tests, since self will refer to a different instance.</p>
<p>Additionally, as others have pointed out, setUp is called before each test.</p>
| 9 | 2008-09-16T15:47:30Z | [
"python",
"unit-testing"
] |
Python's unittest logic | 72,422 | <p>Can someone explain this result to me. The first test succeeds but the second fails, although the variable tested is changed in the first test.</p>
<pre><code>>>> class MyTest(unittest.TestCase):
def setUp(self):
self.i = 1
def testA(self):
self.i = 3
self.assertEqual(self.i, 3)
... | 3 | 2008-09-16T13:50:25Z | 215,576 | <p>From a methodological point of view, individual tests should be independent, otherwise it can produce more hard-to-find bugs. Imagine for instance that testA and testB would be called in a different order.</p>
| 0 | 2008-10-18T21:13:18Z | [
"python",
"unit-testing"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 72,904 | <p>Why you even need this? Why you just do not import it as</p>
<pre><code>from app.sub2 import mod2
</code></pre>
| -8 | 2008-09-16T14:27:57Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 73,009 | <p>I think that what you have to ask yourself is:</p>
<ul>
<li>Why i need to do this?</li>
<li>Is my package separation well done?</li>
</ul>
<p>I don't know the context why you want to do it this way. But for me a cleaner design would be to have the following packages structure:</p>
<pre><code>app/
__init__.py
... | -7 | 2008-09-16T14:36:25Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 73,060 | <p>Don't do relative imports. They'll only make your code more fragile. If you do an absolute import as Matej suggested, you'll be less vulnerable to changes in sys.path and changes in file locations.</p>
| -14 | 2008-09-16T14:40:26Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 73,149 | <p>Everyone seems to want to tell you what you should be doing rather than just answering the question.</p>
<p>The problem is that you're running the module as '__main__' by passing the mod1.py as an argument to the interpreter.</p>
<p>From <a href="http://www.python.org/dev/peps/pep-0328/">PEP 328</a>:</p>
<blockqu... | 189 | 2008-09-16T14:48:56Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 465,129 | <pre><code>main.py
setup.py
app/ ->
__init__.py
package_a/ ->
__init__.py
module_a.py
package_b/ ->
__init__.py
module_b.py
</code></pre>
<ol>
<li>You run <code>python main.py</code>.</li>
<li><code>main.py</code> does: <code>import app.package_a.module_a</code></li>
<l... | 82 | 2009-01-21T12:42:11Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 1,083,169 | <pre><code>def import_path(fullpath):
"""
Import a file with full path specification. Allows one to
import from anywhere, something __import__ does not do.
"""
path, filename = os.path.split(fullpath)
filename, ext = os.path.splitext(filename)
sys.path.append(path)
module = __import__(... | 24 | 2009-07-04T23:27:50Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 5,204,410 | <p>Suppose you run at the top level, then in <code>mod1</code> use:</p>
<pre><code>import sub2.mod2
</code></pre>
<p>instead of</p>
<pre><code>from ..sub2 import mod2
</code></pre>
| -4 | 2011-03-05T14:31:21Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 6,524,846 | <p>From <a href="http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports" rel="nofollow">Python doc</a>,</p>
<blockquote>
<p>In Python 2.5, you can switch importâs behaviour to absolute imports using a <code>from __future__ import absolute_import</code> directive. This absolute- import behav... | 1 | 2011-06-29T17:33:27Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 7,541,369 | <p>Take a look at <a href="http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports" rel="nofollow">http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports</a>. You could do </p>
<pre><code>from .mod1 import stuff
</code></pre>
| 4 | 2011-09-24T19:31:57Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 8,195,271 | <p>"Guido views running scripts within a package as an anti-pattern" (rejected
<a href="http://www.python.org/dev/peps/pep-3122/">PEP-3122</a>)</p>
<p>I have spent so much time trying to find a solution, reading related posts here on Stack Overflow and saying to myself "there must be a better way!". Looks like there i... | 26 | 2011-11-19T16:05:29Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 9,541,554 | <p>This is unfortunately a sys.path hack, but it works quite well.</p>
<p>I encountered this problem with another layer: I already had a module of the specified name, but it was the wrong module.</p>
<p>what I wanted to do was the following (the module I was working from was module3):</p>
<pre><code>mymodule\
__i... | 6 | 2012-03-02T22:58:47Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 9,748,770 | <p>I found it's more easy to set "PYTHONPATH" enviroment variable to the top folder:</p>
<pre><code>bash$ export PYTHONPATH=/PATH/TO/APP
</code></pre>
<p>then:</p>
<pre><code>import sub1.func1
#...more import
</code></pre>
<p>of course, PYTHONPATH is "global", but it didn't raise trouble for me yet.</p>
| -1 | 2012-03-17T09:20:09Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 12,365,065 | <p>On top of what John B said, it seems like setting the <code>__package__</code> variable should help, instead of changing <code>__main__</code> which could screw up other things. But as far as I could test, it doesn't completely work as it should.</p>
<p>I have the same problem and neither PEP 328 or 366 solve the p... | 0 | 2012-09-11T07:47:16Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 14,189,875 | <p>Let me just put this here for my own reference. I know that it is not good Python code, but I needed a script for a project I was working on and I wanted to put the script in a <code>scripts</code> directory.</p>
<pre><code>import os.path
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__fil... | 5 | 2013-01-07T04:26:52Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 15,458,607 | <p>Here is the solution which works for me:</p>
<p>I do the relative imports as <code>from ..sub2 import mod2</code>
and then, if I want to run <code>mod1.py</code> then I go to the parent directory of <code>app</code> and run the module using the python -m switch as <code>python -m app.sub1.mod1</code>.</p>
<p>The... | 67 | 2013-03-17T07:43:34Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 20,449,492 | <p>explanation of <code>nosklo's</code> answer with examples</p>
<p><em>note: all <code>__init__.py</code> files are empty.</em></p>
<pre><code>main.py
app/ ->
__init__.py
package_a/ ->
__init__.py
fun_a.py
package_b/ ->
__init__.py
fun_b.py
</code></pre>
<h3>app/pack... | 8 | 2013-12-08T03:19:42Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 30,736,316 | <p>As @EvgeniSergeev says in the comments to the OP, you can import code from a <code>.py</code> file at an arbitrary location with:</p>
<pre><code>import imp
foo = imp.load_source('module.name', '/path/to/file.py')
foo.MyClass()
</code></pre>
<p>This is taken from <a href="http://stackoverflow.com/a/67692/2071807">... | 3 | 2015-06-09T15:22:27Z | [
"python",
"python-import",
"python-module"
] |
How to do relative imports in Python? | 72,852 | <p>Imagine this directory structure:</p>
<pre><code>app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
</code></pre>
<p>I'm coding <code>mod1</code>, and I need to import something from <code>mod2</code>. How should I do it? </p>
<p>I tried <code>from ..sub2 import... | 309 | 2008-09-16T14:24:02Z | 35,338,828 | <p>This is solved 100%:</p>
<ul>
<li>app/
<ul>
<li>main.py</li>
</ul></li>
<li>settings/
<ul>
<li>local_setings.py</li>
</ul></li>
</ul>
<p>Import settings/local_setting.py in app/main.py:</p>
<p>main.py:</p>
<pre><code>import sys
sys.path.insert(0, ".settings")
try:
from settings.local_settings import *
ex... | 3 | 2016-02-11T12:01:50Z | [
"python",
"python-import",
"python-module"
] |
How do I sort a list of dictionaries by values of the dictionary in Python? | 72,899 | <p>I got a list of dictionaries and want that to be sorted by a value of that dictionary.</p>
<p>This</p>
<pre><code>[{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]
</code></pre>
<p>sorted by name, should become</p>
<pre><code>[{'name':'Bart', 'age':10}, {'name':'Homer', 'age':39}]
</code></pre>
| 822 | 2008-09-16T14:27:47Z | 72,950 | <p>You have to implement your own comparison function that will compare the dictionaries by values of name keys. See <a href="http://wiki.python.org/moin/HowTo/Sorting">Sorting Mini-HOW TO from PythonInfo Wiki</a></p>
| 7 | 2008-09-16T14:31:52Z | [
"python",
"list",
"sorting",
"dictionary"
] |
How do I sort a list of dictionaries by values of the dictionary in Python? | 72,899 | <p>I got a list of dictionaries and want that to be sorted by a value of that dictionary.</p>
<p>This</p>
<pre><code>[{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]
</code></pre>
<p>sorted by name, should become</p>
<pre><code>[{'name':'Bart', 'age':10}, {'name':'Homer', 'age':39}]
</code></pre>
| 822 | 2008-09-16T14:27:47Z | 73,019 | <p>I guess you've meant:</p>
<pre><code>[{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]
</code></pre>
<p>This would be sorted like this:</p>
<pre><code>sorted(l,cmp=lambda x,y: cmp(x['name'],y['name']))
</code></pre>
| 7 | 2008-09-16T14:36:54Z | [
"python",
"list",
"sorting",
"dictionary"
] |
How do I sort a list of dictionaries by values of the dictionary in Python? | 72,899 | <p>I got a list of dictionaries and want that to be sorted by a value of that dictionary.</p>
<p>This</p>
<pre><code>[{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]
</code></pre>
<p>sorted by name, should become</p>
<pre><code>[{'name':'Bart', 'age':10}, {'name':'Homer', 'age':39}]
</code></pre>
| 822 | 2008-09-16T14:27:47Z | 73,044 | <pre><code>my_list = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]
my_list.sort(lambda x,y : cmp(x['name'], y['name']))
</code></pre>
<p><code>my_list</code> will now be what you want.</p>
<p><strong>(3 years later) Edited to add:</strong></p>
<p>The new <code>key</code> argument is more efficient and nea... | 12 | 2008-09-16T14:39:11Z | [
"python",
"list",
"sorting",
"dictionary"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.