问:
pandas keyError "Retries" error while saving DataFrame to s3 in .csv format
I'm getting a keyError while trying to save my dataframe to s3 in .csv format using .to_csv function of pandas. Versions of packages installed in python environment are listed below:

pandas: 1.3.4

botocore: 1.23.48

fsspec: 2022.1.0

s3fs: 2022.1.0

Complete error text is attached below. Let me know if you guys need any other information.

```<code>---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_1428/656569756.py in <module>
----> 1 _df.iloc[:10].to_csv('s3://***confidential path***/{}/{}.csv'.format(d_i, d_i))

~\anaconda3\lib\site-packages\pandas\core\generic.py in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, line_terminator, chunksize, date_format, doublequote, escapechar, decimal, errors, storage_options)
   3464         )
   3465
-> 3466         return DataFrameRenderer(formatter).to_csv(
   3467             path_or_buf,
   3468             line_terminator=line_terminator,

~\anaconda3\lib\site-packages\pandas\io\formats\format.py in to_csv(self, path_or_buf, encoding, sep, columns, index_label, mode, compression, quoting, quotechar, line_terminator, chunksize, date_format, doublequote, escapechar, errors, storage_options)
   1103             formatter=self.fmt,
   1104         )
-> 1105         csv_formatter.save()
   1106
   1107         if created_buffer:

~\anaconda3\lib\site-packages\pandas\io\formats\csvs.py in save(self)
    235         """
    236         # apply compression and byte/text conversion
--> 237         with get_handle(
    238             self.filepath_or_buffer,
    239             self.mode,

~\anaconda3\lib\site-packages\pandas\io\common.py in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
    607
    608     # open URLs
--> 609     ioargs = _get_filepath_or_buffer(
    610         path_or_buf,
    611         encoding=encoding,

~\anaconda3\lib\site-packages\pandas\io\common.py in _get_filepath_or_buffer(filepath_or_buffer, encoding, compression, mode, storage_options)
    356
    357         try:
--> 358             file_obj = fsspec.open(
    359                 filepath_or_buffer, mode=fsspec_mode, **(storage_options or {})
    360             ).open()

~\anaconda3\lib\site-packages\fsspec\core.py in open(urlpath, mode, compression, encoding, errors, protocol, newline, **kwargs)
    454     ``OpenFile`` object.
    455     """
--> 456     return open_files(
    457         urlpath=[urlpath],
    458         mode=mode,

~\anaconda3\lib\site-packages\fsspec\core.py in open_files(urlpath, mode, compression, encoding, errors, name_function, num, protocol, newline, auto_mkdir, expand, **kwargs)
    297     if "r" not in mode and auto_mkdir:
    298         parents = {fs._parent(path) for path in paths}
--> 299         [fs.makedirs(parent, exist_ok=True) for parent in parents]
    300     return OpenFiles(
    301         [

~\anaconda3\lib\site-packages\fsspec\core.py in <listcomp>(.0)
    297     if "r" not in mode and auto_mkdir:
    298         parents = {fs._parent(path) for path in paths}
--> 299         [fs.makedirs(parent, exist_ok=True) for parent in parents]
    300     return OpenFiles(
    301         [

~\anaconda3\lib\site-packages\fsspec\asyn.py in wrapper(*args, **kwargs)
     89     def wrapper(*args, **kwargs):
     90         self = obj or args[0]
---> 91         return sync(self.loop, func, *args, **kwargs)
     92
     93     return wrapper

~\anaconda3\lib\site-packages\fsspec\asyn.py in sync(loop, func, timeout, *args, **kwargs)
     69         raise FSTimeoutError from return_result
     70     elif isinstance(return_result, BaseException):
---> 71         raise return_result
     72     else:
     73         return return_result

~\anaconda3\lib\site-packages\fsspec\asyn.py in _runner(event, coro, result, timeout)
     23         coro = asyncio.wait_for(coro, timeout=timeout)
     24     try:
---> 25         result[0] = await coro
     26     except Exception as ex:
     27         result[0] = ex

~\anaconda3\lib\site-packages\s3fs\core.py in _makedirs(self, path, exist_ok)
    744     async def _makedirs(self, path, exist_ok=False):
    745         try:
--> 746             await self._mkdir(path, create_parents=True)
    747         except FileExistsError:
    748             if exist_ok:

~\anaconda3\lib\site-packages\s3fs\core.py in _mkdir(self, path, acl, create_parents, **kwargs)
    729                         "LocationConstraint": region_name
    730                     }
--> 731                 await self._call_s3("create_bucket", **params)
    732                 self.invalidate_cache("")
    733                 self.invalidate_cache(bucket)

~\anaconda3\lib\site-packages\s3fs\core.py in _call_s3(self, method, *akwarglist, **kwargs)
    279                 err = e
    280         err = translate_boto_error(err)
--> 281         raise err
    282
    283     call_s3 = sync_wrapper(_call_s3)

~\anaconda3\lib\site-packages\s3fs\core.py in _call_s3(self, method, *akwarglist, **kwargs)
    259         for i in range(self.retries):
    260             try:
--> 261                 out = await method(**additional_kwargs)
    262                 return out
    263             except S3_RETRYABLE_ERRORS as e:

~\anaconda3\lib\site-packages\aiobotocore\client.py in _make_api_call(self, operation_name, api_params)
    171             http, parsed_response = event_response
    172         else:
--> 173             http, parsed_response = await self._make_request(
    174                 operation_model, request_dict, request_context)
    175

~\anaconda3\lib\site-packages\aiobotocore\client.py in _make_request(self, operation_model, request_dict, request_context)
    191     async def _make_request(self, operation_model, request_dict, request_context):
    192         try:
--> 193             return await self._endpoint.make_request(operation_model, request_dict)
    194         except Exception as e:
    195             await self.meta.events.emit(

~\anaconda3\lib\site-packages\aiobotocore\endpoint.py in _send_request(self, request_dict, operation_model)
     79         success_response, exception = await self._get_response(
     80             request, operation_model, context)
---> 81         while await self._needs_retry(attempts, operation_model,
     82                                       request_dict, success_response,
     83                                       exception):

~\anaconda3\lib\site-packages\aiobotocore\endpoint.py in _needs_retry(self, attempts, operation_model, request_dict, response, caught_exception)
    211             service_id,
    212             operation_model.name)
--> 213         responses = await self._event_emitter.emit(
    214             event_name, response=response, endpoint=self,
    215             operation=operation_model, attempts=attempts,

~\anaconda3\lib\site-packages\aiobotocore\hooks.py in _emit(self, event_name, kwargs, stop_on_response)
     27                 response = await handler(**kwargs)
     28             else:
---> 29                 response = handler(**kwargs)
     30
     31             responses.append((handler, response))

~\anaconda3\lib\site-packages\botocore\retryhandler.py in __call__(self, attempts, response, caught_exception, **kwargs)
    187         }
    188         if isinstance(self._checker, MaxAttemptsDecorator):
--> 189             retries_context = kwargs['request_dict']['context']['retries']
    190             checker_kwargs.update({'retries_context': retries_context})
    191

KeyError: 'retries'
</code>```

答:
Context: I am running python code on AWS Sagemaker reading in data from S3 using pandas. Had the same issue as yourself.

It appears the KeyError is being raised due to an internal issue in the library aibotocore. This is an open issue as of now: Github

A potential work around is to pin the versions of the libraries as suggested on the thread. The exact set that worked for me was:

```<code>pip install pandas==1.2.5 --use-deprecated=legacy-resolver
pip install fsspec==2022.1.0 --use-deprecated=legacy-resolver
pip install s3fs==2022.1.0 --use-deprecated=legacy-resolver
pip install aiobotocore==2.1.0 --use-deprecated=legacy-resolver
pip install awscli==1.22.24 --use-deprecated=legacy-resolver
pip install boto3==1.20.24 --use-deprecated=legacy-resolver
</code>```

Hope it works for you!

Body:
I'm getting a keyError while trying to save my dataframe to s3 in .csv format using .to_csv function of pandas. Versions of packages installed in python environment are listed below:

pandas: 1.3.4

botocore: 1.23.48

fsspec: 2022.1.0

s3fs: 2022.1.0

Complete error text is attached below. Let me know if you guys need any other information.

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_1428/656569756.py in <module>
----> 1 _df.iloc[:10].to_csv('s3://***confidential path***/{}/{}.csv'.format(d_i, d_i))

~\anaconda3\lib\site-packages\pandas\core\generic.py in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, line_terminator, chunksize, date_format, doublequote, escapechar, decimal, errors, storage_options)
   3464         )
   3465
-> 3466         return DataFrameRenderer(formatter).to_csv(
   3467             path_or_buf,
   3468             line_terminator=line_terminator,

~\anaconda3\lib\site-packages\pandas\io\formats\format.py in to_csv(self, path_or_buf, encoding, sep, columns, index_label, mode, compression, quoting, quotechar, line_terminator, chunksize, date_format, doublequote, escapechar, errors, storage_options)
   1103             formatter=self.fmt,
   1104         )
-> 1105         csv_formatter.save()
   1106
   1107         if created_buffer:

~\anaconda3\lib\site-packages\pandas\io\formats\csvs.py in save(self)
    235         """
    236         # apply compression and byte/text conversion
--> 237         with get_handle(
    238             self.filepath_or_buffer,
    239             self.mode,

~\anaconda3\lib\site-packages\pandas\io\common.py in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
    607
    608     # open URLs
--> 609     ioargs = _get_filepath_or_buffer(
    610         path_or_buf,
    611         encoding=encoding,

~\anaconda3\lib\site-packages\pandas\io\common.py in _get_filepath_or_buffer(filepath_or_buffer, encoding, compression, mode, storage_options)
    356
    357         try:
--> 358             file_obj = fsspec.open(
    359                 filepath_or_buffer, mode=fsspec_mode, **(storage_options or {})
    360             ).open()

~\anaconda3\lib\site-packages\fsspec\core.py in open(urlpath, mode, compression, encoding, errors, protocol, newline, **kwargs)
    454     ``OpenFile`` object.
    455     """
--> 456     return open_files(
    457         urlpath=[urlpath],
    458         mode=mode,

~\anaconda3\lib\site-packages\fsspec\core.py in open_files(urlpath, mode, compression, encoding, errors, name_function, num, protocol, newline, auto_mkdir, expand, **kwargs)
    297     if "r" not in mode and auto_mkdir:
    298         parents = {fs._parent(path) for path in paths}
--> 299         [fs.makedirs(parent, exist_ok=True) for parent in parents]
    300     return OpenFiles(
    301         [

~\anaconda3\lib\site-packages\fsspec\core.py in <listcomp>(.0)
    297     if "r" not in mode and auto_mkdir:
    298         parents = {fs._parent(path) for path in paths}
--> 299         [fs.makedirs(parent, exist_ok=True) for parent in parents]
    300     return OpenFiles(
    301         [

~\anaconda3\lib\site-packages\fsspec\asyn.py in wrapper(*args, **kwargs)
     89     def wrapper(*args, **kwargs):
     90         self = obj or args[0]
---> 91         return sync(self.loop, func, *args, **kwargs)
     92
     93     return wrapper

~\anaconda3\lib\site-packages\fsspec\asyn.py in sync(loop, func, timeout, *args, **kwargs)
     69         raise FSTimeoutError from return_result
     70     elif isinstance(return_result, BaseException):
---> 71         raise return_result
     72     else:
     73         return return_result

~\anaconda3\lib\site-packages\fsspec\asyn.py in _runner(event, coro, result, timeout)
     23         coro = asyncio.wait_for(coro, timeout=timeout)
     24     try:
---> 25         result[0] = await coro
     26     except Exception as ex:
     27         result[0] = ex

~\anaconda3\lib\site-packages\s3fs\core.py in _makedirs(self, path, exist_ok)
    744     async def _makedirs(self, path, exist_ok=False):
    745         try:
--> 746             await self._mkdir(path, create_parents=True)
    747         except FileExistsError:
    748             if exist_ok:

~\anaconda3\lib\site-packages\s3fs\core.py in _mkdir(self, path, acl, create_parents, **kwargs)
    729                         "LocationConstraint": region_name
    730                     }
--> 731                 await self._call_s3("create_bucket", **params)
    732                 self.invalidate_cache("")
    733                 self.invalidate_cache(bucket)

~\anaconda3\lib\site-packages\s3fs\core.py in _call_s3(self, method, *akwarglist, **kwargs)
    279                 err = e
    280         err = translate_boto_error(err)
--> 281         raise err
    282
    283     call_s3 = sync_wrapper(_call_s3)

~\anaconda3\lib\site-packages\s3fs\core.py in _call_s3(self, method, *akwarglist, **kwargs)
    259         for i in range(self.retries):
    260             try:
--> 261                 out = await method(**additional_kwargs)
    262                 return out
    263             except S3_RETRYABLE_ERRORS as e:

~\anaconda3\lib\site-packages\aiobotocore\client.py in _make_api_call(self, operation_name, api_params)
    171             http, parsed_response = event_response
    172         else:
--> 173             http, parsed_response = await self._make_request(
    174                 operation_model, request_dict, request_context)
    175

~\anaconda3\lib\site-packages\aiobotocore\client.py in _make_request(self, operation_model, request_dict, request_context)
    191     async def _make_request(self, operation_model, request_dict, request_context):
    192         try:
--> 193             return await self._endpoint.make_request(operation_model, request_dict)
    194         except Exception as e:
    195             await self.meta.events.emit(

~\anaconda3\lib\site-packages\aiobotocore\endpoint.py in _send_request(self, request_dict, operation_model)
     79         success_response, exception = await self._get_response(
     80             request, operation_model, context)
---> 81         while await self._needs_retry(attempts, operation_model,
     82                                       request_dict, success_response,
     83                                       exception):

~\anaconda3\lib\site-packages\aiobotocore\endpoint.py in _needs_retry(self, attempts, operation_model, request_dict, response, caught_exception)
    211             service_id,
    212             operation_model.name)
--> 213         responses = await self._event_emitter.emit(
    214             event_name, response=response, endpoint=self,
    215             operation=operation_model, attempts=attempts,

~\anaconda3\lib\site-packages\aiobotocore\hooks.py in _emit(self, event_name, kwargs, stop_on_response)
     27                 response = await handler(**kwargs)
     28             else:
---> 29                 response = handler(**kwargs)
     30
     31             responses.append((handler, response))

~\anaconda3\lib\site-packages\botocore\retryhandler.py in __call__(self, attempts, response, caught_exception, **kwargs)
    187         }
    188         if isinstance(self._checker, MaxAttemptsDecorator):
--> 189             retries_context = kwargs['request_dict']['context']['retries']
    190             checker_kwargs.update({'retries_context': retries_context})
    191

KeyError: 'retries'

Body:
Context: I am running python code on AWS Sagemaker reading in data from S3 using pandas. Had the same issue as yourself.

It appears the KeyError is being raised due to an internal issue in the library aibotocore. This is an open issue as of now: Github

A potential work around is to pin the versions of the libraries as suggested on the thread. The exact set that worked for me was:

pip install pandas==1.2.5 --use-deprecated=legacy-resolver
pip install fsspec==2022.1.0 --use-deprecated=legacy-resolver
pip install s3fs==2022.1.0 --use-deprecated=legacy-resolver
pip install aiobotocore==2.1.0 --use-deprecated=legacy-resolver
pip install awscli==1.22.24 --use-deprecated=legacy-resolver
pip install boto3==1.20.24 --use-deprecated=legacy-resolver

Hope it works for you!


来源: stackexchange

问:
How to determine if a property is required?
Given a class something like this:

```<code>public class MyClass : ValidationValues
{
    public string Foo { get; set; }

    [Required(ErrorMessage = "Bar is required.")]
    public string Bar { get; set; }

    // and many more
}

public class ValidationValues
{
    public bool IsValid { get; set; } = true;
    public string InvalidReason { get; set; }
}
</code>```

I need to determine if a property is required while looping over it as a generic list. By looking into the Watch, I've figured out one way, but it feels clunky, and I'm thinking it should be simpler.

For some context, this logic is inside of an Azure Function. So no Views, no MVC, etc. The function is a Blob Storage trigger that picks up a .CSV file with a | delimited list which gets deserialized into a List<MyClass>. We do not want to enforce the Required attributes at deserialization because we want more granular control.

So given a file like this:

```<code>value1 | |
value2 | something

</code>```

What eventually gets sent back to the user is something like this:

```<code>[
    {
        "foo": "value1",
        "bar": "",
        "isValid": false,
        "InvalidReason" : "Bar is required"
    },
    {
        "foo": "value2",
        "bar": "something",
        "isValid": true,
        "InvalidReason" : ""
    }
]

</code>```

Here's what I have now:

```<code>foreach (T item in itemList) // where 'itemList' is a List<T> and in this case T is MyClass
{
    foreach (PropertyInfo property in item.GetType().GetProperties())
    {
        if (property.CustomAttributes.ToList()[0].AttributeType.Name == "RequiredAttribute")
        {
             // validate, log, populate ValidationValues
        }
    }
}

</code>```

This is the part I don't like:

```<code>property.CustomAttributes.ToList()[0].AttributeType.Name == "RequiredAttribute"
</code>```

Sometimes when I figure out a coding challenge, I tell myself, "This is the way". But in this case, I'm pretty sure this isn't the way.

答:
You can rewrite that line using GetCustomAttibute:

```<code>using System.Reflection;
foreach (T item in itemList) // where 'itemList' is a List<T> and in this case T is MyClass
{
    foreach (PropertyInfo property in item.GetType().GetProperties())
    {
        var attribute = property.GetCustomAttibute<RequiredAttribute>();
    }
}
</code>```

ContentLicense: CC BY-SA 4.0

Body:
Given a class something like this:

public class MyClass : ValidationValues
{
    public string Foo { get; set; }

    [Required(ErrorMessage = "Bar is required.")]
    public string Bar { get; set; }

    // and many more
}

public class ValidationValues
{
    public bool IsValid { get; set; } = true;
    public string InvalidReason { get; set; }
}

I need to determine if a property is required while looping over it as a generic list. By looking into the Watch, I've figured out one way, but it feels clunky, and I'm thinking it should be simpler.

For some context, this logic is inside of an Azure Function. So no Views, no MVC, etc. The function is a Blob Storage trigger that picks up a .CSV file with a | delimited list which gets deserialized into a List<MyClass>. We do not want to enforce the Required attributes at deserialization because we want more granular control.

So given a file like this:

value1 | |
value2 | something

What eventually gets sent back to the user is something like this:

[
    {
        "foo": "value1",
        "bar": "",
        "isValid": false,
        "InvalidReason" : "Bar is required"
    },
    {
        "foo": "value2",
        "bar": "something",
        "isValid": true,
        "InvalidReason" : ""
    }
]

Here's what I have now:

foreach (T item in itemList) // where 'itemList' is a List<T> and in this case T is MyClass
{
    foreach (PropertyInfo property in item.GetType().GetProperties())
    {
        if (property.CustomAttributes.ToList()[0].AttributeType.Name == "RequiredAttribute")
        {
             // validate, log, populate ValidationValues
        }
    }
}

This is the part I don't like:

property.CustomAttributes.ToList()[0].AttributeType.Name == "RequiredAttribute"

Sometimes when I figure out a coding challenge, I tell myself, "This is the way". But in this case, I'm pretty sure this isn't the way.

Body:
You can rewrite that line using GetCustomAttibute:

using System.Reflection;
foreach (T item in itemList) // where 'itemList' is a List<T> and in this case T is MyClass
{
    foreach (PropertyInfo property in item.GetType().GetProperties())
    {
        var attribute = property.GetCustomAttibute<RequiredAttribute>();
    }
}


问:
Do I need to pack/unpack data read from a binary file before performing bitwise operations?
I've read two bytes from a binary file into $data and now need to perform a bitwise and.

But when I do

```<code>if (($data & "\x100") != 0) {
</code>```

I get the error Argument "@\0" isn't numeric in numeric ne (!=)

Am I supposed to convert $data first?

答:
Yes, if you have two literal bytes which should be interpreted as a 16-bit number, you need to unpack it.

On top of that, what ThiefMaster said.

ContentLicense: CC BY-SA 3.0

Body:
I've read two bytes from a binary file into $data and now need to perform a bitwise and.

But when I do

if (($data & "\x100") != 0) {

I get the error Argument "@\0" isn't numeric in numeric ne (!=)

Am I supposed to convert $data first?


问:
Move button when clicked
How do I move the button from the div with id of two to the div with id of one when I click the button?

  <button onclick="moveMe"></button>

```
<pre class="lang-js prettyprint-override"><code>function moveMe() {
 // ??
}
</code>```

答:
function moveMe() {
  const divTwo = document.getElementById("two")
  const divOne = document.getElementById("one")

  const newButton = document.createElement("button")
  newButton.innerText = "Click me"

  divOne.appendChild(newButton)
  divTwo.children[1].remove()
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div id="one">
<p>
div one
</p>
</div>

<div id="two">
<p>
div two
</p>
  <button onclick="moveMe()">Click me</button>
</div></code>```

Title: Move button when clicked

Body:
How do I move the button from the div with id of two to the div with id of one when I click the button?

  <button onclick="moveMe"></button>

function moveMe() {
 // ??
}

Body:
function moveMe() {
  const divTwo = document.getElementById("two")
  const divOne = document.getElementById("one")

  const newButton = document.createElement("button")
  newButton.innerText = "Click me"

  divOne.appendChild(newButton)
  divTwo.children[1].remove()
}

<div id="one">
<p>
div one
</p>
</div>

<div id="two">
<p>
div two
</p>
  <button onclick="moveMe()">Click me</button>
</div>


问:
Get Original URL before being redirected by IIS
Using IIS, we're currently redirecting URLs. But to display canonical URL's in the site for SEO, we want the original URL.

Example:
Actual URL: www.example.com/topsellers/mobiles
Redirected URL: www.example.com/products?type=mobiles

IIS Rewrite Rules:

```<code><rule name="url1" stopProcessing="true">
            <match url="topsellers/([a-zA-Z0-9]+)" />
            <action type="Rewrite" url="products?type={R:1}" />
        </rule>
</code>```

Is there a way in C# to get the Actual URL in the controller?

I have tried using HttpContext.Current.Request, HttpRequest, and many other objects from C#, but couldn't access the required one. Can anyone help me out with this, if there's any solution?

Technology Stack being used: ASP NET CORE V2.2.1

答:
```<code>@using Microsoft.AspNetCore.Http.Features

@{var path = Context.Request.HttpContext.Features.Get<IHttpRequestFeature>().RawTarget.ToString();}
</code>```

This is how I was able to get the actual URL that the user can see in the address bar after a rewrite to a different page/URL. (This is written in a Razor .cshtml view)

Body:
Using IIS, we're currently redirecting URLs. But to display canonical URL's in the site for SEO, we want the original URL.

Example:
Actual URL: www.example.com/topsellers/mobiles
Redirected URL: www.example.com/products?type=mobiles

IIS Rewrite Rules:

<rule name="url1" stopProcessing="true">
            <match url="topsellers/([a-zA-Z0-9]+)" />
            <action type="Rewrite" url="products?type={R:1}" />
        </rule>

Is there a way in C# to get the Actual URL in the controller?

I have tried using HttpContext.Current.Request, HttpRequest, and many other objects from C#, but couldn't access the required one. Can anyone help me out with this, if there's any solution?

Technology Stack being used: ASP NET CORE V2.2.1

Body:
@using Microsoft.AspNetCore.Http.Features

@{var path = Context.Request.HttpContext.Features.Get<IHttpRequestFeature>().RawTarget.ToString();}

This is how I was able to get the actual URL that the user can see in the address bar after a rewrite to a different page/URL. (This is written in a Razor .cshtml view)


问:
Wordpress ACF oEmbed field - Embedding Bandcamp content
I've set up with Advanced Custom Field a custom oEmbed field, called embed.

And it's displayed in the front end in the simplest way :

```<code>the_field('embed');
</code>```

Pasting the media url from Youtube, Vimeo or Souncloud works fine and the field displays  the player and it's content correctly.

However embedding Bandcamp content doesn't work.
If i paste the url directly, it just displays the url and doesn't grab the player from Bandcamp.
If i paste the iframe html provided by Bandcamp, it shows the html.

So i'm wondering if there is a workaround.

答:
As pointed out by Roberto oEmbed doesn't support Bandcamp.

Changing the field type from oEmbed to WYSIWYG did the trick for me and rendered bandcamp iframe, displaying the player as intended.

Body:
I've set up with Advanced Custom Field a custom oEmbed field, called embed.

And it's displayed in the front end in the simplest way :

the_field('embed');

Pasting the media url from Youtube, Vimeo or Souncloud works fine and the field displays  the player and it's content correctly.

However embedding Bandcamp content doesn't work.
If i paste the url directly, it just displays the url and doesn't grab the player from Bandcamp.
If i paste the iframe html provided by Bandcamp, it shows the html.

So i'm wondering if there is a workaround.


问:
Basic User Authentication for Static Site using AWS & S3 Bucket
I am looking to add Basic User Authentication to a Static Site I will have up on AWS so that only those with the proper username + password which I will supply to those users have access to see the site. I found s3auth and it seems to be exactly what I am looking for, however, I am wondering if I will need to somehow set the authorization for pages besides the index.html. For example, I have 3 pages- index, about and contact.html, without authentication setup for about.html what is stopping an individual for directly accessing the site via www.mywebsite.com/about.html? I am more so looking for clarification or any resources anyone can provide to explain this!

Thank you for your help!

答:
By now, this is also possible with CloudFront functions which I like more because it reduces the complexity even more (from what is already not too complex with Lambda). Here's my writeup on what I just did...

It's basically 3 things that need to be done:

Create a CloudFront function to add Basic Auth into the request.

Configure the Origin of the CloudFront distribution correctly in a few places.

Activate the CloudFront function.
That's it, no particular bells & whistles otherwise. Here's what I've done:

First, go to CloudFront, then click on Functions on the left, create a new function with a name of your choice (no region etc. necessary) and then add the following as the code of the function:

```<code>function handler(event) {

    var user = "myuser";
    var pass = "mypassword";

    function encodeToBase64(str) {
        var chars =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        for (
            // initialize result and counter
            var block, charCode, idx = 0, map = chars, output = "";
            // if the next str index does not exist:
            //   change the mapping table to "="
            //   check if d has no fractional digits
            str.charAt(idx | 0) || ((map = "="), idx % 1);
            // "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
            output += map.charAt(63 & (block >> (8 - (idx % 1) * 8)))
        ) {
        charCode = str.charCodeAt((idx += 3 / 4));
        if (charCode > 0xff) {
            throw new InvalidCharacterError("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."
        );
        }
        block = (block << 8) | charCode;
      }
      return output;
    }

    var requiredBasicAuth = "Basic " + encodeToBase64(`${user}:${pass}`);
    var match = false;
    if (event.request.headers.authorization) {
        if (event.request.headers.authorization.value === requiredBasicAuth) {
            match = true;
        }
    }

    if (!match) {
      return {
        statusCode: 401,
        statusDescription: "Unauthorized",
        headers: {
          "www-authenticate": { value: "Basic" },
        },
      };
    }

    return event.request;
}
</code>```

Then you can test with directly on the UI and assuming it works and assuming you have customized username and password, publish the function.

Please note that I have found individual pieces of the function above on the Internet so this is not my own code (other than piecing it together). I wish I would still find the sources so I can quote them here but I can't find them anymore. Credits to the creators though! :-)

Next, open your CloudFront distribution and do the following:

Make sure your S3 bucket in the origin is configured as a REST endpoint and not a website endpoint, i.e. it must end on .s3.amazonaws.com and not have the word website in the hostname.

Also in the Origin settings, under "S3 bucket access", select "Yes use OAI (bucket can restrict access to only CloudFront)". In the setting below click on "Create OAI" to create a new OAI (unless you have an existing one and know what you're doing). And select "Yes, update the bucket policy" to allow AWS to add the necessary permissions to your OAI.

Finally, open your Behavior of the CloudFront distribution and scroll to the bottom. Under "Function associations", for "Viewer request" select "CloudFront Function" and select your newly created CloudFront function. Save your changes.

And that should be it. With a bit of luck a matter of a couple of minutes (realistically more, I know) and especially not additional complexity once this is all set up.

Body:
By now, this is also possible with CloudFront functions which I like more because it reduces the complexity even more (from what is already not too complex with Lambda). Here's my writeup on what I just did...

It's basically 3 things that need to be done:

Create a CloudFront function to add Basic Auth into the request.

Configure the Origin of the CloudFront distribution correctly in a few places.

Activate the CloudFront function.
That's it, no particular bells & whistles otherwise. Here's what I've done:

First, go to CloudFront, then click on Functions on the left, create a new function with a name of your choice (no region etc. necessary) and then add the following as the code of the function:

function handler(event) {

    var user = "myuser";
    var pass = "mypassword";

    function encodeToBase64(str) {
        var chars =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        for (
            // initialize result and counter
            var block, charCode, idx = 0, map = chars, output = "";
            // if the next str index does not exist:
            //   change the mapping table to "="
            //   check if d has no fractional digits
            str.charAt(idx | 0) || ((map = "="), idx % 1);
            // "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
            output += map.charAt(63 & (block >> (8 - (idx % 1) * 8)))
        ) {
        charCode = str.charCodeAt((idx += 3 / 4));
        if (charCode > 0xff) {
            throw new InvalidCharacterError("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."
        );
        }
        block = (block << 8) | charCode;
      }
      return output;
    }

    var requiredBasicAuth = "Basic " + encodeToBase64(`${user}:${pass}`);
    var match = false;
    if (event.request.headers.authorization) {
        if (event.request.headers.authorization.value === requiredBasicAuth) {
            match = true;
        }
    }

    if (!match) {
      return {
        statusCode: 401,
        statusDescription: "Unauthorized",
        headers: {
          "www-authenticate": { value: "Basic" },
        },
      };
    }

    return event.request;
}

Then you can test with directly on the UI and assuming it works and assuming you have customized username and password, publish the function.

Please note that I have found individual pieces of the function above on the Internet so this is not my own code (other than piecing it together). I wish I would still find the sources so I can quote them here but I can't find them anymore. Credits to the creators though! :-)

Next, open your CloudFront distribution and do the following:

Make sure your S3 bucket in the origin is configured as a REST endpoint and not a website endpoint, i.e. it must end on .s3.amazonaws.com and not have the word website in the hostname.

Also in the Origin settings, under "S3 bucket access", select "Yes use OAI (bucket can restrict access to only CloudFront)". In the setting below click on "Create OAI" to create a new OAI (unless you have an existing one and know what you're doing). And select "Yes, update the bucket policy" to allow AWS to add the necessary permissions to your OAI.

Finally, open your Behavior of the CloudFront distribution and scroll to the bottom. Under "Function associations", for "Viewer request" select "CloudFront Function" and select your newly created CloudFront function. Save your changes.

And that should be it. With a bit of luck a matter of a couple of minutes (realistically more, I know) and especially not additional complexity once this is all set up.


问:
PreFlush / OnFlush / PostFlush Symfony Doctrine
How can we edit manually final SQL query before execution in Symfony / Doctrine?

I need to change value MANUALLY in SQL, i know i can edit it postPersist in entity, but i'm in a special case that this solution is not working in my schema .

A small idea :

```<code>public function preFlush/onFlush/PostFlush((PreFlushEventArgs/.../...)  $eventArgs) {
    $em = $eventArgs->getEntityManager();
    $uow = $em->getUnitOfWork();

    foreach ($uow->getScheduledEntityInsertions() as $entity) {

        if($entity instanceof MyEntity) {
              //Change my value directly in SQL query

        }

    }

}
</code>```

答:
```<code>public function getSubscribedEvents(): array
{
    return [
        Events::preUpdate  => 'preUpdate',
    ];
}

public function preUpdate(LifecycleEventArgs $args): void
{
    $object = $args->getObject();

    if ($object instanceof MyEntity) {
        $object->setSomething('something');

        $meta = $args->getObjectManager()->getClassMetadata(MyEntity::class);
        $om->getUnitOfWork()->recomputeSingleEntityChangeSet($meta, $object);
    }
}
</code>```

Body:
How can we edit manually final SQL query before execution in Symfony / Doctrine?

I need to change value MANUALLY in SQL, i know i can edit it postPersist in entity, but i'm in a special case that this solution is not working in my schema .

A small idea :

public function preFlush/onFlush/PostFlush((PreFlushEventArgs/.../...)  $eventArgs) {
    $em = $eventArgs->getEntityManager();
    $uow = $em->getUnitOfWork();

    foreach ($uow->getScheduledEntityInsertions() as $entity) {

        if($entity instanceof MyEntity) {
              //Change my value directly in SQL query

        }

    }

}


问:
Removing the space between a plot generated by R and the caption in an Rtex file
I am trying to edit an Rtex file and I'm creating plots. I have a plot below, which does compile. However, there is an empty white space below the caption. I can't figure out how to remove it. I have tried to search for solutions but to no avail:

https://support.sisense.com/kb/en/article/remove-whitespace-margins-from-plotly-charts - Relevant but for plotly. Was not
able to adapt it to my R code.

http://ostack.cn/qa/?qa=829717/ - Involved Python rather than R. I
could not adapt the solution into the R plot.

https://tex.stackexchange.com/questions/273786/latex-files-generated-by-tikzdevice-leaves-large-space-between-image-and-caption - Involved tikzpicture instead of R. I could not adapt the solution to the R plot.

The code which I'm running as well as the output is shown below. I am grateful if someone can help me. I have also tried the trimmws() function but it gave the following error:

```<code>## "Error in mysub(paste0("^", whitespace, "+"), x):  argument"x" is missing, with no default."
</code>```

```<code>\documentclass[a4paper,11pt]{article}

%Packages
\usepackage[justification=centering]{caption}
\usepackage{blindtext}
\begin{document}
\title{F-distribution}
\maketitle
\section{Diagram showing the $F$-distribution for a selected pair of values of the degree of freedom}
\blindtext
\begin{figure}[h!]
<<echo=FALSE, cache=TRUE, fig.width=5, fig.height=5>>=
curve(df(x, df1=20, df2=20))
@
\caption{Hi}
\end{figure}
\blindtext
\blindtext
\end{document}
</code>```

答:
Your image does have quite a lot of white space around it. You can move the caption further up with this quick hack:

```<code>\documentclass[a4paper,11pt]{article}

%Packages
\usepackage[justification=centering]{caption}
\usepackage{blindtext}
\begin{document}
\title{F-distribution}
\maketitle
\section{Diagram showing the $F$-distribution for a selected pair of values of the degree of freedom}
\blindtext
\begin{figure}[h!]
<<echo=FALSE, cache=TRUE, fig.width=5, fig.height=5>>=
curve(df(x, df1=20, df2=20))
@
\vskip-1cm
\caption{Hi}
\end{figure}
\blindtext
\blindtext
\end{document}
</code>```

Body:
I am trying to edit an Rtex file and I'm creating plots. I have a plot below, which does compile. However, there is an empty white space below the caption. I can't figure out how to remove it. I have tried to search for solutions but to no avail:

https://support.sisense.com/kb/en/article/remove-whitespace-margins-from-plotly-charts - Relevant but for plotly. Was not
able to adapt it to my R code.

http://ostack.cn/qa/?qa=829717/ - Involved Python rather than R. I
could not adapt the solution into the R plot.

https://tex.stackexchange.com/questions/273786/latex-files-generated-by-tikzdevice-leaves-large-space-between-image-and-caption - Involved tikzpicture instead of R. I could not adapt the solution to the R plot.

The code which I'm running as well as the output is shown below. I am grateful if someone can help me. I have also tried the trimmws() function but it gave the following error:

## "Error in mysub(paste0("^", whitespace, "+"), x):  argument"x" is missing, with no default."

\documentclass[a4paper,11pt]{article}

%Packages
\usepackage[justification=centering]{caption}
\usepackage{blindtext}
\begin{document}
\title{F-distribution}
\maketitle
\section{Diagram showing the $F$-distribution for a selected pair of values of the degree of freedom}
\blindtext
\begin{figure}[h!]
<<echo=FALSE, cache=TRUE, fig.width=5, fig.height=5>>=
curve(df(x, df1=20, df2=20))
@
\caption{Hi}
\end{figure}
\blindtext
\blindtext
\end{document}

Body:
Your image does have quite a lot of white space around it. You can move the caption further up with this quick hack:

\documentclass[a4paper,11pt]{article}

%Packages
\usepackage[justification=centering]{caption}
\usepackage{blindtext}
\begin{document}
\title{F-distribution}
\maketitle
\section{Diagram showing the $F$-distribution for a selected pair of values of the degree of freedom}
\blindtext
\begin{figure}[h!]
<<echo=FALSE, cache=TRUE, fig.width=5, fig.height=5>>=
curve(df(x, df1=20, df2=20))
@
\vskip-1cm
\caption{Hi}
\end{figure}
\blindtext
\blindtext
\end{document}


问:
Is the .NET SDK not backwards compatible with older runtimes?
We have installed .NET desktop runtime 6.0.1 on our machines. Building happens in a docker container with an image built on mcr.microsoft.com/dotnet/sdk:6.0-windowsservercore-ltsc2022, which was apparently updated to SDK 6.0.102.
Now our app will crash at startup saying

```<code>Unhandled exception. System.IO.FileLoadException: Could not load file or assembly 'PresentationFramework, Version=6.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
</code>```

Local builds work just fine, I have SDK 6.0.101 installed.

The target framework in the project is defined as

```<code><TargetFramework>net6.0-windows</TargetFramework>
</code>```

so I thought it should run on any .NET 6.0, not only on 6.0.2, no matter with which SDK it's built.
According to MS Docs it should even run on 6.1 by default, unless a different <RollForward> policy is defined.

What are we missing?

Edit:
It must be something with WPF, because console applications built on that container work just fine...

答:
This seems to be a known bug in .net 6.0.2

see:
https://github.com/dotnet/core/issues/7172#issuecomment-1034105137

for a workaround you could try this:
https://github.com/dotnet/winforms/issues/6663#issuecomment-1034371295

Or just use the sdk:6.0.101-windowsservercore-ltsc2022 image till a fixed version is released

Body:
We have installed .NET desktop runtime 6.0.1 on our machines. Building happens in a docker container with an image built on mcr.microsoft.com/dotnet/sdk:6.0-windowsservercore-ltsc2022, which was apparently updated to SDK 6.0.102.
Now our app will crash at startup saying

Unhandled exception. System.IO.FileLoadException: Could not load file or assembly 'PresentationFramework, Version=6.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)

Local builds work just fine, I have SDK 6.0.101 installed.

The target framework in the project is defined as

<TargetFramework>net6.0-windows</TargetFramework>

so I thought it should run on any .NET 6.0, not only on 6.0.2, no matter with which SDK it's built.
According to MS Docs it should even run on 6.1 by default, unless a different <RollForward> policy is defined.

What are we missing?

Edit:
It must be something with WPF, because console applications built on that container work just fine...


问:
SwiftUI MVVM With ObservedObject
UPDATE (2/10/22 @ 11:30am CST):

I rewrote the workflow so that my viewModel conforms to the ObservableObject protocol and no longer conforms to the CreateUser class. Now my variables are updating properly (tested via print("\(viewModel.loadingStatus)") both before and after the loadingState value is set.

However, my view that uses a switch statement to show the current state of the view (i.e. switches on loadingState) does not update, even though the value of loadingState in the viewModel is being set correctly and I'm able to use that print statement to show that it's being set, in the view itself.

I even tried setting changing the viewModel's @Published var loadingState to read as:

```<code>@Published var loadingState: LoadingState = .loading {
    willSet {
        objectwillchange.send()
    }
}
</code>```

Of course, according to Apple's docs, that's exactly the same behavior as @Published by itself, and it did nothing to solve the issue.

It seems as though everything else is working properly, except for the view switch case not updating when loadingState changes in the viewModel.

Original Post:

I have a multi-page form where all of the views of the form share the same ObservableObject(s). What I would like to do is split up each individual view into an MVVM workflow so that the UI stuff is in the view and the data stuff is in the viewModel.

The issue is that since all of these views share the same ObservableObject(s), and the functions in each of the views make heavy use of these ObservableObjects (storing variables, using other functions, etc.), the viewModel can't just be an ObservableObject of its own, because I would need to write a custom intializer for everything (and there are a lot of variables). This also means that in my NavigationLink as well as the view's preview, I would have to call to every single one of those individually initialized vars.

CreateUser (the shared ObservableObject that has all of the form variables as @Published vars) is where I am setting all of the form variables as the user moves through the pages. The best I came up with was to conform my viewModel to CreateUser instead of simply ObservableObject. That seemed to work and after quite a bit of recoding, there were no more errors and my app built. That being said, as it's not working, I have a feeling that this is not the correct way to do it.

Unfortunately, it seems that the viewModel is not updating any of the CreateUser variables, nor is it accurately referencing the CreateUser variables that are being set. I tested this by using page 3 of the form (where the username is entered) as the test for the MVVM architecture, and asked it to print the variable from page 2. It returned the default value instead of what was actually set.

This means my data is not being passed through properly and my variables are not being referenced the way they should, so page 3 (the MVVM view) is stuck showing the loading state, even though it should show the loaded state once the json fetch is finished.

What is the proper procedure for instituting an MVVM workflow when you have multiple other ObservedObjects being utilized in each view? This is for the latest SwiftUI/XCode for iOS 15+.

Here's a bit of code to further explain (obviously very stripped down for this post, but you get the idea if you read my explanation above):

Main View:

```<code>import Combine
import SwiftUI

struct UsernameView: View {

@StateObject var viewModel: ViewModel

    var body: some View {
        VStack {
            List {
                switch viewModel.loadingState {
                    case .loading:
                        Section {
                            Text("I am loading")
                        }

                    case .loaded:
                        Section {
                            Text("I am loaded")
                        }

                    case .failed:
                        Section {
                            Text("Something went wrong")
                        }
                }
            }
        }
        .task {
            viewModel.userActivity = Date.now
            jsonFetch.sink (receiveCompletion: { completion in
                switch completion {
                    case .failure:
                        viewModel.loadingState = .failed
                    case .finished:
                        viewModel.loadingState = .loaded
                    }
                },
                receiveValue: { loadedData in
                    viewModel.userData = loadedData
                }).store(in: &viewModel.dataTransfer.requests)
        }
    }
}
</code>```

View Model:

```<code>import Combine
import SwiftUI

extension UsernameView {
    class ViewModel: CreateUser {

        @ObservedObject var textBindingManager = TextBindingManager(limit: 15)

        @Published var loadingState: LoadingState = .loading
        @Published var userData: [Usernames] = []
        @Published var usernameExists: [UsernameExists] = []

        func editingChanged(_ value: String) {
            username = String(value.prefix(textBindingManager.characterLimit))

            let jsonFetchUserExistsURL = URL(string: "https://api.foo.com/userselect?user=\(user)")
            let jsonFetchUserExistsTask = dataTransfer.jsonFetch(jsonFetchUserExistsURL, defaultValue: [UsernameExists]())

            if username.count >= 8 {
                guard network.isNetworkActive else { loadingAlert = true; return }
                Task {
                    status = .loading
                    usernameExists.removeAll()
                    jsonFetchUserExistsTask.sink (receiveCompletion: { completion in
                        switch completion {
                        case .failure:
                            self.loadingState = .failed
                        case .finished:
                            return
                        }
                    },
                    receiveValue: { loadedUserExists in
                        self.usernameExists = loadedUserExists
                    }).store(in: &dataTransfer.requests)
                }
            }
        }

extension UsernameView.ViewModel {
    enum LoadingState {
        case loading, loaded, failed
    }
}
</code>```

CreateUser:

```<code>import Combine
import SwiftUI

class CreateUser: ObservableObject {

    @ObservedObject var dataTransfer = NetworkTransfer()
    let network: NetworkMonitor

    init(network: NetworkMonitor) {
        self.network = network
    }

    @Published var userActivity = Date.now
    @Published var userActivityAlert = false
    @Published var birthday = Date()
    @Published var username = ""
}
</code>```

答:
We don't use MVVM in SwiftUI, SwiftUI is managing our Views on screen like UILabels etc for us, we work with the view data in View structs, hence no need for extra view model objects. We also don't use sink in our ObservableObjects we instead assign to @Published which completes the pipleline and ties its life-cycle to the object (no need for cancellables).

Since you are using async/await in a task you don't even need Combine's ObservableObject anymore and can simply use the .task modifier and set the result on a @State.

Take a look at task(id:priority:) and you'll notice it can re-run the task when ever the id param changes and cancelling previous tasks. It's now as simple as setting result of your await to an @State which can either be the data array or a custom struct containing the data and other related vars.

We do however use an ObservableObject reference type to manage the life cycle of our value-type model data, i.e. to persist or sync it.

Title: SwiftUI MVVM With ObservedObject

Body:
UPDATE (2/10/22 @ 11:30am CST):

I rewrote the workflow so that my viewModel conforms to the ObservableObject protocol and no longer conforms to the CreateUser class. Now my variables are updating properly (tested via print("\(viewModel.loadingStatus)") both before and after the loadingState value is set.

However, my view that uses a switch statement to show the current state of the view (i.e. switches on loadingState) does not update, even though the value of loadingState in the viewModel is being set correctly and I'm able to use that print statement to show that it's being set, in the view itself.

I even tried setting changing the viewModel's @Published var loadingState to read as:

@Published var loadingState: LoadingState = .loading {
    willSet {
        objectwillchange.send()
    }
}

Of course, according to Apple's docs, that's exactly the same behavior as @Published by itself, and it did nothing to solve the issue.

It seems as though everything else is working properly, except for the view switch case not updating when loadingState changes in the viewModel.

Original Post:

I have a multi-page form where all of the views of the form share the same ObservableObject(s). What I would like to do is split up each individual view into an MVVM workflow so that the UI stuff is in the view and the data stuff is in the viewModel.

The issue is that since all of these views share the same ObservableObject(s), and the functions in each of the views make heavy use of these ObservableObjects (storing variables, using other functions, etc.), the viewModel can't just be an ObservableObject of its own, because I would need to write a custom intializer for everything (and there are a lot of variables). This also means that in my NavigationLink as well as the view's preview, I would have to call to every single one of those individually initialized vars.

CreateUser (the shared ObservableObject that has all of the form variables as @Published vars) is where I am setting all of the form variables as the user moves through the pages. The best I came up with was to conform my viewModel to CreateUser instead of simply ObservableObject. That seemed to work and after quite a bit of recoding, there were no more errors and my app built. That being said, as it's not working, I have a feeling that this is not the correct way to do it.

Unfortunately, it seems that the viewModel is not updating any of the CreateUser variables, nor is it accurately referencing the CreateUser variables that are being set. I tested this by using page 3 of the form (where the username is entered) as the test for the MVVM architecture, and asked it to print the variable from page 2. It returned the default value instead of what was actually set.

This means my data is not being passed through properly and my variables are not being referenced the way they should, so page 3 (the MVVM view) is stuck showing the loading state, even though it should show the loaded state once the json fetch is finished.

What is the proper procedure for instituting an MVVM workflow when you have multiple other ObservedObjects being utilized in each view? This is for the latest SwiftUI/XCode for iOS 15+.

Here's a bit of code to further explain (obviously very stripped down for this post, but you get the idea if you read my explanation above):

Main View:

import Combine
import SwiftUI

struct UsernameView: View {

@StateObject var viewModel: ViewModel

    var body: some View {
        VStack {
            List {
                switch viewModel.loadingState {
                    case .loading:
                        Section {
                            Text("I am loading")
                        }

                    case .loaded:
                        Section {
                            Text("I am loaded")
                        }

                    case .failed:
                        Section {
                            Text("Something went wrong")
                        }
                }
            }
        }
        .task {
            viewModel.userActivity = Date.now
            jsonFetch.sink (receiveCompletion: { completion in
                switch completion {
                    case .failure:
                        viewModel.loadingState = .failed
                    case .finished:
                        viewModel.loadingState = .loaded
                    }
                },
                receiveValue: { loadedData in
                    viewModel.userData = loadedData
                }).store(in: &viewModel.dataTransfer.requests)
        }
    }
}

View Model:

import Combine
import SwiftUI

extension UsernameView {
    class ViewModel: CreateUser {

        @ObservedObject var textBindingManager = TextBindingManager(limit: 15)

        @Published var loadingState: LoadingState = .loading
        @Published var userData: [Usernames] = []
        @Published var usernameExists: [UsernameExists] = []

        func editingChanged(_ value: String) {
            username = String(value.prefix(textBindingManager.characterLimit))

            let jsonFetchUserExistsURL = URL(string: "https://api.foo.com/userselect?user=\(user)")
            let jsonFetchUserExistsTask = dataTransfer.jsonFetch(jsonFetchUserExistsURL, defaultValue: [UsernameExists]())

            if username.count >= 8 {
                guard network.isNetworkActive else { loadingAlert = true; return }
                Task {
                    status = .loading
                    usernameExists.removeAll()
                    jsonFetchUserExistsTask.sink (receiveCompletion: { completion in
                        switch completion {
                        case .failure:
                            self.loadingState = .failed
                        case .finished:
                            return
                        }
                    },
                    receiveValue: { loadedUserExists in
                        self.usernameExists = loadedUserExists
                    }).store(in: &dataTransfer.requests)
                }
            }
        }

extension UsernameView.ViewModel {
    enum LoadingState {
        case loading, loaded, failed
    }
}

CreateUser:

import Combine
import SwiftUI

class CreateUser: ObservableObject {

    @ObservedObject var dataTransfer = NetworkTransfer()
    let network: NetworkMonitor

    init(network: NetworkMonitor) {
        self.network = network
    }

    @Published var userActivity = Date.now
    @Published var userActivityAlert = false
    @Published var birthday = Date()
    @Published var username = ""
}


问:
Rasterize polygons based on maximum overlap (using R packages terra or stars)
I have a question concerning rasterization of polygons by maximum overlap, i.e assign the value of the polygon that has the highst area overlap with the raster cell.

The real world exercise is to rasterize polygons of soil-IDs in R, in order to produce relatively low resolution maps of soil properties as model inputs.

The problem is that the rasterize() function of the terra package (and similar stars' st_rasterize()) assigns the cell value from the polygon that contains the cell midpoint. If a raster cell contains multiple polygons, I would rather like to select the value of the polygon (soil-ID), which has the highest aerea cover in a raster cell.

Here is a small self-contained example that visualizes my problem, using terra.

```<code>library(terra)

f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
r <- rast(v, ncols = 3, nrow = 3)
rcc <- vect(xyFromCell(r, cell = 1:ncell(r)))

x <- rasterize(v, r, field = "NAME_2")
plot(x)
lines(r, col = "light gray")
lines(v)
points(rcc)
</code>```

Mostly, the polygons that contain the cell center also seem to have the highest area share. However, in some cases (top row, 3rd cell), this is not the case.
The problem appears to get worse the bigger the cells are compared with the polygons. I could therefore start with high resolution raster, and than resample to the desired (lower) resolution, using an aggregation function (e.g. the mode). But, maybe someone has a more efficient idea?

Thank you for your help!

答:
Please find one possible solution using terra and sf libraries.

The idea is to convert the SpatRaster r into a SpatVector and then into an sf object in order to take advantage of the sf::st_join() function using the largest = TRUE argument. The rest of the code then consists of simply converting the sf object back into a SpatVector and then a SpatRaster using the terra::rasterize() function.

So, please find below a reprex that details the procedure.

Reprex

Code

library(terra)
library(sf)

# Your data
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
r <- rast(v, ncols = 3, nrow = 3)
rcc <- vect(xyFromCell(r, cell = 1:ncell(r)))

# Convert the 'SpatRaster' 'r' into a 'SpatVector (i.e. 'r_poly')
r_poly <- terra::as.polygons(r)

# Convert 'r_poly' into a 'sf' object (i.e. 'r_poly_sf')
r_poly_sf <- sf::st_as_sf(r_poly)

# Convert 'v' into a 'sf' object (i.e. 'v_sf')
v_sf <- sf::st_as_sf(v)

# Left join r_poly_sf with v_sf based on the largest overlap
results_sf <- sf::st_join(r_poly_sf, v_sf, largest = TRUE)

# Convert 'results_sf' into a SpatVector (i.e. 'results_vect')
results_vect <- terra::vect(results_sf)

# Rasterize 'results_vect' to get a 'SpatRaster' (i.e. 'results')
results <- terra::rasterize(results_vect, r, field = "NAME_2")
```
<ul>
<li><p>Output</p>
<p>NB: please note that the cell of the upper right corner is <code>NA</code> because no polygon from <code>r</code> overlaps <code>v</code> (if needed you can still set the value for cells that do not overlap by using the <code>background=</code> argument inside the <code>terra::rasterize()</code> function).</p>
</li>
</ul>
<pre class="lang-r prettyprint-override"><code>results
#> class       : SpatRaster
#> dimensions  : 3, 3, 1  (nrow, ncol, nlyr)
#> resolution  : 0.2613707, 0.2446047  (x, y)
#> extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> source      : memory
#> name        :   NAME_2
#> min value   : Capellen
#> max value   :   Remich

terra::values(results, dataframe=TRUE)
#>       NAME_2
#> 1   Clervaux
#> 2   Clervaux
#> 3       <NA>
#> 4    Redange
#> 5     Mersch
#> 6 Echternach
#> 7   Capellen
#> 8 Luxembourg
#> 9     Remich
</code>```

Visualization

plot(results)
lines(r, col = "light gray")
lines(v)
points(rcc)
```
<p><img src="https://i.imgur.com/jMmZyRh.png" alt="" /></p>
<p><sup>Created on 2022-02-10 by the reprex package (v2.0.1)</sup></p>
<hr />
<p>Exactly the same as above but with <code>rast(v, ncols = 5, nrow =5)</code> to get a result that can be compared with the figure you gave in your question.</p>
<pre class="lang-r prettyprint-override"><code>library(terra)
library(sf)

# Your data
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
r <- rast(v, ncols = 5, nrow = 5)
rcc <- vect(xyFromCell(r, cell = 1:ncell(r)))

# Convert the 'SpatRaster' 'r' into a 'SpatVector (i.e. 'r_poly')
r_poly <- terra::as.polygons(r)

# Convert 'r_poly' into a 'sf' object (i.e. 'r_poly_sf')
r_poly_sf <- sf::st_as_sf(r_poly)

# Convert 'v' into a 'sf' object (i.e. 'v_sf')
v_sf <- sf::st_as_sf(v)

# Left join r_poly_sf with v_sf based on the largest overlap
results_sf <- sf::st_join(r_poly_sf, v_sf, largest = TRUE)
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries

# Convert 'results_sf' into a SpatVector (i.e. 'results_vect')
results_vect <- terra::vect(results_sf)

# Rasterize 'results_vect' to get a 'SpatRaster' (i.e. 'results')
results <- terra::rasterize(results_vect, r, field = "NAME_2")
results
#> class       : SpatRaster
#> dimensions  : 5, 5, 1  (nrow, ncol, nlyr)
#> resolution  : 0.1568224, 0.1467628  (x, y)
#> extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> source      : memory
#> name        :   NAME_2
#> min value   : Capellen
#> max value   :    Wiltz

terra::values(results, dataframe=TRUE)
#>              NAME_2
#> 1          Clervaux
#> 2          Clervaux
#> 3          Clervaux
#> 4              <NA>
#> 5              <NA>
#> 6             Wiltz
#> 7             Wiltz
#> 8           Vianden
#> 9           Vianden
#> 10             <NA>
#> 11          Redange
#> 12          Redange
#> 13           Mersch
#> 14       Echternach
#> 15       Echternach
#> 16         Capellen
#> 17         Capellen
#> 18       Luxembourg
#> 19     Grevenmacher
#> 20     Grevenmacher
#> 21 Esch-sur-Alzette
#> 22 Esch-sur-Alzette
#> 23 Esch-sur-Alzette
#> 24           Remich
#> 25           Remich

plot(results)
lines(r, col = "light gray")
lines(v)
points(rcc)
</code>```

Created on 2022-02-10 by the reprex package (v2.0.1)

Body:
I have a question concerning rasterization of polygons by maximum overlap, i.e assign the value of the polygon that has the highst area overlap with the raster cell.

The real world exercise is to rasterize polygons of soil-IDs in R, in order to produce relatively low resolution maps of soil properties as model inputs.

The problem is that the rasterize() function of the terra package (and similar stars' st_rasterize()) assigns the cell value from the polygon that contains the cell midpoint. If a raster cell contains multiple polygons, I would rather like to select the value of the polygon (soil-ID), which has the highest aerea cover in a raster cell.

Here is a small self-contained example that visualizes my problem, using terra.

library(terra)

f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
r <- rast(v, ncols = 3, nrow = 3)
rcc <- vect(xyFromCell(r, cell = 1:ncell(r)))

x <- rasterize(v, r, field = "NAME_2")
plot(x)
lines(r, col = "light gray")
lines(v)
points(rcc)

Mostly, the polygons that contain the cell center also seem to have the highest area share. However, in some cases (top row, 3rd cell), this is not the case.
The problem appears to get worse the bigger the cells are compared with the polygons. I could therefore start with high resolution raster, and than resample to the desired (lower) resolution, using an aggregation function (e.g. the mode). But, maybe someone has a more efficient idea?

Thank you for your help!

Body:
Please find one possible solution using terra and sf libraries.

The idea is to convert the SpatRaster r into a SpatVector and then into an sf object in order to take advantage of the sf::st_join() function using the largest = TRUE argument. The rest of the code then consists of simply converting the sf object back into a SpatVector and then a SpatRaster using the terra::rasterize() function.

So, please find below a reprex that details the procedure.

Reprex

Code

library(terra)
library(sf)

# Your data
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
r <- rast(v, ncols = 3, nrow = 3)
rcc <- vect(xyFromCell(r, cell = 1:ncell(r)))

# Convert the 'SpatRaster' 'r' into a 'SpatVector (i.e. 'r_poly')
r_poly <- terra::as.polygons(r)

# Convert 'r_poly' into a 'sf' object (i.e. 'r_poly_sf')
r_poly_sf <- sf::st_as_sf(r_poly)

# Convert 'v' into a 'sf' object (i.e. 'v_sf')
v_sf <- sf::st_as_sf(v)

# Left join r_poly_sf with v_sf based on the largest overlap
results_sf <- sf::st_join(r_poly_sf, v_sf, largest = TRUE)

# Convert 'results_sf' into a SpatVector (i.e. 'results_vect')
results_vect <- terra::vect(results_sf)

# Rasterize 'results_vect' to get a 'SpatRaster' (i.e. 'results')
results <- terra::rasterize(results_vect, r, field = "NAME_2")

Output

NB: please note that the cell of the upper right corner is NA because no polygon from r overlaps v (if needed you can still set the value for cells that do not overlap by using the background= argument inside the terra::rasterize() function).

results
#> class       : SpatRaster
#> dimensions  : 3, 3, 1  (nrow, ncol, nlyr)
#> resolution  : 0.2613707, 0.2446047  (x, y)
#> extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> source      : memory
#> name        :   NAME_2
#> min value   : Capellen
#> max value   :   Remich

terra::values(results, dataframe=TRUE)
#>       NAME_2
#> 1   Clervaux
#> 2   Clervaux
#> 3       <NA>
#> 4    Redange
#> 5     Mersch
#> 6 Echternach
#> 7   Capellen
#> 8 Luxembourg
#> 9     Remich

Visualization

plot(results)
lines(r, col = "light gray")
lines(v)
points(rcc)

Created on 2022-02-10 by the reprex package (v2.0.1)

Exactly the same as above but with rast(v, ncols = 5, nrow =5) to get a result that can be compared with the figure you gave in your question.

library(terra)
library(sf)

# Your data
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
r <- rast(v, ncols = 5, nrow = 5)
rcc <- vect(xyFromCell(r, cell = 1:ncell(r)))

# Convert the 'SpatRaster' 'r' into a 'SpatVector (i.e. 'r_poly')
r_poly <- terra::as.polygons(r)

# Convert 'r_poly' into a 'sf' object (i.e. 'r_poly_sf')
r_poly_sf <- sf::st_as_sf(r_poly)

# Convert 'v' into a 'sf' object (i.e. 'v_sf')
v_sf <- sf::st_as_sf(v)

# Left join r_poly_sf with v_sf based on the largest overlap
results_sf <- sf::st_join(r_poly_sf, v_sf, largest = TRUE)
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries

# Convert 'results_sf' into a SpatVector (i.e. 'results_vect')
results_vect <- terra::vect(results_sf)

# Rasterize 'results_vect' to get a 'SpatRaster' (i.e. 'results')
results <- terra::rasterize(results_vect, r, field = "NAME_2")
results
#> class       : SpatRaster
#> dimensions  : 5, 5, 1  (nrow, ncol, nlyr)
#> resolution  : 0.1568224, 0.1467628  (x, y)
#> extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> source      : memory
#> name        :   NAME_2
#> min value   : Capellen
#> max value   :    Wiltz

terra::values(results, dataframe=TRUE)
#>              NAME_2
#> 1          Clervaux
#> 2          Clervaux
#> 3          Clervaux
#> 4              <NA>
#> 5              <NA>
#> 6             Wiltz
#> 7             Wiltz
#> 8           Vianden
#> 9           Vianden
#> 10             <NA>
#> 11          Redange
#> 12          Redange
#> 13           Mersch
#> 14       Echternach
#> 15       Echternach
#> 16         Capellen
#> 17         Capellen
#> 18       Luxembourg
#> 19     Grevenmacher
#> 20     Grevenmacher
#> 21 Esch-sur-Alzette
#> 22 Esch-sur-Alzette
#> 23 Esch-sur-Alzette
#> 24           Remich
#> 25           Remich

plot(results)
lines(r, col = "light gray")
lines(v)
points(rcc)

Created on 2022-02-10 by the reprex package (v2.0.1)


问:
Microsoft Graph api responsible for creating notifications doesn’t work on mobile
I am working with below MS Graph API.

```<code>POST https://graph.microsoft.com/beta/teams/7155e3c8-175e-4311-97ef-572edc3aa3db/sendActivityNotification
Content-Type: application/json

{
    "topic": {
        "source": "text",
        "value": "Weekly Virtual Social",
        "webUrl": "Teams webUrl"
    },
    "previewText": {
        "content": "It will be fun!"
    },
    "activityType": "eventCreated",
    "recipient": {
        "@odata.type": "microsoft.graph.channelMembersNotificationRecipient",
        "teamId": "7155e3c8-175e-4311-97ef-572edc3aa3db",
        "channelId": "19:0ea5de04de4743bcb4cd20cb99235d99@thread.tacv2"
    }
}
</code>```

Documentation here https://learn.microsoft.com/en-us/graph/teams-send-activityfeednotifications

I am successfully able to run the API for my bot application in Teams Desktop APP - meaning the notifications created by the app are visible in the activity feed in the MS desktop app.

But the same notifications are not seen in the mobile app of MS Teams for both Android and iOS.

To be noted: I can see the push notification on the iOS but if click on the banner it opens up the Teams App but that particular notification is not present there.

Is that the limitation from the API or something else that needed to be configured?

答:
The behaviour is quite strange here, If the payload has a topic like below that is "source": "text" will show the push notifications but not in the feed.

```<code>"topic": {
  "source": "text",
  "value": "Weekly Virtual Social",
  "webUrl": "Teams webUrl"
},
</code>```

But, after updating the topic like below source: 'entityUrl' works and show the notifications in the feed.

```<code>topic: {
   source: 'entityUrl',
   value: `https://graph.microsoft.com/v1.0/teams/...../`,
},
</code>```

Just a note, if one wants to link the notification to a certain message - meaning if you click the notification it'll take to that particular message. Use the below value.

https://graph.microsoft.com/v1.0/teams/${team_id}/channels/${channel_id}/messages/${reply_to_id}

Body:
I am working with below MS Graph API.

POST https://graph.microsoft.com/beta/teams/7155e3c8-175e-4311-97ef-572edc3aa3db/sendActivityNotification
Content-Type: application/json

{
    "topic": {
        "source": "text",
        "value": "Weekly Virtual Social",
        "webUrl": "Teams webUrl"
    },
    "previewText": {
        "content": "It will be fun!"
    },
    "activityType": "eventCreated",
    "recipient": {
        "@odata.type": "microsoft.graph.channelMembersNotificationRecipient",
        "teamId": "7155e3c8-175e-4311-97ef-572edc3aa3db",
        "channelId": "19:0ea5de04de4743bcb4cd20cb99235d99@thread.tacv2"
    }
}

Documentation here https://learn.microsoft.com/en-us/graph/teams-send-activityfeednotifications

I am successfully able to run the API for my bot application in Teams Desktop APP - meaning the notifications created by the app are visible in the activity feed in the MS desktop app.

But the same notifications are not seen in the mobile app of MS Teams for both Android and iOS.

To be noted: I can see the push notification on the iOS but if click on the banner it opens up the Teams App but that particular notification is not present there.

Is that the limitation from the API or something else that needed to be configured?

Body:
The behaviour is quite strange here, If the payload has a topic like below that is "source": "text" will show the push notifications but not in the feed.

"topic": {
  "source": "text",
  "value": "Weekly Virtual Social",
  "webUrl": "Teams webUrl"
},

But, after updating the topic like below source: 'entityUrl' works and show the notifications in the feed.

topic: {
   source: 'entityUrl',
   value: `https://graph.microsoft.com/v1.0/teams/...../`,
},

Just a note, if one wants to link the notification to a certain message - meaning if you click the notification it'll take to that particular message. Use the below value.

https://graph.microsoft.com/v1.0/teams/${team_id}/channels/${channel_id}/messages/${reply_to_id}


问:
XAMPP Mysql can't running on Mac
Im using XAMPP 7.4 on My Macbook air 2020
i've tried so many ways or command like :

sudo killall mysqld
sudo /Applications/XAMPP/xamppfiles/bin/mysql.server start

but it keep won't running.

答:
Try:

Open XAMPP -> Services -> MySQL -> Start

or

open terminal in your XAMPP (button Open Terminal) and restart mysql

```<code>systemctl restart mysql
</code>```

Title: XAMPP Mysql can't running on Mac

Body:
Try:

Open XAMPP -> Services -> MySQL -> Start

or

open terminal in your XAMPP (button Open Terminal) and restart mysql

systemctl restart mysql


问:
assert properties of a object are non-null using a function's arguments
I want to achieve a type-safe API that takes a generic and only executes if a condition is met:

```<code>const doSomethingWith = (data: unknown) => data

const fn = <T extends unknown>(data: T, opts: { if: boolean }): void => {
  if (opts.if == false) return;

  doSomethingWith(data)
}
</code>```

How can I make opts.if a valid predicate in this context? Consider the following cases:

```<code>type Data = { abc: string }

// should be valid - checks to make sure data.abc is non-null
const data: Partial<Data> = {}
fn<Data>(data, { if: data.abc != null  })

// should be valid - all data is given
const data2: Data = { abc: '123' }
fn<Data>(data2)

// should be invalid - no null check occurs
const data3: Partial<Data> = {}
fn<Data>(data3)
</code>```

Typescript Playground.

答:
I would approach this by making the opts.if argument be a function as opposed to a static boolean value.

This way, it's very simple to make the predicate match the type of the data that is passed as first argument.

const doSomethingWith = <T>(data: T) => data

const fn = <T>(data: T, opts?: { if: (t: T) => boolean }): void => {
  if (opts?.if(data) === false) return;

  doSomethingWith(data)
}

type Data = { abc: string }

const data: Partial<Data> = {}
const data2: Data = { abc: '123' }
const data3: Partial<Data> = {}

fn(data, { if: d => d.abc != null  }) // OK, valid predicate
fn(data2) // OK, no predicate
fn(data3, { if: d => d.foobar != null  }) // NOT OK, predicte doesn't match data
```

Playground

Body:
I want to achieve a type-safe API that takes a generic and only executes if a condition is met:

const doSomethingWith = (data: unknown) => data

const fn = <T extends unknown>(data: T, opts: { if: boolean }): void => {
  if (opts.if == false) return;

  doSomethingWith(data)
}

How can I make opts.if a valid predicate in this context? Consider the following cases:

type Data = { abc: string }

// should be valid - checks to make sure data.abc is non-null
const data: Partial<Data> = {}
fn<Data>(data, { if: data.abc != null  })

// should be valid - all data is given
const data2: Data = { abc: '123' }
fn<Data>(data2)

// should be invalid - no null check occurs
const data3: Partial<Data> = {}
fn<Data>(data3)

<a href="https://www.typescriptlang.org/play?#code/MYewdgzgLgBAJiAyiAtgUygCwJZgOYDq2WMAvDABRwCGU1AXDAK5gDWYIA7mAJRkB88WtQCwAKHGhIsAGZgyMADwAVGGgAeUNGDgRmbDt35VhjZQBoYIAA5QIAfkYBvGNhmMARiBAAbNNXkAXx5GADcQbDgBGCdxGFcZShs7ewA6NzJyGWofCDQ+ACcMJgKwAG5xOPgkVAwcfCIsEzoecUDKsSgAT2s0GAARYQUXag9gRmgC3DwYdokxAHoFmAhMECYfKI8+0JzImABaGGBMNGBWPSgQGBRqVj6IEr6aOlTR4Fc9DjADsA2fSTgaBCOiMAAK1AKUGwOUUgzognITjmcjhwmML2olhcbkYmLeYxgAEJyH8fD54sEOksVmsNlsdnsokcchTMZ8YHhsKFtIDpCDqAAmRjw6jDGDvRgAcgAjIKAMxS2biVGijHCQWteY01brTYwbauMC7Hz7I4cGBkiknM6sKzAYAlCB84GY+XgyHQ2Fq4YosBohHNajynhAA" rel="nofollow noreferrer">Typescript Playground.

Body:
I would approach this by making the opts.if argument be a function as opposed to a static boolean value.

This way, it's very simple to make the predicate match the type of the data that is passed as first argument.

const doSomethingWith = <T>(data: T) => data

const fn = <T>(data: T, opts?: { if: (t: T) => boolean }): void => {
  if (opts?.if(data) === false) return;

  doSomethingWith(data)
}

type Data = { abc: string }

const data: Partial<Data> = {}
const data2: Data = { abc: '123' }
const data3: Partial<Data> = {}

fn(data, { if: d => d.abc != null  }) // OK, valid predicate
fn(data2) // OK, no predicate
fn(data3, { if: d => d.foobar != null  }) // NOT OK, predicte doesn't match data

<a href="https://www.typescriptlang.org/play?jsx=0&ssl=18&ssc=81&pln=1&pc=1#code/MYewdgzgLgBAJiAyiAtgUygCwJZgOYDq2WMAvDADwAqAfABRwCGUjAXDFQJRk3zOMBYAFDDQkWADMwZSrQb92VADQwQABygQA-OwDeMbBPZ0oi7qV4AjECAA2aRtIC+ndgDcQ2ODxi7hMAwkYOnVNLQA6Q3kWc3IJRlsING4AJwwAVxSwAG5hf3gkVAwcfCIsaMZOYSc8oSgATzU0GAARfhl9RktgdmgU3DwYGpEhMWg+FnYABUYUqGwEijaWXnJdYbHYJhYAJnZlxg6YLp6YAHIARh2AZjOh0XBx7cZr6dn5xYPV32HaqQqVPpDOxvBZ4OETjAAITkMDpWy2AIuGAAehRMAA8gBpFRuBJeGBqNJwbDAZhoYT-Z47bhozE4mBgECE4mk8mUsAVa6AwIgnxwcISGyWWbQ2HwxFDWnogByGKo9JURLQJOAUGaCDQEDAZ1gKGYwEwE0YQA" rel="nofollow noreferrer">Playground


问:
Specifying Tf.exe and MsBuild version in CC.Net
The problem

I am porting a build to a newer version of CC.Net (from 1.4.4 to a very recent version of 1.6).  Previously the build mixed <msbuild /> tags with <exec /> tags that call batch files with tf.exe commands in them.

The build must have relied on the source control version being the same between the msbuild and tf.exe commands, because now I get an error on the tf.exe command:

Unable to determine the workspace.

When I launch a command prompt, with runas as the same user the service is using, and call the same command the batch file used (from the same directory), I get the same error.

However if I launch a Visual Studio 2010 command prompt, with runas, and call that command (from the same directory), I don't get the error.

This also makes sense, because the %UserProfile%\Local Settings\Application Data\Microsoft\Team Foundation\3.0\Cache\VersionControl.config file has the workspace mapping, but the ...\1.0\Cache\VersionControl.config file does not.

The solution I'd like to try

I can't simply change the PATH for the batch file because it actually needs to build against an older version of Visual Studio, due to licensing.

I think I could solve this by specifying the TFS/msbuild version used with the <msbuild /> tag.  But I'm not sure how to go about this.  I think it would be some sort of server configuration, but I haven't found anything about this in the docs.

My question is: How do I specify the version of TFS that Cruise Control uses for <msbuild /> tags?  Or is there another way to solve this?

答:
As far as I know, there is no way to specify this directly with msbuild. The way it is solved at our place is that we set the executable for msbuild to a bat-file that's basically a copy of the desired vcvars-script (used by Visual Studio Command Prompt to set up the environment).

```<code><tasks>
      <msbuild>
        <executable>vcvars_VC10_amd64.bat</executable>
        <workingDirectory>SomeFolder</workingDirectory>
        <environment>
            <variable name="foo" value="bar" />
        </environment>
        <projectFile>msbuild.proj</projectFile>
        <buildArgs>/m:2  /p:Configuration=Release /p:Platform=x64 /v:normal</buildArgs>
        <targets>Clean;Build;Test</targets>
        <timeout>3600</timeout>
      </msbuild>
  </tasks>
</code>```

Maybe you could get away with setting up your desired paths using the <environment> property as well.

Body:
As far as I know, there is no way to specify this directly with msbuild. The way it is solved at our place is that we set the executable for msbuild to a bat-file that's basically a copy of the desired vcvars-script (used by Visual Studio Command Prompt to set up the environment).

<tasks>
      <msbuild>
        <executable>vcvars_VC10_amd64.bat</executable>
        <workingDirectory>SomeFolder</workingDirectory>
        <environment>
            <variable name="foo" value="bar" />
        </environment>
        <projectFile>msbuild.proj</projectFile>
        <buildArgs>/m:2  /p:Configuration=Release /p:Platform=x64 /v:normal</buildArgs>
        <targets>Clean;Build;Test</targets>
        <timeout>3600</timeout>
      </msbuild>
  </tasks>

Maybe you could get away with setting up your desired paths using the <environment> property as well.


问:
Hosting on amazon s3 for private group
I was researching on hosting a static website on Amazon S3, but needs to make it accessible only for certain group of people who knows credentials.

Couldn't find any option other than IP restrcition below. I can't do this as I don't know IP for every person who would access this.

```<code>{
  "Version": "2012-10-17",
    "Statement": [
      {
        "Sid": "PublicReadGetObject",
        "Effect": "Allow",
        "Principal": "*",
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::testfoo/*",
        "Condition": {
          "IpAddress": {
            "aws:SourceIp": "150.101.204.0/24"
          },
          "NotIpAddress": {
            "aws:SourceIp": "150.101.204.188/32"
          }
        }
      }
    ]
}
</code>```

I am looking for an option like we do it in apache with a .htaccess where basic auth username and password will be requested before granting access.

答:
By now, this has become relatively straightforward on AWS. It's basically 3 things that need to be done:

Create a CloudFront function to add Basic Auth into the request.

Configure the Origin of the CloudFront distribution correctly in a few places.

Activate the CloudFront function.
That's it, no particular bells & whistles otherwise. Here's what I've done:

First, go to CloudFront, then click on Functions on the left, create a new function with a name of your choice (no region etc. necessary) and then add the following as the code of the function:

```<code>function handler(event) {

    var user = "myuser";
    var pass = "mypassword";

    function encodeToBase64(str) {
        var chars =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        for (
            // initialize result and counter
            var block, charCode, idx = 0, map = chars, output = "";
            // if the next str index does not exist:
            //   change the mapping table to "="
            //   check if d has no fractional digits
            str.charAt(idx | 0) || ((map = "="), idx % 1);
            // "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
            output += map.charAt(63 & (block >> (8 - (idx % 1) * 8)))
        ) {
        charCode = str.charCodeAt((idx += 3 / 4));
        if (charCode > 0xff) {
            throw new InvalidCharacterError("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."
        );
        }
        block = (block << 8) | charCode;
      }
      return output;
    }

    var requiredBasicAuth = "Basic " + encodeToBase64(`${user}:${pass}`);
    var match = false;
    if (event.request.headers.authorization) {
        if (event.request.headers.authorization.value === requiredBasicAuth) {
            match = true;
        }
    }

    if (!match) {
      return {
        statusCode: 401,
        statusDescription: "Unauthorized",
        headers: {
          "www-authenticate": { value: "Basic" },
        },
      };
    }

    return event.request;
}
</code>```

Then you can test with directly on the UI and assuming it works and assuming you have customized username and password, publish the function.

Please note that I have found individual pieces of the function above on the Internet so this is not my own code (other than piecing it together). I wish I would still find the sources so I can quote them here but I can't find them anymore. Credits to the creators though! :-)

Next, open your CloudFront distribution and do the following:

Make sure your S3 bucket in the origin is configured as a REST endpoint and not a website endpoint, i.e. it must end on .s3.amazonaws.com and not have the word website in the hostname.

Also in the Origin settings, under "S3 bucket access", select "Yes use OAI (bucket can restrict access to only CloudFront)". In the setting below click on "Create OAI" to create a new OAI (unless you have an existing one and know what you're doing). And select "Yes, update the bucket policy" to allow AWS to add the necessary permissions to your OAI.

Finally, open your Behavior of the CloudFront distribution and scroll to the bottom. Under "Function associations", for "Viewer request" select "CloudFront Function" and select your newly created CloudFront function. Save your changes.

And that should be it. With a bit of luck a matter of a couple of minutes (realistically more, I know) and especially not additional complexity once this is all set up.

Title: Hosting on amazon s3 for private group

Body:
I was researching on hosting a static website on Amazon S3, but needs to make it accessible only for certain group of people who knows credentials.

Couldn't find any option other than IP restrcition below. I can't do this as I don't know IP for every person who would access this.

{
  "Version": "2012-10-17",
    "Statement": [
      {
        "Sid": "PublicReadGetObject",
        "Effect": "Allow",
        "Principal": "*",
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::testfoo/*",
        "Condition": {
          "IpAddress": {
            "aws:SourceIp": "150.101.204.0/24"
          },
          "NotIpAddress": {
            "aws:SourceIp": "150.101.204.188/32"
          }
        }
      }
    ]
}

I am looking for an option like we do it in apache with a .htaccess where basic auth username and password will be requested before granting access.

Body:
By now, this has become relatively straightforward on AWS. It's basically 3 things that need to be done:

Create a CloudFront function to add Basic Auth into the request.

Configure the Origin of the CloudFront distribution correctly in a few places.

Activate the CloudFront function.
That's it, no particular bells & whistles otherwise. Here's what I've done:

First, go to CloudFront, then click on Functions on the left, create a new function with a name of your choice (no region etc. necessary) and then add the following as the code of the function:

function handler(event) {

    var user = "myuser";
    var pass = "mypassword";

    function encodeToBase64(str) {
        var chars =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        for (
            // initialize result and counter
            var block, charCode, idx = 0, map = chars, output = "";
            // if the next str index does not exist:
            //   change the mapping table to "="
            //   check if d has no fractional digits
            str.charAt(idx | 0) || ((map = "="), idx % 1);
            // "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
            output += map.charAt(63 & (block >> (8 - (idx % 1) * 8)))
        ) {
        charCode = str.charCodeAt((idx += 3 / 4));
        if (charCode > 0xff) {
            throw new InvalidCharacterError("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."
        );
        }
        block = (block << 8) | charCode;
      }
      return output;
    }

    var requiredBasicAuth = "Basic " + encodeToBase64(`${user}:${pass}`);
    var match = false;
    if (event.request.headers.authorization) {
        if (event.request.headers.authorization.value === requiredBasicAuth) {
            match = true;
        }
    }

    if (!match) {
      return {
        statusCode: 401,
        statusDescription: "Unauthorized",
        headers: {
          "www-authenticate": { value: "Basic" },
        },
      };
    }

    return event.request;
}

Then you can test with directly on the UI and assuming it works and assuming you have customized username and password, publish the function.

Please note that I have found individual pieces of the function above on the Internet so this is not my own code (other than piecing it together). I wish I would still find the sources so I can quote them here but I can't find them anymore. Credits to the creators though! :-)

Next, open your CloudFront distribution and do the following:

Make sure your S3 bucket in the origin is configured as a REST endpoint and not a website endpoint, i.e. it must end on .s3.amazonaws.com and not have the word website in the hostname.

Also in the Origin settings, under "S3 bucket access", select "Yes use OAI (bucket can restrict access to only CloudFront)". In the setting below click on "Create OAI" to create a new OAI (unless you have an existing one and know what you're doing). And select "Yes, update the bucket policy" to allow AWS to add the necessary permissions to your OAI.

Finally, open your Behavior of the CloudFront distribution and scroll to the bottom. Under "Function associations", for "Viewer request" select "CloudFront Function" and select your newly created CloudFront function. Save your changes.

And that should be it. With a bit of luck a matter of a couple of minutes (realistically more, I know) and especially not additional complexity once this is all set up.


问:
jboss-cli : How do I read one specific system property using jboss-cli?
I'm new to jboss-cli and working through the 'jboss-cli recipes'.

Question

How do I read one specific property using jboss-cli? E.g.

jboss.home.dir (e.g. "-Djboss.home.dir=/path/to/my/jboss")

Xmx ("-Xmx=4G")

Context

The "CLI Recipes" documentation has this helpful example to get all system properties. However its 'too much infomration'. I want to script reading one specific property.

https://docs.jboss.org/author/display/WFLY10/CLI+Recipes#CLIRecipes-

Overview of all system properties in JBoss AS7+ including OS system
  properties and properties specified on command line using -D, -P or
  --properties arguments.

```<code>Standalone
[standalone@IP_ADDRESS:9999 /] /core-service=platform-mbean/type=runtime:read-attribute(name=system-properties)
</code>```

Thanks in advance

答:
jboss-cli

If you have a cli command like ehsavoie suggested :resolve-expression(expression=${jboss.home.dir}) and want to use the content of the "result" property within jboss-cli you can save it in a variable. You can use backticks (`) to evaluate expressions.

simple expression

```<code>[standalone@localhost:9990 /] :resolve-expression(expression=${jboss.home.dir})
{
    "outcome" => "success",
    "result" => "/home/user/wildfly"
}
</code>```

use in valiable

```<code>[standalone@localhost:9990 /] set wildflydirectory=`:resolve-expression(expression=${jboss.home.dir})`
[standalone@localhost:9990 /] echo $wildflydirectory
/home/user/wildfly
</code>```

PowerShell

If you happen to use the PowerShell you can use a one-liner to extract even deeply nested results with the help of the cli's --output-json option and PowerShell's ConvertFrom-Json cmdlet. In this way the parsing problem from James Roberts's approach with grep and sed are gone.

```<code>$value=(Invoke-Expression "./jboss-cli.ps1 -c --command=':resolve-expression(expression=`${jboss.home.dir})' --output-json" | ConvertFrom-Json).result
</code>```

It is a bit tricky to quote the command and escape the correct PowerShell special characters.

Body:
I'm new to jboss-cli and working through the 'jboss-cli recipes'.

Question

How do I read one specific property using jboss-cli? E.g.

jboss.home.dir (e.g. "-Djboss.home.dir=/path/to/my/jboss")

Xmx ("-Xmx=4G")

Context

The "CLI Recipes" documentation has this helpful example to get all system properties. However its 'too much infomration'. I want to script reading one specific property.

https://docs.jboss.org/author/display/WFLY10/CLI+Recipes#CLIRecipes-

Overview of all system properties in JBoss AS7+ including OS system
  properties and properties specified on command line using -D, -P or
  --properties arguments.

Standalone
[standalone@IP_ADDRESS:9999 /] /core-service=platform-mbean/type=runtime:read-attribute(name=system-properties)

Thanks in advance

Body:
jboss-cli

If you have a cli command like ehsavoie suggested :resolve-expression(expression=${jboss.home.dir}) and want to use the content of the "result" property within jboss-cli you can save it in a variable. You can use backticks (`) to evaluate expressions.

simple expression

[standalone@localhost:9990 /] :resolve-expression(expression=${jboss.home.dir})
{
    "outcome" => "success",
    "result" => "/home/user/wildfly"
}

use in valiable

[standalone@localhost:9990 /] set wildflydirectory=`:resolve-expression(expression=${jboss.home.dir})`
[standalone@localhost:9990 /] echo $wildflydirectory
/home/user/wildfly

PowerShell

If you happen to use the PowerShell you can use a one-liner to extract even deeply nested results with the help of the cli's --output-json option and PowerShell's ConvertFrom-Json cmdlet. In this way the parsing problem from James Roberts's approach with grep and sed are gone.

$value=(Invoke-Expression "./jboss-cli.ps1 -c --command=':resolve-expression(expression=`${jboss.home.dir})' --output-json" | ConvertFrom-Json).result

It is a bit tricky to quote the command and escape the correct PowerShell special characters.


问:
How to retrieve my heroku credentials in authenticator
I happened to integrate my heroku account with the authenticator, I recently changed my phone and restored the old one, I can't access my heroku account because I lost the authenticator passwords</p>

答: Try to contact with heroku support, they may can retrieve your account after you proove that you are legitimate.</p>


问:
Print terraform template rendered output in terminal?
In the terraform docs it shows how to use a template. Is there any way to log this rendered output the console?

https://www.terraform.io/docs/configuration/interpolation.html#templates

```<code>data "template_file" "example" {
  template = "${hello} ${world}!"
  vars {
    hello = "goodnight"
    world = "moon"
  }
}

output "rendered" {
  value = "${template_file.example.rendered}"
}
</code>```

答:
Note: If you specify the template as a literal string instead of loading a file, the inline template must use double dollar signs (like $${hello}).

https://www.terraform.io/language/configuration-0-11/interpolation#templates

This is how it worked:

```<code>data "template_file" "example" {
  template = "$${v1} $${v2}!"
  vars = {
    v1 = "hello"
    v2 = "world"
  }
}

output "rendered" {
  value = data.template_file.example.rendered

}
</code>```

Outputs: rendered = "hello world!"

Body:
In the terraform docs it shows how to use a template. Is there any way to log this rendered output the console?

https://www.terraform.io/docs/configuration/interpolation.html#templates

data "template_file" "example" {
  template = "${hello} ${world}!"
  vars {
    hello = "goodnight"
    world = "moon"
  }
}

output "rendered" {
  value = "${template_file.example.rendered}"
}

Body:
Note: If you specify the template as a literal string instead of loading a file, the inline template must use double dollar signs (like $${hello}).

https://www.terraform.io/language/configuration-0-11/interpolation#templates

This is how it worked:

data "template_file" "example" {
  template = "$${v1} $${v2}!"
  vars = {
    v1 = "hello"
    v2 = "world"
  }
}

output "rendered" {
  value = data.template_file.example.rendered

}

Outputs: rendered = "hello world!"