Python – best way to have interdependent variable in a class

Newbie Python questions: I want to model something that can have a several inter-connected instance variables and if I set one, I want to recalculate the others. Any can be set.

E.g. a circle – each instance has an area, circumference and radius (let’s ignore the diameter). If I set the area, I want the circumference & radius re-calculated and vice-versa. Should I :

class circle:     def area(self):        return cmath.pi*self.radius*self.radius     def circumference(self):       return 2 * cmath.pi * self.radius     def radius(self):       return self.circumference/(2*cmath.pi)  circle1 = circle() circle1.radius = 5 print "Circle1's area is {0} and its circumference is {1}".format(circle1.area, circle1.circumference)  

Now there’s nothing in the code sample to calculate the area once the radius has been set, so do I need @property with setters and getters for the radius, area and circumference to calculate the other attributes when this one is set or am I barking up completely the wrong tree?

Unity Nunit testing cannot find my class objects

I have just recently updated my Unity application and am having some issues porting over (any of) my unit tests. It seems like none of my general class objects are available in these tests. I have tried numerous things and after searching online I have yet to find anything. Here is my setup…

Here is my Asset folder structure. I added the “Editor” folder and “GivenA” test via the test runner.

enter image description here

Here is how things look in VS.

enter image description here

And here are the Tests.asmdef file settings

enter image description here

All of this worked perfectly previous to my Unity update, any help would be GREATLY appreciated. If more info is needed, I can supply that as well. Thanks!

Exception Handling in Class

I have read a few sites on how to handle exceptions and I think I understand it better. Here is how I plan to handle it going forward. I used a lot of the guidelines here, https://isocpp.org/wiki/faq/exceptions.

Is this a sensible to handle it with c++14 as I’m using visual studio 2015. Would it be different with C++17?

#include <iostream> #include <exception> #include <string>  #define THROWFOORTE(what) throw FooRuntimeException(what, __FILE__, __func__, __LINE__) #define THROWFOORE(what) throw FooRangeError(what, __FILE__, __func__, __LINE__)  struct FooRuntimeException     : public std::runtime_error {     FooRuntimeException(const char* what,          const char* file, const char* func, int line)         : runtime_error(std::string(file) +":" +              std::string(func) + ":" + std::to_string(line)              + " " + std::string(what)) {} };  struct FooRangeError     : public std::range_error {     FooRangeError(const char* what,         const char* file, const char* func, int line)         : range_error(std::string(file) + ":" +             std::string(func) + ":" + std::to_string(line)             + " " + std::string(what)) {} };  class Foo { public:     Foo() = default;     ~Foo() {};      void throwRunTimeException()     {         THROWFOORTE("Runtime Error");     }     void throwRangeError()     {         THROWFOORE("Range Error");     } };  void HandleException() {     try { throw; }     catch (const FooRuntimeException &e)     {         std::cerr << e.what() << '\n'; // catches standard error message     }     catch (const FooRangeError&e)     {         std::cerr << e.what() << '\n';; // catches standard error message     }     return; }  int main() {     try     {         auto foo = Foo();         foo.throwRunTimeException();     }     catch (...) {         HandleException();     }        try     {         auto foo = Foo();         foo.throwRangeError();     }     catch (...) {         HandleException();     }      return 0; } 

PHP http request class

I would like to get you opinion on this class, which is used to send http request (at that moment only POST method ).

It is working ok, but I would like to get response on : maintainability, reusability, security, code formatting, etc…

Class HttpRequest{      private $  POST = 'POST';     private $  PUT = 'PUT';     private $  GET = 'GET';     private $  DELETE = 'DELETE';     private $  PATCH = 'PATCH';      private $  body;     private $  options;     private $  handle;     private $  httpCode;     private $  response;      public function __construct(){}       /**      * send post request      * @param url       * @param header       * @param options      * @param body      * @return json object      */     public function post($  url, $  header, $  options, $  body){         if(!$  this->handle || get_resource_type($  this->handle) == "Unknown"){             $  this->handle = curl_init();          }         curl_setopt($  this->handle, CURLOPT_URL, $  url);         curl_setopt($  this->handle, CURLOPT_HTTPHEADER, $  header);         curl_setopt_array($  this->handle, $  options);         // curl_setopt($  this->handle, CURLOPT_SSL_VERIFYPEER, true);         // curl_setopt($  this->handle, CURLOPT_SSL_VERIFYHOST, 0);         curl_setopt($  this->handle, CURLOPT_RETURNTRANSFER, true);         curl_setopt($  this->handle, CURLOPT_CUSTOMREQUEST, $  this->POST);         curl_setopt($  this->handle, CURLOPT_POSTFIELDS, $  body);         $  this->response = curl_exec($  this->handle);         $  this->httpCode = curl_getinfo($  this->handle, CURLINFO_HTTP_CODE);         curl_close($  this->handle);         return $  this->response;     }       public function getResponse(){         return $  this->response;     }      public function getHttpCode(){         return $  this->httpCode;     }      /**      * send get request      * @param url       * @param header       * @param options      */     public function get($  url,$  header=array(), $  options=array()){         /**          * @todo           * implemets this method          */     }      /**      * send patch request      */      public function patch(){         /**          * @todo           * implemets this method          */     }     /**      * send delete request      */     public function delete(){         /**          * @todo           * implemets this method          */     }     /**      * send put request      */     public function put(){         /**          * @todo           * implemets this method          */     } } 

?>

How to solve a JAR conflict in Tomcat web app required for a plugin that has a dependency on a class already in WEB-INF/lib?

I built a plugin for a web app that uses tomcat. The plugin is registered as a servlet bean. Now I want to use rabbitmq with the latest amqp client lib. Which has a dependency on classes in slf4j-api-1.7.25.jar. Unfortunately the web app has also a dependency on slf4j but an older version. So adding the the new jar file crashes the web app. Is there anything to rescue? I have two dependencies out of my control.

How can I wrap other model classes in one class neatly?

I made a wrapper, and recently I’ve reflected about what is a poor design decision in my opinion, however, I’m not sure what would be the better alternative.

Currently, my wrapper works like this (names changed):

    import wrapper     api = wrapper.Api(params...)     # fetch fetches data from the API     objects = wrapper.model.fetch(api, params...)     # get_special might be only relevant to a certain object type     other_objects = wrapper.other_model.get_special(api, params...) 

Obviously, passing the API as a parameter isn’t very oop-y, and regardless it just looks bad. I considered just putting all of those classes in the API class, but I don’t think that’s a good idea, either. What would be a better design choice?

Thanks in advance 😀