How to sort a NSArray alphabetically?

Question: How can I sort an array filled with [UIFont familyNames] into alphabetical order?
Answer: The simplest approach is, to provide a sort selector (Apple's documentation for details)
Objective-C
sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

Swift
let descriptor: NSSortDescriptor = NSSortDescriptor(key: "YourKey", ascending: true, selector: "localizedCaseInsensitiveCompare:")
let sortedResults: NSArray = temparray.sortedArrayUsingDescriptors([descriptor])

Apple provides several selectors for alphabetic sorting:

compare:
caseInsensitiveCompare:
localizedCompare:
localizedCaseInsensitiveCompare:
localizedStandardCompare:

Swift
var students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
students.sort()
print(students)
// Prints "["Abena", "Akosua", "Kofi", "Kweku", "Peter"]"

Reference
Comment: I updated the link. Thanks for pointing that out. localizedCaseInsensitiveCompare: is a method of NSString and should be sufficient to sort an array of strings.
Comment: Documentation link is outdated, code sample isn't really enough to actually sort the array. localizedCaseInsensitiveCompare: needs to be defined somehow.
Comment: And this can be easily applied to an array containing any custom objects (not just NSString). You just have to make sure that all objects inside your array have implemented the message that the selector is specifying. Then inside this message you just call the NSString compare for the properties of your object that you want to compare.
Comment: Such simple things must not be so complex.
Answer: The other answers provided here mention using @selector(localizedCaseInsensitiveCompare:)
This works great for an array of NSString, however if you want to extend this to another type of object, and sort those objects according to a 'name' property, you should do this instead:
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
sortedArray=[anArray sortedArrayUsingDescriptors:@[sort]];

Your objects will be sorted according to the name property of those objects.
If you want the sorting to be case insensitive, you would need to set the descriptor like this
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];

Comment: +1, Thank you. Funny this is the more common use case than the accepted answer.
Comment: this sorts capital letters first and then small letters
Comment: I get an error when I try to sort an array of strings this way, because `name` is not a valid key. What key do I use to sort strings alphabetically with an NSSortDescriptor?
Comment: +1, The sort descriptor along with selector is exactly what I wanted and no other answer had this. Thanks a lot man
Answer: A more powerful way of sorting a list of NSString to use things like NSNumericSearch :
NSArray *sortedArrayOfString = [arrayOfString sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
        }];

Combined with SortDescriptor, that would give something like :
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
        return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
    }];
NSArray *sortedArray = [anArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

Answer: Another easy method to sort an array of strings consists by using the NSString description property this way:
NSSortDescriptor *valueDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES];
arrayOfSortedStrings = [arrayOfNotSortedStrings sortedArrayUsingDescriptors:@[valueDescriptor]];

Comment: That seems a bit useless; there's no reason to sort strings this way (and there's probably a performance hit for doing so), and for other objects, its description property is rarely useful for sorting purposes.
Answer: Use below code for sorting in alphabetical order:
    NSArray *unsortedStrings = @[@"Verdana", @"MS San Serif", @"Times New Roman",@"Chalkduster",@"Impact"];

    NSArray *sortedStrings =
    [unsortedStrings sortedArrayUsingSelector:@selector(compare:)];

    NSLog(@"Unsorted Array : %@",unsortedStrings);
    NSLog(@"Sorted Array : %@",sortedStrings);

Below is console log :
2015-04-02 16:17:50.614 ToDoList[2133:100512] Unsorted Array : (
    Verdana,
    "MS San Serif",
    "Times New Roman",
    Chalkduster,
    Impact
)

2015-04-02 16:17:50.615 ToDoList[2133:100512] Sorted Array : (
    Chalkduster,
    Impact,
    "MS San Serif",
    "Times New Roman",
    Verdana
)

Answer: This already has good answers for most purposes, but I'll add mine which is more specific.
In English, normally when we alphabetise, we ignore the word "the" at the beginning of a phrase. So "The United States" would be ordered under "U" and not "T".
This does that for you.
It would probably be best to put  these in categories.
// Sort an array of NSStrings alphabetically, ignoring the word "the" at the beginning of a string.

-(NSArray*) sortArrayAlphabeticallyIgnoringThes:(NSArray*) unsortedArray {

    NSArray * sortedArray = [unsortedArray sortedArrayUsingComparator:^NSComparisonResult(NSString* a, NSString* b) {

        //find the strings that will actually be compared for alphabetical ordering
        NSString* firstStringToCompare = [self stringByRemovingPrecedingThe:a];
        NSString* secondStringToCompare = [self stringByRemovingPrecedingThe:b];

        return [firstStringToCompare compare:secondStringToCompare];
    }];
    return sortedArray;
}

// Remove "the"s, also removes preceding white spaces that are left as a result. Assumes no preceding whitespaces to start with. nb: Trailing white spaces will be deleted too.

-(NSString*) stringByRemovingPrecedingThe:(NSString*) originalString {
    NSString* result;
    if ([[originalString substringToIndex:3].lowercaseString isEqualToString:@"the"]) {
        result = [[originalString substringFromIndex:3] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    }
    else {
        result = originalString;
    }
    return result;
}

Comment: I just looked at this years later, and I realised it should also include "a" and "an". These should be easy to add.
Answer: -(IBAction)SegmentbtnCLK:(id)sender
{ [self sortArryofDictionary];
    [self.objtable reloadData];}
-(void)sortArryofDictionary
{ NSSortDescriptor *sorter;
    switch (sortcontrol.selectedSegmentIndex)
    {case 0:
            sorter=[[NSSortDescriptor alloc]initWithKey:@"Name" ascending:YES];
            break;
        case 1:
            sorter=[[NSSortDescriptor alloc]initWithKey:@"Age" ascending:YES];
            default:
            break; }
    NSArray *sortdiscriptor=[[NSArray alloc]initWithObjects:sorter, nil];
    [arr sortUsingDescriptors:sortdiscriptor];
    }

How to sort a NSArray alphabetically?


Div fixada quando chega no top

Question: Bom não sei nem perguntar e nomear como é o nome dessa funcionalidade, algo semelhante ao site da Airbnb quando o card lateral direito chega ao topo da pagina ele fica fixo movendo apenas o conteúdo do meio da página.
Como fazer algo parecido com isso ?
Até achei esta resposta @6153074 porém este está sumindo no meu caso não posso dar fadeIn ou fadeOut
Não estou conseguindo, olha como está.

.banner-full {
    img {
        width: 99vw !important;
        height: 70vh !important;
    }

    .cardContact {
        height: 500px;
        background-color: red;
        width: 400px;

        position: sticky;
        top: 900px; /* distancia que o elemento vai parar antes do topo */
    }
}

Esta aparecendo desta maneira no DevTools do chrome:
Comment: Leia sobre: [position sticky](https://pt.stackoverflow.com/questions/167290/como-funciona-o-position-sticky)
Comment: Ele define a div com "position:fixed", quando o "scroll" chega ao  topo dessa div
Comment: @WallaceMaxters, sim mas lembrando  que o  position sticky não é suportado por todos os browsers ainda, assim, na verdade em  sua maior parte verssões antigas como  chrome, se ele quiser algo crossbrowser seria melhor usar o  position fixed e javascript  >> https://caniuse.com/#feat=css-sticky aqui tem uma  lista de navegadores suportados[
Comment: @MarcosBrinner na verdade só não funciona no IE... Então eu diria que o suporte atende 95% dos navegadores....
Comment: Muito obrigado, se alguem quiser formular uma resposta para que eu aceite, fique a vontade.
Comment: Vc escreveu o CSS em SCSS é isso mesmo? Vc está usando Bootstrap?
Comment: Ops. estou usando materialize.
Answer: Acredito que o suporte dos browser não seja tão ruim assim, apesar de não funcionar no IE, a opção do position:sticky Aqui vc pode consultar o suporte da propriedade:  https://caniuse.com/#feat=css-sticky
Veja o Exemplo: (deixei os comments no css para facilitar)

body{
  height: 1000px;
}
.box {
    position: sticky;
    top: 50px; /* distancia que o elemento vai parar antes do topo */
    width: 50px;
    height: 50px;
    background: #f00;
    margin-top: 160px; /* distancia que o elemento vai estar do topo antes da rolagem */
    margin-right: 50px;
    margin-left: auto;
}

EDIT:
Exemplo de código depois da átualização da pergunta feita pelo autor
(veja esse exemplo em "Página toda" para ver melhor o resultado)

.banner-full img {
  width: 99vw !important;
  height: 70vh !important;
}

.cardContact {
  height: 500px;
  background-color: red;
  width: 400px;

  position: sticky;
  top: 60px;
  /* distancia que o elemento vai parar antes do topo */
}

      gdfgfdgfd

Comment: Poderia me ajudar para ver aonde estou errando, não está dando. Atualizei minha pergunta
Comment: @RenanRodrigues já entendi o seu problema. Se a .ROW não vai ser travada então não tem como "travar" um elemento que está dentro dela, pois vc não consegue travar "meia div" entende. Editei a resposta e coloquei mais um exemplo utilizando o código que vc colocou na atualização da pergunta.
Comment: Tudo certo agora, obrigado.
Comment: @RenanRodrigues Tranquilo jovem boa sorte ai!

Div fixada quando chega no top


Delphi Script (Changing Section Name for VMProtect)

Question: I don't know Delphi and would someone help me to change a small Delhpi script from vmprotect?
function GetRandomSectionName: String;
var I:Integer;
    B:Byte;
begin
  Result:='';
  for I:=1 to 8 do
   begin
    B:=32+Random(Ord('z')-32);
    Result:=Result+Chr(B);
   end;
end;

procedure OnAfterSaveFile;
var I:Integer;
begin
  with VMProtector.OutputFile do
   for I:=0 to Sections.Count-1 do
    Sections.Items[I].Name:=GetRandomSectionName;
end;

It should only randomize the sections which is starting with .vmp the rest should stay.
Answer:
It should only randomize the sections which is starting with .vmp the rest should stay.

Use the System.Pos function to match a substring in a string.
procedure OnAfterSaveFile;
var I:Integer;
begin
  with VMProtector.OutputFile do
   for I:=0 to Sections.Count-1 do
     if Pos('.vmp',Sections.Items[I].Name) = 1 then // Only .vmp sections
       Sections.Items[I].Name:=GetRandomSectionName;
end;


Error returning promise from Ember Data

Question: I am working on my first Ember app and got it to display the way I wanted with the route returning a static JSON object from model():
element: {
  name: "First Element",
  divisions: [{
    name: "First Division",
    sets: [{name: "Set 1"},{name: "Set 2"},{name: "Set 3"}]
  }, {
    name: "Second Division",
    sets: [{name: "Set 1"},{name: "Set 2"},{name: "Set 3"}]
  }]
}

Now I am trying to refactor to use Ember Data + Mirage and having an awful time.
Here's my index.js route
export default Ember.Route.extend({
    model() {
        return this.store.find('element', 1);
    },

If I set up my Mirage config.js like this:
  this.get('/elements', function() {
    return {
      elements: [
          {
            id: 1,
            name: 'First Element',
            divisions: [1, 2]
          }
      ]
    }
  });

then I get this error:
Your Ember app tried to GET '/elements/1', but there was no route defined to handle this request.

If I set up my Mirage config.js like this:
  this.get('/elements/1', function() {
    return {
       id: 1,
       name: 'First Element',
       divisions: [1, 2]
    }
  });

then I get this error:
22:46:40.883 "Error while processing route: index" "Assertion Failed: normalizeResponse must return a valid JSON API document:
    * One or more of the following keys must be present: "data", "errors", "meta"." "EmberError@http://localhost:4200/assets/vendor.js:25582:15

EDIT:
So this isn't a solution to the problem as stated but it got me past this.  I gave up on Pretender and started again creating an actual Rails server according to this excellent tutorial: http://emberigniter.com/modern-bridge-ember-and-rails-5-with-json-api/
I was able to do everything I wanted this way and if I ever want to make this a production app, I'm a lot closer.
Comment: So, if you look [here](http://jsonapi.org/format/#document-top-level). The top level attribute in your Mirage response needs to be either: "data", "errors", or "meta". The first step is to wrap your "element(s)" attribute inside of a "data" attribute. That didn't solve the problem for me, but it's a logical first step.
Comment: Your API response seems to be invalid for ember-data. Can you update your post with what your server sends? Ember-data expects data to be formatted as described here : http://guides.emberjs.com/v2.0.0/models/connecting-to-an-http-server/
Comment: Can you show your router ? You are probably missing a route
Comment: What's going on here is that I have a single element (right now) which contains many divisions, each of which contain many sets.  When I had this working with the static JSON object, I used {{#each in my .hbs template to list out each division, and within that division each set.  So the model for my template is an element.
Comment: Do you use custom adapter or something? Also try removing `divisions: [1, 2]` from config.js and see if it eliminates error.
Answer: So the issue is that you aren't actually adhering to the JSON API specification. You can solve this by reading Mirage's page on how to conform.
Essentially you need to either be returning an object at the top level of your JSON response in the case of a GET /foo/1 call. You'll also need to change your "elements" attribute to "data" for GET /foo and that should do the trick. Right now there isn't a simple, re-usable way to do this Mirage out of the box. The best bet right now for both issues is to use the solution presented in this issue.
Answer: ember error normalizeResponse must return a valid JSON API document
can be fixed in three ways
return a valid JSONAPI response
see your error message:

normalizeResponse must return a valid JSON API document:
* One or more of the following keys must be present: "data", "errors", "meta".

  this.get('/elements/1', function() {
    return {
      data: {
        id: 1,
        name: 'First Element',
        divisions: [1, 2]
      }
    }
  });

see also https://jsonapi.org/examples/
normalize all responses
// app/serializers/application.js

import EmberData from "ember-data";

export default EmberData.JSONAPISerializer.extend({
    normalizeResponse() {
        return {
            data: this._super(...arguments),
        };
    },
    //normalize(){},
    //serialize(){},
    // ...
});

problem: error handling
by wrapping all responses in { data: ... }, they never return errors
on errors, the response should be
  this.get('/elements/1', function() {
    return {
      errors: [
        {
          id: 12345,
          title: 'title for error #12345'
        }
      ]
    }
  });

see also https://jsonapi.org/format/#error-objects
replace JSONAPI with REST
sed -i 's/JSONAPISerializer/RESTSerializer/g' app/serializers/*.js
sed -i 's/JSONAPIAdapter/RESTAdapter/g' app/adapters/*.js

ember docs: adapters and serializers
duplicate: How can ember application be integrated to use with json-server?

Error returning promise from Ember Data


Trabalhar com Windows Forms com designer no Linux?

Question: No Linux (Ubuntu) é possível criar aplicações com Windows Forms no Mono ou similar, usando um designer como o do Visual Studio?
Estou enfrentando problema, pois necessito desenvolver com o Windows Forms em um ambiente Linux com um designer parecido com o do Visual Studio.
Tentei usar o citado pelo site do Mono, mas na hora de criar os eventos de clique, etc não é possível fazer tal ação.
Answer: Possível é, mas o Windows Forms do Mono é bem problemático, de um modo geral as pessoas não usam. Alguns problemas podem ser vistos na FAQ.
Em geral há preferência de uso do GTK#. Anda tendo algumas outras opções, mas nada que pegou tração.
Note que a página do designer indica que ela está desatualizada. Ocorre até porque está essencialmente abandonado. Pode ser que seja um problema existente mesmo. Não espere muito suporte em algo que "ninguém" usa.
Uma alternativa é desenvolver no Visual Studio (sei que não pode, de acordo com outra pergunta, mas é uma opção) e depois usar no Mono. Não sei o quanto dará certo. Na teoria é para dar, mas na prática duvido que tudo funcione 100%.
De qualquer forma é possível programar sem o designer.
Considere usar o MAUI.
Coloquei no GitHub para referência futura.
Comment: Sim. Mas a experiência pode sofrer um pouco. Nem tudo no GTK funciona perfeitamente no Windows. Funciona, mas algumas coisas são diferentes e ficam esquisitas. Nada crítico, depende do gosto. Note que o GTK# também não tem recebido muita atenção, ele roda em cima de um GTK mais antigo, mas é a opção mais segura no Mono até o momento e até onde eu sei.
Comment: Quando uso o GTK# é possível levar a solução para o Visual Studio? Porque o Windows Forms é principalmente para uso na sala de aula, pois pessoalmente o GTK# me auxilia bem.


GNU make special target executes common target with special option

Question: I'm using a GNU makefile to build two variants of an application, which only differ in a macro set by -D:
var_a:
  @g++ -o binary -DVARA main.cpp

var_b:
  @g++ -o binary -DVARB main.cpp

Is there any built-in way to do this using prerquisites?
My only idea is to solve this with recursive make, but I don't feel comfortable with it:
var_a:
  make binary VARIANT=VARA

binary:
  @g++ -o $@ -D$(VARIANT) main.cpp

Answer: It is virtually never a good idea for a recipe to build a target different than $@.
You can use target-specific variables:
var_a var_b: main.cpp
        g++ -o $@ $(CPPFLAGS) $<

var_a: CPPFLAGS += -DVARA
var_b: CPPFLAGS += -DVARB

Answer: A cleaner approach would be to use a VARIANT handler variable and update the defs based on the VARIANT recieved.
DEFAULT_VARIANT=var_a
# -------- set default variant if none given ------ <<<
ifndef VARIANT
   VARIANT=$(DEFAULT_VARIANT)
endif

ifeq ($(VARIANT),var_a)
   DEFS = -DVARA
endif

ifeq ($(VARIANT),var_b)
   DEFS = -DVARB
endif

# Your target and recipe remains same and no recursive calls needed
binary:
  @g++ -o $@ $(DEFS) main.cpp

Execute target : make binary VARIANT=var_b


How to promote QPdfview in Qt Designer

Question: I am trying to upgrade my PDF viewer on a form from QWebEngineView to QPdfView (PyQt6). I originally promoted QWebEngineView in the QT Designer without an issue but as far as PDF functionality its a bit limited so upgrading to QPdfView. I promoted using QWidget as base:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPdfView" name="widget" native="true">
    <property name="geometry">
     <rect>
      <x>109</x>
      <y>69</y>
      <width>291</width>
      <height>371</height>
     </rect>
    </property>
   </widget>
   <widget class="QComboBox" name="comboBox">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>490</y>
      <width>79</width>
      <height>22</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>530</y>
      <width>80</width>
      <height>22</height>
     </rect>
    </property>
    <property name="text">
     <string>PushButton</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>19</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>QPdfView</class>
   <extends>QWidget</extends>
   PyQt6.QtPdfWidgets
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

However, when I try to load it using:
import sys
from PyQt6 import QtWidgets, uic

app = QtWidgets.QApplication(sys.argv)

window = uic.loadUi("mainwindow.ui")
window.show()
app.exec()

I get the following:
Traceback (most recent call last):
  File "/home/serveracct/proj/promos/main.py", line 14, in <module>
    window = MainWindow()
  File "/home/serveracct/proj/promos/main.py", line 10, in __init__
    uic.loadUi("test2.ui", self)
  File "/home/serveracct/.pyenv/versions/3.10.8/lib/python3.10/site-packages/PyQt6/uic/load_ui.py", line 86, in loadUi
    return DynamicUILoader(package).loadUi(uifile, baseinstance)
  File "/home/serveracct/.pyenv/versions/3.10.8/lib/python3.10/site-packages/PyQt6/uic/Loader/loader.py", line 62, in loadUi
    return self.parse(filename)
  File "/home/serveracct/.pyenv/versions/3.10.8/lib/python3.10/site-packages/PyQt6/uic/uiparser.py", line 1014, in parse
    self._handle_widget(ui_file.widget)
  File "/home/serveracct/.pyenv/versions/3.10.8/lib/python3.10/site-packages/PyQt6/uic/uiparser.py", line 842, in _handle_widget
    self.traverseWidgetTree(el)
  File "/home/serveracct/.pyenv/versions/3.10.8/lib/python3.10/site-packages/PyQt6/uic/uiparser.py", line 818, in traverseWidgetTree
    handler(self, child)
  File "/home/serveracct/.pyenv/versions/3.10.8/lib/python3.10/site-packages/PyQt6/uic/uiparser.py", line 280, in createWidget
    self.traverseWidgetTree(elem)
  File "/home/serveracct/.pyenv/versions/3.10.8/lib/python3.10/site-packages/PyQt6/uic/uiparser.py", line 818, in traverseWidgetTree
    handler(self, child)
  File "/home/serveracct/.pyenv/versions/3.10.8/lib/python3.10/site-packages/PyQt6/uic/uiparser.py", line 271, in createWidget
    self.stack.push(self._setupObject(widget_class, parent, elem))
  File "/home/serveracct/.pyenv/versions/3.10.8/lib/python3.10/site-packages/PyQt6/uic/uiparser.py", line 233, in _setupObject
    obj = self.factory.createQtObject(class_name, object_name,
  File "/home/serveracct/.pyenv/versions/3.10.8/lib/python3.10/site-packages/PyQt6/uic/objcreator.py", line 119, in createQtObject
    return self._cpolicy.instantiate(ctor, object_name, ctor_args,
  File "/home/serveracct/.pyenv/versions/3.10.8/lib/python3.10/site-packages/PyQt6/uic/Loader/qobjectcreator.py", line 145, in instantiate
    return ctor(*ctor_args, **ctor_kwargs)
TypeError: QPdfView(parent: Optional[QWidget]): not enough arguments

I tried promoting QPdfView in Qt Designer and tried to load the window using uic.loadUi()
Answer: This is a "bug" (quotes required) caused by the fact that, unlike any other widget, the parent argument of QPdfView is required.
When child widgets are created by uic, they are always created with their parent in the constructor, using the parent=[parent widget] keyword.
PyQt classes are a bit peculiar, and their constructors don't behave exactly like normal Python classes.
Consider the following case:
class MyClass:
    def __init__(self, parent):
        pass

With the above, doing MyClass(parent=None) will be perfectly valid.
The QPdfView constructor is the following:
QPdfView(parent: Optional[QWidget])

This would make us think that using keyworded parent argument will be equally acceptable as using it as a positional one, but, in reality this won't work.
I suspect that the main problem relies on Qt, and PyQt just "fails" because it correctly follows the wrong signature: the parent shouldn't be mandatory, and that can be demonstrated by the fact that you can perfectly do QPdfView(None).
There are two possible workarounds: fix the PyQt file that constructs the classes, or use a custom QPdfView subclass as a promoted widget.
Fix qobjectcreator.py
The culprit is the qobjectcreator.py file; its path should be the following:
<python-lib>/site-packages/PyQt6/uic/Loader/.
Then go to the instantiate() function (around line 136) and insert the following before the return:
        if ctor.__name__ == 'QPdfView' and 'parent' in ctor_kwargs:
            ctor_args = (ctor_kwargs.pop('parent'), )

Use a promoted subclass
This is probably a better choice, as it doesn't require changing PyQt files and ensures compatibility for future versions.
Just create a QPdfView subclass like this:
class MyPdfView(QPdfView):
    def __init__(self, parent=None):
        super().__init__(parent)

Then use MyPdfView instead of QPdfView as class name for the promoted widget, using the file name as header.
You can even include that class in your main script, just ensure that you properly use the if __name__ == '__main__': block to create the QApplication.
Comment: An even simpler solution is to use `loadUiType`. This will follow a different code path which eventually calls `exec` on a generated Python module rather than creating objects dynamically. The real issue here is that the signature of QPdfView doesn't allow *keyword arguments*. The critical section is in *uic/uiparser.py* around line 230. The parser correctly assumes the parent is optional and creates a kwargs dict rather than an args tuple. Thus, strictly speaking, it's just doing exactly what the user told it to do (which was to create a subclass of QWidget).
Comment: @ekhumoro Oh, that's interesting. I wanted to test `loadUiType` but then I completely forgot it, as I thought that having the keyword argument would break things in any case. Anyway, I've filed a [QTBUG-120313](https://bugreports.qt.io/browse/QTBUG-120313) as I believe that there's no valid reason for having it mandatory in the first place (as it shouldn't on QPdfDocument too). I'll send a report to the PyQt ml too.
Comment: Bah - in actual fact, `loadUiType` won't work either, because the generated module still produces a keyword argument. So when `setupUi` is called, the same problem will occur...
Comment: @musicamante I was racking my brains out because the error message keep saying parent optional...promoted subclass worked. Thank you.
Comment: @ScottyCov You're welcome. I've seen your deleted answer, I don't know why it worked but since you deleted it you probably did something that made you believe it worked: from the class/inheritance point of view, PyQt6 and PySide6 are identical and only follow what Qt tells them: QPdfView is always a QAbstractScrollArea. Since Designer doesn't allow promotion of abstract classes, you must use the closest available ancestor, which is QFrame in this case. You cannot use another *derived* class like QScrollArea, because it's incompatible: it would be like promoting a QListView for a QTableView.

How to promote QPdfview in Qt Designer


Writing a 2 by 2 matrix as a product of elementary matrices

Question: Problem:
Write the following matrix as a product of elementary matrices:
$$ \begin{bmatrix}
 1 & 3 \\
 3 & 5\\
\end{bmatrix}
$$
Answer:
Call the matrix we are given $M$. Note that since the determinant of this matrix is non-zero we can write it as a product of elementary matrices.
\begin{align*}
\begin{bmatrix}
 1 & 3 \\
 3 & 5\\
\end{bmatrix}  &\sim
\begin{bmatrix}
 1 & 3 \\
 0 & -4 \\
\end{bmatrix} \\
 &\sim
\begin{bmatrix}
 1 & 3 \\
 0 & 1 \\
\end{bmatrix} \\
 &\sim
\begin{bmatrix}
 1 & 0 \\
 0 & 1 \\
\end{bmatrix}
\end{align*}
The matrix operations performed were
\begin{align*}
R_2 &= -3R_1 + R_2 \\
R_2 &= -\dfrac{1}{4} R_2 \\
R_1 &= -3R_2 + R_1
\end{align*}
\begin{align*}
e_1 &= \begin{bmatrix}
 1 & 0 \\
 -3 & 1 \\
\end{bmatrix} \\
e_2 &= \begin{bmatrix}
 1 & 0 \\
 0 & -\dfrac{1}{4} \\
\end{bmatrix} \\
e_3 &= \begin{bmatrix}
 1 & -3 \\
 0 & 1 \\
\end{bmatrix}
\end{align*}
Now observe that:
$$ e_3e_2e_1M = I $$
Now we solve for $M$.
\begin{align*}
e_2e_1M &= e_3^{-1} I \\
e_1M &= e_2^{-1} e_3^{-1} \\
M &= e_1^{-1}e_2^{-1} e_3^{-1} \\
e_1^{-1} &=  \begin{bmatrix}
 1 & 0 \\
 3 & 0 \\
\end{bmatrix} \\
e_2^{-1} &=  \begin{bmatrix}
 1 & 0 \\
 0 & -4 \\
\end{bmatrix} \\
e_3^{-1} &=  \begin{bmatrix}
 1 & 3 \\
 0 & 1 \\
\end{bmatrix}
\end{align*}
Using SciLab I see that my solution is wrong. Where did I go wrong?
Comment: The fourth entry of $e^{-1}_1$ should be 1, not 0.
Comment: @Rócherz Thanks, it now checks. If you could post your comment as an answer, I could accept it and then close the question.
Answer: The fourth entry of $e^{-1}_1$ should be 1, not 0.


Converting data with commas to variables in JavaScript

Question: I have a row of a lines that is like this, these are the lines of html output of a php code that comes from a search.
here is the browser output page source (not the server source)
**<!DOCTYPE html>

    <meta`enter code here` content="text/html; charset=windows-1252" http-equiv="content-type">
    <title>my page</title>

      <title>My Page</title>
      <form name="myform" action="systemmonitor.php" method="GET">

My page

          <input name="sea1" placeholder="customtext" size="25" type="text">

          <input name="sea2" placeholder="customtext" size="25" type="text">

          <input name="sea" value="submit" type="submit">

data00,data01,data02,data03, , , and so on
data10,data11,data12,data13, , , and so on
data20,data21,data22,data23, , , and so on
(and so on in rows)

      </form>

here is the browser server source (php)
    **<!DOCTYPE html>

        <meta`enter code here` content="text/html; charset=windows-1252" http-equiv="content-type">
        <title>my page</title>

          <title>My Page</title>
          <form name="myform" action="systemmonitor.php" method="GET">

My page

              <input name="sea1" placeholder="customtext" size="25" type="text">

              <input name="sea2" placeholder="customtext" size="25" type="text">

              <input name="sea" value="submit" type="submit">

    <?php
$searchthis = $_GET["sea1"];
$matches = array();

$handle = @fopen("sources.csv", "r");
if ($handle)
{
    while (!feof($handle))
    {
        $buffer = fgets($handle);
        if(strpos($buffer, $searchthis) !== FALSE)
            $matches[] = $buffer;
    }
    fclose($handle);
}

//show results:
$myString = implode( ', ', $matches );
print_r($myString);
?>

          </form>

Now, in the real page where is data0,data1,data2... is a php code that make this stuff from the html forms above, now the php output i dont what to see as is, but i want to convert to variables, and show only the variables where i want to, something likes this its ok:
var parsed[0][0]=data00;
var parsed[0][1]=data01;
var parsed[0][2]=data02;
var parsed[0][3]=data03;
var parsed[1][0]=data10;

0,X (X is the number of the colume)
1,X (X is the number of the row)

and then i show only the data where I want.
for example i will put this code somewhere in the page:
document.write(parsed[0][3]);

the data00 data01 is just a custom data that php will make from before.
how I can do this convert?
Comment: Your numbering in your first snippet makes no sense! Why is the first row `(01, 02, 03)` but the second row `(11, 22, 33)`?
Comment: is just a custom data, nothing else, where is 11 for examble will be ''house'' or anything else. thx.
Comment: You cannot have variables that start with digits in JS.
Comment: Is the PHP code your own? Can you modify it? Is the search page hosted on the same domain as the page you want to run this javascript on?
Comment: @Eric i can modify the PHP code, its mine, yes its all hosted on the same domain, and same page..
Comment: Show us the PHP then!
Comment: @Eric here it is, see the update question.
Comment: Ok, final question - Why do you need the javascript? Why can't you use `
Comment: @Eric becouse this code will show only the data by lines, not be lines&columes of the csv-style text output, if i can search with php with lines&columes then i dont javascript, what ever is php or js, its ok for me. thx. i dont know php well, this php was from a tutorial.
Answer: Is this what you want?
// the string containing the data
var externalData = 'one, two, three\n'
                 + 'four, five, six\n'
                 + 'seven, eight, nine';

// split the string into rows, and split each row into cells
var parsed = externalData.split('\n').map(function(row) {
    return row.split(', ');
});

console.log(parsed[0][1]);  // 'two'

Comment: @user2670708: Have a proper look at the code. `parsed[0][1]` returns the second value of the first row (`two`), `three` is the *third* value of the first row, so you need `parsed[0][2]`, etc. If you don't know how arrays work, I recommend to read a tutorial: http://eloquentjavascript.net/chapter4.html.
Comment: @user2670708: The value of `externalData` is just an *example*. You apply the same procedure on your string/data. Don't you see the resemblance of the example and your data?
Comment: this makes ''one,two,three four,five,six seven,eight,nine''
but i want to read only ''three'' (for example) or only seven?
Comment: yes but one two three have to be the real data that come from the page.
how i can see my custom data instead of 'one' or 'two' ?
Comment: i know that is example, but i dont know how to put my data in there, because the data is coming automatically from a php code and the format is specific, (one,two,three,and so on).
Comment: Mean that if my php output was = **'one, two, three\n' + 'four, five, six\n' + 'seven, eight, nine';** ...was ok, but i comes just on **one,two,three and so one**, how can i put this ((php-output)) on the javascript code? if am say it well.
Comment: @user2670708: If you are using Ajax to get the data, then your data is in the response text. You don't really provide any information about how you get the data, so it's difficult to give any suggestion.
Comment: @user2670708: I would suggest to put an HTML around the output and give it an ID. Then you just read the elements content with `var data = document.getElementById('my_data').innerHTML;`. If the rows are really denoted through `` elements, then you have to split on `` instead of `\n`. Or you could wrap each line in an HTML element (like a span) give it a class and use `getElementsByTag` to get a list of those so that you can iterate over them and extract the data. If would recommend to read a bit about JavaScript and DOM traversal/manipulation (Google).


when enabling errors to bigquery I do not receive the bad record number

Question: I'm using bigquery command line tool to upload these records:
{name: "a"}
{name1: "b"}
{name: "c"}

.
➜ ~ bq load --source_format=NEWLINE_DELIMITED_JSON  my_dataset.my_table ./names.json

this is the result I get:
Upload complete.
Waiting on bqjob_r7fc5650eb01d5fd4_000001560878b74e_1 ... (2s) Current status: DONE
BigQuery error in load operation: Error processing job 'my_dataset:bqjob...4e_1': JSON table encountered too many errors, giving up.
Rows: 2; errors: 1.
Failure details:
- JSON parsing error in row starting at position 5819 at file:
file-00000000. No such field: name1.

when I use bq --format=prettyjson show -j <jobId> I get:
 {
  "status": {
    "errorResult": {
      "location": "file-00000000",
      "message": "JSON table encountered too many errors, giving up. Rows: 2; errors: 1.",
      "reason": "invalid"
    },
    "errors": [
      {
        "location": "file-00000000",
        "message": "JSON table encountered too many errors, giving up. Rows: 2; errors: 1.",
        "reason": "invalid"
      },
      {
        "message": "JSON parsing error in row starting at position 5819 at file:
        file-00000000. No such field: name1.",
        "reason": "invalid"
      }
    ],
    "state": "DONE"
  }
}

As you can see I receive an error which tells me in what line I had an error. : Rows: 2; errors: 1
Now I'm trying to enable errors by using max_bad_errors
 ➜  ~ bq load --source_format=NEWLINE_DELIMITED_JSON --max_bad_records=3 my_dataset.my_table ./names.json

here is what I receive:
Upload complete.
Waiting on bqjob_...ce1_1 ... (4s) Current status: DONE
Warning encountered during job execution:

JSON parsing error in row starting at position 5819 at file: file-00000000. No such field: name1.

when I use bq --format=prettyjson show -j <jobId> I get:
{
  .
  .
  .
  "status": {
    "errors": [
      {
        "message": "JSON parsing error in row starting at position 5819 at file: file-00000000. No such field: name1.",
        "reason": "invalid"
      }
    ],
    "state": "DONE"
  },
}

when I check - it actually uploads the good records to the table and ignores the bad record,
but  now I do not know in what record the error was.
Is this a big query bug?
can it be fixed so I receive record number also when enabling bad records?
Answer: Yes this is what max_bad_records does. If the number of errors is below max_bad_records the load will succeed. The error message tells you the start position of the failed line, 5819, and the file name, file-00000000. The file name is changed since you're doing an upload and load.
The previous "Rows: 2; errors: 1" means 2 rows are parsed and there is 1 error. It's not always the 2nd row in the file. A big file can be processed by many workers in parallel. Worker n starts processing at position xxxx, parsed two rows, and found an error. It'll also report the same error message and apparently 2 doesn't mean the 2nd row in the file. And it doesn't make sense for worker n to scan the file from beginning to find out which line it starts with. Instead, it'll just report the start position of the line.
Comment: what does the position tell me?
how can I calculate the record number using the position?
I'm trying to know which records weren't uploaded to bigquery
Comment: please see this question - it explains the extended issue:
http://stackoverflow.com/questions/38581844/bigquery-bug-do-not-receive-all-bad-records-when-uploading-data
Comment: "The previous "Rows: 2; errors: 1" means 2 rows are parsed and there is 1 error"   - this isn't true, I did different tests  and Rows always gave me the row number - and not the number of rows parsed!!
Comment: The position is the start offset of the record in the file. In text files this basically tells you that the record starts at the nth char in the file.

Did you test with a file big enough that will be split and parsed by several workers? For small files only one worker is used and sure the rows it parsed will be equal to the row number.


How do I process the trigger file in Azure Data Factory

Question: I want to process a file that is placed on our Blob storage using a trigger.
The trigger looks like this:
trigger definition:
Daily a new file is created with a data-timestamp in the name.
Q: I want to read the content of this JSON (which has a different name daily), but how?
Answer:
To get the contents of the file which is triggering the pipeline, you need to use look up activity with parameters. First create a pipeline parameter as shown below:

Now in look up, I have created a dataset. For this dataset, I have created a parameter to use as filename. I pass this value from pipeline using the above created parameter value.

Now when you create the storage event trigger for this pipeline, after filling the trigger definition and clicking continue, parameters tab will be displayed. Here give the value to fileName pipeline parameter as @triggerBody().fileName.

When a file is uploaded/created, then the filename will be captured and stored in fileName parameter which will be used in look up to get the content of that file.


How do I solve this system of equations?

Question: I am doing something that is commmon practice in economics to uniquely identify matrices. After deriving 3 unrotated factors from PCA, I then want to rotate them to be able to interpret them in economic terms.
The usual procedure is to choose an orthogonal matrix $U$ (3x3 in my case) such that $UU'=I$. Being a 3x3 matrix I need 3 additional restrictions to uniquely identify it. So, first, I compute $UU'$:
$UU' = \begin{bmatrix} u_{11} & u_{12} & u_{13} \\ u_{21} & u_{22} & u_{23}\\ u_{31} & u_{32} & u_{33} \end{bmatrix} \begin{bmatrix} u_{11} & u_{21} & u_{31} \\ u_{12} & u_{22} & u_{32}\\ u_{13} & u_{23} & u_{33} \end{bmatrix} = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}$
and I get 6 equations in 9 unkowns:
(1) $u_{11}^2 + u_{12}^2 + u_{13}^2 = 1$,
(2) $u_{21}^2 + u_{22}^2 + u_{23}^2 = 1$,
(3) $u_{31}^2 + u_{32}^2 + u_{33}^2 = 1$,
(4) $u_{11}u_{21} + u_{12}u_{22} + u_{13}u_{23} = 0$,
(5) $u_{21}u_{31} + u_{22}u_{23} + u_{23}u_{33} = 0$,
(6) $u_{11}u_{31} + u_{22}u_{23} + u_{23}u_{33} = 0$
Then I add 3 additional restrictions to uniquely identify the matrix:
(7) $0.45u_{12} + 0.65u_{22} + 0.88u_{32} = 0$,
(8) $0.45u_{13} + 0.65u_{23} + 0.88u_{33} = 0$,
(9) $VCov(U_3F)' = 0$ (where this is the first derivative of the variance-covariance)
and I get a system of 9 equations in 9 unkowns which I can then solve.
Now, my silly question is: shall I treat it as a sytem of quadratic equations? I find difficult to see the structure of it. As a result, I find difficult to find the right function in R to solve it. But the most important thing for me is to understand what type of system I am solving and how can I structure it in a way it's clearer to me. Then if anyone has any suggestion on how to solve it in R would be incredibly useful.
Thanks a lot for your help!
Answer: $U$ is an orthonormal matrix, with columns having norm $1$ and orthogonal to each other. The solution is not unique, so coming up with a solution should suffice for you I guess, if there are no other restrictions. You can create a random matrix, and then orthonormalize its columns using gram-schmidt process. The following R script does it for you:
<code>library('matlib')
U = GramSchmidt(replicate(3, rnorm(3)))
</code>
Of course, GS process needs linearly independent columns, but with random generation you'll highly likely get one.
Comment: Well, $x$ equations and $x$ unknowns doesn't mean unique solution. For example, if you intersect two circles, you'll have two solutions. I'm not familiar with your (9)-th condition, but I think it's better to post your updated question in Math SE. You can satisfy your 7,8-th condition by choosing your first column as normalized version of $[0.45,0.65,0.88]^T$.
Comment: Thanks so much, this is super helpful! But, as you rightly point it out, the matrix is not uniquely identified. Let's assume I have equations for the restrictions (I have 3 other restrictions in fact - question edited to show you the full picture). How would you solve it? I need a package who reads symbolic equations and what else? I lost 5 days on this, you would be life-saver if you could help
Comment: thanks I will post into Math SE. I agree, indeed I will have 9 solutions for every  $u_{ij}$ unkown but in so doing the matrix U will be uniquely identified.  Now I am looking into **nleqslv** in R to see whether I could solve it


wxPython: Hide a widget and remove the leftover space

Question: I want to hide a widget in a sizer and not leave an empty space.
I do not want to remove the widget, because I want to show it again later.
Is there a way to hide a widget and have the shown widgets after it move over one space to fill the empty spot?
Here is a simple example of a space being left:
import wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        sizer = wx.GridSizer(1, 3, 0, 0)

        a = wx.Button(self, -1, 'a')
        b = wx.Button(self, -1, 'b')
        c = wx.Button(self, -1, 'c')

        sizer.Add(a, 0, 0, 0)
        sizer.Add(b, 0, 0, 0)
        sizer.Add(c, 0, 0, 0)

        b.Hide()

        self.SetSizer(sizer)

class MyFrame(wx.Frame):
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, parent=None, title="Remove Spaces")
        panel = MyPanel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(panel, 1, wx.EXPAND)
        self.SetSizer(sizer)
        self.Fit()
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

Answer: Do you have to use wx.GridSizer? Because it will give you a fixed layout, so even you hide a control the grid will still have that space displayed.
Have you considered using wx.GridBagSizer? Try this one:
class MyPanel(wx.Panel):
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        self.parent = parent

        self.sizer = wx.GridBagSizer()

        self.a = wx.Button(self, -1, 'a')
        self.b = wx.Button(self, -1, 'b')
        self.c = wx.Button(self, -1, 'c')
        self.a.Bind(wx.EVT_BUTTON, self.button_clicked)

        self.sizer.Add(self.a, pos=(0, 0))
        self.sizer.Add(self.b, pos=(0, 1))
        self.sizer.Add(self.c, pos=(0, 2))

        self.SetSizer(self.sizer)

    def button_clicked(self, event):
        if self.b.IsShown():
            self.b.Hide()
        else:
            self.b.Show()
        self.parent.Fit()

Using FlexGridSizer:
class MyPanel(wx.Panel):
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        self.parent = parent

        self.sizer = wx.FlexGridSizer(1, 3, 0, 0)

        self.a = wx.Button(self, -1, 'a')
        self.b = wx.Button(self, -1, 'b')
        self.c = wx.Button(self, -1, 'c')
        self.a.Bind(wx.EVT_BUTTON, self.button_clicked)

        self.sizer.Add(self.a)
        self.sizer.Add(self.b)
        self.sizer.Add(self.c)

        self.SetSizer(self.sizer)

    def button_clicked(self, event):
        if self.b.IsShown():
            self.b.Hide()
        else:
            self.b.Show()
        self.parent.Fit()

Comment: Your example does remove the empty space, but as far as I can see, this will only work for a Bag Sizer.

Is there a way to remove empty spaces *regardless* of the sizer type (grid sizer, flex grid sizer, bag sizer, etc.)?
Comment: As far as i know, `GridSizer` is pretty static. You would need to readd that last button to the sizer in the second position.
Comment: What about a `FlexGridSizer`?
Comment: I marked your answer as correct because you did show a way to remove empty space, even if it is not using a different sizer.
Comment: @Kade I've added the one with `FlexGridSizer` as you mentioned.
Comment: @ Selçuk Cihan It looks like this only works if you hide all items in the column, regardless of it being a `Flex Sizer` or a `BagSizer`. The `Grid Sizer` does not do this, because each 'slot' is the same size.

I do not think there is a way to make the elements shift over one space to fill empty spaces without re-positioning everything in a `Bag Sizer`.

Thanks for your help.
Comment: @Kade sure if there are multiple rows and you only hide an item in one row, then it will not cause the empty space to collapse because otherwise the items in other rows would be affected.
Comment: A more recent answer says that instead of Fit(), you should call Layout() on the topmost container: https://stackoverflow.com/a/64540783


A sum of products symmetric in the images under all the embeddings

Question: Let $\mathbb{Q}\subset K\subset \mathbb{C}$ where $K$ is a finite extension of $\mathbb{Q}$. Let $\sigma_1, \dots, \sigma_n$ be all the embeddings $K\rightarrow \mathbb{C}$. Is it true that elements of $\mathbb{C}$ like $\sigma_1(x)\sigma_2(y)+\sigma_2(x)\sigma_3(y)+\dots+\sigma_n(x)\sigma_1(y)\in \mathbb{Q}$?
Comment: To be clear your sum is $\sigma_n(x)\sigma_1(y)+\sum_{i=1}^{n-1} \sigma_i(x)\sigma_{i+1}(y)$?
Comment: Yes. This is the sum I meant.
Answer: This is not true in general when $n\ge 3$ (trivially true otherwise).
First one shoud notice that your sum depends on the numbering of the set of embeddings $\Gamma$.
Suppose for simplicity that $K$ is Galois over $\mathbb Q$ (otherwise your statement has little chance to be true). And suppose
the statement is true. For any $\tau\in\Gamma$, there exists a permutation $\phi_\tau$ of $\{1, \dots, n\}$ such that for all $i\le n$, we have $\tau\sigma_i=\sigma_{\phi_\tau(i)}$. Now apply $\tau$ to $\sum_i \sigma_i(x)\sigma_{i+1}(y)$ (identify $n+1$ to $1$), we get
$$ \sum_i \sigma_{\phi_\tau(i)}(x)\sigma_{\phi_\tau(i+1)}(y)
=\sum_i \sigma_i(x)\sigma_{i+1}(y). $$
So
$$\sum_i (\sigma_{i+1}(y)-\sigma_{\phi_\tau(\phi_\tau^{-1}(i)+1)}(y))\sigma_i(x)=0$$
Fix $y$ and let $x$ varies in $K$. Using Dedekind's independance lemma and then make $y$ vary in $K$, we have
$$\phi_{\tau}^{-1}(i+1)=\phi_\tau^{-1}(i)+1$$
for all $i$. Now take $\tau=\sigma_2\sigma_1^{-1}$. This implies that $\phi_\tau(1)=2$. The above relation then imply that $\phi_\tau(i)=i+1$. In other words, $\tau\sigma_i=\sigma_{i+1}$ and $\tau^i\sigma_1=\sigma_i$. Thus $\tau$ has order $n$ and $\Gamma$ is cyclic.
Conclusion: if $\sum_i \sigma_i(x)\sigma_{i+1}(y)\in \mathbb Q$ for all $x,y\in K$, then $\Gamma$ is cyclic and the numbering $\sigma_1, \dots, \sigma_n$ of elements of $\Gamma$ follows the rule that $\sigma_{i+1}=\tau\sigma_i$ for some generator $\tau$ of $\Gamma$. The converse is easily see to be true.
Comment: I think your manipulations of varying $x$ and $y$ could be replaced by taking a [normal basis](http://en.wikipedia.org/wiki/Normal_basis) for $K/Q$, and by taking $x$ and $y$ to be two among them. This could simplify things, I suppose. P.S. The link is to supply a reference, not to intervene with your own notations. Thanks.
Comment: @awllower: I don't know. But why not, you could try. I would be happy to see a different solution.
Comment: I write a messy answer. Hope you like it. :D
Answer: I tried to make a counter-example. I don't really understand the more advanced answers.

If we take $$K  = \mathbb Q(\sqrt[3]{2},\sqrt{-3})$$ be the splitting field of $X^3-2$, and set $y=1$ then we have six automorphisms which are defined by how they map the pair $(\sqrt[3]{2},\sqrt{-3})$ to
$$\begin{array}{|r|l|c|} \hline
(\sqrt[3]{2},+\sqrt{-3}) & (\omega\sqrt[3]{2},+\sqrt{-3}) & (\omega^2 \sqrt[3]{2},+\sqrt{-3}) \\ \hline
(\sqrt[3]{2},-\sqrt{-3}) & (\omega\sqrt[3]{2},-\sqrt{-3}) & (\omega^2 \sqrt[3]{2},-\sqrt{-3}) \\ \hline
\end{array}$$
If we choose these automorphisms in this order
1,2,3
6,5,4

then given $x=y=\sqrt[3]{2}$ the form is $$(1 \cdot \omega + \omega \cdot \omega^2 + \omega^2 \cdot \omega^2 + \omega^2 \cdot \omega + \omega \cdot 1 + 1 \cdot 1)(\sqrt[3]{2})^2 = \sqrt{-3} \not \in \mathbb Q$$

on the other hand I think the form is invariant if you pick the order
1,5,3
4,2,6

Comment: The two definitions of $K$ don't coincide. You meant the second one ?
Comment: OK, I edit to make clear it is the splitting field.
Comment: @QiL'8, thanks you I am a beginner so I got it wrong: I guess I meant the splitting field of X^3-2.
Comment: anyway the key here was given by you: the order in which we choose automorphisms let's us choose them in a very odd "non-symmetric" order to get irrational values.
Comment: @QiL'8, thanks a lot
Answer: First let us modify the proof given by Qil'8:
If $x$ and $y$ are two different numbers among a normal basis of $K/Q$, then $\sigma_i(x)$ are linearly independent. Hence, by use of the same method as Qil'8, we can prove the same result, instead of letting $x$ and $y$ vary and using Dedekind theorem.
Another aproach would be to take a normal basis generated by $\theta$, and write
$x=\sum_ia_i\sigma_i(\theta)$
$y=\sum_ib_i\sigma_i(\theta)$,
so that the expression becomes
$\alpha=\sum_m\sum_n\sum_ia_mb_n\sigma_i\sigma_m(\theta)\sigma_i\sigma_n(\theta)$.
Write moreover $\sigma_i\sigma_j=\sigma_{\phi_i(j)}$. Then We shall compute the stabilizer $G_{\alpha}$ of $\alpha$. By direct computations we find that $\sigma_j$ belongs to $G_{\alpha}$ if and only if
$\sum_ia_{\phi_i^{-1}\phi_j^{-1}(m)}b_{\phi_{i+1}^{-1}\phi_j^{-1}(n)}=\sum_ia_{\phi_i^{-1}(m)}b_{\phi_{i+1}^{-1}(n)}$ for every $m$ and $n$, leading us to the following equation:
$\sum_ia_{\phi_i^{-1}(m)}(b_{\phi_{i+1}^{-1}(n)}-b_{\phi_{\phi_j^{-1}(i)+1}^{-1}\phi_j^{-1}(n)})=0$
For any given $j$, we can adjust the coefficients so that the second factor is $0$, except at one place. And we can set the coefficient for $a$ to be $1$ at that place, thus contradicting the condition. Hence it is generally not rational.
P.S. The purpose is not to provide a shorter answer, but to view the general case, and, indeed, for some fixed $x$ and $y$, we can explicitly compute the above expressions and thus determine the size of the stabiliser.
P.P.S.
We can compute the above expressions in the general case, i.e. with $a$ and $b$ varying, thus determine which extensions could satisfy the requirements:
It is seen that, if such an expressions are fixed for all $a$ and $b$, then we would obtain:
$b_{\phi_{i+1}^{-1}(n)}-b_{\phi_{\phi_j^{-1}(i)+1}^{-1}\phi_j^{-1}(n)}=0$,
for any $i$, $j$, and $n$. Thus we conclude that $\phi_{i+1}^{-1}(n)=\phi_{\phi_j^{-1}(i)+1}^{-1}\phi_j^{-1}(n)$, that is,
$\phi_j\phi_{\phi_j^{-1}(i)+1}=\phi_{i+1}$.
Rendering this into the language of automorphisms, we find that
$\sigma_j\sigma_{\phi_j^{-1}(i)+1}=\sigma_{i+1}$,
so that $\phi_j^{-1}(i)+1=\phi_j^{-1}(i+1)$.
And this condition has been shown to imply that the extension is cyclic, and the automorphisms are ordered in ascending order according to its exponent with respect to a specific generator. Hence this expression indeed shows that in general the expression is not a rational number. And, let me emphasize again, from this above equation, one can compute the stabilisers of given $a$ and $b$.
Comment: You say the $\sigma_i(x)$ are linearly independent, but only over $\mathbb Q$, right ? Then what is the next step ?
Comment: Yes, that is over $Q$. And then substitute into your relations the normal basis expression to simplify things. Or one can employ the following calculations and assume that $x$ and $y$ come from some normal basis, for example, assume $x=\theta$ and $y=\sigma(\theta)$.
Comment: But how do you order the $\sigma$'s ? Note that the original statement is true for cyclic extensions when the automorphisms are ordered in a suitable way.
Comment: @QiL'8 I edited: hope it helps.


Regex to only allow you to type "." after the first "0" then any number after in a calculator app

Question: I'm trying to use a regex replace to not allow you to input any other digit other than a decimal after you first input a zero. For a calculator app. So that the display can't look like 000003 and instead would just stay on 0 until you type 3.
Comment: Where is your code?
Answer: I have used this in the past as a quick fix and worked for me.
input= input.replace(/^0*/g,"0").replace(/^0*(\d)/g, "$1");
replace all leading 0s with only one '0' but then delete that one '0' if it is followed by a digit.
Also to ensure only 1 dot is entered replace multiple dots with 1 dot:
input = input.replace(/\.+/g, ".");
Comment: That solved the problem of multiple zeros at the start. Now I'm trying to use regex to have it display "0." when you type "." instead of just "." Here's what I have so far `${number.replace(/^0+/, "") + num}`
Answer: Will this work :-)

input= input.replace(/^0*/g,"0").replace(/^0*(\d)/g, "$1").replace(/^(\.)/g,".0").replace(/\.+/g, ".");


Using sudo: Registry directory is still not writeable by the current user. Why?

Question: I'm trying to run:
sudo pecl channel-update pecl.php.net

Because every time I try and install a package it prompts me to run this instead. But when I run those commands I get: "Registry directory is not writeable by the current user" Now this question is very similar but suggests using sudo which I'm already doing. When I run sudo whoami I'm getting root. Could this be related to http://pear.php.net going down?
Answer: I had exactly the same problem (on my Mac Book), this is how I solved it:
I did a reinstall of PEAR using the following steps: http://jason.pureconcepts.net/2012/10/install-pear-pecl-mac-os-x/
After the installation of PEAR (which is bundled with PECL) my PECL commands did work again.
Comment: i followed the same steps and pear seems to be installed but i get the message: WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update and when i try i get Updating channel "pecl.php.net"
Updating Channel "pecl.php.net" in registry failed"
Comment: Note: the linked article uses the http url but now it's working only by using https: `curl -O https://pear.php.net/go-pear.phar`. A blind copy-paste the of command will download a
"Moved here..." html file and not the real file.


How do I get the category links as I had them in Drupal 6, before migrating the site to Drupal 7?

Question: I am migrating my Drupal 6 site to Drupal 7. I have vocabulary/category links like http://www.megaleecher.net/category/12.
How do I replicate this with Drupal 7? I exported the below view to Drupal 7, but it does not work.
<code>$view = new view;
$view->name = 'vocabulary';
$view->description = '';
$view->tag = '';
$view->base_table = 'node';
$view->human_name = '';
$view->core = 0;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Defaults */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'time';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'node';
/* Sort criterion: Node: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Argument: Taxonomy: Vocabulary ID */
$handler->display->display_options['arguments']['vid']['id'] = 'vid';
$handler->display->display_options['arguments']['vid']['table'] = 'vocabulary';
$handler->display->display_options['arguments']['vid']['field'] = 'vid';
$handler->display->display_options['arguments']['vid']['style_plugin'] = 'default_summary';
$handler->display->display_options['arguments']['vid']['default_argument_type'] = 'fixed';

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page_1');
$handler->display->display_options['defaults']['query'] = FALSE;
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['distinct'] = TRUE;
$handler->display->display_options['path'] = 'category/%';
</code>
Comment: OK, it seems while the d2d migrate module imported the taxonomy and nodes it did not configure/imported/created the term-node relationship. Any ideas how to do ?
Answer: check this migrate and migrate_extras module, it is very handy framework to migrate content, you can easily migrate

node
taxonomy/categories
users

Edited:
Let me clarify with your scenario,
while migrating from source(D6) to destination(D7),
you have to define source field and destination fields as shown in below code sample.
first you have to migrate Taxonomy terms from D6 to D7.
later on you have to migrate node entity, while preparing node migrate class, you have to migrate term reference field as it is. so that in prepareRow function we can replace with old term_id with new term_id which is already migrated.
For example see sample code.
<code>abstract class JiveBlogMigration extends Migration {

public function __construct() {
// Always call the parent constructor first for basic setup
parent::__construct();

// With migrate_ui enabled, migration pages will indicate people involved in
// the particular migration, with their role and contact info. We default the
// list in the shared class; it can be overridden for specific migrations.

  }

}

/**
 *
 * Jive Blog Post Information Migration class.
 *
 */
class JiveBlogPostMigration extends JiveBlogMigration {

  public function __construct() {
  // The basic setup is similar to BeerTermMigraiton
  parent::__construct();
  $this->description = t('Jive Blog Post Data Migration.');
  $this->map = new MigrateSQLMap($this->machineName,
                array('jive_blog_post_id' => array(
                        'type' => 'int',
                        'not null' => TRUE,
                        'description' => 'Jive Blog Post ID.'
                    )
                ),
                MigrateDestinationNode::getKeySchema()
   );

// Added ContainerType field for mapping
$query = db_select('migrate_jive_blog_post', 'mjbp')
        ->fields('mjbp', array('jive_blog_post_id', 'jive_user_id', 'jive_group_id', 'jive_container_type', 'subject', 'body', 'permalink', 'minorEdit', 'creationDate', 'modificationDate', 'status', 'displayName', 'blogstatus', 'feedEnabled', 'feedFullContent', 'pingOverride', 'pingServices', 'trackbacksEnabled', 'trackbacksModeration', 'trackbacksNotification')); //SELECT the fields from node
//generate the count for the number of records to be imported
$count_query = db_select('migrate_jive_blog_post', 'b');
$count_query->addExpression('COUNT(jive_blog_post_id)', 'cnt');
$source_fields = array(
    'jive_blog_post_id' => t('The id of a particular document in the jive system'),
    'term_data' => t("The taxonomy terms associated with the node"),
    'jive_container_id' => t("The social group or space id associated to the node decided in prepareRow()."),
);
$this->source = new MigrateSourceSQL($query, $source_fields, $count_query);

// Set up our destination - nodes of type blog
$this->destination = new MigrateDestinationNode('blog');

// Mapped fields
$this->addFieldMapping('title', 'subject')
        ->description(t('Mapping beer name in source to node title'));

// References to related objects (such as the author of the content) are
// most likely going to be identifiers from the source data, not Drupal
// identifiers (such as uids). You can use the mapping from the relevant
// migration to translate from the old ID to the Drupal identifier.
// Note that we also provide a default value of 1 - if the lookup fails to
// find a corresponding uid for the aid, the owner will be the administrative
// account.
$this->addFieldMapping('uid', 'jive_user_id')
        ->sourceMigration('JiveUserAccount')
        ->defaultValue(1);
// Mapping for Tags
$this->addFieldMapping('Tags', 'term_data');

// Additional mapping fields
$this->addFieldMapping('field_displayname', 'displayname');
$this->addFieldMapping('field_pingoverride', 'pingoverride');
$this->addFieldMapping('field_pingservices', 'pingservices');
$this->addFieldMapping('field_feedenabled', 'feedenabled');
$this->addFieldMapping('field_feedfullcontent', 'feedfullcontent');
$this->addFieldMapping('field_status', 'blogstatus');
$this->addFieldMapping('field_tbmoderation', 'trackbacksmoderation');
$this->addFieldMapping('field_tbnotification', 'trackbacksnotification');
$this->addFieldMapping('field_minoredit', 'minoredit');
$this->addFieldMapping('field_trackbacksenabled', 'trackbacksenabled');

//This is for path
$this->addFieldMapping('path', 'permalink');
$this->addFieldMapping('pathauto_perform_alias')
        ->defaultValue(0);

// Group mapping for space, personal, group associated Blog posts
$this->addFieldMapping('og_groups', 'jive_container_id');

$this->addFieldMapping('body', 'body');
$this->addFieldMapping('created', 'creationdate');
$this->addFieldMapping('changed', 'modificationdate');
$this->addFieldMapping('status', 'status');

// Unmapped destination fields
$this->addUnmigratedDestinations(array('revision_uid', 'teaser', 'revision', 'language', 'promote', 'sticky',
    'field_group_sticky', 'field_content_images', 'field_featured_content',));

//specify the unmapped source fields.
$this->addUnmigratedSources(array('jive_group_id', 'jive_container_type'));
}

public function prepareRow($current_row) {

 $source_id = $current_row->jive_blog_post_id;

 //populate all the tags associated with a particular node using the getDrupalTags() method
 //that returns an array of all the tags associated with a particular node.
 $term_data = JiveMigrationUtilities::getDrupalTags($source_id, 38); //38 is the object type for the blogpost in jive.
 if ($term_data) {
  $current_row->term_data = $term_data;
 }
 if ($current_row->jive_group_id != 'NULL') {
  $current_row->jive_container_id = JiveMigrationUtilities::getDrupalContainerNid($current_row->jive_container_type, $current_row->jive_group_id);
  }
 return TRUE;
}

}
</code>
Usual flow of migration module, it will create a mapping table for each migration class like migrate_map_jiveblogmigration, it contains sourceid1 and destid1 columns. for example D6 nid is as sourceid1 and D7 nid is as destid1.
in prepareRow function you can write a logic to replace old term_id with new term_id for each row of node migration.
Comment: I used migrate module to do the migration, thanks for introducing the migrate_extra module this seems will do the trick. Will update. Thanks
Comment: I checked the module but already have migrated node, categories and users. I need to make them accessible by menu as described above ?
Comment: if you have already migrated node, categories and users using migration framework. you can directly access those node/categories/user's as you normally do. let me know, if you are not clear. will try to help
Comment: How it does not work - I tried http://mysite.gotpantheon.com/categories/internet but it does not work and says "page not found"
Comment: and neither did the "category id" as in the original site.
Answer: D2D module did not established the nid-term relationship when importing, so while we had the nodes and terms imported successfully there was no pointer relating the two which resulted in no results for category/vocabs.
Used Drush site-upgrade and things imported correctly.


How to override theme_file_widget?

Question: Is there any method to override this function theme_file_widget.
I tried to to do the following in my module.
function mymodule_file_widget($variables)
and also in my theme
function mytheme_file_widget($variables)
but did not work. I cleared the cache also. Is there any method to override this function?
Answer: The correct way to override the theme function is:
/**
 * Implements hook_theme_registry_alter().
 */
function mymodule_theme_registry_alter(&$theme_registry) {
  $theme_registry['file_widget']['function'] = 'mymodule_file_widget';
}

function mymodule_file_widget($variables) {
 //your code
}

How to override theme_file_widget?


Measure the fluctuation of data

Question: I have 2 series of data, for example:
s1: 0.3 0.3 0.4 0.8 0.6 0.5 0.7
s2: 0.7 0.7 0.6 0.8 0.7 0.5 0.6
It's easy to see that the data in s1 is fluctuating a lot (min=0.3, max=0.8) while in s2 all the values close to others.
Which metric should I use to measure the fluctuation like this? I tried with standard deviation but it's not very clear.
Comment: What's the problem with standard deviation? I get  SD for s1 of 0.195 and for s2 of 0.098, which captures the contrast you rightly identify.
Answer: Depending on what exactly you are trying to measure, there could be a selection of metrics to choose. Standard deviation is indeed a good choice to measure the amount of variation or dispersion of a dataset.
The formula is typically given by:
$\sigma = \sqrt{\frac{1}{N}\sum_{i=1}^{N} (x_i-\mu)^2 }$
where $N$ is the sample size, $x_i$ is each individual observation, and $\mu$ is the mean of the dataset.
Below I've plotted the two distributions, you can see that $\sigma_{s1}=0.2$ and $\sigma_{s2}=0.1$. This is telling you that on average, the data in s1 varies twice as much with respect to the mean value compared to s2. I encourage you to read up on the metric to gather a better understanding.
Edit:
As @NickCox has rightly pointed out, standard deviation is also commonly formulated as:
$\sigma = \sqrt{\frac{1}{N-1}\sum_{i=1}^{N} (x_i-\mu)^2 }$
Note that for relatively large sample sizes this will make little appreciable difference although please see this this answer for a discussion on the point.
Comment: Helpful. Note to those new to these ideas: Many people use $1/(N-1)$ rather than $N$, in the notation here. That convention underlies the results in my comment on the question.   Also _number of samples_ is common wording in some sciences, but _sample size_ or _number of observations_ is more likely wording from statistical texts or courses.
Comment: Yes indeed you are absolutely correct @NickCox, I believe the merits of both formulations can be discussed late into the night by statisticians...guess you can spot the engineer with sloppy jargon and *close enough* approach. I'll update my answer to note these points more clearly for future readers.
Answer: Standard deviations are not scale invariant, i.e., they are expressed in the units of the measure. For a scale invariant metric that is dimensionless and comparable across measures that differ widely in location and scale, use the coefficient of variation. The CV is calculated as the std dev divided by the mean times 100.
Comment: Clearly correct in what you say.  But here the units of measurement are evidently the same. When that is so the SD having the same units of measurement as the data is not a problem, and indeed is arguably a real feature. The coefficient of variation is most useful whenever  variation is essentially multiplicative, i.e. SD/mean really is approximately constant. That doesn't appear so here.
Comment: The example undermines nothing I said. Indeed the CV is widely used in biology to compare mice and elephants, figuratively speaking. The answer to your question is naturally that there are different measures of variability and people should focus on what helps them most in their problem, but it's really not a good idea if anyone assumes that variability is a single scalar phenomenon to be measured in just one correct way. (If the mean price is 5 and the SD is 10, the stock is probably doomed any way.)
Comment: @NickCox  Thanks for the clarifying edits. Fair point but consider this: prices of two stocks are both valued in dollars (the same unit of measure) but differ widely in their mean and std deviations. So, stock 1 has an average of 500 dollars with a std deviation of 50 while stock 2 has an average of 5 dollars with a std deviation of 10. Which stock has more variability?
Comment: @NickCox  Ha, ha...right you are about stock 2 -- bad example but the main point stands. Suggesting that I suggested that there is "one correct way" to measure variability would, of course, be a misinterpretation of my comment.
Comment: Quite so. That is why I said "if anyone assumes". That cap evidently doesn't fit you, but the broad point remains that "more variability" can't be assigned without deciding on the measure. We don't disagree about any principle, I trust; I sense that you would use the CV much more frequently than I would, but that's a matter of taste and style to some extent.
Comment: @nickcox   Yes, I use it whenever it's appropriate and particularly in evaluating the variability in time series such as equities. It's really quite, quite a powerful, underutilized metric.
Comment: Mileages do vary. I've seen some extraordinary misuses, none with your name on them. For an attempt at even-handedness, see http://stats.stackexchange.com/questions/118497/how-to-interpret-the-coefficient-of-variation
Answer: I would suggest that you need to understand the underlying distribution of the data first before determining what measure of variability to use. So it's not a simple as saying it's "standard deviation".
Certainly for Normal distributions, standard deviation is a good one. I personally use +/- 2 standard deviation for comparison if I do want to find a difference, or +/- 3 standard deviation for comparison if I don't want to find a difference.
Standard deviation as defined for Normal Distribution wouldn't work for Binomial Distribution (coin flipping e.g.). The standard deviation is based on the probability of either event happening.
Std Deviation as defined for Normal Distribution also doesn't work for Poisson Distribution (length of a queue e.g.), though using the central limit theorem, your distribution can approximate a Normal.

Measure the fluctuation of data


how to manipulate data from text to CSV

Question: I have text file data.txt like this:

Name male  female
bayu 1 0
jonson 1 0
anna 0 1

i have try to convert this file using pandas python
import pandas as pd
df = pd.read_fwf('data.txt')
df.to_csv('data.csv')

I want to get results data.csv like this:

Gender name
bayu male
jonson male
Anna  Female

what should i do?
Comment: please accept the answer which best suits you that way it get flushed from the unanswered queue and helps other who are looking for same solution.
Answer: import numpy as np
df['Gender'] = np.where(df['male'].eq(1), 'male', 'female')
df = df.drop(columns={'male', 'female'})
df.to_csv('data.csv', index=False)

    Name    Gender
0   bayu    male
1   jonson  male
2   anna    female

Answer: You can use idxmax() before to save to csv:
df['Gender'] = df[['male', 'female']].idxmax(axis=1)
df = df.drop(columns=['male', 'female'])

Answer: Simply can be done with replace and rename ..
DataFrame:
>>> df
    Name  female  male
0   bayu       0     1
1  jason       0     1
2   anna       1     0

Result:
>>> df.replace({ 0: "male", 1: "Female"}).drop(columns={'male'}).rename(columns={'female': 'Gender'})
    Name  Gender
0   bayu    male
1  jason    male
2   anna  Female

saving dataframe to csv..
>>> df = df.replace({ 0: "male", 1: "Female"}).drop(columns {'male'}).rename(columns={'female': 'Gender'})
>>> df.to_csv('data.csv', index=False)

Your CSV will be looks like below with comma delimited ..
$ cat data.csv
Name,Gender
bayu,male
jason,male
anna,Female

Or, Just with idxmax and df.assign  to assign back the values to nw columns altogether.
>>> df.assign(Gender = df[['male','female']].idxmax(axis=1)).drop(columns={'male', 'female'})
    Name  Gender
0   bayu    male
1  jason    male
2   anna  female

2) Another way around creating a function and then using apply method.
 def setval(col):
   col = str(col)
   if col.startswith('0'):
     return  'Male'
   if col.startswith('1'):
     return  'Female'

>>> df.assign(Gender = df[ 'female'].apply(setval)).drop({"female", "male"}, axis=1)
    Name  Gender
0   bayu    Male
1  jason    Male
2   anna  Female

3) Just for the sake of posterity ..
You can create a dict and then map the dict against one of the column assign the new values to a new column with df.assignfurther dropping the un-desired columns.
Here, yo can use either map or replace method at your ease ..
>>> newVal = {0:'Male', 1: 'Female'}   # dictionary paired with key, values

a) in the below example axis=1 represents the column which is newer method.
>>> df.assign(Gender=df.female.map(newVal)).drop({'male', 'female'},axis=1)
    Name  Gender
0   bayu    Male
1  jason    Male
2   anna  Female

b) while in the below example we are explicitly mentioning it to be applied on the column which is old method.
>>> df.assign(Gender=df.female.map(newVal)).drop(columns={'male', 'female'})
    Name  Gender
0   bayu    Male
1  jason    Male
2   anna  Female

With .replace
>>> df.assign(Gender=df.female.replace(newVal)).drop({'male', 'female'},axis=1)
    Name  Gender
0   bayu    Male
1  jason    Male
2   anna  Female

Comment: Thanks @Joe  for appreciation.

how to manipulate data from text to CSV


Poisson process and queuing system

Question: In a queuing system, the inter-arrival times are known to be exponentially distributed.
My textbook states: "It can be shown that, if the underlying distribution of inter-arrival times { T1, T2, ..., Tn } is exponential, the arrival times are uniformly distributed on the interval (0, T). The arrival times- T1, (T1 + T2), (T1 + T2 + T3), ... , (T1 + ... + Tn) are obtained by adding inter-arrival times."
But isn't the sum of exponential random variables distributed as Erlang? Implying that arrival times should be Erlang distributed?
How are arrival times distributed? Is the distribution of arrival times Uniform or Erlang? Why so? I'd like some clarity on the topic.
Answer: If interarrival times ($IA$) are i.i.d. $IA\sim \text{Exponential}(\lambda)$ then the $n$th arrival time is $S_n \sim \text{Erlang}(n,\lambda)$. as a quick check, you can see that $S_1 \sim \text{Erlang}(1,\lambda)$ which is the same as $\text{Exponential}(\lambda)$.
However, the textbook is giving you more info. Given that the arrival times are on the interval $[0,T]$, they are distributed uniformly on that interval as a consequence of the interarrival distribution (or alternatively, a consequence of a stationary Poisson Process).
Notice the difference between arrival time $S_n$ and this conditional arrival time $(S_n|S_n\in [0, T])$
$$(S_n|S_n\in [0, T])\sim \text{Uniform}(0, T)$$.
This extends to the case $(S_n|S_n\in [t_1, t_2])\sim \text{Uniform}(t_1, t_2)$ with $t_1<t_2$.
See the proof/derivation here on Math.SE.

With Exponential interarrivals, $(S_n|S_n\in [t_1, t_2])\sim \text{Uniform}(t_1, t_2)$ for $t_1<t_2$ doesn't invalidate that $S_n \sim \text{Erlang}(n,\lambda)$.  For a visual example, see two graphs below generated with the same Exponential interarrival times.

MATLAB code:
<code>Rate = .85;
ArrivalNumber = 5;
SampleSize = 50000;
% Add Interarrival Times
S5 = sum((-1/Rate)*log(1-rand(SampleSize,ArrivalNumber)),2);

fh=@(x) ((Rate^ArrivalNumber)*x.^(ArrivalNumber-1).*exp(-Rate.*x))...
    ./factorial(ArrivalNumber-1);  % Erlang PDF
Times = cumsum((-1/Rate)*log(1-rand(20000,50)),2);
IntervalTimes = [];
Interval = [15 35];
for i = 1:size(Times,1)
    Times_i = Times(i,:);
    IntervalTimes_i = Times_i(Times_i >= Interval(1) & Times_i <= Interval(2));
    IntervalTimes = [IntervalTimes; IntervalTimes_i(:)];
end

figure, hold on, box on
title('Event Times on Interval [15, 35]')
h = histogram(IntervalTimes,'Normalization','pdf','DisplayName','Simulation')
xlim([0 40])
p = plot([15 35],ones(1,2)./(35-15),'k-','LineWidth',2.2,'DisplayName','Uniform(15,35)')
xlabel('Arrival Times on Interval [15, 35]')
ylabel('PDF')
legend('show')

figure, hold on, box on
title('S_5 \sim Erlang(5,\lambda = 0.85)')
h = histogram(S5,'Normalization','pdf','DisplayName','Sum of Exponential(\lambda)')
Xrange = 0:.01:25;
p = plot(Xrange,fh(Xrange),'k-','LineWidth',2.2,'DisplayName','Erlang(5,\lambda)')
xlim([0 20])
legend('show')
xlabel('5th Arrival Time, S_5')
ylabel('PDF')
</code>

Poisson process and queuing system


Como instalar LLVM?

Question: Como instalo o clang/LLVM no windows para correr programas C em bash se aparece sempre "failed to find msbuilt toolsets" quando instalo o llvm??
Comment: [MSBuild Toolset (ToolsVersion)](https://msdn.microsoft.com/pt-br/library/bb383796.aspx)
Comment: Não consigo instalar o clang, pelo que percebi essa pagina ajuda a integrar num projeto
Comment: Veja essa [pergunta](https://stackoverflow.com/questions/42696948) feita no **SO** e a [resposta](https://stackoverflow.com/a/42697374/8353154) dela.
Answer: Pode baixar os instaladores para windows em: http://releases.llvm.org/download.html
No caso tem duas versões:

http://releases.llvm.org/5.0.0/LLVM-5.0.0-win32.exe
http://releases.llvm.org/5.0.0/LLVM-5.0.0-win64.exe

Para versões mais recentes acesse o link citado no começo

Note que no momento da instalação irá aparecer a seguinte tela:

Selecione como deixei no exemplo, pode trocar para All Users se desejar, o icone no desktop é opcional, mas pra facilitar pode deixar lá.

Nota: É provável que tenha que instalar o http://landinghub.visualstudio.com/visual-cpp-build-tools, se ainda não tiver instalado.

Depois de instalado pra testarmos apenas, crie um arquivo na pasta que desejar com nome hello.c, então adicione isto nele:
#include <stdio.h>

int main()
{
    printf("Stack Overflow");
    return 0;
}

Então via CMD ou PowerShell navegue até a pasta, algo como:
cd c:\pasta\aonde\salvei\meu\programa

Então digite isto e aperte Enter:
clang hello.c -o hello.exe

Depois no CMD digite isto:
hello.exe

Se tudo correr bem irá aparecer o texto:

Stack Overflow

Exemplo de teste no meu computador:

Como instalar LLVM?


Simple examples of infinite groups containing non-trivial finite subgroups?

Question: What are some non-trivial examples of finite subgroups of infinite groups? An example is simpler to understand than another if the background required to understand the example is less than the other.
I discount the answers here and here because they answer the question: "which infinite groups have all infinite subgroups, or all finite subgroups?". (also the answers aren't particularly simple)
This question is dropping the all, and is dropping the request for infinite subgroups. However, this question is not dropping the triviality condition, as the simplest example I can think of is the trivial example: the sub-group containing only the identity element.
Comment: What are you non-trivial examples ? There might be too many examples. Just take a finite group $\Gamma_1$ and any other group $\Gamma_2$, then the free product $\Gamma_1*\Gamma_2$ contains $\Gamma_1$ as a subgroup.
Answer: The matrix group $M_2(\Bbb{R})$ is infinite, and has a finite cyclic subgroup $C_2$ of diagonal matrices $id,-id$.
Answer: The infinite group $SO(3)$ of all 3d rotations has many finite subgroups, including for example the symmetry groups of solids (e.g., a dodecahedron $\implies A_5$, or a regular prism $\implies D_{2n}$) in $\Bbb R^3$.
Comment: Doe the infinite group of all 2D rotations not have such finite subgroups?


What is the significance of this matrix?

Question: Consider I have a matrix $A$ of dimension $N$$\times$$M$ where $N$>$M$. I am interested to modify $A$ by the equation $A_1=A*C*pinv(D)$, where $*$ implies matrix multiplication, $pinv$ implies pseudo inverse. Matrix $C$ and $D$ are square matrix of size $M$$\times$$M$. Matrix $C=A'*A$ which we can diagonalize as  $\Sigma^M_{i=1} u_i \sigma_i v_i^t$ where $u_i$ are eigenvectors and $\sigma_i$ are corresponding eigenvalues. Now we derive matrix $D$ by the largest $k$ eigenvalues of $C$, $D = \Sigma^k_{i=1} u_i \sigma_i v_i^t$.  My question is what can we infer of $C*pinv(D)$? Is there any significance of this matrix, if such an operator is used anywhere? Any comment or suggestion or link for further reading would be highly appreciated.
Comment: Does it honour $P^2 = P$?
Answer: Numerical evaluation shows that the $A_1 = \Sigma^k_{i=1} u_i \sigma_i v_i^t$,  where $u_i$ and $v_i$ are left and right singular vectors of $A$ and $\sigma_i$ are the singular values. I think it an be proved as well, but I have not tried. I am writing answer instead of deleting the post as it may be helpful to someone.


'Undefined' JSON data

Question: This is my first time using JSON. I have an external json file and I am using the json file in my html file to create divs with every json data read. I can get the correct output for all of the values, except for one which is the 'descript' value. I have tried printing it to the cosole log but all I'm getting for that particular value is 'undefined' but the other values are alright. Any idea on why this happened?
JSON file:
[{"title":"3G","filePath":"https://example.com","descript":"hello world"}, {"title":"4G", "filePath":"https://example.com", "descript": "test"} ]

HTML file:
 $.ajax({
      url : "testJSON.json",
      type : "get", // whichever you like
      contentType:"json",
      success : function(list)
        {
          var divCol  = "
";
          var divWell = "
";
          var divClose= "
";

          list.forEach(function(obj, index) {

            var title     = "
"      + obj.title    + "
";
            var desc      = "
"       + obj.descript + "
";
            var linkStart = "";
            var linkEnd   = "CSV";
            var div = divCol    +
            divWell     +
            title       +
            desc        +
            linkStart       +
           // image       +
            linkEnd +
            divClose +
            divClose;

              console.log(list)
           $("#imdaFiles").append(div); // insert the div you've just created

           })
        }
    });

Comment: there is an error in the JSON file in "descript:" "test" as double quotes are after the colon. It should be  "descript": "test"
Comment: @DavisMolinari sorry, that was not part of the original code. The problem still persists
Comment: @nurul98 I m not sure, i  think its JSON string .Try with `list=JSON.parse(list)` on  success function 1st line
Comment: @prasad nope that did not work
Answer: Just a short notice that might solve the issue.. you misplaced a double quote on the right side of the last descript key in your JSON.
Comment: that typo error is not part of the original code so the problem still persists

'Undefined' JSON data


HAProxy path regexp based on map lookup

Question: I'm using HAProxy 1.8.14 on a server running Debian stretch.
I want to route requests to different backends for a certain set of domains but only for some specific paths. Since there's quite a few domains and varying paths allowed I think a solution using maps would be nice.
I've tried to use a map to lookup a backend based on hdr(host) with the condition that the path should match with a regular expression mapped to hdr(host). I've tried the following but I can't get it to work:
use_backend bk-%[hdr(host),lower,map_dom(/etc/haproxy/host_to_backend.map,bk_default)] if { path_reg %[hdr(host),lower,map_dom(/etc/haproxy/domain_path_whitelist.map)] }

Example of host_to_backend.map:
a.foo.org a
b.foo.org b
c.foo.org c

Example of domain_path_whitelist.map (regexps not tested):
a.foo.org ^/(yada|info)/.*$
b.foo.org ^.*$
c.foo.org ^/bar/.*$

To avoid the regexp complexities I've also tried a 'beg' alternative:
use_backend bk-%[hdr(host),lower,map_dom(/etc/haproxy/host_to_backend.map,bk_default)] if { path_beg %[hdr(host),lower,map_dom(/etc/haproxy/domain_path_whitelist.map)] }

...but no luck.
Is it possible to solve my specific problem using maps? If not, can you suggest an alternative solution?
Comment: The alternative solution would be haproxy acls https://cbonte.github.io/haproxy-dconv/1.8/configuration.html#7 I can grab some examples of how to do this. Also, curious if it can be done with maps
Comment: Yup, I'd really prefer not to use acls however. Map files are so much easier to maintain, especially with hundreds of domains.
Comment: What you're doing with maps looks reasonable (not sure if the duplicative `bk` is intentional in the backend names). What about it isn't working?
Comment: The duplicate `bk` is just a typo. Well I just can't get the path to match with the regexp looked up from the map. The first part which looks up the backend name works well but I can't get the condition become true.
Comment: (I've fixed the typo now)
Comment: I'm going to test this in my local today and see if I can't help you out
Comment: Thank you @jmoney for trying to help
Comment: You're welcome! Wish I had a better answer for you.
Answer: I found the HAProxy community and asked there too. I'll probably go with the map_reg variant:
use_backend bk-%[base,lower,map_reg(/etc/haproxy/base_to_backend.map,bk_default)]

Answer: This cannot be done the way you are attempting because log format variables referenced %[] cannot be used in the acl match context. The path_beg acl is trying to literally match %[hdr(host),lower,map_dom(/etc/haproxy/domain_path_whitelist.map)].
Also note, to match a regex on the path, you'd want to use path_reg
An alternative solution, is to use an acl without an attempt to map in a variable, for example:
use_backend bk-%[hdr(host),lower,map_dom(/etc/haproxy/host_to_backend.map,bk_default)] if { path_req ^/(yada|info)/.*$ }

Comment: Do you know of any other way to compare the path with a regexp loaded from a map file?
Comment: I'm reading the following in the documentation and can't help but wonder it may be part of a solution:

>>  The "-M" flag allows an ACL to use a map file. If this flag is set, the file is
parsed as two column file. The first column contains the patterns used by the
ACL, and the second column contain the samples. The sample can be used later by
a map. This can be useful in some rare cases where an ACL would just be used to
check for the existence of a pattern in a map before a mapping is applied. <<

but I'm not sure I fully understand the concept described. What do you think?
Comment: Hmm, yeah I missed that, it does look like it could work? My only concern is if it is going to expect all to match "The "-f" flag is followed by the name of a file from which all lines will be
read as individual values" i'm playing with it now
Comment: Have you figured out how to use it? the -M flag, looks like that in combo with -f
Comment: Yeah as I read it you use -f and then -M to use the file as a map file but I don't understand how to apply "The first column contains the patterns used by the
ACL, and the second column contain the samples. The sample can be used later by a map." So, no, I haven't figured it out yet :(

HAProxy path regexp based on map lookup


How can I programmatically extend the registration form?

Question: I'm trying to extend registration form programatically to add an additional field. I was following an example from Extend _form in Drupal 8

I created a new service that will redirect to the new form.
I created a route subscriber to that will return the new form.
I created a form class that will extend the form.

It works for the user login form but wont work on user registration form because it is an _entity_form and I guess it should be treated differently. I changed the router subscriber to match registration form but i get this error.

Call to a member function getEntityTypeId() on null.
if ($route = $collection->get('user.register')) {$route->setDefault('_entity_form',
'\Drupal\form_overwrite\Form\NewUserLoginForm');

Can anyone suggest please what's the right way to extend this form?
I tried with the all known hook_form_alter but even like that I'm not sure how to save the new field content in the configuration files.
Comment: To add a new field to an entity form you simply add this field to the entity. For the entity User you find this here /admin/config/people/accounts/fields. If you want to design a separate register form enable the form mode Register on Manage Form Display at the bottom of the page in Custom Display Settings.
Answer: I had this same question and it doesn't seem to be answered anywhere so I'll give the answer that I figure out, hopefully this helps someone.
First you need to add the form to the entity do this in the module file in hook_entity_type_build something like the following sample:
/**
 * Implements hook_entity_build
 */
function MYMODULE_entity_type_build(array &$entity_types) {

  $entity_types['user']->setFormClass(
    'MYMODULE_register',
    'Drupal\MYMODULE\Form\myModuleRegisterForm'
  );
}

Create a new class src/Form/myModuleRegisterForm.php to extend RegisterForm:
namespace Drupal\MYMODULE\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\user\RegisterForm;

class myModuleRegisterForm extends RegisterForm {

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {

    $form = parent::form($form, $form_state);

    // Change the form here

    return $form;
  }

}

Then load it in your controller
public function myModuleRegister {

  $build = [];

  $entity = \Drupal::entityTypeManager()
    ->getStorage('user')
    ->create([]);

  $formObject = \Drupal::entityTypeManager()
    ->getFormObject('user', 'MYMODULE_register')
    ->setEntity($entity);

   $build['form'] = \Drupal::formBuilder()->getForm($formObject);

   return $build;
}

That pretty much covers it, obviously you need the route to point to the appropriate function in your controller but otherwise this should do it.  Hope it helps.
Answer: The other answers focus on overwriting the route itself. If you don't care about changing the route, but just want to change the form markup, here's how to do it:
In your .module file:
function your_module_entity_type_alter(array &$entity_types) {
  $entity_types['user']->setFormClass('register', '\Drupal\your_module\Form\NewRegisterForm');
}

This overrides the Annotations set in User.php
*   handlers = {
 *     "storage" = "Drupal\user\UserStorage",
 *     "storage_schema" = "Drupal\user\UserStorageSchema",
 *     "access" = "Drupal\user\UserAccessControlHandler",
 *     "list_builder" = "Drupal\user\UserListBuilder",
 *     "views_data" = "Drupal\user\UserViewsData",
 *     "route_provider" = {
 *       "html" = "Drupal\user\Entity\UserRouteProvider",
 *     },
 *     "form" = {
 *       "default" = "Drupal\user\ProfileForm",
 *       "cancel" = "Drupal\user\Form\UserCancelForm",
 *       "register" = "Drupal\user\RegisterForm"
 *     },
 *     "translation" = "Drupal\user\ProfileTranslationHandler"
 *   },

The one we're interesting in is the "register" key under "form".
Then in your NewRegisterForm.php you don't need to implement __construct or create, as these are inherited from RegisterForm. In my case, I just wanted to change the submit button text, so I have:
class NewRegisterForm extends RegisterForm {

  protected function actions(array $form, FormStateInterface $form_state) {
    $element = parent::actions($form, $form_state);
    $element['submit']['#value'] = 'Submit';
    return $element;
  }

}

Hope that helps, took me a while to find why overwriting the route didn't do what I wanted.
Comment: Thank you for the perfect answer, how can I modify fields on the form?
Answer: I had problem with my class throwing an error :
Error: Call to a member function getEntityTypeId() on null in Drupal\Core\Entity\EntityForm->getBaseFormId()

Then I found this code and I think that for Entity form you have to add __construct and create functions:
<?php
namespace Drupal\my_module\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\user\Entity\User;
use Drupal\user\RegisterForm;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a user register form.
 */
class NewUserRegisterForm extends RegisterForm {

    public function __construct(EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, ModuleHandlerInterface $moduleHandler) {
        $this->setEntity(new User([], 'user'));
        $this->setModuleHandler($moduleHandler);

        parent::__construct($entity_manager, $language_manager, $entity_type_bundle_info, $time);
    }

    /**
     * @inheritdoc
     */
    public static function create(ContainerInterface $container) {
        return new static(
            $container->get('entity.manager'),
            $container->get('language_manager'),
            $container->get('entity_type.bundle.info'),
            $container->get('datetime.time'),
            $container->get('module_handler')
        );
    }

    public function form(array $form, FormStateInterface $form_state) {
        $form = parent::form($form, $form_state);
        $form['test'] = [
            '#markup' => '
Test extended form
',
        ];
        return $form;
    }
}

Code from David Duymelinck Thanks to him.
Comment: This code is a hack to use an entity form in `_form` of a route. Normally you use it in `_entity_form` and then the entity is provided by EntityForm:getEntityFromRouteMatch().
Comment: I do not know if it's related but now if I try to create a new account I have an error: Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'uuid' cannot be null: INSERT INTO {users} (uid, uuid, langcode) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array
(
    [:db_insert_placeholder_0] => 175
    [:db_insert_placeholder_1] =>
    [:db_insert_placeholder_2] => fr
)
 in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 805)


Finding the probability from a Poisson distribution.

Question: The number of visitors in a museum  during a fixed time interval follows a Poisson distribution.
Assume that the mean rate of visitors is 2 per minute.
Find the probability that there will be exactly 20 visitors in the next 5 minutes.
What I did was first to calculate the mean rate for every 5 minutes which gives me the following:
mean rate for every 5 minutes = (2/min) x 5 min = 10
$$P(X=20)= e^{-10}\frac{10^{20}}{20!}
$$
I am not 100% certain if my solution is correct, so can someone help me?
Answer: Your answer is correct. The standard formula gives us that the probability of $k$ events in an interval in which there are $\lambda$ events on average is
$$
P(X=k)= e^{-\lambda} \frac{\lambda^k}{k!}.$$
In your case, since you have an average of $2$ visitors per minute, we can conclude that you will have $2\cdot5=10$ visitors on average in any $5$ minute interval.  So, using $\lambda=10$ and $k=20$, we have that the probability of exactly $20$ visitors in a $5$ minute period is
$$
P(X=20) = e^{-10} \frac{10^{20}}{20!} \approx 0.0018660... .$$


Is it possible to create sub-node that can assign on a node?

Question: The idea is to create a chapter(sub-node) that can assign on a book(node:page). I know this can be done with taxonomy, but I dont wanna have 1000+ of taxonomy on my site. Is it possible, if so, how?
Comment: Are you using Drupal 7?
Comment: yes, its a Drupal 7 site
Answer: Do you mean you want a Book that has chapters and you want them linked like a book? There is a module for that in Core called book. https://drupal.org/documentation/modules/book
Comment: I didnt even know Drupal has such feature on default module. TY


Create a list with the common elements of other lists in C#

Question: I know how merge lists, but if I only want the common objects?
Example:
List A={1,2,3,4,5}
List B={4,5,6}

I know how to create this list
List C={1,2,3,4,5,4,5,6}

but I want to create this
List C={4,5}

Comment: What language is this? It doesn't look like C#.
Answer: With Linq and the Intersect method:
C = A.Intersect(B)