Unit test which asserts that current thread is the main thread

The question is does it make sense to write a unit test which asserts that the current thread is the main thread? Pros/cons?

Recently I’ve seen the unit test which asserts the current thread for the callback of service. I’m not sure, that it’s a good idea, I believe it’s more kind of integration test. In my opinion, the unit test should assert method in isolation and should not know about the nature of the consumer of service.

For example, in the iOS, the consumer of this service is intended to be a UI which by default has a constraint to run a code at the main thread.

PHP Class – get current request

my first question at stackoverflow:

I have a class that loads JSON data.

I request data from outside the class with eg:

echo $ class->country->name

echo $ class->country->timezone

echo $ class->country->population

Now I need to check if the requested properties do exist and if there is a valid value. As there are hundreds of properties I try to find a way to check the requested properties (country, name – country, timezone – country, population) per request within the class.

Thank you, harry_bo

How can I get the current node view mode and add to block template suggestion?

In my code, I need to get the current view mode of the node to be able to pass it to be used as a block template suggestion.

The function I have set up is able to grab all available view modes, but I just want the current one. Possibly a condition that I haven’t come across yet would do the trick?

function mytheme_theme_suggestions_block_alter(array &$  suggestions, array $  variables) { // Add template suggestions based on the current view mode and node type.   if ($  node = \Drupal::routeMatch()->getParameter('node')) {     $  view_modes = Drupal::entityQuery('entity_view_mode')    ->condition('targetEntityType', 'node')    ->execute();     foreach ($  view_modes as $  view_mode) {    $  view_mode = str_replace('node.', '', $  view_mode);    $  suggestions[] = 'block--' . $  node->getType() .'-'. $  view_mode;   }  } } 

Spotify current track to vk.com user’s status

This is refactored and much more expanded version of the script reviewed here. The whole authorization process is added.

import os import secrets import string import time import webbrowser  import furl import requests import simplejson as json  import config   URL_CODE_BASE_VK = 'https://oauth.vk.com/authorize' URL_CODE_BASE_SP = 'https://accounts.spotify.com/authorize' URL_TOKEN_VK = 'https://oauth.vk.com/access_token' URL_TOKEN_SP = 'https://accounts.spotify.com/api/token' URL_TRACK = 'https://api.spotify.com/v1/me/player/currently-playing' URL_STATUS = 'https://api.vk.com/method/status.set' EXP_IN_TOKEN_SP = 3400 EXP_IN_TOKEN_VK = 86400 FILE_TOKEN_VK = 'vk_token.json' FILE_TOKEN_SP = 'sp_token.json' INP_MSG = '''Enter the full URL, that you have been redirected on after giving the permissions: '''   def get_auth_code_vk():     url_code_params = {         'client_id': config.CLIENT_ID_VK,         'response_type': 'code',         'redirect_uri': 'https://oauth.vk.com/blank.html',         'v': 5.92,         'scope': 'status',         'state': gen_state(),         'display': 'page'     }      code = url_open(URL_CODE_BASE_VK, url_code_params)     return parse_code(code)   def get_auth_code_sp():     url_code_params = {         'client_id': config.CLIENT_ID_SP,         'response_type': 'code',         'redirect_uri': 'https://www.spotify.com/',         'scope': 'user-read-currently-playing',         'state': gen_state()     }      code = url_open(URL_CODE_BASE_SP, url_code_params)     return parse_code(code)   def gen_state():     symbols = string.ascii_lowercase + string.digits     return ''.join(secrets.choice(symbols) for _ in range(12))   def url_open(url_base, url_params):     url_code_full = furl.furl(url_base).add(url_params).url     webbrowser.open_new_tab(url_code_full)      input_url = input(INP_MSG)     return input_url   def parse_code(url):     return (url.split("code=")[1]).split("&state=")[0]   def get_token_vk():     data = {         'grant_type': 'authorization_code',         'code': get_auth_code_vk(),         'redirect_uri': 'https://oauth.vk.com/blank.html',         'client_id': 6782333,         'client_secret': config.CLIENT_SECRET_VK     }      response = requests.post(url=URL_TOKEN_VK, data=data).json()     write_file(FILE_TOKEN_VK, response)   def get_token_sp():     data = {         'grant_type': 'authorization_code',         'code': get_auth_code_sp(),         'redirect_uri': 'https://www.spotify.com/',         'client_id': config.CLIENT_ID_SP,         'client_secret': config.CLIENT_SECRET_SP     }      response = requests.post(url=URL_TOKEN_SP, data=data).json()     write_file(FILE_TOKEN_SP, response)   def write_file(tkn_file, response):     token = {}     token['token'] = response["access_token"]     token['time'] = time.time()      with open(tkn_file, 'w') as file:         file.write(json.dumps(token))   def load_file(tkn_file):     with open(tkn_file) as file:         data = json.load(file)     return data   def set_status():     params = {         'user_id': 8573490,         'v': 5.92,         'access_token': load_file(FILE_TOKEN_VK)['token'],         'text': current_track()     }      response = requests.get(url=URL_STATUS, params=params)     error = http_error(response)     if error:         return error     return response   def track_data():     tkn_file = load_file(FILE_TOKEN_SP)['token']     headers = {         'Accept': 'application/json',         'Authorization': f'Bearer {tkn_file}'     }      return requests.get(url=URL_TRACK, headers=headers)   def current_track():     response = track_data()     error = http_error(response)     if error:         return error      data = response.json()     artist = data['item']['artists'][0]['name']     track = data['item']['name']      return f'{artist} - {track}'   def http_error(response):     try:         response.raise_for_status()         return None     except requests.exceptions.HTTPError as error:         return error   def check_playback():     if track_data().status_code == 204:         print("Not playing")     else:         set_status()         print(current_track())   def token_missing(file):     return not os.path.isfile(file)   def token_expired(file, exp_in):     return time.time() - load_file(file)['time'] > exp_in   def token_not_valid(file, exp_in):     return token_missing(file) or token_expired(file, exp_in)   def run_script():     if token_not_valid(FILE_TOKEN_VK, EXP_IN_TOKEN_VK):         get_token_vk()      if token_not_valid(FILE_TOKEN_SP, EXP_IN_TOKEN_SP):         get_token_sp()      check_playback()   if __name__ == "__main__":     run_script() 

Docking station that supports both USB-C windows laptops and current Touch Bar MacBook Pros for Dual display output

I have been asked to look into docking station solutions for a dual 27-inch display setup, that supports both current generation Touch Bar MacBook Pros, USB-c windows laptops (lennevo thinkpads)and older retina MacBook Pros.

There are two issues here:

1 Current Touch Bar MacBook Pros dont support dual display output over USB-c but use thunderbolt instead so very few docking stations work for both windows usb-c and Mac usb-c for dual displays.

2 Older MacBook Pros don’t have usb-c and would need a different solution.

So I guess I wonder if there is a docking station that supports both windows and Mac dual display output over USBC, and if its possible to have an extra adapter for older MacBook Pros

Looking all over amazon and this form there doesn’t seem to be one?

Is there another solution perhaps with smaller individual dongles ?

So what solution allows for the most mobility for the workforce with a broad range of devices. Ideal solution is that an employee with any of the above devices (older MBP, new MBP or Thinkpad) can sit down at any terminal and power two displays straight away.

I know the question is a little similar to some others but I think the problem is unique.