Reading internal flash memory contens

Question: could someone please give a hint on how to read from flash.
I have tried to set value inside flash and then read it afterwards inside the code.
Inside ld file I have done following.
/* Specify the memory areas */
MEMORY
{
FLASH (rx)      : ORIGIN = 0x8000000, LENGTH = 2048K
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 192K
CCMRAM (rw)      : ORIGIN = 0x10000000, LENGTH = 64K
DATA (rwx)        : ORIGIN = 0x080E0000, LENGTH = 128k
}

/* Define output sections */
SECTIONS
{
    .user_data :
    {
      . = ALIGN(4);
         *(.user_data)
      . = ALIGN(4);
    } > DATA

Inside the code I set memory location to value of 1.
Then try to use that value to blink led.
/**
  ******************************************************************************
  * File Name          : main.c
  * Description        : Main program body
  ******************************************************************************
  *
  * COPYRIGHT(c) 2016 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "gpio.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
__attribute__((__section__(".user_data"))) const uint8_t userConfig[64];
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
void write_data_to_flash(uint8_t address, uint32_t data){
    HAL_FLASH_Unlock();

      __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );

     FLASH_Erase_Sector(FLASH_SECTOR_11, VOLTAGE_RANGE_3);

      HAL_FLASH_Program(TYPEPROGRAM_BYTE, &userConfig[address], 1);

      HAL_FLASH_Lock();
}

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

int main(void)
{

  /* USER CODE BEGIN 1 */
    uint8_t pause;
  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();

  /* USER CODE BEGIN 2 */

 write_data_to_flash(0,0xAABBCCDD);
//  write_data_to_flash(4,0xBBCCDDEE);
//  write_data_to_flash(2,0xCC);
//  write_data_to_flash(3,0xDD);

  pause = userConfig[0];

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
      HAL_GPIO_TogglePin(LD4_GPIO_Port,LD4_Pin);
      HAL_Delay(pause*1000);

  }
  /* USER CODE END 3 */

}

/** System Clock Configuration
*/
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  __HAL_RCC_PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 50;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

#ifdef USE_FULL_ASSERT

/**
   * @brief Reports the name of the source file and the source line number
   * where the assert_param error has occurred.
   * @param file: pointer to the source file name
   * @param line: assert_param error line source number
   * @retval None
   */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */

}

#endif

/**
  * @}
  */

/**
  * @}
*/

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Answer:
could someone please give a hint on how to read from flash.

FLASH_SECTOR_11 on the F4 is a 128Kb sector located at address 0x080E0000 (see table 5, RM0090).
There's no need to bother with all that linker stuff, just set your userConfig pointer to equal 0x080E0000 and use it for normal read access thereafter:
const volatile uint8_t *userConfig=(const volatile uint8_t *)0x080E0000;

Note that ST's flash organisation differs between device families and even between devices of the same family so if you port your code to, say, the F0, then you'll have to revisit this code.
Comment: Any way, we can do this for an array ?

Reading internal flash memory contens


How to display/define Mirror/Stripping pairs with mdadm

Question: I want to make a standard linux software Raid10 over 4 HDD. The server has 4HDDs, 2 pairs from different vendors in order to avoid batch problems.
I want to have the mirror over two different Vendors, and then the Stripe over the mirror pairs. I could do that by manually creating Raid1/0, but mdadm supports Raid level 10. I just cant figure out how the Raid10 is then handled and how the data is distributed.
mdadm --detail /dev/md10
/dev/md10:
        Version : 1.2
  Creation Time : Wed May 28 11:06:23 2014
     Raid Level : raid10
     Array Size : 1953260544 (1862.77 GiB 2000.14 GB)
  Used Dev Size : 976630272 (931.39 GiB 1000.07 GB)
   Raid Devices : 4
  Total Devices : 4
    Persistence : Superblock is persistent

    Update Time : Wed May 28 11:06:23 2014
          State : clean, resyncing (PENDING)
 Active Devices : 4
Working Devices : 4
 Failed Devices : 0
  Spare Devices : 0

         Layout : near=2
     Chunk Size : 512K

           Name : phost:10  (local to host phost)
           UUID : a3de0ad5:9e694ee1:addc6786:c4449e40
         Events : 0

    Number   Major   Minor   RaidDevice State
       0       8        1        0      active sync   /dev/sda1
       1       8       81        1      active sync   /dev/sdf1
       2       8       97        2      active sync   /dev/sdg1
       3       8      113        3      active sync   /dev/sdh1

does not really give any information about that.
How it should be:

Raid 1 / Mirror over /dev/sda1 /dev/sdf1 and /dev/sdg1 /dev/sdh1
Raid 0 over the two Raid 1 pairs

Is it possible to do that with the built in "level=10", how can I see what pairs are mirrored?
Thanks a lot for you help
Answer: I can't find a anything that says if you create a RAID10 array of 4 devices then the mirrors will be (ab) (cd) or whatever. If you want to guarantee the placement then you can create a nested raid by first of all creating two mirrors then creating a stripe with them.
Create the mirrors
mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda1 /dev/sdf1
mdadm --create /dev/md1 --level=1 --raid-devices=2 /dev/sdg1 /dev/sdh1

Then stripe them
mdadm --create /dev/md10 --level=0 --raid-devices=2 /dev/md0 /dev/md1

So now /dev/md10 is a stripe of two mirrors and you know the location of each type of disk.
Comment: I did that, Thank you. I usually just prefer that when a software offers a functionality (level=10) to use it and not use a workaround. But even after long research I could not find any information, which I still find very strange, it kind of is important.
Comment: btw. are you sure it is not '--level=0' at the stripe command?


Storing user information across all Angular controllers

Question: I have created an authentication controller which stores authenticated user information using localstorage (I am using ngStorage from Gias Kay Lee). What is the best place (in an Angular way) to access this information in different controllers. I tried using a service, code:
myApp.factory("UserService", ["$localStorage", function ($localStorage) {
    var model = {
        IsAuthenticated: $localStorage.IsAuthenticated,
        Username: $localStorage.UserName
    };
    return model;
}]);

but this (as the documentation states) behaves as a Singleton.
I also looked at using $rootScope but as far as I can see this can only be initialised in myApp.run. Am I correct?
Of course I could inject ngStorage in every controller but I am curious on how to decouple a dependency on this third party module by creating a custom global function (like the service above).
Related to this above is getting the user information to the $scope:
$scope.localStorageToScope = function () {
    $scope.UserName = $localStorage.UserName;
    $scope.UserEmail = $localStorage.UserEmail;
    $scope.UserRole = $localStorage.UserRole;
    $scope.IsAuthenticated = $localStorage.IsAuthenticated;
}

I can only think of repeating this code to every controller which I do not want of course. What would be a better way?
Side note regarding security: all secure user actions are re-authenticated on the server so no harm is done when someone hacks the localstorage mechanism.
Answer: So actually you want to encapsulate a third party service to reduce your dependency on a third party library. Your third party library provides a service $localStorage with some properties you want to access. Here is one way to do so:
function YourAuthService($localStorage) {
  this.isAuthenticated = function () {
    return $localStorage.IsAuthenticated;
  }

  this.username = function() {
    return $localStorage.username;
  }
}
myApp.service('YourAuthService', YourAuthService);

That would create an injectable wrapper service that you can use like this:
function YourController(YourAuthService) {
  console.log("Current username=" + YourAuthService.username());
  console.log("Current isAuthenticated=" + YourAuthService.isAuthenticated());
}

Note how you don't need to know how your auth service figures out the data that you query. You just call the appropriate method and your auth service will do the rest. This could easily be adapted to other storage types.
If you want to create such a simple wrapper with less code then (in your special case) it could be done like this:
function YourAuthService($localStorage) {
  ["isAuthenticated", "username", "furtherProperty", "yadayada"].forEach(function(propertyName) {
    this[propertyName] = function() {
      return $localStorage[propertyName];
    }
  });
}
myApp.service('YourAuthService', YourAuthService);

Comment: Great help! Thank you very much, just tested successfully your first approach. Second did not work because ngStorage does not seem to support an array like storage mechanism. But I like the suggestion.
Answer: There are basically two way to use the authentication data through out the app. First way you have already specified i.e. using a service, which is a good idea.
$rootScope should be avoided as this may result in unexpected behavior since this can be accessed anywhere by any module. So second step can be to mimic this $rootScope concept by creating a common controller like BaseController for a single page app and inject it to your body tag.

Now you can store your app wide common data like authentication data or anything other in the scope of BaseController which will be directly available to every other controllers scope since this will now be a parent controller scope of all controller.
Second approach can be more useful since you don't have to inject your service in every controller like in first step instead you can just use the $scope variable to access any data which is available in common controller since an scope of a controller is always being created with the parent scope.
Hope this helps!
Comment: Really interesting suggestion. Thank you for sharing this idea. Although I did not yet implement an SPA like site (I am still using separate HTML pages because some contain heavy weight JQuery controls/images, like the home page, which I do not want to spoil my core Angular pages with. But my next move will be completely removing the JQuery controls in favour of a full Angular site. Then your idea will become very handy.
Answer: I believe this is what you're looking for. Using a method in your UserService to reload your model data. The tutorial should explain it.
//In app-modules
app.run(['authService', function (authService) {
     authService.fillAuthData();
}]);

//In authService
var _fillAuthData = function () {

    var authData = localStorageService.get('authorizationData');
    if (authData)
    {
        _authentication.isAuth = true;
        _authentication.userName = authData.userName;
    }
 }

http://www.codeproject.com/Articles/784106/AngularJS-Token-Authentication-using-ASP-NET-Web-A


Floated block-level element: is it necessary to set the width, if yes, how?

Question: Say I have textboxes, dropdownlists and submit buttons. They are all inline-elements. Which means that "officially" margin, padding, width and height properties should be ignored (in practice not really). If I were to go the right way to set the height to a button I would write something like display:block and then define the height. But there are considerations that a block level element would expand itself unexpectedly so I'd better set its width to some fixed value. The problem is that I don't know its width since it can be dynamically defined upon the text of the button.
Another scenario: I wish to create a menu via
 and
. I want to have it horizontally aligned, with some items grouped to the left, and with a few stretched to the right. Both
 and
 are block-level elements. Since I wish my menu to take all available horizontal space, then to play with the items height and to have menu items pushed to both sides, the block-mode is fine to me. I'll just use float:left and float:right to achieve the task. But again use should kinda set a width to menu elements, since they are block elements. I do not know their widths because the text of the items can vary. But it seems that everything is rendered just fine as it is.
I have not noticed any issues with both inline elements forced to render as block elements without being floated or width set, or with the list item example. It works just fine in IE7, FF3, Opera 9 and Safari whatever the current version is. The question remains: should I worry about these inline-to-block elements or real block elements floated but without the width set or just leave everything as it is? Am I missing something or is it just one more of those things you simply should not expect to get right?
Answer:
Say I have textboxes, dropdownlists
  and submit buttons. They are all
  inline-elements. Which means that
  "officially" margin, padding, width
  and height properties should be
  ignored (in practice not really). If I
  were to go the right way to set the
  height to a button I would write
  something like display:block and then
  define the height. But there are
  considerations that a block level
  element would expand itself
  unexpectedly so I'd better set its
  width to some fixed value. The problem
  is that I don't know its width since
  it can be dynamically defined upon the
  text of the button.

Unless you're referring to Internet Explorer's box model quirks, you shouldn't be worried about anything expanding or contracting unexpectedly. As long as you take care of normalizing for browser variation, you will be fine. If you have an unexpected and unwanted side-effect along the way to design glory, you debug it, as that is programmer error you contend with. CSS can be quirky, but that is not an appropriate excuse 95% of the time for most major browsers. The other 5% we don't talk about.
This is how I understand your problem:
You want a menu of floated
 so that you have a horizontal menu that spans the width of the viewport (what your user sees 'in the browser window') to be consistently the full width of the viewport.
It sounds like you are thinking in terms of a fixed width design, when really what sounds like it would suit your purposes is a fluid-width design. This means you create a design that is 'elastic' and expands and contracts relative to the size of the user's browser window. If you created your design anticipating pixel widths being set on each element, you can probably find an elegant way to maintain a fluid-width header and navigation, but have a fixed-width main content area. You can find a happy medium without a full redesign. This walkthrough will likely be what you're looking for. A good explanation of the terms fixed-width and fluid-width can be found here if you're unfamiliar with the jargon and want a closer look at these ideas.
One more for the road:
Setting a width on all floated elements is not only recommended, but is a part of the CSS2 standard as per W3C.

Angelina
Comment: Link 'normalizing for browser variation' in second line is broken.
Answer: You're getting yourself a bit over stressed I think :)
Check out these links, especially 9.4, 9.5 and 10.3. And ctrl-f "inline-block"
Answer: If we take the "Official" line that you mention then life is easy as we don't need to worry about widths, heights and on for these elements.
Don't think pixel perfect, don't worry about the minutia of presentation, follow the standards, allow the browser & its users to define styles...
(if only it was really that straight-forward in the "un-Offical" real-world)
Answer: Ok, I suppose my question was not understood correctly.
What will happen if i do not set width for floated boxes? I can't set it, I don;t know it. because it depends on the content. Will all boxes just shrink to their content width? This is how it's working in practice. And i'm satisfied with it. Is there any official confirmation that this behaviour should be expected?
Added:
I foudn something interesting here: http://www.webmasterworld.com/css/3811603.htm

I suggest checking the recommendations for a more technical explanantion for the differences, but simply put, the behaviour of floats changed between css2(1998) and css2.1(a couple months of 2005, and then 2007). Per css2 it was mandatory to set a width on floats, while the "shrink-wrapping" you see in ff3 is css2.1. That means pre-Jul/2007 browsers are conforming to the recommendation relevant to their time of manufacture when they stretch div#container the full viewport width. (Which doesn't explain Opera, but it always has done its own thing.)

If so, then i'm contended. As I mentioned, it works in current IE7, FF3, Opera 9 and Safari.
Any more comments?
Comment: it will "shrinkwrap" and this is the behaviour expected and defined in the links I posted
Comment: The only browser I'm aware of that stuck with the CSS 2.0 behaviour (full width, non-shrinkwrap) was IE5/Mac. Which is not worth worrying about today, and its old layout engine (Tasman) will almost certainly have much bigger rendering problems than just floats.
Comment: @annakata
Thank you, I looked through you links, but maybe too quickly. Will do a more thorough reading when I'm home in the evening.
Answer: (assuming if i understand you problem)
In most cases you don't need to set width on list items.
But (yes, there is a but here) if you have a link with display:block (let say you want to use sliding door) you have to do a little trick:
the html:

Link

Link

Link

And CSS:
.menu li {
    float:left
}
    .menu li a {
        display:block;
    }
    /* ===================== */
    /* = and the IE6 trick = */
    /* ===================== */
    * html .menu li a {
        width:1%;
        white-space:nowrap;
    }

If you don't do like this, then the LINK will be stretched to all parent. The LINK not the LIST Element ;) Doing this way you set a "min-width" to IE6 and you won't have any other problems.
Comment: That's funny. I have links inside that are exactly blocks....

I tried to set the width:1px to LIs, but that collapsed the menu into a tiny mess.

Thanks for a great hint!

But really, do I have to worry about IE6 these days? I haven't seen it anywhere since maybe two years already...
Comment: Yes, you still have to worry about IE6. I hope this is the last year of IE6 :D


How to theme the node edit form to group fields into collapsible containers?

Question: I'd like to tidy up an unwieldy node edit form using collapsible fieldset elements.
I can do this by implementing a form_alter hook but will this affect Drupal/other modules' ability to process/validate the fields? Obviously it will change the #parents of several elements, and I'm unclear whether this change will also affect $form_state['values'].
Is it safe to do this, or is there another way to achieve the same?
Example code
$form['group1'] = [
  '#type' => 'fieldset',
  '#title' => 'My Grouped Fields',
  '#collapsible' => TRUE,
  '#collapsed' => TRUE,
  'children' => [
    'body' => $form['body'],
    'field_smallprint' => $form['field_smallprint']
    ],
  ];
unset($form['body'], $form['field_smallprint']);

It seems to me that this is probably safe unless #tree is TRUE, but I don't want to implement something that might lose data, hence the question here.
Answer: I would recommend you to first try the Field Group module that provides the option to do that kind of grouping and many more options.


Jordan Normal form method

Question: I have the matrix $A=\begin{pmatrix}0& 0& 1\\1& 0& -3\\0& 1& 3\\\end{pmatrix}$ to put into Jordan normal form. I have looked at various websites but they all seem to use different methods and I'm getting very confused.
I have that the eigenvalues are $1$ (multiplicity $3$) and I tried to row reduce $(A-I)$ to get the eigenvectors but I got the identity matrix implying that the eigenvectors are $0$.
The textbook I have shows an example which suggests that I should calculate $(A-I)^2$, then choose a vector $v$ (at random?) so that $v$ is not in the nullspace of $(A-I)^2$  but I don't follow what happens after that.
Thank you!
Comment: That has come up a few times in my search for help
Comment: We have $A=SJS^{-1}$. Do you need $S$ or just $J$?
Comment: I need both i think for a later question.
Answer: The matrix's characteristic polynomial is
$$\;\begin{vmatrix}t&0&\!\!-1\\\!\!-1&t&3\\0&\!\!-1&t-3\end{vmatrix}=(t-1)^3\;$$
Calculating the eigenspace ofr the unique eigenvalue $\;\lambda=1\;$  (btw, we already know the matrix isn't diagonalizable. Why?), inputting $\;t=1\;$ and forming the associated homogeneous system:
$$\begin{cases}&\;\;\;x&&-z=0\\&-x+&y+&\;3z=0\end{cases}\;\;\implies x=z\;,\;\;y=-2z$$
and thus the eigenspace is of dimension one, which gives he JNF (why in the above we don't need the third equation?)
$$\begin{pmatrix}1&1&0\\0&1&1\\0&0&1\end{pmatrix}$$
Comment: Thank you so much- it all makes sense now!

Jordan Normal form method


Misplacement of UISearchController

Question: I have a problem with my UISearchController. I can not place it correctly in relation to the tableView.
Table of results does not cover the entire area of the main table:

resultsTableController = ScuoleSearchResultsTableController()
resultsTableController.tableView.delegate = self
searchController = UISearchController(searchResultsController: resultsTableController)
searchController.searchResultsUpdater = self
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
searchController.delegate = self
searchController.dimsBackgroundDuringPresentation = true // default is YES
searchController.searchBar.delegate = self    // so we can monitor text changes + others
// Search is now just presenting a view controller. As such, normal view controller
// presentation semantics apply. Namely that presentation will walk up the view controller
// hierarchy until it finds the root view controller or one that defines a presentation context.
definesPresentationContext = true

Comment: If you comment out the `definesPresentationContext` line, does that fix it? It looks like this is merely a line that got left in when you copied this code from someone else.
Comment: If that _doesn't_ fix it, you will need to show how you are handling / customizing the presentation of the ScuoleSearchResultsTableController. It is ordinary presentation, and is fullscreen by default, so _you_ must be doing something to it as part of the presentation/animation that messes it up.
Comment: Hi Matt, if I comment that line, doesn't fix. ScuoleSearchResultsTableController is the controller that manages the search, I literally followed the example of Apple.
Comment: from what I understand, it seems a problem of frame of the table that manages the search results. What do you think?
Comment: Yes, obviously. But the question is why. If you followed Apple's example, ScuoleSearchResultsTableController will be a UITableViewController. So its table view is its main view, and will be resized automatically. What we need to know is why that isn't happening. For example, if this table is _inside_ another view, then evidently you forgot to give it constraints to resize it when its superview is resized.
Comment: No, it is just a UITableViewController without any parent view..
Comment: @ClaudioBarbera Have you set the height of the header? Not that I'm 100% sure it's necessary when you set a header in this way... But I'm just checking...
Comment: No, @Lyndsey i've not set height.
Comment: But you've messed with the transition / presentation in some way, right? For example, did you change the transition style so that it is Current Context instead of Full Screen? - And your ScuoleSearchResultsTableController is a storyboard scene, right? You see, I've never done that; I always just create the search controller's table view controller in code.
Comment: I thought I messed up something, but I created a new UITableViewController Storyboard. ScuoleSearchResultsTableController is not a storyboard scene, it's created in code..
Answer: Believe it or not, I solved simply by flagging an UINavigationController setting:
UINavigationBar.translucent = true

Thanks for all replies

Misplacement of UISearchController


Visual Studio 2012 keeps losing connection with TFS 2010

Question: I'm working with Visual Studio 2012 and when I wanted to check in my pending changes (on TFS 2010), all of sudden VS loses its connection with TFS and says (on the Team Explorer window): "HTTP Code 404: not found". The option to check in (or to shelve) is disabled. When I try to refresh the source control explorer window, it briefly shows my folders, but then closes it again (saying it was disconnected with TFS). The funny thing is that I still can get the latest version from TFS and even see my pending changes, but when I try to check these in or shelve my pending changes, it loses the connection.
I tried looking for an answer on Google, but the only answer I found was clearing the TFS cache at C:\Users\\Local Settings\Application Data\Microsoft\Team Foundation\4.0\Cache or starting up VS with the VS command prompt with the command "devenv /ResetSkipPkgs".
I'm clueless. Thanks in advance!
Comment: Clearing the TFS cache per your instructions worked for me. Good luck!
Comment: How annoying is this when bouncing between numerous projects daily that live in TFS 2010 and 2012. To clear the cache and have it work, you have to close/open VS.  Huge pain when this happens multiples times a day.  I have found no real solution.  But, it does work, then eventually breaks again.
Comment: There might be a lot of things causing it. Try following this article:

http://blogs.msdn.com/b/granth/archive/2008/06/26/troubleshooting-connections-to-tfs.aspx
Comment: This continues with VS 2013. Microsoft knows how to irk its users shoot itself in its foot damn too well!
Answer: On a Windows 7 client running against TFS 2010, clear the contents of the following folder:
<code>%LOCALAPPDATA%\Microsoft\Team Foundation\3.0\Cache
</code>
On a Windows 7 client running against TFS 2012, clear the contents of the following folder:
<code>%LOCALAPPDATA%\Microsoft\Team Foundation\4.0\Cache
</code>
On a Windows 7 client running against TFS 2013, clear the contents of the following folder:
<code>%LOCALAPPDATA%\Microsoft\Team Foundation\5.0\Cache
</code>


Overwrite values in for-loop PHP

Question: How can i print values in same place when i echo the statement instead of printing the values in the same/next line (if using br tag). This is a sample for-loop
for($i=0;$i<10;$i++)
{
 echo $i;
}

The output will be:

0123456789

But I want my output to be the one as shown below as it should overwrite values in the same place.

9

I will display all the values slowly one by one using timer or sleep but my objective here is to overwrite the previous value or display it in the same place.
Comment: You need to use AJAX or JS / jQuery, you cannot achieve the desired functionality like this. PHP will process it's code completely before outputting it to the browser.
Comment: If you want to make a timer - this is a wrong approach.
Comment: Please read about web basics.
Comment: So, basically you want to print the last value?
Comment: You just want to echo the last value of the loop ?
Comment: [Check This](https://3v4l.org/bZjSP). If you simply want to show the last value only
Comment: no i will make sure to display each value as a blink or in sleep but want all the values to be displayed in the same place
Comment: The page isn't displayed until PHP finishes. The browser doesn't see all the changes as they're being made by the PHP script.
Answer: I'm sorry, but you can't do it.
If you want to change the output in the browser itself, after you flushed the buffer, you have to use JS.
Think of it this way:
When you echo, you put some values in the buffer stream. When the buffer is flushed, you see the printed value in the browser.
You can manage the buffer, flush it, clean it, change it, but you can't change whats already out there.
I hope that helps some how.
Look at this link for more information about stream output:
Output Control
Comment: Sure. Glad I could help :)
Comment: Thank you . Finally got the appropriate answer.

Overwrite values in for-loop PHP


Composed cosine similarity

Question: I have the following problem.
I have 3 vectors $u,v,w$ of n dimensions.
I'm able to find cosine similarities between $u$ and $v$, and between $v$ and $w$: $cosine(u,v)$ and $cosine(v,w)$.
Can i use these cosine similarities to compute the cosine similarity between $u$ and $v$ $cosine(u,w)$?
Thanks
Comment: This is like with correlation coefficient. In general case - no, you cannot point-estimate (compute precise value) of cosine(u,w), knowing only the other two cosines. Cosine or correlation can be imagined as the angle between a pair of vectors starting from the same origin. So you have 3 vectors and you know two angles of the three. Because in general case 3 vectors lie in 3D space, the 3rd angle can vary though the other two are fixed (known).
Answer: As said by @ttnphns in the comment, you cannot use the two to calculate the third similarity. What you can do though is to use them to find bounds for it. One such "triangle inequality" is described in the Wikipedia article and more by Shubert (2021) in the "A Triangle Inequality for Cosine Similarity" paper that discusses many other such bounds and their properties.
Comment: Speaking of the computation of the bounds, I might suggest the OP to visit also this thread https://stats.stackexchange.com/q/254282/3277 . It is about correlation.
Comment: Thanks a lot for your fast and relevant answers!

Composed cosine similarity


Transmissão EFD Reinf - "falha no acesso ao Sistema de Procuração Eletrônica"

Question: Depois de atualizar as classes dos xsds para a versão 1.3.02, estou tentando transmitir eventos de abertura (R1000) e fechamento (R2099) do EFD Reinf. Fui corrigindo os erros conforme foram aparecendo até parar no seguinte:

MS0020 - Ocorreu uma falha no acesso ao Sistema de Procuração
  Eletrônica. Aguarde alguns minutos e tente novamente. (2478979111)

Não consegui progredir daí. O CNPJ nos eventos é a raiz da matriz, com oito dígitos, alinhada à direita com zeros, que de consultas ao manual e alguns fóruns foi o que eu entendi que deveria ser. O certificado é um A1 que está cadastrado no E-CAC como procurador da empresa que transmite os dados.
A mensagem parece ser um erro interno deles, mas como não vi ninguém mais falando sobre esse erro em particular imagino que seja alguma coisa no preenchimento. Seguem os xmls(hashs e assinaturas omitidos por brevidade).
Envio:
<Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/envioLoteEventos/v1_03_02">
<loteEventos>
    <evento id="ID1XXXXXXXXXXXXXX2018043013431200002">
        <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtInfoContribuinte/v1_03_02">
            <evtInfoContri id="ID1XXXXXXXXXXXXXX2018043013431200000">
                <ideEvento>
                    <tpAmb>2</tpAmb>
                    <procEmi>1</procEmi>
                    <verProc>1.0</verProc>
                </ideEvento>
                <ideContri>
                    <tpInsc>2</tpInsc>
                    <nrInsc>XXXXXXXXXXXXXX</nrInsc>
                </ideContri>
                <infoContri>
                    <inclusao>
                        <idePeriodo>
                            <iniValid>2017-01</iniValid>
                        </idePeriodo>
                        <infoCadastro>
                            <classTrib>99</classTrib>
                            <indEscrituracao>0</indEscrituracao>
                            <indDesoneracao>0</indDesoneracao>
                            <indAcordoIsenMulta>0</indAcordoIsenMulta>
                            <indSitPJ>0</indSitPJ>
                            <contato>
                                <nmCtt>Teste</nmCtt>
                                <cpfCtt>12345678900</cpfCtt>
                                <foneFixo>2122223333</foneFixo>
                                <foneCel>21912345678</foneCel>
                                <email>email@example.com</email>
                            </contato>
                        </infoCadastro>
                    </inclusao>
                </infoContri>
            </evtInfoContri>
            <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
                <SignedInfo>
                    <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
                    <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
                    <Reference URI="#ID1XXXXXXXXXXXXXX2018043013431200000">
                        <Transforms>
                            <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                            <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
                        </Transforms>
                        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                        <DigestValue>...</DigestValue>
                    </Reference>
                </SignedInfo>
                <SignatureValue>...</SignatureValue>
                <KeyInfo>
                    <X509Data>
                        <X509Certificate>...</X509Certificate>
                    </X509Data>
                </KeyInfo>
            </Signature>
        </Reinf>
    </evento>
    <evento id="ID1XXXXXXXXXXXXXX2018043013431200003">
        <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtFechamento/v1_03_02">
            <evtFechaEvPer id="ID1XXXXXXXXXXXXXX2018043013431200001">
                <ideEvento>
                    <perApur>2017-01</perApur>
                    <tpAmb>2</tpAmb>
                    <procEmi>1</procEmi>
                    <verProc>1</verProc>
                </ideEvento>
                <ideContri>
                    <tpInsc>2</tpInsc>
                    <nrInsc>XXXXXXXXXXXXXX</nrInsc>
                </ideContri>
                <ideRespInf>
                    <nmResp>Teste</nmResp>
                    <cpfResp>12345678900</cpfResp>
                    <telefone>2122223333</telefone>
                    <email>email@example.com</email>
                </ideRespInf>
                <infoFech>
                    <evtServTm>N</evtServTm>
                    <evtServPr>N</evtServPr>
                    <evtAssDespRec>N</evtAssDespRec>
                    <evtAssDespRep>N</evtAssDespRep>
                    <evtComProd>N</evtComProd>
                    <evtCPRB>N</evtCPRB>
                    <evtPgtos>N</evtPgtos>
                </infoFech>
            </evtFechaEvPer>
            <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
                <SignedInfo>
                    <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
                    <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
                    <Reference URI="#ID1XXXXXXXXXXXXXX2018043013431200001">
                        <Transforms>
                            <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                            <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
                        </Transforms>
                        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                        <DigestValue>a17VdbLyWAEaq1ABm7pMNLI340ZlQO2b4splHna5xKA=</DigestValue>
                    </Reference>
                </SignedInfo>
                <SignatureValue>...</SignatureValue>
                <KeyInfo>
                    <X509Data>
                        <X509Certificate>...</X509Certificate>
                    </X509Data>
                </KeyInfo>
            </Signature>
        </Reinf>
    </evento>
</loteEventos>

Resposta:
<ReceberLoteEventosResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <ReceberLoteEventosResult>
        <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/retornoLoteEventos/v1_03_02">
            <retornoLoteEventos id="IDB5853EC5FA5EE568D41CBEC09DD254E0">
                <ideTransmissor>
                    <IdTransmissor>16567897000170</IdTransmissor>
                </ideTransmissor>
                <status>
                    <cdStatus>0</cdStatus>
                    <descRetorno>SUCESSO</descRetorno>
                </status>
                <retornoEventos>
                    <evento id="ID1XXXXXXXXXXXXXX2018043013431200002">
                        <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtTotal/v1_03_02">
                            <evtTotal id="ID1525755225">
                                <ideEvento>
                                    <perApur />
                                </ideEvento>
                                <ideContri>
                                    <tpInsc>2</tpInsc>
                                    <nrInsc>XXXXXXXXXXXXXX</nrInsc>
                                </ideContri>
                                <ideRecRetorno>
                                    <ideStatus>
                                        <cdRetorno>1</cdRetorno>
                                        <descRetorno>ERRO</descRetorno>
                                        <regOcorrs>
                                            <tpOcorr>1</tpOcorr>
                                            <localErroAviso />
                                            <codResp>MS0020</codResp>
                                            <dscResp>Ocorreu uma falha no acesso ao Sistema de Procuração Eletrônica. Aguarde alguns minutos e tente novamente. (2478979111)</dscResp>
                                        </regOcorrs>
                                    </ideStatus>
                                </ideRecRetorno>
                                <infoRecEv>
                                    <dhProcess>2018-04-30T13:43:13.8918393-03:00</dhProcess>
                                    <tpEv>1000</tpEv>
                                    <idEv>ID1XXXXXXXXXXXXXX2018043013431200000</idEv>
                                    <hash>...</hash>
                                </infoRecEv>
                                <infoTotal />
                            </evtTotal>
                            <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
                                <SignedInfo>
                                    <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
                                    <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
                                    <Reference URI="#ID1525755225">
                                        <Transforms>
                                            <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                                            <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
                                        </Transforms>
                                        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                                        <DigestValue>...</DigestValue>
                                    </Reference>
                                </SignedInfo>
                                <SignatureValue>...</SignatureValue>
                                <KeyInfo>
                                    <X509Data>
                                        <X509Certificate>MIIHcDCCBVigAwIBAgICTGkwDQYJKoZIhvcNAQELBQAwgY4xCzAJBgNVBAYTAkJSMRMwEQYDVQQKDApJQ1AtQnJhc2lsMTYwNAYDVQQLDC1TZWNyZXRhcmlhIGRhIFJlY2VpdGEgRmVkZXJhbCBkbyBCcmFzaWwgLSBSRkIxMjAwBgNVBAMMKUF1dG9yaWRhZGUgQ2VydGlmaWNhZG9yYSBkbyBTRVJQUk9SRkIgU1NMMB4XDTE3MDkyNzE3MjI1MloXDTE4MDkyNzE3MjI1MlowgboxCzAJBgNVBAYTAkJSMRMwEQYDVQQKDApJQ1AtQnJhc2lsMTYwNAYDVQQLDC1TZWNyZXRhcmlhIGRhIFJlY2VpdGEgRmVkZXJhbCBkbyBCcmFzaWwgLSBSRkIxETAPBgNVBAsMCEFSU0VSUFJPMRowGAYDVQQLDBFSRkIgZS1TZXJ2aWRvciBBMTEvMC0GA1UEAwwmcHJlcHJvZGVmZHJlaW5mLnJlY2VpdGEuZmF6ZW5kYS5nb3YuYnIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDKOtmV56Mu/VbANmjngQ5L8o2INr+IQlXJC7rthuAitaPDS0SpSCxsETtFRik10Ri9bl/DDh1NMtRfGgLBh1RJI232gyoRcmqAG0XTY8ZQOLCd/VMpi5BOmypNnXjaj99evEI6QIvFplVJ/q63jdJX3G+pdvebbrX2EUeTcDiTFWvzG/R+Yx7pwjVZCTiRmDFfrcWoA++dXdQM1nmaqzob09TAbixPNsKYP2gG6KDnO358gyt+7ysIpGeZzrPcfS61rGl657AS1scX6LcVhJOqjdWm/tzHSAoecFpzCnThkb+R5A9DApcJ/mJYDviWG9JI0zMptnStbqv1CWf3rAHJAgMBAAGjggKoMIICpDAfBgNVHSMEGDAWgBQgjRFcVcMBb6tW8YPMaKmrwtq1YzBeBgNVHSAEVzBVMFMGBmBMAQIBWzBJMEcGCCsGAQUFBwIBFjtodHRwOi8vcmVwb3NpdG9yaW8uc2VycHJvLmdvdi5ici9kb2NzL2RwY2Fjc2VycHJvcmZic3NsLnBkZjCBiwYDVR0fBIGDMIGAMD2gO6A5hjdodHRwOi8vcmVwb3NpdG9yaW8uc2VycHJvLmdvdi5ici9sY3IvYWNzZXJwcm9yZmJzc2wuY3JsMD+gPaA7hjlodHRwOi8vY2VydGlmaWNhZG9zMi5zZXJwcm8uZ292LmJyL2xjci9hY3NlcnByb3JmYnNzbC5jcmwwVwYIKwYBBQUHAQEESzBJMEcGCCsGAQUFBzAChjtodHRwOi8vcmVwb3NpdG9yaW8uc2VycHJvLmdvdi5ici9jYWRlaWFzL2Fjc2VycHJvcmZic3NsLnA3YjCCAQkGA1UdEQSCAQAwgf2gOwYFYEwBAwigMgQwU0VSVklDTyBGRURFUkFMIERFIFBST0NFU1NBTUVOVE8gREUgREFET1MgU0VSUFJPgiZwcmVwcm9kZWZkcmVpbmYucmVjZWl0YS5mYXplbmRhLmdvdi5icqA4BgVgTAEDBKAvBC0xODAzMTk4MDI4NTYwMTY4ODAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDCgIgYFYEwBAwKgGQQXRURVQVJETyBZT1NISURBIFNBTE9NQU+gGQYFYEwBAwOgEAQOMzM2ODMxMTEwMDAxMDeBHWVkdWFyZG8ueW9zaGlkYUBzZXJwcm8uZ292LmJyMA4GA1UdDwEB/wQEAwIF4DAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwDQYJKoZIhvcNAQELBQADggIBAEA7eq1vHVjPuVdP5K/zlUlJCW7hwhn0kJDCU5ybiPQE9xW48Wx8ypz6sM9axbp9PfNMV1JkBqiDK30nNYF+xOgS+OJsNFZfdAwMgOQeZYemuTZgc6V3/QZbrhehZ447qAlZxvYaHD0fZhJuP8pMOu5gS1JOxQHHbAke/qJEVo1SQhUFttFs9daCeTlEF0xtj8wUvnKF0BF86Cc6+LI+ab0F8CVmph64uWaxLDvJ/h6LftxOcnz59z5+uIOdEQLMQWl0Qgxk+PUvhU/CacwP2B94ouQdfyaCLbSgAFck7nhgwowxX7JA3V4OlQRw7aAjt8Kikh/NjWYTfF/GZeEy90XCW8DbBmg4bSIdnaAds9Y6ne4CjO1fPILoDZRXBAzKrvBYyN6/GOd/u1nlMe3OvEPa8zpc565kxbUZZGFqPk5Tv4+11od7rJ+l/4KTj0ok0hRXnpEWQBsbSQ/YsFSOIOaID2uvyl/Q2C0YRbea+OrtmRgMqMAXWUn9TxMbAw2KgoV/V4/3Gv4QVZ/HRGPLidv9UVxf10n8imNwYrjo1focwIym254uUmyeRYcrCWh+UR0x3dwU5agb2uRNkoJ8aTGwpNNhlFdUDCwIulzi8EQRBIp1nuqEW7mQPOnH+PdKwAxd7ESfehnESjmrw0i55AZJF0rhn7CW8lxi2RXmjj9S</X509Certificate>
                                    </X509Data>
                                </KeyInfo>
                            </Signature>
                        </Reinf>
                    </evento>
                    <evento id="ID1XXXXXXXXXXXXXX2018043013431200003">
                        <Reinf xmlns="http://www.reinf.esocial.gov.br/schemas/evtTotal/v1_03_02">
                            <evtTotal id="ID1526439992">
                                <ideEvento>
                                    <perApur>2017-01</perApur>
                                </ideEvento>
                                <ideContri>
                                    <tpInsc>2</tpInsc>
                                    <nrInsc>XXXXXXXXXXXXXX</nrInsc>
                                </ideContri>
                                <ideRecRetorno>
                                    <ideStatus>
                                        <cdRetorno>1</cdRetorno>
                                        <descRetorno>ERRO</descRetorno>
                                        <regOcorrs>
                                            <tpOcorr>1</tpOcorr>
                                            <localErroAviso />
                                            <codResp>MS0020</codResp>
                                            <dscResp>Ocorreu uma falha no acesso ao Sistema de Procuração Eletrônica. Aguarde alguns minutos e tente novamente. (2478979111)</dscResp>
                                        </regOcorrs>
                                    </ideStatus>
                                </ideRecRetorno>
                                <infoRecEv>
                                    <dhProcess>2018-04-30T13:43:13.9543704-03:00</dhProcess>
                                    <tpEv>2099</tpEv>
                                    <idEv>ID1XXXXXXXXXXXXXX2018043013431200001</idEv>
                                    <hash>...=</hash>
                                </infoRecEv>
                                <infoTotal />
                            </evtTotal>
                            <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
                                <SignedInfo>
                                    <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
                                    <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
                                    <Reference URI="#ID1526439992">
                                        <Transforms>
                                            <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                                            <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
                                        </Transforms>
                                        <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                                        <DigestValue>...</DigestValue>
                                    </Reference>
                                </SignedInfo>
                                <SignatureValue>...</SignatureValue>
                                <KeyInfo>
                                    <X509Data>
                                        <X509Certificate>...</X509Certificate>
                                    </X509Data>
                                </KeyInfo>
                            </Signature>
                        </Reinf>
                    </evento>
                </retornoEventos>
            </retornoLoteEventos>
        </Reinf>
    </ReceberLoteEventosResult>

Se alguém tiver alguma ideia agradeço muito!
Comment: Pela mensagem parece ser uma instabilidade interna mesmo, da ligação com o sistema de procuração eletrônica. Você não consegue fazer um teste com outro contribuinte que você tenha o certificado digital, para tirar a prova? Se o erro estiver só no uso da procuração eletrônica, manda uma mensagem no Fale Conosco: https://idg.receita.fazenda.gov.br/contato/fale-conosco/empresa/sped/efd-reinf-1
Comment: De qualquer forma, eu não aconselharia enviar o R-1000 e o R-2099 juntos, já que não há controle sobre a ordem de processamento, e dessa forma o R-2099 pode ser processado antes do R-1000, ou seja, antes mesmo de haver o contribuinte. Então envie o R-1000, receba a resposta, se for positiva, envie o R-2099.
Comment: Vou tentar transmitir como outro CNPJ e retorno. O R1000 e o R2099 estavam juntos por razões de teste também, mas é bom saber. Sabe se, dentro de um evento tem-se garantia de que as informações e, portanto, os erros são processados em ordem?
Comment: @PedroGaspar testei com outro contribuinte e o erro foi mesmo.
Comment: Sim, o evento é processado todo de uma vez. Mas esse outro contribuinte que você testou você tem o certificado digital ou é procuração também? Tem que ser um que você tenha acesso ao  certificado digital diretamente, sem ser procuração.
Comment: É um que eu tenho acesso direto ao certificado. EDIT: e o erro continua o mesmo, mesmo não precisando de procuração.
Answer: O problema está na tag tpInsc, preenchida como <tpInsc>2</tpInsc>, relativo ao preenchimento para pessoa física, ao invés de <tpInsc>1</tpInsc> para pessoa jurídica. Trocando isso o problema foi resolvido.


How to use a Scala Map[Any,Any]

Question: I am very confused about why Scala doesn't allow things that it should be able to infer. If I want to store arbitrary types in a Map I imagine this should be possible but am blocked where there seems to be enough information to perform the operation.
scala> var m: Map[Any, Any] = Map()
m: Map[Any,Any] = Map()

scala> m = ("one" -> 1, 2 -> "two", 3 -> 3)
<console>:8: error: type mismatch;
 found   : ((String, Int), (Int, String), (Int, Int))
 required: Map[Any,Any]
       m = ("one" -> 1, 2 -> "two", 3 -> 3)
           ^

scala> m += ("one" -> 1)

scala> m += (2 -> "two")

scala> m += (3 -> 3)

scala> m
res25: Map[Any,Any] = Map(one -> 1, 2 -> two, 3 -> 3)

scala> m("one").getClass
res26: Class[_] = class java.lang.Integer

scala> m(3).getClass
res27: Class[_] = class java.lang.Integer

scala> val result = m("one")+m(3)
<console>:8: error: type mismatch;
 found   : Any
 required: String
       val result = m("one")+m(3)
                              ^

scala> val result: Int = m("one")+m(3)
<console>:8: error: type mismatch;
 found   : Any
 required: String
       val result: Int = m("one")+m(3)
                                   ^

scala> val mr1 = m("one")
mr1: Any = 1

scala> val mr1 = m("one") => Int
<console>:1: error: not a legal formal parameter.
Note: Tuples cannot be directly destructured in method or function parameters.
      Either create a single parameter accepting the Tuple1,
      or consider a pattern matching anonymous function: `{ case (param1, param1) => ... }
       val mr1 = m("one") => Int
                  ^

Ultimately I do know the type of the Key (String), and since the value's real types are known, I should be at least able to cast them, right? I also realize the code has to be type safe.
I'm clearly doing trial and mostly error here, can anyone offer guidance.
Answer: This is wrong:
m = ("one" -> 1, 2 -> "two", 3 -> 3)

You are trying to assign a Tuple3 to m, which you declared as a Map[Any,Any].  You meant:
m = Map("one" -> 1, 2 -> "two", 3 -> 3)

However, it's worth noting that a having things of type Any in your code is almost certainly a bad idea.  Type-safety is great and is one of the major benefits of using Scala.  You should take advantage of it.
Answer: In
m = ("one" -> 1, 2 -> "two", 3 -> 3)

you are trying to assign to m so the right-hand side must be compatible with Map[Any, Any]. However, ("one" -> 1, 2 -> "two", 3 -> 3) is a tuple which is not compatible, hence the error.
In
m += ("one" -> 1)

you are calling the + method of Map[Any, Any] with a Tuple2[Any, Any] argument. While ("one" -> 1) has type (String, Int) this is compatible with (Any, Any) so they can be added individually.
Note you can also do:
m += ("one" -> 1, 2 -> "two", 3 -> 3)

When you lookup elements in the map, the type of the return value is Any, so the types of m("one") and m(3) are both Any. Since Any has no + operator, m("one")+m(3) will not compile. If you know both values are Ints then you can cast them:
m("one").asInstanceOf[Int] +m(3).asInstanceOf[Int]

Comment: Yeah, more trial and error I found. `var mr2 = mr1.asInstanceOf[java.lang.Integer]`

How to use a Scala Map[Any,Any]


Log or square-root transformation for ARIMA

Question: With the below dataset, I have a series which needs transforming. Easy enough. However, how do you decide which of the SQRT or LOG transformations is better? And how do you draw that conclusion?
x<-c(75800,54700,85000,74600,103900,82000,77000,103600,62900,60700,58800,134800,81200,47700,76200,81900,95400,85400,84400,103400,63000,65500,59200,128000,74400,57100,75600,88300,111100,95000,91500,111400,73700,72800,64900,146300,83100,66200,101700,100100,120100,100200,97000,120600,88400,83500,73200,141800,87700,82700,106000,103900,121000,98800,96900,115400,87500,86500,81800,135300,88900,77100,109000,104000,113000,99000,104500,109400,92900,88700,90500,140200,91700,78800,114700,100700,113300,122800,117900,122200,102900,85300,92800,143800,88400,75400,111200,96300,114600,108300,113400,116600,103400,87300,88200,149800,90100,78800,108900,126300,122000,125100,119600,148800,114600,101600,108800,174100,101100,89900,126800,126400,141400,144700,132800,149000,124200,101500,106100,168100,104200,79900,126100,121600,139500,143100,144100,154500,129500,109800,116200,171100,106700,85500,132500,133700,135600,149400,157700,144500,165400,122700,113700,175000,113200,94400,138600,132400,129200,165700,153300,141900,170300,127800,124100,206700,131700,112700,170900,153000,146700,197800,173800,165400,201700,147000,144200,244900,146700,124400,168600,193400,167900,209800,198400,184300,214300,156200,154900,251200,127900,125100,171500,167000,163900,200900,188900,168000,203100,169800,171900,241300,141400,140600,172200,192900,178700,204600,222900,179900,229900,173100,174600,265400,147600,140800,171900,189900,185100,218400,207100,178800,228800,176900,170300,251500,149900,150300,192000,185100,184500,228800,219000,180000,241500,184300,174600,264500,166100,151900,194600,214600,201700,229400,233600,197500,254600,194000,201100,279500,175800,167200,235900,207400,215900,261800,236800,222400,281500,214100,218200,295000,194400,180200,250400,212700,251300,280200,249300,240000,304200,236900,232500,300700,207300,196900,246600,262500,272800,282300,271100,265600,313500,268000,256500,318100,232700,198500,268900,244300,262400,289200,286600,281100,330700,262000,244300,309300,246900,211800,263100,307700,284900,303800,296900,290400,356200,283700,274500,378300,263100,226900,283800,299900,296000,327600,313500,291700,333000,246500,227400,333200,239500,218600,283500,267900,294500,318600,318700,283400,351600,268400,251100,365100,249100,216400,245500,232100,236300,275600,296500,296900,354300,277900,287200,420200,299700,268200,329700,353600,356200,396500,379500,349100,437900,350600,338600,509100,342300,288800,378400,371200,395800,450000,414100,387600,486600,355300,358800,526800,346300,295600,361500,415300,402900,484100,412700,395800,491300,391000,374900,569200,369500,314900,422500,436400,439700,509200,461700,449500,560600,435000,429900,633400,417900,365700,459200,466500,488500,531500,483500,485400,575700,458000,433500,642600,409600,363100,430100,503900,500400,557400,565500,526700,628900,547700,520400,731200,494400,416800,558700,537100,556200,686700,616600,582600,725800,577700,552100,806700,554200,455000,532600,693000,619400,727100,684700)
y<-ts(x,frequency=12, start=c(1976,1))
#Transforming the data to log or sqrt and plotting it
log.y<-log(y)
plot(log.y)
sqrt.y<-sqrt(y)
plot(sqrt.y)

Comment: I am alluding to your extra machinery for "Level Shifts, Seasonal Pulses and/or Local Time Trends".
Comment: Log if that's the choice. Behaviour looks much simpler on log scale, trend is more nearly linear and variability about constant. But a Poisson regression might do just as well without the transforming-back-transforming stuff so long as you think about serial dependence. That's heresy for those who incline to the view "time series require time series software, period".
Comment: @NickCox Solutions shouldn't be less than they should be or more than they should be. If you have time series data then the analysis (probably) requires time series software as simple solutions often lead to simply incorrect conclusions. Here I stand , I can do no other ... said a favorite heretic of mine.
Comment: There are many examples of models fitted to data in time that are not time series models, e.g. growth models. At first blush this model looks well fitted by an exponential plus sinusoidal terms. If so, I would not push it into a Box-Jenkins-type ARIMA plus bells and whistles mode. Unfortunately, there is no substantive context in this question to guide recommendations.
Comment: @NickCox validation of the Gaussian assumptions are not bells and whistles but the underpining of the GLM.
Comment: @Nick These are schemes for pulling non-random structure out of the residuals and rendering it free of both stochastic and deterministic structure. These are definitely not bells and whistles just because they don't appear in some software offerings.
Comment: I understand your broad intention here. "Bells and whistles" is not pejorative in my language; it just refers to secondary features in addition to whatever captures trend and seasonality.
Comment: The devil is in the details. There are no secondary features just a seamless integration/identification of significant features. Ignoring some features con lead to misidentification of other features such as the ignoring of outliers can lead to misidentification of supposedly needed power transformations. It is not widely known but the Box-Cox test premises no anomalies. Please see http://www.autobox.com/pdfs/vegas_ibf_09a.pdf for a discussion about the classic Airline Series from Box and Jenkins.
Comment: Thank you both for the detailed info, wish i had a chance earlier to log in. @NickCox hope this is further context re previous post. The dataset is airline passenger monthly short term arrivals. Dataset is really just something to learn from. The basis for my post was to truly understand why you would take a specific transformation option over the other to then fit SARIMA(p,d,q)(P,D,Q)12 model. Actually, trying to replicate this for SAS (pretty new to SAS) I thought if I can see it in R I can transfer my knowledge of R and replicate it using SAS as I am trying to learn the language.
Comment: @IrishStat We are applying different vocabulary to the same enterprise. I look at this series and I see trend (massive) and seasonality (clear). The question then is what else might there be as well. I call those secondary features. Your procedures find various other details and build them into your model. I suspect that many data analysts would (a) prefer more parsimonious models to yours (b) wonder about how many parameters you are estimating and how many preliminary significance tests go into your model choice. These questions range from style preferences to important technicalities.
Comment: @Nick with 427 values , the number of parameters(12) in my final model is hardly a consideration. What is very true is the notion of "many t tests" but this is an aspect of search procedures which speak to augmentation while limiting the scope of that augmentation. Evaluating opportunities (the model sample space) has to be done carefully and in a way that doesn't overfit but sufficiently fits.
Answer: This question is answered beautifully by means of a spread-versus-level plot: a cube root transformation will stabilize the spreads of the data, providing a useful basis for further exploration and analysis.

The data show a clear seasonality:
plot(y)

Take advantage of this by slicing the data into annual (or possibly biennial) groups.  Within each group compute resistant descriptors of their typical value and their spread.  Good choices are based on the 5-letter summary, consisting of the median (which splits the data into upper and lower halves), the medians of the two halves (the "hinges" or "fourths"), and the extremes.  Because the extremes are not resistant to outliers, use the difference of the hinges to represent the spread.  (This "fourth-spread" is the length of a box in a properly constructed box-and-whisker plot.)
spread <- function(x) {
  n <- length(x)
  n.med <- (n + 1)/2
  n.fourth <- (floor(n.med) + 1)/2
  y <- sort(x)[c(floor(n.fourth), ceiling(n.fourth),
               floor(n+1 - n.fourth), ceiling(n+1 - n.fourth))]
  return( y %*% c(-1,-1,1,1)/2 )
}
years <- floor((1:length(x) - 1) / 12)
z <- split(x, years)
boxplot(z, names=(min(years):max(years))+1976, ylab="y")

The boxplots clearly get longer over time as the level of the data rises.  This heteroscedasticity complicates analyses and interpretations.  Often a power transformation can reduce or remove the heteroscedasticity altogether.
A spread versus level plot shows whether a power transformation (which includes the logarithm) will be helpful for stabilizing the spread within the groups and suggests an appropriate value for the power: it is directly related to the slope of the spread-vs.-level plot on log-log scales.
z.med <- unlist(lapply(z, median))
z.spread <- unlist(lapply(z, spread))
fit <- lm(log(z.spread) ~ log(z.med))
plot(log(z.med), log(z.spread), xlab="Log Level", ylab="Log Spread",
     main="Spread vs. Level Plot")
abline(fit, lwd=2, col="Red")

This plot shows good linearity and no large outliers, attesting to a fairly regular relationship between spread and level throughout the time period.
When the fitted slope is $p$, the power to use is $\lambda=1-p$.  Upon applying the suggested power transformation, the spread is (approximately) constant regardless of the level (and therefore regardless of the year):
lambda <- 1 - coef(fit)[2]
boxplot(lapply(z, function(u) u^lambda), names=(min(years):max(years))+1976,
        ylab=paste("y^", round(lambda, 2), sep=""),
        main="Boxplots of Re-expressed Values")

plot(y^lambda, main= "Re-expressed Values", ylab=paste("y^", round(lambda, 2), sep=""))

Often, powers that are reciprocals of small integers have useful or natural interpretations.  Here, $\lambda = 0.32$ is so close to $1/3$ that it may as well be the cube root.  In practice, one might choose to use the cube root, or perhaps round it to the even simpler fraction $1/2$ and take the square root, or sometimes go all the way to the logarithm (which corresponds to $\lambda = 0$).

Conclusions
In this example, the spread-versus-level plot (by virtue of its approximate linearity and lack of outliers) has shown that a power transformation will effectively stabilize the spread of the data and has automatically suggested the power to use.  Although powers can be computed using various methods, none of the standard methods provides the insight or diagnostic power afforded by the spread-versus-level plot.  This should be in the toolkit of every data analyst.

References
Tukey, John W.  Exploratory Data Analysis.  Addison-Wesley, 1977.
Hoaglin, David C., Frederick Mosteller, and John W. Tukey, Understanding Robust and Exploratory Data Analysis.  John Wiley and Sons, 1983.
Comment: This is very nicely done as always. I would put a bit more emphasis on getting trend right first and a bit less on getting the handling of variability. That boosts the case for logarithms. No bias against cube roots, which feature in various papers of mine.
Comment: @whuber thanks for the detailed write up. Really helpful post and some neat R code too.
Comment: @Nick I agree that the trends (pre-2004 and post-2004 separately) are more linear for the logs.  Some method to handle the heteroscedasticity would be essential for making forecasts: without that, forecasts based on the untransformed data would have optimistically narrow prediction intervals whereas forecasts based on logs would have pessimistically wide intervals. A short-term forecast may be robust to deviations from linearity--making cube roots a good first choice--but making longer-term forecasts does require close attention to the trend, initially favoring the log as you claim.
Comment: @Stats It's ok, but one of the virtues of the Box-Cox transformation (instead of the straight power transformation) is that it does not reverse trends.  The Box-Cox analog of your transformation is $x\to (x^{-0.87}-1)/(-0.87).$ Notice how the division by the (negative) power reverses the trend back again.  See https://stats.stackexchange.com/a/467525/919 for more discussion.
Comment: @whuber I did power transformation on a ts and got -0.870478 as value of lambda (since slope > 1). After applying it, the *trend is reversed* - Is this (negative power and trend reversal) okay while stabilizing the variance?
Comment: In the `fit <- lm(log(z.spread) ~ log(z.med))`, should I include the constant or not?
Comment: Can I use Lowess to get the level and subtract it from y (then use abs) to get the spread?
Comment: @lovetl2002  Certainly! That is one of the original uses of Lowess.
Answer: Transformations are like drugs ! Some are good for you and some aren't !. Haphazard selection of  transformations should be studiously avoided.
a) One of the requirements in order to perform valid statistical tests of necessity is that the variance of the errors from the proposed model must not be proven to be non-constant. If the variance of the errors changes at discrete points in time then one has recourse to Generalized Least Squares or GLM .
b) If the variance of the errors is linearly relatable to the level of the observed series then a Logarithmic Transformation might be appropriate. If the square root of the variance of the errors is linearly relatable to the level of the original series then a Square Root transformation is appropriate. More generally the appropriate power transformation is found via the Box-Cox test where the optimal lambda is found. Note that the Box-Cox test is universally applicable and doesn't soley requite time series or spatail data.
All of the above ( a and b ) require that the mean of the errors cannot be proven to differ significantly from zero for all points. If your data is not time series or spatial in nature then the only anomaly you can detect is a pulse. However if your data is time series or spatial then Level Shifts , Seasonal Pulses and/or Local Time Trends might be suggested to render the mean value of the error term to be 0.0 everywhere or at least not significantly different from 0.0 .
In my opinion one should never willy-nilly transform the data unless one has to in order to satisfy (in part) the Gaussian assumptions. Some econometricians take logs for the simple and simply wrong reason in order to obtain direct estimates of elasticity's rather than assessing the % change in Y for a % change in x from the best model.
Now one caveat, if one knows from theory or at least one thinks that one knows from theory that transformations are necessary i.e. proven by previous well-documented research , then by all means follow that paradigm as it may prove to be more beneficial that the empirical procedures I have laid out here.
In closing use the original data, minimize any warping of the results by mindless transformations, test all assumptions and sleep well at night. Statisticians like Doctors should never do harm to their data/patients y providing drugs/transformations that have nasty and unwarranted side-effects.
Hope This Helps .
Data Analysis using time series techniques on time series data:
a plot  suggests a series that has structural change. The Chow Test yielded a signifciant break point  .  . Analysis of the modt recent 147 values starting at 1999/5 yielded   with a Residual Plot  with the following ACF  . The forecast plot is  . The final model is  with all parameters statistically significant and no unwarranted power transformations which often unfortunately lead to wildly explosive and unrealistic forecasts. Power transforms are justified whrn it is proven via a Box-Cox test that the variablility of the ERRORS is related to the expected value as detailed here. N.B. that the variability of the original series is not used but the variability of model errors.
Comment: Thanks for the great post @IrishStat went through your attached doc too, great info!
Comment: If I can help you with SAS I would be glad to. Please contact me at my email address available from info.
Answer: @digdeep, as usual @whuber provided an excellent and comprehensive answer from a statistical view point. I'm not trained in statistics, so take this response with a grain of salt. I have used the response below in my real world practice data, so I hope this is helpful.
I'll try to provide a non statistician view of transformation of time series data for Arima modeling. There is no straightforward answer. Since you are interested in knowing which transformation to use, it might be helpful to review why we do transformation.We do transformation for 3 main reasons and there might be ton of other reasons:

Transformation makes the data's linear structure more usable for
ARIMA modeling.
If variance in the data is increasing or changing then transformation of data might be helpful to stabilize the variance in data.
Transformation also makes the errors/residuals in
    ARIMA model normally distributed which is a requirement in ARIMA modeling proposed by Box-Jenkins.

There are several data transformations including Box-Cox, Log, square root, quartic and inverse and other transformations mentioned @irishstat. As with all the statistical methods there is no good guidance/answer on which transformation to select for a particular dataset.
As the famous statistician G.E.P Box said "All models are wrong but some are useful", this would apply to the transformations as well "All transformations are wrong but some are useful".
The best way to choose a transformation is to experiment. Since you have a long time series, I would hold out the last 12 - 24 months, and build a model using all the transformation and see if a particular transformation is helpful at predicting your out of sample data accurately. Also examine the residuals for normality assumption of your model. Hopefully, this would guide you in choosing an appropriate transformation. You might also want to compare this with non-transformed data and see if the transformation helped your model.
@whuber's excellent graphical representation of your data motivated me to explore this data graphically using a decomposition method. I might  add, R has an excellent decomposition method called STL which would be helpful in identifying patterns that you would normally not notice. For a dataset like this, STL decomposition is helpful in not only selecting an appropriate method for analyzing your data, it might also  be helpful in identifying anomalies such as outliers/level shift/change in seasonality etc., See below. Notice that the remainder (irregular) component of the data, looks like there is stochastic seasonality and the variation is not random, there appears to be a pattern. See also change in level of trend component after 2004/2005 that @whuber is refrencing.
Hopefully this is helpful.
g <- stl(y,s.window = "periodic")
plot(g)


Como colocar texto em um icone?

Question: Gostaria de fazer um ícone de carrinho de compra, e colocar um numero dentro dele para ir iterando conforme os produtos que o cliente coloca no carrinho, mas não consegui fazer, um exemplo:
OBs: HTML e CSS, o icone é font-awesome, estou utilizando Bootstrap 4

HTML

CSS
.content-carrinho a {
    padding-right: 40px;
}

.content-carrinho span{
    color: white;
}

Comment: Depende do que é este icone, é uma fonte do fontawsome ou do gliph? é um SVG? é uma imagem com ``? Dê detalhes, existem muitas maneiras de se fazer uma mesma coisa, pois vai depender da tecnologia que você usou inicialmente para poder fornecer-lhe uma resposta que vai servir especificamente para o seu caso.
Comment: é um font-awesome, desculpe nao falar na pergunta
Comment: Posta o que já fez até o momento, só a parte HTML do tal icone.
Comment: pronto, a imagem que esta no post é de exemplo
Comment: joão tá usando algum outro framework que eu não saiba? o que significa `d-flex`? Isso pode conflitar com qualquer solução que eu forneça em uma resposta, melhor você ler as recomendações de como formular um exemplo: https://pt.stackoverflow.com/help/mcve, se não as pessoas vão ficar te dados várias soluções a maioria pode falhar por conflitar com algo que tem no teu site e então vamos ficar o dia inteiro até algo funcionar.
Comment: Mesmo não tendo certeza se irá conflitar, eu formulei uma resposta, experimenta https://pt.stackoverflow.com/a/237805/3635, se funcionar blz, caso contrário siga o que disse no comentário anterior.
Comment: Obrigado pelo topico de Criação de Exemplos, vou dar uma lida, é meu segundo post aqui, to meio perdido, mas o d-flex é uma propriedade do Bootstrap 4 que deixa o container flex. Obrigado pelo ajuda, vou tentar fazer o exemplo e volto para um feedback
Answer: Você pode fazer o ícone com um container para o texto.
Exemplo: https://jsfiddle.net/Ljquqtrv/
Answer: Você pode usar o seletor de pseudo-elemento chamado ::after (para navegadores antigos use :after) para posicionar um texto acima e usar o atributo do HTML5 chamado data- para customizar o valor, então vamos criar um atributo customizado:
<span data-qtd="0"

Troque o zero pela quantidade que tiver no carrinho e no ::after use content: attr() para pegar o valor do atributo:
content: attr(data-qtd);

deve ficar assim:

.content-carrinho a {
    padding-right: 40px;
}

.content-carrinho span {
    color: #0c0c0c;
    font-size: 42px; /* ajuste o tamanho do ICONE aqui*/
    position: relative; /* é necessário para enquadrar o ::after */
}

.fa-shopping-cart::after {
    font-size: 24px; /* ajuste o tamanho do texto aqui */
    top: 10px; /* ajusta no centro e vertical */
    content: attr(data-qtd); /*pega o valor do atributo */

    text-align: center;
    color: white;
    position: absolute;
    left: 0;
    width: 100%;
    height: 100%;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

Como colocar texto em um icone?


Setting default value for Foreign Key attribute in Django

Question: What is the best way to set a default value for a foreign key field in a model? Suppose I have two models, Student and Exam with student having exam_taken as foreign key. How would I ideally set a default value for it? Here's a log of my effort
class Student(models.Model):
   ....
   .....
   exam_taken = models.ForeignKey("Exam", default=1)

Works, but have a hunch there's a better way.
def get_exam():
    return Exam.objects.get(id=1)

class Student(models.Model):
    ....
    .....
    exam_taken = models.ForeignKey("Exam", default=get_exam)

But this fails with tables does not exist error while syncing.
Any help would be appreciated.
Comment: @TomIngram: `default=get_exam()` will call `get_exam` immediately and store the value permanently, whereas `default=get_exam` stores the method which would later be called each time the `default` attribute is used, to get the value at that moment. It's often used with datetime, i.e. `default=datetime.now`, *not* `default=datetime.now()`.
Comment: @TomIngram: The difference is that when you add the parenthesis, it's no longer a "callable"; it's a static value, no different that just putting an integer there.
Comment: how about: http://stackoverflow.com/questions/937954/how-do-you-specify-a-default-for-a-django-foreignkey-model-or-adminmodel-field
Comment: @NitzanTomer It for a AdminModelField. Had seen it previously.
Comment: @TomIngram: I'm not debating the merits of one approach over another. My point was only that it is valid, and the author seems to want it that way.
Comment: @ChrisPratt "This can be a value or a callable object. If callable it will be called every time a new object is created." so this means `default=get_exam()` and `default=get_exam` effectively is the same, unless you can re-call default?
Comment: @ChrisPratt hello I understand the difference etc, what I guess I'm struggling with is working out why. Giving it a bit of thought I'm thinking that as external states change perhaps the required default value will also and you would need to re-evaluate this value using the callback you passed, maybe I'm also over emphasising or misinterpreting `default` as to me it reads as something that should be relatively static, thanks it's been interesting and thought provoking for me at least :)
Answer: As already implied in @gareth's answer, hard-coding a default id value might not always be the best idea:
If the id value does not exist in the database, you're in trouble. Even if that specific id value does exist, the corresponding object may change. In any case, when using a hard-coded id value, you'd have to resort to things like data-migrations or manual editing of existing database content.
To prevent that, you could use get_or_create() in combination with a unique field (other than id).
Here's one way to do it:
from django.db import models

class Exam(models.Model):
    title = models.CharField(max_length=255, unique=True)
    description = models.CharField(max_length=255)

    @classmethod
    def get_default_pk(cls):
        exam, created = cls.objects.get_or_create(
            title='default exam',
            defaults=dict(description='this is not an exam'),
        )
        return exam.pk

class Student(models.Model):
    exam_taken = models.ForeignKey(
        to=Exam, on_delete=models.CASCADE, default=Exam.get_default_pk
    )

Here an Exam.title field is used to get a unique object, and an Exam.description field illustrates how we can use the defaults argument (for get_or_create) to fully specify the default Exam object.
Note that we return a pk, as suggested by the docs:

For fields like ForeignKey that map to model instances, defaults should be the value of the field they reference (pk unless to_field is set) instead of model instances.

Also note that default callables are evaluated in Model.__init__() (source). So, if your default value depends on another field of the same model, or on the request context, or on the state of the client-side form, you should probably look elsewhere.
Comment: @Elcast If you look closely, the [tuple is unpacked](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences) in the example: `exam, created = cls.objects.get_or_create(...)`, so the first item is assigned to `exam`, the second item is assigned to `created`. Exactly like the [example in the Django docs](https://docs.djangoproject.com/en/4.1/ref/models/querysets/#get-or-create).
Comment: It doesn't work if U have not any migrations yet for me. I use field role for User, where I use `get_default_user_role` callback as default parameter like in your example. So if I have no migrations I got `no such table: users_role` on `python manage.py makemigration`. But if I comment `role field` in *User* model and `run makemigrations` - it is no problems. After what I uncomment `role field` and `run migrations` again with no problems. Maybe U can halp me with that?
Comment: @parfeniukink: I would be happy to help, but it is a bit difficult to tell what is going wrong without seeing your actual code. Perhaps you could create a new Question with some more details?
Comment: Oh, Thanks a lot, but I've finished this task )))
Comment: I like the approach but get_or_create returns a tuple object and tuple have no pk ! exam[0] would have pk !
Comment: You're correct, I have not followed (copy paste) your code 100% so I got different results, adding created make sense now. Thanks for share and the explanation..
Comment: @djvg I Had one doubt about your answer. (This might be a silly question). Generally, we use parenthesis when calling a function in Python. But there isn't any parenthesis in (default=Exam.get_default_pk). Can you please explain why this is the case?
Comment: @DigvijayNikam `Exam.get_default_pk` (*without* parentheses) is a "reference" to the method itself: it does not *call* the method. Later, if you instantiate a `Student()` object, without specifying a value for the `exam_taken` field, it will call `Exam.get_default_pk()` (*with* parentheses) to determine the actual `default` value, at that time. Also see the [example in the documentation](https://docs.djangoproject.com/en/stable/ref/models/fields/#default).
Answer: I would modify @vault's answer above slightly (this may be a new feature). It is definitely desirable to refer to the field by a natural name. However instead of overriding the Manager I would simply use the to_field param of ForeignKey:

class Country(models.Model):
    sigla   = models.CharField(max_length=5, unique=True)

    def __unicode__(self):
        return u'%s' % self.sigla

class City(models.Model):
    nome   = models.CharField(max_length=64, unique=True)
    nation = models.ForeignKey(Country, to_field='sigla', default='IT')

Answer: I use natural keys to adopt a more natural approach:
<app>/models.py
from django.db import models

class CountryManager(models.Manager):
    """Enable fixtures using self.sigla instead of `id`"""

    def get_by_natural_key(self, sigla):
        return self.get(sigla=sigla)

class Country(models.Model):
    objects = CountryManager()
    sigla   = models.CharField(max_length=5, unique=True)

    def __unicode__(self):
        return u'%s' % self.sigla

class City(models.Model):
    nome   = models.CharField(max_length=64, unique=True)
    nation = models.ForeignKey(Country, default='IT')

Comment: I tried this in Django 1.6 but I get the error, "Invalid literal for int() with base 10: 'IT'. (My string is different, but you get the idea.)
Comment: This worked fine, though, and seems more Pythonic: `default=lambda: Country.objects.filter(sigla='IT').first()`
Comment: Strange, I remember to have tested this with django 1.6. Googling I found it's a tuple problem, try: `def get_by_natural_key(self, sigla): return (self.get(sigla=sigla),)` or also with `default=('IT',)`. I'm just guessing ;)
Comment: "Natural" is a problematic concept here.  What you seem to be saying is that you're forcing the user to choose a five-character ID for each item, but that is not inherently more natural than a serial number.
Answer: The issue with most of these approaches are that they use HARD CODED values or lambda methods inside the Model which are not supported anymore since Django Version 1.7.
In my opinion, the best approach here is to use a sentinel method which can also be used for the on_delete argument.
So, in your case, I would do
# Create or retrieve a placeholder
def get_sentinel_exam():
    return Exam.objects.get_or_create(name="deleted",grade="N/A")[0]

# Create an additional method to return only the id - default expects an id and not a Model object
def get_sentinel_exam_id():
    return get_sentinel_exam().id

class Exam(models.Model):
    ....
    # Making some madeup values
    name=models.CharField(max_length=200) # "English", "Chemistry",...
    year=models.CharField(max_length=200) # "2012", "2022",...

class Student(models.Model):
    ....
    .....
    exam_taken = models.ForeignKey("Exam",
                       on_delete=models.SET(get_sentinel_exam),
                       default=get_sentinel_exam_id
                 )

Now, when you just added the exam_taken field uses a guaranteed existing value while also, when deleting the exam, the Student themself are not deleted and have a foreign key to a deleted value.
Comment: This worked perfectly — I had a website admin where there were overall site settings, and one of them was the default language. I don't know in advance which languages will be available, and this enabled me to provide a default of "undefined".
Comment: What about performance? It doesn't execute a database hit for every call?
Comment: @realnot It's been some time that I used it, so this may be opinionated. Calling the `get_sentinel_exam` method executes a query whenever a referential action is invoked (`ON DELETE` in this case). So to answer your question - unless you don't delete a row, a second query is not performed
Comment: @Advena Had one doubt in your answer. (This might be a silly question). Generally, we use parenthesis when calling a function in Python. But there isn't any parenthesis in (default=get_sentinel_exam_id). Can you please explain why this is the case?
Comment: @DigvijayNikam, been some time i worked in Python, but I would believe that this is a method reference. With parenthesis, you're asking for it to execute immediately, except when the model is actually used. The fallback exam id could change for some reason. (Somebody correct me if I'm mistaken)
Answer: In my case, I wanted to set the default to any existing instance of the related model.  Because it's possible that the Exam with id 1 has been deleted, I've done the following:
class Student(models.Model):
    exam_taken = models.ForeignKey("Exam", blank=True)

    def save(self, *args, **kwargs):
        try:
            self.exam_taken
        except:
            self.exam_taken = Exam.objects.first()
        super().save(*args, **kwargs)

If exam_taken doesn't exist, django.db.models.fields.related_descriptors.RelatedObjectDoesNotExist will be raised when a attempting to access it.
Answer: You could use this pattern:
class Other(models.Model):
    DEFAULT_PK=1
    name=models.CharField(max_length=1024)

class FooModel(models.Model):
    other=models.ForeignKey(Other, default=Other.DEFAULT_PK)

Of course you need to be sure that there is a row in the table of Other. You should use a datamigration to be sure it exists.
Answer: You need to use get_or_create() in get_exam() and on_delete for models.ForeignKey() as shown below. *Don't forget to put .id just after get_or_create(id=1)[0] because default  in models.ForeignKey() needs id of an Exam object otherwise there is an error and you can see my answer explaining about get_or_create():
def get_exam():        # Here                # ↓ Don't forget
    return Exam.objects.get_or_create(id=1)[0].id

class Student(models.Model):
    # ...
    exam_taken = models.ForeignKey(
        "Exam",
        default=get_exam,
        on_delete=models.CASCADE # Here
    )

And, you can put get_exam() in Student class as shown below:
class Student(models.Model):
    # ...
    def get_exam(): # Here
        return Exam.objects.get_or_create(id=1)[0].id

    exam_taken = models.ForeignKey(
        "Exam",
        default=get_exam,
        on_delete=models.CASCADE
    )

I recommend to use @classmethod for get_exam() in Student class as shown below so that other classes can also use get_exam() by class name. *@classmethod can do more things than @staticmethod according to my answer:
class Student(models.Model):
    # ...
    @classmethod # Here
    def get_exam(cls):
        return Exam.objects.get_or_create(id=1)[0].id

    exam_taken = models.ForeignKey(
        "Exam",
        default=get_exam,
        on_delete=models.CASCADE
    )

And instead of get_exam, you can assign get_exam() to default as shown below:
def get_exam():
    return Exam.objects.get_or_create(id=1)[0].id

class Student(models.Model):
    # ...
    exam_taken = models.ForeignKey(
        "Exam", # ↓ Here ↓
        default=get_exam(),
        on_delete=models.CASCADE
    )

class Student(models.Model):
    # ...
    def get_exam():
        return Exam.objects.get_or_create(id=1)[0].id

    exam_taken = models.ForeignKey(
        "Exam", # ↓ Here ↓
        default=get_exam(),
        on_delete=models.CASCADE
    )

Answer: I'm looking for the solution in Django Admin, then I found this:
class YourAdmin(admin.ModelAdmin)

    def get_changeform_initial_data(self, request):
        return {'owner': request.user}

this also allows me to use the current user.
see django docs
Answer: the best way I know is to use lambdas
class TblSearchCase(models.Model):
    weights = models.ForeignKey('TblSearchWeights', models.DO_NOTHING, default=lambda: TblSearchWeights.objects.get(weight_name='value_you_want'))

so you can specify the default row..
default=lambda: TblSearchWeights.objects.get(weight_name='value_you_want')

Comment: Error: Cannot serialize function 'lambda'
Comment: that was Django 2, I'm not sure about 3 or 4.
Answer: I'm adding something on top of @djvg answer and providing some illustration to @Super Kai one's concerning the class method way of solving this problem:
Using a class method is also useful in this case, and probably in a lot of other cases, because you may have to add other ForeignKey relationships to your model later on like so:
from django.db import models

class Exam(models.Model):
    title = models.CharField(max_length=255, unique=True)
    description = models.CharField(max_length=255)

    @classmethod
    def get_default_pk(cls):
        exam, created = cls.objects.get_or_create(
            title='default exam',
            defaults=dict(description='this is not an exam'),
        )
        return exam.pk

class Student(models.Model):
    exam_taken = models.ForeignKey(
        to=Exam, on_delete=models.CASCADE, default=Exam.get_default_pk
    )

# NEW
class Teacher(models.Model):
        exam_given = models.ForeignKey(
            to=Exam, on_delete=models.CASCADE, default=Exam.get_default_pk
        )

In my case, I came here to figure out how to provide a default image to a Photo model whom I wanted to be used by several other models that would also use that default image like so:
class Photo(models.Model):

    title = models.CharField(max_length=200, unique=True)
    image = models.ImageField(upload_to='photos',
        default="default_photo_folder/default_image")

    @classmethod
    def get_or_create_default_photo_pk(cls):
        obj, created_bool = cls.objects.get_or_create(
            title='default photo', # ↓ title value will be automatically included in defaults ↓
            defaults=dict(image="default_photo_folder/default_image"),
            )
    return obj.pk

------------------------------------------------------------------
# From photoapp.models import Photo

class Event(models.Model):
    photo = models.ForeignKey(Photo,
                        on_delete=models.SET_DEFAULT,
                        related_name='event_photos',
                        default=Photo.get_or_create_default_photo_pk)

class Place(models.Model):
    photo = models.ForeignKey(Photo,
                        on_delete=models.SET_DEFAULT,
                        related_name='place_photos',
                        default=Photo.get_or_create_default_photo_pk)

Please note that in this example I already have a default photo in my media/default_photo folder. I also used models.SET_DEFAULT to set my default photo as the object's photo when its original photo would be deleted. As stated in the doc, I also used a unique field lookup in my get_or_create() method to avoid extra headache .


How to output HTML from graphql in Gatsby

Question: I'm using Prismic CMS and Gatsby (both latest version) and the officical gatsby-source-prismic plugin.
I'm trying to fetch blog posts from Prismic and display them on my blogpage which works fine.
The only thing I struggle is the HTML Serializer at v3-plugin.
I used this template https://github.com/LekoArts/gatsby-starter-prismic-i18n as my starter.- As this git is based on v2, I had to rewrite almost everything to adjust it to v3.
My "Code-Block" Slice just outputs me preformatted HTML (after Prism Syntax Highlight) which is wrong:
This is my Output:

my Code Component:
const CodeBlock = ({ input }) => {
  return (
    <>
      //this was for v2
      <Content dangerouslySetInnerHTML={{ __html: input.primary.code_block.html }} />
      //this should be for v3
      <RichText render={input.primary.code_block.raw} htmlSerializer={htmlSerializer} />
    </>
  )
}

my htmlSerializer:
    import React from 'react'
import { Elements } from 'prismic-richtext'
import Prism from 'prismjs'

const propsWithUniqueKey = function (props, key) {
  return Object.assign(props || {}, { key })
}

// Labels with this name will be inline code
const codeInline = ['text']
// Labels with these names will become code blocks
const codeBlock = ['javascript', 'css', 'scss', 'jsx', 'bash', 'json', 'diff', 'markdown', 'graphql']

const htmlSerializer = (type, element, content, children, index) => {
  var props = {}

  switch (type) {
    // First differentiate between a label and a preformatted field (e.g. the Code Block slice)
    case Elements.label: {
      // Use the inline code for labels that are in the array of "codeInline"
      if (codeInline.includes(element.data.label)) {
        return `<code class="language-${element.data.label}">${content}</code>`
      }
      // Use the blockquote for labels with the name "quote"
      if (element.data.label === 'quote') {
        return `<blockquote><p>${content}</p></blockquote>`
      }
      // Use the code block for labels that are in the array of "codeBlock"
      // Choose the right PrismJS highlighting with the label name
      if (codeBlock.includes(element.data.label)) {
        return `
${Prism.highlight(content, Prism.languages[element.label])}
`
      }
      return null
    }
    case Elements.preformatted: {
      props = { className: `language--${element.label}` }

      if (codeBlock.includes(element.label)) {

        const text = Prism.highlight(
          element.text,
          Prism.languages[element.label]
        );
        return React.createElement('div', {}, `
${Prism.highlight(
          element.text,
          Prism.languages[element.label]
        )}
`)

        // my other try
        return React.createElement('pre', { className: `language-${element.label}` },
          React.createElement('code', { className: `language-${element.label}` }, text))

      }
      return null
    }
    default: {
      return null
    }
  }
}

export default htmlSerializer

In v3 I get an error when I try to add the serializer to my gatsby-config.js .
My "version" outpuits at least the pre and code-tags but the prism-highlighter outputs just HTML. The RichText.render method doesn't work as intended, as it seems or I got a logic error in my serializer.
Answer: Maybe it's not the built-in/ideal solution but it will certainly work, using markdown-to-jsx dependency.
<Markdown>input.primary.code_block.html</Markdown>

Additionally, using your approach you can try:
  <RichText render={input.primary.code_block.html} htmlSerializer={htmlSerializer} />


Showing that the intersection of simple sets is a simple set

Question: The set of canonical intervals: $\mathcal{I}=\{[a,b) \quad | \quad a,b \in \mathbb{R} \quad \text{and} \quad a < b \}$
Simple sets: A subset $S$ of $\mathbb{R}$ is said to be simple if it's the union of a finite
number of canonical intervals.
-Attempt-
Let $s_1 =\cup_{i=1}^n I_i$ and $s_2 = \cup_{i=1}^m J_i$
I need to show that $s_1 \cap s_2$ and the difference between $s_1$ and $s_2$ is a simple set.

$s_1 \cap s_2$

Let $D= s_1 \cup s_2$ $\implies |D|=m+n$ intervals
Let $X=\{ \inf(K_i) | \; i \in \{1,2,3,...m+n\}, \; K_i \in D\}$
Let $Y=\{ \sup(K_i) | \; i \in \{1,2,3,...m+n\}, \; K_i \in D\}$
then $s_1 \cap s_2 = [\max{(X)},\min{(Y)}) \in \mathcal{I}$

$s_1 - s_2$

case 1:
$s_1 \cap s_2 = \emptyset \implies s_1 - s_2 =s_1$
case2:
$s_1 \cap s_2 \neq \emptyset \implies s_1 - s_2 = s_1 - (s_1 \cap s_2)$
But, $s_1 \cap s_2 =I \in \mathcal{I}$
Now let $j$ be the minimal among all the indices of intervals in $s_1$ st $a_j \leq \inf(I) \implies I \supseteq [a_j,bj)$. Now $[a_j,b_j) = \underbrace{[\min(a_j , \inf(I)), \sup(I))}_\text{I}\cup [\sup(I),b_1)$
Now delete $I$ from $[a_j,b_j) \implies s_1 - s_2 = s_1 - (s_1 \cap s_2) = [\sup(I),b_1) \cup_{i=1}^{j-1} I_i \cup_{i=j+1}^{n} I_i$
Answer: It's easier just to use the fact that intersection distributes over union:
$$\begin{align*}
\bigcup_{i=1}^nI_i\cap\bigcup_{i=1}^mJ_i&=\bigcup_{i=1}^nI_i\cap\bigcup_{j=1}^mJ_j\\
&=\bigcup_{i=1}^n\left(I_i\cap\bigcup_{j=1}^mJ_j\right)\\
&=\bigcup_{i=1}^n\bigcup_{j=1}^m(I_i\cap J_j)\;.
\end{align*}$$
Each $I_i\cap J_j$ is an intersection of canonical intervals, which you can easily check is either empty or a canonical interval, so $\bigcup_{i=1}^n\bigcup_{j=1}^m(I_i\cap J_j)$ is a finite union of canonical intervals and therefore a simple set.