Pyspark: Unable to write files to Azure Blob Storage

Question: Im currently working on a project where I try to store AVRO files on Azure Blob Storage using PySpark, but without any luck. Writing csv files is also not working. Using spark-submit and providing the --jars argument with the paths to the jar files yields the same error.
I am running the code sample in a Jupyter Notebook within VsCode.
Could someone please help me resolve this error?
Sample Code:
import pyspark
from pyspark.sql import SparkSession
import pyspark.sql.functions as f

STORAGE_ACCOUNT_NAME = "mystorageaccount"
CONTAINER_NAME = "mycontainer"
SPARK_AVRO_JAR_PATH = "/path_to_jar/spark-avro_2.12-3.2.0.jar"
AZURE_STORAGE_BLOB_JAR_PATH = "/path_to_jar/azure-storage-blob-12.14.3.jar"
AZURE_STORAGE_JAR_PATH = "/path_to_jar/azure-storage-8.6.6.jar"
HADOOP_AZURE_JAR_PATH = "/path_to_jar/hadoop-azure-3.3.1.jar"
DESTINATION_CONTAINER_STR = f"wasbs://{CONTAINER_NAME}@{STORAGE_ACCOUNT_NAME}.blob.core.windows.net/"

cpu = multiprocessing.cpu_count() -1
spark = SparkSession.builder \
    .master(f"local[{cpu}]") \
    .appName("MyApp") \
    .config('spark.jars', f"{SPARK_AVRO_JAR_PATH},{AZURE_STORAGE_BLOB_JAR_PATH},{AZURE_STORAGE_JAR_PATH},{HADOOP_AZURE_JAR_PATH}")\
    .config("spark.driver.memory", "15g")\
    .getOrCreate()
spark.conf.set("spark.sql.execution.arrow.pyspark.enabled", "true")
spark.sparkContext.setLogLevel("WARN")
spark.conf.set(
    f"fs.azure.account.key.{STORAGE_ACCOUNT_NAME}.blob.core.windows.net",
    f"{STORAGE_ACCOUNT_ACCESS_KEY}"
)

file_name = "test"
df = spark.createDataFrame(["10","11","13"], "string").toDF("age")
df.write.format("avro").save(f"{DESTINATION_CONTAINER_STR}{file_name}")

Error messages:
Py4JJavaError                             Traceback (most recent call last)
/var/folders/93/_09pzbms1d9cyfm7mkt6hnhm0000gn/T/ipykernel_74366/438907801.py in <module>
     81 file_name = "test"
     82 df = spark.createDataFrame(["10","11","13"], "string").toDF("age")
---> 83 df.write.format("csv").save(f"{DESTINATION_CONTAINER_STR}{file_name}")

~/jens/.venv/lib/python3.9/site-packages/pyspark/sql/readwriter.py in save(self, path, format, mode, partitionBy, **options)
    738             self._jwrite.save()
    739         else:
--> 740             self._jwrite.save(path)
    741
    742     @since(1.4)

~/jens/.venv/lib/python3.9/site-packages/py4j/java_gateway.py in __call__(self, *args)
   1307
   1308         answer = self.gateway_client.send_command(command)
-> 1309         return_value = get_return_value(
   1310             answer, self.gateway_client, self.target_id, self.name)
   1311

~/jens/.venv/lib/python3.9/site-packages/pyspark/sql/utils.py in deco(*a, **kw)
    109     def deco(*a, **kw):
    110         try:
--> 111             return f(*a, **kw)
    112         except py4j.protocol.Py4JJavaError as e:
    113             converted = convert_exception(e.java_exception)

~/jens/.venv/lib/python3.9/site-packages/py4j/protocol.py in get_return_value(answer, gateway_client, target_id, name)
    324             value = OUTPUT_CONVERTER[type](answer[2:], gateway_client)
    325             if answer[1] == REFERENCE_TYPE:
--> 326                 raise Py4JJavaError(
    327                     "An error occurred while calling {0}{1}{2}.\n".
    328                     format(target_id, ".", name), value)

Py4JJavaError: An error occurred while calling o152.save.
: java.lang.NoClassDefFoundError: org/eclipse/jetty/util/ajax/JSON$Convertor
    at org.apache.hadoop.fs.azure.NativeAzureFileSystem.createDefaultStore(NativeAzureFileSystem.java:1441)
    at org.apache.hadoop.fs.azure.NativeAzureFileSystem.initialize(NativeAzureFileSystem.java:1366)
    at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:3469)
    at org.apache.hadoop.fs.FileSystem.access$300(FileSystem.java:174)
    at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:3574)
    at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:3521)
    at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:540)
    at org.apache.hadoop.fs.Path.getFileSystem(Path.java:365)
    at org.apache.spark.sql.execution.datasources.DataSource.planForWritingFileFormat(DataSource.scala:461)
    at org.apache.spark.sql.execution.datasources.DataSource.planForWriting(DataSource.scala:556)
    at org.apache.spark.sql.DataFrameWriter.saveToV1Source(DataFrameWriter.scala:382)
    at org.apache.spark.sql.DataFrameWriter.saveInternal(DataFrameWriter.scala:355)
    at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:239)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.ClientServerConnection.waitForCommands(ClientServerConnection.java:182)
    at py4j.ClientServerConnection.run(ClientServerConnection.java:106)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.util.ajax.JSON$Convertor
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 25 more

Versions:
(.venv) jens % pyspark --version
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /___/ .__/\_,_/_/ /_/\_\   version 3.2.0
      /_/

Using Scala version 2.12.15, OpenJDK 64-Bit Server VM, 1.8.0_302
Branch HEAD
Compiled by user ubuntu on 2021-10-06T12:46:30Z
Revision 5d45a415f3a29898d92380380cfd82bfc7f579ea
Url https://github.com/apache/spark
Type --help for more information.

(.venv) jens % python --version
Python 3.9.7

Comment: Your error doesn't match your code: `df.write.format("csv").save(f"{DESTINATION_CONTAINER_STR}{file_name}")`. Please update your code and/or error info to accurately reflect what you are running
Answer: It doesn't look like access error to me it seems more like code error.


Apache2 process stuck at 100% cpu, CLOSE_WAIT socket lingering

Question: I've troubleshooted the heck out of this today, and I can't seem to find any information on how to determine what is happening exactly.
Basically, on my development server, another developer is causing CLOSE_WAIT connections that eat up one or more apache2 processes for several hours if I don't restart apache2.

strace on any of the processes yields no information, only that it was able to attach.
mod_proxy is not enabled.
KeepAlive is on, KeepAliveTimeout is 15 seconds, MaxKeepAliveRequests is 100.

From what I've been reading, this may or may not be an apache issue at all, just that that's how CLOSE_WAIT works (the server is waiting for a FIN packet to close the connection).
I just can't believe that a server would be crippled so easily by not receiving a packet from a remote host telling it to close the connection. Especially without any intervention for well over an hour.
Any tips? I'm about to pull my hair out.

Edit :  Also, there are no unusual entries in any apache log files.

Edit 2: lsof -i shows only a single CLOSE_WAIT per hanging process. (That's what has been bothering me about this, as most other discussions talk about many CLOSE_WAIT connections, while I only have one per process.)
The nature of the code that is running (php) doesn't really lend itself to closing open connections and whatnot. I can run the same code that he is executing with the same session data, and not result in a hanging process.
Answer: Try setting up mod-status, and in the config (debian /etc/apache2/mods-enabled/status.conf) set your IP in Allow from, and set
<code>ExtendedStatus On
</code>
Then visit your server's default host website, and append /server-status/ to the end of the URL. That should give you more information on what the server is up to.
Sorry that this isn't really a fix, but more a way to get more information! I wasn't able to just comment on your question.
Comment: By the way : If you're trying to set up mod_status and you have many VirtualHosts in your config, you need to use a different port to call /server-status as the VirtualHost gets called before mod_status can be used.
Comment: Thanks much for this! At least now I can see what page is being executed and rule out any code problems.
Answer: See Running lsof -i shows a lot of connections in CLOSE_WAIT ? Should I worry and for similar discussion. Probably your developer doesn't close properly the connection. CLOSE_WAIT should time-out in normal circumstances, something is preventing it.
You could try a roll-back to application version that didn't have this bug and see what changed in-between.
Comment: lsof -i shows only a single CLOSE_WAIT per hanging process. (That's what has been bothering me about this, as most other discussions talk about many CLOSE_WAIT connections, while I only have one per process.)

The nature of the code that is running (php) doesn't really lend itself to closing open connections and whatnot. I can run the same code that he is executing with the same session data, and not result in a hanging process.


"CS0016: Could not write to output file" error when starting an app in IIS 7

Question: I am running Windows 7, and am not usually a developer in this setting, and have recently built a WCF Rest Service in C#, that I'm now trying to deploy to IIS just on my local machine. After much wrangling, I setup up the application, but when I navigate to the application, I get an error message:
    Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0016: Could not write to output file 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\scom_sibyll\8c0b945e\9329016\App_global.asax.eagmqgcd.dll' -- 'Access is denied. '

I have hunted the web to the best of my ability, and have changed the permissions on the Temporary ASP.NET Files fodler to allow the Network Service account full rights, and done the same with the Temp folder. It copies a number of files before failing, so it has write permissions presumably, so I checked the permissions to read from my source folder, and that is working as well. I additionally noted it's crapping out when it tries to to cache the DLL file, and tried turning off my Antivirus protection, as well as turning off UAC, just to see if I could figure out what is blocking this from occurring. I'm fresh out of ideas now. Anybody have any suggestions?
Comment: Make sure that your IIS Identity has the correct permissions on your folders.
Answer: For those looking here as I did, if the accepted answer doesn't resolve the issue you might try following this article: http://lordzoltan.blogspot.com/2011/02/aspnet-2-and-4-default-application-pool.html
In summary, it seems that the same error is sometimes displayed when the app pool user doesn't have access to the %TMP%/%TEMP% folder.
You'll need to grant IIS_IUSRS read and modify access over the temp folder of the user the app pool is running as.
This could either be the temp folder in the app pool user's profile, e.g. c:\Windows\ServiceProfiles\NetworkService\AppData\Local\Temp, or the system temp folder at c:\windows\temp.
Setting this up this resolved the issue for me.
Comment: +1 Granting access to `C:\Windows\Temp` seemed to fix it for me (despite the error message being to the contrary). Thanks!
Comment: +1. I ran into this issue after I was cleaning up many gigs of temp files in C:\Windows\Temp. The command I issued accidentally deleted the Temp folder itself and I had to recreate it. Any custom permissions on it were nuked at that time and then I started getting the error in the original question.
Comment: This solution worked perfectly for me. I had this issue after installing PHP on IIS 7
Comment: Perfect solution, worked for me. Most blogs suggest to change the App Pool Identity to Network User which is dangerous (albeit localhost).
Comment: Granting access to `C:\Windows\Temp` to IIS_USRS worked in Server 2016 as well. Thanks!
Answer: Sounds like the account that the WCF service is running under does not have access to write to the "Temporary ASP.NET Files" directory.
You could also try re-running regiis.
%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis -i
ASP.NET IIS Registration Tool (Aspnet_regiis.exe)
Comment: Check which application pool your web app is running in.  On the properties form of that application ppol you will be able to see the Identity the pool is running as.

Unless, you are using impersonate and IS is configured to use windows authentication.

Is there an entry posted to the event log?  Does it show which account could not access the folder?
Comment: That is what I would have thought as well, but the WCF service should be running as Network Service by default, right? Is ther ea way to confirm this? I checked the rights for this account. Additionally, I had already tried the aspnet_regiis command, I forgot to mention it, thank you. Any other ideas?
Comment: pERFECT! Thanks, sorry for the newbie question - it was not set to the right user account. Feel stupid now:D
Answer: On Windows 8/Server 2012 there is no support for aspnet_regiis any more.
I tried reinstall using windows features: fail.
I tried reinstalling IIS: fail.
I tried reinstall through WebPI: fail.
I solved the issue by setting the ACL's on the Windows Temp Directory.
Here is a powershell that does the job:
$dir = "C:\Windows\Temp"
$acl = get-acl -path $dir
$new = "IIS_IUSRS","Modify","ContainerInherit,ObjectInherit","None","Allow"
$accessRule = new-object System.Security.AccessControl.FileSystemAccessRule $new
$acl.SetAccessRule($accessRule)
$acl | Set-Acl $dir

Comment: I think full control is a little too much. Modify is just fine.
Answer: There're 3 step to do:
1 - check if "Application Pool Identity" is NetworkService

2 - NETWORK_SERVICE account needs full control on:
    . C:\Windows\Temp
    . C:\Windows\Microsoft.NET\Framework[related framework]\Temporary ASP.NET Files\

3 - restart the IIS

Error Solved
You could also try re-running regiis.
"%windir%\Microsoft.NET\Framework[related framework]\aspnet_regiis -i"
Answer: This KB resolved it for me, it appears that the temp file path was nonexistent in the OS environmental variables.
http://support.microsoft.com/kb/825791
Comment: 4 hours - and this was the cure!
An application had changed the environment variables and cocked it up - cheers fella!
Comment: Typing iisreset on command line as suggested in the answer link (after changing permission on temp folders) did the job for me
Answer: I granted read and write access to C:\Windows\Temp for the IIS_WPG group. This worked for me. I'm on Server 2003 R2 and IIS 6 and the group name is different. I fount it from http://learn.iis.net/page.aspx/140/understanding-built-in-user-and-group-accounts-in-iis/ where it says:
•The IUSR built-in account replaces the IUSR_MachineName account.
•The IIS_IUSRS built-in group replaces the IIS_WPG group.
Thanks to Dommer for suggesting the windows temp folder and to zcrar70 for the nice summery and a link with detailed description.
Answer: For me the solution was a combination of the fixes described here. I had to give to the NETWORK_SERVICE account full control on
C:\Windows\Temp
and
C:\Windows\Microsoft.NET\Framework[related framework]\Temporary ASP.NET Files\
and also change the Application Pool Identity to NetworkService.
Also do not forget to restart the IIS after you give full control to NETWORK_SERVICE on the Temp folders
Comment: To quote a great musical piece from Ghostbusters. -- Saving the day'! https://www.youtube.com/watch?v=mNuG25oq_b0
Answer: I was getting the same error while developing a web api using asp.net core 2.0 and it got resolved after restarting the machine.
Answer: Some times the Temp files might be locked by other process in the workstation. As a first step please reboot the workstation and check the application.
Answer: On Windows 8 absolutely nothing worked for me. One day app pools suddenly decided they no longer want to work under the NetworkService account.
I solved the problem by changing the app pool to work under my own user account. Not a great solution I know, but it worked.
Answer: This error happens when I use a bat file to delete temporary files. It probably deletes the directory itself and the given permissions are gone. So you have to restore them somehow.
The easiest way is to grant full control over following directories for Everyone:
C:\Windows\Temp
C:\Windows\Microsoft.NET\Framework[related framework]\Temporary ASP.NET Files\
Answer: I have permissions but I'm starting to get that error.
"restart machine" works for me
Answer: I started getting this error too recently in a corporate environment after the company implemented various security measures that took away admin rights from the users.
For developers, the company authorized the creation of accounts that could be used and we had to add these to the PC's admin group and then change the application pool's identity to use the account.
EXAMPLE (Windows 10)
Account created for developers: domain\developer
Someone with admin rights to the PC will have to do the following:

Go to Control Panel\User Accounts\Manage User Accounts
Click on the Advanced tab, and click on the Advanced user management button
In the lusrmgr page, click on the Groups folder in the left column to bring up groups in the center panel
Then secondary-click on the Administrators group and select "Add to Group..."
In the Administrators Properties panel, click the "Add..." button
A "Select Users, Computers, Service Accounts, or Groups" dialog opens. Add the account (domain\developer in this example)
Then click on the OK button (this button will be disabled if you don't have admin rights)

Now configure the App Pool:

Open IIS Manager, select Application Pools
Click on the application pool you want so it's highlighted, then click "Advanced Settings..." from the Actions panel on the right
Under the Process Model section, click on the Identity setting and the ellipses button
In the Application Pool Identity dialog, select "Custom account" and then click the Set button
Enter the account and the password

At this point I was able to close everything, restart IIS and then run my app. It could then access the temp folders it couldn't before.
Answer: In my case, when I change Load User Profile to True (App Pool -> Advance setting -> Load User Profile  -> set to true) it works.


How can I convert (binary) integer into array?

Question: I have the following form select field in my HTML code -
<select multiple class="form-control" name="uploadSites[]" id="uploadSitesDecoded">
    <option value="1">Site 1</option>
    <option value="2">Site 2</option>
    <option value="4">Site 3</option>
    <option value="8">Site 4</option>
    <option value="16">Site 5</option>
    <option value="32">Site 6</option>
</select>

Now I'd like to pre-select the options based on an integer value, e.g. the value 15 should pre-select Site 1, 2, 3 and 4.
As far as I know this can be done using the jQuery trigger method -
$('#uploadSitesDecoded').val([1,2,4,8]).trigger('change');

So what I'm trying to do is to convert 15 to a string or array as 1,2,4,8 (unless someone knows an easier way).
Comment: The fact that a select is involved is peripheral. The crux of the question is numeric conversion.
Comment: Just in case there are others like me who are guessing at what the conversion formula is, can you please detail what logic you are expecting to be ran against 15, or any number, to get the resulting array?  As it is, I see that 1+2+4+8 = 15, but that's just me guessing.
Comment: these are binary values, so e.g. 1111 (=15) would be Site 1, 2, 3 and 4.
Comment: Possible duplicate of [How to get all options of a select using jQuery?](https://stackoverflow.com/questions/590163/how-to-get-all-options-of-a-select-using-jquery)
Comment: Why does this seem like somebody writing C got into web development and decided to use boolean masks for options, instead of just sending an array?
Comment: I'm not a C developer :) actually I'm writing this in my spare time to learn :) I only want to store a single value into the DB instead of all the options.
Comment: Will probably help future readers if you edit your question details to clarify that the input integer represents a binary number and that you want to create an array of values that correspond to the "on" positions (where the bits have a value of 1). So "15" results in `[8, 4, 2, 1]` because "1111" contains "1" values at each of those positions while "6" would result in `[4, 2]` because "110" contains "1" values at only the "4" and "2" positions. Not sure the parenthetical in your title gets the message across.
Answer: parseInt(n, 10).toString(2)
This will give you the bit by bit representation of n, you can then loop through it char by char to get the power of 2 values corresponding:
let n = 15; // The number you want to turn into an array of power of 2
let array = [];
let binaryRepresentation = parseInt(n, 10).toString(2);
binaryRepresentation = binaryRepresentation.split("").reverse().join(""); // You need to reverse the string to get the power of 2 corresponding
for(let i = binaryRepresentation.length - 1; i >= 0; i--){
     if(binaryRepresentation[i] == 1){
         array.push(Math.pow(2, i));
     }
}
console.log(array); // Check the array

This exemple will give you [8, 4, 2, 1]
Comment: Thanks! Nearly what I was looking for, but looks like 6 would return [2,1]
Comment: My bad, you need to reverse the string, edited my answer. It should work as expected now, but do not hesitate to tell me if something is going wrong
Answer: One possible way to achieve this is to convert the integer value in to a binary string using toString(2). Then you can loop through the string and set the option matching the index of the string to selected, if the value is a 1. Something like this:

$('input').on('input', function() {
  var $options = $('#uploadSitesDecoded option');
  var binaryString = parseInt(this.value, 10).toString(2);
  binaryString.split('').reverse().forEach(function(n, i) {
    $options.eq(i).prop('selected', n === '1');
  });
}).trigger('input');

<input type="number" value="1" min="0" max="63" />

<select multiple class="form-control" name="uploadSites[]" id="uploadSitesDecoded" size="8">
  <option value="1">Site 1</option>
  <option value="2">Site 2</option>
  <option value="4">Site 3</option>
  <option value="8">Site 4</option>
  <option value="16">Site 5</option>
  <option value="32">Site 6</option>
</select>
Answer: I don't know if it is the most optimal way, but here is my solution.
First I change the given int to a binary and than reverse it. Than loop over it to find the 1's in the binary.

function dec2bin(dec){
    return (dec >>> 0).toString(2);
}

function reverseString(str) {
    return str.split("").reverse().join("");
}

$(document).ready(function() {
  var binary = reverseString(dec2bin(15));
  var values = [];
  var step = 1;

  for (var i = 0; i < binary.length; i++) {
    if(parseInt(binary.charAt(i)) === 1){
      values.push(step);
    }
    step *=2;
  }

  $('#uploadSitesDecoded').val(values).trigger('change');
});

<select multiple class="form-control" name="uploadSites[]" id="uploadSitesDecoded">
    <option value="1">Site 1</option>
    <option value="2">Site 2</option>
    <option value="4">Site 3</option>
    <option value="8">Site 4</option>
    <option value="16">Site 5</option>
    <option value="32">Site 6</option>
</select>
Answer: It looks like you're trying find an array of numbers that are both:

power of two (2^n)
less then 15

You may use Array.filter to accomplish this
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
let powerOfTwo = [1, 2, 4, 8, 16, 32, 64]

let result = powerOfTwo.filter(function(item) {
  return item < 15
})

// the result should be [1, 2, 4, 8]

Comment: Turns out that OP is actually looking for an array of values that represent the 1 bits of the binary equivalent of the input value. That means that if the input value were "6" instead of "15", OP needs an array of `[2, 4]` while your suggested approach would produce an array of `[1, 2, 4]`. No worries though, OP's objective is not very clear in the question details at the moment, and your answer is a good, simple way of producing the example output in the question.


How to addsub pages to Sylius

Question: Is there a way to create pages in Sylius with the following structure:
<code>top-level-a
top-level-a/sub-level-a-1
top-level-a/sub-level-a-2
top-level-b
top-level-b/sub-level-b-1
top-level-b/sub-level-b-2
</code>
I have only been able to create top-level pages.
I'm looking to do this in Sylius administration.
Comment: Possible duplicate of [Nested Routes with RoutingAutoBundle (Symfony CMF)](http://stackoverflow.com/questions/21816591/nested-routes-with-routingautobundle-symfony-cmf)
Answer: Currently it is not possible out of the box.
This part of the admin is 99% only symfony-cmf stuff, sylius got it only included. It probably will be done at some point but for sure it won't be before 1.0.0 stable release of sylius.
If you want nested pages, you can extend the class.
Exact same question is here, with answer:
Nested Routes with RoutingAutoBundle (Symfony CMF)
Comment: Ok thank you. For now I will create my own bundle and work around it.

How to addsub pages to Sylius


if I use singin with these dns services, can someone still figure out where I am hosted?

Question: Normally if I go with a hosting company, I use their own dns.  so anyone who does a whois can figure out which company I am hosted with.
If I go with something like dnsmadeeasy etc., I use their dns servers.  can someone still figure out where I am hosted somehow?
Comment: Why would someone care who you're hosted with?
Comment: just curious that's all.
Answer: It has to point to your host's IP address so of course they can.
If you're accessible from the internet, then someone can figure it out. There isn't much of a way around that.
Comment: An IP address can certainly identify your hosting company. Two simple examples: `traceroute` will show the names of the routers in front of your server, which usually include your hosting provider's name. `whois ip.ad.dr.ess` will give the owner of the IP address, usually the hosting provider.

Why do you need to "hide" this information?
Comment: but just an IP alone won't tell them I am hosting with abc hosting?
Comment: Sure it can, you just have to know how/where to look.
Answer: DNS just points at an IP address, regardless of whose nameservers you're using. IP addresses are traceable back to the ISP that ultimately owns them. There is no way around this.
Answer: Yes they will, the DNS name will point at an IP, which in turn belongs to someone somewhere.
You can use various tools to track the location of an IP, for example DNSstuff have a pretty good IP Whois tool
Answer: You might be thinking about Private domain registrations such as what GoDaddy Provides.  DNS information is meant to be shared in order for people to be successfully forwarded to sites/services you want them to reach.  In order for people to reach you, they need to know one or two things: DNS name or an IP.
Through DNS, anyone can find information about who's hosting what for your domain, just do a quick DNS lookup or go to http://www.intodns.com/, and find a bunch of sweet DNS information about a domain.
IP owners can be found doing a quick WHOIS lookup since ARIN keeps a public database of every address it allocates.  They HAVE TO be able to provide administrative contact information.  There is no way to make this private.


Radon-Nikodým (write the density as a limit)

Question:

Let $\mu$ be a probability measure and $\nu$ a $\sigma$-finite measure on $(\mathbb{R},\mathcal{B})$ with $\nu\ll\mu$. Show that it is $\mu$-a.s.
    $$
\lim_{h\to 0}\frac{\nu [x-h,x+h)}{\mu [x-h,x+h)}=\frac{d\nu}{d\mu}(x).
$$

I do not have many ideas...
Set $f:=\frac{d\nu}{d\mu}$. Then $\nu [x-h,x+h)=\int_{[x-h,x+h)}f\, d\mu$ and
$$
\lim_{h\to 0}\frac{\nu [x-h,x+h)}{\mu [x-h,x+h)}=\lim_{h\to 0}\frac{\int_{[x-h,x+h)}f\, d\mu}{\mu [x-h,x+h)}.
$$
But that does not really help.
Can you help me, please?
Miro
Answer: Since $\nu$ is a $\sigma$- finite measure, you can find a sequence of disjoint sets $E_n$ such that $\Bbb{R} = \cup_n E_n$ and $\nu(E_n) < \infty.$ Define now $$\nu_n(C)= \nu(C \cap E_n).$$ Note that $\nu(C) = \sum_n \nu_n(C)$
Let $f_n = \frac{d \nu_n}{d\mu} $ and consider the $\sigma$ algebra $\mathcal{B}_k$ generated by the dyadic decomposition of order k $$\mathcal{B}_k = \sigma \bigg(\bigg(\frac{i-1}{2^k},\frac{i}{2^k}\bigg]; i \in \mathbb{Z}\bigg)$$
 Note that this $\sigma$- algebra contains atoms (indivisible sets) of the form $\bigg(\frac{i-1}{2^k},\frac{i}{2^k}\bigg]$. Define now the conditional expectation of $f_n$ with respect to $\mathcal{B}_k$
$$f_n^k : = \Bbb{E} \big[f_n \big\vert \mathcal{B}_k\big] $$ Note that $$\nu_n \bigg(\bigg(\frac{i-1}{2^k},\frac{i}{2^k}\bigg]\bigg) = \int_{\bigg(\frac{i-1}{2^k},\frac{i}{2^k}\bigg]}  \, d\nu_n =\int_{\bigg(\frac{i-1}{2^k},\frac{i}{2^k}\bigg]}f_n  \, d\mu_n =\int_{\bigg(\frac{i-1}{2^k},\frac{i}{2^k}\bigg]}f_n^k  \, d\mu_n  = f^k_n(i 2^{-k})\int_{\bigg(\frac{i-1}{2^k},\frac{i}{2^k}\bigg]}  \, d\mu_n  = f^k_n(i 2^{-k})\mu_n\bigg(\bigg(\frac{i-1}{2^k},\frac{i}{2^k}\bigg]\bigg) $$
Therefore
$$\frac{\nu_n \bigg(\bigg(\frac{i-1}{2^k},\frac{i}{2^k}\bigg]\bigg)}{\mu_n\bigg(\bigg(\frac{i-1}{2^k},\frac{i}{2^k}\bigg]\bigg)} = f_n^k(i 2^{-k}) $$
Since $f_n^k$ is $\mathcal{B}_k$ measurable and $f(x) = f_n^k(i 2^{-k}) $ for every $x \in \bigg(\frac{i-1}{2^k},\frac{i}{2^k}\bigg]$.
Now note that $(f^k_n,\mathcal{B}_k)_k$ is a martingale.
Indeed we only need to check that
$$  \Bbb{E} \big[f^{k+1}_n \big\vert \mathcal{B}_k\big]  = f^k_n $$
consider the set $\big(\frac{i-1}{2^k},\frac{i}{2^k}\big]$ and evaluate
$$\Bbb{E}\bigg[{1_{\big(\frac{i-1}{2^k},\frac{i}{2^k}\big]}\Bbb{E} \big[f^{k+1}_n \big\vert \mathcal{B}_k\big]}\bigg] =
 \Bbb{E}\bigg[{1_{\big(\frac{2i-2}{2^{k+1}},\frac{2i-1}{2^{k+1}}\big]}\Bbb{E} \big[f^{k+1}_n \big\vert \mathcal{B}_k\big]}\bigg]  + \Bbb{E}\bigg[{1_{\big(\frac{2i-1}{2^{k+1}},\frac{2i}{2^{k+1}}\big]}\Bbb{E} \big[f^{k+1}_n \big\vert \mathcal{B}_k\big]}\bigg] = \nu_n \bigg(\big(\frac{2i-2}{2^{k+1}},\frac{2i-1}{2^{k+1}}\big]\bigg) + \nu_n \bigg(\big(\frac{2i-1}{2^{k+1}},\frac{2i}{2^{k+1}}\big]\bigg)  = \nu_n \bigg(\big(\frac{i-1}{2^{k}},\frac{i}{2^{k}}\big]\bigg) = \Bbb{E}\bigg[{1_{\big(\frac{i-1}{2^k},\frac{i}{2^k}\big]}f^{k}_n\bigg] } $$
Now you only need to apply the martingale convergence theorem and note that $\bigvee_k \mathcal{B}_k = \sigma(\cup_k \mathcal{B}_k) = \mathcal{B}$ to conclude that almost surely $f_n^k \to f_n $ since there are only countable null sets where the convergence fails, you can consider them together and get
$f^k(x):=\sum_n f_n^k(x)  \to \sum_n f_n(x) = f(x) = \frac{d \nu}{d \mu}$
finaly note that for every $x \in \mathbb{R}$ that has not the form $i 2^{-k}$  you can find $h_k, \tilde{h}_k \to 0$ $ \frac{\nu_n \bigg(\bigg(\frac{i-1}{2^k},\frac{i}{2^k}\bigg]\bigg)}{\mu_n\bigg(\bigg(\frac{i-1}{2^k},\frac{i}{2^k}\bigg]\bigg)} =\frac{\nu_n \bigg(\big(x - h_k,x + \tilde{h}_k\big]\bigg)}{\mu_n\bigg(\big(x - h_k,x + \tilde{h}_k\big]\bigg)}= f^k(x) \to f(x)$
I hope this is helpful
remark: you can adapt this argument to the general case with $h \to 0$ and every $x \in \mathbb{R}$
Answer: This is theorem 5.4 page 115 from Theory of the integral, S. Saks. It is proved via Vitali's covering theorem.
Answer: A good starting point here is to study the standard proof to Lebesgue differentiation theorem and then to see how to generalize it to a finite positive Borel measure $\mu$. Actually, the proof goes almost the same with the only exception that to get the Hardy-Littlewood maximal inequality for $\mu$ you will need Besicovitch covering instead of Vitali covering. (Wiki gives a nice sketch of all mentioned notions.)


Question about a guitar lick involving string bending and playing a higher note while the string's bent

Question: I'm transcribing a guitar solo, the one in a live version of Bob Marley's "No Woman No Cry."
At 4:25, he bends the 11th fret of the high e string up a full step, and right before he comes down, I think he hits the 14th fret, then comes back down on the 11th fret.
It's a pretty sick sound, but I have no idea how to do it smoothly. My first stab at it was to bend with the 2nd finger and fret with the pinky, but it is a stretch--first finger bend doesn't seem strong enough to get up a full tone. I also just thought about it while writing this, but does he tap the 14th fret with his picking finger to get it off that fast?
Thanks,
Matt
Comment: Sound like a whole-tone bend on the 16th fret on the B string and a short note on the same fret on the E string to me. It's not that difficult to play; you can use up to three fingers for the bend and your little finger for the note on the E string.
Comment: @Your UncleBob - sounds right! Doing it any other way won't be as smooth. Post your answer! C# seems  a bit of an odd key...
Comment: @YourUncleBob I'm thinking that this is just a technique question, which are on-topic, if I'm not mistaken. Don't see anyone VTCing yet, so if you want some credit, post now before you get snaked! :)
Comment: @YourUncleBob That makes a lot more sense! I was coming into the lick (a few seconds before 4:25) from the G string 7th fret slide up to 10th fret and went to the 9th-11th fret on the B and E string for the little melody. It made figuring out that bend pretty awkward.
Comment: @Tim I assumed this question would get closed as off-topic for being about "transcribing (...) a particular song".
Comment: @YourUncleBob - answer anyway - what's to lose? No closures yet!
Comment: @user45266 Okay then :-)
Comment: the forum is called "_music theory and practice_", surely technique is exactly about the latter of those? I cannot imagine why anyone would think this question was off-topic
Answer: The most straightforward way to play this would be to do a whole-tone bend on the B-string at the 16th fret, and then play the E-string at the same fret during the bend. You can use up to three fingers to do the bend, and the bending motion actually moves the fingers out of the way for your little finger to fret the E-string, so it's quite easy to play it this way.
(The B string is also the string with the lowest tension when using a regular string set in standard tuning, so the larger a bend, the more likely it is played on the B string.)
This recording from the 1975 Natty Dread tour features Al Anderson on lead guitar. He was not Jamaican, but an American guitarist who had attended Berklee College of Music and played in a number of (blues-) rock bands before joining The Wailers in 1974. He added a more rock-tinged sound to the band, as is evident in this solo.


Azure Cli update variable with JSON object in powershell script

Question: I am trying to update Azure pipeline group variable from within a PowerShell script using the az cli.
I have the command updating the variable but its modifying the json and removing all the quotes (")
so instead of updating variable from
{"a": "a"}
to
{"a": "b"}
it updates it to {a: b}
I am running the following command
$json = ConvertTo-Json $a -Compress
Write-Host $json
az pipelines variable-group variable update --group-id 236 --name MyJson --detect true --secret false --value $json

The outut of the write-host is
{"a": "b"}

Output of the the az pipelines variable-group variable update is
{
 "MyJson": {
   "isSecret": null,
   "value": "{a:b}"
  }
}

What am I doing wrong and how can I get it to update without modifying the JSON?
Answer: All the variables are stored as strings and are mutable, and it already means string when it's in the double-quotes. So it removes all the quotes inside. If you want to make the quotes also stores as the value, then you need to transfer them like this:
{\"a\":\"b\"}

Or just use single quotation marks:
'{"a":"b"}'

Finally, it will look like this:

It's not necessary to store the double-quotes as the value. So I don't recommend you do this without any special reason.
Comment: so interesting when you do it with a var put the var in `' '` doesn't work.  So I have had to do a replace and replace all the `"` with `\"` then pass it to the command
Answer: So while @charles works when passing the data straight to the command like that, we are using variables.
While I understand what's going on here, still think the az command could handle this better.
So to get this to work I had to do a replace on the string and replace all " with \"
Annoying that this has to be done but it works, so my final commands look like this:
$new_json = $json.replace("`"", "\`"")
az pipelines variable-group variable update --group-id $(VarGroupId) --name var_name --detect true --secret false --value $new_json

Comment: Why not accept my answer instead of adding another answer when you just copy the solution?!
Comment: The json string must contain backslashes, rather than just escaping the json to fit in the string. The answers are different.


Can I use Glide to have CircleImage with overlapping icon like in Facebook messenger?

Question: I'm searching for a library that could help me make my cricular image view's like in Facebook Messenger (with z blue Messenger icon or grey Facebook icon). How they came up with it? Can it be done with Glide? If not, with what then?
Comment: Possible duplicate of [How do rounded image with Glide library?](http://stackoverflow.com/questions/25278821/how-do-rounded-image-with-glide-library)
Answer: Using CircleImageView/CircularImageView/RoundedImageView is not advised, we have many people reporting issues with them mostly because they hack the Bitmap out of the Drawable which is incompatible with the most basic feature of Glide: .crossFade(), also GIF animations may likely break with them.
The correct Glide way is to use Transformations, you can find a good enough one here:
https://stackoverflow.com/a/25806229/253468
Glide 4.0 will have a built-in CircleCrop transformation.
Overlay icon
The overlay icon can be also added with a transformation:
// .transform(new CircleTransform(context)), new StatusTransform(context, user.getStatus()))
public static class StatusTransform extends BitmapTransformation {
    private final Status status;
    public StatusTransform(Context context, Status status) {
        super(context);
        this.status = status;
    }

    @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        int w = toTransform.getWidth(), h = toTransform.getHeight();
        Bitmap result = pool.get(w, h, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(toTransform);
        switch(status) {
            case Online:
                canvas.draw*(online graphics);
                break;
            case Offline:
                canvas.draw*(offline graphics);
                break;
        }
        return result;
    }

    @Override public String getId() {
        return getClass().getName() + status;
    }
}

or simply have two ImageViews:
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/user_profile_size"
    android:layout_height="@dimen/user_profile_size"
    >
    <ImageView
        android:id="@id/user_profile"
        android:layout_width="@dimen/user_profile_size"
        android:layout_height="@dimen/user_profile_size"
        />
    <ImageView
        android:id="@id/user_status"
        android:layout_width="@dimen/user_status_size"
        android:layout_height="@dimen/user_status_size"
        android:layout_gravity="bottom|end"
        />
</FrameLayout>

Both have pros and contras: Transformation results can be cached, so if you have 3 statuses Glide will store each user's profile image 3 times overlayed with a little icon, so it probably will be applied only once and then it's just a matter of decoding the cached PNG (not JPEG, because circle forces transparency). With two ImageViews you have to have two Glide load lines (or a Glide and a setImageResource), it may be a little slower to draw and layout. So you can trade processor time to disk space with the two approaches. With the ImageView approach you can also animate the transitions between status changes and have the status always show, even when the profile image is loading slowly because of bad network.
In case of the Transformation if the code of the transform method changes, you'll need to invalidate the cache, which can be achieved by:
.signature(ApplicationVersionSignature.obatain(context)) // every release
// or
.signature(new StringSignature(String.valueOf( System.currentTimeMillies() / (10 * 60 * 1000) ))) // every 10 minutes

Alternatively this invalidation can be built into the getId() method somehow.
Comment: I'm using that one :)
Comment: Still, it does not resolve my problem. Btw, when Glide 4.0 comes up?
Comment: I'm sorry, somehow I missed that really bad :) There's no set date yet, I think it's still in alpha.
Comment: That's nice :) I must try this - it will be for static purposes so no status changing need. Can I ask You for example of `canvas.draw*(online graphics)` ?
Comment: It's not actual code, I just sketched this here in the editor. I meant stuff like: `drawBitmap`, `drawCircle`, `drawText`, `drawRoundRect`. Or: `Drawable d = context.getDrawable(R.drawable.ic_status_x); int s = context.getResources().getDimensionPixelSize(R.dimen.user_status_size); d.setBounds(w - s, h - s, w, h); d.draw(canvas);`
Comment: Thanks a lot :) I'm still in my infancy with coding.
Comment: Great library and fastest one.
Comment: Added a transform to round corners, but now GIFs are always static. Short of creating a new gif at runtime I don't see how a transform is better than a circle view.
Comment: @GrzegorzAdamHankiewicz transformations applied to GIF do each frame separately, so it should work. Are you using `asBitmap` by any chance? If you want more help, please open an issue on GitHub so we can have a proper conversation.
Comment: @TWiStErRob found the problem, I was extending `BitmapTransformation` instead of implementing `Transformation`. Looking at https://github.com/wasabeef/glide-transformations helped.
Answer: 1.I would suggest you to checkout Image Management Library by Facebook that is Fresco that is pretty awesome and mature as compared to other Image Loading Library.
2.Fresco has SimpleDraweeView as custom image view which supports Rounded Corners and Circles link and supports Animated(.gif, .webp) as well as Normal Images(.jpg, .png).
3.Fresco handles all the things caching of images with 3 Tier architecture ( BITMAP_MEMORY_CACHE, ENCODED_MEMORY_CACHE and DISK_CACHE). It also reduces OOM(Out Of Memory) issues. When image in a view goes out of screen it automatically recycles the bitmap, hence releasing the memory.
Answer: if your target is only have a CircleImage give a shot at this:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RoundedImageView extends ImageView
{
    public RoundedImageView(Context context)
    {
        super(context);
    }

    public RoundedImageView(Context context, @Nullable AttributeSet attrs)
    {
        super(context, attrs);
    }

    public RoundedImageView(Context context, @Nullable AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public RoundedImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)
    {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public static Bitmap getCroppedBitmap(Bitmap bmp, int radius)
    {
        Bitmap sbmp;

        if (bmp.getWidth() != radius || bmp.getHeight() != radius)
        {
            float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
            float factor = smallest / radius;
            sbmp = Bitmap.createScaledBitmap(bmp, (int) (bmp.getWidth() / factor), (int) (bmp.getHeight() / factor), false);
        }
        else
        {
            sbmp = bmp;
        }

        Bitmap output = Bitmap.createBitmap(radius, radius,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xffa19774;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, radius, radius);

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.parseColor("#BAB399"));
        canvas.drawCircle(radius / 2 + 0.7f,
                radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(sbmp, rect, rect, paint);

        return output;
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        Drawable drawable = getDrawable();

        if (drawable == null)
        {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0)
        {
            return;
        }
        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

        int w = getWidth(), h = getHeight();

        Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
        canvas.drawBitmap(roundBitmap, 0, 0, null);
    }
}

and in your layout you can use this:
<mypkg.example.RoundedImageView
    android:id="@+id/roundimg"
    android:layout_width="54dp"
    android:layout_height="54dp"
    android:layout_gravity="center"/>

and programmatically you can set a letter..
Rect textBounds = new Rect();
String text = "N"
Bitmap bitmap = Bitmap.createBitmap(imageView.getLayoutParams().width, imageView.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.DKGRAY);
TextPaint p = new TextPaint();
p.setColor(Color.WHITE);
p.setTextSize(imageView.getLayoutParams().height / 2);
p.setAntiAlias(true);
p.setTextAlign(Paint.Align.LEFT);
p.getTextBounds(text, 0, text.length(), textBounds);

int cx = imageView.getLayoutParams().width / 2;
int cy = imageView.getLayoutParams().height / 2;
canvas.drawText(text, cx - textBounds.exactCenterX(), cy - textBounds.exactCenterY(), p);

imageView.setImageBitmap(bitmap);

or an image ..
// image grabbed from an input stream
Bitmap photo = BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(photo);

is this useful? more infos and credits here How to create a circular ImageView in Android?


Bootstrap set active tab with request.path_parameters

Question: I have a ruby on rails application which includes a header-file on every page.
Now i want to set the active pathes.
<li class="<%= 'active' if current_page?(users_path) %> dropdown">test

This works without trouble.
Now i have a page for create a new user => users/new. So i tried around with request.path_parameters
<% if ((request.path_parameters[:controller] == "users") && (request.path_parameters[:action] == "new")) %>

test

<% end %>

The problem is that the if-condition is always true.
I can change it to:
<% if ((request.path_parameters[:controller] == "userstest") && (request.path_parameters[:action] == "newtest")) %>

test

<% end %>

and its still true.
Anybody know a solutions for this?
When i try:
current_page?(url_for(:controller => 'users', :action => 'new'))

i get a No route matches-error
Comment: why can't you use a your helper method for users/new too? something like: `test`. Your paths can't be same and i don't think you'll require your post method like create or destroy with delete method
Answer: Use == instead of =
<% if ((request.path_parameters[:controller] == "users") && (request.path_parameters[:action] == "new")) %>

test

<% end %>
There are also helper methods available to get the controller and action.
controller_name and
action_name
Comment: Soory for this question. Thanks! My bad. Sitting to long in front of this code.


Erro ao receber array do php em ajax

Question: tenho um script como seguinte código:

$("#submit").click(function(){

       var dataString ={
  nome : $("#nome").val(),
  status : $("#status").val(),
  id : $("#id").val()
    };

        $.ajax({
            type: "POST",
            url: "../vendors/php/ins_salvar_menu.php",
            data: dataString,
            cache : false,
            success: function(data){
     console.log(data.nome);
     console.log(data);
              if(data.type == 'add'){
                 $("#menu-id").append(data.menu);
              } else if(data.type == 'edit'){
                 $('#nome_show'+data.id).html(data.nome);
                 $('#status_show'+data.id).html(data.status);
              }
              $('#nome').val('');
              $('#status').val('');
              $('#id').val('');

            } ,error: function(xhr, status, error) {
              alert(error);
            },
        });
    });

Que envia os dados para uma pagina PHP cuja seu conteúdo é o seguinte:

<?php
include "../../seguranca/banco/conexao_banco.php";
include "operacaobd_php.php";
date_default_timezone_set ("America/Sao_Paulo");
$data_criacao = date('Y-m-d H:m:s');
$data_modificacao = date('Y-m-d H:m:s');
if($_POST['id'] != ''){
 $tabela="categorias";
 $dados = array(
  'nome' => $_POST['nome'],
  'status' => $_POST['status'],
  'data_modificacao'=> $data_modificacao
 );
 $condicao = array(
  'id' => $_POST['id']
 );
 $sql_upd_categorias_resultado = alterar($tabela, $condicao, $dados);

 $arr['type']  = 'edit';
 $arr['nome'] = $_POST['nome'];
 $arr['status']  = $_POST['status'];
 $arr['id']    = $_POST['id'];
}else{
 $tabela="categorias";
 $dados = array(
  'nome' => $_POST['nome'],
  'status' => $_POST['status'],
  'data_criacao'=> $data_criacao,
  'data_modificacao'=> $data_modificacao
 );
 $sql_ins_categorias_resultado = adicionar($tabela, $dados);

 $arr['type'] = 'add';
 $arr['menu'] = '
lastInsertId().'" >

Drag

lastInsertId().'">'.$_POST['nome'].'
                        /lastInsertId().'">'.$_POST['status'].'
                            lastInsertId().'" nome="'.$_POST['nome'].'" status="'.$_POST['status'].'" >
                            lastInsertId().'">

';
}
return json_encode($arr);

?>

A função adicionar:
function adicionar($adc_tabela, $adc_dados){

// Armazenas os dados do array
$adc_campos = array_keys($adc_dados);
print_r($adc_campos);
// contagem dos campos existentes
$adc_n_campos = count($adc_dados);
// Inicia a sintaxe
$adc_sintaxe = "INSERT INTO ".$adc_tabela." (";
//monta o resto da estrutura
for($adc_aux=0; $adc_aux<$adc_n_campos; $adc_aux++){
    $adc_sintaxe.= $adc_campos[$adc_aux].", ";
}
//retira a ultima virgula
$adc_sintaxe = substr($adc_sintaxe, 0, -2);
// fecha os campos e adciona o VALUES na sintaxe
$adc_sintaxe .= ") VALUES (";
//adiciona os valores na sintaxe
for($adc_aux=0; $adc_aux<$adc_n_campos; $adc_aux++){
    $adc_sintaxe.= ":".$adc_campos[$adc_aux].", ";
}
// Retira a ultima virgula
$adc_sintaxe = substr($adc_sintaxe, 0, -2);
// Fim da sintaxe
$adc_sintaxe .= ")";
// chama a função global para fazer conexão com o Banco de dados
global $conexaobd;
// prepara a sintaxe
$adc_preparado = $conexaobd->prepare($adc_sintaxe);
// seta os valores de cada campo
for($adc_aux=0; $adc_aux<$adc_n_campos; $adc_aux++){
    /*if((!$adc_dados[$adc_campos[$adc_aux]]) AND (!$adc_dados[$adc_campos[$adc_aux]]=="0")){
        $adc_dados[$adc_campos[$adc_aux]] = NULL;
    }
    OBS.: o IF acima buga com o 0, então se algum dos dados for ==0 ele atribui NULL, então o IF acima corrige o problema com o 0
    */
    if(!$adc_dados[$adc_campos[$adc_aux]]){
        if($adc_dados[$adc_campos[$adc_aux]] == "0" ){
            $adc_dados[$adc_campos[$adc_aux]] = 0;
        }else{
            $adc_dados[$adc_campos[$adc_aux]] = NULL;
        }
    }
    $adc_preparado -> BindValue(":".$adc_campos[$adc_aux], $adc_dados[$adc_campos[$adc_aux]]);
    /*
     print_r($adc_dados[$adc_campos[$adc_aux]]);
     echo"
";
    */
}
  return $adc_preparado->execute();
}

O problema em sí:

No arquivo JS ele manda para esta pagina PHP, e retorna os dados. onde ele verifica se data.type=="add" ou se é data.type="edit", infelizmente ao dar um console.log(data.type) a mensagem é undefined, e um console.log(data) retorna um array que não devia estar retornando, que no caso é:
Array(
    [0] => nome
    [1] => status
    [2] => data_criacao
    [3] => data_modificacao
)

qual seria o erro?
Comment: @13dev de onde saiu esse método que você adicionou na pergunta? Esse tipo de edição deve ser evitada, ainda mais que o autor não cita nada desse trecho nem em posts deletados. Sempre opte por orientar que o proprio autor edite a pergunta.
Comment: @Articuno entendido, erro meu, este método é do próprio autor, onde na resposta que fiz pedi esse mesmo método, mas ao pedir disse ao autor para editar a resposta ( enganei-me ao pedir, queria dizer pergunta ).
Answer: Aqui tens o código com algumas alterações e melhores hábitos para a tua programação.
Para ser direto teu código está mau, aqui vai a possível solução que seria usares a função isset():
tem em atenção os comentários que deixei
<?php
include "../../seguranca/banco/conexao_banco.php";
include "operacaobd_php.php";

date_default_timezone_set ("America/Sao_Paulo");
$data_criacao = date('Y-m-d H:m:s');
$data_modificacao = date('Y-m-d H:m:s');
$tabela = "categorias";

// começa sempre a verificar pela negatividade
// ou seja nega sempre a condição só no final é que retornas o pretendido
// usa função isset para verificares se a variável existe
if(!isset($_POST['id'])){

    $dados = array(
        'nome' => $_POST['nome'],
        'status' => $_POST['status'],
        'data_criacao'=> $data_criacao,
        'data_modificacao'=> $data_modificacao
    );

    $sql_ins_categorias_resultado = adicionar($tabela, $dados);

    $arr['type'] = 'add';

    // nunca deves retornar html em ajax é má pratica!
    // tenta retornar so um array com os dados necessários
    // e com jquery ou js tratas os dados retornados pela requisição ajax
    $arr['menu'] = '
lastInsertId().'" >

Drag

lastInsertId().'">'.$_POST['nome'].'
                        /lastInsertId().'">'.$_POST['status'].'
                            lastInsertId().'" nome="'.$_POST['nome'].'" status="'.$_POST['status'].'" >
                            lastInsertId().'">

';

    return json_encode($arr);

}
// se o id esta definido
// verifica se os dados neste caso o POST EXISTE não podes confiar no utilizador
$dados = array(
    'nome' => $_POST['nome'],
    'status' => $_POST['status'],
    'data_modificacao'=> $data_modificacao
);

//porque guardas o retorno se não esta a usa-lo (codigo apresentado na resposta)
//variavel com nome tão grande!
$sql_upd_categorias_resultado = alterar($tabela,  array( 'id' => $_POST['id'] ), $dados);

$arr['type']    = 'edit';

// verifica se os dados existem, não deves confiar no utilizador
$arr['nome']    = (isset($_POST['nome'])) ? $_POST['nome']  : '';
$arr['status']  = (isset($_POST['status'])) ? $_POST['status'] : '';
$arr['id']      = (isset($_POST['id']) && is_numeric($_POST['id'])) ? $_POST['id']  : null;

return json_encode($arr);

?>

EDIT:
Aqui esta o código com os defeitos que apontei corrigidos,
antes de tudo se tiveres interesse entra neste site
que te dará boas maneiras de programar em PHP.
<?php
// minha opiniao: funções que alteram configuração do php eu meto sempre no topo
date_default_timezone_set ("America/Sao_Paulo");

// se possivel usa ingles em nomes de ficheiros exemplo: ConnectDB.php
include "../../seguranca/banco/conexao_banco.php"; // sugestão: muda para OpenDatabase.php ou ConnectDB.php
include ("operacaobd_php.php"); // sugestão muda o nome do arquivo pois não é claro ex: OperationsDatabase.php

// tenta usar nome de variaveis em ingles
// assim todos que verem teu codigo saberá do que se trata
$createData = date('Y-m-d H:m:s');
$modificationData = date('Y-m-d H:m:s');
$table = "categorias";

//verifica se os indices do $_POST existe
$postName = (isset($_POST['nome'])) ? filter_var($_POST['nome'], FILTER_SANITIZE_STRING) : NULL;
$postStatus = (isset($_POST['status'])) ? filter_var($_POST['status'], FILTER_SANITIZE_STRING) : NULL;
$postID = (isset($_POST['id']) && is_numeric($_POST['id'])) ? $_POST['id'] : NULL;

// fiz tudo de uma vez mas faz separadamente
if(is_null($postName) || is_null($postStatus))
{
    return json_decode(['error' => 'algumas das variveis post não estao definidas']);
}

if($postID === NULL){

    $dados = [
        'nome' => $postName,
        'status' => $postStatus,
        'data_criacao'=> $createData,
        'data_modificacao'=> $modificationData,
        'type' => 'add'
    ];

    if(!adicionar($tabela, $dados)){
        return json_encode(['error' => 'Erro ao adicionar a base de dados por favor tente novamente']);
    }

    //$arr['menu'] = '
lastInsertId().'" >
Drag

lastInsertId().'">'.$_POST['nome'].' /lastInsertId().'">'.$_POST['status'].'     lastInsertId().'" nome="'.$_POST['nome'].'" status="'.$_POST['status'].'" >lastInsertId().'">
';

    // aqui adiciona os valores que estavas juntando ao HTML
    // no array acima ($arr['menu']) como exemplo ['data-id' => $conexaobd->lastInsertId()]
    return json_encode(['type' => 'add', 'error' => null]);

}

$dados = [
    'nome' => $postName,
    'status' => $postStatus,
    'data_modificacao'=> $modificationData
];

if(!alterar($table, [ 'id' => $postID ], $dados)){
    return json_encode(['error' => 'Erro ao alterar na base de dados por favor tente novamente!']);
}

return json_encode([
        'type' => 'edit',
        'nome' => $postName,
        'status' => $postStatus,
        'id' => $postID,
        'error' => null
]);

Comment: 13, testei tudo, só consegui trocando o `return` pelo `print`, e agr ele ta recebendo, adicionei tbm no script o `data=JSON.parse(data);`
Comment: posto isto, se quiseres o script 'corrigido' pelos erros que mencionei é só dizeres para alterar a resposta
Comment: obrigado, realmente concordo com sua correção na verdade esse código não é totalmente meu, ele é de um exemplo pego na internet, algumas coisas ai  como função de insert e update são minhas. mas obrigado pela critica construtiva. Se poderes também postar o código corrigido me ajudará muito!
Comment: sim e resolveu o teu problema este codigo ? uns minutos e ja posto o codigo com os defeitos que apontei
Comment: resolveu, sim, na verdade ele é maior que isso, oq postei é uma parte do código do  plugin nestable.
Comment: resposta editada com o script em 'condições'
Comment: Obrigado, ficou show. mas quando dou um console.log(data.type); ele continua me retornando undefined. :C
Comment: faz `console.log(data); ` e mostra o que retornou
Comment: retorna isso:
`Array
(
    [0] => nome
    [1] => status
    [2] => data_criacao
    [3] => data_modificacao
)`
Comment: o correto não seria ele retornar oq esta dentro do json_encode?
Comment: edita a resposta e coloca o código desta função: `adicionar()`, sim seria o correto, mas ele não esta a chegar ate esse return por isso o erro pode estar nessa função
Comment: Adicionado, é uma função que uso em todo o site, tenho ela que é INSERT, tenho a  UPDATE e DELETE
Comment: pois é estas a fazer um `print_r($adc_campos)` dentro da função **adicionar** por isso é que esta a te retornar php e não json!
Comment: retira todo o codigo debug(print_r, var_dump, etc) e testa lá a aplicação
Comment: Então retirei, e nada, ta retornando vazio agora. Só para verificar existe um else do $postID === NULL ?
Comment: Tenta fazer debug linha por linha no arquivo, não porque dentro dessa mesma condição tem um return, o que quer dizer que vai parar ali o script
Comment: Obrigado pela ajuda, to tentando faz tempo aqui, vou ficar tentando se consigo achar esse erro....
Comment: agora que adicionaste `data=JSON.parse(data);` tenta usar o return invés do print
Comment: aparece o seguinte: `Uncaught SyntaxError: Unexpected end of JSON input
    at JSON.parse ()
    at Object.success (menu.js:55)
    at i (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at z (jquery.min.js:4)
    at XMLHttpRequest. (jquery.min.js:4)`
Comment: verifica se o que vem do arquivo php e esta convertido para json
Answer: Em chamadas $.ajax(); do jQuery, quando você recebe um json você pode especificar o parâmetro dataType: "json".
O data recebido por success: function(data){} será convertido para objeto,
se a resposta não for um json válido, vai cair em error: function(xhr, status, error){}
para ser tratado pela sua aplicação.
$.ajax({
    type: "POST",
    url: "../vendors/php/ins_salvar_menu.php",
    data: dataString,
    dataType: "json",
    cache : false,
    success: function(data){
        console.log(data.nome);
        console.log(data);
        if(data.type == 'add'){
            $("#menu-id").append(data.menu);
        } else if(data.type == 'edit'){
            $('#nome_show'+data.id).html(data.nome);
            $('#status_show'+data.id).html(data.status);
        }
        $('#nome').val('');
        $('#status').val('');
        $('#id').val('');

    } ,error: function(xhr, status, error) {
        alert(error);
    },
});

Comment: Não entendi porque negativaram a minha resposta, apenas dei uma dica para evitar o uso do data=JSON.parse(data); implementando um dataType: "json"

Erro ao receber array do php em ajax


Pegar data anterior ao ano 2000 MySql

Question: Preciso selecionar todos os funcionários com data de contratação anterior ao ano 2000. Como posso colocar essa condição na cláusula Where?
SELECT nome_funcionario FROM funcionario WHERE data_contratacao...

Answer: Eu faria de maneira diferente, utilizando a função YEAR().
SELECT nome_funcionario FROM funcionario WHERE YEAR(data_contratacao) < 2000

Answer: SELECT nome_funcionario FROM funcionario WHERE data_contratacao < '2000-01-01'

Pegar data anterior ao ano 2000 MySql


Pressflow online users stats does not show correctly

Question: I can not say enough of performance gain that my site gained through migrating to Pressflow + Varnish. However I have a rather annoying problem with Pressflow. After migrating to Pressflow 6, the statistics of online users does not show correctly. For example the forum shows 70 online users while there are actually over 1000 users online.
I am wondering whether it can be fixed or it is just a "collateral damage" that one should afflict to gain the performance. If it can be fixed, appreciate your hints to do so, and if not, how else can I show the correct stats.
Thanks
Answer: I believe this is because of how Pressflow handles anonymous sessions.  It implements what are called "lazy sessions", which only creates sessions when actually needed.
This behavior can be changed by putting a drupal_start_session() in a custom hook_init.  If you do this, though, you will likely take a performance hit.
I have had to do this with a few projects to fix session problems when moving from Drupal 6 to Pressflow 6.
EDIT TO ADDRESS COMMENT:
You can simply put the hook in a custom module, such as
function mymodule_init ()
{
  drupal_start_session();
}

Note that you should really profile before and after to see what the performance impact is.
Comment: Where should I put the hook_init snippet? Thanks
Comment: if the performance hit is substantial and you only need it to show the total number of online users you could just "estimate" it by extrapolating from the numbers that are reported. For example multiple 70 times 15 to get to the a more realistic number ;-)


cannot edit powershell_ise.exe.config file

Question: On Win7 64bit, i need to edit the powershell_ise.exe.config file Located in C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe.config.
Even after a clean reboot and open the file in notepad++ with admin rights, i cannot save any changes.  "Save Filed Error" "check if this file is opened in another program."
Powershell v3/v4.
does anyone know why or the trick to edit it?
Comment: how can i improve this question.. it's very specific and seemingly straight forward. this file always seems locked and doesn't look like a way to edit it.   why am i getting down rated?
Comment: the reason this is needed is because prior to using POWERSHELL v5, you can't seem to add custom cmdlets for use in the ISE such that certain binding dependencies are DIFFERENT.   So in order to get the custom cmdlets to work , you need to edit this config file!!! and it doesn't seem possible to EDIT it.   man... such disrespect from ppl... Was this really needed to explain rather than getting down voted!
Answer: I think your question does not belong here, it is not related to development but file access and permissions. I'll make an answer anyway.
Permissions on the file do not allow modification, even for Administrators (that is what I see on my system).
You may need to take ownership over the file to change this.
I don't know if this change is recommended, I did not try it.
Comment: tx for the help... what seemed to work is a combination... no matter what i did i could not edit the file (even with the ISE not running).  BUT i renamed the file to something else... then i could edit the file.. .then i renamed it back.   (any further changes, still requires me to rename to change.. and rename it back).   All under an editor with admin rights.   Don't know if ownership made a difference in this case. didn't really go back to try it.. these sets should help others though.
Comment: i will give this as the combination as a solution
Answer: The powershell_ise.exe.config file is owned by TrustedInstaller.  You can use Nirsoft's AdvancedRun utility to open Notepad as TrustedInstaller and modify and save the file.
Opening Notepad.exe as TrustedInstaller with AdvancedRun screenshot


"Define is not defined" in Jest when testing es6 module with RequireJS dependency

Question: I have a Jest test suite that fails to run because the component it's trying to test depends on a RequireJS module. Here's the error I'm seeing:
 FAIL  __tests__/components/MyComponent.test.js
  ● Test suite failed to run

    ReferenceError: define is not defined

      at Object.<anonymous> (node_modules/private-npm-module/utils.js:1:90)

The component has the following import:
import utils from 'private-npm-module';

And the private-npm-module is set up like so:
define('utils', [], function() {
  return {};
});

When MyComponent is transpiled with babel and run in the browser, the dependency operates correctly. This issue only affects the unit test. How can I get my test suite to run on a component with a RequireJS dependency?
I'm using babel-jest as my scriptPreprocessor in package.json's jest config. I'm using jest v0.15.1.
Comment: There is (rejected) feature request for support of AMD JavaScript modules in Jest, https://github.com/facebook/jest/issues/17
Answer: So, RequireJS is not supported by Jest. In my particular case, it was easiest and most appropriate to mock my dependency at the top of MyComponent.test.js:
jest.mock('private-npm-module', () => {
  // mock implementation
})

import MyComponent from '../../components/MyComponent';

This way, when MyComponent is loaded, its dependency is already mocked, so it won't try to load the RequireJS module.
If you really do need to load your RequireJS module for your test, it may be possible to use jest's transform configuration to wrap your implementation in a RequireJS to ES6 converter.


Select unrelated ids on relate / lookup table

Question: I have seen various examples for this, but I can't find a definitive example of how to return all rows from table_A that are not related to a row in table_B using relate table_A_B without a given value for either table_A.id or table_B.id.  The closest matchs I can find to this are:

Get a list of A not related to one or more rows in B but without knowing which value in B is the issue.
Get a list of A not related to given B.
Get list of A with comma-separated field of unrelated B (not sure I saw that, but it seemed like one example could expand as such).
A list of As and Bs not related to each other but no indication of which is not related to the other.

I can also get a list of all potential A_B tuples with:
SELECT A.id, B.id  FROM A
INNER JOIN B ON A.id <> B.id

And I could hypothetically use one of the EXCEPT workarounds (I think) against the relate table, but all attempts were unsuccessful and I imagine that once there are millions of potential combinations returned by that join it will be much less efficient anyway.
So given the table values:
A
id | name
 1 | X
 2 | y
 3 | z

B
id | name
 7 | e
 8 | f
 9 | g

A_B
id | a_id | b_id
 1 |    1 |    7
 2 |    1 |    8
 3 |    1 |    9
 1 |    2 |    7
 2 |    2 |    8
 1 |    3 |    7

Is there a query that would return:
     A | B
     2 | 9
     3 | 8
     3 | 9
Or even better:
 A | B
 y | g
 z | f
 z | g

Or is this asking for trouble?
Answer: Start with a cross join between A and B to get all possible pairs.  Then do a left join to the relation table and choose where there is no match:
select driver.aID, driver.bID
from (select a.id as aID, b.id as bID
      from table_A A cross join table_B B
     ) driver left outer join
     table_A_B ab
     on ab.aID = driver.aID and ab.bID = bID
where ab.aID is null

This works, assuming that the ids are never NULL.
I haven't tested the SQL so it might have syntax errors.
This version gets you the names:
select driver.aName, driver.bName
from (select a.id as aID, b.id as bID, a.name as aName, b.name as Bname
      from table_A A cross join table_B B
     ) driver left outer join
     table_A_B ab
     on ab.aID = driver.aID and ab.bID = bID
where ab.aID is null

Comment: I tried using the `INNER JOIN A.id <> B.id` to get the full list of potentials but it was failing at every turn.  Glad I had the right idea logically and that you knew how to take it home. Thanks!


Altering Multiple Tables at once

Question: I'm trying to alter multiple SQL Server 2008 R2 tables at one time.
This is my code:
use DatabaseName
go

Declare @SchemaUsed varchar(20) = 'dbo'

create table #Tables
(
  TableName varchar(100), Processed int
)

insert into #Tables
  select top 1 table_name, 0
  from INFORMATION_SCHEMA.TABLES
  where TABLE_SCHEMA = @SchemaUsed
  and table_type = 'Base Table'
  and (TABLE_NAME like 'PM%' )
  ORDER BY TABLE_NAME

DECLARE @TableName varchar(max)
DECLARE @SQL varchar(max)

WHILE EXISTS (select top 1 'x' from #Tables where Processed = 0)
BEGIN
    SET @TableName = (select top 1 TableName from #Tables where Processed = 0)

  Set @SQL = 'ALTER TABLE ' + @SchemaUsed + '.' + @TableName + ' ADD [identityID]  bigint IDENTITY(1, 1) NOT NULL '
 -- Set @SQL =     '''' + @SQL + ''''
  Print @SQL
 EXEC  @SQL;

    update #Tables
    set Processed = 1
    where TableName = @TableName
END

drop table #Tables

I can't get this to work to save my life and get the following error:

Lookup Error - SQL Server Database Error: The name 'ALTER TABLE
  dbo.PM1GTVLV ADD [identityID]  bigint IDENTITY(1, 1) NOT NULL ' is not
  a valid identifier.

I've also tried multiple string variations and using sp_executesql as well.
Can someone point out where I've gone wrong?
Answer: Try
DECLARE @SQL NVARCHAR(MAX);

EXEC sp_executesql @SQL;

Instead of EXEC @sql.
As an aside, this is a much more usable version of the same code IMHO:
DECLARE @SchemaUsed VARCHAR(20) = 'dbo';

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += CHAR(13) + CHAR(10) + N'ALTER TABLE '
 + QUOTENAME(@SchemaUsed) + '.'
 + QUOTENAME(name) + ' ADD [identityID]
   BIGINT IDENTITY(1,1) NOT NULL;'
FROM sys.tables
WHERE SCHEMA_NAME([schema_id]) = @SchemaUsed
AND name LIKE 'PM%';

PRINT @sql;

--EXEC sp_executesql @sql;

Or even better:
DECLARE @SchemaUsed VARCHAR(20) = 'dbo';

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += CHAR(13) + CHAR(10) + N'ALTER TABLE '
 + QUOTENAME(@SchemaUsed) + '.'
 + QUOTENAME(name) + ' ADD [identityID]
   BIGINT IDENTITY(1,1) NOT NULL;'
FROM sys.tables AS t
WHERE SCHEMA_NAME([schema_id]) = @SchemaUsed
AND name LIKE 'PM%'
AND NOT EXISTS (SELECT 1 FROM sys.columns AS c
 WHERE [object_id] = t.[object_id]
 AND c.is_identity = 1);

PRINT @sql;

--EXEC sp_executesql @sql;

Comment: I tried the second one and it worked. I think I need to research QUOTENAME. Thanks Aaron, I owe you a drink when I see you again.
Comment: @David, from what I can tell, your error has nothing to do with `QUOTENAME`.  That said, I would agree that `sp_executesql` is often a better choice over a bare `exec`.
Comment: @Tim I was also having a hard time understanding the error - I can understand with `EXEC` but the question stated he also tried `sp_executesql`. The error there was probably about data type, not about invalid identifier, but I still prefer a version like this over cursor, temp table, etc.
Comment: Yes, I'd take a set over a loop, too. However, `exec` will work with a varchar, it's just that without the parentheses, it's trying to match that string to a stored procedure name (at least it appears that way to me).
Answer: To execute a character string, EXEC requires parenthesis around the string (or character variable) as shown in the BOL syntax:
EXEC (@SQL);

Altering Multiple Tables at once


How do you make 100 free throws in a row?

Question: One day during shooting I thought about how many consecutive free throws I could make. It ended in a bet (mainly with myself) that I could do 100 in a row by the end of the season. Working on that goal on and off for a few weeks, I realized that it is hard but still possible. I made 29 consecutive on day one and since then improved to 38. That does not seem like a big improvement, but the number of longer streaks increased a lot and it definitely helped improve my form and my overall shooting.
As I expected, it's more of a "mind game". The challenge seems to be to stay concentrated doing the repetitive task of shooting a free throw for at least fifteen minutes without losing focus. Every time my mind wanders for just a brief moment, chances are I miss the next shot.
My question is, how do pro athletes (from whatever sport) deal with problems like that? I watched the World Darts Championships recently. On that level many of the throws are easy (theoretically). The challenge is more to score consistently under pressure over the duration of a match. I guess the same is true in many other sports where precision is important. Once you mastered the technique, how do you fade out everything else and repeat that same move over and over again without losing focus and precision?
Comment: It's definitely about both. It's a basketball challenge, but I think it must be approached more generally. Some of the most important things I learned about basketball (as a player and coach) came from other fields of sports.
Comment: Surprised no one recommended "granny-style" (google Rick Barry free throws).  The mechanics of it are easiest to replicate over and over and over again, and are less susceptible to muscle fatigue.
Comment: I haven't worked at it in a while but there's an app I've been curious to try out in a while which might be useful for others too. It's called Homecourt and uses your phone's camera and some AI to provide feedback about your shooting angle. There's also an interesting YouTube video about consistency in free throw shooting which just came out today (featuring Steve Nash): https://youtu.be/BKIOqbx3sbU.
Answer: I can give some advice as to what I did to improve my free throws and how that may help in your quest for 100 consecutive.
Just to give you a measure, I left high school shooting 27% from the free throw line. Yes, you read correctly, 27%. I'm 6'5" and played center while in high school.
After being recruited, my future college coach told me that if I wanted to play my freshman year I needed to get that above 65%. By the time my freshman season started I was shooting 84% (from preseason averages) and would regularly shoot over 90% when shooting 100 FT at the end of practice.
My method:

Main thing I needed to change was to stop treating my free throw shot as "unique" from my other shooting attempts during the game. Instead, I shot my FTs just like I shoot my jumpers in a game. For me that was simply one dribble and shoot.
When practicing FTs I always made sure to do them after doing some other drill, be that running, shooting, dribbling, etc. That way you get used to shooting FTs when your legs are tired, arms are tired, mind is tired, or anything of the sort.
While practicing, every FT must be as if its the first meaning, don't make one FT and then just keep your legs stationary while someone passes you back the ball. Re-set every single time so that you get used to having to find your optimum set up.
Fix your form! This will not only help you shoot FTs but will also help your jumper. Add arc to your shot; that's probably the best thing most can do for their game - simply improve the chance the ball will go through the basket. The higher the arc, the larger the area the ball must be within in order to go through the hoop...physics.
Spread your fingers out as much as you can. I had a bad habit of keeping my fingers really close together which didn't give me much control of the ball. Spread fingers also force your hand to "flick" your wrist better in turn leading to better ball rotation on the shot.
Don't forget about the basics: bend legs, hands in cookie jar, etc.

So how will this help with consecutive FT makes? Muscle memory and stamina. I'm out of college now and haven't practice free throws in god knows how long yet I continue to shoot a high percentage whenever I play any sort of league. After reading your post (yesterday) I went to the gym and shot 100 FTs to see how I would fare: 92/100. I didn't keep track of how many in a row (I missed my first 3) but I'm fairly certain I was over 40 in a row before my last miss which ironically enough was at the hundredth FT (lack of focus I guess).
Comment: Rhythm still seems to be very important for me. Most of my misses happen after something broke my routine.
Comment: Thanks a lot. I like your point, that "EVERY FT must be as if its the first". Especially when I do jump shots and I have a rebounder, I tend to get into a "flow". I stop thinking and the movements become very automatic. This definitely helps raising my percentage, but eventually not thinking too much leads to not concentrating enough. For my free throw shooting this is not such a big problem, because I usually do it on my own, which means I get my own rebounds and reset for each shot.
Comment: There is one point I'm not so sure: I've always been an advocate for a high shooting arc. I've been shooting a high arc and I still teach it this way. However reading (and thinking) about the physics, it's not that clear: A higher arc means a larger target area, but it also means less control and a faster ball speed (and thus less margin for error, in case you hit the rim). It seems to be a bit of a trade-off.
Comment: @martin there is definitely an optimum there, the higher is not always better. On the other hand relatively flat-trajectory shots have a much higher chance of deflecting. You want the angle to be about 45°; see point #8 http://www.breakthroughbasketball.com/blog/index.php/21-basketball-shooting-tips/
Answer: There's a reason that Sports Psychology has grown into a pretty large niche of psychology overall. :)
Mastering one's mind will (in my opinion) always be a constant pursuit of mankind in general, and absolutely in sports.  Whether it's hitting 100 free throws in a row in basketball, driving the ball down the fairway every time in golf, or kicking the last-second field goal in American football, athletes will always have areas to improve because (presumably) once you get to the highest level of your sport your muscle memory is already set... all that's left is conquering your mind to make it not get in the way during the moments you need only your muscle memory.
Dr. John F. Murray is probably one of the more famous sports psychologists, and I'm betting he's always going to have work to do because of the complexity, training, and motivation that's involved.
Overall, I don't think there's a perfect answer to your question.  Different people react in different ways to pressure and ability to focus.  On top of that, psychologists apply different training methods to help people.  Ultimately, the people that will succeed are the people that come closest to mind mastery.
Answer: I'm afraid the answer is that it depends. If there were a specific answer, don't you think everyone would try to emulate it? ;)
I suggest looking at examples of what current athletes do to maintain focus. Here's one example about Dirk Nowitzki, currently a career 87.7 percent free throw shooter.
Also, it's worth pointing out that in basketball, no one has to shoot 100 free throws in a row since a player tends to get only one, two, or three shots in a "session" (of course there are exceptions but those are extremely rare). In that case, the challenge is probably remembering the routine from scratch.
Comment: I'm aware, that it's not really a game like situation. Of course it's a different challenge to maintain focus in for "just" one to three free throws (though under the pressure of a game situation). What I like about the whole idea, is to challenge the limits of my concentration span. I probably only chose freethrow shooting because that's something I'm already good (and persistent) at.
Comment: I'm not looking for THE answer. I'm looking for some ideas, which might help me find my own approach. The Nowitzki example is a pretty nice one. I haven't heard that before.
Comment: The [most upvoted answer](https://sports.stackexchange.com/a/2257) provides a way to achieve making 100 free throws in a row.
Answer: A moderately high arc is best. Too high an arc means the ball travels too far. It looks great when it swishes, but the extra distance turns a 15-foot shot into an 18-footer and can result in some real clunkers.
A routine helps to focus. I count silently to six. One, two during two dribbles. Three, four when gripping the ball. Five for knee bend down and six for knee bend up and release.
When gripping the ball, I like to position it so that the long black lines are horizontal and so that the tip of my middle finger is in the center of one of those lines, preferably on the side with no logo stuff.
I'm right-handed, so I position my right foot just left of the center spot on the free-throw line. This puts my shooting arm in a straight line to the center of the rim. A straight line means if you hit the rim short or long, the ball still has a good chance of going in.


Rational distance from an equilateral triangle

Question: Is there a nice proof for the following fact?

In a plane, there does not exist a square such that its vertices are at a rational distance from each vertex of some equilateral triangle.

What if we replace the square with a cyclic quadrilateral?
Well, it seems the following idea works for the first part of the question:
It is well known that there is no equilateral triangle in the Cartesian plane with rational coordinates. Let $(x,y)$ be at rational distance from the square with vertices, $(0,0), (0,a), (a,a), (a,0)$. Then it is easily seen that $2a(x-a)$ is rational. Hence if the coordinate system is scaled by a factor of $a$ and the origin is shifted to $(a,a)$, the coordinates become rational.
Answer: It turns out to be false for general cyclic quadrilateral. Let the triangle have unit side length. Take point $P$ on circumcircle so that $PA=\frac{3}{7}; PB=\frac{5}{7}$ (it would lie on a circle, thats easy to prove from cosine rule). By Pompieu theorem, $PC=\frac{8}{7}$. Now take points $Q,R,S$ with the same but permitted distances, and we have a quadrilateral satisfy yours conditions.


Which is bigger: $(\pi+1)^{\pi+1}$ or $\pi^{\pi+2}$?

Question: I've been struggling for a while with the following problem:
Which is bigger: $(\pi+1)^{\pi+1}$ or $\pi^{\pi+2}$?
Needless to say software aid is not allowed. All manual calculations should be possible to be performed in reasonable time (for example, I did read some solutions to similar problems that to my understanding include calculating 113th power of a decimal number, and this does not count as "reasonable" manual calculation).

Probably useless for the solution, but nevertheless interesting graphs of  $(x+1)^{x+1}-x^{x+2}$:

Using computer:
$(\pi+1)^{\pi+1} = 359.796$
$\pi^{\pi+2} = 359.867$
Comment: I really hope needles aren't actually needed to solve this.
Comment: this Q is exactly the same as the one you reference: $\left(1+\frac{1}{\pi}\right)^{\pi+1}<\pi \to \left(\frac{\pi+1}{\pi}\right)^{\pi+1}<\pi \to \frac{(\pi+1)^{\pi+1}}{(\pi)^{\pi+2}}<1$
Comment: Calculation verifies that the first is smaller by about $0.01\%$.
Comment: Correct, and such small difference makes it hard to prove with rational approximations of $\pi$, @Omnomnomnom
Comment: since the inequality is very sharp, I think you might need a very close approx for $\pi$
Comment: as the linked post suggested, $355/113$ maybe works
Comment: It'll work, but would include extremely lengthy manual calculations, @Yimin, or perhaps you convince me otherwise.
Comment: Needles and pins... as background music maybe :) @alex.jordan
Comment: I thought MSE saw this question in the recent past, no?
Comment: when does $(x+1)^{x+1}=x^{x+2}$?
Answer: In my attempts to solve the problem of the OP, I found it useful to choose a slightly broader perspective. I started by defining a function $f(x)$ in which an adjustable parameter $a$ appears:
$$f(x) = (x + a)log(x+a) - (x+2a)log(x)$$
In terms of parameter $a$, we seek the value of $x$ for which $f(x)$ equals zero. Later on we will focus on the case $a=1$.
There exist rational solutions for $x$ and $a$ whenever $x = n a$, where $n$ is a natural number. Here are the first four of these solutions:
$ \{n = 1, x=4, a=4 \}$; $ \{n = 2, x= 27/8, a = 27/16 \}$; $ \{n=3, x = 256/81, a=256/243 \}$; $ \{n=4, x=3125/1024, a = 3125/4096 \}$. Note that the solution for $n=3$ is close to the values occurring in the OP's problem: $x= \pi$, $a=1$.
We now seek the general solution for the case that $a << x$. Expanding the $ log(x+a)$ term in $f(x)$, dividing by $a$ and rearranging terms leads to:
$$log(x) = 1 + \frac {1}{2}(a/x) - \frac {1}{6}(a/x)^2 + \frac {1}{12}(a/x)^3 - \frac{1}{20}(a/x)^4 + \frac {1}{30}(a/x)^5 - \cdots$$
To get rid of the remaining logarithmic term, we substitute both sides of the equation in the exponential function. Next we divide by $e$. Then we take the reciprocal of both sides. This yields:
$$z = exp \{- \frac {1}{2}pz + \frac {1}{6}(pz)^2 - \frac {1}{12}(pz)^3 + \frac {1}{20}(pz)^4 - \frac {1}{30}(pz)^5 + \cdots \}$$
Where we have introduced the new variable $z = e/x$ and the new parameter $p = a/e$. Expanding the RHS in a Taylor series yields:
$$z = 1 - \frac {1}{2}(pz) + \frac {7}{24} (pz)^2 - \frac {3}{16} (pz)^3 + \frac {743}{5760}(pz)^4 - \frac {215}{2304}(pz)^5 + \cdots$$
From this result we obtain the power series for $z$ in terms of $p$:
$$z = 1 - \frac {1}{2}p + \frac {13}{24}p^2 - \frac {3}{4}p^3 + \frac {6763}{5760}p^4 - \frac {285}{144}p^5 + \cdots$$
Converting this result for $z$ into a series for $x$ gives:
$$x/e = 1 + \frac {1}{2}(a/e) - \frac {7}{24}(a/e)^2 + \frac {1}{3}(a/e)^3 - \frac {911}{1920}(a/e)^4 + \frac {34}{45}(a/e)^5 - \frac {748045}{580608}(a/e)^6 + \cdots$$
For small values of $(a/e)$ the series converges well, since consecutive terms alternate in sign while the pre-factors are roughly similar in magnitude. Unfortunately for the case $a=1$ we do not get the high accuracy that is required to answer the OP's question with certainty.
I chose a simple method to improve the convergence. As pointed out above, there is an rational solution to $x(a)$ quite near $a=1$, namely $x =256/81, a = 256/243$. We can simply add an extra term of order $(n+1)$ to an approximation of order $n$, in order to force the solution through this exact value. If I do this, the results are: $x0 = 3.1380$, $x1 = 3.1421$, $x2 = 3.1405$, $x3 = 3.1413$, $x4 = 3.1409$, $x5 = 3.1411$, $x6 = 3.1410$. From this set of values we can conclude that the zero of $f(x)$ occurs very near the value $x = 3.1410$. The  uncertainty is smaller than $0.0001$.
A different approach is to express the series in a Padé form. This is an elegant and effective method to improve convergence of a series. The $4$-parameter Padé representation for $x$ is:
$$x/e = \frac {1 + (A+B+C+D)(a/e) + (AC+AD+BD)(a/e)^2}{1 + (B+C+D)(a/e) +
BD(a/e)^2} $$
The parameter values are found to be: $A = 1/2$, $B = 7/12$, $C = 47/84$ , $D = 11313/19740$. To enhance the formula further, we adjust the least significant parameter (which is $D$) so that the formula satisfies the exact solution $x = 256/81$ when $a = 256/243$. The new value for $D$ is $0.48387$, which is somewhat smaller than the original value $0.57310$. The formula works very well for all $a$ in the range $0$ to $1.05$. For $a=1$ we obtain the result $x = 3.14105$ with an accuracy of $0.00001$.
In conclusion: for $a=1$ the solution to $f(x) = 0$ occurs for a value of $x$ which is smaller than $\pi = 3.14159$. Hence the function $f(x)$ is negative for $a=1$ and $x= \pi$.
Comment: I liked this answer very much. A lot of ideas, a lot of work, a lot of results. I am going to reward it with 100 points (current bounty). This is going to happen in 5 or 7 days, in accordance with site rules.
Answer: Unfortunately, needles are needed. This work is for $(\pi + 1)/\pi$, not $(\pi+2)/(\pi + 1)$, if you are going to make it more precise, third order approximation is needed.
I think needles are not necessary. $a = \pi$, We try to prove
$\ln(a + 1)/\ln(a) < (\frac{a+1}{a})$.
well, $a \sim 355/113\sim 3$, the error on estimate of $\pi$ is very tiny, will not harm our result, we do $a = 3 + b$, thus $b = 16/113\sim 1/7 \sim 0.14$
we just show it for
$\frac{\ln(4) + \ln(1+ b/4)}{\ln(3) + \ln(1 + b/3)}$
we use Taylor expansion on $\ln(1 + b/4)$ and $\ln(1 + b/3)$ to the first order term.
The following is estimate of error from truncation.
$$\vert\frac{u}{v} - \frac{u + s}{v + t}\vert \le \frac{|ut| + |vs|}{v(v+t)}$$
since $\frac{u}{v}$ is around $4/3 < 2$, $v$ is bounded below by $1$, we know that the error will be bounded by
$2(|t| + |s|) \le 2(b/3)^2 \le 1/100$.
$\ln(1 + b/4) \sim b/4 \sim 0.0354$
$\ln(1 + b/3) \sim b/3 \sim 0.0472$
$\ln(4) = 2\ln(2) \sim 1.386$
$\ln(3) \sim 1.099$
result is $\frac{1.386 + 0.0354}{1.099 + 0.0472} = 1.2400 < (\pi + 1)/(\pi) = 1.32$
By the way,
$\log(\pi + 1)/(\log(\pi)) \sim 1.2414$
Comment: The form of the question means that you should replace $\frac{\pi+1}{\pi}$ with $\frac{\pi+2}{\pi+1}$.  Otherwise you are proving $\pi^{\pi+1} > (\pi+1)^{\pi}$ which is much less sharp.
Comment: @RolfHoyer you are right, I just notice that, then using first order approx is far from enough, in this case, 3rd order is necessary.
Comment: What does "needles" refer to, in this instance?
Comment: @Charles see the comments which follows the question.
Comment: @Charles In original version of the question, I wrote "needles" instead of "needless", and this became a sort of internal joke.
Answer: Just to be clear on what it would take to decide this based on rational approximation: we want to compare the ratio
$$
f(x)=\frac{x^x}{(x-1)^{x+1}}
$$
to unity at $x=(\pi + 1)$.  Its logarithm is
$$
\begin{eqnarray}
g(x)=x\log x - (x+1)\log(x-1) &=& x\log x - (x+1)\log x - (x+1)\log(1-1/x) \\
&=& -\log x - (x+1)\log(1-1/x) \\
&=& -\log x + (x+1)\sum_{k=1}^{\infty}\frac{1}{k}x^{-k} \\
&=& -\log x + 1 +\sum_{k=1}^{\infty}\left(\frac{1}{k}+\frac{1}{k+1}\right)x^{-k},
\end{eqnarray}
$$
valid for $x>1$, the derivative of which is
$$
g'(x)=-\frac{1}{x}-\sum_{k=2}^{\infty}\left(1+\frac{k-1}{k}\right)x^{-k},
$$
which is clearly negative.  So $f(x)$ is decreasing, and is less than one at $x=(\pi + 1)$ if it less than one for some rational $(p/q) < \pi+1$.  A good rational underestimate for $\pi$ is given by $\frac{333}{106}\approx 3.14151 < \pi$.  Indeed, one finds numerically that $f(333/106 + 1) =f(439/106) < 1$ by a few percent:
$$
f\left(\frac{439}{106}\right)^{106}=\frac{(439/106)^{439}}{(333/106)^{545}}=\frac{439^{439} 106^{106}}{333^{545}} \approx 0.98.
$$
Despite being integers, though, the numbers involved are too large for manual calculation.  We can do better by looking at the second derivative of $f(x)$: it is $f''(x) = (g'(x)^2 + g''(x))f(x)$, which is clearly positive, since $g''(x)$ is.  Therefore $f(x)$ always lies below any line segment that connects two points on it.  So for any $x < y < (\pi+1) < z$, we have
$$
f(\pi + 1) < f(y) < \lambda f(x) + (1-\lambda) f(z),
$$
where $\lambda = (z-y)/(z-x)$.  This estimate is good enough for $(x,y,z)=\left(4,\frac{439}{106},\frac{29}{7}\right)$, for which $\lambda = 1/106$, $f(x)=256/243$, and $$f(z)=7\sqrt[7]{\frac{29^{29}}{22^{36}}}=\frac{7\cdot 29^{4}}{22^{5}}\sqrt[7]{\frac{29}{22}}.$$  Putting it together, we need to show that
$$
\frac{128}{12879} + \frac{105\cdot 7 \cdot 29^{4}}{106\cdot 22^{5}}\sqrt[7]{\frac{29}{22}} < 1,
$$
or
$$
\sqrt[7]{1+\frac{7}{22}} < \frac{12751\cdot 106 \cdot 22^5}{12879 \cdot 105 \cdot 7 \cdot 29^4}.
$$
Now, the root is less than its fifth-order Taylor series approximation:
$$
\sqrt[7]{1+7x} < 1 + x - 3x^2 + 13x^3 - 65x^4 + 351x^5 = \frac{5361157}{5153632},
$$
evaluated at $x=1/22$.  So in the end it will suffice to show that
$$
{5361157 \cdot 12879 \cdot 105 \cdot 7 \cdot 29^4} < {5153632 \cdot 12751\cdot 106 \cdot 22^5}.
$$
While these are certainly big numbers (about $20$ digits), they're much smaller than the thousand-digit monsters involved in the approach relying only on the first derivative, and I think you could argue that even if you didn't want to, you could carry out this calculation by hand to prove that $(\pi+1)^{\pi+1} < \pi^{\pi+2}$.
Comment: Thanks a lot, I would say this is the cleanest and the shortest solution, but the last step bothers me, I don't see the way to calculate it manually in reasonable time. :(
Comment: OK, you surprised me with the new addition to the answer, let me take a look...
Comment: I agree that establishing ${5361157 \cdot 12879 \cdot 105 \cdot 7 \cdot 29^4} < {5153632 \cdot 12751\cdot 106 \cdot 22^5}$ manually is reasonable.
Comment: Mind-boggling! Thanks again!
Answer: Consider the function f(x) =($\frac{x+1}{x}$)^(x+1);  the sign of the derivative on [3, 4] depends of the expression xln($\frac{x+1}{x})$ - 1 and both f '(3) and f '(4) are negative and not null on the interval. Hence f is decreasing over [3, 4]. We have f(3) = 3.160493827 > $\pi$  and f(4) = 3.051757813 < $\pi$ . We note that f(3) is nearer of $\pi$ than f(4); calculate f(3.14) = ($\frac{414}{314})^{4.14}$ = 3.141178 < $\pi$. Therefore f($\pi$) < $\pi$. Consequently the second expression is bigger.
Comment: ($\frac{414}{314})^{4.14}$ - how to calculate manually?
Comment: I thought he was allowed a single calculation at most; however an approach to $10^{-4}$ is enough to have the inequality less than $\pi$
Answer: Let $x=(\pi+1)^{\pi+1}$ and $y=\pi^{\pi+2}$
Since $\ln x=(\pi+1)ln(\pi+1)$ and $\ln y=(\pi+2)\ln\pi$ and $\ln x$ is increasing ,
compare $\ln x$ and $\ln y$
So I depend on a internet calculating, $\ln x-\ln y<0$ and so $x<y$
But $\ln x-\ln y=-0.00019...$ tell us that it is difficult to clear for manually.
(I tried to prove for " $f(x)=(x+1)\ln(x+1)-(x+2)\ln x$ is decreasing and $f(\pi)<0$"
But $f(3.14)>0$ ,according to calculating)