Exposing boost::gregorian::date to Python using Boost.Python - boost-python

I'd like to make boost::gregorian::date available to Python using Boost.Python. But how do I create a decent __str__ function when one is not available on the Boost date class? I'd like to write it like this:
BOOST_PYTHON_MODULE(mymodule)
{
class_<boost::gregorian::date>("Date")
.add_property("year", &boost::gregorian::date::year)
.add_property("month", &boost::gregorian::date::month)
.def("__str__", ???)
;
}

After some research I found the answer. You can supply static functions to .def as well. Just give it to_iso_extended_string and it gets the object as first argument.
BOOST_PYTHON_MODULE(mymodule)
{
class_<boost::gregorian::date>("Date")
.add_property("year", &boost::gregorian::date::year)
.add_property("month", &boost::gregorian::date::month)
.def("__str__", &to_iso_extended_string)
;
}

Related

Kotlin creating a Word class for an 8-bit emulator

I'm looking for help with a resurrection of a 6502 emulator I wrote in Java many moons ago and now converting to Kotlin. Yes, there's lot out there, but this is my implementation so I could learn how to create emulators and now, how to use Kotlin.
I require a Word class for addresses, i.e.:
class Word(initValue: Int = 0x0000) {
var value = initValue
get() = field
set(newValue) {
field = newValue and 0xFFFF
}
}
I can't extend Int, thus I assume I have an internal copy inside my class (if there's a better way, I'd love to hear it).
Using this:
val address = Word()
Is trivial and I can use it with lots of address.value += 123 to move to another location. Further to this, I can add functions to perform Add, Inc, Dec etc.
However, is there a way I can modify the class so I can:
address += 123
Directly?
I'm not sure how or what the approach is for this? I'd prefer NOT to have a lot of:
address.add(123) or address.value += 123
in my emulator.
Any advice would be really appreciated.
Unlike Java, Kotlin allows for operator overloading.
Find the documentation here
From the documentation you can use operator keyword to create overloaded function
data class Counter(val dayIndex: Int) {
operator fun plus(increment: Int): Counter {
return Counter(dayIndex + increment)
}
}

AngelScript - Avoid implicit default constructor from running

I'm currently testing some simple AngelScript stuff, and noticed something I find a bit strange when it comes to how objects are initialized from classes.
Let's say I define a class like this:
class MyClass {
int i;
MyClass(int i) {
this.i = i;
}
}
I can create an object of this class by doing this:
MyClass obj = MyClass(5);
However it seems I can also create an object by doing this:
MyClass obj;
The problem here is that obj.i becomes a default value as it is undefined.
Additionally, adding a default constructor to my class and a print function call in each one reveals that when I do MyClass obj = MyClass(5); BOTH constructors are called, not just the one with the matching parameter. This seems risky to me, as it could initialize a lot of properties unnecessarily for this "ghost" instance.
I can avoid this double-initialization by using a handle, but this seems more like a work-around rather than a solution:
MyClass# obj = MyClass(5);
So my question sums up to:
Can I require a specific constructor to be called?
Can I prevent a default constructor from running?
What's the proper way to deal with required parameters when creating objects?
Mind that this is purely in the AngelScript script language, completely separate from the C++ code of the host application. The host is from 2010 and is not open-source, and my knowledge of their implementation is very limited, so if the issue lies there, I can't change it.
In order to declare class and send the value you choose to constructor try:
MyClass obj(5);
To prevent using default constructor create it and use:
.
MyClass()
{
abort("Trying to create uninitialized object of type that require init parameters");
}
or
{
exit(1);
}
or
{
assert(1>2,"Trying to create uninitialized object of type that require init parameters");
}
or
{
engine.Exit();
}
in case that any of those is working in you environment.
declaring the constructor as private seems not to work in AS, unlike other languages.

How to initialize a static dictionary property in a Powershell class?

Is there any way for a static property in a Powershell class to contain a generic dictionary? Without the initialisation syntax that exists in C#, I haven't found a way to do it.
enum Environment
{
Production
QA
Dev
}
class Config
{
# How to get this populated?
static [System.Collections.Generic.Dictionary[Environment, hashtable]] $EnvData
}
Potential workarounds:
Add-Type and a C# class
A GetEnvData() method
Is there a better way I haven't thought of?
Just like in C#, you can use the static constructor (which is what initializers are syntactic sugar for, anyway):
class Config
{
static [System.Collections.Generic.Dictionary[Environment, hashtable]] $EnvData
static Config() {
$d = New-Object ([System.Collections.Generic.Dictionary[Environment, hashtable]])
$d["Production"] = #{Setting="Foo"}
[Config]::EnvData = $d
}
}
The use of a local is not strictly required, but I sleep better knowing the initialization is atomic.
You can also use an initializer ($EnvData = ...) but that's a little tricky in this case, since creating a generic Dictionary in one statement is, well, awkward, and the class syntax doesn't like it if you get complicated (pipes, nested function calls). You could still, if you so wanted, split off initialization into a separate hidden static function and call that for initialization (... $EnvData = [Config]::initialEnvData()), which may be more readable than one big constructor if you've got many conceptually independent properties.

Is there a way to add macro definition to MonoDevelop?

I need a special keyword in my code to be replaced by a consequence of symbols before the build.
For example, I want hello to be replaced by Debug.Log("Hello");
According to this, MonoDevelop didn't have this feature in 2006. Has anything changed?
If no, are there any plugins/external tools implementing it?
I don't think that switching to another IDE will be helpful unless it can use code completion for unity3d.
Please, don't answer that macro definitions are evil.
Update:
I understood that my example was too abstract. In fact, I want to replace read("name"); with
var name;
name=gameObject.Find("name");
if(!name)
return;
name=name.param;
1)Every script should have all needed variables declared + a variable called "self".
2)They should be public.
3)
public static function set_var(target,name:String,value)
{
var fi=typeof(target).GetField(name);
fi.SetValue(target,value);
}
public static function read(name:String,target):String
{
set_var(target,"self",target);
return "var rtmp=self.gameObject.GetComponent(\""+name+"\");"+"if(!rtmp)return;"+name+"=rtmp.param;";
}
4)eval(read("name",this));
5)As far as I know, it wouldn't work in unity C#
6)Probably, set_var can be replaced by assignment
Far better solution:
var component_names = ["hello","thing","foo"];
var component;
for(var name:String in component_names)
{
component = gameObject.GetComponent(name);
if(!component)
return;
set_var(this,name,component.param);
}
(Requires set_var() from the first one)

Extending a class in another file

I have some TypeScript code that is being generated by a tool. I'd like to extend this class in another file. As of 0.9.1.1, what's the best way to go about this?
I thought maybe I could staple my additional functions onto the prototype, but this is giving various errors (which change depending what mood the compiler is in).
For example:
Foo.ts (generated by a tool)
module MyModule {
export class Dog { }
}
Bar.ts
module MyModule {
function bark(): string {return 'woof';}
Dog.prototype.bark = bark;
}
You cannot split a class definition between multiple files in TypeScript. However typescript understands how JavaScript works and will let you write idomatic JavaScript classes just fine:
module MyModule {
export function Dog(){};
}
module MyModule {
function bark(): string {return 'woof';}
Dog.prototype.bark = bark;
}
Try it online
One way around this is to use inheritance:
class BigDog extends Dog{
bark(){}
}
I have encountered your problem as well before, but I had some deeper problems. You can see from basarat's example, that simple functions can be added as an extension to the prototype, but when it comes to static functions, or other static values you might want to extend your (presumably third party) class, then the TSC will warn you, that there is no such method defined on the class statically.
My workaround was the following little hack:
module MyModule {
export function Dog(){};
}
// in the other file
if (typeof MyModule !== 'undefined'){
Cast<any>(MyModule.Dog).Create = ()=>{return new Dog();};
}
// where Cast is a hack, for TS to forcefully cast types :)
Cast<T>(element:any):T{ return element; }
This should cast MyModule.Dog, to an any object, therefore allowing attachment of any kinds of properties, functions.