SQLAlchemy - add mixin class to Automaped classes - sqlalchemy

I'm using SQLAlchemy and am generating classes dynamically for my database via the Automapping functionality.
I need to add a Mixin class with various helper methods to each of these automapped classes.
I tried to create subclasses of the automapped class with the mixin class:
db = create_engine(connection_string)
automapper = automap_base()
automapper.prepare(db, reflect=True)
for class_variable in automapper.__subclasses__():
new_class = type(class_variable.__name__, (class_variable, Mixins), {})
when I try to use these classes I get errors like:
class _ is a subclass of AutomapBase. Mappings are not produced until the .prepare() method is called on the class hierarchy.
If I call automapper.prepare() again, I get warnings like this and mostly just enters an infinite loop:
SAWarning: This declarative base already contains a class with the same class name and module name as sqlalchemy.ext.automap.payments, and will be replaced in the string-lookup table.
I cannot specify the classes explicitly as in this answer, because I don't know the database tables ahead of time.

From the docs, you can augment the Base with your Mixin class. In this case, you could pass your Mixin as the cls parameter.
automapper = automap_base(cls=Mixin)

Related

How to write a wrapper class / Override configuration in pre existing Puppet Class

I would like to override the values of another class in another Puppet Modules. Please help me by suggesting ways.
Existing Class : ( Module Name : MySQL )
class mysql::server (
$config_file = $mysql::params::config_file,
$includedir = $mysql::params::includedir)
{
My Code Logics
}
My Current Class : ( Module Name : Profiles )
Class profiles::mysql () {
class { '::mysql::server':
config_file => '/opt/arunraj',
includedir => true
}
}
When i am doing like above, I am getting duplicate class declaration error. Which is a best way to override a values between two classes
In the first place, your example code is incomplete. You present the definitions of classes mysql::server and profiles::mysql, and the latter contains a resource-style declaration of class mysql::server, but you say nothing about the one or more other declarations of class mysql::server that the given one collides with. What you actually presented is not enough to produce the error you describe.
Note also that using resource-style class declarations is usually poor form, especially for declaring public classes of any module, and most especially for declaring classes belonging to a different module than the one in which the declaration appears. The reasons are a bit technical, but to a large extent they boil down to the risk of eliciting exactly the kind of error you encountered. That happens whenever Puppet evaluates a resource-style declaration of a class for which a declaration (in any style) has already been evaluated, because class parameter values are bound as part of evaluating the first-encountered declaration.
The best way to customize class parameter values is to rely on automatic data binding (Hiera) to bind values to those parameters in the first place. If you have an oddball machine that needs different parameter values then you set them at a higher-priority level of your data hierarchy than the one from which the ordinary values come, and which is scoped narrowly enough to avoid affecting machines that should have the ordinary parameters.
Moreover, to avoid the kind of error you describe, you should also be certain everywhere to use only include-like declarations for any class that might be declared more than once (i.e. any public one, and some private ones). That goes hand in hand with automatic binding because if you don't use resource-like declarations then automatic data binding is your best available means for customizing class parameter values. The classical include-style declaration is via the include function itself, but the require, contain, and hiera_include functions also provide include-style declarations (with various differences in semantics). If you're using an ENC to declare classes then it might produce either style.

get/set methods and constructors in class diagram

recently I was assigned to develop an use case diagram and a class diagram for a conference management system. First I developed use case diagram and then class diagram. In the class diagram I have the following unclear parts:
Do we need to show get and set methods for all the private fields in every class. Or we can omit get and set methods, since it is obvious.
Do we need to show the constructors in a class? If it is not necessary, what is the reason for not showing them? I have seen lot of class diagrams without the constructors but the reason for that is beyond my understanding.
Gets and sets methods are not UML definition. It is just way how to manipulate with attribute values in some programming languages. Pure UML know attribute , its type, name and other properties.
Typical usage of getters and setters in programing is to implement readonly or derived (calculated) attributes.
You do not have to define getters and setters in uml class diagram.
Constructor:
You can define constructor operation in class of course. Constructor operation has keyword "create" at the beginning of its name. You can assign behavior definition to constructor as its method to define how to construct instance of class.
See Common Behavion in UML Superstructure.

Namespace vars between Classes

Synopsis
How do you declare variables in a namespace while using the use statement? (ie., without declaring the namespace with the variable name)
How do you reference namespace variables with the "use" statement without a container reference. (ie., trace(foo) rather than trace(a.foo) [seems kinda pointless if I have to state this after already switching to the namespace])
Explanation
Having read Grant Skinner's "Complete Guide to Using Namespaces", and other articles, such as Jackson Dustan's "Better OOP Through Namespaces", I'm left with the above unanswered questions. I feel as though I'm missing some basic principle, but I can't seem to get namespaces to work. The following examples are written for use with the Flash IDE, so assume the following...
locus.as
package com.atriace {
public namespace locus = "atriace.com";
}
testA.as
package com.atriace {
public class testA {
import com.atriace.locus;
locus var foo:String = "Apple";
public function testA() {}
}
}
testB.as
package com.atriace {
public class testB {
import com.atriace.locus;
use namespace locus;
public function testB() {
trace(foo);
}
}
}
Document Class:
import com.atriace.testA;
import com.atriace.testB;
var a:testA = new testA();
trace(a.foo); // results in "Apple"
var b:testB = new testB(); // compile error: variable "foo" not defined.
Issue #1
In my mind, a namespace is little more than an object to hold variables that has scope level access. Ergo, global is a namespace visible to all functions (since it's the root scope), local is namespace (specific to the current and child scopes), and so on. If true, then switching to a namespace with use should allow you to simply declare variables that happen to exist in both the local and custom namespaces. For example:
use namespace locus
var bar:String = "test"; // this now *should* exist in both local & locus scope/namespace.
Since I'm unaware of a method to iterate over a namespace like a normal object, I don't know whether this is what happens. Furthermore, I haven't seen any cases where someone has declared a custom namespace variable this way, so I assume namespace variables must always be explicitly defined.
Issue #2
You might ask, "what's the goal here?" Quite simply, we want a dynamic pool of variables and methods that any new classes can add to (within the same package). By switching to this namespace prior to calling methods, we can reduce the wordiness of our code. So, class.method() becomes just method().
In testB.as we'd fully expect an error to occur if we never imported the testA.as class and instantiated it; especially because foo isn't a static member of the class (nor do we want it to be). However, since we've instantiated foo at least once, the namespace locus should now have a variable called foo, which means that when testB.as gets instantiated, and the constructor seeks a value for foo, the namespace already has one.
Obviously, there's a flaw in this thinking since the Flash compiler complains that foo has never been declared, and the only way I can reference foo from the document class is by referencing the container (ie., a.foo rather than just switching to the namespace with use, and tracing foo directly).
For the sake of argument, neither inheritance nor static members are a solution to this dilema. This is both an excercise in learning better code techniques, and an answer to the structure of a large utility class that has complicated dependencies. Given the absence of a variable/method, you could simply code around it.
I know it's not a heavily documented topic, which is why I'm hoping some sage here may see what I'm missing. The help would be much appreciated. :)
"use namespace" is for the consumer side. You always have to include the namespace in any declaration:
MyNamespace var foobar : uint;
If you wish to add namespaced package-global variables (you shouldn't as a general rule), you have to define each one of them in a separate .as file as packages only allow one publicly-visible definition per file at the top-level.
In your example above you are using namespaces incorrectly. A namespace can span multiple classes, but does not achieve the cross-class functionality you are looking for. This is more the domain of aspect-oriented programming.

Is it bad to prefix all of my framework class names?

I develop a lot of frameworks for Flash games and applications. I have always prefixed my class names with a random character or two, to avoid conflict with class names that the developer may already have, for example:
class LEntity
Recently I had a co-worker blast me for poor and "annoying" naming of classes who then proceeded to rename every class in the frameworks I've created for people here to use.
I'm having trouble explaining my reasoning thoroughly enough for him to accept what I've done as a good approach.
Is what I've done above actually a bad thing? If not, how can I explain otherwise? If so, why?
Comments are asking about namespaces - I know AS3 in this example has what I know to be called a namespace but I'm not sure if this is the same thing or if it can be used as expected.
Given that Actionscript supports namespaces, there is no reason to use prefixes simply to prevent naming clashes. That's what namespaces are for.
Some people like to use namespaces to significy member variables (ie, underscore prefix, or sometimes m_) and that has some merit, but simply for the sake of name clashing no.
It sounds like you don't quite understand what namespacespackages are in AS3.
An example:
//Class1.as
package com.test.someModule { //This is the package/namespace
public class Class1 {...}
}
//Class2.as
package com.test.otherModule {
import com.test.someModule.Class1; //Class1 can be used as "Class1" now. Otherwise you would do "com.test.someModule.Class1"
import com.test.someModule.*; //You can also use the "*" to "import" all classes in that package
}
I have to agree with your co-worker, your class names are 'annoying'.
In Actionscript 3 we use the package name to define the namespace of a class. If you're not sure what namespace means, take the wikipedia definition (as of the time of writing):
"In general, a namespace is a container for a set of identifiers
(names), and allows the disambiguation of homonym identifiers residing
in different namespaces."
So you will never "conflict with class names" as long as you name your packages correctly. Most developers use what is called the reverse domain notation to name their packages (e.g com.mywebsite.MyGenericNamedClass). Domain names are unique so it's very unlikely you would clash with another class.
As a rule of thumb the class name should be as descriptive as possible, so some of your class names will be the same as someone else's class. Take the default Sprite class for instance:
import flash.display.Sprite;
import uk.co.mywebsite.Sprite;
if you then initialize an object:
var mySprite:Sprite = new Sprite();
The compiler would not know which Sprite you want to initialize (is it the flash sprite or your own custom sprite), and it would throw an error.
The solution is simple: because your packages have been named properly, all you need to do is to use the full class name including the package name to initialize your object:
var mySprite:uk.co.mywebsite.Sprite = new uk.co.mywebsite.Sprite();
var myOtherSprite:flash.display.Sprite = new flash.display.Sprite();
Mind you, you would rarely need to do that. This is only necessary if you want to use those two classes (the default Sprite and your own Sprite) in the same scope. Generally, you would only import your own class:
/* we are not importing this any more
import flash.display.Sprite;*/
//only importing my own class
import uk.co.mywebsite.Sprite;
/* now I can initialize my object without using the full class name, and the compiler knows
I mean my own Sprite class */
var mySprite:Sprite = new Sprite();

How should I define 'static' subroutines in Perl?

I'm used to work in Java, so perhaps this question is a Java-oriented Perl question... anyway, I've created a Person package using Moose.
Now, I would like to add a few subroutines which are "static", that is, they do not refer to a specific Person, but are still closely related to Person package. For example, sub sort_persons gets an array of Person objects.
In Java, I would simply declare such functions as static. But in Perl... what is the common way to do that?
p.s. I think the Perlish terminology for what I'm referring to is "class methods".
There's no such thing as a static method in Perl. Methods that apply to the entire class are conventionally called class methods. These are only distinguished from instance methods by the type of their first argument (which is a package name, not an object). Constructor methods, like new() in most Perl classes, are a common example of class methods.
If you want a particular method to be invoked as a class method only, do something like this:
sub class_method {
my ($class, #args) = #_;
die "class method invoked on object" if ref $class;
# your code
}