API Design – What's the most intuitive way to organize a set of endpoints to get users and their roles in groups?

What's the most intuitive way to organize a set of endpoints to get users and their roles in groups? i.e.

  • Users and their roles in a particular group.
  • Groups in which a user is located and their role in each group.

Trial 1

We started with the following:

message UserWithRole {
  User user = 1;
  Role role = 2;
}

message GetUsersInGroupRequest {
  string group_id = 1;
}

message GetUsersInGroupResponse {
  repeated UserWithRole users = 1;
}

rpc getUsersInGroup (GetUsersInGroupRequest) returns (GetUsersInGroupResponse)

And for the downside (groups to which a user belongs):

message GroupWithRole {
  Group group = 1;
  Role role = 2;
}

message GetGroupsForUserRequest {
  string user_id = 1;
}

message GetGroupsForUserResponse {
  repeated GroupWithRole groups = 1;
}

rpc getGroupsForUser (GetGroupsForUserRequest) returns (GetGroupsForUserResponse)

This felt cumbersome and unnecessarily wordy.

Trial 2

So what about a single model?

message UserGroupMapping {
  User user = 1;
  Group group = 2;
  Role role = 3;
}

message GetUsersInGroupRequest {
  string group_id = 1;
}

message GetUsersInGroupResponse {
  repeated UserGroupMapping user_group_mappings = 1;
}

rpc getUsersInGroup (GetUsersInGroupRequest) returns (GetUsersInGroupResponse)

message GetGroupsForUserRequest {
  string user_id = 1;
}

message GetGroupsForUserResponse {
  repeated UserGroupMapping user_group_mappings = 1;
}

rpc getGroupsForUser (GetGroupsForUserRequest) returns (GetGroupsForUserResponse)

This is appealing because the same model can be used for other endpoints that return both a user and a group:

rpc getUserAndPrimaryGroup (string user_id) returns (UserGroupMapping);

Suppose we consolidate the above as follows:

Trial 2b

message UserGroupMapping {
  User user = 1;
  Group group = 2;
  Role role = 3;
}

message GetUserGroupMappingsRequest {
  enum IdType {
    USER_ID = 0;
    GROUP_ID = 1;
  }
  IdType id_type = 1;
  string id = 2;
}

message GetUserGroupMappingsResponse {
  repeated UserGroupMapping user_group_mappings = 1;
}

rpc getUserGroupMappings (GetUserGroupMappingsRequest) returns (GetUserGroupMappingsResponse)

But In any caseThe problem is that one of the dimensions, User or Group is always unnecessarily repeated even though it is the uniform for a given request, _i.e. If you receive the groups of a user, a repeat is not required User over and over, but here we are.

Summary

I know how it works, but since this will be a very central microservice (used by other microservices that manage resources, payments, audits, etc.), I want to design the API from the start.

In the meantime, I continue to search the Internet protobuf user group administration api design Examples etc., but only a lot of irrelevant results.

What's wrong with this Python program?

Write a Python program as follows:
The program gets as input no negative numbers one by one. Each number is then entered into the keyboard. At the end of the input is -1, which is not part of the input.
The program checks if the input contains ants balanced numbers.
If no, 0 will be displayed, and immediately proceed to the beginning of the next line.
If so, the maximum equalized number that is displayed in the input is determined. Then the frequency is displayed in the first line and the value in the second line. Afterwards you will be immediately taken to the beginning of the next line.
No spaces are displayed. NO text is displayed when input is completed before input is completed.
My codes are as follows:

# PART A
def even_digits(n):
    count = 0
    while n > 9:
        n //= 10
        if n % 10 % 2 == 0:
            count += 1
    return count


# PART B
def digits(n):
    count = 1
    while n > 9:
        n //= 10
        count += 1
    return count


# PART C
def odd_digits(n):
    return digits(n) - even_digits(n)


# PART D
def balanced(n):
    return even_digits(n) == odd_digits(n)


# PART E
n = int(input())
count = 0
max_balanced = -1
while n != -1:
    if balanced(n):
        if count == 0:  # first time we have a balanced number
            max_balanced = n
            count = 1
        elif n > max_balanced:
            max_balanced = n
            count = 1
        elif n == max_balanced:
            count += 1
    n = int(input())
print(count)
if count > 0:
    print(max_balanced)

But when I enter:
444333
7878787878
25
7878787878
99989888
-1

The results of this program were:
1
444333

What is different from the expected results:
2
7878787878

I'm totally a Python freshman and every answer is appreciated.

Lists – What's the best way to group / ungroup rows in a data table?

I struggle with a good solution for grouping / ungrouping rows in a data table. We initially implemented drag-and-drop, but it was a nightmare and a nightmare for end users because of third-party library restrictions.

I came up with a different method, which I hope will be easier to apply, but I'm still not 100% satisfied with it. Take a look at the demo below and let me know how to improve it.

Some articles on my list:

  • Make it clearer what a click on the checkbox will do. Maybe a edit mode or a tooltip?
  • Somehow, tweak the way the "Group Items" buttons appear.

Another idea is to mimic how to group / ungroup elements with a combination Shift + select items> right-click> group / ungroup and keyboard shortcuts. However, since it's a browser app that also supports touch devices AND is targeted at an older demographic, I'm not sure if this is a good idea.

YOUTUBE DEMO: Group and ungroup rows in the data table Demo

r – Tesla's volatility: Can some help me with that? I have no idea what's going on

I'm trying to calculate Tesla's volatility. I use this code, but I have no idea why it does not work. Everyone has an idea how to fix this:

Tesla <-  getSymbols("TSLA", src = "http://pt.stackoverflow.com/yahoo", from = "2014-10-01", to = "2019-11-25", auto.assign = FALSE)

vol.tesla <- volatility(Tesla(,6))

graf_vol_tesla <- ggplot(vol.tesla, aes(x = index(vol.tesla), y = vol.tesla))+ geom_line(color = "violetred4") +
  ggtitle("volatility Tesla") + xlab("Date") + ylab("Volatility") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_date(date_labels = "%b %y", date_breaks = "9 months")
graf_vol_tesla

There is this strange issue back:

I do not know how to automatically select a scale for an xts / zoo object.
Preset to continuous. Error: Invalid input: date_trans works with
Objects of class Date only

Pygame – What's the other step in developing a 2D Side Scrolling Platform Game?

Thanks for responding to Game Development Stack Exchange!

  • Please be sure too answer the question, Provide details and share your research!

But avoid

  • Ask for help, clarify or respond to other answers.
  • Make statements based on opinions; Cover them with references or personal experience.

Use MathJax to format equations. Mathjax reference.

For more information, see our tips for writing great answers.

tcp – What's the best practice for bidirectional communication between two Python scripts?

For a robotic application, I need to establish communication between a Mac (A) and a Raspberry Pi (B), both of which are connected to the Internet. I use Python on both machines. Both A and B must always be able to send messages to each other. For example, navigation commands from A to B and sensor data from B to A.

I'm currently using a raspberry TCP socket as a server.

What is the best way to achieve this? I tried to run the socket code in a separate thread, but in client / server mode, I can not figure out how to send a message from the server while waiting for data from the client.