$这在回调函数中($this in callback function)
我想知道为什么这会起作用:
class Foo { public function doSomethingFunny($subject) { preg_replace_callback( "#pattern#", array($this, 'doX'), $subject ); } private function doX() { echo 'why does this work?'; } }
为什么回调仍在$ this的上下文中? 我期望它只允许公共方法。 我错过了回调工作的基本原理。
I would like to know why this works:
class Foo { public function doSomethingFunny($subject) { preg_replace_callback( "#pattern#", array($this, 'doX'), $subject ); } private function doX() { echo 'why does this work?'; } }
Why is the callback still within the context of $this? I would expect it the allow only public methods. I'm missing something fundamental in how the callback works.
原文:https://stackoverflow.com/questions/5109648
更新时间:2023-11-28 09:11
最满意答案
你需要创建你自己的转换器。 尝试这样的事情:
import uuid from flask_wiki.backend.models import Page from flask_wiki.backend.backend import marsh from marshmallow_sqlalchemy.convert import ModelConverter from flask_wiki.backend.custom_fields import GUIDField from marshmallow import fields #Here you are overwriting the list of types of the SQLAlchemy, adding your custom type class GUIDConverter(ModelConverter): SQLA_TYPE_MAPPING = dict( list(ModelConverter.SQLA_TYPE_MAPPING.items()) + [(GUIDField, fields.Str)] ) class GUIDSerializationField(fields.Field): def _serialize(self, value, attr, obj): if value is None: return value else: if isinstance(value, uuid.UUID): return str(value) else: return None class PageSchema(marsh.ModelSchema): class Meta: model = Page model_converter = GUIDConverter #Tell to Marshmallow to use your custom converter for this model guid = GUIDSerializationField(attribute="guid")
你也可以看到这个链接寻求帮助。 我希望它对我的坏英语有帮助和抱歉
You need to create your own Converter. Try something like this:
import uuid from flask_wiki.backend.models import Page from flask_wiki.backend.backend import marsh from marshmallow_sqlalchemy.convert import ModelConverter from flask_wiki.backend.custom_fields import GUIDField from marshmallow import fields #Here you are overwriting the list of types of the SQLAlchemy, adding your custom type class GUIDConverter(ModelConverter): SQLA_TYPE_MAPPING = dict( list(ModelConverter.SQLA_TYPE_MAPPING.items()) + [(GUIDField, fields.Str)] ) class GUIDSerializationField(fields.Field): def _serialize(self, value, attr, obj): if value is None: return value else: if isinstance(value, uuid.UUID): return str(value) else: return None class PageSchema(marsh.ModelSchema): class Meta: model = Page model_converter = GUIDConverter #Tell to Marshmallow to use your custom converter for this model guid = GUIDSerializationField(attribute="guid")
You can also see this link for help. I hope it helped and sorry for my bad english
相关问答
更多-
来自Steven Loria https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/33#issuecomment-147008000 以下是您可以采取的几种不同的方法: 选项1:字段参数 from marshmallow import Schema, fields, pre_load class BaseSchema(Schema): @pre_load def check_update_fields(self ...
-
平面实施 你可以使用这样的东西: from sqlalchemy.ext.declarative import DeclarativeMeta class AlchemyEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj.__class__, DeclarativeMeta): # an SQLAlchemy class fields = {} for field i ...
-
我想,现在这是不可能的。 但是您可以为每个属性创建两个单独的序列化程序。 我知道,这是一个小解决方法,但它应该工作。 I think, this is not possible now. But you can create two separate serializers for each property. I know, this a little workaround, but it should work.
-
如何使用棉花糖序列化自定义sqlalchemy字段?(How to use marshmallow to serialize a custom sqlalchemy field?)[2022-11-11]
你需要创建你自己的转换器。 尝试这样的事情: import uuid from flask_wiki.backend.models import Page from flask_wiki.backend.backend import marsh from marshmallow_sqlalchemy.convert import ModelConverter from flask_wiki.backend.custom_fields import GUIDField from marshmallow imp ... -
如果您使用自定义序列化,答案是肯定的。 当我们通过重写writeObject方法进行自定义序列化时,您可以控制序列化并可以执行任何操作。 因此,您还可以分配或使用transient字段的值,也可以将其与其他类属性一起编组。 The answer is yes if you are using the custom serializaton. When we do custom serialization by overriding the writeObject method, you take contr ...
-
您可以覆盖ModelConverter类并为您的地理区域指定自定义映射。 请参阅Jair Perrut的答案如何使用marshmallow序列化自定义sqlalchemy字段? from marshmallow_sqlalchemy import ModelConverter from marshmallow import fields class GeoConverter(ModelConverter): SQLA_TYPE_MAPPING = ModelConverter.SQLA_TYPE_ ...
-
在官方纪录片中找到一些信息。 所以,我的问题可以用 started_at = fields.DateTime('%Y-%m-%dT%H:%M:%S+03:00') 硬编码,但看起来比使用fields.Method()附加功能更好fields.Method() Found some info in official documentaries. So, my issue can be solved using started_at = fields.DateTime('%Y-%m-%dT%H:%M:%S+03 ...
-
http://marshmallow.readthedocs.io/en/latest/quickstart.html#specifying-attribute-names class ApiSchema(Schema): class Meta: strict = True _time = fields.Number(attribute="time") _id = fields.String(attribute="id") http://marshmallow.readthedo ...
-
在BookSchema ,您将要添加一个Nested authors字段(但已注释掉),但您需要使用many关键字参数指定它将是一个列表。 class BookSchema(ma.ModelSchema): # A list of author objects authors = ma.Nested(AuthorSchema, many=True) class Meta: model = Book In your BookSchema, you'll want ...
-
使用Marshmallow进行序列化优化,其他解决方案(Serialization optimization using Marshmallow, other solutions)[2022-11-03]
优化我的代码的解决方案是直接从我的SQLAlchemy查询对象转到pandas数据框(我忘了提到我在查询记录集之后在pandas中做了一些繁重的工作)。 因此,我能够跳过这一步 out = [] for result in all: data = {'sid': result.sid out.append(data) 通过使用Pandas的sql_read方法如下: import pandas as pd pd.read_sql(all.statement, all.session.bi ...