O que significa o ponto de exclamação após o nome de um tipo?

Question: Tenho visto muito em Kotlin tipos marcados com um ponto de exclamação ao final. Principalmente quando uso API's do Java.
Algo como
CharSequence!

O que isso significa?
Answer: Indica que o tipo pode ser nulo. Em Kotlin os tipos não podem ser nulos por padrão. Mas em Java eles podem, pelos menos os tipos por referência. Então precisa ter um tipo que permita interoperar com essa condição do Java.
A linguagem precisa respeitar a decisão do Java e por isso para dar mais robustez ao seu próprio código e até ganhar performance é necessário informar para o compilador quando há uma exceção à regra da garantia de não nulo.
Não use se não for interoperar com Java. Na verdade em boa parte dos casos é melhor que tenha uma forma de garantir o tipo não nulo (alguma operação que verifique manualmente e possa garantir que essa condição não mudará para nulo depois) e usar um tipo comum.
Ainda existe o indicador de tipo ? que faz o tipo sempre aceitar nulo e aí você tem que verificar o estado sempre antes de usar. O ! não. Ele permite você entender que ele não é nulo no seu código Kotlin, mas ainda interoperar em Java. Antes de acessar em Kotlin precisa garantir que ele é válido, mas não depois. O ? permite você anular o valor, o ! não permite fazê-lo.
Existem casos que o Java pode dar pistas para inferir melhor a situação possível e já garantir que não é nulo. Existem casos que o tipo é inferido como ! (possivelmente anulável).
Não tenho experiência suficiente, mas acho que se o dado é mutável e compartilhado com Java, dá na mesma porque você não pode nunca confiar nele em ambientes concorrentes ou que você passe o controle da execução para o Java no meio do processamento. E em OOP é fácil fazer isso sem perceber.
Ele acaba fazendo parte do tipo de dado. De fato, em Kotlin é chamado de tipo de plataforma (em alusão ao Java como plataforma). Eu precisaria pesquisar mais para entender se o compilador insere verificação em runtime ou não para garantir que saída para o Java garantem alguma verificação. Faz sentido fazer assim, mas não posso garantir e superficialmente não achei nada, então só fazendo um teste extensivo.
Documentação.
Existe o operador !! que força um erro de NullPoiterException se o objeto for nulo neste caso (colocando como curiosidade).
Há algo semelhante no C# 8, mas ele dá garantia que não é nulo. Algumas pessoas gostam de chamá-lo de dammit operator.
Coloquei no GitHub para referência futura.
Comment: Se tentar colocar um nulo em um tipo  dá erro de compilação. Com uma exclamação no tipo permite o nulo e pode gerar a exceção, duas exclamações não é que permite exceção, gera a exceção se for nulo, porque este é um operador. Existe ainda o `?` que define que o tipo pode ser anulável. Veja https://kotlinlang.org/docs/reference/null-safety.html#nullable-types-and-non-null-types. Eu vou complementar a resposta pra ficar mais completo e claro que esta anotação da exclamação ter a ver com iteroperabilidade.
Comment: Duas interrogações lança uma Exception, no caso de apenas uma interrogação, o que acontece se uma variável receber um valor nulo em tempo de execução, não sendo possível prever em tempo de compilação?
Comment: @GuilhermeCostamilam lança uma exceção, o corre igual ao Java. Note que o `!!` é um operador, veja a outra pergunta lá pra entender, escrevi sobre ele mais pra introduzir a outra pergunta. Ainda há dúvida ou ficou claro?
Comment: Então nenhuma exclamação é erro de compilação, uma exclamação permite nulo e pode gerar uma exceção durante a execução, duas exclamações permite nulo mas gera uma exceção em execução equando isso ocorre, entendi certo?
Comment: @GuilhermeCostamilam veja se melhorou. Melhorou o meu entendimento.
Comment: Com a sua edição e a outra resposta acho que entendi, valeu
Answer: Significa que você está lidando com um platform type: um tipo vindo do Java que pode ou não ser nulo.
O compilador do Kotlin sempre tenta buscar por anotações de nulabilidade e inferir se um tipo pode ser nulo ou não.
Caso as anotações estejam presentes, o tipo será representável como nullable (CharSequence?) ou não (CharSequence).
Caso as anotações não estejam presentes, ele será considerado um platform type (CharSequence!). Na prática isso significa que as checagens de nulo serão menos restritas. Quando métodos são chamados nestes tipos, não são gerados erros em tempo de compilação, mas podem acontecer erros em tempo de execução devido à checagem de nulos que o Kotlin gera para não propagar nulos.
Considere a seguinte atribuição, interoperando com o Java:
// Chamando código Java sem anotação de nulabilidade
val resultWithPlatFormType = myJavaObject.myJavaMethod()

A seguinte chamada pode gerar um erro em tempo de execução:
// Lançará uma exceção caso resultWithPlatFormType seja nulo
resultWithPlatFormType.trim()

Note que os platform types não são "anotáveis", ou seja, a linguagem não tem uma maneira formal de representá-los de forma explícita.
Quando um destes tipos é atribuído a uma variável Kotlin, podemos usar a inferência de tipos ou escolher de forma explícita como queremos tratar o valor:
val inferred = myJavaObject.myJavaMethod() // Será inferido como platform type
val notNull: String = myJavaObject.myJavaMethod()  // Funciona, mas pode falhar em tempo de execução
val nullable: String? = myJavaObject.myJavaMethod() // Sempre funciona


Initialize struct with byte array based on header type

Question: I have several structs in my file. I want to pass a byte array and initialize the the struct. I have tried struct to byte array conversion and vice versa but I am getting errors like "Attempted to read or write protected memory". So I thought passing byte array to initialize the struct might be more efficient. My problem is that one part of structure is dependent of first byte type. So how do I assign another structure to it?Here is the code:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct Payload
{
    MessageTypeID ID1;
    MessageTypeID ID2;
    Byte[] PayloadData;

    public Payload(byte[] payloadArray)
    {
        this.ID1 = (MessageTypeID) payloadArray[0];
        this.ID2 = (MessageTypeID) payloadArray[1];

        switch (this.ID1)
        {
            case MessageTypeID.TEMPERATURE:
                byte[] tempPayloadArray = new byte[payloadArray.Length - 2];
                Temperature temp = new Temperature(Array.Copy(payloadArray,2,tempPayloadArray,0,payloadArray.Length - 2);
                this.PayloadData = temp;  <== ERROR: Cannot convert Temperature to byte[]
                break;
            case MessageTypeID.LEVEL:
            .......
        }
    }
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Temperature
{
    Byte LocalTemp;
    Int16 ExternalTemp1;
    Int16 ExternalTemp2;

    public Temperature(byte[] tempArray)
    {
        this.LocalTemp = tempArray[0];
        this.ExternalTemp1 = tempArray[1];
        this.ExternalTemp2 = tempArray[2];
    }
}
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Level
{
    Byte FluidLevel;
    Int16 FreshnessTimer;
    Byte CalibrationChannelReading;

    public Level(byte[] levelArray)
    {
        this.FluidLevel = levelArray[0];
        this.FreshnessTimer = BitConverter.ToInt16(levelArray, 1);
        this.CalibrationChannelReading = levelArray[3];
    }
}

Comment: Please, do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose.
Comment: Why do you want to assign temp to PayLoadData? If your requirement is that you need a new byte array that needs to be assigned to PayLoadData which represents the structure data then have the Tempearate and other structure return byte array that it represents. (You can keep this in base class if all structs do that)
Comment: I want to structure the array such that if the payload is of type e.g. temperature, then bytes are organized in the right fields.
Answer: Don't roll your own: Use the infrastructure .Net and the CLR provides for you.
If you need to interoperate with unmanaged code, you need to learn about P/Invoke and marshalling:

http://msdn.microsoft.com/en-us/magazine/cc164193.aspx
http://www.codeproject.com/Articles/66245/Marshaling-with-C-Chapter-Introducing-Marshaling
http://msdn.microsoft.com/en-us/library/sd10k43k(v=vs.110).aspx
http://blogs.msdn.com/b/dsvc/archive/2009/02/18/marshalling-complicated-structures-using-pinvoke.aspx

If you are interoperating with other managed code, you need to read about serialization. More at

http://msdn.microsoft.com/en-us/library/ty01x675(v=vs.110).aspx

You can use stock binary serialization, or roll you own. In either case, you want your object to be ISerializable, which requires just one method be implemented for serializing your object into a bytestream: void GetObjectData( SerializationInfo info , StreamingContext context ).
And [implies] a corresponding constructor for deserializing the bytestream back into an object instance having the signature constructor( SerializationInfo information, StreamingContext context).
For example:
[Serializable]
public class Widget : ISerializable
{
  public Widget( SerializationInfo serializationInfo , StreamingContext context )
  {
    // your rehydration logic here
  }
  public void GetObjectData( SerializationInfo info , StreamingContext context )
  {
    // your dehydration logic here
  }
  ...
}

Comment: I accpeted this as answer because although I didn't exactly do what you suggested I did use Serializable and OptionalField in my struct to make it work the way I wanted. Thank you!


Why cast zero to pointer type and access its member work?

Question: Recently, I saw something cool.
#define container_of(ptr, type, member) ({                      \
const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
(type *)( (char *)__mptr - offsetof(type,member) );})

Basically, I know what this macro does. But there is one thing I can't understand.
Why can we cast 0 into pointer type ( struct ) and access its member ? I have already referenced some similar stuff and 0 seems like a null pointer. So why can we cast a null pointer and then access its member?
Comment: I'll leave the full answer to the language lawyers, but the short answer is: We don't access its member. We only take the `typeof` that member. This is a compile-time operation that doesn't do any pointer dereferencing at runtime.
Comment: The term you are looking for is *unevaluated context*.
Comment: BTW, the nonstandard parts of the macro, which facilitate a type check, aren't even needed. The typecheck can be done perfectly within the bounds of the standard: `((type *)( (char *)(0? &(type){0}.member : (ptr)) - offsetof(type,member) ))`
Comment: This link has a good explanation https://radek.io/2012/11/10/magical-container_of-macro/
Answer: typeof is a gcc extension.  The result of this operator is a type name.
This works because its operand is evaluated at compile time, so a pointer need not actually point to anything.  The documentation for this operator states:

The operand of typeof is evaluated for its side effects if and only if it is an expression of variably modified type or the name of such a type.

Since a struct can't contain a variable length array, this guarantees typeof( ((type *)0)->member ) will not be evaluated at run time, so this is a safe operation.
Answer: In the code sample you list, it is casting 0 within typeof.  But typeof is not a function, it is an operator.  You are accessing the type of it and not the value of it.
Reference this question:  Why is typeof null "object"?


Can't get array key to work with Swagger

Question:   /**
  * @SWG\POST(
  *   path="/visa-entry/calculate",
  *   operationId="visaEntryCalculate",
  *   tags={"Visa Entry"},
  *   summary="Calculate the price for an array of visa entries",
  *   description="Calculate the price for an array of visa entries",
  *
  *   @SWG\Parameter(
  *     name="entries",
  *     in="body",
  *     description="Visa Entry IDs to calculate to total price",
  *     required=true,
  *     @SWG\Schema(
  *       type="array",
  *       @SWG\Items(type="number")
  *     ),
  *     collectionFormat="multi"
  *   ),
  *
  *   @SWG\Response(
  *     response=200,
  *     description="OK"
  *   ),
  *
  *   @SWG\Response(
  *     response=400,
  *     description="Bad request"
  *   ),
  * )
  *
  * Calculates a visa entry
  */

What I'm trying to do is to receive an Array of numbers with the key entries.
entries: [1, 2, 3]

This docblock renders the following CURL.
curl -X POST "app.test/visa-entry/calculate" -H "accept: application/json" -H "Content-Type: application/json" -H "X-CSRF-TOKEN: " -d "[0]"

How can I get it to send the array with the key entries?
Comment: 1) By "docblock" do you mean Swagger UI? Can you post an image? 2) Do you need to post as JSON `{"entries": [1, 2, 3] }` or as form data `entries=1,2,3`?
Comment: 1) It's the code I posted. Annotation/docblock, whatever you call it. 2) JSON
Answer: If you don't want the client to post the array directly in the request-body, but with a key, you'll need to specify the in=body parameter as type="object" and define the array as a property of that schema.
Like this:
/**
 * @SWG\POST(
 *   path="/visa-entry/calculate",
 *   operationId="visaEntryCalculate",
 *   tags={"Visa Entry"},
 *   summary="Calculate the price for an array of visa entries",
 *   description="Calculate the price for an array of visa entries",
 *
 *   @SWG\Parameter(
 *     in="body",
 *     name="json",
 *     description="Visa Entry IDs to calculate to total price",
 *     required=true,
 *     @SWG\Schema(
 *       type="object",
 *       @SWG\Property(
 *         property="entries",
 *         type="array",
 *         @SWG\Items(type="number")
 *       )
 *     ),
 *     collectionFormat="multi"
 *   ),
 *
 *   @SWG\Response(
 *     response=200,
 *     description="OK"
 *   ),
 *
 *   @SWG\Response(
 *     response=400,
 *     description="Bad request"
 *   ),
 * )
 *
 * Calculates a visa entry
 */

Comment: Is there any way to initialize array as [1, 2, 3]? Right now it is [0].
Comment: property example causes error, `example=[1, 2, 3]`
Comment: You'll need to use doctrine's array notation {1,2,3} https://zircote.github.io/swagger-php/guide/annotations.html#arrays-and-objects


Apache POI - Allow calculation with time strings

Question: I have an excel sheet which contains some time values. These values are Durations formatted to Strings. When I try to do a sum on those values I get a 0 as result since Excel can't do sums on Strings. When I hit the enter button in the formula bar it does become a time so I the sum works. How do I change the value of the cell from a String to a time value? I already have the date format set up as [hh:mm] with a DateFormat
I start with an amount of time converted into seconds which I convert into a Duration
Duration clockedDuration = Duration.ofSeconds(clockedSeconds)

Then I format the Duration to a String using DurationFormatUtils
DurationFormatUtils.formatDuration(duration.toMillis(), "HH:mm", true)

Then I set the cell value to the String that was just made
c.setCellValue(clockedDuration)

I then set the CellStyle to one that has a DataFormat that's set up as [hh]:mm
Lastly I make a sum of all the values in that particular column (B in this instance)
c = r.createCell(i++)
c.setCellFormula("SUM(B2:B" + lastRow +")")
c.setCellType(Cell.CELL_TYPE_FORMULA)

This is the sheet before I hit the enter button on the first value in the row (09:52)

This is the sheet after I've hit the enter button in the formula bar
Comment: you may want to show some code here. How do you "try do a sum on those values"? Using setCellFormula()?
Comment: I've added my code to the OP
Comment: Are you using the XSSF-Classes (XSSFCell, XSSFWorkbook, ...)? If not, try those :)
Comment: Yes, I'm using the SXSSF variant.
Comment: Try cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
Comment: No luck with that I'm afraid
Answer: I fixed it by dividing the seconds by 86400 to get a decimal value and then use [hh]:mm as the data format.
c.setCellValue(clockedSeconds / 86400)  // 24(hours) * 60(minutes) * 60(seconds) = 86400

Answer: Just a hunch: you mentioned to actually use formulas, and if you are hitting enter on the sheet "the sum works". Applying formulas is a two-liner, in addition to set the "value", you also need to format the cell type as being a formula:
XSSFRow row = worksheet.getRow(rowIndex);
XSSFCell cell = row.getCell(cellIndex);
cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula("sum(start:end)");

Edit: You also will want to make sure to use the XSSF-Classes
Comment: I have it set as a formula cell. I'll edit some code into the OP.
Comment: I've tried that but no luck. With hitting enter I mean that when I hit enter in the formula bar on the time value the formula works. I'll edit screenshots into the OP to make it more clear.
Comment: I've added screenshots now.


Visual Studio Team Services Import History

Question: We are looking to migrate Visual Studio Team Services from on premise TFS 2010. However we have a large amount of value in our checkin history and work item history. We are looking to migrate our local history to Visual Studio Team Services.
I have seen some older posts (which cause various issues like loss of actual dates) but can't find any clear answers for updated service.
Is it possible to import local TFS checkin/history etc into Visual Studio Team Services (formerly Visual Studio Online, Team Foundation Service)? If it is possible is there a guide anywhere?
Answer: UPDATE:
There is a free migration utility for migrating data from TFS 2010+ on-premises (and other systems) to Visual Studio Team Services available from OpsHub: http://aka.ms/OpsHubVSOMigrationUtility.  Microsoft has promoted this tool on the Visual Studio ALM blog and in other places.  It's probably the path of least resistance.
Original Answer:  The most official tool for this process is called the "TFS Integration Platform."  You can find it at codeplex and the Visual Studio Gallery.
It's a very temperamental application (not very polished), but will get the job done.  We recently migrated VCS, about 18000 Work Items, and 1000 Test Cases from TFS 2010 on-prem to Visual Studio Team Services.  We had about four years of history, of which all migrated.  It is true that you lose the checkin/change timestamp, but the change history include the original date in the comment.
Best suggestion is to practice the migration to a sandbox/temp Visual Studio Team Services account so that you can get the .xml configuration exactly they way you need it and then do it "for real."
Notice, the tool will create a rogue workspace on the machine you are doing it from (in some cases).  If you do this from a development machine, you'll likely notice some of your mappings got lost when you go back into Visual Studio.  Just keep an eye out for that.
Part of the download for the Integration Platform tool includes some guidance papers on how to plan, test, and execute the move.  I'd suggest combining that documentation with blog posts for best real-world results.
Comment: How long did it take to do that migration? It's impractically high in my case.
Comment: It was 80+ days in my case. :-( We ended up doing a manual move which made us lose our history.
Comment: Migrating from TFS 2008 requires first a migration to 2010 via TFS Integration Platform and then subsequently using the OpsHub tool
Comment: @MikeCole, about 10 hours IIRC.


Learning Guitar by hearing

Question: I am a little worried/tired of learning guitar by, note-by-note, scale-by-scale learning process.
I have the basic figured out. Now, I was wondering if there is a way/process/resource to learn playing guitar using only sound/hearing skill.
Answer: First of all -- I'm sorry to say -- be prepared to learn that this simply isn't within your capability. Some people simply don't have the ability to discern relative pitch. I don't know for sure whether it's something you're born with, or something you learn as a child -- but there are people who can't answer the question "is the first note higher, lower or the same as the second note?".
Test yourself, or get a friend to test you. If you can hear whether a note is higher, lower or the same as another note, you're set to start learning to play by ear. Being able to hear how much higher or lower is something you can develop; and you will learn it by following the process below.
Start with a simple tune you know well. "Twinkle Twinkle Little Star" is a good example.
Pick a starting note arbitrarily, and play it. Now get a good idea in your head (or by singing) of what the next note should sound like. Find that note on the fretboard. Now you know where it is, play the first two notes of the tune from the beginning. Do the same to find the third note, and so on.
At the same time, find the notes on multiple strings, and think up ways to play the tune making the most efficient use of all the strings, and minimum hand movement. This will teach you to know the various ways you can play the same note.
By the time you've done this for a few tunes, you should begin being able to find the notes faster, because you will be learning what various intervals sound like, and how those intervals relate to relative finger positions on the fretboard.
Another thing you can do is, rather than pick a starting note arbitrarily, choose a key, strum the root chord of that key, hum the first note of the tune, find that note on the fretboard, and pick out the tune from there. Then try it in a different key, to see how you would (sometimes) finger it differently.
Stick to the common guitar keys such as E, A, G, C, D. At first, feel free to use open strings -- but later, challenge yourself to work out a tune using only fretted notes. A tune played in that way is "movable" - you can play it in a different key just by moving your hand up or down the fretboard.
If you get really stuck, it's possible you've chosen too difficult a tune. Shelve it, try something simpler, and come back to it when you've developed more skill.
Comment: My feeling on relative pitch is that it's learned ( unless you're born with perfect pitch ) .... There was a time that I listened to Guns n' Roses "Don't Cry" so much that I could always hear the "G A" ( in GnR downtuning of course .. so F# G# ) in my head...and 9 times out of ten if I sang it in my head and then grabbed my guitar and played it I was right.  Very cool.

Also, I had a bass student that on his first lesson after playing for a short while could not hear the difference between up/down/high/low..... and now years later he's teaching an ear-training course at a local college. cool
Comment: I would downvote this if I could. While there it is much easier to develop the fabled "perfect pitch" for people who started before 11 years of age, relative pitch is a skill that can be trained at any age.
Answer: When I started playing bass and guitar in '89 there was no internet/youtube/guitar learning websites, etc, or cheap tools to slow the music down and keep it at pitch... and I was dead broke ... I couldn't afford to go buy guitar/bass transcription books... I had a cheap electric bass...and an acoustic guitar... and a cassette player .. I used my ear.... and as Dr Mayhem says ... listen to a note or a passage ... play...adjust ... until you have it right...
I'm not going to lie..it CAN be terribly difficult and frustrating at first...you're training your ear to hear music... that's no small feat.  Set yourself up for success...not failure... don't go try to learn a Stevie Ray Vaughan live guitar solo... try to learn something simple... start with a three chord tune "Mary Janes Last Dance" .. or Sweet Home Alabama .. or a simple country song ( Did I just say that out loud? ).
An artists first piece of work is not usually his masterpiece.  Start with the basics and soon you'll be hearing songs on the radio, picking up the guitar 2 hours later and playing it from just what you heard. Seriously.  The first time I saw someone do that I thought it was some black magic.  But I'll never forget the first time I did the same thing - without thinking about it. Fantastic!
Good luck
Comment: it's a journey... I'm going to use bass as an example .. let's take a simple G chord that has three notes repeated in any order ( G, B, D ) ... and the bass player is playing a G note....  you're trying to find the right note that is being played...so you start playing random notes and you land on a D note..  to an untrained ear it might sound absolutely correct ( because it is part of the chord ) .. but as you get more experienced and your ear grows in its ability you'll be able to quickly hear that you're playing the "wrong" note.
Answer: This is pretty much a positive feedback exercise with yourself. There are tools that slow down music while keeping the pitch the same which can make it easier to identify notes in a rapid section, but it boils down to the following:

listen to the note
play a note
adjust until they sound the same

It's how a large majority of guitarists learn. Some never use formal techniques, but do it all by ear.
I would suggest that learning solely like this makes it harder for you to develop certain skills, or to gain an awareness of how different modes or musical themes could fit in with a song, but you should be able to gain a lot of experience anyway.
Answer: I would highly recommend taking some lessons and learning some theory - even if you do become an amazing musician simply by listening, once you begin playing with others, if you don't understand that a guitar is tuned to "E" but the people you are playing with are tuned down to "E flat" to accomodate a singer's low voice, for example, and are playing in the key of "C Sharp" on detuned instruments, with a key change at the chorus, the people you are playing with will have a harder time explaining what's going on to you, you'll have a harder time understanding them, and the disconnect could strain your ability to play effectively with said musicians, or others.
Think of scales as a series of patterns that show you where the right notes are in a certain key. Once you learn the patterns of these scales (there are 8 modes - learn them in one key and you can transpose them onto other keys by sliding your hands to the correct frets) and you'll be able to play any solo in any key. It's not as hard as you think, and the discoveries you'll make along the way ("hey, that scale looks exactly like this chord but with a few extra notes attached!") will make you a better musician.
Answer: Do you like improvising or improvisational music? Jazz/blues/prog fan?
I've been playing guitar for years by just improvising and it's helped me develop my ears a great deal.
If you practice like that all the time, like anything you'll get better at it. You can end up developing your ears and a sort of intuition about what to do on guitar to get the sounds you want.
You still need to spend some time learning theory and practising scales and whatnot so you don't forget what you've learned.
But if you also just spend a lot of time just "jamming", then you'll get better at that also. It depends on what your personality is, and taste in music, as to whether you like that sort of thing.
You just sound like me years ago, when I also grew tired of running scales and exercises all of the time.
When you've only got so much time in a day to practice, after a while it can be more enjoyable to just "jam" and not worry about more formal exercises.
You'll get better at improvising/jamming, but may not progress to the next skill level on guitar as quickly. (see http://en.wikipedia.org/wiki/Practice_(learning_method)#Deliberate_practice )
So that's the tradeoff. But if you're already comfortable at where your skill level is at, then certainly, turn down the exercises and just do more jamming.
Comment: First, those vids were not improvisational as such. They weren't playing by ear - they had learnt those songs beforehand ;)

The thing about jamming, is that the inspiration comes from either within you, other people you're playing with, or music that you're listening to.

You can jam over the top of other people's records, or get some backing tracks.
Comment: Or you can just go at it by yourself. Pick a key or a scale, or a chord progression. Then just experiment - play just random things within those boundaries. Some things will work, others wont. If something starts working, then build off that.

The key is to listen to what you're playing, as you're playing it. You should have a goal in mind, like where you want the melody / harmony to go.

With time, you'll get better at "guessing" which notes to play.

Lastly, I would say - just listen to as many improv performances as you can.
Comment: *Do you like improvising or improvisational music?* : Sorry I don't get it. What's the difference? *If you practice like that all the time, like anything you'll get better at it. You can end up developing your ears and a sort of intuition about what to do on guitar to get the sounds you want.* : This is what I want to do. How to achieve this?
Comment: *it can be more enjoyable to just jam* : But I don't understand what to jam. Its like whenever I am not trying new tab I am always going through scales. I don't like playing chords and doing simple things. I love to [pluck](http://www.youtube.com/watch?v=x0GJjv9SdF8) and doing [finger style stuff](http://www.youtube.com/watch?v=QH_ZLfZRVMo). (I can play both of this tracks) So where should I go from here?
Comment: One last tip: when I improv, I play dud notes all the time :) But the secret it to not dwell on it - a note not in key is not a bad thing. You can use it to your advantage - for example, the a note out of key will create dissonance/tension. If you can resolve that tension, it will sound OK, and probably kind of cool. The trick is that if you play an "outside" note, you just have to remember that the "correct" note was either one semitone above or one semitone below. I.e. just quickly move back one or forward one fret. Once you get used to this, you realise there is no such thing as a bad note

Learning Guitar by hearing


Maximum Likelihood Estimator of SNR for a Known Signal Superimposed in AWGN

Question: Assuming the following model (Fir instant, received signal of a communication / RADAR system):
$$ x[n] = \alpha s[n] + w[n] $$
Where:

$ s[n] $ is a known signal of length $ L $ and known energy $ p = \sum_{n=0}^{L-1} {s}^{2}[n] $.
$ \alpha $ is unknown attenuation factor. Though it is known $ 0 < \alpha \leq 1 $.
$ w[n] $ is additive white gaussian noise (AWGN) where $ E[w[n]] = 0 $ and $ E \left[ w[m]w[n] \right] = \delta[n-m] {\sigma}^{2} $. The noise variance $ {\sigma}^{2} $ is unknown parameter.
There are $ N $ samples, namely, $ n \in [0, \ldots, N-1] $ where $ N > L $.

The question is, what is the Maximum Likelihood Estimator of the SNR of the received signal - $ \frac{{\alpha}^{2}p}{{\sigma}^{2}} $?
Using Matched Filter / Correlation one could calculate the ML estimator of $ \alpha $ and $ {\sigma}^{2} $. Yet since the SNR isn't one to one function of the parameters, it cannot be calculated using them.
Hence the question is: what's the MLE of the SNR of the received signal?
Answer: "... since the SNR isn't one to one function of the parameters, it cannot be calculated using them" isn't always true.  In this case, the MLE of the SNR is exactly what you'd hope, just plug in the MLEs of $\alpha$ and $\sigma^2$.
To see this heuristically, assume the MLE of the SNR, label it $\widehat{SNR}$, is something else.  Then the likelihood calculated using $\widehat{SNR}$ and the MLE of, say, $\alpha$ and using the relationship $SNR = \alpha^2p / \sigma^2$ will be lower than it would be if the MLEs of both $\alpha$ and $\sigma^2$ were used, because the estimate of $\sigma^2$ will be different from the MLE of $\sigma^2$.  Therefore $\widehat{SNR}$ isn't maximizing the likelihood.  Therefore it isn't the MLE of the SNR.
Comment: I couldn't follow your proof. Could that be proven rigorously? Thank You.


How to customized Ag grid react pagination

Question: How to customized Ag grid react pagination, I am using shared documentation below code.
  <AgGridReact
          rowData={rowData}
          columnDefs={columnDefs}
          autoGroupColumnDef={autoGroupColumnDef}
          defaultColDef={defaultColDef}
          suppressRowClickSelection={true}
          groupSelectsChildren={true}
          rowSelection={'multiple'}
          pivotPanelShow={'always'}
          pagination={true}
          onGridReady={onGridReady}
        />

it is displaying default pagination in bottom  after all rows
it is display
1 to 10 of 100 |< <page 1 of 10> >|

but I want per page number user can select using dropdown
i want drop down and inside dropdown like 10,20,30......
so my custom pagination  be look like

 page 10 inside dropdown 1-10 of 100 < >

Answer: Add a select box over your AgGridReact component and attach an event listener to it for the desired result.
select element

  Page Size:
  <select onChange={onPageSizeChanged} id="page-size">
    <option value="10">10</option>
    <option value="100">100</option>
    <option value="500">500</option>
    <option value="1000">1000</option>
  </select>

  <AgGridReact
    rowData={rowData}
    columnDefs={columnDefs}
    autoGroupColumnDef={autoGroupColumnDef}
    defaultColDef={defaultColDef}
    suppressRowClickSelection={true}
    groupSelectsChildren={true}
    rowSelection={'multiple'}
    rowGroupPanelShow={'always'}
    pivotPanelShow={'always'}
    pagination={true}
    paginationPageSize={10}
    onGridReady={onGridReady}
  />

event listener
const onPageSizeChanged = useCallback(() => {
  var value = document.getElementById('page-size').value;
  gridRef.current.api.paginationSetPageSize(Number(value));
}, []);

Result

Working plunker: https://plnkr.co/edit/wBzFQeiTpPNtkoMH?preview


Convergence of a particular fixed point iteration scheme

Question: Setup
I have the following non-linear system of equations:
$$
\mathbf{x} P(\mathbf{x}) = 0
$$
where

$\mathbf{x} \in \mathbb{R}_{>0}^n$ is a probability distribution, i.e., $\sum_i x_i = 1$, and
$P(\mathbf{x})$ is an $n \times n$ matrix that depends on $\mathbf{x}$ (hence the non-linearity.) $P$ is the transition rate (infinitesimal generator) matrix of a Markov chain, but it is probably not important.
Not sure if important: $P_{ij}, i \ne j$ is a strictly convex function of $\mathbf{x}$, and $P_{ii} = - \sum_j P_{ij}$ is concave.

Furthermore I know the following things.

This system has a unique fixed / stationary point, and
The dynamical system $\mathbf{\dot{x}} = \mathbf{x} P(\mathbf{x})$ converges to this fixed point from any starting point.

Problem
I want to show that the following iterative scheme converges to the fixed point.

pick any initial $\mathbf{x}_1$, e.g., $\mathbf{x}_1 = [1/n, \ldots, 1/n]$
given $\mathbf{x}_k$, find $\mathbf{x}_{(k+1)}$ by setting $P_k = P(\mathbf{x_k})$ solving the linear system $\mathbf{x} P_k = 0$

Comments
I have done a lot of numerical simulations to convince myself that this iterative scheme converges, but I would like to prove it formally.
Any help is appreciated!
Comment: Have you tried looking into uniformization? I think I remember that there are results which show that the uniformized Markov chain (your linear system) converges to the same stationary distribution as the continuous-time Markov chain given the uniformization constant is sufficiently large.
Comment: @BernhardGeiger I tried going through a discrete-time Markov chain formulation using uniformization, but it didn't help. Basically, letting $\tilde{P}$ be the matrix of transition probabilities obtained with uniformization, solving the linear system reduces to a matrix multiplication $\mathbf{x}Q$, where $Q = \lim_{m \to \infty} \tilde{P}^m$. But I couldn't manage to analyze $Q$...
Comment: Hmm, I see. But you want to show that your iterative scheme converges, right? I think the proof for convergence is possible via uniformization and related results. You would still need to let the iterative scheme run to get the invariant distribution...

Plus, if you know that there is a unique fixed point, then the matrix $Q$ should have all its rows being equal to this fixed point $\mathbf{x}$, shouldn't it?
Comment: But $Q$ is a function of $\mathbf{x}$. The key part is to show that the sequence of linearizations leads to the desired fixed point (uniformization or not.) But maybe I'm not getting your point? If you want to expand on it in an answer (even incomplete), I'd be happy to have a deeper look!
Answer: I have to admit that I don't have the answer, it is just an educated guess: I just recommended to look into the proofs for uniformization, because there you also solve for the fixed-point of a dynamical system (CTMC) via linearization (DTMC). I think it might be possible to use uniformization also for non-homogeneous Markov chains, as in your case, but there you should look into the proofs.
With non-homogeneity I mean what you call non-linearity: Let $\tilde{P}$ be a function of $\mathbf{x}_{k}$, i.e., include the non-linearity in uniformization. You might need to choose the uniformization constant properly, i.e., according to the extreme cases for $\mathbf{x}_{k}$. If you can then show that none of your changes (w.r.t. standard uniformization) violates conditions made in the proofs, then you know that your iterative scheme converges to the fixed point you are interested in.
On top of that, it might be worth looking into discretization of dynamical systems...
Comment: Great, thanks. I'll have a deeper look over the next days and report back ;-)
Comment: Any progress? Just curious...
Comment: I'll write my progress so far as an answer.
Answer: I tried to follow @Bernhard's recommendations, and this is the progress I made so far. Uniformization in itself didn't help me much. The CTMC converges, so clearly the corresponding uniformized (discrete) chain converges as well.
Consider the uniformized (linearized) chain. Instead of making a single "small" change at each discrete step (direct consequence of the small uniformization constant), I'm letting this linearized chain run until convergence. Then, uniformize again, and repeat the process.
However, @Bernhard's answer prompted me to look into results on nonhomogeneous / nonlinear Markov chains.

Nonhomogeneous Markov chains. There's a flurry of results, but unfortunately none of them seems to apply to my case. In particular, most results require asymptotical homogeneity of the transition matrix, so it doesn't advance me much (it is clear that asymptotical homogeneity is equivalent to convergence my method!) See e.g. Seneta's book, Non-negative Matrices and Markov Chains
Believe it or not, but there is actually some recent theory on nonlinear Markov chains. In a few words, it's a stochastic "way of thinking" of any mapping in the probability simplex. Unfortunately, it is difficult to check the conditions for convergence, see On Ergodic Properties of Nonlinear Markov Chains

In summary: nothing conclusive so far!
Comment: Thanks for the clarification...Sorry that I could not help!
Comment: I'm very thankful still - it pointed me in an interesting direction. I haven't abandoned all hope yet ;-)


Determine the limiting distribution for $\overline{X}^{2}$, where $\overline{X}$ is the sample mean

Question: This question is related somewhat to one that somebody else asked here. Aside from the fact that I don't feel as if that question was sufficiently answered, my question is not about the exact same thing.
I am being asked to determine the limiting distribution for the square of the sample mean, $\overline{X}^{2}$, where $\overline{X}$ is the sample mean from a population with finite mean and variance.
However, I, unlike the other OP, have absolutely no idea how to approach this. I am also not sure how they were supposed to get it converging to something normal-like without knowing about how $X$ is distributed in the first place. In my notes, this problem is given alongside a theorem characterizing weak convergence of measures, but I don't see how to apply that here.
Comment: Yes, that is exactly what the CLT tells you.
Comment: Have you heard of the Central Limit Theorem? Presumably you have because you included that as a tag, but I am surprised you don't mention it at all in your question. If $\bar X$ is asymptotically normal, as the CLT gives, then $\bar X^2$ will be scaled non-central chisquare on 1 df. (You must be familiar with non-central chisquare because you asked about it in a previous question.)
Comment: It is a direct application of delta method. For a more transparent solution, see @Simth's answer.
Comment: @GordonSmyth how do I know that $\overline{X}$ is asymptotically normal, though? Or is that something that the CLT tells us?
Comment: @GordonSmyth okay, on the page in the typed up lecture notes our prof gives us, the top of the page is entitled "Central Limit Theorem", but I'm not sure that the theorem given on that page is actually it. It says "Suppose $X_{1},X_{2}, \cdots $ are i.i.d. r.v's with finite $E[X_{i}] = \mu$ and $Var[X_{i}]=\sigma^{2}$. Then, $\frac{\sqrt{n}(\overline{X}-\mu)}{\sigma} = \frac{\overline{X}-\mu}{\sqrt{\sigma^{2}/n}}= \frac{S_{n}-n\mu}{\sqrt{n \sigma^{2}}} \to^{L} Z \sim N(0,1)$ as $n \to \infty$." Is this it? Because it's called Lindberg-Levy here.
Comment: @GordonSmyth anyway, I don't think we want it to be STANDARD normal asymptotically, because that's what this theorem gives us. This is the only theorem listed on this page, and nowhere else in the chapter is there anything else remotely CLT-like listed. So, you can see why I'm so confused.
Comment: Under reasonable assumptions (like finite variance and mean), the CLT does work...and exactly what those assumptions are, and how weak they can be, are what give rise to 'alternate' CLT definitions.  But they all give the same result.  Wikipedia actually does an OK job on this: https://en.wikipedia.org/wiki/Central_limit_theorem
Comment: @Jeff Take care. Asymptotically, $\bar{X}$ goes to a constant (as will its square, each under suitable conditions); it's a standardized version of $\bar{X}$ (e.g. $sqrt{n}(\bar{X}-\mu)$) for which the CLT applies.
Answer: You're looking for the Delta method here. From your lecture notes,
\begin{align*}
\frac{\sqrt{n}(\bar{X}_n-\mu)}{\sigma}&\overset{d}{\to}N(0,1),\text{ or}\\
\sqrt{n}(\bar{X}_n-\mu)&\overset{d}{\to}N(0,\sigma^2).
\end{align*}
The idea here is to expand $g(\bar{X}_n)=\bar{X}_n^2$ about $\mu$:
\begin{align*}
\bar{X}_n^2=\mu^2+2\mu(\bar{X}_n-\mu)+(\bar{X}_n-\mu)^2.
\end{align*}
Then
\begin{align*}
\sqrt{n}(\bar{X}_n^2-\mu^2)=2\mu\sqrt{n}(\bar{X}_n-\mu)+\sqrt{n}(\bar{X}_n-\mu)^2.
\end{align*}
If you can show $\sqrt{n}(\bar{X}_n-\mu)^2\overset{P}{\to}0$, then Slutsky's theorem implies that both sides tend to $N(0, 4\mu^2\sigma^2)$.
Edit: Just in case it was unclear, you must show that the second term of the right hand side in the final equation converges to 0. Then you use the central limit theorem to get that the first term of the right hand side, and therefore the left hand side, goes to $N(0, 4\mu^2\sigma^2)$.
Comment: $\sqrt{n}(\bar{X} - \mu)^2$ does converge to $0$ in probability, if you would like to write it as $\sqrt{n}(\bar{X} - \mu) \times (\bar{X} - \mu)$.
Comment: @GordonSmyth The problem is that the non-centrality parameter diverges: $\bar{X}_n^2=n^{-1}\|P_1X\|^2\to \sigma^2 n^{-1}\chi^2_{1;\|P_1\mu\|/\sigma} =\sigma^2n^{-1}\chi^2_{1;n\mu/\sigma}$
Comment: @Jeff It is correct and a well-written proof. But it actually didn't apply the delta method, if so, the proof is one line. That may be the only confusing part as he mentioned delta method.
Comment: @GordonSmyth  I ran some simulations and I believe $\sqrt{n}(\bar{X}-\mu)^2$ converges to 0. And the "limiting distribution" for a sequence $T_n$, as I've seen it defined in courses, is any distribution $F$ such that there exist sequences of real numbers $b_n, 0\neq a_n$ such that $a_n(T_n-b_n)\overset{d}{\to}F$.
Comment: @GordonSmyth You must have confused $\sqrt{n}$ with $n$. Did you seriously consider my decomposition, where $\sqrt{n}(\bar{X} - \mu) \to_d N(0, \sigma^2)$ and $\bar{X} - \mu \to_d 0$?
Comment: Even though we assume the normalizing constant is $n$, unless the underlying distribution is normal, how can you say "it is distributed as $\sigma^2\chi_1^2$ regardless of $n$"?
Comment: @Zhanxiong so is Smith's solution correct or not?
Comment: @Smith I understand what you're saying, but what I'm not sure about still is how to show that $\sqrt{n}(\overline{X}-\mu)^{2}$ converges to $0$ in probabillity. Zhanxiong is saying to write it as $\sqrt{n}(\overline{X}-\mu) \cdot (\overline{X}-\mu)$. Then, you have that $\sqrt{n}(\overline{X}-\mu)$ converges to $N(0, \sigma^{2})$ in distribution, and I'm assuming $\overline{X}$ converges to $\mu$ because of the central limit theorem, so that $\overline{X} - \mu$ converges to $0$, but then how does this give us that the whole thing converges in probability to $0$?
Comment: @Smith or, I can show by the WLLN and the continuous mapping theorem for convergence in probability that $(\overline{X}_{n}-\mu)^{2}  \to^{P} 0$, but then what do I do with the $\sqrt{n}$ part? This is really the only part of this I am still having problems with.
Comment: @Zhanxiong, if you're around, you could answer these concerns as well.
Comment: @Jeff The claim follows a simple property which states that $O_P(1) \cdot o_P(1) = o_P(1)$. If you have concerns regarding this property, ask a new question and I may give you a proof.
Comment: @Zhanxiong so it's a little-O, big-O thing? Is that what you're saying?
Comment: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/70580/discussion-between-zhanxiong-and-jeff).


How do I update the Chart.js automatically every 2 seconds?

Question: I would like to display a chart on a website that updates automatically every 2 seconds.
I don't know JavaScript very well yet. I found and tried this example:
https://blog.heimetli.ch/chart-js-update.html
var getData = $.getJSON('/value');
getData.done(function (result) {

  var zeit = result.zeit;
  var flow = result.flow;
  var ctx = document.getElementById("myChart");
  var myChart = new Chart(ctx, {
    type: 'line',
    data: {
      labels: zeit,
      datasets: [
        {
          data: flow,
          label: "Flow>",
          borderColor: "#0004cd",
          fill: false
        },
      ]
    }
  });
});

Unfortunately I have not yet managed to change my code from the example (link) so that it works in my code. What do I have to do?
I want my code updated automatically every 2 seconds. Thanks for your time:)
Comment: use polling or websocket
Comment: yes that's an idea to improve. But I want to run it like this now.
Answer: A very practical (but not that efficient) solution would be JavaScript's setInterval().
Here is an example:
setInterval(function() {
    let getData = $.getJSON('/value');
    getData.done(function(result) {

        var zeit = result.zeit;
        var flow = result.flow;
        var ctx = document.getElementById("myChart");
        var myChart = new Chart(ctx, {
            type: 'line',
            data: {
                labels: zeit,
                datasets: [{
                    data: flow,
                    label: "Flow>",
                    borderColor: "#0004cd",
                    fill: false
                }, ]
            }
        });
    });
}, 2000) // 2000 milliseconds = 2 seconds

This will get the information from the source you provide trough AJAX and will execute the same code you are using for your first render after every request.
The issue with this method would be that every time you update your chart, it will generate a request to the server you specified.
Comment: thanks for the quick answer. i've already tried your idea. that works so well. but now the chart.js doesn't seem liquid. i have to work with these functions.
https://www.chartjs.org/docs/latest/developers/updates.html
yes you're right it would be better if i use a Web-socket. but i want to solve it now so that every time a request is made.


What is the angle x?

Question:

We have to find the angle x

I have tried using these theorems, but failed.
1. The angle at the centre is twice the angle at the circumference.
2. The exterior angle of a triangle is equal to the sum of the two opposite interior angle.
Answer:
Consider the drawing. Extend AO to make diameter AD. Extend AC to make chord AE. Clearly AE and AB are symmetric about AD and are equal. FG is drawn with angle $30^o$ with FB at F, so its intersect with AB make a point which is symmetric with C about AD.  $\angle CAO=\angle DAB=20^o$. So points C, O and P(intersection of FD and AB) are on one circle(are cyclic) so $AO=AC$, that is triangle AOC is isosceles and $X=\angle AC0=80^o$.
Comment: FD & AB straight line can't intersect each other but you consider P as intersection of AB and FD.Also you wrote FD is drawn with angle $30^o$ with FB at F but how do you know that angle DFB = $30^o$
Comment: Sorry it is a typo. I meant  FG. I edited my answer.
Comment: what about P point?
Comment: Of course, P is the intersection of lines FG and AB.
Answer:
Hint:
$\triangle AOC$ is isosceles
with
\begin{align}
\angle OCA
=
\angle AOC
&=
\tfrac12\,(180^\circ-20^\circ)
=80^\circ
.
\end{align}
Comment: @Supriyo Banerjee: well, then $|AD|=|AC|=|AO|$.
Comment: I tried to prove $\triangle AOC$ is isosceles but failed.So please help me to prove this.
Comment: @Supriyo Banerjee: can you see that $\triangle ACD$ is isosceles?
Comment: yeah, $\angle CDA$ & $\angle ACD$ are both $70^o$. So $\triangle ACD$ is isosceles.
Comment: Yeah,then AC =AD & also AD=AO hence AO=AC .Thank you so much.

What is the angle x?


cannot find xdebug helper icon from chrome

Question: I'm using phpstorm and xdebug
I installed xdebug helper extension.
I have used it before fine..
today, I cannot see the icon which enable the debug connection between browser and phpstorm
before [ normal ]
==>

now
==>

what should I do?
Answer: choose setting / extensions / xdebug helper
 = > options
add your testing domain [ localhost ] in Domain filter


Help understanding tendency tones

Question: What should I be listening for when trying to understand tendency tones? From what I understand, unstable tones 'resolve' into a specific stable tone, but I'm having trouble hearing these resolutions unless it's a resolution into the tonic. Should these movements even be described as resolutions if they aren't moving into the tonic? Any answers to these questions or other insights would be greatly appreciated!
Btw I'm referring to tendency tones as stand alone melody notes and not within chords.
Comment: Could you provide an example of what causes you trouble?
Comment: Sure, I guess what I should have mentioned is I'm especially having trouble hearing the resolution when the movement is by a whole step. For example,  4th-3rd degree movement sounds more stable to me then a 6th-5th degree movement
Answer:
I'm having trouble hearing these resolutions unless it's a resolution into the tonic

For the most part this IS what tendency tones do, they resolve to the tones of the tonic chord.
The tones involving the half steps are the real strong tendencies. TI up to DO and FA down to MI.

Those movements imply a dominant chord moving to the tonic. You can fill in the full four part harmony to see how the other voices move...

The dominant is not a tendency tone and is held. The supertonic - RE - moves down to DO the tonic.
The submediant - LA - isn't in that progression, but we can look at common progressions using subdominant chords to see the movement of LA...

Moving the voices to the closes tones of the next chord, LA moves down to SOL.
So, RE and LA sort of have a tendency to descend to tonic tones, although they aren't listed as tendency tones in my harmony textbook.
So, the tendency is the tendency to move to tones of the tonic chord. Fulfilling that move to the tonic, or at least setting up the expectation, is important. Consider a half cadence where DO moves down to TI to play the third of the dominant chord. TI has stability occurring in a cadence like that, and it really doesn't get that strong tendency quality.

Help understanding tendency tones


Fibered categories, introduction or notes

Question: I would like to learn about fibered categories, I know basic category theory, but not algebraic geometry.
Is there a text, or lecture notes, which motivate the definitions from fields other than algebraic geometry?
Comment: Related: http://math.stackexchange.com/questions/1812490/in-category-theory-what-is-a-fibration-and-why-should-i-care-about-them
Answer: The part of the notes of Vistoli on fibered cats in FGA explained can be read without knowing AG (there are parts which do refer to schemes, but these can be ignored). Also, I don't believe the part of Grothendieck's TDTE introducing fibered cats refers to schemes, so you can read this as well. However, without schemes, these things become hard to motivate, so for motivation, you could maybe read Noohi's foundations of topological stacks.
Answer: There is also an entirely logical/category theoretic motivation for fibered categories, as visible here: http://www.mathematik.tu-darmstadt.de/~streicher/FIBR/FibLec.pdf


Cloud Custodian policy to stop EC2 instances containing a tag and not containing a tag value

Question: I want to stop EC2 instances which do not have a tag ABC and whose tag(ABC) value is not of type @gmail.com
I am trying to  use the cloud custodian policy in AWS, I have written like this
  filters:
    - or:
      - "tag:ABC": absent
      - type: value
        key: "tag:ABC"
        op: ne
        value: '/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@gmail.com/'
  actions:
    - stop

However, this code does not have the desired effect. Any idea why?
Comment: Please have a look into this.
Answer: Created this policy using special filters example.
ec2-without-gmail-in-tag.yml
policies:
  - name: ec2-without-gmail-in-tag
    description: |
      Stop EC2 instances that do not have a tag or if the tag exists but doesnt have a specific value
    resource: ec2
    filters:
      - or:
        # check if tag is absent
        - "tag:ABC": absent
        # or check if tag does not contain @gmail.com using a negative lookahead
        - type: value
          key: "tag:ABC"
          op: regex
          value: '^((?!@gmail.com).)*$'

You can test out this filter using python's re module.
$ python

>>> import re

>>> regex = '^((?!@gmail.com).)*$'

>>> re.match(regex, 'Test if @gmail.com matches').group(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'

>>> re.match(regex, 'Test if @gmail matches').group(0)
'Test if @gmail matches'


Splitting array not working in android

Question: I am splitting a string with some sample text in the middle. But it is not splitting. Please help me where i went wrong
String[] str;

parts[1] = "loopstWatch out this is testingloopstThis makes the difference";
str = parts[1].trim().split("loopst");

Comment: It would help if we knew the error message (or the "wrong" output").
Comment: When i am trying to print str value it is displaying nothing
Comment: So you want to split the string with delimiter as 'loopst'?
Comment: yes wat is the error in it plz tell me
Comment: @user1448108 It is working check here http://www.browxy.com/SubmittedCode/15624
Comment: @user1448108 Do you want to remove "loost" from the above string?
Comment: From the string i want to get str[0]=Watch out this is testing str[1]=This makes the difference
Comment: @user1448108 I have posted the answer...Check once..
Answer: String[] arr;
                arr = "loopstWatch out this is testingloopstThis makes the difference".trim().split("loopst");
                Log.e("Data arr[0]",":"+arr[0]);
                Log.e("Data arr[1]",":"+arr[1]);
                Log.e("Data arr[2]",":"+arr[2]);

Output
 arr[0] :""
arr[1] :"Watch out this is testing"
arr[2] :"This makes the difference"

Answer: I think you're doing it backwards.
You want to first split, then get the piece at index 1, then trim, then set str equal to the result:
String s = "loopstWatch out this is testingloopstThis makes the difference";
String str = s.split("loopst")[1].trim();

// str should be equal to "Watch out this is testing"

Splitting array not working in android


How to control for demographic variables in Path Analysis?

Question: I am running a series of path analyses using AMOS and could desperately use some help around how to control for confounding demographic variables;
Q1. I have read that while some demographic variables may correlate with the outcome and predictor variables, if this effect size is small it may not be worth controlling for these effects. Is this true? and what effect size would be the cut off?
Q2. What are the specific steps to adding confounding variables into my model? My understanding is that you add the variable as another exogenous variable and add single-direction arrows point at the endogenous variables, and then bi-directional arrows pointing at all other exogenous variables. Is this correct? (I have also seen some mention of controlling for confounds by adding them as moderators instead?)
Q3. What happens when the confounding variable is nominal? E.g. Employment status (1 = full time, 2 = part time, 3 = unemployed etc..). What are the steps in converting this to a variable that can be added into a model?
I have tried to find resources to address these questions to no avail and would very much appreciate any assistance or recommended reading.
Answer: Q1. Why would you exclude them? Testing them and then removing them looks like you are traveling in a garden of forking paths. Just stick them in.
Q2. This depends on your theory and the result of the model. For something like gender, just regress everything on it. Look at a path diagram for regression to help you to understand.
Q3. You dummy code it, just like regression.
Make sure you have a good understanding of regression before you move to SEM. Regression is a special case of SEM, so take your regression models and make sure you can reproduce them in SEM (same estimates, same standard errors, etc). If you do that, you are 80% of the way to understanding control variables.


Como colocar video dentro de uma view

Question: Preciso colocar um video dentro de uma view, nisso ele tem que  rodar só dentro daquele quadrado não em fullscreen.(Tipo facebook)
Eu já pesquisei sobre e só vi tutoriais full screen. Como fazer isso?
Consegui com esse código:
class BeginController: UIViewController {
 @IBOutlet weak var videoPreviewLayer: UIView!
 var player: AVPlayer!
 var avpController = AVPlayerViewController!()

 override func viewDidLoad() {
    super.viewDidLoad()
    let moviePath = NSBundle.mainBundle().pathForResource("Hello           Moto", ofType: "mp4")
    if let path = moviePath{
        let url = NSURL.fileURLWithPath(path)
        let item = AVPlayerItem(URL: url)
        self.player = AVPlayer(playerItem: item)
        self.avpController = AVPlayerViewController()
        self.avpController.player = self.player
        avpController.view.frame = videoPreviewLayer.frame
        self.addChildViewController(avpController)
        self.view.addSubview(avpController.view)

    }
    // Do any additional setup after loading the view.
}

Comment: Bem vinda ao SOpt. O que você já tentou fazer? Adicione na pergunta.
Comment: Obrigada! Já tentei usar o AVPlayer Controller mas tudo que acho na internet só mostra como o video rodar logo que a view carrega. Eu precisava de dar um jeito de colocar o AVPlayer dentro de um subview e ele rodar somente dentro daquela view. Se alguém tiver uma ideia!
Comment: Você precisa da resposta em swift? Ainda precisa de ajuda?
Answer: Você irá precisar de Utilizar alguns imports para fazer com melhor perfeição depois veja certinho oque cada um pode fazer por você.
import UIKit
import MediaPlayer
import AVKit
import AVFoundation

Agora será necessário criar o componente
var videoPlayer: AVPlayerViewController = {
   var view = AVPlayerViewController()
   view.view.translatesAutoresizingMaskIntoConstraints = false
   return view
}()

let player = AVPlayer()

No viewWillAppear adicione:
self.addChild(videoPlayer)

Depois configure as constante de quanto o componente deve ocupar na tela no exemplo abaixo seria uma exibição como a do Youtube:
self.view.addSubview(videoPlayer.view)
NSLayoutConstraint.activate([
    self.videoPlayer.view.topAnchor.constraint(equalTo: self.view.topAnchor),
    self.videoPlayer.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
    self.videoPlayer.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
    self.videoPlayer.view.heightAnchor.constraint(equalToConstant: 210)
])

self.videoPlayer.player = player
self.videoPlayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

Agora para reproduzir um vídeo basta criar um AVPlayerItem com URL externa ou local veja na função abaixo:
func reproduzirVideo(item: AVPlayerItem){
    self.videoGravacao.player?.replaceCurrentItem(with: item)
    player.play()
}

Espero ter ajudado.
Answer: estou usando desta forma em Obj-C.
#pragma mark - Implementação do Video de Informativo.
// EM - Implementação da View com o Video anunciado.
- (void) informationLoadMovieChanged {

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:filePath];

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
    [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];

    self.player = [AVPlayer playerWithPlayerItem:playerItem];
    self.player.volume = 0.3;

    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    playerLayer.frame = _playerView.bounds;
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    playerLayer.needsDisplayOnBoundsChange = YES;

    self.playerView.layer.needsDisplayOnBoundsChange = YES;
    self.playerView.layer.borderColor = [UIColor grayColor].CGColor;
    self.playerView.layer.borderWidth = 2.5f;
    self.playerView.layer.cornerRadius = 6;
    self.playerView.layer.masksToBounds = YES;

    [self.playerView.layer addSublayer:playerLayer];
    [self.playerView.layer addSublayer:playerLayer];
    [self.playerView setPlayer:self.player];
    [self.player play];

    [self.player.currentItem addObserver:self forKeyPath:AVPlayerItemDidPlayToEndTimeNotification options:NSKeyValueObservingOptionNew context:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

}

- (void)moviePlayDidEnd:(NSNotification*)notification{

    AVPlayerItem *item = [notification object];
    [item seekToTime:kCMTimeZero];
    [self.player play];
}

Está rodando dentro de uma view e redimencionada conforme a view.

Como colocar video dentro de uma view


Como faço para setar uma lista de imagens no listview -> imageview a partir da url da página?

Question: O problema é que sempre que tem uma imagem nova na pagina especifica eu tenho que atualizar a aplicação com a nova URL da imagem setada na página.
Answer: Tem várias api's,  uma API bem simples é o Picasso:
<code>Picasso.with(context).load("http://myimage.png").into(mImageView);
</code>
Comment: Você terá que fazer um notify da sua lista..., Assim terá refresh da lista


Convert a number to a letter in C# for use in Microsoft Excel

Question:
Possible Duplicate:
How to convert a column number (eg. 127) into an excel column (eg. AA)

Ok so I am writing a method which accepts a 2d array as a parameter. I want to put this 2d array onto an Excel worksheet but I need to work out the final cell location. I can get the height easily enough like so:
var height = data.GetLength(1); //`data` is the name of my 2d array

This gives me my Y axis but it's not so easy to get my X axis. Obviously I can get the number like so:
var width = data.GetLength(0);

This gives me a number, which I want to convert to a letter. So, for example, 0 is A, 1 is B and so on until we get to 26 which goes back to A again. I am sure that there is a simple way to do this but I have a total mental block.
What would you do?
Thanks
Comment: use the R1C1 reference style so you don't have to calculate the letter, or get the cell with topLeftCell.Offset(arrayHeight, arrayWidth)
Comment: I don't full understand your question. If you want the x-axis you could do `data[0].GetLength(1)`. Or if your 2D array is of different sizes you could do `data[i].GetLength(1)`
Comment: I am trying to get the name of the Cell, so for example if I had a 4x4 array and I was starting at A1 the final cell name would be "D4". I can get the 4 easy enough with just `data.GetLength(1)` but `data.GetLength(0)` gives me a number, from which I want to work out the corresponding letter ("D").
Comment: @Mathias And the selected answer of that question is more complete than any of the answers here.
Comment: @phoog Apologies, I didn't see this question, thanks for the heads up!
Answer: Here's a version that also handles two-letter columns (after column Z):
static string GetColumnName(int index)
{
    const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    var value = "";

    if (index >= letters.Length)
        value += letters[index / letters.Length - 1];

    value += letters[index % letters.Length];

    return value;
}

Comment: It works but has a mistake, When you sent bigger than 701, It throws IndexOutOfRangeException error.
Comment: @EdwardKarak If you look at the A-Z range as a set of digits, an integer in base 10 *n* can be converted to a number written in A-Z notation (of at most two digits), where: the first digit is the result of the division of *n* to the number of digits (26) and the second digit will be the remainder of that division.
Comment: Please explain the reasoning behind this in words.
Comment: Also, it returns 'AA' for both 26 and 27 - the index is assumed to be zero-based for the first letter, but one-based for the second.
Comment: @bafsar, Yes, this only handles two letter column names.
Comment: @MaSiMan It works correctly for 26 and 27. It returns `AA` and `AB` respectively.
Answer: Just cast it to a char and do "ToString()".
using System;
using System.Collections.Generic;

public class MyClass
{
    public static void RunSnippet()
    {
        int myNumber = 65;
        string myLetter = ((char) myNumber).ToString();
        WL(myLetter);
    }

    #region Helper methods

    public static void Main()
    {
        try
        {
            RunSnippet();
        }
        catch (Exception e)
        {
            string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
            Console.WriteLine(error);
        }
        finally
        {
            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }
    }

    private static void WL(object text, params object[] args)
    {
        Console.WriteLine(text.ToString(), args);
    }

    private static void RL()
    {
        Console.ReadLine();
    }

    private static void Break()
    {
        System.Diagnostics.Debugger.Break();
    }

    #endregion
}

Comment: But it still doesn't fully answer the OP's question of how to convert column numbers into names. Once it gets to 27 your code will return `[`. The first answer [here](http://stackoverflow.com/questions/181596/how-to-convert-a-column-number-eg-127-into-an-excel-column-eg-aa) will do it well.
Comment: This won't even compile, let alone convert the column number properly.
Comment: GC Sid, here is a compilable version.


Help needed in Monitor.PulseAll()

Question: can anybody explain me with simple example to handle Monitor.PulseAll().I have already gone some examples from this stackoverflow.As i am a beginner i feel those are above my head.
Answer: How about (to show the interaction):
static void Main()
{
    object obj = new object();
    Console.WriteLine("Main thread wants the lock");
    lock (obj)
    {
        Console.WriteLine("Main thread has the lock...");
        ThreadPool.QueueUserWorkItem(ThreadMethod, obj);
        Thread.Sleep(1000);
        Console.WriteLine("Main thread about to wait...");
        Monitor.Wait(obj); // this releases and re-acquires the lock
        Console.WriteLine("Main thread woke up");
    }
    Console.WriteLine("Main thread has released the lock");
}
static void ThreadMethod(object obj)
{
    Console.WriteLine("Pool thread wants the lock");
    lock (obj)
    {
        Console.WriteLine("Pool thread has the lock");
        Console.WriteLine("(press return)");
        Console.ReadLine();
        Monitor.PulseAll(obj); // this signals, but doesn't release the lock
        Console.WriteLine("Pool thread has pulsed");
    }
    Console.WriteLine("Pool thread has released the lock");
}

Re signalling; when dealing with Monitor (aka lock), there are two types of blocking; there is the "ready queue", where threads are queued waiting to execute. On the line after Console.WriteLine("Pool thread wants the lock"); the pool queue enters the ready queue. When the lock is released a thread from the ready queue can acquire the lock.
The second queue is for threads that need waking; the call to Wait places the thread in this second queue (and releases the lock temporarily). The call to PulseAll moves all threads from this second queue into the ready queue (Pulse moves only one thread), so that when the pool thread releases the lock the main thread is allowed to pick up the lock again.
It sounds complex (and perhaps it is) - but it isn't as bad as it sounds... honestly. However, threading code is always tricky, and needs to be approached with both caution and a clear head.
Comment: Thanks Marc.I have seen your several post.They are excellent and simple to understand.Thanks you very much from bottom of my heart.
Comment: Marc one question.Here Signalling means "I am about to release" ?.
Answer: Monitor.Wait() will always wait for a pulse.
So, the principal is:

When in doubt, Monitor.Pulse()
When still in doubt, Monitor.PulseAll()

Other than that, I'm not sure what you are asking.  Could you please elaborate?
Edit:
Your general layout should be:
Monitor.Enter(lock);

try
{
    while(!done)
    {
        while(!ready)
        {
            Monitor.Wait(lock);
        }

        // do something, and...

        if(weChangedState)
        {
             Monitor.Pulse(lock);
        }
    }
}
finally
{
    Monitor.Exit(lock);
}

Comment: Thanks John.I am really greatful to you. :)
Comment: Won't the lock been automatically released? or do we need to specify it explicitly? i mean Monitor.Exit(lock)?
Comment: @Prabugoel If you use `lock (obj) { }`, the lock will be automatically released. If you use `Monitor.Enter()`, you have to explicitly `Monitor.Exit()`. How else would the runtime know when to release the lock? `lock (obj)` is just a short-hand for Enter and Exit.

Help needed in Monitor.PulseAll()


First differences filter

Question: I am beginning in signal processing and my professor is asking me to do a first differences filter to my timeserie. I know I am supposed to use the filter() function but I don't know what numerator (b) and denominator (a) coefficient vectors am I supposed to use. Is first differences and first-order the same ?
Answer: First, you should read up on Matlab's documentation of the filter function.  If you just want to take a first difference, what you're looking to generate is the series:
1 * y(n) = 1 * x(n) - 1 * x(n - 1)

Which corresponds to the vector a = 1, and b = [1, -1], so your Matlab code would look like:
y = filter([1,-1],1,x);

Comment: Is it the same thing as an autoregressive moving average ?
Comment: Here, notice we are generating y(n) as a function of x(n), x(n-1).  In an autoregressive process, one generates x(n+1) as a function of lagged values.
Comment: Than you Prototoast. When I plot the spectrum, both reduce significantly the noise floor. The first difference filter will totally flat the fft(white) while the ARMA keep some power in the low frequency(more red). I am wondering which one is fit the real spectrum the best.
Comment: That sort of depends on the properties of the "real" spectrum.  There is no one "right" answer.
Comment: What do you mean by property of the real spectrum ? noise distribution?

First differences filter


Adding Secrets in database onelogin

Question: Is there is a method through which we can add secrets such as user/pass the shell or ssh key or ssl certificates?
For example, if we add in onelogin a database, how can we add the credentials for db in onelogin?
Can you please provide us with a guide if such an option is possible?
Comment: I don't think OneLogin supports DB Connections as of now. You can add Active Directory, LDAP and other HRMS but not DB
Answer: You can use "Secure Notes" feature.
https://onelogin.service-now.com/support?id=kb_article&sys_id=9ff95543db109700d5505eea4b96199c

Adding Secrets in database onelogin


Sequence of analytic fns on open unit disk, $\lim_{k\rightarrow \infty}\sum_{n=0}^{\infty}a_n(k)z^n = \sum_{n=0}^{\infty}a_n z^n$

Question: uniformly convergent on compact subsets.  prove that $\lim_{k\rightarrow \infty}a_n(k) = a_n$ for each n.  This is a homework problem of mine, and I think it's probably the most difficult problem I've faced so far in my time as a math student.  I have a general strategy which I think might work but every time I try to write up the solution I end up teasing out more and more complexities which need to be dealt with, it's been a pretty frustrating process.  My general strategy is this:
Proceed by Contradiction.  Then there is some set of n's, possibly infinite, for which the sequence $a_n(k)$ doesn't converge to $a_n$.  By the well-ordering principle of the integers this set has a least element, call it $n_0$.  Restrict the domain of our functions to some compact ball centered at zero.  We want to show that given arbitrary $\varepsilon>0$ and $K>0$, we can always find some $k>K$ such that $|\sum_{n=0}^{\infty}(a_n(k)-a_n)z^n|>\varepsilon$.  In other words, we need to show that in fact the convergence isn't uniform on this compact ball.
To do this I show that the growth of the sequence of coeffecients $(a_n(k)-a_n)$ can be no faster than a certain exponential growth determined by the size of our compact ball, since the power series difference must converge (I think!).  Then by choosing a point in our compact ball sufficiently close to zero I can make all those coeffecients greater than $n_0$ in our power series difference of negligible size compared to the $n_0$ coeffecient.
As for all those coeffecients less than $n_0$ there are a finite number forming a polynomial which becomes very close to $(a_0(k)-a_0)$ for points very close to zero.  Our coeffecient $(a_{n_0}(k) - a_{n_0})$ stays propped up at a certain value of some minimum magnitude.  But it is multiplied by $z^{n_0}$ for some very small value of z, thus it also gets very small for points near zero, I'm hoping that by not allowing K to grow as we bring our choice of z closer and closer to zero, that $(a_0(k)-a_0)$ will stay large enough so that it doesn't cancel out $(a_{n_0}(k) - a_{n_0})$, so if I can keep them different enough in magnitude and choose a point near enough to zero, I should be able to keep the whole thing's magnitude large enough to be bigger than epsilon.
Whew, that was a mouth full, I hope this question isn't too specific to be considered appropriate for this site.  Does my strategy seem reasonable?  Am I missing something and making things way too complicated?  (very possible).  Thanks.
Answer: Hint: the generalized Cauchy formula expresses the $a_n(k)$ in terms of contour integrals.
Comment: Cauchy's Generalized Integral Formula?
Comment: Yes, it's sometimes called that.  The exact name will vary from text to text.
Comment: Gotta give mad late props on this one, very clever, completely simplified the problem.
Comment: Cauchy's Theorem makes this SO MUCH easier than whatever you may try to do the same thing for real power series of a real variable...  The next time someone asks you why to study complex numbers, think of this.


Error handling with probable missing variable

Question: I have a long chain of calcul with variables like the following one:
balance = (A + B + C) - (D - E) - F - G + H - I

Among these variables, there may be one variables ( not always the same) which impact the overall calculation by throwing an error.
The way I have though about to solve that is to use exceptions handling statement like:
try:
   balance = (A + B + C) - (D - E) - F - G + H - I
   except Exception as (e):
      print(e)
      pass

The problem is that that I still want to have the remaining of the formula as an output.
For example:
if B throw an error, the output would be:
balance = (A + C) - (D - E) - F - G + H - I

if E throw an error, the output would be:
balance = (A + C) - (D) - F - G + H - I

Is there a way to that without writing the try method as many times as the number of variable s? Any contribution is appreciated.
Answer: I found a solution, but it's a bit hard coded but it's the best that I could find in a very short time, if I'll find something better I will update the answer. So the solution is this:
balance = 0 # your initial balance
operations = 7 # number of operations - 1

# Your variables
A = 2
B = 3
I = 1

# a for so you can check all the letterals in your operation
# I forgot to mention that I forgot to put the ifs for A and B, sorry

for i in range(0, operations):
    try:
        balance = (A + B + C) - (D - E) - F - G + H - I
    except Exception as e:
        # this works as a case and switch, but it's made with ifs
        if str(e)[6] == 'C':
            C = 0
        if str(e)[6] == 'D':
            D = 0
        if str(e)[6] == 'E':
            E = 0
        if str(e)[6] == 'F':
            F = 0
        if str(e)[6] == 'G':
            G = 0
        if str(e)[6] == 'H':
            H = 0
        if str(e)[6] == 'I':
            I = 0
        print(e, "->", str(e)[6], "= 0")
print(balance)

Sorry if it's not a very detailed answer but it's my first post on StackOverflow. :)
EDIT: so I found a very nice way of solving this without that many ifs, the question about 'Is it possible without using try/exception?' I think the answer is no, because it's the only way of finding out that there is actually an error. This is the solution:
import re # import regular expression module

balance = 0 # your initial balance
number_operations = 7 # number of operations - 1

# your variables
A = 2
B = 3
H = 1

# this is the pattern to find the variable later when the Exception is called
# error = name 'variable' is not defined
# with this pattern you can get whatever variable is not defined in the balance formula
pattern = r"'.+'"

# a for so you can check all the letterals in your operation
for i in range(0, number_operations):
    try:
        balance = (A + B + C) - (D - E) - F - G + H - I_like_turtles
    except Exception as error:
        letter = re.search(pattern, str(error)) # gives a re object that contains 'C' (in case of undefined C)
        letter = letter.group(0)[1:-1] # gives the string 'C' that we cut so we get just C
        exec("%s = %d" % (letter, 0)) # dinamically creates a variable so we can have C = 0
        print(error, "->", letter, "= 0")
print("C =", C)
print("D =", D)
print("I like turtles =", I_like_turtles)
print(balance)

That last variable was just to test that with this method you can get whatever variable you need. I hope this solved your problem. :D


MySQL Optimizing

Question: My webserver uses a dedicated host:
Intel(R) Xeon(R) CPU E5620 8core
12Gram
Centos32bit/Driectadmin
DISK SAS 80G
Php-cgi

This host is running one website
Use wordpress 2.92(+plugin cache...)
Database size 600MB
only 100online

My website is running very slowly.
Please hep me configure my.cnf.
    [mysqld]
user=mysql
key_buffer=128M
set-variable = max_connections=1000
socket          = /var/lib/mysql/mysql.sock
key_buffer =32M
table_cache = 1024
open_files_limit = 16344
join_buffer_size = 8M
read_buffer_size = 8M
sort_buffer_size = 8M
tmp_table_size=512M
read_rnd_buffer_size=8M
max_heap_table_size=256M
#myisam_sort_buffer_size=256M
thread_cache_size=8
thread_cache=32
query_cache_type=1
query_cache_limit=1024M
query_cache_size=1024M
thread_concurrency = 16
wait_timeout = 10
connect_timeout = 10
interactive_timeout = 10
long_query_time=1
log-slow-queries = /var/log/mysqlslowqueries.log
max_allowed_packet=32M
skip-innodb
[myisamchk]
key_buffer = 64M
sort_buffer = 64M
read_buffer = 16M
write_buffer = 16M

[isamchk]
key_buffer=64M
sort_buffer=64M
read_buffer=16M
write_buffer=16M

And MySQLTuner
-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.1.47-log
[!!] Switch to 64-bit OS - MySQL cannot currently use all of your RAM

-------- Storage Engine Statistics -------------------------------------------
[--] Status: -Archive -BDB -Federated -InnoDB -ISAM -NDBCluster
[--] Data in MyISAM tables: 471M (Tables: 30)
[!!] Total fragmented tables: 2

-------- Performance Metrics -------------------------------------------------
[--] Up for: 1d 6h 39m 2s (6M q [57.776 qps], 60K conn, TX: 999M, RX: 2B)
[--] Reads / Writes: 93% / 7%
[--] Total buffers: 1.3G global + 32.2M per thread (1000 max threads)
[!!] Allocating > 2GB RAM on 32-bit systems can cause system instability
[!!] Maximum possible memory usage: 32.7G (275% of installed RAM)
[OK] Slow queries: 0% (10K/6M)
[OK] Highest usage of available connections: 5% (52/1000)
[OK] Key buffer size / total MyISAM indexes: 32.0M/65.5M
[OK] Key buffer hit rate: 100.0% (938M cached / 108K reads)
[OK] Query cache efficiency: 51.4% (3M cached / 5M selects)
[OK] Query cache prunes per day: 0
[OK] Sorts requiring temporary tables: 0% (929 temp sorts / 2M sorts)
[!!] Temporary tables created on disk: 49% (2M on disk / 4M total)
[OK] Thread cache hit rate: 99% (580 created / 60K connections)
[OK] Table cache hit rate: 97% (281 open / 288 opened)
[OK] Open file limit used: 1% (319/16K)
[OK] Table locks acquired immediately: 99% (7M immediate / 7M locks)

-------- Recommendations -----------------------------------------------------
General recommendations:
    Run OPTIMIZE TABLE to defragment tables for better performance
    Temporary table size is already large - reduce result set size
    Reduce your SELECT DISTINCT queries without LIMIT clauses

Cannot run mysqld
[root@server ~]#
[root@server ~]# mysqld
100607 18:21:17 [Warning] mysqld: Option '--set-variable' is deprecated. Use --
100607 18:21:17 [Warning] '--log_slow_queries' is deprecated and will be remove
100607 18:21:17 [Note] Plugin 'FEDERATED' is disabled.
100607 18:21:17 [Note] Plugin 'InnoDB' is disabled.
mysqld: File '/var/log/mysqlslowqueries.log' not found (Errcode: 13)
100607 18:21:17 [ERROR] Could not use /var/log/mysqlslowqueries.log for logging                                          urn it on again: fix the cause, shutdown the MySQL server and restart it.
100607 18:21:17 [ERROR] Can't start server: Bind on TCP/IP port: Address alread
100607 18:21:17 [ERROR] Do you already have another mysqld server running on po
100607 18:21:17 [ERROR] Aborting

100607 18:21:17 [Note] mysqld: Shutdown complete

And output from top
[root@server ~]# top
top - 18:45:03 up 3 days,  4:40,  1 user,  load average: 1.80, 1.42, 1.40
Tasks: 266 total,   1 running, 265 sleeping,   0 stopped,   0 zombie
Cpu(s):  4.6%us,  2.5%sy,  0.0%ni, 92.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  12462096k total,  2741388k used,  9720708k free,   599584k buffers
Swap:  8385920k total,        0k used,  8385920k free,  1612488k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 7487 mysql     15   0 1138m 241m 3572 S 98.7  2.0 811:12.72 mysqld
 1508 admin     16   0  166m  43m  21m S  0.3  0.4   0:00.80 php-cgi
 2019 apache    15   0  9264 3268 1444 S  0.3  0.0   0:00.06 httpd
 2089 root      15   0  2336 1132  800 R  0.3  0.0   0:00.15 top
32137 admin     15   0  173m  51m  22m S  0.3  0.4   0:02.11 php-cgi
    1 root      15   0  2072  580  504 S  0.0  0.0   0:02.42 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.06 migration/0
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    4 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0
    5 root      RT  -5     0    0    0 S  0.0  0.0   0:00.03 migration/1
    6 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/1
    7 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/1
    8 root      RT  -5     0    0    0 S  0.0  0.0   0:00.18 migration/2
    9 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/2
   10 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/2
   11 root      RT  -5     0    0    0 S  0.0  0.0   0:00.02 migration/3
   12 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/3
   13 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/3
   14 root      RT  -5     0    0    0 S  0.0  0.0   0:00.01 migration/4
   15 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/4
   16 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/4
   17 root      RT  -5     0    0    0 S  0.0  0.0   0:00.01 migration/5
   18 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/5
   19 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/5
   20 root      RT  -5     0    0    0 S  0.0  0.0   0:00.01 migration/6
   21 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/6
   22 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/6
   23 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 migration/7
   24 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/7
   25 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/7
   26 root      RT  -5     0    0    0 S  0.0  0.0   0:00.05 migration/8
   27 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/8
   28 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/8
   29 root      RT  -5     0    0    0 S  0.0  0.0   0:00.05 migration/9

Comment: Well, you need to find out what queries are slowing the site down.  Check the slowqueries log `/var/log/mysqlslowqueries.log`
Comment: I chek it but nothing in this file
Comment: Then what makes you think it's the database that's slowing it down?  What do you see if you run `top`?
Answer: If you don't know where to start, then start with MySQLTuner.
You just download and run the perl script.
It asks for the mysql administrator's username and password and then lists some General statistics, then storage statistics and then performance metrics before finally giving you a list of recommendations.
It saves hours on the initial review of your server instance.
Once you have fixed things, I would suggest running the script every hour or so for a week ( maybe less often / maybe for longer, depending how busy your database is ) to see if any settings need changing after being run for real.
Also have a look at resources like this.
Once you know your MySQL instance is sane, you will need to optimise the specific queries in your application code, you can identify the worst offenders by checking out the slow query log (if anything is in there) and using a profiler like XDebug.
Comment: +1  rackerhacker's scripts are great; I've used them in the past on a small and large MySQL server installs, and i've always seen better performance from them.
Comment: +1 Agreed. Major even posted a comment on my one week old blog, so obviously has an interest in what people think of his scripts and is still looking at ways they can be improved.
Answer: The first problem that you have is that you are running on a 32bit OS.  You have 12G of Ram (if I read your post right) and you will never see Mysql use that much because your running 32bit.  Upgrade Centos to a 64bit OS, and then start using the my.large.cnf file that they provide. That should get you going in a much better direction. If you are still having problems, then things to be evaluated such as is this mostly reads, writes, both. That can tell you that you should be using Innodb or MYISAM.   Innodb is going to be good for lots of updates/writes and reads, where if everything is mostly reads then MyISAM is a good choice.  We'll need to know this as well to help figure out Key Cache, etc. but the first thing is getting the OS to use all that RAM.
Comment: if the `hugemem` kernel is installed, he  can access the 'excess' memory
Comment: Not all of it, and it will be application dependent. That means then you can access that memory but are having extra io as the OS is trying to swap the memory locations back and forth since it's mathematically above what it can support.   This is exactly like enabling PAE on windows.  You only get so far and in heavy usage it can become a hindrance instead of something that helps.
Answer: show status
show variables
show processlist

Are you using php-cgi? fcgid or fastcgi?
Comment: But when i use command :mysqld
many error .
Comment: Wrong command.  `mysqld` starts a new server daemon.  `mysql` starts a client to connect to the server daemon.  `mysql` is the one you need here.  You can run the client and the daemon on the same machine.

The most likely error you will get when you type `mysql` is Access denied.  If that happens, try the -u and -p options to add an appropriate username and password.
Answer: You've not got a lot of users on via mysql yet its consuming 98.7% of your CPU in the screenshot of 'top' is this representative of the way the system is behaving most of the time? If so then something is very wrong (and the high CPU usage is a key symtom of the actucal problem). Even under heavy load with lots of memory, mysqld should be spending a lot of its time in I/O wait.
This bug describes problems with high CPU using 64bit MySQL - although an older version.
What does mysqladmin processlist show?
Answer: Have you taken a look at the running MySQL processes inside mysql? (i.e. via mytop?)
The high CPU load of the MySQLd process gives me the shivers...

MySQL Optimizing


Store PS command output to variable and Invoke-Command

Question: I need to store command output to pass it as variable in function later
#This works
[scriptblock]$command = {Get-EventLog system -newest 1  | Format-List}
$command.Invoke()

But when I try to Write-Host it fails
#This works
[scriptblock]$command = {Get-EventLog system -newest 1  | Format-List}
Write-Host $command.Invoke()

with output
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData

Line of the script in which I'm trying to use it is
$SMTPMessage = New-Object System.Net.Mail.MailMessage('UserName email@example.com','firstname.lastname@example.com','subjectText',($command.Invoke()) )

Thanks
Answer: {ScriptBlock}.Invoke() returns a System.Collections.ObjectModel.Collection<PSObject> which is getting mangled by the Write-Host command.
If you use InvokeReturnAsIs() you get correct results:
PS C:\> [scriptblock]$command = {Get-EventLog system -newest 1  | Format-List}
$command.InvokeReturnAsIs()

Index              : 141723
EntryType          : Information
InstanceId         : 1073748860
Message            : The Application Experience service entered the stopped state.
Category           : (0)
CategoryNumber     : 0
ReplacementStrings : {Application Experience, stopped}
Source             : Service Control Manager
TimeGenerated      : 9/8/2016 9:32:02 AM
TimeWritten        : 9/8/2016 9:32:02 AM
UserName           :

Answer: Format-List return formatting objects. You need Out-String cmdlet to convert them to string:
$OutputAsString = $command.Invoke() | Out-String

Then you can pass that string into methods, which expect string as their input.


app appears different on the simulator than in story board

Question: I just got into iOS development and I'm trying to build a to-do list app. I made the basic layout, but when I run it on the simulator it looks different than from what I see on the storyboard.
Basically, how I did it was that I put a UIView and UITable view in one stack view and pinned it to the 4 edges of the screen. I then set the alignment to "fill" and the distribution to "fill proportionally".
On the storyboard, the UIView is on the top 20-25% of the screen and the rest is the table view. On the simulator, however, it looks like they each take up half of the screen.
here's an image of the simulator and the storyboard showing the difference
Answer: This is due to the fact that you are depending on the stack view to layout the UIView and UITable for you.  If you take them out of the stack view, you can specify a specific height for the top area (UIView) and pin the bottom of the top area (UIView) to the top of the bottom area (UITableView).
Alternatively, you could keep them in a stack view and change the distribution to "fill" and the Content Hugging Priority of the UITableView to be less than the UIView.  That will make the UIView as big as it needs to be and let the UITableView take up the rest of the space, which sounds like what you want.  This is a good write up of the different distribution possibilities for a stack view:
https://spin.atomicobject.com/2016/06/22/uistackview-distribution/
Comment: I did the second option (where u keep it inside the stack view), but I got an error saying that it needs a y position for both the UITableView and the UIView. If I let it fix the error, then I get other constraint errors when I switch to a smaller screen.
Comment: That has nothing to do with what I was talking about.  You need to pin the Stack View to the top/bottom/left/right of the parent view.  You need to have something within the UIView, pinned to the UIView top and bottom, or you need to specify a height or minimum height for the UIView.  It's really hard to give more specifics without knowing what you are trying to do.
Comment: Based on your image, it looks like you want a header.  Well, that header should have a fixed height.  It sounds like you are not specifying a height for the UIView, nor anything within the UIView.


Sobrecarga do operador = em c++

Question: Estou tendo um problema ao implementar a sobrecarga do operador = em uma classe heap.
  Heap& Heap::operator=(const Heap& outro){
  printf("chamei atribuição\n");
  this->escreve();

  Heap *h = new Heap(outro);
  printf("htemp\n");
  h->escreve();

  return *h;
}

Por exemplo: chamo h3 = h1 na main, onde os mesmos são classes heap, que possuem um vetor.
Quando faço a impressão dessa heap intermediaria, dentro da sobrecarga, os valores estão corretos, porém, após o objeto ser devolvido para a main, permanece inalterado, com o valor que inicialmente  estava, ou seja, com o valor que h3 estava antes da chamada de sobrecarga.
Espero ter sido claro em relação ao problema. Obrigado Desde já.
Comment: Estou meio enferrujado em C++ mas acho que não é correto retornar um objeto de heap (alocado com new). Além do mais, a ideia do retorno em operator= é retornar *this. Não entendo pq criar outro objeto.
Answer: O código apresentado tem alguns problemas. São eles:

O retorno do operador de atribuição (operator=) deve ser, na maioria esmagadora das vezes, *this. Não vou entrar nos detalhes de quando isso pode ser diferente, mas pelo nível da pergunta eu apostaria que você se enganou ao retornar uma referência para outro objeto;
Seu código provavelmente está vazando memória. Baseado na sua pergunta não vejo sentido em fazer uma alocação de um novo objeto no operador de atribução;
Dentro do operador de atribuição geralmente os valores do objeto outro são atribuidos ao objeto this. Isso é tão simples quanto atribuir uma variável a outra: membro = outro.membro;

Dito isso, sugiro trocar o código do seu operador de atribuição para:
Heap& Heap::operator=(const Heap& outro){
    // Aqui você atribui os membros do objeto "outro" aos membros do objeto "this".
    membro1 = outro.membro1;
    membro2 = outro.membro2;
    membro3 = outro.membro3;
    membro4 = outro.membro4;
    return *this;
}

Se você quiser passar a responsabilidade de "copiar" as variáveis do outro para o método escreve() não há problema nenhum, mas você deve passar a referência do objeto de origem. Exemplo:
Heap& Heap::operator=(const Heap& outro){
    printf("chamei atribuição\n");
    this->escreve(outro);
    return *this;
}

É claro que para isso funcionar você deve antes modificar ou sobreescrever o método escreve() que você já implementou.
PS: O uso do "prefixo" this-> é opcional.

Sobrecarga do operador = em c++


Turn off Twig cache in general

Question: Drupal does cache Twig templates. Normally, it is a good thing, as long as you don't have a multilingual site.
In my case every t()is not working as the template is cached in the wrong in English, giving German no chance to ever be outputted.
Is this the proper way to deal with t() calls?
 $settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

It seems a bit wrong. As I understand these settings are more for the development process, not for production. Also there really should be a setting to use caching in a language specific way.
Comment: Have you thought of including the language in cache tags?
Answer: In Drupal cache content is language specific. Default is a cache context for the language. Sometimes you need to clear the cache one more time, but then this should work.
You can check this in the database table cache_render. All cid's should contain [languages:language_interface]=langcode and if you visit the same page in different languages you should see multiple cache entries.
Disabling the cache with a null backend is only for development. For production you need to find a solution for the language specific caching issue.
Comment: Thanks for your answer. I have seen that block do have two types of caches. One for the build array and another one for the block itself (theme, label). How to set the build cache I understand - do you know how to set the other one?
Answer: You can pass options to t, specifically the langcode. I believe that would look something like this:
{{ 'Your text here'|t({}, {'langcode' => node.language().getId()} }}

Untested, so the syntax might be slightly different. Also not sure what woud happen if you pass in a node that has a language like "not specified", it would probably fail.

Turn off Twig cache in general