问:
How can I validate my XML using a schema, reading only one object/element at a time by JAXB's XMLStreamReader?
The code below works correctly to unmarshal XML from the stream one object at a time.

But when I un-comment the unmarshaller.setSchema(schema) line the program throws an exception:

[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Subscriber'.]

I have already validated the XML using the javax.xml.validation.Validator class, but my goal is to validate and unmarshal at the same time, one element at a time.

This is my current code:

```<code>SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("/Path to xsd"));

XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new FileReader("/Path to xml"));

JAXBContext jaxbContext = JAXBContext.newInstance(SubscriberType.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
//unmarshaller.setSchema(schema);

streamReader.nextTag();
streamReader.require(XMLStreamConstants.START_ELEMENT, null, "Subscribers");
streamReader.nextTag();
while (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT) {

    JAXBElement<SubscriberType> pt = unmarshaller.unmarshal(streamReader, SubscriberType.class);
    //do something with the unmarshalled object pt...store to db ect.

    if (streamReader.getEventType() == XMLStreamConstants.CHARACTERS) {
        streamReader.next();
    }
}
</code>```

Excerpt of my schema subscriber.xsd:

```<code><?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="unqualified"
        attributeFormDefault="unqualified">

  <xsd:element name="Subscribers" type="SubscriberType" />

  <xsd:complexType name="SubscriberType">
    <xsd:sequence>
      <xsd:element name="Subscriber"
              type="SubscriberInformation"
              minOccurs="1"
              maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
</code>```

答:
Try it with the schema like this:

```<code><?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified">

    <xsd:element name="Subscribers" type="SubscriberType"/>

    <xsd:element name="Subscriber" type="SubscriberInformation" />

    <xsd:complexType name="SubscriberType">
        <xsd:sequence>
            <xsd:element ref="Subscriber" minOccurs="1" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
</code>```

What I believe happens with your schema is this: the JAXB context knows the class for SubscriberType and SubscriberInformation. If you were to give it an XML document with a <Subscribers> root element, it knows that it must unmarshal to the class for SubscriberType. If you gave it an XML document with a <Subscriber> root element however, it normally wouldn't find this element definition in the ObjectFactory class generated by XJC. But since you've used the unmarshal method that takes a second argument, namely the class you're expecting, you've told the unmarshaller that it should interpret its input as a SubscriberType. The result will be an empty SubscriberType instance.

Now, since you're iterating over the <Subscriber> elements one by one (at least that's what I gather you're meaning to do), for the unmarshaller it seems as if it is receiving XML documents with that as root element. It won't complain about not finding that definition since you've taken the task of figuring out the type away with the class argument. But the moment you attach a schema for validation, things break down. The validator doesn't know you're within a <Subscribers> element. It's expecting a full XML document. So it goes to look for an element declaration for <Subscriber> but comes up empty, since that element is only defined within a complex type. It's not a global element defintion (i.e. one under the schema root).

So, two things to do here. One is to define element <Subscriber> as shown above, then reference it in your complex type(s). The other is to change your unmarshal call to unmarshal(streamReader, SubscriberInformation.class) to get the correct type of object back. Also look out for infinite loops or incorrect unmarshalling, since your call to streamReader.next() is in a condition and might not fire.

Writing schemas with JAXB in mind requires a certain style. In general, it's best to define elements globally, then reference them. Only define an element locally within a complex type if it absolutely must remain encapsulated there.

Sorry for the long-winded answer, I'm not very well awake :)

Body:
The code below works correctly to unmarshal XML from the stream one object at a time.

But when I un-comment the unmarshaller.setSchema(schema) line the program throws an exception:

[org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Subscriber'.]

I have already validated the XML using the javax.xml.validation.Validator class, but my goal is to validate and unmarshal at the same time, one element at a time.

This is my current code:

SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("/Path to xsd"));

XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new FileReader("/Path to xml"));

JAXBContext jaxbContext = JAXBContext.newInstance(SubscriberType.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
//unmarshaller.setSchema(schema);

streamReader.nextTag();
streamReader.require(XMLStreamConstants.START_ELEMENT, null, "Subscribers");
streamReader.nextTag();
while (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT) {

    JAXBElement<SubscriberType> pt = unmarshaller.unmarshal(streamReader, SubscriberType.class);
    //do something with the unmarshalled object pt...store to db ect.

    if (streamReader.getEventType() == XMLStreamConstants.CHARACTERS) {
        streamReader.next();
    }
}

Excerpt of my schema subscriber.xsd:

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="unqualified"
        attributeFormDefault="unqualified">

  <xsd:element name="Subscribers" type="SubscriberType" />

  <xsd:complexType name="SubscriberType">
    <xsd:sequence>
      <xsd:element name="Subscriber"
              type="SubscriberInformation"
              minOccurs="1"
              maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>

Body:
Try it with the schema like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified">

    <xsd:element name="Subscribers" type="SubscriberType"/>

    <xsd:element name="Subscriber" type="SubscriberInformation" />

    <xsd:complexType name="SubscriberType">
        <xsd:sequence>
            <xsd:element ref="Subscriber" minOccurs="1" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>

What I believe happens with your schema is this: the JAXB context knows the class for SubscriberType and SubscriberInformation. If you were to give it an XML document with a <Subscribers> root element, it knows that it must unmarshal to the class for SubscriberType. If you gave it an XML document with a <Subscriber> root element however, it normally wouldn't find this element definition in the ObjectFactory class generated by XJC. But since you've used the unmarshal method that takes a second argument, namely the class you're expecting, you've told the unmarshaller that it should interpret its input as a SubscriberType. The result will be an empty SubscriberType instance.

Now, since you're iterating over the <Subscriber> elements one by one (at least that's what I gather you're meaning to do), for the unmarshaller it seems as if it is receiving XML documents with that as root element. It won't complain about not finding that definition since you've taken the task of figuring out the type away with the class argument. But the moment you attach a schema for validation, things break down. The validator doesn't know you're within a <Subscribers> element. It's expecting a full XML document. So it goes to look for an element declaration for <Subscriber> but comes up empty, since that element is only defined within a complex type. It's not a global element defintion (i.e. one under the schema root).

So, two things to do here. One is to define element <Subscriber> as shown above, then reference it in your complex type(s). The other is to change your unmarshal call to unmarshal(streamReader, SubscriberInformation.class) to get the correct type of object back. Also look out for infinite loops or incorrect unmarshalling, since your call to streamReader.next() is in a condition and might not fire.

Writing schemas with JAXB in mind requires a certain style. In general, it's best to define elements globally, then reference them. Only define an element locally within a complex type if it absolutely must remain encapsulated there.

Sorry for the long-winded answer, I'm not very well awake :)


来源: stackexchange

问:
i18next react & express access the key without writing the lang code and the translation key
Hi i trying to supported multi lang in my app , but i must to access to the key by the response that get from express.
i am used i18n in react and express to get from the backend the translation.
like this is work for me:

```<code>t('en.translation.user')
</code>```

but i want to do it like this:

```<code>t('user')
</code>```

here is my code in the client:

```<code>import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import Backend from "i18next-http-backend";

i18n
    .use(Backend)
    .use(initReactI18next)
    .init({
        lng: "en",
        fallbackLng: "en",
        backend: {
                loadPath: "http://localhost:4000/locales/resources.json?lng={{lng}}&ns={{ns}}",
                addPath: "http://localhost:4000/locales/add/{{lng}}/{{ns}}",
                allowMultiLoading: false,
        },
        interpolation: {
            escapeValue: false, // react already safes from xss
        },

    });
export default i18n;
</code>```

my server is like this:

```<code>const i18next = require('i18next');
const Backend = require('i18next-node-fs-backend');
const i18nextMiddleware = require('i18next-express-middleware');

const pathTranslationPublic = path.join(__dirname, '/src/locales');
i18next.use(Backend).init({
    preload: ['en',],
    lowerCaseLng: true,
    backend: {
        loadPath: `${pathTranslationPublic}/{{lng}}/{{ns}}.json`,
        addPath: `${pathTranslationPublic}/{{lng}}/{{ns}}.missing.json`,
        jsonIndent: 4,
    },
    load: 'all',
    lng: 'en',
    fallbackLng: 'en',
    debug: true,
    saveMissing: true,
    formatSeparator:'.'
});
app.use(
    i18nextMiddleware.handle(i18next, {
        removeLngFromUrl: false,
    }),
);
app.get(
    '/locales/resources.json',
    i18nextMiddleware.getResourcesHandler(i18next),
);
</code>```

this is the response from the client:

```<code>{
    "en": {
        "translation": {
            "user": "hello user"
        }
    }
}
</code>```

答:
getResourcesHandler is used for multiloading...

To provide the translations to your react client, just serve them statically, i.e.

```<code>app.use(express.static(pathTranslationPublic));
</code>```

This way the client can have a loadPath like '/{{lng}}/{{ns}}.json'

btw:
i18next-node-fs-backend is deprecated, use i18next-fs-backend instead
i18next-express-middleware is deprecated, use i18next-http-middleware instead

For a react guide with i18next, have a look at: https://dev.to/adrai/how-to-properly-internationalize-a-react-application-using-i18next-3hdb

ContentLicense: CC BY-SA 4.0

Body:
Hi i trying to supported multi lang in my app , but i must to access to the key by the response that get from express.
i am used i18n in react and express to get from the backend the translation.
like this is work for me:

t('en.translation.user')

but i want to do it like this:

t('user')

here is my code in the client:

import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import Backend from "i18next-http-backend";

i18n
    .use(Backend)
    .use(initReactI18next)
    .init({
        lng: "en",
        fallbackLng: "en",
        backend: {
                loadPath: "http://localhost:4000/locales/resources.json?lng={{lng}}&ns={{ns}}",
                addPath: "http://localhost:4000/locales/add/{{lng}}/{{ns}}",
                allowMultiLoading: false,
        },
        interpolation: {
            escapeValue: false, // react already safes from xss
        },

    });
export default i18n;

my server is like this:

const i18next = require('i18next');
const Backend = require('i18next-node-fs-backend');
const i18nextMiddleware = require('i18next-express-middleware');

const pathTranslationPublic = path.join(__dirname, '/src/locales');
i18next.use(Backend).init({
    preload: ['en',],
    lowerCaseLng: true,
    backend: {
        loadPath: `${pathTranslationPublic}/{{lng}}/{{ns}}.json`,
        addPath: `${pathTranslationPublic}/{{lng}}/{{ns}}.missing.json`,
        jsonIndent: 4,
    },
    load: 'all',
    lng: 'en',
    fallbackLng: 'en',
    debug: true,
    saveMissing: true,
    formatSeparator:'.'
});
app.use(
    i18nextMiddleware.handle(i18next, {
        removeLngFromUrl: false,
    }),
);
app.get(
    '/locales/resources.json',
    i18nextMiddleware.getResourcesHandler(i18next),
);

this is the response from the client:

{
    "en": {
        "translation": {
            "user": "hello user"
        }
    }
}

Body:
getResourcesHandler is used for multiloading...

To provide the translations to your react client, just serve them statically, i.e.

app.use(express.static(pathTranslationPublic));

This way the client can have a loadPath like '/{{lng}}/{{ns}}.json'

btw:
i18next-node-fs-backend is deprecated, use i18next-fs-backend instead
i18next-express-middleware is deprecated, use i18next-http-middleware instead

For a react guide with i18next, have a look at: https://dev.to/adrai/how-to-properly-internationalize-a-react-application-using-i18next-3hdb


问:
Android Picasso not loading from LAN url
My simple web server is Python3 SimpleHTTPRequestHandler and
it works just fine. It runs in a LAN machine's picture folder and I have tested it
using a browser and entering a test URL such as:
http://192.168.1.111:8000/2020-02-09/test.jpg

However, that same URL will not load by Picasso.
It will load local pictures via the "file://".
As test, I have tested an internet URL and it also loads successfully.
So I have narrowed down the problem to the LAN address.

I have tried the android:networkSecurityConfig solution but it did not work.
I'm running out of ideas at this point and about to ditch Picasso and
load the images via ftp but did not want to get into managing efficient memory
management.  Please advise...

答:
I spoke a bit too soon!
I tried to bypass Picasso and use an asyncTask based on java.net.URL(lanUrl)
and saw the error in the debug window:
java.io.IOException: Cleartext HTTP traffic to 192.168.1.150 not permitted

So, that got me to
Cleartext http traffic not permitted and revisit the networkSecurityConfig solution.
Using the accepted answer, the error is now gone and Picasso loads successfully. I only wish it had produced that error message!
Thanks goes to Shafi Muhammed.


问:
ContainsKey Thread Safe
In the following code:

```<code>public class StringCache
{
    private readonly object lockobj = new object();

    private readonly Dictionary<int, string> cache = new Dictionary<int, string>();

    public string GetMemberInfo(int key)
    {
        if (cache.ContainsKey(key))
            return cache[key];
        lock (lockobj)
        {
            if (!cache.ContainsKey(key))
                cache[key] = GetString(key);
        }
        return cache[key];
    }

    private static string GetString(int key)
    {
        return "Not Important";
    }
}
</code>```

1) Is ContainsKey thread safe?  IOW, what happens if that method is executing when another thread is adding something to the dictionary?
2) For the first return cache[key], is there any chance that it could return a garbled value?

TIA,

MB

答:
Here's what it says in the MSDN documentation:

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

A Dictionary<(Of <(TKey, TValue>)>)
  can support multiple readers
  concurrently, as long as the
  collection is not modified. Even so,
  enumerating through a collection is
  intrinsically not a thread-safe
  procedure. In the rare case where an
  enumeration contends with write
  accesses, the collection must be
  locked during the entire enumeration.
  To allow the collection to be accessed
  by multiple threads for reading and
  writing, you must implement your own
  synchronization.

If I'm reading that correctly, I don't believe that it is thread safe.

OwnerDisplayName: MBeckius

ContentLicense: CC BY-SA 2.5

Title: ContainsKey Thread Safe

Body:
In the following code:

public class StringCache
{
    private readonly object lockobj = new object();

    private readonly Dictionary<int, string> cache = new Dictionary<int, string>();

    public string GetMemberInfo(int key)
    {
        if (cache.ContainsKey(key))
            return cache[key];
        lock (lockobj)
        {
            if (!cache.ContainsKey(key))
                cache[key] = GetString(key);
        }
        return cache[key];
    }

    private static string GetString(int key)
    {
        return "Not Important";
    }
}

1) Is ContainsKey thread safe?  IOW, what happens if that method is executing when another thread is adding something to the dictionary?
2) For the first return cache[key], is there any chance that it could return a garbled value?

TIA,

MB

OwnerDisplayName: BFree


问:
Using multiple configs in the same group to interpolate values in a yaml file
In Hydra I have the following configuration:

```<code>├── conf
    │   ├── config.yaml
    │   ├── callbacks
    │   │   ├── callback_01.yaml
    │   │   └── callback_02.yaml
    │   └── trainer
    │       ├── default.yaml

</code>```

The callbacks have a structure like this:

```<code>_target_: callback_to_instantiate
</code>```

I need to pass to the trainer/default.yaml both the callbacks through interpolation.
I tried like this:

```<code>    _target_: pytorch_lightning.Trainer
    callbacks:
        - ${callbacks.callback_01}
        - ${callbacks.callback_02}
</code>```

With the config.yaml like this:

```<code>    defaults:
      - _self_
      - trainer: default
</code>```

I did also other trials but it doesn't seem to work. Is there a way to interpolate like that in a yaml file by using two or more yaml files that are in the config group?
I would like if possible to keep this structure.

答:
Currently the recommended approach is:

compose a mapping whose values are the desired callbacks, and then

use the oc.dict.values OmegaConf resolver to get a list of values from that dictionary.

# conf/config.yaml
defaults:
  - callbacks@_callback_dict.cb1: callback_01
  - callbacks@_callback_dict.cb2: callback_02
  - trainer: default
  - _self_
```
<pre class="lang-yaml prettyprint-override"><code># conf/trainer/default.yaml
_target_: pytorch_lightning.Trainer
callbacks: ${oc.dict.values:_callback_dict}
</code>```

# my_app.py
from typing import Any
import hydra
from omegaconf import DictConfig, OmegaConf

@hydra.main(config_path="conf", config_name="config")
def app(cfg: DictConfig) -> Any:
    OmegaConf.resolve(cfg)
    del cfg._callback_dict
    print(OmegaConf.to_yaml(cfg))

if __name__ == "__main__":
    app()
```
<p>At the command line:</p>
```$ python my_app.py
trainer:
  _target_: pytorch_lightning.Trainer
  callbacks:
  - _target_: callback_to_instantiate_01
  - _target_: callback_to_instantiate_02
```

For reference, there is an open issue on Hydra's github repo advocating for an improved user experience around

Body:
In Hydra I have the following configuration:

├── conf
    │   ├── config.yaml
    │   ├── callbacks
    │   │   ├── callback_01.yaml
    │   │   └── callback_02.yaml
    │   └── trainer
    │       ├── default.yaml

The callbacks have a structure like this:

_target_: callback_to_instantiate

I need to pass to the trainer/default.yaml both the callbacks through interpolation.
I tried like this:

    _target_: pytorch_lightning.Trainer
    callbacks:
        - ${callbacks.callback_01}
        - ${callbacks.callback_02}

With the config.yaml like this:

    defaults:
      - _self_
      - trainer: default

I did also other trials but it doesn't seem to work. Is there a way to interpolate like that in a yaml file by using two or more yaml files that are in the config group?
I would like if possible to keep this structure.

Body:
Currently the recommended approach is:

compose a mapping whose values are the desired callbacks, and then

use the oc.dict.values OmegaConf resolver to get a list of values from that dictionary.

# conf/config.yaml
defaults:
  - callbacks@_callback_dict.cb1: callback_01
  - callbacks@_callback_dict.cb2: callback_02
  - trainer: default
  - _self_

# conf/trainer/default.yaml
_target_: pytorch_lightning.Trainer
callbacks: ${oc.dict.values:_callback_dict}

# my_app.py
from typing import Any
import hydra
from omegaconf import DictConfig, OmegaConf

@hydra.main(config_path="conf", config_name="config")
def app(cfg: DictConfig) -> Any:
    OmegaConf.resolve(cfg)
    del cfg._callback_dict
    print(OmegaConf.to_yaml(cfg))

if __name__ == "__main__":
    app()

At the command line:

$ python my_app.py
trainer:
  _target_: pytorch_lightning.Trainer
  callbacks:
  - _target_: callback_to_instantiate_01
  - _target_: callback_to_instantiate_02

For reference, there is an open issue on Hydra's github repo advocating for an improved user experience around


问:
How to trace specific signals using Verilator?
I have a system which I want to trace with Verilator, but using a VCD trace file is highly resource-demanding (hundreds of gigabytes) and time consuming. I changed the trace file type to FST, and the file size is reasonable, but it is still time-consuming. Tracing the whole system is not my goal at the moment because I only want the values of 8 signals, not the 16000 signals of the system.

How can I trace specific signals using Verilator?

Any help or hint would be much appreciated.

答:
The Verilator manual has a FAQ for this: How do I speed up writing large waveform (trace) files?

These are the suggestions (briefly):

Delay calling open

Add the /*verilator tracing_off*/ metacomment

Use the --trace-depth 1 option

FST (which you already tried)

Write your trace files to a local solid-state drive, instead of to a network drive


问:
PostCSS Loader has been initialized using an options object that does not match the API schema
I'm having this problem after upgrading react-scripts from version 4 to 5!

I use customize-cra with the option below:

```<code>module.exports = override(
  addLessLoader({
    lessOptions: {
      javascriptEnabled: true,
      modifyVars: {
        '@primary-color': '#038fde',
      },
    },
  }),
  useBabelRc(),
);
</code>```

```<code>ERROR in ./src/styles/wieldy.less (./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[9].use[1]!./node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[9].use[2]!./node_modules/resolve-url-loader/index.js??ruleSet[1].rules[1].oneOf[9].use[3]!./node_modules/less-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[9].use[4]!./src/styles/wieldy.less)

Module build failed (from ./node_modules/postcss-loader/dist/cjs.js):
ValidationError: Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
 - options has an unknown property 'plugins'. These properties are valid:
   object { postcssOptions?, execute?, sourceMap?, implementation? }
    at validate (C:\Workspace\harv-Harvest-frontend\node_modules\schema-utils\dist\validate.js:105:11)
    at Object.getOptions (C:\Workspace\harv-Harvest-frontend\node_modules\webpack\lib\NormalModule.js:580:19)
    at Object.loader (C:\Workspace\harv-Harvest-frontend\node_modules\postcss-loader\dist\index.js:40:24)
</code>```

答:
Had this issue as well.

seems like since less-loader 6 customize-cra "addLessLoader" is broken.

this PR fixes it, but was never merged.
The same person created this package:
customize-cra-less-loader

to fix this issue:

Install customize-cra-less-loader package

"config-overrides.js" should look like this:

const { override } = require("customize-cra");
const addLessLoader = require("customize-cra-less-loader");

module.exports = override(
  addLessLoader({
    lessLoaderOptions: {
      lessOptions: {
        javascriptEnabled: true,
        modifyVars: {
          '@primary-color': '#038fde',
        }
      }
    }
  })
);```

Body:
I'm having this problem after upgrading react-scripts from version 4 to 5!

I use customize-cra with the option below:

module.exports = override(
  addLessLoader({
    lessOptions: {
      javascriptEnabled: true,
      modifyVars: {
        '@primary-color': '#038fde',
      },
    },
  }),
  useBabelRc(),
);

ERROR in ./src/styles/wieldy.less (./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[9].use[1]!./node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[9].use[2]!./node_modules/resolve-url-loader/index.js??ruleSet[1].rules[1].oneOf[9].use[3]!./node_modules/less-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[9].use[4]!./src/styles/wieldy.less)

Module build failed (from ./node_modules/postcss-loader/dist/cjs.js):
ValidationError: Invalid options object. PostCSS Loader has been initialized using an options object that does not match the API schema.
 - options has an unknown property 'plugins'. These properties are valid:
   object { postcssOptions?, execute?, sourceMap?, implementation? }
    at validate (C:\Workspace\harv-Harvest-frontend\node_modules\schema-utils\dist\validate.js:105:11)
    at Object.getOptions (C:\Workspace\harv-Harvest-frontend\node_modules\webpack\lib\NormalModule.js:580:19)
    at Object.loader (C:\Workspace\harv-Harvest-frontend\node_modules\postcss-loader\dist\index.js:40:24)


问:
Swift: String.removeSubrange - HowTo?
I somehow miss what they want me to do when I want to cut a String in Swift. They told me Swift was "objective". But it rather is "not working". Removing a sub range from a string is explained in the docs (https://developer.apple.com/documentation/swift/string/3018537-removesubrange), which I read. But before I move back to ObjC, let me ask this question: From

```<code>measurements.removeSubrange(1..<4)
</code>```

I made

```<code>logString = logString.removeSubrange(logSizeLimit..logString.count)
</code>```

It said Cannot find operator '..' in scope; did you mean '...'? I changed it to be .... It now tells me:

```<code>(1.) Cannot assign value of type '()' to type 'String'
(2.) Instance method 'removeSubrange' requires the types 'String.Index' and 'Int' be equivalent
</code>```

This is my code:

```<code>public let logSizeLimit = 40960
public var logString = ""
func senderLoggedMessage(_ message:String) {
    var msg = message + "\n"
    msg += logString
    logString = msg
    if logString.count > logSizeLimit {
        logString = logString.removeSubrange(logSizeLimit ... logString.count) // error line
    }
}
</code>```

Well. Does anyone know the answer?

答:
There are a few problems with your code:

removeSubrange is a mutating method that returns Void. So logString.removeSubrange(_:) modifies logString in place. It does not return a new String. You would have exactly the same sort of problem with the -[NSMutableString deleteCharactersInRange:] method in Objective-C.

You are trying to use Int to index into a String's characters. But Swift Strings do not use Int indices.

There is a type, String.Index, which you can use to index into a String with removeSubrange and other functions. But I don't recommend it.

Swift's String is very, very careful about handling Unicode properly. So a String is not just an array of bytes or of UTF-16 code units as in many other languages. A String is a collection of Characters, and a Character is a “user-perceived character” as discussed in Unicode Standard Annex #29: Unicode Text Segmentation.

The Character ‘a’ is one code point (U+0061) and one UTF-8 code unit, but the Character ‘’ is seven code points (U+1f468 U+200d U+1f469 U+200d U+1f467 U+200d U+1f466) and 25 UTF-8 code units.

Because each Character in a String may use a different amount of storage, manipulating a String.Index is not trivial the way manipulating an Int is. You have to use methods on String like index(after:) and distance(from:to:) instead of operators like + and -.

In my experience, it is much, much easier to slice up a String using operations like .prefix(_:) and .drop(while:). These operations generally return a Substring. So at the end you can use String.init to convert the Substring back to a String if needed.

I would write your function like this:

```<code>public let logSizeLimit = 40960
public var logString = ""
func senderLoggedMessage(_ message:String) {
    logString = String("\(message)\n\(logString)"
        .prefix(logSizeLimit))
}
</code>```

Title: Swift: String.removeSubrange - HowTo?

Body:
I somehow miss what they want me to do when I want to cut a String in Swift. They told me Swift was "objective". But it rather is "not working". Removing a sub range from a string is explained in the docs (https://developer.apple.com/documentation/swift/string/3018537-removesubrange), which I read. But before I move back to ObjC, let me ask this question: From

measurements.removeSubrange(1..<4)

I made

logString = logString.removeSubrange(logSizeLimit..logString.count)

It said Cannot find operator '..' in scope; did you mean '...'? I changed it to be .... It now tells me:

(1.) Cannot assign value of type '()' to type 'String'
(2.) Instance method 'removeSubrange' requires the types 'String.Index' and 'Int' be equivalent

This is my code:

public let logSizeLimit = 40960
public var logString = ""
func senderLoggedMessage(_ message:String) {
    var msg = message + "\n"
    msg += logString
    logString = msg
    if logString.count > logSizeLimit {
        logString = logString.removeSubrange(logSizeLimit ... logString.count) // error line
    }
}

Well. Does anyone know the answer?

Body:
There are a few problems with your code:

removeSubrange is a mutating method that returns Void. So logString.removeSubrange(_:) modifies logString in place. It does not return a new String. You would have exactly the same sort of problem with the -[NSMutableString deleteCharactersInRange:] method in Objective-C.

You are trying to use Int to index into a String's characters. But Swift Strings do not use Int indices.

There is a type, String.Index, which you can use to index into a String with removeSubrange and other functions. But I don't recommend it.

Swift's String is very, very careful about handling Unicode properly. So a String is not just an array of bytes or of UTF-16 code units as in many other languages. A String is a collection of Characters, and a Character is a “user-perceived character” as discussed in Unicode Standard Annex #29: Unicode Text Segmentation.

The Character ‘a’ is one code point (U+0061) and one UTF-8 code unit, but the Character ‘’ is seven code points (U+1f468 U+200d U+1f469 U+200d U+1f467 U+200d U+1f466) and 25 UTF-8 code units.

Because each Character in a String may use a different amount of storage, manipulating a String.Index is not trivial the way manipulating an Int is. You have to use methods on String like index(after:) and distance(from:to:) instead of operators like + and -.

In my experience, it is much, much easier to slice up a String using operations like .prefix(_:) and .drop(while:). These operations generally return a Substring. So at the end you can use String.init to convert the Substring back to a String if needed.

I would write your function like this:

public let logSizeLimit = 40960
public var logString = ""
func senderLoggedMessage(_ message:String) {
    logString = String("\(message)\n\(logString)"
        .prefix(logSizeLimit))
}


问:
C++ vector member initialization
I am confused about the output in the following program about the vec in Test. Why it's a vector with size 100 instead of 1? I thought std::vector<T> var{a} is the same as std::vector<T> var = {a}.

```<code>#include <iostream>
#include <vector>

using namespace std;
struct Value {
   int a;
   int b;
};

class Test {
  public:
    std::vector<struct Value> vec{100};
};

class Test2 {
  public:
    std::vector<int> vec{100};
};

int main()
{
    Test test;
    std::cout << "test size: " << test.vec.size() << std::endl;
    Test2 test2;
    std::cout << "test2 size: " << test2.vec.size();

    return 0;
}
</code>```

Output:

```<code>test size: 100
test2 size: 1
</code>```

答:
This may not be a totally helpful answer, but I decided to put a breakpoint in the class declaration for vector in the STD library.

Answer

In the definition of a vector, there are 3 ways it handles the assignment.
A struct will provide a value-construction, where as an int will be assigned as a sized range construction.

It reads std::vector<struct> vect{100};  as building a vector of length 100, while std::vector<int> vect{100}; is acting similarly vect.insert(v.end(),100);

This is based on the type of object passed in for .

For information's sake, the final option is taking a given value, and assigning it to a number of spots. So if you had 100, "x", it would put "x" into your vector 100 times.

The Journey

What I learned from this is that there's a point where your vector takes a size_type input and a _Valty&& input (which I don't know what that is yet. Will be looking it up later) and provides a construction between 3 different args.

My best guess would be that your struct is filling in for 1-args path and acts as a length declaration, while int as a native type falls into the 2-args path and acts as a value assignment.

The sizeof(Value) may == 0, while the size of an int will be 1.

Edit: I guessed 1 and 2 (or _Count == 0, and Count == 1), however I was wrong about this. It's _Count == 0 and _Count == 2. Which was very interesting.

    template <class... _Valty>
    _CONSTEXPR20 void _Construct_n(_CRT_GUARDOVERFLOW const size_type _Count, _Valty&&... _Val) {
        // Dispatches between the three sized constructions.
        // 1-arg -> value-construction, e.g. vector(5)
        // 2-arg -> fill, e.g. vector(5, "meow")
        // 3-arg -> sized range construction, e.g. vector{"Hello", "Fluffy", "World"}
        auto& _Al       = _Getal(); //////////////// For test1, _Count is 100, for test2, _Count is 1;
        auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alty, _Al);
        auto& _My_data  = _Mypair._Myval2;
        _Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _My_data);

        if (_Count != 0) {
            _Buy_nonzero(_Count);
            _Tidy_guard<vector> _Guard{this};

// This one happens with a struct
if constexpr (sizeof...(_Val) == 0) {
                _My_data._Mylast = _Uninitialized_value_construct_n(_My_data._Myfirst, _Count, _Al);
            } else

if constexpr (sizeof...(_Val) == 1) {
                _STL_INTERNAL_STATIC_ASSERT(is_same_v<_Valty..., const _Ty&>);
                _My_data._Mylast = _Uninitialized_fill_n(_My_data._Myfirst, _Count, _Val..., _Al);
            } else

// This one happens with an int
if constexpr (sizeof...(_Val) == 2) {
                _My_data._Mylast = _Uninitialized_copy(_STD forward<_Valty>(_Val)..., _My_data._Myfirst, _Al);
            } else {
                static_assert(_Always_false<_Ty>, "Should be unreachable");
            }
            _Guard._Target = nullptr;
        }

        _Proxy._Release();
    }
```

What's really interesting as that it appears to be happening when the Allocator reference is assigned. I'm by no means an expert in the language, but I really wanted to figure this puzzle one! Thanks for the interesting challenge!

If you've never walked through a class definition before, I would recommend trying it out.

Title: C++ vector member initialization

Body:
I am confused about the output in the following program about the vec in Test. Why it's a vector with size 100 instead of 1? I thought std::vector<T> var{a} is the same as std::vector<T> var = {a}.

#include <iostream>
#include <vector>

using namespace std;
struct Value {
   int a;
   int b;
};

class Test {
  public:
    std::vector<struct Value> vec{100};
};

class Test2 {
  public:
    std::vector<int> vec{100};
};

int main()
{
    Test test;
    std::cout << "test size: " << test.vec.size() << std::endl;
    Test2 test2;
    std::cout << "test2 size: " << test2.vec.size();

    return 0;
}

Output:

test size: 100
test2 size: 1

Body:
This may not be a totally helpful answer, but I decided to put a breakpoint in the class declaration for vector in the STD library.

Answer

In the definition of a vector, there are 3 ways it handles the assignment.
A struct will provide a value-construction, where as an int will be assigned as a sized range construction.

It reads std::vector<struct> vect{100};  as building a vector of length 100, while std::vector<int> vect{100}; is acting similarly vect.insert(v.end(),100);

This is based on the type of object passed in for .

For information's sake, the final option is taking a given value, and assigning it to a number of spots. So if you had 100, "x", it would put "x" into your vector 100 times.

The Journey

What I learned from this is that there's a point where your vector takes a size_type input and a _Valty&& input (which I don't know what that is yet. Will be looking it up later) and provides a construction between 3 different args.

My best guess would be that your struct is filling in for 1-args path and acts as a length declaration, while int as a native type falls into the 2-args path and acts as a value assignment.

The sizeof(Value) may == 0, while the size of an int will be 1.

Edit: I guessed 1 and 2 (or _Count == 0, and Count == 1), however I was wrong about this. It's _Count == 0 and _Count == 2. Which was very interesting.

    template <class... _Valty>
    _CONSTEXPR20 void _Construct_n(_CRT_GUARDOVERFLOW const size_type _Count, _Valty&&... _Val) {
        // Dispatches between the three sized constructions.
        // 1-arg -> value-construction, e.g. vector(5)
        // 2-arg -> fill, e.g. vector(5, "meow")
        // 3-arg -> sized range construction, e.g. vector{"Hello", "Fluffy", "World"}
        auto& _Al       = _Getal(); //////////////// For test1, _Count is 100, for test2, _Count is 1;
        auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alty, _Al);
        auto& _My_data  = _Mypair._Myval2;
        _Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _My_data);

        if (_Count != 0) {
            _Buy_nonzero(_Count);
            _Tidy_guard<vector> _Guard{this};

// This one happens with a struct
if constexpr (sizeof...(_Val) == 0) {
                _My_data._Mylast = _Uninitialized_value_construct_n(_My_data._Myfirst, _Count, _Al);
            } else

if constexpr (sizeof...(_Val) == 1) {
                _STL_INTERNAL_STATIC_ASSERT(is_same_v<_Valty..., const _Ty&>);
                _My_data._Mylast = _Uninitialized_fill_n(_My_data._Myfirst, _Count, _Val..., _Al);
            } else

// This one happens with an int
if constexpr (sizeof...(_Val) == 2) {
                _My_data._Mylast = _Uninitialized_copy(_STD forward<_Valty>(_Val)..., _My_data._Myfirst, _Al);
            } else {
                static_assert(_Always_false<_Ty>, "Should be unreachable");
            }
            _Guard._Target = nullptr;
        }

        _Proxy._Release();
    }

What's really interesting as that it appears to be happening when the Allocator reference is assigned. I'm by no means an expert in the language, but I really wanted to figure this puzzle one! Thanks for the interesting challenge!

If you've never walked through a class definition before, I would recommend trying it out.


问:
How to save a database object in PeeWee, if there's recursion error while saving
When I try to save an instance of an object, an error is occurring.

There's my file structure:

class Loader():
    dbhandle = SqliteDatabase('history.db')

    def __init__(self):
        try:
            Loader.dbhandle.connect()
            Hotel.create_table()
        except peewee.InternalError as px:
            print(str(px))
        try:
            HistoryObject.create_table()
        except peewee.InternalError as px:
            print(str(px))

    def save_to_database(self):
        history_object = HistoryObject(chat_id=0,
                                       command='123',
                                       date_time='123'
                                       )
        history_object.save()
    #...

class BaseModel(Model):
    class Meta:
        database = Loader.dbhandle

class HistoryObject(BaseModel):
    id = PrimaryKeyField(null=False)
    chat_id = IntegerField()
    command = CharField(max_length=100)
    date_time = CharField(max_length=100)

    class Meta:
        db_table = "historyObjects"
```
<p>On calling <code>save to database</code> method of the <code>Loader</code> class, an error occur:</p>
<pre class="lang-py prettyprint-override"><code>Traceback (most recent call last):
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 611, in infinity_polling
    self.polling(none_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout,
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 658, in polling
    self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 720, in __threaded_polling
    raise e
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 680, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\util.py", line 135, in raise_exceptions
    raise self.exception_info
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\util.py", line 87, in run
    task(*args, **kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\Handlers\LowHighPrice.py", line 63, in handle
    make_query(message)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\Handlers\LowHighPrice.py", line 152, in make_query
    loader.save_to_database(0, '123', '123')
  File "C:\Users\342\PycharmProjects\python_basic_diploma\loader.py", line 72, in save_to_database
    history_object.save()
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6577, in save
    pk_value = self._pk
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6542, in get_id
    return getattr(self, self._meta.primary_key.safe_name)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  [Previous line repeated 488 more times]
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4479, in __get__
    return (self.rel_model
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6326, in select
    return ModelSelect(cls, fields, is_default=is_default)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6994, in __init__
    super(ModelSelect, self).__init__([model], fields)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6859, in __init__
    super(_ModelQueryHelper, self).__init__(*args, **kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 2281, in __init__
    super(Select, self).__init__(**kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 812, in __init__
    super(_HashableSource, self).__init__(*args, **kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 768, in __init__
    super(Source, self).__init__()
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 2042, in __init__
    super(Query, self).__init__(**kwargs)
RecursionError: maximum recursion depth exceeded while calling a Python object
"
Traceback (most recent call last):
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 611, in infinity_polling
    self.polling(none_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout,
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 658, in polling
    self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 720, in __threaded_polling
    raise e
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 680, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\util.py", line 135, in raise_exceptions
    raise self.exception_info
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\util.py", line 87, in run
    task(*args, **kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\Handlers\LowHighPrice.py", line 63, in handle
    make_query(message)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\Handlers\LowHighPrice.py", line 152, in make_query
    loader.save_to_database(0, '123', '123')
  File "C:\Users\342\PycharmProjects\python_basic_diploma\loader.py", line 72, in save_to_database
    history_object.save()
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6577, in save
    pk_value = self._pk
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6542, in get_id
    return getattr(self, self._meta.primary_key.safe_name)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  [Previous line repeated 488 more times]
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4479, in __get__
    return (self.rel_model
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6326, in select
    return ModelSelect(cls, fields, is_default=is_default)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6994, in __init__
    super(ModelSelect, self).__init__([model], fields)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6859, in __init__
    super(_ModelQueryHelper, self).__init__(*args, **kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 2281, in __init__
    super(Select, self).__init__(**kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 812, in __init__
    super(_HashableSource, self).__init__(*args, **kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 768, in __init__
    super(Source, self).__init__()
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 2042, in __init__
    super(Query, self).__init__(**kwargs)
RecursionError: maximum recursion depth exceeded while calling a Python object
</code>```

The Loader class also used for instancing and polling bot using pyTelegramBotApi and the method called from another file. Don't know if it could cause problem

The class Loader itself is always instanced once, so __init__ method must be called only once too

A bit more info: While trying to debug it, I wrote in peewee.py package file in getattr a print fucntion to print dest variable. It always equals 'id'.

答: The problem was in another table of the database, who was related to first one with ForeignKeyField(HistoryObject, related_name='id'). The attribute related_name ruined all of it and after removing the attribute everything worked fine

Body:
When I try to save an instance of an object, an error is occurring.

There's my file structure:

class Loader():
    dbhandle = SqliteDatabase('history.db')

    def __init__(self):
        try:
            Loader.dbhandle.connect()
            Hotel.create_table()
        except peewee.InternalError as px:
            print(str(px))
        try:
            HistoryObject.create_table()
        except peewee.InternalError as px:
            print(str(px))

    def save_to_database(self):
        history_object = HistoryObject(chat_id=0,
                                       command='123',
                                       date_time='123'
                                       )
        history_object.save()
    #...

class BaseModel(Model):
    class Meta:
        database = Loader.dbhandle

class HistoryObject(BaseModel):
    id = PrimaryKeyField(null=False)
    chat_id = IntegerField()
    command = CharField(max_length=100)
    date_time = CharField(max_length=100)

    class Meta:
        db_table = "historyObjects"

On calling save to database method of the Loader class, an error occur:

Traceback (most recent call last):
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 611, in infinity_polling
    self.polling(none_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout,
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 658, in polling
    self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 720, in __threaded_polling
    raise e
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 680, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\util.py", line 135, in raise_exceptions
    raise self.exception_info
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\util.py", line 87, in run
    task(*args, **kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\Handlers\LowHighPrice.py", line 63, in handle
    make_query(message)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\Handlers\LowHighPrice.py", line 152, in make_query
    loader.save_to_database(0, '123', '123')
  File "C:\Users\342\PycharmProjects\python_basic_diploma\loader.py", line 72, in save_to_database
    history_object.save()
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6577, in save
    pk_value = self._pk
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6542, in get_id
    return getattr(self, self._meta.primary_key.safe_name)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  [Previous line repeated 488 more times]
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4479, in __get__
    return (self.rel_model
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6326, in select
    return ModelSelect(cls, fields, is_default=is_default)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6994, in __init__
    super(ModelSelect, self).__init__([model], fields)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6859, in __init__
    super(_ModelQueryHelper, self).__init__(*args, **kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 2281, in __init__
    super(Select, self).__init__(**kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 812, in __init__
    super(_HashableSource, self).__init__(*args, **kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 768, in __init__
    super(Source, self).__init__()
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 2042, in __init__
    super(Query, self).__init__(**kwargs)
RecursionError: maximum recursion depth exceeded while calling a Python object
"
Traceback (most recent call last):
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 611, in infinity_polling
    self.polling(none_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout,
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 658, in polling
    self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 720, in __threaded_polling
    raise e
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\__init__.py", line 680, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\util.py", line 135, in raise_exceptions
    raise self.exception_info
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\telebot\util.py", line 87, in run
    task(*args, **kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\Handlers\LowHighPrice.py", line 63, in handle
    make_query(message)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\Handlers\LowHighPrice.py", line 152, in make_query
    loader.save_to_database(0, '123', '123')
  File "C:\Users\342\PycharmProjects\python_basic_diploma\loader.py", line 72, in save_to_database
    history_object.save()
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6577, in save
    pk_value = self._pk
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6542, in get_id
    return getattr(self, self._meta.primary_key.safe_name)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4481, in __get__
    .where(self.field == getattr(instance, dest)))
  [Previous line repeated 488 more times]
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 4479, in __get__
    return (self.rel_model
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6326, in select
    return ModelSelect(cls, fields, is_default=is_default)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6994, in __init__
    super(ModelSelect, self).__init__([model], fields)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 6859, in __init__
    super(_ModelQueryHelper, self).__init__(*args, **kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 2281, in __init__
    super(Select, self).__init__(**kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 812, in __init__
    super(_HashableSource, self).__init__(*args, **kwargs)
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 768, in __init__
    super(Source, self).__init__()
  File "C:\Users\342\PycharmProjects\python_basic_diploma\my_venv\lib\site-packages\peewee.py", line 2042, in __init__
    super(Query, self).__init__(**kwargs)
RecursionError: maximum recursion depth exceeded while calling a Python object

The Loader class also used for instancing and polling bot using pyTelegramBotApi and the method called from another file. Don't know if it could cause problem

The class Loader itself is always instanced once, so __init__ method must be called only once too

A bit more info: While trying to debug it, I wrote in peewee.py package file in getattr a print fucntion to print dest variable. It always equals 'id'.


问:
The client has been disconnected while trying to perform the connection
When trying to connect to a Mosquitto MQTT queue running locally, I get the following error.

```<code>Unhandled exception. System.AggregateException: One or more errors occurred. (The client  has been disconnected while trying to perform the connection)
 ---> System.Net.Mqtt.MqttClientException: The client  has been disconnected while trying to perform the connection
   at System.Net.Mqtt.Sdk.MqttClientImpl.ConnectAsync(MqttClientCredentials credentials, MqttLastWill will, Boolean cleanSession)
</code>```

I am using the default options when setting up the System.Net.Mqtt.MqttClient.

var config = new MqttConfiguration() {
  Port = 1883
};
var client = MqttClient.CreateAsync("localhost", config).Result;
var sessionState = client.ConnectAsync(
                new MqttClientCredentials(clientId: "camerasim")).Result;
```
<p>The following errors show up in the Mosquitto MQTT log.</p>
```1644497589: New connection from 172.17.0.1:56792 on port 1883.

1644497589: New client connected from 172.17.0.1:56792 as camerasim (p2, c0, k0).

1644497589: Bad socket read/write on client camerasim: Invalid arguments provided.
```

答:
The error you are seeing is most likely the result of a change made in Mosquitto 2.0.12:

Fix max_keepalive not applying to MQTT v3.1.1 and v3.1 connections. These clients are now rejected if their keepalive value exceeds max_keepalive. This option allows CVE-2020-13849, which is for the MQTT v3.1.1 protocol itself rather than an implementation, to be addressed.

A change made in 2.0.9 also comes into play:

Fix max_keepalive option not applying to clients connecting with keepalive set to 0. Closes #2117.

These changes were made to address an issue with the MQTT protocol itself which permits a denial of service attack (CVE-2020-13849).

The default value for max_keepalive is 65535 so this change means that attempting to connect with keep alive set to 0 (meaning no keepalive) will fail unless mosquitto.conf specifies max_keepalive 0. Unfortunately the error logged (Bad socket read/write on client XXXXXYYYYY: Invalid arguments provided.) does not really highlight the cause.

There are two available solutions:

Specify max_keepalive 0 in mosquitto.conf (Mosquitto 2.0.13 or later).

When connecting specify a keep alive between 1 and 65535. In xamarin/mqtt this means adding KeepAliveSecs to your config; this defaults to 0.

Note that setting KeepAliveSecs = 1 (as per your answer) will allow you to connect but is probably a little short for most users (KeepAliveSecs = 60 may be more appropriate). e.g.

```<code>var configuration = new MqttConfiguration {
    Port = 1883,
    KeepAliveSecs = 60,
    WaitTimeoutSecs = 2,
};
</code>```

I realise that you have already found a solution to this but as it's likely to affect others I thought it was worth explaining the root cause of the issue. Many MQTT libraries default keep alive to 0 so will be impacted (e.g. Go Paho had an issue logged).

Body:
When trying to connect to a Mosquitto MQTT queue running locally, I get the following error.

Unhandled exception. System.AggregateException: One or more errors occurred. (The client  has been disconnected while trying to perform the connection)
 ---> System.Net.Mqtt.MqttClientException: The client  has been disconnected while trying to perform the connection
   at System.Net.Mqtt.Sdk.MqttClientImpl.ConnectAsync(MqttClientCredentials credentials, MqttLastWill will, Boolean cleanSession)

I am using the default options when setting up the System.Net.Mqtt.MqttClient.

var config = new MqttConfiguration() {
  Port = 1883
};
var client = MqttClient.CreateAsync("localhost", config).Result;
var sessionState = client.ConnectAsync(
                new MqttClientCredentials(clientId: "camerasim")).Result;

The following errors show up in the Mosquitto MQTT log.

1644497589: New connection from 172.17.0.1:56792 on port 1883.

1644497589: New client connected from 172.17.0.1:56792 as camerasim (p2, c0, k0).

1644497589: Bad socket read/write on client camerasim: Invalid arguments provided.

Body:
The error you are seeing is most likely the result of a change made in Mosquitto 2.0.12:

Fix max_keepalive not applying to MQTT v3.1.1 and v3.1 connections. These clients are now rejected if their keepalive value exceeds max_keepalive. This option allows CVE-2020-13849, which is for the MQTT v3.1.1 protocol itself rather than an implementation, to be addressed.

A change made in 2.0.9 also comes into play:

Fix max_keepalive option not applying to clients connecting with keepalive set to 0. Closes #2117.

These changes were made to address an issue with the MQTT protocol itself which permits a denial of service attack (CVE-2020-13849).

The default value for max_keepalive is 65535 so this change means that attempting to connect with keep alive set to 0 (meaning no keepalive) will fail unless mosquitto.conf specifies max_keepalive 0. Unfortunately the error logged (Bad socket read/write on client XXXXXYYYYY: Invalid arguments provided.) does not really highlight the cause.

There are two available solutions:

Specify max_keepalive 0 in mosquitto.conf (Mosquitto 2.0.13 or later).

When connecting specify a keep alive between 1 and 65535. In xamarin/mqtt this means adding KeepAliveSecs to your config; this defaults to 0.

Note that setting KeepAliveSecs = 1 (as per your answer) will allow you to connect but is probably a little short for most users (KeepAliveSecs = 60 may be more appropriate). e.g.

var configuration = new MqttConfiguration {
    Port = 1883,
    KeepAliveSecs = 60,
    WaitTimeoutSecs = 2,
};

I realise that you have already found a solution to this but as it's likely to affect others I thought it was worth explaining the root cause of the issue. Many MQTT libraries default keep alive to 0 so will be impacted (e.g. Go Paho had an issue logged).


问:
Overflow-y: scroll; on side navigation prevents tooltip functionality
I have a side navigation with the following styles

```<code>position: fixed;
left: 0;
top: 0;
height: 100%;
width: 78px;
background: #11101D;
padding: 6px 14px;
z-index: 99;
transition: all 0.5s ease;
overflow-x: visible;
overflow-y: scroll;
</code>```

Which allows the user to scroll vertically on the side navigation.

When a user hovers on a navigation item, a tooltip appears to the right.

```<code>position: absolute;
top: -20px;
left: calc(100% + 15px);
z-index: 3;
background: #fff;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.3);
padding: 6px 12px;
border-radius: 4px;
font-size: 15px;
font-weight: 400;
opacity: 0;
white-space: nowrap;
pointer-events: none;
transition: 0s;
</code>```

However, because of the overflow-y: scroll; on the parent, it doesn't overlap the sidebar, instead you scroll horizontally to see it, which isn't desired.

Current Tooltip

Desired Tooltip (with side nav scroll functionality)

I have a JSFiddle with all the markup and styles.

I want the the side nav to be vertically scrollable while still having the functionality to view the tooltip.

Any help would be greatly appreciated. This is based on a React Application, so would prefer non JQuery solutions.

答:
Apparently, you can't, it's "written in the specs"

Source: CSS overflow-x: visible; and overflow-y: hidden; causing scrollbar issue

Source of the source: overflow-x: visible; doesn't work with overflow-y: auto; any workaround?

<s>But you can wrap the menu in a container to split the overflows, the parent with overflow-x: auto and the child with overflow-y: scroll</s>

Body:
I have a side navigation with the following styles

position: fixed;
left: 0;
top: 0;
height: 100%;
width: 78px;
background: #11101D;
padding: 6px 14px;
z-index: 99;
transition: all 0.5s ease;
overflow-x: visible;
overflow-y: scroll;

Which allows the user to scroll vertically on the side navigation.

When a user hovers on a navigation item, a tooltip appears to the right.

position: absolute;
top: -20px;
left: calc(100% + 15px);
z-index: 3;
background: #fff;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.3);
padding: 6px 12px;
border-radius: 4px;
font-size: 15px;
font-weight: 400;
opacity: 0;
white-space: nowrap;
pointer-events: none;
transition: 0s;

However, because of the overflow-y: scroll; on the parent, it doesn't overlap the sidebar, instead you scroll horizontally to see it, which isn't desired.

Current Tooltip

Desired Tooltip (with side nav scroll functionality)

I have a JSFiddle with all the markup and styles.

I want the the side nav to be vertically scrollable while still having the functionality to view the tooltip.

Any help would be greatly appreciated. This is based on a React Application, so would prefer non JQuery solutions.


问:
Toolbar title not aligned to the center when there's a navigation icon
I have a custom toolbar in which I set the title and other things programatically. The thing is, even though the title has the setting gravity="center", whenever the navigationIcon appears, it is moved a little bit to the side (like in the picture).

I understand that since the icon is not in the xml it must be moving it but is there a way to make the textView stay in the center no matter the icons? Or is there a way to add an ImageView or Button to the xml and then set the navigationIcon to that button so that the icon is contemplated in the code?

This is my xml, I added the buttons but I don't know a way to set the icons programatically.

```<code><?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/toolbar"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="?attr/colorPrimary">

   <androidx.constraintlayout.widget.ConstraintLayout
       android:id="@+id/constraintly"
       android:layout_width="match_parent"
       android:layout_height="41dp"
       android:gravity="center"
       android:layout_gravity="center">

       <Button
           android:id="@+id/navigationIcon"
           android:layout_width="24dp"
           android:layout_height="24dp"
           android:scaleType="center"
           android:layout_marginStart="12dp"
           android:layout_alignParentLeft="true"
           app:layout_constraintStart_toStartOf="parent"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintTop_toTopOf="parent"
           />

       <TextView
           android:id="@+id/title"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           style="@style/NavBar.Title"
           android:singleLine="true"
           android:ellipsize="end"
           android:layout_gravity="center"
           android:gravity="center"
           android:layout_marginEnd="12dp"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintTop_toTopOf="parent"
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintStart_toStartOf="parent"
           tools:text="Title"/>

       <TextView
           android:id="@+id/subtitle"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:singleLine="true"
           android:ellipsize="end"
           android:layout_gravity="center"
           android:gravity="center"
           android:layout_marginEnd="12dp"
           style="@style/NavBar.SubTitle"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintStart_toStartOf="parent"
           app:layout_constraintTop_toBottomOf="@id/centeredTitleTextView"
           tools:text="Subtitle"/>

       <Button
           android:id="@+id/closeIcon"
           android:layout_width="24dp"
           android:layout_height="24dp"
           android:scaleType="center"
           android:layout_marginEnd="12dp"
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintTop_toTopOf="parent"
           />
   </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
</code>```

This is the part where the navigationIcon is set but setNavigationIcon cannot take the view.

```<code>   public void setNavigationIconArrow() {
       super.setNavigationIcon(R.drawable.ic_ui_24dp);
   }

   public void setNavigationIconClose() {
       super.setNavigationIcon(R.drawable.ic_24dp);
   }
</code>```

Thank you.

答:
Because of the icon size, your layout starts after that. So you can't use gravity=center. Also, You can't set static left margin or right margin, Because, Android has so much different screen sizes. So you have to do this programmatically.

1- You have to find the phone's screen width size.

2- Convert that size px to dp.

3- Now you can set the left margin to your layout. Which you want in gravity=center.

I hope, I did help you what you want:)

Body:
I have a custom toolbar in which I set the title and other things programatically. The thing is, even though the title has the setting gravity="center", whenever the navigationIcon appears, it is moved a little bit to the side (like in the picture).

I understand that since the icon is not in the xml it must be moving it but is there a way to make the textView stay in the center no matter the icons? Or is there a way to add an ImageView or Button to the xml and then set the navigationIcon to that button so that the icon is contemplated in the code?

This is my xml, I added the buttons but I don't know a way to set the icons programatically.

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/toolbar"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="?attr/colorPrimary">

   <androidx.constraintlayout.widget.ConstraintLayout
       android:id="@+id/constraintly"
       android:layout_width="match_parent"
       android:layout_height="41dp"
       android:gravity="center"
       android:layout_gravity="center">

       <Button
           android:id="@+id/navigationIcon"
           android:layout_width="24dp"
           android:layout_height="24dp"
           android:scaleType="center"
           android:layout_marginStart="12dp"
           android:layout_alignParentLeft="true"
           app:layout_constraintStart_toStartOf="parent"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintTop_toTopOf="parent"
           />

       <TextView
           android:id="@+id/title"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           style="@style/NavBar.Title"
           android:singleLine="true"
           android:ellipsize="end"
           android:layout_gravity="center"
           android:gravity="center"
           android:layout_marginEnd="12dp"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintTop_toTopOf="parent"
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintStart_toStartOf="parent"
           tools:text="Title"/>

       <TextView
           android:id="@+id/subtitle"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:singleLine="true"
           android:ellipsize="end"
           android:layout_gravity="center"
           android:gravity="center"
           android:layout_marginEnd="12dp"
           style="@style/NavBar.SubTitle"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintStart_toStartOf="parent"
           app:layout_constraintTop_toBottomOf="@id/centeredTitleTextView"
           tools:text="Subtitle"/>

       <Button
           android:id="@+id/closeIcon"
           android:layout_width="24dp"
           android:layout_height="24dp"
           android:scaleType="center"
           android:layout_marginEnd="12dp"
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintTop_toTopOf="parent"
           />
   </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>

This is the part where the navigationIcon is set but setNavigationIcon cannot take the view.

   public void setNavigationIconArrow() {
       super.setNavigationIcon(R.drawable.ic_ui_24dp);
   }

   public void setNavigationIconClose() {
       super.setNavigationIcon(R.drawable.ic_24dp);
   }

Thank you.


问:
Data Ingestion in azure data lake
I Have a requirement where I need to ingest continuous/steam data(Json format) from eventHub to Azure data lake.
I want to follow the layered approach(raw, clean, prepared) to finally store data into delta table.
My doubt is around the raw layer.
out of below two approach which one do you recommend is best.

Event hub -> RawLayer(Raw Json Format) -> cleanLayer (delta table) -> preparedLayer(delta table)

Event hub -> RawLayer(delta table) -> cleanLayer (delta table) -> preparedLayer(delta table)

so shall I store the raw Json format in raw layer or its suggested to create delta table in Raw layer is well.

Regards,

答:
I will let others debate the theoretical approaches.

From a practical standpoint, here are the most common ways to write to disk from Event Hub:

Event Hub Capture, dumps files to a storage account directly from an event hub, but the format is AVRO. This is not practical, but it is the "rawest" form your records can take. If I remember correctly your payload is encoded in base64 and embedded in a common schema. They have guidance on how to extract your data in Spark.

Azure Stream Analytics can output to JSON or parquet. In both cases, events are actually going through a deserialization / serialization process that can't be bypassed. This means the output will look raw (at least in the JSON case) but won't really be. In this scenario ASA should be seen as a streaming ETL/ETL. Don't use it (and pay for it) if you're not actively using its features (transformation, cleaning, enrichment...). Note that ASA doesn't support delta lake as an output yet - so you will still need some post processing to ingest the generated files.

Azure Functions using the proper bindings, but as ASA it will require deserialization that don't really qualify as "raw", unless you take a similar approach to what's done in EH Capture to which point you should just use Capture.

Title: Data Ingestion in azure data lake


问:
Fix header in a table
I am developing a small page that is necessary to fix the header of a table, when the user scrolls, the header is still there. I wrote a small piece of code, a sample of my page, in jsfiddle and it isn't working and I don't know why. What am I doing wrong?

You can see what I have done so far here.

Follow this example

答:
You need something like this (ignore the sizing and paddings, that was just for my sake to get it to work before i pass it to you):

CSS:

```<code>td
    {
        padding:10px;
        }
        th
        {
            position:fixed;
            background:black;
            color:White;
            width:378px;
            padding:10px;
            }
     #tableContainer
     {
         width:400px;
         height:400px;
         overflow-y:scroll;
         overflow-x:hidden;
         border:solid 1px #ccc;
         }
     table
     {
         width:400px;
         }
</code>```

HTML:

```<code><div id='tableContainer'>
<table cellpadding="0" cellspacing='0'>
    <tr>
        <th>Header</th>
    </tr>
    <tr>
        <td>Text</td>
    </tr>
    <tr>
        <td>text</td>
    </tr>
    <tr>
        <td>text</td>
    </tr>
</table>
</div>
</code>```

ContentLicense: CC BY-SA 3.0

Title: Fix header in a table

Body:
You need something like this (ignore the sizing and paddings, that was just for my sake to get it to work before i pass it to you):

CSS:

td
    {
        padding:10px;
        }
        th
        {
            position:fixed;
            background:black;
            color:White;
            width:378px;
            padding:10px;
            }
     #tableContainer
     {
         width:400px;
         height:400px;
         overflow-y:scroll;
         overflow-x:hidden;
         border:solid 1px #ccc;
         }
     table
     {
         width:400px;
         }

HTML:

<div id='tableContainer'>
<table cellpadding="0" cellspacing='0'>
    <tr>
        <th>Header</th>
    </tr>
    <tr>
        <td>Text</td>
    </tr>
    <tr>
        <td>text</td>
    </tr>
    <tr>
        <td>text</td>
    </tr>
</table>
</div>


问:
Access Json object name
I want to get data with request.data.columns in frontend.I can do it with ViewSet with list method but how to do it with generics.APIView.
Below is my viewsets and generics code:</p>
```<code>class TestList(viewsets.ViewSet):
     queryset = Test.objects.all()
     def list(self,request):
         serializer = TestSerializer(self.queryset, many = True)
         return Response({'columns': serializer.data})

class TestList(generics.RetriveAPIView):
    queryset = Test.objects.all()
    serializer_class = TestSerializer
</code>```

答:
Correct code:

```<code>class TestList(APIView):
     queryset = Test.objects.all()
     def list(self,request):
         queryset = self.get_queryset()
         serializer = TestSerializer(queryset, many = True)
         return Response({'columns': serializer.data})
</code>```

Details about why i had to add queryset = self.get_queryset() instead of directly access self.queryset.From official drf documentation:

queryset - The queryset that should be used for returning objects from this view. Typically, you must either set this attribute, or override the get_queryset() method. If you are overriding a view method, it is important that you call get_queryset() instead of accessing this property directly, as queryset will get evaluated once, and those results will be cached for all subsequent requests.

Title: Access Json object name

Body:
I want to get data with request.data.columns in frontend.I can do it with ViewSet with list method but how to do it with generics.APIView.
Below is my viewsets and generics code:

class TestList(viewsets.ViewSet):
     queryset = Test.objects.all()
     def list(self,request):
         serializer = TestSerializer(self.queryset, many = True)
         return Response({'columns': serializer.data})

class TestList(generics.RetriveAPIView):
    queryset = Test.objects.all()
    serializer_class = TestSerializer

Body:
Correct code:

class TestList(APIView):
     queryset = Test.objects.all()
     def list(self,request):
         queryset = self.get_queryset()
         serializer = TestSerializer(queryset, many = True)
         return Response({'columns': serializer.data})

Details about why i had to add queryset = self.get_queryset() instead of directly access self.queryset.From official drf documentation:

queryset - The queryset that should be used for returning objects from this view. Typically, you must either set this attribute, or override the get_queryset() method. If you are overriding a view method, it is important that you call get_queryset() instead of accessing this property directly, as queryset will get evaluated once, and those results will be cached for all subsequent requests.


问:
when should lazy val be used rather than a function
I have a class with around 20 methods which take no parameters and will always return the same values. I am fairly sure that these can be refactored and defined as lazy val and I am wondering about the pros and cons of that change. This is

lazy val
def

reduced runtime cost for repeated access of the same data
reduced space complexity (data isn't cached)

My conclusion based on that is that if the class is frequently instantiated and fields are rarely accessed def should be used, if it is rarely instantiated and the fields are frequently used it should be lazy val.

If they are both frequently instantiated and accessed what would be the preferred strategy to fall back on? Also, I don't know what optimization the language inherently implements so I could be totally wrong. Let me know if that is the case

答:
In general, the only case where a lazy val should definitely be used (aside from the obvious "when it's needed to get the code to compile") is when correctness depends on a given computation executing at-most-once (which implies some impurity in the computation).

Beyond that, there's non-trivial performance overhead (which varies from Scala version to Scala version and JVM to JVM) in using lazy val, especially if multiple threads might access the value, so there's a surprisingly high threshold for "expensive-to-compute" below which def recomputing every time can be faster than a lazy val.  For values which are particularly cheap-to-compute, a regular val may not need that many expected accesses to provide a performance win over a lazy val.

def, even if it's allocating an object graph every time, might even be better in GC terms if the callers are quickly discarding the result (as those objects are unlikely to stay live long enough to get into old-gen, which is where problems can arise).

As with any optimization, profile and benchmark.

EDIT: If you don't actually need the hard guarantee that a value will be computed at-most-once (i.e. an at-least-zero-times guarantee) something like this (sparingly...)

private var _thing: Type = null
def thing: Type = {
  if (_thing eq null) {
    _thing = ???
  }
  _thing
}
```

might actually be worth trying and fit the single-threaded intuition about how lazy val works.

Body:
In general, the only case where a lazy val should definitely be used (aside from the obvious "when it's needed to get the code to compile") is when correctness depends on a given computation executing at-most-once (which implies some impurity in the computation).

Beyond that, there's non-trivial performance overhead (which varies from Scala version to Scala version and JVM to JVM) in using lazy val, especially if multiple threads might access the value, so there's a surprisingly high threshold for "expensive-to-compute" below which def recomputing every time can be faster than a lazy val.  For values which are particularly cheap-to-compute, a regular val may not need that many expected accesses to provide a performance win over a lazy val.

def, even if it's allocating an object graph every time, might even be better in GC terms if the callers are quickly discarding the result (as those objects are unlikely to stay live long enough to get into old-gen, which is where problems can arise).

As with any optimization, profile and benchmark.

EDIT: If you don't actually need the hard guarantee that a value will be computed at-most-once (i.e. an at-least-zero-times guarantee) something like this (sparingly...)

private var _thing: Type = null
def thing: Type = {
  if (_thing eq null) {
    _thing = ???
  }
  _thing
}

might actually be worth trying and fit the single-threaded intuition about how lazy val works.


问:
Making the web page fixed and un-zoomable
I need to make my web page un-zoomable such that the divs don't collide each other or overlap each other. The stackoverflow site is a great example. In a case where the user zooms in the web page, the divs should be in their original positions.

When Zoomed, it's still fixed.

What changes do I need to make to the CSS files?

答:
```<code><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'/>
</code>```

this should work

Body:
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'/>

this should work


问:
Hibernate generating Oracle queries with "(+)" operator instead join
I have to do some improvements to a legacy project using Hibernate 5.0.9 with Oracle database. I noticed that hibernate are generating queries like that

```<code>select a.id, b.id
from a, b
where a.b_id = b.id(+)
</code>```

instead of

```<code>select a.id, b.id
from a left join b on a.b_id = b.id
</code>```

for a code like this

```<code>Criteria criteria = session.createCriteria(A.class, "a");
criteria.createAlias("a.b", "b", JoinType.LEFT_OUTER_JOIN);
criteria = criteria.setProjection(
       Projections.projectionList()
         .add(Projections.property("a.id"))
         .add(Projections.property("b.id"))
  );

criteria.list();
</code>```

At least for me, the "(+)" is complicated to read.
In other project with same Hibernate version, the "joins" are generated normally.

Is this a Hibernate configuration?

答:
Following the suggestion, adding the dialect information in persistence.xml solved the problem.</p>
```<code><property name="hibernate.dialect" value="org.hibernate.dialect.Oracle12cDialect" />
</code>```

Body:
I have to do some improvements to a legacy project using Hibernate 5.0.9 with Oracle database. I noticed that hibernate are generating queries like that

select a.id, b.id
from a, b
where a.b_id = b.id(+)

instead of

select a.id, b.id
from a left join b on a.b_id = b.id

for a code like this

Criteria criteria = session.createCriteria(A.class, "a");
criteria.createAlias("a.b", "b", JoinType.LEFT_OUTER_JOIN);
criteria = criteria.setProjection(
       Projections.projectionList()
         .add(Projections.property("a.id"))
         .add(Projections.property("b.id"))
  );

criteria.list();

At least for me, the "(+)" is complicated to read.
In other project with same Hibernate version, the "joins" are generated normally.

Is this a Hibernate configuration?

Body:
Following the suggestion, adding the dialect information in persistence.xml solved the problem.

<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle12cDialect" />


问:
NodeJS Worker Threads and AWS Lambda: Inconsistent Output
I am trying to execute the following AWS lambda:

index.js:

```<code>const test = require("test");

exports.handler = async (event) => {
await test.main();

};
</code>```

test.js:

```<code>const {Worker} = require("worker_threads");

const main = () => {

    let num1 = 10;
    let num2 = 20;

    const worker = new Worker("./worker.js", {workerData: {num1, num2}});

    worker.on("message", result => {
        console.log(`${num1} + ${num2} is ${result}`);
    });

    worker.on("error", error => {
        console.log(error);
    });

    worker.on("exit", exitCode => {
        console.log(exitCode);
    });

    console.log("I am here!");
}

exports.main = main;
</code>```

worker.js:

```<code>const {parentPort, workerData} = require("worker_threads");

const getSum = (num1, num2) => {
    return num1 + num2;
}

parentPort.postMessage(getSum(workerData.num1, workerData.num2));
</code>```

When I run the same program on my laptop it is working fine. I see the output of the worker thread consistently.

Output on my laptop:

```<code>❯ node index.js
I am here!
10 + 20 is 30
0
</code>```

Output on the lambda:

```<code>START RequestId: c178d74b-da57-4765-9fa7-77d3fc83d645 Version: $LATEST
2021-08-31T14:33:37.353Z    c178d74b-da57-4765-9fa7-77d3fc83d645    INFO    I am here!
END RequestId: c178d74b-da57-4765-9fa7-77d3fc83d645
REPORT RequestId: c178d74b-da57-4765-9fa7-77d3fc83d645  Duration: 2.12 ms   Billed Duration: 3 ms   Memory Size: 10240 MB   Max Memory Used: 108 MB
</code>```

When I run the lambda, the output is very random. Sometimes I see the output of the worker thread and other times I don't.

Why is there a difference in execution of the program on AWS lambda and on my laptop?

答:
You don't await for the worker async operation to complete in the test.js file. Try adding a promise that resolves when worker finishes. Like this:

const { Worker } = require("worker_threads");

const main = async () => {
  let num1 = 10;
  let num2 = 20;

  const worker = new Worker("./worker.js", { workerData: { num1, num2 } });

  worker.on("message", (result) => {
    console.log(`${num1} + ${num2} is ${result}`);
  });

  worker.on("error", (error) => {
    console.log(error);
  });

  console.log("I am here!");

  // Awaiting for the worker to finish here
  return new Promise((resolve) => {
    worker.on("exit", (exitCode) => {
      console.log(exitCode);
      resolve();
    });
  });
};

exports.main = main;

```

Alternatively, you can set context.doNotWaitForEmptyEventLoop = false but it's not recommended as it's error-prone and hard to debug.

Body:
I am trying to execute the following AWS lambda:

index.js:

const test = require("test");

exports.handler = async (event) => {
await test.main();

};

test.js:

const {Worker} = require("worker_threads");

const main = () => {

    let num1 = 10;
    let num2 = 20;

    const worker = new Worker("./worker.js", {workerData: {num1, num2}});

    worker.on("message", result => {
        console.log(`${num1} + ${num2} is ${result}`);
    });

    worker.on("error", error => {
        console.log(error);
    });

    worker.on("exit", exitCode => {
        console.log(exitCode);
    });

    console.log("I am here!");
}

exports.main = main;

worker.js:

const {parentPort, workerData} = require("worker_threads");

const getSum = (num1, num2) => {
    return num1 + num2;
}

parentPort.postMessage(getSum(workerData.num1, workerData.num2));

When I run the same program on my laptop it is working fine. I see the output of the worker thread consistently.

Output on my laptop:

❯ node index.js
I am here!
10 + 20 is 30
0

Output on the lambda:

START RequestId: c178d74b-da57-4765-9fa7-77d3fc83d645 Version: $LATEST
2021-08-31T14:33:37.353Z    c178d74b-da57-4765-9fa7-77d3fc83d645    INFO    I am here!
END RequestId: c178d74b-da57-4765-9fa7-77d3fc83d645
REPORT RequestId: c178d74b-da57-4765-9fa7-77d3fc83d645  Duration: 2.12 ms   Billed Duration: 3 ms   Memory Size: 10240 MB   Max Memory Used: 108 MB

When I run the lambda, the output is very random. Sometimes I see the output of the worker thread and other times I don't.

Why is there a difference in execution of the program on AWS lambda and on my laptop?

Body:
You don't await for the worker async operation to complete in the test.js file. Try adding a promise that resolves when worker finishes. Like this:

const { Worker } = require("worker_threads");

const main = async () => {
  let num1 = 10;
  let num2 = 20;

  const worker = new Worker("./worker.js", { workerData: { num1, num2 } });

  worker.on("message", (result) => {
    console.log(`${num1} + ${num2} is ${result}`);
  });

  worker.on("error", (error) => {
    console.log(error);
  });

  console.log("I am here!");

  // Awaiting for the worker to finish here
  return new Promise((resolve) => {
    worker.on("exit", (exitCode) => {
      console.log(exitCode);
      resolve();
    });
  });
};

exports.main = main;

Alternatively, you can set context.doNotWaitForEmptyEventLoop = false but it's not recommended as it's error-prone and hard to debug.