Programmatically set a datetime field

Question: I already asked a question like this (Set date field in D8).
Ok duplicate entry? NO!
Now i have also to set a time and i'm using a array.
My code looks like this.
$field_array = [
##begin of creating node
'uid' => $new_old_uid["new_uid"],
'type' => $content_type,
'langcode' => $lang,
'status' => (!empty($import_node->status)) ? $import_node->status : 0,

#### How to create date with times?
#Every Version causes that error -> Array to string conversion in ....
### Version 1
'field_event_date_start' => [[ "value"=> date('Y-m-d H:i:00',$imported_date) ]],
'field_event_date_end' => [[ "value"=> date('Y-m-d H:i:00',$imported_date) ]],
### Version 2
'field_event_date_start' => [ "value"=> date('Y-m-d H:i:00',$imported_date) ],
'field_event_date_end' => [ "value"=> date('Y-m-d H:i:00',$imported_date) ],

### How to add a url?
'field_event_url' => (!empty($import_node->field_event_url)) ? array('value'=> $imported_url) : null,
];

UPDATE:
Another try for datetime field:
'field_event_date_start' => array(
    "value" => array(
        "date" => date('Y-m-d',$imported_date),
        "time" => date('H:i:00',$imported_date)
    ),
),

Now I'm getting following error:
Placeholders must have a trailing [] if they are to be expanded with an array of values.
Answer: Datetime fields in Drupal 8 are stored as strings. So you need to convert it into string.
You can use the format method, e.g.
$formatted_date = $date->format('Y-m-d H:i:s');

Refer Inserting the value from Datetime field form
You can also use T as separator,
Like:
\DateTime::format("Y-m-d\TH:i:s"); //need a escape character in front of T.
Refer Why is my Datetime field not saved?
Comment: thx works like charm!
Comment: @DRUPWAY, do you want to update your answer to use the `DateTimeItemInterface` constants.
Answer: If your date field is not a DATETIME but a DATE, use a different format.
And a better solution would be to use the Datime object constant to get the format.
constant DATETIME_STORAGE_FORMAT :: string(12) "Y-m-d\TH:i:s"
constant DATE_STORAGE_FORMAT :: string(5) "Y-m-d"

Working example.
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;

$dtime = DateTime::createFromFormat("m/d/Y - G:i", $row->Date); // not needed
$dtime->setTimezone(new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE));
$dtimeFormat = $dtime->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);

$article->set('field_date', $dtimeFormat);

Answer: Since Drupal 8.5.x this works a little differently.
Firstly it's good practice to set the timezone explicitly so you know what is going on:

See PHP's list of supported timezones
Get the default time zone using date_default_timezone_get(). Please note drupal_get_user_timezone() is deprecated since Drupal 8.8.0 and removed from Drupal 9.0.0.
Use Drupal's storage timezone DateTimeItemInterface::STORAGE_TIMEZONE.

Set the timezone:
$timezone = new \DateTimeZone('UTC');
// Or
$timezone = new \DateTimeZone(date_default_timezone_get());
// Or
$timezone = new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE);

Then create the date object using the PHP DateTime class:
$date_time = new \DateTime('now', $timezone);
$drupal_date_time = DrupalDateTime::createFromDateTime($date_time);

Or the DrupalDateTime class:
$drupal_date_time = new DrupalDateTime('yesterday', $timezone);

I like to use DateTime (or DateTimeImmutable) and then pass into the DrupalDateTime wrapper. That way native methods are highlighted in the IDE and also date comparisons (e.g. $now > $somedatetime work reliably.
Prior to saving the date/time you must set the timezone to DateTimeItemInterface::STORAGE_TIMEZONE:
$drupal_date_time->setTimezone(new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE))

You can then format the date for storage using the the DateTimeItemInterface::DATETIME_STORAGE_FORMAT constant:
$drupal_date_time->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT)

Here is an example usage:
$node = $this->entityTypeManager
    ->getStorage('node')
    ->create([
      'type' => 'article',
      'title' => 'test',
    ]);
// Get the default timezone
$default_timezone = new \DateTimeZone(date_default_timezone_get());
// Set some date/time objects
$now = new \DateTime('now', $default_timezone);
$yesterday = new \DateTime('yesterday', $default_timezone);

// Compare the dates
if($yesterday < $now){
  // Do some stuff
}

// Optionally convert to DrupalDateTime if you need the custom methods, etc
$now = DrupalDateTime::createFromDateTime($now);
$yesterday = DrupalDateTime::createFromDateTime($yesterday);

// Get the storage timezone
$storage_timezone = new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE);

// Save the dates to the fields
$node->set(
    'field_date_now',
    $now
      ->setTimezone($storage_timezone)
      ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT)
);
$node->set(
    'field_date_yesterday',
    $yesterday
      ->setTimezone($storage_timezone)
      ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT)
);

$node->save();

A bonus example — find nodes with a date/time field value that matches a 24hr window:
$date = new \DateTimeImmutable('now - 1 month', new \DateTimeZone(date_default_timezone_get()));

$start_time = $date->modify('midnight');
$start_time = DrupalDateTime::createFromDateTime($start_time);
$end_time = $date->modify('+1 day midnight -1 second');
$end_time = DrupalDateTime::createFromDateTime($end_time);

// Find all NODES with date times that match the 24hr window
$query = \Drupal::entityQuery('node')
  //      ->condition('status', 1)
  ->condition('type', 'event')
  ->condition(
    'field_date',
    $start_time
      ->setTimezone(new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE))
      ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
    '>='
  )
  ->condition(
    'field_date',
    $end_time
      ->setTimezone(new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE))
      ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
    '<='
  )

Comment: You should put the backslashes back. See https://www.drupal.org/docs/develop/coding-standards/namespaces#s-use-ing-classes. Also look how core does it.
Comment: Fair point @leymannx. It just doesn't look as pretty!
Answer: This is what worked for me:
$timestamp = \Drupal::time()->getRequestTime();
$date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
$entity->field_date = $date->format("Y-m-d\TH:i:s");

Comment: You are hardcoding the storage format and timezone. This will work for now, but it may break silently in future versions. You want this:

    `$timestamp = \Drupal::time()->getRequestTime(); $date = DrupalDateTime::createFromTimestamp($timestamp, DateTimeItemInterface::STORAGE_TIMEZONE); $storableDate = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT); $entity->set('field_date, $storableDate);`
Answer: This works great for date fields, as noted. However, if you need to update a timestamp based field use something like:
 $entityref->set('field_timestamp_field', $source_date->getTimestamp());

Programmatically set a datetime field


Modal window open once when user visits website

Question: I'm trying to build simple modal window using jquery ui implementation in my custom module. I want to show this modal once when user visit my website. I choose implementation with cookie.
How to make this modal appears once to user when he visit my website? should I use hook_init?
this is my codes:
mymodule.module
/* menu callback */
function mymodule_menu() {
    $items['mymodule-popup'] = array(
        'title' => 'Join Club',
        'page callback' => 'mymodule_ajax_register',
        'page arguments' => array(1),
        'access callback' => TRUE,
        'type' => MENU_CALLBACK
    );

    return $items;
}

function mymodule_ajax_register() {
    $path = drupal_get_path('module', 'mymodule');
    drupal_add_library('system', 'ui.dialog', false);
    drupal_add_library('system', 'ui.draggable', false);
    drupal_add_js($path.'/mymodule.js');

    $output = '';
    $webform_nid = 30;
    $node = node_load($webform_nid);
    $submission = (object) array();
    $webform = drupal_get_form('webform_client_form_'.$webform_nid, $node, $submission);
    $output .= '
';
    $output .= drupal_render($webform);
    $output .= '
';

    return $output;
}

mymodule.js
(function ($) {
$(document).ready(function(){
    $('#popup').dialog({
        height: 'auto',
        width: 700,
        autoOpen: false,
        modal: true,
    resizable: false
    });

    $('a').click(function(){
        var status = false;
        if(this.className !== 'lightbox-processed'){
            if(!getCookie('newspopup')){
                setCookie('newspopup', 'true', 1);
                $('#popup').dialog('open');
            }else{status = true;}
            if(this.className === 'ui-dialog-titlebar-close ui-corner-all ui-state-hover'){
                $('#popup').dialog('close');
                status = true;
            }else{
            }
        }
        if(this.id === 'bottomNavClose'){
            $('#popup').dialog('close');
        }
        return status;
    });

    function setCookie(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    }

    function getCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)===' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }

    function deleteCookie(name) {
        setCookie(name,"",-1);
    }
});
})(jQuery);

with those codes, when user visit myweb.com/mymodule-popup then click any link/url, modal window (pop up window) will appears.
How to make modal window appears once every time user visit my website?
I need advise from you guys, Thanks before.
Answer: Why invent a new module while there are existing, well-supported ones?
Two existing modules:

The popup_message does what you want and has a lot of features and a large installed base.
popup_announcement - this is a relatively new module with full html support, but with only a few downloads.
Comment: To answer why: Sometimes these modules are simply too much and site becomes too slow. Sometimes it's meant as a part of someone's own module and it's easier to make it anew than to ask strangers to provide hooks you would need. And sometimes it's just an exercise, something one is doing to learn. And sometimes, of course, it's the pure lack of knowledge about ready-made options ;)
Comment: Learning is a good argument. But to make Drupal stronger we must reuse more - this way we will have better modules. Even while learning, the abovementioned modules may serve as code examples.
Comment: Well, only one of my requests for hooks was ever fulfilled, and it was months to wait. My request to restore views functionality that got removed recently (but stayed described as present in the docs) was ignored, even when I provided patch for it. And so on. Sorry, reuse is good idea, but *not* when you need your job done and deadline is closer than years...
Comment: "Hook requests were never fullfilled"? So question is how to convince them. Maybe open a new module rating site where pepole will get rating depending on the level of support they give. But that's a meta-discussion.
Answer: The way I did it was as follows:

In .module file I provided a HTML output for my modal window.
In .info, CSS and JS files was added.
CSS defined overlay and window, so all I needed to do to switch it visible / invisible was to change one element's class, add or remove .modal_visible (name of the class does not really matters but that's what I used)
In JS I created behavior, and algorithm as follows:

If the context was not right (IE new elements added instead of page load), end.
If the cookie "already seen" is set, end.
Set "already seen" cookie
Attach function to close button's onclick, to remove  .modal_visible from overlay
Add .modal_visible to overlay

I sold copyright to code, but algorithm should be simple enough to reuse.


hook_taxonomy_term_view_alter is not being called

Question: On my Drupal 7 site, I have a module which needs to determine the type of page being shown / being loaded.
I first implemented hook_entity_view_alter. Doing this:
/**
 * Implements hook_entity_view_alter
 */
/*
function rsc_search_entity_view_alter(&$build, $type)
{
  echo "$type
";
  echo $build['#entity_type'].'
';
}

shows the info I need, so I know that the hook is being called. BUT, on taxonomy term pages, nothing shows up, although there should be an entity with type 'taxonomy_term'. See the last few lines of code at: http://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/function/taxonomy_term_view/7
So I tried the next thing, which is implementing hook_taxonomy_term_view_alter to catch those term pages:
/**
 * Implements hook_taxonomy_term_view_alter
 */
function rsc_search_taxonomy_term_view_alter(&$build)
{
  $var = $build;
  $build['extra'] = array('#markup'=>'nothing');
  echo 'tax_term';
  if ($build['#view_mode']=='full')
  {
    echo '
';
    var_dump($build);
    echo '
';
  }
}

This, however, NEVER DOES ANYTHING!
As far as I understand the documentation, I
* used the correct function names
* used the correct function parameters
* remembered to clear the caches
Any ideas on what went wrong? Why can't I implement taxonomy alteration hooks?
EDIT
Clive asked whether I am using views taxonomy. But I am using a custom taxonomy page, which still calls taxonomy_term_view
function CSL_custom_admin_menu_alter(&$items)
{
  $items['taxonomy/term/%taxonomy_term'] = array(
    'title' => 'Taxonomy term',
    'title callback' => 'taxonomy_term_title',
    'title arguments' => array(2),
    'page callback' => 'taxonomy_term_page',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    'file' => 'taxonomy.pages.inc',
    'file path' => 'sites/all/modules/CSL_custom_admin'
  );
}

Here is an extract from my taxonomy.pages.inc:
/**
 * Menu callback; displays all nodes associated with a term.
 *
 * @param $term
 *   The taxonomy term.
 * @return
 *   The page content.
 */
function taxonomy_term_page($term)
{
  drupal_set_breadcrumb(CSL_taxonomy_breadcrumb($term));

  drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name);

  $subs = CSL_taxonomy_subs_count(taxonomy_get_tree($term->vid, $term->tid, 1));

  $nids = CSL_taxonomy_nids($term->tid, TRUE, variable_get('default_nodes_main', 10), ( isset($term->field_enable_sort['und'][0]['value'])&&$term->field_enable_sort['und'][0]['value'] ) || ( isset($term->field_toelaat_sorteer['und'][0]['value'])&&$term->field_toelaat_sorteer['und'][0]['value'] ) );

  $build = array();

  // Add term heading if the term has a description
  if (!empty($term->description))
  {
    $build['term_heading'] = array(
        '#prefix' => '
',
        '#suffix' => '
',
        'term' => taxonomy_term_view($term, 'full'),
        '#weight' => 1
    );
  }

  if ($nids && $subs)
  {
    $build['subterms'] = array
    (
        '#prefix' => '
',
        '#suffix' => '
',
        'subterms' => array
        (
            '#theme' => 'CSL_custom_admin_taxsubstable',
            '#rows' => $subs,
            '#weight' => 2
        )
    );

    $build['nodes'] = array
    (
        '#prefix' => '
',
        '#suffix' => '
',
        'nodes' => node_view_multiple(node_load_multiple($nids)),
        '#weight' => 3
    );
  }
  else if ($nids) // no subs
  {
    $build['subterms'] = array
    (
        '#prefix' => '
',
        '#suffix' => '
',
        '#markup' => ''.t('This term has no subcategories.').'',
        '#weight' => 2
    );

    $build['nodes'] = array
    (
        '#prefix' => '
',
        '#suffix' => '
',
        'nodes' => node_view_multiple(node_load_multiple($nids)),
        '#weight' => 3
    );
  }
  else if ($subs) // no nids
  {
    $build['subterms'] = array
    (
        '#prefix' => '
',
        '#suffix' => '
',
        'subterms' => array
        (
            '#theme' => 'CSL_custom_admin_taxsubstable',
            '#rows' => $subs
        ),
        '#weight' => 2
    );

    $build['nodes'] = array
    (
        '#prefix' => '
',
        '#suffix' => '
',
        '#markup' => ''.t('Refer to one of the subcategories for articles.').'',
        '#weight' => 3
    );
  }
  else // nothing to see
  {
    $build['nodes'] = array
    (
        '#prefix' => '
',
        '#suffix' => '
',
        '#markup' => ''.t('There is currently no content in this category.').'',
        '#weight' => 3
    );
  }

  $build['pager_top'] = array(
      '#theme' => 'pager',
      '#weight' => -5
  );

  $build['pager_bottom'] = array(
      '#theme' => 'pager',
      '#weight' => 5
  );

  return $build;
}

I know my module names should not start with a capital letter (CSL_), but that should not affect anything here.
Comment: Any chance you're using the taxonomy page provided by the Views module instead of the default one? That would stop the hook from being called
Comment: No, I am using the core taxonomy module, with the exception of tagonomy.pages.inc . I have overridden the menu callback for taxonomy pages, but my override still calls taxonomy_term_view, which calls drupal_alter, which should invoke hook_taxonomy_term_view_alter. I will edit the above post and add my taxonomy.pages.inc and menu callback.
Comment: I just saw some strange bahaviour today... I was moving on to other things on the site, and I installed the [Relation Module](http://drupal.org/project/relation). To make the relation module work, I had to update CTools (from some rc to 7.x-1.0). So I updated all my modules (on the dev site). Suddenly, rsc_search_taxonomy_term_view_alter started outputting the echo and var_dump mentioned above... Everything else still works fine. My problem is solved; I just hope the solution is persistent. This leads to the next question, however: What triggered the change? Was it a bug in CTools rc perhaps?
Comment: And then it disappeared... The old behaviour is back for no apparrent reason. Once again, no hook is ever called in which an entity of type 'taxonomy_term' appears.
Answer: It is a bug / missing feature in Drupal Core!
The taxonomy module does not exactly conform to standards... For example, where an $entity of type 'node' has the properties bundle and content, 'taxonomy_term' doesn't. Also, in core taxonomy, _view hooks are never called and _alter hooks are only called when the taxonomy term has a non-blank description field. There are some patches available in this discussion, but I wrote my own, because I override taxonomy.pages.inc in any case.
I see a lot of modules struggle with this, and it is still an arguing point for Drupal 8. http://drupal.org/search/apachesolr_search/hook_taxonomy_term_view
Thanks for pointing me in the right direction, Clive. I would otherwise not have looked at my taxonomy.pages.inc
An excerpt from my new custom taxonomy.pages.inc follows. I hope this helps someone!
function taxonomy_term_page($term)
{
  // set breadcrumbs
  // add rss feeds
  // count subcategories and nodes

  $build = array();

  $view_mode = 'full';
  $langcode = $GLOBALS['language_content']->language;

  // NB Always call this, even if no description,
  // because other modules need hook_taxonomy_term_view_alter
  // and hook_entity_view_alter
  $term_renderarray = taxonomy_term_view($term, $view_mode, $langcode);

  // Add term heading if the term has a description
  if (!empty($term->description))
  {
    $build['term_heading'] = array(
        '#prefix' => '
',
        '#suffix' => '
',
        'term' => $term_renderarray,
        '#weight' => 1
    );
  }

  // add stuff to $build['subterms'] and $build['nodes']

  $build['pager_top'] = array(
      '#theme' => 'pager',
      '#weight' => -5
  );

  $build['pager_bottom'] = array(
      '#theme' => 'pager',
      '#weight' => 5
  );

  // Keep modules that expect $entity->content to exist happy
  // otherwise this causes errors with implementations such as
  // field_conditional_state_entity_view()
  $term->content = $build;

  // Allow modules to make their own additions to the term.
  module_invoke_all('taxonomy_term_view', $term, $view_mode, $langcode);
  module_invoke_all('entity_view', $term, 'taxonomy_term', $view_mode, $langcode);

  return $build;
}

whew :)
Comment: I think so. It's fixed for Drupal 8, but [the issue](https://www.drupal.org/node/1067120) is still tagged with "Needs backport to D7".
Comment: This still is an issue un Drupal 7.43 right? Because `metatag` module prints global default metatags when viewing a taxonomy page and looking around the code it looks like `metatag_entity_view` from `metatag.module` does not get triggered when viewing a taxonomy page and in there is what it sets metatags....


Dereference of a possibly null reference in child class

Question: Is it possible to tell the compiler that I know a method will return an object with a non-null property, even though that property is marked as nullable?
For example:

var seat = GetOccupiedSeat();
Console.WriteLine(seat.Person.Name);
// 'Person' may be null here.
// Dereference of a possibly null reference.

private Seat GetOccupiedSeat()
{
    // This method will always return a seat with a valid person and a valid name
    return new Seat(new Person("Bob"));
}

public class Seat
{
    public bool IsAvailable => Person == null;
    public Person? Person { get; set; }

    public Seat(Person? person = null)
    {
        Person = person;
    }
}

public class Person
{
    public string Name { get; set; }
    public Person(string name)
    {
        Name = name;
    }
}

Nullable-analysis attributes seem like the most likely things to help here, but none seem suitable to use in this context.
The kind of thing I was hoping to see was something like
[return: NotNull("Seat.Person")]
private Seat GetOccupiedSeat()
{
    return new Seat(new Person("Bob"));
}

Just wondering if anyone knows whether or not this is possible. Thanks.
Comment: Would'nt a simple `if (seat.Person != null)` do?
Comment: @Shazi OP's intention is not to check *if* `seat.Person` is null, but *communicating* that when returned from `GetOccupiedSeat`, it *won't* be null, which is the purpose of the nullability attributes.
Answer: Not that I know of, in this case.
If you're sure that it shouldn't, at that point in your code, you could add:
Debug.Assert(seat.Person is not null);

This will also tell the compiler to assume seat.Person is not null from there on, and remove the compiler warning (in Debug mode).
Comment: @dbc Basically, it seems to work as such, if you make `GetOccupiedSeat` an *instance* method, and call it after object instantiation, then *after* the compiler will assume that the specified member has been set. It seems it doesn't work for static methods.
Comment: Good suggestion on `MemberNotNullWhenAttribute` - I had actually tried this before posting but it's only valid on properties, fields etc. `Debug.Assert` is a good shout also, but if I'm doing that, I might as well put in proper null-checking (which I probably should be doing anyway)
Comment: Doesn't seem like `[MemberNotNull(nameof(Seat.Person))]` does the job, see https://dotnetfiddle.net/PVzb1O.  Or am I misunderstanding your post?
Comment: @devklick Tbh, `Debug.Assert` is usually my go to option, when there is no other way to specify that something *shouldn't* be null at that point 1.) It's not actually null checking (I am not doing anything if it's null after all) 2.) It removes compiler warnings 3.) It makes the intent clear 4.) It's removed in production code.
Comment: @dbc I suppose it will only work if you make `GetOccupiedSeat` a member of `Seat`.
Comment: `[MemberNotNull(nameof(Person))] public static Seat GetOccupiedSeat()` doesn't seem to work if `GetOccupiedSeat()` is a static factory method, see https://dotnetfiddle.net/sKv194.


Webform Module can't send email from user anonymous

Question: I'm using Drupal 7 and Webform Module for create contacts forms (This problem also what I have in Pressflow 6).
Only authenticated users can send mail through the form. However, I need that anonymous users can also use the form.
The error is as follows:
<code>Error sending e-mail from email@example.com to firstname.lastname@example.com: SMTP Error: Data not accepted.
SMTP server error: 5.7.1 Sender domain not allowed.
</code>
How I can make the form send mail with both roles (authenticated and anonymous)?
How I can avoid spam for users anonymous?
Details
Each time you use the contact form (either anonymous or authenticated) to send an email to the person responsible of receiving these emails.
Answer: Anonymous users can absolutely submit webforms. On the Form Settings page under submission access, there are permission checkboxes for each role. You can use Captcha or Mollom to help prevent spam submissions.

The error you're having seems like you have SMTP misconfigured. Are you currently using the SMTP module? If so, run a test to send an email to yourself. If that fails, then it's definitely the problem.
Comment: Yes. I have SMTP module. I run a test to send an email and the result is OK. My SMTP module is correctly configured.
The error only appers with anonymous user.
Comment: In form settings I have the 2 roles checked.
Comment: Try turning off the SMTP module, clearing cache, and then submitting a form as an anonymous user. I'm not saying this will fix it, but I really don't think it's an issue with Webform.


How to build multi row formula using Cloud Data Fusion

Question: I am trying to build a new column that has the running total of a specific column. Are there directives available to do this? Any suggestions on how to accomplish this?
Answer: Multi-row directives are not available in CDF(Wrangler) for this particular scenario. Running total of a specific column can be achieved through window aggregation plugin,
https://cdap.atlassian.net/wiki/spaces/DOCS/pages/760381517/Window+Aggregation+Analytics+Spark
Eg.
new_col:Accumulate(specific_col, 1, false) as aggregate function.
Make sure you define the right partition column if you need rolling sum for a group for rows, if not create a dummy col for considering whole table as one partition. Also make sure table is not huge in the latter case.


Como acessar um campo (valor) de um JSON

Question: estou com um problema, eu tenho este JSON <code>{"cidade":{"nome":"Mag�","uf":"RJ","atualizacao":"2019-07-27","previsao":[{"dia":"2019-07-27","tempo":"pn","maxima":29,"minima":17,"iuv":6},{"dia":"2019-07-28","tempo":"vn","maxima":26,"minima":19,"iuv":6},{"dia":"2019-07-29","tempo":"vn","maxima":26,"minima":16,"iuv":6},{"dia":"2019-07-30","tempo":"pn","maxima":28,"minima":17,"iuv":6}]}}
</code>, eu preciso apenas pegar o primeiro dado da chave "maxima" e o que eu vejo na internet são soluções usando serialização, eu não desejo salvar todos esses dados. Uma outra dúvida, esse JSON está correto em questão de formatação ? Como eu faço pra acessar ele no Android Studio ?
Comment: para validar json eu costumo usar esse site https://jsonlint.com/. No seu json tem um erro do tem uma } a mais, a penúltima está sobrando.
Answer: No seu caso é um array dentro de outro array voce tem que ir "navegando" e pegando os nodos;
Eu coloco dentro de um AsyncTask e no metodo onPostExecute faço o tratamento de leitura.
Eu faço assim para ler valores
<code>JSONObject obj = null;
JSONArray jsonArray = null;

try {
  obj = new JSONObject(jsonResposta);

  jsonArray = (JSONArray) obj.get("ev_resposta");

} catch (JSONException e) {
  log("JSONException "+e.getMessage()+"\n"+this.getClass().getName().toString()+"\n onPostExecute");
}
</code>
Você também pode fazer obj.get(1); para ler, colocando dentro de um for e fazendo a leitura registro a registro.


How to read parquets from aws bucket as pandas in jupyter using pyspark

Question: I would like to read several parquet files from an aws bucket and convert them all into a single pandas dataframe using pyspark.
bucket = s3.Bucket(name='mybucket')
keys =[]
for obj in bucket.objects.all():
    subsrc = obj.Object()
    print(obj.key)
    keys.append(obj.key)

objects = []
for obj in bucket.objects.all():
    key = obj.key
    body = obj.get()['Body'].read()
    objects.append(obj)

count = 0
for file in objects:
    obj = s3.get_object(Bucket="my-bucket", Key=keys[count])
    obj_df = pd.read_parquet(obj["Body"])
    df_list.append(obj_df)
    count+=1

df = pd.concat(df_list)

But I am getting:
AttributeError: 's3.ServiceResource' object has no attribute 'get_object'

I am not sure how the parquet needs to be read correctly, also. Thank you!
Comment: How you are running your code? using lambda, glue, ec2 or local?
Comment: I am working in a jupyter notebook
Answer: You need to write:
s3 = session.client('s3')

instead of:
s3 = session.resource('s3')


tox, cython and fasttext

Question: I am making a project with tox testing and the use of the fasttext Python package. fasttext uses cython. I run into a cython error during the tox environment setup: ImportError: No module named Cython.Build.
I can get it to work if I let tox use sitepackages.
I have created a small test that yields the same error message:
tox.ini:
[tox]
envlist = py27

[testenv:py27]
# sitepackages=True
commands =
    python -m pytest --doctest-modules testinstall.py
deps=
    pytest
    cython
    fasttext

setup.py:
from setuptools import setup
setup(
    setup_requires=['cython'],
    install_requires=['cython', 'fasttext'],
)

The error I get from tox reads:
Collecting pytest
  Using cached pytest-3.0.6-py2.py3-none-any.whl
Collecting cython
  Using cached Cython-0.25.2-cp27-cp27mu-manylinux1_x86_64.whl
Collecting fasttext
  Using cached fasttext-0.8.3.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-8NfmOs/fasttext/setup.py", line 3, in <module>
        from Cython.Build import cythonize
    ImportError: No module named Cython.Build

I see that there there has been an issue with pip's order of installation of subdependencies, see, e.g., https://github.com/h5py/h5py/issues/535 but I am under the impression that this issue is solved.
I see a workaround at setup_requires with Cython? but I don't see my module can use that workaround.
Is this a fasttext issue? Or I am missing some setup?
Comment: https://github.com/tox-dev/tox/issues/42
Answer: I could cheat tox to install cython first using indexserver. The idea is to pretend to install cython from a different indexserver.
For your case it would be:
[tox]
envlist = py27
# trick to enable pre-installation of Cython
indexserver =
    preinstall = https://pypi.python.org/simple

[testenv:py27]
# sitepackages=True
commands =
    python -m pytest --doctest-modules testinstall.py
deps=
    :preinstall: cython
    pytest
    fasttext

Credits:
https://github.com/cggh/scikit-allel/blob/v1.1.10/tox.ini

tox, cython and fasttext


Footer does not stick to the bottom

Question: here's my plunker: http://plnkr.co/edit/HSVBuzzp1IIDjFdbuf4E?p=preview
in short: I cannot find a way to put the footer at the bottom of the page.
I tried to just rely on html:

I tried using bootstrap ui's class:

or

    ...

but nothing...It just doesn't seem working...
Answer: Check this out:
http://plnkr.co/edit/caLGN05wzH3Ls2PcRoTe?p=preview
html, body {
  height: 100%;
}

.modal-footer {
  position: absolute;
  bottom: 0;
  width: 100%;
}

I have added some more css in the head of the page.
Firstly, if you inspect the page, the length of the body is only half of the page. So we have to give it height 100% so that it takes the whole page.
Secondly, to place footer at the bottom of the page, we need to position it as absolute so that it aligns itself to its first positioned parent(in this case none, as we have not specified position to any other element), and then you can specify bottom as 0 so that it sticks to the bottom.
This is the normal way in which we can put footer at the bottom. You can check if bootstrap might have something inbuilt.
Hope this helps.
Comment: Yes it works but now the straight line delimiting the footer itself just stops midway..
Comment: That is the width issue as we have not specified any width. You need to set the width of the modal-footer to 100%. See modified CSS above.
Comment: you saved my day

Footer does not stick to the bottom


Bootstrap navbar does works when in full screen but not when condensed

Question: Currently, my navbar in bootstrap looks fine when in full screen. It looks like this.
However, bootstrap automatically condense the items in to a stack when below a certain width. That is when it looks bad as the Register and Login are on the same line. Looks like this.
I want the navbar to have the Register and Login routes on the right while containing the routes as a stack when condensed.
Current HTML for the nav.

<link href="https://firstname.lastname@example.com/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    Test
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">

    </button>

          Home

          Features

          Pricing

        {% if current_user.is_authenticated %}

                Logout

        {% else%}

                Login

                Register

        {% endif %}

Answer: Navbar should appear as a stack only in mobile view. You can also consider checking if you have added xs- classes properly to get the desired view. I can only see lg classes in your snippet. Pelase try to share the codepen if it still doesn't work.
Answer: Well, I am not sure if I understand the question pretty well,
but from what I understand I would move the login, register, and logoff into a div and apply a media query in the CSS to do what you ask for.

<link href="https://email@example.com/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    Test
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">

</button>

      Home

      Features

      Pricing

    {% if current_user.is_authenticated %}

            Logout

    {% else%}

            Login

            Register

    {% endif %}

Comment: https://codepen.io/Superawdi/pen/GROeeYJ
Comment: That's one way to do it!


Two pitches on one distorted electric guitar versus the two pitches on separate distorted guitars

Question: Just from practical experience it seems the former (two pitches on one distorted electric guitar) is nosier than the later (the two pitches on separate distorted guitars.) I don't know this is actually the case, but it seems so.
An example could be playing a passage in thirds around middle to high range. On one guitar those thirds seem to get very muddy and noisy. But if the parts are on separate guitars it seems less noisy.
Can someone elaborate on what happens in those two scenarios in terms of electric signals and acoustics? If there isn't really a difference, that would be helpful to know too.
Re. two separate guitars: I was thinking two separate speakers or two separate recorded tracks.
Answer: From https://en.m.wikipedia.org/wiki/Intermodulation:

Intermodulation (IM) or intermodulation distortion (IMD) is the amplitude modulation of signals containing two or more different frequencies, caused by nonlinearities or time variance in a system. The intermodulation between frequency components will form additional components at frequencies that are not just at harmonic frequencies (integer multiples) of either, like harmonic distortion, but also at the sum and difference frequencies of the original frequencies and at sums and differences of multiples of those frequencies.

Distortion can be seen as another word for (or the result of) the non-linearity referred to in that quote. When you make a change in the input signal, you get a disproportionate change in the output signal - as you'll know, that's how distortions work, and that's non-linearity.
The sum and difference frequencies generated from the sinusoidal components of a single note are mostly closely related to the original note's components. When you distort two notes separately, that's all you get - each note with its own sums and differences.
However, if you distort a signal containing two notes mixed, you also get a load of other frequencies generated that represent the sums and differences of the components of each other's sums and differences. Depending on the pitches and spectra of the two notes, these may well be less closely related to the frequencies of the components in the original notes, which can cause the sound to be 'rougher'.
This is one of the the main reasons for the use of power chords in rock - a distorted open fifth creates relatively few messy, 'unrelated' frequency components.
Answer: topo Reinstate Monica's answer is much better, but I'll leave this here anyway.
From a signal processing perspective, you're looking at two different scenarios:

(1) : distort(guitar1 + guitar2)
(2) : distort(guitar1) + distort(guitar2)

There are two operations here: "+" and "distort". Out of these two, "+" is a linear, and "distort" is a non-linear operation. Distortion, by definition, cannot be undone: it means destruction of at least some aspects, some information in the original signal. When you play two different simultaneous notes into the same guitar amplifier, it's like scenario 1 - the signals are first summed and then the sum is distorted in the amp. But if you have a separate amp for each note, it's like scenario 2 - each signal is first distorted, and then the two distorted signals are summed linearly in the air.
For the linear summing operation, you can in theory get the original signal back with a reverse operation (subtraction).

Sum(AB) = A + B
A = Sum(AB) - B

But for the nonlinearity, the distortion, there isn't necessarily any way to reverse the operation and get a non-distorted clean signal back. For example, if the distortion is a simple clipper that saws off tops and bottoms of the sine waves - how do you undo? What if the input signal was like that to begin with and not nice round sine waves?
Answer: Overall, your perception is correct.
There are a lot of different kinds of distortion, but at its most basic level, the effect of distortion is created by clipping the signal above a certain amplitude.
When playing into a distortion pedal or amp, decreasing the volume on your guitar will lower the amount of distortion (lower volume = lower amplitude). Similarly, playing only one note will have a lower volume than playing two notes. So, assuming all other factors are equal, the signal coming from the guitar playing two notes will have more volume (and amplitude) than the signal coming from the guitar playing a single note, and it will therefore be more distorted.
I have heard an anecdote about a hard rock band recording guitars for a record (I can't remember what bank), and they recorded the guitar parts in (at least) two layers: one playing power chords (just root-fifth-octave) and another playing just the thirds of the chords. The idea was that this allowed the third to be more clear and less distorted.


Sessão não funciona como esperado em requisição Ajax e PHP

Question: Bom eu tenho uma página principal index.php onde a pessoa executa uma ação e realiza uma requisição POST com Ajax. Neste arquivo principal eu crio uma sessão com alguns dados que eu quero manter mais seguros e não ficarem visíveis e outros para confirmações.
Exemplo: index.php
<?php
// exemplo da sessão
$_SESSION['EXAMPLE'] = [
    'token' => '123456abcExample',// SEMPRE UM NOVO E ÚNICO TOKEN É GERADO AO CARREGAR A PÁGINA index.php
    'other' => 'stringEtc'
];
?>
...

Tem uma segunda página request.php, para onde mando a requisição ajax com retorno JSON. Nessa segunda página eu faço uma verificação se o token da sessão é o mesmo que foi passado para a requisição AJAX, e apenas sendo o mesmo para continuar o código.
Exemplo: request.php
<?php if ($_SESSION['EXAMPLE']['token'] == $_POST['token']) {...}

Porém, na segunda página o token da sessão chega diferente do que era antes. Vamos dizer que na página principal "index.php" o token criado na sessão fosse "12345abcExampleToken", também é passado pro data do AJAX  igualmente, porém na segunda página ele chega como "789452hjkhToken", ou seja outro totalmente diferente, como se a index.php tivesse sido iniciada de novo e criado um novo token.
Bom, eu gostaria de saber por que isto pode está acontecendo. E se houver uma solução mais segura ("se esta maneira que estou utilizando é realmente segura"), por favor poderia me compartilhar?

Nota - Edição: O token é gerado através de uma função de Classe Estática, achei bom informar, em caso de isto ter algo haver!

class Example
{
    public static function getToken(): string
    {
        return 'createToken';
    }
}

e eu uso assim na sessão:
$_SESSION['EXAMPLE'] = [
    'token' => Example::getToken(),
    'other' => 'stringEtc'
];

Comment: no $.post, o 'token' está sendo enviado corretamente mas no request.php na SESSION está incorreto?
Comment: @Everson sim, está chegando diferente. Eu fiz um "echo" pra exibir a session assim que criada e vejo também o do $.post, e claro da request.php também. Mas nela chega diferente
Comment: Uma das formas de tentar perceber onde está o problema é verificar o post enviado através do inspecionar no separador de rede, olhando para os dados do post. Pode depois fazer um echo no php mal o token é gerado, para que possa comparar os dois
Comment: @Isac Como eu posso fazer isso? "inspecionar no separador de rede"
Comment: Abra o inspecionar fazendo `botão direito` seguido de `Inspecionar`. Regra geral isso corresponde ao `F12` na maior parte dos navegadores. Nessa janela de inspeção o separador de Rede(Network) costuma ser o 3º no Chrome, e apresenta todos os pedidos que foram feitos e mostra os dados enviados/recebidos. Basta clicar em qualquer um para ver a informação especifica desse pedido.
Comment: Eu fiz o teste, e troquei o "token" por "data/hora" e o do "request.php" fica com 2, 3 segundos a mais. Exemplo: `index.php` **2017-08-18 23:22:35** --- `request.php` **2017-08-18 23:22:37**. Isso acontece independente se eu fiz a requisição ou não, pois eu esperei alguns segundos pra fazer a requisição e ver se era nessa hora, mas continuou 2 á 3 segundos de diferença
Comment: Seria melhor colocar o código certo pra analise. Porque para mim isso parece que como você estivesse chamando novamente `getToken()` , talvez seja por usar `include`
Comment: @WéllingthonM.deSouza adicionei o código, e realmente eu uso include/require, tem como eu resolver isso?
Comment: Algumas coisas estão sem comentários, pois estou adicionando agora
Answer: Vamos exemplificar, que esse código seria sua index.php
session_start();
$_SESSION['TOKEN'] = md5(time()); // Gera o Token e salva na sessão
echo "O TOKEN É: " . $_SESSION['TOKEN']; // imprime a Sessão Token na tela

Aqui seria sua request.php, se ela estiver incluindo a
  index.php automaticamente vai gerar outra Token e sobrescrever a $_SESSION['TOKEN']

include "index.php";
if ($_SESSION['TOKEN'] == $_POST['TOKEN']) {
    // isso daria errado, porque ? porque fez o include da index.php
}

Minha recomendação, é você criar um novo arquivo, exemplo: resquest2.php e realizar apenas a verificação dos dados
if ($_SESSION['TOKEN'] == $_POST['TOKEN']) {
    // isso daria certo, porque ? porque não fez o include da index.php
}

Abservação:

Se você chama o método getToken() que esta na classe Example
  em sua index.php você não pode chamar esse método novamente em sua
  request.php, pois ele ira sobrescrever o TOKEN anterior.

Exemplo:
Example::getToken(); // Estaria sobrescrevendo a $_SESSION['TOKEN']
if ($_SESSION['TOKEN'] == $_POST['TOKEN']) {
    // isso daria errado.
}

Comment: Realmente faz sentido, mas eu não estou incluindo a `index.php` =/ os includes que estou fazendo no começo tanto da `index.php`, quanto da `request.php`, são de 2 arquivos nos quais eu inicio algumas coisas, como o `session_start`, conexão com banco de dados e a chamada com `autoload` do composer pros namespaces, no qual incluem o Example::getToken(). Então são esses que podem estar fazendo isso? pois eu faço include dele na index.php e na request.php tb, como eu poderia resolver?
Comment: Você só não pode executar o metodo `getToken()` na **request.php**, porque ele cria outra Hash e sobrescreve a anterior.
Comment: Vou colocar na resposta para poder ficar mais clara.
Comment: Bom, eu fiz um teste ao invés de por `getToken`, eu usei o `rand()` do php, na index ele me gerou `28077`, já na request `19593`. Eu uso o `session_start()` com include em ambas, mas a sessão **req_token** é dado valor apenas na index, tem alguma coisa em mente?
Comment: Este é o header da requisição, se de algo servir :) https://prnt.sc/gabw9e
Comment: Vamos conversar no [chat](https://chat.stackexchange.com/rooms/64093/sessao-nao-funciona-como-esperado-em-requisicao-ajax-e-php)
Comment: Finalmente descobri, muito obrigado pela ajuda :D
Answer: Descobri o que estava causando isto! Eu não sei porque isto acontece, mas era um elemento HTML o qual tinha o atributo style com o background vazio da seguinte maneira:

Talvez seja por ele estar chamando novamente a página


Make text in image thinner for OCR

Question: I'm making an automated text recognition script with Python on Ubuntu.
I'm using Gocr and the recognition render is too low.
Exemple:

Output: <code>_O4_4E34E_4_O4_</code>
I suppose that the type in the image is too bold, so I'm asking if there is a way to make it thinner using an python library or a linux command.
Answer: You probably will need to apply a morphological operation like "erosion" on your image, e.g by using OpenCV. This will make structures thinner. To the cost of the quality, though.
Look here: https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html
Comment: You're welcome! Did it improve the recognition rate?

Make text in image thinner for OCR


reload current state with different params using angular2 router

Question: here's an example of my router:
{
    path: ':lang',
    component: MainComponent
    children: [
      {path: 'page1', component, children},
      {path: 'page2', component, children},
      ...
    ]
  },

examples of possible variants in URL are:

/en/page1
/es/page1/child

As you can see, the very first parameter is a lang which means page's language and after that it's just a path to children pages.
I need to create a link which will reload current state but with different param (language in my case), like this:
/en/path/to/child/page -> /es/path/to/child/page
and the most difficult part for me is this:
I don't want to hardcode /path/to/child/page in the [routherLink], it should be dynamic. Maybe taken from ActivatedRoute but I haven't found a way how to do it yet
it should be a link, like this Spanish, not a method in controller, like: this.router.navigate(...);
Answer: If all your urls are build using same naming convention - the first url part is your language - you can use next solution:
Your link will looks like:
Documents

And getContentUrl can be part of your resolver service which takes current url and trim only data-related path (skip first part)
constructor(private route: Router) {
}

getContentUrl() {
    var urlParts = this.route.url.split('/');

    urlParts.shift();
    return urlParts.join('/');
}

Answer: Like this
this.path = '/path/to/child/page';

To make the other part is dynamic
{{lang}}

In the route you specify path
/:lang/path/to/child/page
Comment: that's the point, I need everything after `/:lang/` to be dynamic. Sorry I didn't mention it


Conversão UnixTime para outro formato

Question: Tenho o seguinte código PHP:
1 $timestamp = $stuMessage->unixTime;
2 fwrite($fh, "timestamp: ".$timestamp."\r\n");
3 $data = gmdate("Y-m-d", $timestamp);
4 $data2 = gmdate("Y-m-d", 1541421652);
5 fwrite($fh, $data. "\r\n");
6 fwrite($fh, $data2. "\r\n");

Na linha 4 eu tenho o retorno normal da data passando aquele valor do Unix que é o mesmo valor que está sendo atribuído à variável $timestamp.
Porém na linha 3 eu recebo um retorno em branco, sem nada.
Comment: Como é gerado esse valor unixTime, pode ser que ele esteja chegando como uma string e o parâmetro $timestamp não esteja sendo aceito pela função gmdate().
Answer: O valor unixTime está chegando como string, e o parâmetro $timestamp não esta sendo aceito pela função gmdate(). Converta para número e passe como parâmetro para a função.

Conversão UnixTime para outro formato


If we know $x+y+z=1$, $x^2+y^2+z^2=2$, and $x^3+y^3+z^3=3$, how to find $x^4+y^4+z^4$?

Question: Let $x$, $y$, and $z$ be such that
$$\begin{align*}
x+y+z&=1\\
 x^2+y^2+z^2&=2 \\
 x^3+y^3+z^3&=3
\end{align*}$$
Then $x^4+y^4+z^4=?$
Comment: are you sure $x,y,z$ are integer numbers?
Answer: Hint:
let $a_{n}=x^n+y^n+z^n$
then we have
$$a_{n+2}=(x+y+z)a_{n+1}-(xy+yz+xz)a_{n}+xyza_{n-1}$$
it is easy to find
$$xy+yz+xz=\dfrac{1}{2}[(x+y+z)^2-(x^2+y^2+z^2)]=-\dfrac{1}{2}$$
$$x^3+y^3+z^3-3xyz=(x+y+z)^3-3(xy+yz+xz)(x+y+z)$$
then we have
$$xyz=\dfrac{1}{6}$$
so
$$a_{n+2}=a_{n+1}+\dfrac{1}{2}a_{n}+\dfrac{1}{6}a_{n-1}$$
so
$$x^4+y^4+z^4=a_{4}=a_{3}+\dfrac{1}{2}a_{2}+\dfrac{1}{6}a_{1}=\dfrac{25}{6}$$
Answer: $x^4+y^4+z^4=\left(x^2+y^2+z^2\right)^2-2\left(x^2y^2+y^2z^2+z^2x^2\right)\tag{1}$
$\left(x^2y^2+y^2z^2+z^2x^2\right)  =(xy+yz+zx)^2-2xyz(x+y+z)\tag{2}$
Now note that
$$\color{red}{\boxed{(xy+yz+zx)=\dfrac{1}{2}\left((x+y+z)^2-\left(x^2+y^2+z^2\right)\right)}}$$
from which you get the value of $xy+yz+zx$. Also, note that,
$$\color{blue}{\boxed{x^3+y^3+z^3-3xyz=(x+y+z)\left(x^2+y^2+z^2-xy-yz-zx\right)}}$$
from which you get the value of $xyz$.
Can you take it from here?
Answer: We can use $$(x+y+z)^2=2\sum xy+(x^2+y^2+z^2)\ \ \ \ (1)$$
$$\sum x^3-3xyz=(x+y+z)(\sum x^2-\sum yz)$$
$$\iff\sum x^3-3xyz=(x+y+z)\{(x+y+z)^2-3\sum xy\}\ \ \ \ (2)$$
to find $xyz=c$(say)$,\sum xy=b$(say)
Then $x,y,z$ are the roots of $t^3-(1)t^2+bt-c=0$
$\implies t^4=t^3-bt^2+ct$
$\implies\sum x^4=\sum x^3-b\sum x^2+c\sum x$
Answer: A key to this kind of question is a formed by Newton's identities, which relate the power sums $p_k=\sum_{i=1}^nx^i$ for different values of $k$ to the elementary symmetric polynomials $e_k$; one can define $e_k$ as the coefficient of $X^k$ in the expansion of $\prod_{i=1}^n(1+x_iX)$. The relations are most easily stated recursively as
$$
  ke_k=\sum_{i=1}^k(-1)^{i-1}p_ie_{k-i}
$$
(note that the final term involves $e_0=1$). This makes converting between values $p_1,p_2,\ldots,p_k$ and values $e_1,e_2,\ldots,e_k$ easy.
The problem actually gives $p_k=k$ for $k=1,2,3$, and asks for $p_4$, which normally Newton's identities cannot do. However the additional fact that makes this doable is that one has $n=3$ here: with only three values $x_i$, we know that $e_k=0$ for all $k>3$, in particular for $k=4$.
So first solve $e_1,e_2,e_3$ from
$$ \begin{align} e_1&=p_1e_0=1 \\ 2e_2&=p_1e_1-p_2e_0=e_1-2 \\
 3e_3&=p_1e_2-p_2e_1+p_3e_0=e_2+2e_1+3
  \end{align}
$$
giving $(e_1,e_2,e_3)=(1,-\frac12,\frac16)$, and then use $e_4=0$ in the reversed identity
$$
  p_4=e_1p_3-e_2p_2+e_3p_4-4e_4=1\times3+\frac12\times2+\frac16\times1-0=\frac{25}6.
$$
Answer: An additional solution is using Groebner bases.  For this problem, consider the ring $R=\mathbb{Q}[x,y,z]$ and the ideal
$$
I=\langle x+y+z-1,x^2+y^2+z^2-2,x^3+y^3+z^3-3\rangle.
$$
Using a computer algebra system we can find a Groebner basis (with the grRevLex order) as
$$
\{x+y+z-1,2y^2+2yz+2z^2-2y-2z-1,6z^3-6z^2-3z-1\}
$$
Then, computing the remainder of $x^4+y^4+z^4$ under division by this basis gives
$$
\frac{25}{6},
$$
as others have found through more elementary means.


Create a Display Suite Custom Block Field Programatically

Question: Can anyone provide sample code for a custom module and/or provide a reference for hook_ds_fields_info that utilizes 'field_type' => DS_FIELD_TYPE_BLOCK, that will actually render in admin/structure/ds/fields.  I'm working in 7.2-2.14 of display suite.
The API documentation appears to be incomplete as listed here:
http://www.drupalcontrib.org/api/drupal/contributions%21ds%21ds.api.php/function/hook_ds_fields_info/7.
I'm thinking I may be missing some sort of related render function for the block to appear (https://ohthehugemanatee.org/blog/2014/04/02/how-to-create-a-custom-display-suite-field/) however even using this example barebones in a standalone module I don't necessarily get my custom block to show.  I am referencing a custom block initially built out in a custom module.
<code>function module_ds_fields_info($entity_type) {
  $fields = array();

  $fields['title'] = array(
    'title' => t('My Custom Module'),
    'field_type' => DS_FIELD_TYPE_BLOCK,
    'ui_limit' => array('article|*', 'audience_page|*', 'overview_page|*', 'page|*', 'product|*', 'product_overview|*'),
    'properties' => array(
      'block' => 'my_custom_module',
      'block_render' => DS_BLOCK_CONTENT,
    ),
  );

  return array('node' => $fields);

}
</code>
Answer: As already reported in this DS issue, you need to alter DS_BLOCK_CONTENT to DS_BLOCK_TEMPLATE.
We had a similar issue where from a certain point in time, the block was no longer rendered.
That point in time was a security update on the DS module, we applied somewhere a month ago :
https://www.drupal.org/sa-contrib-2018-019


How to use existing On-Demand instance in spot fleet?

Question: I'm trying to reduce my expenses and want to start using AWS's spot pricing service. I'm completely new to it, but as I understand I can have instances running for certain amounts of time based on the price that will eventually stop running based on certain conditions. That's fine, I'm also aware you can have spot fleets, and in these fleets you can have an On-Demand instance for when the spot instance is interrupted.
I currently have a an On-Demand instance that hosts an ElasticBeanStalk application (it's an API), is there a way to use this instance inside the spot fleet so that when there's an available spot-instance it's servicing my EBS application then when the spot-instance is interrupted it just goes back to using my current On-Demand instance until another spot-instance is available?
Comment: Elastic Beanstalk supports spot instances, but I'm not aware of a way to make this part of Spot Fleet.
Answer: Sadly, spot fleets don't work like this. If your spot instance gets terminated, no on-demand replacement is going to be created for you automatically. If it worked like this, everyone would be using spot instances in my view.
The on-demand portion of your spot fleet is separate from spot portion. This way your application will always run at minimum capacity (without spot). When spot is available, your spot instances will run along side your on-demand. This way you will have more computational power for cheap, which is very beneficial for any heavy processing application (e.g. batch image processing).
Details of how spot fleet and spot instances work are in  How Spot Fleet works and How Spot Instances work docs.
Nevertheless, if you would like to have such replacement provisioned you would have to develop a custom solution for that.
Comment: @CharlesSmith No problem. And yes, reserved instances would be better if you know you need to run your instance for at least 1 year continuously.
Comment: Oh I see, I was under the impression it could be used to replace a spot instance, if that were the case then yes you're right everyone would be doing that. I'm thinking then it would be better to use the reserved instances. Works out to be roughly the same. Thank you so much for your help!
Comment: Either that or change my application to use only spot-instances and start a new instance when the old one is interrupted? Not sure it's worth the trouble of setting up
Answer: There's a third-party solution called Spot.io that not only replaces the spot instance for an on-demand instance in a scenario like the one you describe but it has an algorithm that anticipates the interruption event and stands up an On-demand instance and has it ready before the interruption occurs.


How to identify rows that contain duplicates in multiple columns and specific values in others

Question: I have a dataframe that contains numerous columns and rows where I want to identify rows that are duplicates in multiple columns and in another column one duplicate contains a specific value and the other duplicate contains another specific value.
Example
ID    FDAT     LACT    DATE      EVENT
1     1/1/2022  1    31/1/2022   FRESH
1     1/1/2022  1    15/2/2022   LUT
1     1/1/2022  1    15/3/2022   BRED
1     1/1/2022  1    15/3/2022   OS
1     1/1/2022  1    15/3/2022   PREG
1     1/1/2022  1    30/3/2022   OS
1     1/1/2022  1    30/3/2022   PREG

I can check for duplicates and a specific value with the following code
df.loc[(df.duplicated(['ID', 'LACT', 'FDAT', 'DATE'])) & (df['EVENT']=='PREG')]

The problem is that I only want to include rows where one of the duplicate values on the same day is "BRED".   In the example above this would be true on the 15th of March and false on the 30th of March.  I would like to delete the "PREG" row on the 15th of March
I am looking for a way to make the statement above conditional of EVENT = 'PREG' and other EVENT from duplicate is 'BRED'
The result I am looking to achieve is
ID    FDAT     LACT    DATE      EVENT
1     1/1/2022  1    31/1/2022   FRESH
1     1/1/2022  1    15/2/2022   LUT
1     1/1/2022  1    15/3/2022   BRED
1     1/1/2022  1    15/3/2022   OS
1     1/1/2022  1    30/3/2022   OS
1     1/1/2022  1    30/3/2022   PREG

Comment: what's the final expected result?
Answer: In your sophisticated case you need to group by columns 'ID', 'LACT', 'FDAT', 'DATE' and apply additional filters by a certain events:
events = {'BRED', 'PREG'}
df.groupby(['ID', 'LACT', 'FDAT', 'DATE'], sort=False).apply(
    lambda x: x[x['EVENT'].ne('PREG')] if set(x['EVENT']).issuperset(events)
    else x).reset_index(drop=True)

   ID      FDAT  LACT       DATE  EVENT
0   1  1/1/2022     1  31/1/2022  FRESH
1   1  1/1/2022     1  15/2/2022    LUT
2   1  1/1/2022     1  15/3/2022   BRED
3   1  1/1/2022     1  15/3/2022     OS
4   1  1/1/2022     1  30/3/2022     OS
5   1  1/1/2022     1  30/3/2022   PREG

Comment: Roman, thank you for the code, it is a large dataset.  I am running it now and will check how it goes when done.   I understand the groupby.  Could you explain what the sort=False does?  I looked up .ne that checks for inequality so if 'PREG' in EVENT then False.  Re set(x['EVENT'].issuperset(events) , I take it that this is checking EVENT is in events   I am wondering what else x does (leaves as "True"?).  .reset_index(drop=True resets index?   Thank you  John
Comment: @JohnH, with `sort=False` Groupby preserves the initial order of rows within each group, `else x` - leaves a group as is if it does not contain events `{'BRED', 'PREG'}`
Comment: When I ran the code it did not remove the rows as expected.  I will edit above with a sample of data and the code I used.
Comment: My bad! I did not save the output to a new dataframe. When I saved as a new datafrae all good.  Thank you.


Errors from a .NET 3.5 project show up as .NET 2.x

Question: While my ASP.NET project uses version 3.5 of the framework, the event log registers errors as coming from version 2.X of the framework such as this:
<code>Event Type:         Error
Event Source:       ASP.NET 2.0.50727.0
Event Category:     None
Event ID:           1334
Date:               2009-05-19
Time:               17:04:29
User:               N/A
Computer:           XXXXXXXXXX

Description:
An unhandled exception occurred and the process was terminated.

Application ID: /LM/W3SVC/234989001/Root
Process ID: 8912
Exception: System.Web.HttpException
Message: The remote host closed the connection. The error code is 0x80072746.
</code>
Comment: I've actually wondered this too, thanks for the question.
Comment: It was a question from a co-worker that I didn't see posted anywhere on here so I thought I'd add it.  Received a good answer before I could answer it myself :-)
Answer: .NET 2.0, 3.0 and 3.5 are all based on the 2.0 CLR. The version you see is the CLR version, not the Framework version, so everything is fine.
Answer: ASP.NET has a different version number to the underlying .Net framework.
ASP.NET 2.0.50727.0 can runs with .Net 3.5 - your error message is reporting the correct version.
Answer: For more information, I found this post by Scott Hanselman informative:
How to set an IIS Application or AppPool to use ASP.NET 3.5 rather than 2.0


General linear mixed model in R which will fit quasi family

Question: I am trying to run a GLMM with a quasibinomial family (my data is 0 inflated and I have a negative min x value), but am receiving this error message as quasi families cannot be used in glmer:
<code>Error in lme4::glFormula(formula = slope_prop ~ temp * gly * day + (1 |  :
  "quasi" families cannot be used in glmer
</code>
Is there any way of running a GLMM which will use quasi families? Or am I doing the wrong thing by trying to use a quasibonomial family in the first place?
Comment: This requires statistical expertise to answer and should IMHO be reopened.
Answer: You could instead use a zero-inflated binomial mixed model. This can be fitted using the GLMMadaptive package and by defining a user-specified family object; see here for more information.
For an example, check this:
<code># install the development version from GitHub
devtools::install_github("drizopoulos/GLMMadaptive")
# Load the package
library("GLMMadaptive")

# Simulate some zero-inflated Binomial data
set.seed(1234)
n <- 100 # number of subjects
K <- 8 # number of measurements per subject
t_max <- 15 # maximum follow-up time

# we constuct a data frame with the design:
# everyone has a baseline measurment, and then measurements at random follow-up times
DF <- data.frame(id = rep(seq_len(n), each = K),
                 time = c(replicate(n, c(0, sort(runif(K - 1, 0, t_max))))),
                 sex = rep(gl(2, n/2, labels = c("male", "female")), each = K))

# design matrices for the fixed and random effects
X <- model.matrix(~ sex * time, data = DF)
Z <- model.matrix(~ time, data = DF)
# design matrices for the fixed and random effects zero-part
X_zi <- model.matrix(~ sex, data = DF)
Z_zi <- model.matrix(~ 1, data = DF)

betas <- c(-2.13, -0.25, 0.24, -0.05) # fixed effects coefficients
gammas <- c(-1.5, 0.5) # fixed-effects zero-part
D11 <- 0.48 # variance of random intercepts
D22 <- 0.1 # variance of random slopes
D_zi <- 0.4 # variance of random intercepts zero-part

# we simulate random effects
b <- cbind(rnorm(n, sd = sqrt(D11)), rnorm(n, sd = sqrt(D22)), rnorm(n, sd = sqrt(D_zi)))
# linear predictor
eta_y <- drop(X %*% betas + rowSums(Z * b[DF$id, 1:2]))
# we simulate binomial longitudinal data with 10 trials
DF$y <- rbinom(n * K, 10, plogis(eta_y))
# linear predictor zero-part
eta.zi <- drop(X_zi %*% gammas + rowSums(Z_zi * b[DF$id, 3]))
# set the extra zeros
DF$y[as.logical(rbinom(n * K, 1, plogis(eta.zi)))] <- 0

# Define the zero-inflated family object
zi.binomial <- function () {
    stats <- make.link("logit")
    log_dens <- function (y, eta, mu_fun, phis, eta_zi) {
        # the log density function
        N <- y[, 1] + y[, 2]
        y <- y[, 1]
        # Binomial part
        mu <- mu_fun(eta)
        out <- as.matrix(dbinom(y, N, mu, log = TRUE))
        # ZI part
        ind_y0 <- y == 0
        ind_y1 <- y > 0
        pis <- as.matrix(plogis(eta_zi))
        # combined
        out[ind_y0, ] <- log(pis[ind_y0, ] + (1 - pis[ind_y0, ]) * exp(out[ind_y0, ]))
        out[ind_y1, ] <- log(1 - pis[ind_y1, ]) + out[ind_y1, ]
        attr(out, "mu_y") <- mu
        out
    }
    structure(list(family = "zero inflated binomial", link = stats$name, linkfun = stats$linkfun,
                   linkinv = stats$linkinv, log_dens = log_dens), class = "family")
}

# Fit the model
fm <- mixed_model(cbind(y, 10 - y) ~ sex * time, random = ~ 1 | id, data = DF,
                  family = zi.binomial(), zi_fixed = ~ sex,
                  initial_values = list(betas = binomial()))

summary(fm)
</code>


How to stop Auth user from visiting own page with non auth user privileges in laravel?

Question: My laravel application is a social media site. Here's the route for visiting another laravel user's profile
Route::get('/dashboard/{id}', [
'uses' => 'UserController@getProfile',
'as' => 'profile.index',
'middleware' => 'auth'
]);

It works just fine. However, I've discovered a bug that when I input the Auth user's ID into the route, I get taken to the same page where I can then add myself as a friend, I do not want this to happen. I would rather get taken back to the home screen if I'm visiting my own profile.
Here's the controller:
 public function getProfile($id)
{

    if(Auth::user() === $id)
        redirect('dashboard');

    $user = User::where('id', $id)->first();

    $posts = Post::where("dash_id", "=", $user->id)->latest()->paginate(3);
    $photos = Photo::paginate(6);
    return view('profile.index',compact('user','posts', 'photos'));

}

I've tried to get it to redirect to 'dashboard' instead of 'profile.index' if it's the Auth user's page instead of pulling up just like a regular non-auth profile, but can't seem to get it to work. Any ideas on how to fix this small bug?
Answer: You try to compare the current user object to the request id, try this code:
public function getProfile($id)
{
    if(Auth::id() === $id) {
        redirect('dashboard');
    }

    $user = User::where('id', $id)->first();

    $posts = Post::where("dash_id", "=", $user->id)->latest()->paginate(3);
    $photos = Photo::paginate(6)

    return view('profile.index',compact('user','posts', 'photos'));
}

Answer: You get user instance by Auth::user() not only the user ID. You are comparing instance with the numeric value. It will not work. You have to use Auth::id() or Auth::user()->id in order to get ID of the logged in user. The following code will work in your case.
public function getProfile($id)
{

    if(Auth::id() == $id)
    {
        redirect('dashboard');
    }
    else
    {
        $user = User::where('id', $id)->first();
        $posts = Post::where("dash_id", "=", $user->id)->latest()->paginate(3);
        $photos = Photo::paginate(6);
        return view('profile.index',compact('user','posts', 'photos'));
    }
}

Let me know if it helps!
Comment: It stopped pulling the Auth profile as a regular profile, but now instead of redirecting I simply get a blank page.
Comment: I got it now, just had to add return into the redirect :) thank you!
Comment: Yep!! This is the answer you need.


Is it an offside in this case?

Question: When two players are facing the goalkeeper alone. and the first player passes the ball to the second and he scores.
notes:

The moment when the ball was passed to the first player none of the two players are offside.
when the first player passes the ball to the second player, the later is closer to the goal line than the ball.
Comment: Possible duplicate of [How is offside determined in soccer?](https://sports.stackexchange.com/questions/3/how-is-offside-determined-in-soccer)
Comment: This is a particular case, I understand how offside is determined in general.
Comment: It would appear not, if the subsequent question is necessary.
Answer: Yes, it's offside (unless there's an additional defender closer to the goal line, let's say in the vicinity of one of the corners. In such a case, the two players can still be said to be facing the goalkeeper alone, but it's rare and I assume you don't mean a situation like this).

when the first player passes the ball to the second player, the later is closer to the goal line than the ball.

That's probably the key here; if the second player would be 'behind' the ball, (s)he wouldn't be offside. Now it's definitely a case of offside.
Comment: I always thought that the position of the player and the last two defenders (including the goalkeeper) are the only determinants of the offside. The fact that the position of the player and the ball matter is the new thing for me here. thanks for the clarification.

Is it an offside in this case?


Create a file and write struct with data member and the file created is empty

Question: I create a program that prompts the user to choose an option, like below:

create a file and name the file.
add component in the file
display the item in the file
exit program

So my problem is the file is created and exists in my directory, but it is empty, so where did I miss something? plus can you check whether the 2 and 3 option program are OK?
Here my program:
#include <stdio.h>
#include <stdlib.h>

int main() {
    int num, name;

start:

    printf("\n1. Create a file.");
    printf("\n2. Add a component to the list.");
    printf("\n3. Display the current list of component.");
    printf("\n4. Exit program.");
    printf("\n\nChoose either these four menu = ");
    scanf("%d", &num);
    fflush(stdin);

    switch (num) {
      case 1:
        printf("\n\nPlease enter file name: ");
        scanf("%d", &name);
        FILE *pf = NULL;
        char username[250];
        char userfile[255];

        printf("username: ");
        scanf("%s", username);
        sprintf(userfile, "%s.txt", username);
        fflush(stdin);
        goto start;
        break;

      case 2:
        pf = fopen(userfile, "w");
        if (!pf) {
            fprintf(stderr, "File opening failed!\n");
            return EXIT_FAILURE;
        }
        struct date {
            int day;
            int month;
            int year;
        };
        struct details {
            char name[20];
            int price;
            int code;
            int qty;
            struct date mfg;
        };
        struct details item[50];
        int n, i;

        printf("Enter number of items:");
        scanf("%d", &n);
        fflush(stdin);
        for (i = 0; i < n; i++) {
            fflush(stdin);
            printf("Item name: \n");
            scanf("%s", item[i].name);
            fflush(stdin);
            printf("Item code: \n");
            scanf("%d", &item[i].code);
            fflush(stdin);
            printf("Quantity: \n");
            scanf("%d", &item[i].qty);
            fflush(stdin);
            printf("price: \n");
            scanf("%d",  &item[i].price);
            fflush(stdin);
            printf("Manufacturing date(dd-mm-yyyy): \n");
            scanf("%d-%d-%d", &item[i].mfg.day,
                  &item[i].mfg.month, &item[i].mfg.year);
        }
        fclose(pf);
        goto start;
        break;

      case 3:
        pf = fopen(userfile, "r");
        if (!userfile) {
            fprintf(stderr, "File opening failed!\n");
            return EXIT_FAILURE;
        }
        {
            printf("             *****  INVENTORY ***** \n");
            printf("------------------------------------------------------------------\n");
            printf("S.N.|    NAME           |   CODE   |  QUANTITY |  PRICE | MFG.DATE \n");
            printf("-------------------------------------------------------- ---------\n");
            for (i = 0; i < n; i++)
                fprintf("%d     %-15s        %-d          %-5d     %-5d %d/%d/%d \n",
                        i + 1, item[i].name, item[i].code, item[i].qty,
                        item[i].price, item[i].mfg.day, item[i].mfg.month,
                        item[i].mfg.year);
            printf("------------------------------------------------------------------\n");
        }
        fclose(pf);
        goto start;
        break;

      case 4:
        printf("Exit Program, Thank You, Sayonara");
        break;
    }
    return 0;
}

Comment: `if (!userfile) {` -> `if (!pf) {` It also looks like you open the file for reading and then try to write to it. This is really a spaghetti goto mess.
Comment: regarding: `fflush(stdin);`  this is specifically called out in the C standard as undefined behavior
Comment: regarding: `scanf("%s", item[i].name);`  with `%s`, always include a MAX WIDTH modifier that is 1 less than the length of the input field.  1 less because `%s` always appends a NUL byte to the input
Answer: There are multiple problems in your code:

the test if (!userfile) is incorrect: you should test if (!pf) instead
you open the file for reading, you should open it for writing with "w", or possibly for appending with "a".
you never write to the file. You should use fprintf(pf, ...) instead of printf.
the arrays userfile, item and variables i and n are local inside the switch statement: their contents go out of scope when you go to the start label outside de switch statement. Move all these definitions outside the switch statement.
fflush(stdin); has undefined behavior. You can consume (flush) the rest of the input line with: int c; while ((c = getchar()) != EOF && c != '\n') continue; You can define a function flush() that does it.
Comment: ok thanks for the info, may i know , i should change the fprint(pf,....) at where? i assume at the  statement case 2 right?
Comment: @MuhammadRahimy: it unclear when you are supposed to write to the file. Option 1 gets the username, 2 creates the file and read items, 3 prints the items from the local array and 4 exits.
Comment: I use the ffprint() , they now write into the text file. But the problem is, it not show in the screen while put the input . So i can double the output for the screen and write to the text file? Example fprint() and below printf()?


C# object to array

Question: Using reflection I have an object which I need to cast into an iterable list of items (type unknown, will be object). Using the Watch window I can see my object is an array of some type as it tells me the number of elements and I can explode the tree view to see the elements themselves.
Firstly, I need to check that the object passed is some kind of array (might be List, might be object[], etc). Then I need to iterate through that array. However, I can't do the type conversion.
Here's how I'm using it (abbreviated):
    private static void Example(object instance, PropertyInfo propInfo)
    {
        object anArray = propInfo.GetValue(instance, null);
        ArrayList myList = anArray as ArrayList;
        foreach (object element in myList)
        {
            // etc
        }
    }

I've tried various different casts. The above doesn't raise an exception but mylist is null when anArray actually exists and contains items. The actual instance being saved is a strongly-typed List<> but could take a limited subset of forms if necessary. But the point of the exercise is that this Example() method doesn't know the basic type of the property.
Comment: @Bad Display Name that's now how the *is* keyword works, what you're doing there is attempting to cast from System.Type to System.Collection.IEnumerable, which doesn't work since System.Type doesn't implement that interface. perhaps you meant **typeof(IEnumerable).IsAssignableFrom(instance.GetType())**
Comment: you can find out the type of object from instance.GetType() and you can compare it with desirebale type using `is`, e.g. `if (instance.GetType() is IEnumerable)`
Answer: Casting it to an ArrayList is only going to work if the object actually is an ArrayList. It wont work with a System.Array, or a System.Collections.Generic.List`1 for example.
I think what you actually should do is cast it to IEnumerable, since that is your only requirement to loop over it...
object anArray = propInfo.GetValue(instance, null);
IEnumerable enumerable = anArray as IEnumerable;
if (enumerable != null)
{
    foreach(object element in enumerable)
    {
        // etc...
    }
}

Comment: matt... is that you?! It's Marlon.... anyway, this doesn't work, IEnumerable needs a type parameter (maybe this was possible on an old version of .NET)
Comment: @Cloud: Not correct. There is [`IEnumerable`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.ienumerable?view=net-8.0) and [`IEnumerable`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1?view=net-8.0)
Answer: Try to cast to IEnumerable. This is the most basic interface all enumerables, arrays, lists etc. implement.
IEnumerable myList = anArray as IEnumerable;
if (myList != null)
{
    foreach (object element in myList)
    {
        // ... do something
    }
}
else
{
    // it's not an array, list, ...
}

Comment: No - it needs a type parameter, this doesn't work
Comment: @Cloud: Not correct. There is [`IEnumerable`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.ienumerable?view=net-8.0) and [`IEnumerable`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1?view=net-8.0)
Comment: If you do get the "Using generic type 'IEnumerable' requires 1 type arguments" error, then you need to use the System.Collections namespace, that is where the IEnumerable definition is. IEnumerable lives in System.Collections.Generic
Answer: Simply Try This
 string[] arr = ((IEnumerable)yourOjbect).Cast<object>()
                             .Select(x => x.ToString())
                             .ToArray();

Comment: This is the simplest way if it is primitive type.
Answer: Try this:
    var myList = anArray as IEnumerable;
    if (mylist != null)
    {
        foreach (var element in myList)
        {
            // etc
        }
    }

You might also need to specify the generic type of the IEnumerable, depending on your situation.
Answer: You should be able to cast it to IEnumerable if it is a collection of any sorts (array, list, etc.). Also PropertyInfo contains a PropertyType property which you could use to find out the actual type if you wanted to.
Answer: Just in My case I need to define data type IEnumerable<string>
var myList = anArray as IEnumerable<string>;

C# object to array


Solvability of Neumann problem

Question: Let $D$ be the domain in $\mathbb{R}^2$ such that $x^2 + y^2 <4$ , and $\alpha, \beta$ and $ \gamma $ be real constants. For which values of these three constants there is no solution for:$$\Delta u =0 , (x,y)\in D$$
$$\partial_n u= \alpha x^2 + \beta y + \gamma, (x,y) \in \partial D$$
 How to solve the equation for the values of these three constants which the solution exists?
Thanks.
Answer: In polar coordinates $(x,y) = (r\cos\phi,r\sin\phi)$ the general solution (which is finite at $r=0$) is
$$ u(r,\phi) = A_0 + \sum_{n=1}^\infty r^n \big[A_n\cos(n\phi) + B_n\sin(n\phi)\big] $$
where $D = \{ 0<r < 2\}$. The gradient is
$$ u_r(r,\phi) = \alpha r^2\cos^2\phi + \beta r\sin(\phi) + \gamma = \frac{\alpha r^2}{2}\big[\cos(2\phi) + 1\big] + \beta r \sin(\phi) + \gamma $$
Evaluated at the boundary:
$$ u_r(2,\phi) = 2\alpha\cos(2\phi) + 2\beta \sin(\phi) + (2\alpha + \gamma) $$
You also have, from the general solution
$$ u_r(2,\phi) = \sum_{n=1}^\infty n2^{n-1}\big[A_n\cos(n\phi) + B_n\sin(\phi)\big] $$
Comparing coefficients gives
\begin{cases}
2\alpha + \gamma = 0 \\
B_1 = 2\beta \\
4A_2 = 2\alpha
\end{cases}
The first condition is required for the solution to exist.
Answer: The Neumann problem has a solution iff $\int_{S}{fd\sigma=0}$  , where $S$,  is the circle with center the origin and radius $2$ and $f(x,y)=\alpha x^2 + \beta y + \gamma$ . A parametrization for this circle is $(2\cos{t}, 2\sin{t})$. Now if we take the above mentioned line - integral we get the desired condition: $2a+\gamma=0$.
Edit The proposition above is
Comment: What is $f$? ---
Comment: @Dylan, $f(x,y)=\alpha x^2 + \beta y + \gamma$
Comment: It would help to specify that in your answer. Also mention which theorem this came from. This is a good answer but needs some more context.
Comment: Ok, thanks. See please "Introduction to PDE with Applications", by Zachamanoglou and Toe. It is just a ref in page 259 , line 14. Potential Theory is used here.

Solvability of Neumann problem


CATransform3DRotate after CATransform3DTranslate

Question: I'm trying to make a folding facebook menu like the one you can see here: http://www.mobileinc.co.uk/2012/05/folding-iphone-sidebar-menu/
For now, I'm juste working on the "folding" part. I have 2 layers:
- left layer has an anchor point at {1,.5}
- right layer has an anchor point at {0,.5}
I can make them fold in the middle of my main view without problem like that:
    CGFloat width = self.view.frame.size.width;
    CGFloat destinationWidth = slider.value * width;
CGFloat hypothenuse = self.view.frame.size.width/2.0f;
CGFloat adjacent = destinationWidth / 2.0f;
CGFloat opposite = sqrtf(powf(hypothenuse, 2)-powf(adjacent, 2));

CGFloat marginLeft = (width - destinationWidth) / 2;

CGFloat angle = asinf(opposite/hypothenuse);

CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0f / -4000.0f;

rightSide.layer.transform = CATransform3DRotate(CATransform3DTranslate(transform,0,0,-opposite),-angle, 0, 1, 0);
leftSide.layer.transform = CATransform3DRotate(CATransform3DTranslate(transform,0,0,-opposite),angle, 0, 1, 0);

This works perfectly. The problem comes when I try to translate my layers on the x axis like this:
    CATransform3DTranslate(transform,-marginLeft,0,-opposite)
Layers still fold with the good z translation but it ends with an awkward angle as if the whole view had been rotated.
This is obviously linked to the perspective (when I don't set m34, it "works") but I can't figure how to compensate it...
Thank you in advance!
Cheers,
TeuBeu2
Comment: I understand it has to do with the vanishing point but I can't get to change it. I tried modifying the main view anchor point but it doesn't seem to work...
Comment: What does using the -opposite for the z translation do?
Answer: You're composing 4 transforms, in this order:

Rotate
Translate Z
Translate X
Project

The problem is that you want to do the translate X after the projection, not before. As it is, you're pulling the fold back towards the center a bit with the projection. Try this:
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0f / -4000.0f;

transform = CATransform3DConcat(transform, CATransform3DMakeTranslation(-marginLeft,0,0));

rightSide.layer.transform = ...

Disclaimer: I haven't actually tested this


Using AIC to Measure Clustering Quality, how many k to account for Variance Parameter(s)?

Question: I have written a custom clustering function which takes a vector of initial position estimates of k cluster centres (a 1 dimensional vector). Internally the function then "calibrates" the final clustering result using a minimum distance measure over weighted data, based on this input. Each cluster centre k has its own variance associated with the points it clusters. There is no random initialisation and between cluster distance/disimilarity is irrelevant although, conceptually, the function can be thought of as being similar to kmeans in 1 dimension. The initial position estimate is the model.
I would like to use AIC to select the best of several different possible initial position estimates (models) post calibration, but I am confused as to how I should count the parameters k for the model AIC score. Obviously each given cluster centre counts as 1 parameter, but what about the variance? If I have, e.g. 5 cluster centres, do I count parameters for AIC as 5 plus 1 for a total global variance, or 5 plus 5 for individual variances?
Comment: please search the site "bic clustering". You'll find some useful answers (for bic and aic), including pseudocode (in my answer). You may want also to open "Internal clustering criteria" document from my web page.
Answer: This depends on whether your clustering procedure uses individually different variances from all centres, in which case you need to use the number of individual variances. Note that this is not k-means in 1-d; k-means implicitly assumes that all variances (or in higher d covariance matrices) are the same. If you do it like this (i.e., if you compute the clustering without actually using the different individual variances), you don't even have to count a single variance parameter, because the clustering doesn't rely on it.
That said, k-means-like procedures are not based on statistical mixture models but on models in which the cluster assignment of every observation can be seen as an individual discrete parameter. My experience (and probably also the experience of others; BIC is usually preferred to AIC in clustering and even BIC may choose the number of clusters too high easily) is that AIC doesn't work very well in such a setting. Surely any theoretical justification of AIC will not apply.


How to save .docx file into S3?

Question: I am using python-docx to create document. I want to save this file to AWS S3. Currently what I am doing is, I am creating .docx file and saving it locally using document.save('filename.docx'). Then using s3.meta.client.upload_file('/path/to/file/filename.docx', bucket, 'filename.docx') to save it into S3. Is there any way where I can save .docx file directly into AWS S3 without saving it locally?
Comment: you could always use ```client.upload_fileobj()``` but not entirely sure if you could use it on ```document```
Answer: Yes, you can. As @ewong suggested, here is a sample code snippet:
import boto3
from io import BytesIO
from docx import Document

s3 = boto3.client('s3')
document = Document()
# ...

with BytesIO() as fileobj:
    document.save(fileobj)
    fileobj.seek(0)
    s3.upload_fileobj(fileobj, 'your-bucket-name', 'your/s3/key')

How to save .docx file into S3?


Why does the Opera browser send many "MessageEvent" messages using Nextjs in the console?

Question: I've been using firefox for programming all my life, but recently I decided to try Opera, since I found the browser very interesting, but I'm fed up with this insane spamming of the "MessageEvent" console, does anyone else have this problem? I'm using Nextjs 13
My console screen

I hope someone from the community like me who is experiencing this problem knows how to solve it :C
Answer: Going into Opera Settings and searching cash back and turning it off should fix the issue.


MySql, join tables WHERE IN()

Question: I have two tables and structure here:
CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `membership` varchar(12) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `memberships` (
  `id` int(1) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(30) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `memberships` (`id`, `title`) VALUES
(1,  'Basic'),
(2,  'Bronze'),
(3,  'Gold'),
(4,  'Platinum');

INSERT INTO `users` (`id`, `username`, `membership`) VALUES
(1,  'john', '0'),
(2,  'mike', '1,2'),
(3,  'peter', '3,4'),
(4,  'jane', '3'),
(5,  'rick', '1,2,3');

I need to join both tables and list all users with their memberships using IN function.
This is what I tried, but I'm not able to join tables that way:
SELECT
  *.u
  m.title AS title
FROM
  `users` AS u
  LEFT JOIN `memberships` AS m
    WHERE m.id IN(u.membership)
ORDER BY u.username;

What I would like to have is something like this:
username              membership
=====================================
john                  null
mike                  Basic, Bronze
peter                 Gold, Platinum
jane                  Gold
rick                  Basic, Bronze, Gold

I have created SQL fiddle as well: http://sqlfiddle.com/#!9/39b6e1
Comment: `SELECT u.*` not `*.u`
Answer: How about this?
SELECT
  u.*,
  GROUP_CONCAT(m.title SEPARATOR ', ') as memberships
FROM
  `users` AS u
  LEFT JOIN `memberships` AS m
    ON FIND_IN_SET(m.id, u.membership)
GROUP BY u.username
ORDER BY u.username

Gives you:
id  username    membership  memberships
4   jane        3           Gold
1   john        0           (null)
2   mike        1,2         Bronze, Basic
3   peter       3,4         Gold, Platinum
5   rick        1,2,3       Basic, Bronze, Gold

Updated SQL Fiddle
Comment: Isn't that a complicated way to do what `FIND_IN_SET()` does?
Comment: Thanks @Barmar. I wasn't aware of `FIND_IN_SET()`. Updated the answer to use that.
Comment: @Miro Markaravanes thank you works perfect. I have accepted your answer.

MySql, join tables WHERE IN()


Relação de agregação e atributo das classes

Question: A agregação só ocorre quando uma classe A, que agrega uma classe B, tem um atributo do tipo B?
Pode haver agregação sem, necessariamente, uma classe ter um atributo do tipo da outra?
Por exemplo, se eu instancio um objeto de outra classe em um método da minha classe, mas esse objeto instanciado não é atributo da minha classe, a relação ainda é de composição?
Comment: Aqui há uma boa explicaçãõ e não apenas de agregação https://pt.stackoverflow.com/questions/25619/composi%C3%A7%C3%A3o-e-agrega%C3%A7%C3%A3o-quais-as-diferen%C3%A7as-e-como-usar
Answer: TL;DR
Depende do ponto de vista, mas em geral a agregação num modelo de classes ocorre apenas quando há um atributo.
Estrutura de classes
Em geral, quando falamos em diagramas de classes, estamos nos referindo à estrutura delas. Portanto, a agregação somente ocorreria no caso de A ter um atributo do tipo B.
Não estamos preocupados com detalhes de implementação enquanto estamos modelando ou fazendo um projeto em alto nível do sistema.
Análise estática do código
Por outro lado, se fizermos uma análise estática do código de uma classe pronta para determinar dependências, podemos encontrá-las:

Explícitas ou diretas: em tipos de variáveis e parâmetros, por exemplo. Geralmente vai haver um import para cada referência.
Implícitas ou indiretas: ao usar uma interface sem saber a implementação ou quando há encadeamento de chamadas, tal como em objetoB.getObjetoC().getAtributo(), pois nesse caso dependemos de C sem que haja uma ligação direta com a classe em A.

Análise dinâmica em execução
Indo mais a fundo, podemos ainda fazer uma análise em tempo de execução para determinar as dependências reais que uma classe precisa para executar.
Considera-se aqui que algumas das implementações de interfaces podem ser providas:

Em tempo de execução, por exemplo através de plugins ou por bibliotecas que não estão presentes durante a compilação
Pelo uso de reflexão, instanciando ou acessando classes pelo nome delas
De acordo com a entrada do usuário ou configuração do programa, onde os tipos de instâncias podem variar
Por bibliotecas que geram classes dinamicamente

Considerações
A modelagem estrutural das classes é boa para se pensar no domínio do sistema, mas não garante e nem deve tentar garantir a forma alguma como o sistema executa nem suas dependências em tempo de execução.
A análise estática do código ajuda a determinar o acoplamento e coesão de uma classe, de forma que deve-se sempre tentar diminuir a quantidade de dependências.
Por fim, a análise em tempo de execução é a única que dá total garantia do que realmente está sendo executado, mas também é a mais custosa e complexa. Ainda, ela é dependente do momento, o que significa que o comportamento do sistema varia ao longo do tempo de uso e algumas vezes é bem difícil reproduzir o cenário desejado.


Warning: Unreachable code , using funtion component (Arrow function) in Reactjs 18

Question: I created a function component and return JSX in react 18, but I was getting a strange warning; that "Unreachable code"
const Header = () => {
  return

hello

;

};

export default Header;

I was thinking it happened because of the missing parenthesis "()" but when I added it
const Header = () => {
  return (

hello world!

;
  )

};

export default Header;

And the error I got was
Parsing error: Unexpected token, expected "," (8:10)
')' expected.
Declaration or statement expected.
Answer: That was because I added an unexpected ";" after my JSX When I removed it, It worked
const Header = () => {
  return

hello

};

export default Header;

or
const Header = () => {
  return (

hello world!

  )

};

export default Header;

I only removed the ";" semicolon after the JSX in the return statement
Comment: Is it solves your problem?


Which test to compare the difference between the amount of job categories for men and women

Question: I have a dataset with men and women (independent variable) and want to know if the job spectrum (dependent variable) of the men is broader than the job spectrum of the women. Their jobs are categorized according to a reliable multinominal categorisation system (ISCO). Some men and women have a job in the same category. In some categories there are only men, in some only women. I found that the jobs the men (N=417) have fall in 34 distinct categories whereas the jobs that the women (N=121) have fall in 19 distinct categories. I want to know if this difference is significant. I am not interested in the difference between the categories but in the difference in the amount of categories. I have no covariate or moderator. Which test do I use?
The chi-square seems most promising but this is a comparison between cells, not necessarily between distributions? A multinomial regression falls flat because many cells have a count of zero. Binominal analyses don't make sense because I'd be comparing an independent variable with two cases (male or female) with a range of categories (19 vs 34).
Comment: The [bootstrap](https://en.wikipedia.org/wiki/Bootstrapping_(statistics)) technique might be an option to gain confidence intervals for the male and female group.
Comment: Occupations were divided into 96 possible categories (managers, service workers, machine operators, etc) not all 96 categories were present in my dataset. For some job categories there were both men and women (teachers) for some only men (machine operators). Men were represented in 34 different categories, women in 19 different categories.
Comment: @dipetkov thank you for thinking with me. It was a hypothesis that I had before writing (I preregistered it on OSF!) and I had decided on a chi square but now I have the data I am unsure whether my interpretation is right. Because I know it as a test to find differences between cells while I am looking for a difference in distribution. https://www.bmj.com/about-bmj/resources-readers/publications/statistics-square-one/8-chi-squared-tests seems to indicate I am on the right track after all but I am in doubt
Comment: Can you be more precise about what is "job spectrum"? What does "difference in the amount of categories" mean? Say jobs were completely segregated by gender and half the job categories were filled in by men, the rest by women. That's the same number of categories for both genders; is the "spectrum" different or the same in this case?
Comment: Thank you for adding more detail about what you observe in the data. However, this still doesn't clarify the meaning of job spectrum. It seems you've come upon an aspect of the data that makes men and women employment appear particularly different and now you want give this data-generated hypothesis the "statistically significant" label.
Comment: @dipetkov as for the job spectrum: is 'larger variety in jobs' more clear? In your example above then yes, if it's the same number of job categories for both genders, the job spectrum is similar. I edited my original question to clarify
Comment: @Scriddie could you eloborate on why you think comparing confidence interval is the best way for me to find a difference? (as opposed to the other analyses)
Comment: The null hypothesis of the chi-squared test implies that $p_{woman}(job_i) = p_{man}(job_i)$ for all jobs. It doesn't say anything about the number of empty cells and it will detect the difference in job probabilities in my extreme example. So it is not the test to use for job spectrum.
Comment: @dipetkov Thank you for clarifying with a null hypothesis equation and explaining, I get what you are saying. I hope someone can point me into the right direction as to what is the right analysis is.
Comment: @dipetkov also I appreciate you pointing out that one should not generate hypotheses after the data has been analyzed.
Answer: Context: I have doubts that "number of distinct job categories with at least one person employed" can be meaningfully interpreted as "variety in occupations". Consider for example, that a single woman can increase the "job spectrum" for women by 5% by getting employed in a job without previous reports of female employment.
With that caveat aside, you could compare the number of occupations that employ men and women with the bootstrap as suggested by @Scriddie.
The challenge is that there are 417 reports of male employment and 121 reports of female employment, ie., there are 3.5 times more men than women in the dataset. (An interesting observation that may be saying something about employment equality on its own?)
It's reasonable to assume that if we were to receive ~300 additional reports of female employment, the job spectrum for women would increase. What we can do instead is to draw bootstrap samples (ie. with replacement) of size n = 121 from the observed distribution of male employment. Then we can assess how often we observe 19 or fewer distinct job categories in a sample of n = 121 jobs by men.
Here is how this test can be implemented in R.
<code>k_f <- 19 # occupations that employ women
k_m <- 34 # occupations that employ men
n_f <- 121 # women in dataset
n_m <- 417 # men in dataset

# I don't have the actual data, so simulate fake employment data,
# under one of two conditions.

# (a) All 34 categories with men employed are equally likely.
# In this case, men have a wider job spectrum.
# prob <- rep(1, k_m)

# (b) Only 10 of job categories have significant male employment.
# In this case, women have a wider job spectrum.
prob <- c(rep(1, 10), rep(0.01, k_m - 10))

set.seed(1234)

# Create a sample of size = n_m jobs by men.
occupations_men <- c(seq(k_m), sample.int(k_m, n_m - k_m, replace = TRUE, prob = prob))

# Define a function to draw bootstrap samples of size = n_j
# and count the number of unique job categories in the bootstrap sample.
bootstrap_men_jobs <- function() {
  x <- sample(occupations_men, n_f, replace = TRUE)
  length(unique(x))
}

# Generate many bootstrap samples.
boot_reps <- 1000
boot_sample <- replicate(boot_reps, bootstrap_men_jobs())

# The p-value is the probability of observing 19 or fewer jobs which employ men.
mean(boot_sample <= k_f)
#> [1] 0.627
</code>
Comment: thank you for clarifying Scriddies suggestion and giving me an example. I have not tried bootstrapping before but I see how this can answer my question. Many thanks!