I used a graph package coinor.gimpy to implement some algorithms in network theory. And I want to use the graph class as one input of a function, however, I want to check whether this variable is this class or not, I don't know how to do, following are some of the code.  from coinor.gimpy import Graph     g=Graph() print type(g)   and the result is   &lt;class 'coinor.gimpy.graph.Graph'&gt;   Then inside one function, the code might be like the following,  def Dijkstra(g)     if type(g) == ??         then ...   my question is: what should I write to replace the question mark? Thanks.
4398
What is the best way to check whether a given object is of a given type? How about checking whether the object inherits from a given type?  Let's say I have an object o. How do I check whether it's a str?
78567
Can someone please explain to me how to deal with equivalence relations on classes instead of sets? Is there some sort of generalisation of relations?  Thank you
3181
When writing custom classes it is often important to allow equivalence by means of the == and != operators. In Python, this is made possible by implementing the __eq__ and __ne__ special methods, respectively. The easiest way I've found to do this is the following method:  class Foo:     def __init__(self, item):         self.item = item      def __eq__(self, other):         if isinstance(other, self.__class__):             return self.__dict__ == other.__dict__         else:             return False      def __ne__(self, other):         return not self.__eq__(other)   Do you know of more elegant means of doing this? Do you know of any particular disadvantages to using the above method of comparing __dict__s?  Note: A bit of clarification--when __eq__ and __ne__ are undefined, you'll find this behavior:  &gt;&gt;&gt; a = Foo(1) &gt;&gt;&gt; b = Foo(1) &gt;&gt;&gt; a is b False &gt;&gt;&gt; a == b False   That is, a == b evaluates to False because it really runs a is b, a test of identity (i.e., "Is a the same object as b?").  When __eq__ and __ne__ are defined, you'll find this behavior (which is the one we're after):  &gt;&gt;&gt; a = Foo(1) &gt;&gt;&gt; b = Foo(1) &gt;&gt;&gt; a is b False &gt;&gt;&gt; a == b True
7012
In Python 2.5, the following code raises a TypeError:  &gt;&gt;&gt; class X:       def a(self):         print "a"  &gt;&gt;&gt; class Y(X):       def a(self):         super(Y,self).a()         print "b"  &gt;&gt;&gt; c = Y() &gt;&gt;&gt; c.a() Traceback (most recent call last):   File "&lt;stdin&gt;", line 1, in &lt;module&gt;   File "&lt;stdin&gt;", line 3, in a TypeError: super() argument 1 must be type, not classobj   If I replace the class X with class X(object), it will work. What's the explanation for this?
66370
I'd like to map an int value pbs_errno declared as a global in the library libtorque.so using ctypes.  Currently I can load the library like so:  from ctypes import * libtorque = CDLL("libtorque.so")   and have successfully mapped a bunch of the functions. However, for error checking purposes many of them set the pbs_errno variable so I need access to that as well. However if I try to access it I get:  &gt;&gt;&gt; pytorque.libtorque.pbs_errno &lt;_FuncPtr object at 0x9fc690&gt;   Of course, it's not a function pointer and attempting to call it results in a seg fault.  It's declared as int pbs_errno; in the main header and extern int pbs_errno; in the API header files.  Objdump shows the symbol as:  00000000001294f8 g    DO .bss   0000000000000004  Base        pbs_errno
18916
I'm learning Go and I wrote this for a programming challenge. It is working (building and running) but I feel the code is not what Go code should be.  I used an OOP design. Is it correctly implemented in GO ?   In my main, I will call the method pod.playTurn(target, speed)  package main  import (     "fmt"     "math"     "os"     "strconv" ) // Dot is king type Dot struct {     x, y int } // Pod is life type Pod struct {     position                Dot     vx, vy, angle, nextCpID int     hasShieldOn             bool }  func squaredDist(a, b Dot) int {     return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) } func distance(a, b Dot) float64 {     return math.Sqrt(float64(squaredDist(a, b))) }  func (pod Pod) getAngle(p Dot) float64 {     d := distance(p, pod.position)     dx := float64(p.x-pod.position.x) / d     dy := float64(p.y-pod.position.y) / d     a := math.Acos(dx) * 180.0 / math.Pi      // If the point I want is below me, I have to shift the angle for it to be correct     if dy &lt; 0 {         a = 360.0 - a     }     return a }  func (pod Pod) diffAngle(p Dot) float64 {     a := pod.getAngle(p)     pangle := float64(pod.angle)      right := 0.0     if pangle &lt;= a {         right = a - pangle     } else {         right = 360.0 - pangle + a     }      left := 0.0     if pangle &gt;= a {         left = pangle - a     } else {         left = pangle + 360.0 - a     }      if right &lt; left {         return right     }     return -left }  func (pod Pod) rotate(p Dot) int {     a := pod.diffAngle(p)      // Can't turn more than 18° in one turn !     if a &gt; 18.0 {         a = 18.0     } else if a &lt; -18.0 {         a = -18.0     }      pod.angle += int(math.Round(a))      if pod.angle &gt;= 360.0 {         pod.angle = pod.angle - 360.0     } else if pod.angle &lt; 0.0 {         pod.angle += 360.0     }     return pod.angle }  func (pod Pod) boost(t int) (int, int) {     if pod.hasShieldOn {         return pod.vx, pod.vy     }     pangle := float64(pod.angle)     pod.vx += int(math.Round(math.Cos(pangle) * float64(t)))     pod.vy += int(math.Round(math.Sin(pangle) * float64(t)))     return pod.vx, pod.vy }  // t shoud become a float later on func (pod Pod) move(t int) (int, int) {     pod.position.x += pod.vx * t     pod.position.y += pod.vy * t     return pod.position.x, pod.position.y }  func (pod Pod) endTurn() (int, int) {     // todo rounding position if needed     pod.vx = int(float64(pod.vx) * 0.85)     pod.vy = int(float64(pod.vy) * 0.85)     return pod.vx, pod.vy }  func (pod Pod) playTurn(p Dot, t int) {     pod.angle = pod.rotate(p)     pod.vx, pod.vy = pod.boost(t)     pod.position.x, pod.position.y = pod.move(1)     pod.vx, pod.vy = pod.endTurn()     fmt.Fprintf(os.Stderr, "\nPredicted Pod position : ")     fmt.Fprintf(os.Stderr, "\n(%d, %d) speed (%d,%d)", pod.position.x, pod.position.y, pod.vx, pod.vy) }   Am I using struct correctly ? Is it acceptable object oriented code ?
84853
Comment on Duplicate Reference: Why would this be marked duplicate when it was asked years prior to the question referenced as a duplicate?  I also believe the question, detail, and response is much better than the referenced question.  I've been a C++ programmer for quite a while but I'm new to Java and new to Eclipse.  I want to use the  to visualize some data I'm working with.  This code is organized like this:  ./com ./com/touchgraph ./com/touchgraph/graphlayout ./com/touchgraph/graphlayout/Edge.java ./com/touchgraph/graphlayout/GLPanel.java ./com/touchgraph/graphlayout/graphelements ./com/touchgraph/graphlayout/graphelements/GESUtils.java ./com/touchgraph/graphlayout/graphelements/GraphEltSet.java ./com/touchgraph/graphlayout/graphelements/ImmutableGraphEltSet.java ./com/touchgraph/graphlayout/graphelements/Locality.java ./com/touchgraph/graphlayout/graphelements/TGForEachEdge.java ./com/touchgraph/graphlayout/graphelements/TGForEachNode.java ./com/touchgraph/graphlayout/graphelements/TGForEachNodePair.java ./com/touchgraph/graphlayout/graphelements/TGNodeQueue.java ./com/touchgraph/graphlayout/graphelements/VisibleLocality.java ./com/touchgraph/graphlayout/GraphLayoutApplet.java ./com/touchgraph/graphlayout/GraphListener.java ./com/touchgraph/graphlayout/interaction ./com/touchgraph/graphlayout/interaction/DragAddUI.java ./com/touchgraph/graphlayout/interaction/DragMultiselectUI.java ./com/touchgraph/graphlayout/interaction/DragNodeUI.java ./com/touchgraph/graphlayout/interaction/GLEditUI.java ./com/touchgraph/graphlayout/interaction/GLNavigateUI.java ./com/touchgraph/graphlayout/interaction/HVRotateDragUI.java ./com/touchgraph/graphlayout/interaction/HVScroll.java ./com/touchgraph/graphlayout/interaction/HyperScroll.java ./com/touchgraph/graphlayout/interaction/LocalityScroll.java ./com/touchgraph/graphlayout/interaction/RotateScroll.java ./com/touchgraph/graphlayout/interaction/TGAbstractClickUI.java ./com/touchgraph/graphlayout/interaction/TGAbstractDragUI.java ./com/touchgraph/graphlayout/interaction/TGAbstractMouseMotionUI.java ./com/touchgraph/graphlayout/interaction/TGAbstractMousePausedUI.java ./com/touchgraph/graphlayout/interaction/TGSelfDeactivatingUI.java ./com/touchgraph/graphlayout/interaction/TGUIManager.java ./com/touchgraph/graphlayout/interaction/TGUserInterface.java ./com/touchgraph/graphlayout/interaction/ZoomScroll.java ./com/touchgraph/graphlayout/LocalityUtils.java ./com/touchgraph/graphlayout/Node.java ./com/touchgraph/graphlayout/TGAbstractLens.java ./com/touchgraph/graphlayout/TGException.java ./com/touchgraph/graphlayout/TGLayout.java ./com/touchgraph/graphlayout/TGLensSet.java ./com/touchgraph/graphlayout/TGPaintListener.java ./com/touchgraph/graphlayout/TGPanel.java ./com/touchgraph/graphlayout/TGPoint2D.java ./com/touchgraph/graphlayout/TGScrollPane.java ./TG-APACHE-LICENSE.txt ./TGGL ReleaseNotes.txt ./TGGraphLayout.html ./TGGraphLayout.jar   How do I add this project in Eclipse and get it compiling and running quickly?
37905
(reference is Lawvere/Schanuel, Session 31, Ex. 1)  I'm trying to calculate the exponential object $A^A$ and its evalution map $e \colon A \times A^A \to A$ in the category of graphs, where $A$ is the "arrow graph" (ie. one arrow and two dots).  In the following, $D$ is the graph with one dot and no arrows, $1$ is the terminal object in this category (graph with one dot and one arrow, the loop).  So far I have:   The points of $\mathbf{1}\to A^A$ correspond to the maps $A\to A$ (via two standard isomorphisms), and since $\mathbf{1}$ is the loop, and there is one map of graphs $A \to A$, there is one loop in $A^A$. The dots $D\to A^A$ correspond to the maps $A \times D \to A$, of which there are four, hence four dots in $A^A$. The arrows $A \to A^A$ correspond to the maps $A \times A \to A$, of which there are four, hence four arrows in $A^A$.   But I'm stuck on how to put these together to constitute $A^A$ and its evaluation map.
49858
I'm very new to the TikZ environment. I'm trying to replace the "4" in the following graph by a Greek letter (ideally a \tau^{*}). Any idea ? Thanks in advance  \begin{document} \begin{tikzpicture}[     declare function={             gamma(\x)= 0.25*\x*\x*exp(-0.5*\x);     } ]  \begin{axis}[   no markers, domain=1:13, samples=200,   axis lines*=left, xlabel=$\tau$, ylabel=$V(\tau)$,   every axis y label/.style={at=(current axis.above origin),anchor=south},   every axis x label/.style={at=(current axis.right of origin),anchor=west},   xtick={4}, ytick=\empty,   enlargelimits=false, clip=false, axis on top,   grid = major,       xmin=0, ymin=0, ] \addplot [very thick,cyan] {gamma(x)};  \end{axis} \end{tikzpicture}  \end{document}
81778
When you visit the global search page:        and want to search for a tag like we used to do on the site search like so:     [haskell] pattern   you're directed to the error page with the Panda.  If tag search is supposed to work, can this be fixed? If tag search is not supposed to work there can you at a minimum not throw an error?
58964
I have a class with two class methods (using the classmethod() function) for getting and setting what is essentially a static variable.  I tried to use the property() function with these, but it results in an error.  I was able to reproduce the error with the following in the interpreter: class Foo(object):     _var = 5     @classmethod     def getvar(cls):         return cls._var     @classmethod     def setvar(cls, value):         cls._var = value     var = property(getvar, setvar)  I can demonstrate the class methods, but they don't work as properties: &gt;&gt;&gt; f = Foo() &gt;&gt;&gt; f.getvar() 5 &gt;&gt;&gt; f.setvar(4) &gt;&gt;&gt; f.getvar() 4 &gt;&gt;&gt; f.var Traceback (most recent call last):   File &quot;&lt;stdin&gt;&quot;, line 1, in ? TypeError: 'classmethod' object is not callable &gt;&gt;&gt; f.var=5 Traceback (most recent call last):   File &quot;&lt;stdin&gt;&quot;, line 1, in ? TypeError: 'classmethod' object is not callable  Is it possible to use the property() function with @classmethod decorated functions?
29326
Here is my test code and picture:  import bpy   # Simple property reading/writing from ID properties. # This is what the RNA would do internally. def get_float(self):     return self["testprop"]   def set_float(self, value):     self["testprop"] = value  #bpy.types.Scene.test_float = bpy.props.FloatProperty(get=get_float,set=set_float) bpy.types.Scene.test_float = bpy.props.FloatProperty()   bpy.context.scene.test_float = 12.34     When I don't explicitly set the "get/set" function for my property, after assigning the value, I can get the property value by 'scene["test_float"]', but if I explicitly set the "get/set" function, then if I try to type in the same commands, I will receive a error "KeyError: 'bpy_struct[key]: key "test_float" not found'" . So what should I do to get the same results by using explicitly  "get/set" function?
88690
I am struggling to understand how option processing works in classes. As a MWE  foo.tex:  \documentclass[draft,figures]{foo} \begin{document}   \includegraphics{example-image} \end{document}   foo.cls:  \ProvidesClass{foo} \RequirePackage{foo} \LoadClassWithOptions{article} \RequirePackage{graphicx}   foo.sty  \ProvidesPackage{foo} \RequirePackage{etoolbox} \newbool{foo@figures} \DeclareOption{figures}{\booltrue{foo@figures}} \ProcessOptions\relax \ifbool{foo@figures}{\PassOptionsToPackage{final}{graphicx}}{}   When I process foo.tex with pdflatex the log shows     LaTeX Warning: Unused global option(s):       [figures].   but I also see the graphic, which means that I am passing the final option to the graphicx package. Why does LaTeX think the figures option is unused?  Moving \LoadClassWithOptions to before the package is loaded gets rid of the warning. In my real use case, the class that is being loaded, loads the graphicx package and therefore I need to load the package before loading the class.
3567
Given the following code, what does the if __name__ == "__main__": do?  # Threading example import time, thread  def myfunction(string, sleeptime, lock, *args):     while True:         lock.acquire()         time.sleep(sleeptime)         lock.release()         time.sleep(sleeptime)  if __name__ == "__main__":     lock = thread.allocate_lock()     thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))     thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
89893
The __debug__ variable is handy in part because it affects every module. If I want to create another variable that works the same way, how would I do it?  The variable (let's be original and call it 'foo') doesn't have to be truly global, in the sense that if I change foo in one module, it is updated in others. I'd be fine if I could set foo before importing other modules and then they would see the same value for it.
46642
Let's say I have a class that has a member called data which is a list.    I want to be able to initialize the class with, for example, a filename (which contains data to initialize the list) or with an actual list.  What's your technique for doing this?  Do you just check the type by looking at __class__?  Is there some trick I might be missing?  I'm used to C++ where overloading by argument type is easy.
106335
What does the following mean?  Class.Function(variable := 1 + 1)   What is this operator called, and what does it do?