问:
Bar plot with polar axis
I want to make my plot similar to this-

Currently, what I am able to get is this:

I am not able to do the following things:

Get the time labels inside the inner circle

Get the ticks inside the circle. This question - How to create minor ticks for polar plot matplotlib, had an idea which I tried but it was messing up with other parts of the plot.

Time labels only work properly when the step size is 3. For other steps, it does not align with the bars.

Below is the reproducible code to produce the plot

```<code>arr = np.random.randint(0, 24, size = 50000)
df = pd.DataFrame({"COL": arr}).COL.value_counts().sort_index()

N = 24
bottom = 1000
theta, width = np.linspace(0.0, 2 * np.pi, N, endpoint=False, retstep=True)

plt.figure(figsize = (10, 6))
ax = plt.subplot(111, polar=True)

bars = ax.bar(
    theta, df,
    width=width-0.03,
    bottom=bottom,
    color="#f39c12", edgecolor="black"
)
bars = ax.bar(
    theta, [3000]*24,
    width=width-0.03,
    bottom=bottom,
    color="#f39c12", alpha=0.2
)

ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
ax.grid(False)
ax.spines['polar'].set_visible(False)
ax.set_rticks([])

ticks = [f"{i}:00" for i in range(0, 24, 3)]
ax.set_xticklabels(ticks)

_ = ax
</code>```

答:
I just drew two lines and a circle to make the "clock icon"

```<code>## Draw a "clock" icon inside of the graph
##lines for hands of a clock
x1, y1 = [0, 90], [0, 0.5*bottom]
x2, y2 = [0,0], [0, 0.5*bottom]
plt.plot(x1, y1, x2, y2, linewidth=2.5, solid_capstyle='round', color='#0066ff', alpha=1)

##circle for clockface
circle = pl.Circle((0, 0), 0.65*bottom, transform=ax.transData._b, linewidth=3, fill=False, color="#0066ff", alpha=1)
ax.add_artist(circle)
</code>```

did you ever figure out how to write the numbers inside of the clock?

Title: Bar plot with polar axis

Body:
I want to make my plot similar to this-

Currently, what I am able to get is this:

I am not able to do the following things:

Get the time labels inside the inner circle

Get the ticks inside the circle. This question - How to create minor ticks for polar plot matplotlib, had an idea which I tried but it was messing up with other parts of the plot.

Time labels only work properly when the step size is 3. For other steps, it does not align with the bars.

Below is the reproducible code to produce the plot

arr = np.random.randint(0, 24, size = 50000)
df = pd.DataFrame({"COL": arr}).COL.value_counts().sort_index()

N = 24
bottom = 1000
theta, width = np.linspace(0.0, 2 * np.pi, N, endpoint=False, retstep=True)

plt.figure(figsize = (10, 6))
ax = plt.subplot(111, polar=True)

bars = ax.bar(
    theta, df,
    width=width-0.03,
    bottom=bottom,
    color="#f39c12", edgecolor="black"
)
bars = ax.bar(
    theta, [3000]*24,
    width=width-0.03,
    bottom=bottom,
    color="#f39c12", alpha=0.2
)

ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
ax.grid(False)
ax.spines['polar'].set_visible(False)
ax.set_rticks([])

ticks = [f"{i}:00" for i in range(0, 24, 3)]
ax.set_xticklabels(ticks)

_ = ax

Body:
I just drew two lines and a circle to make the "clock icon"

## Draw a "clock" icon inside of the graph
##lines for hands of a clock
x1, y1 = [0, 90], [0, 0.5*bottom]
x2, y2 = [0,0], [0, 0.5*bottom]
plt.plot(x1, y1, x2, y2, linewidth=2.5, solid_capstyle='round', color='#0066ff', alpha=1)

##circle for clockface
circle = pl.Circle((0, 0), 0.65*bottom, transform=ax.transData._b, linewidth=3, fill=False, color="#0066ff", alpha=1)
ax.add_artist(circle)

did you ever figure out how to write the numbers inside of the clock?


来源: stackexchange

问:
Highlighting unmatched brackets in vim
I'm getting burned repeatedly by unmatched parentheses while writing python code in vim.  I like how they're handled for C code - vim highlights in red all of the curly braces following the unmatched paren.  I looked at the c.vim syntax file briefly to try to understand it, but the section that handles bracket errors is very complex.  Can anyone explain how that code works and suggest how I might write something similar for python code?

Example C code with unmatched parens:

```<code>int main(void
{  /* brace highlighted in red */
}  /* brace highlighted in red */
</code>```

Since python code doesn't have curly braces to highlight, we'll have to choose something else (perhaps other parentheses).

BTW, I tried out this vim plugin but I wasn't happy with the behavior.

Edit:

I'm using python to generate C++ code (a language that likes parentheses and semicolons).  I have a nasty habit of leaving the trailing paren off a file.write() method call.  It would be nice if I could get vim to make that mistake more visually obvious.

Update:

Ok, here's what I've tried so far.

```<code>:syn region pParen transparent start="(" end=")" contains=ALL
:syn match pError display ")"
:hi def link pError Error
</code>```

Unfortunately, all this does is highlight as an error the right paren of all balanced parentheses, the opposite of what I want to do.  I really don't understand what I'm doing here (just copied off of the existing C syntax file).  If anyone could explain what I did (wrong), I would appreciate it.

答:
If I understand correctly and you are trying to look at non-matching parenthesis in C code (that was generated in python), I would recommend you install rainbow.vim from Dr Chip's Site.  This will highlight braces in different colours depending on the levels of indentation and will highlight unmatching braces in red as you have requested.  A screenshot http://img294.imageshack.us/img294/8586/rainbow.jpg http://img294.imageshack.us/img294/8586/rainbow.jpg

To install, download rainbow.vim and place in vimfiles/after/syntax/c/ (create this directory if it is not present).

On Linux, this will be ~/.vim/after/syntax/c/rainbow.vim

On Windows, it may be c:\vim\vimfiles\after\syntax\c\rainbow.vim or possibly somewhere else, see :help runtimepath.

Note that there are some plugins that conflict with rainbow.vim, but it's not too hard to make them co-operate.

If you are trying to highlight non-matching parenthesis in the python code, you could modify rainbow.vim to use the python syntax clusters instead of the C ones, but this is a little more involved, but you could use something along the lines of (modified version of Dr Chip's rainbow code):

```<code>syn cluster pyParenGroup contains=pythonString,pythonRawString,pythonEscape,pythonNumber,pythonBuiltin,pythonException
syn match pyParenError display ')'
syn region  pyParen     transparent matchgroup=hlLevel0 start='(' end=')' contains=@pyParenGroup,pyParen1
syn region  pyParen1        transparent matchgroup=hlLevel1 start='(' end=')' contains=@pyParenGroup,pyParen2
syn region  pyParen2        transparent matchgroup=hlLevel2 start='(' end=')' contains=@pyParenGroup,pyParen3
syn region  pyParen3        transparent matchgroup=hlLevel3 start='(' end=')' contains=@pyParenGroup,pyParen4
syn region  pyParen4        transparent matchgroup=hlLevel4 start='(' end=')' contains=@pyParenGroup,pyParen5
syn region  pyParen5        transparent matchgroup=hlLevel5 start='(' end=')' contains=@pyParenGroup,pyParen6
syn region  pyParen6        transparent matchgroup=hlLevel6 start='(' end=')' contains=@pyParenGroup,pyParen7
syn region  pyParen7        transparent matchgroup=hlLevel7 start='(' end=')' contains=@pyParenGroup,pyParen8
syn region  pyParen8        transparent matchgroup=hlLevel8 start='(' end=')' contains=@pyParenGroup,pyParen9
syn region  pyParen9        transparent matchgroup=hlLevel9 start='(' end=')' contains=@pyParenGroup,pyParen
hi link pyParenError Error

if &bg == "dark"
    hi default   hlLevel0 ctermfg=red         guifg=red1
    hi default   hlLevel1 ctermfg=yellow      guifg=orange1
    hi default   hlLevel2 ctermfg=green       guifg=yellow1
    hi default   hlLevel3 ctermfg=cyan        guifg=greenyellow
    hi default   hlLevel4 ctermfg=magenta     guifg=green1
    hi default   hlLevel5 ctermfg=red         guifg=springgreen1
    hi default   hlLevel6 ctermfg=yellow      guifg=cyan1
    hi default   hlLevel7 ctermfg=green       guifg=slateblue1
    hi default   hlLevel8 ctermfg=cyan        guifg=magenta1
    hi default   hlLevel9 ctermfg=magenta     guifg=purple1
else
    hi default   hlLevel0 ctermfg=red         guifg=red3
    hi default   hlLevel1 ctermfg=darkyellow  guifg=orangered3
    hi default   hlLevel2 ctermfg=darkgreen   guifg=orange2
    hi default   hlLevel3 ctermfg=blue        guifg=yellow3
    hi default   hlLevel4 ctermfg=darkmagenta guifg=olivedrab4
    hi default   hlLevel5 ctermfg=red         guifg=green4
    hi default   hlLevel6 ctermfg=darkyellow  guifg=paleturquoise3
    hi default   hlLevel7 ctermfg=darkgreen   guifg=deepskyblue4
    hi default   hlLevel8 ctermfg=blue        guifg=darkslateblue
    hi default   hlLevel9 ctermfg=darkmagenta guifg=darkviolet
endif
</code>```

EDIT:

As a test, I downloaded gvim70.zip and vim70rt.zip from ftp://ftp.vim.org/pub/vim/pc/ (these are the Windows versions of Vim 7.0).  I unzipped the two files into a new directory and ran gvim.exe from vim/vim70/gvim.exe.  I do not have any vim configuration stored in "C:\Documents and Settings", so running this vim is the same as running a 'vanilla' configuration.  I then downloaded pyprint.py from amk.ca/python/simple/pyprint.html as a piece of sample code and copied the above code into a file called code.vim.  In gVim, I entered :e pyprint.py.  It opened in the white-background window, with no syntax highlighting.  I then entered :syntax on, which switched the default syntax highlighting on.  I added a second ) character on line 8. Finally, I entered :source code.vim, which made the second ) character be highlighted in red.

I've also carried out this test on Linux (with Vim 7.2), by entering the following command sequence:

```<code>cd ~
mv .vimrc old_dot_vimrc
mv .gvimrc old_dot_gvimrc
mv .vim old_dot_vim
vim pyprint.py
:e pyprint.py
" Add extra bracket here!
:syntax on
:source code.vim
</code>```

Again, the second bracket is highlighted and everything else seems normal.

OwnerDisplayName: Kristo

ContentLicense: CC BY-SA 2.5

Title: Highlighting unmatched brackets in vim

Body:
I'm getting burned repeatedly by unmatched parentheses while writing python code in vim.  I like how they're handled for C code - vim highlights in red all of the curly braces following the unmatched paren.  I looked at the c.vim syntax file briefly to try to understand it, but the section that handles bracket errors is very complex.  Can anyone explain how that code works and suggest how I might write something similar for python code?

Example C code with unmatched parens:

int main(void
{  /* brace highlighted in red */
}  /* brace highlighted in red */

Since python code doesn't have curly braces to highlight, we'll have to choose something else (perhaps other parentheses).

BTW, I tried out this vim plugin but I wasn't happy with the behavior.

Edit:

I'm using python to generate C++ code (a language that likes parentheses and semicolons).  I have a nasty habit of leaving the trailing paren off a file.write() method call.  It would be nice if I could get vim to make that mistake more visually obvious.

Update:

Ok, here's what I've tried so far.

:syn region pParen transparent start="(" end=")" contains=ALL
:syn match pError display ")"
:hi def link pError Error

Unfortunately, all this does is highlight as an error the right paren of all balanced parentheses, the opposite of what I want to do.  I really don't understand what I'm doing here (just copied off of the existing C syntax file).  If anyone could explain what I did (wrong), I would appreciate it.

LastEditorDisplayName: Kristo

Body:
If I understand correctly and you are trying to look at non-matching parenthesis in C code (that was generated in python), I would recommend you install rainbow.vim from Dr Chip's Site.  This will highlight braces in different colours depending on the levels of indentation and will highlight unmatching braces in red as you have requested.  A screenshot http://img294.imageshack.us/img294/8586/rainbow.jpg http://img294.imageshack.us/img294/8586/rainbow.jpg

To install, download rainbow.vim and place in vimfiles/after/syntax/c/ (create this directory if it is not present).

On Linux, this will be ~/.vim/after/syntax/c/rainbow.vim

On Windows, it may be c:\vim\vimfiles\after\syntax\c\rainbow.vim or possibly somewhere else, see :help runtimepath.

Note that there are some plugins that conflict with rainbow.vim, but it's not too hard to make them co-operate.

If you are trying to highlight non-matching parenthesis in the python code, you could modify rainbow.vim to use the python syntax clusters instead of the C ones, but this is a little more involved, but you could use something along the lines of (modified version of Dr Chip's rainbow code):

syn cluster pyParenGroup contains=pythonString,pythonRawString,pythonEscape,pythonNumber,pythonBuiltin,pythonException
syn match pyParenError display ')'
syn region  pyParen     transparent matchgroup=hlLevel0 start='(' end=')' contains=@pyParenGroup,pyParen1
syn region  pyParen1        transparent matchgroup=hlLevel1 start='(' end=')' contains=@pyParenGroup,pyParen2
syn region  pyParen2        transparent matchgroup=hlLevel2 start='(' end=')' contains=@pyParenGroup,pyParen3
syn region  pyParen3        transparent matchgroup=hlLevel3 start='(' end=')' contains=@pyParenGroup,pyParen4
syn region  pyParen4        transparent matchgroup=hlLevel4 start='(' end=')' contains=@pyParenGroup,pyParen5
syn region  pyParen5        transparent matchgroup=hlLevel5 start='(' end=')' contains=@pyParenGroup,pyParen6
syn region  pyParen6        transparent matchgroup=hlLevel6 start='(' end=')' contains=@pyParenGroup,pyParen7
syn region  pyParen7        transparent matchgroup=hlLevel7 start='(' end=')' contains=@pyParenGroup,pyParen8
syn region  pyParen8        transparent matchgroup=hlLevel8 start='(' end=')' contains=@pyParenGroup,pyParen9
syn region  pyParen9        transparent matchgroup=hlLevel9 start='(' end=')' contains=@pyParenGroup,pyParen
hi link pyParenError Error

if &bg == "dark"
    hi default   hlLevel0 ctermfg=red         guifg=red1
    hi default   hlLevel1 ctermfg=yellow      guifg=orange1
    hi default   hlLevel2 ctermfg=green       guifg=yellow1
    hi default   hlLevel3 ctermfg=cyan        guifg=greenyellow
    hi default   hlLevel4 ctermfg=magenta     guifg=green1
    hi default   hlLevel5 ctermfg=red         guifg=springgreen1
    hi default   hlLevel6 ctermfg=yellow      guifg=cyan1
    hi default   hlLevel7 ctermfg=green       guifg=slateblue1
    hi default   hlLevel8 ctermfg=cyan        guifg=magenta1
    hi default   hlLevel9 ctermfg=magenta     guifg=purple1
else
    hi default   hlLevel0 ctermfg=red         guifg=red3
    hi default   hlLevel1 ctermfg=darkyellow  guifg=orangered3
    hi default   hlLevel2 ctermfg=darkgreen   guifg=orange2
    hi default   hlLevel3 ctermfg=blue        guifg=yellow3
    hi default   hlLevel4 ctermfg=darkmagenta guifg=olivedrab4
    hi default   hlLevel5 ctermfg=red         guifg=green4
    hi default   hlLevel6 ctermfg=darkyellow  guifg=paleturquoise3
    hi default   hlLevel7 ctermfg=darkgreen   guifg=deepskyblue4
    hi default   hlLevel8 ctermfg=blue        guifg=darkslateblue
    hi default   hlLevel9 ctermfg=darkmagenta guifg=darkviolet
endif

EDIT:

As a test, I downloaded gvim70.zip and vim70rt.zip from ftp://ftp.vim.org/pub/vim/pc/ (these are the Windows versions of Vim 7.0).  I unzipped the two files into a new directory and ran gvim.exe from vim/vim70/gvim.exe.  I do not have any vim configuration stored in "C:\Documents and Settings", so running this vim is the same as running a 'vanilla' configuration.  I then downloaded pyprint.py from amk.ca/python/simple/pyprint.html as a piece of sample code and copied the above code into a file called code.vim.  In gVim, I entered :e pyprint.py.  It opened in the white-background window, with no syntax highlighting.  I then entered :syntax on, which switched the default syntax highlighting on.  I added a second ) character on line 8. Finally, I entered :source code.vim, which made the second ) character be highlighted in red.

I've also carried out this test on Linux (with Vim 7.2), by entering the following command sequence:

cd ~
mv .vimrc old_dot_vimrc
mv .gvimrc old_dot_gvimrc
mv .vim old_dot_vim
vim pyprint.py
:e pyprint.py
" Add extra bracket here!
:syntax on
:source code.vim

Again, the second bracket is highlighted and everything else seems normal.

OwnerDisplayName: Alan Budden

ContentLicense: CC BY-SA 3.0

LastEditorDisplayName: Al


问:
presentModalViewController ontop of subview?
I am trying to recreate the iPhone's tabView, but with my own style, buttons, etc. I didn't want to have to totally redo my app, so I simply added a view to the bottom like this [window addSubview:theToolbar]; theToolbar.frame = CGRectMake(0, 425, 320, 44); in my appDelegate.

However, when trying to do this from a view inside a navigationController theToolbar is over it. Is there anyway to somehow present it to the front?

Here's my code to present the view:

```<code>AppSettingsController *appSettings = [[AppSettingsController alloc] initWithNibName:nil bundle:nil];
appSettings.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:appSettings animated:YES];
[appSettings release];
</code>```

Thanks.

答:
it's impossible show partial views of the viewcontroller. if your want to use the same toolbar, you should retain the toolbar to your appdelegate, and add the toolbar to each viewcontroller when it is in view.

or you should just use uiview's as viewcontrollers

Body:
I am trying to recreate the iPhone's tabView, but with my own style, buttons, etc. I didn't want to have to totally redo my app, so I simply added a view to the bottom like this [window addSubview:theToolbar]; theToolbar.frame = CGRectMake(0, 425, 320, 44); in my appDelegate.

However, when trying to do this from a view inside a navigationController theToolbar is over it. Is there anyway to somehow present it to the front?

Here's my code to present the view:

AppSettingsController *appSettings = [[AppSettingsController alloc] initWithNibName:nil bundle:nil];
appSettings.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:appSettings animated:YES];
[appSettings release];

Thanks.


问:
Django Rest Framework gives 302 in Unit tests when force_login() on detail view?
I'm using Django Rest Framework to serve an API. I've got a couple tests which work great. To do a post the user needs to be logged in and I also do some checks for the detail view for a logged in user. I do this as follows:

```<code>class DeviceTestCase(APITestCase):
    USERNAME = "username"
    EMAIL = 'a@b.com'
    PASSWORD = "password"

    def setUp(self):
        self.sa_group, _ = Group.objects.get_or_create(name=settings.KEYCLOAK_SA_WRITE_PERMISSION_NAME)
        self.authorized_user = User.objects.create_user(self.USERNAME, self.EMAIL, self.PASSWORD)
        self.sa_group.user_set.add(self.authorized_user)

    def test_post(self):
        device = DeviceFactory.build()
        url = reverse('device-list')

        self.client.force_login(self.authorized_user)
        response = self.client.post(url, data={'some': 'test', 'data': 'here'}, format='json')
        self.client.logout()

        self.assertEqual(status.HTTP_201_CREATED, response.status_code)
        # And some more tests here

    def test_detail_logged_in(self):
        device = DeviceFactory.create()

        url = reverse('device-detail', kwargs={'pk': device.pk})

        self.client.force_login(self.authorized_user)
        response = self.client.get(url)
        self.client.logout()

        self.assertEqual(status.HTTP_200_OK, response.status_code, 'Wrong response code for {}'.format(url))
        # And some more tests here
</code>```

The first test works great. It posts the new record and all checks pass. The second test fails though. It gives an error saying

```<code>AssertionError: 200 != 302 : Wrong response code for /sa/devices/1/
</code>```

It turns out the list view redirects the user to the login screen. Why does the first test log the user in perfectly, but does the second test redirect the user to the login screen? Am I missing something?

Here is the view:

```<code>class APIAuthGroup(InAuthGroup):
    """
A permission to allow all GETS, but only allow a POST if a user is logged in,
and is a member of the slimme apparaten role inside keycloak.
    """
    allowed_group_names = [settings.KEYCLOAK_SA_WRITE_PERMISSION_NAME]

    def has_permission(self, request, view):
        return request.method in SAFE_METHODS \
               or super(APIAuthGroup, self).has_permission(request, view)

class DevicesViewSet(DatapuntViewSetWritable):
    """
    A view that will return the devices and makes it possible to post new ones
    """

    queryset = Device.objects.all().order_by('id')

    serializer_class = DeviceSerializer
    serializer_detail_class = DeviceSerializer

    http_method_names = ['post', 'list', 'get']

    permission_classes = [APIAuthGroup]
</code>```

答:
Here is why you are getting this error.

Dependent Libraries

I did some searching by Class Names to find which libraries you were using so that I can re-create the problem on my machine. The library causing the problem is the one called keycloak_idc. This library installs another library mozilla_django_oidc which would turn out to be the reason you are getting this.

Why This Library Is Causing The Problem

Inside the README file of this library, it gives you instructions on how to set it up. These are found in this file. Inside these instructions, it instructed you to add the AUTHENTICATION_BACKENDS

```<code>AUTHENTICATION_BACKENDS = [
'keycloak_oidc.auth.OIDCAuthenticationBackend',
...
]
</code>```

When you add this authentication backend, all your requests pass through a Middleware defined inside the SessionRefresh class defined inside mozilla_django_oidc/middleware.py. Inside this class, the method process_request() is always called.

The first thing this method does is call the is_refreshable_url() method which always returns False if the request method was POST. Otherwise (when the request method is GET), it will return True.

Now the body of this if condition was as follows.

```<code>        if not self.is_refreshable_url(request):
            LOGGER.debug('request is not refreshable')
            return
        # lots of stuff in here
        return HttpResponseRedirect(redirect_url)
</code>```

Since this is a middleware, if the request was POST and the return was None, Django would just proceed with actually doing your request. However when the request is GET and the line return HttpResponseRedirect(redirect_url) is triggered instead, Django will not even proceed with calling your view and will return the 302 response immediately.

The Solution

After a couple of hours debugging this, I do not the exact logic behind this middleware or what exactly are you trying to do to provide a concrete solution since this all started based off guess-work but a naive fix can be that you remove the AUTHENTICATION_BACKENDS from your settings file. While I feel that this is not acceptable, maybe you can try using another library that accomplishes what you're trying to do or find an alternative way to do it. Also, maybe you can contact the author and see what they think.

ContentLicense: CC BY-SA 4.0

Body:
I'm using Django Rest Framework to serve an API. I've got a couple tests which work great. To do a post the user needs to be logged in and I also do some checks for the detail view for a logged in user. I do this as follows:

class DeviceTestCase(APITestCase):
    USERNAME = "username"
    EMAIL = 'a@b.com'
    PASSWORD = "password"

    def setUp(self):
        self.sa_group, _ = Group.objects.get_or_create(name=settings.KEYCLOAK_SA_WRITE_PERMISSION_NAME)
        self.authorized_user = User.objects.create_user(self.USERNAME, self.EMAIL, self.PASSWORD)
        self.sa_group.user_set.add(self.authorized_user)

    def test_post(self):
        device = DeviceFactory.build()
        url = reverse('device-list')

        self.client.force_login(self.authorized_user)
        response = self.client.post(url, data={'some': 'test', 'data': 'here'}, format='json')
        self.client.logout()

        self.assertEqual(status.HTTP_201_CREATED, response.status_code)
        # And some more tests here

    def test_detail_logged_in(self):
        device = DeviceFactory.create()

        url = reverse('device-detail', kwargs={'pk': device.pk})

        self.client.force_login(self.authorized_user)
        response = self.client.get(url)
        self.client.logout()

        self.assertEqual(status.HTTP_200_OK, response.status_code, 'Wrong response code for {}'.format(url))
        # And some more tests here

The first test works great. It posts the new record and all checks pass. The second test fails though. It gives an error saying

AssertionError: 200 != 302 : Wrong response code for /sa/devices/1/

It turns out the list view redirects the user to the login screen. Why does the first test log the user in perfectly, but does the second test redirect the user to the login screen? Am I missing something?

Here is the view:

class APIAuthGroup(InAuthGroup):
    """
A permission to allow all GETS, but only allow a POST if a user is logged in,
and is a member of the slimme apparaten role inside keycloak.
    """
    allowed_group_names = [settings.KEYCLOAK_SA_WRITE_PERMISSION_NAME]

    def has_permission(self, request, view):
        return request.method in SAFE_METHODS \
               or super(APIAuthGroup, self).has_permission(request, view)

class DevicesViewSet(DatapuntViewSetWritable):
    """
    A view that will return the devices and makes it possible to post new ones
    """

    queryset = Device.objects.all().order_by('id')

    serializer_class = DeviceSerializer
    serializer_detail_class = DeviceSerializer

    http_method_names = ['post', 'list', 'get']

    permission_classes = [APIAuthGroup]

Body:
Here is why you are getting this error.

Dependent Libraries

I did some searching by Class Names to find which libraries you were using so that I can re-create the problem on my machine. The library causing the problem is the one called keycloak_idc. This library installs another library mozilla_django_oidc which would turn out to be the reason you are getting this.

Why This Library Is Causing The Problem

Inside the README file of this library, it gives you instructions on how to set it up. These are found in this file. Inside these instructions, it instructed you to add the AUTHENTICATION_BACKENDS

AUTHENTICATION_BACKENDS = [
'keycloak_oidc.auth.OIDCAuthenticationBackend',
...
]

When you add this authentication backend, all your requests pass through a Middleware defined inside the SessionRefresh class defined inside mozilla_django_oidc/middleware.py. Inside this class, the method process_request() is always called.

The first thing this method does is call the is_refreshable_url() method which always returns False if the request method was POST. Otherwise (when the request method is GET), it will return True.

Now the body of this if condition was as follows.

        if not self.is_refreshable_url(request):
            LOGGER.debug('request is not refreshable')
            return
        # lots of stuff in here
        return HttpResponseRedirect(redirect_url)

Since this is a middleware, if the request was POST and the return was None, Django would just proceed with actually doing your request. However when the request is GET and the line return HttpResponseRedirect(redirect_url) is triggered instead, Django will not even proceed with calling your view and will return the 302 response immediately.

The Solution

After a couple of hours debugging this, I do not the exact logic behind this middleware or what exactly are you trying to do to provide a concrete solution since this all started based off guess-work but a naive fix can be that you remove the AUTHENTICATION_BACKENDS from your settings file. While I feel that this is not acceptable, maybe you can try using another library that accomplishes what you're trying to do or find an alternative way to do it. Also, maybe you can contact the author and see what they think.


问:
How to correctly scale custom thumb for a switch widget?
I've a custom thumb set from a drawable for a Switch widget inside a LinearLayout. I can't get it to show correctly on all kinds of screens. On most screens, it looks like this-

Which is fine, but on some screens (example - 440 dpi), it'd look like this-

How do I get this to look uniform across all screens? I've tried many things, including switchMinWidth, setting android:width= to 0dp, using thumbTextPadding but nothing works. Everything has the exact same result, it works on some screens but not on others.

Here's my Switch widget placed inside a LinearLayout with a layout_weight of 2. There are usually 5 widgets in each Horizontal LinearLayout but this specific one has 4 as Switch is supposed to take 2 slots.

        <Switch
            android:id="@+id/btndegreerad"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@drawable/degree_rad_track"
            android:showText="true"
            android:switchTextAppearance="@style/SwitchTextTheme"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textOff="@string/btnrad"
            android:textOn="@string/btndegree"
            android:thumb="@drawable/degree_rad_thumb"
            android:thumbTextPadding="@dimen/_3sdp"
            android:thumbTint="@color/colorSwitch"
            android:track="@drawable/degree_rad_track" />
```

<p>Here's the <code>degree_rad_thumb</code> drawable-</p>

<pre class="lang-xml prettyprint-override"><code><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <size android:height="@dimen/_38sdp" android:width="0dp"/>
            <corners android:bottomRightRadius="10dp"
                android:bottomLeftRadius="10dp"
                android:topRightRadius="10dp"
                android:topLeftRadius="10dp"/>
        </shape>
    </item>
    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <size android:height="@dimen/_38sdp" android:width="0dp"/>
            <corners android:bottomRightRadius="10dp"
                android:bottomLeftRadius="10dp"
                android:topRightRadius="10dp"
                android:topLeftRadius="10dp"/>
        </shape>
    </item>
</selector>
</code>```

Here's also degree_rad_track, though this is probably not needed-

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <solid android:color="@color/colorFadedPrimary"/>
            <corners android:bottomRightRadius="10dp"
                android:bottomLeftRadius="10dp"
                android:topRightRadius="10dp"
                android:topLeftRadius="10dp"/>
        </shape>
    </item>
    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/colorFadedPrimary"/>
            <corners android:bottomRightRadius="10dp"
                android:bottomLeftRadius="10dp"
                android:topRightRadius="10dp"
                android:topLeftRadius="10dp"/>
        </shape>
    </item>
</selector>
```

答:
I am currently working on a similar layout using Switch with custom drawable for its thumb and track. From what I have learned, LinearLayout weight property can affect your Switch track's drawable (either cropped or squished) if your device width doesn't provide enough room.

With this unwanted behavior, I recommend changing your layout type to ConstraintLayout if you really need to get your custom drawable working on different screen sizes. This solution is also recommended in this guide.

But if you really want to use LinearLayout, don't use layout_weight for your Switch.

Body:
I've a custom thumb set from a drawable for a Switch widget inside a LinearLayout. I can't get it to show correctly on all kinds of screens. On most screens, it looks like this-

Which is fine, but on some screens (example - 440 dpi), it'd look like this-

How do I get this to look uniform across all screens? I've tried many things, including switchMinWidth, setting android:width= to 0dp, using thumbTextPadding but nothing works. Everything has the exact same result, it works on some screens but not on others.

Here's my Switch widget placed inside a LinearLayout with a layout_weight of 2. There are usually 5 widgets in each Horizontal LinearLayout but this specific one has 4 as Switch is supposed to take 2 slots.

        <Switch
            android:id="@+id/btndegreerad"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@drawable/degree_rad_track"
            android:showText="true"
            android:switchTextAppearance="@style/SwitchTextTheme"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textOff="@string/btnrad"
            android:textOn="@string/btndegree"
            android:thumb="@drawable/degree_rad_thumb"
            android:thumbTextPadding="@dimen/_3sdp"
            android:thumbTint="@color/colorSwitch"
            android:track="@drawable/degree_rad_track" />

Here's the degree_rad_thumb drawable-

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <size android:height="@dimen/_38sdp" android:width="0dp"/>
            <corners android:bottomRightRadius="10dp"
                android:bottomLeftRadius="10dp"
                android:topRightRadius="10dp"
                android:topLeftRadius="10dp"/>
        </shape>
    </item>
    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <size android:height="@dimen/_38sdp" android:width="0dp"/>
            <corners android:bottomRightRadius="10dp"
                android:bottomLeftRadius="10dp"
                android:topRightRadius="10dp"
                android:topLeftRadius="10dp"/>
        </shape>
    </item>
</selector>

Here's also degree_rad_track, though this is probably not needed-

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <solid android:color="@color/colorFadedPrimary"/>
            <corners android:bottomRightRadius="10dp"
                android:bottomLeftRadius="10dp"
                android:topRightRadius="10dp"
                android:topLeftRadius="10dp"/>
        </shape>
    </item>
    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/colorFadedPrimary"/>
            <corners android:bottomRightRadius="10dp"
                android:bottomLeftRadius="10dp"
                android:topRightRadius="10dp"
                android:topLeftRadius="10dp"/>
        </shape>
    </item>
</selector>


问:
Navigate to a fragment from another graph without it being the start destination
In my first graph, I have the following:

```<code><?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/firstGraph"
    app:startDestination="@id/listFragment">

    <fragment
        android:id="@+id/listFragment"
        android:name="com.example.ListFragment">

        <action
            android:id="@+id/action_list_to_details"
            app:destination="@id/detailsFragment" />

    </fragment>

    <fragment
        android:id="@+id/detailsFragment"
        android:name="com.example.DetailsFragment">

    </fragment>
</navigation>
</code>```

In my second graph I have the following:

```<code><?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/secondGraph"
    app:startDestination="@id/dashboardFragment">

    <include app:graph="@navigation/firstGraph" />

    <fragment
        android:id="@+id/dashboardFragment"
        android:name="com.example.DashboardFragment">
        <action
            android:id="@+id/action_dashboard_to_notification"
            app:destination="@id/notificationFragment"/>
    </fragment>

    <fragment
        android:id="@+id/notificationFragment"
        android:name="com.example.NotificationsFragment">

        <action
            android:id="@+id/action_notification_to_details"
            app:destination="@id/firstGraph"/>

    </fragment>
</navigation>
</code>```

I want to navigate from "notificationFragment" to "detailsFragment" directly without it being the start destination, with including the second graph stack

答:
As per the nested graph documentation:

[Nested graphs] also provide a level of encapsulation — destinations outside of the nested graph do not have direct access to any of the destinations within the nested graph.

There is one exception to that, when you are navigating using a URI, effectively deep linking into any destination:

Unlike navigation using action or destination IDs, you can navigate to any URI in your graph, regardless of whether the destination is visible. You can navigate to a destination on the current graph or a destination on a completely different graph.

Therefore you can add an implicit deep link to your graph:

```<code><fragment
    android:id="@+id/detailsFragment"
    android:name="com.example.DetailsFragment">
    <deepLink app:uri="android-app://your.package.name/details" />
</fragment>
</code>```

Then navigate to that destination via URI:

```<code>val uri = Uri.parse("android-app://your.package.name/details")
navController.navigate(uri)
</code>```

It doesn't matter what your URI is, as long as the <deepLink> and what you pass to navigate match. Any arguments you have would need to be encoded in the URL.

Body:
In my first graph, I have the following:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/firstGraph"
    app:startDestination="@id/listFragment">

    <fragment
        android:id="@+id/listFragment"
        android:name="com.example.ListFragment">

        <action
            android:id="@+id/action_list_to_details"
            app:destination="@id/detailsFragment" />

    </fragment>

    <fragment
        android:id="@+id/detailsFragment"
        android:name="com.example.DetailsFragment">

    </fragment>
</navigation>

In my second graph I have the following:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/secondGraph"
    app:startDestination="@id/dashboardFragment">

    <include app:graph="@navigation/firstGraph" />

    <fragment
        android:id="@+id/dashboardFragment"
        android:name="com.example.DashboardFragment">
        <action
            android:id="@+id/action_dashboard_to_notification"
            app:destination="@id/notificationFragment"/>
    </fragment>

    <fragment
        android:id="@+id/notificationFragment"
        android:name="com.example.NotificationsFragment">

        <action
            android:id="@+id/action_notification_to_details"
            app:destination="@id/firstGraph"/>

    </fragment>
</navigation>

I want to navigate from "notificationFragment" to "detailsFragment" directly without it being the start destination, with including the second graph stack

Body:
As per the nested graph documentation:

[Nested graphs] also provide a level of encapsulation — destinations outside of the nested graph do not have direct access to any of the destinations within the nested graph.

There is one exception to that, when you are navigating using a URI, effectively deep linking into any destination:

Unlike navigation using action or destination IDs, you can navigate to any URI in your graph, regardless of whether the destination is visible. You can navigate to a destination on the current graph or a destination on a completely different graph.

Therefore you can add an implicit deep link to your graph:

<fragment
    android:id="@+id/detailsFragment"
    android:name="com.example.DetailsFragment">
    <deepLink app:uri="android-app://your.package.name/details" />
</fragment>

Then navigate to that destination via URI:

val uri = Uri.parse("android-app://your.package.name/details")
navController.navigate(uri)

It doesn't matter what your URI is, as long as the <deepLink> and what you pass to navigate match. Any arguments you have would need to be encoded in the URL.


问:
Best Practices when Choosing SQL Keys Types
I am very new to the SQL database. I need to create a database for my internship as our DA resigned suddenly.

The data is available but it is not inputted into a database yet. I am trying to follow the tutorials online but got stuck on what to choose for the different key types.

I hope to get the feedback of more experienced folks to get your guidance.

Table columns:

```<code>entry id (unique)
entry timestamp
username (unique but can appear more than once if the same user input a new meal record)
email address
user first and last name
meals taken date
meal type
meal calories
meal duration
meal cost
meal location
user notes
</code>```

For primary key = entry id

For candidate key, I will pick username & and entry ID. Are there other columns that I should select as candidate keys? Would email make more sense? But a username can be repeated if they input another meal record. Does that matter?

```<code>For compound Key =
email address + user first and last name?
record date + user name?
</code>```

Are there other keys I need to classify?

Online tutorials these are the most basic keys I need to identify. But I am not sure if I am making the right choice. I appreciate any feedback.

答:
Your data seems to contain multiple entities.  Based on your simple description, I can identify:

users

meals

locations

Then there seems to be this thing called a entries which is a user, eating (buying?) a meal at a location.  This is a 3-way junction table among the entities.

This is a guess on what you are trying to represent.  But it sounds like multiple tables.

Body:
I am very new to the SQL database. I need to create a database for my internship as our DA resigned suddenly.

The data is available but it is not inputted into a database yet. I am trying to follow the tutorials online but got stuck on what to choose for the different key types.

I hope to get the feedback of more experienced folks to get your guidance.

Table columns:

entry id (unique)
entry timestamp
username (unique but can appear more than once if the same user input a new meal record)
email address
user first and last name
meals taken date
meal type
meal calories
meal duration
meal cost
meal location
user notes

For primary key = entry id

For candidate key, I will pick username & and entry ID. Are there other columns that I should select as candidate keys? Would email make more sense? But a username can be repeated if they input another meal record. Does that matter?

For compound Key =
email address + user first and last name?
record date + user name?

Are there other keys I need to classify?

Online tutorials these are the most basic keys I need to identify. But I am not sure if I am making the right choice. I appreciate any feedback.


问:
Regular Expression to return false if anything in the string is other than exactly what the pattern checks for
I'm trying to make a pattern that will return false if there's ANYTHING in the string other than exactly what the pattern checks for.

I believe that this is supposed to be achieved by putting the entire pattern between a "^" and a "$".

```<code>Sub Macro1()
    Dim cellValue As String
    cellValue = "7:11 AM NBR Unavail"

    Dim callPattern As String
    callPattern = "^[1]{0,1}[1-9][:][0-9]{2}[ ]AM|PM$"

    Dim regEx As New RegExp

    With regEx
        .IgnoreCase = False
        .Pattern = callPattern
    End With

    If regEx.Test(cellValue) Then
        MsgBox ("Got it!")
    Else
        MsgBox ("Don't got it...")
    End If
End Sub
</code>```

The variable cellValue has text after the AM|PM that the pattern checks for, yet when I run it I still get the "Got it!" message.

I experimented with the following string and pattern earlier and the "$" at the end worked as intended. Why isn't it working with the above code?

```<code>cellValue = "7:11 PM (555) 444-3333"
callPattern = "^[1]{0,1}[1-9][:][0-9]{2}[ ]AM|PM[ ][(][0-9]{3}[)][ ][0-9]{3}[-][0-9]{4}$"
</code>```

答:
You can reduce the pattern down to this:

```<code>^1?[1-9]:\d{2} (AM|PM) \(\d{3}\) \d{3}-\d{4}$
</code>```

Test it here

Body:
I'm trying to make a pattern that will return false if there's ANYTHING in the string other than exactly what the pattern checks for.

I believe that this is supposed to be achieved by putting the entire pattern between a "^" and a "$".

Sub Macro1()
    Dim cellValue As String
    cellValue = "7:11 AM NBR Unavail"

    Dim callPattern As String
    callPattern = "^[1]{0,1}[1-9][:][0-9]{2}[ ]AM|PM$"

    Dim regEx As New RegExp

    With regEx
        .IgnoreCase = False
        .Pattern = callPattern
    End With

    If regEx.Test(cellValue) Then
        MsgBox ("Got it!")
    Else
        MsgBox ("Don't got it...")
    End If
End Sub

The variable cellValue has text after the AM|PM that the pattern checks for, yet when I run it I still get the "Got it!" message.

I experimented with the following string and pattern earlier and the "$" at the end worked as intended. Why isn't it working with the above code?

cellValue = "7:11 PM (555) 444-3333"
callPattern = "^[1]{0,1}[1-9][:][0-9]{2}[ ]AM|PM[ ][(][0-9]{3}[)][ ][0-9]{3}[-][0-9]{4}$"

Body:
You can reduce the pattern down to this:

^1?[1-9]:\d{2} (AM|PM) \(\d{3}\) \d{3}-\d{4}$

Test it here


问:
JMH forks, threads and debug
I started to work with JMH lately and wondered if there is a possible way to debug it.

First thing I did was try to debug it like any other program, but it threw "No transports initialized", so I couldn't debug it in the old fashioned way.

Next thing I did is to try to search on the internet and found someone who said that you need to put the forks to 0, tried it and it worked.

Unfortunately, I couldn't really understand why the forks are impacting the debug, or how forks impact the way I see things in the JMH.
All I know so far is that when you put .forks(number) on the OptionBuilder it says on how many process the benchmark will run. But if I put .forks(3) it's running each @Benchmark method on 3 process async?

An explanation about the .forks(number), .threads(number) how they are changing the way benchmarks run and how they impact debug would really clear things.

答:
This was quite a pain in the soft side to get to work (now that I wrote this, it sounds trivial)

First of all I was trying to debug DTraceAsmProfiler (perfasm, but for Mac), using gradle.

First of all, I have a gradle task called jmh that looks like this:

```<code>task jmh(type: JavaExec, dependsOn: jmhClasses) {
    // the actual class to run
    main = 'com.eugene.AdditionPollution'
    classpath = sourceSets.jmh.compileClasspath + sourceSets.jmh.runtimeClasspath

    // I build JMH from sources, thus need this
    repositories {
        mavenLocal()
        maven {
            url '/Users/eugene/.m2/repository'
        }
    }
}
</code>```

Then I needed to create a Debug Configuration in Intellij (nothing un-usual):

And then simply run:

```<code>sudo gradle -Dorg.gradle.debug=true jmh --debug-jvm
</code>```

Title: JMH forks, threads and debug

Body:
This was quite a pain in the soft side to get to work (now that I wrote this, it sounds trivial)

First of all I was trying to debug DTraceAsmProfiler (perfasm, but for Mac), using gradle.

First of all, I have a gradle task called jmh that looks like this:

task jmh(type: JavaExec, dependsOn: jmhClasses) {
    // the actual class to run
    main = 'com.eugene.AdditionPollution'
    classpath = sourceSets.jmh.compileClasspath + sourceSets.jmh.runtimeClasspath

    // I build JMH from sources, thus need this
    repositories {
        mavenLocal()
        maven {
            url '/Users/eugene/.m2/repository'
        }
    }
}

Then I needed to create a Debug Configuration in Intellij (nothing un-usual):

And then simply run:

sudo gradle -Dorg.gradle.debug=true jmh --debug-jvm


问:
How do I pass a function that takes a float and returns a float to another function?
I am trying to write a "map" function on one of my classes in Java. How do I pass a function that takes a float and return a float to another function? Something like this: </p>

```<code>void map( MapFn fn )
{
    for ( int  i = 0 ; i < this.rows; ++i )
    {
        for ( int  j = 0 ; j < this.cols; ++j )
        {
            this.inner[i][j] = fn(this.inner[i][j])
        }
    }
}
</code>```

答:
As suggested in the comments by @boly38, the java.util.function package is your friend (or just functional interfaces/lambdas in general) here.  For example, it provides the DoubleUnaryOperator class.  Accepting an instance of this as your method argument and using the applyAsDouble() method will do (effectively) what you want.

There is none provided specifically for float however.  If you need one you can always define your own:

```<code>@FunctionalInterface
public interface FloatUnaryOperator {
    float applyAsFloat(float in);
}
</code>```

So having your method be:

```<code>public void doThing(final DoubleUnaryOperator mapper) {
    for (final double d : someArray) {
        System.out.println(mapper.applyAsDouble(d));
    }
}
</code>```

Instances of these can be created via lambdas (or any other means) such as f -> f * 2.0f; (which will double any given float).

Body:
I am trying to write a "map" function on one of my classes in Java. How do I pass a function that takes a float and return a float to another function? Something like this:

void map( MapFn fn )
{
    for ( int  i = 0 ; i < this.rows; ++i )
    {
        for ( int  j = 0 ; j < this.cols; ++j )
        {
            this.inner[i][j] = fn(this.inner[i][j])
        }
    }
}

Body:
As suggested in the comments by @boly38, the java.util.function package is your friend (or just functional interfaces/lambdas in general) here.  For example, it provides the DoubleUnaryOperator class.  Accepting an instance of this as your method argument and using the applyAsDouble() method will do (effectively) what you want.

There is none provided specifically for float however.  If you need one you can always define your own:

@FunctionalInterface
public interface FloatUnaryOperator {
    float applyAsFloat(float in);
}

So having your method be:

public void doThing(final DoubleUnaryOperator mapper) {
    for (final double d : someArray) {
        System.out.println(mapper.applyAsDouble(d));
    }
}

Instances of these can be created via lambdas (or any other means) such as f -> f * 2.0f; (which will double any given float).


问:
How to keep looping Pandas Dataframe
I have a function that is very repetitive. I would like to keep looping instead of having all this code </p>

```<code>
</code>```

答:
You can use this syntax by appropriate modification:</p>

```<code>for i in range(2,6):
    df['finalvalue{}'.format(i)] = df.iloc[::-1, :].groupby([df.id, df['finalvalue{}'.format(i-1)].diff().lt(0).cumsum()])['finalvalue{}'.format(i-1)].cumsum()
</code>```

Title: How to keep looping Pandas Dataframe

Body:
You can use this syntax by appropriate modification:

for i in range(2,6):
    df['finalvalue{}'.format(i)] = df.iloc[::-1, :].groupby([df.id, df['finalvalue{}'.format(i-1)].diff().lt(0).cumsum()])['finalvalue{}'.format(i-1)].cumsum()


问:
Why does exponentiating a numpy.float64 return nan?
When I am raising a negative numpy.float64 to an exponent, I am receiving an nan. Why is the complex math not supported? Is the only workaround a cast to float?

```<code>>>> from numpy import float64, power
>>> r = float64(-12025.433836763057)
>>> p = 0.74
>>> r**p
nan
>>> power(r, p)
__main__:1: RuntimeWarning: invalid value encountered in power
nan
>>> float(r)**p
(-715.6124638577838+762.049873596874j)
>>>
</code>```

A suggested duplicate has a similar question, with an answer stating that this is a bug in numpy. Is this the end of the road?

答:
Behind the scenes, python is casting that float(r)**p to return a 'complex' type.

The numpy power function is intended to work with numpy array_like structures where all the items are the same size and stored in a contiguous block of memory and its return type is inferred from its arguments.

If you are expecting complex numbers, the best approach would be to use the complex64 or complex128 types. These require more memory because each complex type consists of a real and imaginary component. So complex64 would consist of two float32 numbers and complex128 would consist of two float64 numbers.

```<code>>>> import numpy as np
>>> r = np.complex128(-12025.433836763057)
>>> p = 0.74
>>> np.power(r, p)
(-715.6124638577835+762.0498735968736j)
</code>```

You can also cast directly in the power function:

```<code>>>> import numpy as np
>>> r = np.float64(-12025.433836763057)
>>> p = 0.74
>>> np.power(r.astype(np.complex128), p)
(-715.6124638577835+762.0498735968736j)
</code>```

But the simplest approach might be to just alter the power function's return type to expect a complex number:

```<code>>>> import numpy as np
>>> r = np.float64(-12025.433836763057)
>>> p = 0.74
>>> np.power(r, p, dtype=np.complex128)
(-715.6124638577835+762.0498735968736j)
</code>```

What's interesting is that numpy does normally allow casting types from float64 to complex as long as they keep the same level of precision. However it doesn't seem to allow implicit casting of any ufunc function return types even if the casting='same_kind' kwarg is overridden.

```<code>>>> np.can_cast(np.float64, complex)
True
>>> np.can_cast(np.float64, np.complex64)
False
>>> np.can_cast(np.float64, np.complex128)
True
</code>```

According to the docs, if scalar arguments are passed to the ufunc (as opposed to arrays) it uses the logic in np.result_type and np.promote_types to determine the ufunc's return type.

https://docs.scipy.org/doc/numpy/reference/ufuncs.html

https://docs.scipy.org/doc/numpy/reference/generated/numpy.result_type.html#numpy.result_type

```<code>>>> np.result_type(r, p)
dtype('float64')
</code>```

Body:
When I am raising a negative numpy.float64 to an exponent, I am receiving an nan. Why is the complex math not supported? Is the only workaround a cast to float?

>>> from numpy import float64, power
>>> r = float64(-12025.433836763057)
>>> p = 0.74
>>> r**p
nan
>>> power(r, p)
__main__:1: RuntimeWarning: invalid value encountered in power
nan
>>> float(r)**p
(-715.6124638577838+762.049873596874j)
>>>

A suggested duplicate has a similar question, with an answer stating that this is a bug in numpy. Is this the end of the road?

Body:
Behind the scenes, python is casting that float(r)**p to return a 'complex' type.

The numpy power function is intended to work with numpy array_like structures where all the items are the same size and stored in a contiguous block of memory and its return type is inferred from its arguments.

If you are expecting complex numbers, the best approach would be to use the complex64 or complex128 types. These require more memory because each complex type consists of a real and imaginary component. So complex64 would consist of two float32 numbers and complex128 would consist of two float64 numbers.

>>> import numpy as np
>>> r = np.complex128(-12025.433836763057)
>>> p = 0.74
>>> np.power(r, p)
(-715.6124638577835+762.0498735968736j)

You can also cast directly in the power function:

>>> import numpy as np
>>> r = np.float64(-12025.433836763057)
>>> p = 0.74
>>> np.power(r.astype(np.complex128), p)
(-715.6124638577835+762.0498735968736j)

But the simplest approach might be to just alter the power function's return type to expect a complex number:

>>> import numpy as np
>>> r = np.float64(-12025.433836763057)
>>> p = 0.74
>>> np.power(r, p, dtype=np.complex128)
(-715.6124638577835+762.0498735968736j)

What's interesting is that numpy does normally allow casting types from float64 to complex as long as they keep the same level of precision. However it doesn't seem to allow implicit casting of any ufunc function return types even if the casting='same_kind' kwarg is overridden.

>>> np.can_cast(np.float64, complex)
True
>>> np.can_cast(np.float64, np.complex64)
False
>>> np.can_cast(np.float64, np.complex128)
True

According to the docs, if scalar arguments are passed to the ufunc (as opposed to arrays) it uses the logic in np.result_type and np.promote_types to determine the ufunc's return type.

https://docs.scipy.org/doc/numpy/reference/ufuncs.html

https://docs.scipy.org/doc/numpy/reference/generated/numpy.result_type.html#numpy.result_type

>>> np.result_type(r, p)
dtype('float64')


问:
Whats wrong with my If-Else conditional code that receives user input and categorizes the data by "hot" or "cold"
```<code>HighTemp1= input()
print(‘Temperature 1:’ +  HighTemp1)
LowTemp2= input()
print(‘Temperature 2:’ + LowTemp2)
if (HighTemp1>80):
   print( ‘The high temperature was Hot’)
elif(HighTemp1> 40 and <80):
   print(‘The high temperature was Average’)
else:
   print(‘The high temperature was Cold’)

if (LowTemp2>80):
   print( ‘The low temperature was Hot’)
elif(LowTemp2> 40 and <80):
   print(‘The low temperature was Average’)
else:
   print(‘The low temperature was Cold’)
</code>```

So, i'm suppose to Determine the category of two user-input temperatures for a day and output it in the following way:

Temp        Category
>80         Hot
40 to 80    Average
<40         Cold
```

Example#1:
Temperature 1: 81
Temperature 2: 37
The high temperature was Hot
The low temperature was Cold

I've gotten so many errors on this code and I'm not sure what's wrong. The code won't run in IDLE because as I run it line by line(since I get a multiple statement error), there's either a type error "cannot use > operator with both int and string" or a syntax error. What am I not understanding that's preventing my code from running?

答:
First, input() returns a string. You need to cast it into an int for your comparisons.
Second, watch your "quotes". The characters you use in your strings as quotes are not simple ASCII. I replaced them all.
Third, your comparisons were flawed. I redid the "high" ones and merely fixed the "low" ones. You can see the difference.
Also, the input() method takes a string as an input arg to output to the user, so s/he knows to input something. Probably a good thing to do.

```<code>HighTemp1 = int(input('High: ').strip())
print('Temperature 1: %d' %  HighTemp1)
LowTemp2= int(input('Low: ').strip())
print('Temperature 2: %d' % LowTemp2)
if HighTemp1 > 80:
   print('The high temperature was Hot')
# elif 40 < HighTemp1 <= 80: # simplifies to next line
elif HighTemp1 > 40:
   print('The high temperature was Average')
else:
   print('The high temperature was Cold')

if (LowTemp2 > 80):  # parens not required here
   print('The low temperature was Hot')
elif(LowTemp2 > 40):  # and LowTemp2<=80):
#                     # the above is redundant
# note that your original "and LowTemp2<80" introduced a bug for value 80
   print('The low temperature was Average')
else:
   print('The low temperature was Cold')
</code>```

Body:
HighTemp1= input()
print(‘Temperature 1:’ +  HighTemp1)
LowTemp2= input()
print(‘Temperature 2:’ + LowTemp2)
if (HighTemp1>80):
   print( ‘The high temperature was Hot’)
elif(HighTemp1> 40 and <80):
   print(‘The high temperature was Average’)
else:
   print(‘The high temperature was Cold’)

if (LowTemp2>80):
   print( ‘The low temperature was Hot’)
elif(LowTemp2> 40 and <80):
   print(‘The low temperature was Average’)
else:
   print(‘The low temperature was Cold’)

So, i'm suppose to Determine the category of two user-input temperatures for a day and output it in the following way:

Temp        Category
>80         Hot
40 to 80    Average
<40         Cold

Example#1:
Temperature 1: 81
Temperature 2: 37
The high temperature was Hot
The low temperature was Cold

I've gotten so many errors on this code and I'm not sure what's wrong. The code won't run in IDLE because as I run it line by line(since I get a multiple statement error), there's either a type error "cannot use > operator with both int and string" or a syntax error. What am I not understanding that's preventing my code from running?

Body:
First, input() returns a string. You need to cast it into an int for your comparisons.
Second, watch your "quotes". The characters you use in your strings as quotes are not simple ASCII. I replaced them all.
Third, your comparisons were flawed. I redid the "high" ones and merely fixed the "low" ones. You can see the difference.
Also, the input() method takes a string as an input arg to output to the user, so s/he knows to input something. Probably a good thing to do.

HighTemp1 = int(input('High: ').strip())
print('Temperature 1: %d' %  HighTemp1)
LowTemp2= int(input('Low: ').strip())
print('Temperature 2: %d' % LowTemp2)
if HighTemp1 > 80:
   print('The high temperature was Hot')
# elif 40 < HighTemp1 <= 80: # simplifies to next line
elif HighTemp1 > 40:
   print('The high temperature was Average')
else:
   print('The high temperature was Cold')

if (LowTemp2 > 80):  # parens not required here
   print('The low temperature was Hot')
elif(LowTemp2 > 40):  # and LowTemp2<=80):
#                     # the above is redundant
# note that your original "and LowTemp2<80" introduced a bug for value 80
   print('The low temperature was Average')
else:
   print('The low temperature was Cold')


问:
Kubernetes cluster "cni config uninitialized"
The problem i'm running into is very similar to the other existing post, except they all have the same solution therefore im creating a new thread.

The Problem:
The Master node is still in "NotReady" status after installing Flannel.

Expected result:
Master Node becomes "Ready" after installing Flannel.

Background:
I am following this guide when installing Flannel

My concern is that I am using Kubelet v1.17.2 by default that just came out like last month (Can anyone confirm if v1.17.2 works with Flannel?"

Here is the output after running the command on master node: kubectl describe node machias

```<code>Name:               machias
Roles:              master
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=machias
                    kubernetes.io/os=linux
                    node-role.kubernetes.io/master=
Annotations:        flannel.alpha.coreos.com/backend-data: {"VtepMAC":"be:78:65:7f:ae:6d"}
                    flannel.alpha.coreos.com/backend-type: vxlan
                    flannel.alpha.coreos.com/kube-subnet-manager: true
                    flannel.alpha.coreos.com/public-ip: 192.168.122.172
                    kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
                    node.alpha.kubernetes.io/ttl: 0
                    volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp:  Sat, 15 Feb 2020 01:00:01 -0500
Taints:             node.kubernetes.io/not-ready:NoExecute
                    node-role.kubernetes.io/master:NoSchedule
                    node.kubernetes.io/not-ready:NoSchedule
Unschedulable:      false
Lease:
  HolderIdentity:  machias
  AcquireTime:     <unset>
  RenewTime:       Sat, 15 Feb 2020 13:54:56 -0500
Conditions:
  Type             Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----             ------  -----------------                 ------------------                ------                       -------
  MemoryPressure   False   Sat, 15 Feb 2020 13:54:52 -0500   Sat, 15 Feb 2020 00:59:54 -0500   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure     False   Sat, 15 Feb 2020 13:54:52 -0500   Sat, 15 Feb 2020 00:59:54 -0500   KubeletHasNoDiskPressure     kubelet has no disk pressure
  PIDPressure      False   Sat, 15 Feb 2020 13:54:52 -0500   Sat, 15 Feb 2020 00:59:54 -0500   KubeletHasSufficientPID      kubelet has sufficient PID available
  Ready            False   Sat, 15 Feb 2020 13:54:52 -0500   Sat, 15 Feb 2020 00:59:54 -0500   KubeletNotReady              runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized
Addresses:
  InternalIP:  192.168.122.172
  Hostname:    machias
Capacity:
  cpu:                2
  ephemeral-storage:  38583284Ki
  hugepages-2Mi:      0
  memory:             4030364Ki
  pods:               110
Allocatable:
  cpu:                2
  ephemeral-storage:  35558354476
  hugepages-2Mi:      0
  memory:             3927964Ki
  pods:               110
System Info:
  Machine ID:                 20cbe0d737dd43588f4a2bccd70681a2
  System UUID:                ee9bc138-edee-471a-8ecc-f1c567c5f796
  Boot ID:                    0ba49907-ec32-4e80-bc4c-182fccb0b025
  Kernel Version:             5.3.5-200.fc30.x86_64
  OS Image:                   Fedora 30 (Workstation Edition)
  Operating System:           linux
  Architecture:               amd64
  Container Runtime Version:  docker://19.3.5
  Kubelet Version:            v1.17.2
  Kube-Proxy Version:         v1.17.2
PodCIDR:                      10.244.0.0/24
PodCIDRs:                     10.244.0.0/24
Non-terminated Pods:          (6 in total)
  Namespace                   Name                                          CPU Requests  CPU Limits  Memory Requests  Memory Limits  AGE
  ---------                   ----                                          ------------  ----------  ---------------  -------------  ---
  kube-system                 etcd-machias                     0 (0%)        0 (0%)      0 (0%)           0 (0%)         12h
  kube-system                 kube-apiserver-machias            250m (12%)    0 (0%)      0 (0%)           0 (0%)         12h
  kube-system                 kube-controller-manager-machias    200m (10%)    0 (0%)      0 (0%)           0 (0%)         12h
  kube-system                 kube-flannel-ds-amd64-rrfht                   100m (5%)     100m (5%)   50Mi (1%)        50Mi (1%)      12h
  kube-system                 kube-proxy-z2q7d                              0 (0%)        0 (0%)      0 (0%)           0 (0%)         12h
  kube-system                 kube-scheduler-machias            100m (5%)     0 (0%)      0 (0%)           0 (0%)         12h
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource           Requests    Limits
  --------           --------    ------
  cpu                650m (32%)  100m (5%)
  memory             50Mi (1%)   50Mi (1%)
  ephemeral-storage  0 (0%)      0 (0%)
Events:              <none>
</code>```

And the following command: kubectl get pods --all-namespaces

```<code>NAMESPACE     NAME                                         READY   STATUS    RESTARTS   AGE
kube-system   coredns-6955765f44-7nz46                     0/1     Pending   0          12h
kube-system   coredns-6955765f44-xk5r2                     0/1     Pending   0          13h
kube-system   etcd-machias.cs.unh.edu                      1/1     Running   0          13h
kube-system   kube-apiserver-machias.cs.unh.edu            1/1     Running   0          13h
kube-system   kube-controller-manager-machias.cs.unh.edu   1/1     Running   0          13h
kube-system   kube-flannel-ds-amd64-rrfht                  1/1     Running   0          12h
kube-system   kube-flannel-ds-amd64-t7p2p                  1/1     Running   0          12h
kube-system   kube-proxy-fnn78                             1/1     Running   0          12h
kube-system   kube-proxy-z2q7d                             1/1     Running   0          13h
kube-system   kube-scheduler-machias.cs.unh.edu            1/1     Running   0          13h
</code>```

Thank you for your help!

答: PodCIDR value is showing as 10.244.0.0/24.For flannel to work correctly, you must pass --pod-network-cidr=10.244.0.0/16 to kubeadm init.

Body:
The problem i'm running into is very similar to the other existing post, except they all have the same solution therefore im creating a new thread.

The Problem:
The Master node is still in "NotReady" status after installing Flannel.

Expected result:
Master Node becomes "Ready" after installing Flannel.

Background:
I am following this guide when installing Flannel

My concern is that I am using Kubelet v1.17.2 by default that just came out like last month (Can anyone confirm if v1.17.2 works with Flannel?"

Here is the output after running the command on master node: kubectl describe node machias

Name:               machias
Roles:              master
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=machias
                    kubernetes.io/os=linux
                    node-role.kubernetes.io/master=
Annotations:        flannel.alpha.coreos.com/backend-data: {"VtepMAC":"be:78:65:7f:ae:6d"}
                    flannel.alpha.coreos.com/backend-type: vxlan
                    flannel.alpha.coreos.com/kube-subnet-manager: true
                    flannel.alpha.coreos.com/public-ip: 192.168.122.172
                    kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
                    node.alpha.kubernetes.io/ttl: 0
                    volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp:  Sat, 15 Feb 2020 01:00:01 -0500
Taints:             node.kubernetes.io/not-ready:NoExecute
                    node-role.kubernetes.io/master:NoSchedule
                    node.kubernetes.io/not-ready:NoSchedule
Unschedulable:      false
Lease:
  HolderIdentity:  machias
  AcquireTime:     <unset>
  RenewTime:       Sat, 15 Feb 2020 13:54:56 -0500
Conditions:
  Type             Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----             ------  -----------------                 ------------------                ------                       -------
  MemoryPressure   False   Sat, 15 Feb 2020 13:54:52 -0500   Sat, 15 Feb 2020 00:59:54 -0500   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure     False   Sat, 15 Feb 2020 13:54:52 -0500   Sat, 15 Feb 2020 00:59:54 -0500   KubeletHasNoDiskPressure     kubelet has no disk pressure
  PIDPressure      False   Sat, 15 Feb 2020 13:54:52 -0500   Sat, 15 Feb 2020 00:59:54 -0500   KubeletHasSufficientPID      kubelet has sufficient PID available
  Ready            False   Sat, 15 Feb 2020 13:54:52 -0500   Sat, 15 Feb 2020 00:59:54 -0500   KubeletNotReady              runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized
Addresses:
  InternalIP:  192.168.122.172
  Hostname:    machias
Capacity:
  cpu:                2
  ephemeral-storage:  38583284Ki
  hugepages-2Mi:      0
  memory:             4030364Ki
  pods:               110
Allocatable:
  cpu:                2
  ephemeral-storage:  35558354476
  hugepages-2Mi:      0
  memory:             3927964Ki
  pods:               110
System Info:
  Machine ID:                 20cbe0d737dd43588f4a2bccd70681a2
  System UUID:                ee9bc138-edee-471a-8ecc-f1c567c5f796
  Boot ID:                    0ba49907-ec32-4e80-bc4c-182fccb0b025
  Kernel Version:             5.3.5-200.fc30.x86_64
  OS Image:                   Fedora 30 (Workstation Edition)
  Operating System:           linux
  Architecture:               amd64
  Container Runtime Version:  docker://19.3.5
  Kubelet Version:            v1.17.2
  Kube-Proxy Version:         v1.17.2
PodCIDR:                      10.244.0.0/24
PodCIDRs:                     10.244.0.0/24
Non-terminated Pods:          (6 in total)
  Namespace                   Name                                          CPU Requests  CPU Limits  Memory Requests  Memory Limits  AGE
  ---------                   ----                                          ------------  ----------  ---------------  -------------  ---
  kube-system                 etcd-machias                     0 (0%)        0 (0%)      0 (0%)           0 (0%)         12h
  kube-system                 kube-apiserver-machias            250m (12%)    0 (0%)      0 (0%)           0 (0%)         12h
  kube-system                 kube-controller-manager-machias    200m (10%)    0 (0%)      0 (0%)           0 (0%)         12h
  kube-system                 kube-flannel-ds-amd64-rrfht                   100m (5%)     100m (5%)   50Mi (1%)        50Mi (1%)      12h
  kube-system                 kube-proxy-z2q7d                              0 (0%)        0 (0%)      0 (0%)           0 (0%)         12h
  kube-system                 kube-scheduler-machias            100m (5%)     0 (0%)      0 (0%)           0 (0%)         12h
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource           Requests    Limits
  --------           --------    ------
  cpu                650m (32%)  100m (5%)
  memory             50Mi (1%)   50Mi (1%)
  ephemeral-storage  0 (0%)      0 (0%)
Events:              <none>

And the following command: kubectl get pods --all-namespaces

NAMESPACE     NAME                                         READY   STATUS    RESTARTS   AGE
kube-system   coredns-6955765f44-7nz46                     0/1     Pending   0          12h
kube-system   coredns-6955765f44-xk5r2                     0/1     Pending   0          13h
kube-system   etcd-machias.cs.unh.edu                      1/1     Running   0          13h
kube-system   kube-apiserver-machias.cs.unh.edu            1/1     Running   0          13h
kube-system   kube-controller-manager-machias.cs.unh.edu   1/1     Running   0          13h
kube-system   kube-flannel-ds-amd64-rrfht                  1/1     Running   0          12h
kube-system   kube-flannel-ds-amd64-t7p2p                  1/1     Running   0          12h
kube-system   kube-proxy-fnn78                             1/1     Running   0          12h
kube-system   kube-proxy-z2q7d                             1/1     Running   0          13h
kube-system   kube-scheduler-machias.cs.unh.edu            1/1     Running   0          13h

Thank you for your help!


问:
How to get the actual URL of a browser copied image in JavaScript
Ok, so when we do a "Copy Image" on a image (not image location) in Firefox for example, the data is copied to the clipboard.  If we paste into an editable
 and output the HTML of that
, we can see that it is the actual URL of the image and not the base64 URL or some object URL.  If we paste that into Paint for example, we get the actual image.  Is the URL embedded somewhere inside the clipboard data?  If so, how would we retrieve it on paste?

Here is the code to call for the paste event:

```<code>let cd = (event.clipboardData || event.originalEvent.clipboardData);

for (item of cd.items) {
    if (item.type.indexOf("image") >= 0) {
            ..... get the URL value ....

        break;
    }
}
</code>```

答:
This seems to provide a url in Firefox:

"use strict";
window.addEventListener("paste", e=> {
    let cd = e.clipboardData;
    if( cd.items.length == 2) {
       cd.items[0].getAsString( parseFragment)
    }

    function parseFragment( html) {
        let url = "";
        html.replace( /src\s*=\s*"(.*)"/, (match, p0) => (url = p0) )
        console.log( url);
    }
});```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div contentEditable style="border:thick dotted red; height: 100px; width: 200px">
</div></code>```

However

The code was arrived at by trial and error and head scratching:

the paste event generates an items object that is a DataTransferList object which is a list of DataTransferItem objects.

DataTransferItem objects have a getAsString method which takes a call back

A callback requested on the first of two DataTransferItem objects for an image paste operation in Firefox received an html fragment string.

The regex to get the src attribute of the  tag in the string is rudimentary - it just looks for a double quoted string.

So I'd say it's at least possible but without claiming the code above is robust.

Determining if Firefox's way of handling image data posts is covered in standards or works in other browsers requires more research. I personally found  no standardized way of finding information about posted image data when looking into it.

Body:
Ok, so when we do a "Copy Image" on a image (not image location) in Firefox for example, the data is copied to the clipboard.  If we paste into an editable
 and output the HTML of that
, we can see that it is the actual URL of the image and not the base64 URL or some object URL.  If we paste that into Paint for example, we get the actual image.  Is the URL embedded somewhere inside the clipboard data?  If so, how would we retrieve it on paste?

Here is the code to call for the paste event:

let cd = (event.clipboardData || event.originalEvent.clipboardData);

for (item of cd.items) {
    if (item.type.indexOf("image") >= 0) {
            ..... get the URL value ....

        break;
    }
}

Body:
This seems to provide a url in Firefox:

"use strict";
window.addEventListener("paste", e=> {
    let cd = e.clipboardData;
    if( cd.items.length == 2) {
       cd.items[0].getAsString( parseFragment)
    }

    function parseFragment( html) {
        let url = "";
        html.replace( /src\s*=\s*"(.*)"/, (match, p0) => (url = p0) )
        console.log( url);
    }
});

<div contentEditable style="border:thick dotted red; height: 100px; width: 200px">
</div>

However

The code was arrived at by trial and error and head scratching:

the paste event generates an items object that is a DataTransferList object which is a list of DataTransferItem objects.

DataTransferItem objects have a getAsString method which takes a call back

A callback requested on the first of two DataTransferItem objects for an image paste operation in Firefox received an html fragment string.

The regex to get the src attribute of the <img> tag in the string is rudimentary - it just looks for a double quoted string.

So I'd say it's at least possible but without claiming the code above is robust.

Determining if Firefox's way of handling image data posts is covered in standards or works in other browsers requires more research. I personally found  no standardized way of finding information about posted image data when looking into it.


问:
Should I index my foreign keys if they are already part of a unique constraint?
In Postgresql, I have a table with a unique constraint on two foreign keys like below:

```<code>CREATE TABLE project_ownerships(
  id BIGSERIAL  PRIMARY KEY,
  project_id BIGINT REFERENCES projects ON DELETE CASCADE,
  user_id BIGINT REFERENCES users ON DELETE CASCADE,
  role SMALLINT,
  CONSTRAINT project_user_unique UNIQUE (project_id, user_id)
);
</code>```

Having set a unique constraint on the two foreign keys project_id and user_id, does psql automatically create an index for each of them too? Or am I supposed to manually create an index for them still? Like below:

```<code>CREATE TABLE project_ownerships(
  id BIGSERIAL  PRIMARY KEY,
  project_id BIGINT REFERENCES projects ON DELETE CASCADE,
  user_id BIGINT REFERENCES users ON DELETE CASCADE,
  role SMALLINT,
  CONSTRAINT project_user_unique UNIQUE (project_id, user_id)
);

CREATE INDEX po_project_id_idx ON project_ownerships (project_id);
CREATE INDEX po_user_id_idx ON project_ownerships (user_id);
</code>```

I have read the text here, just wanted to make sure I understood it correctly in terms of actual implementation details. Specifically, with a composite index created-CONSTRAINT project_user_unique UNIQUE (project_id, user_id)- will Postgresql be able to execute an index scan(if deemed necessary) when I'm doing a join on project_id, user_id or both? Do I still need to separately create an index for each of the foreign keys?

答:
A unique constraint on (project_id, user_id) creates an index, equivalent to:

```<code>create unique index unq_project_ownerships_project_user
    on project_ownerships(project_id, user_id);
</code>```

With this index, an index on project_ownerships(project_id) would be redundant.  Wherever this index could be used, the unique index could be used.

However an index on project_ownerships(user_id) could still be useful, depending on your queries.  It could be used in situations where the unique index would not be used.

Body:
In Postgresql, I have a table with a unique constraint on two foreign keys like below:

CREATE TABLE project_ownerships(
  id BIGSERIAL  PRIMARY KEY,
  project_id BIGINT REFERENCES projects ON DELETE CASCADE,
  user_id BIGINT REFERENCES users ON DELETE CASCADE,
  role SMALLINT,
  CONSTRAINT project_user_unique UNIQUE (project_id, user_id)
);

Having set a unique constraint on the two foreign keys project_id and user_id, does psql automatically create an index for each of them too? Or am I supposed to manually create an index for them still? Like below:

CREATE TABLE project_ownerships(
  id BIGSERIAL  PRIMARY KEY,
  project_id BIGINT REFERENCES projects ON DELETE CASCADE,
  user_id BIGINT REFERENCES users ON DELETE CASCADE,
  role SMALLINT,
  CONSTRAINT project_user_unique UNIQUE (project_id, user_id)
);

CREATE INDEX po_project_id_idx ON project_ownerships (project_id);
CREATE INDEX po_user_id_idx ON project_ownerships (user_id);

I have read the text here, just wanted to make sure I understood it correctly in terms of actual implementation details. Specifically, with a composite index created-CONSTRAINT project_user_unique UNIQUE (project_id, user_id)- will Postgresql be able to execute an index scan(if deemed necessary) when I'm doing a join on project_id, user_id or both? Do I still need to separately create an index for each of the foreign keys?

Body:
A unique constraint on (project_id, user_id) creates an index, equivalent to:

create unique index unq_project_ownerships_project_user
    on project_ownerships(project_id, user_id);

With this index, an index on project_ownerships(project_id) would be redundant.  Wherever this index could be used, the unique index could be used.

However an index on project_ownerships(user_id) could still be useful, depending on your queries.  It could be used in situations where the unique index would not be used.


问:
Create aggregate df out of webscraping multiple pages using Rvest and Glue
I'm working on scraping data from a table on the following website

```<code>https://fantasy.nfl.com/research/scoringleaders?position=1&statCategory=stats&statSeason=2019&statType=weekStats&statWeek=1
</code>```

I want to create a scrape that takes all 17 weeks, all four positions (qb,rb,wr,te) and takes the first 4 pages to get the first 100 rows (only 25 shown on a page at a time).

```<code>library(tidyverse)
library(rvest)
library(glue)

scrape_19 <- function(week, position, page) {

    Sys.sleep(3)

    cat(".")

    url <- glue("https://fantasy.nfl.com/research/scoringleaders?{page}position={position}&sort=pts&statCategory=stats&statSeason=2019&statType=weekStats&statWeek={week}")

read_html(url) %>%
  html_nodes("table") %>%
  html_table(header = T) %>%
  simplify() %>%
  first() %>%
  setNames(paste0(colnames(.), as.character(.[1,]))) %>%
  slice(-1) %>%
  list()
}
</code>```

Here are all the iterations of each call in glue:

```<code>week = 1:17;
position = 1:4;
page = c("", "offset=26&", "offset=51&", "offset=76&")
</code>```

The problem I run into is when I try to make one df with all the data for each week, position and page. Here is code that works for week and position but will not work for an additional nested df.

```<code>scaffold <- tibble(week = weeks,
                   position = list(positions)) %>% tidyr::unnest()
scaffold

tbl_data <- scaffold %>%
            mutate(data = purrr::map2(week, position, ~scrape_19(.x, .y)[[1]]))
</code>```

Basically, I need help in crafting the scaffold and turning that scaffold into the final total data set with all weeks, positions and pages.

答:
Here is my attempt. I am not sure if glue() is the way to go. See below.

```<code>first_name <- c("Fred", "Ana", "Bob")
last_name <- c("JOhnson", "Trump")
glue('My name is {first_name} {last_name}.')
</code>```

Error: Variables must be length 1 or 3

Your case is similar to this example. So I tried to create all possible links using loops with map(). Then, I checked if all URLs exist or not. I used map_dfr() in order to loop through all URLs and bind all data frames. In this process, I added week and position information as well. If position is 1, it is QB. If necessary, replace these numbers by yourself. Note that I scraped four URLs in this demonstration.

```<code>library(httr)
library(rvest)
library(tidyverse)

# Create all URLs.

# Create 4 base URLs
paste("https://fantasy.nfl.com/research/scoringleaders?",
      c("", "offset=26&", "offset=51&", "offset=76&"),
      "position={position}&sort=pts&statCategory=stats&statSeason=2019&statType=weekStats&statWeek={week}",
      sep = "") -> mytemp

# For each base URL, create 4 URLs. (4 x 4 = 16 URLs)
map(.x = 1:4,
    .f = function(x){gsub(x = mytemp, pattern = "\\{position\\}", replacement = x)}) %>%
unlist -> mytemp

# For each of the 16 URLs, create 17 URLs
map(.x = 1:17,
    .f = function(x){gsub(x = mytemp, pattern = "\\{week\\}", replacement = x)}) %>%
unlist -> myurls

# Check if any URLs are invalid
sapply(myurls, url_success) %>% table

# TRUE
#  272

# Scrape the tables
map_dfr(.x = myurls[1:4],
        .f = function(x){read_html(x) %>%
                         html_nodes("table") %>%
                         html_table() %>%
                         simplify() %>%
                         first() %>%
                         setNames(paste0(colnames(.), as.character(.[1,]))) %>%
                         slice(-1) %>%
                         mutate(position = str_extract(string = x, pattern = "(?<=position=)\\d+(?=&)"),
                                week = str_extract(string = x, pattern = "(?<=statWeek=)\\d+"))},
        .id = "url") -> foo

   url Rank                               Player  Opp PassingYds PassingTD PassingInt RushingYds RushingTD ReceivingRec ReceivingYds
1    1    1               Lamar Jackson QB - BAL @MIA        324         5          -          6         -            -            -
2    1    2                Dak Prescott QB - DAL  NYG        405         4          -         12         -            -            -
3    1    3              Deshaun Watson QB - HOU  @NO        268         3          1         40         1            -            -
4    1    4            Matthew Stafford QB - DET @ARI        385         3          -         22         -            -            -
5    1    5              Patrick Mahomes QB - KC @JAX        378         3          -          2         -            -            -

   ReceivingTD RetTD MiscFumTD Misc2PT FumLost FantasyPoints position week
1            -     -         -       -       -         33.56        1    1
2            -     -         -       -       -         33.40        1    1
3            -     -         -       -       -         30.72        1    1
4            -     -         -       -       1         27.60        1    1
5            -     -         -       -       -         27.32        1    1
</code>```

Body:
I'm working on scraping data from a table on the following website

https://fantasy.nfl.com/research/scoringleaders?position=1&statCategory=stats&statSeason=2019&statType=weekStats&statWeek=1

I want to create a scrape that takes all 17 weeks, all four positions (qb,rb,wr,te) and takes the first 4 pages to get the first 100 rows (only 25 shown on a page at a time).

library(tidyverse)
library(rvest)
library(glue)

scrape_19 <- function(week, position, page) {

    Sys.sleep(3)

    cat(".")

    url <- glue("https://fantasy.nfl.com/research/scoringleaders?{page}position={position}&sort=pts&statCategory=stats&statSeason=2019&statType=weekStats&statWeek={week}")

read_html(url) %>%
  html_nodes("table") %>%
  html_table(header = T) %>%
  simplify() %>%
  first() %>%
  setNames(paste0(colnames(.), as.character(.[1,]))) %>%
  slice(-1) %>%
  list()
}

Here are all the iterations of each call in glue:

week = 1:17;
position = 1:4;
page = c("", "offset=26&", "offset=51&", "offset=76&")

The problem I run into is when I try to make one df with all the data for each week, position and page. Here is code that works for week and position but will not work for an additional nested df.

scaffold <- tibble(week = weeks,
                   position = list(positions)) %>% tidyr::unnest()
scaffold

tbl_data <- scaffold %>%
            mutate(data = purrr::map2(week, position, ~scrape_19(.x, .y)[[1]]))

Basically, I need help in crafting the scaffold and turning that scaffold into the final total data set with all weeks, positions and pages.

Body:
Here is my attempt. I am not sure if glue() is the way to go. See below.

first_name <- c("Fred", "Ana", "Bob")
last_name <- c("JOhnson", "Trump")
glue('My name is {first_name} {last_name}.')

Error: Variables must be length 1 or 3

Your case is similar to this example. So I tried to create all possible links using loops with map(). Then, I checked if all URLs exist or not. I used map_dfr() in order to loop through all URLs and bind all data frames. In this process, I added week and position information as well. If position is 1, it is QB. If necessary, replace these numbers by yourself. Note that I scraped four URLs in this demonstration.

library(httr)
library(rvest)
library(tidyverse)

# Create all URLs.

# Create 4 base URLs
paste("https://fantasy.nfl.com/research/scoringleaders?",
      c("", "offset=26&", "offset=51&", "offset=76&"),
      "position={position}&sort=pts&statCategory=stats&statSeason=2019&statType=weekStats&statWeek={week}",
      sep = "") -> mytemp

# For each base URL, create 4 URLs. (4 x 4 = 16 URLs)
map(.x = 1:4,
    .f = function(x){gsub(x = mytemp, pattern = "\\{position\\}", replacement = x)}) %>%
unlist -> mytemp

# For each of the 16 URLs, create 17 URLs
map(.x = 1:17,
    .f = function(x){gsub(x = mytemp, pattern = "\\{week\\}", replacement = x)}) %>%
unlist -> myurls

# Check if any URLs are invalid
sapply(myurls, url_success) %>% table

# TRUE
#  272

# Scrape the tables
map_dfr(.x = myurls[1:4],
        .f = function(x){read_html(x) %>%
                         html_nodes("table") %>%
                         html_table() %>%
                         simplify() %>%
                         first() %>%
                         setNames(paste0(colnames(.), as.character(.[1,]))) %>%
                         slice(-1) %>%
                         mutate(position = str_extract(string = x, pattern = "(?<=position=)\\d+(?=&)"),
                                week = str_extract(string = x, pattern = "(?<=statWeek=)\\d+"))},
        .id = "url") -> foo

   url Rank                               Player  Opp PassingYds PassingTD PassingInt RushingYds RushingTD ReceivingRec ReceivingYds
1    1    1               Lamar Jackson QB - BAL @MIA        324         5          -          6         -            -            -
2    1    2                Dak Prescott QB - DAL  NYG        405         4          -         12         -            -            -
3    1    3              Deshaun Watson QB - HOU  @NO        268         3          1         40         1            -            -
4    1    4            Matthew Stafford QB - DET @ARI        385         3          -         22         -            -            -
5    1    5              Patrick Mahomes QB - KC @JAX        378         3          -          2         -            -            -

   ReceivingTD RetTD MiscFumTD Misc2PT FumLost FantasyPoints position week
1            -     -         -       -       -         33.56        1    1
2            -     -         -       -       -         33.40        1    1
3            -     -         -       -       -         30.72        1    1
4            -     -         -       -       1         27.60        1    1
5            -     -         -       -       -         27.32        1    1