Graphene Enums not working when using without creating a new class - django

From the documentation, it's evident that we are not bound to create new classes for using Enums.
I have the following code snippet:
from graphene import Enum, InputObjectType
GRAPH_TYPES = [
('step', 'Step graph'),
('bar', 'Bar graph'),
('line', 'Line graph'),
('dot', 'Dot graph'),
]
class DataType(Enum):
VELOCITY = 'velocity'
ACCELERATION = 'accelration'
class SomeInput(InputObjectType):
data_type = DataType('DataTypeEnum')
graph_type = Enum('GraphTypeEnum', GRAPH_TYPES)
When I head over to GraphiQL, I am able to see SomeInput but graph_type is missing inside.
Package versions:
graphene-django==2.12.1
graphene==2.1.8

For anyone who stumbles upon this, it turns out to be something related to the initialization of the declared Enum.
The inline declaration Enum('GraphTypeEnum', GRAPH_TYPES) has to be updated like this:
Enum('GraphTypeEnum', GRAPH_TYPES)().

Related

React Native SectionList has incomprehensible flow type error

If have this simple SectionList definition in my code:
const s = (
<SectionList
renderItem={({ item }) => <Text>abc</Text>}
renderSectionHeader={({ section }) => <Text>abc</Text>}
sections={[{ data: [1, 2, 3], title: 'abc' }]}
/>
);
And flow generates this error message which refers to the whole "tag block" (it is actually copy pasted from VSCode):
[flow] props of React element `SectionList` (This type is incompatible with See also: React element `SectionList`)
What is happening here?
EDIT I am using
flow-bin: 0.56.0
react: 16.0.0
react-native: 0.49.1
EDIT2 So the example can be reduced to this simple line (without impacting the error message):
<SectionList sections={[]} />;
EDIT3 I just discovered that flow complains about several types that are defined in the React Native library (mainly about missing type arguments for generic types). I am wondering if I should use an older flow-bin version. Is there a compatibility table for React Native and flow?
I had a similar problem with react-native: 0.49.3 and flow-bin: 0.53.0. After checking the type definitions from the SectionList source, got it to work without type warnings as follows:
type Row = {
name: string,
}
type Section = {
title: string,
data: $ReadOnlyArray<Row>,
}
const mySections: $ReadOnlyArray<Section> = [...] // your data here
<SectionList sections={mySections} />
So the key for me was to use $ReadOnlyArray<T> instead of Array<T>. Perhaps this helps you!
React has a built in Flow type for sections called SectionBase:
import type { SectionBase } from 'react-native/Libraries/Lists/SectionList'
type Section = SectionBase<DataType>
So based on what Martin answered you would write: SectionBase<Row>
The OP's code should have gotten no complaints from Flow as it is valid.
I had a related issue in that I was using a state that held sections and I could not find the right type for Flow. I could not use SectionBase directly since it did not have the extra title property I needed. I solved it by using an intersection type:
type Section = { title: string } & SectionBase<ScheduleChange>
type State { sections: Section[] }
...
<SectionList
sections = this.state.sections
...
/>
interface SectionDataModel {
...
}
const [sections, setSections] = useState<ReadonlyArray<SectionDataModel> | []>([])
This worked for me

Unknown database type enum requested,Doctrine

when i want to generate Entity from data base i have this Error:
Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it
how can i resolve this issue.
Thanks in advance
You could try to do something like this in the onBootstrap module of your Module.php, to tell Doctrine to treat your enum like a string
$em = $e->getApplication()->getServiceManager()->get('Doctrine\ORM\EntityManager');
$platform = $em->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
Add the following line to your bootstrap.php
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
If you really want to work with enums and don't convert them to strings, you should implement your custom type (it's really not a big deal).
See enter link description here
But also, you have to extend list of types on your platform.
So, simplest way to do that - override useless method \Doctrine\DBAL\Types\Type::getMappedDatabaseTypes with your own so like that:
class EnumType extends Type
{
const NAME = "enum";
// ... (your implemented methods)
public function getMappedDatabaseTypes(AbstractPlatform $platform)
{
return ['enum'];
}
}
Have a fun :)

How can I convert a C++ enum to ctypes.Structure using Python 2.7.2?

I have searched and searched, but I haven't found an example that does what I need to do.
I found
How can I represent an 'Enum' in Python?
here on SO, but it doesn't cover ctypes.Structure.
I also found
Using enums in ctypes.Structure
here on SO, but it includes pointers, which I am not familiar with.
I have a header file that includes typedef enum, that I need to use in a ctypes.Structure in a Python file.
C++ header file:
typedef enum {
ID_UNUSED,
ID_DEVICE_NAME,
ID_SCSI,
ID_DEVICE_NUM,
} id_type_et;
Python file (The way I am currently doing it):
class IdTypeEt(ctypes.Structure):
_pack_ = 1
_fields_ = [ ("ID_UNUSED", ctypes.c_int32),
("ID_DEVICE_NAME", ctypes.c_char*64),
("ID_SCSI", ctypes.c_int32),
("ID_DEVICE_NUM", ctypes.c_int32) ]
Any advice would be greatly appreciated. The simpler, the better.
An enum is not a structure, it's an integral type with a pre-defined set of values (the enumerator constants). It doesn't make sense to represent it with ctypes.Structure. You're looking for something like this:
from ctypes import c_int
id_type_et = c_int
ID_UNUSED = id_type_et(0)
ID_DEVICE_NAME = id_type_et(1)
ID_SCSI = id_type_et(2)
ID_DEVICE_NUM = id_type_et(3)

Inspect Ember.js: Get the type of an object (Class)?

I use console.log() a lot, especially in combination with Ember.inspect(). But there's one thing I miss:
How can I find out the type of an object (Class)?
For example: Getting something like <Sandbox.ApplicationController:ember288> when inspecting Ember.get("controller")?
If you just want the model name (for example app/models/comment.js has the model name comment), you can use thing.constructor.modelName.
For example:
var aComment = this.get('store').createRecord('comment');
aComment.get('constructor.modelName') // => 'comment'
I understand you are looking for a string for debugging purposes, but I originally came to this question wanting to know specifically how to get the type of the object, not a string describing the object.
Using the built in Javascript property constructor will yield the class used to construct the instance. For example you could do:
person = App.Person.create();
person.constructor // returns App.Person
person.constructor.toString() // return "App.Person"
If you get Class, you can usually call toString() (or as a shortcut concat an empty string + '') to get something like <Sandbox.ApplicationController:ember288>
Another useful feature (in chrome) is the dir command.
dir(App.User)
This will give you the full object information, rather than just the name.
Be aware that some of these answers suggested here only work in development. Once your code is in production most of those methods / class names will get minified.
import Model from '#ember-data/model';
export default class Animal extends Model {
// ...
}
So in development:
const model = this.store.createRecord('animal');
model.constructor.name // returns Animal
in production:
const model = this.store.createRecord('animal');
model.constructor.name // returns 'i' (or any other single letter).
To avoid this, use constructor.toString()
const model = this.store.createRecord('animal');
model.constructor.toString() // returns 'model:animal'

Pyamf register_class not mapping strongly typed objects as expected

I'm using Pyamf as my backend for my Flex app and I'm seeing some weird problems with the mapping of the stongly typed classes.
Here is the model that I'm returning
class MilestonActBase(RewardActBase):
def __unicode__(self):
return self.milestone.title
class Meta:
abstract = True
class SouvenirAct(MilestonActBase):
souvenir = models.ForeignKey(Souvenir)
group = models.ForeignKey(Group, blank=True, null=True)
def __unicode__(self):
return self.souvenir.title
Here is my method that returns the objects in my views.py:
try:
pyamf.register_class(Souvenir, 'com.rain.dennys.services.vo.Souvenir')
pyamf.register_class(SouvenirAct, 'com.rain.dennys.services.vo.SouvenirAct')
except ValueError:
print "Classes already registered"
#login_required
def get_souvenir_acts(http_request):
user = http_request.user
souvenirActs = SouvenirAct.objects.filter(user=user)
return souvenirActs
Here is my AS3 class:
package com.rain.dennys.model
{
[RemoteClass (alias="com.rain.dennys.services.vo.SouvenirAct")]
[Bindable]
public class SouvenirAct extends RewardActBase
{
public var souvenir:Souvenir;
public function SouvenirAct()
{
}
}
}
When I call the service, I get back and array of anonymous objects, even though I've done the register_class in python and RemoteClass in Flex. So that doesn't make sense to me. I must be doing something wrong?
In playing around with it, I've tried a few different things. One thing that kinda worked was to iterate on the array in Flex and cast the items as SouvenirAct objects like so:
private function onResult(r:Array):void
{
for each(var o:Object in r)
{
var c:SouvenirAct = o as SouvenirAct;
}
}
When I do that in Flex, I get my SouvenirAct objects are typed as they should be, BUT then the child souvenir objects are all null. So when I force the casting of the SouvenirAct objects in the return result, I get null for the child properties that are strongly typed.
Has anyone see this before? Is there a different way I should be mapping classes?
So I'm now pretty certain the problem was with the netConnection class. I switched it out so I could use RemoteObject, and now everything works exactly as expected.
This is how I was connecting:
netConnection.connect("http://127.0.0.1:8000/gateway/");
netConnection.addEventListener(NetStatusEvent.NET_STATUS, onError);
var responder:Responder = new Responder(onResult, handleFault);
Then I switched to what is described here: http://www.adobe.com/devnet/flex/articles/flex_django.html If anyone else runs into this, and you are using netConnection, my advice is to go with RemoteObject
Okay, so this is kind of a guess but this has stung me a few times. Have you ever instantiated an instance of Souvenir anywhere in your flex application? If not... AS did not bother to compile it and you'll get anonymous objects back.
When you do your onResult looping block of code, it works because you're instantiating an object of SouvenirAct, but never instantiating a Souvenir (child), so it's still null because ActionScript never compiled it...Try this before your service call
//TODO: remove me later
var imjustheretocompile:Souvenir = new Souvenir();
var alsoCompileMetoo:SouvenirAct = new SouvenirAct();
Now since you've created an instance of SouvenirAct, it should actually be compiled into your app. This is usually never a problem since we presume you will be using that class at some point, then you can go back and remove the imjustheretocompile and alsoCompileMetoo variables.