InAppPurchase verification & separate server for game logic

Question: I'm developing an app using Unity (for Android and iOS). I'm using the SOOMLA plugin to allow users to purchase Gems (virtual currency) with In App Purchase.
Users and Gems and all other game logic go through my server on Azure.
I want the following procedure to take place as a single transaction in some way:

User buys Gems with IAP
App notifies server
Server validates the purchase and updates data

But if the internet connection breaks down between step 1 and step 2 - the user payed for Gems that he did not receive (not good!)
So my current approach is this:

User initiates a purchase
App notifies the server
Server blindly updates data accordingly
User buys Gems with IAP
If the purchase is cancelled, notify server to undo it

That way, the user is guaranteed to get his purchased Gems, but I am not guaranteed to get paid (not great...)
Note: I don't want to manage user Gems in the store itself. I want everything on my own server. So the SOOMLA's balance is meaningless to me. I don't care for it.
I was thinking maybe the app can store the purchase data in persistent storage until it manages to notify the server about it, and then delete it. But I was also thinking that this might be a bad solution. Hence this question.

I imagine the best solution as something that will properly handle this scenario:

User buys Gems with IAP
IAP succeeds
Internet breaks down
My own server isn't notified
User uninstalls app from his device
User may then install the app on other devices:

Either he was charged and he got the gems by some magic
Or he was refunded automatically, since the gems were not received

So far it seems like this is impossible by any means, which makes me disappointed with the technology of IAP's. Hoping for answers that will prove me wrong.

Seems like all I'd ever need is the ability get a user's purchase history from my server, with a secured request to Google Play or Apple Store. But that's just not part of the framework.

So what are others doing? What is the best approach?
Comment: My understanding is that SOOMLA does a check on game start to see if any items were purchased but not processed yet. And if it finds some then it runs the code and you will have a OnPurchase event fire. answers.soom.la is the dedicated forum for SOOMLA related questions. You might get a quicker answer there.
Comment: @cocojiambo Yes, as a part of the SOOMLA's purchase verification process. The problem is when SOOMLA has completely processed the purchase and the OnPurchased event has already fired. This is when I would logically notify my own server of the purchase and it's verification detailed provided by SOOMLA, but what if the connection breaks just before this step?
Comment: I see. Ok so here is some speculation... All purchases from Google have a unique token against them. It you were to hook onto the OnGameStarted event and as SOOMLA does its own checks between its records against the Google ones, you do your own at that moment, by checking all tokens against your server. Should be possible. This is me just thinking aloud here.
Comment: Looking for a solution that would handle the case where user uses device A to make a purchase, internet breaks down after purchase before notifying my own server, and then user uninstalls from device A never to be seen again. User may then install on new device, and either he wasn't charged and didn't get gems in the end, or he did. That would be the perfect solution I'm looking for, and that I expect this technology to have.
Comment: Refer to [How does Apple notify iOS apps of refunds of in-app purchases (IAP)?](https://stackoverflow.com/q/6439482/6521116)
Answer: In general, you seem to suffer from the Two Generals' Problem which was

the first computer communication problem to be proved to be unsolvable.

Since everywhere in your communication protocol a message can be lost (even the acknowledgement or the acknowledgement`s acknowledgement or the ...) you cannot be 100% sure that both communication parties (the user device and your server) have agreed upon the same state. You can only say that upon a certain probability the state information has been interchanged successfully.
I would send a couple of ACKs back-and-forth and store the purchase if a sufficient number got trough. Quote from Wikipedia:

Also, the first general can send a marking on each message saying it is message 1, 2, 3 ... of n. This method will allow the second general to know how reliable the channel is and send an appropriate number of messages back to ensure a high probability of at least one message being received

For customer satisfaction I would take the odds in their favor - 1% not delivered goods will get you in a lot of trouble but 1% loss on your side is acceptable.
Comment: So in short, there is no perfect solution. You will have the losses on your end and be fine with it, and do your best to minimize those possible losses. Is this correct?
Comment: exactly - it is surprising how many seemingly trivial problems have been proven to be unsolvable
Answer: Considering your Gems are a virtual currency, then the natural in-app product type should be consumable, i.e. they are not restorable purchases.
To consume a purchase with a Google Play purchase you will call consumePurchase. On iOS you will call finishTransaction on the SKPaymentQueue. In both marketplaces the consumable purchase will remain in an active state until they have been consumed. If the user deletes the app off their device before the purchase is consumed, they will be able to re-install, and restore their previous unconsumed purchases.
In-between the initial purchase and consumption is where you want to put your server-side validation. When a purchase is made, send the receipt or token to your server, perform the validation and respond accordingly. The app should wait for a valid response from the server before consuming the purchase.
(Note that consumed purchases will not appear in the in_app collection on an iTunes receipt. They are only present if the purchase has not been consumed yet).
If the server is timing-out or network connectivity is lost the purchases will remain in an active state and the app should continue trying to send the details periodically until it receives a response it is expecting.
Purchases for both Google Play and iOS are stored locally so you will just need to run a process that looks for unconsumed purchases once network connectivity is re-established.
You can treat the provisioning of Gems in the same way a bank handles deposits of cheques; the new balance will be immediately updated but the amount spendable will not match until the cheque (or in your case a validation) is cleared.
Some pseudo code to make the process clear:
Purchase product or Restore purchases
While consumable purchases > 0
  Send purchase receipt to API
  If response is ok
    If purchase is valid
      Consume product
      Allocate gems
      Break
    Else
      Remove retroactive gem allocation
      Discipline the naughty user
      Break
  Else
    Retroactively allocate un-spendable gems
    Pause process until network is re-established
      Re-send receipt to API

Comment: Sounds brilliant. Wondering if I can use SOOMLA without changing too much, if I can pretend its a non-consumable product, maybe it'll just skip the consumption part? I'll dig into the source code and find out. Seems like a good answer so far!
Comment: @SimpleVar I'm not familiar with SOOMLA but if you were to use non consumable products, you will need to ensure that when restoring purchases they do not allocate Gems that have already been allocated. This will need to be done on your server, where you keep a registrar of transaction/order ID's. Consumable products have the benefit of holding state of their consumption; thereby indicating if the server knows of the transaction and that the Gems have been allocated or not.
Answer: Some advice:

User buys Gems with IAP

IAP succeeds

Internet breaks down

My own server isn't notified

User uninstalls app from his device

User may then install the app on other devices:
Either he was charged and he got the gems by some magic, or he was refunded automatically, since the gems were not received.

At step 3, receipt information is stored on the user's device. If the user uninstalls and reinstalls your app, the receipt information will be lost. But you can restore it; Apple talk about that here. You can resend a restored receipt to your server. At your server, you verify that plus Gems for the user so he can receive what he should be.

he was refunded automatically, since the gems were not received

This seem impossible with IAP because Apple does not allow a user to cancel their purchase. (With Google IAB, refund are allowed, more about that here)
Comment: So there is no cross-platform solution? I'd imagine if only I could simply access the purchase history of each user from my secured server with some store API, it'd be all very easy. But it seems that this is impossible.
Answer: You can try restoring the In App purchases made with a particular account.
The feature is given just for this case when either the payment was made and user didn't receive the items he was promised or when he switched device.
Upon restoring the purchase you'll receive the purchased product again from the iTunes server and then you can accordingly notify your server.
Answer: I don't have much knowledge about android but after reading your post I was really interested to search keenly and more over how game like clash of clans in app purchase logic work and prevent freedom fake purchase hacks.
After doing some research I would like to share my thoughts over your problem, You can implement it by following approach:
Make your in app purchase verification completely online. For e.g., you can consider the game clash of clans.
How it works:
1)Game loads, synced with server. Network connection required, as network connection breaks game reloads from server.
2)User have 10 gems, server also have 10 gems.
3)User purchased gems, server verify purchase separately for the purchased consumables, gems credited to the users account.
4)If in can case network fails, then also server can verify purchase and later on it update it in account of user, whether it is on any device.
5)This will also help you to bypass many fake in app purchase hacks like freedom (prevention) and lucky patcher.
Google provide api for server side to verify or get purchase detail and when purchase from application side and server side match then only you credit gems or consumable item into users account.
For more information about in app purchases and their hack preventions you can visit these links:
1)Server side verification of in app purchase part 1 and part 2.
2)How would you verify in app purchase billing server side.
3)Verify purchase via PHP.
4)Secure in app purchase scenario on web server.
Hope this might lead you in direction which you want to go, also I would love to hear your thoughts over the same.


Kentico CMS & Campaign Monitor

Question: Just wondering if anyone has any pointers on how to integrate campaign monitor with Kentico CMS. Kentico has its own built in newsletter signup but I want campaign monitor to be updated when a user suscribes. I tried simply modifying the aspx page with a campaign monitor html form but when I try and submit an email address nothing happens.
When I view source kenitco is surrounding the page contentwith a form which I believe is affecting the CM form.
I have tried adding a static html page but the form action is somehow being modified to point to the active page instead of pointing to Campaign monitor subscription page.
Any help would be greatly appreciated.
Answer: I have solved this. The problem was that aspx pages do not allow you to submit a form other than to itself.  I modified the template to be a asp page and it now serves the form fine.
This maynot be the best solution but works for me!

Kentico CMS & Campaign Monitor


Advice on Setting up Proper Permissions on Ubuntu 10.04 Server

Question: I run a server powered Ubuntu 10.04 LTS. the software stack that I use comprises of

PHP fpm-fcgi Version 5.3.6
Web Server:   nginx/0.8.54

To make, things like wordpress work properly. I have done chown www-data:www-data on my public_html folder. This was all files are easily modifiable by nginx and things like Auto Updating wordpress work as expected.
The problem arises when I login via ftp and try to upload new files or change existing ones. Since, I use username as Gaurish & all files are owned www-data my requests are denied.
Response:   220 (vsFTPd 2.2.2)
Command:    USER gaurish
Response:   331 Please specify the password.
Command:    PASS ************
Response:   230 Login successful.
Command:    OPTS UTF8 ON
Response:   200 Always in UTF8 mode.
Status: Connected
Status: Starting upload of /tmp/cachegrind.out.5513
Command:    CWD /home/gaurish
Response:   250 Directory successfully changed.
Command:    PWD
Response:   257 "/home/gaurish"
Command:    TYPE I
Response:   200 Switching to Binary mode.
Command:    PORT 192,168,1,6,214,6
Response:   200 PORT command successful. Consider using PASV.
Command:    STOR cachegrind.out.5513
Response:   553 Could not create file.
Error:  Critical error
Status: Disconnected from server

I need a way by which I(gaurish) & nginx(www-data) can both modify the files. Any idea how to do that?
Answer: You need to do a few things to get a complete solution:

Add your user to the www-data group (as per @fbh's answer).
Configure the FTP server umask to 007 so that files are uploaded with group write privileges.
Set the setgid flag (g+s) on your directories such that new files are group-owned by the directory's group, rather than the file creator's primary group.
Answer: Shared group is one way as others have previously answered.
A more complex alternative would be to use a source revision control system like CVS, Subversion, GIT, etc.. Then a single admin cgi page,running as the webserver, can update all modified files on the server. This also ensures that you have a backup of the previous file before you overwrite it, given that source control is enforced.
Whatever you do, learn and use SSH and SCP not FTP. FTP should be used for the convenience of your users if absolutely needed, not for system administration.
Comment: Any idea how can I run nginx can user, instead of www-data?
Answer: Add your user to the webservers group
usermod -a -G www-data gaurish


Install WebSvn to use an Invalid SSL Certificate

Question: Im trying to Install WebSvn on my server but the repositories im trying to connect to have a self signed certificate so websvn is unable to connect.

Server certificate verification failed: certificate issued for a different hostname, issuer is not trusted

Is there some sort of setting im missing in setting this up?
Answer: i don't know websvn, but your problem is: the self-signed certificate is not trusted.
do you get this message in your browser or on the server?
when in the browser, import your self-signed as trusted to the browser cert store. when it is on the server, try to add SSLCACertificateFile to your apache config.
Comment: Yeh i get that same message when viewing the Repo in either a browser or tortiosesvn but they have the ability to accept it anyway. I dont wuite understand how i go about adding the cerfificate file to my apache config, can you help me out with this?
Comment: this blog post gives you a step-by-step guide: http://blog.laimbock.com/2007/08/28/svn-and-websvn-over-ssl-with-self-signed-certificates/


how can i assign the hash code of any object into the instance veriable of the same object?

Question: how can i assign the hash code of any object into the instance veriable of the same object at compile time ?
Answer: You can't. Objects don't exist at compile time.
Comment: how can i do the same at run time? as the objects creates all the hash code of these objects should be stored in the instance veriable array?
Comment: I think you really need to state your question better. What exactly are you trying to do? what is the "instance variable array" you mention - you never mentioned an array previously.

If for instance you want an array with the hash of a series of objects, then you just need to store the hash in the array as you create the objects.


Prove that a quadratic form is harmonic if and only if its integral over a sphere is $0$.

Question: Lef $F:\mathbb{R}^n\rightarrow \mathbb{R}$ be a quadratic form. I have to prove that $F$ is harmonic ($\Delta F=0$) if and only if
$$\int_{\mathbb{S}^{n-1}(1)}F|_{\mathbb{S}^{n-1}(1)}dv_{g_0}=0.$$ Where $dv_{g_0}$ is just a way of saying "the usual metric".
I'm trying to apply the divergence theorem to prove it, but I not able to choose a vector field which gives me the aswer. I'm also trying to write $F$ as $F(v)=\langle f(v),v\rangle$ with $f$ self-adjoint, but nothing really works. i also tried this (but don't know how to continue) using the divergence theorem in the first equallity:
$$0=\int_{\mathbb{S}^{n-1}(1)}\Delta F=\int_{\mathbb{S}^{n-1}(1)}div(\nabla F)=\int_{\mathbb{S}^{n-1}(1)}div(\nabla \langle f(v),v\rangle)$$
Any hint?
Comment: We have to show this with the following formula $\Delta (F|_{\mathbb{S}^{n-1}(1)}) = (\overline{\Delta} F)|_{\mathbb{S}^{n-1}(1)}) - 2(n-1)(F|_{\mathbb{S}^{n-1}(1)})$.
Someone who has an idea or hint to show the same proof with this?
Comment: I've already found a solution or let's say I think that I've found it.
Answer: Assume that we have an orthonormal basis, and that  $F(x)=\sum_{i,k=1}^n a_{ik} x_ix_k$ with a symmetric matrix $A$. Then it is easily computed that $\Delta F(x)\equiv2\,{\rm tr}(A)$; hence $F$ is harmonic iff ${\rm tr}(A)=0$.
On the other hand, integrating $F$ over a spherically symmetric domain $S\subset{\mathbb R}^n$ leads to $$\int_S x_i\,x_k\> {\rm dvol}=0\quad(i\ne k),\qquad \int_S x_i^2\>{\rm dvol}=c\quad (1\leq i\leq n)\ ,$$
with some constant $c\ne0$. It follows that $$\int_S F(x)\>{\rm dvol}=c\>{\rm tr}(A)\ .$$


Shipping line items not displaying with [commerce-order:line-items]

Question: I have Commerce and Shipping and Flat Rate installed and working correctly. Shipping is added to an Order and charged to the customer.
But when I go to display the shipping line items, e.g. in a order receipt email, they are missing.
My Rule uses this:
Order Details:
[commerce-order:commerce_line_items]

Total Price: [commerce-order:commerce_order_total]

This correctly displays each product line item, and the order total, including shipping line items, but does not display the shipping line items.
I read about a filter in the View at admin/structure/views/view/commerce_line_item_table/edit/default and I see that it is set to only display product line items. But I don't want to mess with that View unless I know what I am doing.
Can I Add a new View display? If so, which kind (Data export?) How to call that display in my Rule?
Thank you.
Answer: If you enable the Token module (maybe you already have?), you are given a new view mode on all fieldable entity types called Tokens. You need to enable this in the Custom display settings fieldset on the Manage display page for order types (found under Administration > Store > Configuration > Order settings). Once you've done this, you'll be able to choose any eligible View you want for rendering the line item field's tokens, so if you add another display to the default View and remove that line item type filter, you can use that just for rendering the token.
You can read more w/ screenshots here: https://www.drupal.org/node/1299662
Comment: OK. Tutorial published on my blog at http://websitebackendsolutions.com/blog/how-display-all-drupal-commerce-line-items-using-token . I will also update the Shipping doc page. Thanks Ryan!
Comment: Thanks Ryan, I get it, but what type of display should I select when adding the display to the view?
Comment: Well, I just tried a few, Block seemed to be the one. It certainly worked and I got the shipping line items in the [commerce-order:commerce_line_items] token, as I expected. That was some deep juju to accomplish what would seem to be a very common task, though ... Or am I missing something obvious?
Comment: Common task it may be, but it's not immediately obvious how it should be improved. I don't know a more explicit way given the current Field UI / Token constraints to choose different display formatters for a field based token. : )
Comment: Ryan, can I ask what is the rationale behind not including all line item types by default? Also, if I wrote up a little tutorial on this, where should I put it?
Comment: The View you're using is used just to show the cart contents, so we filter out non-product line items. The shipping total then appears in the footer of the View alongside taxes where the order total price components are displayed. We could've used multiple Views to accomplish a better behavior, but we also don't have the Shipping module in core and altering Views is unreliable (since it may have been customized in the UI prior to installing Shipping). A tutorial would be great! Do you have a blog? If not, use a child page in the Shipping 2.x documentation: https://www.drupal.org/node/1296716


Combobox doesnt initialize with SelectedValue WPF

Question: Having issues getting my Combobox to Initialize with the current selecteditem.
ClinicList returns ObservableCollection<Clinic>
      <ComboBox ItemsSource="{Binding Source={StaticResource ClinicList}}"
 DisplayMemberPath="Name" SelectedItem="{Binding SelectedClinic,
    UpdateSourceTrigger=PropertyChanged}"></ComboBox>

My ComboBox is within a listview bound to an employee. An employee can have multiple Clinics assigned.
public class Employee{
...
public ObservableCollection<ClinicView> EmployeeClinics { get; set; }
}

public class ClinicView:INotifyPropertyChanged {

        private Clinic selectedClinic;
        public Clinic SelectedClinic {
            get { return selectedClinic; }
            set {
                selectedClinic = value;
                OnPropertyChanged("SelectedClinic");
            }
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName) {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
public class Clinic{
   public int ClinicId{get;set;}
   public string Name {get;set;
}

The combobox shows as blank when first loaded, and will have the correct # of clinics per Employee as assigned. The combobox drop down has the correct Clinics in it, and when I select one, it replaces the blank combobox text and updates the employee correctly.
My question is why doesnt the Combobox Initially fill with correct text and is blank?

Edit:
ClinicList is being set via the ODP in window.resources
 public static ObservableCollection<Clinic> GetClinicList() {
//gets list of clinics from db with ID
}

 <ObjectDataProvider MethodName="GetClinicList"
                            ObjectType="{x:Type local:MappingGenerators}"
                            x:Key="ClinicList">
        </ObjectDataProvider>

Comment: Are you setting the `selectedClinic` property during initialization?
Comment: Can you please show where you are declaring ClinicList?
Comment: Yes, just confirmed. I wouldnt even have blank comboboxes if not, right? @SaagarEliasJacky
Comment: @markokstate if the `selectedClinic` is not set, the ComboBox won't show anything selected; doesn't mean the ComboBox will not be initialized. I think the `selectedClinic` property is being set as null during initialization and so not showing up...
Comment: If I break during runtime my Employee.EmployeeClinics contains the items I expect to see. I'm wondering if its with the Combobox SelectedValue/SelectedValuePath options?
Answer: Found this post to be a life saver.
https://rachel53461.wordpress.com/2011/08/20/comboboxs-selecteditem-not-displaying/
Here are the key passages:

The problem is simple: By default WPF compares SelectedItem to each
  item in the ItemsSource by reference, meaning that unless the
  SelectedItem points to the same item in memory as the ItemsSource
  item, it will decide that the item doesn't exist in the ItemsSource
  and so no item gets selected.
To work around this, you can either use the ComboBox's SelectedValue
  and SelectedValuePath to set the SelectedItem by Value instead of by
  Item


What is the Fourier series of Dirichlet function?

Question: Given the Dirichlet function defined as:
$$f(x) = \begin{cases}0 & x \in \mathbb{Q} \\ 1 & x \in \mathbb{R} \setminus \mathbb{Q}\end{cases}$$
Find the corresponding Fourier series.
Before starting, I believe that $f(x)$ is periodic for any real period greater than zero. Therefore I feel free to choose $2\pi$ as period. Hence the Fourier series will have the following form:
$$F(x) = a_0 + \sum_{n=1}^\infty a_n \cos nx + b_n \sin nx$$
With the following coefficients:
$$a_0 = \frac1{2\pi} \int_{-\pi}^\pi f(x) dx \\
a_n = \frac1\pi \int_{-\pi}^\pi f(x) \cos nx dx \\
b_n = \frac1\pi \int_{-\pi}^\pi f(x) \sin nx dx$$
My function is not Riemann-integrable, however is Lebesgue-integrable. Knowing the following theorem...
$$I = I_1 \cup I_2 \\
g(x) = \begin{cases}a & x \in I_1 \\ b & x \in I_2\end{cases} \\
\int_I g(x) dx = \int_{I_1} g(x) dx + \int_{I_2} g(x) dx$$
... I can easily calculate the coefficients $a_0 = 1, a_n = b_n = 0$. Therefore the Fourier series corresponding to the Dirichlet function is:
$$F(x) = 1$$
Which does not converge to the function.
This is what I discovered. Is my result exact? Is the procedure right?
Comment: First of all, your function is not $2\pi$-periodic, but it is periodic with any *rational* period. Otherwise it looks fine.
Answer: Since you define the Fourier series via Lebesgue integral, you should be able to observe the following general fact:

Suppose $f$ and $g$ are periodic of period $T$. Also suppose that $f$ is Lebesgue integrable on its period. If  the set $\{x:f(x)\ne g(x)\}$ has Lebesgue measure zero, then the Fourier series of $g$ is the same as the Fourier series of $f$.

Indeed, modifying a function on a null set does not change any of the integrals that determine the Fourier coefficients.
In your example $f$ is identically $1$, $g$ is the Dirichlet function, and $T$ can be any rational number.
Yes, the Fourier series of a discontinuous function need not converge to that function pointwise. (Even for some continuous functions the pointwise convergence fails, though examples are harder to come by.)


Estou tentando construir um gerador de apostas da Megasena com os seguintes parâmetros

Question: eu estou tentando construir um gerador de apostas da mega sena com os seguintes parâmetros:
quantidade de dezenas e quantidade de jogos
Regras:

Os números não podem se duplicar entre os jogos.
Considerar as dezenas entre 00 e 59.

Saída final:
  1 - Lista de jogos
  2 - Lista de números que se repetiram mais de uma vez

Ex: Considerando parâmetros quantidade dezenas = 7 e jogos = 2
Saída:
      3 - 5 - 7 - 9 - 20 - 55 - 56
      5 - 20-51 - 55 -56-  57 - 59

 Duplicados:

    55 - 2x
     5 - 2x

eis o código que eu construí até agora:
<?php

function getRandomNumbers($num, $min, $max, $repeat = false, $sort = false)
{
    if ((($max - $min) + 1) >= $num) {
        $numbers = array();

        while (count($numbers) < $num) {
            $number = mt_rand($min, $max);

            if ($repeat || !in_array($number, $numbers)) {
                $numbers[] = $number;
            }
        }

        switch ($sort) {
        case SORT_ASC:
            sort($numbers);
            break;
        case SORT_DESC:
            rsort($numbers);
            break;
        }

        return $numbers;
    }

    return false;
}

?>

<?php

if ($numbers = getRandomNumbers(6, 1, 60, false, SORT_ASC)) {
    print implode(', ', $numbers);
} else {
    print 'A faixa de valores entre $min e $max deve ser igual ou superior à' .
        ' quantidade de números requisitados';
}

?>

porém não consigo implementar as condições de restrição, podem auxiliar por favor?
Comment: Os jogos são sempre 7 números?
Answer: Se não pode repetir entre eles seria mais fácil definir quais são as escolhas possíveis, então:
$NumerosDiposniveis = range(0, 59);

Quando selecionar um deles, então execute:
unset($NumerosDisponiveis[$IndexQueFoiGerado]);

Basicamente isso:
function gerarCombinacao($QntDezenas, &$NumerosDisponiveis){

    $QntDisponivel = count($NumerosDisponiveis);

    if($QntDisponivel < $QntDezenas){
        return false;
    }

    for($n = 0; $n < $QntDezenas; $n++){

        $EscolhaAleatoria = random_int(0, $QntDisponivel - ($n + 1));

        $NumerosDisponiveis = array_values($NumerosDisponiveis);

        $Combinacao[] = str_pad($NumerosDisponiveis[$EscolhaAleatoria], 2, '0', STR_PAD_LEFT);
        unset($NumerosDisponiveis[$EscolhaAleatoria]);

    }

    return implode('-', $Combinacao);

}

Então poderia executar:
$NumerosDisponiveis = range(0, 59);

for($i = 0; $i < 10; $i++){
    echo gerarCombinacao(6, $NumerosDisponiveis) . PHP_EOL;
}

Teste isto.
No caso esse é o máximo de números que podem ser gerado, não é possível gerar mais de 10 porque não pode repetir os números segundo você em:

1 - Os números não podem se duplicar entre os jogos
Comment: esse resolveu o que eu precisava, muito obrigado!
Answer: Como os números não podem se repetir entre os cartões outra forma de fazer isso é gerar todos os números possíveis (0 a 59) embaralha-los e gerar o número máximo de cartões com sete números, nesse caso são oito. No final você pode escolher quais cartões vai exibir ou manipular.
O ponto chave dessa solução é o array_chunk() que quebra o array de números disponíveis por tamanho (7 elementos) e gera assim os cartões. array_pop() é responsavel por eliminar o último cartão já que ele possui apenas 4 elmentos.
Exemplo - idoene
<?php

function gerarCartao(){
   $numeros = range(0, 59);
   shuffle($numeros);
   $cartoes = array();

   $cartoes = array_chunk($numeros, 7);
   array_pop($cartoes);
   return $cartoes;

}

print_r(gerarCartao());

A saida é algo como:
Array ( [0] => Array ( [0] => 32 [1] => 55 [2] => 22 [3] => 53 [4] => 16 [5] => 38 [6] => 49 ) [1] => Array ( [0] => 44 [1] => 27 [2] => 28 [3] => 25 [4] => 47 [5] => 14 [6] => 48 )


Are PIDs in python multiprocessing map guaranteed to have increasing order with respect to an iterable?

Question: I have a list that I multi-process in chunks, let's say it's:
l = range(100)

Let's assume I have 10 cpus (as in multiprocessing.cpu_count()). List l is divided into 10 chunks and then each process is responsible for each chunk through foo function:
#pseudocode
def foo(chunk):
   pid = multiprocessing.current_process().pid
   for elem in chunk:
      pr_elem = process(elem)
      write pr_elem  in ('out' + str(pid)) file

from more_itertools import divide

p = multiprocessing.Pool(multiprocessing.cpu_count())
p.map(foo, divide(multiprocessing.cpu_count(), l))

Is the order of assigned PIDs always of increasing order with respect to iterable? Can I rely on this order and combine the results from out[pid] files and the order will match chunks (and hence elements) in list?
Answer: Definitely not. I have 8 logical cores, 4 physical cores. In the following code I create a pool of size 4 and invoke the map function specifying a chunksize of 1 so that a worker function only grabs one submitted task at a time. I also insert a sleep call immediately as the first instruction in my worker function to ensure that a given process does not process all the submitted tasks. This way I expect each of the 4 processes in the pool to process one submitted task each and that the print statements will occur in the order of submitted tasks.
import multiprocessing
import time

def worker(x):
    time.sleep(1 + x)
    print(x, multiprocessing.current_process().pid)

def main():
    pool = multiprocessing.Pool(4)
    pool.map(worker, range(4), chunksize=1)

if __name__ == '__main__':
    main()

Prints:
0 26632
1 15800
2 25916
3 30356

Comment: Thanks. I made a similar experiment few times but I have always received increasing order, hence the question
Comment: I ran this 5 or 6 times and *never* got them in increasing order, although it could certainly happen and should happen now and then. Perhaps there was something amiss in your experiment.
Answer:
Is the order of assigned PIDs always of increasing order with respect to iterable?

No, it's not guaranteed.
You can use enumerate to have 2-tuples passed to your function, from which you can easily extract the order.
def foo(job):
   chunk_num, chunk = job
   # ...

n = multiprocessing.cpu_count()
with multiprocessing.Pool(n) as p:
    p.map(foo, enumerate(divide(n, l)))

Comment: Thanks. Can you briefly tell me why it is not guaranteed?
Comment: Nvm. I see it now


How can I limit the bandwidth over an SSHFS mount?

Question: I am transfering large media files to my local machine over an SSHFS mount. I am not too concerned about speed and want to provide a better QOS to other network services and so want to limit the bandwidth for these transfers.
I can't seem to find an option for this in the man page - how can I achieve this?
# uname -a
Linux core1 4.19.68-coreos #1 SMP Wed Sep 4 02:59:18 -00 2019 x86_64 Intel(R) Core(TM) i5-6600K CPU @ 3.50GHz GenuineIntel GNU/Linux

Answer: One way would be to use a combination of iptables MARK to tag traffic to/from port 22 and traffic control tc to limit the egress rate of matched packets using a suitable qdisc.
An easier if less flexible way to get this working would be to pipe your sending process through pv (pipe viewer) using the --rate-limit and --buffer-size switches: https://linux.die.net/man/1/pv. You don't say if you're using a cli command like cp or rsync, or a gui desktop drag-and-drop to transfer your files. Using pv is likely a nonstarter if you're using a gui.
Comment: 2 very cool answers, I'm using [sonarr](https://github.com/Sonarr/Sonarr) so I guess just cp from the filesystem's perspective - I'll try the first one as the second one would be tricky with sonarr
Comment: Could you post an example for `tc` and/or add more relevant documentation?


How to sustain bends and vibrato better in the guitar?

Question:
Possible Duplicate:
What factors affect a guitar's ability to sustain a note?
How to play a long sustained note on an electric guitar?

My guitar, a Washburn N2, doesn't have a good clean sound for bend and vibrato - the sound with my amplifier "dies" a lot. I tried a distortion pedal and bends sustain much better but I don't like the sound of distortion, I want it powerful and clean. Is there another effect that don`t make sound of the guitar too artificial , just clean and bright and good for bending and vibrato? My amplifier has an overdrive but is not very good either.
Answer: A compressor pedal can add sustain to your signal without distortion.
I would also consider having your guitar set up by a professional as there are several factors of a good setup that can improve sustain such as string height and pickup height. Higher action may help sustain by preventing unwanted vibration from the string against the fretboard and lowering pickups can reduce the magnetic pull on the string allowing the string to vibrate longer. Also problems at your nut or bridge can take it's toll on sustain.


Como converter java.util.Date para java.sql.Date mantendo-se horas, minutos e segundos?

Question: Estou fazendo um programinha em Java. Nele eu recebo uma data do tipo java.util.Date, porém preciso dele em java.sql.Date para que possa inserir no banco de dados. Mas, nessa data, eu tenho hora, minuto e segundo e eu gostaria de inserir com todos esses dados. Alguém sabe como fazer isso?
Obrigado.
Answer: Para converter de java.util.Date para java.sql.Date:
java.util.Date a = ...;
java.sql.Date b = new java.sql.Date(a.getTime());

Para inserir com data e hora, utilize a classe java.sql.Timestamp:
java.util.Date a = ...;
java.sql.Timestamp b = new java.sql.Timestamp(a.getTime());

Você pode utilizar um Timestamp com um PreparedStatement no método setTimestamp(int, Timestamp). Para obter um desses de um ResultSet, utilize o método getTimestamp(int) ou o método getTimestamp(String).
Comment: Perfeito cara, resolveu meu problema! Muito obrigado


Pattern in lua with anchors not matching

Question: Why this does not match? I want to match the exact pattern 2 letters followed by 3 numbers
   s = "dd123"
   for w in string.gmatch(s, "^%a%a%d%d%d$") do
      print(w)
      matched = true
    end

Comment: What's your expected output?
Comment: single liner works. `print(string.match("dd123", "^%a%a%d%d%d$"))`
Comment: Its funny this shows up in the `regex` SO filtered tag.
Comment: @sln It had the regex tag, but I edited it out.
Answer: If you just want to see if a string matches a pattern, use string.match instead.
s = "dd123"
print(string.match(s, "^%a%a%d%d%d$")) -- dd123

string.gmatch is for finding all matches in a string, and doesn't work correctly with ^ and $.
Comment: `string.gmatch` doesn't allow anchoring to the start of the string (this is in [the manual](http://www.lua.org/manual/5.1/manual.html#pdf-string.gmatch)). I'm not sure why `$` is allowed though.
Comment: Why `string.match` fails in the for loop?


Standard form numbers and algebra

Question: I have a situation like this:
$3\times 10^n$ and $4\times 10^m$ and $a\times 10^p$ are all in standard form.
$$\frac{3\times 10^n}{4\times 10^m} = a\times 10^p.$$
Is it possible to calculate the value of $a$ only by using the above equation and no further data? And if so what is the value of $a$?
Comment: Standard form is?
Comment: In my neck of the woods, your "standard form" is our "scientific notation".
Answer: You have
$$\frac{3\times 10^n}{4\times 10^m} = 0.75\times 10^{n-m}.$$
If by "standard form" you mean that the the number $a$ must be between $1$ and $10$, strictly less than $10$, then $a = 7.5$ with $p=n-m+1$

Standard form numbers and algebra


Volume of a hypercone with cap

Question: What is the volume of this region:
$
C(x,\eta(x),\theta,r) = \{{x+\lambda y\in\mathbb{R}^d: y\in\mathbb{R}^d, ||y||_2=1, y^T\eta(x)\geq \cos\theta, \lambda\in[0,r]\}}
$
where $\theta\in(0,\pi/2)$, $r>0$ and $\eta(x)$ is a unit vector. This is a rather abstract way of writing down a cone with a cap (but also in higher dimensions).
For instance, for $d=1$ (then $\theta=0$) the region is a line of length $r$.
For $d=2$ the region is the segment of a circle with area $\frac{\theta}{\pi}\pi r^2 = \theta r^2$.
I'm after a general formula for $d\geq 2$. A first guess would be
$
Vol(C(x,\eta(x),\theta,r)) = \left(\frac{\theta}{\pi}\right)^{d-1}\sigma_d r^d
$
where $\sigma_d$ is the volume of the $d$-dimensional unit ball. Can anyone show this?
Comment: How does $\eta(x)$ depend on $x$? Do you mean it's just $x$ after normalization?
Comment: So $\eta(x)$ is just a unit vector that does depend on $x$. It tells you the direction that the hypercone is pointing in. The volume of the region will be independent of $x$ and $\eta(x)$ but I had to put it in so to define the region.
Answer: This could also be described as a hyperspherical sector. Its volume is the same fraction of the volume of the entire hypersphere as the hyperspherical part of its surface is of the surface of the entire hypersphere. As explained in my answer here, the surface element is $(1-\cos^2\theta)^{(d-3)/2}\mathrm d\cos\theta=(\sin^2\theta)^{(d-3)/2}\sin\theta\mathrm d\theta=\sin^{d-2}\theta\mathrm d\theta$. This needs to be integrated from $\theta$ to $1$; I don't think there's a nice closed form for the result; at least it's not the one you conjectured.
Answer: Let the axis of the cone $C$ be the $x_1$-axis and assume for the moment $r=1$. Then the points $(t, x'):=(t,x_2,\ldots, x_n)\in C$ are characterized by
$$ |x'| \leq \min\{t\ \tan \theta, \sqrt{1-t^2}\}\ ,\qquad 0\leq t\leq 1\ .$$
It follows that a plane $t={\rm const.}$ intersects $C$ in an $(n-1)$-dimensional ball of radius $t\ \tan\theta$ if $t\in[0,\cos\theta]$, and of radius $\sqrt{1-t^2}$ if $t\in[\cos\theta, 1]$. It follows that the volume of $C$ is given by
$${\rm vol}(C)= \int_0^{\cos\theta}\sigma_{n-1}(t\ \tan\theta)^{n-1}\ dt + \int_{\cos\theta}^1 \sigma_{n-1}(\sqrt{1-t^2})^{n-1}\ dt=\ldots\ .$$
For arbitrary $r>0$ multiply this by $r^n$.
Comment: @alext87: I don't quite understand why you wrote this and/or it was accepted after I gave a much simpler answer -- does either of you think my answer is wrong?
Comment: @joriki: I don't remember whether your answer was already here when I started working on mine. Now that I have looked at it I'd say that your post about "hyperspherical sectors" referred to another framework, used different notation, and above all, didn't exactly match the sophistication to be expected from the OP.
Comment: I don't understand. What do you mean by "framework"? And where's the different notation? The only notation I used was $\theta$, which the OP introduced. Also I'm not sure that my post requires more sophistication, but that might depend on the perspective.

Volume of a hypercone with cap


getting output as json instead of array

Question: I am trying to match the input with the array of names that I have created. Here is the code for it:
<code>>>> from fuzzywuzzy import process
>>> names = ["Adam Gilchrist","Adam Barbar","Adam lobiof","Jaffer Wilson","Janet Wilson","Jane Cold"]
>>> process.extract('adamgilchrist',names)
[('Adam Gilchrist', 84), ('Adam lobiof', 50), ('Jane Cold', 50), ('Adam Barbar', 40), ('Janet Wilson', 30)]
</code>
What I am getting is just an array of the name and confidence.
I want to display the output as json something like this:
<code>{
  "results": [
    {
      "name": "Adam Gilchrist",
      "confidence": 84
    },
    {
      "name": "Adam lobiof",
      "confidence": 50
    },
    {
      "name": "Jane Cold",
      "confidence": 50
    },
    {
      "name": "Adam Barbar",
      "confidence": 40
    },
    {
      "name": "Janet Wilson",
      "confidence": 30
    }
  ]
}
</code>
I am using python 2.7 on windows 10 sysytem
Answer: You can use tuple unpacking in combination with a list comprehension for this:
<code>...
result = process.extract('adamgilchrist',names)
result = [{"name": name, "confidence": confidence} for name, confidence in result]
</code>
which results in
<code>[{'confidence': 84, 'name': 'Adam Gilchrist'}, {'confidence': 50, 'name': 'Adam lobiof'}, {'confidence': 50, 'name': 'Jane Cold'}, {'confidence': 40, 'name': 'Adam Barbar'}, {'confidence': 30, 'name': 'Janet Wilson'}]
</code>
You can reach your final result (if necessary) with minor adjustments:
<code>{'result': results}
</code>
which in turn is then
<code>{'result': [
    {'confidence': 84, 'name': 'Adam Gilchrist'},
    {'confidence': 50, 'name': 'Adam lobiof'},
    {'confidence': 50, 'name': 'Jane Cold'},
    {'confidence': 40, 'name': 'Adam Barbar'},
    {'confidence': 30, 'name': 'Janet Wilson'}]
}
</code>

getting output as json instead of array


What do these circled 7's and 8's mean?

Question: I'm playing Schubert's 3rd impromptu, op. 90 D899. There is a circled number precisely under each trill, shown below, and I don't know how to interpret them.

I read here that they might indicate a change in the hand's position, but then

I don't see why the circle notation would be restricted to trills (as the left hand has to do other brusque shifts) and, more importantly,
it doesn't explain the circled 7 and 8!

Certainly the circled 1, 2 and 5 match the fingering of their respective notes, but how should I interpret the other two numbers? I don't think that they mean intervals as they seem too vague a notation for that purpose and they don't sound right to me.
Any clue on how to play these trills?
Comment: Do they appear in ascending order through the piece? Can you give us any details about what edition you're reading from?
Answer: These circled numbers appear in the 1948 Henle edition edited by Walter Gieseking. They appear in numerical order except for no.1 which appears a second time between 6 and 7 (but over an identical measure). They presumably refer to editor's notes by Gieseking (and which are missing in the file on imslp.org).
The numbers are not in the newer Henle edition.

What do these circled 7's and 8's mean?


Google Maps changing placemark icon solely based on styleUrl string syntax

Question: I've written a simple script in python that converts a CSV file to a KML file using the simplekml package. It colors my points based on one of my data values.
My problem is this: I'm trying to use the smallest icon possible because my placemarks are very close to each other. This works well when the style ID is "icon-1739-7CB342" but fails (Google My Maps renders a drop) when the style ID is "2"
The result is that the default KML generated by simplekml cannot be rendered with the small point as I cannot control the IDs.
My code:
import simplekml
import pandas
import sys

infile = sys.argv[1]
outfile = infile.split(".")[0]+".kml"

def genkml():
    kml = simplekml.Kml()
    ok = simplekml.Style()
    ok.iconstyle.color = 'ff42b37c'  # Green
    ok.iconstyle.icon.href = 'https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png'
    ok.iconstyle.scale = 1

    rc = simplekml.Style()
    rc.iconstyle.color = 'ff00eaff'  # Yellow
    rc.iconstyle.icon.href = 'https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png'
    rc.iconstyle.scale = 1

    seco = simplekml.Style()
    seco.iconstyle.color = 'ff0051e6'  # Red
    seco.iconstyle.icon.href = 'https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png'
    seco.iconstyle.scale = 1

    df = pandas.read_csv(infile)
    cuenta = 1
    for lon, lat, desc in zip(df["longitude"], df["latitude"], df["desc"]):
        nogal = kml.newpoint()
        nogal.name = "Nogal"+"-"+str(cuenta)
        nogal.description = desc.upper()
        nogal.coords = [(lon, lat)]
        if "OK" in nogal.description:
            nogal.style = ok
        if "RC" in nogal.description:
            nogal.style = rc
        if "SECO" in nogal.description:
            nogal.style = seco
        cuenta += 1
    kml.save(outfile)
    print(kml.kml())

if __name__ == "__main__":
    genkml()

My test input file:
type,date time,latitude,longitude,accuracy(m),altitude(m),geoid_height(m),speed(m/s),bearing(deg),sat_used,sat_inview,name,desc
W,2021-05-30 21:08:27,28.66231833,-100.83812667,1,257.122,-23.722,0.000,,28,33,,Q31 ok
W,2021-05-30 21:11:56,28.66243667,-100.83811000,1,256.922,-23.722,0.000,,26,35,,O32 ok

The resulting KML (original):
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
    xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Document id="1">

        <Placemark id="12">
            <name>Nogal-1</name>
            <description>Q31 OK</description>

            <Point id="11">
                <coordinates>-100.83812667,28.66231833,0.0</coordinates>
            </Point>
        </Placemark>
        <Placemark id="14">
            <name>Nogal-2</name>
            <description>Q32 OK</description>

            <Point id="13">
                <coordinates>-100.83811,28.66243667,0.0</coordinates>
            </Point>
        </Placemark>
    </Document>
</kml>

The modified KML, with a manually added style (ID = "icon-1739-7CB342") that renders as a small dot for Nogal-2 and a drop for Nogal-1 (please note that the only difference between the styles is the ID):
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
    xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Document id="1">

        <Placemark id="12">
            <name>Nogal-1</name>
            <description>Q31 OK</description>

            <Point id="11">
                <coordinates>-100.83812667,28.66231833,0.0</coordinates>
            </Point>
        </Placemark>
        <Placemark id="14">
            <name>Nogal-2</name>
            <description>Q32 OK</description>

            <Point id="13">
                <coordinates>-100.83811,28.66243667,0.0</coordinates>
            </Point>
        </Placemark>
    </Document>
</kml>

The rendered image by Google My Maps. I would like to use Nogal-2's icon without having to manually add the icon-1739-7CB342 style
Comment: Perhaps change the `` to `https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png` and scale it to be smaller.
Comment: Thank you @geocodezip. That's the URL I'm using in both cases, but the icons render differently.
Comment: @geocodezip even though scale is a float, I've tried a number <1 but it doesn't compare to the "dot" in [image](https://i.stack.imgur.com/gJbOo.png) for Nogal-2
Comment: Mean to type `http://www.gstatic.com/mapspro/images/stock/959-wht-circle-blank.png` for the "dot" url in my comment above.
Comment: @geocodezip this renders a circle, but not as small as the dot (even with the smallest scale I could use of 0.4 or lower). It's a great suggestion, though. I'll use it if the ID issue is not demystified.
Comment: My guess on the id is that numeric IDs are not allowed (`The lexical domain (xsd:NCName) of this datatype **doesn't allow the definition of numerical identifiers** or identifiers containing whitespace.`, from [ref](http://books.xmlschemata.org/relaxng/ch19-77151.html)) by the schema.  Try adding a letter before the first number, or just using a letter.
Answer: Try changing your style ID(s) to start with a letter.  Per the XML spec (and therefore in KML), ID attributes cannot start with a number.  If that doesn't fix it, let us know and I can take a closer look at your KML.
Comment: Hi @Christiaan, the problem is that ID(s) are determined by simplekms. I went ahead and changed "2" with "icon-2", with same results. I have the impression that Google My Maps interprets the ID, but I haven't found any documentation supporting that. In previous tests the icon's color is rendered by the color code in the ID rather than the one in the color attribute. Thank you for your help.
Comment: Have you made sure to change ALL the IDs to not start with numbers, including the Document, Placemark and Point IDs?  I'm wondering if the xml parsing is failing part way through due to the IDs.  And I'm sure you've done this, but double check that you've updated the style IDs in both the Style tags and in the styleUrl tags that reference them.  Can you share an updated KML that still shows the issue?  Also, have you tried opening the KML in Google Earth Pro, and does it show the same issue?
Comment: Hi @Christiaan, Google Earth Pro shows the issue in a different way: The "icon-2" ID shows the drop, and the "icon-1739-7CB342"  ID shows a square with a red "X". The updated KML with all IDs starting with letters can be found [here](https://pastebin.com/cd58KP1E). Thanks for your help.


Any way to leave selenium web pages open after running your script completely in python?

Question: Ive been looking around the web for a solution to this. The issue im having is that my webpages close immediatelt after they open up, this is becuase the script is done. But, in my case i want them to stay so i can use them. is there a certian way to leave the pages open, even after the script finishes? This is my code...
import selenium.webdriver as webdriver
from selenium.webdriver.chrome.options import Options

chrome_path = r"C:\Users\Michael\PycharmProjects\FlashCop\chromedriver.exe"
times = ""
start_page = ""
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

def start_pages(target_page):
    for x in range(0, len(page_number)):
        driver = webdriver.Chrome()
        driver.get(target_page)
        chrome_options.add_experimental_option("detach", True)

while times == "":
    times = input("How many pages do you want?\n")

url = input("Yeezy Supply or Adidas?""\nEither 'YS' or 'Adidas'\n")
url_choice = url.lower()

page_number = list()
for i in range(0, int(times)):
    page_number.append(times)

if url_choice == 'ys':
    start_page = 'https://yeezysupply.com/'
    start_pages(start_page)
elif url_choice == 'adidas':
    start_page = 'https://www.adidas.com/yeezy'
    start_pages(start_page)

Comment: Can you please explain what is your final goal?
I would like to better understand why you choose to use selenium.
Comment: Does this answer your question? [Chromedriver closing after test](https://stackoverflow.com/questions/43612340/chromedriver-closing-after-test)
Answer: The experimental option detach is what will keep the browser open. You define this option, but haven't added it to your driver.
def start_pages(target_page):
    for x in range(0, len(page_number)):
        chrome_options.add_experimental_option("detach", True)
        driver = webdriver.Chrome(chrome_path, chrome_options=chrome_options)
        driver.get(target_page)

Comment: How can we do the same with FireFox ?


Merit function vs Largrange Functions vs Penalty Funcitons

Question: I've been reading up on constraint optimization. I've come across the three terms:

Merit Function
Lagrange Function
Penalty Function

I'm pretty sure all these three things are the same. That is, they quantify how much an iterate satisfies both the objective and the constraint. However, I would like a second opinion to clarify.
Answer: No, they're not all the same and it's important to understand the differences between them.
Start with a simple optimization problem
$\min f(x)$
subject to
$g(x) = 0$
where we can assume for simplicity that $f$ and $g$ are smooth (at least twice continously differentiable.)
The Lagrangian function is
$L(x,\lambda)=f(x)+\lambda g(x)$
Note that $L$ is a function of $x$ and $\lambda$.  The first order necessary condition for a point $x^{*}$ to be a minimizer is that there is a $\lambda^{*}$ such that $(x^{*},\lambda^{*})$ is a stationary point of $L$.  In the method of multipliers, we try to solve the nonlinear system of equations
$\nabla_{x,\lambda} L(x,y)=0$
This is typically done by alternately minimizing with respect to $x$ and updating $\lambda$.  Given a Lagrange multiplier estimate $\lambda^{(k)}$, we minimize $L(x,\lambda^{k})$ to get $x^{(k)}$.  Then we update $\lambda$ with
$\lambda^{(k+1)}=\lambda^{(k)} +\alpha_{k} g(x^{(k)})$
Where $\alpha_{k}$ is a step size parameter that can be set in various ways.
An penalty function for our problem is a function that is $0$ if $g(x)=0$ and greater than $0$ when $g(x) \neq 0$.  A commonly used penalty function is the quadratic penalty function
$\phi(g(x))=g(x)^{2}$
In the penalty function method, we solve an unconstrained problem of the form
$\min_{x} f(x)+\rho \phi(g(x))$
where $\rho$ is a penalty parameter that is increased until the solution of the penalized problem is close to satisfying $g(x)=0$.  Note that $\rho$ is not a Lagrange multiplier in this case.
For problems with inequality constraints a commonly used penalty function is
$\phi(g(x))=\max(g(x),0)^{2}$.
An augmented Lagrangian function combines the penalty function idea with the Lagrangian:
$\hat{L}(x,\lambda; \rho)=f(x)+\lambda g(x) + \rho \phi(g(x))$
Augmented Lagrangian methods minimize $\hat{L}$ with respect to $x$, update the
Lagrange multiplier estimate $\lambda$ and then (if necessary) update the penalty parameter $\rho$ in each iteration. In practice, augmented Lagrangian methods outperform simple penalty methods and the method of multipliers.
Merit functions are used in a variety of nonlinear programming algorithms.  You'll most commonly see them used in sequential quadratic programming methods.  In these methods, a search direction, $d^{(k)}$, is computed at each iteration.  The step is from $x^{(k})$ to
$x^{(k+1)}=x^{(k)}+\alpha_{k} d^{(k)}$
where the step size parameter $\alpha_{k}$ is determined by minimizing a
merit function
$\min_{\alpha} M(x^{(k)}+\alpha d^{(k)})$
The merit function is typically something like a penalized objective function or an augmented Lagrangian, but there's a great deal of freedom in the form of the merit function.
These functions and the associated methods are described in many textbooks on nonlinear optimization.  A good discussion can be found in Numerical Optimization by Nocedal and Wright.
Comment: Thanks for the comprehensive response Brian!
Comment: Are there any important "conceptual" or "heuristic" differences between these three different types of objectives? It seems to me, and perhaps also to the OP, that they are all different implementations of the same basic idea: instead of having a constraint ($g(x) = 0$), remove the constraint and add a term to the objective function which is greater than $0$ when the constraint is violated. While hypothetically the Lagrangian could reward constraint violations when $g(x) < 0$ (since $\lambda \ge 0$ by assumption), that doesn't happen since ultimately one solves for the minimum w.r.t. $x$.


Does Codeigniter input->post() remove html tags?

Question: Does using $this->input->post(); in Codeigniter, remove any html and javascript the user of the website may have entered, or do you have to use htmlspecialchars();?
Answer: $this->input->post() only contains post data..
You could always call $this->input->post(NULL, TRUE); as per the manual to return data filtered through the XSS filter.
You can see here exactly what's happening, is it basically re-builds the $_POST array using fetch_from_array. This invokes the security module for XSS $this->security->xss_clean($array[$index]); (if you set XSS filter to true).
If there's something that XSS filter doesn't catch, your code is exposed.
I would generally never filter my raw input data like this before it's being used, but rather only when it's about to be used, and in that specific way.
Malicious content is nearly impossible to prevent if you filter it on input, because it's designed to be run as output.

When you're printing to an HTML document ensure your special chars are html encoded
When calling SQL, ensure you're running parameterized queries without SQL injection exploits
When you're making system calls, don't let them take control of your system.

As mentioned in the comments of my answer, the XSS input filter is an inadequate attempt at preventing attacks.
If in doubt, consult OWASP.
Comment: I'd say,though, that htmlspecialchars() would be better, as the xss filter is pretty a resource drainer, although surely much more safer
Comment: The XSS filter is not safe, it even creates the impression that it would protect from something while you need to protect your own.
Comment: @DamienPirsy Every tool has a job to do. I would never filter Post data like this. Perhaps I'll add an explanation in my answer...
Answer: CodeIgniter has a security class and you can use it anytime you want. Also you can add htmlspecialchars() whenever and wherever you want.
Answer: The post() function accepts a second argument that passes the input through the XSS filter which would sanitize all html and javascript.
$this->input->post('some_data', TRUE);

Answer: It's all in the docs.

To return an array of all POST items call without any parameters.
To return all POST items and pass them through the XSS filter set the
  first parameter NULL while setting the second parameter to boolean;
The function returns FALSE (boolean) if there are no items in the
  POST.
$this->input->post(NULL, TRUE); // returns all POST items with XSS filter
$this->input->post(); // returns all POST items without XSS filter

Same thing with individual items and GET requests.
Comment: This does not work, when the html is sent to the backend


How to adjust widget to compact mode

Question: I've got some troubles with new format of Today Extensions. It's ok in the extended mode it, but the compact mode is a real pain. I can't get how this compression mechanism works. In my case a small font and top of view just disappears, its not what i like to see.
I've tried to change frame of self.view and make some items hidden, but no sence.
Anyone else had same? How can I fix it?
Comment: In compact mode, the widget has a height of 110. It cannot be less than this even if you programatically set the height less than 110.
Answer: don't put constraints on the bottom on your view... maybe is the reason why your top is being squizzed due to priorities...
in code, resize your own view as per your calculated content height (like tableview.count*height_row + otherview.height), set that height to the preferred content size in expanded mode, and you should be fine with expanded/collapsed.
else put some code...

How to adjust widget to compact mode


Computing average of data in multiple (different) columns in SPSS

Question: I have a SPSS data file, which i am trying to reduce. However the data which belongs together is spread within multiple columns. I.e. I have one row per subject, but each subject has done multiple different conditions as well as procedures. The conditions are in columns condition_1, condition_2 etc. The procedures are in rows procedure_1, procedure_2 etc. The values (reaction times) are in columns reaction_time_1, reaction_time_2. Now I want to compute the average of all reactions times for each person where the condition and procedure is the same. So if condition_x is the same as condition_y and procedure_x is the same as procedure_y I want to get the average over all of those items reaction_time_x and reaction_time_y.
How can I do this with SPSS? I easily know how to compute averages in general, but in this case I have to select only a subset of the rows based on the conditions, and I do not know how to do this.
Comment: Your explanation of the problem is not very clear. Please show the data themselves, maybe excerpt from the data.
Answer: Given your explanation I think one easy solution (without resorting to making new variables) would be to use temporary and select if statements to produce summary statistics for the desired categories. For instance, the below statement would produce the mean for reaction_time_1 when the category_1 and the procedure_1 both equal 1 (and eliminate all other cases from the calculation which do not meet that criteria).
temporary.
select if category_1 = 1 and procedure_1 = 1.
freq var reaction_time_1 /statistics = mean.
execute.

Unless your procedures and categories are not mutually exclusive, there is surely a better way to organize your variables, but this will produce the requested output as is.
Answer: It appears to me that you want within-case means, so check out the mean transformation function.   You will still have to apply appropriate IF or DO IF tests, though.
Answer: You could use a syntax of this sort for your scenario:
if (condition_x = condition_y & procedure_x = procedure_y) time_xy = mean (reaction_time_x, reaction_time_y) .
execute .

This would create a mean reaction time for conditions and procedures x and y when equal. Though it would only work if condition and procedure are variables (i.e. columns) in the datasets. If your data set is stacked, or shape in a person-period format (i.e. many rows per case) it won't work. For the cases in which procedure and condition are different, the created variable would be empty. Hope it works!


Read file content per row of Spark DataFrame

Question: We have an AWS S3 bucket with millions of documents in a complex hierarchy, and a CSV file with (among other data) links to a subset of those files, I estimate this file will be about 1000 to 10.000 rows. I need to join the data from the CSV file with the contents of the documents for further processing in Spark. In case it matters, we're using Scala and Spark 2.4.4 on an Amazon EMR 6.0.0 cluster.
I can think of two ways to do this. First is to add a transformation on the CSV DataFrame that adds the content as a new column:
val df = spark.read.format("csv").load("<csv file>")
val attempt1 = df.withColumn("raw_content", spark.sparkContext.textFile($"document_url"))

or variations thereof (for example, wrapping it in a udf) don't seem to work, I think because sparkContext.textFile returns an RDD, so I'm not sure it's even supposed to work this way? Even if I get it working, is the best way to keep it performant in Spark?
An alternative I tried to think of is to use spark.sparkContext.wholeTextFiles upfront and then join the two dataframes together:
val df = spark.read.format("csv").load("<csv file>")
val contents = spark.sparkContext.wholeTextFiles("<s3 bucket>").toDF("document_url", "raw_content");
val attempt2 = df.join(contents, df("document_url") === contents("document_url"), "left")

but wholeTextFiles doesn't go into subdirectories and the needed paths are hard to predict, and I'm also unsure of the performance impact of trying to build an RDD of the entire bucket of millions of files if I only need a small fraction of it, since the S3 API probably doesn't make it very fast to list all the objects in the bucket.
Any ideas? Thanks!
Answer: I did figure out a solution in the end:
val df = spark.read.format("csv").load("<csv file>")
val allS3Links = df.map(row => row.getAs[String]("document_url")).collect()
val joined = allS3Links.mkString(",")
val contentsDF = spark.sparkContext.wholeTextFiles(joined).toDF("document_url", "raw_content");

The downside to this solution is that it pulls all the urls to the driver, but it's workable in my case (100,000 * ~100 char length strings is not that much) and maybe even unavoidable.


Bayesian posterior distributions

Question: I have the model $y_{i,t}=x_{i,t}'\beta_{i} + \epsilon_{i,t}$ where $x_{i,t}$ is a k-dimensional vector of explanatory variables and $\beta_{i}$ is a k-dimensional parameter vector, where $\beta_{i}=\Gamma'w_{i}+\eta_{i}$ where $w_{i}$ is an l-dimensional vector of explanatory variables and $\Gamma$ is a $l\times k$ parameter matrix.
Assuming that $\epsilon_{i,t} \sim N(0,\sigma_{\epsilon}^{2})$ and $\eta_{i} \sim N(0,\Sigma_{\eta})$ and also assuming uninformative priors for the parameters $\sigma_{\epsilon}^{2}, \Sigma_{\eta}, \beta_{i}$ and $\Gamma$ I cannot seem to find their posterior distributions. Any help ? The reason I need their posterior distributions is to use Gibbs sampling later to estimate them.
Comment: what values can $i$ and $t$ take on?
Comment: Does it matter ? $i$ can take the values 1,2,...,4000 and $t=1,2,...,31$
Answer: This is a hierarchical or linear mixed model. Be careful about using improper noninformative priors in this context since they often (not always) lead to improper posteriors. See for instance:

Hobert, James P., and George Casella. "The effect of improper priors on Gibbs sampling in hierarchical linear mixed models." Journal of the American Statistical Association 91.436 (1996): 1461-1473.

Unfortunately, there is no easy way to sample from the corresponding posterior distribution. Also, you need the conditionals, rather than the posteriors, in order to implement a Gibbs sampler.
Some useful are packages for sampling from the posterior associated to this kind of models are spBayes (adaptive Metropolis within Gibbs) and MCMCglmm (block Gibbs sampler).
Comment: In Gibbs sampling the posteriors of one parameter are conditional on the other parameters in any case. I would like to hardcode it, so without the use of the package :). If I find the posteriors analytically then I can draw from them. It is just that I cannot find any literature on this :(

Bayesian posterior distributions


Mixed effects model for group differences in R

Question: I'm trying to run a mixed-effects analysis on some data that I have, but cannot determine if I am using the correct model.
First, I am trying to determine if there are between-group difference in 5 independent variables. My data consists of a number of participants who performed 31 different tasks (we'll call them Items), during which these 5 variables were measured. I would like to test for differences in these variables, not necessarily in relation to each other, but I would like to keep them in one model so as to avoid excessive testing. I would, however, like to control for the variance between these items, and if possible also between participants.
I currently am using the lme4 package to run a logistic regression:
glmer(Group~Var1+Var2+Var3+Var4+Var5 + (1|item),family=binomial)

My thinking here is that if there are group differences, then the variables should also predict group membership, effectively testing my hypothesis, if in an indirect way. Please correct me if this assumption is incorrect.
Ideally I would like to run this with Participant as an addition random factor, but I don't think that makes sense in a model testing for group differences.
The alternative is to run separate regression models for each of my independent variables, with Group, Item, and Participant as random effects. However, as I said before, I don't want to over-test the data, so I'm not sure if this is an advisable way to go about it.
Can anyone let me know if my current setup is a valid way to test for significant differences of multiple variables between 2 groups?
EDIT:
If the above is NOT valid, and the test should be the other way around, lmer(Var1 ~ Group + (1|item)), is it then recommended to also model participants as random variables, or will this interfere with the fixed effect of Group?
Comment: (Note that you haven't specified `family=binomial`, so I think you may be getting a linear mixed model.)
Comment: Thanks for the catch, gung. I had it specified when I ran the model, but I forgot to type it out here.
Comment: This is not appropriate if you are not interested in the relationship between the variables—in this model they will each be adjusted or "controlled" for the others. This has a fundamentally different interpretation than doing 5 independent tests. Also, adding them all into one model does not remove the multiple comparison problem. If the 5 variables are different in context and have their own hypotheses, then you could justify not adjusting for multiple comparisons. On the other hand, if you are just throwing things against a wall to see what sticks, then you should adjust for this.
Comment: @Moose so if I understand correctly, my model works to test the hypothesis that the variables differ between the two groups, but are also hypothesized to have some relationship among themselves as well. However, it requires correction for multiple comparisons.
On the other hand, if we assume the 5 variables are completely independent, the more valid model is to run something like Var1~Group+(1|item)+(1|participant), for each of the 5 variables.
Comment: I'll add a complete answer. If it's sufficient, please indicate it as so :) Also, you first refer to the 5 variables as "independent" when, in the experimental sense, they are the dependent variables (you measured them in response to item).
Answer: In the model that you specified in your initial question,
glmer(Group~Var1+Var2+Var3+Var4+Var5 + (1|item),family=binomial)

you would be asking the following question: which variables, measured across a number of items, are independent predictors of group assignment/membership?
If this was a test to try and diagnose a pathology, then it might make sense. However, it sounds more like an experimental design with treatment assignment. In this case, each response should be tested separately. The model you left in the comments section would be correct:
lmer(Var1 ~ Group + (1|item) + (1|participant), data=data)

This design would be considered a crossed random effects model. You can confirm that you specified it correctly as a similar design is shown here.
Regarding multiple testing, one cannot add many terms to a model to try and avoid the multiple comparisons problem because: 1) there are no tricks to avoid multiple testing if you do in fact make multiple tests and 2) because the tests would have a different interpretation in a full model since their estimates are controlled for the values of the other variables. There are certainly situations where one would want to do this, especially in observational studies, but for experimental designs, it would be pretty unusual.
My two cents on whether you should adjust for multiple comparisons or not will depend on the context. If each response has its own pre-specified hypothesis, are frequently tested together or if some are more like positive controls (we expect them to change, but it is not the main hypothesis of interest), then you could justify using the unadjusted p-values. However, if you don't have clearly defined hypotheses or if you take "a significant result, any significant result", then you should adjust the p-values to maintain your confidence at the nominal level.
Comment: Thank you! This is very clear. I have just one question for clarification: does adding the (1 | participant) term interfere with Group being the main predictor? As the participants are spread across two groups.
To be more specific, does adding this extra term require extra large sample size, more robust (hypothesized) effect, etc?
Comment: No problem for the group effect. I don't think sample size should be a problem either since you have so many tasks in each person (the person-specific intercept will be well-estimated) and I presume that you have quite complete/balanced data. And please "accept" the answer if it is satisfactory :)


awk/sed replace multiple newlines in the record except end of record

Question: I have file where:

field delimiter is \x01
the record delimiter is \n

Some lines contain multiple newlines I need to remove them, however I don't want to remove the legitimate newlines at the end of each lines. I have tried this with awk:
awk -F '\x01' 'NF < 87 {getline s; $0 = $0 s} 1' infile > outfile

But this is only working when the line contains one newline in the record (except end of line newline). This does not work for multiple newlines.
Note: the record contains 87 fields.
What am I doing wrong here?
Example of file:
PL^ANov-21^A29-11-2021^A0^A00^A00^A0000000
test^A00000000
 Test^A^A^A^A
PL^ANov-21^A29-11-2021^A0^A00^A00^A0000000
test^A00000000
 Test^A^A^A^A
SL^ANov-21^A30-11-2021^AB^A0000^A1234567^A00000
test^A12102120^A00000^A00^A^A

NOTE: The file contains 11 fields; field separate \x01; record separator \n
Expected result:
PL^ANov-21^A29-11-2021^A0^A00^A00^A0000000test^A00000000 Test^A^A^A^A
PL^ANov-21^A29-11-2021^A0^A00^A00^A0000000test^A00000000 Test^A^A^A^A
SL^ANov-21^A30-11-2021^AB^A0000^A1234567^A00000test^A12102120^A00000^A00^A^A

Note: I need to preserve the field delimiter (\x01) and record delimiter (\n)
Thank you very much in advance for looking into this.
The file always contains 87 fields;
The fild delimiter is '\x01', but when viewing in Linux it is represented as '^A'
Some lines contain newlines - I need to remove them, but I don't want to remove the legitimate newlines at the end of each line.
The newline appears twice in the 1st and second record and once in third record - this are the newlines I want to remove.
In the examples/expected results there are 11 delimiters "x01" represented as "^A",
I expect to have 3 records and not 6, i.e.:

First record:

test^A00000000 should be joined to the previous line
Test^A^A^A^A should be joined to the first line as well
forming one record:
PL^ANov-21^A29-11-2021^A0^A00^A00^A0000000test^A00000000 Test^A^A^A^A

Second record

test^A00000000 should be joined to the previous line
Test^A^A^A^A should be joined to that previous line as well
forming one record:
PL^ANov-21^A29-11-2021^A0^A00^A00^A0000000test^A00000000 Test^A^A^A^A

Third record:

test^A12102120^A00000^A00^A^A should be joined to the previous line
forming one record:
SL^ANov-21^A30-11-2021^AB^A0000^A1234567^A00000test^A12102120^A00000^A00^A^A
Note:
The example of awk - provided works when there is one unwanted newline in the record  but not when there are multiple newlines
Thank you so very much. It works perfectly. Thank you for explaining it so well to me too.
Comment: for sample purposes we do not need to see 87 fields; you should be able to demonstrate the issue with 4-5 fields (each no more than 5-10 characters each), and perhaps 5-10 lines (some with single newline, some with multiple newlines)
Comment: Welcome to SO, thanks for sharing your efforts, please post samples of input and expected output in your question to make it more clear, thank you
Comment: Welcome on StackOverflow. May I suggest that you take a look at the [help center](https://stackoverflow.com/help) and especially at [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting)?
Comment: Do your input files always contain a multiple of 87 fields? If not, you need to add an information to your question: what do you want to do with the remaining fields? Please answer by editing your question, not in comments.
Comment: Your example of input and expected result are not consistent. 1) what are these `^`? Do they represent the field separator? If yes, please edit your question and use only this one instead of `\x01` (invisible) and `^`. When you will have received an answer it will be easy to adapt. 2) In your expected result you concatenate the last field of an input record and the first field of the next (`A0000000test`). Is it really what you want? 3) You mention 11 fields but there 12 `^`-separated fields in your expected result. Is it an error? Please answer by editing your question, not in comments.
Comment: You write: "_Some lines contain multiple newlines I need to remove them, however I don't want to remove the legitimate newlines at the end of each lines_". This does not really make sense. By definition a line contains only one newline, at the end. Please clarify by editing your question, not in comments.
Comment: See my answer for an explanation of why your attempt does not work as you expect, and for a working solution. As you know how to define the fields separator (`-F'\x01`) why don't you simplify your question by using a visible one, for instance `|`? And why don't you use only the 12 fields example, instead of mixing 87 and 12? I strongly suggest that you learn how to format your question with markdown (see my other comment for the link), and that you modify your question with **only** 12 fields and **only** `|` as fields separator. It will become clear and easy to answer.
Comment: And, by the way, you know [what to do when someone answers your question](https://stackoverflow.com/help/someone-answers), don't you?
Answer: This might work for you (GNU sed):
sed ':a;N;s/\x01/&/87;Ta;s/\n//g' file

Gather up lines until there are 87 separators, remove any newlines and print the result.
Answer: What's wrong with your attempt is that you concatenate two lines, print the result and move to the next line. NF is then reset to the next fields count. As all your lines have less than 87 fields the NF < 87 condition is useless, your script would work the same without it.
Try this awk script:
$ awk -F'\x01' -vn=87 -vi=0 '
    {printf("%s", $0); i+=NF; if(i==n) {i=0; print "";} else i-=1;}' file

Here, we use the real \x01 field separator and the NF fields count. Variable i counts the number of already printed fields. We first print the current line without the trailing newline (printf("%s", $0)). Then we update our i fields counter. If it is equal to n we reset it and print a newline. Else we decrement it such that we do not count the last field of this line and the first of the next as 2 separate fields.
Demo with n=12 instead of 87 and your own input file (with \x01 field separators):
$ awk -F'\x01' -vn=12 -vi=0 '
    {printf("%s", $0); i+=NF; if(i==n) {i=0; print "";} else i-=1;}' file |
  sed 's/\x01/|/g'
PL|Nov-21|29-11-2021|0|00|00|0000000test|00000000 Test||||
PL|Nov-21|29-11-2021|0|00|00|0000000test|00000000 Test||||
SL|Nov-21|30-11-2021|B|0000|1234567|00000test|12102120|00000|00||

The sed command shows the result with the \x01 replaced by | for easier viewing.


Extend python staticmethod in other class

Question: I have two classes which are responsible for some calculations. In first class I have calculateStatisticalFeatures static method which calculate come stuff and returns a DataFrame for me. In other class I would like to do almost the same but add one more calculation basing on the same new input data from second class. I found something like decorators but somehow I was not able to use it.
Method in first class:
<code>@staticmethod
def calculateStatisticsFeatures(self, inputData) -> pd.DataFrame:
    #some calculations
    features = pd.DataFrame(np.array([[skewn, kurt, entropyVal, meanCalc]]), columns=['skewness', 'kurtosis','entropy', 'meanCalc'])
    return features
</code>
I was trying to use decorator like this to extend my first class method in second class but I can't paste the data.
<code>@firstClass.calculateStatisticalFeatures(self.inputData)
def TestDecor(self):
    # new calculation
</code>
Is it somehow possible to add this calculations to second class? Thank you in advance :)
Comment: Err honestly, why makes things static, when you want to do inheritance?
Comment: I used staticmethod because I don't need in instantiate first class. I just want to reuse one method from second class and add new functionality to it.
Comment: Yeah, and that is were static gets nasty. If you want inherit things, then static is an obstacle ... so rather consider to improve change your design, instead of working around the flaws of static.
Comment: Why is `self` as a variable in the staticmethod(calculateStatisticsFeatures)
Comment: @GhostCat first I need to learn how to change :) I am guite beginner in Python :)
Comment: @Vassago As far as I understand it is necessary to use this method later in first class so I added it into each method. Withoud self in methods I had an errors
Comment: You use self when you have non-static methods? I guess the real answer here: get yourself a good book on python and OO programming ;-)
Comment: #GhostCat Ok thanks. I understood that self is not accessible when I call this method from other place. I think I will remove static and just try split my class into two classes and inherit what I need. What book would you recommend as good?
Answer: Maybe this is what you want?
<code>>>> import functools
>>>
>>> class A():
...     @staticmethod
...     def test(func):
...             @functools.wraps(func)
...             def wrapper(*args, **kw):
...                     # features = pd.DataFrame(np.array([[skewn, kurt, entropyVal, meanCalc]]), columns=['skewness', 'kurtosis','entropy', 'meanCalc'])
...                     print(kw.get("inputData"))
...                     return func(*args, **kw)
...             return wrapper
...
>>> class B(A):
...     @A.test
...     def testb(self, **kw):
...             print('test')
...
>>>
>>> B().testb(inputData="inputData")
inputData
test
</code>
Comment: Thank you for this. After I analyzed all comments, as I wrote I will change my code and remove static.
Answer: I removed the staticmethod and just inherited second class. I was not aware that in Python we can inherit from classes in one class.


Is it possible to dispute the closure of a question?

Question: I have seen, a couple of times now, a well-written question describing a real Magento problem that needs a real solution, and it's been closed as off-topic. In both cases I strongly disagree that the question is off-topic.
Example: Random Installation Wizard Redirect Magento 18.104.22.168
The reason given was that the problem "can't be reproduced OR seemingly went away on its own (or went away when a typo was fixed)". In the above case, none of these things are true. The problem turned out to be difficult to reproduce, not impossible, and didn't go away on its own.
The question was closed the same day it was asked. Talk about an itchy trigger finger! Did the people involved in closing it actually bother to check if it really met the criteria?
Sorry, I didn't intend for this question to be an appeal about another question. My point is, what can I do to appeal cases such as this? Do I have to be the person who originally asked it?

I could try editing the question, like it suggests in the closure notice, but I see someone already tried that to no avail.
At the moment I can't add comments as I only have 33 rep.
I tried clicking through to the profile of one of the people who closed it, and his profile page explicitly states he won't read StackExchange emails since no-one's paying him to do so. Should I, as he goes on to suggest, ask the same question again? What's to stop him closing that one?
Comment: Reopened, thanks.
Answer: Posting here works. We'll figure out a good cadence and age for closures.


Como colocar hora e data em campos inputs com jQuery e jQuery UI?

Question: Como fazer a inclusão dos arquivos de estilo e dos arquivos JavaScript do jQuery UI para que os inputs de data e hora mostrem perfeitamente os campos em pt_BR? É preciso modificar os atributos das bibliotecas?
Answer: Eis o código cujo os estilos podem ser colocados no header e as bibliotecas javascript juntamente com os códigos e atributos podem ser colocados no footer ou em arquivo externo e serve para qualquer sistema que use html/css/javascript/jquery para funcionar.
<link href="https://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.min.css" type="text/css" rel="stylesheet" />
<link href="css/jquery.ui.timepicker.css" type="text/css" rel="stylesheet" />


Can I reference GitHub Organization Secrets from a repo's Environment Secrets?

Question: Our organization has a bunch of repos on GitHub using GitHub Actions to deploy, some of which need access to the same secrets (token for our code coverage tool as one example). Organization-level secrets work great for this.
Individual repos also require secrets which differ based whether they are deploying to staging or production. Environment secrets work great for this by allowing the action to be generic and just reference AWS_KEY rather than conditionally use PROD_AWS_KEY or STAGING_AWS_KEY, for instance.
But I can't seem to find a way to use both features together. If two repos want to use environment secrets to change the value of the secret they are accessing, but have that underlying secret be shared to the repos from the organization level.
CircleCI had a feature called contexts which allowed us to share a prod and staging context to any repo that needed them but I can't seem to get analogous behaviour out of GitHub Actions. Does anyone know if this is possible, and if so how?
Comment: Environments are repository specific in GitHub Action. Unfortunately, you can't share them at an Organization level (yet?) like the CircleCI contexts. The workaround is as you explained: to duplicate the environment secrets in each repo (if you want to use the environment secret) or creating secrets for each env at the Organization level.
Comment: There is an (ongoing) discussion regarding adding support for [Organization-wide Environments and Environment Secrets](https://github.com/orgs/community/discussions/15379).
Please upvote this so that (hopefully) GitHub chooses to implement this in the near feature, so we won't have to resort to hacks like described in the good answer by @HumanTarget ;-).
Answer: So after some messing around I found an answer to this that works, at least in my particular use case.
It is possible to use a variable/secret value to access an organization secret. If I store the name of the organization secret in the value of an environment variable then I can use it to access the correct org secret at action runtime.
Lets assume the repo has two environments setup; production and staging. Also assume the organization has a secret to access an AWS key for each of there corresponding AWS accounts, named AWS_KEY_PROD and AWS_KEY_STAGE for instance.
In each GitHub Action environment lets create an environment variable named AWS_KEY where the value is the name of the org secret that makes sense for the environment we are working with. So the value of AWS_KEY for the production environment is AWS_KEY_PROD and for the staging environment its AWS_KEY_STAGE. Then we can use the value to access the corresponding org secret.
jobs:
  staging-job:
    name: example staging job
    runs-on: ubuntu-latest
    environment: staging
    steps:

      #Should print 'AWS_KEY_STAGE'
      - name: get secret name from environment vars
        run: echo "${{ vars.AWS_KEY }}"

      - name: get secret value using secret name
        run: someCommand "${{ secrets[vars.AWS_KEY] }}"

  production-job:
    name: example production job
    runs-on: ubuntu-latest
    environment: production
    steps:

      #Should print 'AWS_KEY_PROD'
      - name: get secret name from environment vars
        run: echo "${{ vars.AWS_KEY }}"

      - name: get secret value using secret name
        run: someCommand "${{ secrets[vars.AWS_KEY] }}"

As you can see the two different jobs use the exact same steps definition (except for the comment added here for clarity) to access the different secrets based on the environment.